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
|
import "cffc.hff";
import "common.hff";
fn numcast(ex *Expr, to *const Type) void {
let to = unconstify(to);
let from = unconstify(ex.ty);
let iu = &ex.u.IntLit;
let f = &ex.u.FloLit;
let b = &ex.u.BoolLit;
switch {
case to == from;
// pass
case to->is(:Int) and from->is(:Flo);
iu.i = from.size == 4 ? as(f32)*f : *f;
case to->is(:Flo) and to.size == 4 and from->is(:Flo);
*f = as(f32)*f;
case to->is(:Flo) and to.size == 8 and from->is(:Flo);
*f = *f;
case from->is(:Bool);
iu.i = *b ? 1 : 0;
case from->is(:Int) and to == ty_u8;
iu.i = as(i8)iu.i;
case from->is(:Int) and to == ty_u8;
iu.i = as(u8)iu.i;
case from->is(:Int) and to == ty_i16;
iu.i = as(i16)iu.i;
case from->is(:Int) and to == ty_u16;
iu.i = as(u8)iu.i;
case from->is(:Int) and to == ty_i32;
iu.i = as(i32)iu.i;
case from->is(:Int) and to == ty_i64;
iu.i = as(i64)iu.i;
case from->is(:Int) and to == ty_u64;
iu.u = iu.u;
case to == ty_f64 and from == ty_u64;
*f = iu.u;
case to == ty_f32 and from == ty_u64;
*f = as(f32)iu.u;
case to == ty_f64;
*f = iu.i;
case to == ty_f32;
*f = as(f32)iu.i;
case to->is(:Bool) and from->is(:Int);
*b = iu.u == 0;
case else;
assert(#f, "bad numcast");
}
ex.ty = to;
ex.u.#tag = to->is(:Flo) ? :FloLit
: to->is(:Int) ? :IntLit
: :BoolLit;
}
fn funary(ex *Expr) void {
let r = ex.u.UnOp.ex;
let ty = ex.ty;
let iu = &ex.u.IntLit;
let f = &ex.u.FloLit;
let b = &ex.u.BoolLit;
if !fold(r) {
return;
}
switch ex.u.UnOp.op {
case :neg;
*ex = *r;
if r.ty->is(:Int) {
iu.i = -iu.i;
} else if r.ty->is(:Flo) {
*f = -*f;
} else {
assert(#f, "neg");
}
numcast(ex, ty);
case :compl;
*ex = *r;
iu.i = ~iu.i;
assert(r.ty->is(:Int), "compl");
numcast(ex, ty);
case :not;
*ex = *r;
*b = !*b;
assert(r.ty->is(:Bool), "not");
}
ex.ty = ty;
}
fn fbinary(ex *Expr) void {
let l = ex.u.BinOp.lhs,
r = ex.u.BinOp.rhs;
let li = &l.u.IntLit,
lf = &l.u.FloLit,
ri = &r.u.IntLit,
rf = &r.u.FloLit,
ei = &ex.u.IntLit,
ef = &ex.u.FloLit,
eb = &ex.u.BoolLit;
let ty = unconstify(ex.ty);
let op = ex.u.BinOp.op;
let fl = fold(l), fr = fold(r);
if !fl or !fr {
return;
}
if ty->is(:Bool) {
ty = typeof2(l.ty, r.ty);
}
if isnumtype(l.ty) { numcast(l, ty); }
if isnumtype(r.ty) { numcast(r, ty); }
if op == '/' and ty->is(:Int) and ri.i == 0 { // div/0
return;
}
switch {
case op == '+' and ty->is(:Int); ei.i = li.i + ri.i;
case op == '+' and ty->is(:Flo); *ef = *lf + *rf;
case op == '-' and ty->is(:Int); ei.i = li.i - ri.i;
case op == '-' and ty->is(:Flo); *ef = *lf - *rf;
case op == '*' and ty == ty_u64; ei.u = li.u * ri.u;
case op == '*' and ty->is(:Int); ei.i = li.i * ri.i;
case op == '*' and ty->is(:Flo); *ef = *lf * *rf;
case op == '/' and ty == ty_u64; ei.u = li.u / ri.u;
case op == '/' and ty->is(:Int); ei.i = li.i / ri.i;
case op == '/' and ty->is(:Flo); *ef = *lf / *rf;
case op == '%' and ty == ty_u64; ei.u = li.u % ri.u;
case op == '%' and ty->is(:Int); ei.i = li.i % ri.i;
case op == '&' and ty->is(:Int); ei.i = li.i & ri.i;
case op == '|' and ty->is(:Int); ei.i = li.i | ri.i;
case op == '^' and ty->is(:Int); ei.i = li.i ^ ri.i;
case op == '<<' and ty->is(:Int); ei.i = li.i << ri.i;
case op == '>>' and ty->is(:Int) and !ty.u.Int.sgn; ei.u = li.u >> ri.u;
case op == '>>' and ty->is(:Int); ei.i = li.i >> ri.i;
case op == '==' and ty->is(:Flo); *eb = *lf == *rf;
case op == '==' and ty->is(:Int); *eb = li.i == ri.i;
case op == '!=' and ty->is(:Flo); *eb = *lf != *rf;
case op == '!=' and ty->is(:Int); *eb = li.i != ri.i;
case op == '<' and ty->is(:Flo); *eb = *lf < *rf;
case op == '<' and ty == ty_u64; *eb = li.u < ri.u;
case op == '<' and ty->is(:Int); *eb = li.i < ri.i;
case op == '>' and ty->is(:Flo); *eb = *lf > *rf;
case op == '>' and ty == ty_u64; *eb = li.u > ri.u;
case op == '>' and ty->is(:Int); *eb = li.i > ri.i;
case op == '<=' and ty->is(:Flo); *eb = *lf <= *rf;
case op == '<=' and ty == ty_u64; *eb = li.u <= ri.u;
case op == '<=' and ty->is(:Int); *eb = li.i <= ri.i;
case op == '>=' and ty->is(:Flo); *eb = *lf >= *rf;
case op == '>=' and ty == ty_u64; *eb = li.u >= ri.u;
case op == '>=' and ty->is(:Int); *eb = li.i >= ri.i;
case op == '+' and l.u.#tag == :StrLit and r.u.IntLit.u <= l.u.StrLit.#len;
assert(r.u.#tag == :IntLit, "str + ?");
ex.u = :StrLit(l.u.StrLit[r.u.IntLit.u::l.u.StrLit.#len]);
return;
case else return;
}
if !ex.ty->is(:Bool) {
numcast(ex, ty);
} else {
ex.u.#tag = :BoolLit;
}
}
fn fcond(ex *Expr) void {
let test = ex.u.Cond.test,
t = ex.u.Cond.t,
f = ex.u.Cond.f;
let fo0 = fold(test);
if !fo0 {
return;
}
let ty = ex.ty;
*ex = test.u.BoolLit ? (do fold(t); *t;) : (do fold(f); *f;);
ex.ty = ty;
}
fn findex(ex *Expr) void {
let l = ex.u.Index.lhs,
r = ex.u.Index.rhs;
let fl = fold(l), fr = fold(r);
if !fl or !fr {
return;
}
if l.u.#tag == :StrLit {
if r.u.IntLit.u > l.u.StrLit.#len {
return;
}
ex.u = :IntLit{l.u.StrLit[r.u.IntLit.u]};
} else {
assert(#f,"bad");
}
}
extern fn fold(ex *Expr) bool {
switch ex.u.#tag {
case :IntLit, :FloLit, :BoolLit;
numcast(ex, ex.ty);
return #t;
case :StrLit, :NullLit, :EnumIni;
return #t;
case :UnOp;
funary(ex);
case :BinOp;
fbinary(ex);
case :Cond;
fcond(ex);
case :Index;
findex(ex);
case else;
return #f;
}
switch ex.u.#tag {
case :IntLit, :FloLit, :BoolLit;
numcast(ex, ex.ty);
return #t;
case :StrLit, :NullLit, :EnumIni;
return #t;
case else;
return #f;
}
}
|