travel fn path for compilation
This commit is contained in:
@@ -1,30 +1,33 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::{AddrID, IRLData, IRLFunction, IRLInstruction, IRUInstruction, Namespace, VarID};
|
||||
use crate::ir::{FnID, SymbolSpace};
|
||||
|
||||
use super::{IRLFunction, IRLInstruction, IRUInstruction, Namespace, Symbol, VarID};
|
||||
|
||||
pub struct IRLProgram {
|
||||
pub fns: Vec<IRLFunction>,
|
||||
pub data: Vec<IRLData>,
|
||||
sym_space: SymbolSpace,
|
||||
entry: Symbol,
|
||||
}
|
||||
|
||||
// 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();
|
||||
let data_len = ns.data.len();
|
||||
for (i, d) in ns.data.iter().enumerate() {
|
||||
data.push(IRLData {
|
||||
addr: AddrID(i),
|
||||
data: d.clone(),
|
||||
})
|
||||
}
|
||||
pub fn create(ns: &Namespace) -> Option<Self> {
|
||||
let mut start = None;
|
||||
for (i, f) in ns.fns.iter().enumerate() {
|
||||
let f = f.as_ref().unwrap();
|
||||
let f = f.as_ref()?;
|
||||
if f.name == "start" {
|
||||
start = Some(FnID(i));
|
||||
}
|
||||
}
|
||||
let start = start?;
|
||||
let mut builder = SymbolSpace::with_entries(&[start]);
|
||||
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 stack = HashMap::new();
|
||||
let mut alloc = |i: &VarID| {
|
||||
let mut alloc_stack = |i: &VarID| {
|
||||
if !stack.contains_key(i) {
|
||||
stack.insert(*i, 8);
|
||||
}
|
||||
@@ -32,38 +35,42 @@ impl IRLProgram {
|
||||
for i in &f.instructions {
|
||||
instructions.push(match i {
|
||||
IRUInstruction::Mv { dest, src } => {
|
||||
alloc(dest);
|
||||
alloc_stack(dest);
|
||||
IRLInstruction::Mv {
|
||||
dest: *dest,
|
||||
src: *src,
|
||||
}
|
||||
}
|
||||
IRUInstruction::Ref { dest, src } => {
|
||||
alloc(dest);
|
||||
alloc_stack(dest);
|
||||
IRLInstruction::Ref {
|
||||
dest: *dest,
|
||||
src: *src,
|
||||
}
|
||||
}
|
||||
IRUInstruction::LoadData { dest, src } => {
|
||||
alloc(dest);
|
||||
alloc_stack(dest);
|
||||
let addr = builder.ro_data(src, &ns.data[src.0]);
|
||||
IRLInstruction::LoadAddr {
|
||||
dest: *dest,
|
||||
src: AddrID(src.0),
|
||||
src: addr,
|
||||
}
|
||||
}
|
||||
IRUInstruction::LoadFn { dest, src } => {
|
||||
alloc(dest);
|
||||
alloc_stack(dest);
|
||||
let sym = builder.func(src);
|
||||
IRLInstruction::LoadAddr {
|
||||
dest: *dest,
|
||||
src: AddrID(src.0 + data_len),
|
||||
src: sym,
|
||||
}
|
||||
}
|
||||
IRUInstruction::Call { dest, f, args } => {
|
||||
alloc(dest);
|
||||
alloc_stack(dest);
|
||||
let fid = &ns.fn_map[f];
|
||||
let sym = builder.func(fid);
|
||||
IRLInstruction::Call {
|
||||
dest: *dest,
|
||||
f: AddrID(ns.fn_map[f].0 + data_len),
|
||||
f: sym,
|
||||
args: args.iter().map(|a| (*a, 8)).collect(),
|
||||
}
|
||||
}
|
||||
@@ -74,14 +81,34 @@ impl IRLProgram {
|
||||
IRUInstruction::Ret { src } => IRLInstruction::Ret { src: *src },
|
||||
});
|
||||
}
|
||||
fns.push(IRLFunction {
|
||||
name: f.name.clone(),
|
||||
addr: AddrID(i + data_len),
|
||||
instructions,
|
||||
args: f.args.iter().map(|a| (*a, 8)).collect(),
|
||||
stack,
|
||||
})
|
||||
builder.write_fn(
|
||||
sym,
|
||||
IRLFunction {
|
||||
name: f.name.clone(),
|
||||
instructions,
|
||||
args: f.args.iter().map(|a| (*a, 8)).collect(),
|
||||
stack,
|
||||
},
|
||||
);
|
||||
}
|
||||
Self { fns, data }
|
||||
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());
|
||||
Some(Self { sym_space, entry })
|
||||
}
|
||||
|
||||
pub fn entry(&self) -> Symbol {
|
||||
self.entry
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for IRLProgram {
|
||||
type Target = SymbolSpace;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.sym_space
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user