11use cranelift:: prelude:: * ;
22use num_traits:: cast:: ToPrimitive ;
33use rustpython_bytecode:: bytecode:: {
4- BinaryOperator , CodeObject , ComparisonOperator , Constant , Instruction , Label , NameScope ,
5- UnaryOperator ,
4+ self , BinaryOperator , BorrowedConstant , CodeObject , ComparisonOperator , Instruction , Label ,
5+ NameScope , UnaryOperator ,
66} ;
77use std:: collections:: HashMap ;
88
@@ -105,7 +105,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
105105 . or_insert_with ( || builder. create_block ( ) )
106106 }
107107
108- pub fn compile ( & mut self , bytecode : & CodeObject ) -> Result < ( ) , JitCompileError > {
108+ pub fn compile < C : bytecode:: Constant > (
109+ & mut self ,
110+ bytecode : & CodeObject < C > ,
111+ ) -> Result < ( ) , JitCompileError > {
109112 let offset_to_label: HashMap < & usize , & Label > =
110113 bytecode. label_map . iter ( ) . map ( |( k, v) | ( v, k) ) . collect ( ) ;
111114
@@ -128,15 +131,18 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
128131 continue ;
129132 }
130133
131- self . add_instruction ( & instruction) ?;
134+ self . add_instruction ( & instruction, & bytecode . constants ) ?;
132135 }
133136
134137 Ok ( ( ) )
135138 }
136139
137- fn load_const ( & mut self , constant : & Constant ) -> Result < ( ) , JitCompileError > {
140+ fn load_const < C : bytecode:: Constant > (
141+ & mut self ,
142+ constant : BorrowedConstant < C > ,
143+ ) -> Result < ( ) , JitCompileError > {
138144 match constant {
139- Constant :: Integer { value } => {
145+ BorrowedConstant :: Integer { value } => {
140146 let val = self . builder . ins ( ) . iconst (
141147 types:: I64 ,
142148 value. to_i64 ( ) . ok_or ( JitCompileError :: NotSupported ) ?,
@@ -147,16 +153,16 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
147153 } ) ;
148154 Ok ( ( ) )
149155 }
150- Constant :: Float { value } => {
151- let val = self . builder . ins ( ) . f64const ( * value) ;
156+ BorrowedConstant :: Float { value } => {
157+ let val = self . builder . ins ( ) . f64const ( value) ;
152158 self . stack . push ( JitValue {
153159 val,
154160 ty : JitType :: Float ,
155161 } ) ;
156162 Ok ( ( ) )
157163 }
158- Constant :: Boolean { value } => {
159- let val = self . builder . ins ( ) . iconst ( types:: I8 , * value as i64 ) ;
164+ BorrowedConstant :: Boolean { value } => {
165+ let val = self . builder . ins ( ) . iconst ( types:: I8 , value as i64 ) ;
160166 self . stack . push ( JitValue {
161167 val,
162168 ty : JitType :: Bool ,
@@ -167,7 +173,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
167173 }
168174 }
169175
170- fn add_instruction ( & mut self , instruction : & Instruction ) -> Result < ( ) , JitCompileError > {
176+ pub fn add_instruction < C : bytecode:: Constant > (
177+ & mut self ,
178+ instruction : & Instruction ,
179+ constants : & [ C ] ,
180+ ) -> Result < ( ) , JitCompileError > {
171181 match instruction {
172182 Instruction :: JumpIfFalse { target } => {
173183 let cond = self . stack . pop ( ) . ok_or ( JitCompileError :: BadBytecode ) ?;
@@ -222,7 +232,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
222232 let val = self . stack . pop ( ) . ok_or ( JitCompileError :: BadBytecode ) ?;
223233 self . store_variable ( name. clone ( ) , val)
224234 }
225- Instruction :: LoadConst { value } => self . load_const ( value ) ,
235+ Instruction :: LoadConst { idx } => self . load_const ( constants [ * idx ] . borrow_constant ( ) ) ,
226236 Instruction :: ReturnValue => {
227237 let val = self . stack . pop ( ) . ok_or ( JitCompileError :: BadBytecode ) ?;
228238 if let Some ( ref ty) = self . sig . ret {
0 commit comments