Skip to content

Commit

Permalink
Split Common.h into Numeric.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Sep 23, 2021
1 parent c8e6ef9 commit 1531863
Show file tree
Hide file tree
Showing 65 changed files with 318 additions and 215 deletions.
26 changes: 14 additions & 12 deletions libevmasm/Assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@

#include <json/json.h>

#include <fstream>
#include <range/v3/algorithm/any_of.hpp>
#include <range/v3/view/enumerate.hpp>

#include <fstream>
#include <limits>

using namespace std;
using namespace solidity;
using namespace solidity::evmasm;
Expand Down Expand Up @@ -67,7 +69,7 @@ unsigned Assembly::codeSize(unsigned subTagSize) const

for (AssemblyItem const& i: m_items)
ret += i.bytesRequired(tagSize);
if (util::numberEncodingSize(ret) <= tagSize)
if (numberEncodingSize(ret) <= tagSize)
return static_cast<unsigned>(ret);
}
}
Expand Down Expand Up @@ -178,7 +180,7 @@ void Assembly::assemblyStream(ostream& _out, string const& _prefix, StringMap co
_out << _prefix << "stop" << endl;
for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size())
_out << _prefix << "data_" << toHex(u256(i.first)) << " " << toHex(i.second) << endl;
_out << _prefix << "data_" << toHex(u256(i.first)) << " " << util::toHex(i.second) << endl;

for (size_t i = 0; i < m_subs.size(); ++i)
{
Expand All @@ -189,7 +191,7 @@ void Assembly::assemblyStream(ostream& _out, string const& _prefix, StringMap co
}

if (m_auxiliaryData.size() > 0)
_out << endl << _prefix << "auxdata: 0x" << toHex(m_auxiliaryData) << endl;
_out << endl << _prefix << "auxdata: 0x" << util::toHex(m_auxiliaryData) << endl;
}

string Assembly::assemblyString(StringMap const& _sourceCodes) const
Expand Down Expand Up @@ -309,7 +311,7 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
collection.append(createJsonValue("PUSH data", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data())));
break;
case VerbatimBytecode:
collection.append(createJsonValue("VERBATIM", sourceIndex, i.location().start, i.location().end, toHex(i.verbatimData())));
collection.append(createJsonValue("VERBATIM", sourceIndex, i.location().start, i.location().end, util::toHex(i.verbatimData())));
break;
default:
assertThrow(false, InvalidOpcode, "");
Expand All @@ -321,7 +323,7 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
Json::Value& data = root[".data"] = Json::objectValue;
for (auto const& i: m_data)
if (u256(i.first) >= m_subs.size())
data[toStringInHex((u256)i.first)] = toHex(i.second);
data[toStringInHex((u256)i.first)] = util::toHex(i.second);

for (size_t i = 0; i < m_subs.size(); ++i)
{
Expand All @@ -332,7 +334,7 @@ Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices)
}

if (m_auxiliaryData.size() > 0)
root[".auxdata"] = toHex(m_auxiliaryData);
root[".auxdata"] = util::toHex(m_auxiliaryData);

return root;
}
Expand Down Expand Up @@ -596,14 +598,14 @@ LinkerObject const& Assembly::assemble() const
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::numberEncodingSize(bytesRequiredForCode);
unsigned bytesPerTag = 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::numberEncodingSize(bytesRequiredIncludingData);
unsigned bytesPerDataRef = numberEncodingSize(bytesRequiredIncludingData);
uint8_t dataRefPush = static_cast<uint8_t>(pushInstruction(bytesPerDataRef));
ret.bytecode.reserve(bytesRequiredIncludingData);

