Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): Add magic primitive #1766

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,7 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
Expression.Unreachable.make(wasm_mod),
],
)
| Magic => failwith("Unreachable case; should never get here: Magic")
| Assert => failwith("Unreachable case; should never get here: Assert")
| BuiltinId =>
failwith("Unreachable case; should never get here: BuiltinId")
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/codegen/garbage_collection.re
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ let rec apply_gc = (~level, ~loop_context, ~implicit_return=false, instrs) => {
| _ => ()
};
MPrim1(WasmFromGrain, handle_imm(~non_gc_instr=true, imm));
| MPrim1((Box | BoxBind | Throw) as prim1, imm) =>
| MPrim1((Box | BoxBind | Throw | Magic) as prim1, imm) =>
MPrim1(prim1, handle_imm(imm))
| MPrim1(prim1, imm) =>
MPrim1(prim1, handle_imm(~non_gc_instr=true, imm))
Expand Down
1 change: 1 addition & 0 deletions compiler/src/codegen/mashtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ type prim1 =
| ArrayLength
| Assert
| Throw
| Magic
| WasmFromGrain
| WasmToGrain
| WasmUnaryI32({
Expand Down
1 change: 1 addition & 0 deletions compiler/src/middle_end/analyze_purity.re
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module PurityArg: Anf_iterator.IterArgument = {
UntagUint8 |
TagUint16 |
UntagUint16 |
Magic |
Not |
Box |
Unbox |
Expand Down
1 change: 1 addition & 0 deletions compiler/src/middle_end/anftree.re
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type prim1 =
| ArrayLength
| Assert
| Throw
| Magic
| WasmFromGrain
| WasmToGrain
| WasmUnaryI32({
Expand Down
1 change: 1 addition & 0 deletions compiler/src/middle_end/anftree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type prim1 =
| ArrayLength
| Assert
| Throw
| Magic
| WasmFromGrain
| WasmToGrain
| WasmUnaryI32({
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/middle_end/linearize.re
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ let rec transl_imm =
}
| _ => failwith("Builtin must be a string literal")
}
| TExpPrim1(Magic, arg) => transl_imm(~boxed, ~tail, arg)
| TExpPrim1(op, arg) =>
let tmp = gensym("unary");
let (comp, comp_setup) = transl_comp_expression(e);
Expand Down Expand Up @@ -1034,6 +1035,7 @@ and transl_comp_expression =
}
| _ => failwith("Builtin must be a string literal")
}
| TExpPrim1(Magic, arg) => transl_comp_expression(~name?, ~tail, arg)
| TExpPrim1(Assert, arg) =>
let (arg_imm, arg_setup) = transl_imm(arg);
let assertion_error =
Expand Down
1 change: 1 addition & 0 deletions compiler/src/parsing/parsetree.re
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ type prim1 =
| ArrayLength
| Assert
| Throw
| Magic
| WasmFromGrain
| WasmToGrain
| WasmUnaryI32({
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/typed/translprim.re
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let prim_map =
("@ignore", Primitive1(Ignore)),
("@assert", Primitive1(Assert)),
("@throw", Primitive1(Throw)),
("@magic", Primitive1(Magic)),
("@unreachable", Primitive0(Unreachable)),
("@is", Primitive2(Is)),
("@eq", Primitive2(Eq)),
Expand Down Expand Up @@ -1589,6 +1590,7 @@ let transl_prim = (env, desc) => {
| Ignore
| Assert
| Throw
| Magic
| BuiltinId => []
};
(
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/typed/typecore.re
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ let prim1_type =
| Assert => prim_type([Builtin_types.type_bool], Builtin_types.type_void)
| Throw =>
prim_type([Builtin_types.type_exception], newgenvar(~name="a", ()))
| Magic =>
prim_type([newgenvar(~name="a", ())], newgenvar(~name="b", ()))
| WasmFromGrain =>
prim_type([newgenvar(~name="a", ())], Builtin_types.type_wasmi32)
| WasmToGrain =>
Expand Down
1 change: 1 addition & 0 deletions compiler/src/typed/typedtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type prim1 =
| ArrayLength
| Assert
| Throw
| Magic
| WasmFromGrain
| WasmToGrain
| WasmUnaryI32({
Expand Down
1 change: 1 addition & 0 deletions compiler/src/typed/typedtree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type prim1 =
| ArrayLength
| Assert
| Throw
| Magic
| WasmFromGrain
| WasmToGrain
| WasmUnaryI32({
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/suites/basic_functionality.re
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ describe("basic functionality", ({test, testSkip}) => {
)
);

assertRun(
"magic",
{|
primitive magic = "@magic"
let helloBytes = b"hello"
print(magic(helloBytes) ++ " world")
|},
"hello world\n",
);

assertFilesize(
~config_fn=smallestFileConfig,
"smallest_grain_program",
Expand Down