Merge LLVMBuilder and FoldingBuilder, calling
[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/IRBuilder.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 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M);
380 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M);
381 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar);
382 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar);
383 void LLVMDeleteGlobal(LLVMValueRef GlobalVar);
384 int LLVMHasInitializer(LLVMValueRef GlobalVar);
385 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar);
386 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal);
387 int LLVMIsThreadLocal(LLVMValueRef GlobalVar);
388 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal);
389 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar);
390 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant);
391
392 /* Operations on functions */
393 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
394                              LLVMTypeRef FunctionTy);
395 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name);
396 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M);
397 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M);
398 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn);
399 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn);
400 void LLVMDeleteFunction(LLVMValueRef Fn);
401 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn);
402 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn);
403 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC);
404 const char *LLVMGetCollector(LLVMValueRef Fn);
405 void LLVMSetCollector(LLVMValueRef Fn, const char *Coll);
406
407 /* Operations on parameters */
408 unsigned LLVMCountParams(LLVMValueRef Fn);
409 void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
410 LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
411 LLVMValueRef LLVMGetParamParent(LLVMValueRef Inst);
412 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn);
413 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn);
414 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg);
415 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg);
416
417 /* Operations on basic blocks */
418 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB);
419 int LLVMValueIsBasicBlock(LLVMValueRef Val);
420 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val);
421 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB);
422 unsigned LLVMCountBasicBlocks(LLVMValueRef Fn);
423 void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks);
424 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn);
425 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn);
426 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB);
427 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB);
428 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn);
429 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef Fn, const char *Name);
430 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBB,
431                                        const char *Name);
432 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BB);
433
434 /* Operations on instructions */
435 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst);
436 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB);
437 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB);
438 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst);
439 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst);
440
441 /* Operations on call sites */
442 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC);
443 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr);
444
445 /* Operations on phi nodes */
446 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
447                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count);
448 unsigned LLVMCountIncoming(LLVMValueRef PhiNode);
449 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index);
450 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index);
451
452 /*===-- Instruction builders ----------------------------------------------===*/
453
454 /* An instruction builder represents a point within a basic block, and is the
455  * exclusive means of building instructions using the C interface.
456  */
457
458 LLVMBuilderRef LLVMCreateBuilder();
459 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
460                          LLVMValueRef Instr);
461 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
462 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
463 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
464 void LLVMDisposeBuilder(LLVMBuilderRef Builder);
465
466 /* Terminators */
467 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef);
468 LLVMValueRef LLVMBuildRet(LLVMBuilderRef, LLVMValueRef V);
469 LLVMValueRef LLVMBuildBr(LLVMBuilderRef, LLVMBasicBlockRef Dest);
470 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef, LLVMValueRef If,
471                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else);
472 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef, LLVMValueRef V,
473                              LLVMBasicBlockRef Else, unsigned NumCases);
474 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef, LLVMValueRef Fn,
475                              LLVMValueRef *Args, unsigned NumArgs,
476                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
477                              const char *Name);
478 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef);
479 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef);
480
481 /* Add a case to the switch instruction */
482 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
483                  LLVMBasicBlockRef Dest);
484
485 /* Arithmetic */
486 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
487                           const char *Name);
488 LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
489                           const char *Name);
490 LLVMValueRef LLVMBuildMul(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
491                           const char *Name);
492 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
493                            const char *Name);
494 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
495                            const char *Name);
496 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
497                            const char *Name);
498 LLVMValueRef LLVMBuildURem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
499                            const char *Name);
500 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
501                            const char *Name);
502 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
503                            const char *Name);
504 LLVMValueRef LLVMBuildShl(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
505                            const char *Name);
506 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
507                            const char *Name);
508 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
509                            const char *Name);
510 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
511                           const char *Name);
512 LLVMValueRef LLVMBuildOr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
513                           const char *Name);
514 LLVMValueRef LLVMBuildXor(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
515                           const char *Name);
516 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
517 LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name);
518
519 /* Memory */
520 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
521 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,
522                                   LLVMValueRef Val, const char *Name);
523 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
524 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
525                                   LLVMValueRef Val, const char *Name);
526 LLVMValueRef LLVMBuildFree(LLVMBuilderRef, LLVMValueRef PointerVal);
527 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef, LLVMValueRef PointerVal,
528                            const char *Name);
529 LLVMValueRef LLVMBuildStore(LLVMBuilderRef, LLVMValueRef Val, LLVMValueRef Ptr);
530 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
531                           LLVMValueRef *Indices, unsigned NumIndices,
532                           const char *Name);
533
534 /* Casts */
535 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef, LLVMValueRef Val,
536                             LLVMTypeRef DestTy, const char *Name);
537 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef, LLVMValueRef Val,
538                            LLVMTypeRef DestTy, const char *Name);
539 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef, LLVMValueRef Val,
540                            LLVMTypeRef DestTy, const char *Name);
541 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef, LLVMValueRef Val,
542                              LLVMTypeRef DestTy, const char *Name);
543 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef, LLVMValueRef Val,
544                              LLVMTypeRef DestTy, const char *Name);
545 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef, LLVMValueRef Val,
546                              LLVMTypeRef DestTy, const char *Name);
547 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef, LLVMValueRef Val,
548                              LLVMTypeRef DestTy, const char *Name);
549 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef, LLVMValueRef Val,
550                               LLVMTypeRef DestTy, const char *Name);
551 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef, LLVMValueRef Val,
552                             LLVMTypeRef DestTy, const char *Name);
553 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef, LLVMValueRef Val,
554                                LLVMTypeRef DestTy, const char *Name);
555 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef, LLVMValueRef Val,
556                                LLVMTypeRef DestTy, const char *Name);
557 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef, LLVMValueRef Val,
558                               LLVMTypeRef DestTy, const char *Name);
559
560 /* Comparisons */
561 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef, LLVMIntPredicate Op,
562                            LLVMValueRef LHS, LLVMValueRef RHS,
563                            const char *Name);
564 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef, LLVMRealPredicate Op,
565                            LLVMValueRef LHS, LLVMValueRef RHS,
566                            const char *Name);
567
568 /* Miscellaneous instructions */
569 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
570 LLVMValueRef LLVMBuildCall(LLVMBuilderRef, LLVMValueRef Fn,
571                            LLVMValueRef *Args, unsigned NumArgs,
572                            const char *Name);
573 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If,
574                              LLVMValueRef Then, LLVMValueRef Else,
575                              const char *Name);
576 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef, LLVMValueRef List, LLVMTypeRef Ty,
577                             const char *Name);
578 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef, LLVMValueRef VecVal,
579                                      LLVMValueRef Index, const char *Name);
580 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef, LLVMValueRef VecVal,
581                                     LLVMValueRef EltVal, LLVMValueRef Index,
582                                     const char *Name);
583 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef, LLVMValueRef V1,
584                                     LLVMValueRef V2, LLVMValueRef Mask,
585                                     const char *Name);
586
587
588 /*===-- Module providers --------------------------------------------------===*/
589
590 /* Encapsulates the module M in a module provider, taking ownership of the
591  * module.
592  * See the constructor llvm::ExistingModuleProvider::ExistingModuleProvider.
593  */
594 LLVMModuleProviderRef
595 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M);
596
597 /* Destroys the module provider MP as well as the contained module.
598  * See the destructor llvm::ModuleProvider::~ModuleProvider.
599  */
600 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP);
601
602
603 /*===-- Memory buffers ----------------------------------------------------===*/
604
605 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
606                                              LLVMMemoryBufferRef *OutMemBuf,
607                                              char **OutMessage);
608 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
609                                     char **OutMessage);
610 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
611
612
613 /*===-- Pass Managers -----------------------------------------------------===*/
614
615 /** Constructs a new whole-module pass pipeline. This type of pipeline is
616     suitable for link-time optimization and whole-module transformations.
617     See llvm::PassManager::PassManager. */
618 LLVMPassManagerRef LLVMCreatePassManager();
619
620 /** Constructs a new function-by-function pass pipeline over the module
621     provider. It does not take ownership of the module provider. This type of
622     pipeline is suitable for code generation and JIT compilation tasks.
623     See llvm::FunctionPassManager::FunctionPassManager. */
624 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef MP);
625
626 /** Initializes, executes on the provided module, and finalizes all of the
627     passes scheduled in the pass manager. Returns 1 if any of the passes
628     modified the module, 0 otherwise. See llvm::PassManager::run(Module&). */
629 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M);
630
631 /** Initializes all of the function passes scheduled in the function pass
632     manager. Returns 1 if any of the passes modified the module, 0 otherwise.
633     See llvm::FunctionPassManager::doInitialization. */
634 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM);
635
636 /** Executes all of the function passes scheduled in the function pass manager
637     on the provided function. Returns 1 if any of the passes modified the
638     function, false otherwise.
639     See llvm::FunctionPassManager::run(Function&). */
640 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F);
641
642 /** Finalizes all of the function passes scheduled in in the function pass
643     manager. Returns 1 if any of the passes modified the module, 0 otherwise.
644     See llvm::FunctionPassManager::doFinalization. */
645 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM);
646
647 /** Frees the memory of a pass pipeline. For function pipelines, does not free
648     the module provider.
649     See llvm::PassManagerBase::~PassManagerBase. */
650 void LLVMDisposePassManager(LLVMPassManagerRef PM);
651
652
653 #ifdef __cplusplus
654 }
655
656 namespace llvm {
657   class ModuleProvider;
658   class MemoryBuffer;
659   class PassManagerBase;
660   
661   #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)   \
662     inline ty *unwrap(ref P) {                          \
663       return reinterpret_cast<ty*>(P);                  \
664     }                                                   \
665                                                         \
666     inline ref wrap(const ty *P) {                      \
667       return reinterpret_cast<ref>(const_cast<ty*>(P)); \
668     }
669   
670   #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)  \
671     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
672                                                         \
673     template<typename T>                                \
674     inline T *unwrap(ref P) {                           \
675       return cast<T>(unwrap(P));                        \
676     }
677   
678   #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref)   \
679     DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
680                                                         \
681     template<typename T>                                \
682     inline T *unwrap(ref P) {                           \
683       T *Q = dynamic_cast<T*>(unwrap(P));               \
684       assert(Q && "Invalid cast!");                     \
685       return Q;                                         \
686     }
687   
688   DEFINE_ISA_CONVERSION_FUNCTIONS   (Type,               LLVMTypeRef          )
689   DEFINE_ISA_CONVERSION_FUNCTIONS   (Value,              LLVMValueRef         )
690   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module,             LLVMModuleRef        )
691   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock,         LLVMBasicBlockRef    )
692   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder,          LLVMBuilderRef       )
693   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder,       LLVMTypeHandleRef    )
694   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider,     LLVMModuleProviderRef)
695   DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer,       LLVMMemoryBufferRef  )
696   DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase,    LLVMPassManagerRef   )
697   
698   #undef DEFINE_STDCXX_CONVERSION_FUNCTIONS
699   #undef DEFINE_ISA_CONVERSION_FUNCTIONS
700   #undef DEFINE_SIMPLE_CONVERSION_FUNCTIONS
701   
702   /* Specialized opaque type conversions.
703    */
704   inline Type **unwrap(LLVMTypeRef* Tys) {
705     return reinterpret_cast<Type**>(Tys);
706   }
707   
708   inline LLVMTypeRef *wrap(const Type **Tys) {
709     return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
710   }
711   
712   /* Specialized opaque value conversions.
713    */ 
714   inline Value **unwrap(LLVMValueRef *Vals) {
715     return reinterpret_cast<Value**>(Vals);
716   }
717   
718   template<typename T>
719   inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
720     #if DEBUG
721     for (LLVMValueRef *I = Vals, E = Vals + Length; I != E; ++I)
722       cast<T>(*I);
723     #endif
724     return reinterpret_cast<T**>(Vals);
725   }
726   
727   inline LLVMValueRef *wrap(const Value **Vals) {
728     return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
729   }
730 }
731
732 #endif /* !defined(__cplusplus) */
733
734 #endif /* !defined(LLVM_C_CORE_H) */