slices (offsets now real in backend)

This commit is contained in:
2024-12-07 23:56:00 -05:00
parent 0e0dbd647d
commit 606cb30c6b
14 changed files with 305 additions and 215 deletions

View File

@@ -2,7 +2,7 @@ use std::collections::HashMap;
use crate::ir::{FnID, SymbolSpace};
use super::{IRLFunction, IRLInstruction, IRUInstruction, Namespace, Symbol, VarID};
use super::{IRLFunction, IRLInstruction, IRUInstruction, Len, Namespace, Symbol, VarID};
pub struct IRLProgram {
sym_space: SymbolSpace,
@@ -14,10 +14,9 @@ pub struct IRLProgram {
impl IRLProgram {
pub fn create(ns: &Namespace) -> Option<Self> {
let mut start = None;
for (i, f) in ns.fns.iter().enumerate() {
let f = f.as_ref()?;
if f.name == "start" {
start = Some(FnID(i));
for (i, f) in ns.iter_fns() {
if f?.name == "start" {
start = Some(i);
}
}
let start = start?;
@@ -25,78 +24,121 @@ impl IRLProgram {
let entry = builder.func(&start);
while let Some((sym, i)) = builder.pop_fn() {
let f = ns.fns[i.0].as_ref().unwrap();
let mut instructions = Vec::new();
let mut instrs = Vec::new();
let mut stack = HashMap::new();
let mut alloc_stack = |i: &VarID| {
if !stack.contains_key(i) {
stack.insert(*i, 8);
}
let mut makes_call = false;
let mut alloc_stack = |i: &VarID| -> bool {
let size = *stack
.entry(*i)
.or_insert(ns.size_of_var(i).expect("unsized type"));
size == 0
};
for i in &f.instructions {
instructions.push(match i {
match i {
IRUInstruction::Mv { dest, src } => {
alloc_stack(dest);
IRLInstruction::Mv {
if alloc_stack(dest) {
continue;
}
instrs.push(IRLInstruction::Mv {
dest: *dest,
src: *src,
}
});
}
IRUInstruction::Ref { dest, src } => {
alloc_stack(dest);
IRLInstruction::Ref {
if alloc_stack(dest) {
continue;
}
instrs.push(IRLInstruction::Ref {
dest: *dest,
src: *src,
}
});
}
IRUInstruction::LoadData { dest, src } => {
alloc_stack(dest);
let addr = builder.ro_data(src, &ns.data[src.0]);
IRLInstruction::LoadAddr {
dest: *dest,
src: addr,
if alloc_stack(dest) {
continue;
}
let data = &ns.data[src.0];
let sym = builder.ro_data(src, data);
instrs.push(IRLInstruction::LoadData {
dest: *dest,
offset: 0,
len: data.len() as Len,
src: sym,
});
}
IRUInstruction::LoadSlice { dest, src, len } => {
if alloc_stack(dest) {
continue;
}
let sym = builder.ro_data(src, &ns.data[src.0]);
instrs.push(IRLInstruction::LoadAddr {
dest: *dest,
offset: 0,
src: sym,
});
let sym = builder.anon_ro_data(&(*len as u64).to_le_bytes());
instrs.push(IRLInstruction::LoadData {
dest: *dest,
offset: 8,
len: 8,
src: sym,
});
}
IRUInstruction::LoadFn { dest, src } => {
alloc_stack(dest);
let sym = builder.func(src);
IRLInstruction::LoadAddr {
dest: *dest,
src: sym,
if alloc_stack(dest) {
continue;
}
let sym = builder.func(src);
instrs.push(IRLInstruction::LoadAddr {
dest: *dest,
offset: 0,
src: sym,
});
}
IRUInstruction::Call { dest, f, args } => {
alloc_stack(dest);
makes_call = true;
let fid = &ns.fn_map[f];
let sym = builder.func(fid);
IRLInstruction::Call {
instrs.push(IRLInstruction::Call {
dest: *dest,
f: sym,
args: args.iter().map(|a| (*a, 8)).collect(),
}
args: args
.iter()
.map(|a| (*a, ns.size_of_var(a).expect("unsized type")))
.collect(),
});
}
IRUInstruction::AsmBlock { instructions, args } => IRLInstruction::AsmBlock {
instructions: instructions.clone(),
args: args.clone(),
},
IRUInstruction::Ret { src } => IRLInstruction::Ret { src: *src },
});
IRUInstruction::AsmBlock { instructions, args } => {
instrs.push(IRLInstruction::AsmBlock {
instructions: instructions.clone(),
args: args.clone(),
})
}
IRUInstruction::Ret { src } => instrs.push(IRLInstruction::Ret { src: *src }),
};
}
builder.write_fn(
sym,
IRLFunction {
name: f.name.clone(),
instructions,
args: f.args.iter().map(|a| (*a, 8)).collect(),
instructions: instrs,
makes_call,
args: f
.args
.iter()
.map(|a| (*a, ns.size_of_var(a).expect("unsized type")))
.collect(),
stack,
},
);
}
let sym_space = builder.finish().expect("we failed the mission");
println!("fns:");
for (a, f) in sym_space.fns() {
println!(" {:?}: {}", a, f.name);
}
println!("datas: {}", sym_space.ro_data().len());
// println!("fns:");
// for (a, f) in sym_space.fns() {
// println!(" {:?}: {}", a, f.name);
// }
// println!("datas: {}", sym_space.ro_data().len());
Some(Self { sym_space, entry })
}