@@ -28,21 +28,24 @@ impl Converter {
2828 }
2929
3030 pub fn convert ( & mut self , code : PyCodeObject ) {
31- self . code = Some ( code) ;
32- for op_code in self . code . unwrap ( ) . co_code {
33- self . dispatch ( op_code) ;
31+ // self.code = Some(code);
32+ for op_code in & code. co_code {
33+ self . dispatch ( & code , op_code) ;
3434 }
3535 }
3636
37- fn dispatch ( & mut self , op_code : ( usize , String , Option < usize > ) ) {
37+ fn dispatch ( & mut self , code : & PyCodeObject , op_code : & ( usize , String , Option < usize > ) ) {
3838 debug ! ( "Converting op code: {:?}" , op_code) ;
3939 match ( op_code. 1 . as_ref ( ) , op_code. 2 ) {
4040 ( "LOAD_CONST" , Some ( consti) ) => {
4141 // println!("Loading const at index: {}", consti);
42- let value = match self . code . unwrap ( ) . co_consts [ consti] {
42+ let cons = & code. co_consts [ consti] ;
43+ let value = match cons {
4344 // NativeType::Boolean { value } => { bytecode::Constant::Boolean { true } },
44- // NativeType::Int { value } => { bytecode::Constant::Integer { value: value } },
45- _ => { panic ! ( "Not impl " ) ; }
45+ NativeType :: Int { 0 : value } => { bytecode:: Constant :: Integer { value : * value } } ,
46+ NativeType :: Str { 0 : ref value } => { bytecode:: Constant :: String { value : value. clone ( ) } }
47+ NativeType :: NoneType => { bytecode:: Constant :: None }
48+ _ => { panic ! ( "Not impl {:?}" , cons) ; }
4649 } ;
4750 self . emit ( bytecode:: Instruction :: LoadConst { value : value } ) ;
4851 } ,
@@ -56,12 +59,12 @@ impl Converter {
5659 } ,
5760 ( "STORE_NAME" , Some ( namei) ) => {
5861 // println!("Loading const at index: {}", consti);
59- let name = self . code . unwrap ( ) . co_names [ namei] . clone ( ) ;
62+ let name = code. co_names [ namei] . clone ( ) ;
6063 self . emit ( bytecode:: Instruction :: StoreName { name } ) ;
6164 } ,
6265 ( "LOAD_NAME" , Some ( namei) ) => {
6366 // println!("Loading const at index: {}", consti);
64- let name = self . code . unwrap ( ) . co_names [ namei] . clone ( ) ;
67+ let name = code. co_names [ namei] . clone ( ) ;
6568 self . emit ( bytecode:: Instruction :: LoadName { name } ) ;
6669 } ,
6770 ( "LOAD_GLOBAL" , Some ( namei) ) => {
@@ -76,12 +79,10 @@ impl Converter {
7679 self . emit ( bytecode:: Instruction :: BuildList { size : count } ) ;
7780 } ,
7881
79- /*
8082 ( "BUILD_SLICE" , Some ( count) ) => {
81- let curr_frame = self.curr_frame();
8283 assert ! ( count == 2 || count == 3 ) ;
84+ self . emit ( bytecode:: Instruction :: BuildSlice { size : count } ) ;
8385 } ,
84- */
8586
8687 ( "GET_ITER" , None ) => {
8788 self . emit ( bytecode:: Instruction :: GetIter ) ;
@@ -91,28 +92,28 @@ impl Converter {
9192 self . emit ( bytecode:: Instruction :: ForIter ) ;
9293 } ,
9394
94- /*
9595 ( "COMPARE_OP" , Some ( cmp_op_i) ) => {
96- let curr_frame = self.curr_frame();
96+ let op = match cmp_op_i {
97+ 0 => bytecode:: ComparisonOperator :: Less ,
98+ 1 => bytecode:: ComparisonOperator :: LessOrEqual ,
99+ 2 => bytecode:: ComparisonOperator :: Equal ,
100+ 3 => bytecode:: ComparisonOperator :: NotEqual ,
101+ 4 => bytecode:: ComparisonOperator :: Greater ,
102+ 5 => bytecode:: ComparisonOperator :: GreaterOrEqual ,
103+ _ => { panic ! ( "Not impl {:?}" , cmp_op_i) ; }
104+ } ;
105+ self . emit ( bytecode:: Instruction :: CompareOperation { op : op} ) ;
97106 } ,
98107 ( "POP_JUMP_IF_TRUE" , Some ( ref target) ) => {
99- let curr_frame = self.curr_frame();
100- let v = curr_frame.stack.pop().unwrap();
101- if *v == NativeType::Boolean(true) {
102- curr_frame.lasti = curr_frame.labels.get(target).unwrap().clone();
103- }
104- None
105-
108+ self . emit ( bytecode:: Instruction :: JumpIf { target : * target} ) ;
106109 }
110+ /*
107111 ("POP_JUMP_IF_FALSE", Some(ref target)) => {
108- let curr_frame = self.curr_frame();
109- let v = curr_frame.stack.pop().unwrap();
110- if *v == NativeType::Boolean(false) {
111- curr_frame.lasti = curr_frame.labels.get(target).unwrap().clone();
112- }
113- None
114-
115- }
112+ // Convert into two internal bytecodes:
113+ self.emit(bytecode::Instruction::UnaryOperation { op: bytecode::UnaryOperation::Not } );
114+ self.emit(bytecode::Instruction::JumpIf { target: target} );
115+ }*/
116+ /*
116117 ("JUMP_FORWARD", Some(ref delta)) => {
117118 let curr_frame = self.curr_frame();
118119 let last_offset = curr_frame.get_bytecode_offset().unwrap();
@@ -129,16 +130,11 @@ impl Converter {
129130 self.unwind("break".to_string());
130131 None //?
131132 },
133+ */
132134 ( "RAISE_VARARGS" , Some ( argc) ) => {
133- let curr_frame = self.curr_frame();
134- // let (exception, params, traceback) = match argc {
135- let exception = match argc {
136- 1 => curr_frame.stack.pop().unwrap(),
137- 0 | 2 | 3 => panic!("Not implemented!"),
138- _ => panic!("Invalid paramter for RAISE_VARARGS, must be between 0 to 3")
139- };
140- panic!("{:?}", exception);
135+ self . emit ( bytecode:: Instruction :: Raise { argc : argc } ) ;
141136 }
137+ /*
142138 ("INPLACE_ADD", None) => {
143139 self.emit(bytecode::Instruction::BinaryOperation { op: BinaryOperator::Add });
144140 },
@@ -179,12 +175,12 @@ impl Converter {
179175 ( "BINARY_SUBTRACT" , None ) => {
180176 self . emit ( bytecode:: Instruction :: BinaryOperation { op : bytecode:: BinaryOperator :: Subtract } ) ;
181177 } ,
182-
183- /*
178+ ( "BINARY_SUBSCR" , None ) => {
179+ self . emit ( bytecode:: Instruction :: BinaryOperation { op : bytecode:: BinaryOperator :: Subscript } ) ;
180+ } ,
184181 ( "ROT_TWO" , None ) => {
185- // TODO: self.emit(Instruction::BinaryOperation { op: bytecode::BinaryOperator::RotTwo });
182+ self . emit ( bytecode :: Instruction :: Rotate { amount : 2 } ) ;
186183 }
187- */
188184 ( "UNARY_NEGATIVE" , None ) => {
189185 self . emit ( bytecode:: Instruction :: UnaryOperation { op : bytecode:: UnaryOperator :: Minus } ) ;
190186 } ,
@@ -210,14 +206,10 @@ impl Converter {
210206 let pos_count = ( argc & 0xFF ) as usize ;
211207 self . emit ( bytecode:: Instruction :: CallFunction { count : pos_count } ) ;
212208 } ,
213- /*
214209 ( "RETURN_VALUE" , None ) => {
215- // Hmmm... what is this used?
216- // I believe we need to push this to the next frame
217- self.curr_frame().return_value = (*self.curr_frame().stack.pop().unwrap()).clone();
218- Some("return".to_string())
219- self.emit(bytecode::Instruction::CallFunction { });
210+ self . emit ( bytecode:: Instruction :: ReturnValue ) ;
220211 } ,
212+ /*
221213 ("SETUP_LOOP", Some(delta)) => {
222214 let curr_frame = self.curr_frame();
223215 let curr_offset = curr_frame.get_bytecode_offset().unwrap();
0 commit comments