diff options
author | chriseth <chris@ethereum.org> | 2018-04-06 22:25:13 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2018-05-01 03:34:43 +0800 |
commit | 2968639406888d97bfae70e4adf41674ac60fd83 (patch) | |
tree | cabaf0aff5bfd64fb067b6b537534fc851bc1b41 /libsolidity/codegen | |
parent | 52c94418795f829c4a225fdf4742eec7a1961232 (diff) | |
download | dexon-solidity-2968639406888d97bfae70e4adf41674ac60fd83.tar.gz dexon-solidity-2968639406888d97bfae70e4adf41674ac60fd83.tar.zst dexon-solidity-2968639406888d97bfae70e4adf41674ac60fd83.zip |
Removed signed shift right from the utilities.
Diffstat (limited to 'libsolidity/codegen')
-rw-r--r-- | libsolidity/codegen/ABIFunctions.cpp | 22 | ||||
-rw-r--r-- | libsolidity/codegen/ABIFunctions.h | 2 | ||||
-rw-r--r-- | libsolidity/codegen/CompilerUtils.cpp | 18 | ||||
-rw-r--r-- | libsolidity/codegen/CompilerUtils.h | 2 | ||||
-rw-r--r-- | libsolidity/codegen/ExpressionCompiler.cpp | 2 | ||||
-rw-r--r-- | libsolidity/codegen/LValue.cpp | 2 |
6 files changed, 24 insertions, 24 deletions
diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index 6c4ddc02..3e3aa0ae 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -371,7 +371,7 @@ string ABIFunctions::conversionFunction(Type const& _from, Type const& _to) if (toCategory == Type::Category::Integer) body = Whiskers("converted := <convert>(<shift>(value))") - ("shift", shiftRightFunction(256 - from.numBytes() * 8, false)) + ("shift", shiftRightFunction(256 - from.numBytes() * 8)) ("convert", conversionFunction(IntegerType(from.numBytes() * 8), _to)) .render(); else @@ -458,8 +458,8 @@ string ABIFunctions::splitExternalFunctionIdFunction() } )") ("functionName", functionName) - ("shr32", shiftRightFunction(32, false)) - ("shr64", shiftRightFunction(64, false)) + ("shr32", shiftRightFunction(32)) + ("shr64", shiftRightFunction(64)) .render(); }); } @@ -831,7 +831,7 @@ string ABIFunctions::abiEncodingFunctionCompactStorageArray( templ("encodeToMemoryFun", encodeToMemoryFun); std::vector<std::map<std::string, std::string>> items(itemsPerSlot); for (size_t i = 0; i < itemsPerSlot; ++i) - items[i]["shiftRightFun"] = shiftRightFunction(i * storageBytes * 8, false); + items[i]["shiftRightFun"] = shiftRightFunction(i * storageBytes * 8); templ("items", items); return templ.render(); } @@ -927,7 +927,7 @@ string ABIFunctions::abiEncodingFunctionStruct( } else memberTempl("preprocess", ""); - memberTempl("retrieveValue", shiftRightFunction(intraSlotOffset * 8, false) + "(slotValue)"); + memberTempl("retrieveValue", shiftRightFunction(intraSlotOffset * 8) + "(slotValue)"); } else { @@ -1434,14 +1434,15 @@ string ABIFunctions::shiftLeftFunction(size_t _numBits) } } -string ABIFunctions::shiftRightFunction(size_t _numBits, bool _signed) +string ABIFunctions::shiftRightFunction(size_t _numBits) { solAssert(_numBits < 256, ""); - string functionName = "shift_right_" + to_string(_numBits) + (_signed ? "_signed" : "_unsigned"); + // Note that if this is extended with signed shifts, + // the opcodes SAR and SDIV behave differently with regards to rounding! - // NOTE: SAR rounds differently than SDIV - if (m_evmVersion.hasBitwiseShifting() && !_signed) + string functionName = "shift_right_" + to_string(_numBits) + "_unsigned"; + if (m_evmVersion.hasBitwiseShifting()) { return createFunction(functionName, [&]() { return @@ -1461,11 +1462,10 @@ string ABIFunctions::shiftRightFunction(size_t _numBits, bool _signed) return Whiskers(R"( function <functionName>(value) -> newValue { - newValue := <div>(value, <multiplier>) + newValue := div(value, <multiplier>) } )") ("functionName", functionName) - ("div", _signed ? "sdiv" : "div") ("multiplier", toCompactHexWithPrefix(u256(1) << _numBits)) .render(); }); diff --git a/libsolidity/codegen/ABIFunctions.h b/libsolidity/codegen/ABIFunctions.h index 41bb70b2..db4d40f5 100644 --- a/libsolidity/codegen/ABIFunctions.h +++ b/libsolidity/codegen/ABIFunctions.h @@ -195,7 +195,7 @@ private: std::string copyToMemoryFunction(bool _fromCalldata); std::string shiftLeftFunction(size_t _numBits); - std::string shiftRightFunction(size_t _numBits, bool _signed); + std::string shiftRightFunction(size_t _numBits); /// @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. std::string roundUpFunction(); diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 45ad1f47..48b77eb3 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -599,15 +599,15 @@ void CompilerUtils::splitExternalFunctionType(bool _leftAligned) if (_leftAligned) { m_context << Instruction::DUP1; - rightShiftNumberOnStack(64 + 32, false); + rightShiftNumberOnStack(64 + 32); // <input> <address> m_context << Instruction::SWAP1; - rightShiftNumberOnStack(64, false); + rightShiftNumberOnStack(64); } else { m_context << Instruction::DUP1; - rightShiftNumberOnStack(32, false); + rightShiftNumberOnStack(32); m_context << ((u256(1) << 160) - 1) << Instruction::AND << Instruction::SWAP1; } m_context << u256(0xffffffffUL) << Instruction::AND; @@ -675,7 +675,7 @@ void CompilerUtils::convertType( // conversion from bytes to integer. no need to clean the high bit // only to shift right because of opposite alignment IntegerType const& targetIntegerType = dynamic_cast<IntegerType const&>(_targetType); - rightShiftNumberOnStack(256 - typeOnStack.numBytes() * 8, false); + rightShiftNumberOnStack(256 - typeOnStack.numBytes() * 8); if (targetIntegerType.numBits() < typeOnStack.numBytes() * 8) convertType(IntegerType(typeOnStack.numBytes() * 8), _targetType, _cleanupNeeded); } @@ -1242,7 +1242,7 @@ unsigned CompilerUtils::loadFromMemoryHelper(Type const& _type, bool _fromCallda bool leftAligned = _type.category() == Type::Category::FixedBytes; // add leading or trailing zeros by dividing/multiplying depending on alignment int shiftFactor = (32 - numBytes) * 8; - rightShiftNumberOnStack(shiftFactor, false); + rightShiftNumberOnStack(shiftFactor); if (leftAligned) leftShiftNumberOnStack(shiftFactor); } @@ -1271,14 +1271,14 @@ void CompilerUtils::leftShiftNumberOnStack(unsigned _bits) m_context << (u256(1) << _bits) << Instruction::MUL; } -void CompilerUtils::rightShiftNumberOnStack(unsigned _bits, bool _isSigned) +void CompilerUtils::rightShiftNumberOnStack(unsigned _bits) { solAssert(_bits < 256, ""); - // NOTE: SAR rounds differently than SDIV - if (m_context.evmVersion().hasBitwiseShifting() && !_isSigned) + // NOTE: If we add signed right shift, SAR rounds differently than SDIV + if (m_context.evmVersion().hasBitwiseShifting()) m_context << _bits << Instruction::SHR; else - m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); + m_context << (u256(1) << _bits) << Instruction::SWAP1 << Instruction::DIV; } unsigned CompilerUtils::prepareMemoryStore(Type const& _type, bool _padToWords) diff --git a/libsolidity/codegen/CompilerUtils.h b/libsolidity/codegen/CompilerUtils.h index 476a7559..8e3a8a5d 100644 --- a/libsolidity/codegen/CompilerUtils.h +++ b/libsolidity/codegen/CompilerUtils.h @@ -254,7 +254,7 @@ public: /// Helper function to shift top value on the stack to the right. /// Stack pre: <value> <shift_by_bits> /// Stack post: <shifted_value> - void rightShiftNumberOnStack(unsigned _bits, bool _isSigned = false); + void rightShiftNumberOnStack(unsigned _bits); /// Appends code that computes tha Keccak-256 hash of the topmost stack element of 32 byte type. void computeHashStatic(); diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 019867c5..a8222e21 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -548,7 +548,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) if (m_context.runtimeContext()) // We have a runtime context, so we need the creation part. - utils().rightShiftNumberOnStack(32, false); + utils().rightShiftNumberOnStack(32); else // Extract the runtime part. m_context << ((u256(1) << 32) - 1) << Instruction::AND; diff --git a/libsolidity/codegen/LValue.cpp b/libsolidity/codegen/LValue.cpp index e19cf41e..77684683 100644 --- a/libsolidity/codegen/LValue.cpp +++ b/libsolidity/codegen/LValue.cpp @@ -267,7 +267,7 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc else if (m_dataType->category() == Type::Category::FixedBytes) { solAssert(_sourceType.category() == Type::Category::FixedBytes, "source not fixed bytes"); - CompilerUtils(m_context).rightShiftNumberOnStack(256 - 8 * dynamic_cast<FixedBytesType const&>(*m_dataType).numBytes(), false); + CompilerUtils(m_context).rightShiftNumberOnStack(256 - 8 * dynamic_cast<FixedBytesType const&>(*m_dataType).numBytes()); } else { |