Browse Source

* codegen for CODECALL

* fixes for EXTCODECOPY
cl-refactor
artur-zawlocki 10 years ago
parent
commit
00a872ec98
  1. 9
      evmcc/Compiler.cpp
  2. 1
      evmcc/ExecutionEngine.cpp
  3. 25
      evmcc/Ext.cpp
  4. 3
      evmcc/Ext.h
  5. 1
      evmcc/test/ext/extcodecopy1.evm
  6. 11
      evmcc/test/ext/extcodecopy1.lll

9
evmcc/Compiler.cpp

@ -873,9 +873,10 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
}
case Instruction::CALL:
case Instruction::CALLCODE:
{
auto gas = stack.pop();
auto receiveAddress = stack.pop();
auto codeAddress = stack.pop();
auto value = stack.pop();
auto inOff = stack.pop();
auto inSize = stack.pop();
@ -891,7 +892,11 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto sizeReq = builder.CreateSelect(cmp, inSizeReq, outSizeReq, "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);
stack.push(ret);
break;

1
evmcc/ExecutionEngine.cpp

@ -27,7 +27,6 @@ ExecutionEngine::ExecutionEngine()
}
int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module)
{
auto module = _module.get(); // Keep ownership of the module in _module

25
evmcc/Ext.cpp

@ -60,6 +60,7 @@ Ext::Ext(llvm::IRBuilder<>& _builder, llvm::Module* module)
m_arg5 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg5");
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.arg8");
Type* elements[] = {
i256Ty, // i256 address;
@ -90,12 +91,12 @@ 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_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);
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_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_exp = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_exp", module);
m_codeAt = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_codeAt", 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);
@ -176,7 +177,7 @@ Value* Ext::create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value*
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]);
auto receiveAddress = bswap(_receiveAddress); // to BE
@ -186,7 +187,10 @@ llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::V
m_builder.CreateStore(_inSize, m_arg5);
m_builder.CreateStore(_outOff, m_arg6);
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);
_gas = m_builder.CreateLoad(m_args[0]); // Return gas
return m_builder.CreateLoad(m_args[1]);
@ -216,9 +220,7 @@ llvm::Value* Ext::codeAt(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_codeAt, args);
return m_builder.CreateLoad(m_args[1]);
return m_builder.CreateCall(m_codeAt, m_args[0]);
}
llvm::Value* Ext::codesizeAt(llvm::Value* _addr)
@ -315,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 value = llvm2eth(*_value);
@ -333,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&& outRef = dev::bytesConstRef(Runtime::getMemory().data() + outOff, outSize);
dev::eth::OnOpFunc onOp{}; // TODO: Handle that thing
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);
@ -357,12 +360,12 @@ EXPORT void ext_exp(i256* _left, i256* _right, i256* _ret)
*_ret = eth2llvm(ret);
}
EXPORT void ext_codeAt(h256* _addr256, i256* _ret)
EXPORT unsigned char* ext_codeAt(h256* _addr256)
{
auto&& ext = Runtime::getExt();
auto addr = dev::right160(*_addr256);
auto& code = ext.codeAt(addr);
*_ret = *reinterpret_cast<i256*>(const_cast<unsigned char*>(&code[0]));
return const_cast<unsigned char*>(code.data());
}
EXPORT void ext_codesizeAt(h256* _addr256, i256* _ret)

3
evmcc/Ext.h

@ -39,7 +39,7 @@ public:
void suicide(llvm::Value* _address);
llvm::Value* calldataload(llvm::Value* _index);
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* exp(llvm::Value* _left, llvm::Value* _right);
@ -61,6 +61,7 @@ private:
llvm::Value* m_arg5;
llvm::Value* m_arg6;
llvm::Value* m_arg7;
llvm::Value* m_arg8;
llvm::Value* m_data;
llvm::Function* m_init;
llvm::Function* m_store;

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