asm_helpers.h (2335B)
1 #ifndef KIT_ASM_HELPERS_H 2 #define KIT_ASM_HELPERS_H 3 4 /* Lightweight asm-driver surface consumed by per-arch instruction 5 * parsers. The driver itself is opaque to per-arch code; these helpers 6 * are the only seam. Implementations live in src/asm/asm.c. */ 7 8 #include "arch/arch.h" 9 #include "asm/asm_lex.h" 10 #include "core/core.h" 11 #include "obj/obj.h" 12 13 typedef struct AsmDriver AsmDriver; 14 15 /* ---- token plumbing ---- */ 16 AsmTok asm_driver_peek(AsmDriver*); 17 AsmTok asm_driver_next(AsmDriver*); 18 int asm_driver_at_eol(AsmDriver*); 19 int asm_driver_tok_is_punct(AsmTok t, u32 p); 20 int asm_driver_eat_comma(AsmDriver*); 21 int asm_driver_eat_punct(AsmDriver*, u32 punct); 22 void asm_driver_expect_punct(AsmDriver*, u32 punct, const char* what); 23 24 /* Source position for diagnostics. */ 25 SrcLoc asm_driver_loc(AsmDriver*); 26 27 /* Owning subsystems. */ 28 MCEmitter* asm_driver_mc(AsmDriver*); 29 ObjBuilder* asm_driver_ob(AsmDriver*); 30 Compiler* asm_driver_compiler(AsmDriver*); 31 Pool* asm_driver_pool(AsmDriver*); 32 ObjSecId asm_driver_cur_section(AsmDriver*); 33 34 /* Diagnostics: emits then longjmps via Compiler.panic. No return. */ 35 _Noreturn void asm_driver_panic(AsmDriver*, const char* fmt, ...); 36 37 /* ---- symbol + expression parsing ---- */ 38 ObjSymId asm_driver_intern_sym(AsmDriver*, Sym name); 39 40 /* Parse a constant integer expression. Panics if the expression 41 * references a symbol. */ 42 i64 asm_driver_parse_const(AsmDriver*); 43 44 /* Parse a `sym ± const` expression. Both outputs valid: pure constants 45 * leave *sym_out == OBJ_SYM_NONE. */ 46 void asm_driver_parse_sym_expr(AsmDriver*, ObjSymId* sym_out, i64* off_out); 47 48 /* ---- inline-asm constructor ---- 49 * 50 * Build an AsmDriver around a memory-backed AsmLexer + caller-supplied 51 * MCEmitter. Used by inline-asm template walkers (one driver per asm 52 * line) to reuse the existing per-arch instruction parsers verbatim 53 * over a substituted source buffer. 54 * 55 * The driver is heap-allocated through c->ctx->heap and must be released 56 * with asm_driver_close_inline. It does not own the AsmLexer or the 57 * MCEmitter — the caller retains ownership of both. The driver does 58 * not initialize a default section; inline asm always emits into the 59 * MCEmitter's currently-active section. */ 60 AsmDriver* asm_driver_open_inline(Compiler*, MCEmitter*, AsmLexer*); 61 void asm_driver_close_inline(AsmDriver*); 62 63 #endif