Core.h revision 193323
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
43extern "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 */
53typedef struct LLVMOpaqueModule *LLVMModuleRef;
54
55/**
56 * Each value in the LLVM IR has a type, an LLVMTypeRef. See the llvm::Type
57 * class.
58 */
59typedef 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 */
66typedef struct LLVMOpaqueTypeHandle *LLVMTypeHandleRef;
67
68typedef struct LLVMOpaqueValue *LLVMValueRef;
69typedef struct LLVMOpaqueBasicBlock *LLVMBasicBlockRef;
70typedef struct LLVMOpaqueBuilder *LLVMBuilderRef;
71
72/* Used to provide a module to JIT or interpreter.
73 * See the llvm::ModuleProvider class.
74 */
75typedef struct LLVMOpaqueModuleProvider *LLVMModuleProviderRef;
76
77/* Used to provide a module to JIT or interpreter.
78 * See the llvm::MemoryBuffer class.
79 */
80typedef struct LLVMOpaqueMemoryBuffer *LLVMMemoryBufferRef;
81
82/** See the llvm::PassManagerBase class. */
83typedef struct LLVMOpaquePassManager *LLVMPassManagerRef;
84
85typedef enum {
86    LLVMZExtAttribute       = 1<<0,
87    LLVMSExtAttribute       = 1<<1,
88    LLVMNoReturnAttribute   = 1<<2,
89    LLVMInRegAttribute      = 1<<3,
90    LLVMStructRetAttribute  = 1<<4,
91    LLVMNoUnwindAttribute   = 1<<5,
92    LLVMNoAliasAttribute    = 1<<6,
93    LLVMByValAttribute      = 1<<7,
94    LLVMNestAttribute       = 1<<8,
95    LLVMReadNoneAttribute   = 1<<9,
96    LLVMReadOnlyAttribute   = 1<<10
97} LLVMAttribute;
98
99typedef 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
116typedef enum {
117  LLVMExternalLinkage,    /**< Externally visible function */
118  LLVMAvailableExternallyLinkage,
119  LLVMLinkOnceAnyLinkage, /**< Keep one copy of function when linking (inline)*/
120  LLVMLinkOnceODRLinkage, /**< Same, but only replaced by something
121                            equivalent. */
122  LLVMWeakAnyLinkage,     /**< Keep one copy of function when linking (weak) */
123  LLVMWeakODRLinkage,     /**< Same, but only replaced by something
124                            equivalent. */
125  LLVMAppendingLinkage,   /**< Special purpose, only applies to global arrays */
126  LLVMInternalLinkage,    /**< Rename collisions when linking (static
127                               functions) */
128  LLVMPrivateLinkage,     /**< Like Internal, but omit from symbol table */
129  LLVMDLLImportLinkage,   /**< Function to be imported from DLL */
130  LLVMDLLExportLinkage,   /**< Function to be accessible from DLL */
131  LLVMExternalWeakLinkage,/**< ExternalWeak linkage description */
132  LLVMGhostLinkage,       /**< Stand-in functions for streaming fns from
133                               bitcode */
134  LLVMCommonLinkage       /**< Tentative definitions */
135} LLVMLinkage;
136
137typedef enum {
138  LLVMDefaultVisibility,  /**< The GV is visible */
139  LLVMHiddenVisibility,   /**< The GV is hidden */
140  LLVMProtectedVisibility /**< The GV is protected */
141} LLVMVisibility;
142
143typedef enum {
144  LLVMCCallConv           = 0,
145  LLVMFastCallConv        = 8,
146  LLVMColdCallConv        = 9,
147  LLVMX86StdcallCallConv  = 64,
148  LLVMX86FastcallCallConv = 65
149} LLVMCallConv;
150
151typedef enum {
152  LLVMIntEQ = 32, /**< equal */
153  LLVMIntNE,      /**< not equal */
154  LLVMIntUGT,     /**< unsigned greater than */
155  LLVMIntUGE,     /**< unsigned greater or equal */
156  LLVMIntULT,     /**< unsigned less than */
157  LLVMIntULE,     /**< unsigned less or equal */
158  LLVMIntSGT,     /**< signed greater than */
159  LLVMIntSGE,     /**< signed greater or equal */
160  LLVMIntSLT,     /**< signed less than */
161  LLVMIntSLE      /**< signed less or equal */
162} LLVMIntPredicate;
163
164typedef enum {
165  LLVMRealPredicateFalse, /**< Always false (always folded) */
166  LLVMRealOEQ,            /**< True if ordered and equal */
167  LLVMRealOGT,            /**< True if ordered and greater than */
168  LLVMRealOGE,            /**< True if ordered and greater than or equal */
169  LLVMRealOLT,            /**< True if ordered and less than */
170  LLVMRealOLE,            /**< True if ordered and less than or equal */
171  LLVMRealONE,            /**< True if ordered and operands are unequal */
172  LLVMRealORD,            /**< True if ordered (no nans) */
173  LLVMRealUNO,            /**< True if unordered: isnan(X) | isnan(Y) */
174  LLVMRealUEQ,            /**< True if unordered or equal */
175  LLVMRealUGT,            /**< True if unordered or greater than */
176  LLVMRealUGE,            /**< True if unordered, greater than, or equal */
177  LLVMRealULT,            /**< True if unordered or less than */
178  LLVMRealULE,            /**< True if unordered, less than, or equal */
179  LLVMRealUNE,            /**< True if unordered or not equal */
180  LLVMRealPredicateTrue   /**< Always true (always folded) */
181} LLVMRealPredicate;
182
183
184/*===-- Error handling ----------------------------------------------------===*/
185
186void LLVMDisposeMessage(char *Message);
187
188
189/*===-- Modules -----------------------------------------------------------===*/
190
191/* Create and destroy modules. */
192/** See llvm::Module::Module. */
193LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID);
194
195/** See llvm::Module::~Module. */
196void LLVMDisposeModule(LLVMModuleRef M);
197
198/** Data layout. See Module::getDataLayout. */
199const char *LLVMGetDataLayout(LLVMModuleRef M);
200void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple);
201
202/** Target triple. See Module::getTargetTriple. */
203const char *LLVMGetTarget(LLVMModuleRef M);
204void LLVMSetTarget(LLVMModuleRef M, const char *Triple);
205
206/** See Module::addTypeName. */
207int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty);
208void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name);
209
210/** See Module::dump. */
211void LLVMDumpModule(LLVMModuleRef M);
212
213
214/*===-- Types -------------------------------------------------------------===*/
215
216/* LLVM types conform to the following hierarchy:
217 *
218 *   types:
219 *     integer type
220 *     real type
221 *     function type
222 *     sequence types:
223 *       array type
224 *       pointer type
225 *       vector type
226 *     void type
227 *     label type
228 *     opaque type
229 */
230
231/** See llvm::LLVMTypeKind::getTypeID. */
232LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty);
233
234/* Operations on integer types */
235LLVMTypeRef LLVMInt1Type(void);
236LLVMTypeRef LLVMInt8Type(void);
237LLVMTypeRef LLVMInt16Type(void);
238LLVMTypeRef LLVMInt32Type(void);
239LLVMTypeRef LLVMInt64Type(void);
240LLVMTypeRef LLVMIntType(unsigned NumBits);
241unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy);
242
243/* Operations on real types */
244LLVMTypeRef LLVMFloatType(void);
245LLVMTypeRef LLVMDoubleType(void);
246LLVMTypeRef LLVMX86FP80Type(void);
247LLVMTypeRef LLVMFP128Type(void);
248LLVMTypeRef LLVMPPCFP128Type(void);
249
250/* Operations on function types */
251LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
252                             LLVMTypeRef *ParamTypes, unsigned ParamCount,
253                             int IsVarArg);
254int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy);
255LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy);
256unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy);
257void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest);
258
259/* Operations on struct types */
260LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, unsigned ElementCount,
261                           int Packed);
262unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy);
263void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest);
264int LLVMIsPackedStruct(LLVMTypeRef StructTy);
265
266/* Operations on array, pointer, and vector types (sequence types) */
267LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount);
268LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace);
269LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount);
270
271LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty);
272unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy);
273unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy);
274unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy);
275
276/* Operations on other types */
277LLVMTypeRef LLVMVoidType(void);
278LLVMTypeRef LLVMLabelType(void);
279LLVMTypeRef LLVMOpaqueType(void);
280
281/* Operations on type handles */
282LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy);
283void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy);
284LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle);
285void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle);
286
287
288/*===-- Values ------------------------------------------------------------===*/
289
290/* The bulk of LLVM's object model consists of values, which comprise a very
291 * rich type hierarchy.
292 */
293
294#define LLVM_FOR_EACH_VALUE_SUBCLASS(macro) \
295  macro(Argument)                           \
296  macro(BasicBlock)                         \
297  macro(InlineAsm)                          \
298  macro(User)                               \
299    macro(Constant)                         \
300      macro(ConstantAggregateZero)          \
301      macro(ConstantArray)                  \
302      macro(ConstantExpr)                   \
303      macro(ConstantFP)                     \
304      macro(ConstantInt)                    \
305      macro(ConstantPointerNull)            \
306      macro(ConstantStruct)                 \
307      macro(ConstantVector)                 \
308      macro(GlobalValue)                    \
309        macro(Function)                     \
310        macro(GlobalAlias)                  \
311        macro(GlobalVariable)               \
312      macro(UndefValue)                     \
313    macro(Instruction)                      \
314      macro(BinaryOperator)                 \
315      macro(CallInst)                       \
316        macro(IntrinsicInst)                \
317          macro(DbgInfoIntrinsic)           \
318            macro(DbgDeclareInst)           \
319            macro(DbgFuncStartInst)         \
320            macro(DbgRegionEndInst)         \
321            macro(DbgRegionStartInst)       \
322            macro(DbgStopPointInst)         \
323          macro(EHSelectorInst)             \
324          macro(MemIntrinsic)               \
325            macro(MemCpyInst)               \
326            macro(MemMoveInst)              \
327            macro(MemSetInst)               \
328      macro(CmpInst)                        \
329      macro(FCmpInst)                       \
330      macro(ICmpInst)                       \
331      macro(VFCmpInst)                      \
332      macro(VICmpInst)                      \
333      macro(ExtractElementInst)             \
334      macro(GetElementPtrInst)              \
335      macro(InsertElementInst)              \
336      macro(InsertValueInst)                \
337      macro(PHINode)                        \
338      macro(SelectInst)                     \
339      macro(ShuffleVectorInst)              \
340      macro(StoreInst)                      \
341      macro(TerminatorInst)                 \
342        macro(BranchInst)                   \
343        macro(InvokeInst)                   \
344        macro(ReturnInst)                   \
345        macro(SwitchInst)                   \
346        macro(UnreachableInst)              \
347        macro(UnwindInst)                   \
348    macro(UnaryInstruction)                 \
349      macro(AllocationInst)                 \
350        macro(AllocaInst)                   \
351        macro(MallocInst)                   \
352      macro(CastInst)                       \
353        macro(BitCastInst)                  \
354        macro(FPExtInst)                    \
355        macro(FPToSIInst)                   \
356        macro(FPToUIInst)                   \
357        macro(FPTruncInst)                  \
358        macro(IntToPtrInst)                 \
359        macro(PtrToIntInst)                 \
360        macro(SExtInst)                     \
361        macro(SIToFPInst)                   \
362        macro(TruncInst)                    \
363        macro(UIToFPInst)                   \
364        macro(ZExtInst)                     \
365      macro(ExtractValueInst)               \
366      macro(FreeInst)                       \
367      macro(LoadInst)                       \
368      macro(VAArgInst)
369
370/* Operations on all values */
371LLVMTypeRef LLVMTypeOf(LLVMValueRef Val);
372const char *LLVMGetValueName(LLVMValueRef Val);
373void LLVMSetValueName(LLVMValueRef Val, const char *Name);
374void LLVMDumpValue(LLVMValueRef Val);
375
376/* Conversion functions. Return the input value if it is an instance of the
377   specified class, otherwise NULL. See llvm::dyn_cast_or_null<>. */
378#define LLVM_DECLARE_VALUE_CAST(name) \
379  LLVMValueRef LLVMIsA##name(LLVMValueRef Val);
380LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST)
381
382/* Operations on constants of any type */
383LLVMValueRef LLVMConstNull(LLVMTypeRef Ty); /* all zeroes */
384LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty); /* only for int/vector */
385LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty);
386int LLVMIsConstant(LLVMValueRef Val);
387int LLVMIsNull(LLVMValueRef Val);
388int LLVMIsUndef(LLVMValueRef Val);
389
390/* Operations on scalar constants */
391LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
392                          int SignExtend);
393LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N);
394LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text);
395
396/* Operations on composite constants */
397LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
398                             int DontNullTerminate);
399LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
400                            LLVMValueRef *ConstantVals, unsigned Length);
401LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
402                             int packed);
403LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);
404
405/* Constant expressions */
406LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty);
407LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal);
408LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal);
409LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
410LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
411LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
412LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
413LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
414LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
415LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
416LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
417LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
418LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
419LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
420LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
421LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
422                           LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
423LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
424                           LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
425LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
426LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
427LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant);
428LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
429                          LLVMValueRef *ConstantIndices, unsigned NumIndices);
430LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
431LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
432LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
433LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
434LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
435LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
436LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
437LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
438LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
439LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
440LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
441LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType);
442LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
443                             LLVMValueRef ConstantIfTrue,
444                             LLVMValueRef ConstantIfFalse);
445LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
446                                     LLVMValueRef IndexConstant);
447LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
448                                    LLVMValueRef ElementValueConstant,
449                                    LLVMValueRef IndexConstant);
450LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
451                                    LLVMValueRef VectorBConstant,
452                                    LLVMValueRef MaskConstant);
453LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
454                                   unsigned NumIdx);
455LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
456                                  LLVMValueRef ElementValueConstant,
457                                  unsigned *IdxList, unsigned NumIdx);
458LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty,
459                                const char *AsmString, const char *Constraints,
460                                int HasSideEffects);
461
462/* Operations on global variables, functions, and aliases (globals) */
463LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global);
464int LLVMIsDeclaration(LLVMValueRef Global);
465LLVMLinkage LLVMGetLinkage(LLVMValueRef Global);
466void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage);
467const char *LLVMGetSection(LLVMValueRef Global);
468void LLVMSetSection(LLVMValueRef Global, const char *Section);
469LLVMVisibility LLVMGetVisibility(LLVMValueRef Global);
470void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz);
471unsigned LLVMGetAlignment(LLVMValueRef Global);
472void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes);
473
474/* Operations on global variables */
475LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name);
476LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name);
477LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M);
478LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M);
479LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar);
480LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar);
481void LLVMDeleteGlobal(LLVMValueRef GlobalVar);
482LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar);
483void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal);
484int LLVMIsThreadLocal(LLVMValueRef GlobalVar);
485void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal);
486int LLVMIsGlobalConstant(LLVMValueRef GlobalVar);
487void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant);
488
489/* Operations on aliases */
490LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
491                          const char *Name);
492
493/* Operations on functions */
494LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
495                             LLVMTypeRef FunctionTy);
496LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name);
497LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M);
498LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M);
499LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn);
500LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn);
501void LLVMDeleteFunction(LLVMValueRef Fn);
502unsigned LLVMGetIntrinsicID(LLVMValueRef Fn);
503unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn);
504void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC);
505const char *LLVMGetGC(LLVMValueRef Fn);
506void LLVMSetGC(LLVMValueRef Fn, const char *Name);
507void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA);
508void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA);
509
510/* Operations on parameters */
511unsigned LLVMCountParams(LLVMValueRef Fn);
512void LLVMGetParams(LLVMValueRef Fn, LLVMValueRef *Params);
513LLVMValueRef LLVMGetParam(LLVMValueRef Fn, unsigned Index);
514LLVMValueRef LLVMGetParamParent(LLVMValueRef Inst);
515LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn);
516LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn);
517LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg);
518LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg);
519void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA);
520void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA);
521void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align);
522
523/* Operations on basic blocks */
524LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB);
525int LLVMValueIsBasicBlock(LLVMValueRef Val);
526LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val);
527LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB);
528unsigned LLVMCountBasicBlocks(LLVMValueRef Fn);
529void LLVMGetBasicBlocks(LLVMValueRef Fn, LLVMBasicBlockRef *BasicBlocks);
530LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn);
531LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn);
532LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB);
533LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB);
534LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn);
535LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef Fn, const char *Name);
536LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBB,
537                                       const char *Name);
538void LLVMDeleteBasicBlock(LLVMBasicBlockRef BB);
539
540/* Operations on instructions */
541LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst);
542LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB);
543LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB);
544LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst);
545LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst);
546
547/* Operations on call sites */
548void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC);
549unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr);
550void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute);
551void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
552                              LLVMAttribute);
553void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
554                                unsigned align);
555
556/* Operations on call instructions (only) */
557int LLVMIsTailCall(LLVMValueRef CallInst);
558void LLVMSetTailCall(LLVMValueRef CallInst, int IsTailCall);
559
560/* Operations on phi nodes */
561void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
562                     LLVMBasicBlockRef *IncomingBlocks, unsigned Count);
563unsigned LLVMCountIncoming(LLVMValueRef PhiNode);
564LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index);
565LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index);
566
567/*===-- Instruction builders ----------------------------------------------===*/
568
569/* An instruction builder represents a point within a basic block, and is the
570 * exclusive means of building instructions using the C interface.
571 */
572
573LLVMBuilderRef LLVMCreateBuilder(void);
574void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
575                         LLVMValueRef Instr);
576void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr);
577void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block);
578LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder);
579void LLVMClearInsertionPosition(LLVMBuilderRef Builder);
580void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr);
581void LLVMDisposeBuilder(LLVMBuilderRef Builder);
582
583/* Terminators */
584LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef);
585LLVMValueRef LLVMBuildRet(LLVMBuilderRef, LLVMValueRef V);
586LLVMValueRef LLVMBuildBr(LLVMBuilderRef, LLVMBasicBlockRef Dest);
587LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef, LLVMValueRef If,
588                             LLVMBasicBlockRef Then, LLVMBasicBlockRef Else);
589LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef, LLVMValueRef V,
590                             LLVMBasicBlockRef Else, unsigned NumCases);
591LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef, LLVMValueRef Fn,
592                             LLVMValueRef *Args, unsigned NumArgs,
593                             LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
594                             const char *Name);
595LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef);
596LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef);
597
598/* Add a case to the switch instruction */
599void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
600                 LLVMBasicBlockRef Dest);
601
602/* Arithmetic */
603LLVMValueRef LLVMBuildAdd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
604                          const char *Name);
605LLVMValueRef LLVMBuildSub(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
606                          const char *Name);
607LLVMValueRef LLVMBuildMul(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
608                          const char *Name);
609LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
610                           const char *Name);
611LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
612                           const char *Name);
613LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
614                           const char *Name);
615LLVMValueRef LLVMBuildURem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
616                           const char *Name);
617LLVMValueRef LLVMBuildSRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
618                           const char *Name);
619LLVMValueRef LLVMBuildFRem(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
620                           const char *Name);
621LLVMValueRef LLVMBuildShl(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
622                           const char *Name);
623LLVMValueRef LLVMBuildLShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
624                           const char *Name);
625LLVMValueRef LLVMBuildAShr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
626                           const char *Name);
627LLVMValueRef LLVMBuildAnd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
628                          const char *Name);
629LLVMValueRef LLVMBuildOr(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
630                          const char *Name);
631LLVMValueRef LLVMBuildXor(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
632                          const char *Name);
633LLVMValueRef LLVMBuildNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name);
634LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name);
635
636/* Memory */
637LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
638LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,
639                                  LLVMValueRef Val, const char *Name);
640LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
641LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef, LLVMTypeRef Ty,
642                                  LLVMValueRef Val, const char *Name);
643LLVMValueRef LLVMBuildFree(LLVMBuilderRef, LLVMValueRef PointerVal);
644LLVMValueRef LLVMBuildLoad(LLVMBuilderRef, LLVMValueRef PointerVal,
645                           const char *Name);
646LLVMValueRef LLVMBuildStore(LLVMBuilderRef, LLVMValueRef Val, LLVMValueRef Ptr);
647LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
648                          LLVMValueRef *Indices, unsigned NumIndices,
649                          const char *Name);
650
651/* Casts */
652LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef, LLVMValueRef Val,
653                            LLVMTypeRef DestTy, const char *Name);
654LLVMValueRef LLVMBuildZExt(LLVMBuilderRef, LLVMValueRef Val,
655                           LLVMTypeRef DestTy, const char *Name);
656LLVMValueRef LLVMBuildSExt(LLVMBuilderRef, LLVMValueRef Val,
657                           LLVMTypeRef DestTy, const char *Name);
658LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef, LLVMValueRef Val,
659                             LLVMTypeRef DestTy, const char *Name);
660LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef, LLVMValueRef Val,
661                             LLVMTypeRef DestTy, const char *Name);
662LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef, LLVMValueRef Val,
663                             LLVMTypeRef DestTy, const char *Name);
664LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef, LLVMValueRef Val,
665                             LLVMTypeRef DestTy, const char *Name);
666LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef, LLVMValueRef Val,
667                              LLVMTypeRef DestTy, const char *Name);
668LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef, LLVMValueRef Val,
669                            LLVMTypeRef DestTy, const char *Name);
670LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef, LLVMValueRef Val,
671                               LLVMTypeRef DestTy, const char *Name);
672LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef, LLVMValueRef Val,
673                               LLVMTypeRef DestTy, const char *Name);
674LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef, LLVMValueRef Val,
675                              LLVMTypeRef DestTy, const char *Name);
676
677/* Comparisons */
678LLVMValueRef LLVMBuildICmp(LLVMBuilderRef, LLVMIntPredicate Op,
679                           LLVMValueRef LHS, LLVMValueRef RHS,
680                           const char *Name);
681LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef, LLVMRealPredicate Op,
682                           LLVMValueRef LHS, LLVMValueRef RHS,
683                           const char *Name);
684
685/* Miscellaneous instructions */
686LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
687LLVMValueRef LLVMBuildCall(LLVMBuilderRef, LLVMValueRef Fn,
688                           LLVMValueRef *Args, unsigned NumArgs,
689                           const char *Name);
690LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If,
691                             LLVMValueRef Then, LLVMValueRef Else,
692                             const char *Name);
693LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef, LLVMValueRef List, LLVMTypeRef Ty,
694                            const char *Name);
695LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef, LLVMValueRef VecVal,
696                                     LLVMValueRef Index, const char *Name);
697LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef, LLVMValueRef VecVal,
698                                    LLVMValueRef EltVal, LLVMValueRef Index,
699                                    const char *Name);
700LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef, LLVMValueRef V1,
701                                    LLVMValueRef V2, LLVMValueRef Mask,
702                                    const char *Name);
703LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef, LLVMValueRef AggVal,
704                                   unsigned Index, const char *Name);
705LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef, LLVMValueRef AggVal,
706                                  LLVMValueRef EltVal, unsigned Index,
707                                  const char *Name);
708
709
710/*===-- Module providers --------------------------------------------------===*/
711
712/* Encapsulates the module M in a module provider, taking ownership of the
713 * module.
714 * See the constructor llvm::ExistingModuleProvider::ExistingModuleProvider.
715 */
716LLVMModuleProviderRef
717LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M);
718
719/* Destroys the module provider MP as well as the contained module.
720 * See the destructor llvm::ModuleProvider::~ModuleProvider.
721 */
722void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP);
723
724
725/*===-- Memory buffers ----------------------------------------------------===*/
726
727int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
728                                             LLVMMemoryBufferRef *OutMemBuf,
729                                             char **OutMessage);
730int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
731                                    char **OutMessage);
732void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf);
733
734
735/*===-- Pass Managers -----------------------------------------------------===*/
736
737/** Constructs a new whole-module pass pipeline. This type of pipeline is
738    suitable for link-time optimization and whole-module transformations.
739    See llvm::PassManager::PassManager. */
740LLVMPassManagerRef LLVMCreatePassManager(void);
741
742/** Constructs a new function-by-function pass pipeline over the module
743    provider. It does not take ownership of the module provider. This type of
744    pipeline is suitable for code generation and JIT compilation tasks.
745    See llvm::FunctionPassManager::FunctionPassManager. */
746LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef MP);
747
748/** Initializes, executes on the provided module, and finalizes all of the
749    passes scheduled in the pass manager. Returns 1 if any of the passes
750    modified the module, 0 otherwise. See llvm::PassManager::run(Module&). */
751int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M);
752
753/** Initializes all of the function passes scheduled in the function pass
754    manager. Returns 1 if any of the passes modified the module, 0 otherwise.
755    See llvm::FunctionPassManager::doInitialization. */
756int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM);
757
758/** Executes all of the function passes scheduled in the function pass manager
759    on the provided function. Returns 1 if any of the passes modified the
760    function, false otherwise.
761    See llvm::FunctionPassManager::run(Function&). */
762int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F);
763
764/** Finalizes all of the function passes scheduled in in the function pass
765    manager. Returns 1 if any of the passes modified the module, 0 otherwise.
766    See llvm::FunctionPassManager::doFinalization. */
767int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM);
768
769/** Frees the memory of a pass pipeline. For function pipelines, does not free
770    the module provider.
771    See llvm::PassManagerBase::~PassManagerBase. */
772void LLVMDisposePassManager(LLVMPassManagerRef PM);
773
774
775#ifdef __cplusplus
776}
777
778namespace llvm {
779  class ModuleProvider;
780  class MemoryBuffer;
781  class PassManagerBase;
782
783  #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)   \
784    inline ty *unwrap(ref P) {                          \
785      return reinterpret_cast<ty*>(P);                  \
786    }                                                   \
787                                                        \
788    inline ref wrap(const ty *P) {                      \
789      return reinterpret_cast<ref>(const_cast<ty*>(P)); \
790    }
791
792  #define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)  \
793    DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
794                                                        \
795    template<typename T>                                \
796    inline T *unwrap(ref P) {                           \
797      return cast<T>(unwrap(P));                        \
798    }
799
800  #define DEFINE_STDCXX_CONVERSION_FUNCTIONS(ty, ref)   \
801    DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)         \
802                                                        \
803    template<typename T>                                \
804    inline T *unwrap(ref P) {                           \
805      T *Q = dynamic_cast<T*>(unwrap(P));               \
806      assert(Q && "Invalid cast!");                     \
807      return Q;                                         \
808    }
809
810  DEFINE_ISA_CONVERSION_FUNCTIONS   (Type,               LLVMTypeRef          )
811  DEFINE_ISA_CONVERSION_FUNCTIONS   (Value,              LLVMValueRef         )
812  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Module,             LLVMModuleRef        )
813  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock,         LLVMBasicBlockRef    )
814  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>,        LLVMBuilderRef       )
815  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(PATypeHolder,       LLVMTypeHandleRef    )
816  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ModuleProvider,     LLVMModuleProviderRef)
817  DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer,       LLVMMemoryBufferRef  )
818  DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBase,    LLVMPassManagerRef   )
819
820  #undef DEFINE_STDCXX_CONVERSION_FUNCTIONS
821  #undef DEFINE_ISA_CONVERSION_FUNCTIONS
822  #undef DEFINE_SIMPLE_CONVERSION_FUNCTIONS
823
824  /* Specialized opaque type conversions.
825   */
826  inline Type **unwrap(LLVMTypeRef* Tys) {
827    return reinterpret_cast<Type**>(Tys);
828  }
829
830  inline LLVMTypeRef *wrap(const Type **Tys) {
831    return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
832  }
833
834  /* Specialized opaque value conversions.
835   */
836  inline Value **unwrap(LLVMValueRef *Vals) {
837    return reinterpret_cast<Value**>(Vals);
838  }
839
840  template<typename T>
841  inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
842    #if DEBUG
843    for (LLVMValueRef *I = Vals, E = Vals + Length; I != E; ++I)
844      cast<T>(*I);
845    #endif
846    return reinterpret_cast<T**>(Vals);
847  }
848
849  inline LLVMValueRef *wrap(const Value **Vals) {
850    return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
851  }
852}
853
854#endif /* !defined(__cplusplus) */
855
856#endif /* !defined(LLVM_C_CORE_H) */
857