asm.h (2935B)
1 #ifndef KIT_ARCH_AA64_ASM_H 2 #define KIT_ARCH_AA64_ASM_H 3 4 /* AArch64 standalone .s instruction parser. 5 * 6 * Owns the per-mnemonic operand grammar (registers, immediates, shift / 7 * extend modifiers, memory addressing). Reads tokens from the lexer 8 * the asm driver hands it, emits encoded words through MCEmitter, and 9 * issues relocations against ObjSymIds for symbolic operands. 10 * 11 * The driver exposes a tiny per-arch handle (AA64Asm) plus a single 12 * entry point (aa64_asm_insn) called for each mnemonic line. Symbol 13 * resolution and label management live on the driver side. */ 14 15 #include "asm/asm_lex.h" 16 #include "core/core.h" 17 18 typedef struct AsmDriver AsmDriver; 19 typedef struct ArchAsm ArchAsm; 20 21 typedef struct AA64Asm AA64Asm; 22 23 /* Private pseudo operand used by the aa64 inline-asm binder. Semantic CG 24 * operands never expose physical registers, so native.c lowers register 25 * constraints into this arch-private shape before template substitution. 26 * Operand.v.local carries the 5-bit physical register number; pad[0] carries 27 * AA64_INLINE_OPCLS_*. 28 */ 29 enum { 30 AA64_INLINE_OPK_REG = 0xf0u, 31 AA64_INLINE_OPCLS_INT = 0u, 32 AA64_INLINE_OPCLS_FP = 1u, 33 }; 34 35 /* Construct/destroy. Pure: no allocations beyond the AA64Asm struct 36 * itself (which lives on the compiler's TU arena). */ 37 AA64Asm* aa64_asm_open(Compiler* c); 38 void aa64_asm_close(AA64Asm*); 39 ArchAsm* aa64_arch_asm_new(Compiler* c); 40 41 /* Parse one mnemonic line. `mnemonic` is the first identifier on the 42 * line (or "b.cond" composite). The driver has already consumed the 43 * mnemonic identifier and any trailing dot-suffix. This function 44 * consumes operands up to (but not including) the next ASM_TOK_NEWLINE or 45 * ASM_TOK_EOF, and writes the encoded instruction(s) through the driver's 46 * MCEmitter. Diagnostics on parse failure go through compiler_panic. */ 47 void aa64_asm_insn(AA64Asm*, AsmDriver*, Sym mnemonic); 48 49 /* ---- inline-asm entry points (Phase 4b Track C) ---- */ 50 51 #include "arch/arch.h" 52 53 /* Bind the operand arrays + clobbers from the cg-side asm_block call onto 54 * the AA64Asm handle. The arrays are borrowed for the lifetime of the 55 * subsequent aa64_asm_run_template call; the caller owns the storage. 56 * 57 * Operand indexing follows the GCC convention: outputs are indexed 58 * 0..nout-1, then inputs nout..nout+nin-1. Template placeholders %N 59 * resolve into this combined list. */ 60 void aa64_inline_bind(AA64Asm*, const AsmConstraint* outs, u32 nout, 61 Operand* out_ops, const AsmConstraint* ins, u32 nin, 62 const Operand* in_ops, const Sym* clobbers, u32 nclob); 63 64 /* Walk the inline-asm template, substituting placeholders into per-line 65 * source text and re-lexing each line through aa64_asm_insn. Must be 66 * called after aa64_inline_bind. Emits into `mc` (must equal the 67 * MCEmitter the caller's CGTarget is using). */ 68 void aa64_asm_run_template(AA64Asm*, MCEmitter* mc, const char* tmpl); 69 70 #endif