IMPORTS WORKING
This commit is contained in:
+47
-13
@@ -1,14 +1,18 @@
|
||||
use super::*;
|
||||
use crate::backend::{LinkedProgram, SymTable, Symbol};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Encoder {
|
||||
use super::*;
|
||||
use crate::backend::{LibImport, LinkedProgram, SymImport, SymTable, Symbol};
|
||||
|
||||
pub struct Encoder<'a> {
|
||||
pub data: Vec<u8>,
|
||||
pub sym_tab: SymTable<u64>,
|
||||
pub missing: Vec<(usize, Symbol)>,
|
||||
pub sym_refs: HashMap<Symbol, Vec<usize>>,
|
||||
pub program: &'a Program<X86_64>,
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
p.encode_data(&mut encoder.data, &mut encoder.sym_tab);
|
||||
|
||||
@@ -28,9 +32,26 @@ pub fn compile(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> {
|
||||
encoder.data[pos..pos + 4].copy_from_slice(&addr_offset(pos, addr))
|
||||
}
|
||||
|
||||
let imports = p
|
||||
.external
|
||||
.iter()
|
||||
.map(|e| LibImport {
|
||||
name: e.file.clone(),
|
||||
syms: e
|
||||
.syms
|
||||
.iter()
|
||||
.map(|&s| SymImport {
|
||||
name: p.sym_info(s).name.clone(),
|
||||
usages: encoder.sym_refs.entry(s).or_default().clone(),
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(LinkedProgram {
|
||||
code: encoder.data,
|
||||
entry: p.entry.and_then(|e| encoder.sym_tab.get(e)),
|
||||
imports,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,10 +68,10 @@ fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMs
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
impl Encoder<'_> {
|
||||
// assembly
|
||||
|
||||
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegModeImm>) -> Result<(), CompilerMsg> {
|
||||
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegImmMem>) -> Result<(), CompilerMsg> {
|
||||
let src = src.into();
|
||||
let width = dst.width;
|
||||
if width == BitWidth::B16 {
|
||||
@@ -59,7 +80,7 @@ impl Encoder {
|
||||
let dst8 = dst.gt8();
|
||||
let b64 = width == BitWidth::B64;
|
||||
let b8 = width == BitWidth::B8;
|
||||
let src8 = if let RegModeImm::Reg(src) = src {
|
||||
let src8 = if let RegImmMem::Reg(src) = src {
|
||||
src.gt8()
|
||||
} else {
|
||||
false
|
||||
@@ -70,7 +91,7 @@ impl Encoder {
|
||||
.push(0x40 | dst8 as u8 | ((b64 as u8) << 3) | ((src8 as u8) << 2));
|
||||
}
|
||||
match src {
|
||||
RegModeImm::Reg(src) => {
|
||||
RegImmMem::Reg(src) => {
|
||||
if dst.width != src.width {
|
||||
return Err("src and dst are not the same size".into());
|
||||
}
|
||||
@@ -78,7 +99,7 @@ impl Encoder {
|
||||
let modrm = 0b11_000_000 | (src.base() << 3) | dst.base();
|
||||
self.data.push(modrm);
|
||||
}
|
||||
RegModeImm::Imm(imm) => {
|
||||
RegImmMem::Imm(imm) => {
|
||||
if imm > width.max() {
|
||||
return Err("immediate cannot fit in register".into());
|
||||
}
|
||||
@@ -111,6 +132,11 @@ impl Encoder {
|
||||
self.sym_offset4(sym);
|
||||
}
|
||||
|
||||
pub fn call_mem(&mut self, sym: Symbol) {
|
||||
self.data.extend([0xff, 0x15]);
|
||||
self.sym_offset4(sym);
|
||||
}
|
||||
|
||||
pub fn ret(&mut self) {
|
||||
self.data.push(0xc3);
|
||||
}
|
||||
@@ -145,7 +171,11 @@ impl Encoder {
|
||||
let Some(addr) = self.sym_tab.get(sym) else {
|
||||
let pos = self.data.len();
|
||||
self.data.extend([0; 4]);
|
||||
self.missing.push((pos, sym));
|
||||
if self.program.sym_info(sym).external {
|
||||
self.sym_refs.entry(sym).or_default().push(pos);
|
||||
} else {
|
||||
self.missing.push((pos, sym));
|
||||
}
|
||||
return;
|
||||
};
|
||||
self.data.extend(addr_offset(self.data.len(), addr));
|
||||
@@ -164,6 +194,8 @@ impl Encoder {
|
||||
RegImm::Imm(imm) => self.push_imm(imm),
|
||||
},
|
||||
Instr::Pop(reg) => self.pop(reg),
|
||||
Instr::CallMem(sym) => self.call_mem(sym),
|
||||
Instr::Sub => self.data.extend([0x48, 0x83, 0xec, 0x28]),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -176,12 +208,14 @@ fn addr_offset(pos: usize, addr: u64) -> [u8; 4] {
|
||||
offset.to_le_bytes()
|
||||
}
|
||||
|
||||
impl Encoder {
|
||||
pub fn new(sym_count: usize) -> Self {
|
||||
impl<'a> Encoder<'a> {
|
||||
pub fn new(program: &'a Program<X86_64>) -> Self {
|
||||
Self {
|
||||
data: Default::default(),
|
||||
sym_tab: SymTable::new(sym_count),
|
||||
sym_tab: SymTable::new(program.sym_count()),
|
||||
missing: Default::default(),
|
||||
sym_refs: Default::default(),
|
||||
program,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user