questionable refactoring
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
compiler::program::{Addr, Instr, SymTable},
|
compiler::program::{Addr, Instr, SymTable}, ir::Symbol, util::LabeledFmt
|
||||||
ir::Symbol,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -169,14 +168,29 @@ impl LinkerInstruction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is not even remotely worth it but technically it doesn't use the heap I think xdddddddddd
|
||||||
impl<R: std::fmt::Debug, S: std::fmt::Debug> std::fmt::Debug for LinkerInstruction<R, S> {
|
impl<R: std::fmt::Debug, S: std::fmt::Debug> std::fmt::Debug for LinkerInstruction<R, S> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.fmt_label(f, &|f, s| write!(f, "{s:?}"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DebugInstr<'a, R, S, L: Fn(&mut std::fmt::Formatter<'_>, &S) -> std::fmt::Result> {
|
||||||
|
instr: &'a LinkerInstruction<R, S>,
|
||||||
|
label: &'a L,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: std::fmt::Debug, S: std::fmt::Debug> LabeledFmt<S> for LinkerInstruction<R, S> {
|
||||||
|
fn fmt_label(&self, f: &mut std::fmt::Formatter<'_>, label: &dyn crate::util::Labeler<S>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::ECall => write!(f, "ecall"),
|
Self::ECall => write!(f, "ecall"),
|
||||||
Self::EBreak => write!(f, "ebreak"),
|
Self::EBreak => write!(f, "ebreak"),
|
||||||
Self::Li { dest, imm } => write!(f, "li {dest:?}, {imm:?}"),
|
Self::Li { dest, imm } => write!(f, "li {dest:?}, {imm:?}"),
|
||||||
Self::Mv { dest, src } => write!(f, "mv {dest:?}, {src:?}"),
|
Self::Mv { dest, src } => write!(f, "mv {dest:?}, {src:?}"),
|
||||||
Self::La { dest, src } => write!(f, "la {dest:?}, {src:?}"),
|
Self::La { dest, src } => {
|
||||||
|
write!(f, "la {dest:?}, @")?;
|
||||||
|
label(f, src)
|
||||||
|
},
|
||||||
Self::Load {
|
Self::Load {
|
||||||
width,
|
width,
|
||||||
dest,
|
dest,
|
||||||
@@ -207,8 +221,14 @@ impl<R: std::fmt::Debug, S: std::fmt::Debug> std::fmt::Debug for LinkerInstructi
|
|||||||
imm,
|
imm,
|
||||||
} => write!(f, "{}i {dest:?}, {src:?}, {imm}", opstr(*op, *funct)),
|
} => write!(f, "{}i {dest:?}, {src:?}, {imm}", opstr(*op, *funct)),
|
||||||
Self::Jal { dest, offset } => write!(f, "jal {dest:?}, {offset:?}"),
|
Self::Jal { dest, offset } => write!(f, "jal {dest:?}, {offset:?}"),
|
||||||
Self::Call(s) => write!(f, "call {s:?}"),
|
Self::Call(s) => {
|
||||||
Self::J(s) => write!(f, "j {s:?}"),
|
write!(f, "call ")?;
|
||||||
|
label(f, s)
|
||||||
|
}
|
||||||
|
Self::J(s) => {
|
||||||
|
write!(f, "j ")?;
|
||||||
|
label(f, s)
|
||||||
|
}
|
||||||
Self::Ret => write!(f, "ret"),
|
Self::Ret => write!(f, "ret"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
compiler::{arch::riscv::Reg, create_program, Addr},
|
compiler::{arch::riscv::Reg, debug::DebugInfo, UnlinkedProgram},
|
||||||
ir::{
|
ir::{
|
||||||
arch::riscv64::{RV64Instruction as AI, RegRef},
|
arch::riscv64::{RV64Instruction as AI, RegRef},
|
||||||
IRLInstruction as IRI, IRLProgram, Len, Size,
|
IRLInstruction as IRI, IRLProgram, Len, Size,
|
||||||
@@ -47,9 +47,10 @@ fn mov_mem(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
|
pub fn compile(program: &IRLProgram) -> UnlinkedProgram<LI> {
|
||||||
let mut fns = Vec::new();
|
let mut fns = Vec::new();
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
|
let mut dbg = DebugInfo::new(program.labels().to_vec());
|
||||||
for (sym, d) in program.ro_data() {
|
for (sym, d) in program.ro_data() {
|
||||||
data.push((d.clone(), *sym));
|
data.push((d.clone(), *sym));
|
||||||
}
|
}
|
||||||
@@ -83,7 +84,9 @@ pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
|
|||||||
v.push(LI::sd(ra, stack_ra, sp));
|
v.push(LI::sd(ra, stack_ra, sp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let mut irli = Vec::new();
|
||||||
for i in &f.instructions {
|
for i in &f.instructions {
|
||||||
|
irli.push((v.len(), format!("{i:?}")));
|
||||||
match i {
|
match i {
|
||||||
IRI::Mv { dest, src } => todo!(),
|
IRI::Mv { dest, src } => todo!(),
|
||||||
IRI::Ref { dest, src } => todo!(),
|
IRI::Ref { dest, src } => todo!(),
|
||||||
@@ -212,6 +215,7 @@ pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
dbg.push_fn(irli);
|
||||||
if has_stack {
|
if has_stack {
|
||||||
if let Some(stack_ra) = stack_ra {
|
if let Some(stack_ra) = stack_ra {
|
||||||
v.push(LI::ld(ra, stack_ra, sp));
|
v.push(LI::ld(ra, stack_ra, sp));
|
||||||
@@ -221,5 +225,10 @@ pub fn compile(program: IRLProgram) -> (Vec<u8>, Option<Addr>) {
|
|||||||
v.push(LI::Ret);
|
v.push(LI::Ret);
|
||||||
fns.push((v, *sym));
|
fns.push((v, *sym));
|
||||||
}
|
}
|
||||||
create_program(fns, data, Some(program.entry()), &program)
|
UnlinkedProgram {
|
||||||
|
fns: fns.into_iter().map(|(v, s, ..)| (v, s)).collect(),
|
||||||
|
ro_data: data,
|
||||||
|
start: Some(program.entry()),
|
||||||
|
dbg,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/compiler/debug.rs
Normal file
23
src/compiler/debug.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::ir::Symbol;
|
||||||
|
|
||||||
|
pub struct DebugInfo {
|
||||||
|
pub sym_labels: Vec<Option<String>>,
|
||||||
|
pub ir_lower: Vec<Vec<(usize, String)>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DebugInfo {
|
||||||
|
pub fn new(sym_labels: Vec<Option<String>>) -> Self {
|
||||||
|
Self {
|
||||||
|
ir_lower: Vec::new(),
|
||||||
|
sym_labels,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push_fn(&mut self, instrs: Vec<(usize, String)>) {
|
||||||
|
self.ir_lower.push(instrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sym_label(&self, s: Symbol) -> Option<&String> {
|
||||||
|
self.sym_labels[*s].as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use super::program::Addr;
|
use super::{program::Addr, LinkedProgram};
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct ELF64Header {
|
pub struct ELF64Header {
|
||||||
@@ -102,3 +102,9 @@ pub fn create(program: Vec<u8>, start_offset: Addr) -> Vec<u8> {
|
|||||||
unsafe fn as_u8_slice<T: Sized>(p: &T) -> &[u8] {
|
unsafe fn as_u8_slice<T: Sized>(p: &T) -> &[u8] {
|
||||||
core::slice::from_raw_parts((p as *const T) as *const u8, size_of::<T>())
|
core::slice::from_raw_parts((p as *const T) as *const u8, size_of::<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl LinkedProgram {
|
||||||
|
pub fn to_elf(self) -> Vec<u8> {
|
||||||
|
create(self.code, self.start.expect("no start found"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
pub mod arch;
|
pub mod arch;
|
||||||
|
mod debug;
|
||||||
mod elf;
|
mod elf;
|
||||||
mod program;
|
mod program;
|
||||||
mod target;
|
mod target;
|
||||||
|
|
||||||
|
use arch::riscv;
|
||||||
pub use program::*;
|
pub use program::*;
|
||||||
|
|
||||||
use crate::ir::IRLProgram;
|
use crate::ir::IRLProgram;
|
||||||
|
|
||||||
pub fn compile(program: IRLProgram) -> Vec<u8> {
|
pub fn compile(program: &IRLProgram) -> UnlinkedProgram<riscv::LinkerInstruction> {
|
||||||
let (compiled, start) = arch::riscv::compile(program);
|
arch::riscv::compile(program)
|
||||||
elf::create(compiled, start.expect("no start method found"))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,35 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::ir::{IRLProgram, Symbol};
|
use crate::{
|
||||||
|
ir::Symbol,
|
||||||
|
util::{Labelable, LabeledFmt, Labeler},
|
||||||
|
};
|
||||||
|
|
||||||
pub fn create_program<I: Instr>(
|
use super::debug::DebugInfo;
|
||||||
fns: Vec<(Vec<I>, Symbol)>,
|
|
||||||
ro_data: Vec<(Vec<u8>, Symbol)>,
|
pub struct LinkedProgram {
|
||||||
start: Option<Symbol>,
|
pub code: Vec<u8>,
|
||||||
program: &IRLProgram,
|
pub start: Option<Addr>,
|
||||||
) -> (Vec<u8>, Option<Addr>) {
|
}
|
||||||
|
|
||||||
|
pub struct UnlinkedProgram<I: Instr> {
|
||||||
|
pub fns: Vec<(Vec<I>, Symbol)>,
|
||||||
|
pub ro_data: Vec<(Vec<u8>, Symbol)>,
|
||||||
|
pub start: Option<Symbol>,
|
||||||
|
pub dbg: DebugInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I: Instr> UnlinkedProgram<I> {
|
||||||
|
pub fn link(self) -> LinkedProgram {
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
let mut sym_table = SymTable::new(fns.len() + ro_data.len());
|
let mut sym_table = SymTable::new(self.fns.len() + self.ro_data.len());
|
||||||
let mut missing = HashMap::<Symbol, Vec<(Addr, I)>>::new();
|
let mut missing = HashMap::<Symbol, Vec<(Addr, I)>>::new();
|
||||||
for (val, id) in ro_data {
|
for (val, id) in self.ro_data {
|
||||||
sym_table.insert(id, Addr(data.len() as u64));
|
sym_table.insert(id, Addr(data.len() as u64));
|
||||||
data.extend(val);
|
data.extend(val);
|
||||||
}
|
}
|
||||||
data.resize(data.len() + (4 - data.len() % 4), 0);
|
data.resize(data.len() + (4 - data.len() % 4), 0);
|
||||||
for (fun, id) in fns {
|
for (fun, id) in self.fns {
|
||||||
sym_table.insert(id, Addr(data.len() as u64));
|
sym_table.insert(id, Addr(data.len() as u64));
|
||||||
for i in fun {
|
for i in fun {
|
||||||
let i_pos = Addr(data.len() as u64);
|
let i_pos = Addr(data.len() as u64);
|
||||||
@@ -37,21 +50,14 @@ pub fn create_program<I: Instr>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (s, f) in program.fns() {
|
|
||||||
println!(
|
|
||||||
"{}: {:?}",
|
|
||||||
f.name,
|
|
||||||
sym_table.get(*s).map(|a| {
|
|
||||||
let pos = a.0 + 0x1000 + 0x40 + 0x38;
|
|
||||||
format!("0x{:x}", pos)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
assert!(missing.is_empty());
|
assert!(missing.is_empty());
|
||||||
(
|
LinkedProgram {
|
||||||
data,
|
code: data,
|
||||||
start.map(|s| sym_table.get(s).expect("start symbol doesn't exist")),
|
start: self
|
||||||
)
|
.start
|
||||||
|
.map(|s| sym_table.get(s).expect("start symbol doesn't exist")),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Instr {
|
pub trait Instr {
|
||||||
@@ -83,3 +89,31 @@ impl SymTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<I: Instr + Labelable<Symbol> + LabeledFmt<Symbol>> std::fmt::Debug for UnlinkedProgram<I> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
for ((v, s), irli) in self.fns.iter().zip(&self.dbg.ir_lower) {
|
||||||
|
writeln!(f, "{}:", self.dbg.sym_label(*s).unwrap())?;
|
||||||
|
let mut liter = irli.iter();
|
||||||
|
let mut cur = liter.next();
|
||||||
|
for (i, instr) in v.iter().enumerate() {
|
||||||
|
if let Some(c) = cur {
|
||||||
|
if i == c.0 {
|
||||||
|
writeln!(f, " {}:", c.1)?;
|
||||||
|
cur = liter.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeln!(
|
||||||
|
f,
|
||||||
|
" {:?}",
|
||||||
|
instr.labeled(&|f: &mut std::fmt::Formatter, s: &Symbol| write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
self.dbg.sym_label(*s).unwrap_or(&format!("{:?}", *s))
|
||||||
|
))
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct IRLFunction {
|
pub struct IRLFunction {
|
||||||
pub name: String,
|
|
||||||
pub instructions: Vec<IRLInstruction>,
|
pub instructions: Vec<IRLInstruction>,
|
||||||
pub stack: HashMap<VarID, Size>,
|
pub stack: HashMap<VarID, Size>,
|
||||||
pub args: Vec<(VarID, Size)>,
|
pub args: Vec<(VarID, Size)>,
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ impl IRLProgram {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let data = &p.data[src.0];
|
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 {
|
instrs.push(IRLInstruction::LoadData {
|
||||||
dest: dest.id,
|
dest: dest.id,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
@@ -75,14 +76,14 @@ impl IRLProgram {
|
|||||||
let Type::Array(ty, len) = &def.ty else {
|
let Type::Array(ty, len) = &def.ty else {
|
||||||
return Err(format!("tried to load {} as slice", p.type_name(&def.ty)));
|
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 {
|
instrs.push(IRLInstruction::LoadAddr {
|
||||||
dest: dest.id,
|
dest: dest.id,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
src: sym,
|
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 {
|
instrs.push(IRLInstruction::LoadData {
|
||||||
dest: dest.id,
|
dest: dest.id,
|
||||||
offset: 8,
|
offset: 8,
|
||||||
@@ -133,7 +134,6 @@ impl IRLProgram {
|
|||||||
builder.write_fn(
|
builder.write_fn(
|
||||||
sym,
|
sym,
|
||||||
IRLFunction {
|
IRLFunction {
|
||||||
name: f.name.clone(),
|
|
||||||
instructions: instrs,
|
instructions: instrs,
|
||||||
makes_call,
|
makes_call,
|
||||||
args: f
|
args: f
|
||||||
@@ -144,14 +144,10 @@ impl IRLProgram {
|
|||||||
ret_size: p.size_of_type(&f.ret).expect("unsized type"),
|
ret_size: p.size_of_type(&f.ret).expect("unsized type"),
|
||||||
stack,
|
stack,
|
||||||
},
|
},
|
||||||
|
Some(f.name.clone()),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let sym_space = builder.finish().expect("we failed the mission");
|
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 })
|
Ok(Self { sym_space, entry })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ impl std::ops::Deref for WritableSymbol {
|
|||||||
pub struct SymbolSpace {
|
pub struct SymbolSpace {
|
||||||
ro_data: Vec<(Symbol, Vec<u8>)>,
|
ro_data: Vec<(Symbol, Vec<u8>)>,
|
||||||
fns: Vec<(Symbol, IRLFunction)>,
|
fns: Vec<(Symbol, IRLFunction)>,
|
||||||
|
labels: Vec<Option<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SymbolSpaceBuilder {
|
pub struct SymbolSpaceBuilder {
|
||||||
@@ -27,6 +28,7 @@ pub struct SymbolSpaceBuilder {
|
|||||||
data_map: HashMap<DataID, Symbol>,
|
data_map: HashMap<DataID, Symbol>,
|
||||||
ro_data: Vec<(Symbol, Vec<u8>)>,
|
ro_data: Vec<(Symbol, Vec<u8>)>,
|
||||||
fns: Vec<(Symbol, IRLFunction)>,
|
fns: Vec<(Symbol, IRLFunction)>,
|
||||||
|
labels: Vec<Option<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolSpace {
|
impl SymbolSpace {
|
||||||
@@ -38,6 +40,7 @@ impl SymbolSpace {
|
|||||||
data_map: HashMap::new(),
|
data_map: HashMap::new(),
|
||||||
ro_data: Vec::new(),
|
ro_data: Vec::new(),
|
||||||
fns: Vec::new(),
|
fns: Vec::new(),
|
||||||
|
labels: Vec::new(),
|
||||||
};
|
};
|
||||||
for e in entries {
|
for e in entries {
|
||||||
s.func(e);
|
s.func(e);
|
||||||
@@ -50,23 +53,26 @@ impl SymbolSpace {
|
|||||||
pub fn fns(&self) -> &[(Symbol, IRLFunction)] {
|
pub fn fns(&self) -> &[(Symbol, IRLFunction)] {
|
||||||
&self.fns
|
&self.fns
|
||||||
}
|
}
|
||||||
|
pub fn labels(&self) -> &[Option<String>] {
|
||||||
|
&self.labels
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SymbolSpaceBuilder {
|
impl SymbolSpaceBuilder {
|
||||||
pub fn pop_fn(&mut self) -> Option<(WritableSymbol, FnID)> {
|
pub fn pop_fn(&mut self) -> Option<(WritableSymbol, FnID)> {
|
||||||
self.unwritten_fns.pop()
|
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();
|
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) {
|
match self.data_map.get(id) {
|
||||||
Some(s) => *s,
|
Some(s) => *s,
|
||||||
None => {
|
None => {
|
||||||
let sym = self.reserve();
|
let sym = self.reserve();
|
||||||
self.data_map.insert(*id, *sym);
|
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();
|
let data = data.into();
|
||||||
self.ro_data.push((*sym, data));
|
self.ro_data.push((*sym, data));
|
||||||
|
self.labels[sym.0 .0] = name;
|
||||||
*sym
|
*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.fns.push((*sym, func));
|
||||||
|
self.labels[sym.0 .0] = name;
|
||||||
*sym
|
*sym
|
||||||
}
|
}
|
||||||
pub fn reserve(&mut self) -> WritableSymbol {
|
pub fn reserve(&mut self) -> WritableSymbol {
|
||||||
let val = self.symbols;
|
let val = self.symbols;
|
||||||
self.symbols += 1;
|
self.symbols += 1;
|
||||||
|
self.labels.push(None);
|
||||||
WritableSymbol(Symbol(val))
|
WritableSymbol(Symbol(val))
|
||||||
}
|
}
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
@@ -104,6 +123,7 @@ impl SymbolSpaceBuilder {
|
|||||||
Some(SymbolSpace {
|
Some(SymbolSpace {
|
||||||
fns: self.fns,
|
fns: self.fns,
|
||||||
ro_data: self.ro_data,
|
ro_data: self.ro_data,
|
||||||
|
labels: self.labels,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ pub struct VarDef {
|
|||||||
pub struct DataDef {
|
pub struct DataDef {
|
||||||
pub ty: Type,
|
pub ty: Type,
|
||||||
pub origin: Origin,
|
pub origin: Origin,
|
||||||
|
pub label: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
|||||||
13
src/main.rs
13
src/main.rs
@@ -1,5 +1,6 @@
|
|||||||
#![feature(box_patterns)]
|
#![feature(box_patterns)]
|
||||||
#![feature(try_trait_v2)]
|
#![feature(try_trait_v2)]
|
||||||
|
#![feature(trait_alias)]
|
||||||
|
|
||||||
use ir::{IRLProgram, IRUProgram};
|
use ir::{IRLProgram, IRUProgram};
|
||||||
use parser::{NodeParsable, PModule, PStatement, ParserCtx};
|
use parser::{NodeParsable, PModule, PStatement, ParserCtx};
|
||||||
@@ -21,15 +22,16 @@ use common::*;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let file = std::env::args_os().nth(1);
|
let file = std::env::args_os().nth(1);
|
||||||
let gdb = std::env::args().nth(2).is_some_and(|a| a == "--debug");
|
let gdb = std::env::args().nth(2).is_some_and(|a| a == "--debug");
|
||||||
|
let asm = std::env::args().nth(2).is_some_and(|a| a == "--asm");
|
||||||
if let Some(path) = file {
|
if let Some(path) = file {
|
||||||
let file = std::fs::read_to_string(path).expect("failed to read file");
|
let file = std::fs::read_to_string(path).expect("failed to read file");
|
||||||
run_file(&file, gdb);
|
run_file(&file, gdb, asm);
|
||||||
} else {
|
} else {
|
||||||
run_stdin();
|
run_stdin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_file(file: &str, gdb: bool) {
|
fn run_file(file: &str, gdb: bool, asm: bool) {
|
||||||
let mut ctx = ParserCtx::from(file);
|
let mut ctx = ParserCtx::from(file);
|
||||||
let res = PModule::parse_node(&mut ctx);
|
let res = PModule::parse_node(&mut ctx);
|
||||||
if ctx.output.errs.is_empty() {
|
if ctx.output.errs.is_empty() {
|
||||||
@@ -50,13 +52,18 @@ fn run_file(file: &str, gdb: bool) {
|
|||||||
output.write_for(&mut stdout(), file);
|
output.write_for(&mut stdout(), file);
|
||||||
if output.errs.is_empty() {
|
if output.errs.is_empty() {
|
||||||
let program = IRLProgram::create(&namespace).expect("morir");
|
let program = IRLProgram::create(&namespace).expect("morir");
|
||||||
let bin = compiler::compile(program);
|
let unlinked = compiler::compile(&program);
|
||||||
|
if asm {
|
||||||
|
println!("{:?}", unlinked);
|
||||||
|
} else {
|
||||||
|
let bin = unlinked.link().to_elf();
|
||||||
println!("compiled");
|
println!("compiled");
|
||||||
save_run(&bin, gdb);
|
save_run(&bin, gdb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ctx.output.write_for(&mut stdout(), file);
|
ctx.output.write_for(&mut stdout(), file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ impl FnLowerable for PExpr {
|
|||||||
DataDef {
|
DataDef {
|
||||||
ty: Type::Bits(8).arr(data.len() as u32),
|
ty: Type::Bits(8).arr(data.len() as u32),
|
||||||
origin: Origin::File(l.span),
|
origin: Origin::File(l.span),
|
||||||
|
label: format!("string \"{}\"", s.replace("\n", "\\n"))
|
||||||
},
|
},
|
||||||
data,
|
data,
|
||||||
);
|
);
|
||||||
@@ -26,6 +27,7 @@ impl FnLowerable for PExpr {
|
|||||||
DataDef {
|
DataDef {
|
||||||
ty,
|
ty,
|
||||||
origin: Origin::File(l.span),
|
origin: Origin::File(l.span),
|
||||||
|
label: format!("char '{c}'"),
|
||||||
},
|
},
|
||||||
c.to_string().as_bytes().to_vec(),
|
c.to_string().as_bytes().to_vec(),
|
||||||
);
|
);
|
||||||
@@ -40,6 +42,7 @@ impl FnLowerable for PExpr {
|
|||||||
DataDef {
|
DataDef {
|
||||||
ty,
|
ty,
|
||||||
origin: Origin::File(l.span),
|
origin: Origin::File(l.span),
|
||||||
|
label: format!("num {n:?}")
|
||||||
},
|
},
|
||||||
n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec(),
|
n.whole.parse::<i64>().unwrap().to_le_bytes().to_vec(),
|
||||||
);
|
);
|
||||||
|
|||||||
40
src/util/label.rs
Normal file
40
src/util/label.rs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// this is not even remotely worth it but technically it doesn't use the heap I think xdddddddddd
|
||||||
|
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
pub trait Labeler<S> = Fn(&mut std::fmt::Formatter<'_>, &S) -> std::fmt::Result;
|
||||||
|
|
||||||
|
pub trait Labelable<S> {
|
||||||
|
fn labeled<L: Labeler<S>>(&self, l: L) -> Labeled<Self, L, S>
|
||||||
|
where
|
||||||
|
Self: Sized;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Labeled<'a, T, L: Labeler<S>, S> {
|
||||||
|
data: &'a T,
|
||||||
|
labeler: L,
|
||||||
|
pd: PhantomData<S>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait LabeledFmt<S> {
|
||||||
|
fn fmt_label(
|
||||||
|
&self,
|
||||||
|
f: &mut std::fmt::Formatter<'_>,
|
||||||
|
label: &dyn Labeler<S>,
|
||||||
|
) -> std::fmt::Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: LabeledFmt<S>, S> Labelable<S> for T {
|
||||||
|
fn labeled<L: Labeler<S>>(&self, l: L) -> Labeled<Self, L, S> {
|
||||||
|
Labeled {
|
||||||
|
data: self,
|
||||||
|
labeler: l,
|
||||||
|
pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: LabeledFmt<S>, L: Labeler<S>, S> std::fmt::Debug for Labeled<'_, T, L, S> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.data.fmt_label(f, &self.labeler)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
mod padder;
|
mod padder;
|
||||||
mod bits;
|
mod bits;
|
||||||
|
mod label;
|
||||||
|
|
||||||
pub use padder::*;
|
pub use padder::*;
|
||||||
pub use bits::*;
|
pub use bits::*;
|
||||||
|
pub use label::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user