Skip to content

Commit

Permalink
Merge pull request ethereum#12004 from ethereum/disambiguateBytesRequ…
Browse files Browse the repository at this point in the history
…ired

Disambiguate bytesRequired
  • Loading branch information
chriseth authored Sep 23, 2021
2 parents 9552270 + 50ce1f5 commit c8e6ef9
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,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)
{
Expand All @@ -67,7 +67,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);
}
}
Expand Down Expand Up @@ -590,20 +590,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);

Expand All @@ -620,7 +620,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);
Expand Down Expand Up @@ -650,7 +650,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);
Expand Down Expand Up @@ -755,7 +755,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);
}
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/Assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Assembly
/// 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(
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/AssemblyItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/ConstantOptimiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libsolidity/ast/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion libsolutil/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libyul/backends/evm/ConstantOptimiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down

0 comments on commit c8e6ef9

Please sign in to comment.