Browse Source

Merge branch 'develop-evmcc' into pr-jit

cl-refactor
Paweł Bylica 10 years ago
parent
commit
3146b6d05d
  1. 1
      evmcc/CMakeLists.txt
  2. 125
      evmcc/evmcc.cpp
  3. 1
      evmcc/test/except/badinst1.evm
  4. 169
      evmcc/test/vmtests/vmPerformanceTest.json
  5. 2
      libdevcore/FixedHash.h
  6. 8
      libevm/VM.h
  7. 53
      libevmjit/Arith256.cpp
  8. 6
      libevmjit/Arith256.h
  9. 33
      libevmjit/Compiler.cpp
  10. 7
      libevmjit/CompilerHelper.h
  11. 23
      libevmjit/ExecutionEngine.cpp
  12. 21
      libevmjit/Ext.cpp
  13. 8
      libevmjit/GasMeter.cpp
  14. 2
      libevmjit/GasMeter.h
  15. 16
      libevmjit/Runtime.cpp
  16. 11
      libevmjit/Runtime.h
  17. 3
      libevmjit/Type.h
  18. 3
      libevmjit/Utils.h
  19. 11
      libevmjit/VM.cpp
  20. 46
      test/vm.cpp
  21. 2
      test/vm.h
  22. 1
      windows/LibEthereum.vcxproj.filters
  23. 2
      windows/evmcc.vcxproj

1
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)

125
evmcc/evmcc.cpp

@ -1,4 +1,5 @@
#include <chrono>
#include <iostream>
#include <fstream>
#include <ostream>
@ -18,92 +19,104 @@
void show_usage()
{
// FIXME: Use arg[0] as program name?
std::cerr << "usage: evmcc (-b|-c|-d)+ <inputfile.bc>\n";
// FIXME: Use arg[0] as program name?
std::cerr << "usage: evmcc (-b|-c|-d)+ <inputfile.bc>\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<size_t>(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<char>(ifs)),
(std::istreambuf_iterator<char>()));
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<char>(ifs)),
(std::istreambuf_iterator<char>()));
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<std::chrono::microseconds>(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;
}

1
evmcc/test/except/badinst1.evm

@ -0,0 +1 @@
4a

169
evmcc/test/vmtests/vmPerformanceTest.json

