Implement signed addition for sol->yul code generation.

This commit is contained in:
Daniel Kirchner 2019-06-12 11:24:19 +02:00 committed by chriseth
parent 5b92640ab5
commit 88988af561
5 changed files with 75 additions and 24 deletions

View File

@ -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_uint_" + to_string(_bits);
string functionName = "checked_add_" + _type.identifier();
// 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
Whiskers(R"(
function <functionName>(x, y) -> sum {
<?shortType>
let mask := <mask>
sum := add(and(x, mask), and(y, mask))
if and(sum, not(mask)) { revert(0, 0) }
<!shortType>
sum := add(x, y)
if lt(sum, x) { revert(0, 0) }
</shortType>
<?signed>
// overflow, if x >= 0 and y > (maxValue - x)
if and(iszero(slt(x, 0)), sgt(y, sub(<maxValue>, x))) { revert(0, 0) }
// underflow, if x < 0 and y < (minValue - x)
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)
}
)")
("shortType", _bits < 256)
("functionName", functionName)
("mask", toCompactHexWithPrefix((u256(1) << _bits) - 1))
("signed", _type.isSigned())
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
.render();
});
}

View File

@ -93,7 +93,7 @@ public:
std::string roundUpFunction();
/// signature: (x, y) -> sum
std::string overflowCheckedUIntAddFunction(size_t _bits);
std::string overflowCheckedIntAddFunction(IntegerType const& _type);
/// signature: (x, y) -> product
std::string overflowCheckedUIntMulFunction(size_t _bits);

View File

@ -1126,18 +1126,27 @@ string IRGeneratorForStatements::binaryOperation(
if (IntegerType const* type = dynamic_cast<IntegerType const*>(&_type))
{
string fun;
// TODO: Only division is implemented for signed integers for now.
if (!type->isSigned())
// TODO: Implement all operations for signed and unsigned types.
switch (_operator)
{
if (_operator == Token::Add)
fun = m_utils.overflowCheckedUIntAddFunction(type->numBits());
else if (_operator == Token::Sub)
fun = m_utils.overflowCheckedUIntSubFunction();
else if (_operator == Token::Mul)
fun = m_utils.overflowCheckedUIntMulFunction(type->numBits());
case Token::Add:
fun = m_utils.overflowCheckedIntAddFunction(*type);
break;
case Token::Sub:
if (!type->isSigned())
fun = m_utils.overflowCheckedUIntSubFunction();
break;
case Token::Mul:
if (!type->isSigned())
fun = m_utils.overflowCheckedUIntMulFunction(type->numBits());
break;
case Token::Div:
fun = m_utils.overflowCheckedIntDivFunction(*type);
break;
default:
break;
}
if (_operator == Token::Div)
fun = m_utils.overflowCheckedIntDivFunction(*type);
solUnimplementedAssert(!fun.empty(), "");
return fun + "(" + _left + ", " + _right + ")\n";
}

View File

@ -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