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
31 changes: 19 additions & 12 deletions src/parser/context-decls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ Result<> ParseDeclsCtx::addFunc(Name name,
Exactness exact,
std::optional<LocalsT>,
std::vector<Annotation>&& annotations,
Index pos) {
Index pos,
DefKind kind) {
CHECK_ERR(checkImport(pos, import));
auto f = addFuncDecl(pos, name, import);
CHECK_ERR(f);
CHECK_ERR(addExports(in, wasm, *f, exports, ExternalKind::Function));
funcDefs.push_back(
{name, pos, Index(funcDefs.size()), std::move(annotations)});
{name, pos, Index(funcDefs.size()), std::move(annotations), kind});
return Ok{};
}

Expand Down Expand Up @@ -110,13 +111,14 @@ Result<> ParseDeclsCtx::addTable(Name name,
ImportNames* import,
TableType type,
std::optional<ExprT>,
Index pos) {
Index pos,
DefKind kind) {
CHECK_ERR(checkImport(pos, import));
auto t = addTableDecl(pos, name, import, type);
CHECK_ERR(t);
CHECK_ERR(addExports(in, wasm, *t, exports, ExternalKind::Table));
// TODO: table annotations
tableDefs.push_back({name, pos, Index(tableDefs.size()), {}});
tableDefs.push_back({name, pos, Index(tableDefs.size()), {}, kind});
return Ok{};
}

Expand Down Expand Up @@ -167,13 +169,14 @@ Result<> ParseDeclsCtx::addMemory(Name name,
const std::vector<Name>& exports,
ImportNames* import,
MemType type,
Index pos) {
Index pos,
DefKind kind) {
CHECK_ERR(checkImport(pos, import));
auto m = addMemoryDecl(pos, name, import, type);
CHECK_ERR(m);
CHECK_ERR(addExports(in, wasm, *m, exports, ExternalKind::Memory));
// TODO: memory annotations
memoryDefs.push_back({name, pos, Index(memoryDefs.size()), {}});
memoryDefs.push_back({name, pos, Index(memoryDefs.size()), {}, kind});
return Ok{};
}

Expand Down Expand Up @@ -213,13 +216,14 @@ Result<> ParseDeclsCtx::addGlobal(Name name,
ImportNames* import,
GlobalTypeT,
std::optional<ExprT>,
Index pos) {
Index pos,
DefKind kind) {
CHECK_ERR(checkImport(pos, import));
auto g = addGlobalDecl(pos, name, import);
CHECK_ERR(g);
CHECK_ERR(addExports(in, wasm, *g, exports, ExternalKind::Global));
// TODO: global annotations
globalDefs.push_back({name, pos, Index(globalDefs.size()), {}});
globalDefs.push_back({name, pos, Index(globalDefs.size()), {}, kind});
return Ok{};
}

Expand All @@ -239,7 +243,8 @@ Result<> ParseDeclsCtx::addElem(
e->name = name;
}
// TODO: element segment annotations
elemDefs.push_back({name, pos, Index(wasm.elementSegments.size()), {}});
elemDefs.push_back(
{name, pos, Index(wasm.elementSegments.size()), {}, DefKind::Definition});
wasm.addElementSegment(std::move(e));
return Ok{};
}
Expand All @@ -264,7 +269,8 @@ Result<> ParseDeclsCtx::addData(Name name,
}
d->data = std::move(data);
// TODO: data segment annotations
dataDefs.push_back({name, pos, Index(wasm.dataSegments.size()), {}});
dataDefs.push_back(
{name, pos, Index(wasm.dataSegments.size()), {}, DefKind::Definition});
wasm.addDataSegment(std::move(d));
return Ok{};
}
Expand Down Expand Up @@ -292,13 +298,14 @@ Result<> ParseDeclsCtx::addTag(Name name,
const std::vector<Name>& exports,
ImportNames* import,
TypeUseT type,
Index pos) {
Index pos,
DefKind kind) {
CHECK_ERR(checkImport(pos, import));
auto t = addTagDecl(pos, name, import);
CHECK_ERR(t);
CHECK_ERR(addExports(in, wasm, *t, exports, ExternalKind::Tag));
// TODO: tag annotations
tagDefs.push_back({name, pos, Index(tagDefs.size()), {}});
tagDefs.push_back({name, pos, Index(tagDefs.size()), {}, kind});
return Ok{};
}

