λ Support functions! #5
@@ -21,7 +21,7 @@ function slope(p1, p2) -> u64
|
||||
(getY(p2) - p1.y) / (getX(p2) - p1.x);
|
||||
|
||||
origin = newPoint(0, 0);
|
||||
farther = newPoint(4, 4);
|
||||
farther = newPoint(2, 4);
|
||||
|
||||
mySlope = slope(origin, farther);
|
||||
print mySlope;
|
||||
@@ -199,6 +199,7 @@ impl<M: Module> Backend<M> {
|
||||
let func_id = match self.defined_functions.entry(interned_name) {
|
||||
hash_map::Entry::Occupied(entry) => *entry.get(),
|
||||
hash_map::Entry::Vacant(vac) => {
|
||||
tracing::warn!(name = ?function_name, "compiling undeclared function");
|
||||
let func_id = self.module.declare_function(
|
||||
function_name,
|
||||
Linkage::Export,
|
||||
|
||||
@@ -3,6 +3,7 @@ use cranelift_codegen::isa::CallConv;
|
||||
use cranelift_jit::JITBuilder;
|
||||
use cranelift_module::{FuncId, Linkage, Module, ModuleResult};
|
||||
use std::collections::HashMap;
|
||||
use std::alloc::Layout;
|
||||
use std::ffi::CStr;
|
||||
use std::fmt::Write;
|
||||
use target_lexicon::Triple;
|
||||
@@ -93,12 +94,17 @@ impl RuntimeFunctions {
|
||||
pub fn register_jit_implementations(builder: &mut JITBuilder) {
|
||||
let allocation_pointer = unsafe {
|
||||
std::alloc::alloc_zeroed(
|
||||
std::alloc::Layout::from_size_align(1024 * 1024, 1024 * 1024)
|
||||
Layout::from_size_align(1024 * 1024, 1024 * 1024)
|
||||
.expect("reasonable layout is reasonable"),
|
||||
)
|
||||
};
|
||||
let allocation_pointer_pointer = unsafe {
|
||||
let res = std::alloc::alloc(Layout::for_value(&allocation_pointer)) as *mut *mut u8;
|
||||
*res = allocation_pointer;
|
||||
res as *const u8
|
||||
};
|
||||
builder.symbol("print", runtime_print as *const u8);
|
||||
builder.symbol("__global_allocation_pointer__", allocation_pointer);
|
||||
builder.symbol("__global_allocation_pointer__", allocation_pointer_pointer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use clap::Parser;
|
||||
use codespan_reporting::files::SimpleFiles;
|
||||
use cranelift_codegen::timing::compile;
|
||||
use ngr::backend::Backend;
|
||||
use ngr::eval::Value;
|
||||
use ngr::syntax;
|
||||
use ngr::type_infer::TypeInferenceResult;
|
||||
use pretty::termcolor::StandardStream;
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
@@ -42,7 +44,10 @@ fn jit(ir: ngr::ir::Program<ngr::ir::Type>) -> Result<fn(), ngr::backend::Backen
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.with(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.init();
|
||||
let cli = CommandLineArguments::parse();
|
||||
let mut file_database = SimpleFiles::new();
|
||||
let mut console = StandardStream::stdout(pretty::termcolor::ColorChoice::Auto);
|
||||
@@ -107,6 +112,8 @@ fn main() {
|
||||
|
||||
match jit(ir) {
|
||||
Err(e) => emit(e.into()),
|
||||
Ok(compiled_function) => compiled_function(),
|
||||
Ok(compiled_function) => {
|
||||
compiled_function()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user