mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 19:30:44 +00:00
PUSH instruction implementation
This commit is contained in:
parent
d8430db13c
commit
4266ce54fc
3 changed files with 104 additions and 43 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include <llvm/IR/IRBuilder.h>
|
#include <llvm/IR/IRBuilder.h>
|
||||||
|
|
||||||
|
#include <libevmface/Instruction.h>
|
||||||
|
|
||||||
#include "Stack.h"
|
#include "Stack.h"
|
||||||
|
|
||||||
namespace evmcc
|
namespace evmcc
|
||||||
|
|
@ -37,64 +39,87 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
auto& context = getGlobalContext();
|
auto& context = getGlobalContext();
|
||||||
|
|
||||||
auto module = std::make_unique<Module>("main", context);
|
auto module = std::make_unique<Module>("main", context);
|
||||||
IRBuilder<> builder(context);
|
IRBuilder<> builder(context);
|
||||||
|
|
||||||
// Create globals for memory, memory size, stack and stack top
|
|
||||||
auto memory = new GlobalVariable(*module, Types.word8ptr, false,
|
|
||||||
GlobalValue::LinkageTypes::PrivateLinkage,
|
|
||||||
Constant::getNullValue(Types.word8ptr), "memory");
|
|
||||||
auto memSize = new GlobalVariable(*module, Types.size, false,
|
|
||||||
GlobalValue::LinkageTypes::PrivateLinkage,
|
|
||||||
ConstantInt::get(Types.size, 0), "memsize");
|
|
||||||
auto stack2 = new GlobalVariable(*module, Types.word256arr, false,
|
|
||||||
GlobalValue::LinkageTypes::PrivateLinkage,
|
|
||||||
ConstantAggregateZero::get(Types.word256arr), "stack");
|
|
||||||
auto stackTop2 = new GlobalVariable(*module, Types.size, false,
|
|
||||||
GlobalValue::LinkageTypes::PrivateLinkage,
|
|
||||||
ConstantInt::get(Types.size, 0), "stackTop");
|
|
||||||
|
|
||||||
// Create value for void* malloc(size_t)
|
|
||||||
auto mallocVal = Function::Create(FunctionType::get(Types.word8ptr, { Types.size }, false),
|
|
||||||
GlobalValue::LinkageTypes::ExternalLinkage, "malloc", module.get());
|
|
||||||
|
|
||||||
// Create main function
|
// Create main function
|
||||||
FunctionType* funcType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
|
auto mainFuncType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
|
||||||
Function* mainFunc = Function::Create(funcType, Function::ExternalLinkage, "main", module.get());
|
auto mainFunc = Function::Create(mainFuncType, Function::ExternalLinkage, "main", module.get());
|
||||||
|
|
||||||
BasicBlock* entryBlock = BasicBlock::Create(context, "entry", mainFunc);
|
auto entryBlock = BasicBlock::Create(context, "entry", mainFunc);
|
||||||
builder.SetInsertPoint(entryBlock);
|
builder.SetInsertPoint(entryBlock);
|
||||||
|
|
||||||
// Initialize memory with call to malloc, update memsize
|
// Init stack
|
||||||
std::vector<Value*> mallocMemArgs = { ConstantInt::get(Types.size, 100) };
|
|
||||||
auto mallocMemCall = builder.CreateCall(mallocVal, mallocMemArgs, "malloc_mem");
|
|
||||||
builder.CreateStore(mallocMemCall, memory);
|
|
||||||
builder.CreateStore(ConstantInt::get(Types.size, 100), memSize);
|
|
||||||
|
|
||||||
auto stack = Stack(builder, module.get());
|
auto stack = Stack(builder, module.get());
|
||||||
|
|
||||||
uint64_t words[] = { 1, 2, 3, 4 };
|
for (auto pc = bytecode.cbegin(); pc != bytecode.cend(); ++pc)
|
||||||
auto val = llvm::APInt(256, 4, words);
|
{
|
||||||
auto c = ConstantInt::get(Types.word256, val);
|
using dev::eth::Instruction;
|
||||||
|
|
||||||
stack.push(c);
|
auto inst = static_cast<Instruction>(*pc);
|
||||||
stack.push(ConstantInt::get(Types.word256, 0x1122334455667788));
|
switch (inst)
|
||||||
|
{
|
||||||
|
case Instruction::PUSH1:
|
||||||
|
case Instruction::PUSH2:
|
||||||
|
case Instruction::PUSH3:
|
||||||
|
case Instruction::PUSH4:
|
||||||
|
case Instruction::PUSH5:
|
||||||
|
case Instruction::PUSH6:
|
||||||
|
case Instruction::PUSH7:
|
||||||
|
case Instruction::PUSH8:
|
||||||
|
case Instruction::PUSH9:
|
||||||
|
case Instruction::PUSH10:
|
||||||
|
case Instruction::PUSH11:
|
||||||
|
case Instruction::PUSH12:
|
||||||
|
case Instruction::PUSH13:
|
||||||
|
case Instruction::PUSH14:
|
||||||
|
case Instruction::PUSH15:
|
||||||
|
case Instruction::PUSH16:
|
||||||
|
case Instruction::PUSH17:
|
||||||
|
case Instruction::PUSH18:
|
||||||
|
case Instruction::PUSH19:
|
||||||
|
case Instruction::PUSH20:
|
||||||
|
case Instruction::PUSH21:
|
||||||
|
case Instruction::PUSH22:
|
||||||
|
case Instruction::PUSH23:
|
||||||
|
case Instruction::PUSH24:
|
||||||
|
case Instruction::PUSH25:
|
||||||
|
case Instruction::PUSH26:
|
||||||
|
case Instruction::PUSH27:
|
||||||
|
case Instruction::PUSH28:
|
||||||
|
case Instruction::PUSH29:
|
||||||
|
case Instruction::PUSH30:
|
||||||
|
case Instruction::PUSH31:
|
||||||
|
case Instruction::PUSH32:
|
||||||
|
{
|
||||||
|
auto numBytes = static_cast<size_t>(inst) - static_cast<size_t>(Instruction::PUSH1) + 1;
|
||||||
|
auto value = llvm::APInt(256, 0);
|
||||||
|
for (decltype(numBytes) i = 0; i < numBytes; ++i) // TODO: Use pc as iterator
|
||||||
|
{
|
||||||
|
++pc;
|
||||||
|
value <<= 8;
|
||||||
|
value |= *pc;
|
||||||
|
}
|
||||||
|
auto c = builder.getInt(value);
|
||||||
|
stack.push(c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto top = stack.top();
|
//uint64_t words[] = { 1, 2, 3, 4 };
|
||||||
stack.push(top); // dup
|
//auto val = llvm::APInt(256, 4, words);
|
||||||
|
//auto c = ConstantInt::get(Types.word256, val);
|
||||||
|
|
||||||
stack.pop();
|
//stack.push(c);
|
||||||
|
//stack.push(ConstantInt::get(Types.word256, 0x1122334455667788));
|
||||||
|
|
||||||
|
//auto top = stack.top();
|
||||||
|
//stack.push(top); // dup
|
||||||
|
|
||||||
/*
|
//stack.pop();
|
||||||
std::vector<Value*> mallocStackArgs = { ConstantInt::get(sizeTy, 200) };
|
|
||||||
auto mallocStackCall = builder.CreateCall(mallocVal, mallocStackArgs, "malloc_stack");
|
|
||||||
auto mallocCast = builder.CreatePointerBitCastOrAddrSpaceCast(mallocStackCall, int256ptr);
|
|
||||||
builder.CreateStore(mallocCast, stackVal);
|
|
||||||
*/
|
|
||||||
|
|
||||||
builder.CreateRet(ConstantInt::get(Type::getInt32Ty(context), 13));
|
builder.CreateRet(ConstantInt::get(Type::getInt32Ty(context), 0));
|
||||||
|
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1
evmcc/bytecode/push_test.evm
Normal file
1
evmcc/bytecode/push_test.evm
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
60656107d26204a0c763026921f4640bc5588eb165372d0f1dca6e661ba1d901961c71670c55f7bc23038e3868056bc75e2d630fffff69021e19e0c9bab24000016a085d1c6e8050f0ea1c71bd6b0688be36543f3c36e638e37a6c03d41f73d55d0d482ae55555376dc76810d0fe03c91964d31c71c6f46e615dd0360c07d931663b14e38e38b16f2da3f99955a3adcf27ebb1caaaaaaa6e7014ccba6a8bb1ed35bd86bf065c71c71c2b7109491c5d4781b79c9009de6bfb8e38e38de8720414a0f6fdec81304d4c563e740bffffffffa573118427b3b4a05bc8a8a4de8459868000000000017406eb15e7331e727940d4ac54b7cdca1c71c71c71bd750567a91c9fefc96ebaa626a22f98c5e638e38e38e37a76032abd16c5b68006e15d5aa307e383f4e55555555555377701a6427bdc4f0d58eab5f48a3ec67f64e21c71c71c71c6f478080dd0a0c9b9ff2c2a0c740b06853a0a980ee38e38e38e38b17903c679cb5e8f2f9cb3b5d6652b0e7334f746faaaaaaaaaaaaa6e7a01b873815917ebb2bf3b890a1af495d6235bae3c71c71c71c71c2b7b07ae4cca96e1a55dfa49c85ad3c3e60e426b92fb8e38e38e38e38de87c036018bf074e292bcc7d6c8bea0f9699443046178bffffffffffffffa57d0e7d34c64a9c85d4460dbbca87196b61618a4bd2168000000000000000017e05b901f48a5b994d6572502bc4ea43140486666416aa1c71c71c71c71c71bd7f047889870c178fc477414ea231d70467a388fffe31b4e638e38e38e38e38e37a
|
||||||
35
evmcc/lll/push_test.lll
Normal file
35
evmcc/lll/push_test.lll
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
|
||||||
|
(asm
|
||||||
|
101 ;; PUSH1
|
||||||
|
2002 ;; PUSH2
|
||||||
|
303303 ;; PUSH3
|
||||||
|
40444404 ;; PUSH4
|
||||||
|
50555555505 ;; PUSH5
|
||||||
|
60666666666606
|
||||||
|
7777777777777777
|
||||||
|
888888888888888888
|
||||||
|
99999999999999999999
|
||||||
|
10000000000000000000001
|
||||||
|
10111111111111111111111101
|
||||||
|
2022222222222222222222222202
|
||||||
|
303333333333333333333333333303
|
||||||
|
4044444444444444444444444444444404
|
||||||
|
505555555555555555555555555555555505
|
||||||
|
60666666666666666666666666666666666606
|
||||||
|
7077777777777777777777777777777777777707
|
||||||
|
808888888888888888888888888888888888888808
|
||||||
|
90999999999999999999999999999999999999999909
|
||||||
|
100000000000000000000000000000000000000000000001
|
||||||
|
10111111111111111111111111111111111111111111111101
|
||||||
|
2022222222222222222222222222222222222222222222222202
|
||||||
|
303333333333333333333333333333333333333333333333333303
|
||||||
|
40444444444444444444444444444444444444444444444444444404
|
||||||
|
50555555555555555555555555555555555555555555555555555555505
|
||||||
|
6066666666666666666666666666666666666666666666666666666666606
|
||||||
|
707777777777777777777777777777777777777777777777777777777777707
|
||||||
|
808888888888888888888888888888888888888888888888888888888888888808
|
||||||
|
90999999999999999999999999999999999999999999999999999999999999999909
|
||||||
|
100000000000000000000000000000000000000000000000000000000000000000000001
|
||||||
|
10111111111111111111111111111111111111111111111111111111111111111111111101
|
||||||
|
2022222222222222222222222222222222222222222222222222222222222222222222222202 ;; PUSH32
|
||||||
|
)
|
||||||
Loading…
Reference in a new issue