Skip to content
Draft
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
1 change: 1 addition & 0 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
("i64.shr_u", "makeBinary(BinaryOp::ShrUInt64)"),
("i64.rotl", "makeBinary(BinaryOp::RotLInt64)"),
("i64.rotr", "makeBinary(BinaryOp::RotRInt64)"),
("i64.add128", "makeWideIntBinary(WideIntBinaryOp::AddInt128)"),
("f32.abs", "makeUnary(UnaryOp::AbsFloat32)"),
("f32.neg", "makeUnary(UnaryOp::NegFloat32)"),
("f32.ceil", "makeUnary(UnaryOp::CeilFloat32)"),
Expand Down
21 changes: 16 additions & 5 deletions src/gen-s-parser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3405,12 +3405,23 @@ switch (buf[0]) {
switch (buf[4]) {
case 'a': {
switch (buf[5]) {
case 'd':
if (op == "i64.add"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::AddInt64));
return Ok{};
case 'd': {
switch (buf[7]) {
case '\0':
if (op == "i64.add"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::AddInt64));
return Ok{};
}
goto parse_error;
case '1':
if (op == "i64.add128"sv) {
CHECK_ERR(makeWideIntBinary(ctx, pos, annotations, WideIntBinaryOp::AddInt128));
return Ok{};
}
goto parse_error;
default: goto parse_error;
}
goto parse_error;
}
case 'n':
if (op == "i64.and"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::AndInt64));
Expand Down
16 changes: 16 additions & 0 deletions src/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
}
}
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitWideIntBinary(WideIntBinary* curr) {
if (curr->op == AddInt128) {
uint64_t highRHS = pop().geti64();
uint64_t lowRHS = pop().geti64();
uint64_t highLHS = pop().geti64();
uint64_t lowLHS = pop().geti64();

uint64_t lowRes = lowLHS + lowRHS;
uint64_t highRes = highLHS + highRHS + (lowRes < lowLHS);

push(Literal(lowRes));
push(Literal(highRes));
return {};
}
WASM_UNREACHABLE("TODO");
}
Flow visitDrop(Drop* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitReturn(Return* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitMemorySize(MemorySize* curr) { WASM_UNREACHABLE("TODO"); }
Expand Down
1 change: 1 addition & 0 deletions src/ir/ReFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void ReFinalize::visitMemoryFill(MemoryFill* curr) { curr->finalize(); }
void ReFinalize::visitConst(Const* curr) { curr->finalize(); }
void ReFinalize::visitUnary(Unary* curr) { curr->finalize(); }
void ReFinalize::visitBinary(Binary* curr) { curr->finalize(); }
void ReFinalize::visitWideIntBinary(WideIntBinary* curr) { curr->finalize(); }
void ReFinalize::visitSelect(Select* curr) { curr->finalize(); }
void ReFinalize::visitDrop(Drop* curr) { curr->finalize(); }
void ReFinalize::visitReturn(Return* curr) { curr->finalize(); }
Expand Down
8 changes: 8 additions & 0 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,14 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}
}

void visitWideIntBinary(WideIntBinary* curr) {
size_t num = 4;
curr->operands.resize(num);
for (size_t i = 0; i < num; ++i) {
note(&curr->operands[i], Type::i64);
}
}

