-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathjit.rs
466 lines (396 loc) · 17.1 KB
/
jit.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use crate::frontend::*;
use cranelift::prelude::*;
use cranelift_jit::{JITBuilder, JITModule};
use cranelift_module::{DataDescription, Linkage, Module};
use std::collections::HashMap;
use std::slice;
/// The basic JIT class.
pub struct JIT {
/// The function builder context, which is reused across multiple
/// FunctionBuilder instances.
builder_context: FunctionBuilderContext,
/// The main Cranelift context, which holds the state for codegen. Cranelift
/// separates this from `Module` to allow for parallel compilation, with a
/// context per thread, though this isn't in the simple demo here.
ctx: codegen::Context,
/// The data description, which is to data objects what `ctx` is to functions.
data_description: DataDescription,
/// The module, with the jit backend, which manages the JIT'd
/// functions.
module: JITModule,
}
impl Default for JIT {
fn default() -> Self {
let mut flag_builder = settings::builder();
flag_builder.set("use_colocated_libcalls", "false").unwrap();
flag_builder.set("is_pic", "false").unwrap();
let isa_builder = cranelift_native::builder().unwrap_or_else(|msg| {
panic!("host machine is not supported: {}", msg);
});
let isa = isa_builder
.finish(settings::Flags::new(flag_builder))
.unwrap();
let builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
let module = JITModule::new(builder);
Self {
builder_context: FunctionBuilderContext::new(),
ctx: module.make_context(),
data_description: DataDescription::new(),
module,
}
}
}
impl JIT {
/// Compile a string in the toy language into machine code.
pub fn compile(&mut self, input: &str) -> Result<*const u8, String> {
// First, parse the string, producing AST nodes.
let (name, params, the_return, stmts) =
parser::function(input).map_err(|e| e.to_string())?;
// Then, translate the AST nodes into Cranelift IR.
self.translate(params, the_return, stmts)?;
// Next, declare the function to jit. Functions must be declared
// before they can be called, or defined.
//
// TODO: This may be an area where the API should be streamlined; should
// we have a version of `declare_function` that automatically declares
// the function?
let id = self
.module
.declare_function(&name, Linkage::Export, &self.ctx.func.signature)
.map_err(|e| e.to_string())?;
// Define the function to jit. This finishes compilation, although
// there may be outstanding relocations to perform. Currently, jit
// cannot finish relocations until all functions to be called are
// defined. For this toy demo for now, we'll just finalize the
// function below.
self.module
.define_function(id, &mut self.ctx)
.map_err(|e| e.to_string())?;
// Now that compilation is finished, we can clear out the context state.
self.module.clear_context(&mut self.ctx);
// Finalize the functions which we just defined, which resolves any
// outstanding relocations (patching in addresses, now that they're
// available).
self.module.finalize_definitions().unwrap();
// We can now retrieve a pointer to the machine code.
let code = self.module.get_finalized_function(id);
Ok(code)
}
/// Create a zero-initialized data section.
pub fn create_data(&mut self, name: &str, contents: Vec<u8>) -> Result<&[u8], String> {
// The steps here are analogous to `compile`, except that data is much
// simpler than functions.
self.data_description.define(contents.into_boxed_slice());
let id = self
.module
.declare_data(name, Linkage::Export, true, false)
.map_err(|e| e.to_string())?;
self.module
.define_data(id, &self.data_description)
.map_err(|e| e.to_string())?;
self.data_description.clear();
self.module.finalize_definitions().unwrap();
let buffer = self.module.get_finalized_data(id);
// TODO: Can we move the unsafe into cranelift?
Ok(unsafe { slice::from_raw_parts(buffer.0, buffer.1) })
}
// Translate from toy-language AST nodes into Cranelift IR.
fn translate(
&mut self,
params: Vec<String>,
the_return: String,
stmts: Vec<Expr>,
) -> Result<(), String> {
// Our toy language currently only supports I64 values, though Cranelift
// supports other types.
let int = self.module.target_config().pointer_type();
for _p in ¶ms {
self.ctx.func.signature.params.push(AbiParam::new(int));
}
// Our toy language currently only supports one return value, though
// Cranelift is designed to support more.
self.ctx.func.signature.returns.push(AbiParam::new(int));
// Create the builder to build a function.
let mut builder = FunctionBuilder::new(&mut self.ctx.func, &mut self.builder_context);
// Create the entry block, to start emitting code in.
let entry_block = builder.create_block();
// Since this is the entry block, add block parameters corresponding to
// the function's parameters.
//
// TODO: Streamline the API here.
builder.append_block_params_for_function_params(entry_block);
// Tell the builder to emit code in this block.
builder.switch_to_block(entry_block);
// And, tell the builder that this block will have no further
// predecessors. Since it's the entry block, it won't have any
// predecessors.
builder.seal_block(entry_block);
// The toy language allows variables to be declared implicitly.
// Walk the AST and declare all implicitly-declared variables.
let variables =
declare_variables(int, &mut builder, ¶ms, &the_return, &stmts, entry_block);
// Now translate the statements of the function body.
let mut trans = FunctionTranslator {
int,
builder,
variables,
module: &mut self.module,
};
for expr in stmts {
trans.translate_expr(expr);
}
// Set up the return variable of the function. Above, we declared a
// variable to hold the return value. Here, we just do a use of that
// variable.
let return_variable = trans.variables.get(&the_return).unwrap();
let return_value = trans.builder.use_var(*return_variable);
// Emit the return instruction.
trans.builder.ins().return_(&[return_value]);
// Tell the builder we're done with this function.
trans.builder.finalize();
Ok(())
}
}
/// A collection of state used for translating from toy-language AST nodes
/// into Cranelift IR.
struct FunctionTranslator<'a> {
int: types::Type,
builder: FunctionBuilder<'a>,
variables: HashMap<String, Variable>,
module: &'a mut JITModule,
}
impl<'a> FunctionTranslator<'a> {
/// When you write out instructions in Cranelift, you get back `Value`s. You
/// can then use these references in other instructions.
fn translate_expr(&mut self, expr: Expr) -> Value {
match expr {
Expr::Literal(literal) => {
let imm: i32 = literal.parse().unwrap();
self.builder.ins().iconst(self.int, i64::from(imm))
}
Expr::Add(lhs, rhs) => {
let lhs = self.translate_expr(*lhs);
let rhs = self.translate_expr(*rhs);
self.builder.ins().iadd(lhs, rhs)
}
Expr::Sub(lhs, rhs) => {
let lhs = self.translate_expr(*lhs);
let rhs = self.translate_expr(*rhs);
self.builder.ins().isub(lhs, rhs)
}
Expr::Mul(lhs, rhs) => {
let lhs = self.translate_expr(*lhs);
let rhs = self.translate_expr(*rhs);
self.builder.ins().imul(lhs, rhs)
}
Expr::Div(lhs, rhs) => {
let lhs = self.translate_expr(*lhs);
let rhs = self.translate_expr(*rhs);
self.builder.ins().udiv(lhs, rhs)
}
Expr::Eq(lhs, rhs) => self.translate_icmp(IntCC::Equal, *lhs, *rhs),
Expr::Ne(lhs, rhs) => self.translate_icmp(IntCC::NotEqual, *lhs, *rhs),
Expr::Lt(lhs, rhs) => self.translate_icmp(IntCC::SignedLessThan, *lhs, *rhs),
Expr::Le(lhs, rhs) => self.translate_icmp(IntCC::SignedLessThanOrEqual, *lhs, *rhs),
Expr::Gt(lhs, rhs) => self.translate_icmp(IntCC::SignedGreaterThan, *lhs, *rhs),
Expr::Ge(lhs, rhs) => self.translate_icmp(IntCC::SignedGreaterThanOrEqual, *lhs, *rhs),
Expr::Call(name, args) => self.translate_call(name, args),
Expr::GlobalDataAddr(name) => self.translate_global_data_addr(name),
Expr::Identifier(name) => {
// `use_var` is used to read the value of a variable.
let variable = self.variables.get(&name).expect("variable not defined");
self.builder.use_var(*variable)
}
Expr::Assign(name, expr) => self.translate_assign(name, *expr),
Expr::IfElse(condition, then_body, else_body) => {
self.translate_if_else(*condition, then_body, else_body)
}
Expr::WhileLoop(condition, loop_body) => {
self.translate_while_loop(*condition, loop_body)
}
}
}
fn translate_assign(&mut self, name: String, expr: Expr) -> Value {
// `def_var` is used to write the value of a variable. Note that
// variables can have multiple definitions. Cranelift will
// convert them into SSA form for itself automatically.
let new_value = self.translate_expr(expr);
let variable = self.variables.get(&name).unwrap();
self.builder.def_var(*variable, new_value);
new_value
}
fn translate_icmp(&mut self, cmp: IntCC, lhs: Expr, rhs: Expr) -> Value {
let lhs = self.translate_expr(lhs);
let rhs = self.translate_expr(rhs);
self.builder.ins().icmp(cmp, lhs, rhs)
}
fn translate_if_else(
&mut self,
condition: Expr,
then_body: Vec<Expr>,
else_body: Vec<Expr>,
) -> Value {
let condition_value = self.translate_expr(condition);
let then_block = self.builder.create_block();
let else_block = self.builder.create_block();
let merge_block = self.builder.create_block();
// If-else constructs in the toy language have a return value.
// In traditional SSA form, this would produce a PHI between
// the then and else bodies. Cranelift uses block parameters,
// so set up a parameter in the merge block, and we'll pass
// the return values to it from the branches.
self.builder.append_block_param(merge_block, self.int);
// Test the if condition and conditionally branch.
self.builder
.ins()
.brif(condition_value, then_block, &[], else_block, &[]);
self.builder.switch_to_block(then_block);
self.builder.seal_block(then_block);
let mut then_return = self.builder.ins().iconst(self.int, 0);
for expr in then_body {
then_return = self.translate_expr(expr);
}
// Jump to the merge block, passing it the block return value.
self.builder.ins().jump(merge_block, &[then_return]);
self.builder.switch_to_block(else_block);
self.builder.seal_block(else_block);
let mut else_return = self.builder.ins().iconst(self.int, 0);
for expr in else_body {
else_return = self.translate_expr(expr);
}
// Jump to the merge block, passing it the block return value.
self.builder.ins().jump(merge_block, &[else_return]);
// Switch to the merge block for subsequent statements.
self.builder.switch_to_block(merge_block);
// We've now seen all the predecessors of the merge block.
self.builder.seal_block(merge_block);
// Read the value of the if-else by reading the merge block
// parameter.
let phi = self.builder.block_params(merge_block)[0];
phi
}
fn translate_while_loop(&mut self, condition: Expr, loop_body: Vec<Expr>) -> Value {
let header_block = self.builder.create_block();
let body_block = self.builder.create_block();
let exit_block = self.builder.create_block();
self.builder.ins().jump(header_block, &[]);
self.builder.switch_to_block(header_block);
let condition_value = self.translate_expr(condition);
self.builder
.ins()
.brif(condition_value, body_block, &[], exit_block, &[]);
self.builder.switch_to_block(body_block);
self.builder.seal_block(body_block);
for expr in loop_body {
self.translate_expr(expr);
}
self.builder.ins().jump(header_block, &[]);
self.builder.switch_to_block(exit_block);
// We've reached the bottom of the loop, so there will be no
// more backedges to the header to exits to the bottom.
self.builder.seal_block(header_block);
self.builder.seal_block(exit_block);
// Just return 0 for now.
self.builder.ins().iconst(self.int, 0)
}
fn translate_call(&mut self, name: String, args: Vec<Expr>) -> Value {
let mut sig = self.module.make_signature();
// Add a parameter for each argument.
for _arg in &args {
sig.params.push(AbiParam::new(self.int));
}
// For simplicity for now, just make all calls return a single I64.
sig.returns.push(AbiParam::new(self.int));
// TODO: Streamline the API here?
let callee = self
.module
.declare_function(&name, Linkage::Import, &sig)
.expect("problem declaring function");
let local_callee = self.module.declare_func_in_func(callee, self.builder.func);
let mut arg_values = Vec::new();
for arg in args {
arg_values.push(self.translate_expr(arg))
}
let call = self.builder.ins().call(local_callee, &arg_values);
self.builder.inst_results(call)[0]
}
fn translate_global_data_addr(&mut self, name: String) -> Value {
let sym = self
.module
.declare_data(&name, Linkage::Export, true, false)
.expect("problem declaring data object");
let local_id = self.module.declare_data_in_func(sym, self.builder.func);
let pointer = self.module.target_config().pointer_type();
self.builder.ins().symbol_value(pointer, local_id)
}
}
fn declare_variables(
int: types::Type,
builder: &mut FunctionBuilder,
params: &[String],
the_return: &str,
stmts: &[Expr],
entry_block: Block,
) -> HashMap<String, Variable> {
let mut variables = HashMap::new();
let mut index = 0;
for (i, name) in params.iter().enumerate() {
// TODO: cranelift_frontend should really have an API to make it easy to set
// up param variables.
let val = builder.block_params(entry_block)[i];
let var = declare_variable(int, builder, &mut variables, &mut index, name);
builder.def_var(var, val);
}
let zero = builder.ins().iconst(int, 0);
let return_variable = declare_variable(int, builder, &mut variables, &mut index, the_return);
builder.def_var(return_variable, zero);
for expr in stmts {
declare_variables_in_stmt(int, builder, &mut variables, &mut index, expr);
}
variables
}
/// Recursively descend through the AST, translating all implicit
/// variable declarations.
fn declare_variables_in_stmt(
int: types::Type,
builder: &mut FunctionBuilder,
variables: &mut HashMap<String, Variable>,
index: &mut usize,
expr: &Expr,
) {
match *expr {
Expr::Assign(ref name, _) => {
declare_variable(int, builder, variables, index, name);
}
Expr::IfElse(ref _condition, ref then_body, ref else_body) => {
for stmt in then_body {
declare_variables_in_stmt(int, builder, variables, index, stmt);
}
for stmt in else_body {
declare_variables_in_stmt(int, builder, variables, index, stmt);
}
}
Expr::WhileLoop(ref _condition, ref loop_body) => {
for stmt in loop_body {
declare_variables_in_stmt(int, builder, variables, index, stmt);
}
}
_ => (),
}
}
/// Declare a single variable declaration.
fn declare_variable(
int: types::Type,
builder: &mut FunctionBuilder,
variables: &mut HashMap<String, Variable>,
index: &mut usize,
name: &str,
) -> Variable {
let var = Variable::new(*index);
if !variables.contains_key(name) {
variables.insert(name.into(), var);
builder.declare_var(var, int);
*index += 1;
}
var
}