From f4202d9144057642846a05501329cd4969301b3c Mon Sep 17 00:00:00 2001 From: artur-zawlocki Date: Tue, 28 Oct 2014 22:25:08 +0000 Subject: [PATCH 1/5] 1. Indenting spaces converted to tabs 2. Options changed: -G --> -g --- evmcc/evmcc.cpp | 125 ++++++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 56 deletions(-) diff --git a/evmcc/evmcc.cpp b/evmcc/evmcc.cpp index abaf3e511..1e0b7e1cd 100644 --- a/evmcc/evmcc.cpp +++ b/evmcc/evmcc.cpp @@ -1,4 +1,5 @@ +#include #include #include #include @@ -18,92 +19,104 @@ void show_usage() { - // FIXME: Use arg[0] as program name? - std::cerr << "usage: evmcc (-b|-c|-d)+ \n"; + // FIXME: Use arg[0] as program name? + std::cerr << "usage: evmcc (-b|-c|-d)+ \n"; } int main(int argc, char** argv) { - std::string input_file; - bool opt_dissassemble = false; - bool opt_show_bytes = false; + std::string input_file; + bool opt_dissassemble = false; + bool opt_show_bytes = false; bool opt_compile = false; bool opt_interpret = false; bool opt_dump_graph = false; bool opt_unknown = false; + bool opt_verbose = false; size_t initialGas = 10000; - for (int i = 1; i < argc; i++) - { - std::string option = argv[i]; - if (option == "-b") - opt_show_bytes = true; - else if (option == "-c") - opt_compile = true; - else if (option == "-d") + for (int i = 1; i < argc; i++) + { + std::string option = argv[i]; + if (option == "-b") + opt_show_bytes = true; + else if (option == "-c") + opt_compile = true; + else if (option == "-d") opt_dissassemble = true; else if (option == "-i") opt_interpret = true; - else if (option == "-g") + else if (option == "--dump-cfg") opt_dump_graph = true; - else if (option == "-G" && i + 1 < argc) + else if (option == "-g" && i + 1 < argc) { std::string gasValue = argv[++i]; initialGas = boost::lexical_cast(gasValue); std::cerr << "Initial gas set to " << initialGas << "\n"; } + else if (option == "-v") + opt_verbose = true; else if (option[0] != '-' && input_file.empty()) input_file = option; - else - { - opt_unknown = true; - break; - } - } - - if (opt_unknown || - input_file.empty() || - (!opt_show_bytes && !opt_compile && !opt_dissassemble && !opt_interpret)) - { - show_usage(); - exit(1); - } - - std::ifstream ifs(input_file); - if (!ifs.is_open()) - { - std::cerr << "cannot open file " << input_file << std::endl; - exit(1); - } - - std::string src((std::istreambuf_iterator(ifs)), - (std::istreambuf_iterator())); - - boost::algorithm::trim(src); + else + { + opt_unknown = true; + break; + } + } + + if (opt_unknown || + input_file.empty() || + (!opt_show_bytes && !opt_compile && !opt_dissassemble && !opt_interpret)) + { + show_usage(); + exit(1); + } + + std::ifstream ifs(input_file); + if (!ifs.is_open()) + { + std::cerr << "cannot open file " << input_file << std::endl; + exit(1); + } + + std::string src((std::istreambuf_iterator(ifs)), + (std::istreambuf_iterator())); + + boost::algorithm::trim(src); using namespace dev; - bytes bytecode = fromHex(src); + bytes bytecode = fromHex(src); - if (opt_show_bytes) - { - std::cout << memDump(bytecode) << std::endl; - } + if (opt_show_bytes) + std::cout << memDump(bytecode) << std::endl; - if (opt_dissassemble) - { - std::string assembly = eth::disassemble(bytecode); - std::cout << assembly << std::endl; - } + if (opt_dissassemble) + { + std::string assembly = eth::disassemble(bytecode); + std::cout << assembly << std::endl; + } - if (opt_compile || opt_interpret) - { - auto compiler = eth::jit::Compiler(); + if (opt_compile || opt_interpret) + { + auto compilationStartTime = std::chrono::high_resolution_clock::now(); + + auto compiler = eth::jit::Compiler(); auto module = compiler.compile({bytecode.data(), bytecode.size()}); + auto compilationEndTime = std::chrono::high_resolution_clock::now(); + module->dump(); + if (opt_verbose) + { + std::cerr << "*** Compilation time: " + << std::chrono::duration_cast(compilationEndTime - compilationStartTime).count() + << std::endl; + } + if (opt_dump_graph) { std::ofstream ofs("blocks.dot"); @@ -119,7 +132,7 @@ int main(int argc, char** argv) auto result = engine.run(std::move(module), gas); return result; } - } + } - return 0; + return 0; } From 017bfd9d7a706ee750a88e1dc2a47e9bc81b1e31 Mon Sep 17 00:00:00 2001 From: artur-zawlocki Date: Tue, 28 Oct 2014 22:29:00 +0000 Subject: [PATCH 2/5] Changed semantics of JUMPDEST so that *the next* instruction is a jump destination --- libevmjit/Compiler.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libevmjit/Compiler.cpp b/libevmjit/Compiler.cpp index a03543d91..cd680d927 100644 --- a/libevmjit/Compiler.cpp +++ b/libevmjit/Compiler.cpp @@ -91,9 +91,12 @@ void Compiler::createBasicBlocks(bytesConstRef bytecode) case Instruction::JUMPDEST: { - // A basic block starts here. - splitPoints.insert(currentPC); - indirectJumpTargets.push_back(currentPC); + // A basic block starts at the next instruction. + if (currentPC + 1 < bytecode.size()) + { + splitPoints.insert(currentPC + 1); + indirectJumpTargets.push_back(currentPC + 1); + } break; } @@ -653,8 +656,7 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode, case Instruction::JUMPDEST: { - // Extra asserts just in case. - assert(currentPC == basicBlock.begin()); + // Nothing to do break; } From 732c40b57a667f04c790e17d2fa31f56c4b42c2e Mon Sep 17 00:00:00 2001 From: artur-zawlocki Date: Wed, 29 Oct 2014 10:29:34 +0000 Subject: [PATCH 3/5] MULMOD/ADDMOD implemented in separate functions [FIXES #80566276] --- libevmjit/Arith256.cpp | 53 +++++++++++++++++++++++++++++++++++++----- libevmjit/Arith256.h | 6 +++++ libevmjit/Compiler.cpp | 6 ++--- test/vm.cpp | 26 +++++++++++++++++++-- 4 files changed, 79 insertions(+), 12 deletions(-) diff --git a/libevmjit/Arith256.cpp b/libevmjit/Arith256.cpp index 094ad2ea5..caf7bf3d0 100644 --- a/libevmjit/Arith256.cpp +++ b/libevmjit/Arith256.cpp @@ -21,15 +21,20 @@ Arith256::Arith256(llvm::IRBuilder<>& _builder) : m_result = m_builder.CreateAlloca(Type::i256, nullptr, "arith.result"); m_arg1 = m_builder.CreateAlloca(Type::i256, nullptr, "arith.arg1"); m_arg2 = m_builder.CreateAlloca(Type::i256, nullptr, "arith.arg2"); + m_arg3 = m_builder.CreateAlloca(Type::i256, nullptr, "arith.arg3"); using Linkage = GlobalValue::LinkageTypes; - llvm::Type* argTypes[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr}; - m_mul = Function::Create(FunctionType::get(Type::Void, argTypes, false), Linkage::ExternalLinkage, "arith_mul", getModule()); - m_div = Function::Create(FunctionType::get(Type::Void, argTypes, false), Linkage::ExternalLinkage, "arith_div", getModule()); - m_mod = Function::Create(FunctionType::get(Type::Void, argTypes, false), Linkage::ExternalLinkage, "arith_mod", getModule()); - m_sdiv = Function::Create(FunctionType::get(Type::Void, argTypes, false), Linkage::ExternalLinkage, "arith_sdiv", getModule()); - m_smod = Function::Create(FunctionType::get(Type::Void, argTypes, false), Linkage::ExternalLinkage, "arith_smod", getModule()); + llvm::Type* arg2Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr}; + llvm::Type* arg3Types[] = {Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr}; + + m_mul = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mul", getModule()); + m_div = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_div", getModule()); + m_mod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_mod", getModule()); + m_sdiv = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_sdiv", getModule()); + m_smod = Function::Create(FunctionType::get(Type::Void, arg2Types, false), Linkage::ExternalLinkage, "arith_smod", getModule()); + m_addmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_addmod", getModule()); + m_mulmod = Function::Create(FunctionType::get(Type::Void, arg3Types, false), Linkage::ExternalLinkage, "arith_mulmod", getModule()); } Arith256::~Arith256() @@ -43,6 +48,15 @@ llvm::Value* Arith256::binaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::V return m_builder.CreateLoad(m_result); } +llvm::Value* Arith256::ternaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3) +{ + m_builder.CreateStore(_arg1, m_arg1); + m_builder.CreateStore(_arg2, m_arg2); + m_builder.CreateStore(_arg3, m_arg3); + m_builder.CreateCall4(_op, m_arg1, m_arg2, m_arg3, m_result); + return m_builder.CreateLoad(m_result); +} + llvm::Value* Arith256::mul(llvm::Value* _arg1, llvm::Value* _arg2) { return binaryOp(m_mul, _arg1, _arg2); @@ -68,6 +82,17 @@ llvm::Value* Arith256::smod(llvm::Value* _arg1, llvm::Value* _arg2) return binaryOp(m_smod, _arg1, _arg2); } +llvm::Value* Arith256::addmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3) +{ + return ternaryOp(m_addmod, _arg1, _arg2, _arg3); +} + +llvm::Value* Arith256::mulmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3) +{ + return ternaryOp(m_mulmod, _arg1, _arg2, _arg3); +} + + } } } @@ -113,6 +138,22 @@ EXPORT void arith_smod(i256* _arg1, i256* _arg2, i256* _result) *_result = eth2llvm(arg2 == 0 ? arg2 : dev::s2u(dev::u2s(arg1) % dev::u2s(arg2))); } +EXPORT void arith_mulmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* _result) +{ + dev::u256 arg1 = llvm2eth(*_arg1); + dev::u256 arg2 = llvm2eth(*_arg2); + dev::u256 arg3 = llvm2eth(*_arg3); + *_result = eth2llvm(dev::u256((dev::bigint(arg1) * dev::bigint(arg2)) % arg3)); +} + +EXPORT void arith_addmod(i256* _arg1, i256* _arg2, i256* _arg3, i256* _result) +{ + dev::u256 arg1 = llvm2eth(*_arg1); + dev::u256 arg2 = llvm2eth(*_arg2); + dev::u256 arg3 = llvm2eth(*_arg3); + *_result = eth2llvm(dev::u256((dev::bigint(arg1) + dev::bigint(arg2)) % arg3)); +} + } diff --git a/libevmjit/Arith256.h b/libevmjit/Arith256.h index 32afae8a7..2268d5076 100644 --- a/libevmjit/Arith256.h +++ b/libevmjit/Arith256.h @@ -20,18 +20,24 @@ public: llvm::Value* mod(llvm::Value* _arg1, llvm::Value* _arg2); llvm::Value* sdiv(llvm::Value* _arg1, llvm::Value* _arg2); llvm::Value* smod(llvm::Value* _arg1, llvm::Value* _arg2); + llvm::Value* mulmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3); + llvm::Value* addmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3); private: llvm::Value* binaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2); + llvm::Value* ternaryOp(llvm::Function* _op, llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3); llvm::Function* m_mul; llvm::Function* m_div; llvm::Function* m_mod; llvm::Function* m_sdiv; llvm::Function* m_smod; + llvm::Function* m_mulmod; + llvm::Function* m_addmod; llvm::Value* m_arg1; llvm::Value* m_arg2; + llvm::Value* m_arg3; llvm::Value* m_result; }; diff --git a/libevmjit/Compiler.cpp b/libevmjit/Compiler.cpp index cd680d927..d36ee5009 100644 --- a/libevmjit/Compiler.cpp +++ b/libevmjit/Compiler.cpp @@ -488,9 +488,8 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode, { auto lhs = stack.pop(); auto rhs = stack.pop(); - auto sum = m_builder.CreateAdd(lhs, rhs); auto mod = stack.pop(); - auto res = arith.mod(sum, mod); + auto res = arith.addmod(lhs, rhs, mod); stack.push(res); break; } @@ -499,9 +498,8 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode, { auto lhs = stack.pop(); auto rhs = stack.pop(); - auto prod = m_builder.CreateMul(lhs, rhs); auto mod = stack.pop(); - auto res = arith.mod(prod, mod); + auto res = arith.mulmod(lhs, rhs, mod); stack.push(res); break; } diff --git a/test/vm.cpp b/test/vm.cpp index d62329af4..c056abf5b 100644 --- a/test/vm.cpp +++ b/test/vm.cpp @@ -24,7 +24,7 @@ #include #include #include - +#include //#define FILL_TESTS using namespace std; @@ -357,7 +357,7 @@ void FakeExtVM::importExec(mObject& _o) thisTxCode.clear(); code = &thisTxCode; if (_o["code"].type() == str_type) - if (_o["code"].get_str().find_first_of("0x") == 0) + if (_o["code"].get_str().find_first_of("0x") != 0) thisTxCode = compileLLL(_o["code"].get_str()); else thisTxCode = fromHex(_o["code"].get_str().substr(2)); @@ -518,10 +518,16 @@ void doTests(json_spirit::mValue& v, bool _fillin) auto argv = boost::unit_test::framework::master_test_suite().argv; auto useJit = argc >= 2 && std::string(argv[1]) == "--jit"; + auto showTimes = false; + for (auto i = 0; i < argc; ++i) + showTimes |= std::string(argv[i]) == "--show-times"; + auto vmKind = useJit ? VMFace::JIT : VMFace::Interpreter; auto vm = VMFace::create(vmKind, fev.gas); bytes output; auto outOfGas = false; + + auto startTime = std::chrono::high_resolution_clock::now(); try { output = vm->go(fev).toVector(); @@ -538,6 +544,16 @@ void doTests(json_spirit::mValue& v, bool _fillin) { cnote << "VM did throw an exception: " << _e.what(); } + + auto endTime = std::chrono::high_resolution_clock::now(); + if (showTimes) + { + auto testDuration = endTime - startTime; + cnote << "Execution time: " + << std::chrono::duration_cast(testDuration).count() + << " ms"; + } + auto gas = vm->gas(); // delete null entries in storage for the sake of comparison @@ -760,8 +776,14 @@ BOOST_AUTO_TEST_CASE(vmPushDupSwapTest) dev::test::executeTests("vmPushDupSwapTest"); } +BOOST_AUTO_TEST_CASE(vmPerformanceTest) +{ + dev::test::executeTests("vmPerformanceTest"); +} + BOOST_AUTO_TEST_CASE(vmSystemOperationsTest) { dev::test::executeTests("vmSystemOperationsTest"); } + From 4c7fddd3e81fe5ca7adc9643d5dec7fa038a721d Mon Sep 17 00:00:00 2001 From: artur-zawlocki Date: Wed, 29 Oct 2014 11:03:41 +0000 Subject: [PATCH 4/5] added dependency on libethereum to evmcc --- evmcc/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/evmcc/CMakeLists.txt b/evmcc/CMakeLists.txt index ae9162162..52ce6ec38 100644 --- a/evmcc/CMakeLists.txt +++ b/evmcc/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable(${EXECUTABLE} ${SRC_LIST}) target_link_libraries(${EXECUTABLE} devcore) target_link_libraries(${EXECUTABLE} ethcore) +target_link_libraries(${EXECUTABLE} ethereum) target_link_libraries(${EXECUTABLE} evm) target_link_libraries(${EXECUTABLE} evmface) target_link_libraries(${EXECUTABLE} evmjit) From 0432706b2fa1391bf4bac7f3aeff0e6a93a52d90 Mon Sep 17 00:00:00 2001 From: artur-zawlocki Date: Wed, 29 Oct 2014 11:05:01 +0000 Subject: [PATCH 5/5] added inlcude of setjmp.h required for jmp_buf type --- libevmjit/Runtime.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libevmjit/Runtime.h b/libevmjit/Runtime.h index 418652c40..e71b646cf 100644 --- a/libevmjit/Runtime.h +++ b/libevmjit/Runtime.h @@ -1,6 +1,8 @@ #pragma once + +#include #include #include