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
|
#include "all.h"
#include <stdarg.h>
typedef struct Bitset Bitset;
typedef struct Vec Vec;
struct Vec {
ulong mag;
size_t esz;
ulong cap;
union {
long long ll;
long double ld;
void *ptr;
} align[];
};
enum {
VMin = 2,
VMag = 0xcabba9e,
NPtr = 256,
};
Typ typ[NTyp];
Ins insb[NIns], *curi;
static void *ptr[NPtr];
static void **pool = ptr;
static int nptr = 1;
void
die_(char *file, char *s, ...)
{
va_list ap;
fprintf(stderr, "%s: dying: ", file);
va_start(ap, s);
vfprintf(stderr, s, ap);
va_end(ap);
fputc('\n', stderr);
abort();
}
void *
emalloc(size_t n)
{
void *p;
p = calloc(1, n);
if (!p)
die("emalloc, out of memory");
return p;
}
void *
alloc(size_t n)
{
void **pp;
if (n == 0)
return 0;
if (nptr >= NPtr) {
pp = emalloc(NPtr * sizeof(void *));
pp[0] = pool;
pool = pp;
nptr = 1;
}
return pool[nptr++] = emalloc(n);
}
void
freeall()
{
void **pp;
for (;;) {
for (pp = &pool[1]; pp < &pool[nptr]; pp++)
free(*pp);
pp = pool[0];
if (!pp)
break;
free(pool);
pool = pp;
nptr = NPtr;
}
nptr = 1;
}
Blk *
blknew()
{
static Blk z;
Blk *b;
b = alloc(sizeof *b);
*b = z;
return b;
}
void
emit(int op, int k, Ref to, Ref arg0, Ref arg1)
{
if (curi == insb)
die("emit, too many instructions");
*--curi = (Ins){
.op = op, .cls = k,
.to = to, .arg = {arg0, arg1}
};
}
void
emiti(Ins i)
{
emit(i.op, i.cls, i.to, i.arg[0], i.arg[1]);
}
void
idup(Ins **pd, Ins *s, ulong n)
{
*pd = alloc(n * sizeof(Ins));
memcpy(*pd, s, n * sizeof(Ins));
}
Ins *
icpy(Ins *d, Ins *s, ulong n)
{
memcpy(d, s, n * sizeof(Ins));
return d + n;
}
void *
vnew(ulong len, size_t esz)
{
ulong cap;
Vec *v;
for (cap=VMin; cap<len; cap*=2)
;
v = alloc(cap * esz + sizeof(Vec));
v->mag = VMag;
v->cap = cap;
v->esz = esz;
return v + 1;
}
void
vgrow(void *vp, ulong len)
{
Vec *v;
void *v1;
v = *(Vec **)vp - 1;
assert(v+1 && v->mag == VMag);
if (v->cap >= len)
return;
v1 = vnew(len, v->esz);
memcpy(v1, v+1, v->cap * v->esz);
*(Vec **)vp = v1;
}
int
phicls(int t, Tmp *tmp /*, int c*/)
{
if (tmp[t].phi)
return tmp[t].phi;
return t;
#if 0
int t1;
t1 = tmp[t].phi;
if (!t1)
t1 = t;
if (t != t1) {
t1 = phitmp(t1, tmp, c);
if (c)
tmp[t].phi = t1;
}
return t1;
#endif
}
Ref
newtmp(char *prfx, int k, Fn *fn)
{
static int n;
int t;
t = fn->ntmp++;
vgrow(&fn->tmp, fn->ntmp);
if (prfx)
sprintf(fn->tmp[t].name, "%s%d", prfx, ++n);
fn->tmp[t].cls = k;
fn->tmp[t].slot = -1;
fn->tmp[t].nuse = +1;
fn->tmp[t].ndef = +1;
return TMP(t);
}
void
chuse(Ref r, int du, Fn *fn)
{
if (rtype(r) == RTmp)
fn->tmp[r.val].nuse += du;
}
Ref
getcon(int64_t val, Fn *fn)
{
int c;
for (c=0; c<fn->ncon; c++)
if (fn->con[c].type == CBits && fn->con[c].bits.i == val)
return CON(c);
vgrow(&fn->con, ++fn->ncon);
fn->con[c] = (Con){.type = CBits, .bits.i = val};
return CON(c);
}
void
addcon(Con *c0, Con *c1)
{
if (c0->type == CUndef)
*c0 = *c1;
else {
if (c1->type == CAddr) {
assert(c0->type != CAddr && "adding two addresses");
c0->type = CAddr;
strcpy(c0->label, c1->label);
}
c0->bits.i += c1->bits.i;
}
}
void
bsinit(BSet *bs, uint n)
{
n = (n + NBit-1) / NBit;
bs->nt = n;
bs->t = alloc(n * sizeof bs->t[0]);
}
uint
bscount(BSet *bs)
{
uint i, j, n;
n = 0;
for (i=0; i<bs->nt; i++)
for (j=0; j<NBit; j++)
if (bs->t[i] & BIT(j))
n++;
return n;
}
static inline uint
bsmax(BSet *bs)
{
return bs->nt * NBit;
}
void
bsset(BSet *bs, uint elt)
{
assert(elt < bsmax(bs));
bs->t[elt/NBit] |= BIT(elt%NBit);
}
void
bsclr(BSet *bs, uint elt)
{
assert(elt < bsmax(bs));
bs->t[elt/NBit] &= ~BIT(elt%NBit);
}
#define BSOP(f, op) \
void \
f(BSet *a, BSet *b) \
{ \
uint i; \
\
assert(a->nt == b->nt); \
for (i=0; i<a->nt; i++) \
a->t[i] op b->t[i]; \
}
BSOP(bscopy, =)
BSOP(bsunion, |=)
BSOP(bsinter, &=)
BSOP(bsdiff, &= ~)
int
bsequal(BSet *a, BSet *b)
{
uint i;
assert(a->nt == b->nt);
for (i=0; i<a->nt; i++)
if (a->t[i] != b->t[i])
return 0;
return 1;
}
void
bszero(BSet *bs)
{
memset(bs->t, 0, bs->nt * sizeof bs->t[0]);
}
/* iterates on a bitset, use as follows
*
* for (i=0; bsiter(set, &i); i++)
* use(i);
*
*/
int
bsiter(BSet *bs, uint *elt)
{
uint i;
for (i=*elt;; i++) {
while (i < bsmax(bs) && !bs->t[i/NBit])
i = (i + NBit) & -NBit;
if (i >= bsmax(bs))
return 0;
if (bshas(bs, i)) {
*elt = i;
return 1;
}
}
}
void
dumpts(BSet *bs, Tmp *tmp, FILE *f)
{
uint t;
fprintf(f, "[");
for (t=Tmp0; bsiter(bs, &t); t++)
fprintf(f, " %s", tmp[t].name);
fprintf(f, " ]\n");
}
|