Skip to content

Commit adf1071

Browse files
committed
Support extended opcodes.
1 parent 24c2366 commit adf1071

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

src/processor/instructions.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum CmpOperator {
1818
}
1919

2020
impl CmpOperator {
21-
pub fn from_bytecode(n: u8) -> Self {
21+
pub fn from_bytecode(n: usize) -> Self {
2222
match n {
2323
0 => CmpOperator::Lt,
2424
1 => CmpOperator::Leq,
@@ -70,7 +70,7 @@ pub enum Instruction {
7070
StoreFast(usize),
7171
LoadGlobal(usize),
7272
CallFunction(usize, usize), // nb_args, nb_kwargs
73-
RaiseVarargs(u16),
73+
RaiseVarargs(usize),
7474
MakeFunction { has_defaults: bool, has_kwdefaults: bool, has_annotations: bool, has_closure: bool },
7575
BuildConstKeyMap(usize),
7676
}
@@ -124,11 +124,15 @@ impl<'a, I> Iterator for InstructionDecoder<I> where I: Iterator<Item=&'a u8> {
124124
self.pending_nops -= 1;
125125
return Some(Instruction::Nop)
126126
};
127-
let opcode = self.bytestream.next();
128-
let oparg = self.bytestream.next();
129-
if let (Some(opcode), Some(oparg)) = (opcode, oparg) {
130-
let opcode = *opcode;
131-
let oparg = *oparg;
127+
let mut opcode = 144;
128+
let mut oparg: usize = 0;
129+
while opcode == 144 {
130+
match self.bytestream.next() {
131+
Some(op) => { opcode = *op },
132+
None => return None,
133+
}
134+
oparg = (oparg << 8) | (*self.bytestream.next().unwrap() as usize);
135+
}
132136
let inst = match opcode {
133137
1 => Instruction::PopTop,
134138
4 => Instruction::DupTop,
@@ -139,40 +143,36 @@ impl<'a, I> Iterator for InstructionDecoder<I> where I: Iterator<Item=&'a u8> {
139143
87 => Instruction::PopBlock,
140144
88 => Instruction::EndFinally,
141145
89 => Instruction::PopExcept,
142-
90 => Instruction::StoreName(oparg as usize),
143-
93 => Instruction::ForIter(oparg as usize),
144-
95 => Instruction::StoreAttr(oparg as usize),
145-
97 => Instruction::StoreGlobal(oparg as usize),
146-
100 => Instruction::LoadConst(oparg as usize),
147-
101 => Instruction::LoadName(oparg as usize),
148-
102 => Instruction::BuildTuple(oparg as usize),
149-
106 => Instruction::LoadAttr(oparg as usize),
146+
90 => Instruction::StoreName(oparg),
147+
93 => Instruction::ForIter(oparg),
148+
95 => Instruction::StoreAttr(oparg),
149+
97 => Instruction::StoreGlobal(oparg),
150+
100 => Instruction::LoadConst(oparg),
151+
101 => Instruction::LoadName(oparg),
152+
102 => Instruction::BuildTuple(oparg),
153+
106 => Instruction::LoadAttr(oparg),
150154
107 => Instruction::CompareOp(CmpOperator::from_bytecode(oparg)),
151-
110 => Instruction::JumpForward(oparg as usize),
152-
113 => Instruction::JumpAbsolute(oparg as usize),
153-
114 => Instruction::PopJumpIfFalse(oparg as usize),
154-
116 => Instruction::LoadGlobal(oparg as usize),
155-
120 => Instruction::SetupLoop(oparg as usize + 1),
156-
121 => Instruction::SetupExcept(oparg as usize + 1),
157-
124 => Instruction::LoadFast(oparg as usize),
158-
125 => Instruction::StoreFast(oparg as usize),
159-
130 => Instruction::RaiseVarargs(self.read_argument() as u16),
160-
131 => Instruction::CallFunction(oparg as usize, 0),
155+
110 => Instruction::JumpForward(oparg),
156+
113 => Instruction::JumpAbsolute(oparg),
157+
114 => Instruction::PopJumpIfFalse(oparg),
158+
116 => Instruction::LoadGlobal(oparg),
159+
120 => Instruction::SetupLoop(oparg + 1),
160+
121 => Instruction::SetupExcept(oparg + 1),
161+
124 => Instruction::LoadFast(oparg),
162+
125 => Instruction::StoreFast(oparg),
163+
130 => Instruction::RaiseVarargs(oparg),
164+
131 => Instruction::CallFunction(oparg, 0),
161165
132 => Instruction::MakeFunction {
162166
has_defaults: oparg & 0x01 != 0,
163167
has_kwdefaults: oparg & 0x02 != 0,
164168
has_annotations: oparg & 0x04 != 0,
165169
has_closure: oparg & 0x08 != 0,
166170
},
167-
156 => Instruction::BuildConstKeyMap(oparg as usize),
171+
156 => Instruction::BuildConstKeyMap(oparg),
168172
144 => { self.arg_prefix = Some(self.read_argument()); Instruction::Nop },
169173
_ => panic!(format!("Opcode not supported: {:?}", (opcode, oparg))),
170174
};
171175
Some(inst)
172-
}
173-
else {
174-
None
175-
}
176176
}
177177
}
178178

src/processor/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ fn run_code<EP: EnvProxy>(state: &mut State<EP>, call_stack: &mut Vec<Frame>) ->
490490
let obj = state.store.deref(&pop_stack!(state, frame.var_stack));
491491
match obj.content {
492492
ObjectContent::True => (),
493-
ObjectContent::False => frame.program_counter = target,
493+
ObjectContent::False => frame.program_counter = target / WORD_SIZE,
494494
_ => unimplemented!(),
495495
}
496496
}

0 commit comments

Comments
 (0)