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},
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<u8>, Option<Addr>) {
pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
let mut fns = Vec::new();
let mut data = Vec::new();
for d in program.data {

View File

@@ -12,9 +12,9 @@ mod target;
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 binary = elf::create(compiled, start.expect("no start method found"));
binary

View File

@@ -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<IRLFunction>,
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 {
let mut fns = Vec::new();
let mut data = Vec::new();

View File

@@ -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 {
}
}
}

View File

@@ -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);

View File

@@ -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::<i64>().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)?,
})