huge refactor, can now define structs out of order
This commit is contained in:
@@ -1,41 +1,38 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::ir::{AsmBlockArgType, IRUFunction, IRUInstrInst, Size, SymbolSpace, VarOffset};
|
||||
use crate::ir::{AsmBlockArgType, UInstrInst, Size, SymbolSpace, UFunc, VarOffset};
|
||||
|
||||
use super::{
|
||||
IRLFunction, IRLInstruction, IRUInstruction, IRUProgram, Len, Symbol, SymbolSpaceBuilder, Type,
|
||||
IRLFunction, LInstruction, Len, Symbol, SymbolSpaceBuilder, Type, UInstruction, UProgram,
|
||||
VarID,
|
||||
};
|
||||
|
||||
pub struct IRLProgram {
|
||||
pub struct LProgram {
|
||||
sym_space: SymbolSpace,
|
||||
entry: Symbol,
|
||||
}
|
||||
|
||||
// NOTE: there are THREE places here where I specify size (8)
|
||||
|
||||
impl IRLProgram {
|
||||
pub fn create(p: &IRUProgram) -> Result<Self, String> {
|
||||
let mut start = None;
|
||||
for (i, f) in p.iter_fns() {
|
||||
if f.name == "start" {
|
||||
start = Some(i);
|
||||
}
|
||||
}
|
||||
let start = start.ok_or("no start method found")?;
|
||||
impl LProgram {
|
||||
pub fn create(p: &UProgram) -> Result<Self, String> {
|
||||
let start = p
|
||||
.names
|
||||
.lookup::<UFunc>("start")
|
||||
.ok_or("no start method found")?;
|
||||
let mut ssbuilder = SymbolSpaceBuilder::with_entries(&[start]);
|
||||
let entry = ssbuilder.func(&start);
|
||||
while let Some((sym, i)) = ssbuilder.pop_fn() {
|
||||
let f = p.fns[i.0].as_ref().unwrap();
|
||||
let mut fbuilder = IRLFunctionBuilder::new(p, &mut ssbuilder);
|
||||
let mut fbuilder = LFunctionBuilder::new(p, &mut ssbuilder);
|
||||
for i in &f.instructions {
|
||||
fbuilder.insert_instr(i);
|
||||
}
|
||||
if fbuilder.instrs.last().is_none_or(|i| !i.is_ret()) {
|
||||
fbuilder.instrs.push(IRLInstruction::Ret { src: None });
|
||||
fbuilder.instrs.push(LInstruction::Ret { src: None });
|
||||
}
|
||||
let res = fbuilder.finish(f);
|
||||
ssbuilder.write_fn(sym, res, Some(f.name.clone()));
|
||||
ssbuilder.write_fn(sym, res, Some(p.names.get(i).to_string()));
|
||||
}
|
||||
let sym_space = ssbuilder.finish().expect("we failed the mission");
|
||||
Ok(Self { sym_space, entry })
|
||||
@@ -46,10 +43,10 @@ impl IRLProgram {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IRLFunctionBuilder<'a> {
|
||||
program: &'a IRUProgram,
|
||||
pub struct LFunctionBuilder<'a> {
|
||||
program: &'a UProgram,
|
||||
builder: &'a mut SymbolSpaceBuilder,
|
||||
instrs: Vec<IRLInstruction>,
|
||||
instrs: Vec<LInstruction>,
|
||||
stack: HashMap<VarID, Size>,
|
||||
subvar_map: HashMap<VarID, VarOffset>,
|
||||
makes_call: bool,
|
||||
@@ -62,8 +59,8 @@ pub struct LoopCtx {
|
||||
bot: Symbol,
|
||||
}
|
||||
|
||||
impl<'a> IRLFunctionBuilder<'a> {
|
||||
pub fn new(program: &'a IRUProgram, builder: &'a mut SymbolSpaceBuilder) -> Self {
|
||||
impl<'a> LFunctionBuilder<'a> {
|
||||
pub fn new(program: &'a UProgram, builder: &'a mut SymbolSpaceBuilder) -> Self {
|
||||
Self {
|
||||
instrs: Vec::new(),
|
||||
stack: HashMap::new(),
|
||||
@@ -92,50 +89,56 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
self.subvar_map.insert(i, off);
|
||||
}
|
||||
}
|
||||
pub fn insert_instr(&mut self, i: &IRUInstrInst) -> Option<Option<String>> {
|
||||
pub fn insert_instr(&mut self, i: &UInstrInst) -> Option<Option<String>> {
|
||||
match &i.i {
|
||||
IRUInstruction::Mv { dest, src } => {
|
||||
UInstruction::Mv { dest, src } => {
|
||||
self.alloc_stack(dest.id)?;
|
||||
self.map_subvar(src.id);
|
||||
self.instrs.push(IRLInstruction::Mv {
|
||||
self.instrs.push(LInstruction::Mv {
|
||||
dest: dest.id,
|
||||
dest_offset: 0,
|
||||
src: src.id,
|
||||
src_offset: 0,
|
||||
});
|
||||
}
|
||||
IRUInstruction::Ref { dest, src } => {
|
||||
UInstruction::Ref { dest, src } => {
|
||||
self.alloc_stack(dest.id)?;
|
||||
self.map_subvar(src.id);
|
||||
self.instrs.push(IRLInstruction::Ref {
|
||||
self.instrs.push(LInstruction::Ref {
|
||||
dest: dest.id,
|
||||
src: src.id,
|
||||
});
|
||||
}
|
||||
IRUInstruction::LoadData { dest, src } => {
|
||||
UInstruction::LoadData { dest, src } => {
|
||||
self.alloc_stack(dest.id)?;
|
||||
let data = &self.program.data[src.0];
|
||||
let ddef = self.program.get_data(*src);
|
||||
let sym = self.builder.ro_data(src, data, Some(ddef.label.clone()));
|
||||
self.instrs.push(IRLInstruction::LoadData {
|
||||
let data = self.program.expect(*src);
|
||||
let sym = self.builder.ro_data(
|
||||
src,
|
||||
&data.content,
|
||||
Some(self.program.names.get(dest.id).to_string()),
|
||||
);
|
||||
self.instrs.push(LInstruction::LoadData {
|
||||
dest: dest.id,
|
||||
offset: 0,
|
||||
len: data.len() as Len,
|
||||
len: data.content.len() as Len,
|
||||
src: sym,
|
||||
});
|
||||
}
|
||||
IRUInstruction::LoadSlice { dest, src } => {
|
||||
UInstruction::LoadSlice { dest, src } => {
|
||||
self.alloc_stack(dest.id)?;
|
||||
let data = &self.program.data[src.0];
|
||||
let def = self.program.get_data(*src);
|
||||
let Type::Array(_, len) = &def.ty else {
|
||||
let data = self.program.expect(*src);
|
||||
let Type::Array(_, len) = &data.ty else {
|
||||
return Some(Some(format!(
|
||||
"tried to load {} as slice",
|
||||
self.program.type_name(&def.ty)
|
||||
self.program.type_name(&data.ty)
|
||||
)));
|
||||
};
|
||||
let sym = self.builder.ro_data(src, data, Some(def.label.clone()));
|
||||
self.instrs.push(IRLInstruction::LoadAddr {
|
||||
let sym = self.builder.ro_data(
|
||||
src,
|
||||
&data.content,
|
||||
Some(self.program.names.get(dest.id).to_string()),
|
||||
);
|
||||
self.instrs.push(LInstruction::LoadAddr {
|
||||
dest: dest.id,
|
||||
offset: 0,
|
||||
src: sym,
|
||||
@@ -144,23 +147,23 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
let sym = self
|
||||
.builder
|
||||
.anon_ro_data(&(*len as u64).to_le_bytes(), Some(format!("len: {}", len)));
|
||||
self.instrs.push(IRLInstruction::LoadData {
|
||||
self.instrs.push(LInstruction::LoadData {
|
||||
dest: dest.id,
|
||||
offset: 8,
|
||||
len: 8,
|
||||
src: sym,
|
||||
});
|
||||
}
|
||||
IRUInstruction::LoadFn { dest, src } => {
|
||||
UInstruction::LoadFn { dest, src } => {
|
||||
self.alloc_stack(dest.id)?;
|
||||
let sym = self.builder.func(src);
|
||||
self.instrs.push(IRLInstruction::LoadAddr {
|
||||
self.instrs.push(LInstruction::LoadAddr {
|
||||
dest: dest.id,
|
||||
offset: 0,
|
||||
src: sym,
|
||||
});
|
||||
}
|
||||
IRUInstruction::Call { dest, f, args } => {
|
||||
UInstruction::Call { dest, f, args } => {
|
||||
self.alloc_stack(dest.id);
|
||||
self.makes_call = true;
|
||||
let fid = &self.program.fn_map[&f.id];
|
||||
@@ -171,7 +174,7 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let call = IRLInstruction::Call {
|
||||
let call = LInstruction::Call {
|
||||
dest,
|
||||
f: sym,
|
||||
args: args
|
||||
@@ -184,7 +187,7 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
};
|
||||
self.instrs.push(call);
|
||||
}
|
||||
IRUInstruction::AsmBlock { instructions, args } => {
|
||||
UInstruction::AsmBlock { instructions, args } => {
|
||||
let mut inputs = Vec::new();
|
||||
let mut outputs = Vec::new();
|
||||
for a in args {
|
||||
@@ -199,15 +202,15 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
self.instrs.push(IRLInstruction::AsmBlock {
|
||||
self.instrs.push(LInstruction::AsmBlock {
|
||||
instructions: instructions.clone(),
|
||||
inputs,
|
||||
outputs,
|
||||
})
|
||||
}
|
||||
IRUInstruction::Ret { src } => {
|
||||
UInstruction::Ret { src } => {
|
||||
self.map_subvar(src.id);
|
||||
self.instrs.push(IRLInstruction::Ret {
|
||||
self.instrs.push(LInstruction::Ret {
|
||||
src: if self.program.size_of_var(src.id).expect("unsized var") == 0 {
|
||||
None
|
||||
} else {
|
||||
@@ -215,9 +218,9 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
},
|
||||
})
|
||||
}
|
||||
IRUInstruction::Construct { dest, fields } => {
|
||||
UInstruction::Construct { dest, fields } => {
|
||||
self.alloc_stack(dest.id)?;
|
||||
let ty = &self.program.get_var(dest.id).ty;
|
||||
let ty = &self.program.expect(dest.id).ty;
|
||||
let &Type::Struct { id, ref args } = ty else {
|
||||
return Some(Some(format!(
|
||||
"Failed to contruct type {}",
|
||||
@@ -226,7 +229,7 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
};
|
||||
for (&fid, var) in fields {
|
||||
self.map_subvar(var.id);
|
||||
self.instrs.push(IRLInstruction::Mv {
|
||||
self.instrs.push(LInstruction::Mv {
|
||||
dest: dest.id,
|
||||
src: var.id,
|
||||
dest_offset: self.program.field_offset(id, fid).expect("field offset"),
|
||||
@@ -234,19 +237,19 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
})
|
||||
}
|
||||
}
|
||||
IRUInstruction::If { cond, body } => {
|
||||
UInstruction::If { cond, body } => {
|
||||
self.map_subvar(cond.id);
|
||||
let sym = self.builder.reserve();
|
||||
self.instrs.push(IRLInstruction::Branch {
|
||||
self.instrs.push(LInstruction::Branch {
|
||||
to: *sym,
|
||||
cond: cond.id,
|
||||
});
|
||||
for i in body {
|
||||
self.insert_instr(i);
|
||||
}
|
||||
self.instrs.push(IRLInstruction::Mark(*sym));
|
||||
self.instrs.push(LInstruction::Mark(*sym));
|
||||
}
|
||||
IRUInstruction::Loop { body } => {
|
||||
UInstruction::Loop { body } => {
|
||||
let top = self.builder.reserve();
|
||||
let bot = self.builder.reserve();
|
||||
let old = self.loopp;
|
||||
@@ -254,21 +257,21 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
bot: *bot,
|
||||
top: *top,
|
||||
});
|
||||
self.instrs.push(IRLInstruction::Mark(*top));
|
||||
self.instrs.push(LInstruction::Mark(*top));
|
||||
for i in body {
|
||||
self.insert_instr(i);
|
||||
}
|
||||
self.instrs.push(IRLInstruction::Jump(*top));
|
||||
self.instrs.push(IRLInstruction::Mark(*bot));
|
||||
self.instrs.push(LInstruction::Jump(*top));
|
||||
self.instrs.push(LInstruction::Mark(*bot));
|
||||
self.loopp = old;
|
||||
}
|
||||
IRUInstruction::Break => {
|
||||
self.instrs.push(IRLInstruction::Jump(
|
||||
UInstruction::Break => {
|
||||
self.instrs.push(LInstruction::Jump(
|
||||
self.loopp.expect("Tried to break outside of loop").bot,
|
||||
));
|
||||
}
|
||||
IRUInstruction::Continue => {
|
||||
self.instrs.push(IRLInstruction::Jump(
|
||||
UInstruction::Continue => {
|
||||
self.instrs.push(LInstruction::Jump(
|
||||
self.loopp.expect("Tried to break outside of loop").top,
|
||||
));
|
||||
}
|
||||
@@ -276,7 +279,7 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
Some(None)
|
||||
}
|
||||
|
||||
pub fn finish(self, f: &IRUFunction) -> IRLFunction {
|
||||
pub fn finish(self, f: &UFunc) -> IRLFunction {
|
||||
IRLFunction {
|
||||
instructions: self.instrs,
|
||||
makes_call: self.makes_call,
|
||||
@@ -292,7 +295,7 @@ impl<'a> IRLFunctionBuilder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for IRLProgram {
|
||||
impl std::ops::Deref for LProgram {
|
||||
type Target = SymbolSpace;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
|
||||
Reference in New Issue
Block a user