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
|
import "ir.hff";
import "common.hff";
import "map.hff";
fn dump(fmt *const u8, ...) void {
let ap va_list #?;
ap->start(fmt);
for let c = *fmt; c != 0; c = *++fmt {
if c != '%' {
fputc(c, stderr);
continue;
}
switch (c = *++fmt) {
case '%';
fputc('%', stderr);
case 'd';
efmt("%d", ap->arg(int));
case 'a';
let arg = ap->arg(IRArg);
switch arg {
case Val val;
switch val.u {
case IImm i;
efmt("%I", i);
case FImm f;
efmt("%f", f);
case SImm s;
efmt("%S", s);
case BImm b;
efmt("%s", b ? "#t" : "#f");
case Null;
efmt("#null");
case Tmp t;
efmt("%%%d", t);
case Local l;
efmt("$%s.%d", l.name, l.u.Let.id);
case Global g;
efmt("@%s.%d", g.name, g.u.Static.id);
}
case Ty ty; efmt("%t", ty);
case Fn decl; efmt("@%s", decl.name);
case else assert(#f, "arg? %d", arg.#tag);
}
case 't';
efmt("%t", ap->arg(*Type));
case else
assert(#f, "bad fmt %c", c);
}
}
ap->end();
}
extern fn irdump(inst *IRInst) void {
let labels Map<*IRInst, int, struct {
fn hash(a *IRInst) u32 { return as(iptrint)a; }
fn eq(a *IRInst, b *IRInst) bool { return a == b; }
}> = {};
defer labels->clear();
let labid = 0;
for let a = inst; a; a = a.next {
if a.branch {
let slot = labels->get_slot(a.branch);
if *slot == 0 {
*slot = ++labid;
}
}
}
#'dump for ; inst; inst = inst.next {
let label = labels->get(inst);
if label {
dump(".L%d:\t", *label);
} else {
dump("\t");
}
defmacro exprinst(tag, nam, n) [
if inst.t == (tag) {
dump("%%%d(%t) = " ## nam ## " ", inst.args[0].Val.u.Tmp, inst.args[0].Val.ty);
for let $i = 1; $i < (n); ++$i {
dump("%a", inst.args[$i]);
if $i < (n) - 1 {
dump(", ");
}
}
dump("\n");
continue #'dump;
}
]
defmacro stmtinst(tag, nam, n) [
if inst.t == (tag) {
dump(nam##" ");
for let $i = 0; $i < (n); ++$i {
dump("%a", inst.args[$i]);
if $i < (n) - 1 {
dump(", ");
}
}
dump("\n");
continue #'dump;
}
]
exprinst(:neg, "neg", 2)
exprinst(:not, "not", 2)
exprinst(:compl, "compl", 2)
exprinst(:fneg, "fneg", 2)
exprinst(:add, "add", 3);
exprinst(:sub, "sub", 3);
exprinst(:mul, "mul", 3);
exprinst(:div, "div", 3);
exprinst(:mod, "mod", 3);
exprinst(:band, "band", 3);
exprinst(:bor, "bor", 3);
exprinst(:xor, "xor", 3);
exprinst(:lsl, "lsl", 3);
exprinst(:lsr, "lsr", 3);
exprinst(:asr, "asr", 3);
exprinst(:eq, "eq", 3);
exprinst(:neq, "neq", 3);
exprinst(:lt, "lt", 3);
exprinst(:lteq, "lteq", 3);
exprinst(:fadd, "fadd", 3);
exprinst(:fsub, "fsub", 3);
exprinst(:fmul, "fmul", 3);
exprinst(:fdiv, "fdiv", 3);
if inst.t == :call {
let fnty = inst.args[1].Fn.u.Fn.ty.u.Fn;
dump("%%%d(%t) = call %a, ", inst.args[0].Val.u.Tmp, inst.args[0].Val.ty, inst.args[1]);
let n = inst.call_nargs;
for let i = 0; i < n; ++i {
dump("%a(%t)", inst.args[i + 2],
i < fnty.params.#len ? fnty.params[i] : inst.args[i + 2].Val.ty);
if i < n - 1 {
dump(", ");
}
}
dump("\n");
continue #'dump;
}
exprinst(:copy, "copy", 2)
stmtinst(:nop, "nop", 0);
stmtinst(:ret0, "ret", 0);
stmtinst(:ret, "ret", 1);
if inst.t == :beqz {
dump("beqz %a, .L%d\n", inst.args[0], *labels->get(inst.branch));
continue #'dump;
}
if inst.t == :b {
dump("b .L%d\n", *labels->get(inst.branch));
continue #'dump;
}
assert(#f, "inst? %d\n", inst.t);
};
}
|