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
2 changes: 2 additions & 0 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@
("i64.shr_u", "makeBinary(BinaryOp::ShrUInt64)"),
("i64.rotl", "makeBinary(BinaryOp::RotLInt64)"),
("i64.rotr", "makeBinary(BinaryOp::RotRInt64)"),
("i64.add128", "makeWideIntAddSub(WideIntAddSubOp::AddInt128)"),
("i64.sub128", "makeWideIntAddSub(WideIntAddSubOp::SubInt128)"),
("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(makeWideIntAddSub(ctx, pos, annotations, WideIntAddSubOp::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
22 changes: 22 additions & 0 deletions src/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
}
}
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitWideIntAddSub(WideIntAddSub* curr) {
if (curr->op == AddInt128 || curr->op == SubInt128) {
uint64_t highRHS = pop().geti64();
uint64_t lowRHS = pop().geti64();
uint64_t highLHS = pop().geti64();
uint64_t lowLHS = pop().geti64();

uint64_t lowRes, highRes;
if (curr->op == AddInt128) {
lowRes = lowLHS + lowRHS;
highRes = highLHS + highRHS + (lowRes < lowLHS);
} else {
lowRes = lowLHS - lowRHS;
highRes = highLHS - highRHS - (lowLHS < lowRHS);
}

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::visitWideIntAddSub(WideIntAddSub* 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
7 changes: 7 additions & 0 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,13 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
}
}

void visitWideIntAddSub(WideIntAddSub* curr) {
note(&curr->leftLow, Type::i64);
note(&curr->leftHigh, Type::i64);
note(&curr->rightLow, Type::i64);
note(&curr->rightHigh, Type::i64);
}

void visitSelect(Select* curr, std::optional<Type> type = std::nullopt) {
if (type) {
note(&curr->ifTrue, *type);
Expand Down
4 changes: 4 additions & 0 deletions src/ir/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
}
return ret + visit(curr->left) + visit(curr->right);
}
CostType visitWideIntAddSub(WideIntAddSub* curr) {
return 1 + visit(curr->leftLow) + visit(curr->leftHigh) +
visit(curr->rightLow) + visit(curr->rightHigh);
}
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 visitWideIntAddSub(WideIntAddSub* 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 visitWideIntAddSub(WideIntAddSub* 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 visitWideIntAddSub(WideIntAddSub* curr) {}
void visitSelect(Select* curr) {
self()->noteSubtype(curr->ifTrue, curr);
self()->noteSubtype(curr->ifFalse, curr);
Expand Down
12 changes: 12 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ struct NullInstrParserCtx {
Result<> makeBinary(Index, const std::vector<Annotation>&, BinaryOp) {
return Ok{};
}
Result<> makeWideIntAddSub(Index,
const std::vector<Annotation>&,
WideIntAddSubOp) {
return Ok{};
}

Result<> makeUnary(Index, const std::vector<Annotation>&, UnaryOp) {
return Ok{};
}
Expand Down Expand Up @@ -2159,6 +2165,12 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
return withLoc(pos, irBuilder.makeBinary(op));
}

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

Result<>
makeUnary(Index pos, const std::vector<Annotation>& annotations, UnaryOp op) {
return withLoc(pos, irBuilder.makeUnary(op));
Expand Down
12 changes: 12 additions & 0 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ template<typename Ctx>
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<>
makeWideIntAddSub(Ctx&, Index, const std::vector<Annotation>&, WideIntAddSubOp op);

template<typename Ctx>
Result<> makeUnary(Ctx&, Index, const std::vector<Annotation>&, UnaryOp op);
template<typename Ctx>
Expand Down Expand Up @@ -1592,6 +1596,14 @@ Result<> makeBinary(Ctx& ctx,
return ctx.makeBinary(pos, annotations, op);
}

template<typename Ctx>
Result<> makeWideIntAddSub(Ctx& ctx,
Index pos,
const std::vector<Annotation>& annotations,
WideIntAddSubOp op) {
return ctx.makeWideIntAddSub(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 visitWideIntAddSub(WideIntAddSub* 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 visitWideIntAddSub(WideIntAddSub* curr) {
return Super::visitWideIntAddSub(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
14 changes: 14 additions & 0 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,20 @@ struct PrintExpressionContents
}
restoreNormalColor(o);
}
void visitWideIntAddSub(WideIntAddSub* curr) {
prepareColor(o);
switch (curr->op) {
case AddInt128:
o << "i64.add128";
break;
case SubInt128:
o << "i64.sub128";
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 visitWideIntAddSub(WideIntAddSub* curr) {}

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

// wide arithmetic opcodes

I64Add128 = 0x13,
I64Sub128 = 0x14,

// reference types opcodes

TableGrow = 0x0f,
Expand Down
14 changes: 14 additions & 0 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,20 @@ class Builder {
ret->finalize();
return ret;
}
WideIntAddSub* makeWideIntAddSub(WideIntAddSubOp op,
Expression* leftLow,
Expression* leftHigh,
Expression* rightLow,
Expression* rightHigh) {
auto* ret = wasm.allocator.alloc<WideIntAddSub>();
ret->op = op;
ret->leftLow = leftLow;
ret->leftHigh = leftHigh;
ret->rightLow = rightLow;
ret->rightHigh = rightHigh;
ret->finalize();
return ret;
}
Select*
makeSelect(Expression* condition, Expression* ifTrue, Expression* ifFalse) {
auto* ret = wasm.allocator.alloc<Select>();
Expand Down
7 changes: 7 additions & 0 deletions src/wasm-delegations-fields.def
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,13 @@ DELEGATE_FIELD_IMMEDIATE_TYPED_CHILD(StructNotify, ref)
DELEGATE_FIELD_INT(StructNotify, index)
DELEGATE_FIELD_CASE_END(StructNotify)

DELEGATE_FIELD_CASE_START(WideIntAddSub)
DELEGATE_FIELD_INT(WideIntAddSub, op)
DELEGATE_FIELD_CHILD(WideIntAddSub, leftLow)
DELEGATE_FIELD_CHILD(WideIntAddSub, leftHigh)
DELEGATE_FIELD_CHILD(WideIntAddSub, rightLow)
DELEGATE_FIELD_CHILD(WideIntAddSub, rightHigh)
DELEGATE_FIELD_CASE_END(WideIntAddSub)

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(WideIntAddSub);

#undef DELEGATE
30 changes: 30 additions & 0 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,33 @@ class ExpressionRunner : public OverriddenVisitor<SubType, Flow> {
VISIT(condition, curr->condition)
return condition.getSingleValue().geti32() ? ifTrue : ifFalse; // ;-)
}
Flow visitWideIntAddSub(WideIntAddSub* curr) {
VISIT(leftLow, curr->leftLow);
VISIT(leftHigh, curr->leftHigh);
VISIT(rightLow, curr->rightLow);
VISIT(rightHigh, curr->rightHigh);
if (curr->op == AddInt128 || curr->op == SubInt128) {
uint64_t lowLHS = leftLow.getSingleValue().geti64();
uint64_t highLHS = leftHigh.getSingleValue().geti64();
uint64_t lowRHS = rightLow.getSingleValue().geti64();
uint64_t highRHS = rightHigh.getSingleValue().geti64();

uint64_t lowRes, highRes;
if (curr->op == AddInt128) {
lowRes = lowLHS + lowRHS;
highRes = highLHS + highRHS + (lowRes < lowLHS);
} else {
lowRes = lowLHS - lowRHS;
highRes = highLHS - highRHS - (lowLHS < lowRHS);
}

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 +3074,9 @@ class ConstantExpressionRunner : public ExpressionRunner<SubType> {
Flow visitAtomicCmpxchg(AtomicCmpxchg* curr) {
return Flow(NONCONSTANT_FLOW);
}
Flow visitWideIntAddSub(WideIntAddSub* curr) {
return ExpressionRunner<SubType>::visitWideIntAddSub(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<> makeWideIntAddSub(WideIntAddSubOp op);
Result<> makeSelect(std::optional<Type> type = std::nullopt);
Result<> makeDrop();
Result<> makeReturn();
Expand Down
20 changes: 20 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,11 @@ enum StringEqOp {
StringEqCompare,
};

enum WideIntAddSubOp {
AddInt128,
SubInt128,
};

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

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

WideIntAddSubOp op;
Expression* leftLow;
Expression* leftHigh;
Expression* rightLow;
Expression* rightHigh;

void finalize();
};

class Select : public SpecificExpression<Expression::SelectId> {
public:
Select() = default;
Expand Down
5 changes: 5 additions & 0 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3998,6 +3998,11 @@ Result<> WasmBinaryReader::readInst() {
}
case BinaryConsts::MemoryFill:
return builder.makeMemoryFill(getMemoryName(getU32LEB()));
case BinaryConsts::I64Add128:
return builder.makeWideIntAddSub(AddInt128);
case BinaryConsts::I64Sub128:
return builder.makeWideIntAddSub(SubInt128);

case BinaryConsts::TableSize:
return builder.makeTableSize(getTableName(getU32LEB()));
case BinaryConsts::TableGrow:
Expand Down
Loading
Loading