C and Objective Caml bindings for the various getParent methods of the IR.
[oota-llvm.git] / include / llvm-c / Core.h
1 /*===-- llvm-c/Core.h - Core Library C Interface ------------------*- C -*-===*\
2 |*                                                                            *|
3 |*                     The LLVM Compiler Infrastructure                       *|
4 |*                                                                            *|
5 |* This file is distributed under the University of Illinois Open Source      *|
6 |* License. See LICENSE.TXT for details.                                      *|
7 |*                                                                            *|
8 |*===----------------------------------------------------------------------===*|
9 |*                                                                            *|
10 |* This header declares the C interface to libLLVMCore.a, which implements    *|
11 |* the LLVM intermediate representation.                                      *|
12 |*                                                                            *|
13 |* LLVM uses a polymorphic type hierarchy which C cannot represent, therefore *|
14 |* parameters must be passed as base types. Despite the declared types, most  *|
15 |* of the functions provided operate only on branches of the type hierarchy.  *|
16 |* The declared parameter names are descriptive and specify which type is     *|
17 |* required. Additionally, each type hierarchy is documented along with the   *|
18 |* functions that operate upon it. For more detail, refer to LLVM's C++ code. *|
19 |* If in doubt, refer to Core.cpp, which performs paramter downcasts in the   *|
20 |* form unwrap<RequiredType>(Param).                                          *|
21 |*                                                                            *|
22 |* Many exotic languages can interoperate with C code but have a harder time  *|
23 |* with C++ due to name mangling. So in addition to C, this interface enables *|
24 |* tools written in such languages.                                           *|
25 |*                                                                            *|
26 |* When included into a C++ source file, also declares 'wrap' and 'unwrap'    *|
27 |* helpers to perform opaque reference<-->pointer conversions. These helpers  *|
28 |* are shorter and more tightly typed than writing the casts by hand when     *|
29 |* authoring bindings. In assert builds, they will do runtime type checking.  *|
30 |*                                                                            *|
31 \*===----------------------------------------------------------------------===*/
32
33 #ifndef LLVM_C_CORE_H
34 #define LLVM_C_CORE_H
35
36 #ifdef __cplusplus
37
38 /* Need these includes to support the LLVM 'cast' template for the C++ 'wrap' 
39    and 'unwrap' conversion functions. */
40 #include "llvm/Module.h"
41 #include "llvm/Support/LLVMBuilder.h"
42
43 extern "C" {
44 #endif
45
46
47 /* Opaque types. */
48
49 /**
50  * The top-level container for all other LLVM Intermediate Representation (IR)
51  * objects. See the llvm::Module class.
52  */
53 typedef struct LLVMOpaqueModule *LLVMModuleRef;
54
55 /**
56  * Each value in the LLVM IR has a type, an LLVMTypeRef. See the llvm::Type
57  * class.
58  */
59 typedef struct LLVMOpaqueType *LLVMTypeRef;
60
61 /**
62  * When building recursive types using LLVMRefineType, LLVMTypeRef values may
63  * become invalid; use LLVMTypeHandleRef to resolve this problem. See the
64  * llvm::AbstractTypeHolder class.
65  */
66 typedef struct LLVMOpaqueTypeHandle *LLVMTypeHandleRef;
67
68 typedef struct LLVMOpaqueValue *LLVMValueRef;
69 typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef;
70 typedef struct LLVMOpaqueBuilder *LLVMBuilderRef;
71
72 /* Used to provide a module to JIT or interpreter.
73  * See the llvm::ModuleProvider class.
74  */
75 typedef struct LLVMOpaqueModuleProvider *LLVMModuleProviderRef;
76
77 /* Used to provide a module to JIT or interpreter.
78  * See the llvm::MemoryBuffer class.
79  */
80 typedef struct LLVMOpaqueMemoryBuffer *LLVMMemoryBufferRef;
81
82 /** See the llvm::PassManagerBase class. */
83 typedef struct LLVMOpaquePassManager *LLVMPassManagerRef;
84
85 typedef enum {
86   LLVMVoidTypeKind,        /**< type with no size */
87   LLVMFloatTypeKind,       /**< 32 bit floating point type */
88   LLVMDoubleTypeKind,      /**< 64 bit floating point type */
89   LLVMX86_FP80TypeKind,    /**< 80 bit floating point type (X87) */
90   LLVMFP128TypeKind,       /**< 128 bit floating point type (112-bit mantissa)*/
91   LLVMPPC_FP128TypeKind,   /**< 128 bit floating point type (two 64-bits) */
92   LLVMLabelTypeKind,       /**< Labels */
93   LLVMIntegerTypeKind,     /**< Arbitrary bit width integers */
94   LLVMFunctionTypeKind,    /**< Functions */
95   LLVMStructTypeKind,      /**< Structures */
96   LLVMArrayTypeKind,       /**< Arrays */
97   LLVMPointerTypeKind,     /**< Pointers */
98   LLVMOpaqueTypeKind,      /**< Opaque: type with unknown structure */
99   LLVMVectorTypeKind       /**< SIMD 'packed' format, or other vector type */
100 } LLVMTypeKind;
101
102 typedef enum {
103   LLVMExternalLinkage,    /**< Externally visible function */
104   LLVMLinkOnceLinkage,    /**< Keep one copy of function when linking (inline)*/
105   LLVMWeakLinkage,        /**< Keep one copy of function when linking (weak) */
106   LLVMAppendingLinkage,   /**< Special purpose, only applies to global arrays */
107   LLVMInternalLinkage,    /**< Rename collisions when linking (static
108                                functions) */
109   LLVMDLLImportLinkage,   /**< Function to be imported from DLL */
110   LLVMDLLExportLinkage,   /**< Function to be accessible from DLL */
111   LLVMExternalWeakLinkage,/**< ExternalWeak linkage description */
112   LLVMGhostLinkage        /**< Stand-in functions for streaming fns from
113                                bitcode */
114 } LLVMLinkage;
115
116 typedef enum {
117   LLVMDefaultVisibility,  /**< The GV is visible */
118   LLVMHiddenVisibility,   /**< The GV is hidden */
119   LLVMProtectedVisibility /**< The GV is protected */
120 } LLVMVisibility;
121
122 typedef enum {
123   LLVMCCallConv           = 0,
124   LLVMFastCallConv        = 8,
125   LLVMColdCallConv        = 9,
126   LLVMX86StdcallCallConv  = 64,
127   LLVMX86FastcallCallConv = 65
128 } LLVMCallConv;
129
130 typedef enum {
131   LLVMIntEQ = 32, /**< equal */
132   LLVMIntNE,      /**< not equal */
133   LLVMIntUGT,     /**< unsigned greater than */
134   LLVMIntUGE,     /**< unsigned greater or equal */
135   LLVMIntULT,     /**< unsigned less than */
136   LLVMIntULE,     /**< unsigned less or equal */
137   LLVMIntSGT,     /**< signed greater than */
138   LLVMIntSGE,     /**< signed greater or equal */
139   LLVMIntSLT,     /**< signed less than */
140   LLVMIntSLE      /**< signed less or equal */
141 } LLVMIntPredicate;
142
143 typedef enum {
144   LLVMRealPredicateFalse, /**< Always false (always folded) */
145   LLVMRealOEQ,            /**< True if ordered and equal */
146   LLVMRealOGT,            /**< True if ordered and greater than */
147   LLVMRealOGE,            /**< True if ordered and greater than or equal */
148   LLVMRealOLT,            /**< True if ordered and less than */
149   LLVMRealOLE,            /**< True if ordered and less than or equal */
150   LLVMRealONE,            /**< True if ordered and operands are unequal */
151   LLVMRealORD,            /**< True if ordered (no nans) */
152   LLVMRealUNO,            /**< True if unordered: isnan(X) | isnan(Y) */
153   LLVMRealUEQ,            /**< True if unordered or equal */
154   LLVMRealUGT,            /**< True if unordered or greater than */
155   LLVMRealUGE,            /**< True if unordered, greater than, or equal */
156   LLVMRealULT,            /**< True if unordered or less than */
157   LLVMRealULE,            /**< True if unordered, less than, or equal */
158   LLVMRealUNE,            /**< True if unordered or not equal */
159   LLVMRealPredicateTrue   /**< Always true (always folded) */
160 } LLVMRealPredicate;
161
162
163 /*===-- Error handling ----------------------------------------------------===*/
164
165 void LLVMDisposeMessage(char *Message);
166
167
168 /*===-- Modules -----------------------------------------------------------===*/
169
170 /* Create and destroy modules. */ 
171 /** See llvm::Module::Module. */
172 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID);
173
174 /** See llvm::Module::~Module. */
175 void LLVMDisposeModule(LLVMModuleRef M);
176
177 /** Data layout. See Module::getDataLayout. */
178 const char *LLVMGetDataLayout(LLVMModuleRef M);
179 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple);
180
181 /** Target triple. See Module::getTargetTriple. */
182 const char *LLVMGetTarget(LLVMModuleRef M);
183 void LLVMSetTarget(LLVMModuleRef M, const char *Triple);
184
185 /** See Module::addTypeName. */
186 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty);
187 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name);
188
189 /** See Module::dump. */
190 void LLVMDumpModule(LLVMModuleRef M);
191
192
193 /*===-- Types -------------------------------------------------------------===*/
194
195 /* LLVM types conform to the following hierarchy:
196  * 
197  *   types:
198  *     integer type
199  *     real type
200  *     function type
201  *     sequence types:
202  *       array type
203  *       pointer type
204  *       vector type
205  *     void type
206  *     label type
207  *     opaque type
208  */
209
210 /** See llvm::LLVMTypeKind::getTypeID. */
211 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty);
212
213 /** See llvm::DerivedType::refineAbstractTypeTo. */
214 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType);
215
216 /* Operations on integer types */
217 LLVMTypeRef LLVMInt1Type();
218 LLVMTypeRef LLVMInt8Type();
219 LLVMTypeRef LLVMInt16Type();
220 LLVMTypeRef LLVMInt32Type();
221 LLVMTypeRef LLVMInt64Type();
222 LLVMTypeRef LLVMIntType(unsigned NumBits);
223 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy);
224
225 /* Operations on real types */
226 LLVMTypeRef LLVMFloatType();
227 LLVMTypeRef LLVMDoubleType();
228 LLVMTypeRef LLVMX86FP80Type();
229 LLVMTypeRef LLVMFP128Type();
230 LLVMTypeRef LLVMPPCFP128Type();
231
232 /* Operations on function types */
233 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
234                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
235                              int IsVarArg);
236 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy);
237 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy);
238 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy);
239 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest);
240
241 /* Operations on struct types */
242 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, unsigned ElementCount,
243                            int Packed);
244 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy);
245 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest);
246 int LLVMIsPackedStruct(LLVMTypeRef StructTy);
247
248 /* Operations on array, pointer, and vector types (sequence types) */
249 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount);
250 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace);
251 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount);
252
253 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty);
254 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy);
255 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy);
256 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy);
257
258 /* Operations on other types */
259 LLVMTypeRef LLVMVoidType();
260 LLVMTypeRef LLVMLabelType();
261 LLVMTypeRef LLVMOpaqueType();
262
263 /* Operations on type handles */
264 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy);
265 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy);
266 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle);
267 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle);
268
269
270 /*===-- Values ------------------------------------------------------------===*/
271
272 /* The bulk of LLVM's object model consists of values, which comprise a very
273  * rich type hierarchy.
274  * 
275  *   values:
276  *     constants:
277  *       scalar constants
278  *       composite contants
279  *       globals:
280  *         global variable
281  *         function
282  *         alias
283  *       basic blocks
284  */
285
286 /* Operations on all values */
287 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val);
288 const char *LLVMGetValueName(LLVMValueRef Val);
289 void LLVMSetValueName(LLVMValueRef Val, const char *Name);
290 void LLVMDumpValue(LLVMValueRef Val);
291
292 /* Operations on constants of any type */
293 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty); /* all zeroes */
294 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty); /* only for int/vector */
295 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty);
296 int LLVMIsConstant(LLVMValueRef Val);
297 int LLVMIsNull(LLVMValueRef Val);
298 int LLVMIsUndef(LLVMValueRef Val);
299
300 /* Operations on scalar constants */
301 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
302                           int SignExtend);
303 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N);
304 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text);
305
306 /* Operations on composite constants */
307 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
308                              int DontNullTerminate);
309 LLVMValueRef LLVMConstArray(LLVMTypeRef ArrayTy,
310                             LLVMValueRef *ConstantVals, unsigned Length);
311 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
312                              int packed);
313 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);
314
315 /* Constant expressions */
316 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty);
317 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal);
318 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal);
319 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
320 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
321 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
322 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
323 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
324 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
325 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
326 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
327 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
328 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
329 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
330 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
331 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
332                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
333 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
334                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
335 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
336 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
337 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
338 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
339                           LLVMValueRef *ConstantIndices, unsigned NumIndices);
340 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
341 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
342 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
343 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
344 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
345 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
346 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
347 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
348 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
349 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
350 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
351 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
352 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
353                              LLVMValueRef ConstantIfTrue,
354                              LLVMValueRef ConstantIfFalse);
355 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
356                                      LLVMValueRef IndexConstant);
357 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
358                                     LLVMValueRef ElementValueConstant,
359                                     LLVMValueRef IndexConstant);
360 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
361                                     LLVMValueRef VectorBConstant,
362                                     LLVMValueRef MaskConstant);
363
364 /* Operations on global variables, functions, and aliases (globals) */
365 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global);
366 int LLVMIsDeclaration(LLVMValueRef Global);
367 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global);
368 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage);
369 const char *LLVMGetSection(LLVMValueRef Global);
370 void LLVMSetSection(LLVMValueRef Global, const char *Section);
371 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global);
372 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz);
373 unsigned LLVMGetAlignment(LLVMValueRef Global);
374 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes);
375
376 /* Operations on global variables */
377 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name);
378 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name);
379 void LLVMDeleteGlobal(LLVMValueRef GlobalVar);
380 int LLVMHasInitializer(LLVMValueRef GlobalVar);
381 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar);
382 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal);
383 int LLVMIsThreadLocal(LLVMValueRef GlobalVar);
384 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal);
385 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar);
386 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant);
387
388 /* Operations on functions */
389 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
390                              LLVMTypeRef FunctionTy);
391 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name);
392 void LLVMDeleteFunction(LLVMValueRef Fn);
393 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn);
394 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn);
395 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC);
396 const char *LLVMGetCollector(LLVMValueRef Fn);
397 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll);
398
399 /* Operations on parameters */
400 unsigned LLVMCountParams(LLVMValueRef Fn);
401 void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
402 LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
403 LLVMValueRef LLVMGetParamParent(LLVMValueRef Inst);
404
405 /* Operations on basic blocks */
406 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb);
407 int LLVMValueIsBasicBlock(LLVMValueRef Val);
408 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val);
409 LLVMValueRef LLVMGetBasicBlockParent(LLVMValueRef V);
410 unsigned LLVMCountBasicBlocks(LLVMValueRef Fn);
411 void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks);
412 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn);
413 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef Fn, const char *Name);
414 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBB,
415                                        const char *Name);
416 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BB);
417
418 /* Operations on instructions */
419 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst);
420
421 /* Operations on call sites */
422 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC);
423 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr);
424
425 /* Operations on phi nodes */
426 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
427                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count);
428 unsigned LLVMCountIncoming(LLVMValueRef PhiNode);
429 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index);
430 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index);
431
432 /*===-- Instruction builders ----------------------------------------------===*/
433
434 /* An instruction builder represents a point within a basic block, and is the
435  * exclusive means of building instructions using the C interface.
436  */
437
438 LLVMBuilderRef LLVMCreateBuilder();
439 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
440 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
441 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
442 void LLVMDisposeBuilder(LLVMBuilderRef Builder);
443
444 /* Terminators */
445 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef);
446 LLVMValueRef LLVMBuildRet(LLVMBuilderRef, LLVMValueRef V);
447 LLVMValueRef LLVMBuildBr(LLVMBuilderRef, LLVMBasicBlockRef Dest);
448 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef, LLVMValueRef If,
449                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else);
450 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef, LLVMValueRef V,
451                              LLVMBasicBlockRef Else, unsigned NumCases);
452 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef, LLVMValueRef Fn,
453                              LLVMValueRef *Args, unsigned NumArgs,
454                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
455                              const char *Name);
456 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef);
457 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef);
458
459 /* Add a case to the switch instruction */
460 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
461                  LLVMBasicBlockRef Dest);
462
463 /* Arithmetic */
464 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
465                           const char *Name);
466 LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
467                           const char *Name);
468 LLVMValueRef LLVMBuildMul(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
469                           const char *Name);
470 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
471                            const char *Name);
472 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
473                            const char *Name);
474 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
475                            const char *Name);
476 LLVMValueRef LLVMBuildURem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
477                            const char *Name);
478 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
479                            const char *Name);
480 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
481                            const char *Name);
482 LLVMValueRef LLVMBuildShl(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
483                            const char *Name);
484 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
485                            const char *Name);
486 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
487                            const char *Name);
488 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
489                           const char *Name);
490 LLVMValueRef LLVMBuildOr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
491                           const char *Name);
492 LLVMValueRef LLVMBuildXor(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
493                           const char *Name);
494 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
495 LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name);
496
497 /* Memory */
498 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
499 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,
500                                   LLVMValueRef Val, const char *Name);
501 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
502 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
503                                   LLVMValueRef Val, const char *Name);
504 LLVMValueRef LLVMBuildFree(LLVMBuilderRef, LLVMValueRef PointerVal);
505 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef, LLVMValueRef PointerVal,
506                            const char *Name);
507 LLVMValueRef LLVMBuildStore(LLVMBuilderRef, LLVMValueRef Val, LLVMValueRef Ptr);
508 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
509                           LLVMValueRef *Indices, unsigned NumIndices,
510                           const char *Name);
511
512 /* Casts */
513 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef, LLVMValueRef Val,
514                             LLVMTypeRef DestTy, const char *Name);
515 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef, LLVMValueRef Val,
516                            LLVMTypeRef DestTy, const char *Name);
517 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef, LLVMValueRef Val,
518                            LLVMTypeRef DestTy, const char *Name);
519 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef, LLVMValueRef Val,
520                              LLVMTypeRef DestTy, const char *Name);
521 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef, LLVMValueRef Val,
522                              LLVMTypeRef DestTy, const char *Name);
523 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef, LLVMValueRef Val,
524                              LLVMTypeRef DestTy, const char *Name);
525 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef, LLVMValueRef Val,
526                              LLVMTypeRef DestTy, const char *Name);
527 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef, LLVMValueRef Val,
528                               LLVMTypeRef DestTy, const char *Name);
529 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef, LLVMValueRef Val,
530                             LLVMTypeRef DestTy, const char *Name);
531 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef, LLVMValueRef Val,
532                                LLVMTypeRef DestTy, const char *Name);
533 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef, LLVMValueRef Val,
534                                LLVMTypeRef DestTy, const char *Name);
535 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef, LLVMValueRef Val,
536                               LLVMTypeRef DestTy, const char *Name);
537
538 /* Comparisons */
539 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef, LLVMIntPredicate Op,
540                            LLVMValueRef LHS, LLVMValueRef RHS,
541                            const char *Name);
542 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef, LLVMRealPredicate Op,
543                            LLVMValueRef LHS, LLVMValueRef RHS,
544                            const char *Name);
545
546 /* Miscellaneous instructions */
547 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
548 LLVMValueRef LLVMBuildCall(LLVMBuilderRef, LLVMValueRef Fn,
549                            LLVMValueRef *Args, unsigned NumArgs,
550                            const char *Name);
551 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If,
552                              LLVMValueRef Then, LLVMValueRef Else,
553                              const char *Name);
554 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef, LLVMValueRef List, LLVMTypeRef Ty,
555                             const char *Name);
556 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef, LLVMValueRef VecVal,
557                                      LLVMValueRef Index, const char *Name);
558 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef, LLVMValueRef VecVal,
559                                     LLVMValueRef EltVal, LLVMValueRef Index,
560                                     const char *Name);
561 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef, LLVMValueRef V1,
562                                     LLVMValueRef V2, LLVMValueRef Mask,
563                                     const char *Name);
564
565
566 /*===-- Module providers --------------------------------------------------===*/
567
568 /* Encapsulates the module M in a module provider, taking ownership of the
569  * module.
570  * See the constructor llvm::ExistingModuleProvider::ExistingModuleProvider.
571  */
572 LLVMModuleProviderRef
573 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M);
574
575 /* Destroys the module provider MP as well as the contained module.
576  * See the destructor llvm::ModuleProvider::~ModuleProvider.
577  */
578 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP);
579
580
581 /*===-- Memory buffers ----------------------------------------------------===*/
582
583 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
584                                              LLVMMemoryBufferRef *OutMemBuf,
585                                              char **OutMessage);
586 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
587                                     char **OutMessage);
588 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
589
590
591 /*===-- Pass Managers -----------------------------------------------------===*/
592
593 /** Constructs a new whole-module pass pipeline. This type of pipeline is
594     suitable for link-time optimization and whole-module transformations.
595     See llvm::PassManager::PassManager. */
596 LLVMPassManagerRef LLVMCreatePassManager();
597
598 /** Constructs a new function-by-function pass pipeline over the module
599     provider. It does not take ownership of the module provider. This type of
600     pipeline is suitable for code generation and JIT compilation tasks.
601     See llvm::FunctionPassManager::FunctionPassManager. */
602 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef MP);
603
604 /** Initializes, executes on the provided module, and finalizes all of the
605     passes scheduled in the pass manager. Returns 1 if any of the passes
606     modified the module, 0 otherwise. See llvm::PassManager::run(Module&). */
607 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M);
608
609 /** Initializes all of the function passes scheduled in the function pass
610     manager. Returns 1 if any of the passes modified the module, 0 otherwise.
611     See llvm::FunctionPassManager::doInitialization. */
612 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM);
613
614 /** Executes all of the function passes scheduled in the function pass manager
615     on the provided function. Returns 1 if any of the passes modified the
616     function, false otherwise.
617     See llvm::FunctionPassManager::run(Function&). */
618 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F);
619
620 /** Finalizes all of the function passes scheduled in in the function pass
621     manager. Returns 1 if any of the passes modified the module, 0 otherwise.
622     See llvm::FunctionPassManager::doFinalization. */
623 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM);
624
625 /** Frees the memory of a pass pipeline. For function pipelines, does not free
626     the module provider.
627     See llvm::PassManagerBase::~PassManagerBase. */
628 void LLVMDisposePassManager(LLVMPassManagerRef PM);
629
630
631 #ifdef __cplusplus
632 }
633
634 namespace llvm {
635   class ModuleProvider;
636   class MemoryBuffer;
637   class PassManagerBase;
638   
639   #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)   \
640     inline ty *unwrap(ref P) {                          \
641       return reinterpret_cast<ty*>(P);                  \
642     }                                                   \
643                                                         \
644     inline ref wrap(const ty *P) {                      \
645       return reinterpret_cast<ref>(const_cast<ty*>(P)); \
646     }
647   
648   #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)  \
649     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
650                                                         \
651     template<typename T>                                \
652     inline T *unwrap(ref P) {                           \
653       return cast<T>(unwrap(P));                        \
654     }
655   
656   #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref)   \
657     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
658                                                         \
659     template<typename T>                                \
660     inline T *unwrap(ref P) {                           \
661       T *Q = dynamic_cast<T*>(unwrap(P));               \
662       assert(Q && "Invalid cast!");                     \
663       return Q;                                         \
664     }
665   
666   DEFINE_ISA_CONVERSION_FUNCTIONS   (Type,               LLVMTypeRef          )
667   DEFINE_ISA_CONVERSION_FUNCTIONS   (Value,              LLVMValueRef         )
668   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module,             LLVMModuleRef        )
669   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock,         LLVMBasicBlockRef    )
670   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMFoldingBuilder, LLVMBuilderRef       )
671   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder,       LLVMTypeHandleRef    )
672   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider,     LLVMModuleProviderRef)
673   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer,       LLVMMemoryBufferRef  )
674   DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase,    LLVMPassManagerRef   )
675   
676   #undef DEFINE_STDCXX_CONVERSION_FUNCTIONS
677   #undef DEFINE_ISA_CONVERSION_FUNCTIONS
678   #undef DEFINE_SIMPLE_CONVERSION_FUNCTIONS
679   
680   /* Specialized opaque type conversions.
681    */
682   inline Type **unwrap(LLVMTypeRef* Tys) {
683     return reinterpret_cast<Type**>(Tys);
684   }
685   
686   inline LLVMTypeRef *wrap(const Type **Tys) {
687     return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
688   }
689   
690   /* Specialized opaque value conversions.
691    */ 
692   inline Value **unwrap(LLVMValueRef *Vals) {
693     return reinterpret_cast<Value**>(Vals);
694   }
695   
696   template<typename T>
697   inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
698     #if DEBUG
699     for (LLVMValueRef *I = Vals, E = Vals + Length; I != E; ++I)
700       cast<T>(*I);
701     #endif
702     return reinterpret_cast<T**>(Vals);
703   }
704   
705   inline LLVMValueRef *wrap(const Value **Vals) {
706     return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
707   }
708 }
709
710 #endif /* !defined(__cplusplus) */
711
712 #endif /* !defined(LLVM_C_CORE_H) */