All of DataLayout, IRBuilder and started on ExecutionEngine.

This commit is contained in:
Siu Kwan Lam 2013-01-29 17:47:15 -06:00
commit 437b1e9490
20 changed files with 688 additions and 41 deletions

19
newbinding/src/CodeGen.py Normal file
View file

@ -0,0 +1,19 @@
from binding import *
from namespace import llvm
@llvm.Class()
class Reloc:
Model = Enum('Default', 'Static', 'PIC_', 'DynamicNoPIC')
@llvm.Class()
class CodeModel:
Model = Enum('Default', 'JITDefault', 'Small', 'Kernel', 'Medium', 'Large')
@llvm.Class()
class TLSModel:
Model = Enum('GeneralDynamic', 'LocalDynamic', 'InitialExec', 'LocalExec')
@llvm.Class()
class CodeGenOpt:
'Actually a namespace'
Level = Enum('None', 'Less', 'Default', 'Aggressive')

View file

@ -0,0 +1,97 @@
from binding import *
from namespace import llvm
from LLVMContext import LLVMContext
from StringRef import StringRef
from Module import Module
from Type import Type, IntegerType, StructType
from SmallVector import SmallVector_Value
from GlobalVariable import GlobalVariable
DataLayout = llvm.Class()
StructLayout = llvm.Class()
@DataLayout
class DataLayout:
_include_ = 'llvm/DataLayout.h'
_new_string = Constructor(cast(str, StringRef))
_new_module = Constructor(ptr(Module))
@CustomPythonStaticMethod
def new(arg):
if isinstance(arg, Module):
return DataLayout._new_module(arg)
else:
return DataLayout._new_string(arg)
delete = Destructor()
isLittleEndian = Method(cast(Bool, bool))
isBigEndian = Method(cast(Bool, bool))
getStringRepresentation = Method(cast(StringRef, str))
@CustomPythonMethod
def __str__(self):
return self.getStringRepresentation()
isLegalInteger = Method(cast(Bool, bool), cast(int, Unsigned))
isIllegalInteger = Method(cast(Bool, bool), cast(int, Unsigned))
exceedsNaturalStackAlignment = Method(cast(Bool, bool), cast(int, Unsigned))
fitsInLegalInteger = Method(cast(Bool, bool), cast(int, Unsigned))
getPointerABIAlignment = Method(cast(Unsigned, int),
cast(int, Unsigned)).require_only(0)
getPointerPrefAlignment = Method(cast(Unsigned, int),
cast(int, Unsigned)).require_only(0)
getPointerSize = Method(cast(Unsigned, int),
cast(int, Unsigned)).require_only(0)
getPointerSizeInBits = Method(cast(Unsigned, int),
cast(int, Unsigned)).require_only(0)
getTypeSizeInBits = Method(cast(Uint64, int), ptr(Type))
getTypeStoreSize = Method(cast(Uint64, int), ptr(Type))
getTypeStoreSizeInBits = Method(cast(Uint64, int), ptr(Type))
getTypeAllocSize = Method(cast(Uint64, int), ptr(Type))
getTypeAllocSizeInBits = Method(cast(Uint64, int), ptr(Type))
getABITypeAlignment = Method(cast(Unsigned, int), ptr(Type))
getABIIntegerTypeAlignment = Method(cast(Unsigned, int), cast(int, Unsigned))
getCallFrameTypeAlignment = Method(cast(Unsigned, int), ptr(Type))
getPrefTypeAlignment = Method(cast(Unsigned, int), ptr(Type))
getPreferredTypeAlignmentShift = Method(cast(Unsigned, int), ptr(Type))
_getIntPtrType = Method(ptr(IntegerType),
ref(LLVMContext), cast(int, Unsigned))
_getIntPtrType.require_only(1)
_getIntPtrType.realname = 'getIntPtrType'
_getIntPtrType2 = Method(ptr(Type), ptr(Type))
_getIntPtrType2.realname = 'getIntPtrType'
@CustomPythonMethod
def getIntPtrType(self, *args):
if isinstance(args[0], LLVMContext):
return self._getIntPtrType(*args)
else:
return self._getIntPtrType(*args)
_getIndexedOffset = Method(cast(Uint64, int), ptr(Type),
ref(SmallVector_Value))
_getIndexedOffset.realname = 'getIndexedOffset'
@CustomPythonMethod
def getIndexedOffset(self, *args):
import extra
args = list(args)
args[1] = extra.make_small_vector_from_values(args[1])
return self.getIndexedOffset(*args)
getStructLayout = Method(const(ptr(StructLayout)), ptr(StructType))
getPreferredAlignment = Method(cast(Unsigned, int), ptr(GlobalVariable))
getPreferredAlignmentLog = Method(cast(Unsigned, int), ptr(GlobalVariable))
@StructLayout
class StructLayout:
pass

