aboutsummaryrefslogtreecommitdiff
path: root/minias.h
blob: 42b3e62f8e1615a7b95c1eefa42d146ad653ba8b (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#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;
  int64_t addend;
} 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_FILL,
  ASM_DIR_BYTE,
  ASM_DIR_SHORT,
  ASM_DIR_INT,
  ASM_DIR_QUAD,
  ASM_DIR_BALIGN,
  // Instructions
  ASM_NOP,
  ASM_RET,
  ASM_PUSH,
  ASM_POP,
  ASM_CALL,
  ASM_CLTD,
  ASM_CQTO,
  ASM_JMP,
  ASM_LEAVE,
  ASM_ADD,
  ASM_AND,
  ASM_CMP,
  ASM_CVTSS2SD,
  ASM_CVTSD2SS,
  ASM_CVTSI2SD,
  ASM_CVTSI2SS,
  ASM_DIV,
  ASM_IDIV,
  ASM_LEA,
  ASM_MUL,
  ASM_MULSD,
  ASM_MULSS,
  ASM_IMUL,
  ASM_MOV,
  ASM_MOVAPS,
  ASM_MOVSD,
  ASM_MOVSS,
  ASM_MOVSX,
  ASM_MOVZX,
  ASM_NEG,
  ASM_OR,
  ASM_PXOR,
  ASM_SAL,
  ASM_SAR,
  ASM_SET,
  ASM_SHL,
  ASM_SHR,
  ASM_SUB,
  ASM_TEST,
  ASM_UCOMISD,
  ASM_UCOMISS,
  ASM_XCHG,
  ASM_XOR,
  ASM_XORPD,
  ASM_XORPS,
  // 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,

  ASM_XMM0,
  ASM_XMM1,
  ASM_XMM2,
  ASM_XMM3,
  ASM_XMM4,
  ASM_XMM5,
  ASM_XMM6,
  ASM_XMM7,
  ASM_XMM8,
  ASM_XMM9,
  ASM_XMM10,
  ASM_XMM11,
  ASM_XMM12,
  ASM_XMM13,
  ASM_XMM14,
  ASM_XMM15,
  
  /* RIP is in a special class of its own. */
  ASM_RIP,
  ASM_NO_REG,

  ASM_REG_END,
} AsmKind;


typedef union Parsev Parsev;

typedef struct Label {
  AsmKind kind;
  const char *name;
} Label;

typedef struct Globl {
  AsmKind kind;
  const char *name;
} Globl;

typedef struct DirSection {
  AsmKind kind;
  int32_t type;
  const char *name;
  const char *flags;
} DirSection;

typedef struct {
  int64_t c;
  const char *l;
} Value;

typedef struct Byte {
  AsmKind kind;
  Value value;
} Byte;

typedef struct Short {
  AsmKind kind;
  Value value;
} Short;

typedef struct Int {
  AsmKind kind;
  Value value;
} Int;

typedef struct Quad {
  AsmKind kind;
  Value value;
} Quad;

typedef struct Balign {
  AsmKind kind;
  uint64_t align;
} Balign;

typedef struct Fill {
  AsmKind kind;
  int32_t size;
  int32_t repeat;
  int64_t value;
} Fill;

typedef struct Imm {
  AsmKind kind;
  uint32_t nbytes;
  Value v;
} Imm;

typedef struct Memarg {
  AsmKind kind;
  AsmKind base;
  AsmKind index;
  uint32_t scale;
  Value disp;
} Memarg;

typedef struct String {
  AsmKind kind;
  size_t len;
  uint8_t *data;
} String;
typedef String Ascii;
typedef String Asciiz;

typedef struct Call {
  AsmKind kind;
  uint32_t indirect;
  union {
    const Parsev *indirect;
    Value  direct;
  } target;
} Call;

typedef struct Jmp {
  AsmKind kind;
  uint32_t variant;
  const char *target;
} Jmp;

typedef struct Instr {
  AsmKind kind;
  uint32_t variant;
  const Parsev *arg1;
  const Parsev *arg2;
  const 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;
  Fill fill;
  Byte dirbyte;
  Short dirshort;
  Int dirint;
  Quad dirquad;
  Imm imm;
  String string;
  // Temporary values.
  Value value;
  const char *charptr;
  int64_t i64;
};

extern size_t curlineno;

/* parse.c */

typedef struct AsmLine AsmLine;
struct AsmLine {
  int64_t lineno;
  Parsev v;
  AsmLine *next;
};

AsmLine *parse(void);

/* 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);