Disambiguate bytesRequired

This commit is contained in:
chriseth 2021-09-22 11:19:53 +02:00
parent d9ad047c21
commit 50ce1f5ddd
7 changed files with 15 additions and 15 deletions

View File

@ -56,7 +56,7 @@ AssemblyItem const& Assembly::append(AssemblyItem const& _i)
return m_items.back(); return m_items.back();
} }
unsigned Assembly::bytesRequired(unsigned subTagSize) const unsigned Assembly::codeSize(unsigned subTagSize) const
{ {
for (unsigned tagSize = subTagSize; true; ++tagSize) for (unsigned tagSize = subTagSize; true; ++tagSize)
{ {
@ -66,7 +66,7 @@ unsigned Assembly::bytesRequired(unsigned subTagSize) const
for (AssemblyItem const& i: m_items) for (AssemblyItem const& i: m_items)
ret += i.bytesRequired(tagSize); ret += i.bytesRequired(tagSize);
if (util::bytesRequired(ret) <= tagSize) if (util::numberEncodingSize(ret) <= tagSize)
return static_cast<unsigned>(ret); return static_cast<unsigned>(ret);
} }
} }
@ -589,20 +589,20 @@ LinkerObject const& Assembly::assemble() const
"Cannot push and assign immutables in the same assembly subroutine." "Cannot push and assign immutables in the same assembly subroutine."
); );
unsigned bytesRequiredForCode = bytesRequired(static_cast<unsigned>(subTagSize)); unsigned bytesRequiredForCode = codeSize(static_cast<unsigned>(subTagSize));
m_tagPositionsInBytecode = vector<size_t>(m_usedTags, numeric_limits<size_t>::max()); m_tagPositionsInBytecode = vector<size_t>(m_usedTags, numeric_limits<size_t>::max());
map<size_t, pair<size_t, size_t>> tagRef; map<size_t, pair<size_t, size_t>> tagRef;
multimap<h256, unsigned> dataRef; multimap<h256, unsigned> dataRef;
multimap<size_t, size_t> subRef; multimap<size_t, size_t> subRef;
vector<unsigned> sizeRef; ///< Pointers to code locations where the size of the program is inserted vector<unsigned> 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<uint8_t>(pushInstruction(bytesPerTag)); uint8_t tagPush = static_cast<uint8_t>(pushInstruction(bytesPerTag));
unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast<unsigned>(m_auxiliaryData.size()); unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast<unsigned>(m_auxiliaryData.size());
for (auto const& sub: m_subs) for (auto const& sub: m_subs)
bytesRequiredIncludingData += static_cast<unsigned>(sub->assemble().bytecode.size()); bytesRequiredIncludingData += static_cast<unsigned>(sub->assemble().bytecode.size());
unsigned bytesPerDataRef = util::bytesRequired(bytesRequiredIncludingData); unsigned bytesPerDataRef = util::numberEncodingSize(bytesRequiredIncludingData);
uint8_t dataRefPush = static_cast<uint8_t>(pushInstruction(bytesPerDataRef)); uint8_t dataRefPush = static_cast<uint8_t>(pushInstruction(bytesPerDataRef));
ret.bytecode.reserve(bytesRequiredIncludingData); ret.bytecode.reserve(bytesRequiredIncludingData);
@ -619,7 +619,7 @@ LinkerObject const& Assembly::assemble() const
break; break;
case Push: case Push:
{ {
unsigned b = max<unsigned>(1, util::bytesRequired(i.data())); unsigned b = max<unsigned>(1, util::numberEncodingSize(i.data()));
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b))); ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b)));
ret.bytecode.resize(ret.bytecode.size() + b); ret.bytecode.resize(ret.bytecode.size() + b);
bytesRef byr(&ret.bytecode.back() + 1 - b, b); bytesRef byr(&ret.bytecode.back() + 1 - b, b);
@ -649,7 +649,7 @@ LinkerObject const& Assembly::assemble() const
assertThrow(i.data() <= numeric_limits<size_t>::max(), AssemblyException, ""); assertThrow(i.data() <= numeric_limits<size_t>::max(), AssemblyException, "");
auto s = subAssemblyById(static_cast<size_t>(i.data()))->assemble().bytecode.size(); auto s = subAssemblyById(static_cast<size_t>(i.data()))->assemble().bytecode.size();
i.setPushedValue(u256(s)); i.setPushedValue(u256(s));
unsigned b = max<unsigned>(1, util::bytesRequired(s)); unsigned b = max<unsigned>(1, util::numberEncodingSize(s));
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b))); ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b)));
ret.bytecode.resize(ret.bytecode.size() + b); ret.bytecode.resize(ret.bytecode.size() + b);
bytesRef byr(&ret.bytecode.back() + 1 - b, 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."); assertThrow(tagId < tagPositions.size(), AssemblyException, "Reference to non-existing tag.");
size_t pos = tagPositions[tagId]; size_t pos = tagPositions[tagId];
assertThrow(pos != numeric_limits<size_t>::max(), AssemblyException, "Reference to tag without position."); assertThrow(pos != numeric_limits<size_t>::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); bytesRef r(ret.bytecode.data() + i.first, bytesPerTag);
toBigEndian(pos, r); toBigEndian(pos, r);
} }

