mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 19:30:44 +00:00
Push call data on stack - CALLDATALOAD
This commit is contained in:
parent
fcde2f3d22
commit
b9cda13a9a
7 changed files with 38 additions and 2 deletions
|
|
@ -298,6 +298,14 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
|||
break;
|
||||
}
|
||||
|
||||
case Instruction::CALLDATALOAD:
|
||||
{
|
||||
auto index = stack.pop();
|
||||
auto value = ext.calldataload(index);
|
||||
stack.push(value);
|
||||
break;
|
||||
}
|
||||
|
||||
case Instruction::GASPRICE:
|
||||
{
|
||||
auto value = ext.gasprice();
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module)
|
|||
ext->origin = dev::Address(101010101010101010);
|
||||
ext->value = 0xabcd;
|
||||
ext->gasPrice = 1002;
|
||||
std::string calldata = "Hello World!";
|
||||
std::string calldata = "Hello the Beautiful World of Ethereum!";
|
||||
ext->data = calldata;
|
||||
Ext::init(std::move(ext));
|
||||
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ Ext::Ext(llvm::IRBuilder<>& _builder)
|
|||
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_calldataload = Function::Create(TypeBuilder<void(i<256>*, i<256>*), true>::get(ctx), Linkage::ExternalLinkage, "ext_calldataload", module);
|
||||
|
||||
m_builder.CreateCall(m_init, m_data);
|
||||
}
|
||||
|
|
@ -100,6 +101,13 @@ Value* Ext::callvalue() { return getDataElem(3, "callvalue"); }
|
|||
Value* Ext::calldatasize() { return getDataElem(5, "calldatasize"); }
|
||||
Value* Ext::gasprice() { return getDataElem(4, "gasprice"); }
|
||||
|
||||
Value* Ext::calldataload(Value* _index)
|
||||
{
|
||||
m_builder.CreateStore(_index, m_args[0]);
|
||||
m_builder.CreateCall(m_calldataload, m_args);
|
||||
return m_builder.CreateLoad(m_args[1]);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
|
|
@ -128,6 +136,16 @@ EXPORT void ext_setStore(i256* _index, i256* _value)
|
|||
g_ext->setStore(index, value);
|
||||
}
|
||||
|
||||
EXPORT void ext_calldataload(i256* _index, i256* _value)
|
||||
{
|
||||
auto index = static_cast<size_t>(llvm2eth(*_index));
|
||||
assert(index + 31 > index); // TODO: Handle large index
|
||||
auto b = reinterpret_cast<byte*>(_value);
|
||||
for (size_t i = index, j = 31; i <= index + 31; ++i, --j)
|
||||
b[j] = i < g_ext->data.size() ? g_ext->data[i] : 0;
|
||||
// TODO: It all can be done by adding padding to data or by using min() algorithm without branch
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -26,6 +26,8 @@ public:
|
|||
llvm::Value* calldatasize();
|
||||
llvm::Value* gasprice();
|
||||
|
||||
llvm::Value* calldataload(llvm::Value* _index);
|
||||
|
||||
private:
|
||||
llvm::Value* getDataElem(unsigned _index, const llvm::Twine& _name = "");
|
||||
|
||||
|
|
@ -37,6 +39,7 @@ private:
|
|||
llvm::Function* m_init;
|
||||
llvm::Function* m_store;
|
||||
llvm::Function* m_setStore;
|
||||
llvm::Function* m_calldataload;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ namespace evmcc
|
|||
{
|
||||
|
||||
/// Representation of 256-bit value binary compatible with LLVM i256
|
||||
// TODO: Replace with h256
|
||||
struct i256
|
||||
{
|
||||
uint64_t a;
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
30333234363a
|
||||
30333234363a600035602635601335
|
||||
|
|
|
|||
|
|
@ -6,4 +6,10 @@ ORIGIN
|
|||
CALLVALUE
|
||||
CALLDATASIZE
|
||||
GASPRICE
|
||||
0
|
||||
CALLDATALOAD
|
||||
38
|
||||
CALLDATALOAD
|
||||
19
|
||||
CALLDATALOAD
|
||||
)
|
||||
Loading…
Reference in a new issue