mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implement signed addition for sol->yul code generation.
This commit is contained in:
parent
5b92640ab5
commit
88988af561
@ -379,27 +379,32 @@ string YulUtilFunctions::roundUpFunction()
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
string YulUtilFunctions::overflowCheckedUIntAddFunction(size_t _bits)
|
string YulUtilFunctions::overflowCheckedIntAddFunction(IntegerType const& _type)
|
||||||
{
|
{
|
||||||
solAssert(0 < _bits && _bits <= 256 && _bits % 8 == 0, "");
|
string functionName = "checked_add_" + _type.identifier();
|
||||||
string functionName = "checked_add_uint_" + to_string(_bits);
|
// TODO: Consider to add a special case for unsigned 256-bit integers
|
||||||
|
// and use the following instead:
|
||||||
|
// sum := add(x, y) if lt(sum, x) { revert(0, 0) }
|
||||||
return m_functionCollector->createFunction(functionName, [&]() {
|
return m_functionCollector->createFunction(functionName, [&]() {
|
||||||
return
|
return
|
||||||
Whiskers(R"(
|
Whiskers(R"(
|
||||||
function <functionName>(x, y) -> sum {
|
function <functionName>(x, y) -> sum {
|
||||||
<?shortType>
|
<?signed>
|
||||||
let mask := <mask>
|
// overflow, if x >= 0 and y > (maxValue - x)
|
||||||
sum := add(and(x, mask), and(y, mask))
|
if and(iszero(slt(x, 0)), sgt(y, sub(<maxValue>, x))) { revert(0, 0) }
|
||||||
if and(sum, not(mask)) { revert(0, 0) }
|
// underflow, if x < 0 and y < (minValue - x)
|
||||||
<!shortType>
|
if and(slt(x, 0), slt(y, sub(<minValue>, x))) { revert(0, 0) }
|
||||||
|
<!signed>
|
||||||
|
// overflow, if x > (maxValue - y)
|
||||||
|
if gt(x, sub(<maxValue>, y)) { revert(0, 0) }
|
||||||
|
</signed>
|
||||||
sum := add(x, y)
|
sum := add(x, y)
|
||||||
if lt(sum, x) { revert(0, 0) }
|
|
||||||
</shortType>
|
|
||||||
}
|
}
|
||||||
)")
|
)")
|
||||||
("shortType", _bits < 256)
|
|
||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("mask", toCompactHexWithPrefix((u256(1) << _bits) - 1))
|
("signed", _type.isSigned())
|
||||||
|
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||||
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ public:
|
|||||||
std::string roundUpFunction();
|
std::string roundUpFunction();
|
||||||
|
|
||||||
/// signature: (x, y) -> sum
|
/// signature: (x, y) -> sum
|
||||||
std::string overflowCheckedUIntAddFunction(size_t _bits);
|
std::string overflowCheckedIntAddFunction(IntegerType const& _type);
|
||||||
|
|
||||||
/// signature: (x, y) -> product
|
/// signature: (x, y) -> product
|
||||||
std::string overflowCheckedUIntMulFunction(size_t _bits);
|
std::string overflowCheckedUIntMulFunction(size_t _bits);
|
||||||
|
@ -1126,18 +1126,27 @@ string IRGeneratorForStatements::binaryOperation(
|
|||||||
if (IntegerType const* type = dynamic_cast<IntegerType const*>(&_type))
|
if (IntegerType const* type = dynamic_cast<IntegerType const*>(&_type))
|
||||||
{
|
{
|
||||||
string fun;
|
string fun;
|
||||||
// TODO: Only division is implemented for signed integers for now.
|
// TODO: Implement all operations for signed and unsigned types.
|
||||||
if (!type->isSigned())
|
switch (_operator)
|
||||||
{
|
{
|
||||||
if (_operator == Token::Add)
|
case Token::Add:
|
||||||
fun = m_utils.overflowCheckedUIntAddFunction(type->numBits());
|
fun = m_utils.overflowCheckedIntAddFunction(*type);
|
||||||
else if (_operator == Token::Sub)
|
break;
|
||||||
|
case Token::Sub:
|
||||||
|
if (!type->isSigned())
|
||||||
fun = m_utils.overflowCheckedUIntSubFunction();
|
fun = m_utils.overflowCheckedUIntSubFunction();
|
||||||
else if (_operator == Token::Mul)
|
break;
|
||||||
|
case Token::Mul:
|
||||||
|
if (!type->isSigned())
|
||||||
fun = m_utils.overflowCheckedUIntMulFunction(type->numBits());
|
fun = m_utils.overflowCheckedUIntMulFunction(type->numBits());
|
||||||
}
|
break;
|
||||||
if (_operator == Token::Div)
|
case Token::Div:
|
||||||
fun = m_utils.overflowCheckedIntDivFunction(*type);
|
fun = m_utils.overflowCheckedIntDivFunction(*type);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
solUnimplementedAssert(!fun.empty(), "");
|
solUnimplementedAssert(!fun.empty(), "");
|
||||||
return fun + "(" + _left + ", " + _right + ")\n";
|
return fun + "(" + _left + ", " + _right + ")\n";
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
contract C {
|
||||||
|
function f(int a, int b) public pure returns (int x) {
|
||||||
|
x = a + b;
|
||||||
|
}
|
||||||
|
function g(int8 a, int8 b) public pure returns (int8 x) {
|
||||||
|
x = a + b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: true
|
||||||
|
// ----
|
||||||
|
// f(int256,int256): 5, 6 -> 11
|
||||||
|
// f(int256,int256): -2, 1 -> -1
|
||||||
|
// f(int256,int256): -2, 2 -> 0
|
||||||
|
// f(int256,int256): 2, -2 -> 0
|
||||||
|
// f(int256,int256): -5, -6 -> -11
|
||||||
|
// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0, 0x0F -> 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
|
||||||
|
// f(int256,int256): 0x0F, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0 -> 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
|
||||||
|
// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 1 -> FAILURE
|
||||||
|
// f(int256,int256): 1, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> FAILURE
|
||||||
|
// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000001, -1 -> 0x8000000000000000000000000000000000000000000000000000000000000000
|
||||||
|
// f(int256,int256): -1, 0x8000000000000000000000000000000000000000000000000000000000000001 -> 0x8000000000000000000000000000000000000000000000000000000000000000
|
||||||
|
// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000000, -1 -> FAILURE
|
||||||
|
// f(int256,int256): -1, 0x8000000000000000000000000000000000000000000000000000000000000000 -> FAILURE
|
||||||
|
// g(int8,int8): 5, 6 -> 11
|
||||||
|
// g(int8,int8): -2, 1 -> -1
|
||||||
|
// g(int8,int8): -2, 2 -> 0
|
||||||
|
// g(int8,int8): 2, -2 -> 0
|
||||||
|
// g(int8,int8): -5, -6 -> -11
|
||||||
|
// g(int8,int8): 126, 1 -> 127
|
||||||
|
// g(int8,int8): 1, 126 -> 127
|
||||||
|
// g(int8,int8): 127, 1 -> FAILURE
|
||||||
|
// g(int8,int8): 1, 127 -> FAILURE
|
||||||
|
// g(int8,int8): -127, -1 -> -128
|
||||||
|
// g(int8,int8): -1, -127 -> -128
|
||||||
|
// g(int8,int8): -127, -2 -> FAILURE
|
||||||
|
// g(int8,int8): -2, -127 -> FAILURE
|
Loading…
Reference in New Issue
Block a user