Make bytecode::Label its own struct

This commit is contained in:
Noah 2019-08-27 21:18:09 +00:00
parent e37f117035
commit e0a52c15f6
3 changed files with 36 additions and 20 deletions

View file

@ -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<CodeObjectStream>;
@ -134,8 +134,6 @@ impl std::fmt::Display for ModeParseError {
}
}
pub(crate) type Label = usize;
impl<O> Default for Compiler<O>
where
O: OutputStream,
@ -2004,7 +2002,7 @@ impl<O: OutputStream> Compiler<O> {
// 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

View file

@ -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<CodeObject> + Into<CodeObject> {
/// Output an instruction

View file

@ -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)
}