A fairly major refactor / simplification.
This commit is contained in:
57
src/backend/runtime.rs
Normal file
57
src/backend/runtime.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use cranelift_codegen::ir::{types, AbiParam, FuncRef, Function, Signature};
|
||||
use cranelift_codegen::isa::CallConv;
|
||||
use cranelift_module::{FuncId, Linkage, Module, ModuleResult};
|
||||
use std::collections::HashMap;
|
||||
use target_lexicon::Triple;
|
||||
use thiserror::Error;
|
||||
|
||||
pub struct RuntimeFunctions {
|
||||
builtin_functions: HashMap<String, FuncId>,
|
||||
_referenced_functions: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum RuntimeFunctionError {
|
||||
#[error("Could not find runtime function named '{0}'")]
|
||||
CannotFindRuntimeFunction(String),
|
||||
}
|
||||
|
||||
impl RuntimeFunctions {
|
||||
pub fn new<M: Module>(platform: &Triple, module: &mut M) -> ModuleResult<RuntimeFunctions> {
|
||||
let mut builtin_functions = HashMap::new();
|
||||
let _referenced_functions = Vec::new();
|
||||
|
||||
let string_param = AbiParam::new(types::I64);
|
||||
let int64_param = AbiParam::new(types::I64);
|
||||
|
||||
let print_id = module.declare_function(
|
||||
"print",
|
||||
Linkage::Import,
|
||||
&Signature {
|
||||
params: vec![string_param, int64_param],
|
||||
returns: vec![],
|
||||
call_conv: CallConv::triple_default(platform),
|
||||
},
|
||||
)?;
|
||||
builtin_functions.insert("print".to_string(), print_id);
|
||||
|
||||
Ok(RuntimeFunctions {
|
||||
builtin_functions,
|
||||
_referenced_functions,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn include_runtime_function<M: Module>(
|
||||
&self,
|
||||
name: &str,
|
||||
module: &mut M,
|
||||
func: &mut Function,
|
||||
) -> Result<FuncRef, RuntimeFunctionError> {
|
||||
match self.builtin_functions.get(name) {
|
||||
None => Err(RuntimeFunctionError::CannotFindRuntimeFunction(
|
||||
name.to_string(),
|
||||
)),
|
||||
Some(func_id) => Ok(module.declare_func_in_func(*func_id, func)),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user