forked from RustPython/RustPython
/
instructions.rs
337 lines (312 loc) · 12.8 KB
/
instructions.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use cranelift::prelude::*;
use num_traits::cast::ToPrimitive;
use rustpython_bytecode::bytecode::{
UnaryOperator, BinaryOperator, CodeObject, ComparisonOperator, Constant, Instruction, Label, NameScope,
};
use std::collections::HashMap;
use super::{JitCompileError, JitSig, JitType};
#[derive(Clone)]
struct Local {
var: Variable,
ty: JitType,
}
struct JitValue {
val: Value,
ty: JitType,
}
impl JitValue {
fn new(val: Value, ty: JitType) -> JitValue {
JitValue { val, ty }
}
}
pub struct FunctionCompiler<'a, 'b> {
builder: &'a mut FunctionBuilder<'b>,
stack: Vec<JitValue>,
variables: HashMap<String, Local>,
label_to_block: HashMap<Label, Block>,
pub(crate) sig: JitSig,
}
impl<'a, 'b> FunctionCompiler<'a, 'b> {
pub fn new(
builder: &'a mut FunctionBuilder<'b>,
arg_names: &[String],
arg_types: &[JitType],
entry_block: Block,
) -> FunctionCompiler<'a, 'b> {
let mut compiler = FunctionCompiler {
builder,
stack: Vec::new(),
variables: HashMap::new(),
label_to_block: HashMap::new(),
sig: JitSig {
args: arg_types.to_vec(),
ret: None,
},
};
let params = compiler.builder.func.dfg.block_params(entry_block).to_vec();
debug_assert_eq!(arg_names.len(), arg_types.len());
debug_assert_eq!(arg_names.len(), params.len());
for ((name, ty), val) in arg_names.iter().zip(arg_types).zip(params) {
compiler
.store_variable(name.clone(), JitValue::new(val, ty.clone()))
.unwrap();
}
compiler
}
fn store_variable(&mut self, name: String, val: JitValue) -> Result<(), JitCompileError> {
let len = self.variables.len();
let builder = &mut self.builder;
let local = self.variables.entry(name).or_insert_with(|| {
let var = Variable::new(len);
let local = Local {
var,
ty: val.ty.clone(),
};
builder.declare_var(var, val.ty.to_cranelift());
local
});
if val.ty != local.ty {
Err(JitCompileError::NotSupported)
} else {
self.builder.def_var(local.var, val.val);
Ok(())
}
}
fn boolean_val(&mut self, val: JitValue) -> Result<Value, JitCompileError> {
match val.ty {
JitType::Float => Err(JitCompileError::NotSupported),
JitType::Int => Ok(val.val),
}
}
pub fn compile(&mut self, bytecode: &CodeObject) -> Result<(), JitCompileError> {
let offset_to_label: HashMap<&usize, &Label> =
bytecode.label_map.iter().map(|(k, v)| (v, k)).collect();
for (offset, instruction) in bytecode.instructions.iter().enumerate() {
if let Some(&label) = offset_to_label.get(&offset) {
let builder = &mut self.builder;
let block = self
.label_to_block
.entry(*label)
.or_insert_with(|| builder.create_block());
// If the current block is not terminated/filled just jump
// into the new block.
if !self.builder.is_filled() {
self.builder.ins().jump(*block, &[]);
}
self.builder.switch_to_block(*block);
}
// Sometimes the bytecode contains instructions after a return
// just ignore those until we are at the next label
if self.builder.is_filled() {
continue;
}
self.add_instruction(&instruction)?;
}
Ok(())
}
fn add_instruction(&mut self, instruction: &Instruction) -> Result<(), JitCompileError> {
match instruction {
Instruction::JumpIfFalse { target } => {
let cond = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
let then_block = self.builder.create_block();
self.label_to_block.insert(*target, then_block);
let val = self.boolean_val(cond)?;
self.builder.ins().brz(val, then_block, &[]);
let block = self.builder.create_block();
self.builder.ins().fallthrough(block, &[]);
self.builder.switch_to_block(block);
Ok(())
}
Instruction::Jump { target } => {
let target_block = self.builder.create_block();
self.label_to_block.insert(*target, target_block);
self.builder.ins().jump(target_block, &[]);
Ok(())
}
Instruction::LoadName {
name,
scope: NameScope::Local,
} => {
let local = self
.variables
.get(name)
.ok_or(JitCompileError::BadBytecode)?;
self.stack.push(JitValue {
val: self.builder.use_var(local.var),
ty: local.ty.clone(),
});
Ok(())
}
Instruction::StoreName {
name,
scope: NameScope::Local,
} => {
let val = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
self.store_variable(name.clone(), val)
}
Instruction::LoadConst {
value: Constant::Integer { value },
} => {
let val = self.builder.ins().iconst(
types::I64,
value.to_i64().ok_or(JitCompileError::NotSupported)?,
);
self.stack.push(JitValue {
val,
ty: JitType::Int,
});
Ok(())
}
Instruction::LoadConst {
value: Constant::Float { value },
} => {
let val = self.builder.ins().f64const(*value);
self.stack.push(JitValue {
val,
ty: JitType::Float,
});
Ok(())
}
Instruction::ReturnValue => {
let val = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
if let Some(ref ty) = self.sig.ret {
if val.ty != *ty {
return Err(JitCompileError::NotSupported);
}
} else {
self.sig.ret = Some(val.ty.clone());
self.builder
.func
.signature
.returns
.push(AbiParam::new(val.ty.to_cranelift()));
}
self.builder.ins().return_(&[val.val]);
Ok(())
}
Instruction::CompareOperation { op, .. } => {
// the rhs is popped off first
let b = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
match (a.ty, b.ty) {
(JitType::Int, JitType::Int) => {
let cond = match op {
ComparisonOperator::Equal => IntCC::Equal,
ComparisonOperator::NotEqual => IntCC::NotEqual,
ComparisonOperator::Less => IntCC::SignedLessThan,
ComparisonOperator::LessOrEqual => IntCC::SignedLessThanOrEqual,
ComparisonOperator::Greater => IntCC::SignedGreaterThan,
ComparisonOperator::GreaterOrEqual => IntCC::SignedLessThanOrEqual,
_ => return Err(JitCompileError::NotSupported),
};
let val = self.builder.ins().icmp(cond, a.val, b.val);
self.stack.push(JitValue {
val,
ty: JitType::Int, // TODO: Boolean
});
Ok(())
}
_ => Err(JitCompileError::NotSupported),
}
}
Instruction::UnaryOperation { op, .. } => {
let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
match a.ty {
JitType::Int => match op {
UnaryOperator::Minus => {
// Compile minus as 0 - a.
let zero = self.builder.ins().iconst(types::I64, 0);
let (out, carry) = self.builder.ins().isub_ifbout(zero, a.val);
self.builder.ins().trapif(
IntCC::Overflow,
carry,
TrapCode::IntegerOverflow,
);
self.stack.push(JitValue {
val: out,
ty: JitType::Int,
});
Ok(())
}
UnaryOperator::Plus => {
// Nothing to do
self.stack.push(a);
Ok(())
}
_ => Err(JitCompileError::NotSupported),
},
_ => Err(JitCompileError::NotSupported),
}
}
Instruction::BinaryOperation { op, .. } => {
// the rhs is popped off first
let b = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
let a = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
match (a.ty, b.ty) {
(JitType::Int, JitType::Int) => match op {
BinaryOperator::Add => {
let (out, carry) = self.builder.ins().iadd_ifcout(a.val, b.val);
self.builder.ins().trapif(
IntCC::Overflow,
carry,
TrapCode::IntegerOverflow,
);
self.stack.push(JitValue {
val: out,
ty: JitType::Int,
});
Ok(())
}
BinaryOperator::Subtract => {
let (out, carry) = self.builder.ins().isub_ifbout(a.val, b.val);
self.builder.ins().trapif(
IntCC::Overflow,
carry,
TrapCode::IntegerOverflow,
);
self.stack.push(JitValue {
val: out,
ty: JitType::Int,
});
Ok(())
}
_ => Err(JitCompileError::NotSupported),
},
(JitType::Float, JitType::Float) => match op {
BinaryOperator::Add => {
self.stack.push(JitValue {
val: self.builder.ins().fadd(a.val, b.val),
ty: JitType::Float,
});
Ok(())
}
BinaryOperator::Subtract => {
self.stack.push(JitValue {
val: self.builder.ins().fsub(a.val, b.val),
ty: JitType::Float,
});
Ok(())
}
BinaryOperator::Multiply => {
self.stack.push(JitValue {
val: self.builder.ins().fmul(a.val, b.val),
ty: JitType::Float,
});
Ok(())
}
BinaryOperator::Divide => {
self.stack.push(JitValue {
val: self.builder.ins().fdiv(a.val, b.val),
ty: JitType::Float,
});
Ok(())
}
_ => Err(JitCompileError::NotSupported),
},
_ => Err(JitCompileError::NotSupported),
}
}
_ => Err(JitCompileError::NotSupported),
}
}
}