Skip to content

Commit 1be79c9

Browse files
committed
Make Instruction only 8 bytes
1 parent e87144e commit 1be79c9

File tree

5 files changed

+193
-176
lines changed

5 files changed

+193
-176
lines changed

bytecode/src/bytecode.rs

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl CodeFlags {
143143
#[repr(transparent)]
144144
// XXX: if you add a new instruction that stores a Label, make sure to add it in
145145
// Instruction::label_arg{,_mut}
146-
pub struct Label(pub usize);
146+
pub struct Label(pub u32);
147147
impl fmt::Display for Label {
148148
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
149149
self.0.fmt(f)
@@ -153,6 +153,8 @@ impl fmt::Display for Label {
153153
/// Transforms a value prior to formatting it.
154154
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
155155
pub enum ConversionFlag {
156+
/// No conversion
157+
None,
156158
/// Converts by calling `str(<value>)`.
157159
Str,
158160
/// Converts by calling `ascii(<value>)`.
@@ -161,7 +163,14 @@ pub enum ConversionFlag {
161163
Repr,
162164
}
163165

164-
pub type NameIdx = usize;
166+
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
167+
pub enum RaiseKind {
168+
Reraise,
169+
Raise,
170+
RaiseCause,
171+
}
172+
173+
pub type NameIdx = u32;
165174

166175
/// A Single bytecode instruction.
167176
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -199,7 +208,7 @@ pub enum Instruction {
199208
},
200209
LoadConst {
201210
/// index into constants vec
202-
idx: usize,
211+
idx: u32,
203212
},
204213
UnaryOperation {
205214
op: UnaryOperator,
@@ -218,7 +227,7 @@ pub enum Instruction {
218227
},
219228
Pop,
220229
Rotate {
221-
amount: usize,
230+
amount: u32,
222231
},
223232
Duplicate,
224233
GetIter,
@@ -249,10 +258,10 @@ pub enum Instruction {
249258
},
250259
MakeFunction(MakeFunctionFlags),
251260
CallFunctionPositional {
252-
nargs: usize,
261+
nargs: u32,
253262
},
254263
CallFunctionKeyword {
255-
nargs: usize,
264+
nargs: u32,
256265
},
257266
CallFunctionEx {
258267
has_kwargs: bool,
@@ -297,56 +306,57 @@ pub enum Instruction {
297306
WithCleanupFinish,
298307
PopBlock,
299308
Raise {
300-
argc: usize,
309+
kind: RaiseKind,
301310
},
302311
BuildString {
303-
size: usize,
312+
size: u32,
304313
},
305314
BuildTuple {
306-
size: usize,
307315
unpack: bool,
316+
size: u32,
308317
},
309318
BuildList {
310-
size: usize,
311319
unpack: bool,
320+
size: u32,
312321
},
313322
BuildSet {
314-
size: usize,
315323
unpack: bool,
324+
size: u32,
316325
},
317326
BuildMap {
318-
size: usize,
319327
unpack: bool,
320328
for_call: bool,
329+
size: u32,
321330
},
322331
BuildSlice {
323-
size: usize,
332+
/// whether build a slice with a third step argument
333+
step: bool,
324334
},
325335
ListAppend {
326-
i: usize,
336+
i: u32,
327337
},
328338
SetAdd {
329-
i: usize,
339+
i: u32,
330340
},
331341
MapAdd {
332-
i: usize,
342+
i: u32,
333343
},
334344

335345
PrintExpr,
336346
LoadBuildClass,
337347
UnpackSequence {
338-
size: usize,
348+
size: u32,
339349
},
340350
UnpackEx {
341351
before: u8,
342352
after: u8,
343353
},
344354
FormatValue {
345-
conversion: Option<ConversionFlag>,
355+
conversion: ConversionFlag,
346356
},
347357
PopException,
348358
Reverse {
349-
amount: usize,
359+
amount: u32,
350360
},
351361
GetAwaitable,
352362
BeforeAsyncWith,
@@ -360,7 +370,7 @@ pub enum Instruction {
360370
/// required to support named expressions of Python 3.8 in dict comprehension
361371
/// today (including Py3.9) only required in dict comprehension.
362372
MapAddRev {
363-
i: usize,
373+
i: u32,
364374
},
365375
}
366376

@@ -612,7 +622,7 @@ impl<C: Constant> CodeObject<C> {
612622
let label_targets = self.label_targets();
613623

614624
for (offset, instruction) in self.instructions.iter().enumerate() {
615-
let arrow = if label_targets.contains(&Label(offset)) {
625+
let arrow = if label_targets.contains(&Label(offset as u32)) {
616626
">>"
617627
} else {
618628
" "
@@ -819,39 +829,41 @@ impl Instruction {
819829
};
820830
}
821831

822-
let cellname = |i: usize| {
832+
let varname = |i: u32| varnames[i as usize].as_ref();
833+
let name = |i: u32| names[i as usize].as_ref();
834+
let cellname = |i: u32| {
823835
cellvars
824-
.get(i)
825-
.unwrap_or_else(|| &freevars[i - cellvars.len()])
836+
.get(i as usize)
837+
.unwrap_or_else(|| &freevars[i as usize - cellvars.len()])
826838
.as_ref()
827839
};
828840

829841
match self {
830-
ImportName { idx } => w!(ImportName, names[*idx].as_ref()),
842+
ImportName { idx } => w!(ImportName, name(*idx)),
831843
ImportNameless => w!(ImportNameless),
832844
ImportStar => w!(ImportStar),
833-
ImportFrom { idx } => w!(ImportFrom, names[*idx].as_ref()),
834-
LoadFast(idx) => w!(LoadFast, *idx, varnames[*idx].as_ref()),
835-
LoadNameAny(idx) => w!(LoadNameAny, *idx, names[*idx].as_ref()),
836-
LoadGlobal(idx) => w!(LoadGlobal, *idx, names[*idx].as_ref()),
845+
ImportFrom { idx } => w!(ImportFrom, name(*idx)),
846+
LoadFast(idx) => w!(LoadFast, *idx, varname(*idx)),
847+
LoadNameAny(idx) => w!(LoadNameAny, *idx, name(*idx)),
848+
LoadGlobal(idx) => w!(LoadGlobal, *idx, name(*idx)),
837849
LoadDeref(idx) => w!(LoadDeref, *idx, cellname(*idx)),
838850
LoadClassDeref(idx) => w!(LoadClassDeref, *idx, cellname(*idx)),
839-
StoreFast(idx) => w!(StoreFast, *idx, varnames[*idx].as_ref()),
840-
StoreLocal(idx) => w!(StoreLocal, *idx, names[*idx].as_ref()),
841-
StoreGlobal(idx) => w!(StoreGlobal, *idx, names[*idx].as_ref()),
851+
StoreFast(idx) => w!(StoreFast, *idx, varname(*idx)),
852+
StoreLocal(idx) => w!(StoreLocal, *idx, name(*idx)),
853+
StoreGlobal(idx) => w!(StoreGlobal, *idx, name(*idx)),
842854
StoreDeref(idx) => w!(StoreDeref, *idx, cellname(*idx)),
843-
DeleteFast(idx) => w!(DeleteFast, *idx, varnames[*idx].as_ref()),
844-
DeleteLocal(idx) => w!(DeleteLocal, *idx, names[*idx].as_ref()),
845-
DeleteGlobal(idx) => w!(DeleteGlobal, *idx, names[*idx].as_ref()),
855+
DeleteFast(idx) => w!(DeleteFast, *idx, varname(*idx)),
856+
DeleteLocal(idx) => w!(DeleteLocal, *idx, name(*idx)),
857+
DeleteGlobal(idx) => w!(DeleteGlobal, *idx, name(*idx)),
846858
DeleteDeref(idx) => w!(DeleteDeref, *idx, cellname(*idx)),
847859
LoadClosure(i) => w!(LoadClosure, *i, cellname(*i)),
848860
Subscript => w!(Subscript),
849861
StoreSubscript => w!(StoreSubscript),
850862
DeleteSubscript => w!(DeleteSubscript),
851-
StoreAttr { idx } => w!(StoreAttr, names[*idx].as_ref()),
852-
DeleteAttr { idx } => w!(DeleteAttr, names[*idx].as_ref()),
863+
StoreAttr { idx } => w!(StoreAttr, name(*idx)),
864+
DeleteAttr { idx } => w!(DeleteAttr, name(*idx)),
853865
LoadConst { idx } => {
854-
let value = &constants[*idx];
866+
let value = &constants[*idx as usize];
855867
match value.borrow_constant() {
856868
BorrowedConstant::Code { code } if expand_codeobjects => {
857869
writeln!(f, "{:20} ({:?}):", "LoadConst", code)?;
@@ -865,13 +877,13 @@ impl Instruction {
865877
}
866878
}
867879
}
868-
UnaryOperation { op } => w!(UnaryOperation, format!("{:?}", op)),
869-
BinaryOperation { op } => w!(BinaryOperation, format!("{:?}", op)),
880+
UnaryOperation { op } => w!(UnaryOperation, format_args!("{:?}", op)),
881+
BinaryOperation { op } => w!(BinaryOperation, format_args!("{:?}", op)),
870882
BinaryOperationInplace { op } => {
871-
w!(BinaryOperationInplace, format!("{:?}", op))
883+
w!(BinaryOperationInplace, format_args!("{:?}", op))
872884
}
873-
LoadAttr { idx } => w!(LoadAttr, names[*idx].as_ref()),
874-
CompareOperation { op } => w!(CompareOperation, format!("{:?}", op)),
885+
LoadAttr { idx } => w!(LoadAttr, name(*idx)),
886+
CompareOperation { op } => w!(CompareOperation, format_args!("{:?}", op)),
875887
Pop => w!(Pop),
876888
Rotate { amount } => w!(Rotate, amount),
877889
Duplicate => w!(Duplicate),
@@ -903,7 +915,7 @@ impl Instruction {
903915
BeforeAsyncWith => w!(BeforeAsyncWith),
904916
SetupAsyncWith { end } => w!(SetupAsyncWith, end),
905917
PopBlock => w!(PopBlock),
906-
Raise { argc } => w!(Raise, argc),
918+
Raise { kind } => w!(Raise, format_args!("{:?}", kind)),
907919
BuildString { size } => w!(BuildString, size),
908920
BuildTuple { size, unpack } => w!(BuildTuple, size, unpack),
909921
BuildList { size, unpack } => w!(BuildList, size, unpack),
@@ -913,15 +925,15 @@ impl Instruction {
913925
unpack,
914926
for_call,
915927
} => w!(BuildMap, size, unpack, for_call),
916-
BuildSlice { size } => w!(BuildSlice, size),
928+
BuildSlice { step } => w!(BuildSlice, step),
917929
ListAppend { i } => w!(ListAppend, i),
918930
SetAdd { i } => w!(SetAdd, i),
919931
MapAddRev { i } => w!(MapAddRev, i),
920932
PrintExpr => w!(PrintExpr),
921933
LoadBuildClass => w!(LoadBuildClass),
922934
UnpackSequence { size } => w!(UnpackSequence, size),
923935
UnpackEx { before, after } => w!(UnpackEx, before, after),
924-
FormatValue { .. } => w!(FormatValue), // TODO: write conversion
936+
FormatValue { conversion } => w!(FormatValue, format_args!("{:?}", conversion)),
925937
PopException => w!(PopException),
926938
Reverse { amount } => w!(Reverse, amount),
927939
GetAwaitable => w!(GetAwaitable),

0 commit comments

Comments
 (0)