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
|
import "ir.hff";
import "common.hff";
import "map.hff";
extern fn ir_genstatic(IR *IRCtx, decl *Decl) void {
}
struct IntTraits {
fn hash(a int) u32 { return (a * 7128347) + 972183; }
fn eq(a int, b int) bool { return a == b; }
}
struct InstStream {
IR *IRCtx,
head *IRInst,
tail *IRInst,
loopbrks Map<int, *IRInst, IntTraits>,
loopconts Map<int, *IRInst, IntTraits>,
fn push(S *InstStream, inst *IRInst) void {
if S.tail {
S.tail.next = inst;
} else {
S.head = inst;
}
S.tail = inst;
}
fn mkinst(S *InstStream, inst0 IRInst, narg uint) *IRInst {
let inst *IRInst = S.IR.alloc->alloc(sizeof IRInst + (narg * sizeof IRArg), alignof IRInst);
*inst = inst0;
return inst;
}
fn mkinst1(S *InstStream, inst0 IRInst, a IRArg) *IRInst {
let inst = S->mkinst(inst0, 1);
inst.args[0] = a;
return inst;
}
fn mkinst2(S *InstStream, inst0 IRInst, a IRArg, b IRArg) *IRInst {
let inst = S->mkinst(inst0, 2);
inst.args[0] = a;
inst.args[1] = b;
return inst;
}
fn mkinst3(S *InstStream, inst0 IRInst, a IRArg, b IRArg, c IRArg) *IRInst {
let inst = S->mkinst(inst0, 3);
inst.args[0] = a;
inst.args[1] = b;
inst.args[2] = c;
return inst;
}
fn pushnop(S *InstStream) *IRInst {
S->push(S->mkinst({:nop}, 0));
return S.tail;
}
}
fn genblock(S *InstStream, block [#]Stmt) void;
fn genexpr(S *InstStream, ex *Expr) IRValue {
static tmpid int = 0;
// fold(ex);
defmacro genunop(u,t) [ {
let child = genexpr(S, (u).ex);
let res = IRValue{ex.ty, .u: :Tmp(tmpid++)};
let inst = S->mkinst2({t}, :Val(res), :Val(child));
S->push(inst);
return res;
} ]
defmacro genbinop(b,t) [ {
let lhs = genexpr(S, (b).lhs);
let rhs = genexpr(S, (b).rhs);
let res = IRValue{ex.ty, .u: :Tmp(tmpid++)};
let inst = S->mkinst3({t}, :Val(res), :Val(lhs), :Val(rhs));
S->push(inst);
return res;
} ]
switch ex.u {
case IntLit i; return {ex.ty, :IImm(i.i)};
case FloLit f; return {ex.ty, :FImm(f)};
case StrLit s; return {ex.ty, :SImm(s)};
case BoolLit b; return {ex.ty, :BImm(b)};
case NullLit; return {ex.ty, :Null};
case UnOp u;
switch u.op {
case :neg; genunop(u, ex.ty->is(:Int) ? :neg : :fneg);
case :not; genunop(u, :not);
case :compl; genunop(u, :compl);
case :deref; genunop(u, :load);
case :preinc, :predec;
let one = u.op == :preinc ? 1 : -1;
switch u.ex.u {
case Symbol decl;
let ex = genexpr(S, u.ex);
let inced = IRValue{ex.ty, .u: :Tmp(tmpid++)};
let rhs = S->mkinst3({:add}, :Val(inced),
:Val(ex), :Val{ty_int, :IImm(one)});
S->push(rhs);
let inst = S->mkinst2({:setvar}, :Val(ex), :Val(inced));
S->push(inst);
let ex = genexpr(S, u.ex);
if decl.u.#tag == :Let {
return {ex.ty, :Local(decl)};
} else {
return {ex.ty, :Global(decl)};
}
}
}
case BinOp b;
switch b.op {
case '+'; genbinop(b, !ex.ty->is(:Flo) ? :add : :fadd);
case '-'; genbinop(b, !ex.ty->is(:Flo) ? :sub : :fsub);
case '*'; genbinop(b, !ex.ty->is(:Flo) ? :mul : :fmul);
case '/'; genbinop(b, !ex.ty->is(:Flo) ? :div : :fdiv);
case '%'; genbinop(b, :mod);
case '&'; genbinop(b, :band);
case '|'; genbinop(b, :bor);
case '^'; genbinop(b, :xor);
case '<<'; genbinop(b, :lsl);
case '>>'; genbinop(b, ex.ty.u.Int.sgn ? :asr : :lsr);
case '=='; genbinop(b, :eq);
case '!='; genbinop(b, :neq);
case '<'; genbinop(b, :lt);
case '<='; genbinop(b, :lteq);
case '>';
let lhs = genexpr(S, (b).lhs);
let rhs = genexpr(S, (b).rhs);
let res = IRValue{ex.ty, .u: :Tmp(tmpid++)};
let inst = S->mkinst3({:lt}, :Val(res), :Val(rhs), :Val(lhs));
S->push(inst);
return res;
case '>=';
let lhs = genexpr(S, (b).lhs);
let rhs = genexpr(S, (b).rhs);
let res = IRValue{ex.ty, .u: :Tmp(tmpid++)};
let inst = S->mkinst3({:lteq}, :Val(res), :Val(rhs), :Val(lhs));
S->push(inst);
return res;
case '=';
switch b.lhs.u {
case Symbol decl;
let inst = S->mkinst2({:setvar}, :Val(genexpr(S, b.lhs)), :Val(genexpr(S, b.rhs)));
S->push(inst);
if decl.u.#tag == :Let {
return {ex.ty, :Local(decl)};
} else {
return {ex.ty, :Global(decl)};
}
case UnOp u;
assert(u.op == :deref, "deref?? unop=");
let child = genexpr(S, u.ex);
let inst = S->mkinst2({:store}, :Val(child), :Val(genexpr(S, b.rhs)));
S->push(inst);
let it = genexpr(S, b.lhs);
return it;
case else
assert(#f, "nyi =");
}
}
case Cond c;
let test = genexpr(S, c.test);
let beqz = S->mkinst1({:beqz}, :Val(test));
S->push(beqz);
let t = genexpr(S, c.t);
let res = IRValue{ex.ty, :Tmp(tmpid++)};
let inst = S->mkinst2({:copy}, :Val(res), :Val(t));
S->push(inst);
let b = S->mkinst({:b}, 0);
S->push(b);
beqz.branch = S->pushnop();
let f = genexpr(S, c.f);
let inst = S->mkinst2({:copy}, :Val(res), :Val(f));
S->push(inst);
b.branch = S->pushnop();
return res;
case Call c;
let n = c.args.#len;
let inst = S->mkinst({:call, .call_nargs: n}, n + 2);
inst.args[1] = :Fn(c.lhs.u.Symbol);
if c.lhs.u.#tag == :Symbol {
for let i = 0; i < n; ++i {
inst.args[i + 2] = :Val(genexpr(S, &c.args[i]));
}
S->push(inst);
} else {
}
let res = IRValue{ex.ty, :Tmp(tmpid++)};
inst.args[0] = :Val(res);
return res;
case Symbol decl;
if decl.u.#tag == :Let {
return {ex.ty, :Local(decl)};
} else {
return {ex.ty, :Global(decl)};
}
}
assert(#f, "NYI ex");
}
fn genstmt(S *InstStream, stmt *Stmt) void {
switch stmt.u {
case Block block;
genblock(S, block);
case Expr *ex;
genexpr(S, ex);
case Decl decl;
if decl.u.#tag == :Let and !decl.u.Let.ini->empty() {
let Let = &decl.u.Let;
let inst = S->mkinst2({:setvar}, :Val{Let.ty, :Local(decl)},
:Val(genexpr(S, &Let.ini.Some)));
S->push(inst);
}
case If If;
let t = genexpr(S, &If.test);
let beqz = S->mkinst1({:beqz}, :Val(t));
S->push(beqz);
genblock(S, If.t);
if If.f.#ptr {
let b = S->mkinst({:b}, 0);
S->push(b);
beqz.branch = S->pushnop();
genblock(S, If.f);
b.branch = S->pushnop();
} else {
beqz.branch = S->pushnop();
}
case While loop;
let begin = S->pushnop();
let t = genexpr(S, &loop.test);
let beqz = S->mkinst1({:beqz}, :Val(t));
let b = S->mkinst({:b}, 0);
let end = S->mkinst({:nop}, 0);
S->push(beqz);
S.loopconts->put(loop.id, begin);
S.loopbrks->put(loop.id, end);
genblock(S, loop.body);
S->push(b);
b.branch = begin;
S->push(end);
beqz.branch = end;
case For loop;
genblock(S, loop.ini);
let begin = S->pushnop();
let t = genexpr(S, &loop.test);
let beqz = S->mkinst1({:beqz}, :Val(t));
let b = S->mkinst({:b}, 0);
let next = S->mkinst({:nop}, 0);
let end = S->mkinst({:nop}, 0);
S->push(beqz);
S.loopconts->put(loop.id, next);
S.loopbrks->put(loop.id, end);
genblock(S, loop.body);
S->push(next);
if !loop.next->empty() {
genexpr(S, &loop.next.Some);
}
S->push(b);
b.branch = begin;
S->push(end);
beqz.branch = end;
case Break id;
let jumpto = *S.loopbrks->get(id);
S->push(S->mkinst({:b, .branch: jumpto},0));
case Continue id;
let jumpto = *S.loopconts->get(id);
S->push(S->mkinst({:b, .branch: jumpto},0));
case Return retex;
if retex->empty() {
S->push(S->mkinst({:ret0}, 0));
} else {
let inst = S->mkinst1({:ret}, :Val(genexpr(S, &retex.Some)));
S->push(inst);
}
case else
assert(#f, "NYI st");
}
}
fn genblock(S *InstStream, block [#]Stmt) void {
foreach_ptr(st, _, block) {
genstmt(S, st);
}
}
extern fn ir_genfn(IR *IRCtx, f *Fn) void {
let stream InstStream = {.IR: IR};
defer stream.loopbrks->clear();
defer stream.loopconts->clear();
genblock(&stream, f.body.Some);
stream->push(stream->mkinst({:ret0}, 0));
efmt("----------------------\n");
efmt("function %s.%d:\n", container_of(f, Decl, u.Fn).name, f.id);
irdump(stream.head);
}
|