mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
Extract remain assembly to asm.rs
This commit is contained in:
parent
0fcb1df344
commit
ec4e19d01f
2 changed files with 30 additions and 9 deletions
|
@ -22,7 +22,8 @@ pub enum Register {
|
|||
R15,
|
||||
}
|
||||
|
||||
const REX_W: u8 = 0x48;
|
||||
const REX: u8 = 0x40;
|
||||
const REX_W: u8 = REX + 0x8;
|
||||
|
||||
fn add_rm_extension(reg: Register, byte: u8) -> u8 {
|
||||
if reg as u8 > 7 {
|
||||
|
@ -68,3 +69,27 @@ pub fn mov_register64bit_register64bit<'a>(buf: &mut Vec<'a, u8>, dst: Register,
|
|||
let src_mod = (src as u8 % 8) << 3;
|
||||
buf.extend(&[rex, 0x89, 0xC0 + dst_mod + src_mod]);
|
||||
}
|
||||
|
||||
pub fn ret_near<'a>(buf: &mut Vec<'a, u8>) {
|
||||
buf.push(0xC3);
|
||||
}
|
||||
|
||||
pub fn pop_register64bit<'a>(buf: &mut Vec<'a, u8>, reg: Register) {
|
||||
if reg as u8 > 7 {
|
||||
let rex = add_reg_extension(reg, REX);
|
||||
let reg_mod = reg as u8 % 8;
|
||||
buf.extend(&[rex, 0x58 + reg_mod]);
|
||||
} else {
|
||||
buf.push(0x50 + reg as u8);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_register64bit<'a>(buf: &mut Vec<'a, u8>, reg: Register) {
|
||||
if reg as u8 > 7 {
|
||||
let rex = add_reg_extension(reg, REX);
|
||||
let reg_mod = reg as u8 % 8;
|
||||
buf.extend(&[rex, 0x50 + reg_mod]);
|
||||
} else {
|
||||
buf.push(0x50 + reg as u8);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,19 +57,15 @@ impl<'a> Backend<'a> for X86_64Backend<'a> {
|
|||
// TODO: handle allocating and cleaning up data on the stack.
|
||||
let mut out = bumpalo::vec![in self.env.arena];
|
||||
if !self.leaf_proc {
|
||||
// push rbp (0x55)
|
||||
// mov rbp, rsp (0x48, 0x89, 0xE5)
|
||||
out.extend(&[0x55]);
|
||||
asm::mov_register64bit_register64bit(&mut out, Register::RBP, Register::RSP)
|
||||
asm::push_register64bit(&mut out, Register::RBP);
|
||||
asm::mov_register64bit_register64bit(&mut out, Register::RBP, Register::RSP);
|
||||
}
|
||||
out.extend(&self.buf);
|
||||
|
||||
if !self.leaf_proc {
|
||||
// pop rbp
|
||||
out.push(0x5D);
|
||||
asm::pop_register64bit(&mut out, Register::RBP);
|
||||
}
|
||||
// ret
|
||||
out.push(0xC3);
|
||||
asm::ret_near(&mut out);
|
||||
|
||||
out.into_bump_slice()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue