Skip to content

Commit

Permalink
[Parser] Parse the remaining array operations
Browse files Browse the repository at this point in the history
Parse `array.new_elem`, `array.init_data`, and `array.init_elem`.
  • Loading branch information
tlively committed Dec 8, 2023
1 parent f73773b commit 18aef96
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 100 deletions.
38 changes: 36 additions & 2 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,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 +311,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 +461,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,6 +482,14 @@ 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{}; }
};

Expand Down Expand Up @@ -968,6 +979,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 +1153,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,6 +1684,14 @@ 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));
}
Expand Down
33 changes: 29 additions & 4 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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 @@ -1579,7 +1581,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 @@ -1624,11 +1630,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 Down Expand Up @@ -1838,8 +1851,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
4 changes: 2 additions & 2 deletions src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
[[nodiscard]] Result<> makeArrayLen();
[[nodiscard]] Result<> makeArrayCopy(HeapType destType, HeapType srcType);
[[nodiscard]] Result<> makeArrayFill(HeapType type);
// [[nodiscard]] Result<> makeArrayInitData();
// [[nodiscard]] Result<> makeArrayInitElem();
[[nodiscard]] Result<> makeArrayInitData(HeapType type, Name data);
[[nodiscard]] Result<> makeArrayInitElem(HeapType type, Name elem);
[[nodiscard]] Result<> makeRefAs(RefAsOp op);
// [[nodiscard]] Result<> makeStringNew();
// [[nodiscard]] Result<> makeStringConst();
Expand Down
2 changes: 2 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,7 @@ class ArrayFill : public SpecificExpression<Expression::ArrayFillId> {

class ArrayInitData : public SpecificExpression<Expression::ArrayInitDataId> {
public:
ArrayInitData() = default;
ArrayInitData(MixedArena& allocator) {}

Name segment;
Expand All @@ -1749,6 +1750,7 @@ class ArrayInitData : public SpecificExpression<Expression::ArrayInitDataId> {

class ArrayInitElem : public SpecificExpression<Expression::ArrayInitElemId> {
public:
ArrayInitElem() = default;
ArrayInitElem(MixedArena& allocator) {}

Name segment;
Expand Down
18 changes: 16 additions & 2 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,23 @@ Result<> IRBuilder::makeArrayFill(HeapType type) {
return Ok{};
}

// Result<> IRBuilder::makeArrayInitData() {}
Result<> IRBuilder::makeArrayInitData(HeapType type, Name data) {
ArrayInitData curr;
CHECK_ERR(visitArrayInitData(&curr));
CHECK_ERR(validateTypeAnnotation(type, curr.ref));
push(builder.makeArrayInitData(
data, curr.ref, curr.index, curr.offset, curr.size));
return Ok{};
}

// Result<> IRBuilder::makeArrayInitElem() {}
Result<> IRBuilder::makeArrayInitElem(HeapType type, Name elem) {
ArrayInitElem curr;
CHECK_ERR(visitArrayInitElem(&curr));
CHECK_ERR(validateTypeAnnotation(type, curr.ref));
push(builder.makeArrayInitElem(
elem, curr.ref, curr.index, curr.offset, curr.size));
return Ok{};
}

Result<> IRBuilder::makeRefAs(RefAsOp op) {
RefAs curr;
Expand Down
Loading

0 comments on commit 18aef96

Please sign in to comment.