Browse Source

Merge pull request #1491 from LianaHus/sol_removeAAFriendship

Solidity refactoring. Assembly and AssemblyItem are no longer friend classes
cl-refactor
chriseth 10 years ago
parent
commit
9452811238
  1. 40
      libevmcore/Assembly.cpp
  2. 2
      libevmcore/Assembly.h
  3. 5
      libevmcore/AssemblyItem.h

40
libevmcore/Assembly.cpp

@ -34,9 +34,9 @@ void Assembly::append(Assembly const& _a)
for (AssemblyItem i: _a.m_items) for (AssemblyItem i: _a.m_items)
{ {
if (i.type() == Tag || i.type() == PushTag) if (i.type() == Tag || i.type() == PushTag)
i.m_data += m_usedTags; i.setData(i.data() + m_usedTags);
else if (i.type() == PushSub || i.type() == PushSubSize) else if (i.type() == PushSub || i.type() == PushSubSize)
i.m_data += m_subs.size(); i.setData(i.data() + m_usedTags);
append(i); append(i);
} }
m_deposit = newDeposit; m_deposit = newDeposit;
@ -106,34 +106,34 @@ ostream& Assembly::stream(ostream& _out, string const& _prefix, StringMap const&
for (AssemblyItem const& i: m_items) for (AssemblyItem const& i: m_items)
{ {
_out << _prefix; _out << _prefix;
switch (i.m_type) switch (i.type())
{ {
case Operation: case Operation:
_out << " " << instructionInfo(i.instruction()).name << "\t" << i.getJumpTypeAsString(); _out << " " << instructionInfo(i.instruction()).name << "\t" << i.getJumpTypeAsString();
break; break;
case Push: case Push:
_out << " PUSH " << i.m_data; _out << " PUSH " << i.data();
break; break;
case PushString: case PushString:
_out << " PUSH \"" << m_strings.at((h256)i.m_data) << "\""; _out << " PUSH \"" << m_strings.at((h256)i.data()) << "\"";
break; break;
case PushTag: case PushTag:
_out << " PUSH [tag" << i.m_data << "]"; _out << " PUSH [tag" << i.data() << "]";
break; break;
case PushSub: case PushSub:
_out << " PUSH [$" << h256(i.m_data).abridged() << "]"; _out << " PUSH [$" << h256(i.data()).abridged() << "]";
break; break;
case PushSubSize: case PushSubSize:
_out << " PUSH #[$" << h256(i.m_data).abridged() << "]"; _out << " PUSH #[$" << h256(i.data()).abridged() << "]";
break; break;
case PushProgramSize: case PushProgramSize:
_out << " PUSHSIZE"; _out << " PUSHSIZE";
break; break;
case Tag: case Tag:
_out << "tag" << i.m_data << ": " << endl << _prefix << " JUMPDEST"; _out << "tag" << i.data() << ": " << endl << _prefix << " JUMPDEST";
break; break;
case PushData: case PushData:
_out << " PUSH [" << hex << (unsigned)i.m_data << "]"; _out << " PUSH [" << hex << (unsigned)i.data() << "]";
break; break;
default: default:
BOOST_THROW_EXCEPTION(InvalidOpcode()); BOOST_THROW_EXCEPTION(InvalidOpcode());
@ -189,7 +189,7 @@ Assembly& Assembly::optimise(bool _enable)
return *this; return *this;
std::vector<pair<AssemblyItems, function<AssemblyItems(AssemblyItemsConstRef)>>> rules; std::vector<pair<AssemblyItems, function<AssemblyItems(AssemblyItemsConstRef)>>> rules;
// jump to next instruction // jump to next instruction
rules.push_back({ { PushTag, Instruction::JUMP, Tag }, [](AssemblyItemsConstRef m) -> AssemblyItems { if (m[0].m_data == m[2].m_data) return {m[2]}; else return m.toVector(); }}); rules.push_back({ { PushTag, Instruction::JUMP, Tag }, [](AssemblyItemsConstRef m) -> AssemblyItems { if (m[0].data() == m[2].data()) return {m[2]}; else return m.toVector(); }});
unsigned total = 0; unsigned total = 0;
for (unsigned count = 1; count > 0; total += count) for (unsigned count = 1; count > 0; total += count)
@ -331,16 +331,16 @@ bytes Assembly::assemble() const
// m_data must not change from here on // m_data must not change from here on
for (AssemblyItem const& i: m_items) for (AssemblyItem const& i: m_items)
switch (i.m_type) switch (i.type())
{ {
case Operation: case Operation:
ret.push_back((byte)i.m_data); ret.push_back((byte)i.data());
break; break;
case PushString: case PushString:
{ {
ret.push_back((byte)Instruction::PUSH32); ret.push_back((byte)Instruction::PUSH32);
unsigned ii = 0; unsigned ii = 0;
for (auto j: m_strings.at((h256)i.m_data)) for (auto j: m_strings.at((h256)i.data()))
if (++ii > 32) if (++ii > 32)
break; break;
else else
@ -351,30 +351,30 @@ bytes Assembly::assemble() const
} }
case Push: case Push:
{ {
byte b = max<unsigned>(1, dev::bytesRequired(i.m_data)); byte b = max<unsigned>(1, dev::bytesRequired(i.data()));
ret.push_back((byte)Instruction::PUSH1 - 1 + b); ret.push_back((byte)Instruction::PUSH1 - 1 + b);
ret.resize(ret.size() + b); ret.resize(ret.size() + b);
bytesRef byr(&ret.back() + 1 - b, b); bytesRef byr(&ret.back() + 1 - b, b);
toBigEndian(i.m_data, byr); toBigEndian(i.data(), byr);
break; break;
} }
case PushTag: case PushTag:
{ {
ret.push_back(tagPush); ret.push_back(tagPush);
tagRef[ret.size()] = (unsigned)i.m_data; tagRef[ret.size()] = (unsigned)i.data();
ret.resize(ret.size() + bytesPerTag); ret.resize(ret.size() + bytesPerTag);
break; break;
} }
case PushData: case PushSub: case PushData: case PushSub:
{ {
ret.push_back(dataRefPush); ret.push_back(dataRefPush);
dataRef.insert(make_pair((h256)i.m_data, ret.size())); dataRef.insert(make_pair((h256)i.data(), ret.size()));
ret.resize(ret.size() + bytesPerDataRef); ret.resize(ret.size() + bytesPerDataRef);
break; break;
} }
case PushSubSize: case PushSubSize:
{ {
auto s = m_data[i.m_data].size(); auto s = m_data[i.data()].size();
byte b = max<unsigned>(1, dev::bytesRequired(s)); byte b = max<unsigned>(1, dev::bytesRequired(s));
ret.push_back((byte)Instruction::PUSH1 - 1 + b); ret.push_back((byte)Instruction::PUSH1 - 1 + b);
ret.resize(ret.size() + b); ret.resize(ret.size() + b);
@ -390,7 +390,7 @@ bytes Assembly::assemble() const
break; break;
} }
case Tag: case Tag:
tagPos[(unsigned)i.m_data] = ret.size(); tagPos[(unsigned)i.data()] = ret.size();
ret.push_back((byte)Instruction::JUMPDEST); ret.push_back((byte)Instruction::JUMPDEST);
break; break;
default: default:

2
libevmcore/Assembly.h

@ -65,7 +65,7 @@ public:
template <class T> Assembly& operator<<(T const& _d) { append(_d); return *this; } template <class T> Assembly& operator<<(T const& _d) { append(_d); return *this; }
AssemblyItems const& getItems() const { return m_items; } AssemblyItems const& getItems() const { return m_items; }
AssemblyItem const& back() const { return m_items.back(); } AssemblyItem const& back() const { return m_items.back(); }
std::string backString() const { return m_items.size() && m_items.back().m_type == PushString ? m_strings.at((h256)m_items.back().m_data) : std::string(); } std::string backString() const { return m_items.size() && m_items.back().type() == PushString ? m_strings.at((h256)m_items.back().data()) : std::string(); }
void onePath() { if (asserts(!m_totalDeposit && !m_baseDeposit)) BOOST_THROW_EXCEPTION(InvalidDeposit()); m_baseDeposit = m_deposit; m_totalDeposit = INT_MAX; } void onePath() { if (asserts(!m_totalDeposit && !m_baseDeposit)) BOOST_THROW_EXCEPTION(InvalidDeposit()); m_baseDeposit = m_deposit; m_totalDeposit = INT_MAX; }
void otherPath() { donePath(); m_totalDeposit = m_deposit; m_deposit = m_baseDeposit; } void otherPath() { donePath(); m_totalDeposit = m_deposit; m_deposit = m_baseDeposit; }

5
libevmcore/AssemblyItem.h

@ -40,8 +40,6 @@ class Assembly;
class AssemblyItem class AssemblyItem
{ {
friend class Assembly;
public: public:
enum class JumpType { Ordinary, IntoFunction, OutOfFunction }; enum class JumpType { Ordinary, IntoFunction, OutOfFunction };
@ -54,6 +52,9 @@ public:
AssemblyItemType type() const { return m_type; } AssemblyItemType type() const { return m_type; }
u256 const& data() const { return m_data; } u256 const& data() const { return m_data; }
void setType(AssemblyItemType const _type) { m_type = _type; }
void setData(u256 const& _data) { m_data = _data; }
/// @returns the instruction of this item (only valid if type() == Operation) /// @returns the instruction of this item (only valid if type() == Operation)
Instruction instruction() const { return Instruction(byte(m_data)); } Instruction instruction() const { return Instruction(byte(m_data)); }

Loading…
Cancel
Save