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