diff --git a/src/compiler/arch/riscv64/compile.rs b/src/compiler/arch/riscv64/compile.rs index 6f88535..97be242 100644 --- a/src/compiler/arch/riscv64/compile.rs +++ b/src/compiler/arch/riscv64/compile.rs @@ -4,13 +4,13 @@ use crate::{ compiler::{arch::riscv64::Reg, create_program, Addr}, ir::{ arch::riscv64::{RV64Instruction as AI, RegRef}, - IRLInstruction as IRI, Program, + IRLInstruction as IRI, IRLProgram, }, }; use super::{LinkerInstruction as LI, *}; -pub fn compile(program: Program) -> (Vec, Option) { +pub fn compile(program: IRLProgram) -> (Vec, Option) { let mut fns = Vec::new(); let mut data = Vec::new(); for d in program.data { diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 2b95d2e..980feeb 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -12,9 +12,9 @@ mod target; pub use program::*; -use crate::ir::Program; +use crate::ir::IRLProgram; -pub fn compile(program: Program) -> Vec { +pub fn compile(program: IRLProgram) -> Vec { let (compiled, start) = arch::riscv64::compile(program); let binary = elf::create(compiled, start.expect("no start method found")); binary diff --git a/src/ir/lower/program.rs b/src/ir/lower/program.rs index 6e10394..df7b6af 100644 --- a/src/ir/lower/program.rs +++ b/src/ir/lower/program.rs @@ -2,12 +2,14 @@ use std::collections::HashMap; use super::{AddrID, IRLData, IRLFunction, IRLInstruction, IRUInstruction, Namespace, VarID}; -pub struct Program { +pub struct IRLProgram { pub fns: Vec, pub data: Vec, } -impl Program { +// NOTE: there are THREE places here where I specify size (8) + +impl IRLProgram { pub fn create(ns: &Namespace) -> Self { let mut fns = Vec::new(); let mut data = Vec::new(); diff --git a/src/ir/upper/namespace.rs b/src/ir/upper/namespace.rs index 178b129..a83976b 100644 --- a/src/ir/upper/namespace.rs +++ b/src/ir/upper/namespace.rs @@ -53,8 +53,8 @@ impl Namespace { pub fn get_fn(&self, id: FnID) -> &FnDef { &self.fn_defs[id.0] } - pub fn get_fn_var(&self, id: VarID) -> &FnDef { - &self.fn_defs[self.fn_map[&id].0] + pub fn get_fn_var(&self, id: VarID) -> Option<&FnDef> { + Some(&self.fn_defs[self.fn_map.get(&id)?.0]) } pub fn get_type(&self, id: TypeID) -> &TypeDef { &self.type_defs[id.0] @@ -102,7 +102,6 @@ impl Namespace { self.fn_defs.push(def); self.fns.push(None); - id } pub fn def_type(&mut self, def: TypeDef) -> TypeID { @@ -230,4 +229,3 @@ impl Idents { } } } - diff --git a/src/main.rs b/src/main.rs index 8bbec6f..86d72b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ #![feature(box_patterns)] #![feature(try_trait_v2)] -use ir::{Namespace, Program}; +use ir::{Namespace, IRLProgram}; use parser::{NodeParsable, PModule, PStatement, ParserCtx}; use std::{ fs::{create_dir_all, OpenOptions}, @@ -43,7 +43,7 @@ fn run_file(file: &str, gdb: bool) { // for def in &namespace.var_defs { // println!("{}: {}", def.name, namespace.type_name(&def.ty)); // } - let program = Program::create(&namespace); + let program = IRLProgram::create(&namespace); let bin = compiler::compile(program); println!("compiled"); save_run(&bin, gdb); diff --git a/src/parser/v3/lower/expr.rs b/src/parser/v3/lower/expr.rs index e90d98d..9f48b42 100644 --- a/src/parser/v3/lower/expr.rs +++ b/src/parser/v3/lower/expr.rs @@ -7,20 +7,20 @@ impl FnLowerable for PExpr { Some(match self { PExpr::Lit(l) => match l.as_ref()? { super::PLiteral::String(s) => { - let dest = ctx.map.temp_var(l.span, Type::Bits(8).arr().rf()); + let dest = ctx.map.temp_var(l.span, Type::Bits(64).arr().rf()); let src = ctx.map.def_data(s.as_bytes().to_vec()); ctx.push(IRUInstruction::LoadData { dest, src }); dest } super::PLiteral::Char(c) => { - let dest = ctx.map.temp_var(l.span, Type::Bits(8).arr().rf()); + let dest = ctx.map.temp_var(l.span, Type::Bits(64).arr().rf()); let src = ctx.map.def_data(c.to_string().as_bytes().to_vec()); ctx.push(IRUInstruction::LoadData { dest, src }); dest } super::PLiteral::Number(n) => { // TODO: temp - let dest = ctx.map.temp_var(l.span, Type::Bits(8).arr().rf()); + let dest = ctx.map.temp_var(l.span, Type::Bits(64)); let src = ctx .map .def_data(n.whole.parse::().unwrap().to_le_bytes().to_vec()); @@ -69,20 +69,27 @@ impl FnLowerable for PExpr { return None; } PExpr::Call(e, args) => { - let f = e.lower(ctx)?; + let fe = e.lower(ctx)?; let mut nargs = Vec::new(); for arg in args.iter() { let arg = arg.lower(ctx)?; nargs.push(arg); } - let temp = ctx.temp(ctx.map.get_fn_var(f).ret.clone()); + let def = ctx.map.get_fn_var(fe); + let ty = match def { + Some(def) => def.ret.clone(), + None => { + ctx.err_at(e.span, format!("Expected function, found {}", ctx.map.type_name(&ctx.map.get_var(fe).ty))); + Type::Error + }, + }; + let temp = ctx.temp(ty); ctx.push(IRUInstruction::Call { dest: temp, - f, + f: fe, args: nargs, }); - // ctx.err(format!("Expected function, found {:?}", f)); - return None; + temp } PExpr::Group(e) => e.lower(ctx)?, })