Core.cpp 141 KB

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