Expand All @@ -620,7 +622,7 @@ LinkerObject const& Assembly::assemble() const
break;
case Push:
{
unsigned b = max<unsigned>(1, util::numberEncodingSize(i.data()));
unsigned b = max<unsigned>(1, 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 +652,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::numberEncodingSize(s));
unsigned b = max<unsigned>(1, 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 +757,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::numberEncodingSize(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space.");
assertThrow(numberEncodingSize(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space.");
bytesRef r(ret.bytecode.data() + i.first, bytesPerTag);
toBigEndian(pos, r);
}
Expand Down
14 changes: 8 additions & 6 deletions libevmasm/AssemblyItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#include <libevmasm/Assembly.h>

#include <libsolutil/CommonData.h>
#include <libsolutil/Numeric.h>
#include <libsolutil/StringUtils.h>
#include <libsolutil/FixedHash.h>
#include <liblangutil/SourceLocation.h>

#include <fstream>
#include <limits>

using namespace std;
using namespace solidity;
Expand Down Expand Up @@ -71,7 +73,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::numberEncodingSize(data()));
return 1 + max<size_t>(1, numberEncodingSize(data()));
case PushSubSize:
case PushProgramSize:
return 1 + 4; // worst case: a 16MB program
Expand Down Expand Up @@ -189,7 +191,7 @@ string AssemblyItem::toAssemblyText(Assembly const& _assembly) const
break;
}
case Push:
text = toHex(util::toCompactBigEndian(data(), 1), util::HexPrefix::Add);
text = toHex(toCompactBigEndian(data(), 1), util::HexPrefix::Add);
break;
case PushTag:
{
Expand All @@ -207,7 +209,7 @@ string AssemblyItem::toAssemblyText(Assembly const& _assembly) const
text = string("tag_") + to_string(static_cast<size_t>(data())) + ":";
break;
case PushData:
text = string("data_") + util::toHex(data());
text = string("data_") + toHex(data());
break;
case PushSub:
case PushSubSize:
Expand All @@ -226,16 +228,16 @@ string AssemblyItem::toAssemblyText(Assembly const& _assembly) const
text = string("bytecodeSize");
break;
case PushLibraryAddress:
text = string("linkerSymbol(\"") + util::toHex(data()) + string("\")");
text = string("linkerSymbol(\"") + toHex(data()) + string("\")");
break;
case PushDeployTimeAddress:
text = string("deployTimeAddress()");
break;
case PushImmutable:
text = string("immutable(\"") + toHex(util::toCompactBigEndian(data(), 1), util::HexPrefix::Add) + "\")";
text = string("immutable(\"") + "0x" + util::toHex(toCompactBigEndian(data(), 1)) + "\")";
break;
case AssignImmutable:
text = string("assignImmutable(\"") + toHex(util::toCompactBigEndian(data(), 1), util::HexPrefix::Add) + "\")";
text = string("assignImmutable(\"") + "0x" + util::toHex(toCompactBigEndian(data(), 1)) + "\")";
break;
case UndefinedItem:
assertThrow(false, AssemblyException, "Invalid assembly item.");
Expand Down
2 changes: 2 additions & 0 deletions libevmasm/BlockDeduplicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#pragma once

#include <libsolutil/Common.h>
#include <libsolutil/Numeric.h>


#include <cstddef>
#include <vector>
Expand Down
8 changes: 4 additions & 4 deletions libevmasm/ConstantOptimiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ bigint LiteralMethod::gasNeeded() const
return combineGas(
simpleRunGas({Instruction::PUSH1}),
// PUSHX plus data
(m_params.isCreation ? GasCosts::txDataNonZeroGas(m_params.evmVersion) : GasCosts::createDataGas) + dataGas(util::toCompactBigEndian(m_value, 1)),
(m_params.isCreation ? GasCosts::txDataNonZeroGas(m_params.evmVersion) : GasCosts::createDataGas) + dataGas(toCompactBigEndian(m_value, 1)),
0
);
}
Expand All @@ -146,13 +146,13 @@ bigint CodeCopyMethod::gasNeeded() const
// Data gas for copy routines: Some bytes are zero, but we ignore them.
bytesRequired(copyRoutine()) * (m_params.isCreation ? GasCosts::txDataNonZeroGas(m_params.evmVersion) : GasCosts::createDataGas),
// Data gas for data itself
dataGas(util::toBigEndian(m_value))
dataGas(toBigEndian(m_value))
);
}

AssemblyItems CodeCopyMethod::execute(Assembly& _assembly) const
{
bytes data = util::toBigEndian(m_value);
bytes data = toBigEndian(m_value);
assertThrow(data.size() == 32, OptimizerException, "Invalid number encoding.");
AssemblyItems actualCopyRoutine = copyRoutine();
actualCopyRoutine[4] = _assembly.newData(data);
Expand Down 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::numberEncodingSize(~_value) < util::numberEncodingSize(_value))
else if (numberEncodingSize(~_value) < numberEncodingSize(_value))
// Negated is shorter to represent
return findRepresentation(~_value) + AssemblyItems{Instruction::NOT};
else
Expand Down
6 changes: 4 additions & 2 deletions libevmasm/ControlFlowGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@

#pragma once

#include <vector>
#include <memory>
#include <libsolutil/Common.h>
#include <libsolutil/Assertions.h>
#include <libevmasm/ExpressionClasses.h>

