[Sol->Yul] Style fixes

addresses the points @chriseth mentioned in #6909
This commit is contained in:
Mathias Baumann 2019-06-17 15:26:40 +02:00
parent d774e30686
commit 8ca27c2bb0
4 changed files with 29 additions and 26 deletions

View File

@ -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 m_functionCollector->createFunction(functionName, [&]() {
return return
Whiskers(R"( 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, // Note that if this is extended with signed shifts,
// the opcodes SAR and SDIV behave differently with regards to rounding! // 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 m_functionCollector->createFunction(functionName, [&]() {
return return
Whiskers(R"( 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, ""); solAssert(_numBytes <= 32, "");
size_t numBits = _numBytes * 8; 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 m_functionCollector->createFunction(functionName, [&]() {
return return
Whiskers(R"( Whiskers(R"(
@ -359,7 +359,7 @@ string YulUtilFunctions::dynamicUpdateByteSliceFunction(size_t _numBytes)
)") )")
("functionName", functionName) ("functionName", functionName)
("mask", formatNumber((bigint(1) << numBits) - 1)) ("mask", formatNumber((bigint(1) << numBits) - 1))
("shl", dynamicShiftLeftFunction()) ("shl", shiftLeftFunctionDynamic())
.render(); .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, ""); solUnimplementedAssert(!_splitFunctionTypes, "");
string functionName = string functionName =
"read_from_storage_" + "read_from_storage_dynamic" +
string(_splitFunctionTypes ? "split_" : "") + string(_splitFunctionTypes ? "split_" : "") +
"_" + "_" +
_type.identifier(); _type.identifier();
@ -687,7 +687,7 @@ string YulUtilFunctions::dynamicReadFromStorage(Type const& _type, bool _splitFu
} }
)") )")
("functionName", functionName) ("functionName", functionName)
("extract", dynamicExtractFromStorageValue(_type, false)) ("extract", extractFromStorageValueDynamic(_type, _splitFunctionTypes))
.render(); .render();
}); });
} }
@ -715,7 +715,7 @@ string YulUtilFunctions::updateStorageValueFunction(Type const& _type, boost::op
("update", ("update",
_offset.is_initialized() ? _offset.is_initialized() ?
updateByteSliceFunction(_type.storageBytes(), *_offset) : updateByteSliceFunction(_type.storageBytes(), *_offset) :
dynamicUpdateByteSliceFunction(_type.storageBytes()) updateByteSliceFunctionDynamic(_type.storageBytes())
) )
("offset", _offset.is_initialized() ? "" : "offset, ") ("offset", _offset.is_initialized() ? "" : "offset, ")
("prepare", prepareStoreFunction(_type)) ("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, ""); solUnimplementedAssert(!_splitFunctionTypes, "");
string functionName = string functionName =
"extract_from_storage_value_" + "extract_from_storage_value_dynamic" +
string(_splitFunctionTypes ? "split_" : "") + string(_splitFunctionTypes ? "split_" : "") +
_type.identifier(); _type.identifier();
return m_functionCollector->createFunction(functionName, [&] { return m_functionCollector->createFunction(functionName, [&] {
@ -748,8 +748,8 @@ string YulUtilFunctions::dynamicExtractFromStorageValue(Type const& _type, bool
} }
)") )")
("functionName", functionName) ("functionName", functionName)
("shr", dynamicShiftRightFunction()) ("shr", shiftRightFunctionDynamic())
("cleanupStorage", cleanupFromStorageFunction(_type, false)) ("cleanupStorage", cleanupFromStorageFunction(_type, _splitFunctionTypes))
.render(); .render();
}); });
} }
@ -772,7 +772,7 @@ string YulUtilFunctions::extractFromStorageValue(Type const& _type, size_t _offs
)") )")
("functionName", functionName) ("functionName", functionName)
("shr", shiftRightFunction(_offset * 8)) ("shr", shiftRightFunction(_offset * 8))
("cleanupStorage", cleanupFromStorageFunction(_type, false)) ("cleanupStorage", cleanupFromStorageFunction(_type, _splitFunctionTypes))
.render(); .render();
}); });
} }

View File

@ -74,9 +74,9 @@ public:
std::string leftAlignFunction(Type const& _type); std::string leftAlignFunction(Type const& _type);
std::string shiftLeftFunction(size_t _numBits); std::string shiftLeftFunction(size_t _numBits);
std::string dynamicShiftLeftFunction(); std::string shiftLeftFunctionDynamic();
std::string shiftRightFunction(size_t _numBits); 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 /// @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 /// _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); std::string updateByteSliceFunction(size_t _numBytes, size_t _shiftBytes);
/// signature: (value, shiftBytes, toInsert) -> result /// 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 /// @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. /// 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 /// @param _splitFunctionTypes if false, returns the address and function signature in a
/// single variable. /// single variable.
std::string readFromStorage(Type const& _type, size_t _offset, bool _splitFunctionTypes); 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 /// @returns a function that extracts a value type from storage slot that has been
/// retrieved already. /// retrieved already.
@ -134,7 +134,7 @@ public:
/// @param _splitFunctionTypes if false, returns the address and function signature in a /// @param _splitFunctionTypes if false, returns the address and function signature in a
/// single variable. /// single variable.
std::string extractFromStorageValue(Type const& _type, size_t _offset, bool _splitFunctionTypes); 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 /// 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 /// the specified slot and offset. If offset is not given, it is expected as

View File

@ -54,8 +54,8 @@ string IRLocalVariable::setToZero() const
IRStorageItem::IRStorageItem( IRStorageItem::IRStorageItem(
IRGenerationContext& _context, IRGenerationContext& _context,
VariableDeclaration const& _varDecl VariableDeclaration const& _varDecl
) ):
:IRStorageItem( IRStorageItem(
_context, _context,
*_varDecl.annotation().type, *_varDecl.annotation().type,
_context.storageLocationOfVariable(_varDecl) _context.storageLocationOfVariable(_varDecl)
@ -66,8 +66,8 @@ IRStorageItem::IRStorageItem(
IRGenerationContext& _context, IRGenerationContext& _context,
Type const& _type, Type const& _type,
std::pair<u256, unsigned> slot_offset std::pair<u256, unsigned> slot_offset
) ):
: IRLValue(_context, &_type), IRLValue(_context, &_type),
m_slot(toCompactHexWithPrefix(slot_offset.first)), m_slot(toCompactHexWithPrefix(slot_offset.first)),
m_offset(slot_offset.second) m_offset(slot_offset.second)
{ {
@ -94,7 +94,7 @@ string IRStorageItem::retrieveValue() const
solUnimplementedAssert(m_type->category() != Type::Category::Function, ""); solUnimplementedAssert(m_type->category() != Type::Category::Function, "");
if (m_offset.type() == typeid(string)) if (m_offset.type() == typeid(string))
return return
m_context.utils().dynamicReadFromStorage(*m_type, false) + m_context.utils().readFromStorageDynamic(*m_type, false) +
"(" + "(" +
m_slot + m_slot +
", " + ", " +

View File

@ -101,6 +101,9 @@ private:
); );
std::string const m_slot; 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<std::string, unsigned> const m_offset; boost::variant<std::string, unsigned> const m_offset;
}; };