ir_asm.h (2260B)
1 /* intermediate representation for assembly */ 2 3 #ifndef AS_IR_ASM_H 4 #define AS_IR_ASM_H 5 6 #include <stddef.h> /* size_t */ 7 #include <stdint.h> /* uint64_t, int64_t */ 8 9 #include "asm_code.h" /* struct code */ 10 11 #define BYTE_SIZE (1) 12 #define SHORT_SIZE (2) 13 #define LONG_SIZE (4) 14 #define QUAD_SIZE (8) 15 16 struct Expr; 17 struct Name; 18 struct SectionInfo; 19 struct Table; 20 struct Vector; 21 22 /* unresolved relocation categories. the generic kinds are shared across 23 * all arch backends. arch-specific kinds are defined in each archs 24 * inst.h starting from UNRES_ARCH_BASE so they dont collide */ 25 enum UnresolvedKind { 26 /* arch-neutral */ 27 UNRES_EXTERN, 28 UNRES_EXTERN_PC32, 29 UNRES_ABS64, 30 UNRES_CALL, 31 UNRES_PCREL_HI, 32 UNRES_PCREL_LO, 33 UNRES_GOT_HI, 34 UNRES_GOT_LO, 35 /* arch-specific kinds start here */ 36 UNRES_ARCH_BASE = 0x1000, 37 }; 38 39 struct UnresolvedInfo { 40 const struct Name *label; 41 uintptr_t offset; 42 struct SectionInfo *src_section; 43 int add; 44 /* an enum UnresolvedKind, or an architecture's own from UNRES_ARCH_BASE */ 45 int kind; 46 }; 47 48 struct Data { 49 size_t len; 50 unsigned char *buf; 51 }; 52 53 enum IrKind { 54 IR_LABEL, 55 IR_CODE, 56 IR_DATA, 57 IR_BSS, 58 IR_ZERO, 59 IR_ALIGN, 60 IR_EXPR_BYTE, 61 IR_EXPR_SHORT, 62 IR_EXPR_LONG, 63 IR_EXPR_QUAD, 64 }; 65 66 struct IR { 67 enum IrKind kind; 68 union { 69 const struct Name *label; 70 struct Code code; 71 struct Data data; 72 struct { 73 const struct Expr *expr; 74 int64_t addend; /* calculated in resolve_relative_address */ 75 } expr; 76 size_t bss; 77 size_t zero; 78 int align; 79 int section; 80 } u; 81 uint64_t address; 82 }; 83 84 struct IR *new_ir_label(const struct Name *label); 85 struct IR *new_ir_code(const struct Code *code); 86 struct IR *new_ir_data(const void *data, size_t size); 87 struct IR *new_ir_bss(size_t size); 88 struct IR *new_ir_zero(size_t size); 89 struct IR *new_ir_align(int align); 90 struct IR *new_ir_expr(enum IrKind kind, const struct Expr *expr); 91 92 int calc_label_address(uint64_t start_address, struct Vector *sections, struct Table *label_table); 93 int resolve_relative_address( 94 struct Vector *sections, struct Table *label_table, struct Vector *unresolved 95 ); 96 void emit_irs(struct Vector *sections); 97 98 #endif /* AS_IR_ASM_H */