View file

@ -0,0 +1,34 @@
from binding import *
from namespace import llvm
from Module import Module
from JITMemoryManager import JITMemoryManager
from CodeGen import CodeGenOpt, Reloc, CodeModel
from DataLayout import DataLayout
ExecutionEngine = llvm.Class()
@ExecutionEngine
class ExecutionEngine:
_include_ = ('llvm/ExecutionEngine/ExecutionEngine.h',
'llvm/ExecutionEngine/JIT.h') # force linking of jit
delete = Destructor()
create = CustomStaticMethod('ExecutionEngine_create',
ptr(ExecutionEngine),
ownedptr(Module), cast(bool, Bool),
PyObjectPtr, CodeGenOpt.Level,
cast(bool, Bool)).require_only(1)
createJIT = CustomStaticMethod('ExecutionEngine_createJIT',
ptr(ExecutionEngine),
ownedptr(Module), PyObjectPtr,
ptr(JITMemoryManager),
CodeGenOpt.Level,
cast(bool, Bool),
Reloc.Model,
CodeModel.Model).require_only(1)
addModule = Method(Void, ptr(Module))
getDataLayout = Method(const(ownedptr(DataLayout)))
removeModule = Method(cast(Bool, bool), ptr(Module))

View file

@ -0,0 +1,8 @@
from binding import *
from namespace import llvm
from GlobalValue import GlobalValue
@llvm.Class(GlobalValue)
class GlobalVariable:
pass

View file

