Browse Source

PUSH instruction implementation

cl-refactor
Paweł Bylica 10 years ago
parent
commit
4266ce54fc
  1. 123
      evmcc/Compiler.cpp
  2. 1
      evmcc/bytecode/push_test.evm
  3. 35
      evmcc/lll/push_test.lll

123
evmcc/Compiler.cpp

@ -3,6 +3,8 @@
#include <llvm/IR/IRBuilder.h>
#include <libevmface/Instruction.h>
#include "Stack.h"
namespace evmcc
@ -37,64 +39,87 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
using namespace llvm;
auto& context = getGlobalContext();
auto module = std::make_unique<Module>("main", 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
FunctionType* funcType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
Function* mainFunc = Function::Create(funcType, Function::ExternalLinkage, "main", module.get());
auto mainFuncType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
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);
// Initialize memory with call to malloc, update memsize
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);
// Init stack
auto stack = Stack(builder, module.get());
uint64_t words[] = { 1, 2, 3, 4 };
auto val = llvm::APInt(256, 4, words);
auto c = ConstantInt::get(Types.word256, val);
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));
for (auto pc = bytecode.cbegin(); pc != bytecode.cend(); ++pc)
{
using dev::eth::Instruction;
auto inst = static_cast<Instruction>(*pc);
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;
}
}
}
//uint64_t words[] = { 1, 2, 3, 4 };
//auto val = llvm::APInt(256, 4, words);
//auto c = ConstantInt::get(Types.word256, val);
//stack.push(c);
//stack.push(ConstantInt::get(Types.word256, 0x1122334455667788));
//auto top = stack.top();
//stack.push(top); // dup
//stack.pop();
builder.CreateRet(ConstantInt::get(Type::getInt32Ty(context), 0));
return module;
}

1
evmcc/bytecode/push_test.evm

@ -0,0 +1 @@
60656107d26204a0c763026921f4640bc5588eb165372d0f1dca6e661ba1d901961c71670c55f7bc23038e3868056bc75e2d630fffff69021e19e0c9bab24000016a085d1c6e8050f0ea1c71bd6b0688be36543f3c36e638e37a6c03d41f73d55d0d482ae55555376dc76810d0fe03c91964d31c71c6f46e615dd0360c07d931663b14e38e38b16f2da3f99955a3adcf27ebb1caaaaaaa6e7014ccba6a8bb1ed35bd86bf065c71c71c2b7109491c5d4781b79c9009de6bfb8e38e38de8720414a0f6fdec81304d4c563e740bffffffffa573118427b3b4a05bc8a8a4de8459868000000000017406eb15e7331e727940d4ac54b7cdca1c71c71c71bd750567a91c9fefc96ebaa626a22f98c5e638e38e38e37a76032abd16c5b68006e15d5aa307e383f4e55555555555377701a6427bdc4f0d58eab5f48a3ec67f64e21c71c71c71c6f478080dd0a0c9b9ff2c2a0c740b06853a0a980ee38e38e38e38b17903c679cb5e8f2f9cb3b5d6652b0e7334f746faaaaaaaaaaaaa6e7a01b873815917ebb2bf3b890a1af495d6235bae3c71c71c71c71c2b7b07ae4cca96e1a55dfa49c85ad3c3e60e426b92fb8e38e38e38e38de87c036018bf074e292bcc7d6c8bea0f9699443046178bffffffffffffffa57d0e7d34c64a9c85d4460dbbca87196b61618a4bd2168000000000000000017e05b901f48a5b994d6572502bc4ea43140486666416aa1c71c71c71c71c71bd7f047889870c178fc477414ea231d70467a388fffe31b4e638e38e38e38e38e37a

35
evmcc/lll/push_test.lll

@ -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…
Cancel
Save