-
Notifications
You must be signed in to change notification settings - Fork 5
/
x86_64-linux.brg
384 lines (340 loc) · 9.23 KB
/
x86_64-linux.brg
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
374
375
376
377
378
379
380
381
382
383
384
%{
#include "compat.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include "cc.h"
#define MAX_STRUCT_PARAM_SIZE 16
typedef struct tree TREE_TYPE;
#define LEFT_KID(p) ((p)->kids[0])
#define RIGHT_KID(p) ((p)->kids[1])
#define TREE_OP(p) ((p)->op)
#define TREE_STATE(p) ((p)->x.state)
#define burg(name) burg_##name
enum {
RAX, RBX, RCX, RDX, RSI, RDI,
R8, R9, R10, R11, R12, R13, R14, R15,
NUM_IREGS
};
enum {
XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15,
NUM_FREGS
};
#define NUM_IARG_REGS 6
#define NUM_FARG_REGS 8
#define NUM_IRET_REGS 2
#define NUM_FRET_REGS 2
#define REG_SAVE_AREA_SIZE (NUM_IARG_REGS * 8 + NUM_FARG_REGS * 16)
#define STACK_PARAM_BASE_OFF 16
static struct symbol *iregs[NUM_IREGS];
static struct symbol *fregs[NUM_FREGS];
static struct symbol *rsp, *rbp, *rip;
static struct symbol *iarg_regs[NUM_IARG_REGS];
static struct symbol *farg_regs[NUM_FARG_REGS];
static struct symbol *iret_regs[NUM_IRET_REGS];
static struct symbol *fret_regs[NUM_FRET_REGS];
static int cseg;
%}
%term ADDRGP8=8259
%term ADDRPP8=8275
%term ADDRLP8=8291
%term INDIRI1=1137
%term INDIRI2=2161
%term INDIRI4=4209
%term INDIRI8=8305
%term INDIRU1=1138
%term INDIRU2=2162
%term INDIRU4=4210
%term INDIRU8=8306
%term INDIRF4=4211
%term INDIRF8=8307
%term INDIRF16=16499
%term INDIRP8=8308
%term INDIRS=117
%term ASGNI1=1153
%term ASGNI2=2177
%term ASGNI4=4225
%term ASGNI8=8321
%term ASGNU1=1154
%term ASGNU2=2178
%term ASGNU4=4226
%term ASGNU8=8322
%term ASGNF4=4227
%term ASGNF8=8323
%term ASGNF16=16515
%term ASGNP8=8324
%term ASGNS=133
%term ADDI4=4273
%term ADDI8=8369
%term ADDU4=4274
%term ADDU8=8370
%term ADDF4=4275
%term ADDF8=8371
%term ADDF16=16563
%term ADDP8=8372
%%
stmt: ASGNI4(reg, mem) ""
reg: ADDI4(reg, mem) ""
mem4: INDIRI4(addr) ""
addr: ADDRLP8 ""
%%
static void init(int argc, char *argv[])
{
iregs[RAX] = mkreg("%rax", RAX, IREG);
reg_alias(iregs[RAX], Q, "%rax");
reg_alias(iregs[RAX], L, "%eax");
reg_alias(iregs[RAX], W, "%ax");
reg_alias(iregs[RAX], B, "%al");
iregs[RBX] = mkreg("%rbx", RBX, IREG);
reg_alias(iregs[RBX], Q, "%rbx");
reg_alias(iregs[RBX], L, "%ebx");
reg_alias(iregs[RBX], W, "%bx");
reg_alias(iregs[RBX], B, "%bl");
iregs[RCX] = mkreg("%rcx", RCX, IREG);
reg_alias(iregs[RCX], Q, "%rcx");
reg_alias(iregs[RCX], L, "%ecx");
reg_alias(iregs[RCX], W, "%cx");
reg_alias(iregs[RCX], B, "%cl");
iregs[RDX] = mkreg("%rdx", RDX, IREG);
reg_alias(iregs[RDX], Q, "%rdx");
reg_alias(iregs[RDX], L, "%edx");
reg_alias(iregs[RDX], W, "%dx");
reg_alias(iregs[RDX], B, "%dl");
iregs[RSI] = mkreg("%rsi", RSI, IREG);
reg_alias(iregs[RSI], Q, "%rsi");
reg_alias(iregs[RSI], L, "%esi");
reg_alias(iregs[RSI], W, "%si");
reg_alias(iregs[RSI], B, "%sil");
iregs[RDI] = mkreg("%rdi", RDI, IREG);
reg_alias(iregs[RDI], Q, "%rdi");
reg_alias(iregs[RDI], L, "%edi");
reg_alias(iregs[RDI], W, "%di");
reg_alias(iregs[RDI], B, "%dil");
for (int i = R8, j = 8; i <= R15; i++, j++) {
iregs[i] = mkreg(format("%r%d", j), i, IREG);
reg_alias(iregs[i], Q, format("%r%d", j));
reg_alias(iregs[i], L, format("%r%dd", j));
reg_alias(iregs[i], W, format("%r%dw", j));
reg_alias(iregs[i], B, format("%r%db", j));
}
for (int i = XMM0; i <= XMM15; i++) {
const char *name = format("%xmm%d", i);
fregs[i] = mkreg(name, i, FREG);
reg_alias(fregs[i], Q, name);
reg_alias(fregs[i], L, name);
}
rsp = mksreg("%rsp");
rbp = mksreg("%rbp");
rip = mksreg("%rip");
// preserved
reg_preserved(rsp);
reg_preserved(rbp);
reg_preserved(iregs[RBX]);
reg_preserved(iregs[R12]);
reg_preserved(iregs[R13]);
reg_preserved(iregs[R14]);
reg_preserved(iregs[R15]);
// integer args
iarg_regs[0] = iregs[RDI];
iarg_regs[1] = iregs[RSI];
iarg_regs[2] = iregs[RDX];
iarg_regs[3] = iregs[RCX];
iarg_regs[4] = iregs[R8];
iarg_regs[5] = iregs[R9];
// float args
for (int i = XMM0, j = 0; i < XMM8; i++, j++)
farg_regs[j] = fregs[i];
// integer return
iret_regs[0] = iregs[RAX];
iret_regs[1] = iregs[RDX];
// float return
fret_regs[0] = fregs[XMM0];
fret_regs[1] = fregs[XMM1];
print("\t.file\t\"%s\"\n", basename(strdup(opts.ifile)));
}
static void finalize(void)
{
print("\t.ident\t\"9cc: %s-%s-%s\"\n", VERSION, IR->os, IR->arch);
}
static void defun(struct symbol *s)
{
if (s->sclass != STATIC)
print("\t.globl %s\n", s->x.name);
print("\t.type\t%s, @function\n", s->x.name);
print("%s:\n", s->x.name);
print("\tpushq %s\n", rbp->name);
print("\tmovq %s, %s\n", rsp->name, rbp->name);
gencode(s);
emitcode(s);
print("\tleave\n");
print("\tret\n");
print("\t.size\t%s, .-%s\n", s->x.name, s->x.name);
}
static void defsym(struct symbol *s)
{
static unsigned int strlabel, stclabel;
if (s->string) {
s->x.name = format("__string_literal.%u", strlabel++);
} else if (s->temporary) {
s->x.name = s->name;
} else if (s->scope >= LOCAL && s->sclass == STATIC) {
s->x.name = format("%s.%u", s->name, stclabel++);
} else if (s->scope == GLOBAL || s->sclass == EXTERN) {
if (opts.fleading_underscore)
s->x.name = format("_%s", s->name);
else
s->x.name = s->name;
} else {
s->x.name = s->name;
}
}
static void defvar(struct symbol *s)
{
struct type *ty = s->type;
int align = TYPE_ALIGN(ty);
size_t size = TYPE_SIZE(ty);
if (cseg == DATA) {
print("\t.align %d\n", align);
print("\t.type\t%s, @object\n", s->x.name);
print("\t.size\t%s, %lu\n", s->x.name, size);
print("%s:\n", s->x.name);
} else if (cseg == BSS) {
print("\t.comm %s,%lu,%d\n", s->x.name, size, align);
} else if (cseg == RODATA) {
print("%s:\n", s->x.name);
}
}
static void defconst(int suffix, int size, union value *v)
{
switch (suffix) {
case I: case U: case P:
if (size == Byte)
print("\t.byte %d\n", (int)v->i);
else if (size == Word)
print("\t.short %d\n", (int)v->i);
else if (size == Long)
print("\t.long %d\n", (int)v->i);
else if (size == Quad)
print("\t.quad %ld\n", v->i);
else
CC_UNAVAILABLE();
break;
case F:
if (size == Long) {
float f = v->d;
print("\t.long %u\n", *(uint32_t *)&f);
} else if (size == Quad) {
double d = v->d;
print("\t.quad %lu\n", *(uint64_t *)&d);
} else {
CC_UNAVAILABLE();
}
break;
default:
CC_UNAVAILABLE();
}
}
static void defaddress(const char *s, long offset)
{
if (offset == 0)
print("\t.quad %s\n", s);
else if (offset > 0)
print("\t.quad %s+%ld\n", s, offset);
else
print("\t.quad %s%ld\n", s, offset);
}
static void defstring(const char *s, long n)
{
size_t len;
if (n == 0)
return;
if (n < 0) {
print("\t.string \"%s\"\n", s);
return;
}
len = strlen(s);
if (len + 1 == n) {
print("\t.string \"%s\"\n", s);
} else if (len + 1 < n) {
print("\t.string \"%s\"\n", s);
IR->defzero(n - len - 1);
} else {
// truncate
print("\t.ascii \"");
for (size_t i = 0; i < n; i++)
print("%c", s[i]);
print("\"\n");
}
}
static void defzero(size_t size)
{
print("\t.zero %lu\n", size);
}
static void export(struct symbol *s)
{
print("\t.globl %s\n", s->x.name);
}
static void local(struct symbol *s)
{
print("\t.local %s\n", s->x.name);
}
static void segment(int seg)
{
if (cseg == seg)
return;
cseg = seg;
if (seg == TEXT)
print("\t.text\n");
else if (seg == DATA)
print("\t.data\n");
else if (seg == RODATA)
print("\t.section\t.rodata\n");
/* BSS: ignored */
}
struct interface *IR = &(struct interface) {
// os/arch
.os = "linux",
.arch = "x86_64",
// size align rank
.boolmetrics = {1, 1, 10},
.charmetrics = {1, 1, 20},
.shortmetrics = {2, 2, 30},
.wcharmetrics = {4, 4, 40},
.intmetrics = {4, 4, 40},
.longmetrics = {8, 8, 50},
.longlongmetrics = {8, 8, 60},
.floatmetrics = {4, 4, 70},
.doublemetrics = {8, 8, 80},
.longdoublemetrics = {8, 8, 90},
.ptrmetrics = {8, 8, 0},
.voidmetrics = {0, 1, 0},
// func
.init = init,
.finalize = finalize,
.defsym = defsym,
.defvar = defvar,
.defconst = defconst,
.defaddress = defaddress,
.defstring = defstring,
.defzero = defzero,
.export = export,
.local = local,
.defun = defun,
.segment = segment,
.gen = gen,
.emit = emit,
// xface
.x = {
.label = burg(label),
.rule = burg(rule),
.nt_kids = burg(nts_kids),
.rule_names = burg(rule_names),
.nt_names = burg(nt_names),
.templates = burg(rule_templates),
.nts = burg(nts),
.max_kids = burg(max_nts),
.nts_count = burg(max_nt),
},
};