solidity/Assembly.h

136 lines
5.4 KiB
C
Raw Normal View History

2014-05-26 09:34:43 +00:00
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file CodeFragment.h
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#pragma once
2014-05-26 10:12:22 +00:00
#include <iostream>
2014-05-26 17:41:46 +00:00
#include <sstream>
#include <libdevcore/Common.h>
#include <libevmface/Instruction.h>
2014-05-26 09:34:43 +00:00
#include "Exceptions.h"
namespace dev
{
2014-05-26 09:34:43 +00:00
namespace eth
{
enum AssemblyItemType { UndefinedItem, Operation, Push, PushString, PushTag, PushSub, PushSubSize, Tag, PushData };
2014-05-26 09:34:43 +00:00
2014-05-26 10:12:22 +00:00
class Assembly;
2014-05-26 09:34:43 +00:00
class AssemblyItem
{
2014-05-26 10:12:22 +00:00
friend class Assembly;
2014-05-26 09:34:43 +00:00
public:
AssemblyItem(u256 _push): m_type(Push), m_data(_push) {}
AssemblyItem(Instruction _i): m_type(Operation), m_data((byte)_i) {}
2014-05-27 15:46:57 +00:00
AssemblyItem(AssemblyItemType _type, u256 _data = 0): m_type(_type), m_data(_data) {}
2014-05-26 09:34:43 +00:00
2014-05-26 17:41:46 +00:00
AssemblyItem tag() const { assert(m_type == PushTag || m_type == Tag); return AssemblyItem(Tag, m_data); }
AssemblyItem pushTag() const { assert(m_type == PushTag || m_type == Tag); return AssemblyItem(PushTag, m_data); }
2014-05-26 09:34:43 +00:00
AssemblyItemType type() const { return m_type; }
u256 data() const { return m_data; }
2014-05-26 17:41:46 +00:00
int deposit() const;
2014-05-26 10:26:08 +00:00
2014-05-27 15:46:57 +00:00
bool match(AssemblyItem const& _i) const { return _i.m_type == UndefinedItem || (m_type == _i.m_type && (m_type != Operation || m_data == _i.m_data)); }
2014-05-26 09:34:43 +00:00
private:
AssemblyItemType m_type;
u256 m_data;
};
2014-05-27 15:46:57 +00:00
typedef std::vector<AssemblyItem> AssemblyItems;
typedef vector_ref<AssemblyItem const> AssemblyItemsConstRef;
std::ostream& operator<<(std::ostream& _out, AssemblyItemsConstRef _i);
inline std::ostream& operator<<(std::ostream& _out, AssemblyItems const& _i) { return operator<<(_out, AssemblyItemsConstRef(&_i)); }
2014-05-26 09:34:43 +00:00
class Assembly
{
public:
AssemblyItem newTag() { return AssemblyItem(Tag, m_usedTags++); }
2014-05-26 17:41:46 +00:00
AssemblyItem newPushTag() { return AssemblyItem(PushTag, m_usedTags++); }
AssemblyItem newData(bytes const& _data) { h256 h = (u256)std::hash<std::string>()(asString(_data)); m_data[h] = _data; return AssemblyItem(PushData, h); }
AssemblyItem newSub(Assembly const& _sub) { h256 h = h256::random(s_fixedHashEngine); m_subs[h] = _sub; return AssemblyItem(PushSub, h); }
AssemblyItem newPushString(std::string const& _data) { h256 h = (u256)std::hash<std::string>()(_data); m_strings[h] = _data; return AssemblyItem(PushString, h); }
AssemblyItem newPushSubSize(h256 const& _subId) { return AssemblyItem(PushSubSize, _subId); }
2014-05-26 17:41:46 +00:00
AssemblyItem append() { return append(newTag()); }
void append(Assembly const& _a);
void append(Assembly const& _a, int _deposit);
AssemblyItem const& append(AssemblyItem const& _i);
AssemblyItem const& append(std::string const& _data) { return append(newPushString(_data)); }
AssemblyItem const& append(bytes const& _data) { return append(newData(_data)); }
2014-07-27 23:58:34 +00:00
AssemblyItem appendSubSize(Assembly const& _a) { auto ret = newSub(_a); append(newPushSubSize(ret.data())); return ret; }
2014-05-26 17:41:46 +00:00
AssemblyItem appendJump() { auto ret = append(newPushTag()); append(Instruction::JUMP); return ret; }
AssemblyItem appendJumpI() { auto ret = append(newPushTag()); append(Instruction::JUMPI); return ret; }
AssemblyItem appendJump(AssemblyItem const& _tag) { auto ret = append(_tag.pushTag()); append(Instruction::JUMP); return ret; }
AssemblyItem appendJumpI(AssemblyItem const& _tag) { auto ret = append(_tag.pushTag()); append(Instruction::JUMPI); return ret; }
template <class T> Assembly& operator<<(T const& _d) { append(_d); return *this; }
AssemblyItem const& back() { return m_items.back(); }
2014-05-27 17:11:37 +00:00
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(); }
2014-05-26 10:12:22 +00:00
2014-05-26 17:41:46 +00:00
void onePath() { assert(!m_totalDeposit && !m_baseDeposit); m_baseDeposit = m_deposit; m_totalDeposit = INT_MAX; }
void otherPath() { donePath(); m_totalDeposit = m_deposit; m_deposit = m_baseDeposit; }
void donePaths() { donePath(); m_totalDeposit = m_baseDeposit = 0; }
void ignored() { m_baseDeposit = m_deposit; }
void endIgnored() { m_deposit = m_baseDeposit; m_baseDeposit = 0; }
2014-05-26 10:12:22 +00:00
2014-05-26 17:41:46 +00:00
void popTo(int _deposit) { while (m_deposit > _deposit) append(Instruction::POP); }
2014-06-30 22:16:01 +00:00
void injectStart(AssemblyItem const& _i);
2014-05-26 17:41:46 +00:00
std::string out() const { std::stringstream ret; streamOut(ret); return ret.str(); }
int deposit() const { return m_deposit; }
2014-05-26 09:34:43 +00:00
bytes assemble() const;
Assembly& optimise(bool _enable);
std::ostream& streamOut(std::ostream& _out, std::string const& _prefix = "") const;
2014-05-26 09:34:43 +00:00
private:
void donePath() { if (m_totalDeposit != INT_MAX && m_totalDeposit != m_deposit) BOOST_THROW_EXCEPTION(InvalidDeposit()); }
2014-05-26 17:41:46 +00:00
unsigned bytesRequired() const;
unsigned m_usedTags = 0;
2014-05-27 15:46:57 +00:00
AssemblyItems m_items;
mutable std::map<h256, bytes> m_data;
std::map<h256, Assembly> m_subs;
2014-05-26 17:41:46 +00:00
std::map<h256, std::string> m_strings;
int m_deposit = 0;
int m_baseDeposit = 0;
int m_totalDeposit = 0;
2014-05-26 09:34:43 +00:00
};
2014-05-26 17:41:46 +00:00
inline std::ostream& operator<<(std::ostream& _out, Assembly const& _a)
{
_a.streamOut(_out);
return _out;
}
2014-05-26 09:34:43 +00:00
}
}