Browse Source

Send Ext static data to running contract. ADDRESS instruction

cl-refactor
Paweł Bylica 10 years ago
parent
commit
ac795c481b
  1. 7
      evmcc/Compiler.cpp
  2. 1
      evmcc/ExecutionEngine.cpp
  3. 59
      evmcc/Ext.cpp
  4. 4
      evmcc/Ext.h
  5. 1
      evmcc/bytecode/ext_test.evm
  6. 4
      evmcc/lll/ext_test.lll

7
evmcc/Compiler.cpp

@ -263,6 +263,13 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
break;
}
case Instruction::ADDRESS:
{
auto value = ext.address();
stack.push(value);
break;
}
}
}

1
evmcc/ExecutionEngine.cpp

@ -69,6 +69,7 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module)
exec->finalizeObject();
auto ext = std::make_unique<dev::eth::ExtVMFace>();
ext->myAddress = dev::Address(1122334455667788);
Ext::init(std::move(ext));
auto entryFunc = module->getFunction("main");

59
evmcc/Ext.cpp

@ -19,6 +19,12 @@ using Linkage = llvm::GlobalValue::LinkageTypes;
namespace evmcc
{
// TODO: Copy of dev::eth::fromAddress in VM.h
inline dev::u256 fromAddress(dev::Address _a)
{
return (dev::u160)_a;
}
std::unique_ptr<dev::eth::ExtVMFace> g_ext;
void Ext::init(std::unique_ptr<dev::eth::ExtVMFace> _ext)
@ -26,17 +32,45 @@ void Ext::init(std::unique_ptr<dev::eth::ExtVMFace> _ext)
g_ext = std::move(_ext);
}
struct ExtData
{
i256 address;
i256 caller;
i256 origin;
i256 callvalue;
i256 gasprice;
i256 calldatasize;
const byte* calldata;
};
Ext::Ext(llvm::IRBuilder<>& _builder)
: m_builder(_builder)
{
auto module = m_builder.GetInsertBlock()->getParent()->getParent();
auto&& ctx = _builder.getContext();
m_args[0] = m_builder.CreateAlloca(m_builder.getIntNTy(256), nullptr, "ext.index");
m_args[1] = m_builder.CreateAlloca(m_builder.getIntNTy(256), nullptr, "ext.value");
m_store = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_store", 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");
Type* elements[] = {
i256Ty,
i256Ty,
i256Ty,
i256Ty,
i256Ty,
i256Ty,
m_builder.getInt8PtrTy()
};
auto extDataTy = StructType::create(elements, "ext.Data");
m_data = m_builder.CreateAlloca(extDataTy, nullptr, "ext.data");
m_init = Function::Create(FunctionType::get(m_builder.getVoidTy(), extDataTy->getPointerTo(), false), Linkage::ExternalLinkage, "ext_init", module);
m_store = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_store", module);
m_setStore = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_setStore", module);
m_builder.CreateCall(m_init, m_data);
}
llvm::Value* Ext::store(llvm::Value* _index)
@ -53,9 +87,26 @@ void Ext::setStore(llvm::Value* _index, llvm::Value* _value)
m_builder.CreateCall(m_setStore, m_args);
}
Value* Ext::address()
{
auto valuePtr = m_builder.CreateStructGEP(m_data, 0);
return m_builder.CreateLoad(valuePtr);
}
extern "C"
{
EXPORT void ext_init(ExtData* _extData)
{
_extData->address = eth2llvm(fromAddress(g_ext->myAddress));
_extData->caller = eth2llvm(fromAddress(g_ext->caller));
_extData->origin = eth2llvm(fromAddress(g_ext->origin));
_extData->callvalue = eth2llvm(g_ext->value);
_extData->gasprice = eth2llvm(g_ext->gasPrice);
_extData->calldatasize = eth2llvm(g_ext->data.size());
_extData->calldata = g_ext->data.data();
}
EXPORT void ext_store(i256* _index, i256* _value)
{
auto index = llvm2eth(*_index);

4
evmcc/Ext.h

@ -19,10 +19,14 @@ public:
llvm::Value* store(llvm::Value* _index);
void setStore(llvm::Value* _index, llvm::Value* _value);
llvm::Value* address();
private:
llvm::IRBuilder<>& m_builder;
llvm::Value* m_args[2];
llvm::Value* m_data;
llvm::Function* m_init;
llvm::Function* m_store;
llvm::Function* m_setStore;
};

1
evmcc/bytecode/ext_test.evm

@ -0,0 +1 @@
30

4
evmcc/lll/ext_test.lll

@ -0,0 +1,4 @@
(asm
ADDRESS
)
Loading…
Cancel
Save