void visitSelect(Select* curr, std::optional<Type> type = std::nullopt) {
if (type) {
note(&curr->ifTrue, *type);
Expand Down
7 changes: 7 additions & 0 deletions src/ir/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,13 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
}
return ret + visit(curr->left) + visit(curr->right);
}
CostType visitWideIntBinary(WideIntBinary* curr) {
CostType ret = 1;
for (auto* child : curr->operands) {
ret += visit(child);
}
return ret;
}
CostType visitSelect(Select* curr) {
return 1 + visit(curr->condition) + visit(curr->ifTrue) +
visit(curr->ifFalse);
Expand Down
1 change: 1 addition & 0 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ class EffectAnalyzer {
}
}
}
void visitWideIntBinary(WideIntBinary* curr) {}
void visitSelect(Select* curr) {}
void visitDrop(Drop* curr) {}
void visitReturn(Return* curr) { parent.branchesOut = true; }
Expand Down
1 change: 1 addition & 0 deletions src/ir/possible-contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ struct InfoCollector
addRoot(curr);
}
void visitBinary(Binary* curr) { addRoot(curr); }
void visitWideIntBinary(WideIntBinary* curr) { addRoot(curr); }
void visitSelect(Select* curr) {
receiveChildValue(curr->ifTrue, curr);
receiveChildValue(curr->ifFalse, curr);
Expand Down
1 change: 1 addition & 0 deletions src/ir/subtype-exprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
void visitConst(Const* curr) {}
void visitUnary(Unary* curr) {}
void visitBinary(Binary* curr) {}
void visitWideIntBinary(WideIntBinary* curr) {}
void visitSelect(Select* curr) {
self()->noteSubtype(curr->ifTrue, curr);
self()->noteSubtype(curr->ifFalse, curr);
Expand Down
10 changes: 10 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ struct NullInstrParserCtx {
Result<> makeBinary(Index, const std::vector<Annotation>&, BinaryOp) {
return Ok{};
}
Result<>
makeWideIntBinary(Index, const std::vector<Annotation>&, WideIntBinaryOp) {
return Ok{};
}
Result<> makeUnary(Index, const std::vector<Annotation>&, UnaryOp) {
return Ok{};
}
Expand Down Expand Up @@ -2159,6 +2163,12 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
return withLoc(pos, irBuilder.makeBinary(op));
}

Result<> makeWideIntBinary(Index pos,
const std::vector<Annotation>& annotations,
WideIntBinaryOp op) {
return withLoc(pos, irBuilder.makeWideIntBinary(op));
}

Result<>
makeUnary(Index pos, const std::vector<Annotation>& annotations, UnaryOp op) {
return withLoc(pos, irBuilder.makeUnary(op));
Expand Down
13 changes: 13 additions & 0 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ Result<> makeNop(Ctx&, Index, const std::vector<Annotation>&);
template<typename Ctx>
Result<> makeBinary(Ctx&, Index, const std::vector<Annotation>&, BinaryOp op);
template<typename Ctx>
Result<> makeWideIntBinary(Ctx&,
Index,
const std::vector<Annotation>&,
WideIntBinaryOp op);
template<typename Ctx>
Result<> makeUnary(Ctx&, Index, const std::vector<Annotation>&, UnaryOp op);
template<typename Ctx>
Result<> makeSelect(Ctx&, Index, const std::vector<Annotation>&);
Expand Down Expand Up @@ -1592,6 +1597,14 @@ Result<> makeBinary(Ctx& ctx,
return ctx.makeBinary(pos, annotations, op);
}

template<typename Ctx>
Result<> makeWideIntBinary(Ctx& ctx,
Index pos,
const std::vector<Annotation>& annotations,
WideIntBinaryOp op) {
return ctx.makeWideIntBinary(pos, annotations, op);
}

template<typename Ctx>
Result<> makeUnary(Ctx& ctx,
Index pos,
Expand Down
4 changes: 4 additions & 0 deletions src/passes/I64ToI32Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,10 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> {
}
}

void visitWideIntBinary(WideIntBinary* curr) {
WASM_UNREACHABLE("TODO: wide arithmetic lowering");
}

void visitSelect(Select* curr) {
if (handleUnreachable(curr)) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/passes/Precompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ class PrecomputingExpressionRunner
return Flow(NONCONSTANT_FLOW);
}

Flow visitWideIntBinary(WideIntBinary* curr) {
return Super::visitWideIntBinary(curr);
}

Flow visitStringEncode(StringEncode* curr) {
// string.encode_wtf16_array is effectively an Array write operation, so
// just like ArraySet and ArrayCopy above we must mark it as disallowed
Expand Down
11 changes: 11 additions & 0 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,17 @@ struct PrintExpressionContents
}
restoreNormalColor(o);
}
void visitWideIntBinary(WideIntBinary* curr) {
prepareColor(o);
switch (curr->op) {
case AddInt128:
o << "i64.add128";
break;
default:
WASM_UNREACHABLE("invalid wide int binary op");
}
restoreNormalColor(o);
}
void visitSelect(Select* curr) {
prepareColor(o) << "select";
restoreNormalColor(o);
Expand Down
1 change: 1 addition & 0 deletions src/passes/TypeGeneralizing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ struct TransferFn : OverriddenVisitor<TransferFn> {
void visitConst(Const* curr) {}
void visitUnary(Unary* curr) {}
void visitBinary(Binary* curr) {}
void visitWideIntBinary(WideIntBinary* curr) {}

void visitSelect(Select* curr) {
if (curr->type.isRef()) {
Expand Down
4 changes: 4 additions & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,10 @@ enum ASTNodes {
MemoryCopy = 0x0a,
MemoryFill = 0x0b,

// wide arithmetic opcodes

I64Add128 = 0x13,

// reference types opcodes

TableGrow = 0x0f,
Expand Down
8 changes: 8 additions & 0 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,14 @@ class Builder {
ret->finalize();
return ret;
}
WideIntBinary* makeWideIntBinary(WideIntBinaryOp op,
const std::vector<Expression*>& operands) {
auto* ret = wasm.allocator.alloc<WideIntBinary>();
ret->op = op;
ret->operands.set(operands);
ret->finalize();
return ret;
}
Select*
makeSelect(Expression* condition, Expression* ifTrue, Expression* ifFalse) {
auto* ret = wasm.allocator.alloc<Select>();
Expand Down
4 changes: 4 additions & 0 deletions src/wasm-delegations-fields.def
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ DELEGATE_FIELD_IMMEDIATE_TYPED_CHILD(StructNotify, ref)
DELEGATE_FIELD_INT(StructNotify, index)
DELEGATE_FIELD_CASE_END(StructNotify)

DELEGATE_FIELD_CASE_START(WideIntBinary)
DELEGATE_FIELD_INT(WideIntBinary, op)
DELEGATE_FIELD_CHILD_VECTOR(WideIntBinary, operands)
DELEGATE_FIELD_CASE_END(WideIntBinary)

DELEGATE_FIELD_MAIN_END

Expand Down
1 change: 1 addition & 0 deletions src/wasm-delegations.def
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ DELEGATE(ResumeThrow);
DELEGATE(StackSwitch);
DELEGATE(StructWait);
DELEGATE(StructNotify);
DELEGATE(WideIntBinary);

#undef DELEGATE
22 changes: 22 additions & 0 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,25 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
VISIT(condition, curr->condition)
return condition.getSingleValue().geti32() ? ifTrue : ifFalse; // ;-)
}
Flow visitWideIntBinary(WideIntBinary* curr) {
Literals arguments;
VISIT_ARGUMENTS(flow, curr->operands, arguments);
if (curr->op == AddInt128) {
uint64_t lowLHS = arguments[0].geti64();
uint64_t highLHS = arguments[1].geti64();
uint64_t lowRHS = arguments[2].geti64();
uint64_t highRHS = arguments[3].geti64();

uint64_t lowRes = lowLHS + lowRHS;
uint64_t highRes = highLHS + highRHS + (lowRes < lowLHS);

Literals results;
results.push_back(Literal(lowRes));
results.push_back(Literal(highRes));
return results;
}
WASM_UNREACHABLE("invalid wide int binary op");
}
Flow visitDrop(Drop* curr) {
VISIT(value, curr->value)
return Flow();
Expand Down Expand Up @@ -3047,6 +3066,9 @@ class ConstantExpressionRunner : public ExpressionRunner<SubType> {
Flow visitAtomicCmpxchg(AtomicCmpxchg* curr) {
return Flow(NONCONSTANT_FLOW);
}
Flow visitWideIntBinary(WideIntBinary* curr) {
return ExpressionRunner<SubType>::visitWideIntBinary(curr);
}
Flow visitAtomicWait(AtomicWait* curr) { return Flow(NONCONSTANT_FLOW); }
Flow visitAtomicNotify(AtomicNotify* curr) { return Flow(NONCONSTANT_FLOW); }
Flow visitStructWait(StructWait* curr) { return Flow(NONCONSTANT_FLOW); }
Expand Down
1 change: 1 addition & 0 deletions src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
Result<> makeConst(Literal val);
Result<> makeUnary(UnaryOp op);
Result<> makeBinary(BinaryOp op);
Result<> makeWideIntBinary(WideIntBinaryOp op);
Result<> makeSelect(std::optional<Type> type = std::nullopt);
Result<> makeDrop();
Result<> makeReturn();
Expand Down
15 changes: 15 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ enum StringEqOp {
StringEqCompare,
};

enum WideIntBinaryOp {
AddInt128,
};

//
// Expressions
//
Expand Down Expand Up @@ -767,6 +771,7 @@ class Expression {
StackSwitchId,
StructWaitId,
StructNotifyId,
WideIntBinaryId,
NumExpressionIds
};
Id _id;
Expand Down Expand Up @@ -1298,6 +1303,16 @@ class Binary : public SpecificExpression<Expression::BinaryId> {
void finalize();
};

class WideIntBinary : public SpecificExpression<Expression::WideIntBinaryId> {
public:
WideIntBinary(MixedArena& allocator) : operands(allocator) {}

WideIntBinaryOp op;
ExpressionList operands;

void finalize();
};

class Select : public SpecificExpression<Expression::SelectId> {
public:
Select() = default;
Expand Down
2 changes: 2 additions & 0 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3998,6 +3998,8 @@ Result<> WasmBinaryReader::readInst() {
}
case BinaryConsts::MemoryFill:
return builder.makeMemoryFill(getMemoryName(getU32LEB()));
case BinaryConsts::I64Add128:
return builder.makeWideIntBinary(AddInt128);
case BinaryConsts::TableSize:
return builder.makeTableSize(getTableName(getU32LEB()));
case BinaryConsts::TableGrow:
Expand Down
9 changes: 9 additions & 0 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,15 @@ Result<> IRBuilder::makeBinary(BinaryOp op) {
return Ok{};
}

Result<> IRBuilder::makeWideIntBinary(WideIntBinaryOp op) {
WideIntBinary curr(wasm.allocator);
curr.op = op;
CHECK_ERR(visitWideIntBinary(&curr));
push(builder.makeWideIntBinary(
op, std::vector<Expression*>(curr.operands.begin(), curr.operands.end())));
return Ok{};
}

Result<> IRBuilder::makeSelect(std::optional<Type> type) {
Select curr;
CHECK_ERR(visitSelect(&curr));
Expand Down
11 changes: 11 additions & 0 deletions src/wasm/wasm-stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,17 @@ void BinaryInstWriter::visitSelect(Select* curr) {
}
}

void BinaryInstWriter::visitWideIntBinary(WideIntBinary* curr) {
o << static_cast<int8_t>(BinaryConsts::MiscPrefix);
switch (curr->op) {
case AddInt128:
o << U32LEB(BinaryConsts::I64Add128);
break;
default:
WASM_UNREACHABLE("invalid wide int binary op");
}
}

void BinaryInstWriter::visitReturn(Return* curr) {
o << static_cast<int8_t>(BinaryConsts::Return);
}
Expand Down
Loading
Loading