Skip to content

Commit

Permalink
[Parser] Parse start declarations (WebAssembly#6256)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlively committed Jan 30, 2024
1 parent 8b85d5d commit 8504571
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
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

0 comments on commit 8504571

Please sign in to comment.