@ -0,0 +1,169 @@
{
"for100000" : {
"callcreates" : [ ],
"env" : {
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty" : "256",
"currentGasLimit" : "10000000",
"currentNumber" : "0",
"currentTimestamp" : "1",
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"code" : "0x620186a06080545d60006080530b0f60295960a0536080530160a0546001608053036080546008585d",
"data" : "0x",
"gas" : "10000000",
"gasPrice" : "100000000000000",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000"
},
"gas" : "7799983",
"out" : "0x0",
"post" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x620186a06080545d60006080530b0f60295960a0536080530160a0546001608053036080546008585d",
"nonce" : "0",
"storage" : {
}
}
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x620186a06080545d60006080530b0f60295960a0536080530160a0546001608053036080546008585d",
"nonce" : "0",
"storage" : { }
}
}
},
"recloop" : {
"callcreates" : [ ],
"env" : {
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty" : "256",
"currentGasLimit" : "10000000",
"currentNumber" : "0",
"currentTimestamp" : "1",
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"code" : "0x600c6204000060006015585d60005460206000f25d6000820e602e59602a60018303600183016015585d6030585d805d905090509058",
"data" : "0x",
"gas" : "10000000",
"gasPrice" : "100000000000000",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000"
},
"gas" : "4232808",
"out" : "0x0000000000000000000000000000000000000000000000000000000000040000",
"post" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x600c6204000060006015585d60005460206000f25d6000820e602e59602a60018303600183016015585d6030585d805d905090509058",
"nonce" : "0",
"storage" : {
}
}
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x600c6204000060006015585d60005460206000f25d6000820e602e59602a60018303600183016015585d6030585d805d905090509058",
"nonce" : "0",
"storage" : { }
}
}
},
"fib25" : {
"callcreates" : [
],
"env" : {
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty" : "256",
"currentGasLimit" : "10000000",
"currentNumber" : "0",
"currentTimestamp" : "1",
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"code" : "0x600860196011585d60005460206000f25d6003810a6031596022600282036011585d602c600183036011585d016034585d60015d90509058",
"data" : "0x",
"gas" : "10000000",
"gasPrice" : "100000000000000",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000"
},
"gas" : "7374139",
"out" : "0x0000000000000000000000000000000000000000000000000000000000012511",
"post" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x600860196011585d60005460206000f25d6003810a6031596022600282036011585d602c600183036011585d016034585d60015d90509058",
"nonce" : "0",
"storage" : {
}
}
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x600860196011585d60005460206000f25d6003810a6031596022600282036011585d602c600183036011585d016034585d60015d90509058",
"nonce" : "0",
"storage" : {
}
}
}
},
"ackermann36" : {
"callcreates" : [
],
"env" : {
"currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
"currentDifficulty" : "256",
"currentGasLimit" : "10000000",
"currentNumber" : "0",
"currentTimestamp" : "1",
"previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6"
},
"exec" : {
"address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6",
"caller" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"code" : "0x600a600360066013585d60005460206000f25d6000820e604b596000810e603a59603660018303603284600185036013585d6013585d6047585d60466001830360016013585d5d6050585d600181015d905090509058",
"data" : "0x",
"gas" : "10000000",
"gasPrice" : "100000000000000",
"origin" : "cd1722f3947def4cf144679da39c4c32bdc35681",
"value" : "1000000000000000000"
},
"gas" : "5865402",
"out" : "0x00000000000000000000000000000000000000000000000000000000000001fd",
"post" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x600a600360066013585d60005460206000f25d6000820e604b596000810e603a59603660018303603284600185036013585d6013585d6047585d60466001830360016013585d5d6050585d600181015d905090509058",
"nonce" : "0",
"storage" : {
}
}
},
"pre" : {
"0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : {
"balance" : "1000000000000000000",
"code" : "0x600a600360066013585d60005460206000f25d6000820e604b596000810e603a59603660018303603284600185036013585d6013585d6047585d60466001830360016013585d5d6050585d600181015d905090509058",
"nonce" : "0",
"storage" : {
}
}
}
},
}

2
libdevcore/FixedHash.h

@ -158,7 +158,7 @@ public:
return ret;
}
template <unsigned P, unsigned M> inline FixedHash& shiftBloom(FixedHash<M> const& _h) { return (*this |= _h.nbloom<P, N>()); }
template <unsigned P, unsigned M> inline FixedHash& shiftBloom(FixedHash<M> const& _h) { return (*this |= _h.template nbloom<P, N>()); }
template <unsigned P, unsigned M> inline FixedHash<M> nbloom() const
{

8
libevm/VM.h

@ -427,9 +427,13 @@ template <class Ext> dev::bytesConstRef dev::eth::VM::go(Ext& _ext, OnOpFunc con
break;
case Instruction::SIGNEXTEND:
{
auto k = m_stack[m_stack.size() - 2];
m_stack[m_stack.size() - 2] = m_stack.back();
unsigned k = m_stack.back().convert_to<unsigned>();
m_stack.pop_back();
auto& b = m_stack.back();
if (k <= 31)
if ((b >> (k * 8)) & 0x80)
for (unsigned i = 31; i > k; --i)
b |= (u256(0xff) << i);
break;
}
case Instruction::SHA3:

53
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));
}
}

6
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;
};

