tests all pass
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
use crate::backend::Backend;
|
use crate::backend::Backend;
|
||||||
use crate::eval::EvalError;
|
use crate::eval::EvalError;
|
||||||
use crate::ir::{Expression, Program, TopLevel, Type};
|
use crate::ir::{Expression, Program, Type};
|
||||||
use crate::syntax::Location;
|
|
||||||
use cranelift_jit::JITModule;
|
use cranelift_jit::JITModule;
|
||||||
use cranelift_object::ObjectModule;
|
use cranelift_object::ObjectModule;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use proptest::arbitrary::Arbitrary;
|
use proptest::arbitrary::Arbitrary;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use target_lexicon::Triple;
|
use target_lexicon::Triple;
|
||||||
|
|
||||||
@@ -56,45 +54,11 @@ impl Backend<ObjectModule> {
|
|||||||
//let allocator = Arena::<()>::new();
|
//let allocator = Arena::<()>::new();
|
||||||
//program.pretty(&allocator).render(80, &mut std::io::stdout())?;
|
//program.pretty(&allocator).render(80, &mut std::io::stdout())?;
|
||||||
let mut backend = Self::object_file(Triple::host())?;
|
let mut backend = Self::object_file(Triple::host())?;
|
||||||
let mut function_map = HashMap::new();
|
|
||||||
let mut main_function_body = vec![];
|
|
||||||
|
|
||||||
for item in program.items {
|
|
||||||
match item {
|
|
||||||
TopLevel::Function(name, args, rettype, body) => {
|
|
||||||
let function_id = backend.compile_function(
|
|
||||||
&mut HashMap::new(),
|
|
||||||
name.as_str(),
|
|
||||||
args.as_slice(),
|
|
||||||
rettype,
|
|
||||||
body,
|
|
||||||
)?;
|
|
||||||
function_map.insert(name, function_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
TopLevel::Statement(stmt) => {
|
|
||||||
main_function_body.push(stmt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let main_function_body = Expression::Block(
|
|
||||||
Location::manufactured(),
|
|
||||||
Type::Primitive(crate::eval::PrimitiveType::Void),
|
|
||||||
main_function_body,
|
|
||||||
);
|
|
||||||
|
|
||||||
let my_directory = tempfile::tempdir()?;
|
let my_directory = tempfile::tempdir()?;
|
||||||
let object_path = my_directory.path().join("object.o");
|
let object_path = my_directory.path().join("object.o");
|
||||||
let executable_path = my_directory.path().join("test_executable");
|
let executable_path = my_directory.path().join("test_executable");
|
||||||
|
|
||||||
backend.compile_function(
|
backend.compile_program("gogogo", program)?;
|
||||||
&mut HashMap::new(),
|
|
||||||
"gogogo",
|
|
||||||
&[],
|
|
||||||
Type::Primitive(crate::eval::PrimitiveType::Void),
|
|
||||||
main_function_body,
|
|
||||||
)?;
|
|
||||||
let bytes = backend.bytes()?;
|
let bytes = backend.bytes()?;
|
||||||
std::fs::write(&object_path, bytes)?;
|
std::fs::write(&object_path, bytes)?;
|
||||||
Self::link(&object_path, &executable_path)?;
|
Self::link(&object_path, &executable_path)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user