diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index e4eaaf5e8482..b2d9571d9ded 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -2295,14 +2295,13 @@ void IRGeneratorForStatements::endVisit(IndexAccess const& _indexAccess) } case DataLocation::Memory: { - string indexAccessFunction = m_utils.memoryArrayIndexAccessFunction(arrayType); - string const memAddress = - indexAccessFunction + - "(" + - IRVariable(_indexAccess.baseExpression()).part("mpos").name() + - ", " + - expressionAsType(*_indexAccess.indexExpression(), *TypeProvider::uint256()) + - ")"; + string const indexAccessFunction = m_utils.memoryArrayIndexAccessFunction(arrayType); + string const baseRef = IRVariable(_indexAccess.baseExpression()).part("mpos").name(); + string const indexExpression = expressionAsType( + *_indexAccess.indexExpression(), + *TypeProvider::uint256() + ); + string const memAddress = indexAccessFunction + "(" + baseRef + ", " + indexExpression + ")"; setLValue(_indexAccess, IRLValue{ *arrayType.baseType(), @@ -2312,28 +2311,28 @@ void IRGeneratorForStatements::endVisit(IndexAccess const& _indexAccess) } case DataLocation::CallData: { - string indexAccessFunction = m_utils.calldataArrayIndexAccessFunction(arrayType); - string const indexAccessFunctionCall = - indexAccessFunction + - "(" + - IRVariable(_indexAccess.baseExpression()).commaSeparatedList() + - ", " + - expressionAsType(*_indexAccess.indexExpression(), *TypeProvider::uint256()) + - ")"; + string const indexAccessFunction = m_utils.calldataArrayIndexAccessFunction(arrayType); + string const baseRef = IRVariable(_indexAccess.baseExpression()).commaSeparatedList(); + string const indexExpression = expressionAsType( + *_indexAccess.indexExpression(), + *TypeProvider::uint256() + ); + string const calldataAddress = indexAccessFunction + "(" + baseRef + ", " + indexExpression + ")"; + if (arrayType.isByteArrayOrString()) define(_indexAccess) << m_utils.cleanupFunction(*arrayType.baseType()) << "(calldataload(" << - indexAccessFunctionCall << + calldataAddress << "))\n"; else if (arrayType.baseType()->isValueType()) define(_indexAccess) << m_utils.readFromCalldata(*arrayType.baseType()) << "(" << - indexAccessFunctionCall << + calldataAddress << ")\n"; else - define(_indexAccess) << indexAccessFunctionCall << "\n"; + define(_indexAccess) << calldataAddress << "\n"; break; } }