Skip to content

Commit f4e0270

Browse files
authored
Merge pull request #12605 from nishant-sachdeva/indexed_log_topic_differs_between_legacy_and_ir_if_explicitly_downcast
Code generators needed fixing of the cleanup process during typecasting of bytes and integers
2 parents e87d959 + e2711b7 commit f4e0270

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

libsolidity/codegen/ir/IRGeneratorForStatements.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp)
805805
if (auto type = dynamic_cast<IntegerType const*>(commonType))
806806
isSigned = type->isSigned();
807807

808-
string args = expressionAsType(_binOp.leftExpression(), *commonType, true);
809-
args += ", " + expressionAsType(_binOp.rightExpression(), *commonType, true);
808+
string args = expressionAsCleanedType(_binOp.leftExpression(), *commonType);
809+
args += ", " + expressionAsCleanedType(_binOp.rightExpression(), *commonType);
810810

811811
auto functionType = dynamic_cast<FunctionType const*>(commonType);
812812
solAssert(functionType ? (op == Token::Equal || op == Token::NotEqual) : true, "Invalid function pointer comparison!");
@@ -1037,7 +1037,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
10371037
else
10381038
{
10391039
solAssert(parameterTypes[i]->sizeOnStack() == 1, "");
1040-
indexedArgs.emplace_back(convert(arg, *paramTypes[i], true));
1040+
indexedArgs.emplace_back(convertAndCleanup(arg, *parameterTypes[i]));
10411041
}
10421042
}
10431043
else
@@ -2727,32 +2727,43 @@ void IRGeneratorForStatements::assignInternalFunctionIDIfNotCalledDirectly(
27272727
m_context.addToInternalDispatch(_referencedFunction);
27282728
}
27292729

2730-
IRVariable IRGeneratorForStatements::convert(IRVariable const& _from, Type const& _to, bool _forceCleanup)
2730+
IRVariable IRGeneratorForStatements::convert(IRVariable const& _from, Type const& _to)
27312731
{
2732-
if (_from.type() == _to && !_forceCleanup)
2732+
if (_from.type() == _to)
27332733
return _from;
27342734
else
27352735
{
27362736
IRVariable converted(m_context.newYulVariable(), _to);
2737-
define(converted, _from, _forceCleanup);
2737+
define(converted, _from);
27382738
return converted;
27392739
}
27402740
}
27412741

2742-
std::string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to, bool _forceCleanup)
2742+
IRVariable IRGeneratorForStatements::convertAndCleanup(IRVariable const& _from, Type const& _to)
2743+
{
2744+
IRVariable converted(m_context.newYulVariable(), _to);
2745+
defineAndCleanup(converted, _from);
2746+
return converted;
2747+
}
2748+
2749+
std::string IRGeneratorForStatements::expressionAsType(Expression const& _expression, Type const& _to)
27432750
{
27442751
IRVariable from(_expression);
27452752
if (from.type() == _to)
2746-
{
2747-
if (_forceCleanup)
2748-
return m_utils.cleanupFunction(_to) + "(" + from.commaSeparatedList() + ")";
2749-
else
2750-
return from.commaSeparatedList();
2751-
}
2753+
return from.commaSeparatedList();
27522754
else
27532755
return m_utils.conversionFunction(from.type(), _to) + "(" + from.commaSeparatedList() + ")";
27542756
}
27552757

2758+
std::string IRGeneratorForStatements::expressionAsCleanedType(Expression const& _expression, Type const& _to)
2759+
{
2760+
IRVariable from(_expression);
2761+
if (from.type() == _to)
2762+
return m_utils.cleanupFunction(_to) + "(" + expressionAsType(_expression, _to) + ")";
2763+
else
2764+
return expressionAsType(_expression, _to) ;
2765+
}
2766+
27562767
std::ostream& IRGeneratorForStatements::define(IRVariable const& _var)
27572768
{
27582769
if (_var.type().sizeOnStack() > 0)

libsolidity/codegen/ir/IRGeneratorForStatements.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,19 @@ class IRGeneratorForStatements: public IRGeneratorForStatementsBase
8686
IRVariable evaluateExpression(Expression const& _expression, Type const& _to);
8787

8888
/// Defines @a _var using the value of @a _value while performing type conversions, if required.
89-
/// If @a _forceCleanup is set to true, it also cleans the value of the variable after the conversion.
90-
void define(IRVariable const& _var, IRVariable const& _value, bool _forceCleanup = false)
89+
void define(IRVariable const& _var, IRVariable const& _value)
9190
{
92-
declareAssign(_var, _value, true, _forceCleanup);
91+
bool _declare = true;
92+
declareAssign(_var, _value, _declare);
93+
}
94+
95+
/// Defines @a _var using the value of @a _value while performing type conversions, if required.
96+
/// It also cleans the value of the variable.
97+
void defineAndCleanup(IRVariable const& _var, IRVariable const& _value)
98+
{
99+
bool _forceCleanup = true;
100+
bool _declare = true;
101+
declareAssign(_var, _value, _declare, _forceCleanup);
93102
}
94103

95104
/// @returns the name of a function that computes the value of the given constant
@@ -166,13 +175,20 @@ class IRGeneratorForStatements: public IRGeneratorForStatementsBase
166175
);
167176

168177
/// Generates the required conversion code and @returns an IRVariable referring to the value of @a _variable
169-
/// If @a _forceCleanup is set to true, it also cleans the value of the variable after the conversion.
170-
IRVariable convert(IRVariable const& _variable, Type const& _to, bool _forceCleanup = false);
178+
IRVariable convert(IRVariable const& _variable, Type const& _to);
179+
180+
/// Generates the required conversion code and @returns an IRVariable referring to the value of @a _variable
181+
/// It also cleans the value of the variable.
182+
IRVariable convertAndCleanup(IRVariable const& _from, Type const& _to);
183+
184+
/// @returns a Yul expression representing the current value of @a _expression,
185+
/// converted to type @a _to if it does not yet have that type.
186+
std::string expressionAsType(Expression const& _expression, Type const& _to);
171187

172188
/// @returns a Yul expression representing the current value of @a _expression,
173189
/// converted to type @a _to if it does not yet have that type.
174-
/// If @a _forceCleanup is set to true, it also cleans the value, in case it already has type @a _to.
175-
std::string expressionAsType(Expression const& _expression, Type const& _to, bool _forceCleanup = false);
190+
/// It also cleans the value, in case it already has type @a _to.
191+
std::string expressionAsCleanedType(Expression const& _expression, Type const& _to);
176192

177193
/// @returns an output stream that can be used to define @a _var using a function call or
178194
/// single stack slot expression.

0 commit comments

Comments
 (0)