#pragma once
#include <libevmasm/Instruction.h>
#include <libevmasm/Exceptions.h>
#include <liblangutil/SourceLocation.h>
#include <libsolutil/Common.h>
#include <libsolutil/Numeric.h>
#include <libsolutil/Assertions.h>
#include <optional>
#include <iostream>
#include <sstream>
namespace solidity::evmasm
{
enum AssemblyItemType
{
UndefinedItem,
Operation,
Push,
PushTag,
PushSub,
PushSubSize,
PushProgramSize,
Tag,
PushData,
PushLibraryAddress, PushDeployTimeAddress, PushImmutable, AssignImmutable, VerbatimBytecode };
enum class Precision { Precise , Approximate };
class Assembly;
class AssemblyItem;
using AssemblyItems = std::vector<AssemblyItem>;
class AssemblyItem
{
public:
enum class JumpType { Ordinary, IntoFunction, OutOfFunction };
AssemblyItem(u256 _push, langutil::SourceLocation _location = langutil::SourceLocation()):
AssemblyItem(Push, std::move(_push), std::move(_location)) { }
AssemblyItem(Instruction _i, langutil::SourceLocation _location = langutil::SourceLocation()):
m_type(Operation),
m_instruction(_i),
m_location(std::move(_location))
{}
AssemblyItem(AssemblyItemType _type, u256 _data = 0, langutil::SourceLocation _location = langutil::SourceLocation()):
m_type(_type),
m_location(std::move(_location))
{
if (m_type == Operation)
m_instruction = Instruction(uint8_t(_data));
else
m_data = std::make_shared<u256>(std::move(_data));
}
explicit AssemblyItem(bytes _verbatimData, size_t _arguments, size_t _returnVariables):
m_type(VerbatimBytecode),
m_instruction{},
m_verbatimBytecode{{_arguments, _returnVariables, std::move(_verbatimData)}}
{}
AssemblyItem(AssemblyItem const&) = default;
AssemblyItem(AssemblyItem&&) = default;
AssemblyItem& operator=(AssemblyItem const&) = default;
AssemblyItem& operator=(AssemblyItem&&) = default;
AssemblyItem tag() const { assertThrow(m_type == PushTag || m_type == Tag, util::Exception, ""); return AssemblyItem(Tag, data()); }
AssemblyItem pushTag() const { assertThrow(m_type == PushTag || m_type == Tag, util::Exception, ""); return AssemblyItem(PushTag, data()); }
AssemblyItem toSubAssemblyTag(size_t _subId) const;
std::pair<size_t, size_t> splitForeignPushTag() const;
void setPushTagSubIdAndTag(size_t _subId, size_t _tag);
AssemblyItemType type() const { return m_type; }
u256 const& data() const { assertThrow(m_type != Operation, util::Exception, ""); return *m_data; }
void setData(u256 const& _data) { assertThrow(m_type != Operation, util::Exception, ""); m_data = std::make_shared<u256>(_data); }
std::pair<std::string, std::string> nameAndData() const;
bytes const& verbatimData() const { assertThrow(m_type == VerbatimBytecode, util::Exception, ""); return std::get<2>(*m_verbatimBytecode); }
Instruction instruction() const { assertThrow(m_type == Operation, util::Exception, ""); return m_instruction; }
bool operator==(AssemblyItem const& _other) const
{
if (type() != _other.type())
return false;
if (type() == Operation)
return instruction() == _other.instruction();
else if (type() == VerbatimBytecode)
return *m_verbatimBytecode == *_other.m_verbatimBytecode;
else
return data() == _other.data();
}
bool operator!=(AssemblyItem const& _other) const { return !operator==(_other); }
bool operator<(AssemblyItem const& _other) const
{
if (type() != _other.type())
return type() < _other.type();
else if (type() == Operation)
return instruction() < _other.instruction();
else if (type() == VerbatimBytecode)
return *m_verbatimBytecode == *_other.m_verbatimBytecode;
else
return data() < _other.data();
}
bool operator==(Instruction _instr) const
{
return type() == Operation && instruction() == _instr;
}
bool operator!=(Instruction _instr) const { return !operator==(_instr); }
static std::string computeSourceMapping(
AssemblyItems const& _items,
std::map<std::string, unsigned> const& _sourceIndicesMap
);
size_t bytesRequired(size_t _addressLength, Precision _precision = Precision::Precise) const;
size_t arguments() const;
size_t returnValues() const;
size_t deposit() const { return returnValues() - arguments(); }
bool canBeFunctional() const;
void setLocation(langutil::SourceLocation const& _location) { m_location = _location; }
langutil::SourceLocation const& location() const { return m_location; }
void setJumpType(JumpType _jumpType) { m_jumpType = _jumpType; }
JumpType getJumpType() const { return m_jumpType; }
std::string getJumpTypeAsString() const;
void setPushedValue(u256 const& _value) const { m_pushedValue = std::make_shared<u256>(_value); }
u256 const* pushedValue() const { return m_pushedValue.get(); }
std::string toAssemblyText(Assembly const& _assembly) const;
size_t m_modifierDepth = 0;
void setImmutableOccurrences(size_t _n) const { m_immutableOccurrences = _n; }
private:
size_t opcodeCount() const noexcept;
AssemblyItemType m_type;
Instruction m_instruction; std::shared_ptr<u256> m_data; std::optional<std::tuple<size_t, size_t, bytes>> m_verbatimBytecode;
langutil::SourceLocation m_location;
JumpType m_jumpType = JumpType::Ordinary;
mutable std::shared_ptr<u256> m_pushedValue;
mutable std::optional<size_t> m_immutableOccurrences;
};
inline size_t bytesRequired(AssemblyItems const& _items, size_t _addressLength, Precision _precision = Precision::Precise)
{
size_t size = 0;
for (AssemblyItem const& item: _items)
size += item.bytesRequired(_addressLength, _precision);
return size;
}
std::ostream& operator<<(std::ostream& _out, AssemblyItem const& _item);
inline std::ostream& operator<<(std::ostream& _out, AssemblyItems const& _items)
{
for (AssemblyItem const& item: _items)
_out << item;
return _out;
}
}