Browse Source

Moving Compiler to separated files

cl-refactor
Paweł Bylica 11 years ago
parent
commit
f378909442
  1. 75
      evmcc/Compiler.cpp
  2. 34
      evmcc/Compiler.h
  3. 96
      evmcc/evmcc.cpp
  4. 4
      windows/evmcc.vcxproj
  5. 4
      windows/evmcc.vcxproj.filters

75
evmcc/Compiler.cpp

@ -0,0 +1,75 @@
#include "Compiler.h"
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
namespace evmcc
{
Compiler::Compiler()
{
auto& context = llvm::getGlobalContext();
Types.word8 = llvm::Type::getInt8Ty(context);
Types.word8ptr = llvm::Type::getInt8PtrTy(context);
Types.word256 = llvm::Type::getIntNTy(context, 256);
Types.word256ptr = Types.word256->getPointerTo();
Types.word256arr = llvm::ArrayType::get(Types.word256, 100);
Types.size = llvm::Type::getInt64Ty(context);
}
void Compiler::compile(const dev::bytes& bytecode)
{
using namespace llvm;
auto& context = getGlobalContext();
Module* module = new 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 stack = new GlobalVariable(*module, Types.word256arr, false,
GlobalValue::LinkageTypes::PrivateLinkage,
ConstantAggregateZero::get(Types.word256arr), "stack");
auto stackTop = new GlobalVariable(*module, Types.size, false,
GlobalValue::LinkageTypes::PrivateLinkage,
ConstantInt::get(Types.size, 0), "stackTop");
// Create value for void* malloc(size_t)
std::vector<Type*> mallocArgTypes = { Types.size };
Value* mallocVal = Function::Create(FunctionType::get(Types.word8ptr, mallocArgTypes, false),
GlobalValue::LinkageTypes::ExternalLinkage, "malloc", module);
// Create main function
FunctionType* funcType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
Function* mainFunc = Function::Create(funcType, Function::ExternalLinkage, "main", module);
BasicBlock* 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);
/*
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), 0));
module->dump();
}
}

34
evmcc/Compiler.h

@ -0,0 +1,34 @@
#pragma once
#include <llvm/IR/Type.h>
#include <libdevcore/Common.h>
namespace evmcc
{
class Compiler
{
private:
struct
{
llvm::Type* word8;
llvm::Type* word8ptr;
llvm::Type* word256;
llvm::Type* word256ptr;
llvm::Type* word256arr;
llvm::Type* size;
} Types;
public:
Compiler();
void compile(const dev::bytes& bytecode);
};
}

96
evmcc/evmcc.cpp

@ -1,101 +1,19 @@
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <libdevcore/Common.h>
#include <libdevcore/CommonIO.h>
#include <libevmface/Instruction.h>
#include <boost/algorithm/string.hpp>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
using namespace dev; #include <boost/algorithm/string.hpp>
class EVMCCompiler
{
private:
struct
{
llvm::Type* word8;
llvm::Type* word8ptr;
llvm::Type* word256;
llvm::Type* word256ptr;
llvm::Type* word256arr;
llvm::Type* size;
} Types;
public: #include <libdevcore/Common.h>
#include <libdevcore/CommonIO.h>
#include <libevmface/Instruction.h>
EVMCCompiler() #include "Compiler.h"
{
auto& context = llvm::getGlobalContext();
Types.word8 = llvm::Type::getInt8Ty(context);
Types.word8ptr = llvm::Type::getInt8PtrTy(context);
Types.word256 = llvm::Type::getIntNTy(context, 256);
Types.word256ptr = Types.word256->getPointerTo();
Types.word256arr = llvm::ArrayType::get(Types.word256, 100);
Types.size = llvm::Type::getInt64Ty(context);
}
void compile(const bytes& bytecode) using namespace dev;
{
using namespace llvm;
auto& context = getGlobalContext();
Module* module = new 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 stack = new GlobalVariable(*module, Types.word256arr, false,
GlobalValue::LinkageTypes::PrivateLinkage,
ConstantAggregateZero::get(Types.word256arr), "stack");
auto stackTop = new GlobalVariable(*module, Types.size, false,
GlobalValue::LinkageTypes::PrivateLinkage,
ConstantInt::get(Types.size, 0), "stackTop");
// Create value for void* malloc(size_t)
std::vector<Type*> mallocArgTypes = { Types.size };
Value* mallocVal = Function::Create(FunctionType::get(Types.word8ptr, mallocArgTypes, false),
GlobalValue::LinkageTypes::ExternalLinkage, "malloc", module);
// Create main function
FunctionType* funcType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
Function* mainFunc = Function::Create(funcType, Function::ExternalLinkage, "main", module);
BasicBlock* 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);
/*
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), 0));
module->dump();
}
};
void show_usage() void show_usage()
{ {
@ -173,7 +91,7 @@ int main(int argc, char** argv)
if (opt_compile) if (opt_compile)
{ {
EVMCCompiler().compile(bytecode); evmcc::Compiler().compile(bytecode);
} }
return 0; return 0;

4
windows/evmcc.vcxproj

@ -19,8 +19,12 @@
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\evmcc\Compiler.cpp" />
<ClCompile Include="..\evmcc\evmcc.cpp" /> <ClCompile Include="..\evmcc\evmcc.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClInclude Include="..\evmcc\Compiler.h" />
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{D51A4898-BD9E-4961-BCAE-1FE7F854BB4D}</ProjectGuid> <ProjectGuid>{D51A4898-BD9E-4961-BCAE-1FE7F854BB4D}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>

4
windows/evmcc.vcxproj.filters

@ -2,5 +2,9 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <ItemGroup>
<ClCompile Include="..\evmcc\evmcc.cpp" /> <ClCompile Include="..\evmcc\evmcc.cpp" />
<ClCompile Include="..\evmcc\Compiler.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\evmcc\Compiler.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>
Loading…
Cancel
Save