Skip to content

Commit 4637667

Browse files
authored
feat(compiler): Refactor exports (#1244)
1 parent 8827a96 commit 4637667

File tree

354 files changed

+9983
-9977
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

354 files changed

+9983
-9977
lines changed

compiler/src/codegen/compcore.re

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,28 @@ let swap_slots =
4747
swap_slots_f64,
4848
]);
4949

50-
/* These are the bare-minimum imports needed for basic runtime support */
50+
/** These are the bare-minimum imports needed for basic runtime support */
51+
52+
/* The Grain environment */
53+
let grain_env_mod = grain_env_name;
5154
let module_runtime_id = Ident.create_persistent("moduleRuntimeId");
5255
let reloc_base = Ident.create_persistent("relocBase");
5356
let table_size = Ident.create_persistent("GRAIN$TABLE_SIZE");
54-
let grain_env_mod = Ident.create_persistent(grain_env_name);
55-
let malloc_mod = Ident.create_persistent("GRAIN$MODULE$runtime/malloc");
56-
let gc_mod = Ident.create_persistent("GRAIN$MODULE$runtime/gc");
57-
let exception_mod = Ident.create_persistent("GRAIN$MODULE$runtime/exception");
58-
let console_mod = Ident.create_persistent("console");
57+
58+
/* Memory allocation */
59+
let malloc_mod = "GRAIN$MODULE$runtime/malloc";
60+
let malloc_ident = Ident.create_persistent("malloc");
61+
let malloc_closure_ident = Ident.create_persistent("GRAIN$EXPORT$malloc");
62+
63+
/* Garbage collection */
64+
let gc_mod = "GRAIN$MODULE$runtime/gc";
65+
let incref_ident = Ident.create_persistent("incRef");
66+
let incref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$incRef");
67+
let decref_ident = Ident.create_persistent("decRef");
68+
let decref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$decRef");
69+
70+
/* Exceptions */
71+
let exception_mod = "GRAIN$MODULE$runtime/exception";
5972
let print_exception_ident = Ident.create_persistent("printException");
6073
let print_exception_closure_ident =
6174
Ident.create_persistent("GRAIN$EXPORT$printException");
@@ -66,64 +79,69 @@ let index_out_of_bounds_ident =
6679
Ident.create_persistent("GRAIN$EXPORT$IndexOutOfBounds");
6780
let match_failure_ident =
6881
Ident.create_persistent("GRAIN$EXPORT$MatchFailure");
69-
let malloc_ident = Ident.create_persistent("malloc");
70-
let malloc_closure_ident = Ident.create_persistent("GRAIN$EXPORT$malloc");
71-
let incref_ident = Ident.create_persistent("incRef");
72-
let incref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$incRef");
73-
let equal_mod = Ident.create_persistent("GRAIN$MODULE$runtime/equal");
82+
83+
/* Equality checking */
84+
let equal_mod = "GRAIN$MODULE$runtime/equal";
7485
let equal_ident = Ident.create_persistent("equal");
7586
let equal_closure_ident = Ident.create_persistent("GRAIN$EXPORT$equal");
76-
let decref_ident = Ident.create_persistent("decRef");
77-
let decref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$decRef");
87+
88+
/* JS-runner support */
89+
let console_mod = "console";
7890
let tracepoint_ident = Ident.create_persistent("tracepoint");
7991

8092
let grain_main = "_gmain";
8193
let grain_start = "_start";
8294

8395
let required_global_imports = [
8496
{
97+
mimp_id: reloc_base,
8598
mimp_mod: grain_env_mod,
86-
mimp_name: reloc_base,
99+
mimp_name: Ident.name(reloc_base),
87100
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), false),
88101
mimp_kind: MImportWasm,
89102
mimp_setup: MSetupNone,
90103
mimp_used: true,
91104
},
92105
{
106+
mimp_id: module_runtime_id,
93107
mimp_mod: grain_env_mod,
94-
mimp_name: module_runtime_id,
108+
mimp_name: Ident.name(module_runtime_id),
95109
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), false),
96110
mimp_kind: MImportWasm,
97111
mimp_setup: MSetupNone,
98112
mimp_used: false,
99113
},
100114
{
115+
mimp_id: print_exception_closure_ident,
101116
mimp_mod: exception_mod,
102-
mimp_name: print_exception_closure_ident,
117+
mimp_name: Ident.name(print_exception_closure_ident),
103118
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
104119
mimp_kind: MImportWasm,
105120
mimp_setup: MSetupNone,
106121
mimp_used: false,
107122
},
108123
{
124+
mimp_id: assertion_error_closure_ident,
109125
mimp_mod: exception_mod,
110-
mimp_name: assertion_error_closure_ident,
126+
mimp_name: Ident.name(assertion_error_closure_ident),
111127
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
112128
mimp_kind: MImportWasm,
113129
mimp_setup: MSetupNone,
114130
mimp_used: false,
115131
},
116132
{
133+
mimp_id: index_out_of_bounds_ident,
117134
mimp_mod: exception_mod,
118-
mimp_name: index_out_of_bounds_ident,
135+
mimp_name: Ident.name(index_out_of_bounds_ident),
119136
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
120137
mimp_kind: MImportWasm,
121138
mimp_setup: MSetupNone,
122139
mimp_used: false,
123140
},
124141
{
142+
mimp_id: match_failure_ident,
125143
mimp_mod: exception_mod,
126-
mimp_name: match_failure_ident,
144+
mimp_name: Ident.name(match_failure_ident),
127145
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
128146
mimp_kind: MImportWasm,
129147
mimp_setup: MSetupNone,
@@ -133,32 +151,36 @@ let required_global_imports = [
133151

134152
let grain_runtime_imports = [
135153
{
154+
mimp_id: malloc_closure_ident,
136155
mimp_mod: gc_mod,
137-
mimp_name: malloc_closure_ident,
156+
mimp_name: Ident.name(malloc_closure_ident),
138157
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
139158
mimp_kind: MImportWasm,
140159
mimp_setup: MSetupNone,
141160
mimp_used: false,
142161
},
143162
{
163+
mimp_id: incref_closure_ident,
144164
mimp_mod: gc_mod,
145-
mimp_name: incref_closure_ident,
165+
mimp_name: Ident.name(incref_closure_ident),
146166
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
147167
mimp_kind: MImportWasm,
148168
mimp_setup: MSetupNone,
149169
mimp_used: false,
150170
},
151171
{
172+
mimp_id: decref_closure_ident,
152173
mimp_mod: gc_mod,
153-
mimp_name: decref_closure_ident,
174+
mimp_name: Ident.name(decref_closure_ident),
154175
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
155176
mimp_kind: MImportWasm,
156177
mimp_setup: MSetupNone,
157178
mimp_used: false,
158179
},
159180
{
181+
mimp_id: equal_closure_ident,
160182
mimp_mod: equal_mod,
161-
mimp_name: equal_closure_ident,
183+
mimp_name: Ident.name(equal_closure_ident),
162184
mimp_type: MGlobalImport(Types.StackAllocated(WasmI32), true),
163185
mimp_kind: MImportWasm,
164186
mimp_setup: MSetupNone,
@@ -171,8 +193,9 @@ let runtime_global_imports =
171193

172194
let required_function_imports = [
173195
{
196+
mimp_id: print_exception_ident,
174197
mimp_mod: exception_mod,
175-
mimp_name: print_exception_ident,
198+
mimp_name: Ident.name(print_exception_ident),
176199
mimp_type:
177200
MFuncImport(
178201
[Types.StackAllocated(WasmI32), Types.StackAllocated(WasmI32)],
@@ -183,8 +206,9 @@ let required_function_imports = [
183206
mimp_used: false,
184207
},
185208
{
209+
mimp_id: assertion_error_ident,
186210
mimp_mod: exception_mod,
187-
mimp_name: assertion_error_ident,
211+
mimp_name: Ident.name(assertion_error_ident),
188212
mimp_type:
189213
MFuncImport(
190214
[Types.StackAllocated(WasmI32), Types.StackAllocated(WasmI32)],
@@ -198,8 +222,9 @@ let required_function_imports = [
198222

199223
let grain_function_imports = [
200224
{
225+
mimp_id: malloc_ident,
201226
mimp_mod: gc_mod,
202-
mimp_name: malloc_ident,
227+
mimp_name: Ident.name(malloc_ident),
203228
mimp_type:
204229
MFuncImport(
205230
[Types.StackAllocated(WasmI32), Types.StackAllocated(WasmI32)],
@@ -210,8 +235,9 @@ let grain_function_imports = [
210235
mimp_used: false,
211236
},
212237
{
238+
mimp_id: incref_ident,
213239
mimp_mod: gc_mod,
214-
mimp_name: incref_ident,
240+
mimp_name: Ident.name(incref_ident),
215241
mimp_type:
216242
MFuncImport(
217243
[Types.StackAllocated(WasmI32), Types.StackAllocated(WasmI32)],
@@ -222,8 +248,9 @@ let grain_function_imports = [
222248
mimp_used: false,
223249
},
224250
{
251+
mimp_id: decref_ident,
225252
mimp_mod: gc_mod,
226-
mimp_name: decref_ident,
253+
mimp_name: Ident.name(decref_ident),
227254
mimp_type:
228255
MFuncImport(
229256
[Types.StackAllocated(WasmI32), Types.StackAllocated(WasmI32)],
@@ -234,16 +261,18 @@ let grain_function_imports = [
234261
mimp_used: false,
235262
},
236263
{
264+
mimp_id: tracepoint_ident,
237265
mimp_mod: console_mod,
238-
mimp_name: tracepoint_ident,
266+
mimp_name: Ident.name(tracepoint_ident),
239267
mimp_type: MFuncImport([Types.StackAllocated(WasmI32)], []),
240268
mimp_kind: MImportWasm,
241269
mimp_setup: MSetupNone,
242270
mimp_used: false,
243271
},
244272
{
273+
mimp_id: equal_ident,
245274
mimp_mod: equal_mod,
246-
mimp_name: equal_ident,
275+
mimp_name: Ident.name(equal_ident),
247276
mimp_type:
248277
MFuncImport(
249278
[Types.HeapAllocated, Types.HeapAllocated, Types.HeapAllocated],
@@ -264,7 +293,7 @@ let runtime_imports =
264293
let runtime_imports_tbl = {
265294
let tbl = Ident_tbl.create(64);
266295
List.iter(
267-
({mimp_name} as imp) => Ident_tbl.add(tbl, mimp_name, imp),
296+
({mimp_id} as imp) => Ident_tbl.add(tbl, mimp_id, imp),
268297
runtime_imports,
269298
);
270299
tbl;
@@ -315,11 +344,10 @@ let get_wasm_imported_name = (~runtime_import=true, mod_, name) => {
315344
Ident_tbl.find(runtime_imports_tbl, name).mimp_used =
316345
true;
317346
};
318-
Printf.sprintf("wimport_%s_%s", Ident.name(mod_), Ident.name(name));
347+
Ident.unique_name(name);
319348
};
320349

321-
let get_grain_imported_name = (mod_, name) =>
322-
Printf.sprintf("gimport_%s_%s", Ident.name(mod_), Ident.name(name));
350+
let get_grain_imported_name = (mod_, name) => Ident.unique_name(name);
323351

324352
let call_exception_printer = (wasm_mod, env, args) => {
325353
let args = [
@@ -3480,23 +3508,23 @@ let compute_table_size = (env, {function_table_elements}) => {
34803508
let compile_imports = (wasm_mod, env, {imports}) => {
34813509
let compile_module_name = name =>
34823510
fun
3483-
| MImportWasm => Ident.name(name)
3484-
| MImportGrain => "GRAIN$MODULE$" ++ Ident.name(name);
3511+
| MImportWasm => name
3512+
| MImportGrain => "GRAIN$MODULE$" ++ name;
34853513

34863514
let compile_import_name = (name, kind, ty) =>
34873515
switch (kind, ty) {
3488-
| (MImportGrain, MGlobalImport(_)) => "GRAIN$EXPORT$" ++ Ident.name(name)
3489-
| _ => Ident.name(name)
3516+
| (MImportGrain, MGlobalImport(_)) => "GRAIN$EXPORT$" ++ name
3517+
| _ => name
34903518
};
34913519

3492-
let compile_import = ({mimp_mod, mimp_name, mimp_type, mimp_kind}) => {
3520+
let compile_import = ({mimp_id, mimp_mod, mimp_name, mimp_type, mimp_kind}) => {
34933521
let module_name = compile_module_name(mimp_mod, mimp_kind);
34943522
let item_name = compile_import_name(mimp_name, mimp_kind, mimp_type);
34953523
let internal_name =
34963524
switch (mimp_kind) {
3497-
| MImportGrain => get_grain_imported_name(mimp_mod, mimp_name)
3525+
| MImportGrain => get_grain_imported_name(mimp_mod, mimp_id)
34983526
| MImportWasm =>
3499-
get_wasm_imported_name(~runtime_import=false, mimp_mod, mimp_name)
3527+
get_wasm_imported_name(~runtime_import=false, mimp_mod, mimp_id)
35003528
};
35013529
switch (mimp_kind, mimp_type) {
35023530
| (MImportGrain, MGlobalImport(ty, mut)) =>
@@ -3539,29 +3567,26 @@ let compile_imports = (wasm_mod, env, {imports}) => {
35393567
);
35403568

35413569
List.iter(compile_import, imports);
3542-
Import.add_memory_import(
3543-
wasm_mod,
3544-
"mem",
3545-
Ident.name(grain_env_mod),
3546-
"mem",
3547-
false,
3548-
);
3570+
Import.add_memory_import(wasm_mod, "mem", grain_env_mod, "mem", false);
35493571
Import.add_table_import(
35503572
wasm_mod,
35513573
global_function_table,
3552-
Ident.name(grain_env_mod),
3574+
grain_env_mod,
35533575
global_function_table,
35543576
);
35553577
};
35563578

35573579
let compile_exports = (wasm_mod, env, {imports, exports, globals}) => {
35583580
let compile_export = (i, export) => {
35593581
switch (export) {
3560-
| GlobalExport({ex_global_name}) =>
3561-
let internal_name = Ident.unique_name(ex_global_name);
3562-
let exported_name = "GRAIN$EXPORT$" ++ Ident.name(ex_global_name);
3582+
| GlobalExport({ex_global_internal_name, ex_global_name}) =>
3583+
let ex_global_name = "GRAIN$EXPORT$" ++ ex_global_name;
35633584
ignore @@
3564-
Export.add_global_export(wasm_mod, internal_name, exported_name);
3585+
Export.add_global_export(
3586+
wasm_mod,
3587+
ex_global_internal_name,
3588+
ex_global_name,
3589+
);
35653590
| FunctionExport({ex_function_internal_name, ex_function_name}) =>
35663591
ignore @@
35673592
Export.add_function_export(
@@ -3580,11 +3605,10 @@ let compile_exports = (wasm_mod, env, {imports, exports, globals}) => {
35803605
List.filter(
35813606
fun
35823607
| GlobalExport({ex_global_name}) =>
3583-
if (StringSet.mem(Ident.name(ex_global_name), exported_globals^)) {
3608+
if (StringSet.mem(ex_global_name, exported_globals^)) {
35843609
false;
35853610
} else {
3586-
exported_globals :=
3587-
StringSet.add(Ident.name(ex_global_name), exported_globals^);
3611+
exported_globals := StringSet.add(ex_global_name, exported_globals^);
35883612
true;
35893613
}
35903614
| FunctionExport({ex_function_name}) =>

compiler/src/codegen/mashtree.re

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,9 @@ type import_setup =
462462

463463
[@deriving sexp]
464464
type import = {
465-
mimp_mod: Ident.t,
466-
mimp_name: Ident.t,
465+
mimp_id: Ident.t,
466+
mimp_mod: string,
467+
mimp_name: string,
467468
mimp_type: import_type,
468469
mimp_kind: import_kind,
469470
mimp_setup: import_setup,
@@ -476,7 +477,10 @@ type export =
476477
ex_function_name: string,
477478
ex_function_internal_name: string,
478479
})
479-
| GlobalExport({ex_global_name: Ident.t});
480+
| GlobalExport({
481+
ex_global_name: string,
482+
ex_global_internal_name: string,
483+
});
480484

481485
[@deriving sexp]
482486
type mash_function = {

0 commit comments

Comments
 (0)