Skip to content

Commit a28ebb4

Browse files
ospencerphated
andauthored
feat(compiler): Add magic primitive (#1766)
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
1 parent d512c5a commit a28ebb4

File tree

13 files changed

+25
-1
lines changed

13 files changed

+25
-1
lines changed

compiler/src/codegen/compcore.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,7 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
22952295
Expression.Unreachable.make(wasm_mod),
22962296
],
22972297
)
2298+
| Magic => failwith("Unreachable case; should never get here: Magic")
22982299
| Assert => failwith("Unreachable case; should never get here: Assert")
22992300
| BuiltinId =>
23002301
failwith("Unreachable case; should never get here: BuiltinId")

compiler/src/codegen/garbage_collection.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ let rec apply_gc = (~level, ~loop_context, ~implicit_return=false, instrs) => {
456456
| _ => ()
457457
};
458458
MPrim1(WasmFromGrain, handle_imm(~non_gc_instr=true, imm));
459-
| MPrim1((Box | BoxBind | Throw) as prim1, imm) =>
459+
| MPrim1((Box | BoxBind | Throw | Magic) as prim1, imm) =>
460460
MPrim1(prim1, handle_imm(imm))
461461
| MPrim1(prim1, imm) =>
462462
MPrim1(prim1, handle_imm(~non_gc_instr=true, imm))

compiler/src/codegen/mashtree.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ type prim1 =
221221
| ArrayLength
222222
| Assert
223223
| Throw
224+
| Magic
224225
| WasmFromGrain
225226
| WasmToGrain
226227
| WasmUnaryI32({

compiler/src/middle_end/analyze_purity.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module PurityArg: Anf_iterator.IterArgument = {
7373
UntagUint8 |
7474
TagUint16 |
7575
UntagUint16 |
76+
Magic |
7677
Not |
7778
Box |
7879
Unbox |

compiler/src/middle_end/anftree.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ type prim1 =
207207
| ArrayLength
208208
| Assert
209209
| Throw
210+
| Magic
210211
| WasmFromGrain
211212
| WasmToGrain
212213
| WasmUnaryI32({

compiler/src/middle_end/anftree.rei

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ type prim1 =
208208
| ArrayLength
209209
| Assert
210210
| Throw
211+
| Magic
211212
| WasmFromGrain
212213
| WasmToGrain
213214
| WasmUnaryI32({

compiler/src/middle_end/linearize.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ let rec transl_imm =
341341
}
342342
| _ => failwith("Builtin must be a string literal")
343343
}
344+
| TExpPrim1(Magic, arg) => transl_imm(~boxed, ~tail, arg)
344345
| TExpPrim1(op, arg) =>
345346
let tmp = gensym("unary");
346347
let (comp, comp_setup) = transl_comp_expression(e);
@@ -1034,6 +1035,7 @@ and transl_comp_expression =
10341035
}
10351036
| _ => failwith("Builtin must be a string literal")
10361037
}
1038+
| TExpPrim1(Magic, arg) => transl_comp_expression(~name?, ~tail, arg)
10371039
| TExpPrim1(Assert, arg) =>
10381040
let (arg_imm, arg_setup) = transl_imm(arg);
10391041
let assertion_error =

compiler/src/parsing/parsetree.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ type prim1 =
375375
| ArrayLength
376376
| Assert
377377
| Throw
378+
| Magic
378379
| WasmFromGrain
379380
| WasmToGrain
380381
| WasmUnaryI32({

compiler/src/typed/translprim.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ let prim_map =
9090
("@ignore", Primitive1(Ignore)),
9191
("@assert", Primitive1(Assert)),
9292
("@throw", Primitive1(Throw)),
93+
("@magic", Primitive1(Magic)),
9394
("@unreachable", Primitive0(Unreachable)),
9495
("@is", Primitive2(Is)),
9596
("@eq", Primitive2(Eq)),
@@ -1589,6 +1590,7 @@ let transl_prim = (env, desc) => {
15891590
| Ignore
15901591
| Assert
15911592
| Throw
1593+
| Magic
15921594
| BuiltinId => []
15931595
};
15941596
(

compiler/src/typed/typecore.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ let prim1_type =
194194
| Assert => prim_type([Builtin_types.type_bool], Builtin_types.type_void)
195195
| Throw =>
196196
prim_type([Builtin_types.type_exception], newgenvar(~name="a", ()))
197+
| Magic =>
198+
prim_type([newgenvar(~name="a", ())], newgenvar(~name="b", ()))
197199
| WasmFromGrain =>
198200
prim_type([newgenvar(~name="a", ())], Builtin_types.type_wasmi32)
199201
| WasmToGrain =>

0 commit comments

Comments
 (0)