diff --git a/newbinding/include/llvm_binding/extra.h b/newbinding/include/llvm_binding/extra.h index bde92a7..75739de 100644 --- a/newbinding/include/llvm_binding/extra.h +++ b/newbinding/include/llvm_binding/extra.h @@ -14,6 +14,7 @@ #include #include #include +#include #include "auto_pyobject.h" @@ -732,3 +733,17 @@ PyObject* BasicBlock_getInstList(llvm::BasicBlock* BB) "llvm::Instruction"); } +static +PyObject* IRBuilder_CreateAggregateRet(llvm::IRBuilder<>* builder, + PyObject* Vals, + unsigned N) +{ + using namespace llvm; + std::vector vec_values; + bool ok = extract::from_py_sequence(vec_values, Vals, "llvm::Value"); + if (not ok) return NULL; + Value** ptr_values = &vec_values[0]; + ReturnInst* inst = builder->CreateAggregateRet(ptr_values, N); + return pycapsule_new(inst, "llvm::Value", "llvm:ReturnInst"); +} + diff --git a/newbinding/src/IRBuilder.py b/newbinding/src/IRBuilder.py index e0cac79..2965367 100644 --- a/newbinding/src/IRBuilder.py +++ b/newbinding/src/IRBuilder.py @@ -2,6 +2,7 @@ from binding import * from namespace import llvm from LLVMContext import LLVMContext from BasicBlock import BasicBlock +from Instruction import Instruction from Instruction import ReturnInst, CallInst, BranchInst, SwitchInst from Instruction import IndirectBrInst, InvokeInst, ResumeInst, PHINode from Instruction import UnreachableInst, AllocaInst, LoadInst, StoreInst @@ -23,12 +24,30 @@ class IRBuilder: new = Constructor(ref(LLVMContext)) delete = Destructor() - SetInsertPoint = Method(Void, ptr(BasicBlock)) + GetInsertBlock = Method(ptr(BasicBlock)) + + _SetInsertPoint_end_of_bb = Method(Void, ptr(BasicBlock)) + _SetInsertPoint_end_of_bb.realname = 'SetInsertPoint' + _SetInsertPoint_before_instr = Method(Void, ptr(Instruction)) + _SetInsertPoint_before_instr.realname = 'SetInsertPoint' + + @CustomPythonMethod + def SetInsertPoint(self, pt): + if isinstance(pt, Instruction): + return self._SetInsertPoint_before_instr(pt) + elif isinstance(pt, BasicBlock): + return self._SetInsertPoint_end_of_bb(pt) + else: + raise ValueError("Expected either an Instruction or a BasicBlock") isNamePreserving = Method(cast(Bool, bool)) CreateRetVoid = Method(ptr(ReturnInst)) CreateRet = Method(ptr(ReturnInst), ptr(Value)) + CreateAggregateRet = CustomMethod('IRBuilder_CreateAggregateRet', + PyObjectPtr, # ptr(ReturnInst), + PyObjectPtr, # list of Value + cast(int, Unsigned)) CreateBr = Method(ptr(BranchInst), ptr(BasicBlock)) @@ -42,9 +61,18 @@ class IRBuilder: CreateIndirectBr = Method(ptr(IndirectBrInst), ptr(Value), cast(int, Unsigned)).require_only(1) - CreateInvoke = Method(ptr(InvokeInst), ptr(Value), ptr(BasicBlock), + _CreateInvoke = Method(ptr(InvokeInst), ptr(Value), ptr(BasicBlock), ptr(BasicBlock), ref(SmallVector_Value), cast(str, StringRef)).require_only(4) + _CreateInvoke.realname = 'CreateInvoke' + + @CustomPythonMethod + def CreateInvoke(self, *args): + import extra + args = list(args) + valuelist = args[3] + args[3] = extra.make_small_vector_from_values(*valuelist) + return self._CreateInvoke(*args) CreateResume = Method(ptr(ResumeInst), ptr(Value)) @@ -114,8 +142,11 @@ class IRBuilder: ptr(Value), cast(str, StringRef)).require_only(1) - CreateAlloca = Method(ptr(AllocaInst), ptr(Type), ptr(Value), - cast(str, StringRef)).require_only(2) + CreateAlloca = Method(ptr(AllocaInst), + ptr(Type), # ty + ptr(Value), # arysize = 0 + cast(str, StringRef), # name = '' + ).require_only(1) CreateLoad = Method(ptr(LoadInst), ptr(Value), cast(str, StringRef)).require_only(1) @@ -208,6 +239,14 @@ class IRBuilder: cast(str, StringRef)).require_only(2) _CreateCall.realname = 'CreateCall' + @CustomPythonMethod + def CreateCall(self, *args): + import extra + args = list(args) + valuelist = args[1] + args[1] = extra.make_small_vector_from_values(*valuelist) + return self._CreateCall(*args) + # Skip specialized CreateICmp* and CreateFCmp* CreateICmp = Method(ptr(Value), CmpInst.Predicate, ptr(Value), ptr(Value), @@ -219,13 +258,6 @@ class IRBuilder: 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 self._CreateCall(*args) CreateSelect = Method(ptr(Value), ptr(Value), ptr(Value), ptr(Value), cast(str, StringRef)).require_only(3) diff --git a/newbinding/src/Instruction.py b/newbinding/src/Instruction.py index 79cf4a3..ca64072 100644 --- a/newbinding/src/Instruction.py +++ b/newbinding/src/Instruction.py @@ -5,6 +5,7 @@ from ADT.StringRef import StringRef from CallingConv import CallingConv from Attributes import Attributes from Constant import ConstantInt +from Type import Type Instruction = llvm.Class(User) @@ -57,6 +58,17 @@ FPToSIInst = llvm.Class(CastInst) FPToUIInst = llvm.Class(CastInst) FPTruncInst = llvm.Class(CastInst) +AtomicOrdering = llvm.Enum('AtomicOrdering', + 'NotAtomic', 'Unordered', 'Monotonic', 'Acquire', + 'Release', 'AcquireRelease', + 'SequentiallyConsistent') + +SynchronizationScope = llvm.Enum('SynchronizationScope', + 'SingleThread', 'CrossThread') + + + + @Instruction class Instruction: removeFromParent = Method() @@ -129,6 +141,18 @@ class CallInst: setCalledFunction = Method(Void, ptr(Function)) isInlineAsm = Method(cast(Bool, bool)) + CreateMalloc = StaticMethod(ptr(Instruction), + ptr(BasicBlock), # insertAtEnd + ptr(Type), # intptrty + ptr(Type), # allocty + ptr(Value), # allocsz + ptr(Value), # array size = 0 + ptr(Function), # malloc fn = 0 + cast(str, StringRef), # name + ).require_only(4) + + CreateFree = StaticMethod(ptr(Instruction), ptr(Value), ptr(BasicBlock)) + @CmpInst class CmpInst: Predicate = Enum('FCMP_FALSE', 'FCMP_OEQ', 'FCMP_OGT', 'FCMP_OGE', @@ -201,6 +225,10 @@ class StoreInst: getAlignment = Method(cast(Unsigned, int)) setAlignment = Method(Void, cast(int, Unsigned)) + setAtomic = Method(Void, + AtomicOrdering, + SynchronizationScope).require_only(1) + classof = StaticMethod(cast(Bool, bool), ptr(Value)) @TerminatorInst @@ -298,6 +326,10 @@ class LoadInst: getAlignment = Method(cast(Unsigned, int)) setAlignment = Method(Void, cast(int, Unsigned)) + setAtomic = Method(Void, + AtomicOrdering, + SynchronizationScope).require_only(1) + classof = StaticMethod(cast(Bool, bool), ptr(Value)) @VAArgInst @@ -345,11 +377,3 @@ class FPToUIInst: class FPTruncInst: pass -AtomicOrdering = llvm.Enum('AtomicOrdering', - 'NotAtomic', 'Unordered', 'Monotonic', 'Acquire', - 'Release', 'AcquireRelease', - 'SequentiallyConsistent') - -SynchronizationScope = llvm.Enum('SynchronizationScope', - 'SingleThread', 'CrossThread') -