From 573f631465b1dbaf3fe9b85844ab15b11e8fbc9d Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Sun, 4 Oct 2020 21:17:14 +0200 Subject: [PATCH] cranelift wip 0.3 --- .vscode/settings.json | 1 + src/jit.rs | 45 +++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b60caec..1d09a76 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "cSpell.ignoreWords": [ + "binemit", "codegen", "cranelift", "cranelift simplejit", diff --git a/src/jit.rs b/src/jit.rs index befa819..4223cd9 100644 --- a/src/jit.rs +++ b/src/jit.rs @@ -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); -- GitLab