pe work
This commit is contained in:
+21
-9
@@ -9,29 +9,35 @@ pub struct Asm {
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Instr {
|
||||
Mov { dst: RegMode, src: RegImm },
|
||||
Mov { dst: RegMode, src: RegModeImm },
|
||||
Int(u8),
|
||||
Call(Symbol),
|
||||
Ret,
|
||||
Syscall,
|
||||
Lea { dst: RegMode, sym: Symbol },
|
||||
Push(Reg),
|
||||
Push(RegImm),
|
||||
Pop(Reg),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum RegImm {
|
||||
pub enum RegModeImm {
|
||||
Reg(RegMode),
|
||||
Imm(u64),
|
||||
}
|
||||
|
||||
impl From<RegMode> for RegImm {
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum RegImm {
|
||||
Reg(Reg),
|
||||
Imm(u64),
|
||||
}
|
||||
|
||||
impl From<RegMode> for RegModeImm {
|
||||
fn from(value: RegMode) -> Self {
|
||||
Self::Reg(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for RegImm {
|
||||
impl From<u64> for RegModeImm {
|
||||
fn from(value: u64) -> Self {
|
||||
Self::Imm(value)
|
||||
}
|
||||
@@ -39,7 +45,7 @@ impl From<u64> for RegImm {
|
||||
|
||||
mod fns {
|
||||
use super::*;
|
||||
pub fn mov(dst: RegMode, src: impl Into<RegImm>) -> Instr {
|
||||
pub fn mov(dst: RegMode, src: impl Into<RegModeImm>) -> Instr {
|
||||
Instr::Mov {
|
||||
dst,
|
||||
src: src.into(),
|
||||
@@ -50,9 +56,15 @@ mod fns {
|
||||
Instr::Lea { dst, sym }
|
||||
}
|
||||
|
||||
pub fn push(reg: RegMode) -> Instr {
|
||||
assert!(reg.width == BitWidth::B64);
|
||||
Instr::Push(reg.reg)
|
||||
pub fn push(reg: impl Into<RegModeImm>) -> Instr {
|
||||
Instr::Push(match reg.into() {
|
||||
RegModeImm::Reg(reg) => {
|
||||
assert_eq!(reg.width, BitWidth::B64);
|
||||
assert!(!reg.high);
|
||||
RegImm::Reg(reg.reg)
|
||||
}
|
||||
RegModeImm::Imm(imm) => RegImm::Imm(imm),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn pop(reg: RegMode) -> Instr {
|
||||
|
||||
@@ -50,7 +50,7 @@ fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMs
|
||||
impl Encoder {
|
||||
// assembly
|
||||
|
||||
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegImm>) -> Result<(), CompilerMsg> {
|
||||
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegModeImm>) -> Result<(), CompilerMsg> {
|
||||
let src = src.into();
|
||||
let width = dst.width;
|
||||
if width == BitWidth::B16 {
|
||||
@@ -59,7 +59,7 @@ impl Encoder {
|
||||
let dst8 = dst.gt8();
|
||||
let b64 = width == BitWidth::B64;
|
||||
let b8 = width == BitWidth::B8;
|
||||
let src8 = if let RegImm::Reg(src) = src {
|
||||
let src8 = if let RegModeImm::Reg(src) = src {
|
||||
src.gt8()
|
||||
} else {
|
||||
false
|
||||
@@ -70,7 +70,7 @@ impl Encoder {
|
||||
.push(0x40 | dst8 as u8 | ((b64 as u8) << 3) | ((src8 as u8) << 2));
|
||||
}
|
||||
match src {
|
||||
RegImm::Reg(src) => {
|
||||
RegModeImm::Reg(src) => {
|
||||
if dst.width != src.width {
|
||||
return Err("src and dst are not the same size".into());
|
||||
}
|
||||
@@ -78,7 +78,7 @@ impl Encoder {
|
||||
let modrm = 0b11_000_000 | (src.base() << 3) | dst.base();
|
||||
self.data.push(modrm);
|
||||
}
|
||||
RegImm::Imm(imm) => {
|
||||
RegModeImm::Imm(imm) => {
|
||||
if imm > width.max() {
|
||||
return Err("immediate cannot fit in register".into());
|
||||
}
|
||||
@@ -122,6 +122,17 @@ impl Encoder {
|
||||
self.data.push(0x50 | reg.base());
|
||||
}
|
||||
|
||||
pub fn push_imm(&mut self, imm: u64) {
|
||||
const U8: u64 = 2 << 8;
|
||||
if let 0..U8 = imm {
|
||||
self.data.push(0x6a);
|
||||
self.data.push(imm as u8);
|
||||
} else {
|
||||
self.data.push(0x68);
|
||||
self.data.extend(imm.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop(&mut self, reg: Reg) {
|
||||
if reg.gt8() {
|
||||
self.data.push(0x41);
|
||||
@@ -148,7 +159,10 @@ impl Encoder {
|
||||
Instr::Lea { dst, sym } => self.lea(dst, sym),
|
||||
Instr::Call(sym) => self.call(sym),
|
||||
Instr::Ret => self.ret(),
|
||||
Instr::Push(reg) => self.push(reg),
|
||||
Instr::Push(val) => match val {
|
||||
RegImm::Reg(reg) => self.push(reg),
|
||||
RegImm::Imm(imm) => self.push_imm(imm),
|
||||
},
|
||||
Instr::Pop(reg) => self.pop(reg),
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -8,7 +8,7 @@ pub struct RegMode {
|
||||
pub high: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum BitWidth {
|
||||
B64,
|
||||
B32,
|
||||
|
||||
+38
-10
@@ -5,6 +5,11 @@ use crate::{
|
||||
use std::{fs::OpenOptions, io::Write, os::unix::fs::OpenOptionsExt, process::Command};
|
||||
|
||||
pub fn run() {
|
||||
windows();
|
||||
// linux();
|
||||
}
|
||||
|
||||
fn linux() {
|
||||
let mut program = Program::<X86_64>::default();
|
||||
let text = b"Hello world!\n";
|
||||
let text_sym = program.ro_data(text);
|
||||
@@ -39,16 +44,7 @@ pub fn run() {
|
||||
let linked = program.compile().expect("failed to compile");
|
||||
let binary = linked.to_elf();
|
||||
let path = "./x86_64_test";
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.mode(0o750)
|
||||
.open(path)
|
||||
.expect("Failed to create file");
|
||||
file.write_all(&binary).expect("Failed to write to file");
|
||||
file.sync_all().expect("Failed to sync file");
|
||||
drop(file);
|
||||
write(path, &binary);
|
||||
println!("running...");
|
||||
let gdb = false;
|
||||
let mut proc = if gdb {
|
||||
@@ -72,3 +68,35 @@ pub fn run() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
}
|
||||
|
||||
fn windows() {
|
||||
let mut program = Program::<X86_64>::default();
|
||||
let entry = program.func([BInstr::Asm(Asm {
|
||||
instrs: vec![push(39), pop(rax), Instr::Ret],
|
||||
})]);
|
||||
program.entry = Some(entry);
|
||||
let linked = program.compile().expect("failed to compile");
|
||||
let binary = linked.to_pe();
|
||||
let path = "./x86_64_test.exe";
|
||||
write(path, &binary);
|
||||
|
||||
let mut cmd = Command::new("wine");
|
||||
cmd.arg("x86_64_test");
|
||||
let mut proc = cmd.spawn().expect("failed to run");
|
||||
let status = proc.wait().expect("failed to wait");
|
||||
if let Some(code) = status.code() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
}
|
||||
|
||||
fn write(path: &str, binary: &[u8]) {
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.write(true)
|
||||
.truncate(true)
|
||||
.mode(0o750)
|
||||
.open(path)
|
||||
.expect("Failed to create file");
|
||||
file.write_all(binary).expect("Failed to write to file");
|
||||
file.sync_all().expect("Failed to sync file");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user