Some cleanups.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::backend::runtime::RuntimeFunctions;
|
||||
use crate::ir::{Expression, Primitive, Program, Statement, Value, ValueOrRef};
|
||||
use cranelift_codegen::entity::EntityRef;
|
||||
use cranelift_codegen::ir::{
|
||||
@@ -9,26 +8,20 @@ use cranelift_codegen::ir::{
|
||||
use cranelift_codegen::isa::CallConv;
|
||||
use cranelift_codegen::Context;
|
||||
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
|
||||
use cranelift_module::{DataContext, DataId, FuncId, Linkage, Module, ModuleError};
|
||||
use cranelift_module::{FuncId, Linkage, Module, ModuleError};
|
||||
use internment::ArcIntern;
|
||||
|
||||
use super::RuntimeFunctionError;
|
||||
use crate::backend::Backend;
|
||||
use crate::backend::error::BackendError;
|
||||
|
||||
type StringTable = HashMap<ArcIntern<String>, GlobalValue>;
|
||||
|
||||
impl Program {
|
||||
pub fn into_cranelift<E, M>(
|
||||
mut self,
|
||||
module: &mut M,
|
||||
impl<M: Module> Backend<M> {
|
||||
pub fn compile_function(
|
||||
&mut self,
|
||||
function_name: &str,
|
||||
rtfuns: &RuntimeFunctions,
|
||||
pre_defined_strings: &HashMap<String, DataId>,
|
||||
pre_defined_symbols: &HashMap<String, DataId>,
|
||||
) -> Result<FuncId, E>
|
||||
where
|
||||
E: From<ModuleError>,
|
||||
E: From<RuntimeFunctionError>,
|
||||
M: Module,
|
||||
mut program: Program,
|
||||
) -> Result<FuncId, BackendError>
|
||||
{
|
||||
let basic_signature = Signature {
|
||||
params: vec![],
|
||||
@@ -36,19 +29,20 @@ impl Program {
|
||||
call_conv: CallConv::SystemV,
|
||||
};
|
||||
|
||||
let func_id = module.declare_function(function_name, Linkage::Export, &basic_signature)?;
|
||||
let func_id = self.module.declare_function(function_name, Linkage::Export, &basic_signature)?;
|
||||
let mut ctx = Context::new();
|
||||
ctx.func =
|
||||
Function::with_name_signature(UserFuncName::user(0, func_id.as_u32()), basic_signature);
|
||||
|
||||
let string_table = self.build_string_table(module, &mut ctx.func, pre_defined_strings)?;
|
||||
let string_table = self.build_string_table(&mut ctx.func, &program)?;
|
||||
let mut variable_table = HashMap::new();
|
||||
let mut next_var_num = 1;
|
||||
let print_func_ref = rtfuns.include_runtime_function("print", module, &mut ctx.func)?;
|
||||
let pre_defined_symbols: HashMap<String, GlobalValue> = pre_defined_symbols
|
||||
let print_func_ref = self.runtime_functions.include_runtime_function("print", &mut self.module, &mut ctx.func)?;
|
||||
let pre_defined_symbols: HashMap<String, GlobalValue> = self
|
||||
.defined_symbols
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let local_data = module.declare_data_in_func(*v, &mut ctx.func);
|
||||
let local_data = self.module.declare_data_in_func(*v, &mut ctx.func);
|
||||
(k.clone(), local_data)
|
||||
})
|
||||
.collect();
|
||||
@@ -58,7 +52,7 @@ impl Program {
|
||||
let main_block = builder.create_block();
|
||||
builder.switch_to_block(main_block);
|
||||
|
||||
for stmt in self.statements.drain(..) {
|
||||
for stmt in program.statements.drain(..) {
|
||||
match stmt {
|
||||
Statement::Print(ann, var) => {
|
||||
let local_name_ref = string_table.get(&var).unwrap();
|
||||
@@ -121,43 +115,24 @@ impl Program {
|
||||
builder.seal_block(main_block);
|
||||
builder.finalize();
|
||||
|
||||
let _ = module.define_function(func_id, &mut ctx)?;
|
||||
let _ = self.module.define_function(func_id, &mut ctx)?;
|
||||
|
||||
Ok(func_id)
|
||||
}
|
||||
|
||||
fn build_string_table<M: Module>(
|
||||
&self,
|
||||
module: &mut M,
|
||||
fn build_string_table(
|
||||
&mut self,
|
||||
func: &mut Function,
|
||||
pre_defined_strings: &HashMap<String, DataId>,
|
||||
) -> Result<StringTable, ModuleError> {
|
||||
program: &Program,
|
||||
) -> Result<StringTable, BackendError> {
|
||||
let mut string_table = HashMap::new();
|
||||
|
||||
for (idx, interned_value) in self.strings().drain().enumerate() {
|
||||
let global_id = match pre_defined_strings.get(interned_value.as_str()) {
|
||||
for interned_value in program.strings().drain() {
|
||||
let global_id = match self.defined_strings.get(interned_value.as_str()) {
|
||||
Some(x) => *x,
|
||||
None => {
|
||||
let global_id = module.declare_data(
|
||||
&format!("local-string-{}", idx),
|
||||
Linkage::Local,
|
||||
false,
|
||||
false,
|
||||
)?;
|
||||
let mut data_context = DataContext::new();
|
||||
data_context.set_align(8);
|
||||
data_context.define(
|
||||
interned_value
|
||||
.as_str()
|
||||
.to_owned()
|
||||
.into_boxed_str()
|
||||
.into_boxed_bytes(),
|
||||
);
|
||||
module.define_data(global_id, &data_context)?;
|
||||
global_id
|
||||
}
|
||||
None => self.define_string(interned_value.as_str())?,
|
||||
};
|
||||
let local_data = module.declare_data_in_func(global_id, func);
|
||||
let local_data = self.module.declare_data_in_func(global_id, func);
|
||||
string_table.insert(interned_value, local_data);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user