Skip to content

Commit

Permalink
chore(compiler): Update binaryen.ml to v0.20.1 (#1599)
Browse files Browse the repository at this point in the history
* chore(compiler): Update binaryen.ml to v0.20.1

* chore(compiler): Add internal memory name constant use it in Binaryen APIs

* chore(compiler): Remove RttCannon and RttSub variants
  • Loading branch information
phated authored Apr 27, 2023
1 parent 8d76dc9 commit 6930794
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 32 deletions.
2 changes: 1 addition & 1 deletion compiler/esy.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"check-format": "dune build @fmt"
},
"dependencies": {
"@grain/binaryen.ml": ">= 0.19.0 < 0.20.0",
"@grain/binaryen.ml": ">= 0.20.1 < 0.21.0",
"@opam/cmdliner": ">= 1.1.1",
"@opam/dune": ">= 3.6.1 < 4.0.0",
"@opam/dune-build-info": ">= 3.6.1 < 4.0.0",
Expand Down
38 changes: 19 additions & 19 deletions compiler/esy.lock/index.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions compiler/src/codegen/comp_utils.re
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let grain_main = "_gmain";
let grain_start = "_start";
let grain_env_name = "_genv";
let grain_global_function_table = "tbl";
// TODO(#): Use a more descriptive name once we get fixes into Binaryen
let grain_memory = "0";

let wasm_type =
fun
Expand Down Expand Up @@ -114,7 +116,16 @@ let store = (~ty=Type.int32, ~align=?, ~offset=0, ~sz=?, wasm_mod, ptr, arg) =>
sz,
);
let align = Option.value(~default=sz, align);
Expression.Store.make(wasm_mod, sz, offset, align, ptr, arg, ty);
Expression.Store.make(
wasm_mod,
sz,
offset,
align,
ptr,
arg,
ty,
grain_memory,
);
};

let load =
Expand All @@ -130,7 +141,16 @@ let load =
sz,
);
let align = Option.value(~default=sz, align);
Expression.Load.make(~signed, wasm_mod, sz, offset, align, ty, ptr);
Expression.Load.make(
~signed,
wasm_mod,
sz,
offset,
align,
ty,
ptr,
grain_memory,
);
};

let is_grain_env = str => grain_env_name == str;
Expand Down Expand Up @@ -235,6 +255,7 @@ let write_universal_exports =
2,
Type.int32,
get_closure(),
grain_memory,
);
Expression.Call_indirect.make(
wasm_mod,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/codegen/comp_utils.rei
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let grain_main: string;
let grain_start: string;
let grain_env_name: string;
let grain_global_function_table: string;
let grain_memory: string;

let wasm_type: Types.allocation_type => Type.t;

Expand Down
19 changes: 16 additions & 3 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -2189,7 +2189,8 @@ let compile_prim0 = (wasm_mod, env, p0): Expression.t => {
allocate_alt_num_uninitialized(wasm_mod, env, Uint32Type)
| AllocateUint64 =>
allocate_alt_num_uninitialized(wasm_mod, env, Uint64Type)
| WasmMemorySize => Expression.Memory_size.make(wasm_mod)
| WasmMemorySize =>
Expression.Memory_size.make(wasm_mod, grain_memory, false)
| Unreachable => Expression.Unreachable.make(wasm_mod)
| HeapStart => get_runtime_heap_start(wasm_mod)
| HeapTypeMetadata => get_metadata_ptr(wasm_mod)
Expand Down Expand Up @@ -2299,7 +2300,8 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
// no-op
compile_imm(wasm_mod, env, arg)
| WasmToGrain => compiled_arg // no-op
| WasmMemoryGrow => Expression.Memory_grow.make(wasm_mod, compiled_arg)
| WasmMemoryGrow =>
Expression.Memory_grow.make(wasm_mod, compiled_arg, grain_memory, false)
| WasmUnaryI32({wasm_op, ret_type})
| WasmUnaryI64({wasm_op, ret_type})
| WasmUnaryF32({wasm_op, ret_type})
Expand Down Expand Up @@ -2486,6 +2488,8 @@ let compile_primn = (wasm_mod, env: codegen_env, p, args): Expression.t => {
compile_imm(wasm_mod, env, List.nth(args, 0)),
compile_imm(wasm_mod, env, List.nth(args, 1)),
compile_imm(wasm_mod, env, List.nth(args, 2)),
grain_memory,
grain_memory,
),
Expression.Const.make(wasm_mod, const_void()),
],
Expand All @@ -2500,6 +2504,7 @@ let compile_primn = (wasm_mod, env: codegen_env, p, args): Expression.t => {
compile_imm(wasm_mod, env, List.nth(args, 0)),
compile_imm(wasm_mod, env, List.nth(args, 1)),
compile_imm(wasm_mod, env, List.nth(args, 2)),
grain_memory,
),
Expression.Const.make(wasm_mod, const_void()),
],
Expand Down Expand Up @@ -3513,7 +3518,15 @@ let compile_wasm_module = (~env=?, ~name=?, prog) => {
Option.is_none(Grain_utils.Config.memory_base^),
);
let _ =
Memory.set_memory(wasm_mod, 0, Memory.unlimited, "memory", [], false);
Memory.set_memory(
wasm_mod,
0,
Memory.unlimited,
"memory",
[],
false,
grain_memory,
);

let compile_all = () => {
ignore @@ compile_globals(wasm_mod, env, prog);
Expand Down
11 changes: 8 additions & 3 deletions compiler/src/linking/link.re
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ let rec globalize_names = (~function_names, ~global_names, ~label_names, expr) =
| RefTest
| RefCast
| BrOn
| RttCanon
| RttSub
| StructNew
| StructGet
| StructSet
Expand Down Expand Up @@ -734,9 +732,16 @@ let link_all = (linked_mod, dependencies, signature) => {
"memory",
data_segments,
false,
Comp_utils.grain_memory,
);
if (Config.import_memory^) {
Import.add_memory_import(linked_mod, "memory", "env", "memory", false);
Import.add_memory_import(
linked_mod,
Comp_utils.grain_memory,
"env",
"memory",
false,
);
};

let starts =
Expand Down

0 comments on commit 6930794

Please sign in to comment.