Core.cpp 100 KB

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