Skip to content
← Back

src/ir/values/literal/bigint.ghul

1
namespace IR.Values.Literal is
2
use TypeTyped = Semantic.Types.Typed
3
use Semantic.Types.Type
4
5
// System.Numerics.BigInteger has no load instruction, so a `bigint`
6
// literal is built at run time from its value, the way a decimal
7
// literal is built from its decomposition. A magnitude that fits in
8
// a long goes through the `BigInteger(int64)` constructor; anything
9
// wider goes through `BigInteger.Parse` on the digits, which are
10
// written out in decimal whatever radix the source used.
11
class BIGINT: Value, TypeTyped is
12
// The magnitude in decimal digits. A sign comes from a wrapping
13
// unary `-`, so this is never negative.
14
value: string
15
type: Type
16
_as_long: long
17
_fits_long: bool
18
19
is_lightweight_pure: bool => true
20
21
init(value: string, type: Type, as_long: long, fits_long: bool) is
22
super.init()
23
24
self.value = value
25
self.type = type
26
_as_long = as_long
27
_fits_long = fits_long
28
si
29
30
gen(context: IR.CONTEXT) is
31
let body = context.current_srm_body_emitter!
32
33
if _fits_long then
34
body.ldc_i8(_as_long)
35
body.new_object(context.resolve_bigint_constructor(type))
36
else
37
body.ldstr(context.srm_assembly_emitter, value)
38
body.call(context.resolve_bigint_parse(type))
39
fi
40
si
41
42
to_string() -> string =>
43
"literal:[{type}]({value}n)"
44
si
45
si