33
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;
}
@ -153,8 +156,8 @@ void Compiler::createBasicBlocks(bytesConstRef bytecode)
}
else
{
std::cerr << "Bad JUMP at PC " << it->first
<< ": " << it->second << " is not a valid PC\n";
clog(JIT) << "Bad JUMP at PC " << it->first
<< ": " << it->second << " is not a valid PC";
m_directJumpTargets[it->first] = m_badJumpBlock->llvm();
}
}
@ -239,8 +242,8 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytesConstRef bytecode)
dump();
}
if (getenv("EVMCC_OPTIMIZE_STACK"))
{
//if (getenv("EVMCC_OPTIMIZE_STACK"))
//{
std::vector<BasicBlock*> blockList;
for (auto& entry : basicBlocks)
blockList.push_back(&entry.second);
@ -258,7 +261,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytesConstRef bytecode)
std::cerr << "\n\nAfter stack optimization \n\n";
dump();
}
}
//}
for (auto& entry : basicBlocks)
entry.second.localStack().synchronize(stack);
@ -486,9 +489,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;
}
@ -497,9 +499,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;
}
@ -654,8 +655,7 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode,
case Instruction::JUMPDEST:
{
// Extra asserts just in case.
assert(currentPC == basicBlock.begin());
// Nothing to do
break;
}
@ -816,6 +816,11 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode,
break;
}
default: // Invalid instruction - runtime exception
{
_runtimeManager.raiseException(ReturnCode::BadInstruction);
}
}
}

7
libevmjit/CompilerHelper.h

@ -31,6 +31,13 @@ protected:
llvm::IRBuilder<>& m_builder;
llvm::IRBuilder<>& getBuilder() { return m_builder; }
template<typename ..._Args>
llvm::CallInst* createCall(llvm::Function* _func, _Args*... _args)
{
llvm::Value* args[] = {_args...};
return getBuilder().CreateCall(_func, args);
}
friend class RuntimeHelper;
};

23
libevmjit/ExecutionEngine.cpp

@ -75,9 +75,8 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module, u256& _gas, ExtV
auto finalizationStartTime = std::chrono::high_resolution_clock::now();
exec->finalizeObject();
auto finalizationEndTime = std::chrono::high_resolution_clock::now();
std::cerr << "*** Module finalization time: "
<< std::chrono::duration_cast<std::chrono::microseconds>(finalizationEndTime - finalizationStartTime).count()
<< std::endl;
clog(JIT) << "Module finalization time: "
<< std::chrono::duration_cast<std::chrono::microseconds>(finalizationEndTime - finalizationStartTime).count();
// Create fake ExtVM interface
if (!_ext)
@ -117,9 +116,8 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module, u256& _gas, ExtV
returnCode = static_cast<ReturnCode>(result.IntVal.getZExtValue());
auto executionEndTime = std::chrono::high_resolution_clock::now();
std::cerr << "*** Execution time: "
<< std::chrono::duration_cast<std::chrono::microseconds>(executionEndTime - executionStartTime).count()
<< std::endl;
clog(JIT) << "Execution time : "
<< std::chrono::duration_cast<std::chrono::microseconds>(executionEndTime - executionStartTime).count();
}
else
@ -128,19 +126,20 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module, u256& _gas, ExtV
// Return remaining gas
_gas = returnCode == ReturnCode::OutOfGas ? 0 : runtime.getGas();
std::cout << "Max stack size: " << Stack::maxStackSize << std::endl;
clog(JIT) << "Max stack size: " << Stack::maxStackSize;
if (returnCode == ReturnCode::Return)
{
returnData = runtime.getReturnData().toVector(); // TODO: It might be better to place is in Runtime interface
std::cout << "RETURN [ ";
auto&& log = clog(JIT);
log << "RETURN [ ";
for (auto it = returnData.begin(), end = returnData.end(); it != end; ++it)
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)*it << " ";
std::cout << "]\n";
log << std::hex << std::setw(2) << std::setfill('0') << (int)*it << " ";
log << "]";
}
std::cout << "RETURN CODE: " << (int)returnCode << std::endl;
else
cslog(JIT) << "RETURN " << (int)returnCode;
return static_cast<int>(returnCode);
}

21
libevmjit/Ext.cpp

