small stuff
This commit is contained in:
+15
-10
@@ -7,7 +7,7 @@ pub struct Encoder {
|
|||||||
pub missing: Vec<(usize, Symbol)>,
|
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());
|
let mut encoder = Encoder::new(p.sym_count());
|
||||||
|
|
||||||
p.encode_data(&mut encoder.data, &mut encoder.sym_tab);
|
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
|
let addr = encoder
|
||||||
.sym_tab
|
.sym_tab
|
||||||
.get(sym)
|
.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))
|
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>;
|
type BInstr = crate::backend::Instr<X86_64>;
|
||||||
fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMsg> {
|
fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMsg> {
|
||||||
match instr {
|
match instr {
|
||||||
BInstr::Copy { dst, src } => todo!(),
|
|
||||||
BInstr::Asm(asm) => {
|
BInstr::Asm(asm) => {
|
||||||
for i in &asm.instrs {
|
for i in &asm.instrs {
|
||||||
encoder.asm(*i)?;
|
encoder.asm(*i)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -95,13 +95,7 @@ impl Encoder {
|
|||||||
0x8d,
|
0x8d,
|
||||||
0x05 | (dst.base() << 3),
|
0x05 | (dst.base() << 3),
|
||||||
]);
|
]);
|
||||||
let Some(addr) = self.sym_tab.get(sym) else {
|
self.sym_offset4(sym);
|
||||||
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 int(&mut self, code: u8) {
|
pub fn int(&mut self, code: u8) {
|
||||||
@@ -112,6 +106,17 @@ impl Encoder {
|
|||||||
self.data.extend([0x0f, 0x05])
|
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> {
|
pub fn asm(&mut self, instr: Instr) -> Result<(), CompilerMsg> {
|
||||||
match instr {
|
match instr {
|
||||||
Instr::Mov { dst, src } => self.mov(dst, src)?,
|
Instr::Mov { dst, src } => self.mov(dst, src)?,
|
||||||
|
|||||||
@@ -21,6 +21,6 @@ impl Arch for X86_64 {
|
|||||||
type Asm = Asm;
|
type Asm = Asm;
|
||||||
type Addr = u64;
|
type Addr = u64;
|
||||||
fn compile(p: &Program<Self>) -> Result<LinkedProgram<Self::Addr>, CompilerMsg> {
|
fn compile(p: &Program<Self>) -> Result<LinkedProgram<Self::Addr>, CompilerMsg> {
|
||||||
encode_program(p)
|
compile(p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,10 @@ pub struct Func<A: Arch> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub enum Instr<A: Arch> {
|
pub enum Instr<A: Arch> {
|
||||||
|
Set { dst: VarId, src: Vec<u8> },
|
||||||
|
Call { dst: FnId, args: Vec<VarId> },
|
||||||
Copy { dst: VarId, src: VarId },
|
Copy { dst: VarId, src: VarId },
|
||||||
|
Free(VarId),
|
||||||
Asm(A::Asm),
|
Asm(A::Asm),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user