Browse Source

Cleanup LLVM types usage

cl-refactor
Paweł Bylica 10 years ago
parent
commit
b7f31afb7a
  1. 7
      evmcc/BasicBlock.cpp
  2. 36
      evmcc/Compiler.cpp
  3. 4
      evmcc/GasMeter.cpp
  4. 16
      evmcc/Type.cpp
  5. 16
      evmcc/Type.h

7
evmcc/BasicBlock.cpp

@ -4,6 +4,8 @@
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include "Type.h"
namespace evmcc
{
@ -27,10 +29,9 @@ llvm::Value* BasicBlock::Stack::pop()
if (m_backend.empty())
{
// Create PHI node
auto i256Ty = llvm::Type::getIntNTy(m_llvmBB->getContext(), 256);
if (m_llvmBB->empty())
return llvm::PHINode::Create(i256Ty, 0, {}, m_llvmBB);
return llvm::PHINode::Create(i256Ty, 0, {}, m_llvmBB->getFirstNonPHI());
return llvm::PHINode::Create(Type::i256, 0, {}, m_llvmBB);
return llvm::PHINode::Create(Type::i256, 0, {}, m_llvmBB->getFirstNonPHI());
}
auto top = m_backend.back();

36
evmcc/Compiler.cpp

@ -6,6 +6,7 @@
#include <libevmface/Instruction.h>
#include "Type.h"
#include "Memory.h"
#include "Ext.h"
#include "GasMeter.h"
@ -20,9 +21,6 @@ struct
{
llvm::Type* word8;
llvm::Type* word8ptr;
llvm::Type* word256;
llvm::Type* word256ptr;
llvm::Type* word256arr;
llvm::Type* size;
llvm::Type* Void;
llvm::Type* WordLowPrecision;
@ -30,12 +28,10 @@ struct
Compiler::Compiler()
{
Type::init(llvm::getGlobalContext());
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);
Types.Void = llvm::Type::getVoidTy(context);
@ -227,7 +223,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs128 = builder.CreateTrunc(lhs256, Types.WordLowPrecision);
auto rhs128 = builder.CreateTrunc(rhs256, Types.WordLowPrecision);
auto res128 = builder.CreateMul(lhs128, rhs128);
auto res256 = builder.CreateZExt(res128, Types.word256);
auto res256 = builder.CreateZExt(res128, Type::i256);
stack.push(res256);
break;
}
@ -239,7 +235,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs128 = builder.CreateTrunc(lhs256, Types.WordLowPrecision);
auto rhs128 = builder.CreateTrunc(rhs256, Types.WordLowPrecision);
auto res128 = builder.CreateUDiv(lhs128, rhs128);
auto res256 = builder.CreateZExt(res128, Types.word256);
auto res256 = builder.CreateZExt(res128, Type::i256);
stack.push(res256);
break;
}
@ -251,7 +247,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs128 = builder.CreateTrunc(lhs256, Types.WordLowPrecision);
auto rhs128 = builder.CreateTrunc(rhs256, Types.WordLowPrecision);
auto res128 = builder.CreateSDiv(lhs128, rhs128);
auto res256 = builder.CreateSExt(res128, Types.word256);
auto res256 = builder.CreateSExt(res128, Type::i256);
stack.push(res256);
break;
}
@ -263,7 +259,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs128 = builder.CreateTrunc(lhs256, Types.WordLowPrecision);
auto rhs128 = builder.CreateTrunc(rhs256, Types.WordLowPrecision);
auto res128 = builder.CreateURem(lhs128, rhs128);
auto res256 = builder.CreateZExt(res128, Types.word256);
auto res256 = builder.CreateZExt(res128, Type::i256);
stack.push(res256);
break;
}
@ -275,7 +271,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs128 = builder.CreateTrunc(lhs256, Types.WordLowPrecision);
auto rhs128 = builder.CreateTrunc(rhs256, Types.WordLowPrecision);
auto res128 = builder.CreateSRem(lhs128, rhs128);
auto res256 = builder.CreateSExt(res128, Types.word256);
auto res256 = builder.CreateSExt(res128, Type::i256);
stack.push(res256);
break;
}
@ -292,7 +288,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
case Instruction::NEG:
{
auto top = stack.pop();
auto zero = ConstantInt::get(Types.word256, 0);
auto zero = ConstantInt::get(Type::i256, 0);
auto res = builder.CreateSub(zero, top);
stack.push(res);
break;
@ -303,7 +299,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs = stack.pop();
auto rhs = stack.pop();
auto res1 = builder.CreateICmpULT(lhs, rhs);
auto res256 = builder.CreateZExt(res1, Types.word256);
auto res256 = builder.CreateZExt(res1, Type::i256);
stack.push(res256);
break;
}
@ -313,7 +309,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs = stack.pop();
auto rhs = stack.pop();
auto res1 = builder.CreateICmpUGT(lhs, rhs);
auto res256 = builder.CreateZExt(res1, Types.word256);
auto res256 = builder.CreateZExt(res1, Type::i256);
stack.push(res256);
break;
}
@ -323,7 +319,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs = stack.pop();
auto rhs = stack.pop();
auto res1 = builder.CreateICmpSLT(lhs, rhs);
auto res256 = builder.CreateZExt(res1, Types.word256);
auto res256 = builder.CreateZExt(res1, Type::i256);
stack.push(res256);
break;
}
@ -333,7 +329,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs = stack.pop();
auto rhs = stack.pop();
auto res1 = builder.CreateICmpSGT(lhs, rhs);
auto res256 = builder.CreateZExt(res1, Types.word256);
auto res256 = builder.CreateZExt(res1, Type::i256);
stack.push(res256);
break;
}
@ -343,7 +339,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
auto lhs = stack.pop();
auto rhs = stack.pop();
auto res1 = builder.CreateICmpEQ(lhs, rhs);
auto res256 = builder.CreateZExt(res1, Types.word256);
auto res256 = builder.CreateZExt(res1, Type::i256);
stack.push(res256);
break;
}
@ -351,9 +347,9 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
case Instruction::NOT:
{
auto top = stack.pop();
auto zero = ConstantInt::get(Types.word256, 0);
auto zero = ConstantInt::get(Type::i256, 0);
auto iszero = builder.CreateICmpEQ(top, zero, "iszero");
auto result = builder.CreateZExt(iszero, Types.word256);
auto result = builder.CreateZExt(iszero, Type::i256);
stack.push(result);
break;
}
@ -584,7 +580,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
stack.pop();
auto top = stack.pop();
auto zero = ConstantInt::get(Types.word256, 0);
auto zero = ConstantInt::get(Type::i256, 0);
auto cond = builder.CreateICmpNE(top, zero, "nonzero");
auto& targetBlock = basicBlocks.find(jumpTargets[currentPC])->second;
auto& followBlock = basicBlocks.find(currentPC + 1)->second;

