Core.cpp 100 KB

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