Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ members = [
"cachectx",
"typetable",
"scopetable"
]
, "compiler"]
resolver = "2"

13 changes: 13 additions & 0 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "compiler"
version = "0.1.0"
edition = "2021"

[dependencies]
typetable = { path="../typetable" }
symtable = { path="../symtable" }
types = { path="../types" }
object = { path="../object" }
ir = { path="../ir" }
cranelift-codegen = "0"
cranelift-module = "0"
51 changes: 51 additions & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use cranelift_codegen::ir::Function;
use cranelift_module::DataId;
use ir::IRFunc;
use object::ObjectSource;
use std::rc::Rc;
use symtable::SymTable;
use types::{FunctionInitialize, TypeTree};
use typetable::TypeTable;

// right now is just looping on lint source to generate multiple ir and data
pub struct Compiler {
pub lintres: Vec<Rc<Box<TypeTree>>>,
pub fncnt: usize,
pub firs: Vec<Function>,
pub obj: ObjectSource,
pub typtbl: Vec<TypeTable>,
pub gbl: Vec<(DataId, String)>,
}

impl<'table> Compiler {
pub fn new(lintres: Vec<Rc<Box<TypeTree>>>, obj: ObjectSource, typtbl: Vec<TypeTable>) -> Self {
Compiler {
lintres,
firs: vec![],
fncnt: 0,
obj,
typtbl,
gbl: vec![],
}
}

pub fn loopf(&mut self) -> () {
for item in &self.lintres {
match item.as_ref().as_ref() {
TypeTree::ConstInit(y) => {
let id = self.obj.add_const_data("x", y.right.into_data());
self.gbl.push((id, "x".to_string()));
}
TypeTree::FuncInit(y) => {
let func = self.make_ir(&y);
self.firs.push(func);
}
_ => panic!("developer error, unhandled loopfval, {:?}", item.clone()),
}
}
}
fn make_ir(&self, fi: &FunctionInitialize) -> Function {
let mut ir = IRFunc::new(0, SymTable::new());
return ir.begin(fi, &self.gbl);
}
}
10 changes: 5 additions & 5 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use linter::LintSource;
use parser::Parser;
use std::fs::File;
use std::io::Read;
use typetable::TypeTable;

use std::path::Path;
use std::process::Command;

