use crate::{
compiler::state::State,
immediate,
x86::{
self, Ins, Reference,
Register::{R12, RAX},
ASM,
},
};
pub fn eval(s: &State, data: &str) -> ASM {
let index = s
.strings
.get(data)
.unwrap_or_else(|| panic!("String `{}` not found in symbol table", data));
x86::lea(RAX, &label(*index), immediate::STR).into()
}
pub fn inline(s: &State) -> ASM {
let mut asm = ASM(vec![]);
for (symbol, index) in &s.strings {
asm += Ins::from("");
asm += Ins::from(".p2align 3");
asm += x86::label(&label(*index));
asm += Ins(format!(".quad {}", symbol.len()));
asm += Ins(format!(".asciz \"{}\"", symbol));
}
asm
}
fn label(index: usize) -> String {
format!("inc_str_{}", index)
}
#[allow(clippy::identity_op)]
pub fn make(_: &State, size: i64) -> ASM {
let aligned = ((size as i64 + 7) / 8) * 8;
x86::mov(Reference::from(R12 + 0), size.into())
+ x86::mov(RAX.into(), R12.into())
+ x86::or(RAX.into(), immediate::STR.into())
+ x86::add(R12.into(), aligned.into())
}