use std::collections::HashMap;
use super::x86_64_exe_asm::{emit_runtime, Asm, ExeOs, Runtime, RAX, RCX, RDX};
use super::x86_64_exe_obj::{write_elf, write_pe};
use super::{Backend, CodegenError, CodegenResult, Target};
use crate::codegen::ir::*;
use crate::codegen::{GeneratedCode, OutputFormat};
pub(super) fn refuse<T>(what: String) -> CodegenResult<T> {
Err(CodegenError::Unsupported(format!(
"the x86-64 native target does not support {what}"
)))
}
pub struct NativeExeBackend {
os: ExeOs,
}
impl NativeExeBackend {
pub fn new(os: ExeOs) -> Self {
Self { os }
}
}
impl Backend for NativeExeBackend {
fn generate(&mut self, mir: &MirModule) -> CodegenResult<GeneratedCode> {
let bytes = build_executable(mir, self.os)?;
Ok(GeneratedCode::new(OutputFormat::Executable, bytes))
}
fn target(&self) -> Target {
match self.os {
ExeOs::Windows => Target::X86_64Pe,
ExeOs::Linux => Target::X86_64Elf,
}
}
}
pub fn build_executable(mir: &MirModule, os: ExeOs) -> CodegenResult<Vec<u8>> {
let mut a = Asm::default();
let rt = emit_runtime(&mut a, os);
let mut fns = HashMap::new();
for f in &mir.functions {
if f.blocks.is_some() {
fns.insert(f.name.to_string(), (a.label(), f.sig.ret.clone()));
}
}
let Some((main_label, main_ret)) = fns.get("main").cloned() else {
return refuse("a program without `fn main`".into());
};
let entry = a.label();
a.bind(entry);
a.b(&[0x48, 0x83, 0xE4, 0xF0, 0x48, 0x83, 0xEC, 0x20]); a.call(main_label);
let a0 = os.arg_regs()[0];
match main_ret {
MirType::Int(..) => a.mov_rr(a0, RAX),
_ => a.mov_imm(a0, 0),
}
a.call(rt.exit);
let strings: Vec<Vec<u8>> = mir.strings.iter().map(|s| s.as_bytes().to_vec()).collect();
for f in &mir.functions {
if let Some(blocks) = &f.blocks {
let label = fns[f.name.as_ref()].0;
let mut g = FnGen::new(&mut a, os, &rt, &fns, &strings, f)?;
g.a.bind(label);
g.function(blocks)?;
}
}
let linked = a.finish(entry).map_err(CodegenError::Internal)?;
Ok(match os {
ExeOs::Windows => write_pe(&linked),
ExeOs::Linux => {
if !linked.imports.is_empty() {
return Err(CodegenError::Internal("imports in an ELF image".into()));
}
write_elf(&linked)
}
})
}
pub(super) struct FnGen<'a> {
pub(super) a: &'a mut Asm,
pub(super) os: ExeOs,
pub(super) rt: &'a Runtime,
fns: &'a HashMap<String, (usize, MirType)>,
pub(super) strings: &'a [Vec<u8>],
pub(super) f: &'a MirFunction,
slots: HashMap<LocalId, (i32, MirType)>,
blocks: HashMap<BlockId, usize>,
pub(super) str_locals: HashMap<LocalId, usize>,
}
fn supported_local(ty: &MirType) -> bool {
match ty {
MirType::Void | MirType::Bool => true,
MirType::Int(size, _) => *size != IntSize::I128,
MirType::Ptr(inner) => matches!(**inner, MirType::Int(IntSize::I8, _)),
_ => false,
}
}
impl<'a> FnGen<'a> {
fn new(
a: &'a mut Asm,
os: ExeOs,
rt: &'a Runtime,
fns: &'a HashMap<String, (usize, MirType)>,
strings: &'a [Vec<u8>],
f: &'a MirFunction,
) -> CodegenResult<Self> {
let mut str_locals = HashMap::new();
for b in f.blocks.iter().flatten() {
for s in &b.stmts {
if let MirStmtKind::Assign {
dest,
value: MirRValue::Use(MirValue::Const(MirConst::Str(i))),
} = &s.kind
{
str_locals.insert(*dest, *i as usize);
}
}
if let Some(MirTerminator::Call {
func: MirValue::Function(n) | MirValue::Global(n),
args,
dest: Some(d),
..
}) = &b.terminator
{
if n.as_ref() == "build_string_new" {
if let [MirValue::Const(MirConst::Str(i))] = args.as_slice() {
str_locals.insert(*d, *i as usize);
}
}
}
}
let mut slots = HashMap::new();
for (i, l) in f.locals.iter().enumerate() {
let literal = str_locals.contains_key(&l.id)
&& matches!(&l.ty, MirType::Struct(n) if n.as_ref() == "BuildString");
if !supported_local(&l.ty) && !literal {
return refuse(format!("a value of type `{}` in `fn {}`", l.ty, f.name));
}
slots.insert(l.id, (-8 * (i as i32 + 1), l.ty.clone()));
}
Ok(Self {
a,
os,
rt,
fns,
strings,
f,
slots,
blocks: HashMap::new(),
str_locals,
})
}
fn function(&mut self, blocks: &[MirBlock]) -> CodegenResult<()> {
let params: Vec<LocalId> = self
.f
.locals
.iter()
.filter(|l| l.is_param)
.map(|l| l.id)
.collect();
if params.len() > self.os.arg_regs().len() {
return refuse(format!(
"more than {} parameters (`fn {}`)",
self.os.arg_regs().len(),
self.f.name
));
}
let frame = (8 * self.f.locals.len() as u32).div_ceil(16) * 16 + 32;
self.a.prologue(frame);
for (i, p) in params.iter().enumerate() {
let off = self.slot(*p)?.0;
self.a.store(self.os.arg_regs()[i], off);
}
for b in blocks {
let l = self.a.label();
self.blocks.insert(b.id, l);
}
for b in blocks {
self.a.bind(self.blocks[&b.id]);
for s in &b.stmts {
self.stmt(s)?;
}
match &b.terminator {
Some(t) => self.terminator(t)?,
None => {
return refuse(format!(
"a block without a terminator in `fn {}`",
self.f.name
))
}
}
}
Ok(())
}
pub(super) fn slot(&self, id: LocalId) -> CodegenResult<(i32, MirType)> {
self.slots
.get(&id)
.cloned()
.ok_or_else(|| CodegenError::InvalidMir(format!("unknown local {id:?}")))
}
pub(super) fn block(&self, id: BlockId) -> CodegenResult<usize> {
self.blocks
.get(&id)
.copied()
.ok_or_else(|| CodegenError::InvalidMir(format!("unknown block {id:?}")))
}
pub(super) fn value_type(&self, v: &MirValue) -> CodegenResult<MirType> {
Ok(match v {
MirValue::Local(id) => self.slot(*id)?.1,
MirValue::Const(MirConst::Int(_, t) | MirConst::Uint(_, t)) => t.clone(),
MirValue::Const(MirConst::Bool(_)) => MirType::Bool,
MirValue::Const(_) => MirType::Void,
_ => return refuse(format!("the value `{v}` here")),
})
}
pub(super) fn load(&mut self, v: &MirValue) -> CodegenResult<()> {
match v {
MirValue::Local(id) => {
let off = self.slot(*id)?.0;
self.a.load(RAX, off);
}
MirValue::Const(c) => match c {
MirConst::Int(x, t) => {
self.a.mov_imm(RAX, *x as u64);
self.normalize(t);
}
MirConst::Uint(x, t) => {
self.a.mov_imm(RAX, *x as u64);
self.normalize(t);
}
MirConst::Bool(b) => self.a.mov_imm(RAX, *b as u64),
MirConst::Unit | MirConst::Null(_) => self.a.mov_imm(RAX, 0),
MirConst::Str(i) => {
let bytes = self.strings.get(*i as usize).cloned().unwrap_or_default();
let l = self.a.data(&bytes);
self.a.lea_rip(RAX, l);
}
other => return refuse(format!("the constant `{other}`")),
},
MirValue::Global(n) => return refuse(format!("the global `{n}`")),
MirValue::Function(n) => return refuse(format!("`{n}` used as a value")),
}
Ok(())
}
pub(super) fn normalize(&mut self, ty: &MirType) {
let bytes: &[u8] = match ty {
MirType::Int(IntSize::I32, true) => &[0x48, 0x63, 0xC0],
MirType::Int(IntSize::I32, false) => &[0x89, 0xC0],
MirType::Int(IntSize::I16, true) => &[0x48, 0x0F, 0xBF, 0xC0],
MirType::Int(IntSize::I16, false) => &[0x0F, 0xB7, 0xC0],
MirType::Int(IntSize::I8, true) => &[0x48, 0x0F, 0xBE, 0xC0],
MirType::Int(IntSize::I8, false) => &[0x0F, 0xB6, 0xC0],
_ => &[],
};
self.a.b(bytes);
}
pub(super) fn store(&mut self, dest: LocalId) -> CodegenResult<()> {
let (off, ty) = self.slot(dest)?;
self.normalize(&ty);
self.a.store(RAX, off);
Ok(())
}
pub(super) fn call_user(
&mut self,
name: &str,
args: &[MirValue],
dest: Option<LocalId>,
) -> CodegenResult<()> {
let Some((label, _)) = self.fns.get(name).cloned() else {
return refuse(format!(
"a call to `{name}`, which has no native runtime yet"
));
};
let regs = self.os.arg_regs();
if args.len() > regs.len() {
return refuse(format!(
"a call with more than {} arguments (`{name}`)",
regs.len()
));
}
for (i, arg) in args.iter().enumerate() {
self.load(arg)?;
if regs[i] != RAX {
self.a.mov_rr(regs[i], RAX);
}
}
self.a.call(label);
if let Some(d) = dest {
self.store(d)?;
}
Ok(())
}
pub(super) fn load_pair(&mut self, left: &MirValue, right: &MirValue) -> CodegenResult<()> {
self.load(right)?;
self.a.mov_rr(RCX, RAX);
self.load(left)
}
pub(super) fn divide(&mut self, signed: bool, rem: bool) {
if signed {
self.a.b(&[0x48, 0x99, 0x48, 0xF7, 0xF9]); } else {
self.a.b(&[0x31, 0xD2, 0x48, 0xF7, 0xF1]); }
if rem {
self.a.mov_rr(RAX, RDX);
}
}
}