fn main() {
println!("[run] simple exe");
objmaker::from_buffer(
"pub const main = fn() usize {
const m = 7
const x = 5
return x + m
"const z = 1\n
pub const main = fn() usize {\n
const m = 7\n
const x = 4\n
return x + m + z
}",
Path::new("main.ty"),
);
Expand Down
2 changes: 2 additions & 0 deletions ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ linter = { path="../linter" }
types = { path="../types" }
cranelift-frontend = "0"
cranelift-codegen = "0"
cranelift-module = "0"
cranelift-object = "0"
44 changes: 26 additions & 18 deletions ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ use cranelift_codegen::settings;
use cranelift_codegen::verifier::verify_function;
use cranelift_frontend::*;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_module::{DataId, Module};
use cranelift_object::ObjectModule;
use perror::*;
use std::rc::Rc;
use symtable::*;
use types::*;
use typetable::*;

pub struct IRSource<'tt> {
pub struct IRFunc<'gbl, 'md> {
package: u32,
fname: u32,
variables: u32,
scope: SymTable,
t_scope: &'tt TypeTable,
global: Option<&'gbl Vec<(DataId, String)>>,
obj_mod: Option<&'md mut ObjectModule>,
}

impl<'tt> IRSource<'tt> {
pub fn new(package: u32, scope: SymTable, t_scope: &'tt TypeTable) -> Self {
IRSource {
impl<'gbl, 'md> IRFunc<'gbl, 'md> {
pub fn new(package: u32, scope: SymTable) -> Self {
IRFunc {
package,
fname: 0,
variables: 0,
scope,
t_scope,
global: None,
}
}
pub fn handle_const_init(
Expand Down Expand Up @@ -96,10 +97,12 @@ impl<'tt> IRSource<'tt> {
builder.ins().return_(&[arg]);
Ok(temp)
}
pub fn handle_sym(&self, op: &SymbolAccess) -> ResultFir<Variable> {
Ok(Variable::from_u32(
*self.scope.table.get(&op.ident).unwrap(),
))
pub fn handle_sym(&self, op: &SymbolAccess, builder: &mut FunctionBuilder) -> ResultFir<Variable> {
if let Some(val) = self.scope.table.get(&op.ident) {
return Ok(Variable::from_u32(*val));
}
let data = self.obj_mod.unwrap().declare_data_in_func(self.global.builder.
return Ok(Variable::from_u32(2));
}
pub fn handle_u64(&mut self, num: u64, builder: &mut FunctionBuilder) -> ResultFir<Variable> {
let result = self.add_var();
Expand Down Expand Up @@ -166,13 +169,17 @@ impl<'tt> IRSource<'tt> {
_ => panic!("developer error unexpected expression {:?}", expr),
}
}
pub fn begin(&mut self, func_def: Rc<Box<TypeTree>>) -> Function {
pub fn begin(
&mut self,
func_def: &FunctionInitialize,
gbl: &'gbl Vec<(DataId, String)>,
) -> Function {
self.global = Some(gbl);
let mut ctx = FunctionBuilderContext::new();
let mut sig = Signature::new(CallConv::SystemV);
let name = UserFuncName::user(self.package, self.fname);
let func_init = func_def.into_func_init();
// todo:: types need to be worked out, params and returns defined
func_init
func_def
.args
.iter()
.for_each(|_x| sig.params.push(AbiParam::new(I64)));
Expand All @@ -183,7 +190,7 @@ impl<'tt> IRSource<'tt> {
let root_block = builder.create_block();
builder.append_block_params_for_function_params(root_block);
builder.switch_to_block(root_block);
let _result = self.recurse(&func_init.block, &mut builder);
let _result = self.recurse(&func_def.block, &mut builder);
builder.seal_block(root_block);
builder.finalize();
func
Expand Down Expand Up @@ -266,9 +273,10 @@ mod tests {
let mut tt = vec![];
let mut scp = vec![];
let mut linter = LintSource::new("test", &mut scp, &mut tt);
let global: Vec<(DataId, String)> = vec![];
let linter_result = linter.check_func_decl(&func_def).unwrap();
let mut fir = IRSource::new(0, SymTable::new(), &linter.ttbls.get(0).unwrap());
let result = fir.begin(linter_result.0);
let mut fir = IRFunc::new(0, SymTable::new());
let result = fir.begin(&linter_result.0.into_func_init(), &global);
/*
* function u0:0() -> i64 system_v
* {
Expand Down
63 changes: 42 additions & 21 deletions object/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
use cranelift_codegen::ir::Function;
use cranelift_codegen::settings::*;
use cranelift_codegen::Context;
use cranelift_module::{Linkage, Module};
use cranelift_module::DataId;
use cranelift_module::{DataDescription, Linkage, Module};
use cranelift_object::{ObjectBuilder, ObjectModule};

pub fn new_obj_handler(obj_name: &str) -> ObjectModule {
let settings = builder();
let flags = Flags::new(settings);
let isa_builder = cranelift_native::builder().unwrap();
let isa = isa_builder.finish(flags).unwrap();

let obj_builder =
ObjectBuilder::new(isa, obj_name, cranelift_module::default_libcall_names()).unwrap();
ObjectModule::new(obj_builder)
pub struct ObjectSource {
pub obj_mod: ObjectModule,
pub data: DataDescription,
pub name: String,
}

pub fn build_std_fn(om: &mut ObjectModule, func: Function, obj_name: &str) -> () {
let func_id = om
.declare_function(obj_name, Linkage::Export, &func.signature)
.unwrap();
impl ObjectSource {
pub fn new(obj_name: &str) -> ObjectSource {
let settings = builder();
let flags = Flags::new(settings);
let isa_builder = cranelift_native::builder().unwrap();
let isa = isa_builder.finish(flags).unwrap();

let mut ctx = Context::for_function(func);
om.define_function(func_id, &mut ctx).unwrap();
}
let obj_builder =
ObjectBuilder::new(isa, obj_name, cranelift_module::default_libcall_names()).unwrap();
ObjectSource {
obj_mod: ObjectModule::new(obj_builder),
data: DataDescription::new(),
name: obj_name.to_string(),
}
}
pub fn add_const_data(&mut self, name: &str, contents: Vec<u8>) -> DataId {
self.data.define(contents.into_boxed_slice());
let id = self
.obj_mod
.declare_data(name, Linkage::Export, false, false)
.unwrap();
self.obj_mod.define_data(id, &self.data).unwrap();
return id;
}
pub fn add_fn(&mut self, name: &str, func: Function) -> () {
let func_id = self
.obj_mod
.declare_function(name, Linkage::Export, &func.signature)
.unwrap();

pub fn flush_obj(om: ObjectModule) -> Vec<u8> {
let object_product = om.finish();
let bytes = object_product.emit().unwrap();
return bytes;
let mut ctx = Context::for_function(func);
self.obj_mod.define_function(func_id, &mut ctx).unwrap();
}
pub fn flush_self(self) -> Vec<u8> {
let object_product = self.obj_mod.finish();
let bytes = object_product.emit().unwrap();
return bytes;
}
}
48 changes: 14 additions & 34 deletions objmaker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use ir::IRSource;
use ir::IRFunc;
use lexer::TLexer;
use linter::LintSource;
use object::build_std_fn;
use object::flush_obj;
use object::new_obj_handler;
use object::*;
use parser::Parser;
use std::fs::create_dir;
use std::fs::write;
Expand All @@ -12,7 +10,6 @@ use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use symtable::SymTable;
use typetable::TypeTable;

pub fn from_buffer(contents: &str, path: &Path) -> () {
let lex = TLexer::new(&contents);
Expand All @@ -22,7 +19,7 @@ pub fn from_buffer(contents: &str, path: &Path) -> () {
let mut scopes = vec![];
let mut linter = LintSource::new(path.to_str().unwrap(), &mut scopes, &mut type_tables);
let lint_res = linter.lint_check(&ast_parsed);
let mut ir = IRSource::new(0, SymTable::new(), linter.ttbls.get(0).unwrap());
let mut ir = IRFunc::new(0, SymTable::new(), linter.ttbls.get(0).unwrap());
if linter.issues.len() > 0 {
for x in linter.issues {
println!("{}", x);
Expand All @@ -40,38 +37,21 @@ pub fn from_buffer(contents: &str, path: &Path) -> () {
output.push(".ty");
output.push(filename);
output.set_extension("o");
let mut om = new_obj_handler(filename);
build_std_fn(&mut om, result, filename);
write(output, flush_obj(om)).unwrap();
let object_source = ObjectSource::new(&output.to_string_lossy());
let object_source = object_source;
let mut om = object_source;
let res = lint_res.get(0).unwrap().into_init();
om.add_const_data(&res.left, res.right.into_data());
let rc_thing = lint_res.get(1).unwrap().to_owned();
let result = ir.begin(rc_thing);
println!("result {:?}", result);
om.add_fn("main", result);
write(output, om.flush_self()).unwrap();
}

pub fn from_file(input_path: &PathBuf) -> () {
let mut file = File::open(input_path.clone()).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let lex = TLexer::new(&contents);
let mut parse = Parser::new(lex);
let ast_parsed = parse.all().unwrap();
let mut type_tables = vec![];
let mut scopes = vec![];
let mut linter = LintSource::new(input_path.to_str().unwrap(), &mut scopes, &mut type_tables);
let lint_res = linter.lint_check(&ast_parsed);
let mut ir = IRSource::new(0, SymTable::new(), linter.ttbls.get(0).unwrap());
if linter.issues.len() > 0 {
panic!("linter issues exist");
}
let rc_thing = lint_res.first().unwrap().to_owned();
let result = ir.begin(rc_thing);
if !Path::new(".ty").is_dir() {
create_dir(".ty").unwrap();
}
let wo_extension = input_path.with_extension("");
let filename = wo_extension.file_name().unwrap().to_str().unwrap();
let mut output = PathBuf::new();
output.push(".ty");
output.push(filename);
output.set_extension("o");
let mut om = new_obj_handler(filename);
build_std_fn(&mut om, result, filename);
write(output, flush_obj(om)).unwrap();
from_buffer(&contents, input_path);
}
19 changes: 19 additions & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,25 @@ impl TypeTree {
TypeTree::SymbolInit(x) => x.curried.clone(),
}
}
pub fn into_init(&self) -> &Initialization {
match self {
TypeTree::ConstInit(x) => x,
_ => panic!("type lang issue, failed into init"),
}
}
pub fn into_data(&self) -> Vec<u8> {
let mut v = vec![];
match self {
TypeTree::U64(x) => {
v.extend_from_slice(&u64::to_ne_bytes(x.clone()));
}
TypeTree::I64(x) => {
v.extend_from_slice(&i64::to_ne_bytes(x.clone()));
}
_ => panic!("type lang issue, failed into data"),
}
return v;
}
pub fn into_declarator(&self) -> &DeclaratorInfo {
match self {
TypeTree::DeclaratorInfo(x) => x,
Expand Down
Loading