This commit is contained in:
2024-12-06 20:04:04 -05:00
parent 620c4557e9
commit e63f652eb5
6 changed files with 27 additions and 20 deletions

View File

@@ -4,13 +4,13 @@ use crate::{
compiler::{arch::riscv64::Reg, create_program, Addr}, compiler::{arch::riscv64::Reg, create_program, Addr},
ir::{ ir::{
arch::riscv64::{RV64Instruction as AI, RegRef}, arch::riscv64::{RV64Instruction as AI, RegRef},
IRLInstruction as IRI, Program, IRLInstruction as IRI, IRLProgram,
}, },
}; };
use super::{LinkerInstruction as LI, *}; use super::{LinkerInstruction as LI, *};
pub fn compile(program: Program) -> (Vec<u8>, Option<Addr>) { pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
let mut fns = Vec::new(); let mut fns = Vec::new();
let mut data = Vec::new(); let mut data = Vec::new();
for d in program.data { for d in program.data {

View File

@@ -12,9 +12,9 @@ mod target;
pub use program::*; pub use program::*;
use crate::ir::Program; use crate::ir::IRLProgram;
pub fn compile(program: Program) -> Vec<u8> { pub fn compile(program: IRLProgram) -> Vec<u8> {
let (compiled, start) = arch::riscv64::compile(program); let (compiled, start) = arch::riscv64::compile(program);
let binary = elf::create(compiled, start.expect("no start method found")); let binary = elf::create(compiled, start.expect("no start method found"));
binary binary

View File

@@ -2,12 +2,14 @@ use std::collections::HashMap;
use super::{AddrID, IRLData, IRLFunction, IRLInstruction, IRUInstruction, Namespace, VarID}; use super::{AddrID, IRLData, IRLFunction, IRLInstruction, IRUInstruction, Namespace, VarID};
pub struct Program { pub struct IRLProgram {
pub fns: Vec<IRLFunction>, pub fns: Vec<IRLFunction>,
pub data: Vec<IRLData>, pub data: Vec<IRLData>,
} }
impl Program { // NOTE: there are THREE places here where I specify size (8)
impl IRLProgram {
pub fn create(ns: &Namespace) -> Self { pub fn create(ns: &Namespace) -> Self {
let mut fns = Vec::new(); let mut fns = Vec::new();
let mut data = Vec::new(); let mut data = Vec::new();

View File

@@ -53,8 +53,8 @@ impl Namespace {
pub fn get_fn(&self, id: FnID) -> &FnDef { pub fn get_fn(&self, id: FnID) -> &FnDef {
&self.fn_defs[id.0] &self.fn_defs[id.0]
} }
pub fn get_fn_var(&self, id: VarID) -> &FnDef { pub fn get_fn_var(&self, id: VarID) -> Option<&FnDef> {
&self.fn_defs[self.fn_map[&id].0] Some(&self.fn_defs[self.fn_map.get(&id)?.0])
} }
pub fn get_type(&self, id: TypeID) -> &TypeDef { pub fn get_type(&self, id: TypeID) -> &TypeDef {
&self.type_defs[id.0] &self.type_defs[id.0]
@@ -102,7 +102,6 @@ impl Namespace {
self.fn_defs.push(def); self.fn_defs.push(def);
self.fns.push(None); self.fns.push(None);
id id
} }
pub fn def_type(&mut self, def: TypeDef) -> TypeID { pub fn def_type(&mut self, def: TypeDef) -> TypeID {
@@ -230,4 +229,3 @@ impl Idents {
} }
} }
} }

View File

@@ -1,7 +1,7 @@
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(try_trait_v2)] #![feature(try_trait_v2)]
use ir::{Namespace, Program}; use ir::{Namespace, IRLProgram};
use parser::{NodeParsable, PModule, PStatement, ParserCtx}; use parser::{NodeParsable, PModule, PStatement, ParserCtx};
use std::{ use std::{
fs::{create_dir_all, OpenOptions}, fs::{create_dir_all, OpenOptions},
@@ -43,7 +43,7 @@ fn run_file(file: &str, gdb: bool) {
// for def in &namespace.var_defs { // for def in &namespace.var_defs {
// println!("{}: {}", def.name, namespace.type_name(&def.ty)); // println!("{}: {}", def.name, namespace.type_name(&def.ty));
// } // }
let program = Program::create(&namespace); let program = IRLProgram::create(&namespace);
let bin = compiler::compile(program); let bin = compiler::compile(program);
println!("compiled"); println!("compiled");
save_run(&bin, gdb); save_run(&bin, gdb);

View File

@@ -7,20 +7,20 @@ impl FnLowerable for PExpr {
Some(match self { Some(match self {
PExpr::Lit(l) => match l.as_ref()? { PExpr::Lit(l) => match l.as_ref()? {
super::PLiteral::String(s) => { 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()); let src = ctx.map.def_data(s.as_bytes().to_vec());
ctx.push(IRUInstruction::LoadData { dest, src }); ctx.push(IRUInstruction::LoadData { dest, src });
dest dest
} }
super::PLiteral::Char(c) => { 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()); let src = ctx.map.def_data(c.to_string().as_bytes().to_vec());
ctx.push(IRUInstruction::LoadData { dest, src }); ctx.push(IRUInstruction::LoadData { dest, src });
dest dest
} }
super::PLiteral::Number(n) => { super::PLiteral::Number(n) => {
// TODO: temp // 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 let src = ctx
.map .map
.def_data(n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec()); .def_data(n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec());
@@ -69,20 +69,27 @@ impl FnLowerable for PExpr {
return None; return None;
} }
PExpr::Call(e, args) => { PExpr::Call(e, args) => {
let f = e.lower(ctx)?; let fe = e.lower(ctx)?;
let mut nargs = Vec::new(); let mut nargs = Vec::new();
for arg in args.iter() { for arg in args.iter() {
let arg = arg.lower(ctx)?; let arg = arg.lower(ctx)?;
nargs.push(arg); 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 { ctx.push(IRUInstruction::Call {
dest: temp, dest: temp,
f, f: fe,
args: nargs, args: nargs,
}); });
// ctx.err(format!("Expected function, found {:?}", f)); temp
return None;
} }
PExpr::Group(e) => e.lower(ctx)?, PExpr::Group(e) => e.lower(ctx)?,
}) })