Skip to content
← Back

src/ir/wasmgc/wasmgc_types.ghul

1
namespace IR.WasmGC is
2
use Wasm.VALUE_TYPE
3
use Wasm.NumericType
4
5
use Semantic.Types.Type
6
7
/// How the built-in scalar kinds are held on the wasm target: every
8
/// integer up to 32 bits in an i32, every wider one in an i64, and the
9
/// two floating kinds in their own types.
10
class WASMGC_TYPES is
11
/// The wasm number type a value of the scalar kind is held in.
12
numeric(scalar: Scalar) -> NumericType static =>
13
case scalar
14
when Scalar.INT64, Scalar.UINT64, Scalar.INT_NATIVE, Scalar.UINT_NATIVE then NumericType.I64
15
when Scalar.FLOAT32 then NumericType.F32
16
when Scalar.FLOAT64 then NumericType.F64
17
else NumericType.I32
18
esac
19
20
/// The wasm value type a value of the scalar kind is held in.
21
value_type(scalar: Scalar) -> VALUE_TYPE static =>
22
VALUE_TYPE.of_numeric(numeric(scalar))
23
24
/// The scalar kind a value of `type` is, or null where the type
25
/// is not one of the built-in scalars. An enum is held as the `int`
26
/// underneath it.
27
scalar(type: Type?) -> Scalar? static =>
28
if type? /\ isa Semantic.Symbols.ENUM_STRUCT(type.symbol) /\ !type.is_optional then
29
Scalar.INT32
30
else
31
IoC.CONTAINER.instance.value_converter.scalar(type)
32
fi
33
34
/// The numeric type a value of `type` is held as: a scalar's own,
35
/// and an enum's int; null for anything else.
36
numeric_of(type: Type?) -> NumericType? static is
37
if let kind = scalar(type) then
38
return numeric(kind)
39
elif is_enum(type) then
40
return NumericType.I32
41
fi
42
43
return null
44
si
45
46
/// Whether `type` is an enum, which is held as its underlying int.
47
is_enum(type: Type?) -> bool static =>
48
type?.symbol?.symbol_kind =~ Semantic.Symbols.SymbolKind.ENUM
49
50
/// Whether `type` is `string`.
51
is_string(type: Type?) -> bool static =>
52
type? /\ type.symbol == IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type().symbol
53
54
/// Whether `type` is `bigint`, which is held as a box around the
55
/// host's arbitrary-precision integer.
56
is_bigint(type: Type?) -> bool static =>
57
type? /\ !type.is_optional /\ type.symbol == IoC.CONTAINER.instance.innate_symbol_lookup.get_bigint_type().symbol
58
59
/// Whether a value of `type` is nothing at all: absent, or void.
60
is_void(type: Type?) -> bool static =>
61
!type? \/ type.is_void
62
63
/// Whether the kind is held in fewer bits than the wasm type
64
/// carrying it, so a value of it has to be brought back into
65
/// range wherever CIL would truncate it.
66
is_narrow(scalar: Scalar) -> bool static =>
67
case scalar
68
when Scalar.BOOL, Scalar.CHAR, Scalar.INT8, Scalar.UINT8, Scalar.INT16, Scalar.UINT16 then true
69
else false
70
esac
71
72
/// Whether the kind's top bit is part of its magnitude.
73
is_unsigned(scalar: Scalar) -> bool static =>
74
case scalar
75
when Scalar.BOOL, Scalar.CHAR, Scalar.UINT8, Scalar.UINT16 then true
76
when Scalar.UINT32, Scalar.UINT64, Scalar.UINT_NATIVE then true
77
else false
78
esac
79
80
/// Whether the kind is one of the two floating kinds.
81
is_float(scalar: Scalar) -> bool static =>
82
scalar == Scalar.FLOAT32 \/ scalar == Scalar.FLOAT64
83
si
84
si