@ -2,10 +2,16 @@ from binding import *
from namespace import llvm
from LLVMContext import LLVMContext
from BasicBlock import BasicBlock
from Instruction import ReturnInst, CallInst
from SmallVector import SmallVector_Value
from Instruction import ReturnInst, CallInst, BranchInst, SwitchInst
from Instruction import IndirectBrInst, InvokeInst, ResumeInst, PHINode
from Instruction import UnreachableInst, AllocaInst, LoadInst, StoreInst
from Instruction import FenceInst, AtomicCmpXchgInst, AtomicRMWInst, CmpInst
from Instruction import LandingPadInst, VAArgInst
from Instruction import AtomicOrdering, SynchronizationScope
from SmallVector import SmallVector_Value, SmallVector_Unsigned
from StringRef import StringRef
from Value import Value
from Value import Value, MDNode
from Type import Type, IntegerType
IRBuilder = llvm.Class()
@ -24,15 +30,238 @@ class IRBuilder:
CreateRetVoid = Method(ptr(ReturnInst))
CreateRet = Method(ptr(ReturnInst), ptr(Value))
_CreateCall = Method(ptr(CallInst), ptr(Value), ref(SmallVector_Value),
CreateBr = Method(ptr(BranchInst), ptr(BasicBlock))
CreateCondBr = Method(ptr(BranchInst), ptr(Value), ptr(BasicBlock),
ptr(BasicBlock), ptr(MDNode)).require_only(3)
CreateSwitch = Method(ptr(SwitchInst), ptr(Value), ptr(BasicBlock),
cast(int, Unsigned), ptr(MDNode)).require_only(2)
CreateIndirectBr = Method(ptr(IndirectBrInst), ptr(Value),
cast(int, Unsigned)).require_only(1)
CreateInvoke = Method(ptr(InvokeInst), ptr(Value), ptr(BasicBlock),
ptr(BasicBlock), ref(SmallVector_Value),
cast(str, StringRef)).require_only(4)
CreateResume = Method(ptr(ResumeInst), ptr(Value))
CreateUnreachable = Method(ptr(UnreachableInst))
def _binop_has_nsw_nuw():
sig = [ptr(Value), ptr(Value), ptr(Value), cast(str, StringRef),
cast(bool, Bool), cast(bool, Bool)]
op = Method(*sig).require_only(2)
return op
CreateAdd = _binop_has_nsw_nuw()
CreateSub = _binop_has_nsw_nuw()
CreateMul = _binop_has_nsw_nuw()
CreateShl = _binop_has_nsw_nuw()
def _binop_is_exact():
sig = [ptr(Value), ptr(Value), ptr(Value), cast(str, StringRef),
cast(bool, Bool)]
op = Method(*sig).require_only(2)
return op
CreateUDiv = _binop_is_exact()
CreateSDiv = _binop_is_exact()
CreateLShr = _binop_is_exact()
CreateAShr = _binop_is_exact()
def _binop_basic():
sig = [ptr(Value), ptr(Value), ptr(Value), cast(str, StringRef)]
op = Method(*sig).require_only(2)
return op
CreateURem = _binop_basic()
CreateSRem = _binop_basic()
CreateAnd = _binop_basic()
CreateOr = _binop_basic()
CreateXor = _binop_basic()
def _float_binop():
sig = [ptr(Value), ptr(Value), ptr(Value), cast(str, StringRef),
ptr(MDNode)]
op = Method(*sig).require_only(2)
return op
CreateFAdd = _float_binop()
CreateFSub = _float_binop()
CreateFMul = _float_binop()
CreateFDiv = _float_binop()
CreateFRem = _float_binop()
def _unop_has_nsw_nuw():
sig = [ptr(Value), ptr(Value), cast(str, StringRef),
cast(bool, Bool), cast(bool, Bool)]
op = Method(*sig).require_only(1)
return op
CreateNeg = _unop_has_nsw_nuw()
def _float_unop():
sig = [ptr(Value), ptr(Value), cast(str, StringRef), ptr(MDNode)]
op = Method(*sig).require_only(1)
return op
CreateFNeg = _float_unop()
CreateNot = Method(ptr(Value),
ptr(Value), cast(str, StringRef)).require_only(1)
CreateAlloca = Method(ptr(AllocaInst), ptr(Type), ptr(Value),
cast(str, StringRef)).require_only(2)
CreateLoad = Method(ptr(LoadInst),
ptr(Value), cast(str, StringRef)).require_only(1)
CreateStore = Method(ptr(StoreInst), ptr(Value), ptr(Value),
cast(bool, Bool)).require_only(2)
CreateAlignedLoad = Method(ptr(LoadInst), ptr(Value), cast(int, Unsigned),
cast(bool, Bool), cast(str, StringRef))
CreateAlignedLoad.require_only(2)
CreateAlignedStore = Method(ptr(StoreInst), ptr(Value), ptr(Value),
cast(int, Unsigned), cast(bool, Bool))
CreateAlignedStore.require_only(3)
CreateFence = Method(ptr(FenceInst),
AtomicOrdering, SynchronizationScope).require_only(1)
CreateAtomicCmpXchg = Method(ptr(AtomicCmpXchgInst), ptr(Value), ptr(Value),
ptr(Value), AtomicOrdering, SynchronizationScope)
CreateAtomicCmpXchg.require_only(4)
CreateAtomicRMW = Method(ptr(AtomicRMWInst), AtomicRMWInst.BinOp,
ptr(Value), ptr(Value), AtomicOrdering,
SynchronizationScope)
CreateAtomicRMW.require_only(4)
_CreateGEP = Method(ptr(Value), ptr(Value), ref(SmallVector_Value),
cast(str, StringRef))
_CreateGEP.require_only(2)
_CreateGEP.realname = 'CreateGEP'
@CustomPythonMethod
def CreateGEP(self, *args):
import extra
args = list(args)
valuelist = args[1]
args[1] = extra.make_small_vector_from_values(*valuelist)
return self._CreateGEP(*args)
_CreateInBoundsGEP = Method(ptr(Value), ptr(Value), ref(SmallVector_Value),
cast(str, StringRef))
_CreateCall |= Method(ptr(CallInst), ptr(Value), ref(SmallVector_Value))
_CreateInBoundsGEP.require_only(2)
_CreateInBoundsGEP.realname = 'CreateInBoundsGEP'
@CustomPythonMethod
def CreateInBoundsGEP(self, *args):
import extra
args = list(args)
valuelist = args[1]
args[1] = extra.make_small_vector_from_values(*valuelist)
return self._CreateInBoundsGEP(*args)
CreateStructGEP = Method(ptr(Value), ptr(Value), cast(int, Unsigned),
cast(str, StringRef)).require_only(2)
CreateGlobalStringPtr = Method(ptr(Value), cast(str, StringRef),
cast(str, StringRef)).require_only(1)
def _value_type():
sig = [ptr(Value), ptr(Value), ptr(Type), cast(str, StringRef)]
op = Method(*sig).require_only(2)
return op
CreateTrunc = _value_type()
CreateZExt = _value_type()
CreateSExt = _value_type()
CreateZExtOrTrunc = Method(ptr(Value), ptr(Value), ptr(IntegerType),
cast(str, StringRef)).require_only(2)
CreateSExtOrTrunc = Method(ptr(Value), ptr(Value), ptr(IntegerType),
cast(str, StringRef)).require_only(2)
CreateFPToUI = _value_type()
CreateFPToSI = _value_type()
CreateUIToFP = _value_type()
CreateSIToFP = _value_type()
CreateFPTrunc = _value_type()
CreateFPExt = _value_type()
CreatePtrToInt = _value_type()
CreateIntToPtr = _value_type()
CreateBitCast = _value_type()
CreateZExtOrBitCast = _value_type()
CreateSExtOrBitCast = _value_type()
# Skip CreateCast
CreateTruncOrBitCast = _value_type()
CreateIntCast = Method(ptr(Value), ptr(Value), ptr(Type), cast(bool, Bool),
cast(str, StringRef)).require_only(3)
CreateFPCast = _value_type()
_CreateCall = Method(ptr(CallInst), ptr(Value), ref(SmallVector_Value),
cast(str, StringRef)).require_only(2)
_CreateCall.realname = 'CreateCall'
# Skip specialized CreateICmp* and CreateFCmp*
CreateICmp = Method(ptr(Value), CmpInst.Predicate, ptr(Value), ptr(Value),
cast(str, StringRef)).require_only(3)
CreateFCmp = Method(ptr(Value), CmpInst.Predicate, ptr(Value), ptr(Value),
cast(str, StringRef)).require_only(3)
CreatePHI = Method(ptr(PHINode), ptr(Type), cast(int, Unsigned),
cast(str, StringRef)).require_only(2)
@CustomPythonMethod
def CreateCall(self, *args):
import extra
args = list(args)
valuelist = args[1]
args[1] = extra.make_small_vector_from_values(*valuelist)
return IRBuilder._CreateCall(self, *args)
return self._CreateCall(*args)
CreateSelect = Method(ptr(Value), ptr(Value), ptr(Value), ptr(Value),
cast(str, StringRef)).require_only(3)
CreateVAArg = Method(ptr(VAArgInst), ptr(Value), ptr(Type),
cast(str, StringRef)).require_only(2)
CreateExtractElement = _binop_basic()
CreateInsertElement = Method(ptr(Value), ptr(Value), ptr(Value), ptr(Value),
cast(str, StringRef)).require_only(3)
CreateShuffleVector = Method(ptr(Value), ptr(Value), ptr(Value),
ptr(Value), cast(str, StringRef))
CreateShuffleVector.require_only(3)
CreateExtractValue = Method(ptr(Value), ptr(Value), ref(SmallVector_Unsigned),
cast(str, StringRef))
CreateExtractValue.require_only(2)
CreateInsertValue = Method(ptr(Value), ptr(Value), ptr(Value),
ref(SmallVector_Unsigned), cast(str, StringRef))
CreateInsertValue.require_only(3)
CreateLandingPad = Method(ptr(LandingPadInst), ptr(Type), ptr(Value),
cast(int, Unsigned), cast(str, StringRef))
CreateLandingPad.require_only(3)
CreateIsNull = Method(ptr(Value), ptr(Value), cast(str, StringRef))
CreateIsNull.require_only(1)
CreateIsNotNull = Method(ptr(Value), ptr(Value), cast(str, StringRef))
CreateIsNotNull.require_only(1)
CreatePtrDiff = _binop_basic()
# New in llvm 3.3
#CreateVectorSplat = Method(ptr(Value), cast(int, Unsigned), ptr(Value),
# cast(str, StringRef))

View file

@ -26,9 +26,9 @@ IntrinsicInst = llvm.Class(CallInst)
FCmpInst = llvm.Class(CmpInst)
ICmpInst = llvm.Class(CmpInst)
BranchInst = llvm.Class(TerminatorInst)
IndirectBrInst = llvm.Class(TerminatorInst)
InvokeInst = llvm.Class(TerminatorInst)
ResumeInst = llvm.Class(TerminatorInst)
ReturnInst = llvm.Class(TerminatorInst)
SwitchInst = llvm.Class(TerminatorInst)
@ -56,14 +56,14 @@ FPTruncInst = llvm.Class(CastInst)
class Instruction:
pass
@AtomicCmpXchgInst
class AtomicCmpXchgInst:
pass
@AtomicRMWInst
class AtomicRMWInst:
pass
BinOp = Enum('Xchg', 'Add', 'Sub', 'And', 'Nand', 'Or', 'Xor', 'Max', 'Min',
'UMax', 'UMin', 'FIRST_BINOP', 'LAST_BINOP', 'BAD_BINOP')
@BinaryOperator
class BinaryOperator:
@ -75,7 +75,17 @@ class CallInst:
@CmpInst
class CmpInst:
pass
Predicate = Enum('FCMP_FALSE', 'FCMP_OEQ', 'FCMP_OGT', 'FCMP_OGE',
'FCMP_OLT', 'FCMP_OLE', 'FCMP_ONE', 'FCMP_ORD', 'FCMP_UNO',
'FCMP_UEQ', 'FCMP_UGT', 'FCMP_UGE', 'FCMP_ULT', 'FCMP_ULE',
'FCMP_UNE', 'FCMP_TRUE', 'FIRST_FCMP_PREDICATE',
'LAST_FCMP_PREDICATE',
'BAD_FCMP_PREDICATE',
'ICMP_EQ', 'ICMP_NE', 'ICMP_UGT', 'ICMP_UGE', 'ICMP_ULT',
'ICMP_ULE', 'ICMP_SGT', 'ICMP_SGE', 'ICMP_SLT', 'ICMP_SLE',
'FIRST_ICMP_PREDICATE',
'LAST_ICMP_PREDICATE',
'BAD_ICMP_PREDICATE',)
@ExtractElementInst
class ExtractElementInst:
@ -146,11 +156,14 @@ class ICmpInst:
class BranchInst:
pass
@IndirectBrInst
class IndirectBrInst:
pass
@InvokeInst
class InvokeInst:
pass
@ResumeInst
class ResumeInst:
pass
@ -228,3 +241,12 @@ class FPToUIInst:
@FPTruncInst
class FPTruncInst:
pass
AtomicOrdering = llvm.Enum('AtomicOrdering',
'NotAtomic', 'Unordered', 'Monotonic', 'Acquire',
'Release', 'AcquireRelease',
'SequentiallyConsistent')
SynchronizationScope = llvm.Enum('SynchronizationScope',
'SingleThread', 'CrossThread')

View file

@ -0,0 +1,7 @@
from binding import *
from namespace import llvm
@llvm.Class()
class JITMemoryManager:
pass

View file

@ -0,0 +1,7 @@
from binding import *
from namespace import llvm
from Value import MDNode
@MDNode
class MDNode:
pass

View file

@ -36,7 +36,8 @@ class Module:
appendModuleInlineAsm = Method(Void, cast(str, StringRef))
# Function Accessors
getOrInsertFunction = Method(ptr(Constant), cast(str, StringRef), ptr(FunctionType))
getOrInsertFunction = Method(ptr(Constant), cast(str, StringRef),
ptr(FunctionType))
# Utilities
dump = Method(Void)

View file

@ -11,3 +11,7 @@ class SmallVector_Value:
_realname_ = 'SmallVector<llvm::Value*,8>'
delete = Destructor()
@llvm.Class()
class SmallVector_Unsigned:
_realname_ = 'SmallVector<unsigned,8>'
delete = Destructor()

View file

@ -0,0 +1,8 @@
from binding import *
from namespace import llvm
llvm.includes.add('llvm/Support/TargetSelect.h')
llvm.Function('InitializeNativeTarget')
#llvm.Function('InitializeAllTargets')

View file

@ -6,6 +6,7 @@ from raw_ostream import raw_ostream
Type = llvm.Class()
IntegerType = llvm.Class(Type)
CompositeType = llvm.Class(Type)
StructType = llvm.Class(CompositeType)
SequentialType = llvm.Class(CompositeType)
PointerType = llvm.Class(SequentialType)
@ -113,3 +114,6 @@ class SequentialType:
class PointerType:
pass
@StructType
class StructType:
pass

View file

@ -9,6 +9,7 @@ from StringRef import StringRef
Value = llvm.Class()
# forward declarations
Argument = llvm.Class(Value)
MDNode = llvm.Class(Value)
User = llvm.Class(Value)
BasicBlock = llvm.Class(Value)
Constant = llvm.Class(User)