Core.cpp 140 KB

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