From 6ac94ffad050890ca47877df10d769a744f0cfb2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 12:42:29 +0100 Subject: [PATCH 01/20] using boost::program_options for argument parsing --- solc/CMakeLists.txt | 1 + solc/main.cpp | 59 ++++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/solc/CMakeLists.txt b/solc/CMakeLists.txt index 386d4a1a8..689700a64 100644 --- a/solc/CMakeLists.txt +++ b/solc/CMakeLists.txt @@ -8,6 +8,7 @@ set(EXECUTABLE solc) add_executable(${EXECUTABLE} ${SRC_LIST}) +target_link_libraries(${EXECUTABLE} boost_program_options) target_link_libraries(${EXECUTABLE} solidity) install( TARGETS ${EXECUTABLE} DESTINATION bin ) diff --git a/solc/main.cpp b/solc/main.cpp index 61f73b45d..1f8348746 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -23,6 +23,9 @@ #include #include +#include + +#include "BuildInfo.h" #include #include #include @@ -38,43 +41,45 @@ using namespace std; using namespace dev; using namespace solidity; - -void help() -{ - cout << "Usage solc [OPTIONS] " << endl - << "Options:" << endl - << " -o,--optimize Optimize the bytecode for size." << endl - << " -h,--help Show this help message and exit." << endl - << " -V,--version Show the version and exit." << endl; - exit(0); -} +namespace po = boost::program_options; void version() { cout << "solc, the solidity complier commandline interface " << dev::Version << endl - << " by Christian , (c) 2014." << endl + << " by Christian and Lefteris , (c) 2014." << endl << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; exit(0); } int main(int argc, char** argv) { - vector infiles; - bool optimize = false; - for (int i = 1; i < argc; ++i) - { - string arg = argv[i]; - if (arg == "-o" || arg == "--optimize") - optimize = true; - else if (arg == "-h" || arg == "--help") - help(); - else if (arg == "-V" || arg == "--version") - version(); - else - infiles.push_back(argv[i]); + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file"); + + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); + + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::notify(vm); + + if (vm.count("help")) { + cout << desc; + return 0; + } + + if (vm.count("version")) { + version(); + return 0; } map sourceCodes; - if (infiles.empty()) + if (!vm.count("input-file")) { string s; while (!cin.eof()) @@ -84,7 +89,7 @@ int main(int argc, char** argv) } } else - for (string const& infile: infiles) + for (string const& infile: vm["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); CompilerStack compiler; @@ -92,7 +97,7 @@ int main(int argc, char** argv) { for (auto const& sourceCode: sourceCodes) compiler.addSource(sourceCode.first, sourceCode.second); - compiler.compile(optimize); + compiler.compile( vm["optimize"].as()); } catch (ParserError const& exception) { From 3aac7209780829f6d685965b24a5671b5a30097d Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 13:30:55 +0100 Subject: [PATCH 02/20] Unknown solc arguments are now ignored --- solc/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solc/main.cpp b/solc/main.cpp index 1f8348746..6b16c1249 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -66,7 +66,7 @@ int main(int argc, char** argv) p.add("input-file", -1); po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); po::notify(vm); if (vm.count("help")) { From 2cc2fe735e704596b6bb75035520024f07fa17b2 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 14:46:00 +0100 Subject: [PATCH 03/20] Solc cmdline option for ast outputting either to stdout or a file --- solc/CMakeLists.txt | 1 + solc/main.cpp | 74 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/solc/CMakeLists.txt b/solc/CMakeLists.txt index 689700a64..84f87eb53 100644 --- a/solc/CMakeLists.txt +++ b/solc/CMakeLists.txt @@ -8,6 +8,7 @@ set(EXECUTABLE solc) add_executable(${EXECUTABLE} ${SRC_LIST}) +target_link_libraries(${EXECUTABLE} boost_filesystem) target_link_libraries(${EXECUTABLE} boost_program_options) target_link_libraries(${EXECUTABLE} solidity) diff --git a/solc/main.cpp b/solc/main.cpp index 6b16c1249..c79ba52a6 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -22,8 +22,10 @@ #include #include +#include #include +#include #include "BuildInfo.h" #include @@ -51,6 +53,28 @@ void version() exit(0); } +enum class AstOutput +{ + STDOUT, + FILE, + BOTH +}; + +std::istream& operator>>(std::istream& _in, AstOutput& io_output) +{ + std::string token; + _in >> token; + if (token == "stdout") + io_output = AstOutput::STDOUT; + else if (token == "file") + io_output = AstOutput::FILE; + else if (token == "both") + io_output = AstOutput::BOTH; + else + throw boost::program_options::invalid_option_value(token); + return _in; +} + int main(int argc, char** argv) { // Declare the supported options. @@ -59,14 +83,27 @@ int main(int argc, char** argv) ("help", "Show help message and exit") ("version", "Show version and exit") ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file"); + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. Legal values:\n" + "\tstdout: Print it to standar output\n" + "\tfile: Print it to a file with same name\n"); // All positional options should be interpreted as input files po::positional_options_description p; p.add("input-file", -1); + // parse the compiler arguments po::variables_map vm; - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); + } + catch (po::error const& exception) + { + cout << exception.what() << endl; + return -1; + } po::notify(vm); if (vm.count("help")) { @@ -135,13 +172,36 @@ int main(int argc, char** argv) return -1; } - cout << "Syntax trees:" << endl << endl; - for (auto const& sourceCode: sourceCodes) + + // do we need AST output? + if (vm.count("ast")) { - cout << endl << "======= " << sourceCode.first << " =======" << endl; - ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); - printer.print(cout); + auto choice = vm["ast"].as(); + if (choice != AstOutput::FILE) + { + cout << "Syntax trees:" << endl << endl; + for (auto const& sourceCode: sourceCodes) + { + cout << endl << "======= " << sourceCode.first << " =======" << endl; + ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(cout); + } + } + + if (choice != AstOutput::STDOUT) + { + for (auto const& sourceCode: sourceCodes) + { + boost::filesystem::path p(sourceCode.first); + ofstream outFile(p.stem().string() + ".ast"); + ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(outFile); + outFile.close(); + } + } } + + vector contracts = compiler.getContractNames(); cout << endl << "Contracts:" << endl; for (string const& contract: contracts) From 9562f0f3d55ef9245ae7eaf396b9a74972f822ba Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 15:05:23 +0100 Subject: [PATCH 04/20] Solc evm assembly to either file or stdout option --- solc/main.cpp | 56 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/solc/main.cpp b/solc/main.cpp index c79ba52a6..96e834ff1 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -53,23 +53,28 @@ void version() exit(0); } -enum class AstOutput +enum class OutputType { STDOUT, FILE, 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; _in >> token; if (token == "stdout") - io_output = AstOutput::STDOUT; + io_output = OutputType::STDOUT; else if (token == "file") - io_output = AstOutput::FILE; + io_output = OutputType::FILE; else if (token == "both") - io_output = AstOutput::BOTH; + io_output = OutputType::BOTH; else throw boost::program_options::invalid_option_value(token); return _in; @@ -84,10 +89,10 @@ int main(int argc, char** argv) ("version", "Show version and exit") ("optimize", po::value()->default_value(false), "Optimize bytecode for size") ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. Legal values:\n" - "\tstdout: Print it to standar output\n" - "\tfile: Print it to a file with same name\n"); + ("ast", po::value(), + "Request to output the AST of the contract. " outputTypeStr) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " outputTypeStr); // All positional options should be interpreted as input files po::positional_options_description p; @@ -176,8 +181,8 @@ int main(int argc, char** argv) // do we need AST output? if (vm.count("ast")) { - auto choice = vm["ast"].as(); - if (choice != AstOutput::FILE) + auto choice = vm["ast"].as(); + if (choice != OutputType::FILE) { cout << "Syntax trees:" << endl << endl; 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) { @@ -201,14 +206,33 @@ int main(int argc, char** argv) } } - vector contracts = compiler.getContractNames(); + // do we need EVM assembly? + if (vm.count("asm")) + { + auto choice = vm["asm"].as(); + for (string const& contract: contracts) + { + if (choice != OutputType::FILE) + { + cout << endl << "======= " << contract << " =======" << endl + << "EVM assembly:" << endl; + 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 << endl << "======= " << contract << " =======" << endl - << "EVM assembly:" << endl; - compiler.streamAssembly(cout, contract); cout << "Opcodes:" << endl << eth::disassemble(compiler.getBytecode(contract)) << endl << "Binary: " << toHex(compiler.getBytecode(contract)) << endl From 9bebf39249200e6b9ff25fd62869d6bb73925e12 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 15:21:20 +0100 Subject: [PATCH 05/20] Solc option to output binary and opcode --- solc/main.cpp | 62 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/solc/main.cpp b/solc/main.cpp index 96e834ff1..38295dcc3 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -92,7 +92,11 @@ int main(int argc, char** argv) ("ast", po::value(), "Request to output the AST of the contract. " outputTypeStr) ("asm", po::value(), - "Request to output the EVM assembly of the contract. " outputTypeStr); + "Request to output the EVM assembly of the contract. " outputTypeStr) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " outputTypeStr) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " outputTypeStr); // All positional options should be interpreted as input files po::positional_options_description p; @@ -207,16 +211,17 @@ int main(int argc, char** argv) } vector contracts = compiler.getContractNames(); - // do we need EVM assembly? - if (vm.count("asm")) + for (string const& contract: contracts) { - auto choice = vm["asm"].as(); - for (string const& contract: contracts) + cout << endl << "======= " << contract << " =======" << endl; + + // do we need EVM assembly? + if (vm.count("asm")) { + auto choice = vm["asm"].as(); if (choice != OutputType::FILE) { - cout << endl << "======= " << contract << " =======" << endl - << "EVM assembly:" << endl; + cout << "EVM assembly:" << endl; compiler.streamAssembly(cout, contract); } @@ -227,16 +232,49 @@ int main(int argc, char** argv) outFile.close(); } } - } + // do we need opcodes? + if (vm.count("opcodes")) + { + auto choice = vm["opcodes"].as(); + if (choice != OutputType::FILE) + { + cout << "Opcodes:" << endl; + cout << eth::disassemble(compiler.getBytecode(contract)) << endl; + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(contract + ".opcodes"); + outFile << eth::disassemble(compiler.getBytecode(contract)); + outFile.close(); + } + } + + // do we need binary? + if (vm.count("binary")) + { + auto choice = vm["binary"].as(); + if (choice != OutputType::FILE) + { + cout << "Binary:" << endl; + cout << toHex(compiler.getBytecode(contract)) << endl; + } + + if (choice != OutputType::STDOUT) + { // TODO: Think, if we really want that? Could simply output to normal binary + ofstream outFile(contract + ".bin"); + outFile << toHex(compiler.getBytecode(contract)); + outFile.close(); + } + } + + } // end of contracts iteration cout << endl << "Contracts:" << endl; for (string const& contract: contracts) { - cout << "Opcodes:" << endl - << eth::disassemble(compiler.getBytecode(contract)) << endl - << "Binary: " << toHex(compiler.getBytecode(contract)) << endl - << "Interface specification: " << compiler.getJsonDocumentation(contract, DocumentationType::ABI_INTERFACE) << endl + cout << "Interface specification: " << compiler.getJsonDocumentation(contract, DocumentationType::ABI_INTERFACE) << endl << "Natspec user documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_USER) << endl << "Natspec developer documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_DEV) << endl; } From 1dfef5bc491464ba29c490759bae8dce36374667 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 8 Dec 2014 16:42:56 +0100 Subject: [PATCH 06/20] Solc gets arguments for interface and documentation related output --- solc/main.cpp | 155 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 109 insertions(+), 46 deletions(-) diff --git a/solc/main.cpp b/solc/main.cpp index 38295dcc3..8650d429c 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -80,6 +80,96 @@ std::istream& operator>>(std::istream& _in, OutputType& io_output) return _in; } +static void handleBytecode(po::variables_map const& vm, + char const* _argName, + string const& _title, + string const& _contract, + CompilerStack&_compiler, + string const& _suffix) +{ + if (vm.count(_argName)) + { + auto choice = vm[_argName].as(); + if (choice != OutputType::FILE) + { + cout << _title << endl; + if (_suffix == "opcodes") + cout << _compiler.getBytecode(_contract) << endl; + else + cout << toHex(_compiler.getBytecode(_contract)) << endl; + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(_contract + _suffix); + if (_suffix == "opcodes") + outFile << _compiler.getBytecode(_contract); + else + outFile << toHex(_compiler.getBytecode(_contract)); + outFile.close(); + } + } +} + +static void handleJson(po::variables_map const& _vm, + DocumentationType _type, + string const& _contract, + CompilerStack&_compiler) +{ + std::string argName; + std::string suffix; + std::string title; + switch(_type) + { + case DocumentationType::ABI_INTERFACE: + argName = "abi"; + suffix = ".abi"; + title = "Contract ABI"; + break; + case DocumentationType::NATSPEC_USER: + argName = "natspec-user"; + suffix = ".docuser"; + title = "User Documentation"; + break; + case DocumentationType::NATSPEC_DEV: + argName = "natspec-dev"; + suffix = ".docdev"; + title = "Developer Documentation"; + break; + default: + // should never happen + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); + } + + if (_vm.count(argName.c_str())) + { + auto choice = _vm[argName.c_str()].as(); + if (choice != OutputType::FILE) + { + cout << title << endl; + cout << _compiler.getJsonDocumentation(_contract, _type); + } + + if (choice != OutputType::STDOUT) + { + ofstream outFile(_contract + suffix); + outFile << _compiler.getJsonDocumentation(_contract, _type); + outFile.close(); + } + } +} + +static inline bool argToStdout(po::variables_map const& _vm, const char* _name) +{ + return _vm.count(_name) && _vm[_name].as() != OutputType::FILE; +} + +static bool needStdout(po::variables_map const& _vm) +{ + return argToStdout(_vm, "abi") || argToStdout(_vm, "natspec-user") || argToStdout(_vm, "natspec-dev") || + argToStdout(_vm, "asm") || argToStdout(_vm, "opcodes") || argToStdout(_vm, "binary"); +} + int main(int argc, char** argv) { // Declare the supported options. @@ -96,7 +186,13 @@ int main(int argc, char** argv) ("opcodes", po::value(), "Request to output the Opcodes of the contract. " outputTypeStr) ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " outputTypeStr); + "Request to output the contract in binary (hexadecimal). " outputTypeStr) + ("abi", po::value(), + "Request to output the contract's ABI interface. " outputTypeStr) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " outputTypeStr) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " outputTypeStr); // All positional options should be interpreted as input files po::positional_options_description p; @@ -124,6 +220,8 @@ int main(int argc, char** argv) version(); return 0; } + + // create a map of input files to source code strings map sourceCodes; if (!vm.count("input-file")) { @@ -138,6 +236,7 @@ int main(int argc, char** argv) for (string const& infile: vm["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); + // parse the files CompilerStack compiler; try { @@ -182,6 +281,8 @@ int main(int argc, char** argv) } + /* -- act depending on the provided arguments -- */ + // do we need AST output? if (vm.count("ast")) { @@ -213,7 +314,8 @@ int main(int argc, char** argv) vector contracts = compiler.getContractNames(); for (string const& contract: contracts) { - cout << endl << "======= " << contract << " =======" << endl; + if (needStdout(vm)) + cout << endl << "======= " << contract << " =======" << endl; // do we need EVM assembly? if (vm.count("asm")) @@ -233,51 +335,12 @@ int main(int argc, char** argv) } } - // do we need opcodes? - if (vm.count("opcodes")) - { - auto choice = vm["opcodes"].as(); - if (choice != OutputType::FILE) - { - cout << "Opcodes:" << endl; - cout << eth::disassemble(compiler.getBytecode(contract)) << endl; - } - - if (choice != OutputType::STDOUT) - { - ofstream outFile(contract + ".opcodes"); - outFile << eth::disassemble(compiler.getBytecode(contract)); - outFile.close(); - } - } - - // do we need binary? - if (vm.count("binary")) - { - auto choice = vm["binary"].as(); - if (choice != OutputType::FILE) - { - cout << "Binary:" << endl; - cout << toHex(compiler.getBytecode(contract)) << endl; - } - - if (choice != OutputType::STDOUT) - { // TODO: Think, if we really want that? Could simply output to normal binary - ofstream outFile(contract + ".bin"); - outFile << toHex(compiler.getBytecode(contract)); - outFile.close(); - } - } - + handleBytecode(vm, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); + handleBytecode(vm, "binary", "Binary:", contract, compiler, ".binary"); + handleJson(vm, DocumentationType::ABI_INTERFACE, contract, compiler); + handleJson(vm, DocumentationType::NATSPEC_DEV, contract, compiler); + handleJson(vm, DocumentationType::NATSPEC_USER, contract, compiler); } // end of contracts iteration - cout << endl << "Contracts:" << endl; - for (string const& contract: contracts) - { - cout << "Interface specification: " << compiler.getJsonDocumentation(contract, DocumentationType::ABI_INTERFACE) << endl - << "Natspec user documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_USER) << endl - << "Natspec developer documentation: " << compiler.getJsonDocumentation(contract, DocumentationType::NATSPEC_DEV) << endl; - } - return 0; } From e676cd21bc19cfad9b1101068fc1cb82e40aa7ed Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 8 Dec 2014 16:56:41 +0100 Subject: [PATCH 07/20] Register variably-sized variables on stack. --- libsolidity/Compiler.cpp | 7 ++++--- libsolidity/CompilerContext.cpp | 25 +++++++++++++++---------- libsolidity/CompilerContext.h | 10 ++++++---- test/solidityCompiler.cpp | 4 ++-- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 17ad4fd16..66d938d17 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -199,11 +199,12 @@ bool Compiler::visit(FunctionDefinition& _function) unsigned const numReturnValues = _function.getReturnParameters().size(); unsigned const numLocalVariables = _function.getLocalVariables().size(); - for (ASTPointer const& variable: _function.getParameters() + _function.getReturnParameters()) + for (ASTPointer const& variable: _function.getParameters()) m_context.addVariable(*variable); + for (ASTPointer const& variable: _function.getReturnParameters()) + m_context.addAndInitializeVariable(*variable); for (VariableDeclaration const* localVariable: _function.getLocalVariables()) - m_context.addVariable(*localVariable); - m_context.initializeLocalVariables(numReturnValues + numLocalVariables); + m_context.addAndInitializeVariable(*localVariable); _function.getBody().accept(*this); diff --git a/libsolidity/CompilerContext.cpp b/libsolidity/CompilerContext.cpp index b89a8e5b5..fa18520d0 100644 --- a/libsolidity/CompilerContext.cpp +++ b/libsolidity/CompilerContext.cpp @@ -41,20 +41,25 @@ void CompilerContext::addStateVariable(VariableDeclaration const& _declaration) m_stateVariablesSize += _declaration.getType()->getStorageSize(); } -void CompilerContext::initializeLocalVariables(unsigned _numVariables) +void CompilerContext::addVariable(VariableDeclaration const& _declaration) { - if (_numVariables > 0) - { + m_localVariables[&_declaration] = m_localVariablesSize; + m_localVariablesSize += _declaration.getType()->getSizeOnStack(); +} + +void CompilerContext::addAndInitializeVariable(VariableDeclaration const& _declaration) +{ + addVariable(_declaration); + + unsigned const size = _declaration.getType()->getSizeOnStack(); + for (unsigned i = 0; i < size; ++i) *this << u256(0); - for (unsigned i = 1; i < _numVariables; ++i) - *this << eth::Instruction::DUP1; - m_asm.adjustDeposit(-_numVariables); - } + m_asm.adjustDeposit(-size); } bool CompilerContext::isLocalVariable(Declaration const* _declaration) const { - return std::find(m_localVariables.begin(), m_localVariables.end(), _declaration) != m_localVariables.end(); + return m_localVariables.count(_declaration) > 0; } eth::AssemblyItem CompilerContext::getFunctionEntryLabel(FunctionDefinition const& _function) const @@ -67,10 +72,10 @@ eth::AssemblyItem CompilerContext::getFunctionEntryLabel(FunctionDefinition cons unsigned CompilerContext::getBaseStackOffsetOfVariable(Declaration const& _declaration) const { - auto res = find(begin(m_localVariables), end(m_localVariables), &_declaration); + auto res = m_localVariables.find(&_declaration); if (asserts(res != m_localVariables.end())) BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Variable not found on stack.")); - return unsigned(end(m_localVariables) - res - 1); + return m_localVariablesSize - res->second - 1; } unsigned CompilerContext::baseToCurrentStackOffset(unsigned _baseOffset) const diff --git a/libsolidity/CompilerContext.h b/libsolidity/CompilerContext.h index 6a48e1485..7272a368b 100644 --- a/libsolidity/CompilerContext.h +++ b/libsolidity/CompilerContext.h @@ -43,8 +43,8 @@ public: void addMagicGlobal(MagicVariableDeclaration const& _declaration); void addStateVariable(VariableDeclaration const& _declaration); void startNewFunction() { m_localVariables.clear(); m_asm.setDeposit(0); } - void initializeLocalVariables(unsigned _numVariables); - void addVariable(VariableDeclaration const& _declaration) { m_localVariables.push_back(&_declaration); } + void addVariable(VariableDeclaration const& _declaration); + void addAndInitializeVariable(VariableDeclaration const& _declaration); void addFunction(FunctionDefinition const& _function) { m_functionEntryLabels.insert(std::make_pair(&_function, m_asm.newTag())); } void adjustStackOffset(int _adjustment) { m_asm.adjustDeposit(_adjustment); } @@ -98,8 +98,10 @@ private: u256 m_stateVariablesSize; /// Storage offsets of state variables std::map m_stateVariables; - /// Offsets of local variables on the stack. - std::vector m_localVariables; + /// Offsets of local variables on the stack (relative to stack base). + std::map m_localVariables; + /// Sum of stack sizes of local variables + unsigned m_localVariablesSize; /// Labels pointing to the entry points of funcitons. std::map m_functionEntryLabels; }; diff --git a/test/solidityCompiler.cpp b/test/solidityCompiler.cpp index 9862cba83..004740b5e 100644 --- a/test/solidityCompiler.cpp +++ b/test/solidityCompiler.cpp @@ -125,8 +125,8 @@ BOOST_AUTO_TEST_CASE(different_argument_numbers) byte(Instruction::JUMP), // end of f byte(Instruction::JUMPDEST), // beginning of g byte(Instruction::PUSH1), 0x0, - byte(Instruction::DUP1), // initialized e and h - byte(Instruction::PUSH1), byte(0x29 + shift), // ret address + byte(Instruction::PUSH1), 0x0, // initialized e and h + byte(Instruction::PUSH1), byte(0x2a + shift), // ret address byte(Instruction::PUSH1), 0x1, byte(Instruction::PUSH1), 0xff, byte(Instruction::AND), byte(Instruction::PUSH1), 0x2, byte(Instruction::PUSH1), 0xff, byte(Instruction::AND), byte(Instruction::PUSH1), 0x3, byte(Instruction::PUSH1), 0xff, byte(Instruction::AND), From 1462cfbd4bf6f443cf981c38ca84e0cd8b86119a Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 8 Dec 2014 18:19:25 +0100 Subject: [PATCH 08/20] Cleanup before return. --- libsolidity/Compiler.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 66d938d17..7e0f4ca1a 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -195,13 +195,9 @@ bool Compiler::visit(FunctionDefinition& _function) // stack upon entry: [return address] [arg0] [arg1] ... [argn] // reserve additional slots: [retarg0] ... [retargm] [localvar0] ... [localvarp] - unsigned const numArguments = _function.getParameters().size(); - unsigned const numReturnValues = _function.getReturnParameters().size(); - unsigned const numLocalVariables = _function.getLocalVariables().size(); - - for (ASTPointer const& variable: _function.getParameters()) + for (ASTPointer const& variable: _function.getParameters()) m_context.addVariable(*variable); - for (ASTPointer const& variable: _function.getReturnParameters()) + for (ASTPointer const& variable: _function.getReturnParameters()) m_context.addAndInitializeVariable(*variable); for (VariableDeclaration const* localVariable: _function.getLocalVariables()) m_context.addAndInitializeVariable(*localVariable); @@ -216,12 +212,22 @@ bool Compiler::visit(FunctionDefinition& _function) // Note that the fact that the return arguments are of increasing index is vital for this // algorithm to work. + unsigned argumentsSize = 0; + for (ASTPointer const& variable: _function.getParameters()) + argumentsSize += variable->getType()->getSizeOnStack(); + unsigned returnValuesSize = 0; + for (ASTPointer const& variable: _function.getReturnParameters()) + returnValuesSize += variable->getType()->getSizeOnStack(); + unsigned localVariablesSize = 0; + for (VariableDeclaration const* localVariable: _function.getLocalVariables()) + localVariablesSize += localVariable->getType()->getSizeOnStack(); + vector stackLayout; - stackLayout.push_back(numReturnValues); // target of return address - stackLayout += vector(numArguments, -1); // discard all arguments - for (unsigned i = 0; i < numReturnValues; ++i) + stackLayout.push_back(returnValuesSize); // target of return address + stackLayout += vector(argumentsSize, -1); // discard all arguments + for (unsigned i = 0; i < returnValuesSize; ++i) stackLayout.push_back(i); - stackLayout += vector(numLocalVariables, -1); + stackLayout += vector(localVariablesSize, -1); while (stackLayout.back() != int(stackLayout.size() - 1)) if (stackLayout.back() < 0) From 9bcd315e426a439b873ee97ab728494961eb4d73 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 8 Dec 2014 18:52:30 +0100 Subject: [PATCH 09/20] Changes in compiler to support variably sized stack elements. --- libsolidity/Compiler.cpp | 16 ++++------- libsolidity/CompilerContext.cpp | 5 ++++ libsolidity/CompilerContext.h | 3 +- libsolidity/CompilerUtils.cpp | 50 +++++++++++++++++++++++++++++++++ libsolidity/CompilerUtils.h | 47 +++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 libsolidity/CompilerUtils.cpp create mode 100644 libsolidity/CompilerUtils.h diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 7e0f4ca1a..e7263da6f 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -26,6 +26,7 @@ #include #include #include +#include using namespace std; @@ -135,7 +136,7 @@ unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, b for (ASTPointer const& var: _function.getParameters()) { unsigned const numBytes = var->getType()->getCalldataEncodedSize(); - if (numBytes == 0) + if (numBytes == 0 || numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(var->getLocation()) << errinfo_comment("Type " + var->getType()->toString() + " not yet supported.")); @@ -158,7 +159,7 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function) { Type const& paramType = *parameters[i]->getType(); unsigned numBytes = paramType.getCalldataEncodedSize(); - if (numBytes == 0) + if (numBytes == 0 || numBytes > 32) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(parameters[i]->getLocation()) << errinfo_comment("Type " + paramType.toString() + " not yet supported.")); @@ -305,8 +306,7 @@ bool Compiler::visit(Return& _return) VariableDeclaration const& firstVariable = *_return.getFunctionReturnParameters().getParameters().front(); ExpressionCompiler::appendTypeConversion(m_context, *expression->getType(), *firstVariable.getType()); - unsigned stackPosition = m_context.baseToCurrentStackOffset(m_context.getBaseStackOffsetOfVariable(firstVariable)); - m_context << eth::swapInstruction(stackPosition) << eth::Instruction::POP; + CompilerUtils(m_context).moveToStackVariable(firstVariable); } m_context.appendJumpTo(m_returnTag); return false; @@ -320,9 +320,7 @@ bool Compiler::visit(VariableDefinition& _variableDefinition) ExpressionCompiler::appendTypeConversion(m_context, *expression->getType(), *_variableDefinition.getDeclaration().getType()); - unsigned baseStackOffset = m_context.getBaseStackOffsetOfVariable(_variableDefinition.getDeclaration()); - unsigned stackPosition = m_context.baseToCurrentStackOffset(baseStackOffset); - m_context << eth::swapInstruction(stackPosition) << eth::Instruction::POP; + CompilerUtils(m_context).moveToStackVariable(_variableDefinition.getDeclaration()); } return false; } @@ -331,9 +329,7 @@ bool Compiler::visit(ExpressionStatement& _expressionStatement) { Expression& expression = _expressionStatement.getExpression(); ExpressionCompiler::compileExpression(m_context, expression); -// Type::Category category = expression.getType()->getCategory(); - for (unsigned i = 0; i < expression.getType()->getSizeOnStack(); ++i) - m_context << eth::Instruction::POP; + CompilerUtils(m_context).popStackElement(*expression.getType()); return false; } diff --git a/libsolidity/CompilerContext.cpp b/libsolidity/CompilerContext.cpp index fa18520d0..cd22c4e8b 100644 --- a/libsolidity/CompilerContext.cpp +++ b/libsolidity/CompilerContext.cpp @@ -57,6 +57,11 @@ void CompilerContext::addAndInitializeVariable(VariableDeclaration const& _decla m_asm.adjustDeposit(-size); } +void CompilerContext::addFunction(FunctionDefinition const& _function) +{ + m_functionEntryLabels.insert(std::make_pair(&_function, m_asm.newTag())); +} + bool CompilerContext::isLocalVariable(Declaration const* _declaration) const { return m_localVariables.count(_declaration) > 0; diff --git a/libsolidity/CompilerContext.h b/libsolidity/CompilerContext.h index 7272a368b..652e65a63 100644 --- a/libsolidity/CompilerContext.h +++ b/libsolidity/CompilerContext.h @@ -25,6 +25,7 @@ #include #include #include +#include #include namespace dev { @@ -45,7 +46,7 @@ public: void startNewFunction() { m_localVariables.clear(); m_asm.setDeposit(0); } void addVariable(VariableDeclaration const& _declaration); void addAndInitializeVariable(VariableDeclaration const& _declaration); - void addFunction(FunctionDefinition const& _function) { m_functionEntryLabels.insert(std::make_pair(&_function, m_asm.newTag())); } + void addFunction(FunctionDefinition const& _function); void adjustStackOffset(int _adjustment) { m_asm.adjustDeposit(_adjustment); } diff --git a/libsolidity/CompilerUtils.cpp b/libsolidity/CompilerUtils.cpp new file mode 100644 index 000000000..b3d982c99 --- /dev/null +++ b/libsolidity/CompilerUtils.cpp @@ -0,0 +1,50 @@ +/* + 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 . +*/ +/** + * @author Christian + * @date 2014 + * Routines used by both the compiler and the expression compiler. + */ + +#include +#include +#include + +using namespace std; + +namespace dev +{ +namespace solidity +{ + +void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) +{ + unsigned const stackPosition = m_context.baseToCurrentStackOffset(m_context.getBaseStackOffsetOfVariable(_variable)); + unsigned const size = _variable.getType()->getSizeOnStack(); + for (unsigned i = 0; i < size; ++i) + m_context << eth::swapInstruction(stackPosition - size + 1) << eth::Instruction::POP; +} + +void CompilerUtils::popStackElement(Type const& _type) +{ + unsigned const size = _type.getSizeOnStack(); + for (unsigned i = 0; i < size; ++i) + m_context << eth::Instruction::POP; +} + +} +} diff --git a/libsolidity/CompilerUtils.h b/libsolidity/CompilerUtils.h new file mode 100644 index 000000000..3b6f13e92 --- /dev/null +++ b/libsolidity/CompilerUtils.h @@ -0,0 +1,47 @@ +/* + 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 . +*/ +/** + * @author Christian + * @date 2014 + * Routines used by both the compiler and the expression compiler. + */ + +#pragma once + +#include +#include + +namespace dev { +namespace solidity { + +class Type; // forward + +class CompilerUtils +{ +public: + CompilerUtils(CompilerContext& _context): m_context(_context) {} + + /// Moves the value that is at the top of the stack to a stack variable. + void moveToStackVariable(VariableDeclaration const& _variable); + /// Removes the current value from the top of the stack. + void popStackElement(Type const& _type); +private: + CompilerContext& m_context; +}; + +} +} From f8c78bafb598a03c0a0a18e42061b1e1d24c5548 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 8 Dec 2014 22:18:19 +0100 Subject: [PATCH 10/20] Variable-size stack elements for expression compiler. --- libsolidity/Compiler.cpp | 12 ++---- libsolidity/CompilerUtils.cpp | 11 +++++ libsolidity/CompilerUtils.h | 14 ++++++ libsolidity/ExpressionCompiler.cpp | 68 +++++++++++++++++++++++------- libsolidity/ExpressionCompiler.h | 5 ++- test/solidityEndToEndTest.cpp | 35 +++++++++++++++ 6 files changed, 119 insertions(+), 26 deletions(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index e7263da6f..73b3e3245 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -213,15 +213,9 @@ bool Compiler::visit(FunctionDefinition& _function) // Note that the fact that the return arguments are of increasing index is vital for this // algorithm to work. - unsigned argumentsSize = 0; - for (ASTPointer const& variable: _function.getParameters()) - argumentsSize += variable->getType()->getSizeOnStack(); - unsigned returnValuesSize = 0; - for (ASTPointer const& variable: _function.getReturnParameters()) - returnValuesSize += variable->getType()->getSizeOnStack(); - unsigned localVariablesSize = 0; - for (VariableDeclaration const* localVariable: _function.getLocalVariables()) - localVariablesSize += localVariable->getType()->getSizeOnStack(); + unsigned const argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters()); + unsigned const returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters()); + unsigned const localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables()); vector stackLayout; stackLayout.push_back(returnValuesSize); // target of return address diff --git a/libsolidity/CompilerUtils.cpp b/libsolidity/CompilerUtils.cpp index b3d982c99..cbd92d2b7 100644 --- a/libsolidity/CompilerUtils.cpp +++ b/libsolidity/CompilerUtils.cpp @@ -35,6 +35,9 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) { unsigned const stackPosition = m_context.baseToCurrentStackOffset(m_context.getBaseStackOffsetOfVariable(_variable)); unsigned const size = _variable.getType()->getSizeOnStack(); + if (stackPosition - size + 1 > 16) + BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_variable.getLocation()) + << errinfo_comment("Stack too deep.")); for (unsigned i = 0; i < size; ++i) m_context << eth::swapInstruction(stackPosition - size + 1) << eth::Instruction::POP; } @@ -46,5 +49,13 @@ void CompilerUtils::popStackElement(Type const& _type) m_context << eth::Instruction::POP; } +unsigned CompilerUtils::getSizeOnStack(vector> const& _variableTypes) +{ + unsigned size = 0; + for (shared_ptr const& type: _variableTypes) + size += type->getSizeOnStack(); + return size; +} + } } diff --git a/libsolidity/CompilerUtils.h b/libsolidity/CompilerUtils.h index 3b6f13e92..2aac7e4ea 100644 --- a/libsolidity/CompilerUtils.h +++ b/libsolidity/CompilerUtils.h @@ -39,9 +39,23 @@ public: void moveToStackVariable(VariableDeclaration const& _variable); /// Removes the current value from the top of the stack. void popStackElement(Type const& _type); + + template + static unsigned getSizeOnStack(std::vector const& _variables); + static unsigned getSizeOnStack(std::vector> const& _variableTypes); + private: CompilerContext& m_context; }; +template +unsigned CompilerUtils::getSizeOnStack(std::vector const& _variables) +{ + unsigned size = 0; + for (T const& variable: _variables) + size += variable->getType()->getSizeOnStack(); + return size; +} + } } diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 352b0e6d8..5deb50639 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -26,6 +26,7 @@ #include #include #include +#include using namespace std; @@ -174,9 +175,7 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) // explicit type conversion contract -> address, nothing to do. } else - { appendTypeConversion(*firstArgument.getType(), *_functionCall.getType()); - } } else { @@ -203,13 +202,14 @@ bool ExpressionCompiler::visit(FunctionCall& _functionCall) m_context.appendJump(); m_context << returnLabel; + unsigned returnParametersSize = CompilerUtils::getSizeOnStack(function.getReturnParameterTypes()); // callee adds return parameters, but removes arguments and return label - m_context.adjustStackOffset(function.getReturnParameterTypes().size() - arguments.size() - 1); + m_context.adjustStackOffset(returnParametersSize - CompilerUtils::getSizeOnStack(arguments) - 1); // @todo for now, the return value of a function is its first return value, so remove // all others for (unsigned i = 1; i < function.getReturnParameterTypes().size(); ++i) - m_context << eth::Instruction::POP; + CompilerUtils(m_context).popStackElement(*function.getReturnParameterTypes()[i]); break; } case Location::EXTERNAL: @@ -356,7 +356,7 @@ void ExpressionCompiler::endVisit(MemberAccess& _memberAccess) { StructType const& type = dynamic_cast(*_memberAccess.getExpression().getType()); m_context << type.getStorageOffsetOfMember(member) << eth::Instruction::ADD; - m_currentLValue = LValue(m_context, LValue::STORAGE); + m_currentLValue = LValue(m_context, LValue::STORAGE, *_memberAccess.getType()); m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess); break; } @@ -376,7 +376,7 @@ bool ExpressionCompiler::visit(IndexAccess& _indexAccess) m_context << u256(32) << eth::Instruction::MSTORE << u256(0) << eth::Instruction::MSTORE; m_context << u256(64) << u256(0) << eth::Instruction::SHA3; - m_currentLValue = LValue(m_context, LValue::STORAGE); + m_currentLValue = LValue(m_context, LValue::STORAGE, *_indexAccess.getType()); m_currentLValue.retrieveValueIfLValueNotRequested(_indexAccess); return false; @@ -565,6 +565,13 @@ void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack) m_context << ((u256(1) << _typeOnStack.getNumBits()) - 1) << eth::Instruction::AND; } +ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, const Type& _dataType, + unsigned _baseStackOffset): + m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset), + m_stackSize(_dataType.getSizeOnStack()) +{ +} + void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bool _remove) const { switch (m_type) @@ -575,7 +582,8 @@ void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bo if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation()) << errinfo_comment("Stack too deep.")); - *m_context << eth::dupInstruction(stackPos + 1); + for (unsigned i = 0; i < m_stackSize; ++i) + *m_context << eth::dupInstruction(stackPos + 1); break; } case STORAGE: @@ -583,7 +591,17 @@ void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bo break; // no distinction between value and reference for non-value types if (!_remove) *m_context << eth::Instruction::DUP1; - *m_context << eth::Instruction::SLOAD; + if (m_stackSize == 1) + *m_context << eth::Instruction::SLOAD; + else + for (unsigned i = 0; i < m_stackSize; ++i) + { + *m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1; + if (i + 1 < m_stackSize) + *m_context << u256(1) << eth::Instruction::ADD; + else + *m_context << eth::Instruction::POP; + } break; case MEMORY: if (!_expression.getType()->isValueType()) @@ -604,12 +622,13 @@ void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool { case STACK: { - unsigned stackPos = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset)); - if (stackPos > 16) + unsigned stackDiff = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset)) - m_stackSize + 1; + if (stackDiff > 16) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation()) << errinfo_comment("Stack too deep.")); - else if (stackPos > 0) - *m_context << eth::swapInstruction(stackPos) << eth::Instruction::POP; + else if (stackDiff > 0) + for (unsigned i = 0; i < m_stackSize; ++i) + *m_context << eth::swapInstruction(stackDiff) << eth::Instruction::POP; if (!_move) retrieveValue(_expression); break; @@ -617,9 +636,27 @@ void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool case LValue::STORAGE: if (!_expression.getType()->isValueType()) break; // no distinction between value and reference for non-value types - if (!_move) - *m_context << eth::Instruction::DUP2 << eth::Instruction::SWAP1; - *m_context << eth::Instruction::SSTORE; + // stack layout: value value ... value ref + if (!_move) // copy values + { + if (m_stackSize + 1 > 16) + BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation()) + << errinfo_comment("Stack too deep.")); + for (unsigned i = 0; i < m_stackSize; ++i) + *m_context << eth::dupInstruction(m_stackSize + 1) << eth::Instruction::SWAP1; + } + if (m_stackSize > 0) // store high index value first + *m_context << u256(m_stackSize - 1) << eth::Instruction::ADD; + for (unsigned i = 0; i < m_stackSize; ++i) + { + if (i + 1 >= m_stackSize) + *m_context << eth::Instruction::SSTORE; + else + // v v ... v v r+x + *m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2 + << eth::Instruction::SSTORE + << u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB; + } break; case LValue::MEMORY: if (!_expression.getType()->isValueType()) @@ -645,6 +682,7 @@ void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(Expression co void ExpressionCompiler::LValue::fromIdentifier(Identifier const& _identifier, Declaration const& _declaration) { + m_stackSize = _identifier.getType()->getSizeOnStack(); if (m_context->isLocalVariable(&_declaration)) { m_type = STACK; diff --git a/libsolidity/ExpressionCompiler.h b/libsolidity/ExpressionCompiler.h index fbecbdc8e..966be30e2 100644 --- a/libsolidity/ExpressionCompiler.h +++ b/libsolidity/ExpressionCompiler.h @@ -93,8 +93,7 @@ private: enum LValueType { NONE, STACK, MEMORY, STORAGE }; explicit LValue(CompilerContext& _compilerContext): m_context(&_compilerContext) { reset(); } - LValue(CompilerContext& _compilerContext, LValueType _type, unsigned _baseStackOffset = 0): - m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset) {} + LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType, unsigned _baseStackOffset = 0); /// Set type according to the declaration and retrieve the reference. /// @a _expression is the current expression @@ -129,6 +128,8 @@ private: /// If m_type is STACK, this is base stack offset (@see /// CompilerContext::getBaseStackOffsetOfVariable) of a local variable. unsigned m_baseStackOffset; + /// Size of the value of this lvalue on the stack. + unsigned m_stackSize; }; CompilerContext& m_context; diff --git a/test/solidityEndToEndTest.cpp b/test/solidityEndToEndTest.cpp index 9e02438e8..3c2bb0814 100644 --- a/test/solidityEndToEndTest.cpp +++ b/test/solidityEndToEndTest.cpp @@ -1025,6 +1025,41 @@ BOOST_AUTO_TEST_CASE(calls_to_this) BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 10)); } +BOOST_AUTO_TEST_CASE(inter_contract_calls_with_local_vars) +{ + // note that a reference to another contract's function occupies two stack slots, + // so this tests correct stack slot allocation + char const* sourceCode = R"( + contract Helper { + function multiply(uint a, uint b) returns (uint c) { + return a * b; + } + } + contract Main { + Helper h; + function callHelper(uint a, uint b) returns (uint c) { + var fu = h.multiply; + var y = 9; + var ret = fu(a, b); + return ret + y; + } + function getHelper() returns (address haddress) { + return address(h); + } + function setHelper(address haddress) { + h = Helper(haddress); + } + })"; + compileAndRun(sourceCode, 0, "Helper"); + u160 const helperAddress = m_contractAddress; + compileAndRun(sourceCode, 0, "Main"); + BOOST_REQUIRE(callContractFunction(2, helperAddress) == bytes()); + BOOST_REQUIRE(callContractFunction(1, helperAddress) == toBigEndian(helperAddress)); + u256 a(3456789); + u256 b("0x282837623374623234aa74"); + BOOST_REQUIRE(callContractFunction(0, a, b) == toBigEndian(a * b + 9)); +} + BOOST_AUTO_TEST_SUITE_END() } From 8773a7ea0f7be837a9c13aeb2419fcdb7f3b3f6f Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 12:05:32 +0100 Subject: [PATCH 11/20] Style improvements and succinctness in solc main.cpp --- solc/main.cpp | 132 ++++++++++++++++++++++++++++---------------------- 1 file changed, 73 insertions(+), 59 deletions(-) diff --git a/solc/main.cpp b/solc/main.cpp index 8650d429c..9500eb97f 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -60,12 +60,18 @@ enum class OutputType BOTH }; -#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" +static inline bool outputToFile(OutputType type) +{ + return type == OutputType::FILE || type == OutputType::BOTH; +} + +static inline bool outputToStdout(OutputType type) +{ + return type == OutputType::STDOUT || type == OutputType::BOTH; +} + -std::istream& operator>>(std::istream& _in, OutputType& io_output) +static std::istream& operator>>(std::istream& _in, OutputType& io_output) { std::string token; _in >> token; @@ -81,16 +87,16 @@ std::istream& operator>>(std::istream& _in, OutputType& io_output) } static void handleBytecode(po::variables_map const& vm, - char const* _argName, + string const& _argName, string const& _title, string const& _contract, - CompilerStack&_compiler, + CompilerStack& _compiler, string const& _suffix) { if (vm.count(_argName)) { auto choice = vm[_argName].as(); - if (choice != OutputType::FILE) + if (outputToStdout(choice)) { cout << _title << endl; if (_suffix == "opcodes") @@ -99,7 +105,7 @@ static void handleBytecode(po::variables_map const& vm, cout << toHex(_compiler.getBytecode(_contract)) << endl; } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { ofstream outFile(_contract + _suffix); if (_suffix == "opcodes") @@ -111,7 +117,7 @@ static void handleBytecode(po::variables_map const& vm, } } -static void handleJson(po::variables_map const& _vm, +static void handleJson(po::variables_map const& _args, DocumentationType _type, string const& _contract, CompilerStack&_compiler) @@ -141,16 +147,16 @@ static void handleJson(po::variables_map const& _vm, BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); } - if (_vm.count(argName.c_str())) + if (_args.count(argName)) { - auto choice = _vm[argName.c_str()].as(); - if (choice != OutputType::FILE) + auto choice = _args[argName].as(); + if (outputToStdout(choice)) { cout << title << endl; cout << _compiler.getJsonDocumentation(_contract, _type); } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { ofstream outFile(_contract + suffix); outFile << _compiler.getJsonDocumentation(_contract, _type); @@ -159,71 +165,78 @@ static void handleJson(po::variables_map const& _vm, } } -static inline bool argToStdout(po::variables_map const& _vm, const char* _name) +static inline bool argToStdout(po::variables_map const& _args, const char* _name) { - return _vm.count(_name) && _vm[_name].as() != OutputType::FILE; + return _args.count(_name) && _args[_name].as() != OutputType::FILE; } -static bool needStdout(po::variables_map const& _vm) +static bool needStdout(po::variables_map const& _args) { - return argToStdout(_vm, "abi") || argToStdout(_vm, "natspec-user") || argToStdout(_vm, "natspec-dev") || - argToStdout(_vm, "asm") || argToStdout(_vm, "opcodes") || argToStdout(_vm, "binary"); + return argToStdout(_args, "abi") || argToStdout(_args, "natspec-user") || argToStdout(_args, "natspec-dev") || + argToStdout(_args, "asm") || argToStdout(_args, "opcodes") || argToStdout(_args, "binary"); } int main(int argc, char** argv) { +#define OUTPUT_TYPE_STR "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" // Declare the supported options. po::options_description desc("Allowed options"); desc.add_options() - ("help", "Show help message and exit") - ("version", "Show version and exit") - ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. " outputTypeStr) - ("asm", po::value(), - "Request to output the EVM assembly of the contract. " outputTypeStr) - ("opcodes", po::value(), - "Request to output the Opcodes of the contract. " outputTypeStr) - ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " outputTypeStr) - ("abi", po::value(), - "Request to output the contract's ABI interface. " outputTypeStr) - ("natspec-user", po::value(), - "Request to output the contract's Natspec user documentation. " outputTypeStr) - ("natspec-dev", po::value(), - "Request to output the contract's Natspec developer documentation. " outputTypeStr); + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. " OUTPUT_TYPE_STR) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) + ("abi", po::value(), + "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); +#undef OUTPUT_TYPE_STR // All positional options should be interpreted as input files po::positional_options_description p; p.add("input-file", -1); // parse the compiler arguments - po::variables_map vm; + po::variables_map args; try { - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), vm); + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), args); } catch (po::error const& exception) { cout << exception.what() << endl; return -1; } - po::notify(vm); + po::notify(args); - if (vm.count("help")) { + if (args.count("help")) + { cout << desc; return 0; } - if (vm.count("version")) { + if (args.count("version")) + { version(); return 0; } // create a map of input files to source code strings map sourceCodes; - if (!vm.count("input-file")) + if (!args.count("input-file")) { string s; while (!cin.eof()) @@ -233,7 +246,7 @@ int main(int argc, char** argv) } } else - for (string const& infile: vm["input-file"].as>()) + for (string const& infile: args["input-file"].as>()) sourceCodes[infile] = asString(dev::contents(infile)); // parse the files @@ -242,7 +255,8 @@ int main(int argc, char** argv) { for (auto const& sourceCode: sourceCodes) compiler.addSource(sourceCode.first, sourceCode.second); - compiler.compile( vm["optimize"].as()); + // TODO: Perhaps we should not compile unless requested + compiler.compile(args["optimize"].as()); } catch (ParserError const& exception) { @@ -284,10 +298,10 @@ int main(int argc, char** argv) /* -- act depending on the provided arguments -- */ // do we need AST output? - if (vm.count("ast")) + if (args.count("ast")) { - auto choice = vm["ast"].as(); - if (choice != OutputType::FILE) + auto choice = args["ast"].as(); + if (outputToStdout(choice)) { cout << "Syntax trees:" << endl << endl; for (auto const& sourceCode: sourceCodes) @@ -298,7 +312,7 @@ int main(int argc, char** argv) } } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { for (auto const& sourceCode: sourceCodes) { @@ -314,20 +328,20 @@ int main(int argc, char** argv) vector contracts = compiler.getContractNames(); for (string const& contract: contracts) { - if (needStdout(vm)) + if (needStdout(args)) cout << endl << "======= " << contract << " =======" << endl; // do we need EVM assembly? - if (vm.count("asm")) + if (args.count("asm")) { - auto choice = vm["asm"].as(); - if (choice != OutputType::FILE) + auto choice = args["asm"].as(); + if (outputToStdout(choice)) { cout << "EVM assembly:" << endl; compiler.streamAssembly(cout, contract); } - if (choice != OutputType::STDOUT) + if (outputToFile(choice)) { ofstream outFile(contract + ".evm"); compiler.streamAssembly(outFile, contract); @@ -335,11 +349,11 @@ int main(int argc, char** argv) } } - handleBytecode(vm, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); - handleBytecode(vm, "binary", "Binary:", contract, compiler, ".binary"); - handleJson(vm, DocumentationType::ABI_INTERFACE, contract, compiler); - handleJson(vm, DocumentationType::NATSPEC_DEV, contract, compiler); - handleJson(vm, DocumentationType::NATSPEC_USER, contract, compiler); + handleBytecode(args, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); + handleBytecode(args, "binary", "Binary:", contract, compiler, ".binary"); + handleJson(args, DocumentationType::ABI_INTERFACE, contract, compiler); + handleJson(args, DocumentationType::NATSPEC_DEV, contract, compiler); + handleJson(args, DocumentationType::NATSPEC_USER, contract, compiler); } // end of contracts iteration return 0; From 11aa5ad5d48a38bc53fa45249535b4327cf019a9 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 13:43:08 +0100 Subject: [PATCH 12/20] Moving most of the solc functionality to own class and splitting implementation in modular functions --- solc/SolContext.cpp | 368 ++++++++++++++++++++++++++++++++++++++++++++ solc/SolContext.h | 72 +++++++++ solc/main.cpp | 336 +--------------------------------------- 3 files changed, 447 insertions(+), 329 deletions(-) create mode 100644 solc/SolContext.cpp create mode 100644 solc/SolContext.h diff --git a/solc/SolContext.cpp b/solc/SolContext.cpp new file mode 100644 index 000000000..620ad112f --- /dev/null +++ b/solc/SolContext.cpp @@ -0,0 +1,368 @@ +/* + 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 . +*/ +/** + * @author Lefteris + * @date 2014 + * Solidity compiler context class. + */ +#include "SolContext.h" + +#include +#include +#include + +#include + +#include "BuildInfo.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +namespace po = boost::program_options; + +namespace dev +{ +namespace solidity +{ + +static void version() +{ + cout << "solc, the solidity complier commandline interface " << dev::Version << endl + << " by Christian and Lefteris , (c) 2014." << endl + << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; + exit(0); +} + +static inline bool argToStdout(po::variables_map const& _args, const char* _name) +{ + return _args.count(_name) && _args[_name].as() != OutputType::FILE; +} + +static bool needStdout(po::variables_map const& _args) +{ + return argToStdout(_args, "abi") || argToStdout(_args, "natspec-user") || argToStdout(_args, "natspec-dev") || + argToStdout(_args, "asm") || argToStdout(_args, "opcodes") || argToStdout(_args, "binary"); +} + +static inline bool outputToFile(OutputType type) +{ + return type == OutputType::FILE || type == OutputType::BOTH; +} + +static inline bool outputToStdout(OutputType type) +{ + return type == OutputType::STDOUT || type == OutputType::BOTH; +} + + +static std::istream& operator>>(std::istream& _in, OutputType& io_output) +{ + std::string token; + _in >> token; + if (token == "stdout") + io_output = OutputType::STDOUT; + else if (token == "file") + io_output = OutputType::FILE; + else if (token == "both") + io_output = OutputType::BOTH; + else + throw boost::program_options::invalid_option_value(token); + return _in; +} + +void SolContext::handleBytecode(string const& _argName, + string const& _title, + string const& _contract, + string const& _suffix) +{ + if (m_args.count(_argName)) + { + auto choice = m_args[_argName].as(); + if (outputToStdout(choice)) + { + cout << _title << endl; + if (_suffix == "opcodes") + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + // cout << m_compiler.getBytecode(_contract) << endl; + else + cout << toHex(m_compiler.getBytecode(_contract)) << endl; + } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + _suffix); + if (_suffix == "opcodes") + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + // outFile << m_compiler.getBytecode(_contract); + else + outFile << toHex(m_compiler.getBytecode(_contract)); + outFile.close(); + } + } +} + +void SolContext::handleJson(DocumentationType _type, + string const& _contract) +{ + std::string argName; + std::string suffix; + std::string title; + switch(_type) + { + case DocumentationType::ABI_INTERFACE: + argName = "abi"; + suffix = ".abi"; + title = "Contract ABI"; + break; + case DocumentationType::NATSPEC_USER: + argName = "natspec-user"; + suffix = ".docuser"; + title = "User Documentation"; + break; + case DocumentationType::NATSPEC_DEV: + argName = "natspec-dev"; + suffix = ".docdev"; + title = "Developer Documentation"; + break; + default: + // should never happen + BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); + } + + if (m_args.count(argName)) + { + auto choice = m_args[argName].as(); + if (outputToStdout(choice)) + { + cout << title << endl; + cout << m_compiler.getJsonDocumentation(_contract, _type); + } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + suffix); + outFile << m_compiler.getJsonDocumentation(_contract, _type); + outFile.close(); + } + } +} + + + + + + + + + + +bool SolContext::parseArguments(int argc, char** argv) +{ +#define OUTPUT_TYPE_STR "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" + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. " OUTPUT_TYPE_STR) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) + ("abi", po::value(), + "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); +#undef OUTPUT_TYPE_STR + + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); + + // parse the compiler arguments + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); + } + catch (po::error const& exception) + { + cout << exception.what() << endl; + return false; + } + po::notify(m_args); + + if (m_args.count("help")) + { + cout << desc; + return false; + } + + if (m_args.count("version")) + { + version(); + return false; + } + + return true; +} + +bool SolContext::processInput() +{ + if (!m_args.count("input-file")) + { + string s; + while (!cin.eof()) + { + getline(cin, s); + m_sourceCodes[""].append(s); + } + } + else + for (string const& infile: m_args["input-file"].as>()) + m_sourceCodes[infile] = asString(dev::contents(infile)); + + try + { + for (auto const& sourceCode: m_sourceCodes) + m_compiler.addSource(sourceCode.first, sourceCode.second); + // TODO: Perhaps we should not compile unless requested + m_compiler.compile(m_args["optimize"].as()); + } + catch (ParserError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Parser error", m_compiler); + return false; + } + catch (DeclarationError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Declaration error", m_compiler); + return false; + } + catch (TypeError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Type error", m_compiler); + return false; + } + catch (CompilerError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Compiler error", m_compiler); + return false; + } + catch (InternalCompilerError const& exception) + { + SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Internal compiler error", m_compiler); + return false; + } + catch (Exception const& exception) + { + cerr << "Exception during compilation: " << boost::diagnostic_information(exception) << endl; + return false; + } + catch (...) + { + cerr << "Unknown exception during compilation." << endl; + return false; + } + + return true; +} + +void SolContext::actOnInput() +{ + // do we need AST output? + if (m_args.count("ast")) + { + auto choice = m_args["ast"].as(); + if (outputToStdout(choice)) + { + cout << "Syntax trees:" << endl << endl; + for (auto const& sourceCode: m_sourceCodes) + { + cout << endl << "======= " << sourceCode.first << " =======" << endl; + ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(cout); + } + } + + if (outputToFile(choice)) + { + for (auto const& sourceCode: m_sourceCodes) + { + boost::filesystem::path p(sourceCode.first); + ofstream outFile(p.stem().string() + ".ast"); + ASTPrinter printer(m_compiler.getAST(sourceCode.first), sourceCode.second); + printer.print(outFile); + outFile.close(); + } + } + } + + vector contracts = m_compiler.getContractNames(); + for (string const& contract: contracts) + { + if (needStdout(m_args)) + cout << endl << "======= " << contract << " =======" << endl; + + // do we need EVM assembly? + if (m_args.count("asm")) + { + auto choice = m_args["asm"].as(); + if (outputToStdout(choice)) + { + cout << "EVM assembly:" << endl; + m_compiler.streamAssembly(cout, contract); + } + + if (outputToFile(choice)) + { + ofstream outFile(contract + ".evm"); + m_compiler.streamAssembly(outFile, contract); + outFile.close(); + } + } + + handleBytecode("opcodes", "Opcodes:", contract, ".opcodes"); + handleBytecode("binary", "Binary:", contract, ".binary"); + handleJson(DocumentationType::ABI_INTERFACE, contract); + handleJson(DocumentationType::NATSPEC_DEV, contract); + handleJson(DocumentationType::NATSPEC_USER, contract); + } // end of contracts iteration +} + +} +} diff --git a/solc/SolContext.h b/solc/SolContext.h new file mode 100644 index 000000000..922d471ab --- /dev/null +++ b/solc/SolContext.h @@ -0,0 +1,72 @@ +/* + 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 . +*/ +/** + * @author Lefteris + * @date 2014 + * Solidity compiler context class. + */ +#pragma once + +#include + +#include + +namespace dev +{ +namespace solidity +{ + +//forward declaration +enum class DocumentationType: uint8_t; + +enum class OutputType: uint8_t +{ + STDOUT, + FILE, + BOTH +}; + +class SolContext +{ +public: + SolContext() {} + + /// Parse command line arguments and return false if we should not continue + bool parseArguments(int argc, char** argv); + /// Parse the files and create source code objects + bool processInput(); + /// Perform actions on the input depending on provided compiler arguments + void actOnInput(); + +private: + void handleBytecode(std::string const& _argName, + std::string const& _title, + std::string const& _contract, + std::string const& _suffix); + void handleJson(DocumentationType _type, + std::string const& _contract); + + /// Compiler arguments variable map + boost::program_options::variables_map m_args; + /// map of input files to source code strings + std::map m_sourceCodes; + /// Solidity compiler stack + dev::solidity::CompilerStack m_compiler; +}; + +} +} diff --git a/solc/main.cpp b/solc/main.cpp index 9500eb97f..f692725d2 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -20,341 +20,19 @@ * Solidity commandline compiler. */ -#include -#include -#include -#include -#include +#include "SolContext.h" -#include "BuildInfo.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; -using namespace dev; -using namespace solidity; -namespace po = boost::program_options; - -void version() -{ - cout << "solc, the solidity complier commandline interface " << dev::Version << endl - << " by Christian and Lefteris , (c) 2014." << endl - << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl; - exit(0); -} - -enum class OutputType -{ - STDOUT, - FILE, - BOTH -}; - -static inline bool outputToFile(OutputType type) -{ - return type == OutputType::FILE || type == OutputType::BOTH; -} - -static inline bool outputToStdout(OutputType type) -{ - return type == OutputType::STDOUT || type == OutputType::BOTH; -} - - -static std::istream& operator>>(std::istream& _in, OutputType& io_output) -{ - std::string token; - _in >> token; - if (token == "stdout") - io_output = OutputType::STDOUT; - else if (token == "file") - io_output = OutputType::FILE; - else if (token == "both") - io_output = OutputType::BOTH; - else - throw boost::program_options::invalid_option_value(token); - return _in; -} - -static void handleBytecode(po::variables_map const& vm, - string const& _argName, - string const& _title, - string const& _contract, - CompilerStack& _compiler, - string const& _suffix) -{ - if (vm.count(_argName)) - { - auto choice = vm[_argName].as(); - if (outputToStdout(choice)) - { - cout << _title << endl; - if (_suffix == "opcodes") - cout << _compiler.getBytecode(_contract) << endl; - else - cout << toHex(_compiler.getBytecode(_contract)) << endl; - } - - if (outputToFile(choice)) - { - ofstream outFile(_contract + _suffix); - if (_suffix == "opcodes") - outFile << _compiler.getBytecode(_contract); - else - outFile << toHex(_compiler.getBytecode(_contract)); - outFile.close(); - } - } -} - -static void handleJson(po::variables_map const& _args, - DocumentationType _type, - string const& _contract, - CompilerStack&_compiler) -{ - std::string argName; - std::string suffix; - std::string title; - switch(_type) - { - case DocumentationType::ABI_INTERFACE: - argName = "abi"; - suffix = ".abi"; - title = "Contract ABI"; - break; - case DocumentationType::NATSPEC_USER: - argName = "natspec-user"; - suffix = ".docuser"; - title = "User Documentation"; - break; - case DocumentationType::NATSPEC_DEV: - argName = "natspec-dev"; - suffix = ".docdev"; - title = "Developer Documentation"; - break; - default: - // should never happen - BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown documentation _type")); - } - - if (_args.count(argName)) - { - auto choice = _args[argName].as(); - if (outputToStdout(choice)) - { - cout << title << endl; - cout << _compiler.getJsonDocumentation(_contract, _type); - } - - if (outputToFile(choice)) - { - ofstream outFile(_contract + suffix); - outFile << _compiler.getJsonDocumentation(_contract, _type); - outFile.close(); - } - } -} - -static inline bool argToStdout(po::variables_map const& _args, const char* _name) -{ - return _args.count(_name) && _args[_name].as() != OutputType::FILE; -} - -static bool needStdout(po::variables_map const& _args) -{ - return argToStdout(_args, "abi") || argToStdout(_args, "natspec-user") || argToStdout(_args, "natspec-dev") || - argToStdout(_args, "asm") || argToStdout(_args, "opcodes") || argToStdout(_args, "binary"); -} int main(int argc, char** argv) { -#define OUTPUT_TYPE_STR "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" - // Declare the supported options. - po::options_description desc("Allowed options"); - desc.add_options() - ("help", "Show help message and exit") - ("version", "Show version and exit") - ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. " OUTPUT_TYPE_STR) - ("asm", po::value(), - "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) - ("opcodes", po::value(), - "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) - ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) - ("abi", po::value(), - "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) - ("natspec-user", po::value(), - "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) - ("natspec-dev", po::value(), - "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); -#undef OUTPUT_TYPE_STR - - // All positional options should be interpreted as input files - po::positional_options_description p; - p.add("input-file", -1); - - // parse the compiler arguments - po::variables_map args; - try - { - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), args); - } - catch (po::error const& exception) - { - cout << exception.what() << endl; - return -1; - } - po::notify(args); - - if (args.count("help")) - { - cout << desc; - return 0; - } - - if (args.count("version")) - { - version(); - return 0; - } - - // create a map of input files to source code strings - map sourceCodes; - if (!args.count("input-file")) - { - string s; - while (!cin.eof()) - { - getline(cin, s); - sourceCodes[""].append(s); - } - } - else - for (string const& infile: args["input-file"].as>()) - sourceCodes[infile] = asString(dev::contents(infile)); - - // parse the files - CompilerStack compiler; - try - { - for (auto const& sourceCode: sourceCodes) - compiler.addSource(sourceCode.first, sourceCode.second); - // TODO: Perhaps we should not compile unless requested - compiler.compile(args["optimize"].as()); - } - catch (ParserError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Parser error", compiler); - return -1; - } - catch (DeclarationError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Declaration error", compiler); - return -1; - } - catch (TypeError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Type error", compiler); - return -1; - } - catch (CompilerError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Compiler error", compiler); - return -1; - } - catch (InternalCompilerError const& exception) - { - SourceReferenceFormatter::printExceptionInformation(cerr, exception, "Internal compiler error", compiler); - return -1; - } - catch (Exception const& exception) - { - cerr << "Exception during compilation: " << boost::diagnostic_information(exception) << endl; - return -1; - } - catch (...) - { - cerr << "Unknown exception during compilation." << endl; - return -1; - } - - - /* -- act depending on the provided arguments -- */ - - // do we need AST output? - if (args.count("ast")) - { - auto choice = args["ast"].as(); - if (outputToStdout(choice)) - { - cout << "Syntax trees:" << endl << endl; - for (auto const& sourceCode: sourceCodes) - { - cout << endl << "======= " << sourceCode.first << " =======" << endl; - ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); - printer.print(cout); - } - } - - if (outputToFile(choice)) - { - for (auto const& sourceCode: sourceCodes) - { - boost::filesystem::path p(sourceCode.first); - ofstream outFile(p.stem().string() + ".ast"); - ASTPrinter printer(compiler.getAST(sourceCode.first), sourceCode.second); - printer.print(outFile); - outFile.close(); - } - } - } - - vector contracts = compiler.getContractNames(); - for (string const& contract: contracts) - { - if (needStdout(args)) - cout << endl << "======= " << contract << " =======" << endl; - - // do we need EVM assembly? - if (args.count("asm")) - { - auto choice = args["asm"].as(); - if (outputToStdout(choice)) - { - cout << "EVM assembly:" << endl; - compiler.streamAssembly(cout, contract); - } - - if (outputToFile(choice)) - { - ofstream outFile(contract + ".evm"); - compiler.streamAssembly(outFile, contract); - outFile.close(); - } - } - - handleBytecode(args, "opcodes", "Opcodes:", contract, compiler, ".opcodes"); - handleBytecode(args, "binary", "Binary:", contract, compiler, ".binary"); - handleJson(args, DocumentationType::ABI_INTERFACE, contract, compiler); - handleJson(args, DocumentationType::NATSPEC_DEV, contract, compiler); - handleJson(args, DocumentationType::NATSPEC_USER, contract, compiler); - } // end of contracts iteration + dev::solidity::SolContext ctx; + if (!ctx.parseArguments(argc, argv)) + return 1; + if (!ctx.processInput()) + return 1; + ctx.actOnInput(); return 0; } From 7a5c1db126b8bdbaeca30c058f22a0503d3bc93b Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 17:39:34 +0100 Subject: [PATCH 13/20] Styling issues and new class name for the CLI --- ...olContext.cpp => CommandLineInterface.cpp} | 156 ++++++++---------- solc/{SolContext.h => CommandLineInterface.h} | 40 ++--- solc/main.cpp | 17 +- 3 files changed, 100 insertions(+), 113 deletions(-) rename solc/{SolContext.cpp => CommandLineInterface.cpp} (69%) rename solc/{SolContext.h => CommandLineInterface.h} (53%) diff --git a/solc/SolContext.cpp b/solc/CommandLineInterface.cpp similarity index 69% rename from solc/SolContext.cpp rename to solc/CommandLineInterface.cpp index 620ad112f..332dc771d 100644 --- a/solc/SolContext.cpp +++ b/solc/CommandLineInterface.cpp @@ -17,9 +17,9 @@ /** * @author Lefteris * @date 2014 - * Solidity compiler context class. + * Solidity command line interface. */ -#include "SolContext.h" +#include "CommandLineInterface.h" #include #include @@ -69,15 +69,14 @@ static bool needStdout(po::variables_map const& _args) static inline bool outputToFile(OutputType type) { - return type == OutputType::FILE || type == OutputType::BOTH; + return type == OutputType::FILE || type == OutputType::BOTH; } static inline bool outputToStdout(OutputType type) { - return type == OutputType::STDOUT || type == OutputType::BOTH; + return type == OutputType::STDOUT || type == OutputType::BOTH; } - static std::istream& operator>>(std::istream& _in, OutputType& io_output) { std::string token; @@ -93,10 +92,10 @@ static std::istream& operator>>(std::istream& _in, OutputType& io_output) return _in; } -void SolContext::handleBytecode(string const& _argName, - string const& _title, - string const& _contract, - string const& _suffix) +void CommandLineInterface::handleBytecode(string const& _argName, + string const& _title, + string const& _contract, + string const& _suffix) { if (m_args.count(_argName)) { @@ -105,8 +104,8 @@ void SolContext::handleBytecode(string const& _argName, { cout << _title << endl; if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes // cout << m_compiler.getBytecode(_contract) << endl; else cout << toHex(m_compiler.getBytecode(_contract)) << endl; @@ -116,8 +115,8 @@ void SolContext::handleBytecode(string const& _argName, { ofstream outFile(_contract + _suffix); if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes + ; + // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes // outFile << m_compiler.getBytecode(_contract); else outFile << toHex(m_compiler.getBytecode(_contract)); @@ -126,8 +125,8 @@ void SolContext::handleBytecode(string const& _argName, } } -void SolContext::handleJson(DocumentationType _type, - string const& _contract) +void CommandLineInterface::handleJson(DocumentationType _type, + string const& _contract) { std::string argName; std::string suffix; @@ -172,76 +171,67 @@ void SolContext::handleJson(DocumentationType _type, } } +bool CommandLineInterface::parseArguments(int argc, char** argv) +{ +#define OUTPUT_TYPE_STR "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" + // Declare the supported options. + po::options_description desc("Allowed options"); + desc.add_options() + ("help", "Show help message and exit") + ("version", "Show version and exit") + ("optimize", po::value()->default_value(false), "Optimize bytecode for size") + ("input-file", po::value>(), "input file") + ("ast", po::value(), + "Request to output the AST of the contract. " OUTPUT_TYPE_STR) + ("asm", po::value(), + "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) + ("opcodes", po::value(), + "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) + ("binary", po::value(), + "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) + ("abi", po::value(), + "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) + ("natspec-user", po::value(), + "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) + ("natspec-dev", po::value(), + "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); +#undef OUTPUT_TYPE_STR + // All positional options should be interpreted as input files + po::positional_options_description p; + p.add("input-file", -1); + // parse the compiler arguments + try + { + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); + } + catch (po::error const& exception) + { + cout << exception.what() << endl; + return false; + } + po::notify(m_args); + if (m_args.count("help")) + { + cout << desc; + return false; + } + if (m_args.count("version")) + { + version(); + return false; + } - - - - -bool SolContext::parseArguments(int argc, char** argv) -{ -#define OUTPUT_TYPE_STR "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" - // Declare the supported options. - po::options_description desc("Allowed options"); - desc.add_options() - ("help", "Show help message and exit") - ("version", "Show version and exit") - ("optimize", po::value()->default_value(false), "Optimize bytecode for size") - ("input-file", po::value>(), "input file") - ("ast", po::value(), - "Request to output the AST of the contract. " OUTPUT_TYPE_STR) - ("asm", po::value(), - "Request to output the EVM assembly of the contract. " OUTPUT_TYPE_STR) - ("opcodes", po::value(), - "Request to output the Opcodes of the contract. " OUTPUT_TYPE_STR) - ("binary", po::value(), - "Request to output the contract in binary (hexadecimal). " OUTPUT_TYPE_STR) - ("abi", po::value(), - "Request to output the contract's ABI interface. " OUTPUT_TYPE_STR) - ("natspec-user", po::value(), - "Request to output the contract's Natspec user documentation. " OUTPUT_TYPE_STR) - ("natspec-dev", po::value(), - "Request to output the contract's Natspec developer documentation. " OUTPUT_TYPE_STR); -#undef OUTPUT_TYPE_STR - - // All positional options should be interpreted as input files - po::positional_options_description p; - p.add("input-file", -1); - - // parse the compiler arguments - try - { - po::store(po::command_line_parser(argc, argv).options(desc).positional(p).allow_unregistered().run(), m_args); - } - catch (po::error const& exception) - { - cout << exception.what() << endl; - return false; - } - po::notify(m_args); - - if (m_args.count("help")) - { - cout << desc; - return false; - } - - if (m_args.count("version")) - { - version(); - return false; - } - - return true; + return true; } -bool SolContext::processInput() +bool CommandLineInterface::processInput() { if (!m_args.count("input-file")) { @@ -260,7 +250,7 @@ bool SolContext::processInput() { for (auto const& sourceCode: m_sourceCodes) m_compiler.addSource(sourceCode.first, sourceCode.second); - // TODO: Perhaps we should not compile unless requested + // TODO: Perhaps we should not compile unless requested m_compiler.compile(m_args["optimize"].as()); } catch (ParserError const& exception) @@ -299,10 +289,10 @@ bool SolContext::processInput() return false; } - return true; + return true; } -void SolContext::actOnInput() +void CommandLineInterface::actOnInput() { // do we need AST output? if (m_args.count("ast")) @@ -342,13 +332,13 @@ void SolContext::actOnInput() if (m_args.count("asm")) { auto choice = m_args["asm"].as(); - if (outputToStdout(choice)) + if (outputToStdout(choice)) { cout << "EVM assembly:" << endl; m_compiler.streamAssembly(cout, contract); } - if (outputToFile(choice)) + if (outputToFile(choice)) { ofstream outFile(contract + ".evm"); m_compiler.streamAssembly(outFile, contract); diff --git a/solc/SolContext.h b/solc/CommandLineInterface.h similarity index 53% rename from solc/SolContext.h rename to solc/CommandLineInterface.h index 922d471ab..8eb1fff3e 100644 --- a/solc/SolContext.h +++ b/solc/CommandLineInterface.h @@ -17,7 +17,7 @@ /** * @author Lefteris * @date 2014 - * Solidity compiler context class. + * Solidity command line interface. */ #pragma once @@ -40,32 +40,32 @@ enum class OutputType: uint8_t BOTH }; -class SolContext +class CommandLineInterface { public: - SolContext() {} + CommandLineInterface() {} - /// Parse command line arguments and return false if we should not continue - bool parseArguments(int argc, char** argv); - /// Parse the files and create source code objects - bool processInput(); - /// Perform actions on the input depending on provided compiler arguments - void actOnInput(); + /// Parse command line arguments and return false if we should not continue + bool parseArguments(int argc, char** argv); + /// Parse the files and create source code objects + bool processInput(); + /// Perform actions on the input depending on provided compiler arguments + void actOnInput(); private: - void handleBytecode(std::string const& _argName, - std::string const& _title, - std::string const& _contract, - std::string const& _suffix); - void handleJson(DocumentationType _type, - std::string const& _contract); + void handleBytecode(std::string const& _argName, + std::string const& _title, + std::string const& _contract, + std::string const& _suffix); + void handleJson(DocumentationType _type, + std::string const& _contract); - /// Compiler arguments variable map - boost::program_options::variables_map m_args; + /// Compiler arguments variable map + boost::program_options::variables_map m_args; /// map of input files to source code strings - std::map m_sourceCodes; - /// Solidity compiler stack - dev::solidity::CompilerStack m_compiler; + std::map m_sourceCodes; + /// Solidity compiler stack + dev::solidity::CompilerStack m_compiler; }; } diff --git a/solc/main.cpp b/solc/main.cpp index f692725d2..c5f72980d 100644 --- a/solc/main.cpp +++ b/solc/main.cpp @@ -20,19 +20,16 @@ * Solidity commandline compiler. */ - -#include "SolContext.h" - - +#include "CommandLineInterface.h" int main(int argc, char** argv) { - dev::solidity::SolContext ctx; - if (!ctx.parseArguments(argc, argv)) - return 1; - if (!ctx.processInput()) - return 1; - ctx.actOnInput(); + dev::solidity::CommandLineInterface cli; + if (!cli.parseArguments(argc, argv)) + return 1; + if (!cli.processInput()) + return 1; + cli.actOnInput(); return 0; } From 030442811a6bb72b298cb31763fff3fc619777be Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 18:17:54 +0100 Subject: [PATCH 14/20] Explicitly calling dev::operator<<() on two occassions due to mixup with boost --- solc/CommandLineInterface.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index 332dc771d..aef5512e3 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -104,9 +104,11 @@ void CommandLineInterface::handleBytecode(string const& _argName, { cout << _title << endl; if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes - // cout << m_compiler.getBytecode(_contract) << endl; + { + // TODO: Figure out why the wrong operator << (from boost) is used here + dev::operator<<(cout, m_compiler.getBytecode(_contract)); + cout << endl; + } else cout << toHex(m_compiler.getBytecode(_contract)) << endl; } @@ -115,9 +117,9 @@ void CommandLineInterface::handleBytecode(string const& _argName, { ofstream outFile(_contract + _suffix); if (_suffix == "opcodes") - ; - // TODO: Figure out why after moving to own class ostream operator does not work for vector of bytes - // outFile << m_compiler.getBytecode(_contract); + // TODO: Figure out why the wrong operator << (from boost) is used here + dev::operator<<(outFile, m_compiler.getBytecode(_contract)); + else outFile << toHex(m_compiler.getBytecode(_contract)); outFile.close(); From 131998f242c394ca8692b7c7b64210822f6243c4 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 9 Dec 2014 20:29:29 +0100 Subject: [PATCH 15/20] Cleaner interface for Solc CLI bytecode handling --- solc/CommandLineInterface.cpp | 68 +++++++++++++++++++---------------- solc/CommandLineInterface.h | 7 ++-- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/solc/CommandLineInterface.cpp b/solc/CommandLineInterface.cpp index aef5512e3..d3dd39459 100644 --- a/solc/CommandLineInterface.cpp +++ b/solc/CommandLineInterface.cpp @@ -92,41 +92,50 @@ static std::istream& operator>>(std::istream& _in, OutputType& io_output) return _in; } -void CommandLineInterface::handleBytecode(string const& _argName, - string const& _title, - string const& _contract, - string const& _suffix) +void CommandLineInterface::handleBinary(string const& _contract) { - if (m_args.count(_argName)) + auto choice = m_args["binary"].as(); + if (outputToStdout(choice)) { - auto choice = m_args[_argName].as(); - if (outputToStdout(choice)) - { - cout << _title << endl; - if (_suffix == "opcodes") - { - // TODO: Figure out why the wrong operator << (from boost) is used here - dev::operator<<(cout, m_compiler.getBytecode(_contract)); - cout << endl; - } - else - cout << toHex(m_compiler.getBytecode(_contract)) << endl; - } + cout << "Binary: " << endl; + cout << toHex(m_compiler.getBytecode(_contract)) << endl; + } - if (outputToFile(choice)) - { - ofstream outFile(_contract + _suffix); - if (_suffix == "opcodes") - // TODO: Figure out why the wrong operator << (from boost) is used here - dev::operator<<(outFile, m_compiler.getBytecode(_contract)); + if (outputToFile(choice)) + { + ofstream outFile(_contract + ".binary"); + outFile << toHex(m_compiler.getBytecode(_contract)); + outFile.close(); + } +} - else - outFile << toHex(m_compiler.getBytecode(_contract)); - outFile.close(); - } +void CommandLineInterface::handleOpcode(string const& _contract) +{ + // TODO: Figure out why the wrong operator << (from boost) is used here + auto choice = m_args["opcode"].as(); + if (outputToStdout(choice)) + { + cout << "Opcodes: " << endl; + dev::operator<<(cout, m_compiler.getBytecode(_contract)); + cout << endl; + } + + if (outputToFile(choice)) + { + ofstream outFile(_contract + ".opcode"); + dev::operator<<(outFile, m_compiler.getBytecode(_contract)); + outFile.close(); } } +void CommandLineInterface::handleBytecode(string const& _contract) +{ + if (m_args.count("opcodes")) + handleOpcode(_contract); + if (m_args.count("binary")) + handleBinary(_contract); +} + void CommandLineInterface::handleJson(DocumentationType _type, string const& _contract) { @@ -348,8 +357,7 @@ void CommandLineInterface::actOnInput() } } - handleBytecode("opcodes", "Opcodes:", contract, ".opcodes"); - handleBytecode("binary", "Binary:", contract, ".binary"); + handleBytecode(contract); handleJson(DocumentationType::ABI_INTERFACE, contract); handleJson(DocumentationType::NATSPEC_DEV, contract); handleJson(DocumentationType::NATSPEC_USER, contract); diff --git a/solc/CommandLineInterface.h b/solc/CommandLineInterface.h index 8eb1fff3e..7e3ad2502 100644 --- a/solc/CommandLineInterface.h +++ b/solc/CommandLineInterface.h @@ -53,10 +53,9 @@ public: void actOnInput(); private: - void handleBytecode(std::string const& _argName, - std::string const& _title, - std::string const& _contract, - std::string const& _suffix); + void handleBinary(std::string const& _contract); + void handleOpcode(std::string const& _contract); + void handleBytecode(std::string const& _contract); void handleJson(DocumentationType _type, std::string const& _contract); From dc478ba0bdb6fce7fcb1f87bd2a2df49393295e2 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 10 Dec 2014 12:51:26 +0100 Subject: [PATCH 16/20] Take variable stack size correctly into account for return value packer. --- libsolidity/Compiler.cpp | 4 +++- libsolidity/CompilerUtils.cpp | 9 +++++++++ libsolidity/CompilerUtils.h | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libsolidity/Compiler.cpp b/libsolidity/Compiler.cpp index 73b3e3245..a8a074034 100644 --- a/libsolidity/Compiler.cpp +++ b/libsolidity/Compiler.cpp @@ -155,6 +155,7 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function) //@todo this can be also done more efficiently unsigned dataOffset = 0; vector> const& parameters = _function.getReturnParameters(); + unsigned stackDepth = CompilerUtils(m_context).getSizeOnStack(parameters); for (unsigned i = 0; i < parameters.size(); ++i) { Type const& paramType = *parameters[i]->getType(); @@ -163,10 +164,11 @@ void Compiler::appendReturnValuePacker(FunctionDefinition const& _function) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(parameters[i]->getLocation()) << errinfo_comment("Type " + paramType.toString() + " not yet supported.")); - m_context << eth::dupInstruction(parameters.size() - i); + CompilerUtils(m_context).copyToStackTop(stackDepth, paramType); if (numBytes != 32) m_context << (u256(1) << ((32 - numBytes) * 8)) << eth::Instruction::MUL; m_context << u256(dataOffset) << eth::Instruction::MSTORE; + stackDepth -= paramType.getSizeOnStack(); dataOffset += numBytes; } // note that the stack is not cleaned up here diff --git a/libsolidity/CompilerUtils.cpp b/libsolidity/CompilerUtils.cpp index cbd92d2b7..824fd4772 100644 --- a/libsolidity/CompilerUtils.cpp +++ b/libsolidity/CompilerUtils.cpp @@ -42,6 +42,15 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) m_context << eth::swapInstruction(stackPosition - size + 1) << eth::Instruction::POP; } +void CompilerUtils::copyToStackTop(unsigned _stackDepth, const Type& _type) +{ + if (_stackDepth > 16) + BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Stack too deep.")); + unsigned const size = _type.getSizeOnStack(); + for (unsigned i = 0; i < size; ++i) + m_context << eth::dupInstruction(_stackDepth); +} + void CompilerUtils::popStackElement(Type const& _type) { unsigned const size = _type.getSizeOnStack(); diff --git a/libsolidity/CompilerUtils.h b/libsolidity/CompilerUtils.h index 2aac7e4ea..4da533752 100644 --- a/libsolidity/CompilerUtils.h +++ b/libsolidity/CompilerUtils.h @@ -37,6 +37,8 @@ public: /// Moves the value that is at the top of the stack to a stack variable. void moveToStackVariable(VariableDeclaration const& _variable); + /// Copies a variable of type @a _type from a stack depth of @a _stackDepth to the top of the stack. + void copyToStackTop(unsigned _stackDepth, Type const& _type); /// Removes the current value from the top of the stack. void popStackElement(Type const& _type); From 17252cecf1fff3750341e2f3b6b33ef874449538 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 10 Dec 2014 14:16:22 +0100 Subject: [PATCH 17/20] minimum cmake version upgraded to 2.8.12 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00f24c863..a4ed1c400 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # cmake global -cmake_minimum_required(VERSION 2.8.9) +cmake_minimum_required(VERSION 2.8.12) # let cmake autolink dependencies on windows # it's specified globally, cause qt libraries requires that on windows and they are also found globally cmake_policy(SET CMP0020 NEW) From 9c2ce9cbbc6af1c32a88fa08c9597cf68d0933ba Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 10 Dec 2014 14:33:30 +0100 Subject: [PATCH 18/20] Stylistic changes. --- libsolidity/CompilerUtils.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libsolidity/CompilerUtils.cpp b/libsolidity/CompilerUtils.cpp index 824fd4772..d4dfbe3c0 100644 --- a/libsolidity/CompilerUtils.cpp +++ b/libsolidity/CompilerUtils.cpp @@ -35,6 +35,7 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) { unsigned const stackPosition = m_context.baseToCurrentStackOffset(m_context.getBaseStackOffsetOfVariable(_variable)); unsigned const size = _variable.getType()->getSizeOnStack(); + // move variable starting from its top end in the stack if (stackPosition - size + 1 > 16) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_variable.getLocation()) << errinfo_comment("Stack too deep.")); @@ -42,7 +43,7 @@ void CompilerUtils::moveToStackVariable(VariableDeclaration const& _variable) m_context << eth::swapInstruction(stackPosition - size + 1) << eth::Instruction::POP; } -void CompilerUtils::copyToStackTop(unsigned _stackDepth, const Type& _type) +void CompilerUtils::copyToStackTop(unsigned _stackDepth, Type const& _type) { if (_stackDepth > 16) BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Stack too deep.")); From 5f48ab40462582b7e3321a6e1174075d4f584bc7 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 10 Dec 2014 14:37:37 +0100 Subject: [PATCH 19/20] Const change. --- libsolidity/ExpressionCompiler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 5deb50639..f1086c143 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -565,7 +565,7 @@ void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack) m_context << ((u256(1) << _typeOnStack.getNumBits()) - 1) << eth::Instruction::AND; } -ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, const Type& _dataType, +ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType, unsigned _baseStackOffset): m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset), m_stackSize(_dataType.getSizeOnStack()) From 0aefbb6b2defd2f46d52903a33917a302e8ea909 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Wed, 10 Dec 2014 16:56:30 +0100 Subject: [PATCH 20/20] recent changes from solc working on macos --- cmake/EthDependencies.cmake | 2 +- solc/CMakeLists.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/EthDependencies.cmake b/cmake/EthDependencies.cmake index 4877b06d5..8d65bc62b 100644 --- a/cmake/EthDependencies.cmake +++ b/cmake/EthDependencies.cmake @@ -125,7 +125,7 @@ elseif (UNIX) endif() -find_package(Boost 1.54.0 REQUIRED COMPONENTS thread date_time system regex chrono filesystem unit_test_framework) +find_package(Boost 1.54.0 REQUIRED COMPONENTS thread date_time system regex chrono filesystem unit_test_framework program_options) message(" - boost header: ${Boost_INCLUDE_DIRS}") message(" - boost lib : ${Boost_LIBRARIES}") diff --git a/solc/CMakeLists.txt b/solc/CMakeLists.txt index e7cc5a1a6..cf1e42572 100644 --- a/solc/CMakeLists.txt +++ b/solc/CMakeLists.txt @@ -11,8 +11,8 @@ set(EXECUTABLE solc) file(GLOB HEADERS "*.h") add_executable(${EXECUTABLE} ${SRC_LIST} ${HEADERS}) -target_link_libraries(${EXECUTABLE} boost_filesystem) -target_link_libraries(${EXECUTABLE} boost_program_options) +target_link_libraries(${EXECUTABLE} ${Boost_FILESYSTEM_LIBRARY_RELEASE}) +target_link_libraries(${EXECUTABLE} ${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE}) target_link_libraries(${EXECUTABLE} solidity) install( TARGETS ${EXECUTABLE} DESTINATION bin )