Skip to content
Snippets Groups Projects

Misc. improvements

Merged rarbore2 requested to merge misc into main
1 file
+ 22
7
Compare changes
  • Side-by-side
  • Inline
@@ -40,13 +40,9 @@ fn generate_type_string(ty: &Type, rust_types: &Vec<String>) -> String {
Type::UnsignedInteger64 => "u64".to_string(),
Type::Float32 => "f32".to_string(),
Type::Float64 => "f64".to_string(),
Type::Product(fields) => {
fields
.iter()
.map(|field_id| &rust_types[field_id.idx()] as &str)
.fold("(".to_string(), |acc, field| acc + field + ",")
+ ")"
}
Type::Product(fields) => fields.iter().fold("Prod".to_string(), |acc, field| {
format!("{}_{}", acc, field.idx())
}),
Type::Summation(_) => todo!(),
Type::Array(elem, _) => format!("*mut {}", &rust_types[elem.idx()]),
}
@@ -135,6 +131,25 @@ fn codegen(manifest: &ModuleManifest, elf: &[u8]) -> Result<String, anyhow::Erro
}
)?;
// In order to get repr(C), we need to define named structs to attach
// that attribute to. This unfortunately means we can't use anonymous
// products, which would be much nicer than this nonsense.
for id in types_bottom_up(&manifest.types) {
match manifest.types[id.idx()] {
Type::Product(ref fields) => {
write!(
rust_code,
"#[repr(C)] struct {}({});",
rust_types[id.idx()],
fields.iter().fold("".to_string(), |acc, field| {
acc + "," + &rust_types[field.idx()]
})
)?;
}
_ => {}
}
}
// Load the ELF object, and cast the appropriate pointers.
write!(
rust_code,
Loading