small stuff

This commit is contained in:
2026-06-06 22:04:11 -04:00
parent ba706ebb73
commit 69cd249671
3 changed files with 19 additions and 11 deletions
+15 -10
View File
@@ -7,7 +7,7 @@ pub struct Encoder {
pub missing: Vec<(usize, Symbol)>,
}
pub fn encode_program(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> {
pub fn compile(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> {
let mut encoder = Encoder::new(p.sym_count());
p.encode_data(&mut encoder.data, &mut encoder.sym_tab);
@@ -24,7 +24,7 @@ pub fn encode_program(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, Compile
let addr = encoder
.sym_tab
.get(sym)
.ok_or(CompilerMsg::from(format!("unknown symbol {sym:?}")))?;
.ok_or(CompilerMsg::from(format!("missing symbol {sym:?}")))?;
encoder.data[pos..pos + 4].copy_from_slice(&addr_offset(pos, addr))
}
@@ -37,12 +37,12 @@ pub fn encode_program(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, Compile
type BInstr = crate::backend::Instr<X86_64>;
fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMsg> {
match instr {
BInstr::Copy { dst, src } => todo!(),
BInstr::Asm(asm) => {
for i in &asm.instrs {
encoder.asm(*i)?;
}
}
_ => todo!(),
}
Ok(())
}
@@ -95,13 +95,7 @@ impl Encoder {
0x8d,
0x05 | (dst.base() << 3),
]);
let Some(addr) = self.sym_tab.get(sym) else {
let pos = self.data.len();
self.data.extend([0; 4]);
self.missing.push((pos, sym));
return;
};
self.data.extend(addr_offset(self.data.len(), addr));
self.sym_offset4(sym);
}
pub fn int(&mut self, code: u8) {
@@ -112,6 +106,17 @@ impl Encoder {
self.data.extend([0x0f, 0x05])
}
/// inserts a 32 bit offset from a symbol
pub fn sym_offset4(&mut self, sym: Symbol) {
let Some(addr) = self.sym_tab.get(sym) else {
let pos = self.data.len();
self.data.extend([0; 4]);
self.missing.push((pos, sym));
return;
};
self.data.extend(addr_offset(self.data.len(), addr));
}
pub fn asm(&mut self, instr: Instr) -> Result<(), CompilerMsg> {
match instr {
Instr::Mov { dst, src } => self.mov(dst, src)?,
+1 -1
View File
@@ -21,6 +21,6 @@ impl Arch for X86_64 {
type Asm = Asm;
type Addr = u64;
fn compile(p: &Program<Self>) -> Result<LinkedProgram<Self::Addr>, CompilerMsg> {
encode_program(p)
compile(p)
}
}
+3
View File
@@ -24,7 +24,10 @@ pub struct Func<A: Arch> {
}
pub enum Instr<A: Arch> {
Set { dst: VarId, src: Vec<u8> },
Call { dst: FnId, args: Vec<VarId> },
Copy { dst: VarId, src: VarId },
Free(VarId),
Asm(A::Asm),
}