4
evmcc/GasMeter.cpp

@ -7,6 +7,8 @@
#include <libevmface/Instruction.h>
#include <libevm/FeeStructure.h>
#include "Type.h"
namespace evmcc
{
@ -52,7 +54,7 @@ uint64_t getStepCost(dev::eth::Instruction inst) // TODO: Add this function to F
GasMeter::GasMeter(llvm::IRBuilder<>& _builder, llvm::Module* _module):
m_builder(_builder)
{
m_gas = new llvm::GlobalVariable(*_module, m_builder.getIntNTy(256), false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(m_builder.getIntNTy(256)), "gas");
m_gas = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(Type::i256), "gas");
m_gas->setUnnamedAddr(true); // Address is not important
//llvm::Function::Create()

16
evmcc/Type.cpp

@ -0,0 +1,16 @@
#include "Type.h"
#include <llvm/IR/DerivedTypes.h>
namespace evmcc
{
llvm::Type* Type::i256;
void Type::init(llvm::LLVMContext& _context)
{
i256 = llvm::Type::getIntNTy(_context, 256);
}
}

16
evmcc/Type.h

@ -0,0 +1,16 @@
#pragma once
#include <llvm/IR/Type.h>
namespace evmcc
{
struct Type
{
static llvm::Type* i256;
static void init(llvm::LLVMContext& _context);
};
}
Loading…
Cancel
Save