diff options
author | Alex Beregszaszi <alex@rtfs.hu> | 2018-04-05 22:56:02 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2018-05-01 03:34:43 +0800 |
commit | c3608eaf90b49771b2785d86bb0c73dca6e61046 (patch) | |
tree | c21692e6b8175d84e7babb8a164eade88c29af23 | |
parent | 22bfd3da41ae9efa6e68e884f722502ab3adcf50 (diff) | |
download | dexon-solidity-c3608eaf90b49771b2785d86bb0c73dca6e61046.tar.gz dexon-solidity-c3608eaf90b49771b2785d86bb0c73dca6e61046.tar.zst dexon-solidity-c3608eaf90b49771b2785d86bb0c73dca6e61046.zip |
Use native shift instructions in ABIFunctions on Constantinople
-rw-r--r-- | libsolidity/codegen/ABIFunctions.cpp | 87 | ||||
-rw-r--r-- | libsolidity/codegen/ABIFunctions.h | 6 | ||||
-rw-r--r-- | libsolidity/codegen/CompilerContext.h | 3 |
3 files changed, 70 insertions, 26 deletions
diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index 8e890854..f6aa714d 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -1401,37 +1401,74 @@ string ABIFunctions::copyToMemoryFunction(bool _fromCalldata) string ABIFunctions::shiftLeftFunction(size_t _numBits) { + solAssert(_numBits < 256, ""); + string functionName = "shift_left_" + to_string(_numBits); - return createFunction(functionName, [&]() { - solAssert(_numBits < 256, ""); - return - Whiskers(R"( - function <functionName>(value) -> newValue { - newValue := mul(value, <multiplier>) - } - )") - ("functionName", functionName) - ("multiplier", toCompactHexWithPrefix(u256(1) << _numBits)) - .render(); - }); + if (m_evmVersion.hasBitwiseShifting()) + { + return createFunction(functionName, [&]() { + return + Whiskers(R"( + function <functionName>(value) -> newValue { + newValue := shl(<numBits>, value) + } + )") + ("functionName", functionName) + ("numBits", to_string(_numBits)) + .render(); + }); + } + else + { + return createFunction(functionName, [&]() { + return + Whiskers(R"( + function <functionName>(value) -> newValue { + newValue := mul(value, <multiplier>) + } + )") + ("functionName", functionName) + ("multiplier", toCompactHexWithPrefix(u256(1) << _numBits)) + .render(); + }); + } } string ABIFunctions::shiftRightFunction(size_t _numBits, bool _signed) { + solAssert(_numBits < 256, ""); + string functionName = "shift_right_" + to_string(_numBits) + (_signed ? "_signed" : "_unsigned"); - return createFunction(functionName, [&]() { - solAssert(_numBits < 256, ""); - return - Whiskers(R"( - function <functionName>(value) -> newValue { - newValue := <div>(value, <multiplier>) - } - )") - ("functionName", functionName) - ("div", _signed ? "sdiv" : "div") - ("multiplier", toCompactHexWithPrefix(u256(1) << _numBits)) - .render(); - }); + if (m_evmVersion.hasBitwiseShifting()) + { + return createFunction(functionName, [&]() { + return + Whiskers(R"( + function <functionName>(value) -> newValue { + newValue := <shiftOp>(<numBits>, value) + } + )") + ("functionName", functionName) + ("shiftOp", _signed ? "sar" : "shr") + ("numBits", to_string(_numBits)) + .render(); + }); + } + else + { + return createFunction(functionName, [&]() { + return + Whiskers(R"( + function <functionName>(value) -> newValue { + newValue := <div>(value, <multiplier>) + } + )") + ("functionName", functionName) + ("div", _signed ? "sdiv" : "div") + ("multiplier", toCompactHexWithPrefix(u256(1) << _numBits)) + .render(); + }); + } } string ABIFunctions::roundUpFunction() diff --git a/libsolidity/codegen/ABIFunctions.h b/libsolidity/codegen/ABIFunctions.h index 2b582e84..41bb70b2 100644 --- a/libsolidity/codegen/ABIFunctions.h +++ b/libsolidity/codegen/ABIFunctions.h @@ -22,6 +22,8 @@ #pragma once +#include <libsolidity/interface/EVMVersion.h> + #include <libsolidity/ast/ASTForward.h> #include <vector> @@ -48,6 +50,8 @@ using TypePointers = std::vector<TypePointer>; class ABIFunctions { public: + explicit ABIFunctions(EVMVersion _evmVersion = EVMVersion{}) : m_evmVersion(_evmVersion) {} + /// @returns name of an assembly function to ABI-encode values of @a _givenTypes /// into memory, converting the types to @a _targetTypes on the fly. /// Parameters are: <headStart> <value_n> ... <value_1>, i.e. @@ -225,6 +229,8 @@ private: /// Map from function name to code for a multi-use function. std::map<std::string, std::string> m_requestedFunctions; + + EVMVersion m_evmVersion; }; } diff --git a/libsolidity/codegen/CompilerContext.h b/libsolidity/codegen/CompilerContext.h index 098472f7..5776b5d1 100644 --- a/libsolidity/codegen/CompilerContext.h +++ b/libsolidity/codegen/CompilerContext.h @@ -55,7 +55,8 @@ public: explicit CompilerContext(EVMVersion _evmVersion = EVMVersion{}, CompilerContext* _runtimeContext = nullptr): m_asm(std::make_shared<eth::Assembly>()), m_evmVersion(_evmVersion), - m_runtimeContext(_runtimeContext) + m_runtimeContext(_runtimeContext), + m_abiFunctions(m_evmVersion) { if (m_runtimeContext) m_runtimeSub = size_t(m_asm->newSub(m_runtimeContext->m_asm).data()); |