questionable refactoring
This commit is contained in:
@@ -5,7 +5,6 @@ use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IRLFunction {
|
||||
pub name: String,
|
||||
pub instructions: Vec<IRLInstruction>,
|
||||
pub stack: HashMap<VarID, Size>,
|
||||
pub args: Vec<(VarID, Size)>,
|
||||
|
||||
@@ -58,7 +58,8 @@ impl IRLProgram {
|
||||
continue;
|
||||
}
|
||||
let data = &p.data[src.0];
|
||||
let sym = builder.ro_data(src, data);
|
||||
let ddef = p.get_data(*src);
|
||||
let sym = builder.ro_data(src, data, Some(ddef.label.clone()));
|
||||
instrs.push(IRLInstruction::LoadData {
|
||||
dest: dest.id,
|
||||
offset: 0,
|
||||
@@ -75,14 +76,14 @@ impl IRLProgram {
|
||||
let Type::Array(ty, len) = &def.ty else {
|
||||
return Err(format!("tried to load {} as slice", p.type_name(&def.ty)));
|
||||
};
|
||||
let sym = builder.ro_data(src, data);
|
||||
let sym = builder.ro_data(src, data, Some(def.label.clone()));
|
||||
instrs.push(IRLInstruction::LoadAddr {
|
||||
dest: dest.id,
|
||||
offset: 0,
|
||||
src: sym,
|
||||
});
|
||||
|
||||
let sym = builder.anon_ro_data(&(*len as u64).to_le_bytes());
|
||||
let sym = builder.anon_ro_data(&(*len as u64).to_le_bytes(), Some(format!("len: {}", len)));
|
||||
instrs.push(IRLInstruction::LoadData {
|
||||
dest: dest.id,
|
||||
offset: 8,
|
||||
@@ -133,7 +134,6 @@ impl IRLProgram {
|
||||
builder.write_fn(
|
||||
sym,
|
||||
IRLFunction {
|
||||
name: f.name.clone(),
|
||||
instructions: instrs,
|
||||
makes_call,
|
||||
args: f
|
||||
@@ -144,14 +144,10 @@ impl IRLProgram {
|
||||
ret_size: p.size_of_type(&f.ret).expect("unsized type"),
|
||||
stack,
|
||||
},
|
||||
Some(f.name.clone()),
|
||||
);
|
||||
}
|
||||
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());
|
||||
Ok(Self { sym_space, entry })
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ impl std::ops::Deref for WritableSymbol {
|
||||
pub struct SymbolSpace {
|
||||
ro_data: Vec<(Symbol, Vec<u8>)>,
|
||||
fns: Vec<(Symbol, IRLFunction)>,
|
||||
labels: Vec<Option<String>>,
|
||||
}
|
||||
|
||||
pub struct SymbolSpaceBuilder {
|
||||
@@ -27,6 +28,7 @@ pub struct SymbolSpaceBuilder {
|
||||
data_map: HashMap<DataID, Symbol>,
|
||||
ro_data: Vec<(Symbol, Vec<u8>)>,
|
||||
fns: Vec<(Symbol, IRLFunction)>,
|
||||
labels: Vec<Option<String>>,
|
||||
}
|
||||
|
||||
impl SymbolSpace {
|
||||
@@ -38,6 +40,7 @@ impl SymbolSpace {
|
||||
data_map: HashMap::new(),
|
||||
ro_data: Vec::new(),
|
||||
fns: Vec::new(),
|
||||
labels: Vec::new(),
|
||||
};
|
||||
for e in entries {
|
||||
s.func(e);
|
||||
@@ -50,23 +53,26 @@ impl SymbolSpace {
|
||||
pub fn fns(&self) -> &[(Symbol, IRLFunction)] {
|
||||
&self.fns
|
||||
}
|
||||
pub fn labels(&self) -> &[Option<String>] {
|
||||
&self.labels
|
||||
}
|
||||
}
|
||||
|
||||
impl SymbolSpaceBuilder {
|
||||
pub fn pop_fn(&mut self) -> Option<(WritableSymbol, FnID)> {
|
||||
self.unwritten_fns.pop()
|
||||
}
|
||||
pub fn anon_ro_data(&mut self, data: &[u8]) -> Symbol {
|
||||
pub fn anon_ro_data(&mut self, data: &[u8], label: Option<String>) -> Symbol {
|
||||
let sym = self.reserve();
|
||||
self.write_ro_data(sym, data.to_vec())
|
||||
self.write_ro_data(sym, data.to_vec(), label)
|
||||
}
|
||||
pub fn ro_data(&mut self, id: &DataID, data: &[u8]) -> Symbol {
|
||||
pub fn ro_data(&mut self, id: &DataID, data: &[u8], label: Option<String>) -> 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.to_vec())
|
||||
self.write_ro_data(sym, data.to_vec(), label)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,18 +88,31 @@ impl SymbolSpaceBuilder {
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn write_ro_data(&mut self, sym: WritableSymbol, data: Vec<u8>) -> Symbol {
|
||||
pub fn write_ro_data(
|
||||
&mut self,
|
||||
sym: WritableSymbol,
|
||||
data: Vec<u8>,
|
||||
name: Option<String>,
|
||||
) -> Symbol {
|
||||
let data = data.into();
|
||||
self.ro_data.push((*sym, data));
|
||||
self.labels[sym.0 .0] = name;
|
||||
*sym
|
||||
}
|
||||
pub fn write_fn(&mut self, sym: WritableSymbol, func: IRLFunction) -> Symbol {
|
||||
pub fn write_fn(
|
||||
&mut self,
|
||||
sym: WritableSymbol,
|
||||
func: IRLFunction,
|
||||
name: Option<String>,
|
||||
) -> Symbol {
|
||||
self.fns.push((*sym, func));
|
||||
self.labels[sym.0 .0] = name;
|
||||
*sym
|
||||
}
|
||||
pub fn reserve(&mut self) -> WritableSymbol {
|
||||
let val = self.symbols;
|
||||
self.symbols += 1;
|
||||
self.labels.push(None);
|
||||
WritableSymbol(Symbol(val))
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
@@ -104,6 +123,7 @@ impl SymbolSpaceBuilder {
|
||||
Some(SymbolSpace {
|
||||
fns: self.fns,
|
||||
ro_data: self.ro_data,
|
||||
labels: self.labels,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -29,6 +29,7 @@ pub struct VarDef {
|
||||
pub struct DataDef {
|
||||
pub ty: Type,
|
||||
pub origin: Origin,
|
||||
pub label: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
||||
Reference in New Issue
Block a user