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