arch.h (3445B)
1 /* arch dispatch: selects the target architecture backend at compile time 2 * a new target adds its inst.h below and its arch directory under arch/ 3 * core code includes this header instead of reaching into arch/ directly */ 4 5 #ifndef AS_ARCH_H 6 #define AS_ARCH_H 7 8 #include "../config.h" 9 10 /* arch-specific type definitions: struct inst, struct operand, enum opcode 11 * each inst.h may include ir_asm.h for UNRES_ARCH_BASE to define 12 * arch-specific unresolved kinds */ 13 #if TC_TARGET_ARCH == TC_ARCH_X64 14 #include "arch/x64/inst.h" 15 #elif TC_TARGET_ARCH == TC_ARCH_AARCH64 16 #include "arch/aarch64/inst.h" 17 #elif TC_TARGET_ARCH == TC_ARCH_RISCV64 18 #include "arch/riscv64/inst.h" 19 #else 20 #error "unsupported architecture" 21 #endif 22 23 #include "asm_code.h" /* struct code */ 24 25 /* code buffer macros. call through the arch backend function table */ 26 27 #ifndef MAKE_CODE 28 #define MAKE_CODE(inst, code, ...) \ 29 do { \ 30 unsigned char buf[] = {__VA_ARGS__}; \ 31 arch.make_code(inst, code, buf, sizeof(buf)); \ 32 } while (0) 33 #endif 34 35 #define IM8(x) (x) 36 #define IM16(x) (x), ((x) >> 8) 37 #define IM32(x) (x), ((x) >> 8), ((x) >> 16), ((x) >> 24) 38 #define IM64(x) \ 39 (x), ((x) >> 8), ((x) >> 16), ((x) >> 24), ((x) >> 32), ((x) >> 40), ((x) >> 48), ((x) >> 56) 40 41 struct ParseInfo; 42 struct ParseInstTable; 43 struct SectionInfo; 44 struct Table; 45 struct Vector; 46 47 /* per-arch hooks called from arch-neutral core code. each arch backend 48 * implements these and exports a single arch struct */ 49 struct arch_backend { 50 /* mnemonic string table, null-terminated. indexed by raw opcode */ 51 const char *const *raw_op_table; 52 /* instruction shape table, indexed by raw opcode */ 53 const struct ParseInstTable *parse_inst_table; 54 55 /* scan for instruction prefix keywords (lock, rep, etc). returns 56 * the prefix flag bit or 0 if no prefix found. advances info->p 57 * past the keyword and trailing whitespace when a prefix matches */ 58 unsigned char (*scan_prefix)(struct ParseInfo *info); 59 60 /* parse one operand from the input stream. returns a bitmask of 61 * matched operand-shape flags or 0 on failure */ 62 unsigned int (*parse_operand)( 63 struct ParseInfo *info, unsigned int opr_flag, struct Operand *operand 64 ); 65 66 /* encode a parsed instruction into binary. fills code->buf and code->len */ 67 void (*assemble_inst)(struct Inst *inst, struct ParseInfo *info, struct Code *code); 68 69 /* low-level code buffer filler used by MAKE_CODE macro */ 70 void (*make_code)(struct Inst *inst, struct Code *code, unsigned char *buf, int len); 71 72 /* resolve arch-specific relocations for an IR_CODE node. core calls 73 * this for every code IR during the resolution pass. sets 74 * size_upgraded if a branch was promoted to long form and the 75 * fixpoint needs another iteration */ 76 void (*resolve_code_reloc)( 77 struct IR *ir, 78 struct SectionInfo *section, 79 uint64_t address, 80 uint64_t start_address, 81 struct Table *label_table, 82 struct Vector *unresolved, 83 int *size_upgraded 84 ); 85 }; 86 87 extern const struct arch_backend arch; 88 89 #endif /* AS_ARCH_H */