compile help script

fix issue where mir interpreted intern indices as data for constants
This commit is contained in:
Janis 2024-12-23 03:08:13 +01:00
parent 7c01afbf2f
commit d743292710
3 changed files with 42 additions and 121 deletions

View file

@ -868,7 +868,12 @@ impl Mir {
node node
} }
pub fn push_const(&mut self, value: intern::Index, ty: Type) -> u32 { pub fn push_const(
&mut self,
ip: &intern::InternPool,
value: intern::Index,
ty: Type,
) -> u32 {
let inst = match ty { let inst = match ty {
Type::Byte => Inst::ConstantByte, Type::Byte => Inst::ConstantByte,
Type::Word => Inst::ConstantWord, Type::Word => Inst::ConstantWord,
@ -878,7 +883,15 @@ impl Mir {
Type::DoublePrecision => Inst::ConstantDoublePrecision, Type::DoublePrecision => Inst::ConstantDoublePrecision,
_ => unreachable!(), _ => unreachable!(),
}; };
let data = Data::index(value); let data = match ip.get_key(value) {
intern::Key::F32 { bits } => Data::imm32(bits.to_bits()),
intern::Key::F64 { bits } => Data::imm64(bits.to_bits()),
intern::Key::SInt64 { bits } => Data::imm64(bits as u64),
intern::Key::UInt64 { bits } => Data::imm64(bits as u64),
intern::Key::SIntSmall { bits } => Data::imm32(bits as u32),
intern::Key::UIntSmall { bits } => Data::imm32(bits as u32),
_ => unreachable!(),
};
self.push(inst, data) self.push(inst, data)
} }
@ -2542,8 +2555,14 @@ impl Function {
) { ) {
match inst { match inst {
Inst::ConstantBytes => { Inst::ConstantBytes => {
_ = strings; use std::fmt::Write;
todo!() let data = strings.get_bytes(data.as_index());
let mut str = String::new();
write!(&mut str, ".byte ").unwrap();
for b in data {
write!(&mut str, "{b:>02x},").unwrap();
}
self.add_constant(i, str);
} }
Inst::ConstantByte => { Inst::ConstantByte => {
self.add_constant(i, format!(".byte {}", data.as_imm8())); self.add_constant(i, format!(".byte {}", data.as_imm8()));
@ -2703,10 +2722,18 @@ impl Mir {
} }
Inst::ExternRef(ty) => { Inst::ExternRef(ty) => {
// let ty = ty.unwrap_or(Type::QWord); // let ty = ty.unwrap_or(Type::QWord);
ImmRegMem::Rip(RipRelative::Reference(format!( match ty {
"{}", None | Some(Type::Function) => {
strings.get_str(data.as_index()) ImmRegMem::Rip(RipRelative::Reference(format!(
))) "{}",
strings.get_str(data.as_index())
)))
}
Some(ty) => ImmRegMem::Rip(RipRelative::Label(
ty,
format!("{}", strings.get_str(data.as_index())),
)),
}
} }
_ => { _ => {
unreachable!() unreachable!()

View file

@ -162,12 +162,6 @@ pub enum Inst {
/// (name: intern, ty: intern) /// (name: intern, ty: intern)
/// lhs: value node /// lhs: value node
GlobalConstant(intern::Index, intern::Index), GlobalConstant(intern::Index, intern::Index),
/// u32
ConstantU32,
/// lo, hi
ConstantU64,
/// index
ConstantMultiByte,
/// type, value /// type, value
Constant, Constant,
/// size, align /// size, align
@ -235,10 +229,7 @@ pub enum Inst {
impl Inst { impl Inst {
fn is_constant(self) -> bool { fn is_constant(self) -> bool {
match self { match self {
Inst::ConstantU32 Inst::Constant => true,
| Inst::ConstantU64
| Inst::ConstantMultiByte
| Inst::Constant => true,
_ => false, _ => false,
} }
@ -962,34 +953,6 @@ impl IRBuilder<'_, '_> {
self.intern_pool().display_key(val), self.intern_pool().display_key(val),
)?; )?;
} }
Inst::ConstantU32 => {
writeln_indented!(
indent,
w,
"%{} = const i32 {}",
node,
data.as_u32()
)?;
}
Inst::ConstantU64 => {
writeln_indented!(
indent,
w,
"%{} = const i64 {}",
node,
data.as_u64()
)?;
}
Inst::ConstantMultiByte => {
let value = self.intern_pool().get_bytes(data.as_index());
writeln_indented!(
indent,
w,
"%{} = const bytes {:x?}",
node,
value
)?;
}
Inst::Add(ty) => { Inst::Add(ty) => {
let (lhs, rhs) = data.as_lhs_rhs(); let (lhs, rhs) = data.as_lhs_rhs();
writeln_indented!( writeln_indented!(
@ -1414,43 +1377,7 @@ impl<'a> MirBuilder<'a> {
let mir_ty = self let mir_ty = self
.intern_pool() .intern_pool()
.to_mir_type(ty, AMD64_POINTER_TYPE_INFO); .to_mir_type(ty, AMD64_POINTER_TYPE_INFO);
mir.push_const(val, mir_ty) mir.push_const(self.intern_pool(), val, mir_ty)
}
Inst::ConstantU32 => mir.push(
mir::Inst::ConstantDWord,
mir::Data::imm32(data.unwrap().as_u32()),
),
Inst::ConstantU64 => mir.push(
mir::Inst::ConstantQWord,
mir::Data::imm64(data.unwrap().as_u64()),
),
Inst::ConstantMultiByte => {
let bytes =
self.intern_pool().get_bytes(data.unwrap().as_index());
let mut buf = [0u8; 8];
match bytes.len() {
1 => mir.gen_u8(bytes[0]),
2 => mir.gen_u16(u16::from_le_bytes(
bytes[..2].try_into().unwrap(),
)),
3..=4 => {
buf[..bytes.len()].copy_from_slice(bytes);
mir.gen_u32(u32::from_le_bytes(
buf[..4].try_into().unwrap(),
))
}
5..=8 => {
buf[..bytes.len()].copy_from_slice(bytes);
mir.gen_u64(u64::from_le_bytes(
buf[..8].try_into().unwrap(),
))
}
_ => {
unimplemented!(
"constants larger than 8 bytes are not currently supported!"
)
}
}
} }
Inst::Alloca => { Inst::Alloca => {
let (l, r) = data.unwrap().as_lhs_rhs(); let (l, r) = data.unwrap().as_lhs_rhs();
@ -2017,44 +1944,7 @@ impl<'a> MirBuilder<'a> {
let mir_ty = self let mir_ty = self
.intern_pool() .intern_pool()
.to_mir_type(ty, AMD64_POINTER_TYPE_INFO); .to_mir_type(ty, AMD64_POINTER_TYPE_INFO);
mir.push_const(val, mir_ty) mir.push_const(self.intern_pool(), val, mir_ty)
}
Inst::ConstantU32 => mir.push(
mir::Inst::ConstantDWord,
mir::Data::imm32(data.unwrap().as_u32()),
),
Inst::ConstantU64 => mir.push(
mir::Inst::ConstantQWord,
mir::Data::imm64(data.unwrap().as_u64()),
),
Inst::ConstantMultiByte => {
let bytes = self
.intern_pool()
.get_bytes(data.unwrap().as_index());
let mut buf = [0u8; 8];
match bytes.len() {
1 => mir.gen_u8(bytes[0]),
2 => mir.gen_u16(u16::from_le_bytes(
bytes[..2].try_into().unwrap(),
)),
3..=4 => {
buf[..bytes.len()].copy_from_slice(bytes);
mir.gen_u32(u32::from_le_bytes(
buf[..4].try_into().unwrap(),
))
}
5..=8 => {
buf[..bytes.len()].copy_from_slice(bytes);
mir.gen_u64(u64::from_le_bytes(
buf[..8].try_into().unwrap(),
))
}
_ => {
unimplemented!(
"constants larger than 8 bytes are not currently supported!"
)
}
}
} }
_ => { _ => {
unimplemented!() unimplemented!()

4
test.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/bash
cargo run --bin main -- -i "$1" asm > asm.S
clang asm.S -c && clang asm.o -o asm && ./asm; echo $?