From 0b5671c885331e2ab33c1386ba16e0118f2e65b3 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 8 Sep 2021 12:18:00 +0200 Subject: [PATCH] Clarify C++ evaluation order. --- .../codegen/ir/IRGeneratorForStatements.cpp | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index 8f281a10e..48e381e31 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -798,10 +798,8 @@ bool IRGeneratorForStatements::visit(BinaryOperation const& _binOp) if (auto type = dynamic_cast(commonType)) isSigned = type->isSigned(); - string args = - expressionAsType(_binOp.leftExpression(), *commonType, true) + - ", " + - expressionAsType(_binOp.rightExpression(), *commonType, true); + string args = expressionAsType(_binOp.leftExpression(), *commonType, true); + args += ", " + expressionAsType(_binOp.rightExpression(), *commonType, true); string expr; if (op == Token::Equal) @@ -1115,16 +1113,14 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall) IRVariable array = convert(*arguments[0], *TypeProvider::bytesMemory()); IRVariable hashVariable(m_context.newYulVariable(), *TypeProvider::fixedBytes(32)); + string dataAreaFunction = m_utils.arrayDataAreaFunction(*TypeProvider::bytesMemory()); + string arrayLengthFunction = m_utils.arrayLengthFunction(*TypeProvider::bytesMemory()); define(hashVariable) << "keccak256(" << - m_utils.arrayDataAreaFunction(*TypeProvider::bytesMemory()) << - "(" << - array.commaSeparatedList() << - "), " << - m_utils.arrayLengthFunction(*TypeProvider::bytesMemory()) << - "(" << - array.commaSeparatedList() << - "))\n"; + (dataAreaFunction + "(" + array.commaSeparatedList() + ")") << + ", " << + (arrayLengthFunction + "(" + array.commaSeparatedList() +")") << + ")\n"; IRVariable selectorVariable(m_context.newYulVariable(), *TypeProvider::fixedBytes(4)); define(selectorVariable, hashVariable); selector = selectorVariable.name(); @@ -1249,16 +1245,14 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall) { auto array = convert(*arguments[0], *arrayType); + string dataAreaFunction = m_utils.arrayDataAreaFunction(*arrayType); + string arrayLengthFunction = m_utils.arrayLengthFunction(*arrayType); define(_functionCall) << "keccak256(" << - m_utils.arrayDataAreaFunction(*arrayType) << - "(" << - array.commaSeparatedList() << - "), " << - m_utils.arrayLengthFunction(*arrayType) << - "(" << - array.commaSeparatedList() << - "))\n"; + (dataAreaFunction + "(" + array.commaSeparatedList() + ")") << + ", " << + (arrayLengthFunction + "(" + array.commaSeparatedList() +")") << + ")\n"; } break; } @@ -1646,11 +1640,14 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess) expressionAsType(_memberAccess.expression(), *TypeProvider::address()) << ")\n"; else if (member == "code") + { + string externalCodeFunction = m_utils.externalCodeFunction(); define(_memberAccess) << - m_utils.externalCodeFunction() << + externalCodeFunction << "(" << expressionAsType(_memberAccess.expression(), *TypeProvider::address()) << ")\n"; + } else if (member == "codehash") define(_memberAccess) << "extcodehash(" << @@ -1946,7 +1943,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess) assignInternalFunctionIDIfNotCalledDirectly(_memberAccess, resolvedFunctionDef); } else if (auto const* variable = dynamic_cast(_memberAccess.annotation().referencedDeclaration)) - handleVariableReference(*variable, _memberAccess); + handleVariableReference(*variable, _memberAccess); else if (memberFunctionType) { switch (memberFunctionType->kind()) @@ -2149,8 +2146,9 @@ void IRGeneratorForStatements::endVisit(IndexAccess const& _indexAccess) } case DataLocation::CallData: { + string indexAccessFunction = m_utils.calldataArrayIndexAccessFunction(arrayType); string const indexAccessFunctionCall = - m_utils.calldataArrayIndexAccessFunction(arrayType) + + indexAccessFunction + "(" + IRVariable(_indexAccess.baseExpression()).commaSeparatedList() + ", " + @@ -2867,13 +2865,16 @@ void IRGeneratorForStatements::writeToLValue(IRLValue const& _lvalue, IRVariable ")\n"; } else if (auto const* literalType = dynamic_cast(&_value.type())) + { + string writeUInt = m_utils.writeToMemoryFunction(*TypeProvider::uint256()); appendCode() << - m_utils.writeToMemoryFunction(*TypeProvider::uint256()) << + writeUInt << "(" << _memory.address << ", " << m_utils.copyLiteralToMemoryFunction(literalType->value()) + "()" << ")\n"; + } else { solAssert(_lvalue.type.sizeOnStack() == 1, ""); @@ -2949,11 +2950,14 @@ IRVariable IRGeneratorForStatements::readFromLValue(IRLValue const& _lvalue) solUnimplementedAssert(_lvalue.type.sizeOnStack() == 1, ""); solAssert(_lvalue.type == *_immutable.variable->type(), ""); if (m_context.executionContext() == IRGenerationContext::ExecutionContext::Creation) + { + string readFunction = m_utils.readFromMemory(*_immutable.variable->type()); define(result) << - m_utils.readFromMemory(*_immutable.variable->type()) << + readFunction << "(" << to_string(m_context.immutableMemoryOffset(*_immutable.variable)) << ")\n"; + } else define(result) << "loadimmutable(\"" << to_string(_immutable.variable->id()) << "\")\n"; },