From 8ca27c2bb0a862439db36937cdbbd2dd755c862c Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Mon, 17 Jun 2019 15:26:40 +0200 Subject: [PATCH] [Sol->Yul] Style fixes addresses the points @chriseth mentioned in #6909 --- libsolidity/codegen/YulUtilFunctions.cpp | 32 ++++++++++++------------ libsolidity/codegen/YulUtilFunctions.h | 10 ++++---- libsolidity/codegen/ir/IRLValue.cpp | 10 ++++---- libsolidity/codegen/ir/IRLValue.h | 3 +++ 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/libsolidity/codegen/YulUtilFunctions.cpp b/libsolidity/codegen/YulUtilFunctions.cpp index 65991f864..7724dd57e 100644 --- a/libsolidity/codegen/YulUtilFunctions.cpp +++ b/libsolidity/codegen/YulUtilFunctions.cpp @@ -244,9 +244,9 @@ string YulUtilFunctions::shiftLeftFunction(size_t _numBits) }); } -string YulUtilFunctions::dynamicShiftLeftFunction() +string YulUtilFunctions::shiftLeftFunctionDynamic() { - string functionName = "shift_left"; + string functionName = "shift_left_dynamic"; return m_functionCollector->createFunction(functionName, [&]() { return Whiskers(R"( @@ -293,12 +293,12 @@ string YulUtilFunctions::shiftRightFunction(size_t _numBits) }); } -string YulUtilFunctions::dynamicShiftRightFunction() +string YulUtilFunctions::shiftRightFunctionDynamic() { // Note that if this is extended with signed shifts, // the opcodes SAR and SDIV behave differently with regards to rounding! - string const functionName = "shift_right_unsigned"; + string const functionName = "shift_right_unsigned_dynamic"; return m_functionCollector->createFunction(functionName, [&]() { return Whiskers(R"( @@ -341,11 +341,11 @@ string YulUtilFunctions::updateByteSliceFunction(size_t _numBytes, size_t _shift }); } -string YulUtilFunctions::dynamicUpdateByteSliceFunction(size_t _numBytes) +string YulUtilFunctions::updateByteSliceFunctionDynamic(size_t _numBytes) { solAssert(_numBytes <= 32, ""); size_t numBits = _numBytes * 8; - string functionName = "update_byte_slice_" + to_string(_numBytes); + string functionName = "update_byte_slice_dynamic" + to_string(_numBytes); return m_functionCollector->createFunction(functionName, [&]() { return Whiskers(R"( @@ -359,7 +359,7 @@ string YulUtilFunctions::dynamicUpdateByteSliceFunction(size_t _numBytes) )") ("functionName", functionName) ("mask", formatNumber((bigint(1) << numBits) - 1)) - ("shl", dynamicShiftLeftFunction()) + ("shl", shiftLeftFunctionDynamic()) .render(); }); } @@ -671,11 +671,11 @@ string YulUtilFunctions::readFromStorage(Type const& _type, size_t _offset, bool }); } -string YulUtilFunctions::dynamicReadFromStorage(Type const& _type, bool _splitFunctionTypes) +string YulUtilFunctions::readFromStorageDynamic(Type const& _type, bool _splitFunctionTypes) { solUnimplementedAssert(!_splitFunctionTypes, ""); string functionName = - "read_from_storage_" + + "read_from_storage_dynamic" + string(_splitFunctionTypes ? "split_" : "") + "_" + _type.identifier(); @@ -687,7 +687,7 @@ string YulUtilFunctions::dynamicReadFromStorage(Type const& _type, bool _splitFu } )") ("functionName", functionName) - ("extract", dynamicExtractFromStorageValue(_type, false)) + ("extract", extractFromStorageValueDynamic(_type, _splitFunctionTypes)) .render(); }); } @@ -715,7 +715,7 @@ string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::op ("update", _offset.is_initialized() ? updateByteSliceFunction(_type.storageBytes(), *_offset) : - dynamicUpdateByteSliceFunction(_type.storageBytes()) + updateByteSliceFunctionDynamic(_type.storageBytes()) ) ("offset", _offset.is_initialized() ? "" : "offset, ") ("prepare", prepareStoreFunction(_type)) @@ -733,12 +733,12 @@ string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::op }); } -string YulUtilFunctions::dynamicExtractFromStorageValue(Type const& _type, bool _splitFunctionTypes) +string YulUtilFunctions::extractFromStorageValueDynamic(Type const& _type, bool _splitFunctionTypes) { solUnimplementedAssert(!_splitFunctionTypes, ""); string functionName = - "extract_from_storage_value_" + + "extract_from_storage_value_dynamic" + string(_splitFunctionTypes ? "split_" : "") + _type.identifier(); return m_functionCollector->createFunction(functionName, [&] { @@ -748,8 +748,8 @@ string YulUtilFunctions::dynamicExtractFromStorageValue(Type const& _type, bool } )") ("functionName", functionName) - ("shr", dynamicShiftRightFunction()) - ("cleanupStorage", cleanupFromStorageFunction(_type, false)) + ("shr", shiftRightFunctionDynamic()) + ("cleanupStorage", cleanupFromStorageFunction(_type, _splitFunctionTypes)) .render(); }); } @@ -772,7 +772,7 @@ string YulUtilFunctions::extractFromStorageValue(Type const& _type, size_t _offs )") ("functionName", functionName) ("shr", shiftRightFunction(_offset * 8)) - ("cleanupStorage", cleanupFromStorageFunction(_type, false)) + ("cleanupStorage", cleanupFromStorageFunction(_type, _splitFunctionTypes)) .render(); }); } diff --git a/libsolidity/codegen/YulUtilFunctions.h b/libsolidity/codegen/YulUtilFunctions.h index b4e0dc727..9b512f767 100644 --- a/libsolidity/codegen/YulUtilFunctions.h +++ b/libsolidity/codegen/YulUtilFunctions.h @@ -74,9 +74,9 @@ public: std::string leftAlignFunction(Type const& _type); std::string shiftLeftFunction(size_t _numBits); - std::string dynamicShiftLeftFunction(); + std::string shiftLeftFunctionDynamic(); std::string shiftRightFunction(size_t _numBits); - std::string dynamicShiftRightFunction(); + std::string shiftRightFunctionDynamic(); /// @returns the name of a function f(value, toInsert) -> newValue which replaces the /// _numBytes bytes starting at byte position _shiftBytes (counted from the least significant @@ -84,7 +84,7 @@ public: std::string updateByteSliceFunction(size_t _numBytes, size_t _shiftBytes); /// signature: (value, shiftBytes, toInsert) -> result - std::string dynamicUpdateByteSliceFunction(size_t _numBytes); + std::string updateByteSliceFunctionDynamic(size_t _numBytes); /// @returns the name of a function that rounds its input to the next multiple /// of 32 or the input if it is a multiple of 32. @@ -126,7 +126,7 @@ public: /// @param _splitFunctionTypes if false, returns the address and function signature in a /// single variable. std::string readFromStorage(Type const& _type, size_t _offset, bool _splitFunctionTypes); - std::string dynamicReadFromStorage(Type const& _type, bool _splitFunctionTypes); + std::string readFromStorageDynamic(Type const& _type, bool _splitFunctionTypes); /// @returns a function that extracts a value type from storage slot that has been /// retrieved already. @@ -134,7 +134,7 @@ public: /// @param _splitFunctionTypes if false, returns the address and function signature in a /// single variable. std::string extractFromStorageValue(Type const& _type, size_t _offset, bool _splitFunctionTypes); - std::string dynamicExtractFromStorageValue(Type const& _type, bool _splitFunctionTypes); + std::string extractFromStorageValueDynamic(Type const& _type, bool _splitFunctionTypes); /// Returns the name of a function will write the given value to /// the specified slot and offset. If offset is not given, it is expected as diff --git a/libsolidity/codegen/ir/IRLValue.cpp b/libsolidity/codegen/ir/IRLValue.cpp index ff8e4bb13..defabd081 100644 --- a/libsolidity/codegen/ir/IRLValue.cpp +++ b/libsolidity/codegen/ir/IRLValue.cpp @@ -54,8 +54,8 @@ string IRLocalVariable::setToZero() const IRStorageItem::IRStorageItem( IRGenerationContext& _context, VariableDeclaration const& _varDecl -) -:IRStorageItem( +): + IRStorageItem( _context, *_varDecl.annotation().type, _context.storageLocationOfVariable(_varDecl) @@ -66,8 +66,8 @@ IRStorageItem::IRStorageItem( IRGenerationContext& _context, Type const& _type, std::pair slot_offset -) -: IRLValue(_context, &_type), +): + IRLValue(_context, &_type), m_slot(toCompactHexWithPrefix(slot_offset.first)), m_offset(slot_offset.second) { @@ -94,7 +94,7 @@ string IRStorageItem::retrieveValue() const solUnimplementedAssert(m_type->category() != Type::Category::Function, ""); if (m_offset.type() == typeid(string)) return - m_context.utils().dynamicReadFromStorage(*m_type, false) + + m_context.utils().readFromStorageDynamic(*m_type, false) + "(" + m_slot + ", " + diff --git a/libsolidity/codegen/ir/IRLValue.h b/libsolidity/codegen/ir/IRLValue.h index 803223fe7..3c9338ebd 100644 --- a/libsolidity/codegen/ir/IRLValue.h +++ b/libsolidity/codegen/ir/IRLValue.h @@ -101,6 +101,9 @@ private: ); std::string const m_slot; + /// unsigned: Used when the offset is known at compile time, uses optimized + /// functions + /// string: Used when the offset is determined at run time boost::variant const m_offset; };