@@ -18,7 +18,7 @@ pub enum CmpOperator {
18
18
}
19
19
20
20
impl CmpOperator {
21
- pub fn from_bytecode ( n : u32 ) -> Self {
21
+ pub fn from_bytecode ( n : u8 ) -> Self {
22
22
match n {
23
23
0 => CmpOperator :: Lt ,
24
24
1 => CmpOperator :: Leq ,
@@ -71,7 +71,8 @@ pub enum Instruction {
71
71
LoadGlobal ( usize ) ,
72
72
CallFunction ( usize , usize ) , // nb_args, nb_kwargs
73
73
RaiseVarargs ( u16 ) ,
74
- MakeFunction ( usize , usize , usize ) , // nb_default_args, nb_default_kwargs, nb_annot
74
+ MakeFunction ( bool , bool , bool , bool ) , // has_defaults, has_kwdefaults, has_annotations, has_closure
75
+ BuildConstKeyMap ( usize ) ,
75
76
}
76
77
77
78
#[ derive( Debug ) ]
@@ -123,8 +124,12 @@ impl<'a, I> Iterator for InstructionDecoder<I> where I: Iterator<Item=&'a u8> {
123
124
self . pending_nops -= 1 ;
124
125
return Some ( Instruction :: Nop )
125
126
} ;
126
- self . bytestream . next ( ) . map ( |opcode| {
127
- match * opcode {
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;
132
+ let inst = match opcode {
128
133
1 => Instruction :: PopTop ,
129
134
4 => Instruction :: DupTop ,
130
135
25 => Instruction :: BinarySubscr ,
@@ -134,37 +139,35 @@ impl<'a, I> Iterator for InstructionDecoder<I> where I: Iterator<Item=&'a u8> {
134
139
87 => Instruction :: PopBlock ,
135
140
88 => Instruction :: EndFinally ,
136
141
89 => Instruction :: PopExcept ,
137
- 90 => Instruction :: StoreName ( self . read_argument ( ) as usize ) ,
138
- 93 => Instruction :: ForIter ( self . read_argument ( ) as usize ) ,
139
- 95 => Instruction :: StoreAttr ( self . read_argument ( ) as usize ) ,
140
- 97 => Instruction :: StoreGlobal ( self . read_argument ( ) as usize ) ,
141
- 100 => Instruction :: LoadConst ( self . read_argument ( ) as usize ) ,
142
- 101 => Instruction :: LoadName ( self . read_argument ( ) as usize ) ,
143
- 102 => Instruction :: BuildTuple ( self . read_argument ( ) as usize ) ,
144
- 106 => Instruction :: LoadAttr ( self . read_argument ( ) as usize ) ,
145
- 107 => Instruction :: CompareOp ( CmpOperator :: from_bytecode ( self . read_argument ( ) ) ) ,
146
- 110 => Instruction :: JumpForward ( self . read_argument ( ) as usize + 2 ) , // +2, because JumpForward takes 3 bytes, and the relative address is computed from the next instruction.
147
- 113 => Instruction :: JumpAbsolute ( self . read_argument ( ) as usize ) ,
148
- 114 => Instruction :: PopJumpIfFalse ( self . read_argument ( ) as usize ) ,
149
- 116 => Instruction :: LoadGlobal ( self . read_argument ( ) as usize ) ,
150
- 120 => Instruction :: SetupLoop ( self . read_argument ( ) as usize + 2 ) ,
151
- 121 => Instruction :: SetupExcept ( self . read_argument ( ) as usize + 2 ) ,
152
- 124 => Instruction :: LoadFast ( self . read_argument ( ) as usize ) ,
153
- 125 => Instruction :: StoreFast ( self . read_argument ( ) as usize ) ,
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 ) ,
150
+ 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 ) ,
154
159
130 => Instruction :: RaiseVarargs ( self . read_argument ( ) as u16 ) ,
155
- 131 => Instruction :: CallFunction ( self . read_byte ( ) as usize , self . read_byte ( ) as usize ) ,
156
- 132 => {
157
- let arg = self . read_argument ( ) ;
158
- let nb_pos = arg & 0xFF ;
159
- let nb_kw = ( arg >> 8 ) & 0xFF ;
160
- //let nb_annot = (arg >> 16) & 0x7FF; // TODO
161
- let nb_annot = 0 ;
162
- Instruction :: MakeFunction ( nb_pos as usize , nb_kw as usize , nb_annot as usize )
163
- } ,
160
+ 131 => Instruction :: CallFunction ( oparg as usize , 0 ) ,
161
+ 132 => Instruction :: MakeFunction ( oparg & 0x01 != 0 , oparg & 0x02 != 0 , oparg & 0x04 != 0 , oparg & 0x08 != 0 ) ,
162
+ 156 => Instruction :: BuildConstKeyMap ( oparg as usize ) ,
164
163
144 => { self . arg_prefix = Some ( self . read_argument ( ) ) ; Instruction :: Nop } ,
165
- _ => panic ! ( format!( "Opcode not supported: {}" , opcode) ) ,
166
- }
167
- } )
164
+ _ => panic ! ( format!( "Opcode not supported: {:?}" , ( opcode, oparg) ) ) ,
165
+ } ;
166
+ Some ( inst)
167
+ }
168
+ else {
169
+ None
170
+ }
168
171
}
169
172
}
170
173
0 commit comments