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
|
#include "lisc.h"
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,
};
Typ typ[NTyp];
Ins insb[NIns], *curi;
void
diag(char *s)
{
fputs(s, stderr);
fputc('\n', stderr);
abort();
}
void *
alloc(size_t n)
{
void *p;
if (n == 0)
return 0;
p = calloc(1, n);
if (!p)
abort();
return p;
}
Blk *
balloc()
{
static Blk z;
Blk *b;
b = alloc(sizeof *b);
*b = z;
return b;
}
void
emit(int op, int w, Ref to, Ref arg0, Ref arg1)
{
if (curi == insb)
diag("emit: too many instructions");
*--curi = (Ins){op, w, to, {arg0, arg1}};
}
void
emiti(Ins i)
{
emit(i.op, i.wide, i.to, i.arg[0], i.arg[1]);
}
int
bcnt(Bits *b)
{
int z, i, j;
ulong tmp;
i = 0;
for (z=0; z<BITS; z++) {
tmp = b->t[z];
for (j=0; j<64; j++) {
i += 1 & tmp;
tmp >>= 1;
}
}
return i;
}
void
idup(Ins **pd, Ins *s, ulong n)
{
free(*pd);
*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 *
valloc(ulong len, size_t esz)
{
ulong cap;
Vec *v;
v = alloc(len * esz + sizeof(Vec));
v->mag = VMag;
for (cap=VMin; cap<len; cap*=2)
;
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 = valloc(len, v->esz);
memcpy(v1, v+1, v->cap * v->esz);
free(v);
*(Vec **)vp = v1;
}
Ref
newtmp(char *prfx, Fn *fn)
{
static int n;
int t;
t = fn->ntmp++;
vgrow(&fn->tmp, fn->ntmp);
sprintf(fn->tmp[t].name, "%s%d", prfx, ++n);
fn->tmp[t].spill = -1;
return TMP(t);
}
Ref
getcon(int64_t val, Fn *fn)
{
int c;
for (c=0; c<fn->ncon; c++)
if (fn->con[c].type == CNum && fn->con[c].val == val)
return CON(c);
fn->ncon++;
vgrow(&fn->con, fn->ncon);
fn->con[c] = (Con){.type = CNum, .val = val};
return CON(c);
}
|