Solc evm assembly to either file or stdout option

This commit is contained in:
Lefteris Karapetsas 2014-12-08 15:05:23 +01:00
parent 3cb4562e5d
commit 501d6f4a2c

View File

@ -53,23 +53,28 @@ void version()
exit(0); exit(0);
} }
enum class AstOutput enum class OutputType
{ {
STDOUT, STDOUT,
FILE, FILE,
BOTH BOTH
}; };
std::istream& operator>>(std::istream& _in, AstOutput& io_output) #define outputTypeStr "Legal values:\n"\
"\tstdout: Print it to standard output\n"\
"\tfile: Print it to a file with same name\n"\
"\tboth: Print both to a file and the stdout\n"
std::istream& operator>>(std::istream& _in, OutputType& io_output)
{ {
std::string token; std::string token;
_in >> token; _in >> token;
if (token == "stdout") if (token == "stdout")
io_output = AstOutput::STDOUT; io_output = OutputType::STDOUT;
else if (token == "file") else if (token == "file")
io_output = AstOutput::FILE; io_output = OutputType::FILE;
else if (token == "both") else if (token == "both")
io_output = AstOutput::BOTH; io_output = OutputType::BOTH;
else else
throw boost::program_options::invalid_option_value(token); throw boost::program_options::invalid_option_value(token);
return _in; return _in;
@ -84,10 +89,10 @@ int main(int argc, char** argv)
("version", "Show version and exit") ("version", "Show version and exit")
("optimize", po::value<bool>()->default_value(false), "Optimize bytecode for size") ("optimize", po::value<bool>()->default_value(false), "Optimize bytecode for size")
("input-file", po::value<vector<string>>(), "input file") ("input-file", po::value<vector<string>>(), "input file")
("ast", po::value<AstOutput>(), ("ast", po::value<OutputType>(),
"Request to output the AST of the contract. Legal values:\n" "Request to output the AST of the contract. " outputTypeStr)
"\tstdout: Print it to standar output\n" ("asm", po::value<OutputType>(),
"\tfile: Print it to a file with same name\n"); "Request to output the EVM assembly of the contract. " outputTypeStr);
// All positional options should be interpreted as input files // All positional options should be interpreted as input files
po::positional_options_description p; po::positional_options_description p;
@ -176,8 +181,8 @@ int main(int argc, char** argv)
// do we need AST output? // do we need AST output?
if (vm.count("ast")) if (vm.count("ast"))
{ {
auto choice = vm["ast"].as<AstOutput>(); auto choice = vm["ast"].as<OutputType>();
if (choice != AstOutput::FILE) if (choice != OutputType::FILE)
{ {
cout << "Syntax trees:" << endl << endl; cout << "Syntax trees:" << endl << endl;
for (auto const& sourceCode: sourceCodes) for (auto const& sourceCode: sourceCodes)
@ -188,7 +193,7 @@ int main(int argc, char** argv)
} }
} }
if (choice != AstOutput::STDOUT) if (choice != OutputType::STDOUT)
{ {
for (auto const& sourceCode: sourceCodes) for (auto const& sourceCode: sourceCodes)
{ {
@ -201,14 +206,33 @@ int main(int argc, char** argv)
} }
} }
vector<string> contracts = compiler.getContractNames(); vector<string> contracts = compiler.getContractNames();
cout << endl << "Contracts:" << endl; // do we need EVM assembly?
if (vm.count("asm"))
{
auto choice = vm["asm"].as<OutputType>();
for (string const& contract: contracts) for (string const& contract: contracts)
{
if (choice != OutputType::FILE)
{ {
cout << endl << "======= " << contract << " =======" << endl cout << endl << "======= " << contract << " =======" << endl
<< "EVM assembly:" << endl; << "EVM assembly:" << endl;
compiler.streamAssembly(cout, contract); compiler.streamAssembly(cout, contract);
}
if (choice != OutputType::STDOUT)
{
ofstream outFile(contract + ".evm");
compiler.streamAssembly(outFile, contract);
outFile.close();
}
}
}
cout << endl << "Contracts:" << endl;
for (string const& contract: contracts)
{
cout << "Opcodes:" << endl cout << "Opcodes:" << endl
<< eth::disassemble(compiler.getBytecode(contract)) << endl << eth::disassemble(compiler.getBytecode(contract)) << endl
<< "Binary: " << toHex(compiler.getBytecode(contract)) << endl << "Binary: " << toHex(compiler.getBytecode(contract)) << endl