View File

@ -167,7 +167,7 @@ protected:
/// that are referenced in a super-assembly. /// that are referenced in a super-assembly.
std::map<u256, u256> const& optimiseInternal(OptimiserSettings const& _settings, std::set<size_t> _tagsReferencedFromOutside); std::map<u256, u256> const& optimiseInternal(OptimiserSettings const& _settings, std::set<size_t> _tagsReferencedFromOutside);
unsigned bytesRequired(unsigned subTagSize) const; unsigned codeSize(unsigned subTagSize) const;
private: private:
static Json::Value createJsonValue( static Json::Value createJsonValue(

View File

@ -71,7 +71,7 @@ size_t AssemblyItem::bytesRequired(size_t _addressLength) const
case Tag: // 1 byte for the JUMPDEST case Tag: // 1 byte for the JUMPDEST
return 1; return 1;
case Push: case Push:
return 1 + max<size_t>(1, util::bytesRequired(data())); return 1 + max<size_t>(1, util::numberEncodingSize(data()));
case PushSubSize: case PushSubSize:
case PushProgramSize: case PushProgramSize:
return 1 + 4; // worst case: a 16MB program return 1 + 4; // worst case: a 16MB program

View File

@ -192,7 +192,7 @@ AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
if (_value < 0x10000) if (_value < 0x10000)
// Very small value, not worth computing // Very small value, not worth computing
return AssemblyItems{_value}; return AssemblyItems{_value};
else if (util::bytesRequired(~_value) < util::bytesRequired(_value)) else if (util::numberEncodingSize(~_value) < util::numberEncodingSize(_value))
// Negated is shorter to represent // Negated is shorter to represent
return findRepresentation(~_value) + AssemblyItems{Instruction::NOT}; return findRepresentation(~_value) + AssemblyItems{Instruction::NOT};
else else

View File

@ -1135,7 +1135,7 @@ IntegerType const* RationalNumberType::integerType() const
return nullptr; return nullptr;
else else
return TypeProvider::integer( return TypeProvider::integer(
max(util::bytesRequired(value), 1u) * 8, max(util::numberEncodingSize(value), 1u) * 8,
negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned negative ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned
); );
} }
@ -1169,7 +1169,7 @@ FixedPointType const* RationalNumberType::fixedPointType() const
if (v > u256(-1)) if (v > u256(-1))
return nullptr; return nullptr;
unsigned totalBits = max(util::bytesRequired(v), 1u) * 8; unsigned totalBits = max(util::numberEncodingSize(v), 1u) * 8;
solAssert(totalBits <= 256, ""); solAssert(totalBits <= 256, "");
return TypeProvider::fixedPoint( return TypeProvider::fixedPoint(

View File

@ -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. /// Determine bytes required to encode the given integer value. @returns 0 if @a _i is zero.
template <class T> template <class T>
inline unsigned bytesRequired(T _i) inline unsigned numberEncodingSize(T _i)
{ {
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
unsigned i = 0; unsigned i = 0;

View File

@ -128,7 +128,7 @@ Representation const& RepresentationFinder::findRepresentation(u256 const& _valu
Representation routine = represent(_value); Representation routine = represent(_value);
if (bytesRequired(~_value) < bytesRequired(_value)) if (numberEncodingSize(~_value) < numberEncodingSize(_value))
// Negated is shorter to represent // Negated is shorter to represent
routine = min(move(routine), represent("not"_yulstring, findRepresentation(~_value))); routine = min(move(routine), represent("not"_yulstring, findRepresentation(~_value)));