Browse Source

Codegen for EXTCODESIZE & EXTCODECOPY

[Delivers #80644732]
cl-refactor
artur-zawlocki 10 years ago
parent
commit
564a593bbe
  1. 50
      evmcc/Compiler.cpp
  2. 42
      evmcc/Ext.cpp
  3. 4
      evmcc/Ext.h
  4. 24
      evmcc/Memory.cpp
  5. 2
      evmcc/Memory.h

50
evmcc/Compiler.cpp

@ -744,35 +744,51 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
break;
}
case Instruction::CALLDATACOPY:
case Instruction::CODECOPY:
case Instruction::EXTCODESIZE:
{
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 srcIdx = stack.pop();
auto reqBytes = stack.pop();
auto reqMemSize = builder.CreateAdd(destMemIdx, reqBytes, "req_mem_size");
memory.require(reqMemSize);
auto srcPtr = ext.calldata();
auto srcSize = ext.calldatasize();
memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
break;
}
auto memPtr = memory.getData();
auto destPtr = builder.CreateGEP(memPtr, destMemIdx, "dest_mem_ptr");
case Instruction::CODECOPY:
{
auto destMemIdx = stack.pop();
auto srcIdx = stack.pop();
auto reqBytes = stack.pop();
auto srcBasePtr = inst == Instruction::CALLDATACOPY ? ext.calldata() : ext.code();
auto srcPtr = builder.CreateGEP(srcBasePtr, srcIdx, "src_idx");
auto srcPtr = ext.code();
auto srcSize = ext.codesize();
auto srcSize = inst == Instruction::CALLDATACOPY ? ext.calldatasize() : ext.codesize();
// remaining data bytes:
auto remSrcSize = builder.CreateSub(srcSize, srcIdx);
auto remSizeNegative = builder.CreateICmpSLT(remSrcSize, zero256);
auto remSrcBytes = builder.CreateSelect(remSizeNegative, zero256, remSrcSize, "rem_src_bytes");
memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
break;
}
auto tooLittleDataBytes = builder.CreateICmpULT(remSrcBytes, reqBytes);
auto bytesToCopy = builder.CreateSelect(tooLittleDataBytes, remSrcBytes, reqBytes, "bytes_to_copy");
case Instruction::EXTCODECOPY:
{
auto extAddr = stack.pop();
auto destMemIdx = stack.pop();
auto srcIdx = stack.pop();
auto reqBytes = stack.pop();
builder.CreateMemCpy(destPtr, srcPtr, bytesToCopy, 0);
auto srcPtr = ext.codeAt(extAddr);
auto srcSize = ext.codesizeAt(extAddr);
memory.copyBytes(srcPtr, srcSize, srcIdx, destMemIdx, reqBytes);
break;
}

42
evmcc/Ext.cpp

@ -14,6 +14,7 @@ using llvm::types::i;
using Linkage = llvm::GlobalValue::LinkageTypes;
using dev::h256;
using dev::u256;
namespace evmcc
{
@ -94,6 +95,8 @@ Ext::Ext(llvm::IRBuilder<>& _builder, llvm::Module* 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_codesizeAt = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_codesizeAt", module);
m_builder.CreateCall(m_init, m_data);
}
@ -209,9 +212,32 @@ llvm::Value* Ext::exp(llvm::Value* _left, llvm::Value* _right)
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]);
llvm::Value* args[] = {m_args[0], m_args[1]};
m_builder.CreateCall(m_codeAt, args);
return m_builder.CreateLoad(m_args[1]);
}
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"
{
using namespace evmcc;
EXPORT void ext_init(ExtData* _extData)
{
auto&& ext = Runtime::getExt();
@ -307,7 +333,7 @@ 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
auto ret = ext.call(receiveAddress, value, inRef, &gas, outRef, onOp, {}, receiveAddress);
ret = ext.call(receiveAddress, value, inRef, &gas, outRef, onOp, {}, receiveAddress);
}
*_gas = eth2llvm(gas);
@ -331,6 +357,20 @@ EXPORT void ext_exp(i256* _left, i256* _right, i256* _ret)
*_ret = eth2llvm(ret);
}
EXPORT void ext_codeAt(h256* _addr256, i256* _ret)
{
auto&& ext = Runtime::getExt();
auto addr = dev::right160(*_addr256);
auto& code = ext.codeAt(addr);
*_ret = *reinterpret_cast<i256*>(const_cast<unsigned char*>(&code[0]));
}
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()));
}
}

4
evmcc/Ext.h

@ -43,6 +43,8 @@ public:
llvm::Value* sha3(llvm::Value* _inOff, llvm::Value* _inSize);
llvm::Value* exp(llvm::Value* _left, llvm::Value* _right);
llvm::Value* codeAt(llvm::Value* _addr);
llvm::Value* codesizeAt(llvm::Value* _addr);
private:
llvm::Value* getDataElem(unsigned _index, const llvm::Twine& _name = "");
@ -71,6 +73,8 @@ private:
llvm::Function* m_bswap;
llvm::Function* m_sha3;
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);
}
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)
{
if (getenv("EVMCC_DEBUG_MEMORY") == nullptr)

2
evmcc/Memory.h

@ -20,6 +20,8 @@ public:
void storeByte(llvm::Value* _addr, llvm::Value* _byte);
llvm::Value* getData();
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.
void require(llvm::Value* _size);

Loading…
Cancel
Save