@ -109,8 +109,7 @@ llvm::Value* Ext::create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::V
m_builder.CreateStore(_endowment, m_args[0]);
m_builder.CreateStore(_initOff, m_arg2);
m_builder.CreateStore(_initSize, m_arg3);
llvm::Value* args[] = {getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_arg3, m_args[1]};
m_builder.CreateCall(m_create, args);
createCall(m_create, getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_arg3, m_args[1]);
llvm::Value* address = m_builder.CreateLoad(m_args[1]);
address = Endianness::toNative(m_builder, address);
return address;
@ -128,9 +127,7 @@ llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::V
m_builder.CreateStore(_outSize, m_arg7);
auto codeAddress = Endianness::toBE(m_builder, _codeAddress);
m_builder.CreateStore(codeAddress, m_arg8);
llvm::Value* args[] = {getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7, m_arg8, m_args[1]};
m_builder.CreateCall(m_call, args);
createCall(m_call, getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7, m_arg8, m_args[1]);
_gas = m_builder.CreateLoad(m_args[0]); // Return gas
return m_builder.CreateLoad(m_args[1]);
}
@ -139,8 +136,7 @@ llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize)
{
m_builder.CreateStore(_inOff, m_args[0]);
m_builder.CreateStore(_inSize, m_arg2);
llvm::Value* args[] = {getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_args[1]};
m_builder.CreateCall(m_sha3, args);
createCall(m_sha3, getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_args[1]);
llvm::Value* hash = m_builder.CreateLoad(m_args[1]);
hash = Endianness::toNative(m_builder, hash);
return hash;
@ -148,10 +144,10 @@ llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize)
llvm::Value* Ext::exp(llvm::Value* _left, llvm::Value* _right)
{
// TODO: Move ext to Arith256
m_builder.CreateStore(_left, m_args[0]);
m_builder.CreateStore(_right, m_arg2);
llvm::Value* args[] = {getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_args[1]};
m_builder.CreateCall(m_exp, args);
createCall(m_exp, getRuntimeManager().getRuntimePtr(), m_args[0], m_arg2, m_args[1]);
return m_builder.CreateLoad(m_args[1]);
}
@ -166,8 +162,7 @@ llvm::Value* Ext::codesizeAt(llvm::Value* _addr)
{
auto addr = Endianness::toBE(m_builder, _addr);
m_builder.CreateStore(addr, m_args[0]);
llvm::Value* args[] = {getRuntimeManager().getRuntimePtr(), m_args[0], m_args[1]};
m_builder.CreateCall(m_codesizeAt, args);
createCall(m_codesizeAt, getRuntimeManager().getRuntimePtr(), m_args[0], m_args[1]);
return m_builder.CreateLoad(m_args[1]);
}
@ -279,7 +274,7 @@ EXPORT void ext_exp(Runtime* _rt, i256* _left, i256* _right, i256* _ret)
*_ret = eth2llvm(ret);
}
EXPORT unsigned char* ext_codeAt(Runtime* _rt, h256* _addr256) //FIXME: Check endianess
EXPORT unsigned char* ext_codeAt(Runtime* _rt, h256* _addr256)
{
auto&& ext = _rt->getExt();
auto addr = right160(*_addr256);
@ -287,7 +282,7 @@ EXPORT unsigned char* ext_codeAt(Runtime* _rt, h256* _addr256) //FIXME: Check en
return const_cast<unsigned char*>(code.data());
}
EXPORT void ext_codesizeAt(Runtime* _rt, h256* _addr256, i256* _ret) //FIXME: Check endianess
EXPORT void ext_codesizeAt(Runtime* _rt, h256* _addr256, i256* _ret)
{
auto&& ext = _rt->getExt();
auto addr = right160(*_addr256);

8
libevmjit/GasMeter.cpp

@ -101,11 +101,7 @@ GasMeter::GasMeter(llvm::IRBuilder<>& _builder, RuntimeManager& _runtimeManager)
m_builder.CreateCondBr(isOutOfGas, outOfGasBB, updateBB);
m_builder.SetInsertPoint(outOfGasBB);
//auto longjmpFunc = llvm::Intrinsic::getDeclaration(_module, llvm::Intrinsic::eh_sjlj_longjmp);
llvm::Type* args[] = {Type::BytePtr, m_builder.getInt32Ty()};
auto longjmpNative = llvm::Function::Create(llvm::FunctionType::get(Type::Void, args, false), llvm::Function::ExternalLinkage, "longjmp", module);
m_builder.CreateCall2(longjmpNative, m_runtimeManager.getJmpBuf(), Constant::get(ReturnCode::OutOfGas));
_runtimeManager.raiseException(ReturnCode::OutOfGas);
m_builder.CreateUnreachable();
m_builder.SetInsertPoint(updateBB);
@ -147,7 +143,7 @@ void GasMeter::countSStore(Ext& _ext, llvm::Value* _index, llvm::Value* _newValu
auto isDel = m_builder.CreateAnd(oldValueIsntZero, newValueIsZero, "isDel");
auto cost = m_builder.CreateSelect(isAdd, Constant::get(2 * sstoreCost), Constant::get(sstoreCost), "cost");
cost = m_builder.CreateSelect(isDel, Constant::get(0), cost, "cost");
m_builder.CreateCall(m_gasCheckFunc, cost);
createCall(m_gasCheckFunc, cost);
}
void GasMeter::giveBack(llvm::Value* _gas)

2
libevmjit/GasMeter.h

@ -13,7 +13,7 @@ namespace jit
{
class RuntimeManager;
class GasMeter : public CompilerHelper
class GasMeter : public CompilerHelper // TODO: Use RuntimeHelper
{
public:
GasMeter(llvm::IRBuilder<>& _builder, RuntimeManager& _runtimeManager);

16
libevmjit/Runtime.cpp

@ -57,13 +57,9 @@ llvm::Twine getName(RuntimeData::Index _index)
}
}
static Runtime* g_runtime; // FIXME: Remove
Runtime::Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf):
m_ext(_ext)
{
assert(!g_runtime);
g_runtime = this;
set(RuntimeData::Gas, _gas);
set(RuntimeData::Address, fromAddress(_ext.myAddress));
set(RuntimeData::Caller, fromAddress(_ext.caller));
@ -83,11 +79,6 @@ Runtime::Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf):
m_data.jmpBuf = _jmpBuf;
}
Runtime::~Runtime()
{
g_runtime = nullptr;
}
void Runtime::set(RuntimeData::Index _index, u256 _value)
{
m_data.elems[_index] = eth2llvm(_value);
@ -113,6 +104,8 @@ bytesConstRef Runtime::getReturnData() const
RuntimeManager::RuntimeManager(llvm::IRBuilder<>& _builder): CompilerHelper(_builder)
{
m_dataPtr = new llvm::GlobalVariable(*getModule(), Type::RuntimePtr, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(Type::RuntimePtr), "rt");
llvm::Type* args[] = {Type::BytePtr, m_builder.getInt32Ty()};
m_longjmp = llvm::Function::Create(llvm::FunctionType::get(Type::Void, args, false), llvm::Function::ExternalLinkage, "longjmp", getModule());
// Export data
auto mainFunc = getMainFunction();
@ -149,6 +142,11 @@ void RuntimeManager::registerReturnData(llvm::Value* _offset, llvm::Value* _size
set(RuntimeData::ReturnDataSize, _size);
}
void RuntimeManager::raiseException(ReturnCode _returnCode)
{
m_builder.CreateCall2(m_longjmp, getJmpBuf(), Constant::get(_returnCode));
}
llvm::Value* RuntimeManager::get(Instruction _inst)
{
switch (_inst)

11
libevmjit/Runtime.h

@ -1,12 +1,15 @@
#pragma once
#include <setjmp.h>
#include <vector>
#include <libevm/ExtVMFace.h>
#include "CompilerHelper.h"
#include "Utils.h"
#include "Type.h"
#ifdef _MSC_VER
@ -62,7 +65,6 @@ class Runtime
{
public:
Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf);
~Runtime();
Runtime(const Runtime&) = delete;
void operator=(const Runtime&) = delete;
@ -99,16 +101,19 @@ public:
llvm::Value* getGas(); // TODO: Remove
llvm::Value* getCallData();
llvm::Value* getCode();
llvm::Value* getJmpBuf();
void setGas(llvm::Value* _gas);
void registerReturnData(llvm::Value* _index, llvm::Value* _size);
void raiseException(ReturnCode _returnCode);
private:
llvm::Value* getPtr(RuntimeData::Index _index);
void set(RuntimeData::Index _index, llvm::Value* _value);
llvm::Value* getJmpBuf();
llvm::GlobalVariable* m_dataPtr;
llvm::GlobalVariable* m_dataPtr = nullptr;
llvm::Function* m_longjmp = nullptr;
};
}

3
libevmjit/Type.h

@ -43,7 +43,8 @@ enum class ReturnCode
BadJumpDestination = 101,
OutOfGas = 102,
StackTooSmall = 103
StackTooSmall = 103,
BadInstruction = 104,
};
struct Constant

