mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implement signed subtraction for sol->yul code generation.
This commit is contained in:
parent
88988af561
commit
5f6af8b374
@ -464,18 +464,28 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::overflowCheckedUIntSubFunction()
|
||||
string YulUtilFunctions::overflowCheckedIntSubFunction(IntegerType const& _type)
|
||||
{
|
||||
string functionName = "checked_sub_uint";
|
||||
string functionName = "checked_sub_" + _type.identifier();
|
||||
return m_functionCollector->createFunction(functionName, [&] {
|
||||
return
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> diff {
|
||||
if lt(x, y) { revert(0, 0) }
|
||||
<?signed>
|
||||
// underflow, if y >= 0 and x < (minValue + y)
|
||||
if and(iszero(slt(y, 0)), slt(x, add(<minValue>, y))) { revert(0, 0) }
|
||||
// overflow, if y < 0 and x > (maxValue + y)
|
||||
if and(slt(y, 0), sgt(x, add(<maxValue>, y))) { revert(0, 0) }
|
||||
<!signed>
|
||||
if lt(x, y) { revert(0, 0) }
|
||||
</signed>
|
||||
diff := sub(x, y)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("signed", _type.isSigned())
|
||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ public:
|
||||
/// @returns computes the difference between two values.
|
||||
/// Assumes the input to be in range for the type.
|
||||
/// signature: (x, y) -> diff
|
||||
std::string overflowCheckedUIntSubFunction();
|
||||
std::string overflowCheckedIntSubFunction(IntegerType const& _type);
|
||||
|
||||
/// @returns the name of a function that fetches the length of the given
|
||||
/// array
|
||||
|
@ -1133,8 +1133,7 @@ string IRGeneratorForStatements::binaryOperation(
|
||||
fun = m_utils.overflowCheckedIntAddFunction(*type);
|
||||
break;
|
||||
case Token::Sub:
|
||||
if (!type->isSigned())
|
||||
fun = m_utils.overflowCheckedUIntSubFunction();
|
||||
fun = m_utils.overflowCheckedIntSubFunction(*type);
|
||||
break;
|
||||
case Token::Mul:
|
||||
if (!type->isSigned())
|
||||
|
@ -0,0 +1,17 @@
|
||||
contract C {
|
||||
function f(uint a, uint b) public pure returns (uint x) {
|
||||
x = a - b;
|
||||
}
|
||||
function g(uint8 a, uint8 b) public pure returns (uint8 x) {
|
||||
x = a - b;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// f(uint256,uint256): 6, 5 -> 1
|
||||
// f(uint256,uint256): 6, 6 -> 0
|
||||
// f(uint256,uint256): 5, 6 -> FAILURE
|
||||
// g(uint8,uint8): 6, 5 -> 1
|
||||
// g(uint8,uint8): 6, 6 -> 0
|
||||
// g(uint8,uint8): 5, 6 -> FAILURE
|
@ -0,0 +1,42 @@
|
||||
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 -> -1
|
||||
// f(int256,int256): -2, 1 -> -3
|
||||
// f(int256,int256): -2, 2 -> -4
|
||||
// f(int256,int256): 2, -2 -> 4
|
||||
// f(int256,int256): 2, 2 -> 0
|
||||
// f(int256,int256): -5, -6 -> 1
|
||||
// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0, -15 -> 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
|
||||
// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0, -16 -> FAILURE
|
||||
// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, -1 -> FAILURE
|
||||
// f(int256,int256): 15, 0x8000000000000000000000000000000000000000000000000000000000000010 -> 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
|
||||
// f(int256,int256): 16, 0x8000000000000000000000000000000000000000000000000000000000000010 -> FAILURE
|
||||
// f(int256,int256): 1, 0x8000000000000000000000000000000000000000000000000000000000000000 -> FAILURE
|
||||
// f(int256,int256): -1, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> 0x8000000000000000000000000000000000000000000000000000000000000000
|
||||
// f(int256,int256): -2, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> FAILURE
|
||||
// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000001, 1 -> 0x8000000000000000000000000000000000000000000000000000000000000000
|
||||
// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000001, 2 -> FAILURE
|
||||
// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000000, 1 -> FAILURE
|
||||
// g(int8,int8): 5, 6 -> -1
|
||||
// g(int8,int8): -2, 1 -> -3
|
||||
// g(int8,int8): -2, 2 -> -4
|
||||
// g(int8,int8): 2, -2 -> 4
|
||||
// g(int8,int8): 2, 2 -> 0
|
||||
// g(int8,int8): -5, -6 -> 1
|
||||
// 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