Skip to content

Commit 943d47d

Browse files
authored
fix(compiler): Panic immediately when out of memory (#1450)
1 parent a5ff379 commit 943d47d

20 files changed

+206
-253
lines changed

compiler/src/codegen/compcore.re

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ let decref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$decRef");
6666

6767
/* Exceptions */
6868
let exception_mod = "GRAIN$MODULE$runtime/exception";
69-
let print_exception_ident = Ident.create_persistent("printException");
70-
let print_exception_closure_ident =
71-
Ident.create_persistent("GRAIN$EXPORT$printException");
69+
let panic_with_exception_ident =
70+
Ident.create_persistent("panicWithException");
71+
let panic_with_exception_closure_ident =
72+
Ident.create_persistent("GRAIN$EXPORT$panicWithException");
7273
let assertion_error_ident = Ident.create_persistent("AssertionError");
7374
let assertion_error_closure_ident =
7475
Ident.create_persistent("GRAIN$EXPORT$AssertionError");
@@ -109,9 +110,9 @@ let required_global_imports = [
109110
mimp_used: false,
110111
},
111112
{
112-
mimp_id: print_exception_closure_ident,
113+
mimp_id: panic_with_exception_closure_ident,
113114
mimp_mod: exception_mod,
114-
mimp_name: Ident.name(print_exception_closure_ident),
115+
mimp_name: Ident.name(panic_with_exception_closure_ident),
115116
mimp_type: MGlobalImport(Types.Unmanaged(WasmI32), true),
116117
mimp_kind: MImportWasm,
117118
mimp_setup: MSetupNone,
@@ -190,9 +191,9 @@ let runtime_global_imports =
190191

191192
let required_function_imports = [
192193
{
193-
mimp_id: print_exception_ident,
194+
mimp_id: panic_with_exception_ident,
194195
mimp_mod: exception_mod,
195-
mimp_name: Ident.name(print_exception_ident),
196+
mimp_name: Ident.name(panic_with_exception_ident),
196197
mimp_type:
197198
MFuncImport(
198199
[Types.Unmanaged(WasmI32), Types.Unmanaged(WasmI32)],
@@ -346,18 +347,21 @@ let get_wasm_imported_name = (~runtime_import=true, mod_, name) => {
346347

347348
let get_grain_imported_name = (mod_, name) => Ident.unique_name(name);
348349

349-
let call_exception_printer = (wasm_mod, env, args) => {
350+
let call_panic_handler = (wasm_mod, env, args) => {
350351
let args = [
351352
Expression.Global_get.make(
352353
wasm_mod,
353-
get_wasm_imported_name(exception_mod, print_exception_closure_ident),
354+
get_wasm_imported_name(
355+
exception_mod,
356+
panic_with_exception_closure_ident,
357+
),
354358
Type.int32,
355359
),
356360
...args,
357361
];
358362
Expression.Call.make(
359363
wasm_mod,
360-
get_wasm_imported_name(exception_mod, print_exception_ident),
364+
get_wasm_imported_name(exception_mod, panic_with_exception_ident),
361365
args,
362366
Type.int32,
363367
);
@@ -1028,17 +1032,7 @@ let call_error_handler = (wasm_mod, env, err, args) => {
10281032
Type.int32,
10291033
);
10301034
};
1031-
Expression.Block.make(
1032-
wasm_mod,
1033-
gensym_label("call_error_handler"),
1034-
[
1035-
Expression.Drop.make(
1036-
wasm_mod,
1037-
call_exception_printer(wasm_mod, env, [err]),
1038-
),
1039-
Expression.Unreachable.make(wasm_mod),
1040-
],
1041-
);
1035+
call_panic_handler(wasm_mod, env, [err]);
10421036
};
10431037

10441038
let error_if_true = (wasm_mod, env, cond, err, args) =>
@@ -2367,6 +2361,7 @@ let compile_prim0 = (wasm_mod, env, p0): Expression.t => {
23672361
allocate_number_uninitialized(wasm_mod, env, BoxedFloat64)
23682362
| AllocateRational =>
23692363
allocate_number_uninitialized(wasm_mod, env, BoxedRational)
2364+
| Unreachable => Expression.Unreachable.make(wasm_mod)
23702365
};
23712366
};
23722367

@@ -2486,7 +2481,7 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
24862481
[
24872482
Expression.Drop.make(
24882483
wasm_mod,
2489-
call_exception_printer(wasm_mod, env, [compiled_arg]),
2484+
call_panic_handler(wasm_mod, env, [compiled_arg]),
24902485
),
24912486
Expression.Unreachable.make(wasm_mod),
24922487
],

compiler/src/codegen/mashtree.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ type prim0 =
176176
| AllocateInt64
177177
| AllocateFloat32
178178
| AllocateFloat64
179-
| AllocateRational;
179+
| AllocateRational
180+
| Unreachable;
180181

181182
type prim1 =
182183
Parsetree.prim1 =

compiler/src/middle_end/analyze_purity.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let rec analyze_comp_expression =
4848
AllocateRational,
4949
) =>
5050
true
51+
| CPrim0(Unreachable) => false
5152
| CPrim1(
5253
AllocateArray | AllocateTuple | AllocateBytes | AllocateString |
5354
NewInt32 |

compiler/src/middle_end/anftree.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ type prim0 =
162162
| AllocateInt64
163163
| AllocateFloat32
164164
| AllocateFloat64
165-
| AllocateRational;
165+
| AllocateRational
166+
| Unreachable;
166167

167168
type prim1 =
168169
Parsetree.prim1 =

compiler/src/middle_end/anftree.rei

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ type prim0 =
163163
| AllocateInt64
164164
| AllocateFloat32
165165
| AllocateFloat64
166-
| AllocateRational;
166+
| AllocateRational
167+
| Unreachable;
167168

168169
type prim1 =
169170
Parsetree.prim1 =

compiler/src/parsing/parsetree.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ type prim0 =
308308
| AllocateInt64
309309
| AllocateFloat32
310310
| AllocateFloat64
311-
| AllocateRational;
311+
| AllocateRational
312+
| Unreachable;
312313

313314
/** Single-argument operators */
314315
[@deriving (sexp, yojson)]

compiler/src/typed/translprim.re

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ let prim_map =
7676
("@ignore", Primitive1(Ignore)),
7777
("@assert", Primitive1(Assert)),
7878
("@throw", Primitive1(Throw)),
79+
("@unreachable", Primitive0(Unreachable)),
7980
("@is", Primitive2(Is)),
8081
("@eq", Primitive2(Eq)),
8182
("@and", Primitive2(And)),
@@ -1507,7 +1508,8 @@ let transl_prim = (env, desc) => {
15071508
| Primitive0(
15081509
(
15091510
AllocateInt32 | AllocateInt64 | AllocateFloat32 | AllocateFloat64 |
1510-
AllocateRational
1511+
AllocateRational |
1512+
Unreachable
15111513
) as p,
15121514
) => (
15131515
Exp.lambda(~loc, ~attributes=disable_gc, [], Exp.prim0(~loc, p)),

compiler/src/typed/typecore.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ let prim0_type =
104104
| AllocateInt64
105105
| AllocateFloat32
106106
| AllocateFloat64
107-
| AllocateRational => Builtin_types.type_wasmi32;
107+
| AllocateRational => Builtin_types.type_wasmi32
108+
| Unreachable => newvar(~name="a", ());
108109

109110
let prim1_type =
110111
fun

compiler/src/typed/typedtree.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ type prim0 =
180180
| AllocateInt64
181181
| AllocateFloat32
182182
| AllocateFloat64
183-
| AllocateRational;
183+
| AllocateRational
184+
| Unreachable;
184185

185186
type prim1 =
186187
Parsetree.prim1 =

compiler/src/typed/typedtree.rei

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ type prim0 =
180180
| AllocateInt64
181181
| AllocateFloat32
182182
| AllocateFloat64
183-
| AllocateRational;
183+
| AllocateRational
184+
| Unreachable;
184185

185186
type prim1 =
186187
Parsetree.prim1 =

0 commit comments

Comments
 (0)