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