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
|
#include "lisc.h"
static char *rtoa[] = {
[RXX] = "OH GOD!",
[RAX] = "rax",
[RCX] = "rcx",
[RDX] = "rdx",
[RSI] = "rsi",
[RDI] = "rdi",
[R8] = "r8",
[R9] = "r9",
[R10] = "r10",
[R11] = "r11",
[RBX] = "rbx",
[R12] = "r12",
[R13] = "r13",
[R14] = "r14",
[R15] = "r15",
[RBP] = "rbp",
[RSP] = "rsp",
[EAX] = "eax",
[ECX] = "ecx",
[EDX] = "edx",
[ESI] = "esi",
[EDI] = "edi",
[R8D] = "r8d",
[R9D] = "r9d",
[R10D] = "r10d",
[R11D] = "r11d",
[EBX] = "ebx",
[R12D] = "r12d",
[R13D] = "r13d",
[R14D] = "r14d",
[R15D] = "r15d",
};
static void
eref(Ref r, Fn *fn, FILE *f)
{
Con *c;
switch (rtype(r)) {
case RReg:
fprintf(f, "%%%s", rtoa[r.val]);
break;
case RSlot:
fprintf(f, "-%d(%%rbp)", 8 * r.val);
break;
case RCon:
c = &fn->con[r.val];
switch (c->type) {
case CAddr:
fprintf(f, "$%s", c->label);
if (c->val)
fprintf(f, "%+"PRIi64, c->val);
break;
case CNum:
fprintf(f, "$%"PRId64, c->val);
break;
default:
diag("emitref: invalid constant");
}
break;
default:
diag("emitref: invalid reference");
}
}
static void
eop(char *op, Ref a, Ref b, Fn *fn, FILE *f)
{
fprintf(f, "\t%s ", op);
eref(a, fn, f);
if (!req(b, R)) {
fprintf(f, ", ");
eref(b, fn, f);
}
fprintf(f, "\n");
}
static void
eins(Ins i, Fn *fn, FILE *f)
{
static char *opi[] = {
[OAdd] = "add",
[OSub] = "sub",
};
switch (i.op) {
case OAdd:
case OSub:
if (req(i.to, i.arg[1])) {
if (!opdesc[i.op].comm)
diag("emit: unhandled instruction (1)");
i.arg[1] = i.arg[0];
i.arg[0] = i.to;
}
if (!req(i.to, i.arg[0]))
eop("mov", i.arg[0], i.to, fn, f);
eop(opi[i.op], i.arg[1], i.to, fn, f);
break;
case OStore:
i.to = i.arg[1];
/* fall through */
case OCopy:
case OLoad:
if (!req(i.arg[0], i.to))
eop("mov", i.arg[0], i.to, fn, f);
break;
case OSwap:
eop("xchg", i.arg[0], i.arg[1], fn, f);
break;
case OSign:
if (req(i.to, REG(RDX)) && req(i.arg[0], REG(RAX)))
fprintf(f, "\tcqto\n");
else if (req(i.to, REG(EDX)) && req(i.arg[0], REG(EAX)))
fprintf(f, "\tcltq\n");
else
diag("emit: unhandled instruction (2)");
break;
case OXDiv:
eop("idiv", i.arg[0], R, fn, f);
break;
case ONop:
break;
default:
diag("emit: unhandled instruction (3)");
}
}
void
emitfn(Fn *fn, FILE *f)
{
char *js;
Blk *b, *s;
Ins *i;
fprintf(f,
".text\n"
".globl liscf\n"
".type liscf, @function\n"
"liscf:\n"
"\tpush %%rbp\n"
"\tmov %%rsp, %%rbp\n"
"\tsub $%u, %%rsp\n",
fn->nspill * 8
);
for (b=fn->start; b; b=b->link) {
fprintf(f, ".L%s:\n", b->name);
for (i=b->ins; i-b->ins < b->nins; i++)
eins(*i, fn, f);
switch (b->jmp.type) {
case JRet:
fprintf(f,
"\tleave\n"
"\tret\n"
);
break;
case JJmp:
if (b->s1 != b->link)
fprintf(f, "\tjmp .L%s\n", b->s1->name);
break;
case JJez:
if (b->s1 == b->link) {
js = "jne";
s = b->s2;
} else if (b->s2 == b->link) {
js = "je";
s = b->s1;
} else
diag("emit: unhandled jump (1)");
eop("cmp $0,", b->jmp.arg, R, fn, f);
fprintf(f, "\t%s .L%s\n", js, s->name);
break;
default:
diag("emit: unhandled jump (2)");
}
}
}
|