Skip to content

Commit 8504571

Browse files
authored
[Parser] Parse start declarations (#6256)
1 parent 8b85d5d commit 8504571

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/parser/contexts.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
589589
std::vector<DefPos> tableDefs;
590590
std::vector<DefPos> memoryDefs;
591591
std::vector<DefPos> globalDefs;
592+
std::vector<DefPos> startDefs;
592593
std::vector<DefPos> elemDefs;
593594
std::vector<DefPos> dataDefs;
594595
std::vector<DefPos> tagDefs;
@@ -715,6 +716,14 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
715716
std::optional<ExprT>,
716717
Index pos);
717718

719+
Result<> addStart(FuncIdxT, Index pos) {
720+
if (!startDefs.empty()) {
721+
return Err{"unexpected extra 'start' function"};
722+
}
723+
startDefs.push_back({{}, pos, 0});
724+
return Ok{};
725+
}
726+
718727
Result<> addElem(Name, TableIdxT*, std::optional<ExprT>, ElemListT&&, Index);
719728

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

1337+
Result<> addStart(Name name, Index pos) {
1338+
wasm.start = name;
1339+
return Ok{};
1340+
}
1341+
13281342
Result<> addImplicitElems(Type type, std::vector<Expression*>&& elems);
13291343

13301344
Result<> addDeclareElem(Name, std::vector<Expression*>&&, Index) {

src/parser/parsers.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ template<typename Ctx> MaybeResult<> table(Ctx&);
202202
template<typename Ctx> MaybeResult<> memory(Ctx&);
203203
template<typename Ctx> MaybeResult<> global(Ctx&);
204204
template<typename Ctx> MaybeResult<> export_(Ctx&);
205+
template<typename Ctx> MaybeResult<> start(Ctx&);
205206
template<typename Ctx> MaybeResult<typename Ctx::ExprT> maybeElemexpr(Ctx&);
206207
template<typename Ctx> Result<typename Ctx::ElemListT> elemlist(Ctx&, bool);
207208
template<typename Ctx> MaybeResult<> elem(Ctx&);
@@ -2648,6 +2649,23 @@ template<typename Ctx> MaybeResult<> export_(Ctx& ctx) {
26482649
return Ok{};
26492650
}
26502651

2652+
// start ::= '(' 'start' funcidx ')'
2653+
template<typename Ctx> MaybeResult<> start(Ctx& ctx) {
2654+
auto pos = ctx.in.getPos();
2655+
if (!ctx.in.takeSExprStart("start"sv)) {
2656+
return {};
2657+
}
2658+
auto func = funcidx(ctx);
2659+
CHECK_ERR(func);
2660+
2661+
CHECK_ERR(ctx.addStart(*func, pos));
2662+
2663+
if (!ctx.in.takeRParen()) {
2664+
return ctx.in.err("expected end of start declaration");
2665+
}
2666+
return Ok{};
2667+
}
2668+
26512669
// elemexpr ::= '(' 'item' expr ')' | '(' instr ')'
26522670
template<typename Ctx>
26532671
MaybeResult<typename Ctx::ExprT> maybeElemexpr(Ctx& ctx) {
@@ -2896,6 +2914,10 @@ template<typename Ctx> MaybeResult<> modulefield(Ctx& ctx) {
28962914
CHECK_ERR(res);
28972915
return Ok{};
28982916
}
2917+
if (auto res = start(ctx)) {
2918+
CHECK_ERR(res);
2919+
return Ok{};
2920+
}
28992921
if (auto res = elem(ctx)) {
29002922
CHECK_ERR(res);
29012923
return Ok{};

src/parser/wat-parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ Result<> parseModule(Module& wasm, std::string_view input) {
171171
*typeIndices);
172172
CHECK_ERR(parseDefs(ctx, decls.tableDefs, table));
173173
CHECK_ERR(parseDefs(ctx, decls.globalDefs, global));
174+
CHECK_ERR(parseDefs(ctx, decls.startDefs, start));
174175
CHECK_ERR(parseDefs(ctx, decls.elemDefs, elem));
175176
CHECK_ERR(parseDefs(ctx, decls.dataDefs, data));
176177

test/lit/wat-kitchen-sink.wast

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,15 @@
432432
(export "exported-global" (global $g1))
433433
(export "exported-tag" (tag 0))
434434

435+
;; start function
436+
;; CHECK: (export "exported-tag" (tag $imported))
437+
438+
;; CHECK: (start $return-none)
439+
(start $return-none)
440+
435441
;; functions
436442
(func)
437443

438-
;; CHECK: (export "exported-tag" (tag $imported))
439-
440444
;; CHECK: (func $2 (type $void)
441445
;; CHECK-NEXT: (nop)
442446
;; CHECK-NEXT: )

0 commit comments

Comments
 (0)