2014-05-26 09:22:19 +00:00
|
|
|
/*
|
2017-02-02 10:06:28 +00:00
|
|
|
This file is part of solidity.
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2017-02-02 10:06:28 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-05-26 09:22:19 +00:00
|
|
|
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.
|
|
|
|
|
2017-02-02 10:06:28 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-05-26 09:22:19 +00:00
|
|
|
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
|
2017-02-02 10:06:28 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-05-26 09:22:19 +00:00
|
|
|
*/
|
|
|
|
/** @file CodeFragment.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CodeFragment.h"
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2015-08-06 19:00:29 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2014-05-26 09:22:19 +00:00
|
|
|
#include <boost/spirit/include/support_utree.hpp>
|
2015-08-06 19:00:29 +00:00
|
|
|
#pragma warning(pop)
|
|
|
|
#pragma GCC diagnostic pop
|
2014-10-07 21:07:56 +00:00
|
|
|
#include <libdevcore/CommonIO.h>
|
2016-04-01 20:11:01 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2014-05-26 09:22:19 +00:00
|
|
|
#include "CompilerState.h"
|
2014-06-30 22:16:01 +00:00
|
|
|
#include "Parser.h"
|
2014-05-26 09:22:19 +00:00
|
|
|
using namespace std;
|
2014-09-05 15:09:58 +00:00
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::eth;
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2014-06-30 22:16:01 +00:00
|
|
|
void CodeFragment::finalise(CompilerState const& _cs)
|
|
|
|
{
|
|
|
|
if (_cs.usedAlloc && _cs.vars.size() && !m_finalised)
|
|
|
|
{
|
|
|
|
m_finalised = true;
|
|
|
|
m_asm.injectStart(Instruction::MSTORE8);
|
|
|
|
m_asm.injectStart((u256)((_cs.vars.size() + 2) * 32) - 1);
|
|
|
|
m_asm.injectStart((u256)1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 12:17:52 +00:00
|
|
|
CodeFragment::CodeFragment(sp::utree const& _t, CompilerState& _s, ReadCallback const& _readFile, bool _allowASM):
|
|
|
|
m_readFile(_readFile)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
2016-10-15 11:58:57 +00:00
|
|
|
/*
|
|
|
|
std::cout << "CodeFragment. Locals:";
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: _s.defs)
|
2016-10-15 11:58:57 +00:00
|
|
|
std::cout << i.first << ":" << i.second.m_asm.out();
|
|
|
|
std::cout << "Args:";
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: _s.args)
|
2016-10-15 11:58:57 +00:00
|
|
|
std::cout << i.first << ":" << i.second.m_asm.out();
|
|
|
|
std::cout << "Outers:";
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: _s.outers)
|
2016-10-15 11:58:57 +00:00
|
|
|
std::cout << i.first << ":" << i.second.m_asm.out();
|
|
|
|
debugOutAST(std::cout, _t);
|
|
|
|
std::cout << endl << flush;
|
2014-05-26 09:22:19 +00:00
|
|
|
*/
|
|
|
|
switch (_t.which())
|
|
|
|
{
|
|
|
|
case sp::utree_type::list_type:
|
|
|
|
constructOperation(_t, _s);
|
|
|
|
break;
|
|
|
|
case sp::utree_type::string_type:
|
|
|
|
{
|
|
|
|
auto sr = _t.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
|
|
|
|
string s(sr.begin(), sr.end());
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(s);
|
2014-05-26 09:22:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sp::utree_type::symbol_type:
|
|
|
|
{
|
|
|
|
auto sr = _t.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
|
|
|
|
string s(sr.begin(), sr.end());
|
|
|
|
string us = boost::algorithm::to_upper_copy(s);
|
2014-05-26 17:41:46 +00:00
|
|
|
if (_allowASM && c_instructions.count(us))
|
|
|
|
m_asm.append(c_instructions.at(us));
|
2014-08-21 14:14:12 +00:00
|
|
|
else if (_s.defs.count(s))
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(_s.defs.at(s).m_asm);
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (_s.args.count(s))
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(_s.args.at(s).m_asm);
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (_s.outers.count(s))
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(_s.outers.at(s).m_asm);
|
2016-11-01 02:06:40 +00:00
|
|
|
else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-") == string::npos)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
auto it = _s.vars.find(s);
|
|
|
|
if (it == _s.vars.end())
|
2016-11-01 02:07:20 +00:00
|
|
|
error<InvalidName>(std::string("Symbol not found: ") + s);
|
2014-06-11 18:25:21 +00:00
|
|
|
m_asm.append((u256)it->second.first);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else
|
2016-11-01 02:07:20 +00:00
|
|
|
error<BareSymbol>(s);
|
2014-05-26 09:22:19 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case sp::utree_type::any_type:
|
|
|
|
{
|
|
|
|
bigint i = *_t.get<bigint*>();
|
|
|
|
if (i < 0 || i > bigint(u256(0) - 1))
|
2017-10-02 10:48:26 +00:00
|
|
|
error<IntegerOutOfRange>(toString(i));
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append((u256)i);
|
2014-05-26 09:22:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-11-01 02:07:20 +00:00
|
|
|
default:
|
|
|
|
error<CompilerException>("Unexpected fragment type");
|
|
|
|
break;
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 09:25:06 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
/// Returns true iff the instruction is valid as a function.
|
|
|
|
bool validFunctionalInstruction(string us)
|
|
|
|
{
|
|
|
|
auto it = c_instructions.find(us);
|
|
|
|
return !(
|
|
|
|
it == c_instructions.end() ||
|
|
|
|
solidity::isPushInstruction(it->second) ||
|
|
|
|
solidity::isDupInstruction(it->second) ||
|
|
|
|
solidity::isSwapInstruction(it->second) ||
|
|
|
|
it->second == solidity::Instruction::JUMPDEST
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 09:22:19 +00:00
|
|
|
void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
|
|
|
|
{
|
2014-05-26 18:09:15 +00:00
|
|
|
if (_t.tag() == 0 && _t.empty())
|
2014-05-26 09:22:19 +00:00
|
|
|
error<EmptyList>();
|
|
|
|
else if (_t.tag() == 0 && _t.front().which() != sp::utree_type::symbol_type)
|
|
|
|
error<DataNotExecutable>();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string s;
|
|
|
|
string us;
|
|
|
|
switch (_t.tag())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
auto sr = _t.front().get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
|
|
|
|
s = string(sr.begin(), sr.end());
|
|
|
|
us = boost::algorithm::to_upper_copy(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
us = "MLOAD";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
us = "SLOAD";
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
us = "MSTORE";
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
us = "SSTORE";
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
us = "SEQ";
|
|
|
|
break;
|
2014-07-06 02:23:13 +00:00
|
|
|
case 6:
|
|
|
|
us = "CALLDATALOAD";
|
|
|
|
break;
|
2014-05-26 09:22:19 +00:00
|
|
|
default:;
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:25:21 +00:00
|
|
|
auto firstAsString = [&]()
|
|
|
|
{
|
|
|
|
auto i = *++_t.begin();
|
|
|
|
if (i.tag())
|
2017-10-02 10:48:26 +00:00
|
|
|
error<InvalidName>(toString(i));
|
2014-06-11 18:25:21 +00:00
|
|
|
if (i.which() == sp::utree_type::string_type)
|
|
|
|
{
|
|
|
|
auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
|
|
|
|
return string(sr.begin(), sr.end());
|
|
|
|
}
|
|
|
|
else if (i.which() == sp::utree_type::symbol_type)
|
|
|
|
{
|
|
|
|
auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
|
|
|
|
return _s.getDef(string(sr.begin(), sr.end())).m_asm.backString();
|
|
|
|
}
|
|
|
|
return string();
|
|
|
|
};
|
|
|
|
|
2017-06-21 23:43:01 +00:00
|
|
|
auto varAddress = [&](string const& n, bool createMissing = false)
|
2014-06-11 18:25:21 +00:00
|
|
|
{
|
2017-06-21 23:40:55 +00:00
|
|
|
if (n.empty())
|
|
|
|
error<InvalidName>("Empty variable name not allowed");
|
2014-06-11 18:25:21 +00:00
|
|
|
auto it = _s.vars.find(n);
|
|
|
|
if (it == _s.vars.end())
|
2017-06-21 23:43:01 +00:00
|
|
|
{
|
|
|
|
if (createMissing)
|
|
|
|
{
|
|
|
|
// Create new variable
|
|
|
|
bool ok;
|
|
|
|
tie(it, ok) = _s.vars.insert(make_pair(n, make_pair(_s.stackSize, 32)));
|
|
|
|
_s.stackSize += 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error<InvalidName>(std::string("Symbol not found: ") + n);
|
|
|
|
}
|
2014-06-11 18:25:21 +00:00
|
|
|
return it->second.first;
|
|
|
|
};
|
|
|
|
|
2014-05-26 09:22:19 +00:00
|
|
|
// Operations who args are not standard stack-pushers.
|
|
|
|
bool nonStandard = true;
|
|
|
|
if (us == "ASM")
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
for (auto const& i: _t)
|
|
|
|
if (c++)
|
2017-10-02 12:17:52 +00:00
|
|
|
m_asm.append(CodeFragment(i, _s, m_readFile, true).m_asm);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "INCLUDE")
|
|
|
|
{
|
|
|
|
if (_t.size() != 2)
|
2017-07-15 21:17:20 +00:00
|
|
|
error<IncorrectParameterCount>(us);
|
2017-06-22 01:29:01 +00:00
|
|
|
string fileName = firstAsString();
|
|
|
|
if (fileName.empty())
|
|
|
|
error<InvalidName>("Empty file name provided");
|
2017-10-03 09:22:30 +00:00
|
|
|
if (!m_readFile)
|
|
|
|
error<InvalidName>("Import callback not present");
|
2017-10-02 12:17:52 +00:00
|
|
|
string contents = m_readFile(fileName);
|
2017-06-22 01:29:01 +00:00
|
|
|
if (contents.empty())
|
|
|
|
error<InvalidName>(std::string("File not found (or empty): ") + fileName);
|
2017-10-02 12:17:52 +00:00
|
|
|
m_asm.append(CodeFragment::compile(contents, _s, m_readFile).m_asm);
|
2014-06-11 18:25:21 +00:00
|
|
|
}
|
|
|
|
else if (us == "SET")
|
|
|
|
{
|
|
|
|
if (_t.size() != 3)
|
2017-07-15 21:17:20 +00:00
|
|
|
error<IncorrectParameterCount>(us);
|
2014-06-11 18:25:21 +00:00
|
|
|
int c = 0;
|
|
|
|
for (auto const& i: _t)
|
|
|
|
if (c++ == 2)
|
2017-10-02 12:17:52 +00:00
|
|
|
m_asm.append(CodeFragment(i, _s, m_readFile, false).m_asm);
|
2017-06-21 23:43:01 +00:00
|
|
|
m_asm.append((u256)varAddress(firstAsString(), true));
|
2014-06-11 18:25:21 +00:00
|
|
|
m_asm.append(Instruction::MSTORE);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2014-06-11 18:25:21 +00:00
|
|
|
else if (us == "GET")
|
|
|
|
{
|
|
|
|
if (_t.size() != 2)
|
2017-07-15 21:17:20 +00:00
|
|
|
error<IncorrectParameterCount>(us);
|
2014-06-11 18:25:21 +00:00
|
|
|
m_asm.append((u256)varAddress(firstAsString()));
|
|
|
|
m_asm.append(Instruction::MLOAD);
|
|
|
|
}
|
|
|
|
else if (us == "REF")
|
|
|
|
m_asm.append((u256)varAddress(firstAsString()));
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (us == "DEF")
|
|
|
|
{
|
|
|
|
string n;
|
|
|
|
unsigned ii = 0;
|
|
|
|
if (_t.size() != 3 && _t.size() != 4)
|
2017-07-15 21:17:20 +00:00
|
|
|
error<IncorrectParameterCount>(us);
|
2014-05-30 12:55:07 +00:00
|
|
|
vector<string> args;
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: _t)
|
|
|
|
{
|
|
|
|
if (ii == 1)
|
|
|
|
{
|
|
|
|
if (i.tag())
|
2017-10-02 10:48:26 +00:00
|
|
|
error<InvalidName>(toString(i));
|
2014-05-26 09:22:19 +00:00
|
|
|
if (i.which() == sp::utree_type::string_type)
|
|
|
|
{
|
|
|
|
auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
|
|
|
|
n = string(sr.begin(), sr.end());
|
|
|
|
}
|
|
|
|
else if (i.which() == sp::utree_type::symbol_type)
|
|
|
|
{
|
|
|
|
auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
|
2014-05-26 17:41:46 +00:00
|
|
|
n = _s.getDef(string(sr.begin(), sr.end())).m_asm.backString();
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ii == 2)
|
|
|
|
if (_t.size() == 3)
|
2017-06-24 11:56:21 +00:00
|
|
|
{
|
|
|
|
/// NOTE: some compilers could do the assignment first if this is done in a single line
|
2017-10-02 12:17:52 +00:00
|
|
|
CodeFragment code = CodeFragment(i, _s, m_readFile);
|
2017-06-24 11:56:21 +00:00
|
|
|
_s.defs[n] = code;
|
|
|
|
}
|
2014-05-26 09:22:19 +00:00
|
|
|
else
|
|
|
|
for (auto const& j: i)
|
|
|
|
{
|
|
|
|
if (j.tag() || j.which() != sp::utree_type::symbol_type)
|
|
|
|
error<InvalidMacroArgs>();
|
|
|
|
auto sr = j.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>();
|
2014-05-30 12:55:07 +00:00
|
|
|
args.push_back(string(sr.begin(), sr.end()));
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (ii == 3)
|
|
|
|
{
|
2014-05-30 12:55:07 +00:00
|
|
|
auto k = make_pair(n, args.size());
|
|
|
|
_s.macros[k].code = i;
|
|
|
|
_s.macros[k].env = _s.outers;
|
|
|
|
_s.macros[k].args = args;
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: _s.args)
|
2014-05-30 12:55:07 +00:00
|
|
|
_s.macros[k].env[i.first] = i.second;
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: _s.defs)
|
2014-05-30 12:55:07 +00:00
|
|
|
_s.macros[k].env[i.first] = i.second;
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
++ii;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (us == "LIT")
|
|
|
|
{
|
|
|
|
if (_t.size() < 3)
|
2017-07-15 21:17:20 +00:00
|
|
|
error<IncorrectParameterCount>(us);
|
2014-05-26 09:22:19 +00:00
|
|
|
unsigned ii = 0;
|
|
|
|
CodeFragment pos;
|
|
|
|
bytes data;
|
|
|
|
for (auto const& i: _t)
|
|
|
|
{
|
2016-11-08 17:16:45 +00:00
|
|
|
if (ii == 0)
|
|
|
|
{
|
|
|
|
ii++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ii == 1)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
2017-10-02 12:17:52 +00:00
|
|
|
pos = CodeFragment(i, _s, m_readFile);
|
2014-05-26 17:41:46 +00:00
|
|
|
if (pos.m_asm.deposit() != 1)
|
2017-10-02 10:48:26 +00:00
|
|
|
error<InvalidDeposit>(toString(i));
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2016-11-08 17:16:45 +00:00
|
|
|
else if (i.tag() != 0)
|
|
|
|
{
|
2017-10-02 10:48:26 +00:00
|
|
|
error<InvalidLiteral>(toString(i));
|
2016-11-08 17:16:45 +00:00
|
|
|
}
|
|
|
|
else if (i.which() == sp::utree_type::string_type)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
auto sr = i.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::string_type>>();
|
2016-10-31 23:15:57 +00:00
|
|
|
data.insert(data.end(), (byte const *)sr.begin(), (byte const*)sr.end());
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2016-11-08 17:16:45 +00:00
|
|
|
else if (i.which() == sp::utree_type::any_type)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
bigint bi = *i.get<bigint*>();
|
|
|
|
if (bi < 0)
|
2017-10-02 10:48:26 +00:00
|
|
|
error<IntegerOutOfRange>(toString(i));
|
2014-05-26 09:22:19 +00:00
|
|
|
else
|
|
|
|
{
|
2016-10-31 23:17:09 +00:00
|
|
|
bytes tmp = toCompactBigEndian(bi);
|
|
|
|
data.insert(data.end(), tmp.begin(), tmp.end());
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 17:16:45 +00:00
|
|
|
else
|
|
|
|
{
|
2017-10-02 10:48:26 +00:00
|
|
|
error<InvalidLiteral>(toString(i));
|
2016-11-08 17:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ii++;
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append((u256)data.size());
|
2014-08-21 13:53:59 +00:00
|
|
|
m_asm.append(Instruction::DUP1);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(data);
|
|
|
|
m_asm.append(pos.m_asm, 1);
|
|
|
|
m_asm.append(Instruction::CODECOPY);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
nonStandard = false;
|
|
|
|
|
|
|
|
if (nonStandard)
|
|
|
|
return;
|
|
|
|
|
2016-10-26 00:16:29 +00:00
|
|
|
std::map<std::string, Instruction> const c_arith = {
|
|
|
|
{ "+", Instruction::ADD },
|
|
|
|
{ "-", Instruction::SUB },
|
|
|
|
{ "*", Instruction::MUL },
|
|
|
|
{ "/", Instruction::DIV },
|
|
|
|
{ "%", Instruction::MOD },
|
|
|
|
{ "&", Instruction::AND },
|
|
|
|
{ "|", Instruction::OR },
|
|
|
|
{ "^", Instruction::XOR }
|
|
|
|
};
|
|
|
|
std::map<std::string, pair<Instruction, bool>> const c_binary = {
|
|
|
|
{ "<", { Instruction::LT, false } },
|
|
|
|
{ "<=", { Instruction::GT, true } },
|
|
|
|
{ ">", { Instruction::GT, false } },
|
|
|
|
{ ">=", { Instruction::LT, true } },
|
|
|
|
{ "S<", { Instruction::SLT, false } },
|
|
|
|
{ "S<=", { Instruction::SGT, true } },
|
|
|
|
{ "S>", { Instruction::SGT, false } },
|
|
|
|
{ "S>=", { Instruction::SLT, true } },
|
|
|
|
{ "=", { Instruction::EQ, false } },
|
|
|
|
{ "!=", { Instruction::EQ, true } }
|
|
|
|
};
|
|
|
|
std::map<std::string, Instruction> const c_unary = {
|
|
|
|
{ "!", Instruction::ISZERO },
|
|
|
|
{ "~", Instruction::NOT }
|
|
|
|
};
|
2014-05-26 09:22:19 +00:00
|
|
|
|
|
|
|
vector<CodeFragment> code;
|
|
|
|
CompilerState ns = _s;
|
|
|
|
ns.vars.clear();
|
2014-06-30 22:16:01 +00:00
|
|
|
ns.usedAlloc = false;
|
2014-05-26 09:22:19 +00:00
|
|
|
int c = _t.tag() ? 1 : 0;
|
|
|
|
for (auto const& i: _t)
|
|
|
|
if (c++)
|
|
|
|
{
|
|
|
|
if (us == "LLL" && c == 1)
|
2017-10-02 12:17:52 +00:00
|
|
|
code.push_back(CodeFragment(i, ns, m_readFile));
|
2014-05-26 09:22:19 +00:00
|
|
|
else
|
2017-10-02 12:17:52 +00:00
|
|
|
code.push_back(CodeFragment(i, _s, m_readFile));
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2017-07-15 21:17:20 +00:00
|
|
|
auto requireSize = [&](unsigned s) { if (code.size() != s) error<IncorrectParameterCount>(us); };
|
|
|
|
auto requireMinSize = [&](unsigned s) { if (code.size() < s) error<IncorrectParameterCount>(us); };
|
|
|
|
auto requireMaxSize = [&](unsigned s) { if (code.size() > s) error<IncorrectParameterCount>(us); };
|
|
|
|
auto requireDeposit = [&](unsigned i, int s) { if (code[i].m_asm.deposit() != s) error<InvalidDeposit>(us); };
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2014-05-30 12:55:07 +00:00
|
|
|
if (_s.macros.count(make_pair(s, code.size())))
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
2014-05-30 12:55:07 +00:00
|
|
|
Macro const& m = _s.macros.at(make_pair(s, code.size()));
|
2014-05-26 09:22:19 +00:00
|
|
|
CompilerState cs = _s;
|
|
|
|
for (auto const& i: m.env)
|
|
|
|
cs.outers[i.first] = i.second;
|
|
|
|
for (auto const& i: cs.defs)
|
|
|
|
cs.outers[i.first] = i.second;
|
|
|
|
cs.defs.clear();
|
|
|
|
for (unsigned i = 0; i < m.args.size(); ++i)
|
|
|
|
{
|
2014-06-12 17:35:39 +00:00
|
|
|
//requireDeposit(i, 1);
|
2014-05-26 09:22:19 +00:00
|
|
|
cs.args[m.args[i]] = code[i];
|
|
|
|
}
|
2017-10-02 12:17:52 +00:00
|
|
|
m_asm.append(CodeFragment(m.code, cs, m_readFile).m_asm);
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto const& i: cs.defs)
|
|
|
|
_s.defs[i.first] = i.second;
|
|
|
|
for (auto const& i: cs.macros)
|
|
|
|
_s.macros.insert(i);
|
|
|
|
}
|
2017-10-02 09:25:06 +00:00
|
|
|
else if (c_instructions.count(us) && validFunctionalInstruction(us))
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
auto it = c_instructions.find(us);
|
2017-06-25 11:23:34 +00:00
|
|
|
requireSize(instructionInfo(it->second).args);
|
2014-05-26 09:22:19 +00:00
|
|
|
|
|
|
|
for (unsigned i = code.size(); i; --i)
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[i - 1].m_asm, 1);
|
|
|
|
m_asm.append(it->second);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (c_arith.count(us))
|
|
|
|
{
|
|
|
|
auto it = c_arith.find(us);
|
|
|
|
requireMinSize(1);
|
|
|
|
for (unsigned i = code.size(); i; --i)
|
|
|
|
{
|
|
|
|
requireDeposit(i - 1, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[i - 1].m_asm, 1);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
for (unsigned i = 1; i < code.size(); ++i)
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(it->second);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (c_binary.count(us))
|
|
|
|
{
|
|
|
|
auto it = c_binary.find(us);
|
|
|
|
requireSize(2);
|
|
|
|
requireDeposit(0, 1);
|
|
|
|
requireDeposit(1, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[1].m_asm, 1);
|
|
|
|
m_asm.append(code[0].m_asm, 1);
|
|
|
|
m_asm.append(it->second.first);
|
2014-05-26 09:22:19 +00:00
|
|
|
if (it->second.second)
|
2014-10-31 13:32:32 +00:00
|
|
|
m_asm.append(Instruction::ISZERO);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (c_unary.count(us))
|
|
|
|
{
|
|
|
|
auto it = c_unary.find(us);
|
|
|
|
requireSize(1);
|
|
|
|
requireDeposit(0, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[0].m_asm, 1);
|
|
|
|
m_asm.append(it->second);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "IF")
|
|
|
|
{
|
|
|
|
requireSize(3);
|
|
|
|
requireDeposit(0, 1);
|
2014-05-26 18:09:15 +00:00
|
|
|
int minDep = min(code[1].m_asm.deposit(), code[2].m_asm.deposit());
|
2014-05-26 17:41:46 +00:00
|
|
|
|
|
|
|
m_asm.append(code[0].m_asm);
|
2017-06-14 12:13:22 +00:00
|
|
|
auto mainBranch = m_asm.appendJumpI();
|
|
|
|
|
|
|
|
/// The else branch.
|
|
|
|
int startDeposit = m_asm.deposit();
|
2014-05-26 18:09:15 +00:00
|
|
|
m_asm.append(code[2].m_asm, minDep);
|
2014-05-26 17:41:46 +00:00
|
|
|
auto end = m_asm.appendJump();
|
2017-06-14 12:13:22 +00:00
|
|
|
int deposit = m_asm.deposit();
|
|
|
|
m_asm.setDeposit(startDeposit);
|
|
|
|
|
|
|
|
/// The main branch.
|
|
|
|
m_asm << mainBranch.tag();
|
2014-05-26 18:09:15 +00:00
|
|
|
m_asm.append(code[1].m_asm, minDep);
|
|
|
|
m_asm << end.tag();
|
2017-06-14 12:13:22 +00:00
|
|
|
if (m_asm.deposit() != deposit)
|
2017-07-15 21:17:20 +00:00
|
|
|
error<InvalidDeposit>(us);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "WHEN" || us == "UNLESS")
|
|
|
|
{
|
|
|
|
requireSize(2);
|
|
|
|
requireDeposit(0, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
|
|
|
|
m_asm.append(code[0].m_asm);
|
2014-05-26 09:22:19 +00:00
|
|
|
if (us == "WHEN")
|
2014-10-31 13:32:32 +00:00
|
|
|
m_asm.append(Instruction::ISZERO);
|
2014-05-26 17:41:46 +00:00
|
|
|
auto end = m_asm.appendJumpI();
|
2014-05-29 21:21:51 +00:00
|
|
|
m_asm.append(code[1].m_asm, 0);
|
|
|
|
m_asm << end.tag();
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2016-10-26 13:24:01 +00:00
|
|
|
else if (us == "WHILE" || us == "UNTIL")
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
requireSize(2);
|
|
|
|
requireDeposit(0, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
auto begin = m_asm.append(m_asm.newTag());
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[0].m_asm);
|
2016-10-26 13:24:01 +00:00
|
|
|
if (us == "WHILE")
|
|
|
|
m_asm.append(Instruction::ISZERO);
|
2014-05-26 17:41:46 +00:00
|
|
|
auto end = m_asm.appendJumpI();
|
|
|
|
m_asm.append(code[1].m_asm, 0);
|
|
|
|
m_asm.appendJump(begin);
|
|
|
|
m_asm << end.tag();
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "FOR")
|
|
|
|
{
|
|
|
|
requireSize(4);
|
|
|
|
requireDeposit(1, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
|
|
|
|
m_asm.append(code[0].m_asm, 0);
|
2016-11-10 17:16:21 +00:00
|
|
|
auto begin = m_asm.append(m_asm.newTag());
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[1].m_asm);
|
2014-10-31 13:32:32 +00:00
|
|
|
m_asm.append(Instruction::ISZERO);
|
2014-05-26 17:41:46 +00:00
|
|
|
auto end = m_asm.appendJumpI();
|
|
|
|
m_asm.append(code[3].m_asm, 0);
|
|
|
|
m_asm.append(code[2].m_asm, 0);
|
|
|
|
m_asm.appendJump(begin);
|
|
|
|
m_asm << end.tag();
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2017-07-22 06:57:38 +00:00
|
|
|
else if (us == "SWITCH")
|
|
|
|
{
|
|
|
|
requireMinSize(1);
|
|
|
|
|
|
|
|
bool hasDefault = (code.size() % 2 == 1);
|
|
|
|
int startDeposit = m_asm.deposit();
|
|
|
|
int targetDeposit = hasDefault ? code[code.size() - 1].m_asm.deposit() : 0;
|
|
|
|
|
|
|
|
// The conditions
|
|
|
|
AssemblyItems jumpTags;
|
|
|
|
for (unsigned i = 0; i < code.size() - 1; i += 2)
|
|
|
|
{
|
|
|
|
requireDeposit(i, 1);
|
|
|
|
m_asm.append(code[i].m_asm);
|
|
|
|
jumpTags.push_back(m_asm.appendJumpI());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default, if present
|
|
|
|
if (hasDefault)
|
|
|
|
m_asm.append(code[code.size() - 1].m_asm);
|
|
|
|
|
|
|
|
// The targets - appending in reverse makes the top case the most efficient.
|
|
|
|
if (code.size() > 1)
|
|
|
|
{
|
|
|
|
auto end = m_asm.appendJump();
|
|
|
|
for (int i = 2 * (code.size() / 2 - 1); i >= 0; i -= 2)
|
|
|
|
{
|
|
|
|
m_asm << jumpTags[i / 2].tag();
|
|
|
|
requireDeposit(i + 1, targetDeposit);
|
|
|
|
m_asm.append(code[i + 1].m_asm);
|
|
|
|
if (i != 0)
|
|
|
|
m_asm.appendJump(end);
|
|
|
|
}
|
|
|
|
m_asm << end.tag();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_asm.setDeposit(startDeposit + targetDeposit);
|
|
|
|
}
|
2014-06-30 22:16:01 +00:00
|
|
|
else if (us == "ALLOC")
|
|
|
|
{
|
|
|
|
requireSize(1);
|
|
|
|
requireDeposit(0, 1);
|
|
|
|
|
2017-07-11 20:22:38 +00:00
|
|
|
// (alloc N):
|
|
|
|
// - Evaluates to (msize) before the allocation - the start of the allocated memory
|
|
|
|
// - Does not allocate memory when N is zero
|
|
|
|
// - Size of memory allocated is N bytes rounded up to a multiple of 32
|
|
|
|
// - Uses MLOAD to expand MSIZE to avoid modifying memory.
|
|
|
|
|
|
|
|
auto end = m_asm.newTag();
|
|
|
|
m_asm.append(Instruction::MSIZE); // Result will be original top of memory
|
|
|
|
m_asm.append(code[0].m_asm, 1); // The alloc argument N
|
|
|
|
m_asm.append(Instruction::DUP1);
|
|
|
|
m_asm.append(Instruction::ISZERO);// (alloc 0) does not change MSIZE
|
|
|
|
m_asm.appendJumpI(end);
|
2014-06-30 22:16:01 +00:00
|
|
|
m_asm.append(u256(1));
|
2017-07-11 20:22:38 +00:00
|
|
|
m_asm.append(Instruction::DUP2); // Copy N
|
|
|
|
m_asm.append(Instruction::SUB); // N-1
|
|
|
|
m_asm.append(u256(0x1f)); // Bit mask
|
|
|
|
m_asm.append(Instruction::NOT); // Invert
|
|
|
|
m_asm.append(Instruction::AND); // Align N-1 on 32 byte boundary
|
|
|
|
m_asm.append(Instruction::MSIZE); // MSIZE is cheap
|
2014-06-30 22:16:01 +00:00
|
|
|
m_asm.append(Instruction::ADD);
|
2017-07-11 20:22:38 +00:00
|
|
|
m_asm.append(Instruction::MLOAD); // Updates MSIZE
|
|
|
|
m_asm.append(Instruction::POP); // Discard the result of the MLOAD
|
|
|
|
m_asm.append(end);
|
|
|
|
m_asm.append(Instruction::POP); // Discard duplicate N
|
2014-06-30 22:16:01 +00:00
|
|
|
|
|
|
|
_s.usedAlloc = true;
|
|
|
|
}
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (us == "LLL")
|
|
|
|
{
|
|
|
|
requireMinSize(2);
|
|
|
|
requireMaxSize(3);
|
|
|
|
requireDeposit(1, 1);
|
|
|
|
|
2017-06-14 19:38:59 +00:00
|
|
|
auto subPush = m_asm.appendSubroutine(make_shared<Assembly>(code[0].assembly(ns)));
|
2014-08-21 13:53:59 +00:00
|
|
|
m_asm.append(Instruction::DUP1);
|
2014-05-26 09:22:19 +00:00
|
|
|
if (code.size() == 3)
|
|
|
|
{
|
|
|
|
requireDeposit(2, 1);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[2].m_asm, 1);
|
|
|
|
m_asm.append(Instruction::LT);
|
2014-10-31 13:32:32 +00:00
|
|
|
m_asm.append(Instruction::ISZERO);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(Instruction::MUL);
|
2014-08-21 13:53:59 +00:00
|
|
|
m_asm.append(Instruction::DUP1);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2014-07-27 11:09:36 +00:00
|
|
|
m_asm.append(subPush);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[1].m_asm, 1);
|
|
|
|
m_asm.append(Instruction::CODECOPY);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "&&" || us == "||")
|
|
|
|
{
|
|
|
|
requireMinSize(1);
|
|
|
|
for (unsigned i = 0; i < code.size(); ++i)
|
|
|
|
requireDeposit(i, 1);
|
|
|
|
|
2014-05-26 17:41:46 +00:00
|
|
|
auto end = m_asm.newTag();
|
2014-05-26 09:22:19 +00:00
|
|
|
if (code.size() > 1)
|
|
|
|
{
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append((u256)(us == "||" ? 1 : 0));
|
2014-05-26 09:22:19 +00:00
|
|
|
for (unsigned i = 1; i < code.size(); ++i)
|
|
|
|
{
|
|
|
|
// Check if true - predicate
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code[i - 1].m_asm, 1);
|
2014-05-26 09:22:19 +00:00
|
|
|
if (us == "&&")
|
2014-10-31 13:32:32 +00:00
|
|
|
m_asm.append(Instruction::ISZERO);
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.appendJumpI(end);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(Instruction::POP);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if true - predicate
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(code.back().m_asm, 1);
|
2014-05-26 09:22:19 +00:00
|
|
|
|
|
|
|
// At end now.
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(end);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "SEQ")
|
|
|
|
{
|
|
|
|
unsigned ii = 0;
|
|
|
|
for (auto const& i: code)
|
|
|
|
if (++ii < code.size())
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(i.m_asm, 0);
|
2014-05-26 09:22:19 +00:00
|
|
|
else
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(i.m_asm);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
else if (us == "RAW")
|
|
|
|
{
|
|
|
|
for (auto const& i: code)
|
2014-05-26 17:41:46 +00:00
|
|
|
m_asm.append(i.m_asm);
|
2017-06-14 19:26:26 +00:00
|
|
|
// Leave only the last item on stack.
|
|
|
|
while (m_asm.deposit() > 1)
|
|
|
|
m_asm.append(Instruction::POP);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2016-10-23 12:51:41 +00:00
|
|
|
else if (us == "BYTECODESIZE")
|
|
|
|
{
|
|
|
|
m_asm.appendProgramSize();
|
|
|
|
}
|
2016-11-01 02:06:40 +00:00
|
|
|
else if (us.find_first_of("1234567890") != 0 && us.find_first_not_of("QWERTYUIOPASDFGHJKLZXCVBNM1234567890_-") == string::npos)
|
2014-06-11 18:25:21 +00:00
|
|
|
m_asm.append((u256)varAddress(s));
|
2014-05-26 09:22:19 +00:00
|
|
|
else
|
2016-10-26 12:48:13 +00:00
|
|
|
error<InvalidOperation>("Unsupported keyword: '" + us + "'");
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-26 17:41:46 +00:00
|
|
|
|
2017-10-02 12:17:52 +00:00
|
|
|
CodeFragment CodeFragment::compile(string const& _src, CompilerState& _s, ReadCallback const& _readFile)
|
2014-05-26 17:41:46 +00:00
|
|
|
{
|
|
|
|
CodeFragment ret;
|
|
|
|
sp::utree o;
|
|
|
|
parseTreeLLL(_src, o);
|
|
|
|
if (!o.empty())
|
2017-10-02 12:17:52 +00:00
|
|
|
ret = CodeFragment(o, _s, _readFile);
|
2014-05-26 17:41:46 +00:00
|
|
|
_s.treesToKill.push_back(o);
|
|
|
|
return ret;
|
|
|
|
}
|