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 main.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
* Ethereum client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2016-08-30 11:29:37 +00:00
|
|
|
#include <clocale>
|
2014-05-26 09:22:19 +00:00
|
|
|
#include <liblll/Compiler.h>
|
2014-09-05 16:24:29 +00:00
|
|
|
#include <libdevcore/CommonIO.h>
|
|
|
|
#include <libdevcore/CommonData.h>
|
2016-04-01 20:11:01 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2016-11-01 12:02:08 +00:00
|
|
|
#include <solidity/BuildInfo.h>
|
|
|
|
|
2014-05-26 09:22:19 +00:00
|
|
|
using namespace std;
|
2014-09-05 15:09:58 +00:00
|
|
|
using namespace dev;
|
2016-04-02 12:56:43 +00:00
|
|
|
using namespace dev::solidity;
|
2014-09-05 15:09:58 +00:00
|
|
|
using namespace dev::eth;
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2016-11-01 12:02:08 +00:00
|
|
|
static string const VersionString =
|
2017-10-18 11:32:34 +00:00
|
|
|
string(ETH_PROJECT_VERSION) +
|
|
|
|
(string(SOL_VERSION_PRERELEASE).empty() ? "" : "-" + string(SOL_VERSION_PRERELEASE)) +
|
|
|
|
(string(SOL_VERSION_BUILDINFO).empty() ? "" : "+" + string(SOL_VERSION_BUILDINFO));
|
2016-11-01 12:02:08 +00:00
|
|
|
|
2017-08-29 00:01:47 +00:00
|
|
|
static void help()
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
|
|
|
cout
|
|
|
|
<< "Usage lllc [OPTIONS] <file>" << endl
|
2017-10-18 11:32:34 +00:00
|
|
|
<< "Options:" << endl
|
2014-05-26 09:34:43 +00:00
|
|
|
<< " -b,--binary Parse, compile and assemble; output byte code in binary." << endl
|
|
|
|
<< " -x,--hex Parse, compile and assemble; output byte code in hex." << endl
|
2014-05-26 17:41:46 +00:00
|
|
|
<< " -a,--assembly Only parse and compile; show assembly." << endl
|
2014-05-26 09:34:43 +00:00
|
|
|
<< " -t,--parse-tree Only parse; show parse tree." << endl
|
2016-10-25 01:15:21 +00:00
|
|
|
<< " -o,--optimise Turn on/off the optimiser; off by default." << endl
|
2014-05-26 09:34:43 +00:00
|
|
|
<< " -h,--help Show this help message and exit." << endl
|
|
|
|
<< " -V,--version Show the version and exit." << endl;
|
2017-10-18 11:32:34 +00:00
|
|
|
exit(0);
|
2014-05-26 09:22:19 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 00:01:47 +00:00
|
|
|
static void version()
|
2014-05-26 09:22:19 +00:00
|
|
|
{
|
2017-10-18 11:32:34 +00:00
|
|
|
cout << "LLLC, the Lovely Little Language Compiler" << endl;
|
2016-11-01 12:02:08 +00:00
|
|
|
cout << "Version: " << VersionString << endl;
|
2014-05-26 09:22:19 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-08-30 19:32:30 +00:00
|
|
|
/*
|
2016-08-31 20:00:46 +00:00
|
|
|
The equivalent of setlocale(LC_ALL, "C") is called before any user code is run.
|
2016-08-30 19:32:30 +00:00
|
|
|
If the user has an invalid environment setting then it is possible for the call
|
|
|
|
to set locale to fail, so there are only two possible actions, the first is to
|
|
|
|
throw a runtime exception and cause the program to quit (default behaviour),
|
|
|
|
or the second is to modify the environment to something sensible (least
|
|
|
|
surprising behaviour).
|
|
|
|
|
|
|
|
The follow code produces the least surprising behaviour. It will use the user
|
|
|
|
specified default locale if it is valid, and if not then it will modify the
|
|
|
|
environment the process is running in to use a sensible default. This also means
|
|
|
|
that users do not need to install language packs for their OS.
|
|
|
|
*/
|
2017-08-29 00:01:47 +00:00
|
|
|
static void setDefaultOrCLocale()
|
2016-08-30 15:13:21 +00:00
|
|
|
{
|
2016-08-30 19:32:30 +00:00
|
|
|
#if __unix__
|
2016-08-30 15:13:21 +00:00
|
|
|
if (!std::setlocale(LC_ALL, ""))
|
|
|
|
{
|
2016-08-30 11:29:37 +00:00
|
|
|
setenv("LC_ALL", "C", 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-28 14:28:41 +00:00
|
|
|
enum Mode { Binary, Hex, Assembly, ParseTree, Disassemble };
|
2014-05-26 09:22:19 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2016-08-30 19:32:30 +00:00
|
|
|
setDefaultOrCLocale();
|
2016-10-25 01:15:21 +00:00
|
|
|
unsigned optimise = 0;
|
2014-05-26 09:22:19 +00:00
|
|
|
string infile;
|
|
|
|
Mode mode = Hex;
|
|
|
|
|
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
string arg = argv[i];
|
|
|
|
if (arg == "-h" || arg == "--help")
|
|
|
|
help();
|
|
|
|
else if (arg == "-b" || arg == "--binary")
|
|
|
|
mode = Binary;
|
2014-05-26 09:34:43 +00:00
|
|
|
else if (arg == "-x" || arg == "--hex")
|
2014-05-26 09:22:19 +00:00
|
|
|
mode = Hex;
|
2014-05-26 17:41:46 +00:00
|
|
|
else if (arg == "-a" || arg == "--assembly")
|
|
|
|
mode = Assembly;
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (arg == "-t" || arg == "--parse-tree")
|
|
|
|
mode = ParseTree;
|
2016-10-25 11:43:12 +00:00
|
|
|
else if (arg == "-o" || arg == "--optimise")
|
|
|
|
optimise = 1;
|
2014-05-28 14:28:41 +00:00
|
|
|
else if (arg == "-d" || arg == "--disassemble")
|
|
|
|
mode = Disassemble;
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (arg == "-V" || arg == "--version")
|
|
|
|
version();
|
|
|
|
else
|
|
|
|
infile = argv[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
string src;
|
|
|
|
if (infile.empty())
|
2017-10-18 11:54:47 +00:00
|
|
|
src = readStandardInput();
|
2014-05-26 09:22:19 +00:00
|
|
|
else
|
2017-10-18 11:34:29 +00:00
|
|
|
src = readFileAsString(infile);
|
2014-05-26 09:22:19 +00:00
|
|
|
|
2014-05-26 17:41:46 +00:00
|
|
|
vector<string> errors;
|
2014-05-26 09:22:19 +00:00
|
|
|
if (src.empty())
|
2017-10-18 11:32:34 +00:00
|
|
|
{
|
2014-06-05 01:27:29 +00:00
|
|
|
errors.push_back("Empty file.");
|
2017-10-18 11:32:34 +00:00
|
|
|
}
|
2014-05-28 14:28:41 +00:00
|
|
|
else if (mode == Disassemble)
|
|
|
|
{
|
|
|
|
cout << disassemble(fromHex(src)) << endl;
|
|
|
|
}
|
2014-05-26 09:22:19 +00:00
|
|
|
else if (mode == Binary || mode == Hex)
|
|
|
|
{
|
2017-10-18 11:34:29 +00:00
|
|
|
auto bs = compileLLL(src, optimise ? true : false, &errors, readFileAsString);
|
2014-05-26 09:22:19 +00:00
|
|
|
if (mode == Hex)
|
|
|
|
cout << toHex(bs) << endl;
|
|
|
|
else if (mode == Binary)
|
|
|
|
cout.write((char const*)bs.data(), bs.size());
|
|
|
|
}
|
|
|
|
else if (mode == ParseTree)
|
2017-10-18 11:32:34 +00:00
|
|
|
{
|
2014-05-26 09:22:19 +00:00
|
|
|
cout << parseLLL(src) << endl;
|
2017-10-18 11:32:34 +00:00
|
|
|
}
|
2014-05-26 17:41:46 +00:00
|
|
|
else if (mode == Assembly)
|
2017-10-18 11:32:34 +00:00
|
|
|
{
|
2017-10-18 11:34:29 +00:00
|
|
|
cout << compileLLLToAsm(src, optimise ? true : false, &errors, readFileAsString) << endl;
|
2017-10-18 11:32:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-26 17:41:46 +00:00
|
|
|
for (auto const& i: errors)
|
|
|
|
cerr << i << endl;
|
2017-10-18 11:32:34 +00:00
|
|
|
if (errors.size())
|
2014-06-05 01:27:29 +00:00
|
|
|
return 1;
|
2014-05-26 09:22:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|