Core.cpp 140 KB

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