Skip to content
Snippets Groups Projects
Commit 573f6314 authored by Per Lindgren's avatar Per Lindgren
Browse files

cranelift wip 0.3

parent 4930f443
No related branches found
No related tags found
No related merge requests found
{
"cSpell.ignoreWords": [
"binemit",
"codegen",
"cranelift",
"cranelift simplejit",
......
......@@ -44,18 +44,11 @@ impl JIT {
// First, parse the string, producing AST nodes.
let f = &p.fn_decls[0];
// let (name, params, the_return, stmts) =
// parser::function(&input).map_err(|e| e.to_string())?;
// Then, translate the AST nodes into Cranelift IR.
// Translate the AST nodes into Cranelift IR.
self.translate(f).map_err(|e| e.to_string())?;
// // Next, declare the function to simplejit. 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?
// Next, declare the function to simplejit. Functions must be declared
// before they can be called, or defined.
let id = self
.module
.declare_function(f.id.as_str(), Linkage::Export, &self.ctx.func.signature)
......@@ -137,8 +130,6 @@ impl JIT {
// 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.
......@@ -164,15 +155,15 @@ impl JIT {
variables,
module: &mut self.module,
};
for expr in stmts {
trans.translate_expr(expr);
for stmt in &f.body.stmts {
trans.translate_stmt(stmt);
}
// // 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);
// 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]);
......@@ -200,6 +191,18 @@ impl<'a> FunctionTranslator<'a> {
_ => unimplemented!(),
}
}
fn translate_stmt(&mut self, stmt: &Stmt) -> Value {
match stmt {
Stmt::Assign(l, r) => {
let new_value = self.translate_expr(r);
let variable = self.variables.get("res").unwrap();
self.builder.def_var(*variable, new_value);
new_value
}
_ => unimplemented!(),
}
}
}
// Expr::Add(lhs, rhs) => {
......@@ -430,9 +433,9 @@ fn declare_variables(
let mut variables = HashMap::new();
let mut index = 0;
// for
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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment