Skip to content

Commit 752da69

Browse files
authored
feat(compiler)!: Module system (#1584)
1 parent 58c9a51 commit 752da69

File tree

721 files changed

+8610
-8508
lines changed

Some content is hidden

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

721 files changed

+8610
-8508
lines changed

cli/__test__/index.gr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* grainc-flags --no-gc --no-bulk-memory */
2+
module NearTest
23

3-
import WasmI32 from "runtime/unsafe/wasmi32"
4-
import WasmI64 from "runtime/unsafe/wasmi64"
5-
import Env from "./nearEnv"
4+
include "runtime/unsafe/wasmi32"
5+
include "runtime/unsafe/wasmi64"
6+
include "./nearEnv" as Env
67

7-
export let hello = () => {
8+
provide let hello = () => {
89
let value = "Hello, World!"
910
let length = WasmI64.load32U(WasmI32.fromGrain(value), 4n)
1011
let ptr = WasmI64.extendI32U(WasmI32.add(WasmI32.fromGrain(value), 8n))

cli/__test__/nearEnv.gr

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
/* grainc-flags --no-gc --no-bulk-memory */
2+
module NearEnv
23

3-
export foreign wasm storage_read: (
4+
provide foreign wasm storage_read: (
5+
WasmI64,
46
WasmI64,
57
WasmI64,
6-
WasmI64
78
) -> WasmI64 as storageRead from "env"
8-
export foreign wasm storage_write: (
9+
provide foreign wasm storage_write: (
10+
WasmI64,
911
WasmI64,
1012
WasmI64,
1113
WasmI64,
1214
WasmI64,
13-
WasmI64
1415
) -> WasmI64 as storageWrite from "env"
15-
export foreign wasm storage_has_key: (
16+
provide foreign wasm storage_has_key: (
17+
WasmI64,
1618
WasmI64,
17-
WasmI64
1819
) -> WasmI64 as storageHasKey from "env"
1920

20-
export foreign wasm read_register: (
21+
provide foreign wasm read_register: (
22+
WasmI64,
2123
WasmI64,
22-
WasmI64
2324
) -> Void as readRegister from "env"
2425

25-
export foreign wasm value_return: (
26+
provide foreign wasm value_return: (
27+
WasmI64,
2628
WasmI64,
27-
WasmI64
2829
) -> Void as valueReturn from "env"

cli/__test__/wasi.gr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export let fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) -> WasmI32 =
1+
module Wasi
2+
3+
provide let fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) -> WasmI32 =
24
(
35
fd,
46
iovs,

compiler/graindoc/docblock.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ let enumerate_exports = stmts => {
6262
},
6363
decls,
6464
)
65-
| TTopExport(decls) =>
65+
| TTopProvide(decls) =>
6666
List.iter(
67-
({tex_id, tex_loc}: Typedtree.export_declaration) => {
67+
({tex_id, tex_loc}: Typedtree.provide_declaration) => {
6868
id_tbl := Ident.add(tex_id, tex_loc, id_tbl^)
6969
},
7070
decls,

compiler/src/codegen/compcore.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,15 +3548,15 @@ let compile_imports = (wasm_mod, env, {imports}) => {
35483548
let compile_exports = (wasm_mod, env, {imports, exports, globals}) => {
35493549
let compile_export = (i, export) => {
35503550
switch (export) {
3551-
| GlobalExport({ex_global_internal_name, ex_global_name}) =>
3551+
| WasmGlobalExport({ex_global_internal_name, ex_global_name}) =>
35523552
let ex_global_name = "GRAIN$EXPORT$" ++ ex_global_name;
35533553
ignore @@
35543554
Export.add_global_export(
35553555
wasm_mod,
35563556
ex_global_internal_name,
35573557
ex_global_name,
35583558
);
3559-
| FunctionExport({ex_function_internal_name, ex_function_name}) =>
3559+
| WasmFunctionExport({ex_function_internal_name, ex_function_name}) =>
35603560
ignore @@
35613561
Export.add_function_export(
35623562
wasm_mod,
@@ -3573,14 +3573,14 @@ let compile_exports = (wasm_mod, env, {imports, exports, globals}) => {
35733573
/* Exports are already reversed, so keeping the first of any name is the correct behavior. */
35743574
List.filter(
35753575
fun
3576-
| GlobalExport({ex_global_name}) =>
3576+
| WasmGlobalExport({ex_global_name}) =>
35773577
if (StringSet.mem(ex_global_name, exported_globals^)) {
35783578
false;
35793579
} else {
35803580
exported_globals := StringSet.add(ex_global_name, exported_globals^);
35813581
true;
35823582
}
3583-
| FunctionExport({ex_function_name}) =>
3583+
| WasmFunctionExport({ex_function_name}) =>
35843584
if (StringSet.mem(ex_function_name, exported_functions^)) {
35853585
false;
35863586
} else {

compiler/src/codegen/mashtree.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ type import = {
476476

477477
[@deriving sexp]
478478
type export =
479-
| FunctionExport({
479+
| WasmFunctionExport({
480480
ex_function_name: string,
481481
ex_function_internal_name: string,
482482
})
483-
| GlobalExport({
483+
| WasmGlobalExport({
484484
ex_global_name: string,
485485
ex_global_internal_name: string,
486486
});

compiler/src/codegen/transl_anf.re

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,63 +1310,82 @@ let transl_signature = (~functions, ~imports, signature) => {
13101310
},
13111311
imports.specs,
13121312
);
1313-
let sign =
1314-
List.map(
1315-
fun
1316-
| TSigValue(vid, {val_repr, val_internalpath} as vd) => {
1317-
let id =
1318-
switch (val_internalpath) {
1319-
| PIdent(id) => id
1320-
| PExternal(_) =>
1321-
switch (Path_tbl.find_opt(imports.path_map, val_internalpath)) {
1322-
| Some(id) => id
1323-
| None =>
1324-
failwith(
1325-
"Impossible: path to import not found "
1326-
++ Path.name(val_internalpath),
1327-
)
1328-
}
1329-
};
1313+
let rec process_item = (~prefix="", item) => {
1314+
switch (item) {
1315+
| TSigValue(vid, {val_repr, val_internalpath} as vd) =>
1316+
let id =
1317+
switch (val_internalpath) {
1318+
| PIdent(id) => id
1319+
| PExternal(_) =>
1320+
switch (Path_tbl.find_opt(imports.path_map, val_internalpath)) {
1321+
| Some(id) => id
1322+
| None =>
1323+
failwith(
1324+
"Impossible: path to import not found "
1325+
++ Path.name(val_internalpath),
1326+
)
1327+
}
1328+
};
1329+
exports :=
1330+
[
1331+
WasmGlobalExport({
1332+
ex_global_name: prefix ++ Ident.name(vid),
1333+
ex_global_internal_name: Ident.unique_name(id),
1334+
}),
1335+
...exports^,
1336+
];
1337+
switch (val_repr) {
1338+
| ReprFunction(args, rets, _) =>
1339+
switch (Ident_tbl.find_opt(func_map, id)) {
1340+
| Some(internal_name) =>
1341+
let external_name = Ident.name(vid);
13301342
exports :=
13311343
[
1332-
GlobalExport({
1333-
ex_global_name: Ident.name(vid),
1334-
ex_global_internal_name: Ident.unique_name(id),
1344+
WasmFunctionExport({
1345+
ex_function_name: prefix ++ external_name,
1346+
ex_function_internal_name: internal_name,
13351347
}),
13361348
...exports^,
13371349
];
1338-
switch (val_repr) {
1339-
| ReprFunction(args, rets, _) =>
1340-
switch (Ident_tbl.find_opt(func_map, id)) {
1341-
| Some(internal_name) =>
1342-
let external_name = Ident.name(vid);
1343-
exports :=
1344-
[
1345-
FunctionExport({
1346-
ex_function_name: external_name,
1347-
ex_function_internal_name: internal_name,
1348-
}),
1349-
...exports^,
1350-
];
1351-
TSigValue(
1352-
vid,
1353-
{
1354-
...vd,
1355-
val_repr: ReprFunction(args, rets, Direct(external_name)),
1356-
},
1357-
);
1358-
| _ =>
1359-
TSigValue(
1360-
vid,
1361-
{...vd, val_repr: ReprFunction(args, rets, Indirect)},
1362-
)
1363-
}
1364-
| ReprValue(_) => TSigValue(vid, vd)
1365-
};
1350+
TSigValue(
1351+
vid,
1352+
{
1353+
...vd,
1354+
val_repr: ReprFunction(args, rets, Direct(external_name)),
1355+
},
1356+
);
1357+
| _ =>
1358+
TSigValue(
1359+
vid,
1360+
{...vd, val_repr: ReprFunction(args, rets, Indirect)},
1361+
)
13661362
}
1367-
| _ as item => item,
1368-
signature.Cmi_format.cmi_sign,
1369-
);
1363+
| ReprValue(_) => TSigValue(vid, vd)
1364+
};
1365+
| TSigModule(tid, decl, rs) =>
1366+
let decl =
1367+
switch (decl.md_type) {
1368+
| TModIdent(_)
1369+
| TModAlias(_) => decl
1370+
| TModSignature(_) when Option.is_some(decl.md_filepath) => decl
1371+
| TModSignature(signature) => {
1372+
...decl,
1373+
md_type:
1374+
TModSignature(
1375+
List.map(
1376+
process_item(
1377+
~prefix=Printf.sprintf("%s%s.", prefix, Ident.name(tid)),
1378+
),
1379+
signature,
1380+
),
1381+
),
1382+
}
1383+
};
1384+
TSigModule(tid, decl, rs);
1385+
| _ as item => item
1386+
};
1387+
};
1388+
let sign = List.map(process_item, signature.Cmi_format.cmi_sign);
13701389
({...signature, cmi_sign: sign}, exports^);
13711390
};
13721391

compiler/src/formatting/debug.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ let debug_expression = (expr: Parsetree.expression) => {
8989
print_loc("PExpBoxAssign", expr.pexp_loc)
9090
| PExpAssign(expression, expression1) =>
9191
print_loc("PExpAssign", expr.pexp_loc)
92+
| PExpUse(module_, items) => print_loc("PExpUse", expr.pexp_loc)
9293
| /** Used for modules without body expressions */ PExpNull =>
9394
print_loc("PExpNull", expr.pexp_loc)
9495
};

0 commit comments

Comments
 (0)