3
libevmjit/Utils.h

@ -4,6 +4,7 @@
#include <llvm/IR/IRBuilder.h>
#include <libdevcore/Common.h>
#include <libdevcore/Log.h>
namespace dev
{
@ -12,6 +13,8 @@ namespace eth
namespace jit
{
struct JIT: public NoteChannel { static const char* name() { return "JIT"; } };
/// Representation of 256-bit value binary compatible with LLVM i256
// TODO: Replace with h256
struct i256

11
libevmjit/VM.cpp

@ -5,6 +5,7 @@
#include "ExecutionEngine.h"
#include "Compiler.h"
#include "Type.h"
namespace dev
{
@ -20,14 +21,16 @@ bytesConstRef VM::go(ExtVMFace& _ext, OnOpFunc const&, uint64_t)
ExecutionEngine engine;
auto exitCode = engine.run(std::move(module), m_gas, &_ext);
switch (exitCode)
switch (static_cast<ReturnCode>(exitCode))
{
case 101:
case ReturnCode::BadJumpDestination:
BOOST_THROW_EXCEPTION(BadJumpDestination());
case 102:
case ReturnCode::OutOfGas:
BOOST_THROW_EXCEPTION(OutOfGas());
case 103:
case ReturnCode::StackTooSmall:
BOOST_THROW_EXCEPTION(StackTooSmall(1,0));
case ReturnCode::BadInstruction:
BOOST_THROW_EXCEPTION(BadInstruction());
}
m_output = std::move(engine.returnData);

46
test/vm.cpp

@ -20,9 +20,9 @@
* vm test functions.
*/
#include <chrono>
#include <boost/filesystem/path.hpp>
#include "vm.h"
//#define FILL_TESTS
using namespace std;
@ -359,9 +359,9 @@ void FakeExtVM::importExec(mObject& _o)
code = &thisTxCode;
if (_o["code"].type() == str_type)
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));
else
thisTxCode = compileLLL(_o["code"].get_str());
else if (_o["code"].type() == array_type)
for (auto const& j: _o["code"].get_array())
thisTxCode.push_back(toByte(j));
@ -441,8 +441,8 @@ h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _end
m_cache[_newAddress] = AddressState(0, balance(_newAddress) + _endowment, h256(), h256());
// Execute init code.
auto vmObj = VMFace::create(VMFace::Interpreter, *_gas);
VMFace& vm = *vmObj;
auto vmObj = VMFace::create(getVMKind(), *_gas);
auto& vm = *vmObj;
ExtVM evm(*this, _newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _code, o_ms, _level);
bool revert = false;
bytesConstRef out;
@ -500,7 +500,14 @@ void doTests(json_spirit::mValue& v, bool _fillin)
BOOST_REQUIRE(o.count("pre") > 0);
BOOST_REQUIRE(o.count("exec") > 0);
auto argc = boost::unit_test::framework::master_test_suite().argc;
auto argv = boost::unit_test::framework::master_test_suite().argv;
auto useJit = argc >= 2 && std::string(argv[1]) == "--jit";
auto vmKind = useJit ? VMFace::JIT : VMFace::Interpreter;
dev::test::FakeExtVM fev;
fev.setVMKind(vmKind);
fev.importEnv(o["env"].get_obj());
fev.importState(o["pre"].get_obj());
@ -513,16 +520,12 @@ void doTests(json_spirit::mValue& v, bool _fillin)
fev.thisTxCode = get<3>(fev.addresses.at(fev.myAddress));
fev.code = &fev.thisTxCode;
}
auto argc = boost::unit_test::framework::master_test_suite().argc;
auto argv = boost::unit_test::framework::master_test_suite().argv;
auto useJit = argc >= 2 && std::string(argv[1]) == "--jit";
auto vmKind = useJit ? VMFace::JIT : VMFace::Interpreter;
auto vm = VMFace::create(vmKind, fev.gas);
auto vm = VMFace::create(fev.getVMKind(), fev.gas);
bytes output;
auto outOfGas = false;
auto startTime = std::chrono::high_resolution_clock::now();
try
{
output = vm->go(fev, fev.simpleTrace<FakeExtVM>()).toVector();
@ -539,6 +542,20 @@ void doTests(json_spirit::mValue& v, bool _fillin)
{
cnote << "VM did throw an exception: " << _e.what();
}
auto endTime = std::chrono::high_resolution_clock::now();
for (auto i = 0; i < argc; ++i)
{
if (std::string(argv[i]) == "--show-times")
{
auto testDuration = endTime - startTime;
cnote << "Execution time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(testDuration).count()
<< " ms";
}
break;
}
auto gas = vm->gas();
// delete null entries in storage for the sake of comparison
@ -763,6 +780,11 @@ 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");

2
test/vm.h

@ -80,6 +80,8 @@ public:
void importExec(json_spirit::mObject& _o);
json_spirit::mArray exportCallCreates();
void importCallCreates(json_spirit::mArray& _callcreates);
void setVMKind(eth::VMFace::Kind _kind) { m_s.setVMKind(_kind); }
eth::VMFace::Kind getVMKind() const { return m_s.getVMKind(); }
template<typename ExtVMType>
eth::OnOpFunc simpleTrace();

1
windows/LibEthereum.vcxproj.filters

@ -195,6 +195,7 @@
</ClCompile>
<ClCompile Include="..\libevm\VMFace.cpp">
<Filter>libevm</Filter>
</ClCompile>
<ClCompile Include="..\libdevcrypto\CryptoPP.cpp">
<Filter>libdevcrypto</Filter>
</ClCompile>

2
windows/evmcc.vcxproj

@ -171,7 +171,6 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>../../llvm-3.5.0/include;../../builds/llvm3.5/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4068;4244;4267;4800</DisableSpecificWarnings>
<EnablePREfast>false</EnablePREfast>
</ClCompile>
@ -180,7 +179,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>LibEthereum.lib;LibEvmJit.lib;LLVMX86Disassembler.lib;LLVMX86AsmParser.lib;LLVMX86CodeGen.lib;LLVMSelectionDAG.lib;LLVMAsmPrinter.lib;LLVMCodeGen.lib;LLVMScalarOpts.lib;LLVMInstCombine.lib;LLVMTransformUtils.lib;LLVMipa.lib;LLVMAnalysis.lib;LLVMX86Desc.lib;LLVMX86Info.lib;LLVMX86AsmPrinter.lib;LLVMX86Utils.lib;LLVMMCJIT.lib;LLVMTarget.lib;LLVMRuntimeDyld.lib;LLVMObject.lib;LLVMMCParser.lib;LLVMBitReader.lib;LLVMExecutionEngine.lib;LLVMMC.lib;LLVMCore.lib;LLVMSupport.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

Loading…
Cancel
Save