Skip to content
← Back

src/ir/values/literal/decimal.ghul

1
namespace IR.Values.Literal is
2
use TypeTyped = Semantic.Types.Typed
3
use Semantic.Types.Type
4
5
// System.Decimal has no `ldc.dec` opcode. The literal text is
6
// decomposed at compile time into the 96-bit mantissa + scale
7
// + sign form and re-emitted as a call to the five-arg
8
// Decimal::.ctor(int32 lo, int32 mid, int32 hi, bool isNegative,
9
// uint8 scale). Sign comes from a wrapping unary `-`, so the
10
// literal itself is always non-negative.
11
//
12
// Decomposition (`decompose`) lives separately so the caller —
13
// compile_literals — can surface a clean compile error when the
14
// literal overflows 96 bits or its scale exceeds Decimal's 0-28
15
// range, instead of silently truncating or deferring to a
16
// runtime ArgumentOutOfRangeException.
17
class DECIMAL: Value, TypeTyped is
18
value: string
19
type: Type
20
lo: int
21
mid: int
22
hi: int
23
scale: int
24
is_lightweight_pure: bool => true
25
26
init(value: string, type: Type, lo: int, mid: int, hi: int, scale: int) is
27
super.init()
28
29
self.value = value
30
self.type = type
31
self.lo = lo
32
self.mid = mid
33
self.hi = hi
34
self.scale = scale
35
si
36
37
accept(visitor: Visitor) is
38
visitor.visit(self)
39
si
40
41
// Parse `value` (already stripped of its `m`/`M` suffix and
42
// any unary sign) into the four-field form expected by
43
// Decimal's full-resolution constructor. The `error` slot
44
// is non-null when the literal exceeds Decimal's precision
45
// envelope:
46
// - scale (digits after the dot) must be 0-28
47
// - mantissa must fit in 96 bits
48
// Both failures are diagnoseable at parse time; the loop
49
// tracks the high-word carry-out so the caller can reject
50
// before any IL is emitted.
51
decompose(value: string) -> (lo: int, mid: int, hi: int, scale: int, error: string?) static is
52
let dot_index = value.index_of('.')
53
let scale_count: int mut = 0
54
55
let digits_buffer = System.Text.StringBuilder()
56
57
if dot_index < 0 then
58
digits_buffer.append(value)
59
else
60
digits_buffer.append(value.substring(0, dot_index))
61
digits_buffer.append(value.substring(dot_index + 1))
62
scale_count = value.length - dot_index - 1
63
fi
64
65
if scale_count > 28 then
66
return (0, 0, 0, 0, "decimal literal has more than 28 fractional digits")
67
fi
68
69
let digits = digits_buffer.to_string()
70
71
let lo: long mut = 0l
72
let mid: long mut = 0l
73
let hi: long mut = 0l
74
let overflow: bool mut = false
75
76
for i in 0..digits.length do
77
let c = digits[i]
78
79
if c >= '0' /\ c <= '9' then
80
let d = cast long(c - '0')
81
82
let prod_lo = lo * 10l + d
83
lo = prod_lo & 0xFFFFFFFFl
84
let carry_lo = prod_lo >> 32
85
86
let prod_mid = mid * 10l + carry_lo
87
mid = prod_mid & 0xFFFFFFFFl
88
let carry_mid = prod_mid >> 32
89
90
let prod_hi = hi * 10l + carry_mid
91
if prod_hi > 0xFFFFFFFFl then
92
overflow = true
93
fi
94
hi = prod_hi & 0xFFFFFFFFl
95
fi
96
od
97
98
if overflow then
99
return (0, 0, 0, 0, "decimal literal magnitude exceeds maximum")
100
fi
101
102
return (cast int(lo), cast int(mid), cast int(hi), scale_count, null)
103
si
104
105
to_string() -> string =>
106
"literal:[{type}]({value}m)"
107
si
108
si