Skip to content

Commit 50bf8ee

Browse files
authored
feat(compiler)!: Remove 32-bit numbers from Number type (#1683)
1 parent fc4670d commit 50bf8ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+393
-886
lines changed

compiler/src/codegen/compcore.re

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,27 +1298,12 @@ let compile_array_op = (wasm_mod, env, arr_imm, op) => {
12981298
Op.or_int32,
12991299
Expression.Binary.make(
13001300
wasm_mod,
1301-
Op.or_int32,
1302-
Expression.Binary.make(
1303-
wasm_mod,
1304-
Op.eq_int32,
1305-
get_swap(1),
1306-
Expression.Const.make(
1307-
wasm_mod,
1308-
const_int32(
1309-
tag_val_of_boxed_number_tag_type(BoxedInt32),
1310-
),
1311-
),
1312-
),
1313-
Expression.Binary.make(
1301+
Op.eq_int32,
1302+
get_swap(1),
1303+
Expression.Const.make(
13141304
wasm_mod,
1315-
Op.eq_int32,
1316-
get_swap(1),
1317-
Expression.Const.make(
1318-
wasm_mod,
1319-
const_int32(
1320-
tag_val_of_boxed_number_tag_type(BoxedInt64),
1321-
),
1305+
const_int32(
1306+
tag_val_of_boxed_number_tag_type(BoxedInt64),
13221307
),
13231308
),
13241309
),
@@ -2046,17 +2031,21 @@ let allocate_record = (wasm_mod, env, ttag, elts) => {
20462031
);
20472032
};
20482033

2049-
let allocate_uint_uninitialized = (wasm_mod, env, is_32_bit) => {
2034+
// "Alt" number here is defined as one not belonging to the `Number` type
2035+
let allocate_alt_num_uninitialized = (wasm_mod, env, tag) => {
20502036
let get_swap = () => get_swap(wasm_mod, env, 0);
2051-
let make_alloc = () =>
2052-
heap_allocate(wasm_mod, env, if (is_32_bit) {2} else {4});
2053-
2054-
let (tag, label) =
2055-
if (is_32_bit) {
2056-
(Uint32Type, "allocate_unitialized_uint32");
2057-
} else {
2058-
(Uint64Type, "allocate_unitialized_uint64");
2037+
let (num_words, label) =
2038+
switch (tag) {
2039+
| Int32Type => (2, "allocate_unitialized_int32")
2040+
| Float32Type => (2, "allocate_unitialized_float32")
2041+
| Uint32Type => (2, "allocate_unitialized_uint32")
2042+
| Uint64Type => (4, "allocate_unitialized_uint64")
2043+
| _ =>
2044+
failwith(
2045+
"Impossible: allocate_alt_num_uninitialized given non-alt-num tag",
2046+
)
20592047
};
2048+
let make_alloc = () => heap_allocate(wasm_mod, env, num_words);
20602049

20612050
let preamble = [
20622051
store(
@@ -2077,15 +2066,29 @@ let allocate_uint_uninitialized = (wasm_mod, env, is_32_bit) => {
20772066
);
20782067
};
20792068

2080-
type alloc_uint_type =
2069+
type alloc_alt_num_type =
2070+
| Int32(Expression.t)
2071+
| Float32(Expression.t)
20812072
| Uint32(Expression.t)
20822073
| Uint64(Expression.t);
20832074

2084-
let allocate_uint = (wasm_mod, env, uint_value) => {
2075+
let allocate_alt_num = (wasm_mod, env, num_value) => {
20852076
let get_swap = () => get_swap(wasm_mod, env, 0);
20862077

20872078
let (tag, instrs, needed_words, label) =
2088-
switch (uint_value) {
2079+
switch (num_value) {
2080+
| Int32(int32) => (
2081+
Int32Type,
2082+
[store(~offset=4, ~ty=Type.int32, wasm_mod, get_swap(), int32)],
2083+
2,
2084+
"allocate_int32",
2085+
)
2086+
| Float32(float32) => (
2087+
Float32Type,
2088+
[store(~offset=4, ~ty=Type.float32, wasm_mod, get_swap(), float32)],
2089+
2,
2090+
"allocate_float32",
2091+
)
20892092
| Uint32(uint32) => (
20902093
Uint32Type,
20912094
[store(~offset=4, ~ty=Type.int32, wasm_mod, get_swap(), uint32)],
@@ -2126,18 +2129,8 @@ let allocate_uint = (wasm_mod, env, uint_value) => {
21262129
);
21272130
};
21282131

2129-
let allocate_uint32 = (wasm_mod, env, i) => {
2130-
allocate_uint(wasm_mod, env, Uint32(i));
2131-
};
2132-
2133-
let allocate_uint64 = (wasm_mod, env, i) => {
2134-
allocate_uint(wasm_mod, env, Uint64(i));
2135-
};
2136-
21372132
type alloc_number_type =
2138-
| Int32(Expression.t)
21392133
| Int64(Expression.t)
2140-
| Float32(Expression.t)
21412134
| Float64(Expression.t)
21422135
| Rational(Expression.t, Expression.t)
21432136
| BigInt(Expression.t, list(Expression.t));
@@ -2150,14 +2143,6 @@ let allocate_number = (wasm_mod, env, number) => {
21502143

21512144
let (number_tag, swap_slot, instrs, needed_words) =
21522145
switch (number) {
2153-
| Int32(int32) =>
2154-
let slot = 0;
2155-
(
2156-
BoxedInt32,
2157-
slot,
2158-
[store(~offset=8, ~ty=Type.int32, wasm_mod, get_swap(slot), int32)],
2159-
3,
2160-
);
21612146
| Int64(int64) =>
21622147
let slot = 0;
21632148
(
@@ -2166,22 +2151,6 @@ let allocate_number = (wasm_mod, env, number) => {
21662151
[store(~offset=8, ~ty=Type.int64, wasm_mod, get_swap(slot), int64)],
21672152
4,
21682153
);
2169-
| Float32(float32) =>
2170-
let slot = 0;
2171-
(
2172-
BoxedFloat32,
2173-
slot,
2174-
[
2175-
store(
2176-
~offset=8,
2177-
~ty=Type.float32,
2178-
wasm_mod,
2179-
get_swap(slot),
2180-
float32,
2181-
),
2182-
],
2183-
3,
2184-
);
21852154
| Float64(float64) =>
21862155
let slot = 0;
21872156
(
@@ -2343,15 +2312,15 @@ let allocate_number_uninitialized =
23432312
};
23442313

23452314
let allocate_float32 = (wasm_mod, env, i) => {
2346-
allocate_number(wasm_mod, env, Float32(i));
2315+
allocate_alt_num(wasm_mod, env, Float32(i));
23472316
};
23482317

23492318
let allocate_float64 = (wasm_mod, env, i) => {
23502319
allocate_number(wasm_mod, env, Float64(i));
23512320
};
23522321

23532322
let allocate_int32 = (wasm_mod, env, i) => {
2354-
allocate_number(wasm_mod, env, Int32(i));
2323+
allocate_alt_num(wasm_mod, env, Int32(i));
23552324
};
23562325

23572326
let allocate_int64 = (wasm_mod, env, i) => {
@@ -2366,6 +2335,14 @@ let allocate_big_int = (wasm_mod, env, n, d) => {
23662335
allocate_number(wasm_mod, env, BigInt(n, d));
23672336
};
23682337

2338+
let allocate_uint32 = (wasm_mod, env, i) => {
2339+
allocate_alt_num(wasm_mod, env, Uint32(i));
2340+
};
2341+
2342+
let allocate_uint64 = (wasm_mod, env, i) => {
2343+
allocate_alt_num(wasm_mod, env, Uint64(i));
2344+
};
2345+
23692346
let tag_short_value = (wasm_mod, compiled_arg, tag) => {
23702347
Expression.Binary.make(
23712348
wasm_mod,
@@ -2382,16 +2359,18 @@ let tag_short_value = (wasm_mod, compiled_arg, tag) => {
23822359

23832360
let compile_prim0 = (wasm_mod, env, p0): Expression.t => {
23842361
switch (p0) {
2385-
| AllocateInt32 => allocate_number_uninitialized(wasm_mod, env, BoxedInt32)
2362+
| AllocateInt32 => allocate_alt_num_uninitialized(wasm_mod, env, Int32Type)
23862363
| AllocateInt64 => allocate_number_uninitialized(wasm_mod, env, BoxedInt64)
23872364
| AllocateFloat32 =>
2388-
allocate_number_uninitialized(wasm_mod, env, BoxedFloat32)
2365+
allocate_alt_num_uninitialized(wasm_mod, env, Float32Type)
23892366
| AllocateFloat64 =>
23902367
allocate_number_uninitialized(wasm_mod, env, BoxedFloat64)
23912368
| AllocateRational =>
23922369
allocate_number_uninitialized(wasm_mod, env, BoxedRational)
2393-
| AllocateUint32 => allocate_uint_uninitialized(wasm_mod, env, true)
2394-
| AllocateUint64 => allocate_uint_uninitialized(wasm_mod, env, false)
2370+
| AllocateUint32 =>
2371+
allocate_alt_num_uninitialized(wasm_mod, env, Uint32Type)
2372+
| AllocateUint64 =>
2373+
allocate_alt_num_uninitialized(wasm_mod, env, Uint64Type)
23952374
| WasmMemorySize => Expression.Memory_size.make(wasm_mod)
23962375
| Unreachable => Expression.Unreachable.make(wasm_mod)
23972376
};
@@ -2412,12 +2391,12 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
24122391
env,
24132392
BoxedBigInt,
24142393
)
2415-
| NewInt32 => allocate_number(wasm_mod, env, Int32(compiled_arg))
2394+
| NewInt32 => allocate_alt_num(wasm_mod, env, Int32(compiled_arg))
24162395
| NewInt64 => allocate_number(wasm_mod, env, Int64(compiled_arg))
2417-
| NewFloat32 => allocate_number(wasm_mod, env, Float32(compiled_arg))
2396+
| NewFloat32 => allocate_alt_num(wasm_mod, env, Float32(compiled_arg))
24182397
| NewFloat64 => allocate_number(wasm_mod, env, Float64(compiled_arg))
2419-
| NewUint32 => allocate_uint(wasm_mod, env, Uint32(compiled_arg))
2420-
| NewUint64 => allocate_uint(wasm_mod, env, Uint64(compiled_arg))
2398+
| NewUint32 => allocate_alt_num(wasm_mod, env, Uint32(compiled_arg))
2399+
| NewUint64 => allocate_alt_num(wasm_mod, env, Uint64(compiled_arg))
24212400
| LoadAdtVariant => load(~offset=12, wasm_mod, compiled_arg)
24222401
| StringSize
24232402
| BytesSize => load(~offset=4, wasm_mod, compiled_arg)

compiler/src/codegen/transl_anf.re

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,11 +870,6 @@ let rec compile_comp = (~id=?, env, c) => {
870870
| CNumber(Const_number_int(n))
871871
when n <= Literals.simple_number_max && n >= Literals.simple_number_min =>
872872
MImmediate(MImmConst(MConstI32(Int64.to_int32(n))))
873-
| CNumber(Const_number_int(n))
874-
when
875-
n <= Int64.of_int32(Int32.max_int)
876-
&& n >= Int64.of_int32(Int32.min_int) =>
877-
MAllocate(MInt32(Int64.to_int32(n)))
878873
| CNumber(Const_number_int(n)) => MAllocate(MInt64(n))
879874
| CNumber(Const_number_float(f)) => MAllocate(MFloat64(f))
880875
| CNumber(

compiler/src/codegen/value_tags.re

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type heap_tag_type =
1111
| LambdaType
1212
| TupleType
1313
| BytesType
14+
| Int32Type
15+
| Float32Type
1416
| Uint32Type
1517
| Uint64Type;
1618

@@ -24,8 +26,10 @@ let tag_val_of_heap_tag_type =
2426
| LambdaType => 6
2527
| TupleType => 7
2628
| BytesType => 8
27-
| Uint32Type => 9
28-
| Uint64Type => 10;
29+
| Int32Type => 9
30+
| Float32Type => 10
31+
| Uint32Type => 11
32+
| Uint64Type => 12;
2933

3034
let heap_tag_type_of_tag_val =
3135
fun
@@ -37,36 +41,32 @@ let heap_tag_type_of_tag_val =
3741
| 6 => LambdaType
3842
| 7 => TupleType
3943
| 8 => BytesType
40-
| 9 => Uint32Type
41-
| 10 => Uint64Type
44+
| 9 => Int32Type
45+
| 10 => Float32Type
46+
| 11 => Uint32Type
47+
| 12 => Uint64Type
4248
| x => failwith(Printf.sprintf("Unknown tag type: %d", x));
4349

4450
[@deriving sexp]
4551
type boxed_number_tag_type =
46-
| BoxedInt32
4752
| BoxedInt64
4853
| BoxedRational
49-
| BoxedFloat32
5054
| BoxedFloat64
5155
| BoxedBigInt;
5256

5357
let tag_val_of_boxed_number_tag_type =
5458
fun
55-
| BoxedFloat32 => 1
56-
| BoxedFloat64 => 2
57-
| BoxedInt32 => 3
58-
| BoxedInt64 => 4
59-
| BoxedRational => 5
60-
| BoxedBigInt => 6;
59+
| BoxedFloat64 => 1
60+
| BoxedInt64 => 2
61+
| BoxedRational => 3
62+
| BoxedBigInt => 4;
6163

6264
let boxed_number_tag_type_of_tag_val =
6365
fun
64-
| 1 => BoxedFloat32
65-
| 2 => BoxedFloat64
66-
| 3 => BoxedInt32
67-
| 4 => BoxedInt64
68-
| 5 => BoxedRational
69-
| 6 => BoxedBigInt
66+
| 1 => BoxedFloat64
67+
| 2 => BoxedInt64
68+
| 3 => BoxedRational
69+
| 4 => BoxedBigInt
7070
| x => failwith(Printf.sprintf("Unknown boxed num tag type: %d", x));
7171

7272
[@deriving sexp]

compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,13 @@ arrays › array_access
109109
(drop
110110
(if (result i32)
111111
(i32.or
112-
(i32.or
113-
(i32.eq
114-
(local.get $1)
115-
(i32.const 3)
116-
)
117-
(i32.eq
118-
(local.get $1)
119-
(i32.const 4)
120-
)
112+
(i32.eq
113+
(local.get $1)
114+
(i32.const 2)
121115
)
122116
(i32.eq
123117
(local.get $1)
124-
(i32.const 6)
118+
(i32.const 4)
125119
)
126120
)
127121
(call $panicWithException_0

compiler/test/__snapshots__/arrays.1deb7b51.0.snapshot

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,13 @@ arrays › array_access5
109109
(drop
110110
(if (result i32)
111111
(i32.or
112-
(i32.or
113-
(i32.eq
114-
(local.get $1)
115-
(i32.const 3)
116-
)
117-
(i32.eq
118-
(local.get $1)
119-
(i32.const 4)
120-
)
112+
(i32.eq
113+
(local.get $1)
114+
(i32.const 2)
121115
)
122116
(i32.eq
123117
(local.get $1)
124-
(i32.const 6)
118+
(i32.const 4)
125119
)
126120
)
127121
(call $panicWithException_0

compiler/test/__snapshots__/arrays.28fcc534.0.snapshot

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,13 @@ arrays › array_access4
109109
(drop
110110
(if (result i32)
111111
(i32.or
112-
(i32.or
113-
(i32.eq
114-
(local.get $1)
115-
(i32.const 3)
116-
)
117-
(i32.eq
118-
(local.get $1)
119-
(i32.const 4)
120-
)
112+
(i32.eq
113+
(local.get $1)
114+
(i32.const 2)
121115
)
122116
(i32.eq
123117
(local.get $1)
124-
(i32.const 6)
118+
(i32.const 4)
125119
)
126120
)
127121
(call $panicWithException_0

0 commit comments

Comments
 (0)