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();
}
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<unsigned>(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<unsigned>(subTagSize));
unsigned bytesRequiredForCode = codeSize(static_cast<unsigned>(subTagSize));
m_tagPositionsInBytecode = vector<size_t>(m_usedTags, numeric_limits<size_t>::max());
map<size_t, pair<size_t, size_t>> tagRef;
multimap<h256, unsigned> dataRef;
multimap<size_t, size_t> subRef;
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));
unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast<unsigned>(m_auxiliaryData.size());
for (auto const& sub: m_subs)
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));
ret.bytecode.reserve(bytesRequiredIncludingData);
@ -619,7 +619,7 @@ LinkerObject const& Assembly::assemble() const
break;
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.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<size_t>::max(), AssemblyException, "");
auto s = subAssemblyById(static_cast<size_t>(i.data()))->assemble().bytecode.size();
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.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<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);
toBigEndian(pos, r);
}

View File

@ -167,7 +167,7 @@ protected:
/// that are referenced in a super-assembly.
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:
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
return 1;
case Push:
return 1 + max<size_t>(1, util::bytesRequired(data()));
return 1 + max<size_t>(1, util::numberEncodingSize(data()));
case PushSubSize:
case PushProgramSize:
return 1 + 4; // worst case: a 16MB program

View File

@ -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

View File

@ -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(

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.
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
unsigned i = 0;

View File

@ -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)));