Core.cpp 145 KB

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