blob: 7b393e59df0242b8ef8048327b57e63462ebe1b0 (
plain) (
blame)
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
|
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;
}
extern fn fold(ex *Expr) bool {
switch ex.u.#tag {
case :IntLit, :FloLit, :BoolLit;
numcast(ex, ex.ty);
return #t;
case :StrLit, :NullLit;
return #t;
case else;
return #f;
}
}
|