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