remove intermediate enum / directly encode assembly

This commit is contained in:
2026-06-11 21:49:04 -04:00
parent 91f5db6950
commit b03f755252
11 changed files with 414 additions and 668 deletions
-172
View File
@@ -1,172 +0,0 @@
use crate::{arch::x86_64::*, backend::Symbol};
pub struct Asm {
pub instrs: Vec<Instr>,
}
#[derive(Clone, Copy)]
pub enum Instr {
Mov(Mov),
Call(Symbol),
CallM(Symbol),
Ret,
Int(u8),
Syscall,
Lea { dst: RegWH, sym: Symbol },
Push(Push),
Pop(Reg),
Sub,
}
#[derive(Clone, Copy)]
pub enum Mov {
RR { dst: RegH, src: RegH, width: Width },
RI { dst: RegWH, src: u64 },
RM { dst: RegWH, src: Mem },
MI { dst: Mem, src: u32 },
MR { dst: Mem, src: RegWH },
}
#[derive(Clone, Copy)]
pub enum Push {
Reg(Reg, Width64),
Mem(Mem),
Imm(u32),
}
#[derive(Clone, Copy)]
pub struct Mem {
pub reg: Reg64,
pub disp: u32,
}
#[derive(Clone, Copy)]
pub enum RegImmMem {
Reg(RegWH),
Imm(u64),
Mem(Mem),
}
#[derive(Clone, Copy)]
pub enum RegMem {
Reg(RegWH),
Mem(Mem),
}
mod fns {
use crate::io::CompilerMsg;
pub fn mem(reg: RegWH, disp: u32) -> Result<Mem, CompilerMsg> {
Ok(Mem {
reg: Reg64 {
reg: reg.reg,
width: reg.width.try_into().map_err(|_| "width must be 32 or 64")?,
},
disp,
})
}
use super::*;
pub fn mov(dst: impl Into<RegMem>, src: impl Into<RegImmMem>) -> Result<Instr, CompilerMsg> {
let dst = dst.into();
let src = src.into();
Ok(Instr::Mov(match dst {
RegMem::Reg(dst) => match src {
RegImmMem::Reg(src) => {
if src.width != dst.width {
return Err("src and dst are not same width".into());
}
Mov::RR {
dst: dst.regh,
src: src.regh,
width: dst.width,
}
}
RegImmMem::Imm(src) => Mov::RI { dst, src },
RegImmMem::Mem(src) => Mov::RM { src, dst },
},
RegMem::Mem(dst) => match src {
RegImmMem::Reg(src) => Mov::MR { dst, src },
RegImmMem::Imm(src) => {
if src > u32::MAX as u64 {
return Err("cannot move 64 bit immediate into memory".into());
}
Mov::MI {
dst,
src: src as u32,
}
}
RegImmMem::Mem(_) => return Err("cannot move memory to memory".into()),
},
}))
}
pub fn lea(dst: RegWH, sym: Symbol) -> Instr {
Instr::Lea { dst, sym }
}
pub fn push(reg: impl Into<RegImmMem>) -> Result<Instr, CompilerMsg> {
Ok(Instr::Push(match reg.into() {
RegImmMem::Reg(reg) => match reg.width {
Width::B64 => Push::Reg64(reg.reg),
Width::B16 => Push::Reg16(reg.reg),
_ => return Err("register must be 64 or 16 bit".into()),
},
RegImmMem::Imm(imm) => match imm.try_into() {
Ok(imm) => Push::Imm(imm),
Err(_) => return Err("immediate must be 32 bit".into()),
},
RegImmMem::Mem(mem) => Push::Mem(mem),
}))
}
pub fn pop(reg: RegWH) -> Instr {
assert!(reg.width == Width::B64);
Instr::Pop(reg.reg)
}
}
// fromrot
impl From<RegWH> for RegImmMem {
fn from(value: RegWH) -> Self {
Self::Reg(value)
}
}
impl From<RegWH> for RegMem {
fn from(value: RegWH) -> Self {
Self::Reg(value)
}
}
impl From<Mem> for RegImmMem {
fn from(value: Mem) -> Self {
Self::Mem(value)
}
}
impl From<Mem> for RegMem {
fn from(value: Mem) -> Self {
Self::Mem(value)
}
}
impl From<u64> for RegImmMem {
fn from(value: u64) -> Self {
Self::Imm(value)
}
}
impl From<i64> for RegImmMem {
fn from(value: i64) -> Self {
Self::Imm(value as u64)
}
}
impl From<i32> for RegImmMem {
fn from(value: i32) -> Self {
Self::Imm(value as u32 as u64)
}
}
pub use fns::*;
+83
View File
@@ -0,0 +1,83 @@
use std::collections::HashMap;
use super::*;
use crate::backend::{LibImport, LinkedProgram, SymImport, SymTable, Symbol};
use util::*;
pub struct Encoder<'a> {
pub code: Code,
pub sym_tab: SymTable<u64>,
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);
p.encode_data(&mut encoder.code.bytes, &mut encoder.sym_tab);
for f in &p.funcs {
let addr = encoder.code.bytes.len();
encoder.sym_tab.insert(f.sym, addr as u64);
for instr in &f.instrs {
encoder.compile_instr(instr)?;
}
}
for (pos, sym) in encoder.code.missing.drain(..) {
let info = encoder.program.sym_info(sym);
if info.external {
encoder.sym_refs.entry(sym).or_default().push(pos);
} else {
let addr = encoder
.sym_tab
.get(sym)
.ok_or(CompilerMsg::from(format!("missing symbol {}", info.name)))?;
encoder.code.bytes[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.code.bytes,
entry: p.entry.and_then(|e| encoder.sym_tab.get(e)),
imports,
})
}
type BInstr = crate::backend::Instr<X86_64>;
impl<'a> Encoder<'a> {
fn compile_instr(&mut self, instr: &BInstr) -> Result<(), CompilerMsg> {
match instr {
BInstr::Asm(asm) => {
self.code.extend(asm);
}
_ => todo!(),
}
Ok(())
}
pub fn new(program: &'a Program<X86_64>) -> Self {
Self {
code: Code::default(),
sym_tab: SymTable::new(program.sym_count()),
sym_refs: Default::default(),
program,
}
}
}
+192 -175
View File
@@ -1,232 +1,249 @@
use std::collections::HashMap; use crate::backend::Symbol;
use super::*; use super::*;
use crate::backend::{LibImport, LinkedProgram, SymImport, SymTable, Symbol};
use util::*; use util::*;
pub struct Encoder<'a> { type ERes = Result<(), CompilerMsg>;
pub data: Vec<u8>,
pub sym_tab: SymTable<u64>, /// machine code
pub missing: Vec<(usize, Symbol)>, #[derive(Default)]
pub sym_refs: HashMap<Symbol, Vec<usize>>, pub struct Code {
pub program: &'a Program<X86_64>, pub(super) bytes: Vec<u8>,
pub(super) missing: Vec<(usize, Symbol)>,
} }
pub fn compile(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> { #[derive(Clone, Copy)]
let mut encoder = Encoder::new(p); pub struct Mem {
pub reg: Reg,
p.encode_data(&mut encoder.data, &mut encoder.sym_tab); pub disp: u32,
for f in &p.funcs {
let addr = encoder.data.len();
encoder.sym_tab.insert(f.sym, addr as u64);
for instr in &f.instrs {
compile_instr(&mut encoder, instr)?;
}
}
for (pos, sym) in encoder.missing.drain(..) {
let addr = encoder
.sym_tab
.get(sym)
.ok_or(CompilerMsg::from(format!("missing symbol {sym:?}")))?;
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,
})
} }
type BInstr = crate::backend::Instr<X86_64>; #[derive(Clone, Copy)]
fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMsg> { pub enum RegImmMem {
match instr { Reg(Reg),
BInstr::Asm(asm) => { Imm(u64),
for i in &asm.instrs { Mem(Mem),
encoder.asm(*i)?;
}
}
_ => todo!(),
}
Ok(())
} }
impl Encoder<'_> { #[derive(Clone, Copy)]
// assembly pub enum RegMem {
Reg(Reg),
Mem(Mem),
}
pub fn mov_rr(&mut self, dst: RegH, src: RegH, width: Width) { pub fn mem(reg: Reg, disp: u32) -> Mem {
if width == Width::B16 { Mem { reg, disp }
self.data.push(0x66); }
}
if src.requires_rex(width) || dst.requires_rex(width) {
self.data.push(rex(width, src, 0, dst));
}
self.data.push(0x88 | width.gt8() as u8);
self.data.push(modrm_regs(src, dst));
}
pub fn mov_ri(&mut self, dst: RegWH, src: u64) -> Result<(), CompilerMsg> { impl Code {
if dst.width == Width::B16 { pub fn mov(&mut self, dst: impl Into<RegMem>, src: impl Into<RegImmMem>) -> ERes {
self.data.push(0x66); let dst = dst.into();
let src = src.into();
match dst {
RegMem::Reg(dst) => match src {
RegImmMem::Reg(src) => {
if dst.width() != src.width() {
return Err("src and dst are not same width".into());
} }
if dst.incompatible(&src) {
return Err("incompatible registers due to rex".into());
}
let width = dst.width();
self.prefix16(width);
if src.requires_rex() || dst.requires_rex() {
self.bytes.push(rex(width, src, 0, dst));
}
self.bytes.push(0x88 | width.gt8() as u8);
self.bytes.push(modrm_regs(src, dst));
}
RegImmMem::Imm(src) => {
self.prefix16(dst);
if dst.requires_rex() { if dst.requires_rex() {
self.data.push(rex(dst.width, 0, 0, dst)); self.bytes.push(rex(dst.width(), 0, 0, dst));
} }
if src > dst.width.max() { if src > dst.width().max() {
return Err("immediate cannot fit in register".into()); return Err("immediate cannot fit in register".into());
} }
let opcode = 0xb0 | ((dst.width.gt8() as u8) << 3); let opcode = 0xb0 | ((dst.width().gt8() as u8) << 3);
self.data.push(opcode | dst.base()); self.bytes.push(opcode | dst.base());
self.data.extend(&src.to_le_bytes()[..dst.width.bytes()]); self.bytes.extend(&src.to_le_bytes()[..dst.width().bytes()]);
}
RegImmMem::Mem(src) => todo!(),
},
RegMem::Mem(dst) => match src {
RegImmMem::Reg(src) => todo!(),
RegImmMem::Imm(src) => {
if src > u32::MAX as u64 {
return Err("cannot move 64 bit immediate into memory".into());
}
self.bytes.extend([rex(1, dst.reg, 0, 0), 0xc7]);
self.modrm_regdisp(dst.reg, dst.disp);
self.bytes.extend(src.to_le_bytes());
}
RegImmMem::Mem(_) => return Err("cannot move memory to memory".into()),
},
}
Ok(()) Ok(())
} }
pub fn mov_rm(&mut self, dst: RegWH, src: Mem) {} pub fn push(&mut self, reg: impl Into<RegImmMem>) -> ERes {
match reg.into() {
pub fn mov_mr(&mut self, dst: Mem, src: RegWH) {} RegImmMem::Reg(reg) => match reg.width() {
Width::B64 => {
pub fn mov_mi(&mut self, dst: Mem, src: u32) { if reg.gt8() {
self.data.extend([rex(1, dst.reg, 0, 0), 0xc7]); self.bytes.push(0x41);
self.modrm_regdisp(dst.reg, dst.disp); }
self.data.extend(src.to_le_bytes()); self.bytes.push(0x50 | reg.base());
}
Width::B16 => {}
_ => return Err("register must be 64 or 16 bit".into()),
},
RegImmMem::Imm(imm) => match imm.try_into() {
Ok(imm) => {
const U8: u32 = 2 << 8;
if let 0..U8 = imm {
self.bytes.push(0x6a);
self.bytes.push(imm as u8);
} else {
self.bytes.push(0x68);
self.bytes.extend(imm.to_le_bytes());
}
}
Err(_) => return Err("immediate must be 32 bit".into()),
},
RegImmMem::Mem(mem) => todo!(),
}
Ok(())
} }
pub fn lea(&mut self, dst: RegWH, sym: Symbol) { pub fn pop(&mut self, reg: Reg) -> ERes {
self.data match reg.width() {
Width::B64 | Width::B16 => (),
_ => return Err("register must be 64 or 16 bit".into()),
}
self.prefix16(reg);
if reg.gt8() {
self.bytes.push(0x41);
}
self.bytes.push(0x58 | reg.base());
Ok(())
}
pub fn lea(&mut self, dst: Reg, sym: Symbol) {
self.bytes
.extend([rex(1, dst, 0, 0), 0x8d, modrm_disp32(dst)]); .extend([rex(1, dst, 0, 0), 0x8d, modrm_disp32(dst)]);
self.sym_offset4(sym); self.sym_offset4(sym);
} }
pub fn int(&mut self, code: u8) { pub fn int(&mut self, code: u8) {
self.data.extend([0xcd, code]) self.bytes.extend([0xcd, code])
} }
pub fn syscall(&mut self) { pub fn syscall(&mut self) {
self.data.extend([0x0f, 0x05]) self.bytes.extend([0x0f, 0x05])
} }
pub fn call_i(&mut self, sym: Symbol) { pub fn call(&mut self, sym: Symbol) {
self.data.push(0xe8); self.bytes.push(0xe8);
self.sym_offset4(sym); self.sym_offset4(sym);
} }
pub fn call_m(&mut self, sym: Symbol) { pub fn call_mem(&mut self, sym: Symbol) {
self.data.extend([0xff, 0x15]); self.bytes.extend([0xff, 0x15]);
self.sym_offset4(sym); self.sym_offset4(sym);
} }
pub fn ret(&mut self) { pub fn ret(&mut self) {
self.data.push(0xc3); self.bytes.push(0xc3);
} }
pub fn push_r(&mut self, reg: Reg, width: Width64) { pub fn sub(&mut self) {
if reg.gt8() { // sub esp 40 iirc
self.data.push(0x41); self.bytes.extend([0x48, 0x83, 0xec, 0x28]);
}
self.data.push(0x50 | reg.base());
} }
pub fn push_i(&mut self, imm: u32) { fn prefix16(&mut self, width: impl Into<Width>) {
const U8: u32 = 2 << 8; if width.into() == Width::B16 {
if let 0..U8 = imm { self.bytes.push(0x66);
self.data.push(0x6a); }
self.data.push(imm as u8); }
fn modrm_regdisp(&mut self, reg: Reg, disp: u32) {
let disp8 = disp < u8::MAX as u32;
let mod_ = if disp8 { 0b01 } else { 0b10 };
self.bytes.push(modrm(mod_, 0, reg.base()));
if reg.val() == rsp.val() {
// SIB
self.bytes.push(0x24);
}
if disp8 {
self.bytes.push(disp as u8);
} else { } else {
self.data.push(0x68); self.bytes.extend(disp.to_le_bytes());
self.data.extend(imm.to_le_bytes());
} }
} }
pub fn pop(&mut self, reg: Reg) {
if reg.gt8() {
self.data.push(0x41);
}
self.data.push(0x58 | reg.base());
}
/// inserts a 32 bit offset from a symbol /// inserts a 32 bit offset from a symbol
pub fn sym_offset4(&mut self, sym: Symbol) { fn sym_offset4(&mut self, sym: Symbol) {
let Some(addr) = self.sym_tab.get(sym) else { let pos = self.bytes.len();
let pos = self.data.len(); self.bytes.extend([0; 4]);
self.data.extend([0; 4]);
if self.program.sym_info(sym).external {
self.sym_refs.entry(sym).or_default().push(pos);
} else {
self.missing.push((pos, sym)); 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 extend(&mut self, other: &Code) {
match instr { let pos = self.bytes.len();
Instr::Mov(v) => match v { self.bytes.extend(&other.bytes);
Mov::RR { dst, src, width } => self.mov_rr(dst, src, width), self.missing
Mov::RI { dst, src } => self.mov_ri(dst, src)?, .extend(other.missing.iter().map(|&(p, s)| (pos + p, s)));
Mov::RM { dst, src } => self.mov_rm(dst, src),
Mov::MI { dst, src } => self.mov_mi(dst, src),
Mov::MR { dst, src } => self.mov_mr(dst, src),
},
Instr::Int(code) => self.int(code),
Instr::Syscall => self.syscall(),
Instr::Lea { dst, sym } => self.lea(dst, sym),
Instr::Call(sym) => self.call_i(sym),
Instr::CallM(sym) => self.call_m(sym),
Instr::Ret => self.ret(),
Instr::Push(v) => match v {
Push::Reg(reg, width) => self.push_r(reg, width),
Push::Imm(imm) => self.push_i(imm),
},
Instr::Pop(reg) => self.pop(reg),
Instr::Sub => self.data.extend([0x48, 0x83, 0xec, 0x28]),
}
Ok(())
}
pub fn modrm_regdisp(&mut self, reg: impl Into<Reg>, disp: u32) {
let reg = reg.into();
let disp8 = disp < u8::MAX as u32;
let mod_ = if disp8 { 0b01 } else { 0b10 };
self.data.push(modrm(mod_, 0, reg.base()));
if reg == rsp.reg {
// SIB
self.data.push(0x24);
}
if disp8 {
self.data.push(disp as u8);
} else {
self.data.extend(disp.to_le_bytes());
}
} }
} }
impl<'a> Encoder<'a> { pub fn encode(f: impl FnOnce(&mut Code) -> Result<(), CompilerMsg>) -> Result<Code, CompilerMsg> {
pub fn new(program: &'a Program<X86_64>) -> Self { let mut code = Code::default();
Self { f(&mut code)?;
data: Default::default(), Ok(code)
sym_tab: SymTable::new(program.sym_count()), }
missing: Default::default(),
sym_refs: Default::default(), // fromrot
program, impl From<Reg> for RegImmMem {
fn from(value: Reg) -> Self {
Self::Reg(value)
} }
}
impl From<Reg> for RegMem {
fn from(value: Reg) -> Self {
Self::Reg(value)
}
}
impl From<Mem> for RegImmMem {
fn from(value: Mem) -> Self {
Self::Mem(value)
}
}
impl From<Mem> for RegMem {
fn from(value: Mem) -> Self {
Self::Mem(value)
}
}
impl From<u64> for RegImmMem {
fn from(value: u64) -> Self {
Self::Imm(value)
}
}
impl From<i64> for RegImmMem {
fn from(value: i64) -> Self {
Self::Imm(value as u64)
}
}
impl From<i32> for RegImmMem {
fn from(value: i32) -> Self {
Self::Imm(value as u32 as u64)
} }
} }
+3 -4
View File
@@ -1,7 +1,6 @@
mod asm; mod compile;
mod encode; mod encode;
mod reg; mod reg;
mod reg2;
mod test; mod test;
mod util; mod util;
@@ -11,7 +10,7 @@ use crate::{
io::CompilerMsg, io::CompilerMsg,
}; };
pub use asm::*; pub use compile::*;
pub use encode::*; pub use encode::*;
pub use reg::*; pub use reg::*;
pub use test::bin::run as bin_test; pub use test::bin::run as bin_test;
@@ -20,7 +19,7 @@ pub struct X86_64;
impl Arch for X86_64 { impl Arch for X86_64 {
const NAME: &str = "x86_64"; const NAME: &str = "x86_64";
type Asm = Asm; type Asm = Code;
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> {
compile(p) compile(p)
+37 -104
View File
@@ -1,22 +1,8 @@
#[derive(Clone, Copy, PartialEq)] #[derive(Clone, Copy, PartialEq)]
pub struct Reg(u8); pub struct Reg {
val: u8,
#[derive(Clone, Copy)] high: bool,
pub struct RegH { width: Width,
pub reg: Reg,
pub high: bool,
}
#[derive(Clone, Copy)]
pub struct RegWH {
pub regh: RegH,
pub width: Width,
}
#[derive(Clone, Copy)]
pub struct Reg64 {
pub reg: Reg,
pub width: Width64,
} }
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
@@ -27,22 +13,16 @@ pub enum Width {
B8, B8,
} }
#[derive(Debug, Clone, Copy, PartialEq)] def_regs! { Reg;
pub enum Width64 { 0b0000 : rax eax ax al,
B64, 0b0001 : rcx ecx cx cl,
B32, 0b0010 : rdx edx dx dl,
} 0b0011 : rbx ebx bx bl,
def_regs! { RegWH; 0b0100 : rsp esp sp spl norex=ah,
0b0000 : rax eax ax al ah=spl, 0b0101 : rbp ebp bp bpl norex=ch,
0b0001 : rcx ecx cx cl ch=bpl, 0b0110 : rsi esi si sil norex=dh,
0b0010 : rdx edx dx dl dh=sil, 0b0111 : rdi edi di dil norex=bh,
0b0011 : rbx ebx bx bl bh=dil,
0b0100 : rsp esp sp spl,
0b0101 : rbp ebp bp bpl,
0b0110 : rsi esi si sil,
0b0111 : rdi edi di dil,
0b1000 : r8 r8d r8w r8b, 0b1000 : r8 r8d r8w r8b,
0b1001 : r9 r9d r9w r9b, 0b1001 : r9 r9d r9w r9b,
@@ -56,36 +36,35 @@ def_regs! { RegWH;
impl Reg { impl Reg {
pub fn base(&self) -> u8 { pub fn base(&self) -> u8 {
self.0 & 0b111 self.val & 0b111
} }
/// checks if register is not one of the first 8 (0-7) /// checks if register is not one of the first 8 (0-7)
pub fn gt8(&self) -> bool { pub fn gt8(&self) -> bool {
self.0 >= 0b1000 self.val >= 0b1000
} }
pub fn gt4(&self) -> bool { pub fn gt4(&self) -> bool {
self.0 >= 0b0100 self.val >= 0b0100
}
pub fn high(&self) -> bool {
self.high
} }
}
impl RegH { pub fn width(&self) -> Width {
pub fn requires_rex(&self, width: Width) -> bool { self.width
self.gt8() || width == Width::B64 || (self.gt4() && width == Width::B8 && !self.high)
}
}
impl RegWH {
const fn new(val: u8, width: Width, high: bool) -> Self {
Self {
regh: RegH {
reg: Reg(val),
high,
},
width,
}
} }
pub fn requires_rex(&self) -> bool { pub fn requires_rex(&self) -> bool {
self.regh.requires_rex(self.width) self.gt8()
|| self.width == Width::B64
|| (self.gt4() && self.width == Width::B8 && !self.high)
}
pub fn incompatible(&self, other: &Reg) -> bool {
(self.requires_rex() && other.high) || (self.high && other.requires_rex())
}
const fn new(val: u8, width: Width, high: bool) -> Self {
Self { val, high, width }
} }
} }
@@ -112,44 +91,8 @@ impl Width {
} }
} }
impl From<RegWH> for Reg {
fn from(value: RegWH) -> Self {
value.reg
}
}
impl From<RegH> for Reg {
fn from(value: RegH) -> Self {
value.reg
}
}
impl From<Reg64> for Reg {
fn from(value: Reg64) -> Self {
value.reg
}
}
impl From<RegWH> for RegH {
fn from(value: RegWH) -> Self {
value.regh
}
}
impl TryFrom<Width> for Width64 {
type Error = ();
fn try_from(value: Width) -> Result<Self, Self::Error> {
match value {
Width::B64 => Ok(Self::B64),
Width::B32 => Ok(Self::B32),
_ => Err(()),
}
}
}
macro_rules! def_regs { macro_rules! def_regs {
($Struct: ident; $($val:literal : $B64:ident $B32:ident $B16:ident $B8:ident $($B8H:ident=$hval:expr)?,)*) => { ($Struct: ident; $($val:literal : $B64:ident $B32:ident $B16:ident $B8:ident $(norex=$B8H:ident)?,)*) => {
$( $(
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub const $B64: $Struct = $Struct::new($val, Width::B64, false); pub const $B64: $Struct = $Struct::new($val, Width::B64, false);
@@ -161,7 +104,7 @@ macro_rules! def_regs {
pub const $B8 : $Struct = $Struct::new($val, Width::B8 , false); pub const $B8 : $Struct = $Struct::new($val, Width::B8 , false);
$( $(
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub const $B8H: $Struct = $Struct::new($hval.regh.reg.0, Width::B8, true); pub const $B8H: $Struct = $Struct::new($val, Width::B8, true);
)? )?
)* )*
impl $Struct { impl $Struct {
@@ -185,18 +128,8 @@ macro_rules! def_regs {
use def_regs; use def_regs;
impl std::ops::Deref for RegWH { impl From<Reg> for Width {
type Target = RegH; fn from(value: Reg) -> Self {
value.width
fn deref(&self) -> &Self::Target {
&self.regh
}
}
impl std::ops::Deref for RegH {
type Target = Reg;
fn deref(&self) -> &Self::Target {
&self.reg
} }
} }
-107
View File
@@ -1,107 +0,0 @@
#[derive(Clone, Copy, PartialEq)]
pub struct R8<Rex>(u8, std::marker::PhantomData<Rex>);
#[derive(Clone, Copy, PartialEq)]
pub struct R16(u8);
#[derive(Clone, Copy, PartialEq)]
pub struct R32(u8);
#[derive(Clone, Copy, PartialEq)]
pub struct R64(u8);
pub struct Rex;
pub struct NoRex;
pub struct OptionalRex;
pub trait MatchRex<Rex> {
const REX: bool;
}
impl MatchRex<Rex> for Rex {
const REX: bool = true;
}
impl MatchRex<Rex> for OptionalRex {
const REX: bool = true;
}
impl MatchRex<NoRex> for NoRex {
const REX: bool = false;
}
impl MatchRex<NoRex> for OptionalRex {
const REX: bool = false;
}
impl MatchRex<OptionalRex> for OptionalRex {
const REX: bool = false;
}
pub enum AsmReg {
R8(R8<OptionalRex>),
R8Rex(R8<Rex>),
R8NoRex(R8<NoRex>),
R16(R16),
R32(R32),
R64(R64),
}
def_regs! {
0b0000 : rax eax ax =al,
0b0001 : rcx ecx cx =cl,
0b0010 : rdx edx dx =dl,
0b0011 : rbx ebx bx =bl,
0b0100 : rsp esp sp rex=spl norex=ah,
0b0101 : rbp ebp bp rex=bpl norex=ch,
0b0110 : rsi esi si rex=sil norex=dh,
0b0111 : rdi edi di rex=dil norex=bh,
0b1000 : r8 r8d r8w rex=r8b,
0b1001 : r9 r9d r9w rex=r9b,
0b1010 : r10 r10d r10w rex=r10b,
0b1011 : r11 r11d r11w rex=r11b,
0b1100 : r12 r12d r12w rex=r12b,
0b1101 : r13 r13d r13w rex=r13b,
0b1110 : r14 r14d r14w rex=r14b,
0b1111 : r15 r15d r15w rex=r15b,
}
macro_rules! def_regs {
($($val:literal : $B64:ident $B32:ident $B16:ident $(=$B8:ident)? $(rex=$B8Rex:ident)? $(norex=$B8NoRex:ident)?,)*) => {
$(
#[allow(non_upper_case_globals)]
pub const $B64: R64 = R64($val);
#[allow(non_upper_case_globals)]
pub const $B32: R32 = R32($val);
#[allow(non_upper_case_globals)]
pub const $B16: R16 = R16($val);
$(
#[allow(non_upper_case_globals)]
pub const $B8: R8<OptionalRex> = R8($val, std::marker::PhantomData);
)*
$(
#[allow(non_upper_case_globals)]
pub const $B8Rex: R8<Rex> = R8($val, std::marker::PhantomData);
)?
$(
#[allow(non_upper_case_globals)]
pub const $B8NoRex: R8<NoRex> = R8($val, std::marker::PhantomData);
)?
)*
impl AsmReg {
pub fn parse(s: &str) -> Option<Self> {
Some(match s.to_lowercase().as_str() {
$(
stringify!($B64) => Self::R64($B64),
stringify!($B32) => Self::R32($B32),
stringify!($B16) => Self::R16($B16),
$( stringify!($B8 ) => Self::R8($B8), )?
$( stringify!($B8Rex) => Self::R8Rex($B8Rex), )?
$( stringify!($B8NoRex) => Self::R8NoRex($B8NoRex), )?
)*
_ => return None,
})
}
}
};
}
use def_regs;
+37 -40
View File
@@ -18,34 +18,32 @@ fn linux() -> Result<(), CompilerMsg> {
let text_sym2 = program.ro_data("hello_jp", text2); let text_sym2 = program.ro_data("hello_jp", text2);
let hello2 = program.func( let hello2 = program.func(
"hello2", "hello2",
[BInstr::Asm(Asm { [BInstr::Asm(encode(|c| {
instrs: vec![ c.mov(ax, 1)?;
mov(ax, 1)?, c.mov(di, 1)?;
mov(di, 1)?, c.lea(rsi, text_sym2);
lea(rsi, text_sym2), c.mov(dx, text2.len() as u64)?;
mov(dx, text2.len() as u64)?, c.syscall();
Instr::Syscall, c.ret();
Instr::Ret, Ok(())
], })?)],
})],
); );
let entry = program.func( let entry = program.func(
"main", "main",
[BInstr::Asm(Asm { [BInstr::Asm(encode(|c| {
instrs: vec![ c.mov(rdi, 39)?;
mov(di, 39)?, c.push(rdi)?;
push(rdi)?, c.mov(ax, 1)?;
mov(ax, 1)?, c.mov(di, 1)?;
mov(di, 1)?, c.lea(rsi, text_sym);
lea(rsi, text_sym), c.mov(dx, text.len() as u64)?;
mov(dx, text.len() as u64)?, c.syscall();
Instr::Syscall, c.call(hello2);
Instr::Call(hello2), c.mov(ax, 0x3c)?;
mov(ax, 0x3c)?, c.pop(rdi)?;
pop(rdi), c.syscall();
Instr::Syscall, Ok(())
], })?)],
})],
); );
program.entry = Some(entry); program.entry = Some(entry);
let linked = program.compile().expect("failed to compile"); let linked = program.compile().expect("failed to compile");
@@ -86,24 +84,23 @@ fn windows() -> Result<(), CompilerMsg> {
let written = program.ro_data("written", [0; 4]); let written = program.ro_data("written", [0; 4]);
let entry = program.func( let entry = program.func(
"main", "main",
[BInstr::Asm(Asm { [BInstr::Asm(encode(|c| {
instrs: vec![ c.sub();
Instr::Sub,
// stdout // stdout
mov(ecx, -11)?, c.mov(ecx, -11)?;
Instr::CallM(get_std_handle), c.call_mem(get_std_handle);
// write // write
mov(rcx, rax)?, c.mov(rcx, rax)?;
lea(rdx, text_sym), c.lea(rdx, text_sym);
mov(r8d, text.len() as u64)?, c.mov(r8d, text.len() as u64)?;
lea(r9, written), c.lea(r9, written);
mov(mem(rsp, 0x20)?, 0)?, c.mov(mem(rsp, 0x20), 0)?;
Instr::CallM(write_file), c.call_mem(write_file);
// exit // exit
mov(ecx, 39)?, c.mov(ecx, 39)?;
Instr::CallM(exit_process), c.call_mem(exit_process);
], Ok(())
})], })?)],
); );
program.entry = Some(entry); program.entry = Some(entry);
let linked = program.compile().expect("failed to compile"); let linked = program.compile().expect("failed to compile");
+45 -49
View File
@@ -1,86 +1,82 @@
use super::*; use super::*;
macro_rules! eq {
fn eq(expected: impl AsRef<[u8]>, asm: Result<Instr, CompilerMsg>) { ($expected:expr, $instr:ident $args:tt $(,)?) => {
let expected = expected.as_ref(); let expected = $expected.as_ref();
let program = Program::default(); let mut code = Code::default();
let mut encoder = Encoder::new(&program); let res = code.$instr $args;
let asm = match asm { let asm = stringify!($instr $args);
Ok(v) => v, if let Err(e) = res {
Err(e) => { panic!("{asm}: failed to compile: {}", e.msg);
panic!("expected {expected:x?}, failed to compile: {}", e.msg);
} }
let res = &code.bytes[..];
assert_eq!(expected, res, "{asm}: expected {expected:x?}, got {res:x?}");
}; };
if let Err(e) = encoder.asm(asm) {
panic!("expected {expected:x?}, failed to compile: {}", e.msg);
}
let res = encoder.data;
assert_eq!(expected, &res[..], "expected {expected:x?}, got {res:x?}");
} }
#[test] #[test]
fn mov_reg_reg() { fn mov_reg_reg() {
// used objdump on some nasm compiled assembly // used objdump on some nasm compiled assembly
eq([0x48, 0x89, 0xd8], mov(rax, rbx)); eq!([0x48, 0x89, 0xd8], mov(rax, rbx));
eq([0x89, 0xd8], mov(eax, ebx)); eq!([0x89, 0xd8], mov(eax, ebx));
eq([0x66, 0x89, 0xd8], mov(ax, bx)); eq!([0x66, 0x89, 0xd8], mov(ax, bx));
eq([0x88, 0xd8], mov(al, bl)); eq!([0x88, 0xd8], mov(al, bl));
eq([0x88, 0xfc], mov(ah, bh)); eq!([0x88, 0xfc], mov(ah, bh));
eq([0x88, 0xf8], mov(al, bh)); eq!([0x88, 0xf8], mov(al, bh));
eq([0x88, 0xdc], mov(ah, bl)); eq!([0x88, 0xdc], mov(ah, bl));
eq([0x40, 0x88, 0xe7], mov(dil, spl)); eq!([0x40, 0x88, 0xe7], mov(dil, spl));
eq([0x4d, 0x89, 0xc8], mov(r8, r9)); eq!([0x4d, 0x89, 0xc8], mov(r8, r9));
eq([0x45, 0x89, 0xc8], mov(r8d, r9d)); eq!([0x45, 0x89, 0xc8], mov(r8d, r9d));
eq([0x66, 0x45, 0x89, 0xc8], mov(r8w, r9w)); eq!([0x66, 0x45, 0x89, 0xc8], mov(r8w, r9w));
eq([0x45, 0x88, 0xc8], mov(r8b, r9b)); eq!([0x45, 0x88, 0xc8], mov(r8b, r9b));
eq([0x49, 0x89, 0xc0], mov(r8, rax)); eq!([0x49, 0x89, 0xc0], mov(r8, rax));
eq([0x4c, 0x89, 0xc0], mov(rax, r8)); eq!([0x4c, 0x89, 0xc0], mov(rax, r8));
eq([0x4d, 0x89, 0xd1], mov(r9, r10)); eq!([0x4d, 0x89, 0xd1], mov(r9, r10));
eq([0x4d, 0x89, 0xe0], mov(r8, r12)); eq!([0x4d, 0x89, 0xe0], mov(r8, r12));
eq([0x89, 0xe0], mov(eax, esp)); eq!([0x89, 0xe0], mov(eax, esp));
eq([0x89, 0xc4], mov(esp, eax)); eq!([0x89, 0xc4], mov(esp, eax));
} }
#[test] #[test]
fn mov_reg_imm() { fn mov_reg_imm() {
eq( eq!(
[0x49, 0xbf, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12], [0x49, 0xbf, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12],
mov(r15, 0x123456789abcdef0u64), mov(r15, 0x123456789abcdef0u64),
); );
eq( eq!(
[0x49, 0xb8, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12], [0x49, 0xb8, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12],
mov(r8, 0x123456789abcdef0u64), mov(r8, 0x123456789abcdef0u64),
); );
eq( eq!(
[0x49, 0xb9, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12], [0x49, 0xb9, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12],
mov(r9, 0x123456789abcdef0u64), mov(r9, 0x123456789abcdef0u64),
); );
eq([0x41, 0xb9, 0x78, 0x56, 0x34, 0x12], mov(r9d, 0x12345678)); eq!([0x41, 0xb9, 0x78, 0x56, 0x34, 0x12], mov(r9d, 0x12345678));
eq([0x66, 0x41, 0xb9, 0x34, 0x12], mov(r9w, 0x1234)); eq!([0x66, 0x41, 0xb9, 0x34, 0x12], mov(r9w, 0x1234));
eq([0x41, 0xb1, 0x12], mov(r9b, 0x12)); eq!([0x41, 0xb1, 0x12], mov(r9b, 0x12));
eq([0x41, 0xb0, 0x12], mov(r8b, 0x12)); eq!([0x41, 0xb0, 0x12], mov(r8b, 0x12));
eq([0x41, 0xb7, 0x12], mov(r15b, 0x12)); eq!([0x41, 0xb7, 0x12], mov(r15b, 0x12));
eq( eq!(
[0x48, 0xb8, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12], [0x48, 0xb8, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12],
mov(rax, 0x123456789abcdef0u64), mov(rax, 0x123456789abcdef0u64),
); );
eq( eq!(
[0x48, 0xbb, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12], [0x48, 0xbb, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12],
mov(rbx, 0x123456789abcdef0u64), mov(rbx, 0x123456789abcdef0u64),
); );
eq( eq!(
[0x48, 0xbf, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12], [0x48, 0xbf, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12],
mov(rdi, 0x123456789abcdef0u64), mov(rdi, 0x123456789abcdef0u64),
); );
eq([0xbb, 0x78, 0x56, 0x34, 0x12], mov(ebx, 0x12345678)); eq!([0xbb, 0x78, 0x56, 0x34, 0x12], mov(ebx, 0x12345678));
eq([0x66, 0xbb, 0x34, 0x12], mov(bx, 0x1234)); eq!([0x66, 0xbb, 0x34, 0x12], mov(bx, 0x1234));
eq([0xb3, 0x12], mov(bl, 0x12)); eq!([0xb3, 0x12], mov(bl, 0x12));
eq([0xb7, 0x12], mov(bh, 0x12)); eq!([0xb7, 0x12], mov(bh, 0x12));
eq([0xb4, 0x12], mov(ah, 0x12)); eq!([0xb4, 0x12], mov(ah, 0x12));
eq([0x40, 0xb7, 0x12], mov(dil, 0x12)); eq!([0x40, 0xb7, 0x12], mov(dil, 0x12));
} }
+2 -2
View File
@@ -1,12 +1,12 @@
use crate::{ use crate::{
arch::x86_64::Asm, arch::x86_64::Code,
parser::{Node, cursor::Token}, parser::{Node, cursor::Token},
}; };
pub mod x86_64; pub mod x86_64;
pub enum AsmBlock { pub enum AsmBlock {
X86_64(Asm), X86_64(Code),
} }
impl Node for AsmBlock { impl Node for AsmBlock {
+8 -8
View File
@@ -7,9 +7,9 @@ use crate::{
}, },
}; };
impl Node for Asm { impl Node for Code {
fn parse(ctx: &mut crate::parser::ParseCtx) -> Result<Self, crate::io::CompilerMsg> { fn parse(ctx: &mut crate::parser::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
let mut instrs = Vec::new(); let mut c = Code::default();
while let Some(Token::Ident(next)) = ctx.peek() { while let Some(Token::Ident(next)) = ctx.peek() {
match next.as_str() { match next.as_str() {
"mov" => { "mov" => {
@@ -17,7 +17,7 @@ impl Node for Asm {
let dst = parse_reg(ctx)?; let dst = parse_reg(ctx)?;
ctx.expect(Token::Comma)?; ctx.expect(Token::Comma)?;
let src = parse_rmi(ctx)?; let src = parse_rmi(ctx)?;
instrs.push(mov(dst, src)?); c.mov(dst, src)?;
} }
"int" => { "int" => {
ctx.next(); ctx.next();
@@ -27,7 +27,7 @@ impl Node for Asm {
let code = parse_imm(&num, ctx.span)? let code = parse_imm(&num, ctx.span)?
.try_into() .try_into()
.map_err(|_| CompilerMsg::from("Immediate must be a u8"))?; .map_err(|_| CompilerMsg::from("Immediate must be a u8"))?;
instrs.push(Instr::Int(code)); c.int(code);
} }
_ => { _ => {
let msg = format!("Unknown instruction {next}"); let msg = format!("Unknown instruction {next}");
@@ -39,7 +39,7 @@ impl Node for Asm {
} }
} }
} }
Ok(Self { instrs }) Ok(c)
} }
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: crate::parser::DisplayCtx) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter, ctx: crate::parser::DisplayCtx) -> std::fmt::Result {
@@ -60,17 +60,17 @@ pub fn parse_rmi(ctx: &mut crate::parser::ParseCtx) -> Result<RegImmMem, Compile
let next = ctx.expect_next()?; let next = ctx.expect_next()?;
let err = || CompilerMsg::unexpected_token(&next, ctx.span, "a register or immediate"); let err = || CompilerMsg::unexpected_token(&next, ctx.span, "a register or immediate");
Ok(match &next { Ok(match &next {
Token::Ident(ident) => RegImmMem::Reg(RegWH::parse(ident).ok_or_else(err)?), Token::Ident(ident) => RegImmMem::Reg(Reg::parse(ident).ok_or_else(err)?),
Token::Lit(LitTy::Number(num)) => RegImmMem::Imm(parse_imm(num, ctx.span)?), Token::Lit(LitTy::Number(num)) => RegImmMem::Imm(parse_imm(num, ctx.span)?),
_ => return Err(err()), _ => return Err(err()),
}) })
} }
pub fn parse_reg(ctx: &mut crate::parser::ParseCtx) -> Result<RegWH, CompilerMsg> { pub fn parse_reg(ctx: &mut crate::parser::ParseCtx) -> Result<Reg, CompilerMsg> {
let next = ctx.expect_next()?; let next = ctx.expect_next()?;
let err = || CompilerMsg::unexpected_token(&next, ctx.span, "a register"); let err = || CompilerMsg::unexpected_token(&next, ctx.span, "a register");
let Token::Ident(next) = &next else { let Token::Ident(next) = &next else {
return Err(err()); return Err(err());
}; };
RegWH::parse(next).ok_or_else(err) Reg::parse(next).ok_or_else(err)
} }
BIN
View File
Binary file not shown.