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