Commit 4ae9559a authored by qinsoon's avatar qinsoon

gonna start writing the compiler

parent 17f2c310
......@@ -218,8 +218,8 @@ pub enum Terminal {
#[derive(Clone)]
pub struct MuConstant{
ty: P<MuType_>,
val: Constant
pub ty: P<MuType_>,
pub val: Constant
}
pub struct MuFunction {
......@@ -229,19 +229,6 @@ pub struct MuFunction {
pub blocks: Vec<(MuTag, Block)>
}
pub fn declare_const(const_name: MuTag, ty: P<MuType_>, val: Constant) -> Value {
Value::Constant(MuConstant{ty: ty, val: val})
}
pub fn declare_type(type_name: MuTag, ty: P<MuType_>) -> P<MuType_> {
ty
}
pub fn declare_func_sig(sig_name: MuTag, ret_tys: Vec<P<MuType_>>, arg_tys: Vec<P<MuType_>>) -> MuFuncSig {
MuFuncSig::new(ret_tys, arg_tys)
}
pub fn declare_func (fn_name: MuTag, sig: P<MuFuncSig>, entry: MuTag, blocks: Vec<(MuTag, Block)>) -> MuFunction {
MuFunction{fn_name: fn_name, sig: sig, entry: entry, blocks: blocks}
}
#[derive(Copy, Clone)]
pub enum BinOp {
// Int(n) BinOp Int(n) -> Int(n)
......
......@@ -39,7 +39,6 @@
use std::fmt::{self, Display, Debug};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::ptr;
/// An owned smart pointer.
pub struct P<T> {
......
......@@ -255,12 +255,6 @@ macro_rules! is_type (
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MuFuncSig {
ret_tys : Vec<P<MuType_>>,
arg_tys: Vec<P<MuType_>>
}
impl MuFuncSig {
pub fn new(ret_tys: Vec<P<MuType_>>, arg_tys: Vec<P<MuType_>>) -> MuFuncSig {
MuFuncSig {ret_tys : ret_tys, arg_tys: arg_tys}
}
pub ret_tys : Vec<P<MuType_>>,
pub arg_tys: Vec<P<MuType_>>
}
\ No newline at end of file
......@@ -2,6 +2,8 @@
extern crate lazy_static;
pub mod ast;
pub mod vm;
pub mod compiler;
#[allow(dead_code)]
fn main() {
......
use std::collections::HashMap;
use ast::ptr::P;
use ast::ir::*;
use ast::types::*;
pub struct VMContext {
constants: HashMap<MuTag, P<Value>>,
types: HashMap<MuTag, P<MuType_>>,
func_sigs: HashMap<MuTag, P<MuFuncSig>>,
funcs: HashMap<MuTag, MuFunction>
}
impl VMContext {
pub fn new() -> VMContext {
VMContext {
constants: HashMap::new(),
types: HashMap::new(),
func_sigs: HashMap::new(),
funcs: HashMap::new()
}
}
pub fn declare_const(&mut self, const_name: MuTag, ty: P<MuType_>, val: Constant) -> P<Value> {
debug_assert!(!self.constants.contains_key(const_name));
let ret = P(Value::Constant(MuConstant{ty: ty, val: val}));
self.constants.insert(const_name, ret.clone());
ret
}
pub fn declare_type(&mut self, type_name: MuTag, ty: P<MuType_>) -> P<MuType_> {
debug_assert!(!self.types.contains_key(type_name));
self.types.insert(type_name, ty.clone());
ty
}
pub fn declare_func_sig(&mut self, sig_name: MuTag, ret_tys: Vec<P<MuType_>>, arg_tys: Vec<P<MuType_>>) -> P<MuFuncSig> {
debug_assert!(!self.func_sigs.contains_key(sig_name));
let ret = P(MuFuncSig{ret_tys: ret_tys, arg_tys: arg_tys});
self.func_sigs.insert(sig_name, ret.clone());
ret
}
pub fn declare_func (&mut self, fn_name: MuTag, sig: P<MuFuncSig>, entry: MuTag, blocks: Vec<(MuTag, Block)>) {
debug_assert!(!self.funcs.contains_key(fn_name));
let ret = MuFunction{fn_name: fn_name, sig: sig, entry: entry, blocks: blocks};
self.funcs.insert(fn_name, ret);
}
}
\ No newline at end of file
pub mod context;
\ No newline at end of file
......@@ -5,10 +5,18 @@ mod test_ir {
use mu::ast::types::*;
use mu::ast::ir::*;
use mu::ast::ptr::*;
use mu::vm::context::*;
#[test]
#[allow(unused_variables)]
fn factorial() {
fn test_factorial() {
let vm = factorial();
}
#[allow(unused_variables)]
fn factorial() -> VMContext {
let mut vm = VMContext::new();
// .typedef @int_64 = int<64>
// .typedef @int_1 = int<1>
// .typedef @float = float
......@@ -16,19 +24,19 @@ mod test_ir {
// .typedef @void = void
// .typedef @int_8 = int<8>
// .typedef @int_32 = int<32>
let type_def_int64 = declare_type("int_64", P(MuType_::int(64)));
let type_def_int1 = declare_type("int_1", P(MuType_::int(1)));
let type_def_float = declare_type("float", P(MuType_::float()));
let type_def_double = declare_type("double", P(MuType_::double()));
let type_def_void = declare_type("void", P(MuType_::void()));
let type_def_int8 = declare_type("int8", P(MuType_::int(8)));
let type_def_int32 = declare_type("int32", P(MuType_::int(32)));
let type_def_int64 = vm.declare_type("int_64", P(MuType_::int(64)));
let type_def_int1 = vm.declare_type("int_1", P(MuType_::int(1)));
let type_def_float = vm.declare_type("float", P(MuType_::float()));
let type_def_double = vm.declare_type("double", P(MuType_::double()));
let type_def_void = vm.declare_type("void", P(MuType_::void()));
let type_def_int8 = vm.declare_type("int8", P(MuType_::int(8)));
let type_def_int32 = vm.declare_type("int32", P(MuType_::int(32)));
// .const @int_64_1 <@int_64> = 1
let const_def_int64_1 = P(declare_const("int64_1", type_def_int64.clone(), Constant::Int(64, 1)));
let const_def_int64_1 = vm.declare_const("int64_1", type_def_int64.clone(), Constant::Int(64, 1));
// .funcsig @fac_sig = (@int_64) -> (@int_64)
let fac_sig = P(declare_func_sig("fac_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]));
let fac_sig = vm.declare_func_sig("fac_sig", vec![type_def_int64.clone()], vec![type_def_int64.clone()]);
// .funcdef @fac VERSION @fac_v1 <@fac_sig>
let fac_func_ref = P(MuType_::funcref(fac_sig.clone()));
......@@ -148,12 +156,14 @@ mod test_ir {
blk_1_cont.set_content(blk_1_cont_content);
// wrap into a function
let func_fac = declare_func("fac", fac_sig.clone(), "blk_0", vec![
vm.declare_func("fac", fac_sig.clone(), "blk_0", vec![
("blk_0", blk_0),
("blk_1", blk_1),
("blk_1_cont", blk_1_cont),
("blk_2", blk_2)
]
);
vm
}
}
\ No newline at end of file
......@@ -66,7 +66,7 @@ mod tests {
let t14 = MuType_::vector(types[0].clone(), 5);
types.push(P(t14));
let sig = P(MuFuncSig::new(vec![types[10].clone()], vec![types[0].clone(), types[0].clone()]));
let sig = P(MuFuncSig{ret_tys: vec![types[10].clone()], arg_tys: vec![types[0].clone(), types[0].clone()]});
let t15 = MuType_::funcref(sig.clone());
types.push(P(t15));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment