use std::collections::HashMap;
use crate::arch::aarch64::{DecodedInsn, InsnKind, format_text};
use crate::ast::{FnDecl, Stmt};
use crate::ir::Function;
#[must_use]
pub fn build_function(f: &Function<DecodedInsn>, name_at: &HashMap<u64, String>) -> FnDecl {
let mut body = Vec::new();
for block in &f.blocks {
for insn in &block.insns {
body.push(Stmt::asm(format_text(insn), insn.bytes.to_vec()));
if let Some(annotation) = call_or_branch_annotation(insn, name_at) {
body.push(Stmt::Comment(annotation));
}
}
}
FnDecl {
addr: Some(f.addr.0),
name: f.name.clone(),
attrs: Vec::new(),
locals: Vec::new(),
signature: None,
body,
}
}
fn call_or_branch_annotation(insn: &DecodedInsn, name_at: &HashMap<u64, String>) -> Option<String> {
match insn.kind {
InsnKind::BranchLink { target } => name_at.get(&target).map(|n| format!("-> {n}")),
InsnKind::BranchDirect { target } => name_at.get(&target).map(|n| format!("-> {n}")),
_ => None,
}
}