Make Instruction only 8 bytes

This commit is contained in:
Noah committed 2020-12-14 15:55:47 -06:00
1 parent e87144e2c5
commit 1be79c93b0
5 files changed
+193 -176

No files matched your search

+58 -46
View File
@@ -143,7 +143,7 @@ impl CodeFlags {
#[repr(transparent)]
// XXX: if you add a new instruction that stores a Label, make sure to add it in
// Instruction::label_arg{,_mut}
pub struct Label(pub usize);
pub struct Label(pub u32);
impl fmt::Display for Label {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
@@ -153,6 +153,8 @@ impl fmt::Display for Label {
/// Transforms a value prior to formatting it.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ConversionFlag {
/// No conversion
None,
/// Converts by calling `str(<value>)`.
Str,
/// Converts by calling `ascii(<value>)`.
@@ -161,7 +163,14 @@ pub enum ConversionFlag {
Repr,
}
pub type NameIdx = usize;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum RaiseKind {
Reraise,
Raise,
RaiseCause,
}
pub type NameIdx = u32;
/// A Single bytecode instruction.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -199,7 +208,7 @@ pub enum Instruction {
},
LoadConst {
/// index into constants vec
idx: usize,
idx: u32,
},
UnaryOperation {
op: UnaryOperator,
@@ -218,7 +227,7 @@ pub enum Instruction {
},
Pop,
Rotate {
amount: usize,
amount: u32,
},
Duplicate,
GetIter,
@@ -249,10 +258,10 @@ pub enum Instruction {
},
MakeFunction(MakeFunctionFlags),
CallFunctionPositional {
nargs: usize,
nargs: u32,
},
CallFunctionKeyword {
nargs: usize,
nargs: u32,
},
CallFunctionEx {
has_kwargs: bool,
@@ -297,56 +306,57 @@ pub enum Instruction {
WithCleanupFinish,
PopBlock,
Raise {
argc: usize,
kind: RaiseKind,
},
BuildString {
size: usize,
size: u32,
},
BuildTuple {
size: usize,
unpack: bool,
size: u32,
},
BuildList {
size: usize,
unpack: bool,
size: u32,
},
BuildSet {
size: usize,
unpack: bool,
size: u32,
},
BuildMap {
size: usize,
unpack: bool,
for_call: bool,
size: u32,
},
BuildSlice {
size: usize,
/// whether build a slice with a third step argument
step: bool,
},
ListAppend {
i: usize,
i: u32,
},
SetAdd {
i: usize,
i: u32,
},
MapAdd {
i: usize,
i: u32,
},
PrintExpr,
LoadBuildClass,
UnpackSequence {
size: usize,
size: u32,
},
UnpackEx {
before: u8,
after: u8,
},
FormatValue {
conversion: Option<ConversionFlag>,
conversion: ConversionFlag,
},
PopException,
Reverse {
amount: usize,
amount: u32,
},
GetAwaitable,
BeforeAsyncWith,
@@ -360,7 +370,7 @@ pub enum Instruction {
/// required to support named expressions of Python 3.8 in dict comprehension
/// today (including Py3.9) only required in dict comprehension.
MapAddRev {
i: usize,
i: u32,
},
}
@@ -612,7 +622,7 @@ impl<C: Constant> CodeObject<C> {
let label_targets = self.label_targets();
for (offset, instruction) in self.instructions.iter().enumerate() {
let arrow = if label_targets.contains(&Label(offset)) {
let arrow = if label_targets.contains(&Label(offset as u32)) {
">>"
} else {
" "
@@ -819,39 +829,41 @@ impl Instruction {
};
}
let cellname = |i: usize| {
let varname = |i: u32| varnames[i as usize].as_ref();
let name = |i: u32| names[i as usize].as_ref();
let cellname = |i: u32| {
cellvars
.get(i)
.unwrap_or_else(|| &freevars[i - cellvars.len()])
.get(i as usize)
.unwrap_or_else(|| &freevars[i as usize - cellvars.len()])
.as_ref()
};
match self {
ImportName { idx } => w!(ImportName, names[*idx].as_ref()),
ImportName { idx } => w!(ImportName, name(*idx)),
ImportNameless => w!(ImportNameless),
ImportStar => w!(ImportStar),
ImportFrom { idx } => w!(ImportFrom, names[*idx].as_ref()),
LoadFast(idx) => w!(LoadFast, *idx, varnames[*idx].as_ref()),
LoadNameAny(idx) => w!(LoadNameAny, *idx, names[*idx].as_ref()),
LoadGlobal(idx) => w!(LoadGlobal, *idx, names[*idx].as_ref()),
ImportFrom { idx } => w!(ImportFrom, name(*idx)),
LoadFast(idx) => w!(LoadFast, *idx, varname(*idx)),
LoadNameAny(idx) => w!(LoadNameAny, *idx, name(*idx)),
LoadGlobal(idx) => w!(LoadGlobal, *idx, name(*idx)),
LoadDeref(idx) => w!(LoadDeref, *idx, cellname(*idx)),
LoadClassDeref(idx) => w!(LoadClassDeref, *idx, cellname(*idx)),
StoreFast(idx) => w!(StoreFast, *idx, varnames[*idx].as_ref()),
StoreLocal(idx) => w!(StoreLocal, *idx, names[*idx].as_ref()),
StoreGlobal(idx) => w!(StoreGlobal, *idx, names[*idx].as_ref()),
StoreFast(idx) => w!(StoreFast, *idx, varname(*idx)),
StoreLocal(idx) => w!(StoreLocal, *idx, name(*idx)),
StoreGlobal(idx) => w!(StoreGlobal, *idx, name(*idx)),
StoreDeref(idx) => w!(StoreDeref, *idx, cellname(*idx)),
DeleteFast(idx) => w!(DeleteFast, *idx, varnames[*idx].as_ref()),
DeleteLocal(idx) => w!(DeleteLocal, *idx, names[*idx].as_ref()),
DeleteGlobal(idx) => w!(DeleteGlobal, *idx, names[*idx].as_ref()),
DeleteFast(idx) => w!(DeleteFast, *idx, varname(*idx)),
DeleteLocal(idx) => w!(DeleteLocal, *idx, name(*idx)),
DeleteGlobal(idx) => w!(DeleteGlobal, *idx, name(*idx)),
DeleteDeref(idx) => w!(DeleteDeref, *idx, cellname(*idx)),
LoadClosure(i) => w!(LoadClosure, *i, cellname(*i)),
Subscript => w!(Subscript),
StoreSubscript => w!(StoreSubscript),
DeleteSubscript => w!(DeleteSubscript),
StoreAttr { idx } => w!(StoreAttr, names[*idx].as_ref()),
DeleteAttr { idx } => w!(DeleteAttr, names[*idx].as_ref()),
StoreAttr { idx } => w!(StoreAttr, name(*idx)),
DeleteAttr { idx } => w!(DeleteAttr, name(*idx)),
LoadConst { idx } => {
let value = &constants[*idx];
let value = &constants[*idx as usize];
match value.borrow_constant() {
BorrowedConstant::Code { code } if expand_codeobjects => {
writeln!(f, "{:20} ({:?}):", "LoadConst", code)?;
@@ -865,13 +877,13 @@ impl Instruction {
}
}
}
UnaryOperation { op } => w!(UnaryOperation, format!("{:?}", op)),
BinaryOperation { op } => w!(BinaryOperation, format!("{:?}", op)),
UnaryOperation { op } => w!(UnaryOperation, format_args!("{:?}", op)),
BinaryOperation { op } => w!(BinaryOperation, format_args!("{:?}", op)),
BinaryOperationInplace { op } => {
w!(BinaryOperationInplace, format!("{:?}", op))
w!(BinaryOperationInplace, format_args!("{:?}", op))
}
LoadAttr { idx } => w!(LoadAttr, names[*idx].as_ref()),
CompareOperation { op } => w!(CompareOperation, format!("{:?}", op)),
LoadAttr { idx } => w!(LoadAttr, name(*idx)),
CompareOperation { op } => w!(CompareOperation, format_args!("{:?}", op)),
Pop => w!(Pop),
Rotate { amount } => w!(Rotate, amount),
Duplicate => w!(Duplicate),
@@ -903,7 +915,7 @@ impl Instruction {
BeforeAsyncWith => w!(BeforeAsyncWith),
SetupAsyncWith { end } => w!(SetupAsyncWith, end),
PopBlock => w!(PopBlock),
Raise { argc } => w!(Raise, argc),
Raise { kind } => w!(Raise, format_args!("{:?}", kind)),
BuildString { size } => w!(BuildString, size),
BuildTuple { size, unpack } => w!(BuildTuple, size, unpack),
BuildList { size, unpack } => w!(BuildList, size, unpack),
@@ -913,7 +925,7 @@ impl Instruction {
unpack,
for_call,
} => w!(BuildMap, size, unpack, for_call),
BuildSlice { size } => w!(BuildSlice, size),
BuildSlice { step } => w!(BuildSlice, step),
ListAppend { i } => w!(ListAppend, i),
SetAdd { i } => w!(SetAdd, i),
MapAddRev { i } => w!(MapAddRev, i),
@@ -921,7 +933,7 @@ impl Instruction {
LoadBuildClass => w!(LoadBuildClass),
UnpackSequence { size } => w!(UnpackSequence, size),
UnpackEx { before, after } => w!(UnpackEx, before, after),
FormatValue { .. } => w!(FormatValue), // TODO: write conversion
FormatValue { conversion } => w!(FormatValue, format_args!("{:?}", conversion)),
PopException => w!(PopException),
Reverse { amount } => w!(Reverse, amount),
GetAwaitable => w!(GetAwaitable),