Core.cpp 147 KB

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