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] Support standalone import definitions #6191

Merged
merged 1 commit into from
Jan 2, 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
12 changes: 11 additions & 1 deletion src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,11 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return Ok{};
}

Result<>
addMemory(Name, const std::vector<Name>&, ImportNames*, TableTypeT, Index) {
return Ok{};
}

Result<> addGlobal(Name,
const std::vector<Name>&,
ImportNames*,
Expand All @@ -1259,7 +1264,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
Result<> addImplicitElems(Type type, std::vector<Expression*>&& elems);

Result<> addDeclareElem(Name, std::vector<Expression*>&&, Index) {
// TODO: Validate that referenced functions appear in a declaratve element
// TODO: Validate that referenced functions appear in a declarative element
// segment.
return Ok{};
}
Expand All @@ -1273,6 +1278,11 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
Result<>
addData(Name, Name* mem, std::optional<ExprT> offset, DataStringT, Index pos);

Result<>
addTag(Name, const std::vector<Name>, ImportNames*, TypeUseT, Index) {
return Ok{};
}

Result<> addExport(Index, Name value, Name name, ExternalKind kind) {
wasm.addExport(builder.makeExport(name, value, kind));
return Ok{};
Expand Down
70 changes: 70 additions & 0 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ template<typename Ctx> Result<> strtype(Ctx&);
template<typename Ctx> MaybeResult<typename Ctx::ModuleNameT> subtype(Ctx&);
template<typename Ctx> MaybeResult<> deftype(Ctx&);
template<typename Ctx> MaybeResult<typename Ctx::LocalsT> locals(Ctx&);
template<typename Ctx> MaybeResult<> import_(Ctx&);
template<typename Ctx> MaybeResult<> func(Ctx&);
template<typename Ctx> MaybeResult<> table(Ctx&);
template<typename Ctx> MaybeResult<> memory(Ctx&);
Expand Down Expand Up @@ -2168,6 +2169,71 @@ template<typename Ctx> MaybeResult<typename Ctx::LocalsT> locals(Ctx& ctx) {
return {};
}

// import ::= '(' 'import' mod:name nm:name importdesc ')'
// importdesc ::= '(' 'func' id? typeuse ')'
// | '(' 'table' id? tabletype ')'
// | '(' 'memory' id? memtype ')'
// | '(' 'global' id? globaltype ')'
// | '(' 'tag' id? typeuse ')'
template<typename Ctx> MaybeResult<> import_(Ctx& ctx) {
auto pos = ctx.in.getPos();

if (!ctx.in.takeSExprStart("import"sv)) {
return {};
}

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

auto nm = ctx.in.takeName();
if (!nm) {
return ctx.in.err("expected import name");
}
ImportNames names{*mod, *nm};

if (ctx.in.takeSExprStart("func"sv)) {
auto name = ctx.in.takeID();
auto type = typeuse(ctx);
CHECK_ERR(type);
CHECK_ERR(
ctx.addFunc(name ? *name : Name{}, {}, &names, *type, std::nullopt, pos));
} else if (ctx.in.takeSExprStart("table"sv)) {
auto name = ctx.in.takeID();
auto type = tabletype(ctx);
CHECK_ERR(type);
CHECK_ERR(ctx.addTable(name ? *name : Name{}, {}, &names, *type, pos));
} else if (ctx.in.takeSExprStart("memory"sv)) {
auto name = ctx.in.takeID();
auto type = memtype(ctx);
CHECK_ERR(type);
CHECK_ERR(ctx.addMemory(name ? *name : Name{}, {}, &names, *type, pos));
} else if (ctx.in.takeSExprStart("global"sv)) {
auto name = ctx.in.takeID();
auto type = globaltype(ctx);
CHECK_ERR(type);
CHECK_ERR(ctx.addGlobal(
name ? *name : Name{}, {}, &names, *type, std::nullopt, pos));
} else if (ctx.in.takeSExprStart("tag"sv)) {
auto name = ctx.in.takeID();
auto type = typeuse(ctx);
CHECK_ERR(type);
CHECK_ERR(ctx.addTag(name ? *name : Name{}, {}, &names, *type, pos));
} else {
return ctx.in.err("expected import description");
}

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

return Ok{};
}

// func ::= '(' 'func' id? ('(' 'export' name ')')*
// x,I:typeuse t*:vec(local) (in:instr)* ')'
// | '(' 'func' id? ('(' 'export' name ')')*
Expand Down Expand Up @@ -2651,6 +2717,10 @@ template<typename Ctx> MaybeResult<> modulefield(Ctx& ctx) {
CHECK_ERR(res);
return Ok{};
}
if (auto res = import_(ctx)) {
CHECK_ERR(res);
return Ok{};
}
if (auto res = func(ctx)) {
CHECK_ERR(res);
return Ok{};
Expand Down
20 changes: 14 additions & 6 deletions src/parser/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ Result<> parseDefs(Ctx& ctx,
for (auto& def : defs) {
ctx.index = def.index;
WithPosition with(ctx, def.pos);
auto parsed = parser(ctx);
CHECK_ERR(parsed);
assert(parsed);
if (auto parsed = parser(ctx)) {
CHECK_ERR(parsed);
} else {
auto im = import_(ctx);
assert(im);
CHECK_ERR(im);
}
}
return Ok{};
}
Expand Down Expand Up @@ -174,9 +178,13 @@ Result<> parseModule(Module& wasm, std::string_view input) {
ctx.index = i;
CHECK_ERR(ctx.visitFunctionStart(wasm.functions[i].get()));
WithPosition with(ctx, decls.funcDefs[i].pos);
auto parsed = func(ctx);
CHECK_ERR(parsed);
assert(parsed);
if (auto parsed = func(ctx)) {
CHECK_ERR(parsed);
} else {
auto im = import_(ctx);
assert(im);
CHECK_ERR(im);
}
}

// Parse exports.
Expand Down
Loading
Loading