-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstructions.rs
More file actions
188 lines (178 loc) · 5.9 KB
/
Copy pathinstructions.rs
File metadata and controls
188 lines (178 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use super::super::objects::ObjectRef;
#[derive(PartialEq)]
#[derive(Debug)]
#[derive(Clone)]
pub enum CmpOperator {
Lt, // Lower than
Leq, // Lower or equal
Eq, // Equal
Neq, // Not equal
Gt, // Greater than
Geq, // Greater or equal
In,
NotIn,
Is,
IsNot,
ExceptionMatch,
}
impl CmpOperator {
pub fn from_bytecode(n: usize) -> Self {
match n {
0 => CmpOperator::Lt,
1 => CmpOperator::Leq,
2 => CmpOperator::Eq,
3 => CmpOperator::Neq,
4 => CmpOperator::Gt,
5 => CmpOperator::Geq,
6 => CmpOperator::In,
7 => CmpOperator::NotIn,
8 => CmpOperator::Is,
9 => CmpOperator::IsNot,
10=> CmpOperator::ExceptionMatch,
_ => panic!("Invalid cmp operator code.")
}
}
}
#[derive(PartialEq)]
#[derive(Debug)]
#[derive(Clone)]
pub enum Instruction {
PushImmediate(ObjectRef),
PopTop,
DupTop,
Nop,
BinarySubscr,
GetIter,
LoadBuildClass,
ReturnValue,
PopBlock,
EndFinally,
PopExcept,
StoreName(usize),
ForIter(usize),
StoreAttr(usize),
StoreGlobal(usize),
LoadConst(usize),
LoadName(usize),
BuildTuple(usize),
LoadAttr(usize),
SetupLoop(usize),
SetupExcept(usize),
CompareOp(CmpOperator),
JumpForward(usize),
JumpAbsolute(usize),
PopJumpIfFalse(usize),
LoadFast(usize),
StoreFast(usize),
LoadGlobal(usize),
CallFunction(usize, bool), // nb_args + nb_kwargs, has_kwargs
RaiseVarargs(usize),
MakeFunction { has_defaults: bool, has_kwdefaults: bool, has_annotations: bool, has_closure: bool },
BuildConstKeyMap(usize),
}
#[derive(Debug)]
pub struct InstructionDecoder<I> where I: Iterator {
bytestream: I,
arg_prefix: Option<u32>,
pending_nops: u8, // Number of NOPs to be inserted after this instruction to match CPython's addresses (instructions have different sizes)
}
impl<I> InstructionDecoder<I> where I: Iterator {
pub fn new(bytes: I) -> InstructionDecoder<I> {
InstructionDecoder { bytestream: bytes, pending_nops: 0, arg_prefix: None, }
}
}
impl<'a, I> InstructionDecoder<I> where I: Iterator<Item=&'a u8> {
fn read_byte(&mut self) -> u8 {
match self.bytestream.next() {
Some(b) => {
self.pending_nops += 1;
*b
},
_ => panic!("End of stream in the middle of an instruction."),
}
}
fn read_argument(&mut self) -> u32 {
match (self.bytestream.next(), self.bytestream.next()) {
(Some(b1), Some(b2)) => {
self.pending_nops += 2;
let arg = ((*b2 as u32) << 8) + (*b1 as u32);
if let Some(prefix) = self.arg_prefix {
self.arg_prefix = None;
(prefix << 16) + arg
}
else {
arg
}
},
_ => panic!("End of stream in the middle of an instruction."),
}
}
}
impl<'a, I> Iterator for InstructionDecoder<I> where I: Iterator<Item=&'a u8> {
type Item = Instruction;
fn next(&mut self) -> Option<Instruction> {
if self.pending_nops != 0 {
self.pending_nops -= 1;
return Some(Instruction::Nop)
};
let mut opcode = 144;
let mut oparg: usize = 0;
while opcode == 144 {
match self.bytestream.next() {
Some(op) => { opcode = *op },
None => return None,
}
oparg = (oparg << 8) | (*self.bytestream.next().unwrap() as usize);
self.pending_nops += 1;
}
self.pending_nops -= 1;
let inst = match opcode {
1 => Instruction::PopTop,
4 => Instruction::DupTop,
25 => Instruction::BinarySubscr,
68 => Instruction::GetIter,
71 => Instruction::LoadBuildClass,
83 => Instruction::ReturnValue,
87 => Instruction::PopBlock,
88 => Instruction::EndFinally,
89 => Instruction::PopExcept,
90 => Instruction::StoreName(oparg),
93 => Instruction::ForIter(oparg),
95 => Instruction::StoreAttr(oparg),
97 => Instruction::StoreGlobal(oparg),
100 => Instruction::LoadConst(oparg),
101 => Instruction::LoadName(oparg),
102 => Instruction::BuildTuple(oparg),
106 => Instruction::LoadAttr(oparg),
107 => Instruction::CompareOp(CmpOperator::from_bytecode(oparg)),
110 => Instruction::JumpForward(oparg),
113 => Instruction::JumpAbsolute(oparg),
114 => Instruction::PopJumpIfFalse(oparg),
116 => Instruction::LoadGlobal(oparg),
120 => Instruction::SetupLoop(oparg + 1),
121 => Instruction::SetupExcept(oparg + 1),
124 => Instruction::LoadFast(oparg),
125 => Instruction::StoreFast(oparg),
130 => Instruction::RaiseVarargs(oparg),
131 => Instruction::CallFunction(oparg, false),
132 => Instruction::MakeFunction {
has_defaults: oparg & 0x01 != 0,
has_kwdefaults: oparg & 0x02 != 0,
has_annotations: oparg & 0x04 != 0,
has_closure: oparg & 0x08 != 0,
},
141 => Instruction::CallFunction(oparg, true),
156 => Instruction::BuildConstKeyMap(oparg),
144 => panic!("The impossible happened."),
_ => panic!(format!("Opcode not supported: {:?}", (opcode, oparg))),
};
Some(inst)
}
}
#[test]
fn test_load_read() {
let bytes: Vec<u8> = vec![124, 1, 83, 0];
let reader = InstructionDecoder::new(bytes.iter());
let instructions: Vec<Instruction> = reader.collect();
assert_eq!(vec![Instruction::LoadFast(1), Instruction::ReturnValue], instructions);
}