Skip to content

Commit cfeb746

Browse files
authored
Merge pull request RustPython#1320 from RustPython/coolreader18/label-struct
Make bytecode::Label its own struct
2 parents 0d2a7fc + c523286 commit cfeb746

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

bytecode/src/bytecode.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ bitflags! {
5656
}
5757
}
5858

59-
pub type Label = usize;
59+
#[derive(Serialize, Debug, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
60+
pub struct Label(usize);
61+
62+
impl Label {
63+
pub fn new(label: usize) -> Self {
64+
Label(label)
65+
}
66+
}
6067

6168
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6269
/// An indication where the name must be accessed.
@@ -499,8 +506,8 @@ impl Instruction {
499506
SetupFinally { handler } => w!(SetupFinally, label_map[handler]),
500507
EnterFinally => w!(EnterFinally),
501508
EndFinally => w!(EndFinally),
502-
SetupWith { end } => w!(SetupWith, end),
503-
CleanupWith { end } => w!(CleanupWith, end),
509+
SetupWith { end } => w!(SetupWith, label_map[end]),
510+
CleanupWith { end } => w!(CleanupWith, label_map[end]),
504511
PopBlock => w!(PopBlock),
505512
Raise { argc } => w!(Raise, argc),
506513
BuildString { size } => w!(BuildString, size),

compiler/src/compile.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::symboltable::{
1212
make_symbol_table, statements_to_symbol_table, Symbol, SymbolScope, SymbolTable,
1313
};
1414
use num_complex::Complex64;
15-
use rustpython_bytecode::bytecode::{self, CallType, CodeObject, Instruction, Varargs};
15+
use rustpython_bytecode::bytecode::{self, CallType, CodeObject, Instruction, Label, Varargs};
1616
use rustpython_parser::{ast, parser};
1717

1818
type BasicOutputStream = PeepholeOptimizer<CodeObjectStream>;
@@ -134,8 +134,6 @@ impl std::fmt::Display for ModeParseError {
134134
}
135135
}
136136

137-
pub(crate) type Label = usize;
138-
139137
impl<O> Default for Compiler<O>
140138
where
141139
O: OutputStream,
@@ -2004,7 +2002,7 @@ impl<O: OutputStream> Compiler<O> {
20042002

20052003
// Generate a new label
20062004
fn new_label(&mut self) -> Label {
2007-
let l = self.nxt_label;
2005+
let l = Label::new(self.nxt_label);
20082006
self.nxt_label += 1;
20092007
l
20102008
}
@@ -2093,9 +2091,9 @@ fn compile_conversion_flag(conversion_flag: ast::ConversionFlag) -> bytecode::Co
20932091
mod tests {
20942092
use super::Compiler;
20952093
use crate::symboltable::make_symbol_table;
2096-
use rustpython_bytecode::bytecode::CodeObject;
20972094
use rustpython_bytecode::bytecode::Constant::*;
20982095
use rustpython_bytecode::bytecode::Instruction::*;
2096+
use rustpython_bytecode::bytecode::{CodeObject, Label};
20992097
use rustpython_parser::parser;
21002098

21012099
fn compile_exec(source: &str) -> CodeObject {
@@ -2116,15 +2114,21 @@ mod tests {
21162114
LoadConst {
21172115
value: Boolean { value: true }
21182116
},
2119-
JumpIfTrue { target: 1 },
2117+
JumpIfTrue {
2118+
target: Label::new(1)
2119+
},
21202120
LoadConst {
21212121
value: Boolean { value: false }
21222122
},
2123-
JumpIfTrue { target: 1 },
2123+
JumpIfTrue {
2124+
target: Label::new(1)
2125+
},
21242126
LoadConst {
21252127
value: Boolean { value: false }
21262128
},
2127-
JumpIfFalse { target: 0 },
2129+
JumpIfFalse {
2130+
target: Label::new(0)
2131+
},
21282132
Pass,
21292133
LoadConst { value: None },
21302134
ReturnValue
@@ -2141,15 +2145,21 @@ mod tests {
21412145
LoadConst {
21422146
value: Boolean { value: true }
21432147
},
2144-
JumpIfFalse { target: 0 },
2148+
JumpIfFalse {
2149+
target: Label::new(0)
2150+
},
21452151
LoadConst {
21462152
value: Boolean { value: false }
21472153
},
2148-
JumpIfFalse { target: 0 },
2154+
JumpIfFalse {
2155+
target: Label::new(0)
2156+
},
21492157
LoadConst {
21502158
value: Boolean { value: false }
21512159
},
2152-
JumpIfFalse { target: 0 },
2160+
JumpIfFalse {
2161+
target: Label::new(0)
2162+
},
21532163
Pass,
21542164
LoadConst { value: None },
21552165
ReturnValue
@@ -2166,19 +2176,27 @@ mod tests {
21662176
LoadConst {
21672177
value: Boolean { value: true }
21682178
},
2169-
JumpIfFalse { target: 2 },
2179+
JumpIfFalse {
2180+
target: Label::new(2)
2181+
},
21702182
LoadConst {
21712183
value: Boolean { value: false }
21722184
},
2173-
JumpIfTrue { target: 1 },
2185+
JumpIfTrue {
2186+
target: Label::new(1)
2187+
},
21742188
LoadConst {
21752189
value: Boolean { value: false }
21762190
},
2177-
JumpIfFalse { target: 0 },
2191+
JumpIfFalse {
2192+
target: Label::new(0)
2193+
},
21782194
LoadConst {
21792195
value: Boolean { value: true }
21802196
},
2181-
JumpIfFalse { target: 0 },
2197+
JumpIfFalse {
2198+
target: Label::new(0)
2199+
},
21822200
Pass,
21832201
LoadConst { value: None },
21842202
ReturnValue

compiler/src/output_stream.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::compile::Label;
2-
use rustpython_bytecode::bytecode::{CodeObject, Instruction, Location};
1+
use rustpython_bytecode::bytecode::{CodeObject, Instruction, Label, Location};
32

43
pub trait OutputStream: From<CodeObject> + Into<CodeObject> {
54
/// Output an instruction

compiler/src/peephole.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::compile::Label;
21
use crate::output_stream::OutputStream;
32
use arrayvec::ArrayVec;
4-
use rustpython_bytecode::bytecode::{self, CodeObject, Instruction, Location};
3+
use rustpython_bytecode::bytecode::{self, CodeObject, Instruction, Label, Location};
54

65
const PEEPHOLE_BUFFER_SIZE: usize = 20;
76

@@ -92,7 +91,7 @@ where
9291
self.push(instruction, loc.into());
9392
optimize(self);
9493
}
95-
fn set_label(&mut self, label: crate::compile::Label) {
94+
fn set_label(&mut self, label: Label) {
9695
if let Some(instr) = self.buffer.last_mut() {
9796
instr.1.labels.push(label)
9897
}

0 commit comments

Comments
 (0)