Use wrapping arithmetic for exp in unchecked blocks.

This commit is contained in:
chriseth 2020-12-22 16:49:27 +01:00
parent 6bcae84610
commit 2ab83217e3
7 changed files with 18 additions and 2 deletions

View File

@ -802,7 +802,12 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
IRVariable left = convert(_binOp.leftExpression(), *commonType);
IRVariable right = convert(_binOp.rightExpression(), *type(_binOp.rightExpression()).mobileType());
if (auto rationalNumberType = dynamic_cast<RationalNumberType const*>(_binOp.leftExpression().annotation().type))
if (m_context.arithmetic() == Arithmetic::Wrapping)
define(_binOp) << m_utils.wrappingIntExpFunction(
dynamic_cast<IntegerType const&>(left.type()),
dynamic_cast<IntegerType const&>(right.type())
) << "(" << left.name() << ", " << right.name() << ")\n";
else if (auto rationalNumberType = dynamic_cast<RationalNumberType const*>(_binOp.leftExpression().annotation().type))
{
solAssert(rationalNumberType->integerType(), "Invalid literal as the base for exponentiation.");
solAssert(dynamic_cast<IntegerType const*>(commonType), "");
@ -818,7 +823,6 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
dynamic_cast<IntegerType const&>(left.type()),
dynamic_cast<IntegerType const&>(right.type())
) << "(" << left.name() << ", " << right.name() << ")\n";
}
else if (TokenTraits::isShiftOp(op))
{

View File

@ -7,5 +7,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x1

View File

@ -6,5 +6,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x1

View File

@ -7,5 +7,7 @@ contract C {
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x1

View File

@ -9,5 +9,7 @@ contract C {
}
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x00

View File

@ -7,6 +7,8 @@ contract test {
}
}
}
// ====
// compileViaYul: also
// ----
// f(uint256): 0 -> 1, 1
// f(uint256): 1 -> 2, -2

View File

@ -10,5 +10,7 @@ contract test {
return r;
}
}
// ====
// compileViaYul: also
// ----
// f() -> 4