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