Browse Source

Merge branch 'develop-evmcc' of https://github.com/imapp-pl/ethereum into develop-evmcc

Conflicts:
	evmcc/ExecutionEngine.cpp
cl-refactor
Paweł Bylica 10 years ago
parent
commit
a36a432e5f
  1. 59
      evmcc/Compiler.cpp
  2. 53
      evmcc/Ext.cpp
  3. 7
      evmcc/Ext.h
  4. 24
      evmcc/Memory.cpp
  5. 2
      evmcc/Memory.h
  6. 1
      evmcc/test/ext/extcodecopy1.evm
  7. 11
      evmcc/test/ext/extcodecopy1.lll

59
evmcc/Compiler.cpp

@ -745,35 +745,51 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
break; break;
} }
case Instruction::CALLDATACOPY: case Instruction::EXTCODESIZE:
case Instruction::CODECOPY:
{ {
auto zero256 = ConstantInt::get(Type::i256, 0); auto addr = stack.pop();
auto value = ext.codesizeAt(addr);
stack.push(value);
break;
}
case Instruction::CALLDATACOPY:
{
auto destMemIdx = stack.pop(); auto destMemIdx = stack.pop();
auto srcIdx = stack.pop(); auto srcIdx = stack.pop();
auto reqBytes = stack.pop(); auto reqBytes = stack.pop();
auto reqMemSize = builder.CreateAdd(destMemIdx, reqBytes, "req_mem_size"); auto srcPtr = ext.calldata();
memory.require(reqMemSize); auto srcSize = ext.calldatasize();
auto memPtr = memory.getData(); memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
auto destPtr = builder.CreateGEP(memPtr, destMemIdx, "dest_mem_ptr"); break;
}
auto srcBasePtr = inst == Instruction::CALLDATACOPY ? ext.calldata() : ext.code(); case Instruction::CODECOPY:
auto srcPtr = builder.CreateGEP(srcBasePtr, srcIdx, "src_idx"); {
auto destMemIdx = stack.pop();
auto srcIdx = stack.pop();
auto reqBytes = stack.pop();
auto srcSize = inst == Instruction::CALLDATACOPY ? ext.calldatasize() : ext.codesize(); auto srcPtr = ext.code();
// remaining data bytes: auto srcSize = ext.codesize();
auto remSrcSize = builder.CreateSub(srcSize, srcIdx);
auto remSizeNegative = builder.CreateICmpSLT(remSrcSize, zero256);
auto remSrcBytes = builder.CreateSelect(remSizeNegative, zero256, remSrcSize, "rem_src_bytes");
auto tooLittleDataBytes = builder.CreateICmpULT(remSrcBytes, reqBytes); memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
auto bytesToCopy = builder.CreateSelect(tooLittleDataBytes, remSrcBytes, reqBytes, "bytes_to_copy"); break;
}
builder.CreateMemCpy(destPtr, srcPtr, bytesToCopy, 0); case Instruction::EXTCODECOPY:
{
auto extAddr = stack.pop();
auto destMemIdx = stack.pop();
auto srcIdx = stack.pop();
auto reqBytes = stack.pop();
auto srcPtr = ext.codeAt(extAddr);
auto srcSize = ext.codesizeAt(extAddr);
memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
break; break;
} }
@ -847,9 +863,10 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
} }
case Instruction::CALL: case Instruction::CALL:
case Instruction::CALLCODE:
{ {
auto gas = stack.pop(); auto gas = stack.pop();
auto receiveAddress = stack.pop(); auto codeAddress = stack.pop();
auto value = stack.pop(); auto value = stack.pop();
auto inOff = stack.pop(); auto inOff = stack.pop();
auto inSize = stack.pop(); auto inSize = stack.pop();
@ -865,7 +882,11 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto sizeReq = builder.CreateSelect(cmp, inSizeReq, outSizeReq, "sizeReq"); auto sizeReq = builder.CreateSelect(cmp, inSizeReq, outSizeReq, "sizeReq");
memory.require(sizeReq); memory.require(sizeReq);
auto ret = ext.call(gas, receiveAddress, value, inOff, inSize, outOff, outSize); auto receiveAddress = codeAddress;
if (inst == Instruction::CALLCODE)
receiveAddress = ext.address();
auto ret = ext.call(gas, receiveAddress, value, inOff, inSize, outOff, outSize, codeAddress);
gasMeter.giveBack(gas); gasMeter.giveBack(gas);
stack.push(ret); stack.push(ret);
break; break;

53
evmcc/Ext.cpp

@ -14,6 +14,7 @@ using llvm::types::i;
using Linkage = llvm::GlobalValue::LinkageTypes; using Linkage = llvm::GlobalValue::LinkageTypes;
using dev::h256; using dev::h256;
using dev::u256; using dev::u256;
namespace evmcc namespace evmcc
{ {
@ -59,6 +60,7 @@ Ext::Ext(llvm::IRBuilder<>& _builder, llvm::Module* module)
m_arg5 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg5"); m_arg5 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg5");
m_arg6 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg6"); m_arg6 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg6");
m_arg7 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg7"); m_arg7 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg7");
m_arg7 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg8");
Type* elements[] = { Type* elements[] = {
i256Ty, // i256 address; i256Ty, // i256 address;
@ -89,11 +91,13 @@ Ext::Ext(llvm::IRBuilder<>& _builder, llvm::Module* module)
m_balance = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_balance", module); m_balance = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_balance", module);
m_suicide = Function::Create(TypeBuilder<void(i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_suicide", module); m_suicide = Function::Create(TypeBuilder<void(i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_suicide", module);
m_create = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_create", module); m_create = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_create", module);
Type* args[] = {i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy}; Type* args[] = {i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy, i256PtrTy};
m_call = Function::Create(FunctionType::get(m_builder.getVoidTy(), args, false), Linkage::ExternalLinkage, "ext_call", module); m_call = Function::Create(FunctionType::get(m_builder.getVoidTy(), args, false), Linkage::ExternalLinkage, "ext_call", module);
m_bswap = Intrinsic::getDeclaration(module, Intrinsic::bswap, i256Ty); m_bswap = Intrinsic::getDeclaration(module, Intrinsic::bswap, i256Ty);
m_sha3 = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_sha3", module); m_sha3 = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_sha3", module);
m_exp = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_exp", module); m_exp = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_exp", module);
m_codeAt = Function::Create(TypeBuilder<i<8>*(i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_codeAt", module);
m_codesizeAt = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_codesizeAt", module);
m_builder.CreateCall(m_init, m_data); m_builder.CreateCall(m_init, m_data);
} }
@ -173,7 +177,7 @@ Value* Ext::create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value*
return address; return address;
} }
llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize) llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress)
{ {
m_builder.CreateStore(_gas, m_args[0]); m_builder.CreateStore(_gas, m_args[0]);
auto receiveAddress = bswap(_receiveAddress); // to BE auto receiveAddress = bswap(_receiveAddress); // to BE
@ -183,7 +187,10 @@ llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::V
m_builder.CreateStore(_inSize, m_arg5); m_builder.CreateStore(_inSize, m_arg5);
m_builder.CreateStore(_outOff, m_arg6); m_builder.CreateStore(_outOff, m_arg6);
m_builder.CreateStore(_outSize, m_arg7); m_builder.CreateStore(_outSize, m_arg7);
llvm::Value* args[] = {m_args[0], m_arg2, m_arg3, m_arg4, m_arg5, m_arg6, m_arg7, m_args[1]}; auto codeAddress = bswap(_codeAddress); // toBE
m_builder.CreateStore(codeAddress, m_arg8);
llvm::Value* args[] = {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); m_builder.CreateCall(m_call, args);
_gas = m_builder.CreateLoad(m_args[0]); // Return gas _gas = m_builder.CreateLoad(m_args[0]); // Return gas
return m_builder.CreateLoad(m_args[1]); return m_builder.CreateLoad(m_args[1]);
@ -209,9 +216,30 @@ llvm::Value* Ext::exp(llvm::Value* _left, llvm::Value* _right)
return m_builder.CreateLoad(m_args[1]); return m_builder.CreateLoad(m_args[1]);
} }
llvm::Value* Ext::codeAt(llvm::Value* _addr)
{
auto addr = bswap(_addr);
m_builder.CreateStore(addr, m_args[0]);
return m_builder.CreateCall(m_codeAt, m_args[0]);
}
llvm::Value* Ext::codesizeAt(llvm::Value* _addr)
{
auto addr = bswap(_addr);
m_builder.CreateStore(addr, m_args[0]);
llvm::Value* args[] = {m_args[0], m_args[1]};
m_builder.CreateCall(m_codesizeAt, args);
return m_builder.CreateLoad(m_args[1]);
}
}
extern "C" extern "C"
{ {
using namespace evmcc;
EXPORT void ext_init(ExtData* _extData) EXPORT void ext_init(ExtData* _extData)
{ {
auto&& ext = Runtime::getExt(); auto&& ext = Runtime::getExt();
@ -289,7 +317,7 @@ EXPORT void ext_create(i256* _endowment, i256* _initOff, i256* _initSize, h256*
EXPORT void ext_call(i256* _gas, h256* _receiveAddress, i256* _value, i256* _inOff, i256* _inSize, i256* _outOff, i256* _outSize, i256* _ret) EXPORT void ext_call(i256* _gas, h256* _receiveAddress, i256* _value, i256* _inOff, i256* _inSize, i256* _outOff, i256* _outSize, h256* _codeAddress, i256* _ret)
{ {
auto&& ext = Runtime::getExt(); auto&& ext = Runtime::getExt();
auto value = llvm2eth(*_value); auto value = llvm2eth(*_value);
@ -307,7 +335,8 @@ EXPORT void ext_call(i256* _gas, h256* _receiveAddress, i256* _value, i256* _inO
auto&& inRef = dev::bytesConstRef(Runtime::getMemory().data() + inOff, inSize); auto&& inRef = dev::bytesConstRef(Runtime::getMemory().data() + inOff, inSize);
auto&& outRef = dev::bytesConstRef(Runtime::getMemory().data() + outOff, outSize); auto&& outRef = dev::bytesConstRef(Runtime::getMemory().data() + outOff, outSize);
dev::eth::OnOpFunc onOp{}; // TODO: Handle that thing dev::eth::OnOpFunc onOp{}; // TODO: Handle that thing
auto ret = ext.call(receiveAddress, value, inRef, &gas, outRef, onOp, {}, receiveAddress); auto codeAddress = dev::right160(*_codeAddress);
ret = ext.call(receiveAddress, value, inRef, &gas, outRef, onOp, {}, codeAddress);
} }
*_gas = eth2llvm(gas); *_gas = eth2llvm(gas);
@ -331,6 +360,20 @@ EXPORT void ext_exp(i256* _left, i256* _right, i256* _ret)
*_ret = eth2llvm(ret); *_ret = eth2llvm(ret);
} }
EXPORT unsigned char* ext_codeAt(h256* _addr256)
{
auto&& ext = Runtime::getExt();
auto addr = dev::right160(*_addr256);
auto& code = ext.codeAt(addr);
return const_cast<unsigned char*>(code.data());
}
EXPORT void ext_codesizeAt(h256* _addr256, i256* _ret)
{
auto&& ext = Runtime::getExt();
auto addr = dev::right160(*_addr256);
auto& code = ext.codeAt(addr);
*_ret = eth2llvm(u256(code.size()));
} }
} }

7
evmcc/Ext.h

@ -39,10 +39,12 @@ public:
void suicide(llvm::Value* _address); void suicide(llvm::Value* _address);
llvm::Value* calldataload(llvm::Value* _index); llvm::Value* calldataload(llvm::Value* _index);
llvm::Value* create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize); llvm::Value* create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize);
llvm::Value* call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize); llvm::Value* call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress);
llvm::Value* sha3(llvm::Value* _inOff, llvm::Value* _inSize); llvm::Value* sha3(llvm::Value* _inOff, llvm::Value* _inSize);
llvm::Value* exp(llvm::Value* _left, llvm::Value* _right); llvm::Value* exp(llvm::Value* _left, llvm::Value* _right);
llvm::Value* codeAt(llvm::Value* _addr);
llvm::Value* codesizeAt(llvm::Value* _addr);
private: private:
llvm::Value* getDataElem(unsigned _index, const llvm::Twine& _name = ""); llvm::Value* getDataElem(unsigned _index, const llvm::Twine& _name = "");
@ -59,6 +61,7 @@ private:
llvm::Value* m_arg5; llvm::Value* m_arg5;
llvm::Value* m_arg6; llvm::Value* m_arg6;
llvm::Value* m_arg7; llvm::Value* m_arg7;
llvm::Value* m_arg8;
llvm::Value* m_data; llvm::Value* m_data;
llvm::Function* m_init; llvm::Function* m_init;
llvm::Function* m_store; llvm::Function* m_store;
@ -71,6 +74,8 @@ private:
llvm::Function* m_bswap; llvm::Function* m_bswap;
llvm::Function* m_sha3; llvm::Function* m_sha3;
llvm::Function* m_exp; llvm::Function* m_exp;
llvm::Function* m_codeAt;
llvm::Function* m_codesizeAt;
}; };

