parse_asm.h (5306B)
1 /* parser for assembler */ 2 3 #ifndef AS_PARSE_ASM_H 4 #define AS_PARSE_ASM_H 5 6 #include <stddef.h> /* size_t */ 7 #include <stdint.h> /* int64_t */ 8 9 struct DataStorage; 10 struct Inst; 11 struct Name; 12 struct Operand; 13 struct Table; 14 struct Token; 15 struct Vector; 16 17 enum DirectiveType { 18 NODIRECTIVE, 19 DT_ASCII, 20 DT_STRING, 21 DT_SECTION, 22 DT_TEXT, 23 DT_DATA, 24 DT_BSS, 25 DT_ALIGN, 26 DT_P2ALIGN, 27 DT_BALIGN, 28 DT_TYPE, 29 DT_BYTE, 30 DT_SHORT, 31 DT_LONG, 32 DT_QUAD, 33 DT_COMM, 34 DT_ZERO, 35 DT_GLOBL, 36 DT_LOCAL, 37 DT_WEAK, 38 DT_EXTERN, 39 /* gas integer-data directives. .int/.word and the explicit .nbyte forms 40 * are aliases of the sized data emitters but need their own table slots 41 * because the directive dispatch is table-indexed */ 42 DT_INT, 43 DT_WORD, 44 DT_2BYTE, 45 DT_4BYTE, 46 DT_8BYTE, 47 /* space-filling directives: .fill repeats a sized unit, .space/.skip 48 * reserve a byte run. in a nobits section a zero fill reserves address 49 * space instead of emitting file bytes */ 50 DT_FILL, 51 DT_SPACE, 52 DT_SKIP, 53 /* gas compatibility directives */ 54 DT_SET, /* .set sym, expr */ 55 DT_EQU, /* .equ sym, expr (alias for .set) */ 56 DT_FILE, /* .file (debug info, tolerated) */ 57 DT_LOC, /* .loc (debug info, tolerated) */ 58 DT_IDENT, /* .ident (compiler id string, tolerated) */ 59 DT_PREVIOUS, /* .previous (return to previous section, tolerated) */ 60 #ifndef __NO_FLONUM 61 DT_FLOAT, 62 DT_DOUBLE, 63 #endif 64 #if TC_TARGET_PLATFORM == TC_PLATFORM_APPLE 65 DT_SUBSECTIONS_VIA_SYMBOLS, 66 #endif 67 DT_SIZE, 68 DT_CFI, /* .cfi_* prefix, not in table, tolerated */ 69 }; 70 71 #define SF_EXECUTABLE (1 << 0) 72 #define SF_WRITABLE (1 << 1) 73 #define SF_BSS (1 << 2) 74 #define SF_TLS (1 << 3) 75 #define SF_INIT_FUNCS (1 << 8) 76 #define SF_CSTRLITERALS (1 << 9) 77 #define SF_NOTE (1 << 10) 78 79 struct SectionInfo { 80 const struct Name *name; 81 const char *segname; 82 struct Vector *irs; 83 int flag; 84 85 int index; 86 uint64_t start_address; 87 struct DataStorage *ds; 88 size_t align; 89 size_t bss_size; 90 uint64_t offset; /* struct file offset */ 91 92 int rela_count; 93 void *rela_buf; 94 uint64_t rela_ofs; 95 }; 96 97 struct ParseInfo { 98 const char *filename; 99 const char *rawline; 100 const char *p; 101 int lineno; 102 int error_count; 103 104 const struct Token *prefetched; 105 106 struct Table *section_infos; /* <struct sectioninfo*> */ 107 struct Table *label_table; 108 struct Table *size_expr_table; 109 struct SectionInfo *current_section; 110 }; 111 112 struct SectionInfo * 113 set_current_section(struct ParseInfo *info, const char *name, const char *segname, int flag); 114 struct SectionInfo * 115 get_section_info(struct ParseInfo *info, const char *name, const char *segname, int flag); 116 117 extern const char kSecText[]; 118 extern const char kSecRodata[]; 119 extern const char kSecData[]; 120 extern const char kSecBss[]; 121 122 #if TC_TARGET_PLATFORM == TC_PLATFORM_APPLE 123 extern const char kSegText[]; 124 extern const char kSegRodata[]; 125 extern const char kSegData[]; 126 extern const char kSegBss[]; 127 #else 128 #define kSegText NULL 129 #define kSegRodata NULL 130 #define kSegData NULL 131 #define kSegBss NULL 132 #endif 133 134 struct Line { 135 const struct Name *label; 136 struct Inst *inst; 137 enum DirectiveType dir; 138 }; 139 140 enum ExprKind { 141 EX_LABEL, 142 EX_FIXNUM, 143 EX_POS, 144 EX_NEG, 145 EX_ADD, 146 EX_SUB, 147 EX_MUL, 148 EX_DIV, 149 EX_FLONUM, 150 }; 151 152 #ifndef __NO_FLONUM 153 typedef long double Flonum; 154 #endif 155 156 struct Expr { 157 enum ExprKind kind; 158 union { 159 struct { 160 const struct Name *name; 161 } label; 162 int64_t fixnum; 163 struct { 164 struct Expr *lhs; 165 struct Expr *rhs; 166 } bop; 167 struct { 168 struct Expr *sub; 169 } unary; 170 #ifndef __NO_FLONUM 171 Flonum flonum; 172 #endif 173 } u; 174 }; 175 176 int parse_line(struct Line *line, struct ParseInfo *info); 177 void parse_set_p(struct ParseInfo *info, const char *p); 178 int parse_error(struct ParseInfo *info, const char *message); 179 180 struct ParseOpArray { 181 /* enum opcode */ int op; 182 int opr_flags[4 + 1]; 183 }; 184 185 struct ParseInstTable { 186 int count; 187 const struct ParseOpArray **array; 188 }; 189 190 /* table externs and parse_operand are declared via struct arch_backend 191 * in arch.h */ 192 193 int immediate(const char **pp, int64_t *value); 194 const struct Name *unquote_label(const char *p, const char *q); 195 struct Expr *parse_expr(struct ParseInfo *info); 196 struct Expr *new_expr(enum ExprKind kind); 197 198 struct Value { 199 const struct Name *label; 200 int64_t offset; 201 }; 202 203 struct Value calc_expr(struct Table *label_table, const struct Expr *expr); 204 205 int is_label_first_chr(char c); 206 int is_label_chr(char c); 207 208 #define LF_GLOBAL (1 << 0) 209 #define LF_DEFINED (1 << 1) 210 #define LF_REFERRED (1 << 2) 211 #define LF_COMM (1 << 3) 212 #define LF_WEAK (1 << 4) 213 214 enum LabelKind { 215 LK_NONE, 216 LK_FUNC, 217 LK_OBJECT, 218 }; 219 220 struct LabelInfo { 221 struct SectionInfo *section; 222 int flag; 223 uint64_t address; 224 enum LabelKind kind; 225 int size; 226 int align; 227 }; 228 229 struct LabelInfo *add_label_table( 230 struct Table *label_table, 231 const struct Name *label, 232 struct SectionInfo *section, 233 int define, 234 int global 235 ); 236 237 #endif /* AS_PARSE_ASM_H */