diff --git a/src/compile.rs b/src/compile.rs index 40a901e..40173db 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -12,7 +12,7 @@ use crate::symboltable::{ make_symbol_table, statements_to_symbol_table, Symbol, SymbolScope, SymbolTable, }; use num_complex::Complex64; -use rustpython_bytecode::bytecode::{self, CallType, CodeObject, Instruction, Varargs}; +use rustpython_bytecode::bytecode::{self, CallType, CodeObject, Instruction, Label, Varargs}; use rustpython_parser::{ast, parser}; type BasicOutputStream = PeepholeOptimizer; @@ -134,8 +134,6 @@ impl std::fmt::Display for ModeParseError { } } -pub(crate) type Label = usize; - impl Default for Compiler where O: OutputStream, @@ -2004,7 +2002,7 @@ impl Compiler { // Generate a new label fn new_label(&mut self) -> Label { - let l = self.nxt_label; + let l = Label::new(self.nxt_label); self.nxt_label += 1; l } @@ -2093,9 +2091,9 @@ fn compile_conversion_flag(conversion_flag: ast::ConversionFlag) -> bytecode::Co mod tests { use super::Compiler; use crate::symboltable::make_symbol_table; - use rustpython_bytecode::bytecode::CodeObject; use rustpython_bytecode::bytecode::Constant::*; use rustpython_bytecode::bytecode::Instruction::*; + use rustpython_bytecode::bytecode::{CodeObject, Label}; use rustpython_parser::parser; fn compile_exec(source: &str) -> CodeObject { @@ -2116,15 +2114,21 @@ mod tests { LoadConst { value: Boolean { value: true } }, - JumpIfTrue { target: 1 }, + JumpIfTrue { + target: Label::new(1) + }, LoadConst { value: Boolean { value: false } }, - JumpIfTrue { target: 1 }, + JumpIfTrue { + target: Label::new(1) + }, LoadConst { value: Boolean { value: false } }, - JumpIfFalse { target: 0 }, + JumpIfFalse { + target: Label::new(0) + }, Pass, LoadConst { value: None }, ReturnValue @@ -2141,15 +2145,21 @@ mod tests { LoadConst { value: Boolean { value: true } }, - JumpIfFalse { target: 0 }, + JumpIfFalse { + target: Label::new(0) + }, LoadConst { value: Boolean { value: false } }, - JumpIfFalse { target: 0 }, + JumpIfFalse { + target: Label::new(0) + }, LoadConst { value: Boolean { value: false } }, - JumpIfFalse { target: 0 }, + JumpIfFalse { + target: Label::new(0) + }, Pass, LoadConst { value: None }, ReturnValue @@ -2166,19 +2176,27 @@ mod tests { LoadConst { value: Boolean { value: true } }, - JumpIfFalse { target: 2 }, + JumpIfFalse { + target: Label::new(2) + }, LoadConst { value: Boolean { value: false } }, - JumpIfTrue { target: 1 }, + JumpIfTrue { + target: Label::new(1) + }, LoadConst { value: Boolean { value: false } }, - JumpIfFalse { target: 0 }, + JumpIfFalse { + target: Label::new(0) + }, LoadConst { value: Boolean { value: true } }, - JumpIfFalse { target: 0 }, + JumpIfFalse { + target: Label::new(0) + }, Pass, LoadConst { value: None }, ReturnValue diff --git a/src/output_stream.rs b/src/output_stream.rs index 35321dc..9d0ece0 100644 --- a/src/output_stream.rs +++ b/src/output_stream.rs @@ -1,5 +1,4 @@ -use crate::compile::Label; -use rustpython_bytecode::bytecode::{CodeObject, Instruction, Location}; +use rustpython_bytecode::bytecode::{CodeObject, Instruction, Label, Location}; pub trait OutputStream: From + Into { /// Output an instruction diff --git a/src/peephole.rs b/src/peephole.rs index 64c06b8..c67a16a 100644 --- a/src/peephole.rs +++ b/src/peephole.rs @@ -1,7 +1,6 @@ -use crate::compile::Label; use crate::output_stream::OutputStream; use arrayvec::ArrayVec; -use rustpython_bytecode::bytecode::{self, CodeObject, Instruction, Location}; +use rustpython_bytecode::bytecode::{self, CodeObject, Instruction, Label, Location}; const PEEPHOLE_BUFFER_SIZE: usize = 20; @@ -92,7 +91,7 @@ where self.push(instruction, loc.into()); optimize(self); } - fn set_label(&mut self, label: crate::compile::Label) { + fn set_label(&mut self, label: Label) { if let Some(instr) = self.buffer.last_mut() { instr.1.labels.push(label) }