• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Core.cpp ----------------------------------------------------------===//
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 file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm-c/Core.h"
16 #include "llvm/Attributes.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/GlobalAlias.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CallSite.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/system_error.h"
32 #include <cassert>
33 #include <cstdlib>
34 #include <cstring>
35 
36 using namespace llvm;
37 
initializeCore(PassRegistry & Registry)38 void llvm::initializeCore(PassRegistry &Registry) {
39   initializeDominatorTreePass(Registry);
40   initializePrintModulePassPass(Registry);
41   initializePrintFunctionPassPass(Registry);
42   initializeVerifierPass(Registry);
43   initializePreVerifierPass(Registry);
44 }
45 
LLVMInitializeCore(LLVMPassRegistryRef R)46 void LLVMInitializeCore(LLVMPassRegistryRef R) {
47   initializeCore(*unwrap(R));
48 }
49 
50 /*===-- Error handling ----------------------------------------------------===*/
51 
LLVMDisposeMessage(char * Message)52 void LLVMDisposeMessage(char *Message) {
53   free(Message);
54 }
55 
56 
57 /*===-- Operations on contexts --------------------------------------------===*/
58 
LLVMContextCreate()59 LLVMContextRef LLVMContextCreate() {
60   return wrap(new LLVMContext());
61 }
62 
LLVMGetGlobalContext()63 LLVMContextRef LLVMGetGlobalContext() {
64   return wrap(&getGlobalContext());
65 }
66 
LLVMContextDispose(LLVMContextRef C)67 void LLVMContextDispose(LLVMContextRef C) {
68   delete unwrap(C);
69 }
70 
LLVMGetMDKindIDInContext(LLVMContextRef C,const char * Name,unsigned SLen)71 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
72                                   unsigned SLen) {
73   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
74 }
75 
LLVMGetMDKindID(const char * Name,unsigned SLen)76 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
77   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
78 }
79 
80 
81 /*===-- Operations on modules ---------------------------------------------===*/
82 
LLVMModuleCreateWithName(const char * ModuleID)83 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
84   return wrap(new Module(ModuleID, getGlobalContext()));
85 }
86 
LLVMModuleCreateWithNameInContext(const char * ModuleID,LLVMContextRef C)87 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
88                                                 LLVMContextRef C) {
89   return wrap(new Module(ModuleID, *unwrap(C)));
90 }
91 
LLVMDisposeModule(LLVMModuleRef M)92 void LLVMDisposeModule(LLVMModuleRef M) {
93   delete unwrap(M);
94 }
95 
96 /*--.. Data layout .........................................................--*/
LLVMGetDataLayout(LLVMModuleRef M)97 const char * LLVMGetDataLayout(LLVMModuleRef M) {
98   return unwrap(M)->getDataLayout().c_str();
99 }
100 
LLVMSetDataLayout(LLVMModuleRef M,const char * Triple)101 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
102   unwrap(M)->setDataLayout(Triple);
103 }
104 
105 /*--.. Target triple .......................................................--*/
LLVMGetTarget(LLVMModuleRef M)106 const char * LLVMGetTarget(LLVMModuleRef M) {
107   return unwrap(M)->getTargetTriple().c_str();
108 }
109 
LLVMSetTarget(LLVMModuleRef M,const char * Triple)110 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
111   unwrap(M)->setTargetTriple(Triple);
112 }
113 
LLVMDumpModule(LLVMModuleRef M)114 void LLVMDumpModule(LLVMModuleRef M) {
115   unwrap(M)->dump();
116 }
117 
LLVMPrintModuleToFile(LLVMModuleRef M,const char * Filename,char ** ErrorMessage)118 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
119                                char **ErrorMessage) {
120   std::string error;
121   raw_fd_ostream dest(Filename, error);
122   if (!error.empty()) {
123     *ErrorMessage = strdup(error.c_str());
124     return true;
125   }
126 
127   unwrap(M)->print(dest, NULL);
128 
129   if (!error.empty()) {
130     *ErrorMessage = strdup(error.c_str());
131     return true;
132   }
133   dest.flush();
134   return false;
135 }
136 
137 /*--.. Operations on inline assembler ......................................--*/
LLVMSetModuleInlineAsm(LLVMModuleRef M,const char * Asm)138 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
139   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
140 }
141 
142 
143 /*--.. Operations on module contexts ......................................--*/
LLVMGetModuleContext(LLVMModuleRef M)144 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
145   return wrap(&unwrap(M)->getContext());
146 }
147 
148 
149 /*===-- Operations on types -----------------------------------------------===*/
150 
151 /*--.. Operations on all types (mostly) ....................................--*/
152 
LLVMGetTypeKind(LLVMTypeRef Ty)153 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
154   switch (unwrap(Ty)->getTypeID()) {
155   default: llvm_unreachable("Unhandled TypeID.");
156   case Type::VoidTyID:
157     return LLVMVoidTypeKind;
158   case Type::HalfTyID:
159     return LLVMHalfTypeKind;
160   case Type::FloatTyID:
161     return LLVMFloatTypeKind;
162   case Type::DoubleTyID:
163     return LLVMDoubleTypeKind;
164   case Type::X86_FP80TyID:
165     return LLVMX86_FP80TypeKind;
166   case Type::FP128TyID:
167     return LLVMFP128TypeKind;
168   case Type::PPC_FP128TyID:
169     return LLVMPPC_FP128TypeKind;
170   case Type::LabelTyID:
171     return LLVMLabelTypeKind;
172   case Type::MetadataTyID:
173     return LLVMMetadataTypeKind;
174   case Type::IntegerTyID:
175     return LLVMIntegerTypeKind;
176   case Type::FunctionTyID:
177     return LLVMFunctionTypeKind;
178   case Type::StructTyID:
179     return LLVMStructTypeKind;
180   case Type::ArrayTyID:
181     return LLVMArrayTypeKind;
182   case Type::PointerTyID:
183     return LLVMPointerTypeKind;
184   case Type::VectorTyID:
185     return LLVMVectorTypeKind;
186   case Type::X86_MMXTyID:
187     return LLVMX86_MMXTypeKind;
188   }
189 }
190 
LLVMTypeIsSized(LLVMTypeRef Ty)191 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
192 {
193     return unwrap(Ty)->isSized();
194 }
195 
LLVMGetTypeContext(LLVMTypeRef Ty)196 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
197   return wrap(&unwrap(Ty)->getContext());
198 }
199 
200 /*--.. Operations on integer types .........................................--*/
201 
LLVMInt1TypeInContext(LLVMContextRef C)202 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
203   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
204 }
LLVMInt8TypeInContext(LLVMContextRef C)205 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
206   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
207 }
LLVMInt16TypeInContext(LLVMContextRef C)208 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
209   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
210 }
LLVMInt32TypeInContext(LLVMContextRef C)211 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
212   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
213 }
LLVMInt64TypeInContext(LLVMContextRef C)214 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
215   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
216 }
LLVMIntTypeInContext(LLVMContextRef C,unsigned NumBits)217 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
218   return wrap(IntegerType::get(*unwrap(C), NumBits));
219 }
220 
LLVMInt1Type(void)221 LLVMTypeRef LLVMInt1Type(void)  {
222   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
223 }
LLVMInt8Type(void)224 LLVMTypeRef LLVMInt8Type(void)  {
225   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
226 }
LLVMInt16Type(void)227 LLVMTypeRef LLVMInt16Type(void) {
228   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
229 }
LLVMInt32Type(void)230 LLVMTypeRef LLVMInt32Type(void) {
231   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
232 }
LLVMInt64Type(void)233 LLVMTypeRef LLVMInt64Type(void) {
234   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
235 }
LLVMIntType(unsigned NumBits)236 LLVMTypeRef LLVMIntType(unsigned NumBits) {
237   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
238 }
239 
LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy)240 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
241   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
242 }
243 
244 /*--.. Operations on real types ............................................--*/
245 
LLVMHalfTypeInContext(LLVMContextRef C)246 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
247   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
248 }
LLVMFloatTypeInContext(LLVMContextRef C)249 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
250   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
251 }
LLVMDoubleTypeInContext(LLVMContextRef C)252 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
253   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
254 }
LLVMX86FP80TypeInContext(LLVMContextRef C)255 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
256   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
257 }
LLVMFP128TypeInContext(LLVMContextRef C)258 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
259   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
260 }
LLVMPPCFP128TypeInContext(LLVMContextRef C)261 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
262   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
263 }
LLVMX86MMXTypeInContext(LLVMContextRef C)264 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
265   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
266 }
267 
LLVMHalfType(void)268 LLVMTypeRef LLVMHalfType(void) {
269   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
270 }
LLVMFloatType(void)271 LLVMTypeRef LLVMFloatType(void) {
272   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
273 }
LLVMDoubleType(void)274 LLVMTypeRef LLVMDoubleType(void) {
275   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
276 }
LLVMX86FP80Type(void)277 LLVMTypeRef LLVMX86FP80Type(void) {
278   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
279 }
LLVMFP128Type(void)280 LLVMTypeRef LLVMFP128Type(void) {
281   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
282 }
LLVMPPCFP128Type(void)283 LLVMTypeRef LLVMPPCFP128Type(void) {
284   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
285 }
LLVMX86MMXType(void)286 LLVMTypeRef LLVMX86MMXType(void) {
287   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
288 }
289 
290 /*--.. Operations on function types ........................................--*/
291 
LLVMFunctionType(LLVMTypeRef ReturnType,LLVMTypeRef * ParamTypes,unsigned ParamCount,LLVMBool IsVarArg)292 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
293                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
294                              LLVMBool IsVarArg) {
295   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
296   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
297 }
298 
LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy)299 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
300   return unwrap<FunctionType>(FunctionTy)->isVarArg();
301 }
302 
LLVMGetReturnType(LLVMTypeRef FunctionTy)303 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
304   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
305 }
306 
LLVMCountParamTypes(LLVMTypeRef FunctionTy)307 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
308   return unwrap<FunctionType>(FunctionTy)->getNumParams();
309 }
310 
LLVMGetParamTypes(LLVMTypeRef FunctionTy,LLVMTypeRef * Dest)311 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
312   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
313   for (FunctionType::param_iterator I = Ty->param_begin(),
314                                     E = Ty->param_end(); I != E; ++I)
315     *Dest++ = wrap(*I);
316 }
317 
318 /*--.. Operations on struct types ..........................................--*/
319 
LLVMStructTypeInContext(LLVMContextRef C,LLVMTypeRef * ElementTypes,unsigned ElementCount,LLVMBool Packed)320 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
321                            unsigned ElementCount, LLVMBool Packed) {
322   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
323   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
324 }
325 
LLVMStructType(LLVMTypeRef * ElementTypes,unsigned ElementCount,LLVMBool Packed)326 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
327                            unsigned ElementCount, LLVMBool Packed) {
328   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
329                                  ElementCount, Packed);
330 }
331 
LLVMStructCreateNamed(LLVMContextRef C,const char * Name)332 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
333 {
334   return wrap(StructType::create(*unwrap(C), Name));
335 }
336 
LLVMGetStructName(LLVMTypeRef Ty)337 const char *LLVMGetStructName(LLVMTypeRef Ty)
338 {
339   StructType *Type = unwrap<StructType>(Ty);
340   if (!Type->hasName())
341     return 0;
342   return Type->getName().data();
343 }
344 
LLVMStructSetBody(LLVMTypeRef StructTy,LLVMTypeRef * ElementTypes,unsigned ElementCount,LLVMBool Packed)345 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
346                        unsigned ElementCount, LLVMBool Packed) {
347   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
348   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
349 }
350 
LLVMCountStructElementTypes(LLVMTypeRef StructTy)351 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
352   return unwrap<StructType>(StructTy)->getNumElements();
353 }
354 
LLVMGetStructElementTypes(LLVMTypeRef StructTy,LLVMTypeRef * Dest)355 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
356   StructType *Ty = unwrap<StructType>(StructTy);
357   for (StructType::element_iterator I = Ty->element_begin(),
358                                     E = Ty->element_end(); I != E; ++I)
359     *Dest++ = wrap(*I);
360 }
361 
LLVMIsPackedStruct(LLVMTypeRef StructTy)362 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
363   return unwrap<StructType>(StructTy)->isPacked();
364 }
365 
LLVMIsOpaqueStruct(LLVMTypeRef StructTy)366 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
367   return unwrap<StructType>(StructTy)->isOpaque();
368 }
369 
LLVMGetTypeByName(LLVMModuleRef M,const char * Name)370 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
371   return wrap(unwrap(M)->getTypeByName(Name));
372 }
373 
374 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
375 
LLVMArrayType(LLVMTypeRef ElementType,unsigned ElementCount)376 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
377   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
378 }
379 
LLVMPointerType(LLVMTypeRef ElementType,unsigned AddressSpace)380 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
381   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
382 }
383 
LLVMVectorType(LLVMTypeRef ElementType,unsigned ElementCount)384 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
385   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
386 }
387 
LLVMGetElementType(LLVMTypeRef Ty)388 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
389   return wrap(unwrap<SequentialType>(Ty)->getElementType());
390 }
391 
LLVMGetArrayLength(LLVMTypeRef ArrayTy)392 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
393   return unwrap<ArrayType>(ArrayTy)->getNumElements();
394 }
395 
LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy)396 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
397   return unwrap<PointerType>(PointerTy)->getAddressSpace();
398 }
399 
LLVMGetVectorSize(LLVMTypeRef VectorTy)400 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
401   return unwrap<VectorType>(VectorTy)->getNumElements();
402 }
403 
404 /*--.. Operations on other types ...........................................--*/
405 
LLVMVoidTypeInContext(LLVMContextRef C)406 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
407   return wrap(Type::getVoidTy(*unwrap(C)));
408 }
LLVMLabelTypeInContext(LLVMContextRef C)409 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
410   return wrap(Type::getLabelTy(*unwrap(C)));
411 }
412 
LLVMVoidType(void)413 LLVMTypeRef LLVMVoidType(void)  {
414   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
415 }
LLVMLabelType(void)416 LLVMTypeRef LLVMLabelType(void) {
417   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
418 }
419 
420 /*===-- Operations on values ----------------------------------------------===*/
421 
422 /*--.. Operations on all values ............................................--*/
423 
LLVMTypeOf(LLVMValueRef Val)424 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
425   return wrap(unwrap(Val)->getType());
426 }
427 
LLVMGetValueName(LLVMValueRef Val)428 const char *LLVMGetValueName(LLVMValueRef Val) {
429   return unwrap(Val)->getName().data();
430 }
431 
LLVMSetValueName(LLVMValueRef Val,const char * Name)432 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
433   unwrap(Val)->setName(Name);
434 }
435 
LLVMDumpValue(LLVMValueRef Val)436 void LLVMDumpValue(LLVMValueRef Val) {
437   unwrap(Val)->dump();
438 }
439 
LLVMReplaceAllUsesWith(LLVMValueRef OldVal,LLVMValueRef NewVal)440 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
441   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
442 }
443 
LLVMHasMetadata(LLVMValueRef Inst)444 int LLVMHasMetadata(LLVMValueRef Inst) {
445   return unwrap<Instruction>(Inst)->hasMetadata();
446 }
447 
LLVMGetMetadata(LLVMValueRef Inst,unsigned KindID)448 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
449   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
450 }
451 
LLVMSetMetadata(LLVMValueRef Inst,unsigned KindID,LLVMValueRef MD)452 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
453   unwrap<Instruction>(Inst)->setMetadata(KindID, MD? unwrap<MDNode>(MD) : NULL);
454 }
455 
456 /*--.. Conversion functions ................................................--*/
457 
458 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
459   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
460     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
461   }
462 
LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)463 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
464 
465 /*--.. Operations on Uses ..................................................--*/
466 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
467   Value *V = unwrap(Val);
468   Value::use_iterator I = V->use_begin();
469   if (I == V->use_end())
470     return 0;
471   return wrap(&(I.getUse()));
472 }
473 
LLVMGetNextUse(LLVMUseRef U)474 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
475   Use *Next = unwrap(U)->getNext();
476   if (Next)
477     return wrap(Next);
478   return 0;
479 }
480 
LLVMGetUser(LLVMUseRef U)481 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
482   return wrap(unwrap(U)->getUser());
483 }
484 
LLVMGetUsedValue(LLVMUseRef U)485 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
486   return wrap(unwrap(U)->get());
487 }
488 
489 /*--.. Operations on Users .................................................--*/
LLVMGetOperand(LLVMValueRef Val,unsigned Index)490 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
491   Value *V = unwrap(Val);
492   if (MDNode *MD = dyn_cast<MDNode>(V))
493       return wrap(MD->getOperand(Index));
494   return wrap(cast<User>(V)->getOperand(Index));
495 }
496 
LLVMSetOperand(LLVMValueRef Val,unsigned Index,LLVMValueRef Op)497 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
498   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
499 }
500 
LLVMGetNumOperands(LLVMValueRef Val)501 int LLVMGetNumOperands(LLVMValueRef Val) {
502   Value *V = unwrap(Val);
503   if (MDNode *MD = dyn_cast<MDNode>(V))
504       return MD->getNumOperands();
505   return cast<User>(V)->getNumOperands();
506 }
507 
508 /*--.. Operations on constants of any type .................................--*/
509 
LLVMConstNull(LLVMTypeRef Ty)510 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
511   return wrap(Constant::getNullValue(unwrap(Ty)));
512 }
513 
LLVMConstAllOnes(LLVMTypeRef Ty)514 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
515   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
516 }
517 
LLVMGetUndef(LLVMTypeRef Ty)518 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
519   return wrap(UndefValue::get(unwrap(Ty)));
520 }
521 
LLVMIsConstant(LLVMValueRef Ty)522 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
523   return isa<Constant>(unwrap(Ty));
524 }
525 
LLVMIsNull(LLVMValueRef Val)526 LLVMBool LLVMIsNull(LLVMValueRef Val) {
527   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
528     return C->isNullValue();
529   return false;
530 }
531 
LLVMIsUndef(LLVMValueRef Val)532 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
533   return isa<UndefValue>(unwrap(Val));
534 }
535 
LLVMConstPointerNull(LLVMTypeRef Ty)536 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
537   return
538       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
539 }
540 
541 /*--.. Operations on metadata nodes ........................................--*/
542 
LLVMMDStringInContext(LLVMContextRef C,const char * Str,unsigned SLen)543 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
544                                    unsigned SLen) {
545   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
546 }
547 
LLVMMDString(const char * Str,unsigned SLen)548 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
549   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
550 }
551 
LLVMMDNodeInContext(LLVMContextRef C,LLVMValueRef * Vals,unsigned Count)552 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
553                                  unsigned Count) {
554   return wrap(MDNode::get(*unwrap(C),
555                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
556 }
557 
LLVMMDNode(LLVMValueRef * Vals,unsigned Count)558 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
559   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
560 }
561 
LLVMGetMDString(LLVMValueRef V,unsigned * Len)562 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
563   if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
564     *Len = S->getString().size();
565     return S->getString().data();
566   }
567   *Len = 0;
568   return 0;
569 }
570 
LLVMGetNamedMetadataNumOperands(LLVMModuleRef M,const char * name)571 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
572 {
573   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
574     return N->getNumOperands();
575   }
576   return 0;
577 }
578 
LLVMGetNamedMetadataOperands(LLVMModuleRef M,const char * name,LLVMValueRef * Dest)579 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
580 {
581   NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
582   if (!N)
583     return;
584   for (unsigned i=0;i<N->getNumOperands();i++)
585     Dest[i] = wrap(N->getOperand(i));
586 }
587 
LLVMAddNamedMetadataOperand(LLVMModuleRef M,const char * name,LLVMValueRef Val)588 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
589                                  LLVMValueRef Val)
590 {
591   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
592   if (!N)
593     return;
594   MDNode *Op = Val ? unwrap<MDNode>(Val) : NULL;
595   if (Op)
596     N->addOperand(Op);
597 }
598 
599 /*--.. Operations on scalar constants ......................................--*/
600 
LLVMConstInt(LLVMTypeRef IntTy,unsigned long long N,LLVMBool SignExtend)601 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
602                           LLVMBool SignExtend) {
603   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
604 }
605 
LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,unsigned NumWords,const uint64_t Words[])606 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
607                                               unsigned NumWords,
608                                               const uint64_t Words[]) {
609     IntegerType *Ty = unwrap<IntegerType>(IntTy);
610     return wrap(ConstantInt::get(Ty->getContext(),
611                                  APInt(Ty->getBitWidth(),
612                                        makeArrayRef(Words, NumWords))));
613 }
614 
LLVMConstIntOfString(LLVMTypeRef IntTy,const char Str[],uint8_t Radix)615 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
616                                   uint8_t Radix) {
617   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
618                                Radix));
619 }
620 
LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy,const char Str[],unsigned SLen,uint8_t Radix)621 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
622                                          unsigned SLen, uint8_t Radix) {
623   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
624                                Radix));
625 }
626 
LLVMConstReal(LLVMTypeRef RealTy,double N)627 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
628   return wrap(ConstantFP::get(unwrap(RealTy), N));
629 }
630 
LLVMConstRealOfString(LLVMTypeRef RealTy,const char * Text)631 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
632   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
633 }
634 
LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy,const char Str[],unsigned SLen)635 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
636                                           unsigned SLen) {
637   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
638 }
639 
LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal)640 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
641   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
642 }
643 
LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal)644 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
645   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
646 }
647 
648 /*--.. Operations on composite constants ...................................--*/
649 
LLVMConstStringInContext(LLVMContextRef C,const char * Str,unsigned Length,LLVMBool DontNullTerminate)650 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
651                                       unsigned Length,
652                                       LLVMBool DontNullTerminate) {
653   /* Inverted the sense of AddNull because ', 0)' is a
654      better mnemonic for null termination than ', 1)'. */
655   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
656                                            DontNullTerminate == 0));
657 }
LLVMConstStructInContext(LLVMContextRef C,LLVMValueRef * ConstantVals,unsigned Count,LLVMBool Packed)658 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
659                                       LLVMValueRef *ConstantVals,
660                                       unsigned Count, LLVMBool Packed) {
661   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
662   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
663                                       Packed != 0));
664 }
665 
LLVMConstString(const char * Str,unsigned Length,LLVMBool DontNullTerminate)666 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
667                              LLVMBool DontNullTerminate) {
668   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
669                                   DontNullTerminate);
670 }
LLVMConstArray(LLVMTypeRef ElementTy,LLVMValueRef * ConstantVals,unsigned Length)671 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
672                             LLVMValueRef *ConstantVals, unsigned Length) {
673   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
674   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
675 }
LLVMConstStruct(LLVMValueRef * ConstantVals,unsigned Count,LLVMBool Packed)676 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
677                              LLVMBool Packed) {
678   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
679                                   Packed);
680 }
681 
LLVMConstNamedStruct(LLVMTypeRef StructTy,LLVMValueRef * ConstantVals,unsigned Count)682 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
683                                   LLVMValueRef *ConstantVals,
684                                   unsigned Count) {
685   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
686   StructType *Ty = cast<StructType>(unwrap(StructTy));
687 
688   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
689 }
690 
LLVMConstVector(LLVMValueRef * ScalarConstantVals,unsigned Size)691 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
692   return wrap(ConstantVector::get(makeArrayRef(
693                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
694 }
695 
696 /*-- Opcode mapping */
697 
map_to_llvmopcode(int opcode)698 static LLVMOpcode map_to_llvmopcode(int opcode)
699 {
700     switch (opcode) {
701       default: llvm_unreachable("Unhandled Opcode.");
702 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
703 #include "llvm/Instruction.def"
704 #undef HANDLE_INST
705     }
706 }
707 
map_from_llvmopcode(LLVMOpcode code)708 static int map_from_llvmopcode(LLVMOpcode code)
709 {
710     switch (code) {
711 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
712 #include "llvm/Instruction.def"
713 #undef HANDLE_INST
714     }
715     llvm_unreachable("Unhandled Opcode.");
716 }
717 
718 /*--.. Constant expressions ................................................--*/
719 
LLVMGetConstOpcode(LLVMValueRef ConstantVal)720 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
721   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
722 }
723 
LLVMAlignOf(LLVMTypeRef Ty)724 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
725   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
726 }
727 
LLVMSizeOf(LLVMTypeRef Ty)728 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
729   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
730 }
731 
LLVMConstNeg(LLVMValueRef ConstantVal)732 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
733   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
734 }
735 
LLVMConstNSWNeg(LLVMValueRef ConstantVal)736 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
737   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
738 }
739 
LLVMConstNUWNeg(LLVMValueRef ConstantVal)740 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
741   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
742 }
743 
744 
LLVMConstFNeg(LLVMValueRef ConstantVal)745 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
746   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
747 }
748 
LLVMConstNot(LLVMValueRef ConstantVal)749 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
750   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
751 }
752 
LLVMConstAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)753 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
755                                    unwrap<Constant>(RHSConstant)));
756 }
757 
LLVMConstNSWAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)758 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
759                              LLVMValueRef RHSConstant) {
760   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
761                                       unwrap<Constant>(RHSConstant)));
762 }
763 
LLVMConstNUWAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)764 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
765                              LLVMValueRef RHSConstant) {
766   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
767                                       unwrap<Constant>(RHSConstant)));
768 }
769 
LLVMConstFAdd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)770 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
771   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
772                                     unwrap<Constant>(RHSConstant)));
773 }
774 
LLVMConstSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)775 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
776   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
777                                    unwrap<Constant>(RHSConstant)));
778 }
779 
LLVMConstNSWSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)780 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
781                              LLVMValueRef RHSConstant) {
782   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
783                                       unwrap<Constant>(RHSConstant)));
784 }
785 
LLVMConstNUWSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)786 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
787                              LLVMValueRef RHSConstant) {
788   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
789                                       unwrap<Constant>(RHSConstant)));
790 }
791 
LLVMConstFSub(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)792 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
793   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
794                                     unwrap<Constant>(RHSConstant)));
795 }
796 
LLVMConstMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)797 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
798   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
799                                    unwrap<Constant>(RHSConstant)));
800 }
801 
LLVMConstNSWMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)802 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
803                              LLVMValueRef RHSConstant) {
804   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
805                                       unwrap<Constant>(RHSConstant)));
806 }
807 
LLVMConstNUWMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)808 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
809                              LLVMValueRef RHSConstant) {
810   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
811                                       unwrap<Constant>(RHSConstant)));
812 }
813 
LLVMConstFMul(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)814 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
815   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
816                                     unwrap<Constant>(RHSConstant)));
817 }
818 
LLVMConstUDiv(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)819 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
820   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
821                                     unwrap<Constant>(RHSConstant)));
822 }
823 
LLVMConstSDiv(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)824 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
825   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
826                                     unwrap<Constant>(RHSConstant)));
827 }
828 
LLVMConstExactSDiv(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)829 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
830                                 LLVMValueRef RHSConstant) {
831   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
832                                          unwrap<Constant>(RHSConstant)));
833 }
834 
LLVMConstFDiv(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)835 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
836   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
837                                     unwrap<Constant>(RHSConstant)));
838 }
839 
LLVMConstURem(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)840 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
841   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
842                                     unwrap<Constant>(RHSConstant)));
843 }
844 
LLVMConstSRem(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)845 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
846   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
847                                     unwrap<Constant>(RHSConstant)));
848 }
849 
LLVMConstFRem(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)850 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
851   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
852                                     unwrap<Constant>(RHSConstant)));
853 }
854 
LLVMConstAnd(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)855 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
856   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
857                                    unwrap<Constant>(RHSConstant)));
858 }
859 
LLVMConstOr(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)860 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
861   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
862                                   unwrap<Constant>(RHSConstant)));
863 }
864 
LLVMConstXor(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)865 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
866   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
867                                    unwrap<Constant>(RHSConstant)));
868 }
869 
LLVMConstICmp(LLVMIntPredicate Predicate,LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)870 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
871                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
872   return wrap(ConstantExpr::getICmp(Predicate,
873                                     unwrap<Constant>(LHSConstant),
874                                     unwrap<Constant>(RHSConstant)));
875 }
876 
LLVMConstFCmp(LLVMRealPredicate Predicate,LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)877 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
878                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
879   return wrap(ConstantExpr::getFCmp(Predicate,
880                                     unwrap<Constant>(LHSConstant),
881                                     unwrap<Constant>(RHSConstant)));
882 }
883 
LLVMConstShl(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)884 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
885   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
886                                    unwrap<Constant>(RHSConstant)));
887 }
888 
LLVMConstLShr(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)889 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
890   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
891                                     unwrap<Constant>(RHSConstant)));
892 }
893 
LLVMConstAShr(LLVMValueRef LHSConstant,LLVMValueRef RHSConstant)894 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
895   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
896                                     unwrap<Constant>(RHSConstant)));
897 }
898 
LLVMConstGEP(LLVMValueRef ConstantVal,LLVMValueRef * ConstantIndices,unsigned NumIndices)899 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
900                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
901   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
902                                NumIndices);
903   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
904                                              IdxList));
905 }
906 
LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,LLVMValueRef * ConstantIndices,unsigned NumIndices)907 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
908                                   LLVMValueRef *ConstantIndices,
909                                   unsigned NumIndices) {
910   Constant* Val = unwrap<Constant>(ConstantVal);
911   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
912                                NumIndices);
913   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
914 }
915 
LLVMConstTrunc(LLVMValueRef ConstantVal,LLVMTypeRef ToType)916 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
917   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
918                                      unwrap(ToType)));
919 }
920 
LLVMConstSExt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)921 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
922   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
923                                     unwrap(ToType)));
924 }
925 
LLVMConstZExt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)926 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
927   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
928                                     unwrap(ToType)));
929 }
930 
LLVMConstFPTrunc(LLVMValueRef ConstantVal,LLVMTypeRef ToType)931 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
932   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
933                                        unwrap(ToType)));
934 }
935 
LLVMConstFPExt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)936 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
937   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
938                                         unwrap(ToType)));
939 }
940 
LLVMConstUIToFP(LLVMValueRef ConstantVal,LLVMTypeRef ToType)941 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
942   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
943                                       unwrap(ToType)));
944 }
945 
LLVMConstSIToFP(LLVMValueRef ConstantVal,LLVMTypeRef ToType)946 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
947   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
948                                       unwrap(ToType)));
949 }
950 
LLVMConstFPToUI(LLVMValueRef ConstantVal,LLVMTypeRef ToType)951 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
952   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
953                                       unwrap(ToType)));
954 }
955 
LLVMConstFPToSI(LLVMValueRef ConstantVal,LLVMTypeRef ToType)956 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
957   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
958                                       unwrap(ToType)));
959 }
960 
LLVMConstPtrToInt(LLVMValueRef ConstantVal,LLVMTypeRef ToType)961 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
962   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
963                                         unwrap(ToType)));
964 }
965 
LLVMConstIntToPtr(LLVMValueRef ConstantVal,LLVMTypeRef ToType)966 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
967   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
968                                         unwrap(ToType)));
969 }
970 
LLVMConstBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)971 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
972   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
973                                        unwrap(ToType)));
974 }
975 
LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)976 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
977                                     LLVMTypeRef ToType) {
978   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
979                                              unwrap(ToType)));
980 }
981 
LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)982 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
983                                     LLVMTypeRef ToType) {
984   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
985                                              unwrap(ToType)));
986 }
987 
LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)988 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
989                                      LLVMTypeRef ToType) {
990   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
991                                               unwrap(ToType)));
992 }
993 
LLVMConstPointerCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)994 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
995                                   LLVMTypeRef ToType) {
996   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
997                                            unwrap(ToType)));
998 }
999 
LLVMConstIntCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType,LLVMBool isSigned)1000 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1001                               LLVMBool isSigned) {
1002   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1003                                            unwrap(ToType), isSigned));
1004 }
1005 
LLVMConstFPCast(LLVMValueRef ConstantVal,LLVMTypeRef ToType)1006 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1007   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1008                                       unwrap(ToType)));
1009 }
1010 
LLVMConstSelect(LLVMValueRef ConstantCondition,LLVMValueRef ConstantIfTrue,LLVMValueRef ConstantIfFalse)1011 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1012                              LLVMValueRef ConstantIfTrue,
1013                              LLVMValueRef ConstantIfFalse) {
1014   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1015                                       unwrap<Constant>(ConstantIfTrue),
1016                                       unwrap<Constant>(ConstantIfFalse)));
1017 }
1018 
LLVMConstExtractElement(LLVMValueRef VectorConstant,LLVMValueRef IndexConstant)1019 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1020                                      LLVMValueRef IndexConstant) {
1021   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1022                                               unwrap<Constant>(IndexConstant)));
1023 }
1024 
LLVMConstInsertElement(LLVMValueRef VectorConstant,LLVMValueRef ElementValueConstant,LLVMValueRef IndexConstant)1025 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1026                                     LLVMValueRef ElementValueConstant,
1027                                     LLVMValueRef IndexConstant) {
1028   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1029                                          unwrap<Constant>(ElementValueConstant),
1030                                              unwrap<Constant>(IndexConstant)));
1031 }
1032 
LLVMConstShuffleVector(LLVMValueRef VectorAConstant,LLVMValueRef VectorBConstant,LLVMValueRef MaskConstant)1033 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1034                                     LLVMValueRef VectorBConstant,
1035                                     LLVMValueRef MaskConstant) {
1036   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1037                                              unwrap<Constant>(VectorBConstant),
1038                                              unwrap<Constant>(MaskConstant)));
1039 }
1040 
LLVMConstExtractValue(LLVMValueRef AggConstant,unsigned * IdxList,unsigned NumIdx)1041 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1042                                    unsigned NumIdx) {
1043   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1044                                             makeArrayRef(IdxList, NumIdx)));
1045 }
1046 
LLVMConstInsertValue(LLVMValueRef AggConstant,LLVMValueRef ElementValueConstant,unsigned * IdxList,unsigned NumIdx)1047 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1048                                   LLVMValueRef ElementValueConstant,
1049                                   unsigned *IdxList, unsigned NumIdx) {
1050   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1051                                          unwrap<Constant>(ElementValueConstant),
1052                                            makeArrayRef(IdxList, NumIdx)));
1053 }
1054 
LLVMConstInlineAsm(LLVMTypeRef Ty,const char * AsmString,const char * Constraints,LLVMBool HasSideEffects,LLVMBool IsAlignStack)1055 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1056                                 const char *Constraints,
1057                                 LLVMBool HasSideEffects,
1058                                 LLVMBool IsAlignStack) {
1059   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1060                              Constraints, HasSideEffects, IsAlignStack));
1061 }
1062 
LLVMBlockAddress(LLVMValueRef F,LLVMBasicBlockRef BB)1063 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1064   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1065 }
1066 
1067 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1068 
LLVMGetGlobalParent(LLVMValueRef Global)1069 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1070   return wrap(unwrap<GlobalValue>(Global)->getParent());
1071 }
1072 
LLVMIsDeclaration(LLVMValueRef Global)1073 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1074   return unwrap<GlobalValue>(Global)->isDeclaration();
1075 }
1076 
LLVMGetLinkage(LLVMValueRef Global)1077 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1078   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1079   case GlobalValue::ExternalLinkage:
1080     return LLVMExternalLinkage;
1081   case GlobalValue::AvailableExternallyLinkage:
1082     return LLVMAvailableExternallyLinkage;
1083   case GlobalValue::LinkOnceAnyLinkage:
1084     return LLVMLinkOnceAnyLinkage;
1085   case GlobalValue::LinkOnceODRLinkage:
1086     return LLVMLinkOnceODRLinkage;
1087   case GlobalValue::LinkOnceODRAutoHideLinkage:
1088     return LLVMLinkOnceODRAutoHideLinkage;
1089   case GlobalValue::WeakAnyLinkage:
1090     return LLVMWeakAnyLinkage;
1091   case GlobalValue::WeakODRLinkage:
1092     return LLVMWeakODRLinkage;
1093   case GlobalValue::AppendingLinkage:
1094     return LLVMAppendingLinkage;
1095   case GlobalValue::InternalLinkage:
1096     return LLVMInternalLinkage;
1097   case GlobalValue::PrivateLinkage:
1098     return LLVMPrivateLinkage;
1099   case GlobalValue::LinkerPrivateLinkage:
1100     return LLVMLinkerPrivateLinkage;
1101   case GlobalValue::LinkerPrivateWeakLinkage:
1102     return LLVMLinkerPrivateWeakLinkage;
1103   case GlobalValue::DLLImportLinkage:
1104     return LLVMDLLImportLinkage;
1105   case GlobalValue::DLLExportLinkage:
1106     return LLVMDLLExportLinkage;
1107   case GlobalValue::ExternalWeakLinkage:
1108     return LLVMExternalWeakLinkage;
1109   case GlobalValue::CommonLinkage:
1110     return LLVMCommonLinkage;
1111   }
1112 
1113   llvm_unreachable("Invalid GlobalValue linkage!");
1114 }
1115 
LLVMSetLinkage(LLVMValueRef Global,LLVMLinkage Linkage)1116 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1117   GlobalValue *GV = unwrap<GlobalValue>(Global);
1118 
1119   switch (Linkage) {
1120   case LLVMExternalLinkage:
1121     GV->setLinkage(GlobalValue::ExternalLinkage);
1122     break;
1123   case LLVMAvailableExternallyLinkage:
1124     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1125     break;
1126   case LLVMLinkOnceAnyLinkage:
1127     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1128     break;
1129   case LLVMLinkOnceODRLinkage:
1130     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1131     break;
1132   case LLVMLinkOnceODRAutoHideLinkage:
1133     GV->setLinkage(GlobalValue::LinkOnceODRAutoHideLinkage);
1134     break;
1135   case LLVMWeakAnyLinkage:
1136     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1137     break;
1138   case LLVMWeakODRLinkage:
1139     GV->setLinkage(GlobalValue::WeakODRLinkage);
1140     break;
1141   case LLVMAppendingLinkage:
1142     GV->setLinkage(GlobalValue::AppendingLinkage);
1143     break;
1144   case LLVMInternalLinkage:
1145     GV->setLinkage(GlobalValue::InternalLinkage);
1146     break;
1147   case LLVMPrivateLinkage:
1148     GV->setLinkage(GlobalValue::PrivateLinkage);
1149     break;
1150   case LLVMLinkerPrivateLinkage:
1151     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1152     break;
1153   case LLVMLinkerPrivateWeakLinkage:
1154     GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1155     break;
1156   case LLVMDLLImportLinkage:
1157     GV->setLinkage(GlobalValue::DLLImportLinkage);
1158     break;
1159   case LLVMDLLExportLinkage:
1160     GV->setLinkage(GlobalValue::DLLExportLinkage);
1161     break;
1162   case LLVMExternalWeakLinkage:
1163     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1164     break;
1165   case LLVMGhostLinkage:
1166     DEBUG(errs()
1167           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1168     break;
1169   case LLVMCommonLinkage:
1170     GV->setLinkage(GlobalValue::CommonLinkage);
1171     break;
1172   }
1173 }
1174 
LLVMGetSection(LLVMValueRef Global)1175 const char *LLVMGetSection(LLVMValueRef Global) {
1176   return unwrap<GlobalValue>(Global)->getSection().c_str();
1177 }
1178 
LLVMSetSection(LLVMValueRef Global,const char * Section)1179 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1180   unwrap<GlobalValue>(Global)->setSection(Section);
1181 }
1182 
LLVMGetVisibility(LLVMValueRef Global)1183 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1184   return static_cast<LLVMVisibility>(
1185     unwrap<GlobalValue>(Global)->getVisibility());
1186 }
1187 
LLVMSetVisibility(LLVMValueRef Global,LLVMVisibility Viz)1188 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1189   unwrap<GlobalValue>(Global)
1190     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1191 }
1192 
LLVMGetAlignment(LLVMValueRef Global)1193 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1194   return unwrap<GlobalValue>(Global)->getAlignment();
1195 }
1196 
LLVMSetAlignment(LLVMValueRef Global,unsigned Bytes)1197 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1198   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1199 }
1200 
1201 /*--.. Operations on global variables ......................................--*/
1202 
LLVMAddGlobal(LLVMModuleRef M,LLVMTypeRef Ty,const char * Name)1203 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1204   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1205                                  GlobalValue::ExternalLinkage, 0, Name));
1206 }
1207 
LLVMAddGlobalInAddressSpace(LLVMModuleRef M,LLVMTypeRef Ty,const char * Name,unsigned AddressSpace)1208 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1209                                          const char *Name,
1210                                          unsigned AddressSpace) {
1211   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1212                                  GlobalValue::ExternalLinkage, 0, Name, 0,
1213                                  GlobalVariable::NotThreadLocal, AddressSpace));
1214 }
1215 
LLVMGetNamedGlobal(LLVMModuleRef M,const char * Name)1216 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1217   return wrap(unwrap(M)->getNamedGlobal(Name));
1218 }
1219 
LLVMGetFirstGlobal(LLVMModuleRef M)1220 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1221   Module *Mod = unwrap(M);
1222   Module::global_iterator I = Mod->global_begin();
1223   if (I == Mod->global_end())
1224     return 0;
1225   return wrap(I);
1226 }
1227 
LLVMGetLastGlobal(LLVMModuleRef M)1228 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1229   Module *Mod = unwrap(M);
1230   Module::global_iterator I = Mod->global_end();
1231   if (I == Mod->global_begin())
1232     return 0;
1233   return wrap(--I);
1234 }
1235 
LLVMGetNextGlobal(LLVMValueRef GlobalVar)1236 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1237   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1238   Module::global_iterator I = GV;
1239   if (++I == GV->getParent()->global_end())
1240     return 0;
1241   return wrap(I);
1242 }
1243 
LLVMGetPreviousGlobal(LLVMValueRef GlobalVar)1244 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1245   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1246   Module::global_iterator I = GV;
1247   if (I == GV->getParent()->global_begin())
1248     return 0;
1249   return wrap(--I);
1250 }
1251 
LLVMDeleteGlobal(LLVMValueRef GlobalVar)1252 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1253   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1254 }
1255 
LLVMGetInitializer(LLVMValueRef GlobalVar)1256 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1257   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1258   if ( !GV->hasInitializer() )
1259     return 0;
1260   return wrap(GV->getInitializer());
1261 }
1262 
LLVMSetInitializer(LLVMValueRef GlobalVar,LLVMValueRef ConstantVal)1263 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1264   unwrap<GlobalVariable>(GlobalVar)
1265     ->setInitializer(unwrap<Constant>(ConstantVal));
1266 }
1267 
LLVMIsThreadLocal(LLVMValueRef GlobalVar)1268 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1269   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1270 }
1271 
LLVMSetThreadLocal(LLVMValueRef GlobalVar,LLVMBool IsThreadLocal)1272 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1273   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1274 }
1275 
LLVMIsGlobalConstant(LLVMValueRef GlobalVar)1276 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1277   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1278 }
1279 
LLVMSetGlobalConstant(LLVMValueRef GlobalVar,LLVMBool IsConstant)1280 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1281   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1282 }
1283 
1284 /*--.. Operations on aliases ......................................--*/
1285 
LLVMAddAlias(LLVMModuleRef M,LLVMTypeRef Ty,LLVMValueRef Aliasee,const char * Name)1286 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1287                           const char *Name) {
1288   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1289                               unwrap<Constant>(Aliasee), unwrap (M)));
1290 }
1291 
1292 /*--.. Operations on functions .............................................--*/
1293 
LLVMAddFunction(LLVMModuleRef M,const char * Name,LLVMTypeRef FunctionTy)1294 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1295                              LLVMTypeRef FunctionTy) {
1296   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1297                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1298 }
1299 
LLVMGetNamedFunction(LLVMModuleRef M,const char * Name)1300 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1301   return wrap(unwrap(M)->getFunction(Name));
1302 }
1303 
LLVMGetFirstFunction(LLVMModuleRef M)1304 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1305   Module *Mod = unwrap(M);
1306   Module::iterator I = Mod->begin();
1307   if (I == Mod->end())
1308     return 0;
1309   return wrap(I);
1310 }
1311 
LLVMGetLastFunction(LLVMModuleRef M)1312 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1313   Module *Mod = unwrap(M);
1314   Module::iterator I = Mod->end();
1315   if (I == Mod->begin())
1316     return 0;
1317   return wrap(--I);
1318 }
1319 
LLVMGetNextFunction(LLVMValueRef Fn)1320 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1321   Function *Func = unwrap<Function>(Fn);
1322   Module::iterator I = Func;
1323   if (++I == Func->getParent()->end())
1324     return 0;
1325   return wrap(I);
1326 }
1327 
LLVMGetPreviousFunction(LLVMValueRef Fn)1328 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1329   Function *Func = unwrap<Function>(Fn);
1330   Module::iterator I = Func;
1331   if (I == Func->getParent()->begin())
1332     return 0;
1333   return wrap(--I);
1334 }
1335 
LLVMDeleteFunction(LLVMValueRef Fn)1336 void LLVMDeleteFunction(LLVMValueRef Fn) {
1337   unwrap<Function>(Fn)->eraseFromParent();
1338 }
1339 
LLVMGetIntrinsicID(LLVMValueRef Fn)1340 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1341   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1342     return F->getIntrinsicID();
1343   return 0;
1344 }
1345 
LLVMGetFunctionCallConv(LLVMValueRef Fn)1346 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1347   return unwrap<Function>(Fn)->getCallingConv();
1348 }
1349 
LLVMSetFunctionCallConv(LLVMValueRef Fn,unsigned CC)1350 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1351   return unwrap<Function>(Fn)->setCallingConv(
1352     static_cast<CallingConv::ID>(CC));
1353 }
1354 
LLVMGetGC(LLVMValueRef Fn)1355 const char *LLVMGetGC(LLVMValueRef Fn) {
1356   Function *F = unwrap<Function>(Fn);
1357   return F->hasGC()? F->getGC() : 0;
1358 }
1359 
LLVMSetGC(LLVMValueRef Fn,const char * GC)1360 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1361   Function *F = unwrap<Function>(Fn);
1362   if (GC)
1363     F->setGC(GC);
1364   else
1365     F->clearGC();
1366 }
1367 
LLVMAddFunctionAttr(LLVMValueRef Fn,LLVMAttribute PA)1368 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1369   Function *Func = unwrap<Function>(Fn);
1370   const AttrListPtr PAL = Func->getAttributes();
1371   const AttrListPtr PALnew = PAL.addAttr(~0U, Attributes(PA));
1372   Func->setAttributes(PALnew);
1373 }
1374 
LLVMRemoveFunctionAttr(LLVMValueRef Fn,LLVMAttribute PA)1375 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1376   Function *Func = unwrap<Function>(Fn);
1377   const AttrListPtr PAL = Func->getAttributes();
1378   const AttrListPtr PALnew = PAL.removeAttr(~0U, Attributes(PA));
1379   Func->setAttributes(PALnew);
1380 }
1381 
LLVMGetFunctionAttr(LLVMValueRef Fn)1382 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1383   Function *Func = unwrap<Function>(Fn);
1384   const AttrListPtr PAL = Func->getAttributes();
1385   Attributes attr = PAL.getFnAttributes();
1386   return (LLVMAttribute)attr.Raw();
1387 }
1388 
1389 /*--.. Operations on parameters ............................................--*/
1390 
LLVMCountParams(LLVMValueRef FnRef)1391 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1392   // This function is strictly redundant to
1393   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1394   return unwrap<Function>(FnRef)->arg_size();
1395 }
1396 
LLVMGetParams(LLVMValueRef FnRef,LLVMValueRef * ParamRefs)1397 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1398   Function *Fn = unwrap<Function>(FnRef);
1399   for (Function::arg_iterator I = Fn->arg_begin(),
1400                               E = Fn->arg_end(); I != E; I++)
1401     *ParamRefs++ = wrap(I);
1402 }
1403 
LLVMGetParam(LLVMValueRef FnRef,unsigned index)1404 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1405   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1406   while (index --> 0)
1407     AI++;
1408   return wrap(AI);
1409 }
1410 
LLVMGetParamParent(LLVMValueRef V)1411 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1412   return wrap(unwrap<Argument>(V)->getParent());
1413 }
1414 
LLVMGetFirstParam(LLVMValueRef Fn)1415 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1416   Function *Func = unwrap<Function>(Fn);
1417   Function::arg_iterator I = Func->arg_begin();
1418   if (I == Func->arg_end())
1419     return 0;
1420   return wrap(I);
1421 }
1422 
LLVMGetLastParam(LLVMValueRef Fn)1423 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1424   Function *Func = unwrap<Function>(Fn);
1425   Function::arg_iterator I = Func->arg_end();
1426   if (I == Func->arg_begin())
1427     return 0;
1428   return wrap(--I);
1429 }
1430 
LLVMGetNextParam(LLVMValueRef Arg)1431 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1432   Argument *A = unwrap<Argument>(Arg);
1433   Function::arg_iterator I = A;
1434   if (++I == A->getParent()->arg_end())
1435     return 0;
1436   return wrap(I);
1437 }
1438 
LLVMGetPreviousParam(LLVMValueRef Arg)1439 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1440   Argument *A = unwrap<Argument>(Arg);
1441   Function::arg_iterator I = A;
1442   if (I == A->getParent()->arg_begin())
1443     return 0;
1444   return wrap(--I);
1445 }
1446 
LLVMAddAttribute(LLVMValueRef Arg,LLVMAttribute PA)1447 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1448   unwrap<Argument>(Arg)->addAttr(Attributes(PA));
1449 }
1450 
LLVMRemoveAttribute(LLVMValueRef Arg,LLVMAttribute PA)1451 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1452   unwrap<Argument>(Arg)->removeAttr(Attributes(PA));
1453 }
1454 
LLVMGetAttribute(LLVMValueRef Arg)1455 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1456   Argument *A = unwrap<Argument>(Arg);
1457   Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1458     A->getArgNo()+1);
1459   return (LLVMAttribute)attr.Raw();
1460 }
1461 
1462 
LLVMSetParamAlignment(LLVMValueRef Arg,unsigned align)1463 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1464   unwrap<Argument>(Arg)->addAttr(
1465           Attribute::constructAlignmentFromInt(align));
1466 }
1467 
1468 /*--.. Operations on basic blocks ..........................................--*/
1469 
LLVMBasicBlockAsValue(LLVMBasicBlockRef BB)1470 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1471   return wrap(static_cast<Value*>(unwrap(BB)));
1472 }
1473 
LLVMValueIsBasicBlock(LLVMValueRef Val)1474 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1475   return isa<BasicBlock>(unwrap(Val));
1476 }
1477 
LLVMValueAsBasicBlock(LLVMValueRef Val)1478 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1479   return wrap(unwrap<BasicBlock>(Val));
1480 }
1481 
LLVMGetBasicBlockParent(LLVMBasicBlockRef BB)1482 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1483   return wrap(unwrap(BB)->getParent());
1484 }
1485 
LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB)1486 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1487   return wrap(unwrap(BB)->getTerminator());
1488 }
1489 
LLVMCountBasicBlocks(LLVMValueRef FnRef)1490 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1491   return unwrap<Function>(FnRef)->size();
1492 }
1493 
LLVMGetBasicBlocks(LLVMValueRef FnRef,LLVMBasicBlockRef * BasicBlocksRefs)1494 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1495   Function *Fn = unwrap<Function>(FnRef);
1496   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1497     *BasicBlocksRefs++ = wrap(I);
1498 }
1499 
LLVMGetEntryBasicBlock(LLVMValueRef Fn)1500 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1501   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1502 }
1503 
LLVMGetFirstBasicBlock(LLVMValueRef Fn)1504 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1505   Function *Func = unwrap<Function>(Fn);
1506   Function::iterator I = Func->begin();
1507   if (I == Func->end())
1508     return 0;
1509   return wrap(I);
1510 }
1511 
LLVMGetLastBasicBlock(LLVMValueRef Fn)1512 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1513   Function *Func = unwrap<Function>(Fn);
1514   Function::iterator I = Func->end();
1515   if (I == Func->begin())
1516     return 0;
1517   return wrap(--I);
1518 }
1519 
LLVMGetNextBasicBlock(LLVMBasicBlockRef BB)1520 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1521   BasicBlock *Block = unwrap(BB);
1522   Function::iterator I = Block;
1523   if (++I == Block->getParent()->end())
1524     return 0;
1525   return wrap(I);
1526 }
1527 
LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB)1528 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1529   BasicBlock *Block = unwrap(BB);
1530   Function::iterator I = Block;
1531   if (I == Block->getParent()->begin())
1532     return 0;
1533   return wrap(--I);
1534 }
1535 
LLVMAppendBasicBlockInContext(LLVMContextRef C,LLVMValueRef FnRef,const char * Name)1536 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1537                                                 LLVMValueRef FnRef,
1538                                                 const char *Name) {
1539   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1540 }
1541 
LLVMAppendBasicBlock(LLVMValueRef FnRef,const char * Name)1542 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1543   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1544 }
1545 
LLVMInsertBasicBlockInContext(LLVMContextRef C,LLVMBasicBlockRef BBRef,const char * Name)1546 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1547                                                 LLVMBasicBlockRef BBRef,
1548                                                 const char *Name) {
1549   BasicBlock *BB = unwrap(BBRef);
1550   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1551 }
1552 
LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,const char * Name)1553 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1554                                        const char *Name) {
1555   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1556 }
1557 
LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef)1558 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1559   unwrap(BBRef)->eraseFromParent();
1560 }
1561 
LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef)1562 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1563   unwrap(BBRef)->removeFromParent();
1564 }
1565 
LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB,LLVMBasicBlockRef MovePos)1566 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1567   unwrap(BB)->moveBefore(unwrap(MovePos));
1568 }
1569 
LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB,LLVMBasicBlockRef MovePos)1570 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1571   unwrap(BB)->moveAfter(unwrap(MovePos));
1572 }
1573 
1574 /*--.. Operations on instructions ..........................................--*/
1575 
LLVMGetInstructionParent(LLVMValueRef Inst)1576 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1577   return wrap(unwrap<Instruction>(Inst)->getParent());
1578 }
1579 
LLVMGetFirstInstruction(LLVMBasicBlockRef BB)1580 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1581   BasicBlock *Block = unwrap(BB);
1582   BasicBlock::iterator I = Block->begin();
1583   if (I == Block->end())
1584     return 0;
1585   return wrap(I);
1586 }
1587 
LLVMGetLastInstruction(LLVMBasicBlockRef BB)1588 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1589   BasicBlock *Block = unwrap(BB);
1590   BasicBlock::iterator I = Block->end();
1591   if (I == Block->begin())
1592     return 0;
1593   return wrap(--I);
1594 }
1595 
LLVMGetNextInstruction(LLVMValueRef Inst)1596 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1597   Instruction *Instr = unwrap<Instruction>(Inst);
1598   BasicBlock::iterator I = Instr;
1599   if (++I == Instr->getParent()->end())
1600     return 0;
1601   return wrap(I);
1602 }
1603 
LLVMGetPreviousInstruction(LLVMValueRef Inst)1604 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1605   Instruction *Instr = unwrap<Instruction>(Inst);
1606   BasicBlock::iterator I = Instr;
1607   if (I == Instr->getParent()->begin())
1608     return 0;
1609   return wrap(--I);
1610 }
1611 
LLVMInstructionEraseFromParent(LLVMValueRef Inst)1612 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1613   unwrap<Instruction>(Inst)->eraseFromParent();
1614 }
1615 
LLVMGetICmpPredicate(LLVMValueRef Inst)1616 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1617   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1618     return (LLVMIntPredicate)I->getPredicate();
1619   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1620     if (CE->getOpcode() == Instruction::ICmp)
1621       return (LLVMIntPredicate)CE->getPredicate();
1622   return (LLVMIntPredicate)0;
1623 }
1624 
LLVMGetInstructionOpcode(LLVMValueRef Inst)1625 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1626   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1627     return map_to_llvmopcode(C->getOpcode());
1628   return (LLVMOpcode)0;
1629 }
1630 
1631 /*--.. Call and invoke instructions ........................................--*/
1632 
LLVMGetInstructionCallConv(LLVMValueRef Instr)1633 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1634   Value *V = unwrap(Instr);
1635   if (CallInst *CI = dyn_cast<CallInst>(V))
1636     return CI->getCallingConv();
1637   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1638     return II->getCallingConv();
1639   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1640 }
1641 
LLVMSetInstructionCallConv(LLVMValueRef Instr,unsigned CC)1642 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1643   Value *V = unwrap(Instr);
1644   if (CallInst *CI = dyn_cast<CallInst>(V))
1645     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1646   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1647     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1648   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1649 }
1650 
LLVMAddInstrAttribute(LLVMValueRef Instr,unsigned index,LLVMAttribute PA)1651 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1652                            LLVMAttribute PA) {
1653   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1654   Call.setAttributes(
1655     Call.getAttributes().addAttr(index, Attributes(PA)));
1656 }
1657 
LLVMRemoveInstrAttribute(LLVMValueRef Instr,unsigned index,LLVMAttribute PA)1658 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1659                               LLVMAttribute PA) {
1660   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1661   Call.setAttributes(
1662     Call.getAttributes().removeAttr(index, Attributes(PA)));
1663 }
1664 
LLVMSetInstrParamAlignment(LLVMValueRef Instr,unsigned index,unsigned align)1665 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1666                                 unsigned align) {
1667   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1668   Call.setAttributes(
1669     Call.getAttributes().addAttr(index,
1670         Attribute::constructAlignmentFromInt(align)));
1671 }
1672 
1673 /*--.. Operations on call instructions (only) ..............................--*/
1674 
LLVMIsTailCall(LLVMValueRef Call)1675 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1676   return unwrap<CallInst>(Call)->isTailCall();
1677 }
1678 
LLVMSetTailCall(LLVMValueRef Call,LLVMBool isTailCall)1679 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1680   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1681 }
1682 
1683 /*--.. Operations on switch instructions (only) ............................--*/
1684 
LLVMGetSwitchDefaultDest(LLVMValueRef Switch)1685 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1686   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1687 }
1688 
1689 /*--.. Operations on phi nodes .............................................--*/
1690 
LLVMAddIncoming(LLVMValueRef PhiNode,LLVMValueRef * IncomingValues,LLVMBasicBlockRef * IncomingBlocks,unsigned Count)1691 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1692                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1693   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1694   for (unsigned I = 0; I != Count; ++I)
1695     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1696 }
1697 
LLVMCountIncoming(LLVMValueRef PhiNode)1698 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1699   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1700 }
1701 
LLVMGetIncomingValue(LLVMValueRef PhiNode,unsigned Index)1702 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1703   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1704 }
1705 
LLVMGetIncomingBlock(LLVMValueRef PhiNode,unsigned Index)1706 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1707   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1708 }
1709 
1710 
1711 /*===-- Instruction builders ----------------------------------------------===*/
1712 
LLVMCreateBuilderInContext(LLVMContextRef C)1713 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1714   return wrap(new IRBuilder<>(*unwrap(C)));
1715 }
1716 
LLVMCreateBuilder(void)1717 LLVMBuilderRef LLVMCreateBuilder(void) {
1718   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1719 }
1720 
LLVMPositionBuilder(LLVMBuilderRef Builder,LLVMBasicBlockRef Block,LLVMValueRef Instr)1721 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1722                          LLVMValueRef Instr) {
1723   BasicBlock *BB = unwrap(Block);
1724   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1725   unwrap(Builder)->SetInsertPoint(BB, I);
1726 }
1727 
LLVMPositionBuilderBefore(LLVMBuilderRef Builder,LLVMValueRef Instr)1728 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1729   Instruction *I = unwrap<Instruction>(Instr);
1730   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1731 }
1732 
LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder,LLVMBasicBlockRef Block)1733 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1734   BasicBlock *BB = unwrap(Block);
1735   unwrap(Builder)->SetInsertPoint(BB);
1736 }
1737 
LLVMGetInsertBlock(LLVMBuilderRef Builder)1738 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1739    return wrap(unwrap(Builder)->GetInsertBlock());
1740 }
1741 
LLVMClearInsertionPosition(LLVMBuilderRef Builder)1742 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1743   unwrap(Builder)->ClearInsertionPoint();
1744 }
1745 
LLVMInsertIntoBuilder(LLVMBuilderRef Builder,LLVMValueRef Instr)1746 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1747   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1748 }
1749 
LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder,LLVMValueRef Instr,const char * Name)1750 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1751                                    const char *Name) {
1752   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1753 }
1754 
LLVMDisposeBuilder(LLVMBuilderRef Builder)1755 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1756   delete unwrap(Builder);
1757 }
1758 
1759 /*--.. Metadata builders ...................................................--*/
1760 
LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder,LLVMValueRef L)1761 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1762   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1763   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1764 }
1765 
LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder)1766 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1767   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1768               .getAsMDNode(unwrap(Builder)->getContext()));
1769 }
1770 
LLVMSetInstDebugLocation(LLVMBuilderRef Builder,LLVMValueRef Inst)1771 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1772   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1773 }
1774 
1775 
1776 /*--.. Instruction builders ................................................--*/
1777 
LLVMBuildRetVoid(LLVMBuilderRef B)1778 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1779   return wrap(unwrap(B)->CreateRetVoid());
1780 }
1781 
LLVMBuildRet(LLVMBuilderRef B,LLVMValueRef V)1782 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1783   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1784 }
1785 
LLVMBuildAggregateRet(LLVMBuilderRef B,LLVMValueRef * RetVals,unsigned N)1786 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1787                                    unsigned N) {
1788   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1789 }
1790 
LLVMBuildBr(LLVMBuilderRef B,LLVMBasicBlockRef Dest)1791 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1792   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1793 }
1794 
LLVMBuildCondBr(LLVMBuilderRef B,LLVMValueRef If,LLVMBasicBlockRef Then,LLVMBasicBlockRef Else)1795 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1796                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1797   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1798 }
1799 
LLVMBuildSwitch(LLVMBuilderRef B,LLVMValueRef V,LLVMBasicBlockRef Else,unsigned NumCases)1800 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1801                              LLVMBasicBlockRef Else, unsigned NumCases) {
1802   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1803 }
1804 
LLVMBuildIndirectBr(LLVMBuilderRef B,LLVMValueRef Addr,unsigned NumDests)1805 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1806                                  unsigned NumDests) {
1807   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1808 }
1809 
LLVMBuildInvoke(LLVMBuilderRef B,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,LLVMBasicBlockRef Then,LLVMBasicBlockRef Catch,const char * Name)1810 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1811                              LLVMValueRef *Args, unsigned NumArgs,
1812                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1813                              const char *Name) {
1814   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1815                                       makeArrayRef(unwrap(Args), NumArgs),
1816                                       Name));
1817 }
1818 
LLVMBuildLandingPad(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef PersFn,unsigned NumClauses,const char * Name)1819 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1820                                  LLVMValueRef PersFn, unsigned NumClauses,
1821                                  const char *Name) {
1822   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1823                                           cast<Function>(unwrap(PersFn)),
1824                                           NumClauses, Name));
1825 }
1826 
LLVMBuildResume(LLVMBuilderRef B,LLVMValueRef Exn)1827 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1828   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1829 }
1830 
LLVMBuildUnreachable(LLVMBuilderRef B)1831 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1832   return wrap(unwrap(B)->CreateUnreachable());
1833 }
1834 
LLVMAddCase(LLVMValueRef Switch,LLVMValueRef OnVal,LLVMBasicBlockRef Dest)1835 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1836                  LLVMBasicBlockRef Dest) {
1837   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1838 }
1839 
LLVMAddDestination(LLVMValueRef IndirectBr,LLVMBasicBlockRef Dest)1840 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1841   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1842 }
1843 
LLVMAddClause(LLVMValueRef LandingPad,LLVMValueRef ClauseVal)1844 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1845   unwrap<LandingPadInst>(LandingPad)->
1846     addClause(cast<Constant>(unwrap(ClauseVal)));
1847 }
1848 
LLVMSetCleanup(LLVMValueRef LandingPad,LLVMBool Val)1849 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1850   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1851 }
1852 
1853 /*--.. Arithmetic ..........................................................--*/
1854 
LLVMBuildAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1855 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1856                           const char *Name) {
1857   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1858 }
1859 
LLVMBuildNSWAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1860 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1861                           const char *Name) {
1862   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1863 }
1864 
LLVMBuildNUWAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1865 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1866                           const char *Name) {
1867   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1868 }
1869 
LLVMBuildFAdd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1870 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1871                           const char *Name) {
1872   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1873 }
1874 
LLVMBuildSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1875 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1876                           const char *Name) {
1877   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1878 }
1879 
LLVMBuildNSWSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1880 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1881                           const char *Name) {
1882   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1883 }
1884 
LLVMBuildNUWSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1885 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1886                           const char *Name) {
1887   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1888 }
1889 
LLVMBuildFSub(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1890 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1891                           const char *Name) {
1892   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1893 }
1894 
LLVMBuildMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1895 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1896                           const char *Name) {
1897   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1898 }
1899 
LLVMBuildNSWMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1900 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1901                           const char *Name) {
1902   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1903 }
1904 
LLVMBuildNUWMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1905 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1906                           const char *Name) {
1907   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1908 }
1909 
LLVMBuildFMul(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1910 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1911                           const char *Name) {
1912   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1913 }
1914 
LLVMBuildUDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1915 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1916                            const char *Name) {
1917   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1918 }
1919 
LLVMBuildSDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1920 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1921                            const char *Name) {
1922   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1923 }
1924 
LLVMBuildExactSDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1925 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1926                                 LLVMValueRef RHS, const char *Name) {
1927   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1928 }
1929 
LLVMBuildFDiv(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1930 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1931                            const char *Name) {
1932   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1933 }
1934 
LLVMBuildURem(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1935 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1936                            const char *Name) {
1937   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1938 }
1939 
LLVMBuildSRem(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1940 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1941                            const char *Name) {
1942   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1943 }
1944 
LLVMBuildFRem(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1945 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1946                            const char *Name) {
1947   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1948 }
1949 
LLVMBuildShl(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1950 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1951                           const char *Name) {
1952   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1953 }
1954 
LLVMBuildLShr(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1955 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1956                            const char *Name) {
1957   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1958 }
1959 
LLVMBuildAShr(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1960 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1961                            const char *Name) {
1962   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1963 }
1964 
LLVMBuildAnd(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1965 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1966                           const char *Name) {
1967   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1968 }
1969 
LLVMBuildOr(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1970 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1971                          const char *Name) {
1972   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1973 }
1974 
LLVMBuildXor(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1975 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1976                           const char *Name) {
1977   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1978 }
1979 
LLVMBuildBinOp(LLVMBuilderRef B,LLVMOpcode Op,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)1980 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1981                             LLVMValueRef LHS, LLVMValueRef RHS,
1982                             const char *Name) {
1983   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
1984                                      unwrap(RHS), Name));
1985 }
1986 
LLVMBuildNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)1987 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1988   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1989 }
1990 
LLVMBuildNSWNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)1991 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1992                              const char *Name) {
1993   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1994 }
1995 
LLVMBuildNUWNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)1996 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1997                              const char *Name) {
1998   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1999 }
2000 
LLVMBuildFNeg(LLVMBuilderRef B,LLVMValueRef V,const char * Name)2001 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2002   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2003 }
2004 
LLVMBuildNot(LLVMBuilderRef B,LLVMValueRef V,const char * Name)2005 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2006   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2007 }
2008 
2009 /*--.. Memory ..............................................................--*/
2010 
LLVMBuildMalloc(LLVMBuilderRef B,LLVMTypeRef Ty,const char * Name)2011 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2012                              const char *Name) {
2013   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2014   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2015   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2016   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2017                                                ITy, unwrap(Ty), AllocSize,
2018                                                0, 0, "");
2019   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2020 }
2021 
LLVMBuildArrayMalloc(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Val,const char * Name)2022 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2023                                   LLVMValueRef Val, const char *Name) {
2024   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2025   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2026   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2027   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2028                                                ITy, unwrap(Ty), AllocSize,
2029                                                unwrap(Val), 0, "");
2030   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2031 }
2032 
LLVMBuildAlloca(LLVMBuilderRef B,LLVMTypeRef Ty,const char * Name)2033 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2034                              const char *Name) {
2035   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
2036 }
2037 
LLVMBuildArrayAlloca(LLVMBuilderRef B,LLVMTypeRef Ty,LLVMValueRef Val,const char * Name)2038 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2039                                   LLVMValueRef Val, const char *Name) {
2040   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2041 }
2042 
LLVMBuildFree(LLVMBuilderRef B,LLVMValueRef PointerVal)2043 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2044   return wrap(unwrap(B)->Insert(
2045      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2046 }
2047 
2048 
LLVMBuildLoad(LLVMBuilderRef B,LLVMValueRef PointerVal,const char * Name)2049 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2050                            const char *Name) {
2051   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2052 }
2053 
LLVMBuildStore(LLVMBuilderRef B,LLVMValueRef Val,LLVMValueRef PointerVal)2054 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2055                             LLVMValueRef PointerVal) {
2056   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2057 }
2058 
LLVMBuildGEP(LLVMBuilderRef B,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)2059 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2060                           LLVMValueRef *Indices, unsigned NumIndices,
2061                           const char *Name) {
2062   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2063   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2064 }
2065 
LLVMBuildInBoundsGEP(LLVMBuilderRef B,LLVMValueRef Pointer,LLVMValueRef * Indices,unsigned NumIndices,const char * Name)2066 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2067                                   LLVMValueRef *Indices, unsigned NumIndices,
2068                                   const char *Name) {
2069   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2070   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2071 }
2072 
LLVMBuildStructGEP(LLVMBuilderRef B,LLVMValueRef Pointer,unsigned Idx,const char * Name)2073 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2074                                 unsigned Idx, const char *Name) {
2075   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2076 }
2077 
LLVMBuildGlobalString(LLVMBuilderRef B,const char * Str,const char * Name)2078 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2079                                    const char *Name) {
2080   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2081 }
2082 
LLVMBuildGlobalStringPtr(LLVMBuilderRef B,const char * Str,const char * Name)2083 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2084                                       const char *Name) {
2085   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2086 }
2087 
LLVMGetVolatile(LLVMValueRef MemAccessInst)2088 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2089   Value *P = unwrap<Value>(MemAccessInst);
2090   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2091     return LI->isVolatile();
2092   return cast<StoreInst>(P)->isVolatile();
2093 }
2094 
LLVMSetVolatile(LLVMValueRef MemAccessInst,LLVMBool isVolatile)2095 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2096   Value *P = unwrap<Value>(MemAccessInst);
2097   if (LoadInst *LI = dyn_cast<LoadInst>(P))
2098     return LI->setVolatile(isVolatile);
2099   return cast<StoreInst>(P)->setVolatile(isVolatile);
2100 }
2101 
2102 /*--.. Casts ...............................................................--*/
2103 
LLVMBuildTrunc(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2104 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2105                             LLVMTypeRef DestTy, const char *Name) {
2106   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2107 }
2108 
LLVMBuildZExt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2109 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2110                            LLVMTypeRef DestTy, const char *Name) {
2111   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2112 }
2113 
LLVMBuildSExt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2114 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2115                            LLVMTypeRef DestTy, const char *Name) {
2116   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2117 }
2118 
LLVMBuildFPToUI(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2119 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2120                              LLVMTypeRef DestTy, const char *Name) {
2121   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2122 }
2123 
LLVMBuildFPToSI(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2124 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2125                              LLVMTypeRef DestTy, const char *Name) {
2126   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2127 }
2128 
LLVMBuildUIToFP(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2129 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2130                              LLVMTypeRef DestTy, const char *Name) {
2131   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2132 }
2133 
LLVMBuildSIToFP(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2134 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2135                              LLVMTypeRef DestTy, const char *Name) {
2136   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2137 }
2138 
LLVMBuildFPTrunc(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2139 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2140                               LLVMTypeRef DestTy, const char *Name) {
2141   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2142 }
2143 
LLVMBuildFPExt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2144 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2145                             LLVMTypeRef DestTy, const char *Name) {
2146   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2147 }
2148 
LLVMBuildPtrToInt(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2149 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2150                                LLVMTypeRef DestTy, const char *Name) {
2151   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2152 }
2153 
LLVMBuildIntToPtr(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2154 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2155                                LLVMTypeRef DestTy, const char *Name) {
2156   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2157 }
2158 
LLVMBuildBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2159 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2160                               LLVMTypeRef DestTy, const char *Name) {
2161   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2162 }
2163 
LLVMBuildZExtOrBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2164 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2165                                     LLVMTypeRef DestTy, const char *Name) {
2166   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2167                                              Name));
2168 }
2169 
LLVMBuildSExtOrBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2170 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2171                                     LLVMTypeRef DestTy, const char *Name) {
2172   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2173                                              Name));
2174 }
2175 
LLVMBuildTruncOrBitCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2176 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2177                                      LLVMTypeRef DestTy, const char *Name) {
2178   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2179                                               Name));
2180 }
2181 
LLVMBuildCast(LLVMBuilderRef B,LLVMOpcode Op,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2182 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2183                            LLVMTypeRef DestTy, const char *Name) {
2184   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2185                                     unwrap(DestTy), Name));
2186 }
2187 
LLVMBuildPointerCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2188 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2189                                   LLVMTypeRef DestTy, const char *Name) {
2190   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2191 }
2192 
LLVMBuildIntCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2193 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2194                               LLVMTypeRef DestTy, const char *Name) {
2195   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2196                                        /*isSigned*/true, Name));
2197 }
2198 
LLVMBuildFPCast(LLVMBuilderRef B,LLVMValueRef Val,LLVMTypeRef DestTy,const char * Name)2199 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2200                              LLVMTypeRef DestTy, const char *Name) {
2201   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2202 }
2203 
2204 /*--.. Comparisons .........................................................--*/
2205 
LLVMBuildICmp(LLVMBuilderRef B,LLVMIntPredicate Op,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)2206 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2207                            LLVMValueRef LHS, LLVMValueRef RHS,
2208                            const char *Name) {
2209   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2210                                     unwrap(LHS), unwrap(RHS), Name));
2211 }
2212 
LLVMBuildFCmp(LLVMBuilderRef B,LLVMRealPredicate Op,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)2213 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2214                            LLVMValueRef LHS, LLVMValueRef RHS,
2215                            const char *Name) {
2216   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2217                                     unwrap(LHS), unwrap(RHS), Name));
2218 }
2219 
2220 /*--.. Miscellaneous instructions ..........................................--*/
2221 
LLVMBuildPhi(LLVMBuilderRef B,LLVMTypeRef Ty,const char * Name)2222 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2223   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2224 }
2225 
LLVMBuildCall(LLVMBuilderRef B,LLVMValueRef Fn,LLVMValueRef * Args,unsigned NumArgs,const char * Name)2226 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2227                            LLVMValueRef *Args, unsigned NumArgs,
2228                            const char *Name) {
2229   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2230                                     makeArrayRef(unwrap(Args), NumArgs),
2231                                     Name));
2232 }
2233 
LLVMBuildSelect(LLVMBuilderRef B,LLVMValueRef If,LLVMValueRef Then,LLVMValueRef Else,const char * Name)2234 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2235                              LLVMValueRef Then, LLVMValueRef Else,
2236                              const char *Name) {
2237   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2238                                       Name));
2239 }
2240 
LLVMBuildVAArg(LLVMBuilderRef B,LLVMValueRef List,LLVMTypeRef Ty,const char * Name)2241 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2242                             LLVMTypeRef Ty, const char *Name) {
2243   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2244 }
2245 
LLVMBuildExtractElement(LLVMBuilderRef B,LLVMValueRef VecVal,LLVMValueRef Index,const char * Name)2246 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2247                                       LLVMValueRef Index, const char *Name) {
2248   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2249                                               Name));
2250 }
2251 
LLVMBuildInsertElement(LLVMBuilderRef B,LLVMValueRef VecVal,LLVMValueRef EltVal,LLVMValueRef Index,const char * Name)2252 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2253                                     LLVMValueRef EltVal, LLVMValueRef Index,
2254                                     const char *Name) {
2255   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2256                                              unwrap(Index), Name));
2257 }
2258 
LLVMBuildShuffleVector(LLVMBuilderRef B,LLVMValueRef V1,LLVMValueRef V2,LLVMValueRef Mask,const char * Name)2259 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2260                                     LLVMValueRef V2, LLVMValueRef Mask,
2261                                     const char *Name) {
2262   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2263                                              unwrap(Mask), Name));
2264 }
2265 
LLVMBuildExtractValue(LLVMBuilderRef B,LLVMValueRef AggVal,unsigned Index,const char * Name)2266 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2267                                    unsigned Index, const char *Name) {
2268   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2269 }
2270 
LLVMBuildInsertValue(LLVMBuilderRef B,LLVMValueRef AggVal,LLVMValueRef EltVal,unsigned Index,const char * Name)2271 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2272                                   LLVMValueRef EltVal, unsigned Index,
2273                                   const char *Name) {
2274   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2275                                            Index, Name));
2276 }
2277 
LLVMBuildIsNull(LLVMBuilderRef B,LLVMValueRef Val,const char * Name)2278 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2279                              const char *Name) {
2280   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2281 }
2282 
LLVMBuildIsNotNull(LLVMBuilderRef B,LLVMValueRef Val,const char * Name)2283 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2284                                 const char *Name) {
2285   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2286 }
2287 
LLVMBuildPtrDiff(LLVMBuilderRef B,LLVMValueRef LHS,LLVMValueRef RHS,const char * Name)2288 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2289                               LLVMValueRef RHS, const char *Name) {
2290   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2291 }
2292 
2293 
2294 /*===-- Module providers --------------------------------------------------===*/
2295 
2296 LLVMModuleProviderRef
LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M)2297 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2298   return reinterpret_cast<LLVMModuleProviderRef>(M);
2299 }
2300 
LLVMDisposeModuleProvider(LLVMModuleProviderRef MP)2301 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2302   delete unwrap(MP);
2303 }
2304 
2305 
2306 /*===-- Memory buffers ----------------------------------------------------===*/
2307 
LLVMCreateMemoryBufferWithContentsOfFile(const char * Path,LLVMMemoryBufferRef * OutMemBuf,char ** OutMessage)2308 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2309     const char *Path,
2310     LLVMMemoryBufferRef *OutMemBuf,
2311     char **OutMessage) {
2312 
2313   OwningPtr<MemoryBuffer> MB;
2314   error_code ec;
2315   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2316     *OutMemBuf = wrap(MB.take());
2317     return 0;
2318   }
2319 
2320   *OutMessage = strdup(ec.message().c_str());
2321   return 1;
2322 }
2323 
LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef * OutMemBuf,char ** OutMessage)2324 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2325                                          char **OutMessage) {
2326   OwningPtr<MemoryBuffer> MB;
2327   error_code ec;
2328   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2329     *OutMemBuf = wrap(MB.take());
2330     return 0;
2331   }
2332 
2333   *OutMessage = strdup(ec.message().c_str());
2334   return 1;
2335 }
2336 
LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf)2337 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2338   delete unwrap(MemBuf);
2339 }
2340 
2341 /*===-- Pass Registry -----------------------------------------------------===*/
2342 
LLVMGetGlobalPassRegistry(void)2343 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2344   return wrap(PassRegistry::getPassRegistry());
2345 }
2346 
2347 /*===-- Pass Manager ------------------------------------------------------===*/
2348 
LLVMCreatePassManager()2349 LLVMPassManagerRef LLVMCreatePassManager() {
2350   return wrap(new PassManager());
2351 }
2352 
LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M)2353 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2354   return wrap(new FunctionPassManager(unwrap(M)));
2355 }
2356 
LLVMCreateFunctionPassManager(LLVMModuleProviderRef P)2357 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2358   return LLVMCreateFunctionPassManagerForModule(
2359                                             reinterpret_cast<LLVMModuleRef>(P));
2360 }
2361 
LLVMRunPassManager(LLVMPassManagerRef PM,LLVMModuleRef M)2362 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2363   return unwrap<PassManager>(PM)->run(*unwrap(M));
2364 }
2365 
LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM)2366 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2367   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2368 }
2369 
LLVMRunFunctionPassManager(LLVMPassManagerRef FPM,LLVMValueRef F)2370 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2371   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2372 }
2373 
LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM)2374 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2375   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2376 }
2377 
LLVMDisposePassManager(LLVMPassManagerRef PM)2378 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2379   delete unwrap(PM);
2380 }
2381