Browse Source

CREATE instruction. Still needs some work with ExtVM interface.

[#79510898]
cl-refactor
Paweł Bylica 10 years ago
parent
commit
3942b2ed28
  1. 11
      evmcc/Compiler.cpp
  2. 39
      evmcc/Ext.cpp
  3. 4
      evmcc/Ext.h
  4. 2
      evmcc/bytecode/ext_test.evm
  5. 7
      evmcc/lll/ext_test.lll

11
evmcc/Compiler.cpp

@ -713,6 +713,17 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
break;
}
case Instruction::CREATE:
{
auto endowment = stack.pop();
auto initOff = stack.pop();
auto initSize = stack.pop();
auto address = ext.create(endowment, initOff, initSize);
stack.push(address);
break;
}
case Instruction::RETURN:
{
auto index = stack.pop();

39
evmcc/Ext.cpp

@ -17,6 +17,7 @@ using namespace llvm;
using llvm::types::i;
using Linkage = llvm::GlobalValue::LinkageTypes;
using dev::h256;
using dev::u256;
namespace evmcc
{
@ -52,6 +53,8 @@ Ext::Ext(llvm::IRBuilder<>& _builder, llvm::Module* module)
auto i256Ty = m_builder.getIntNTy(256);
m_args[0] = m_builder.CreateAlloca(i256Ty, nullptr, "ext.index");
m_args[1] = m_builder.CreateAlloca(i256Ty, nullptr, "ext.value");
m_arg2 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg2");
m_arg3 = m_builder.CreateAlloca(i256Ty, nullptr, "ext.arg3");
Type* elements[] = {
i256Ty, // i256 address;
@ -77,8 +80,10 @@ Ext::Ext(llvm::IRBuilder<>& _builder, llvm::Module* module)
m_setStore = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_setStore", module);
m_calldataload = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_calldataload", module);
m_balance = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_balance", module);
m_create = Function::Create(TypeBuilder<void(i<256>*, i<256>*, i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_create", module);
m_bswap = Intrinsic::getDeclaration(module, Intrinsic::bswap, i256Ty);
m_builder.CreateCall(m_init, m_data);
}
@ -129,12 +134,24 @@ Value* Ext::bswap(Value* _value)
Value* Ext::balance(Value* _address)
{
auto address = bswap(_address); // to BigEndian // TODO: I don't get it. It seems not needed
auto address = bswap(_address); // to BE
m_builder.CreateStore(address, m_args[0]);
m_builder.CreateCall(m_balance, m_args);
return m_builder.CreateLoad(m_args[1]);
}
Value* Ext::create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize)
{
m_builder.CreateStore(_endowment, m_args[0]);
m_builder.CreateStore(_initOff, m_arg2);
m_builder.CreateStore(_initSize, m_arg3);
Value* args[] = {m_args[0], m_arg2, m_arg3, m_args[1]};
m_builder.CreateCall(m_create, args);
Value* address = m_builder.CreateLoad(m_args[1]);
address = bswap(address); // to LE
return address;
}
extern "C"
{
@ -186,6 +203,26 @@ EXPORT void ext_balance(h256* _address, i256* _value)
*_value = eth2llvm(u);
}
EXPORT void ext_create(i256* _endowment, i256* _initOff, i256* _initSize, h256* _address)
{
auto&& ext = Runtime::getExt();
auto endowment = llvm2eth(*_endowment);
auto initOff = static_cast<size_t>(llvm2eth(*_initOff));
auto initSize = static_cast<size_t>(llvm2eth(*_initSize));
if (ext.balance(ext.myAddress) >= endowment)
{
ext.subBalance(endowment);
u256 gas; // TODO: Handle gas
auto&& initRef = dev::bytesConstRef(Runtime::getMemory().data() + initOff, initSize);
auto&& onOp = dev::bytesConstRef(); // TODO: Handle that thing
h256 address = ext.create(endowment, &gas, initRef, onOp);
*_address = address;
}
else
*_address = {};
}
}
}

4
evmcc/Ext.h

@ -34,6 +34,7 @@ public:
llvm::Value* balance(llvm::Value* _address);
llvm::Value* calldataload(llvm::Value* _index);
llvm::Value* create(llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize);
private:
llvm::Value* getDataElem(unsigned _index, const llvm::Twine& _name = "");
@ -44,12 +45,15 @@ private:
llvm::IRBuilder<>& m_builder;
llvm::Value* m_args[2];
llvm::Value* m_arg2;
llvm::Value* m_arg3;
llvm::Value* m_data;
llvm::Function* m_init;
llvm::Function* m_store;
llvm::Function* m_setStore;
llvm::Function* m_calldataload;
llvm::Function* m_balance;
llvm::Function* m_create;
llvm::Function* m_bswap;
};

2
evmcc/bytecode/ext_test.evm

@ -1 +1 @@
5a3031333234363a4041424344455a36600035602635601335380060016002f2
5a3031333234363a4041424344455a36600035602635601335387f1111222233334444555566667777888899990000aaaabbbbccccddddeeeeffff600054602060006000f00060016002f2

7
evmcc/lll/ext_test.lll

@ -23,6 +23,13 @@ CALLDATALOAD
19
CALLDATALOAD
CODESIZE
0x1111222233334444555566667777888899990000aaaabbbbccccddddeeeeffff
0
MSTORE
32
0
0
CREATE
STOP
1
2

Loading…
Cancel
Save