Skip to content

Commit

Permalink
[Parser] Parse array.new_fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tlively committed Nov 15, 2023
1 parent 968af45 commit c557024
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 84 deletions.
8 changes: 8 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ struct NullInstrParserCtx {
Result<> makeArrayNewElem(Index, HeapTypeT, DataIdxT) {
return Ok{};
}
template<typename HeapTypeT>
Result<> makeArrayNewFixed(Index, HeapTypeT, uint32_t) {
return Ok{};
}
template<typename HeapTypeT> Result<> makeArrayGet(Index, HeapTypeT, bool) {
return Ok{};
}
Expand Down Expand Up @@ -1334,6 +1338,10 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
return withLoc(pos, irBuilder.makeArrayNewElem(type, elem));
}

Result<> makeArrayNewFixed(Index pos, HeapType type, uint32_t arity) {
return withLoc(pos, irBuilder.makeArrayNewFixed(type, arity));
}

Result<> makeArrayGet(Index pos, HeapType type, bool signed_) {
return withLoc(pos, irBuilder.makeArrayGet(type, signed_));
}
Expand Down
8 changes: 7 additions & 1 deletion src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,13 @@ template<typename Ctx> Result<> makeArrayNewElem(Ctx& ctx, Index pos) {
}

template<typename Ctx> Result<> makeArrayNewFixed(Ctx& ctx, Index pos) {
return ctx.in.err("unimplemented instruction");
auto type = typeidx(ctx);
CHECK_ERR(type);
auto arity = ctx.in.takeU32();
if (!arity) {
return ctx.in.err(pos, "expected array.new_fixed arity");
}
return ctx.makeArrayNewFixed(pos, *type, *arity);
}

template<typename Ctx>
Expand Down
9 changes: 7 additions & 2 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,14 +992,19 @@ class Builder {
ret->finalize();
return ret;
}
ArrayNewFixed* makeArrayNewFixed(HeapType type,
const std::vector<Expression*>& values) {
template<typename T>
ArrayNewFixed* makeArrayNewFixed(HeapType type, const T& values) {
auto* ret = wasm.allocator.alloc<ArrayNewFixed>();
ret->values.set(values);
ret->type = Type(type, NonNullable);
ret->finalize();
return ret;
}
ArrayNewFixed*
makeArrayNewFixed(HeapType type,
std::initializer_list<Expression*>&& values) {
return makeArrayNewFixed(type, values);
}
ArrayGet* makeArrayGet(Expression* ref,
Expression* index,
Type type,
Expand Down
3 changes: 2 additions & 1 deletion src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
[[nodiscard]] Result<> makeArrayNewDefault(HeapType type);
[[nodiscard]] Result<> makeArrayNewData(HeapType type, Name data);
[[nodiscard]] Result<> makeArrayNewElem(HeapType type, Name elem);
// [[nodiscard]] Result<> makeArrayNewFixed();
[[nodiscard]] Result<> makeArrayNewFixed(HeapType type, uint32_t arity);
[[nodiscard]] Result<> makeArrayGet(HeapType type, bool signed_);
[[nodiscard]] Result<> makeArraySet(HeapType type);
[[nodiscard]] Result<> makeArrayLen();
Expand Down Expand Up @@ -191,6 +191,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
[[nodiscard]] Result<> visitReturn(Return*);
[[nodiscard]] Result<> visitStructNew(StructNew*);
[[nodiscard]] Result<> visitArrayNew(ArrayNew*);
[[nodiscard]] Result<> visitArrayNewFixed(ArrayNewFixed*);
[[nodiscard]] Result<> visitBreak(Break*,
std::optional<Index> label = std::nullopt);
[[nodiscard]] Result<>
Expand Down
17 changes: 16 additions & 1 deletion src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ Result<> IRBuilder::visitArrayNew(ArrayNew* curr) {
return Ok{};
}

Result<> IRBuilder::visitArrayNewFixed(ArrayNewFixed* curr) {
for (size_t i = 0, size = curr->values.size(); i < size; ++i) {
auto val = pop();
CHECK_ERR(val);
curr->values[size - i - 1] = *val;
}
return Ok{};
}

Result<Expression*> IRBuilder::getBranchValue(Name labelName,
std::optional<Index> label) {
if (!label) {
Expand Down Expand Up @@ -1000,7 +1009,13 @@ Result<> IRBuilder::makeArrayNewElem(HeapType type, Name elem) {
return Ok{};
}

// Result<> IRBuilder::makeArrayNewFixed() {}
Result<> IRBuilder::makeArrayNewFixed(HeapType type, uint32_t arity) {
ArrayNewFixed curr(wasm.allocator);
curr.values.resize(arity);
CHECK_ERR(visitArrayNewFixed(&curr));
push(builder.makeArrayNewFixed(type, curr.values));
return Ok{};
}

Result<> IRBuilder::makeArrayGet(HeapType type, bool signed_) {
ArrayGet curr;
Expand Down
Loading

0 comments on commit c557024

Please sign in to comment.