Skip to content

Commit

Permalink
feat(compiler)!: Remove 32-bit numbers from Number type (grain-lang…
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko authored Feb 26, 2023
1 parent fc4670d commit 50bf8ee
Show file tree
Hide file tree
Showing 48 changed files with 393 additions and 886 deletions.
131 changes: 55 additions & 76 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -1298,27 +1298,12 @@ let compile_array_op = (wasm_mod, env, arr_imm, op) => {
Op.or_int32,
Expression.Binary.make(
wasm_mod,
Op.or_int32,
Expression.Binary.make(
wasm_mod,
Op.eq_int32,
get_swap(1),
Expression.Const.make(
wasm_mod,
const_int32(
tag_val_of_boxed_number_tag_type(BoxedInt32),
),
),
),
Expression.Binary.make(
Op.eq_int32,
get_swap(1),
Expression.Const.make(
wasm_mod,
Op.eq_int32,
get_swap(1),
Expression.Const.make(
wasm_mod,
const_int32(
tag_val_of_boxed_number_tag_type(BoxedInt64),
),
const_int32(
tag_val_of_boxed_number_tag_type(BoxedInt64),
),
),
),
Expand Down Expand Up @@ -2046,17 +2031,21 @@ let allocate_record = (wasm_mod, env, ttag, elts) => {
);
};

let allocate_uint_uninitialized = (wasm_mod, env, is_32_bit) => {
// "Alt" number here is defined as one not belonging to the `Number` type
let allocate_alt_num_uninitialized = (wasm_mod, env, tag) => {
let get_swap = () => get_swap(wasm_mod, env, 0);
let make_alloc = () =>
heap_allocate(wasm_mod, env, if (is_32_bit) {2} else {4});

let (tag, label) =
if (is_32_bit) {
(Uint32Type, "allocate_unitialized_uint32");
} else {
(Uint64Type, "allocate_unitialized_uint64");
let (num_words, label) =
switch (tag) {
| Int32Type => (2, "allocate_unitialized_int32")
| Float32Type => (2, "allocate_unitialized_float32")
| Uint32Type => (2, "allocate_unitialized_uint32")
| Uint64Type => (4, "allocate_unitialized_uint64")
| _ =>
failwith(
"Impossible: allocate_alt_num_uninitialized given non-alt-num tag",
)
};
let make_alloc = () => heap_allocate(wasm_mod, env, num_words);

let preamble = [
store(
Expand All @@ -2077,15 +2066,29 @@ let allocate_uint_uninitialized = (wasm_mod, env, is_32_bit) => {
);
};

type alloc_uint_type =
type alloc_alt_num_type =
| Int32(Expression.t)
| Float32(Expression.t)
| Uint32(Expression.t)
| Uint64(Expression.t);

let allocate_uint = (wasm_mod, env, uint_value) => {
let allocate_alt_num = (wasm_mod, env, num_value) => {
let get_swap = () => get_swap(wasm_mod, env, 0);

let (tag, instrs, needed_words, label) =
switch (uint_value) {
switch (num_value) {
| Int32(int32) => (
Int32Type,
[store(~offset=4, ~ty=Type.int32, wasm_mod, get_swap(), int32)],
2,
"allocate_int32",
)
| Float32(float32) => (
Float32Type,
[store(~offset=4, ~ty=Type.float32, wasm_mod, get_swap(), float32)],
2,
"allocate_float32",
)
| Uint32(uint32) => (
Uint32Type,
[store(~offset=4, ~ty=Type.int32, wasm_mod, get_swap(), uint32)],
Expand Down Expand Up @@ -2126,18 +2129,8 @@ let allocate_uint = (wasm_mod, env, uint_value) => {
);
};

let allocate_uint32 = (wasm_mod, env, i) => {
allocate_uint(wasm_mod, env, Uint32(i));
};

let allocate_uint64 = (wasm_mod, env, i) => {
allocate_uint(wasm_mod, env, Uint64(i));
};

type alloc_number_type =
| Int32(Expression.t)
| Int64(Expression.t)
| Float32(Expression.t)
| Float64(Expression.t)
| Rational(Expression.t, Expression.t)
| BigInt(Expression.t, list(Expression.t));
Expand All @@ -2150,14 +2143,6 @@ let allocate_number = (wasm_mod, env, number) => {

let (number_tag, swap_slot, instrs, needed_words) =
switch (number) {
| Int32(int32) =>
let slot = 0;
(
BoxedInt32,
slot,
[store(~offset=8, ~ty=Type.int32, wasm_mod, get_swap(slot), int32)],
3,
);
| Int64(int64) =>
let slot = 0;
(
Expand All @@ -2166,22 +2151,6 @@ let allocate_number = (wasm_mod, env, number) => {
[store(~offset=8, ~ty=Type.int64, wasm_mod, get_swap(slot), int64)],
4,
);
| Float32(float32) =>
let slot = 0;
(
BoxedFloat32,
slot,
[
store(
~offset=8,
~ty=Type.float32,
wasm_mod,
get_swap(slot),
float32,
),
],
3,
);
| Float64(float64) =>
let slot = 0;
(
Expand Down Expand Up @@ -2343,15 +2312,15 @@ let allocate_number_uninitialized =
};

let allocate_float32 = (wasm_mod, env, i) => {
allocate_number(wasm_mod, env, Float32(i));
allocate_alt_num(wasm_mod, env, Float32(i));
};

let allocate_float64 = (wasm_mod, env, i) => {
allocate_number(wasm_mod, env, Float64(i));
};

let allocate_int32 = (wasm_mod, env, i) => {
allocate_number(wasm_mod, env, Int32(i));
allocate_alt_num(wasm_mod, env, Int32(i));
};

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

let allocate_uint32 = (wasm_mod, env, i) => {
allocate_alt_num(wasm_mod, env, Uint32(i));
};

let allocate_uint64 = (wasm_mod, env, i) => {
allocate_alt_num(wasm_mod, env, Uint64(i));
};

let tag_short_value = (wasm_mod, compiled_arg, tag) => {
Expression.Binary.make(
wasm_mod,
Expand All @@ -2382,16 +2359,18 @@ let tag_short_value = (wasm_mod, compiled_arg, tag) => {

let compile_prim0 = (wasm_mod, env, p0): Expression.t => {
switch (p0) {
| AllocateInt32 => allocate_number_uninitialized(wasm_mod, env, BoxedInt32)
| AllocateInt32 => allocate_alt_num_uninitialized(wasm_mod, env, Int32Type)
| AllocateInt64 => allocate_number_uninitialized(wasm_mod, env, BoxedInt64)
| AllocateFloat32 =>
allocate_number_uninitialized(wasm_mod, env, BoxedFloat32)
allocate_alt_num_uninitialized(wasm_mod, env, Float32Type)
| AllocateFloat64 =>
allocate_number_uninitialized(wasm_mod, env, BoxedFloat64)
| AllocateRational =>
allocate_number_uninitialized(wasm_mod, env, BoxedRational)
| AllocateUint32 => allocate_uint_uninitialized(wasm_mod, env, true)
| AllocateUint64 => allocate_uint_uninitialized(wasm_mod, env, false)
| AllocateUint32 =>
allocate_alt_num_uninitialized(wasm_mod, env, Uint32Type)
| AllocateUint64 =>
allocate_alt_num_uninitialized(wasm_mod, env, Uint64Type)
| WasmMemorySize => Expression.Memory_size.make(wasm_mod)
| Unreachable => Expression.Unreachable.make(wasm_mod)
};
Expand All @@ -2412,12 +2391,12 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
env,
BoxedBigInt,
)
| NewInt32 => allocate_number(wasm_mod, env, Int32(compiled_arg))
| NewInt32 => allocate_alt_num(wasm_mod, env, Int32(compiled_arg))
| NewInt64 => allocate_number(wasm_mod, env, Int64(compiled_arg))
| NewFloat32 => allocate_number(wasm_mod, env, Float32(compiled_arg))
| NewFloat32 => allocate_alt_num(wasm_mod, env, Float32(compiled_arg))
| NewFloat64 => allocate_number(wasm_mod, env, Float64(compiled_arg))
| NewUint32 => allocate_uint(wasm_mod, env, Uint32(compiled_arg))
| NewUint64 => allocate_uint(wasm_mod, env, Uint64(compiled_arg))
| NewUint32 => allocate_alt_num(wasm_mod, env, Uint32(compiled_arg))
| NewUint64 => allocate_alt_num(wasm_mod, env, Uint64(compiled_arg))
| LoadAdtVariant => load(~offset=12, wasm_mod, compiled_arg)
| StringSize
| BytesSize => load(~offset=4, wasm_mod, compiled_arg)
Expand Down
5 changes: 0 additions & 5 deletions compiler/src/codegen/transl_anf.re
Original file line number Diff line number Diff line change
Expand Up @@ -870,11 +870,6 @@ let rec compile_comp = (~id=?, env, c) => {
| CNumber(Const_number_int(n))
when n <= Literals.simple_number_max && n >= Literals.simple_number_min =>
MImmediate(MImmConst(MConstI32(Int64.to_int32(n))))
| CNumber(Const_number_int(n))
when
n <= Int64.of_int32(Int32.max_int)
&& n >= Int64.of_int32(Int32.min_int) =>
MAllocate(MInt32(Int64.to_int32(n)))
| CNumber(Const_number_int(n)) => MAllocate(MInt64(n))
| CNumber(Const_number_float(f)) => MAllocate(MFloat64(f))
| CNumber(
Expand Down
36 changes: 18 additions & 18 deletions compiler/src/codegen/value_tags.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type heap_tag_type =
| LambdaType
| TupleType
| BytesType
| Int32Type
| Float32Type
| Uint32Type
| Uint64Type;

Expand All @@ -24,8 +26,10 @@ let tag_val_of_heap_tag_type =
| LambdaType => 6
| TupleType => 7
| BytesType => 8
| Uint32Type => 9
| Uint64Type => 10;
| Int32Type => 9
| Float32Type => 10
| Uint32Type => 11
| Uint64Type => 12;

let heap_tag_type_of_tag_val =
fun
Expand All @@ -37,36 +41,32 @@ let heap_tag_type_of_tag_val =
| 6 => LambdaType
| 7 => TupleType
| 8 => BytesType
| 9 => Uint32Type
| 10 => Uint64Type
| 9 => Int32Type
| 10 => Float32Type
| 11 => Uint32Type
| 12 => Uint64Type
| x => failwith(Printf.sprintf("Unknown tag type: %d", x));

[@deriving sexp]
type boxed_number_tag_type =
| BoxedInt32
| BoxedInt64
| BoxedRational
| BoxedFloat32
| BoxedFloat64
| BoxedBigInt;

let tag_val_of_boxed_number_tag_type =
fun
| BoxedFloat32 => 1
| BoxedFloat64 => 2
| BoxedInt32 => 3
| BoxedInt64 => 4
| BoxedRational => 5
| BoxedBigInt => 6;
| BoxedFloat64 => 1
| BoxedInt64 => 2
| BoxedRational => 3
| BoxedBigInt => 4;

let boxed_number_tag_type_of_tag_val =
fun
| 1 => BoxedFloat32
| 2 => BoxedFloat64
| 3 => BoxedInt32
| 4 => BoxedInt64
| 5 => BoxedRational
| 6 => BoxedBigInt
| 1 => BoxedFloat64
| 2 => BoxedInt64
| 3 => BoxedRational
| 4 => BoxedBigInt
| x => failwith(Printf.sprintf("Unknown boxed num tag type: %d", x));

[@deriving sexp]
Expand Down
14 changes: 4 additions & 10 deletions compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,13 @@ arrays › array_access
(drop
(if (result i32)
(i32.or
(i32.or
(i32.eq
(local.get $1)
(i32.const 3)
)
(i32.eq
(local.get $1)
(i32.const 4)
)
(i32.eq
(local.get $1)
(i32.const 2)
)
(i32.eq
(local.get $1)
(i32.const 6)
(i32.const 4)
)
)
(call $panicWithException_0
Expand Down
14 changes: 4 additions & 10 deletions compiler/test/__snapshots__/arrays.1deb7b51.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,13 @@ arrays › array_access5
(drop
(if (result i32)
(i32.or
(i32.or
(i32.eq
(local.get $1)
(i32.const 3)
)
(i32.eq
(local.get $1)
(i32.const 4)
)
(i32.eq
(local.get $1)
(i32.const 2)
)
(i32.eq
(local.get $1)
(i32.const 6)
(i32.const 4)
)
)
(call $panicWithException_0
Expand Down
14 changes: 4 additions & 10 deletions compiler/test/__snapshots__/arrays.28fcc534.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,13 @@ arrays › array_access4
(drop
(if (result i32)
(i32.or
(i32.or
(i32.eq
(local.get $1)
(i32.const 3)
)
(i32.eq
(local.get $1)
(i32.const 4)
)
(i32.eq
(local.get $1)
(i32.const 2)
)
(i32.eq
(local.get $1)
(i32.const 6)
(i32.const 4)
)
)
(call $panicWithException_0
Expand Down
Loading

0 comments on commit 50bf8ee

Please sign in to comment.