mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #8845 from ethereum/solYulCleanup
[Sol->Yul] Cleanup for checked arithmetic and comparisons.
This commit is contained in:
commit
4c1e821e01
@ -393,6 +393,8 @@ string YulUtilFunctions::overflowCheckedIntAddFunction(IntegerType const& _type)
|
||||
return
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> sum {
|
||||
x := <cleanupFunction>(x)
|
||||
y := <cleanupFunction>(y)
|
||||
<?signed>
|
||||
// overflow, if x >= 0 and y > (maxValue - x)
|
||||
if and(iszero(slt(x, 0)), sgt(y, sub(<maxValue>, x))) { revert(0, 0) }
|
||||
@ -409,6 +411,7 @@ string YulUtilFunctions::overflowCheckedIntAddFunction(IntegerType const& _type)
|
||||
("signed", _type.isSigned())
|
||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
@ -421,6 +424,8 @@ string YulUtilFunctions::overflowCheckedIntMulFunction(IntegerType const& _type)
|
||||
// Multiplication by zero could be treated separately and directly return zero.
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> product {
|
||||
x := <cleanupFunction>(x)
|
||||
y := <cleanupFunction>(y)
|
||||
<?signed>
|
||||
// overflow, if x > 0, y > 0 and x > (maxValue / y)
|
||||
if and(and(sgt(x, 0), sgt(y, 0)), gt(x, div(<maxValue>, y))) { revert(0, 0) }
|
||||
@ -441,6 +446,7 @@ string YulUtilFunctions::overflowCheckedIntMulFunction(IntegerType const& _type)
|
||||
("signed", _type.isSigned())
|
||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
@ -452,6 +458,8 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
||||
return
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> r {
|
||||
x := <cleanupFunction>(x)
|
||||
y := <cleanupFunction>(y)
|
||||
if iszero(y) { revert(0, 0) }
|
||||
<?signed>
|
||||
// overflow for minVal / -1
|
||||
@ -466,6 +474,7 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
||||
("functionName", functionName)
|
||||
("signed", _type.isSigned())
|
||||
("minVal", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
@ -477,12 +486,15 @@ string YulUtilFunctions::checkedIntModFunction(IntegerType const& _type)
|
||||
return
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> r {
|
||||
x := <cleanupFunction>(x)
|
||||
y := <cleanupFunction>(y)
|
||||
if iszero(y) { revert(0, 0) }
|
||||
r := <?signed>s</signed>mod(x, y)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("signed", _type.isSigned())
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
@ -494,6 +506,8 @@ string YulUtilFunctions::overflowCheckedIntSubFunction(IntegerType const& _type)
|
||||
return
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> diff {
|
||||
x := <cleanupFunction>(x)
|
||||
y := <cleanupFunction>(y)
|
||||
<?signed>
|
||||
// underflow, if y >= 0 and x < (minValue + y)
|
||||
if and(iszero(slt(y, 0)), slt(x, add(<minValue>, y))) { revert(0, 0) }
|
||||
@ -509,6 +523,7 @@ string YulUtilFunctions::overflowCheckedIntSubFunction(IntegerType const& _type)
|
||||
("signed", _type.isSigned())
|
||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
@ -1764,8 +1779,18 @@ string YulUtilFunctions::cleanupFunction(Type const& _type)
|
||||
solUnimplemented("Fixed point types not implemented.");
|
||||
break;
|
||||
case Type::Category::Function:
|
||||
solAssert(dynamic_cast<FunctionType const&>(_type).kind() == FunctionType::Kind::External, "");
|
||||
templ("body", "cleaned := " + cleanupFunction(FixedBytesType(24)) + "(value)");
|
||||
switch (dynamic_cast<FunctionType const&>(_type).kind())
|
||||
{
|
||||
case FunctionType::Kind::External:
|
||||
templ("body", "cleaned := " + cleanupFunction(FixedBytesType(24)) + "(value)");
|
||||
break;
|
||||
case FunctionType::Kind::Internal:
|
||||
templ("body", "cleaned := value");
|
||||
break;
|
||||
default:
|
||||
solAssert(false, "");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Type::Category::Array:
|
||||
case Type::Category::Struct:
|
||||
@ -1938,14 +1963,16 @@ std::string YulUtilFunctions::decrementCheckedFunction(Type const& _type)
|
||||
|
||||
return Whiskers(R"(
|
||||
function <functionName>(value) -> ret {
|
||||
value := <cleanupFunction>(value)
|
||||
if <lt>(value, <minval>) { revert(0,0) }
|
||||
ret := sub(value, 1)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("minval", toCompactHexWithPrefix(minintval))
|
||||
("lt", type.isSigned() ? "slt" : "lt")
|
||||
.render();
|
||||
("functionName", functionName)
|
||||
("minval", toCompactHexWithPrefix(minintval))
|
||||
("lt", type.isSigned() ? "slt" : "lt")
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
||||
@ -1966,14 +1993,16 @@ std::string YulUtilFunctions::incrementCheckedFunction(Type const& _type)
|
||||
|
||||
return Whiskers(R"(
|
||||
function <functionName>(value) -> ret {
|
||||
value := <cleanupFunction>(value)
|
||||
if <gt>(value, <maxval>) { revert(0,0) }
|
||||
ret := add(value, 1)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("maxval", toCompactHexWithPrefix(maxintval))
|
||||
("gt", type.isSigned() ? "sgt" : "gt")
|
||||
.render();
|
||||
("functionName", functionName)
|
||||
("maxval", toCompactHexWithPrefix(maxintval))
|
||||
("gt", type.isSigned() ? "sgt" : "gt")
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
||||
@ -1988,15 +2017,17 @@ string YulUtilFunctions::negateNumberCheckedFunction(Type const& _type)
|
||||
|
||||
return m_functionCollector.createFunction(functionName, [&]() {
|
||||
return Whiskers(R"(
|
||||
function <functionName>(_value) -> ret {
|
||||
if slt(_value, <minval>) { revert(0,0) }
|
||||
ret := sub(0, _value)
|
||||
function <functionName>(value) -> ret {
|
||||
value := <cleanupFunction>(value)
|
||||
if slt(value, <minval>) { revert(0,0) }
|
||||
ret := sub(0, value)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("minval", toCompactHexWithPrefix(minintval))
|
||||
.render();
|
||||
});
|
||||
("functionName", functionName)
|
||||
("minval", toCompactHexWithPrefix(minintval))
|
||||
("cleanupFunction", cleanupFunction(_type))
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::zeroValueFunction(Type const& _type, bool _splitFunctionTypes)
|
||||
|
@ -496,9 +496,9 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
|
||||
isSigned = type->isSigned();
|
||||
|
||||
string args =
|
||||
expressionAsType(_binOp.leftExpression(), *commonType) +
|
||||
expressionAsType(_binOp.leftExpression(), *commonType, true) +
|
||||
", " +
|
||||
expressionAsType(_binOp.rightExpression(), *commonType);
|
||||
expressionAsType(_binOp.rightExpression(), *commonType, true);
|
||||
|
||||
string expr;
|
||||
if (op == Token::Equal)
|
||||
@ -1811,11 +1811,16 @@ IRVariable IRGeneratorForStatements::convert(IRVariable const& _from, Type const
|
||||
}
|
||||
}
|
||||
|
||||
std::string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to)
|
||||
std::string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to, bool _forceCleanup)
|
||||
{
|
||||
IRVariable from(_expression);
|
||||
if (from.type() == _to)
|
||||
return from.commaSeparatedList();
|
||||
{
|
||||
if (_forceCleanup)
|
||||
return m_utils.cleanupFunction(_to) + "(" + from.commaSeparatedList() + ")";
|
||||
else
|
||||
return from.commaSeparatedList();
|
||||
}
|
||||
else
|
||||
return m_utils.conversionFunction(from.type(), _to) + "(" + from.commaSeparatedList() + ")";
|
||||
}
|
||||
|
@ -112,7 +112,8 @@ private:
|
||||
|
||||
/// @returns a Yul expression representing the current value of @a _expression,
|
||||
/// converted to type @a _to if it does not yet have that type.
|
||||
std::string expressionAsType(Expression const& _expression, Type const& _to);
|
||||
/// If @a _forceCleanup is set to true, it also cleans the value, in case it already has type @a _to.
|
||||
std::string expressionAsType(Expression const& _expression, Type const& _to, bool _forceCleanup = false);
|
||||
|
||||
/// @returns an output stream that can be used to define @a _var using a function call or
|
||||
/// single stack slot expression.
|
||||
|
@ -325,6 +325,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
|
||||
case Token::Bool:
|
||||
case Token::Address:
|
||||
case Token::Var:
|
||||
case Token::In:
|
||||
{
|
||||
YulString literal{currentLiteral()};
|
||||
if (m_dialect.builtin(literal))
|
||||
@ -515,6 +516,7 @@ YulString Parser::expectAsmIdentifier()
|
||||
case Token::Bool:
|
||||
case Token::Identifier:
|
||||
case Token::Var:
|
||||
case Token::In:
|
||||
break;
|
||||
default:
|
||||
expectToken(Token::Identifier);
|
||||
|
@ -30,7 +30,8 @@ contract C {
|
||||
return arr[i](x);
|
||||
}
|
||||
}
|
||||
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// test(uint256,uint256): 10, 0 -> 11
|
||||
// test(uint256,uint256): 10, 1 -> 12
|
||||
|
@ -10,6 +10,7 @@ contract C {
|
||||
require(y == bytes2(0xffff));
|
||||
}
|
||||
}
|
||||
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// f() -> "\xff\xff\xff\xff"
|
||||
|
@ -22,7 +22,8 @@ contract C {
|
||||
return garbled != garbled;
|
||||
}
|
||||
}
|
||||
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// test_eq_ok() -> 1
|
||||
// test_eq() -> FAILURE # both should throw #
|
||||
|
@ -0,0 +1,65 @@
|
||||
contract C {
|
||||
function add() public pure returns (uint8, uint8) {
|
||||
uint8 x; uint8 y = 0;
|
||||
assembly { x := 0x0101 }
|
||||
return (x + y, y + x);
|
||||
}
|
||||
function sub() public pure returns (uint8, uint8) {
|
||||
uint8 x; uint8 y = 1;
|
||||
assembly { x := 0x0101 }
|
||||
return (x - y, y - x);
|
||||
}
|
||||
function mul() public pure returns (uint8, uint8) {
|
||||
uint8 x; uint8 y = 1;
|
||||
assembly { x := 0x0101 }
|
||||
return (x * y, y * x);
|
||||
}
|
||||
function div() public pure returns (uint8, uint8) {
|
||||
uint8 x; uint8 y = 1;
|
||||
assembly { x := 0x0101 }
|
||||
return (x / y, y / x);
|
||||
}
|
||||
function mod() public pure returns (uint8, uint8) {
|
||||
uint8 x; uint8 y = 2;
|
||||
assembly { x := 0x0101 }
|
||||
return (x % y, y % x);
|
||||
}
|
||||
function inc_pre() public pure returns (uint8) {
|
||||
uint8 x;
|
||||
assembly { x := 0x0100 }
|
||||
return ++x;
|
||||
}
|
||||
function inc_post() public pure returns (uint8) {
|
||||
uint8 x;
|
||||
assembly { x := 0x0100 }
|
||||
return x++;
|
||||
}
|
||||
function dec_pre() public pure returns (uint8) {
|
||||
uint8 x;
|
||||
assembly { x := not(0xFF) }
|
||||
return --x;
|
||||
}
|
||||
function dec_post() public pure returns (uint8) {
|
||||
uint8 x;
|
||||
assembly { x := not(0xFF) }
|
||||
return x--;
|
||||
}
|
||||
function neg() public pure returns (int8) {
|
||||
int8 x;
|
||||
assembly { x := 0x80 }
|
||||
return -x;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// add() -> 1, 1
|
||||
// sub() -> 0, 0
|
||||
// mul() -> 1, 1
|
||||
// div() -> 1, 1
|
||||
// mod() -> 1, 0
|
||||
// inc_pre() -> 1
|
||||
// inc_post() -> 0
|
||||
// dec_pre() -> FAILURE
|
||||
// dec_post() -> FAILURE
|
||||
// neg() -> FAILURE
|
41
test/libsolidity/semanticTests/viaYul/cleanup/comparison.sol
Normal file
41
test/libsolidity/semanticTests/viaYul/cleanup/comparison.sol
Normal file
@ -0,0 +1,41 @@
|
||||
contract C {
|
||||
function eq() public pure returns (bool) {
|
||||
uint8 x = 1; uint8 y;
|
||||
assembly { y := 0x0101 }
|
||||
return (x == y);
|
||||
}
|
||||
function neq() public pure returns (bool) {
|
||||
uint8 x = 1; uint8 y;
|
||||
assembly { y := 0x0101 }
|
||||
return (x != y);
|
||||
}
|
||||
function geq() public pure returns (bool) {
|
||||
uint8 x = 1; uint8 y;
|
||||
assembly { y := 0x0101 }
|
||||
return (x >= y);
|
||||
}
|
||||
function leq() public pure returns (bool) {
|
||||
uint8 x = 2; uint8 y;
|
||||
assembly { y := 0x0101 }
|
||||
return (x <= y);
|
||||
}
|
||||
function gt() public pure returns (bool) {
|
||||
uint8 x = 2; uint8 y;
|
||||
assembly { y := 0x0101 }
|
||||
return (x > y);
|
||||
}
|
||||
function lt() public pure returns (bool) {
|
||||
uint8 x = 1; uint8 y;
|
||||
assembly { y := 0x0101 }
|
||||
return (x < y);
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// eq() -> true
|
||||
// neq() -> false
|
||||
// geq() -> true
|
||||
// leq() -> false
|
||||
// gt() -> true
|
||||
// lt() -> false
|
Loading…
Reference in New Issue
Block a user