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

[Parser] Parse start declarations #6256

Merged
merged 1 commit into from
Jan 30, 2024
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
14 changes: 14 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
std::vector<DefPos> tableDefs;
std::vector<DefPos> memoryDefs;
std::vector<DefPos> globalDefs;
std::vector<DefPos> startDefs;
std::vector<DefPos> elemDefs;
std::vector<DefPos> dataDefs;
std::vector<DefPos> tagDefs;
Expand Down Expand Up @@ -715,6 +716,14 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
std::optional<ExprT>,
Index pos);

Result<> addStart(FuncIdxT, Index pos) {
if (!startDefs.empty()) {
return Err{"unexpected extra 'start' function"};
}
startDefs.push_back({{}, pos, 0});
return Ok{};
}

Result<> addElem(Name, TableIdxT*, std::optional<ExprT>, ElemListT&&, Index);

Result<> addDeclareElem(Name, ElemListT&&, Index) { return Ok{}; }
Expand Down Expand Up @@ -1325,6 +1334,11 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
std::optional<ExprT> exp,
Index);

Result<> addStart(Name name, Index pos) {
wasm.start = name;
return Ok{};
}

Result<> addImplicitElems(Type type, std::vector<Expression*>&& elems);

Result<> addDeclareElem(Name, std::vector<Expression*>&&, Index) {
Expand Down
22 changes: 22 additions & 0 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ 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<> start(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 @@ -2648,6 +2649,23 @@ template<typename Ctx> MaybeResult<> export_(Ctx& ctx) {
return Ok{};
}

// start ::= '(' 'start' funcidx ')'
template<typename Ctx> MaybeResult<> start(Ctx& ctx) {
auto pos = ctx.in.getPos();
if (!ctx.in.takeSExprStart("start"sv)) {
return {};
}
auto func = funcidx(ctx);
CHECK_ERR(func);

CHECK_ERR(ctx.addStart(*func, pos));

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

// elemexpr ::= '(' 'item' expr ')' | '(' instr ')'
template<typename Ctx>
MaybeResult<typename Ctx::ExprT> maybeElemexpr(Ctx& ctx) {
Expand Down Expand Up @@ -2896,6 +2914,10 @@ template<typename Ctx> MaybeResult<> modulefield(Ctx& ctx) {
CHECK_ERR(res);
return Ok{};
}
if (auto res = start(ctx)) {
CHECK_ERR(res);
return Ok{};
}
if (auto res = elem(ctx)) {
CHECK_ERR(res);
return Ok{};
Expand Down
1 change: 1 addition & 0 deletions src/parser/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ Result<> parseModule(Module& wasm, std::string_view input) {
*typeIndices);
CHECK_ERR(parseDefs(ctx, decls.tableDefs, table));
CHECK_ERR(parseDefs(ctx, decls.globalDefs, global));
CHECK_ERR(parseDefs(ctx, decls.startDefs, start));
CHECK_ERR(parseDefs(ctx, decls.elemDefs, elem));
CHECK_ERR(parseDefs(ctx, decls.dataDefs, data));

Expand Down
8 changes: 6 additions & 2 deletions test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,15 @@
(export "exported-global" (global $g1))
(export "exported-tag" (tag 0))

;; start function
;; CHECK: (export "exported-tag" (tag $imported))

;; CHECK: (start $return-none)
(start $return-none)

;; functions
(func)

;; CHECK: (export "exported-tag" (tag $imported))

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