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 Compiler.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
*/
|
|
|
|
|
2018-03-01 11:06:36 +00:00
|
|
|
#include <liblll/Compiler.h>
|
|
|
|
#include <liblll/Parser.h>
|
|
|
|
#include <liblll/CompilerState.h>
|
|
|
|
#include <liblll/CodeFragment.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
|
|
|
|
2018-03-01 11:06:36 +00:00
|
|
|
bytes dev::eth::compileLLL(string const& _src, dev::solidity::EVMVersion _evmVersion, bool _opt, std::vector<std::string>* _errors, dev::eth::ReadCallback const& _readFile)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
CompilerState cs;
|
2014-05-30 12:47:13 +00:00
|
|
|
cs.populateStandard();
|
2017-10-02 12:17:52 +00:00
|
|
|
auto assembly = CodeFragment::compile(_src, cs, _readFile).assembly(cs);
|
2017-06-30 09:21:06 +00:00
|
|
|
if (_opt)
|
2018-03-01 11:06:36 +00:00
|
|
|
assembly = assembly.optimise(true, _evmVersion);
|
2017-06-30 09:21:06 +00:00
|
|
|
bytes ret = assembly.assemble().bytecode;
|
2014-05-26 09:22:19 +00:00
|
|
|
for (auto i: cs.treesToKill)
|
|
|
|
killBigints(i);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch (Exception const& _e)
|
|
|
|
{
|
|
|
|
if (_errors)
|
2014-10-02 12:20:33 +00:00
|
|
|
{
|
|
|
|
_errors->push_back("Parse error.");
|
2016-10-15 01:27:54 +00:00
|
|
|
_errors->push_back(boost::diagnostic_information(_e));
|
2014-10-02 12:20:33 +00:00
|
|
|
}
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
2016-10-15 01:27:54 +00:00
|
|
|
catch (std::exception const& _e)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
if (_errors)
|
2016-10-15 01:27:54 +00:00
|
|
|
{
|
|
|
|
_errors->push_back("Parse exception.");
|
|
|
|
_errors->push_back(boost::diagnostic_information(_e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (_errors)
|
2016-10-26 13:28:19 +00:00
|
|
|
_errors->push_back("Internal compiler exception.");
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
return bytes();
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:06:36 +00:00
|
|
|
std::string dev::eth::compileLLLToAsm(std::string const& _src, EVMVersion _evmVersion, bool _opt, std::vector<std::string>* _errors, ReadCallback const& _readFile)
|
2014-05-26 17:41:46 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
CompilerState cs;
|
2014-05-30 12:47:13 +00:00
|
|
|
cs.populateStandard();
|
2017-10-02 12:17:52 +00:00
|
|
|
auto assembly = CodeFragment::compile(_src, cs, _readFile).assembly(cs);
|
2017-06-30 09:21:06 +00:00
|
|
|
if (_opt)
|
2018-03-01 11:06:36 +00:00
|
|
|
assembly = assembly.optimise(true, _evmVersion);
|
2017-08-30 01:17:15 +00:00
|
|
|
string ret = assembly.assemblyString();
|
2014-05-26 17:41:46 +00:00
|
|
|
for (auto i: cs.treesToKill)
|
|
|
|
killBigints(i);
|
2017-08-30 01:17:15 +00:00
|
|
|
return ret;
|
2014-05-26 17:41:46 +00:00
|
|
|
}
|
|
|
|
catch (Exception const& _e)
|
|
|
|
{
|
|
|
|
if (_errors)
|
2016-10-15 01:27:54 +00:00
|
|
|
{
|
|
|
|
_errors->push_back("Parse error.");
|
|
|
|
_errors->push_back(boost::diagnostic_information(_e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception const& _e)
|
|
|
|
{
|
|
|
|
if (_errors) {
|
|
|
|
_errors->push_back("Parse exception.");
|
|
|
|
_errors->push_back(boost::diagnostic_information(_e));
|
|
|
|
}
|
2014-05-26 17:41:46 +00:00
|
|
|
}
|
2016-10-15 01:27:54 +00:00
|
|
|
catch (...)
|
2014-05-26 17:41:46 +00:00
|
|
|
{
|
|
|
|
if (_errors)
|
2016-10-26 13:28:19 +00:00
|
|
|
_errors->push_back("Internal compiler exception.");
|
2014-05-26 17:41:46 +00:00
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2014-09-05 15:09:58 +00:00
|
|
|
string dev::eth::parseLLL(string const& _src)
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
2016-11-29 22:04:51 +00:00
|
|
|
sp::utree o;
|
|
|
|
|
2014-07-03 13:00:22 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
parseTreeLLL(_src, o);
|
2016-11-29 22:04:51 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2016-11-30 15:06:13 +00:00
|
|
|
killBigints(o);
|
|
|
|
return string();
|
2016-11-29 22:04:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostringstream ret;
|
|
|
|
debugOutAST(ret, o);
|
|
|
|
killBigints(o);
|
2016-11-30 15:06:13 +00:00
|
|
|
return ret.str();
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|