1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#include <assert.h>
#include <elf.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct {
Elf64_Shdr hdr;
int16_t idx;
int64_t wco;
int64_t offset;
size_t capacity;
uint8_t *data;
} Section;
typedef struct {
const char *name;
int32_t idx;
int64_t offset;
int64_t size;
int global;
int defined;
Section *section;
} Symbol;
typedef struct {
Section *section;
Symbol *sym;
int type;
int64_t offset;
} Relocation;
typedef enum {
// Misc
ASM_SYNTAX_ERROR,
ASM_BLANK,
ASM_LABEL,
ASM_IMM,
ASM_STRING,
ASM_MEMARG,
// Directives
ASM_DIR_GLOBL,
ASM_DIR_SECTION,
ASM_DIR_ASCII,
ASM_DIR_ASCIIZ,
ASM_DIR_DATA,
ASM_DIR_TEXT,
ASM_DIR_BYTE,
ASM_DIR_BALIGN,
// Instructions
ASM_NOP,
ASM_RET,
ASM_PUSH,
ASM_POP,
ASM_CALL,
ASM_JMP,
ASM_LEAVE,
ASM_ADD,
ASM_AND,
ASM_DIV,
ASM_IDIV,
ASM_LEA,
ASM_MUL,
ASM_IMUL,
ASM_MOV,
ASM_MOVSX,
ASM_MOVZX,
ASM_OR,
ASM_SUB,
ASM_XCHG,
ASM_XOR,
// Registers, order matters.
ASM_REG_BEGIN,
ASM_AL,
ASM_CL,
ASM_DL,
ASM_BL,
ASM_SPL,
ASM_BPL,
ASM_SIL,
ASM_DIL,
ASM_R8B,
ASM_R9B,
ASM_R10B,
ASM_R11B,
ASM_R12B,
ASM_R13B,
ASM_R14B,
ASM_R15B,
ASM_AX,
ASM_CX,
ASM_DX,
ASM_BX,
ASM_SP,
ASM_BP,
ASM_SI,
ASM_DI,
ASM_R8W,
ASM_R9W,
ASM_R10W,
ASM_R11W,
ASM_R12W,
ASM_R13W,
ASM_R14W,
ASM_R15W,
ASM_EAX,
ASM_ECX,
ASM_EDX,
ASM_EBX,
ASM_ESP,
ASM_EBP,
ASM_ESI,
ASM_EDI,
ASM_R8D,
ASM_R9D,
ASM_R10D,
ASM_R11D,
ASM_R12D,
ASM_R13D,
ASM_R14D,
ASM_R15D,
ASM_RAX,
ASM_RCX,
ASM_RDX,
ASM_RBX,
ASM_RSP,
ASM_RBP,
ASM_RSI,
ASM_RDI,
ASM_R8,
ASM_R9,
ASM_R10,
ASM_R11,
ASM_R12,
ASM_R13,
ASM_R14,
ASM_R15,
/* RIP is in a special class of its own. */
ASM_RIP,
ASM_REG_END,
} AsmKind;
typedef union Parsev Parsev;
typedef struct {
AsmKind kind;
const char *name;
} Label;
typedef struct {
AsmKind kind;
const char *name;
} Globl;
typedef struct {
AsmKind kind;
const char *name;
const char *flags;
int type;
} DirSection;
typedef struct {
AsmKind kind;
uint8_t b;
} Byte;
typedef struct {
AsmKind kind;
uint64_t align;
} Balign;
typedef struct {
AsmKind kind;
uint8_t nbytes;
const char *l; /* label */
int64_t c; /* constant */
} Imm;
typedef struct {
AsmKind kind;
AsmKind reg;
const char *l; /* label */
int64_t c; /* constant */
} Memarg;
typedef struct {
AsmKind kind;
size_t len;
uint8_t *data;
} String;
typedef String Ascii;
typedef String Asciiz;
typedef struct {
AsmKind kind;
const char *target;
} Call;
typedef struct {
AsmKind kind;
const char *target;
} Jmp;
typedef struct {
AsmKind kind;
uint8_t variant;
Parsev *arg1;
Parsev *arg2;
Parsev *arg3;
} Instr;
union Parsev {
AsmKind kind;
Label label;
Globl globl;
DirSection section;
Balign balign;
Ascii ascii;
Asciiz asciiz;
Memarg memarg;
Instr instr;
Call call;
Jmp jmp;
Byte byte;
Imm imm;
String string;
// Temporary values.
const char *charptr;
int64_t i64;
};
typedef struct AsmLine AsmLine;
struct AsmLine {
int64_t lineno;
Parsev v;
AsmLine *next;
};
extern size_t curlineno;
/* util.c */
void lfatal(const char *fmt, ...);
void fatal(const char *fmt, ...);
void unreachable(void);
void *xmalloc(size_t);
void *xrealloc(void *, size_t);
void *xreallocarray(void *, size_t, size_t);
char *xmemdup(const char *, size_t);
char *xstrdup(const char *s);
void *zalloc(size_t n);
struct hashtable {
size_t len, cap;
struct hashtablekey *keys;
void **vals;
};
struct hashtablekey {
uint64_t hash;
const char *str;
size_t len;
};
void htabkey(struct hashtablekey *, const char *, size_t);
struct hashtable *mkhtab(size_t);
void delhtab(struct hashtable *, void(void *));
void **htabput(struct hashtable *, struct hashtablekey *);
void *htabget(struct hashtable *, struct hashtablekey *);
uint64_t murmurhash64a(const void *, size_t);
|