24
evmcc/Memory.cpp

@ -177,6 +177,30 @@ void Memory::registerReturnData(llvm::Value* _index, llvm::Value* _size)
m_builder.CreateStore(_size, m_returnDataSize); m_builder.CreateStore(_size, m_returnDataSize);
} }
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx,
llvm::Value* _destMemIdx, llvm::Value* _reqBytes)
{
auto zero256 = llvm::ConstantInt::get(Type::i256, 0);
auto reqMemSize = m_builder.CreateAdd(_destMemIdx, _reqBytes, "req_mem_size");
require(reqMemSize);
auto srcPtr = m_builder.CreateGEP(_srcPtr, _srcIdx, "src_idx");
auto memPtr = getData();
auto destPtr = m_builder.CreateGEP(memPtr, _destMemIdx, "dest_mem_ptr");
// remaining source bytes:
auto remSrcSize = m_builder.CreateSub(_srcSize, _srcIdx);
auto remSizeNegative = m_builder.CreateICmpSLT(remSrcSize, zero256);
auto remSrcBytes = m_builder.CreateSelect(remSizeNegative, zero256, remSrcSize, "rem_src_bytes");
auto tooFewSrcBytes = m_builder.CreateICmpULT(remSrcBytes, _reqBytes);
auto bytesToCopy = m_builder.CreateSelect(tooFewSrcBytes, remSrcBytes, _reqBytes, "bytes_to_copy");
m_builder.CreateMemCpy(destPtr, srcPtr, bytesToCopy, 0);
}
void Memory::dump(uint64_t _begin, uint64_t _end) void Memory::dump(uint64_t _begin, uint64_t _end)
{ {
if (getenv("EVMCC_DEBUG_MEMORY") == nullptr) if (getenv("EVMCC_DEBUG_MEMORY") == nullptr)

2
evmcc/Memory.h

@ -20,6 +20,8 @@ public:
void storeByte(llvm::Value* _addr, llvm::Value* _byte); void storeByte(llvm::Value* _addr, llvm::Value* _byte);
llvm::Value* getData(); llvm::Value* getData();
llvm::Value* getSize(); llvm::Value* getSize();
void copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIndex,
llvm::Value* _destMemIdx, llvm::Value* _byteCount);
/// Requires this amount of memory. And counts gas fee for that memory. /// Requires this amount of memory. And counts gas fee for that memory.
void require(llvm::Value* _size); void require(llvm::Value* _size);

1
evmcc/test/ext/extcodecopy1.evm

@ -0,0 +1 @@
60c86000600a303c60005360006020f2

11
evmcc/test/ext/extcodecopy1.lll

@ -0,0 +1,11 @@
(asm
200 ;; byte count
0 ;; source index in code array
10 ;; dest index in memory
ADDRESS
EXTCODECOPY
0 MLOAD ;; to dump memory
0 32 RETURN
)
Loading…
Cancel
Save