Expand Down
6 changes: 4 additions & 2 deletions src/parser/context-defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Result<> ParseDefsCtx::addGlobal(Name,
ImportNames*,
GlobalTypeT,
std::optional<ExprT> exp,
Index) {
Index,
DefKind) {
if (exp) {
wasm.globals[index]->init = *exp;
}
Expand All @@ -67,7 +68,8 @@ Result<> ParseDefsCtx::addTable(Name,
ImportNames*,
TableTypeT,
std::optional<ExprT> init,
Index) {
Index,
DefKind) {
if (init) {
wasm.tables[index]->init = *init;
}
Expand Down
71 changes: 49 additions & 22 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ struct TableType {
Limits limits;
};

enum class DefKind {
ImportDesc,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: maybe just call it Import since the name is scoped to DefKind and not ambiguous? It's not clear to me what Desc is (description?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This PR associates DefKind::ImportDesc with import_(), but the next PR changes it to use importdesc() instead. Looks like the upstream spec calls the relevant grammar production externtype instead of importdesc now, so we could update the enum and function names together in the future.

Definition,
};

// The location, possible name, and index in the respective module index space
// of a module-level definition in the input.
struct DefPos {
Name name;
Index pos;
Index index;
std::vector<Annotation> annotations;
DefKind kind;
};

struct GlobalType {
Expand Down Expand Up @@ -1074,13 +1080,15 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
void setSupertype(HeapTypeT) {}
void finishTypeDef(Name name, Index pos) {
// TODO: type annotations
typeDefs.push_back({name, pos, Index(typeDefs.size()), {}});
typeDefs.push_back(
{name, pos, Index(typeDefs.size()), {}, DefKind::Definition});
}
size_t getRecGroupStartIndex() { return 0; }
void addRecGroup(Index, size_t) {}
void finishRectype(Index pos) {
// TODO: type annotations
recTypeDefs.push_back({{}, pos, Index(recTypeDefs.size()), {}});
recTypeDefs.push_back(
{{}, pos, Index(recTypeDefs.size()), {}, DefKind::Definition});
}

bool skipFunctionBody();
Expand Down Expand Up @@ -1135,7 +1143,8 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
Exactness exact,
std::optional<LocalsT>,
std::vector<Annotation>&&,
Index pos);
Index pos,
DefKind kind);

Result<Table*> addTableDecl(Index pos,
Name name,
Expand All @@ -1146,7 +1155,8 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
ImportNames*,
TableType,
std::optional<ExprT>,
Index);
Index,
DefKind kind);

// TODO: Record index of implicit elem for use when parsing types and instrs.
Result<> addImplicitElems(TypeT, ElemListT&& elems);
Expand All @@ -1158,7 +1168,8 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
const std::vector<Name>& exports,
ImportNames* import,
MemType type,
Index pos);
Index pos,
DefKind kind);

Result<> addImplicitData(DataStringT&& data);

Expand All @@ -1169,14 +1180,15 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
ImportNames* import,
GlobalTypeT,
std::optional<ExprT>,
Index pos);
Index pos,
DefKind kind);

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

Expand All @@ -1196,7 +1208,8 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
const std::vector<Name>& exports,
ImportNames* import,
TypeUseT type,
Index pos);
Index pos,
DefKind kind);

Result<> addExport(Index pos, Ok, Name, ExternalKind) {
exportDefs.push_back(pos);
Expand Down Expand Up @@ -1526,7 +1539,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
Exactness exact,
std::optional<LocalsT> locals,
std::vector<Annotation>&& annotations,
Index pos) {
Index pos,
DefKind kind) {
auto& f = wasm.functions[index];
if (!type.type.isSignature()) {
return in.err(pos, "expected signature type");
Expand Down Expand Up @@ -1556,7 +1570,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
ImportNames*,
Type ttype,
std::optional<ExprT> init,
Index pos) {
Index pos,
DefKind kind) {
auto& t = wasm.tables[index];
if (!ttype.isRef()) {
return in.err(pos, "expected reference type");
Expand All @@ -1572,8 +1587,12 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
return Ok{};
}

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

Expand All @@ -1584,7 +1603,8 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
ImportNames*,
GlobalType type,
std::optional<ExprT>,
Index) {
Index,
DefKind kind) {
auto& g = wasm.globals[index];
g->mutable_ = type.mutability;
g->type = type.type;
Expand All @@ -1600,8 +1620,12 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,

Result<> addDeclareElem(Name, ElemListT&&, Index) { return Ok{}; }

Result<>
addTag(Name, const std::vector<Name>&, ImportNames*, TypeUse use, Index pos) {
Result<> addTag(Name,
const std::vector<Name>&,
ImportNames*,
TypeUse use,
Index pos,
DefKind kind) {
auto& t = wasm.tags[index];
if (!use.type.isSignature()) {
return in.err(pos, "tag type must be a signature");
Expand Down Expand Up @@ -1916,7 +1940,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
Exactness,
std::optional<LocalsT>,
std::vector<Annotation>&&,
Index) {
Index,
DefKind) {
return Ok{};
}

Expand All @@ -1925,10 +1950,11 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
ImportNames*,
TableTypeT,
std::optional<ExprT>,
Index);
Index,
DefKind);

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

Expand All @@ -1937,7 +1963,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
ImportNames*,
GlobalTypeT,
std::optional<ExprT> exp,
Index);
Index,
DefKind);

Result<> addStart(Name name, Index pos) {
wasm.start = name;
Expand All @@ -1961,8 +1988,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
Result<>
addData(Name, Name* mem, std::optional<ExprT> offset, DataStringT, Index pos);

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

Expand Down
8 changes: 5 additions & 3 deletions src/parser/parse-5-defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ Result<> parseDefinitions(
if (!f->imported()) {
CHECK_ERR(ctx.visitFunctionStart(f));
}
if (auto parsed = func(ctx)) {
CHECK_ERR(parsed);
} else {
if (decls.funcDefs[i].kind == DefKind::ImportDesc) {
auto im = import_(ctx);
assert(im);
CHECK_ERR(im);
} else {
auto parsed = func(ctx);
assert(parsed);
CHECK_ERR(parsed);
}
if (!f->imported()) {
auto end = ctx.irBuilder.visitEnd();
Expand Down
Loading
Loading