mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Sol->Yul: Add cleanup to operations.
This commit is contained in:
parent
dfd9008214
commit
7ad319687d
@ -393,6 +393,8 @@ string YulUtilFunctions::overflowCheckedIntAddFunction(IntegerType const& _type)
|
|||||||
return
|
return
|
||||||
Whiskers(R"(
|
Whiskers(R"(
|
||||||
function <functionName>(x, y) -> sum {
|
function <functionName>(x, y) -> sum {
|
||||||
|
x := <cleanupFunction>(x)
|
||||||
|
y := <cleanupFunction>(y)
|
||||||
<?signed>
|
<?signed>
|
||||||
// overflow, if x >= 0 and y > (maxValue - x)
|
// overflow, if x >= 0 and y > (maxValue - x)
|
||||||
if and(iszero(slt(x, 0)), sgt(y, sub(<maxValue>, x))) { revert(0, 0) }
|
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())
|
("signed", _type.isSigned())
|
||||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -421,6 +424,8 @@ string YulUtilFunctions::overflowCheckedIntMulFunction(IntegerType const& _type)
|
|||||||
// Multiplication by zero could be treated separately and directly return zero.
|
// Multiplication by zero could be treated separately and directly return zero.
|
||||||
Whiskers(R"(
|
Whiskers(R"(
|
||||||
function <functionName>(x, y) -> product {
|
function <functionName>(x, y) -> product {
|
||||||
|
x := <cleanupFunction>(x)
|
||||||
|
y := <cleanupFunction>(y)
|
||||||
<?signed>
|
<?signed>
|
||||||
// overflow, if x > 0, y > 0 and x > (maxValue / y)
|
// 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) }
|
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())
|
("signed", _type.isSigned())
|
||||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -452,6 +458,8 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
|||||||
return
|
return
|
||||||
Whiskers(R"(
|
Whiskers(R"(
|
||||||
function <functionName>(x, y) -> r {
|
function <functionName>(x, y) -> r {
|
||||||
|
x := <cleanupFunction>(x)
|
||||||
|
y := <cleanupFunction>(y)
|
||||||
if iszero(y) { revert(0, 0) }
|
if iszero(y) { revert(0, 0) }
|
||||||
<?signed>
|
<?signed>
|
||||||
// overflow for minVal / -1
|
// overflow for minVal / -1
|
||||||
@ -466,6 +474,7 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
|||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("signed", _type.isSigned())
|
("signed", _type.isSigned())
|
||||||
("minVal", toCompactHexWithPrefix(u256(_type.minValue())))
|
("minVal", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -477,12 +486,15 @@ string YulUtilFunctions::checkedIntModFunction(IntegerType const& _type)
|
|||||||
return
|
return
|
||||||
Whiskers(R"(
|
Whiskers(R"(
|
||||||
function <functionName>(x, y) -> r {
|
function <functionName>(x, y) -> r {
|
||||||
|
x := <cleanupFunction>(x)
|
||||||
|
y := <cleanupFunction>(y)
|
||||||
if iszero(y) { revert(0, 0) }
|
if iszero(y) { revert(0, 0) }
|
||||||
r := <?signed>s</signed>mod(x, y)
|
r := <?signed>s</signed>mod(x, y)
|
||||||
}
|
}
|
||||||
)")
|
)")
|
||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("signed", _type.isSigned())
|
("signed", _type.isSigned())
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -494,6 +506,8 @@ string YulUtilFunctions::overflowCheckedIntSubFunction(IntegerType const& _type)
|
|||||||
return
|
return
|
||||||
Whiskers(R"(
|
Whiskers(R"(
|
||||||
function <functionName>(x, y) -> diff {
|
function <functionName>(x, y) -> diff {
|
||||||
|
x := <cleanupFunction>(x)
|
||||||
|
y := <cleanupFunction>(y)
|
||||||
<?signed>
|
<?signed>
|
||||||
// underflow, if y >= 0 and x < (minValue + y)
|
// underflow, if y >= 0 and x < (minValue + y)
|
||||||
if and(iszero(slt(y, 0)), slt(x, add(<minValue>, y))) { revert(0, 0) }
|
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())
|
("signed", _type.isSigned())
|
||||||
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
("maxValue", toCompactHexWithPrefix(u256(_type.maxValue())))
|
||||||
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
("minValue", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -1938,6 +1953,7 @@ std::string YulUtilFunctions::decrementCheckedFunction(Type const& _type)
|
|||||||
|
|
||||||
return Whiskers(R"(
|
return Whiskers(R"(
|
||||||
function <functionName>(value) -> ret {
|
function <functionName>(value) -> ret {
|
||||||
|
value := <cleanupFunction>(value)
|
||||||
if <lt>(value, <minval>) { revert(0,0) }
|
if <lt>(value, <minval>) { revert(0,0) }
|
||||||
ret := sub(value, 1)
|
ret := sub(value, 1)
|
||||||
}
|
}
|
||||||
@ -1945,6 +1961,7 @@ std::string YulUtilFunctions::decrementCheckedFunction(Type const& _type)
|
|||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("minval", toCompactHexWithPrefix(minintval))
|
("minval", toCompactHexWithPrefix(minintval))
|
||||||
("lt", type.isSigned() ? "slt" : "lt")
|
("lt", type.isSigned() ? "slt" : "lt")
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -1966,6 +1983,7 @@ std::string YulUtilFunctions::incrementCheckedFunction(Type const& _type)
|
|||||||
|
|
||||||
return Whiskers(R"(
|
return Whiskers(R"(
|
||||||
function <functionName>(value) -> ret {
|
function <functionName>(value) -> ret {
|
||||||
|
value := <cleanupFunction>(value)
|
||||||
if <gt>(value, <maxval>) { revert(0,0) }
|
if <gt>(value, <maxval>) { revert(0,0) }
|
||||||
ret := add(value, 1)
|
ret := add(value, 1)
|
||||||
}
|
}
|
||||||
@ -1973,6 +1991,7 @@ std::string YulUtilFunctions::incrementCheckedFunction(Type const& _type)
|
|||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("maxval", toCompactHexWithPrefix(maxintval))
|
("maxval", toCompactHexWithPrefix(maxintval))
|
||||||
("gt", type.isSigned() ? "sgt" : "gt")
|
("gt", type.isSigned() ? "sgt" : "gt")
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -1988,13 +2007,15 @@ string YulUtilFunctions::negateNumberCheckedFunction(Type const& _type)
|
|||||||
|
|
||||||
return m_functionCollector.createFunction(functionName, [&]() {
|
return m_functionCollector.createFunction(functionName, [&]() {
|
||||||
return Whiskers(R"(
|
return Whiskers(R"(
|
||||||
function <functionName>(_value) -> ret {
|
function <functionName>(value) -> ret {
|
||||||
if slt(_value, <minval>) { revert(0,0) }
|
value := <cleanupFunction>(value)
|
||||||
ret := sub(0, _value)
|
if slt(value, <minval>) { revert(0,0) }
|
||||||
|
ret := sub(0, value)
|
||||||
}
|
}
|
||||||
)")
|
)")
|
||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("minval", toCompactHexWithPrefix(minintval))
|
("minval", toCompactHexWithPrefix(minintval))
|
||||||
|
("cleanupFunction", cleanupFunction(_type))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -496,9 +496,9 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
|
|||||||
isSigned = type->isSigned();
|
isSigned = type->isSigned();
|
||||||
|
|
||||||
string args =
|
string args =
|
||||||
expressionAsType(_binOp.leftExpression(), *commonType) +
|
expressionAsType(_binOp.leftExpression(), *commonType, true) +
|
||||||
", " +
|
", " +
|
||||||
expressionAsType(_binOp.rightExpression(), *commonType);
|
expressionAsType(_binOp.rightExpression(), *commonType, true);
|
||||||
|
|
||||||
string expr;
|
string expr;
|
||||||
if (op == Token::Equal)
|
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);
|
IRVariable from(_expression);
|
||||||
if (from.type() == _to)
|
if (from.type() == _to)
|
||||||
|
{
|
||||||
|
if (_forceCleanup)
|
||||||
|
return m_utils.cleanupFunction(_to) + "(" + from.commaSeparatedList() + ")";
|
||||||
|
else
|
||||||
return from.commaSeparatedList();
|
return from.commaSeparatedList();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return m_utils.conversionFunction(from.type(), _to) + "(" + from.commaSeparatedList() + ")";
|
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,
|
/// @returns a Yul expression representing the current value of @a _expression,
|
||||||
/// converted to type @a _to if it does not yet have that type.
|
/// 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
|
/// @returns an output stream that can be used to define @a _var using a function call or
|
||||||
/// single stack slot expression.
|
/// single stack slot expression.
|
||||||
|
Loading…
Reference in New Issue
Block a user