Skip to content
Merged
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 @@ -148,6 +148,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
42 changes: 32 additions & 10 deletions src/gen-s-parser.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3427,12 +3427,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 Expand Up @@ -4113,12 +4124,23 @@ switch (buf[0]) {
default: goto parse_error;
}
}
case 'u':
if (op == "i64.sub"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::SubInt64));
return Ok{};
case 'u': {
switch (buf[7]) {
case '\0':
if (op == "i64.sub"sv) {
CHECK_ERR(makeBinary(ctx, pos, annotations, BinaryOp::SubInt64));
return Ok{};
}
goto parse_error;
case '1':
if (op == "i64.sub128"sv) {
CHECK_ERR(makeWideIntAddSub(ctx, pos, annotations, WideIntAddSubOp::SubInt128));
return Ok{};
}
goto parse_error;
default: goto parse_error;
}
goto parse_error;
}
default: goto parse_error;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> {
WASM_UNREACHABLE("TODO");
}
}
Flow visitWideIntAddSub(WideIntAddSub* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitDrop(Drop* curr) { WASM_UNREACHABLE("TODO"); }
Flow visitReturn(Return* 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 @@ -705,6 +705,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 @@ -575,6 +575,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
11 changes: 11 additions & 0 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ 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 +2164,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
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<> 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>
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<> 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
14 changes: 14 additions & 0 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,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;
}
}
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
20 changes: 14 additions & 6 deletions src/support/stdckdint.h
Comment thread
stevenfontanella marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ template<typename T> bool ckd_add(T* output, T a, T b) {
// Atm this polyfill only supports unsigned types.
static_assert(std::is_unsigned_v<T>);

T result = a + b;
if (result < a) {
return true;
}
*output = result;
return false;
*output = a + b;
return *output < a;
#endif
}

template<typename T> bool ckd_sub(T* output, T a, T b) {
#if __has_builtin(__builtin_sub_overflow)
return __builtin_sub_overflow(a, b, output);
#else
// Atm this polyfill only supports unsigned types.
static_assert(std::is_unsigned_v<T>);

*output = a - b;
return *output > a;
#endif
}

Expand Down
5 changes: 5 additions & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,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, rightHigh)
Comment thread
stevenfontanella marked this conversation as resolved.
DELEGATE_FIELD_CHILD(WideIntAddSub, rightLow)
DELEGATE_FIELD_CHILD(WideIntAddSub, leftHigh)
DELEGATE_FIELD_CHILD(WideIntAddSub, leftLow)
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
32 changes: 32 additions & 0 deletions src/wasm-interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,38 @@ 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);

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 lowResult = 0;
uint64_t highResult = 0;

switch (curr->op) {
case AddInt128: {
bool overflowed = std::ckd_add(&lowResult, lowLHS, lowRHS);
highResult = highLHS + highRHS + overflowed;
break;
}
case SubInt128: {
bool overflowed = std::ckd_sub(&lowResult, lowLHS, lowRHS);
highResult = highLHS - highRHS - overflowed;
break;
}
}

Literals results;
results.push_back(Literal(lowResult));
results.push_back(Literal(highResult));
return results;
}
Flow visitDrop(Drop* curr) {
VISIT(value, curr->value)
return 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
2 changes: 2 additions & 0 deletions src/wasm-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ constexpr HeapType noexn = HeapType::noexn;
HeapType getMutI8Array();
HeapType getMutI16Array();

Type getI64Pair();

} // namespace HeapTypes

// A recursion group consisting of one or more HeapTypes. HeapTypes with single
Expand Down
Loading
Loading