Skip to content

Commit

Permalink
[Parser] Parse the remaining array operations (#6158)
Browse files Browse the repository at this point in the history
Parse `array.new_elem`, `array.init_data`, and `array.init_elem`.

Accidentally also includes:

* [Parser] Parse string types and operations (#6161)
  • Loading branch information
tlively authored Dec 13, 2023
1 parent 8e91992 commit 9e63685
Show file tree
Hide file tree
Showing 8 changed files with 941 additions and 145 deletions.
115 changes: 113 additions & 2 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ struct NullTypeParserCtx {
HeapTypeT makeI31() { return Ok{}; }
HeapTypeT makeStructType() { return Ok{}; }
HeapTypeT makeArrayType() { return Ok{}; }
HeapTypeT makeStringType() { return Ok{}; }
HeapTypeT makeStringViewWTF8Type() { return Ok{}; }
HeapTypeT makeStringViewWTF16Type() { return Ok{}; }
HeapTypeT makeStringViewIterType() { return Ok{}; }

TypeT makeI32() { return Ok{}; }
TypeT makeI64() { return Ok{}; }
Expand Down Expand Up @@ -190,6 +194,10 @@ template<typename Ctx> struct TypeParserCtx {
HeapTypeT makeI31() { return HeapType::i31; }
HeapTypeT makeStructType() { return HeapType::struct_; }
HeapTypeT makeArrayType() { return HeapType::array; }
HeapTypeT makeStringType() { return HeapType::string; }
HeapTypeT makeStringViewWTF8Type() { return HeapType::stringview_wtf8; }
HeapTypeT makeStringViewWTF16Type() { return HeapType::stringview_wtf16; }
HeapTypeT makeStringViewIterType() { return HeapType::stringview_iter; }

TypeT makeI32() { return Type::i32; }
TypeT makeI64() { return Type::i64; }
Expand Down Expand Up @@ -284,8 +292,9 @@ struct NullInstrParserCtx {
using FuncIdxT = Ok;
using LocalIdxT = Ok;
using TableIdxT = Ok;
using GlobalIdxT = Ok;
using MemoryIdxT = Ok;
using GlobalIdxT = Ok;
using ElemIdxT = Ok;
using DataIdxT = Ok;
using LabelIdxT = Ok;
using TagIdxT = Ok;
Expand All @@ -310,6 +319,8 @@ struct NullInstrParserCtx {
TableIdxT getTableFromName(Name) { return Ok{}; }
MemoryIdxT getMemoryFromIdx(uint32_t) { return Ok{}; }
MemoryIdxT getMemoryFromName(Name) { return Ok{}; }
ElemIdxT getElemFromIdx(uint32_t) { return Ok{}; }
ElemIdxT getElemFromName(Name) { return Ok{}; }
DataIdxT getDataFromIdx(uint32_t) { return Ok{}; }
DataIdxT getDataFromName(Name) { return Ok{}; }
LabelIdxT getLabelFromIdx(uint32_t, bool) { return Ok{}; }
Expand Down Expand Up @@ -458,7 +469,7 @@ struct NullInstrParserCtx {
return Ok{};
}
template<typename HeapTypeT>
Result<> makeArrayNewElem(Index, HeapTypeT, DataIdxT) {
Result<> makeArrayNewElem(Index, HeapTypeT, ElemIdxT) {
return Ok{};
}
template<typename HeapTypeT>
Expand All @@ -479,7 +490,28 @@ struct NullInstrParserCtx {
template<typename HeapTypeT> Result<> makeArrayFill(Index, HeapTypeT) {
return Ok{};
}
template<typename HeapTypeT>
Result<> makeArrayInitData(Index, HeapTypeT, DataIdxT) {
return Ok{};
}
template<typename HeapTypeT>
Result<> makeArrayInitElem(Index, HeapTypeT, ElemIdxT) {
return Ok{};
}
Result<> makeRefAs(Index, RefAsOp) { return Ok{}; }
Result<> makeStringNew(Index, StringNewOp, bool, MemoryIdxT*) { return Ok{}; }
Result<> makeStringConst(Index, std::string_view) { return Ok{}; }
Result<> makeStringMeasure(Index, StringMeasureOp) { return Ok{}; }
Result<> makeStringEncode(Index, StringEncodeOp, MemoryIdxT*) { return Ok{}; }
Result<> makeStringConcat(Index) { return Ok{}; }
Result<> makeStringEq(Index, StringEqOp) { return Ok{}; }
Result<> makeStringAs(Index, StringAsOp) { return Ok{}; }
Result<> makeStringWTF8Advance(Index) { return Ok{}; }
Result<> makeStringWTF16Get(Index) { return Ok{}; }
Result<> makeStringIterNext(Index) { return Ok{}; }
Result<> makeStringIterMove(Index, StringIterMoveOp) { return Ok{}; }
Result<> makeStringSliceWTF(Index, StringSliceWTFOp) { return Ok{}; }
Result<> makeStringSliceIter(Index) { return Ok{}; }
};

// Phase 1: Parse definition spans for top-level module elements and determine
Expand Down Expand Up @@ -968,6 +1000,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
using GlobalIdxT = Name;
using TableIdxT = Name;
using MemoryIdxT = Name;
using ElemIdxT = Name;
using DataIdxT = Name;
using TagIdxT = Name;

Expand Down Expand Up @@ -1141,6 +1174,20 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return name;
}

Result<Name> getElemFromIdx(uint32_t idx) {
if (idx >= wasm.elementSegments.size()) {
return in.err("elem index out of bounds");
}
return wasm.elementSegments[idx]->name;
}

Result<Name> getElemFromName(Name name) {
if (!wasm.getElementSegmentOrNull(name)) {
return in.err("elem $" + name.toString() + " does not exist");
}
return name;
}

Result<Name> getDataFromIdx(uint32_t idx) {
if (idx >= wasm.dataSegments.size()) {
return in.err("data index out of bounds");
Expand Down Expand Up @@ -1658,9 +1705,73 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return withLoc(pos, irBuilder.makeArrayFill(type));
}

Result<> makeArrayInitData(Index pos, HeapType type, Name data) {
return withLoc(pos, irBuilder.makeArrayInitData(type, data));
}

Result<> makeArrayInitElem(Index pos, HeapType type, Name elem) {
return withLoc(pos, irBuilder.makeArrayInitElem(type, elem));
}

Result<> makeRefAs(Index pos, RefAsOp op) {
return withLoc(pos, irBuilder.makeRefAs(op));
}

Result<> makeStringNew(Index pos, StringNewOp op, bool try_, Name* mem) {
auto m = getMemory(pos, mem);
CHECK_ERR(m);
return withLoc(pos, irBuilder.makeStringNew(op, try_, *m));
}

Result<> makeStringConst(Index pos, std::string_view str) {
return withLoc(pos, irBuilder.makeStringConst(Name(str)));
}

Result<> makeStringMeasure(Index pos, StringMeasureOp op) {
return withLoc(pos, irBuilder.makeStringMeasure(op));
}

Result<> makeStringEncode(Index pos, StringEncodeOp op, Name* mem) {
auto m = getMemory(pos, mem);
CHECK_ERR(m);
return withLoc(pos, irBuilder.makeStringEncode(op, *m));
}

Result<> makeStringConcat(Index pos) {
return withLoc(pos, irBuilder.makeStringConcat());
}

Result<> makeStringEq(Index pos, StringEqOp op) {
return withLoc(pos, irBuilder.makeStringEq(op));
}

Result<> makeStringAs(Index pos, StringAsOp op) {
return withLoc(pos, irBuilder.makeStringAs(op));
}

Result<> makeStringWTF8Advance(Index pos) {
return withLoc(pos, irBuilder.makeStringWTF8Advance());
}

Result<> makeStringWTF16Get(Index pos) {
return withLoc(pos, irBuilder.makeStringWTF16Get());
}

Result<> makeStringIterNext(Index pos) {
return withLoc(pos, irBuilder.makeStringIterNext());
}

Result<> makeStringIterMove(Index pos, StringIterMoveOp op) {
return withLoc(pos, irBuilder.makeStringIterMove(op));
}

Result<> makeStringSliceWTF(Index pos, StringSliceWTFOp op) {
return withLoc(pos, irBuilder.makeStringSliceWTF(op));
}

Result<> makeStringSliceIter(Index pos) {
return withLoc(pos, irBuilder.makeStringSliceIter());
}
};

} // namespace wasm::WATParser
Expand Down
4 changes: 2 additions & 2 deletions src/parser/input-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ inline std::optional<float> ParseInput::takeF32() {
return std::nullopt;
}

inline std::optional<std::string_view> ParseInput::takeString() {
inline std::optional<std::string> ParseInput::takeString() {
if (auto t = peek()) {
if (auto s = t->getString()) {
++lexer;
return s;
return std::string(*s);
}
}
return {};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct ParseInput {
std::optional<uint8_t> takeU8();
std::optional<double> takeF64();
std::optional<float> takeF32();
std::optional<std::string_view> takeString();
std::optional<std::string> takeString();
std::optional<Name> takeName();
bool takeSExprStart(std::string_view expected);
bool peekSExprStart(std::string_view expected);
Expand Down
93 changes: 75 additions & 18 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ template<typename Ctx> MaybeResult<typename Ctx::MemoryIdxT> maybeMemidx(Ctx&);
template<typename Ctx> Result<typename Ctx::MemoryIdxT> memidx(Ctx&);
template<typename Ctx> MaybeResult<typename Ctx::MemoryIdxT> maybeMemuse(Ctx&);
template<typename Ctx> Result<typename Ctx::GlobalIdxT> globalidx(Ctx&);
template<typename Ctx> Result<typename Ctx::ElemIdxT> elemidx(Ctx&);
template<typename Ctx> Result<typename Ctx::DataIdxT> dataidx(Ctx&);
template<typename Ctx> Result<typename Ctx::LocalIdxT> localidx(Ctx&);
template<typename Ctx>
Result<typename Ctx::LabelIdxT> labelidx(Ctx&, bool inDelegate = false);
Expand Down Expand Up @@ -249,6 +251,18 @@ template<typename Ctx> Result<typename Ctx::HeapTypeT> heaptype(Ctx& ctx) {
if (ctx.in.takeKeyword("array"sv)) {
return ctx.makeArrayType();
}
if (ctx.in.takeKeyword("string"sv)) {
return ctx.makeStringType();
}
if (ctx.in.takeKeyword("stringview_wtf8"sv)) {
return ctx.makeStringViewWTF8Type();
}
if (ctx.in.takeKeyword("stringview_wtf16"sv)) {
return ctx.makeStringViewWTF16Type();
}
if (ctx.in.takeKeyword("stringview_iter"sv)) {
return ctx.makeStringViewIterType();
}
auto type = typeidx(ctx);
CHECK_ERR(type);
return *type;
Expand Down Expand Up @@ -282,7 +296,19 @@ template<typename Ctx> MaybeResult<typename Ctx::TypeT> reftype(Ctx& ctx) {
return ctx.makeRefType(ctx.makeStructType(), Nullable);
}
if (ctx.in.takeKeyword("arrayref"sv)) {
return ctx.in.err("arrayref not yet supported");
return ctx.makeRefType(ctx.makeArrayType(), Nullable);
}
if (ctx.in.takeKeyword("stringref"sv)) {
return ctx.makeRefType(ctx.makeStringType(), Nullable);
}
if (ctx.in.takeKeyword("stringview_wtf8"sv)) {
return ctx.makeRefType(ctx.makeStringViewWTF8Type(), Nullable);
}
if (ctx.in.takeKeyword("stringview_wtf16"sv)) {
return ctx.makeRefType(ctx.makeStringViewWTF16Type(), Nullable);
}
if (ctx.in.takeKeyword("stringview_iter"sv)) {
return ctx.makeRefType(ctx.makeStringViewIterType(), Nullable);
}

if (!ctx.in.takeSExprStart("ref"sv)) {
Expand Down Expand Up @@ -1584,7 +1610,11 @@ template<typename Ctx> Result<> makeArrayNewData(Ctx& ctx, Index pos) {
}

template<typename Ctx> Result<> makeArrayNewElem(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
auto type = typeidx(ctx);
CHECK_ERR(type);
auto elem = elemidx(ctx);
CHECK_ERR(elem);
return ctx.makeArrayNewElem(pos, *type, *elem);
}

template<typename Ctx> Result<> makeArrayNewFixed(Ctx& ctx, Index pos) {
Expand Down Expand Up @@ -1629,11 +1659,18 @@ template<typename Ctx> Result<> makeArrayFill(Ctx& ctx, Index pos) {
}

template<typename Ctx> Result<> makeArrayInitData(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
auto type = typeidx(ctx);
CHECK_ERR(type);
auto data = dataidx(ctx);
CHECK_ERR(data);
return ctx.makeArrayInitData(pos, *type, *data);
}

template<typename Ctx> Result<> makeArrayInitElem(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
auto type = typeidx(ctx);
CHECK_ERR(type);
auto elem = elemidx(ctx);
return ctx.makeArrayInitElem(pos, *type, *elem);
}

template<typename Ctx> Result<> makeRefAs(Ctx& ctx, Index pos, RefAsOp op) {
Expand All @@ -1642,61 +1679,69 @@ template<typename Ctx> Result<> makeRefAs(Ctx& ctx, Index pos, RefAsOp op) {

template<typename Ctx>
Result<> makeStringNew(Ctx& ctx, Index pos, StringNewOp op, bool try_) {
return ctx.in.err("unimplemented instruction");
auto mem = maybeMemidx(ctx);
CHECK_ERR(mem);
return ctx.makeStringNew(pos, op, try_, mem.getPtr());
}

template<typename Ctx> Result<> makeStringConst(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
auto str = ctx.in.takeString();
if (!str) {
return ctx.in.err("expected string");
}
return ctx.makeStringConst(pos, *str);
}

template<typename Ctx>
Result<> makeStringMeasure(Ctx& ctx, Index pos, StringMeasureOp op) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringMeasure(pos, op);
}

template<typename Ctx>
Result<> makeStringEncode(Ctx& ctx, Index pos, StringEncodeOp op) {
return ctx.in.err("unimplemented instruction");
auto mem = maybeMemidx(ctx);
CHECK_ERR(mem);
return ctx.makeStringEncode(pos, op, mem.getPtr());
}

template<typename Ctx> Result<> makeStringConcat(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringConcat(pos);
}

template<typename Ctx>
Result<> makeStringEq(Ctx& ctx, Index pos, StringEqOp op) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringEq(pos, op);
}

template<typename Ctx>
Result<> makeStringAs(Ctx& ctx, Index pos, StringAsOp op) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringAs(pos, op);
}

template<typename Ctx> Result<> makeStringWTF8Advance(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringWTF8Advance(pos);
}

template<typename Ctx> Result<> makeStringWTF16Get(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringWTF16Get(pos);
}

template<typename Ctx> Result<> makeStringIterNext(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringIterNext(pos);
}

template<typename Ctx>
Result<> makeStringIterMove(Ctx& ctx, Index pos, StringIterMoveOp op) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringIterMove(pos, op);
}

template<typename Ctx>
Result<> makeStringSliceWTF(Ctx& ctx, Index pos, StringSliceWTFOp op) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringSliceWTF(pos, op);
}

template<typename Ctx> Result<> makeStringSliceIter(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
return ctx.makeStringSliceIter(pos);
}

// =======
Expand Down Expand Up @@ -1843,8 +1888,20 @@ template<typename Ctx> Result<typename Ctx::GlobalIdxT> globalidx(Ctx& ctx) {
return ctx.in.err("expected global index or identifier");
}

// elemidx ::= x:u32 => x
// | v:id => x (if elems[x] = v)
template<typename Ctx> Result<typename Ctx::ElemIdxT> elemidx(Ctx& ctx) {
if (auto x = ctx.in.takeU32()) {
return ctx.getElemFromIdx(*x);
}
if (auto id = ctx.in.takeID()) {
return ctx.getElemFromName(*id);
}
return ctx.in.err("expected elem index or identifier");
}

// dataidx ::= x:u32 => x
// | v:id => x (if data[x] = v)
// | v:id => x (if datas[x] = v)
template<typename Ctx> Result<typename Ctx::DataIdxT> dataidx(Ctx& ctx) {
if (auto x = ctx.in.takeU32()) {
return ctx.getDataFromIdx(*x);
Expand Down
Loading

0 comments on commit 9e63685

Please sign in to comment.