Skip to content
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
13 changes: 13 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,9 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
std::vector<DefPos> dataDefs;
std::vector<DefPos> tagDefs;

// Positions of export definitions.
std::vector<Index> exportDefs;

// Positions of typeuses that might implicitly define new types.
std::vector<Index> implicitTypeDefs;

Expand Down Expand Up @@ -684,6 +687,11 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
ImportNames* import,
TypeUseT type,
Index pos);

Result<> addExport(Index pos, Ok, Name, ExternalKind) {
exportDefs.push_back(pos);
return Ok{};
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a template over these 5 on the second param?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah I'll try that out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, there's only a single addExport method on each context now.

};

// Phase 2: Parse type definitions into a TypeBuilder.
Expand Down Expand Up @@ -1265,6 +1273,11 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
Result<>
addData(Name, Name* mem, std::optional<ExprT> offset, DataStringT, Index pos);

Result<> addExport(Index, Name value, Name name, ExternalKind kind) {
wasm.addExport(builder.makeExport(name, value, kind));
return Ok{};
}

Result<Index> addScratchLocal(Index pos, Type type) {
if (!func) {
return in.err(pos,
Expand Down
55 changes: 55 additions & 0 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ template<typename Ctx> MaybeResult<> func(Ctx&);
template<typename Ctx> MaybeResult<> table(Ctx&);
template<typename Ctx> MaybeResult<> memory(Ctx&);
template<typename Ctx> MaybeResult<> global(Ctx&);
template<typename Ctx> MaybeResult<> export_(Ctx&);
template<typename Ctx> MaybeResult<typename Ctx::ExprT> maybeElemexpr(Ctx&);
template<typename Ctx> Result<typename Ctx::ElemListT> elemlist(Ctx&, bool);
template<typename Ctx> MaybeResult<> elem(Ctx&);
Expand Down Expand Up @@ -2371,6 +2372,56 @@ template<typename Ctx> MaybeResult<> global(Ctx& ctx) {
return Ok{};
}

// export ::= '(' 'export' nm:name exportdesc ')'
// exportdesc ::= '(' 'func' x:funcidx ')'
// | '(' 'table' x:tableidx ')'
// | '(' 'memory' x:memidx ')'
// | '(' 'global' x:globalidx ')'
// | '(' 'tag' x:tagidx ')'
template<typename Ctx> MaybeResult<> export_(Ctx& ctx) {
auto pos = ctx.in.getPos();
if (!ctx.in.takeSExprStart("export"sv)) {
return {};
}

auto name = ctx.in.takeName();
if (!name) {
return ctx.in.err("expected export name");
}

if (ctx.in.takeSExprStart("func"sv)) {
auto idx = funcidx(ctx);
CHECK_ERR(idx);
CHECK_ERR(ctx.addExport(pos, *idx, *name, ExternalKind::Function));
} else if (ctx.in.takeSExprStart("table"sv)) {
auto idx = tableidx(ctx);
CHECK_ERR(idx);
CHECK_ERR(ctx.addExport(pos, *idx, *name, ExternalKind::Table));
} else if (ctx.in.takeSExprStart("memory"sv)) {
auto idx = memidx(ctx);
CHECK_ERR(idx);
CHECK_ERR(ctx.addExport(pos, *idx, *name, ExternalKind::Memory));
} else if (ctx.in.takeSExprStart("global"sv)) {
auto idx = globalidx(ctx);
CHECK_ERR(idx);
CHECK_ERR(ctx.addExport(pos, *idx, *name, ExternalKind::Global));
} else if (ctx.in.takeSExprStart("tag"sv)) {
auto idx = tagidx(ctx);
CHECK_ERR(idx);
CHECK_ERR(ctx.addExport(pos, *idx, *name, ExternalKind::Tag));
} else {
return ctx.in.err("expected export description");
}

if (!ctx.in.takeRParen()) {
return ctx.in.err("expected end of export description");
}
if (!ctx.in.takeRParen()) {
return ctx.in.err("expected end of export");
}
return Ok{};
}

// elemexpr ::= '(' 'item' expr ')' | '(' instr ')'
template<typename Ctx>
MaybeResult<typename Ctx::ExprT> maybeElemexpr(Ctx& ctx) {
Expand Down Expand Up @@ -2611,6 +2662,10 @@ template<typename Ctx> MaybeResult<> modulefield(Ctx& ctx) {
CHECK_ERR(res);
return Ok{};
}
if (auto res = export_(ctx)) {
CHECK_ERR(res);
return Ok{};
}
if (auto res = elem(ctx)) {
CHECK_ERR(res);
return Ok{};
Expand Down
10 changes: 10 additions & 0 deletions src/parser/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ Result<> parseModule(Module& wasm, std::string_view input) {
CHECK_ERR(parsed);
assert(parsed);
}

// Parse exports.
// TODO: It would be more technically correct to interleave these properly
// with the implicit inline exports in other module field definitions.
for (auto pos : decls.exportDefs) {
WithPosition with(ctx, pos);
auto parsed = export_(ctx);
CHECK_ERR(parsed);
assert(parsed);
}
}

return Ok{};
Expand Down
18 changes: 15 additions & 3 deletions test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,7 @@
;; CHECK: (tag $tag-pair (param i32 i64))
(tag $tag-pair (param i32 i64))

;; functions
(func)

;; explicit exports
;; CHECK: (export "g1" (global $g1))

;; CHECK: (export "g1.1" (global $g1))
Expand All @@ -377,6 +375,20 @@

;; CHECK: (export "t0.1" (tag $imported))

;; CHECK: (export "exported-func" (func $fimport$0))
(export "exported-func" (func 0))
;; CHECK: (export "exported-table" (table $timport$0))
(export "exported-table" (table 0))
;; CHECK: (export "exported-memory" (memory $mimport$0))
(export "exported-memory" (memory 0))
;; CHECK: (export "exported-global" (global $g1))
(export "exported-global" (global 0))
;; CHECK: (export "exported-tag" (tag $imported))
(export "exported-tag" (tag 0))

;; functions
(func)

;; CHECK: (func $1 (type $void)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
Expand Down