travel fn path for compilation
This commit is contained in:
@@ -8,8 +8,6 @@ pub struct VarID(pub usize);
|
||||
pub struct FnID(pub usize);
|
||||
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
||||
pub struct DataID(pub usize);
|
||||
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
||||
pub struct AddrID(pub usize);
|
||||
|
||||
impl Debug for VarID {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@@ -34,9 +32,3 @@ impl Debug for DataID {
|
||||
write!(f, "data{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for AddrID {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "@{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
use super::AddrID;
|
||||
|
||||
pub struct IRLData {
|
||||
pub addr: AddrID,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
@@ -6,7 +6,6 @@ use std::collections::HashMap;
|
||||
#[derive(Debug)]
|
||||
pub struct IRLFunction {
|
||||
pub name: String,
|
||||
pub addr: AddrID,
|
||||
pub instructions: Vec<IRLInstruction>,
|
||||
pub stack: HashMap<VarID, usize>,
|
||||
pub args: Vec<(VarID, usize)>,
|
||||
@@ -24,11 +23,11 @@ pub enum IRLInstruction {
|
||||
},
|
||||
LoadAddr {
|
||||
dest: VarID,
|
||||
src: AddrID,
|
||||
src: Symbol,
|
||||
},
|
||||
Call {
|
||||
dest: VarID,
|
||||
f: AddrID,
|
||||
f: Symbol,
|
||||
args: Vec<(VarID, usize)>,
|
||||
},
|
||||
AsmBlock {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
mod func;
|
||||
mod data;
|
||||
mod program;
|
||||
mod symbol;
|
||||
|
||||
pub use func::*;
|
||||
pub use data::*;
|
||||
pub use program::*;
|
||||
pub use symbol::*;
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
122
src/ir/lower/symbol.rs
Normal file
122
src/ir/lower/symbol.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::{DataID, FnID, IRLFunction};
|
||||
|
||||
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
|
||||
pub struct Symbol(usize);
|
||||
/// intentionally does not have copy or clone;
|
||||
/// this should only be consumed once
|
||||
pub struct WritableSymbol(Symbol);
|
||||
|
||||
impl std::ops::Deref for WritableSymbol {
|
||||
type Target = Symbol;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SymbolSpace {
|
||||
ro_data: Vec<(Symbol, Vec<u8>)>,
|
||||
fns: Vec<(Symbol, IRLFunction)>,
|
||||
}
|
||||
|
||||
pub struct SymbolSpaceBuilder {
|
||||
symbols: usize,
|
||||
unwritten_fns: Vec<(WritableSymbol, FnID)>,
|
||||
fn_map: HashMap<FnID, Symbol>,
|
||||
data_map: HashMap<DataID, Symbol>,
|
||||
ro_data: Vec<(Symbol, Vec<u8>)>,
|
||||
fns: Vec<(Symbol, IRLFunction)>,
|
||||
}
|
||||
|
||||
impl SymbolSpace {
|
||||
pub fn with_entries(entries: &[FnID]) -> SymbolSpaceBuilder {
|
||||
let mut s = SymbolSpaceBuilder {
|
||||
symbols: 0,
|
||||
unwritten_fns: Vec::new(),
|
||||
fn_map: HashMap::new(),
|
||||
data_map: HashMap::new(),
|
||||
ro_data: Vec::new(),
|
||||
fns: Vec::new(),
|
||||
};
|
||||
for e in entries {
|
||||
s.func(e);
|
||||
}
|
||||
s
|
||||
}
|
||||
pub fn ro_data(&self) -> &[(Symbol, Vec<u8>)] {
|
||||
&self.ro_data
|
||||
}
|
||||
pub fn fns(&self) -> &[(Symbol, IRLFunction)] {
|
||||
&self.fns
|
||||
}
|
||||
}
|
||||
|
||||
impl SymbolSpaceBuilder {
|
||||
pub fn pop_fn(&mut self) -> Option<(WritableSymbol, FnID)> {
|
||||
self.unwritten_fns.pop()
|
||||
}
|
||||
pub fn ro_data(&mut self, id: &DataID, data: &Vec<u8>) -> Symbol {
|
||||
match self.data_map.get(id) {
|
||||
Some(s) => *s,
|
||||
None => {
|
||||
let sym = self.reserve();
|
||||
self.data_map.insert(*id, *sym);
|
||||
self.write_ro_data(sym, data.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn func(&mut self, id: &FnID) -> Symbol {
|
||||
match self.fn_map.get(id) {
|
||||
Some(s) => *s,
|
||||
None => {
|
||||
let wsym = self.reserve();
|
||||
let sym = *wsym;
|
||||
self.unwritten_fns.push((wsym, *id));
|
||||
self.fn_map.insert(*id, sym);
|
||||
sym
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn write_ro_data(&mut self, sym: WritableSymbol, data: Vec<u8>) -> Symbol {
|
||||
let data = data.into();
|
||||
self.ro_data.push((*sym, data));
|
||||
*sym
|
||||
}
|
||||
pub fn write_fn(&mut self, sym: WritableSymbol, func: IRLFunction) -> Symbol {
|
||||
self.fns.push((*sym, func));
|
||||
*sym
|
||||
}
|
||||
pub fn reserve(&mut self) -> WritableSymbol {
|
||||
let val = self.symbols;
|
||||
self.symbols += 1;
|
||||
WritableSymbol(Symbol(val))
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.fns.len() + self.ro_data.len()
|
||||
}
|
||||
pub fn finish(self) -> Option<SymbolSpace> {
|
||||
if self.unwritten_fns.is_empty() {
|
||||
Some(SymbolSpace {
|
||||
fns: self.fns,
|
||||
ro_data: self.ro_data,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Symbol {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "@{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for Symbol {
|
||||
type Target = usize;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user