disasm.c (2197B)
1 /* Disassembler dispatcher — peer of src/arch/cgtarget.c. */ 2 3 #include "arch/arch.h" 4 5 ArchDisasm* arch_disasm_new(Compiler* c) { 6 const ArchImpl* arch = arch_for_compiler(c); 7 if (arch && arch->disasm_new) { 8 return arch->disasm_new(c); 9 } 10 { 11 SrcLoc loc = {0, 0, 0}; 12 compiler_panic(c, loc, "arch_disasm_new: unsupported target arch %d", 13 (int)c->target.arch); 14 } 15 } 16 17 u32 arch_disasm_decode(ArchDisasm* d, const u8* bytes, size_t len, u64 vaddr, 18 KitInsn* out) { 19 return d->decode(d, bytes, len, vaddr, out); 20 } 21 22 void arch_disasm_free(ArchDisasm* d) { 23 if (!d) return; 24 if (d->destroy) d->destroy(d); 25 } 26 27 KitStatus arch_decode_one(Compiler* c, const u8* bytes, size_t len, u64 pc, 28 KitDecodedInsn* out) { 29 const ArchImpl* arch; 30 if (!c || !bytes || !out) return KIT_INVALID; 31 arch = arch_for_compiler(c); 32 if (!arch || !arch->decode || !arch->decode->decode_one) 33 return KIT_UNSUPPORTED; 34 return arch->decode->decode_one(c, bytes, len, pc, out); 35 } 36 37 KitStatus arch_decode_block(Compiler* c, const u8* bytes, size_t len, u64 pc, 38 KitDecodedInsn* out, u32 cap, u32* n_out) { 39 const ArchImpl* arch; 40 if (n_out) *n_out = 0; 41 if (!c || !bytes || !out || !n_out) return KIT_INVALID; 42 arch = arch_for_compiler(c); 43 if (!arch || !arch->decode || !arch->decode->decode_block) 44 return KIT_UNSUPPORTED; 45 return arch->decode->decode_block(c, bytes, len, pc, out, cap, n_out); 46 } 47 48 ArchInsnFormatter* arch_insn_formatter_new(Compiler* c) { 49 const ArchImpl* arch = arch_for_compiler(c); 50 if (arch && arch->decode && arch->decode->formatter_new) { 51 return arch->decode->formatter_new(c); 52 } 53 { 54 SrcLoc loc = {0, 0, 0}; 55 compiler_panic(c, loc, 56 "arch_insn_formatter_new: unsupported target arch %d", 57 (int)c->target.arch); 58 } 59 } 60 61 KitStatus arch_format_insn(ArchInsnFormatter* f, const KitDecodedInsn* insn, 62 KitInsn* out) { 63 if (!f || !insn || !out) return KIT_INVALID; 64 return f->format(f, insn, out); 65 } 66 67 void arch_insn_formatter_free(ArchInsnFormatter* f) { 68 if (!f) return; 69 if (f->destroy) f->destroy(f); 70 }