#include <vector>
#include <memory>
#include <limits>

namespace solidity::evmasm
{

Expand Down
1 change: 1 addition & 0 deletions libevmasm/ExpressionClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <functional>
#include <tuple>
#include <limits>

using namespace std;
using namespace solidity;
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/Inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <range/v3/view/transform.hpp>

#include <optional>

#include <limits>

using namespace std;
using namespace solidity;
Expand Down
3 changes: 2 additions & 1 deletion libevmasm/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <libevmasm/Exceptions.h>
#include <libsolutil/Common.h>
#include <libsolutil/Assertions.h>
#include <libsolutil/Numeric.h>
#include <functional>

namespace solidity::evmasm
Expand Down Expand Up @@ -301,7 +302,7 @@ bool isValidInstruction(Instruction _inst);
extern const std::map<std::string, Instruction> c_instructions;

/// Iterate through EVM code and call a function on each instruction.
void eachInstruction(bytes const& _mem, std::function<void(Instruction,u256 const&)> const& _onInstruction);
void eachInstruction(bytes const& _mem, std::function<void(Instruction, u256 const&)> const& _onInstruction);

/// Convert from EVM code to simple EVM assembly language.
std::string disassemble(bytes const& _mem, std::string const& _delimiter = " ");
Expand Down
2 changes: 2 additions & 0 deletions libevmasm/JumpdestRemover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <libevmasm/AssemblyItem.h>

#include <limits>

using namespace std;
using namespace solidity;
using namespace solidity::util;
Expand Down
2 changes: 1 addition & 1 deletion libevmasm/KnownState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ KnownState::Id KnownState::applyKeccak256(
{
bytes data;
for (Id a: arguments)
data += util::toBigEndian(*m_expressionClasses->knownConstant(a));
data += toBigEndian(*m_expressionClasses->knownConstant(a));
data.resize(length);
v = m_expressionClasses->find(AssemblyItem(u256(util::keccak256(data)), _location));
}
Expand Down
17 changes: 9 additions & 8 deletions libevmasm/KnownState.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@

#pragma once

#include <utility>
#include <vector>
#include <map>
#include <set>
#include <tuple>
#include <memory>
#include <ostream>

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wredeclared-class-member"
Expand All @@ -58,6 +50,15 @@
#include <libevmasm/ExpressionClasses.h>
#include <libevmasm/SemanticInformation.h>

#include <limits>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <tuple>
#include <memory>
#include <ostream>

namespace solidity::langutil
{
struct SourceLocation;
Expand Down
1 change: 1 addition & 0 deletions liblangutil/Scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <optional>
#include <string_view>
#include <tuple>
#include <array>

using namespace std;

Expand Down
1 change: 1 addition & 0 deletions liblangutil/SemVerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <liblangutil/Exceptions.h>

#include <functional>
#include <limits>

using namespace std;
using namespace solidity;
Expand Down
1 change: 1 addition & 0 deletions libsmtutil/SolverInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <libsmtutil/Sorts.h>

#include <libsolutil/Common.h>
#include <libsolutil/Numeric.h>

#include <range/v3/view.hpp>

Expand Down
2 changes: 2 additions & 0 deletions libsolidity/analysis/ConstantEvaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <libsolidity/ast/TypeProvider.h>
#include <liblangutil/ErrorReporter.h>

#include <limits>

using namespace std;
using namespace solidity;
using namespace solidity::frontend;
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/ast/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ uint32_t ContractDefinition::interfaceId() const
{
uint32_t result{0};
for (auto const& function: interfaceFunctionList(false))
result ^= util::fromBigEndian<uint32_t>(function.first.ref());
result ^= fromBigEndian<uint32_t>(function.first.ref());
return result;
}

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::numberEncodingSize(value), 1u) * 8,
max(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::numberEncodingSize(v), 1u) * 8;
unsigned totalBits = max(numberEncodingSize(v), 1u) * 8;
solAssert(totalBits <= 256, "");

return TypeProvider::fixedPoint(
Expand Down
1 change: 1 addition & 0 deletions libsolidity/ast/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <liblangutil/Exceptions.h>

#include <libsolutil/Common.h>
#include <libsolutil/Numeric.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/LazyInit.h>
#include <libsolutil/Result.h>
Expand Down
1 change: 1 addition & 0 deletions libsolidity/codegen/CompilerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <stack>
#include <queue>
#include <utility>
#include <limits>

namespace solidity::frontend
{
Expand Down
Loading

0 comments on commit 1531863

Please sign in to comment.