checkpoint
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void print(char *variable_name, uint64_t value) {
|
void print(char *variable_name, uint64_t value) {
|
||||||
printf("%s = %lu", variable_name, value);
|
printf("%s = %llu\n", variable_name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void caller() {
|
void caller() {
|
||||||
|
|||||||
7
runtime/sample.c
Normal file
7
runtime/sample.c
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void print(char *variable_name, uint64_t value);
|
||||||
|
|
||||||
|
void gogogo() {
|
||||||
|
print("x", 4);
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ pub mod asts;
|
|||||||
pub mod backend;
|
pub mod backend;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod passes;
|
pub mod passes;
|
||||||
|
pub mod runtime;
|
||||||
pub mod syntax;
|
pub mod syntax;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
pub mod variable_map;
|
pub mod variable_map;
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
use crate::asts::lil::{Program, Statement, Primitive, SimpleExpression, Value};
|
use crate::asts::lil::{Program, Statement, Primitive, SimpleExpression, Value};
|
||||||
use cranelift_codegen::entity::EntityRef;
|
use cranelift_codegen::entity::EntityRef;
|
||||||
use cranelift_codegen::ir::{Function, UserFuncName, UserExternalName, Signature, types, InstBuilder, entities, ExternalName, ExtFuncData, AbiParam};
|
use cranelift_codegen::ir::{Function, UserFuncName, Signature, types, InstBuilder, entities};
|
||||||
use cranelift_codegen::isa::CallConv;
|
use cranelift_codegen::isa::CallConv;
|
||||||
use cranelift_codegen::{Context};
|
use cranelift_codegen::{Context};
|
||||||
use cranelift_frontend::{FunctionBuilderContext, FunctionBuilder, Variable};
|
use cranelift_frontend::{FunctionBuilderContext, FunctionBuilder, Variable};
|
||||||
use cranelift_module::{FuncId, Module, Linkage, ModuleError, ModuleCompiledFunction};
|
use cranelift_module::{FuncId, Module, Linkage, ModuleError, ModuleCompiledFunction};
|
||||||
|
|
||||||
impl<Annotation> Program<Annotation> {
|
impl<Annotation> Program<Annotation> {
|
||||||
|
|
||||||
pub fn into_cranelift<M: Module>(&self, module: &mut M, print_func_id: FuncId) -> Result<ModuleCompiledFunction, ModuleError> {
|
pub fn into_cranelift<M: Module>(&self, module: &mut M, print_func_id: FuncId) -> Result<ModuleCompiledFunction, ModuleError> {
|
||||||
let basic_signature = Signature {
|
let basic_signature = Signature {
|
||||||
params: vec![],
|
params: vec![],
|
||||||
@@ -28,7 +27,7 @@ impl<Annotation> Program<Annotation> {
|
|||||||
|
|
||||||
for stmt in self.statements.iter() {
|
for stmt in self.statements.iter() {
|
||||||
match stmt {
|
match stmt {
|
||||||
Statement::Print(ann, var) => {
|
Statement::Print(_ann, var) => {
|
||||||
let var = builder.use_var(Variable::new(*var));
|
let var = builder.use_var(Variable::new(*var));
|
||||||
builder.ins().call(print_func_ref, &[var]);
|
builder.ins().call(print_func_ref, &[var]);
|
||||||
}
|
}
|
||||||
|
|||||||
42
src/runtime.rs
Normal file
42
src/runtime.rs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
use cranelift_codegen::ir::{Signature, AbiParam, types, ArgumentPurpose, ArgumentExtension, Function, FuncRef};
|
||||||
|
use cranelift_codegen::isa::CallConv;
|
||||||
|
use cranelift_module::{Module, ModuleResult, Linkage, FuncId};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
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>(module: &mut M) -> ModuleResult<RuntimeFunctions> {
|
||||||
|
let mut builtin_functions = HashMap::new();
|
||||||
|
let _referenced_functions = Vec::new();
|
||||||
|
|
||||||
|
let string_param = AbiParam::new(types::R64);
|
||||||
|
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::AppleAarch64
|
||||||
|
})?;
|
||||||
|
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