diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index acf655c82..a97499c3f 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -56,7 +56,7 @@ AssemblyItem const& Assembly::append(AssemblyItem const& _i) return m_items.back(); } -unsigned Assembly::bytesRequired(unsigned subTagSize) const +unsigned Assembly::codeSize(unsigned subTagSize) const { for (unsigned tagSize = subTagSize; true; ++tagSize) { @@ -66,7 +66,7 @@ unsigned Assembly::bytesRequired(unsigned subTagSize) const for (AssemblyItem const& i: m_items) ret += i.bytesRequired(tagSize); - if (util::bytesRequired(ret) <= tagSize) + if (util::numberEncodingSize(ret) <= tagSize) return static_cast(ret); } } @@ -589,20 +589,20 @@ LinkerObject const& Assembly::assemble() const "Cannot push and assign immutables in the same assembly subroutine." ); - unsigned bytesRequiredForCode = bytesRequired(static_cast(subTagSize)); + unsigned bytesRequiredForCode = codeSize(static_cast(subTagSize)); m_tagPositionsInBytecode = vector(m_usedTags, numeric_limits::max()); map> tagRef; multimap dataRef; multimap subRef; vector sizeRef; ///< Pointers to code locations where the size of the program is inserted - unsigned bytesPerTag = util::bytesRequired(bytesRequiredForCode); + unsigned bytesPerTag = util::numberEncodingSize(bytesRequiredForCode); uint8_t tagPush = static_cast(pushInstruction(bytesPerTag)); unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast(m_auxiliaryData.size()); for (auto const& sub: m_subs) bytesRequiredIncludingData += static_cast(sub->assemble().bytecode.size()); - unsigned bytesPerDataRef = util::bytesRequired(bytesRequiredIncludingData); + unsigned bytesPerDataRef = util::numberEncodingSize(bytesRequiredIncludingData); uint8_t dataRefPush = static_cast(pushInstruction(bytesPerDataRef)); ret.bytecode.reserve(bytesRequiredIncludingData); @@ -619,7 +619,7 @@ LinkerObject const& Assembly::assemble() const break; case Push: { - unsigned b = max(1, util::bytesRequired(i.data())); + unsigned b = max(1, util::numberEncodingSize(i.data())); ret.bytecode.push_back(static_cast(pushInstruction(b))); ret.bytecode.resize(ret.bytecode.size() + b); bytesRef byr(&ret.bytecode.back() + 1 - b, b); @@ -649,7 +649,7 @@ LinkerObject const& Assembly::assemble() const assertThrow(i.data() <= numeric_limits::max(), AssemblyException, ""); auto s = subAssemblyById(static_cast(i.data()))->assemble().bytecode.size(); i.setPushedValue(u256(s)); - unsigned b = max(1, util::bytesRequired(s)); + unsigned b = max(1, util::numberEncodingSize(s)); ret.bytecode.push_back(static_cast(pushInstruction(b))); ret.bytecode.resize(ret.bytecode.size() + b); bytesRef byr(&ret.bytecode.back() + 1 - b, b); @@ -754,7 +754,7 @@ LinkerObject const& Assembly::assemble() const assertThrow(tagId < tagPositions.size(), AssemblyException, "Reference to non-existing tag."); size_t pos = tagPositions[tagId]; assertThrow(pos != numeric_limits::max(), AssemblyException, "Reference to tag without position."); - assertThrow(util::bytesRequired(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space."); + assertThrow(util::numberEncodingSize(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space."); bytesRef r(ret.bytecode.data() + i.first, bytesPerTag); toBigEndian(pos, r); } diff --git a/libevmasm/Assembly.h b/libevmasm/Assembly.h index 732aa14a1..1d91c2f90 100644 --- a/libevmasm/Assembly.h +++ b/libevmasm/Assembly.h @@ -167,7 +167,7 @@ protected: /// that are referenced in a super-assembly. std::map const& optimiseInternal(OptimiserSettings const& _settings, std::set _tagsReferencedFromOutside); - unsigned bytesRequired(unsigned subTagSize) const; + unsigned codeSize(unsigned subTagSize) const; private: static Json::Value createJsonValue( diff --git a/libevmasm/AssemblyItem.cpp b/libevmasm/AssemblyItem.cpp index 462199ba4..6c10428dc 100644 --- a/libevmasm/AssemblyItem.cpp +++ b/libevmasm/AssemblyItem.cpp @@ -71,7 +71,7 @@ size_t AssemblyItem::bytesRequired(size_t _addressLength) const case Tag: // 1 byte for the JUMPDEST return 1; case Push: - return 1 + max(1, util::bytesRequired(data())); + return 1 + max(1, util::numberEncodingSize(data())); case PushSubSize: case PushProgramSize: return 1 + 4; // worst case: a 16MB program diff --git a/libevmasm/ConstantOptimiser.cpp b/libevmasm/ConstantOptimiser.cpp index a6bb342bc..3b582b7f4 100644 --- a/libevmasm/ConstantOptimiser.cpp +++ b/libevmasm/ConstantOptimiser.cpp @@ -192,7 +192,7 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value) if (_value < 0x10000) // Very small value, not worth computing return AssemblyItems{_value}; - else if (util::bytesRequired(~_value) < util::bytesRequired(_value)) + else if (util::numberEncodingSize(~_value) < util::numberEncodingSize(_value)) // Negated is shorter to represent return findRepresentation(~_value) + AssemblyItems{Instruction::NOT}; else diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 67e8e5168..ffb8dea4b 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -1135,7 +1135,7 @@ IntegerType const* RationalNumberType::integerType() const return nullptr; else return TypeProvider::integer( - max(util::bytesRequired(value), 1u) * 8, + max(util::numberEncodingSize(value), 1u) * 8, negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned ); } @@ -1169,7 +1169,7 @@ FixedPointType const* RationalNumberType::fixedPointType() const if (v > u256(-1)) return nullptr; - unsigned totalBits = max(util::bytesRequired(v), 1u) * 8; + unsigned totalBits = max(util::numberEncodingSize(v), 1u) * 8; solAssert(totalBits <= 256, ""); return TypeProvider::fixedPoint( diff --git a/libsolutil/CommonData.h b/libsolutil/CommonData.h index 774cedf0a..71387558c 100644 --- a/libsolutil/CommonData.h +++ b/libsolutil/CommonData.h @@ -512,7 +512,7 @@ inline std::string formatNumber(u256 const& _value) /// Determine bytes required to encode the given integer value. @returns 0 if @a _i is zero. template -inline unsigned bytesRequired(T _i) +inline unsigned numberEncodingSize(T _i) { static_assert(std::is_same::value || !std::numeric_limits::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift unsigned i = 0; diff --git a/libyul/backends/evm/ConstantOptimiser.cpp b/libyul/backends/evm/ConstantOptimiser.cpp index 09d87da83..aecd00bf4 100644 --- a/libyul/backends/evm/ConstantOptimiser.cpp +++ b/libyul/backends/evm/ConstantOptimiser.cpp @@ -128,7 +128,7 @@ Representation const& RepresentationFinder::findRepresentation(u256 const& _valu Representation routine = represent(_value); - if (bytesRequired(~_value) < bytesRequired(_value)) + if (numberEncodingSize(~_value) < numberEncodingSize(_value)) // Negated is shorter to represent routine = min(move(routine), represent("not"_yulstring, findRepresentation(~_value)));