BfIRBuilder.cpp 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902
  1. #pragma warning(disable:4146)
  2. #include "BeefySysLib/util/BeefPerf.h"
  3. #include "BfIRBuilder.h"
  4. #include "BfUtil.h"
  5. #include "BfModule.h"
  6. #include "BfContext.h"
  7. #include "BfResolvedTypeUtils.h"
  8. #include "BfIRCodeGen.h"
  9. #include "BfMangler.h"
  10. #include "BfCompiler.h"
  11. #include "BfSystem.h"
  12. #ifdef BF_PLATFORM_WINDOWS
  13. #include "../Backend/BeIRCodeGen.h"
  14. #endif
  15. #pragma warning(push)
  16. #pragma warning(disable:4141)
  17. #pragma warning(disable:4146)
  18. #pragma warning(disable:4291)
  19. #pragma warning(disable:4244)
  20. #pragma warning(disable:4267)
  21. #pragma warning(disable:4624)
  22. #pragma warning(disable:4800)
  23. #pragma warning(disable:4996)
  24. #include "llvm/IR/Module.h"
  25. #include "llvm/IR/Constants.h"
  26. #include "llvm/IR/GlobalValue.h"
  27. #include "llvm/IR/GlobalVariable.h"
  28. #include "llvm/ADT/ArrayRef.h"
  29. #include "llvm/IR/InlineAsm.h"
  30. #include "llvm/Support/FileSystem.h"
  31. #pragma warning(pop)
  32. static bool gDebugDbgLoc = false;
  33. USING_NS_BF;
  34. #define NEW_CMD_INSERTED NewCmdInserted()
  35. #define NEW_CMD_INSERTED_IRVALUE NewCmdInserted();
  36. #define NEW_CMD_INSERTED_IRTYPE NewCmdInserted();
  37. #define NEW_CMD_INSERTED_IRFUNCTYPE NewCmdInserted();
  38. #define NEW_CMD_INSERTED_IRBLOCK NewCmdInserted();
  39. #define NEW_CMD_INSERTED_IRMD NewCmdInserted();
  40. #define BINOPFUNC_APPLY(lhs, rhs, OP) \
  41. auto constLHS = GetConstantById(lhs.mId); \
  42. auto constRHS = GetConstantById(rhs.mId); \
  43. if (constLHS->mConstType == BfConstType_Undef) return lhs; \
  44. if (constRHS->mConstType == BfConstType_Undef) return rhs; \
  45. BF_ASSERT(constLHS->mTypeCode == constRHS->mTypeCode); \
  46. uint64 val = 0; \
  47. switch (constLHS->mTypeCode) \
  48. { \
  49. case BfTypeCode_Boolean: val = OP(constLHS->mInt8, constRHS->mInt8); break; \
  50. case BfTypeCode_Int8: val = OP(constLHS->mInt8, constRHS->mInt8); break; \
  51. case BfTypeCode_Char8: val = OP(constLHS->mUInt8, constRHS->mUInt8); break; \
  52. case BfTypeCode_UInt8: val = OP(constLHS->mUInt8, constRHS->mUInt8); break; \
  53. case BfTypeCode_Int16: val = OP(constLHS->mInt16, constRHS->mInt16); break; \
  54. case BfTypeCode_UInt16: val = OP(constLHS->mUInt16, constRHS->mUInt16); break; \
  55. case BfTypeCode_Char16: val = OP(constLHS->mUInt16, constRHS->mUInt16); break; \
  56. case BfTypeCode_Int32: val = OP(constLHS->mInt32, constRHS->mInt32); break; \
  57. case BfTypeCode_UInt32: val = OP(constLHS->mUInt32, constRHS->mUInt32); break; \
  58. case BfTypeCode_Char32: val = OP(constLHS->mUInt32, constRHS->mUInt32); break; \
  59. case BfTypeCode_Int64: val = OP(constLHS->mInt64, constRHS->mInt64); break; \
  60. case BfTypeCode_UInt64: val = OP(constLHS->mUInt64, constRHS->mUInt64); break; \
  61. case BfTypeCode_Single: \
  62. case BfTypeCode_Double: \
  63. return CreateConst(constLHS->mTypeCode, OP(constLHS->mDouble, constRHS->mDouble)); break; \
  64. default: break; \
  65. } \
  66. return CreateConst(constLHS->mTypeCode, val);
  67. #define INT_BINOPFUNC_APPLY(lhs, rhs, OP) \
  68. auto constLHS = GetConstantById(lhs.mId); \
  69. auto constRHS = GetConstantById(rhs.mId); \
  70. if (constLHS->mConstType == BfConstType_Undef) return lhs; \
  71. if (constRHS->mConstType == BfConstType_Undef) return rhs; \
  72. BF_ASSERT(constLHS->mTypeCode == constRHS->mTypeCode); \
  73. uint64 val = 0; \
  74. switch (constLHS->mTypeCode) \
  75. { \
  76. case BfTypeCode_Boolean: val = OP(constLHS->mInt8, constRHS->mInt8); break; \
  77. case BfTypeCode_Int8: val = OP(constLHS->mInt8, constRHS->mInt8); break; \
  78. case BfTypeCode_Char8: val = OP(constLHS->mUInt8, constRHS->mUInt8); break; \
  79. case BfTypeCode_UInt8: val = OP(constLHS->mUInt8, constRHS->mUInt8); break; \
  80. case BfTypeCode_Int16: val = OP(constLHS->mInt16, constRHS->mInt16); break; \
  81. case BfTypeCode_UInt16: val = OP(constLHS->mUInt16, constRHS->mUInt16); break; \
  82. case BfTypeCode_Char16: val = OP(constLHS->mUInt16, constRHS->mUInt16); break; \
  83. case BfTypeCode_Int32: val = OP(constLHS->mInt32, constRHS->mInt32); break; \
  84. case BfTypeCode_UInt32: val = OP(constLHS->mUInt32, constRHS->mUInt32); break; \
  85. case BfTypeCode_Char32: val = OP(constLHS->mUInt32, constRHS->mUInt32); break; \
  86. case BfTypeCode_Int64: val = OP(constLHS->mInt64, constRHS->mInt64); break; \
  87. case BfTypeCode_UInt64: val = OP(constLHS->mUInt64, constRHS->mUInt64); break; \
  88. default: break; \
  89. } \
  90. return CreateConst(constLHS->mTypeCode, val);
  91. #define BINOP_APPLY(lhs, rhs, OP) \
  92. auto constLHS = GetConstantById(lhs.mId); \
  93. auto constRHS = GetConstantById(rhs.mId); \
  94. if (constLHS->mConstType == BfConstType_Undef) return lhs; \
  95. if (constRHS->mConstType == BfConstType_Undef) return rhs; \
  96. BF_ASSERT(constLHS->mTypeCode == constRHS->mTypeCode); \
  97. uint64 val = 0; \
  98. switch (constLHS->mTypeCode) \
  99. { \
  100. case BfTypeCode_Boolean: val = constLHS->mInt8 OP constRHS->mInt8; break; \
  101. case BfTypeCode_Int8: val = constLHS->mInt8 OP constRHS->mInt8; break; \
  102. case BfTypeCode_Char8: val = constLHS->mUInt8 OP constRHS->mUInt8; break; \
  103. case BfTypeCode_UInt8: val = constLHS->mUInt8 OP constRHS->mUInt8; break; \
  104. case BfTypeCode_Int16: val = constLHS->mInt16 OP constRHS->mInt16; break; \
  105. case BfTypeCode_UInt16: val = constLHS->mUInt16 OP constRHS->mUInt16; break; \
  106. case BfTypeCode_Char16: val = constLHS->mUInt16 OP constRHS->mUInt16; break; \
  107. case BfTypeCode_Int32: val = constLHS->mInt32 OP constRHS->mInt32; break; \
  108. case BfTypeCode_UInt32: val = constLHS->mUInt32 OP constRHS->mUInt32; break; \
  109. case BfTypeCode_Char32: val = constLHS->mUInt32 OP constRHS->mUInt32; break; \
  110. case BfTypeCode_Int64: val = constLHS->mInt64 OP constRHS->mInt64; break; \
  111. case BfTypeCode_UInt64: val = constLHS->mUInt64 OP constRHS->mUInt64; break; \
  112. case BfTypeCode_Single: \
  113. case BfTypeCode_Double: \
  114. return CreateConst(constLHS->mTypeCode, constLHS->mDouble OP constRHS->mDouble); break; \
  115. default: break; \
  116. } \
  117. return CreateConst(constLHS->mTypeCode, val);
  118. #define INT_BINOP_APPLY(constLHS, constRHS, OP) \
  119. if (constLHS->mConstType == BfConstType_Undef) return lhs; \
  120. if (constRHS->mConstType == BfConstType_Undef) return rhs; \
  121. BF_ASSERT(constLHS->mTypeCode == constRHS->mTypeCode); \
  122. uint64 val = 0; \
  123. switch (constLHS->mTypeCode) \
  124. { \
  125. case BfTypeCode_Int8: val = constLHS->mInt8 OP constRHS->mInt8; break; \
  126. case BfTypeCode_Char8: val = constLHS->mUInt8 OP constRHS->mUInt8; break; \
  127. case BfTypeCode_UInt8: val = constLHS->mUInt8 OP constRHS->mUInt8; break; \
  128. case BfTypeCode_Int16: val = constLHS->mInt16 OP constRHS->mInt16; break; \
  129. case BfTypeCode_UInt16: val = constLHS->mUInt16 OP constRHS->mUInt16; break; \
  130. case BfTypeCode_Char16: val = constLHS->mUInt16 OP constRHS->mUInt16; break; \
  131. case BfTypeCode_Int32: val = constLHS->mInt32 OP constRHS->mInt32; break; \
  132. case BfTypeCode_UInt32: val = constLHS->mUInt32 OP constRHS->mUInt32; break; \
  133. case BfTypeCode_Char32: val = constLHS->mUInt32 OP constRHS->mUInt32; break; \
  134. case BfTypeCode_Int64: val = constLHS->mInt64 OP constRHS->mInt64; break; \
  135. case BfTypeCode_UInt64: val = constLHS->mUInt64 OP constRHS->mUInt64; break; \
  136. default: break; \
  137. } \
  138. return CreateConst(constLHS->mTypeCode, val);
  139. #define UNARYOP_APPLY(val, OP) \
  140. auto constVal = GetConstantById(val.mId); \
  141. if (constVal->mConstType == BfConstType_Undef) return val; \
  142. uint64 val = 0; \
  143. switch (constVal->mTypeCode) \
  144. { \
  145. case BfTypeCode_Int8: val = OP constVal->mInt8; break; \
  146. case BfTypeCode_UInt8: val = OP constVal->mUInt8; break; \
  147. case BfTypeCode_Char8: val = OP constVal->mUInt8; break; \
  148. case BfTypeCode_Int16: val = OP constVal->mInt16; break; \
  149. case BfTypeCode_UInt16: val = OP constVal->mUInt16; break; \
  150. case BfTypeCode_Char16: val = OP constVal->mUInt16; break; \
  151. case BfTypeCode_Int32: val = OP constVal->mInt32; break; \
  152. case BfTypeCode_UInt32: val = OP constVal->mUInt32; break; \
  153. case BfTypeCode_Char32: val = OP constVal->mUInt32; break; \
  154. case BfTypeCode_Int64: val = OP constVal->mInt64; break; \
  155. case BfTypeCode_UInt64: val = OP constVal->mUInt64; break; \
  156. case BfTypeCode_Single: \
  157. case BfTypeCode_Double: \
  158. return CreateConst(constVal->mTypeCode, OP constVal->mDouble); break; \
  159. default: break; \
  160. } \
  161. return CreateConst(constVal->mTypeCode, val);
  162. #define CMP_APPLY(lhs, rhs, OP) \
  163. auto constLHS = GetConstantById(lhs.mId); \
  164. auto constRHS = GetConstantById(rhs.mId); \
  165. if ((constLHS->mConstType == BfConstType_Undef) || (constRHS->mConstType == BfConstType_Undef)) \
  166. { \
  167. if (((constLHS->mConstType == BfConstType_Undef) || (constLHS->mTypeCode < BfTypeCode_Length)) && \
  168. ((constRHS->mConstType == BfConstType_Undef) || (constRHS->mTypeCode < BfTypeCode_Length))) \
  169. return GetUndefConstValue(BfTypeCode_Boolean); \
  170. } \
  171. if ((constLHS->mTypeCode < BfTypeCode_Length) && (constRHS->mTypeCode < BfTypeCode_Length)) \
  172. { \
  173. BF_ASSERT(constLHS->mTypeCode == constRHS->mTypeCode); \
  174. bool val = 0; \
  175. switch (constLHS->mTypeCode) \
  176. { \
  177. case BfTypeCode_Boolean: val = constLHS->mInt8 OP constRHS->mInt8; break; \
  178. case BfTypeCode_Int8: val = constLHS->mInt8 OP constRHS->mInt8; break; \
  179. case BfTypeCode_UInt8: val = constLHS->mUInt8 OP constRHS->mUInt8; break; \
  180. case BfTypeCode_Int16: val = constLHS->mInt16 OP constRHS->mInt16; break; \
  181. case BfTypeCode_UInt16: val = constLHS->mUInt16 OP constRHS->mUInt16; break; \
  182. case BfTypeCode_Int32: val = constLHS->mInt32 OP constRHS->mInt32; break; \
  183. case BfTypeCode_UInt32: val = constLHS->mUInt32 OP constRHS->mUInt32; break; \
  184. case BfTypeCode_Int64: val = constLHS->mInt64 OP constRHS->mInt64; break; \
  185. case BfTypeCode_UInt64: val = constLHS->mUInt64 OP constRHS->mUInt64; break; \
  186. case BfTypeCode_Single: val = constLHS->mDouble OP constRHS->mDouble; break; \
  187. case BfTypeCode_Double: val = constLHS->mDouble OP constRHS->mDouble; break; \
  188. default: break; \
  189. } \
  190. return CreateConst(BfTypeCode_Boolean, val ? (uint64)1 : (uint64)0); \
  191. }
  192. static llvm::GlobalValue::LinkageTypes LLVMMapLinkageType(BfIRLinkageType linkageType)
  193. {
  194. llvm::GlobalValue::LinkageTypes llvmLinkageType;
  195. if (linkageType == BfIRLinkageType_Internal)
  196. llvmLinkageType = llvm::GlobalValue::InternalLinkage;
  197. else
  198. llvmLinkageType = llvm::GlobalValue::ExternalLinkage;
  199. return llvmLinkageType;
  200. }
  201. BfIRValue BfIRValue::sValueless(BfIRValueFlags_Value, -1);
  202. bool BfIRValue::IsFake() const
  203. {
  204. return mId < -1;
  205. }
  206. bool BfIRValue::IsConst() const
  207. {
  208. return (mFlags & BfIRValueFlags_Const) != 0;
  209. }
  210. bool BfIRValue::IsArg() const
  211. {
  212. return (mFlags & BfIRValueFlags_Arg) != 0;
  213. }
  214. bool BfIRValue::IsFromLLVM() const
  215. {
  216. return (mFlags & BfIRValueFlags_FromLLVM) != 0;
  217. }
  218. //////////////////////////////////////////////////////////////////////////
  219. BfIRFunction::BfIRFunction()
  220. {
  221. //mFlags = BfIRValueFlags_None;
  222. mId = -1;
  223. }
  224. //////////////////////////////////////////////////////////////////////////
  225. BfIRFunctionType::BfIRFunctionType()
  226. {
  227. mId = -1;
  228. }
  229. //////////////////////////////////////////////////////////////////////////
  230. BfIRBlock::BfIRBlock()
  231. {
  232. mFlags = BfIRValueFlags_None;
  233. mId = -1;
  234. }
  235. //////////////////////////////////////////////////////////////////////////
  236. BfIRConstHolder::BfIRConstHolder(BfModule* module)
  237. {
  238. mModule = module;
  239. }
  240. BfIRConstHolder::~BfIRConstHolder()
  241. {
  242. }
  243. void BfIRConstHolder::FixTypeCode(BfTypeCode& typeCode)
  244. {
  245. if (typeCode == BfTypeCode_IntPtr)
  246. {
  247. if (mModule->mSystem->mPtrSize == 4)
  248. typeCode = BfTypeCode_Int32;
  249. else
  250. typeCode = BfTypeCode_Int64;
  251. }
  252. else if (typeCode == BfTypeCode_UIntPtr)
  253. {
  254. if (mModule->mSystem->mPtrSize == 4)
  255. typeCode = BfTypeCode_UInt32;
  256. else
  257. typeCode = BfTypeCode_UInt64;
  258. }
  259. }
  260. bool BfIRConstHolder::IsInt(BfTypeCode typeCode)
  261. {
  262. return (typeCode >= BfTypeCode_Int8) && (typeCode <= BfTypeCode_Char32);
  263. }
  264. bool BfIRConstHolder::IsSigned(BfTypeCode typeCode)
  265. {
  266. return (typeCode == BfTypeCode_Int64) ||
  267. (typeCode == BfTypeCode_Int32) ||
  268. (typeCode == BfTypeCode_Int16) ||
  269. (typeCode == BfTypeCode_Int8) ||
  270. (typeCode == BfTypeCode_IntPtr);
  271. }
  272. bool BfIRConstHolder::IsFloat(BfTypeCode typeCode)
  273. {
  274. return (typeCode == BfTypeCode_Single) ||
  275. (typeCode == BfTypeCode_Double);
  276. }
  277. const char* BfIRConstHolder::AllocStr(const StringImpl& str)
  278. {
  279. char* strCopy = (char*)mTempAlloc.AllocBytes((int)str.length() + 1);
  280. memcpy(strCopy, str.c_str(), str.length());
  281. return strCopy;
  282. }
  283. BfConstant* BfIRConstHolder::GetConstantById(int id)
  284. {
  285. return (BfConstant*)mTempAlloc.GetChunkedPtr(id);
  286. }
  287. BfConstant* BfIRConstHolder::GetConstant(BfIRValue id)
  288. {
  289. if (!id.IsConst())
  290. return NULL;
  291. #ifdef CHECK_CONSTHOLDER
  292. BF_ASSERT(id.mHolder == this);
  293. #endif
  294. return GetConstantById(id.mId);
  295. }
  296. bool BfIRConstHolder::TryGetBool(BfIRValue id, bool& boolVal)
  297. {
  298. auto constant = GetConstant(id);
  299. if ((constant != NULL) && (constant->mTypeCode == BfTypeCode_Boolean))
  300. {
  301. boolVal = constant->mBool;
  302. return true;
  303. }
  304. return false;
  305. }
  306. BfIRValue BfIRConstHolder::CreateConst(BfTypeCode typeCode, uint64 val)
  307. {
  308. if (typeCode == BfTypeCode_IntUnknown)
  309. typeCode = BfTypeCode_Int64;
  310. else if (typeCode == BfTypeCode_UIntUnknown)
  311. typeCode = BfTypeCode_UInt64;
  312. FixTypeCode(typeCode);
  313. BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
  314. constant->mTypeCode = typeCode;
  315. // Properly sign extend into int64
  316. switch (typeCode)
  317. {
  318. case BfTypeCode_Int8:
  319. constant->mInt64 = (int8)val;
  320. break;
  321. case BfTypeCode_Int16:
  322. constant->mInt64 = (int16)val;
  323. break;
  324. case BfTypeCode_Int32:
  325. constant->mInt64 = (int32)val;
  326. break;
  327. case BfTypeCode_UInt8:
  328. constant->mInt8 = (uint8)val;
  329. break;
  330. case BfTypeCode_UInt16:
  331. constant->mInt64 = (uint16)val;
  332. break;
  333. case BfTypeCode_UInt32:
  334. constant->mInt64 = (uint32)val;
  335. break;
  336. default:
  337. constant->mUInt64 = val;
  338. break;
  339. }
  340. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  341. #ifdef CHECK_CONSTHOLDER
  342. irValue.mHolder = this;
  343. #endif
  344. BF_ASSERT(GetConstant(irValue) == constant);
  345. return irValue;
  346. }
  347. BfIRValue BfIRConstHolder::CreateConst(BfTypeCode typeCode, int val)
  348. {
  349. FixTypeCode(typeCode);
  350. BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
  351. constant->mTypeCode = typeCode;
  352. // Properly sign extend into int64
  353. switch (typeCode)
  354. {
  355. case BfTypeCode_Int8:
  356. constant->mInt64 = (int8)val;
  357. break;
  358. case BfTypeCode_Int16:
  359. constant->mInt64 = (int16)val;
  360. break;
  361. case BfTypeCode_UInt8:
  362. constant->mInt64 = (uint8)val;
  363. break;
  364. case BfTypeCode_UInt16:
  365. constant->mInt64 = (uint16)val;
  366. break;
  367. case BfTypeCode_UInt32:
  368. constant->mInt64 = (uint32)val;
  369. break;
  370. default:
  371. constant->mInt64 = val;
  372. break;
  373. }
  374. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  375. #ifdef CHECK_CONSTHOLDER
  376. irValue.mHolder = this;
  377. #endif
  378. return irValue;
  379. }
  380. BfIRValue BfIRConstHolder::CreateConst(BfTypeCode typeCode, double val)
  381. {
  382. BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
  383. constant->mTypeCode = typeCode;
  384. constant->mDouble = val;
  385. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  386. #ifdef CHECK_CONSTHOLDER
  387. irValue.mHolder = this;
  388. #endif
  389. return irValue;
  390. }
  391. BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* fromHolder)
  392. {
  393. BfConstant* copiedConst = NULL;
  394. if ((fromConst->mConstType == BfConstType_BitCast) || (fromConst->mConstType == BfConstType_BitCastNull))
  395. {
  396. //HMM- This should never happen? Is that true? We always just store string refs as ints
  397. //BF_FATAL("Bad");
  398. auto fromConstBitCast = (BfConstantBitCast*)fromConst;
  399. BfIRValue copiedTarget;
  400. if (fromConstBitCast->mTarget)
  401. {
  402. auto fromTarget = fromHolder->GetConstantById(fromConstBitCast->mTarget);
  403. copiedTarget = CreateConst(fromTarget, fromHolder);
  404. }
  405. auto ptrToInt = mTempAlloc.Alloc<BfConstantBitCast>();
  406. ptrToInt->mConstType = fromConst->mConstType;
  407. ptrToInt->mTarget = copiedTarget.mId;
  408. ptrToInt->mToType = fromConstBitCast->mToType;
  409. copiedConst = (BfConstant*)ptrToInt;
  410. }
  411. else if (fromConst->mConstType == BfConstType_GEP32_2)
  412. {
  413. auto fromConstGEP = (BfConstantGEP32_2*)fromConst;
  414. auto fromTarget = fromHolder->GetConstantById(fromConstGEP->mTarget);
  415. auto copiedTarget = CreateConst(fromTarget, fromHolder);
  416. auto constGEP = mTempAlloc.Alloc<BfConstantGEP32_2>();
  417. constGEP->mConstType = BfConstType_GEP32_2;
  418. constGEP->mTarget = copiedTarget.mId;
  419. copiedConst = (BfConstant*)constGEP;
  420. }
  421. else if (fromConst->mConstType == BfConstType_TypeOf)
  422. {
  423. auto typeOf = (BfTypeOf_Const*)fromConst;
  424. return CreateTypeOf(typeOf->mType);
  425. }
  426. else if (fromConst->mConstType == BfConstType_AggZero)
  427. {
  428. auto aggZero = (BfConstant*)fromConst;
  429. return CreateConstStructZero(fromConst->mIRType);
  430. }
  431. else if (fromConst->mConstType == BfConstType_Array)
  432. {
  433. auto constArray = (BfConstantArray*)fromConst;
  434. BfSizedVector<BfIRValue, 8> copiedVals;
  435. copiedVals.reserve(constArray->mValues.size());
  436. for (auto fromVal : constArray->mValues)
  437. {
  438. auto elementConst = fromHolder->GetConstant(fromVal);
  439. copiedVals.push_back(CreateConst(elementConst, fromHolder));
  440. }
  441. return CreateConstArray(constArray->mType, copiedVals);
  442. }
  443. else if ((IsInt(fromConst->mTypeCode)) || (fromConst->mTypeCode == BfTypeCode_Boolean))
  444. {
  445. return CreateConst(fromConst->mTypeCode, fromConst->mUInt64);
  446. }
  447. else if ((fromConst->mTypeCode == BfTypeCode_Single) || (fromConst->mTypeCode == BfTypeCode_Double))
  448. {
  449. return CreateConst(fromConst->mTypeCode, fromConst->mDouble);
  450. }
  451. else if (fromConst->mTypeCode == BfTypeCode_NullPtr)
  452. {
  453. return CreateConstNull();
  454. }
  455. else
  456. {
  457. BF_FATAL("not handled");
  458. }
  459. BfIRValue retVal;
  460. retVal.mFlags = BfIRValueFlags_Const;
  461. retVal.mId = mTempAlloc.GetChunkedId(copiedConst);
  462. #ifdef CHECK_CONSTHOLDER
  463. retVal.mHolder = this;
  464. #endif
  465. return retVal;
  466. }
  467. BfIRValue BfIRConstHolder::CreateConstNull()
  468. {
  469. BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
  470. constant->mTypeCode = BfTypeCode_NullPtr;
  471. constant->mIRType = BfIRType();
  472. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  473. #ifdef CHECK_CONSTHOLDER
  474. irValue.mHolder = this;
  475. #endif
  476. return irValue;
  477. }
  478. BfIRValue BfIRConstHolder::CreateConstNull(BfIRType ptrType)
  479. {
  480. BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
  481. constant->mTypeCode = BfTypeCode_NullPtr;
  482. constant->mIRType = ptrType;
  483. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  484. #ifdef CHECK_CONSTHOLDER
  485. irValue.mHolder = this;
  486. #endif
  487. return irValue;
  488. }
  489. BfIRValue BfIRConstHolder::CreateConstStructZero(BfIRType aggType)
  490. {
  491. BfConstant* constant = mTempAlloc.Alloc<BfConstant>();
  492. constant->mConstType = BfConstType_AggZero;
  493. constant->mIRType = aggType;
  494. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  495. #ifdef CHECK_CONSTHOLDER
  496. irValue.mHolder = this;
  497. #endif
  498. return irValue;
  499. }
  500. BfIRValue BfIRConstHolder::CreateConstArray(BfIRType type, const BfSizedArray<BfIRValue>& values)
  501. {
  502. BfConstantArray* constant = mTempAlloc.Alloc<BfConstantArray>();
  503. constant->mConstType = BfConstType_Array;
  504. constant->mType = type = type;
  505. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constant));
  506. constant->mValues.mVals = (BfIRValue*)mTempAlloc.AllocBytes(sizeof(BfIRValue) * values.mSize, 8);
  507. memcpy(constant->mValues.mVals, values.mVals, sizeof(BfIRValue) * values.mSize);
  508. constant->mValues.mSize = values.mSize;
  509. #ifdef CHECK_CONSTHOLDER
  510. irValue.mHolder = this;
  511. #endif
  512. return irValue;
  513. }
  514. BfIRValue BfIRConstHolder::CreateTypeOf(BfType* type)
  515. {
  516. BfTypeOf_Const* typeOf = mTempAlloc.Alloc<BfTypeOf_Const>();
  517. typeOf->mConstType = BfConstType_TypeOf;
  518. typeOf->mType = type;
  519. auto irValue = BfIRValue(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(typeOf));
  520. #ifdef CHECK_CONSTHOLDER
  521. irValue.mHolder = this;
  522. #endif
  523. return irValue;
  524. }
  525. BfIRValue BfIRConstHolder::GetUndefConstValue(BfTypeCode typeCode)
  526. {
  527. auto constUndef = mTempAlloc.Alloc<BfConstantUndef>();
  528. constUndef->mConstType = BfConstType_Undef;
  529. constUndef->mTypeCode = typeCode;
  530. BfIRValue undefVal(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(constUndef));
  531. #ifdef CHECK_CONSTHOLDER
  532. castedVal.mHolder = this;
  533. #endif
  534. BF_ASSERT((void*)GetConstant(undefVal) == (void*)constUndef);
  535. return undefVal;
  536. }
  537. //////////////////////////////////////////////////////////////////////////
  538. void BfIRBuilder::OpFailed()
  539. {
  540. mOpFailed = true;
  541. }
  542. uint8 BfIRBuilder::CheckedAdd(uint8 a, uint8 b)
  543. {
  544. uint32 result = (uint32)a + b;
  545. if (result & 0xFFFFFF00)
  546. OpFailed();
  547. return (uint8)result;
  548. }
  549. uint16 BfIRBuilder::CheckedAdd(uint16 a, uint16 b)
  550. {
  551. uint32 result = (uint32)a + b;
  552. if (result & 0xFFFF0000)
  553. OpFailed();
  554. return (uint16)result;
  555. }
  556. uint32 BfIRBuilder::CheckedAdd(uint32 a, uint32 b)
  557. {
  558. uint32 result = a + b;
  559. if (result < a)
  560. OpFailed();
  561. return (uint32)result;
  562. }
  563. uint64 BfIRBuilder::CheckedAdd(uint64 a, uint64 b)
  564. {
  565. uint64 result = a + b;
  566. if (result < a)
  567. OpFailed();
  568. return (uint64)result;
  569. }
  570. int8 BfIRBuilder::CheckedAdd(int8 a, int8 b)
  571. {
  572. int32 result = (int32)a + b;
  573. if ((result > 0x7F) || (result < -0x80))
  574. OpFailed();
  575. return (int8)result;
  576. }
  577. int16 BfIRBuilder::CheckedAdd(int16 a, int16 b)
  578. {
  579. int32 result = (int32)a + b;
  580. if ((result > 0x7FFF) || (result < -0x8000))
  581. OpFailed();
  582. return (int16)result;
  583. }
  584. int32 BfIRBuilder::CheckedAdd(int32 a, int32 b)
  585. {
  586. int32 result = a + b;
  587. if (b >= 0)
  588. {
  589. if (result < a)
  590. OpFailed();
  591. }
  592. else if (result > a)
  593. OpFailed();
  594. return (uint32)result;
  595. }
  596. int64 BfIRBuilder::CheckedAdd(int64 a, int64 b)
  597. {
  598. int64 result = a + b;
  599. if (b >= 0)
  600. {
  601. if (result < a)
  602. OpFailed();
  603. }
  604. else if (result > a)
  605. OpFailed();
  606. return (uint64)result;
  607. }
  608. ///
  609. uint8 BfIRBuilder::CheckedSub(uint8 a, uint8 b)
  610. {
  611. uint32 result = (uint32)a - b;
  612. if (result & 0xFFFFFF00)
  613. OpFailed();
  614. return (uint8)result;
  615. }
  616. uint16 BfIRBuilder::CheckedSub(uint16 a, uint16 b)
  617. {
  618. uint32 result = (uint32)a - b;
  619. if (result & 0xFFFF0000)
  620. OpFailed();
  621. return (uint16)result;
  622. }
  623. uint32 BfIRBuilder::CheckedSub(uint32 a, uint32 b)
  624. {
  625. uint32 result = a - b;
  626. if (result > a)
  627. OpFailed();
  628. return (uint32)result;
  629. }
  630. uint64 BfIRBuilder::CheckedSub(uint64 a, uint64 b)
  631. {
  632. uint64 result = a - b;
  633. if (result > a)
  634. OpFailed();
  635. return (uint64)result;
  636. }
  637. int8 BfIRBuilder::CheckedSub(int8 a, int8 b)
  638. {
  639. int32 result = (int32)a - b;
  640. if ((result > 0x7F) || (result < -0x80))
  641. OpFailed();
  642. return (int8)result;
  643. }
  644. int16 BfIRBuilder::CheckedSub(int16 a, int16 b)
  645. {
  646. int32 result = (int32)a - b;
  647. if ((result > 0x7FFF) || (result < -0x8000))
  648. OpFailed();
  649. return (int16)result;
  650. }
  651. int32 BfIRBuilder::CheckedSub(int32 a, int32 b)
  652. {
  653. int32 result = a - b;
  654. if (b >= 0)
  655. {
  656. if (result > a)
  657. OpFailed();
  658. }
  659. else if (result < a)
  660. OpFailed();
  661. return (uint32)result;
  662. }
  663. int64 BfIRBuilder::CheckedSub(int64 a, int64 b)
  664. {
  665. int64 result = a - b;
  666. if (b >= 0)
  667. {
  668. if (result > a)
  669. OpFailed();
  670. }
  671. else if (result < a)
  672. OpFailed();
  673. return (uint64)result;
  674. }
  675. ///
  676. uint8 BfIRBuilder::CheckedMul(uint8 a, uint8 b)
  677. {
  678. int result = (uint32)a * b;
  679. if (result & 0xFFFFFF00)
  680. OpFailed();
  681. return (uint8)result;
  682. }
  683. uint16 BfIRBuilder::CheckedMul(uint16 a, uint16 b)
  684. {
  685. int result = (uint32)a * b;
  686. if (result & 0xFFFF0000)
  687. OpFailed();
  688. return (uint16)result;
  689. }
  690. uint32 BfIRBuilder::CheckedMul(uint32 a, uint32 b)
  691. {
  692. uint64 result = (uint64)a * b;
  693. uint32 upper = (uint32)(result >> 32);
  694. if ((upper != 0) && (upper != 0xFFFFFFFF))
  695. OpFailed();
  696. return (uint32)result;
  697. }
  698. uint64 BfIRBuilder::CheckedMul(uint64 a, uint64 b)
  699. {
  700. uint32 aHigh;
  701. uint32 aLow;
  702. uint32 bHigh;
  703. uint32 bLow;
  704. // a*b can be decomposed to
  705. // (aHigh * bHigh * 2^64) + (aLow * bHigh * 2^32) + (aHigh * bLow * 2^32) + (aLow * bLow)
  706. aHigh = (uint32)(a >> 32);
  707. aLow = (uint32)(a);
  708. bHigh = (uint32)(b >> 32);
  709. bLow = (uint32)(b);
  710. uint64 ret = 0;
  711. if (aHigh == 0)
  712. {
  713. if (bHigh != 0)
  714. ret = (uint64)aLow * (uint64)bHigh;
  715. }
  716. else if (bHigh == 0)
  717. {
  718. if (aHigh != 0)
  719. ret = (uint64)aHigh * (uint64)bLow;
  720. }
  721. else
  722. OpFailed();
  723. if (ret != 0)
  724. {
  725. uint64 tmp;
  726. if((uint32)(ret >> 32) != 0)
  727. OpFailed();
  728. ret <<= 32;
  729. tmp = (uint64)aLow * (uint64)bLow;
  730. ret += tmp;
  731. if (ret < tmp)
  732. OpFailed();
  733. return ret;
  734. }
  735. return (uint64)aLow * (uint64)bLow;
  736. }
  737. int8 BfIRBuilder::CheckedMul(int8 a, int8 b)
  738. {
  739. int32 result = (int32)a * b;
  740. if ((result > 0x7F) || (result < -0x80))
  741. OpFailed();
  742. return (int8)result;
  743. }
  744. int16 BfIRBuilder::CheckedMul(int16 a, int16 b)
  745. {
  746. int result = a + b;
  747. if ((result > 0x7FFF) || (result < -0x8000))
  748. OpFailed();
  749. return (int16)result;
  750. }
  751. int32 BfIRBuilder::CheckedMul(int32 a, int32 b)
  752. {
  753. int64 result = (int64)a * b;
  754. int32 upper = (int32)(result >> 32);
  755. if ((upper != 0) && (upper != 0xFFFFFFFF))
  756. OpFailed();
  757. return (int32)result;
  758. }
  759. int64 BfIRBuilder::CheckedMul(int64 a, int64 b)
  760. {
  761. bool aNegative = false;
  762. int64 aAbs = a;
  763. if (aAbs < 0)
  764. {
  765. aNegative = true;
  766. aAbs = -aAbs;
  767. }
  768. int64 bAbs = b;
  769. bool bNegative = false;
  770. if (bAbs < 0)
  771. {
  772. bNegative = true;
  773. bAbs = -bAbs;
  774. }
  775. uint64 tmp = CheckedMul((uint64)aAbs, (uint64)bAbs);
  776. // Don't allow overflow into sign flag
  777. if (tmp & 0x8000000000000000LL)
  778. OpFailed();
  779. if (aNegative ^ bNegative)
  780. return -(int64)tmp;
  781. return (int64)tmp;
  782. }
  783. ///
  784. uint8 BfIRBuilder::CheckedShl(uint8 a, uint8 b)
  785. {
  786. if ((a != 0) && (b >= 8))
  787. OpFailed();
  788. uint32 result = (uint32)a << b;
  789. if (result & 0xFFFFFF00)
  790. OpFailed();
  791. return (uint8)result;
  792. }
  793. uint16 BfIRBuilder::CheckedShl(uint16 a, uint16 b)
  794. {
  795. if ((a != 0) && (b >= 16))
  796. OpFailed();
  797. uint32 result = (uint32)a << b;
  798. if (result & 0xFFFF0000)
  799. OpFailed();
  800. return (uint16)result;
  801. }
  802. uint32 BfIRBuilder::CheckedShl(uint32 a, uint32 b)
  803. {
  804. if ((a != 0) && (b >= 32))
  805. OpFailed();
  806. uint32 result = a << b;
  807. if (result < a)
  808. OpFailed();
  809. return (uint32)result;
  810. }
  811. uint64 BfIRBuilder::CheckedShl(uint64 a, uint64 b)
  812. {
  813. if ((a != 0) && (b >= 64))
  814. OpFailed();
  815. uint64 result = a << b;
  816. if (result < a)
  817. OpFailed();
  818. return (uint64)result;
  819. }
  820. int8 BfIRBuilder::CheckedShl(int8 a, int8 b)
  821. {
  822. if ((a != 0) && (b >= 8))
  823. OpFailed();
  824. int32 result = (int32)a << b;
  825. if ((result > 0x7F) || (result < -0x80))
  826. OpFailed();
  827. return (int8)result;
  828. }
  829. int16 BfIRBuilder::CheckedShl(int16 a, int16 b)
  830. {
  831. if ((a != 0) && (b >= 16))
  832. OpFailed();
  833. int32 result = (int32)a << b;
  834. if ((result > 0x7FFF) || (result < -0x8000))
  835. OpFailed();
  836. return (int16)result;
  837. }
  838. int32 BfIRBuilder::CheckedShl(int32 a, int32 b)
  839. {
  840. if ((a != 0) && (b >= 32))
  841. OpFailed();
  842. int64 result = (int64)a << b;
  843. if ((result > 0x7FFFFFFFLL) || (result < -0x80000000LL))
  844. OpFailed();
  845. return (int32)result;
  846. }
  847. int64 BfIRBuilder::CheckedShl(int64 a, int64 b)
  848. {
  849. if ((a != 0) && (b >= 64))
  850. OpFailed();
  851. int64 result = (int64)a << b;
  852. if (((a > 0) && (result < a)) ||
  853. ((a < 0) && (result > a)))
  854. OpFailed();
  855. return result;
  856. }
  857. //////////////////////////////////////////////////////////////////////////
  858. BfIRBuilder::BfIRBuilder(BfModule* module) : BfIRConstHolder(module)
  859. {
  860. mBlockCount = 0;
  861. mModule = module;
  862. mHasDebugLoc = false;
  863. mHasDebugInfo = false;
  864. mIRCodeGen = NULL;
  865. #ifdef BF_PLATFORM_WINDOWS
  866. mBeIRCodeGen = NULL;
  867. #endif
  868. mBfIRCodeGen = NULL;
  869. mDbgVerifyCodeGen = false;
  870. mIgnoreWrites = false;
  871. mCurFakeId = -32;
  872. mOpFailed = false;
  873. mHasGlobalDefs = false;
  874. mNumFunctionsWithBodies = 0;
  875. mActiveFunctionHasBody = false;
  876. mHasStarted = false;
  877. mCmdCount = 0;
  878. mIsBeefBackend = false;
  879. }
  880. bool BfIRBuilder::HasExports()
  881. {
  882. return mHasGlobalDefs || (mNumFunctionsWithBodies > 0);
  883. }
  884. BfIRBuilder::~BfIRBuilder()
  885. {
  886. if (mIRCodeGen != NULL)
  887. {
  888. mIRCodeGen->mStream = NULL;
  889. delete mIRCodeGen;
  890. }
  891. BF_ASSERT(mSavedDebugLocs.size() == 0);
  892. }
  893. String BfIRBuilder::ToString(BfIRValue irValue)
  894. {
  895. if ((irValue.mFlags & BfIRValueFlags_Const) != 0)
  896. {
  897. auto constant = GetConstantById(irValue.mId);
  898. if (constant->mTypeCode == BfTypeCode_None)
  899. {
  900. return "void";
  901. }
  902. else if (constant->mTypeCode == BfTypeCode_NullPtr)
  903. {
  904. if (constant->mInt32 == -1)
  905. return "null";
  906. else if (mBfIRCodeGen != NULL)
  907. {
  908. auto llvmType = mBfIRCodeGen->GetLLVMType(constant->mInt32);
  909. std::string outStr;
  910. llvm::raw_string_ostream strStream(outStr);
  911. llvmType->print(strStream);
  912. strStream.flush();
  913. return StrFormat("null(%s)", outStr.c_str());
  914. }
  915. else
  916. return "???";
  917. }
  918. else if (constant->mTypeCode == BfTypeCode_Boolean)
  919. {
  920. return constant->mBool ? "true" : "false";
  921. }
  922. else if (constant->mTypeCode == BfTypeCode_Single)
  923. {
  924. return StrFormat("Constant %ff", constant->mDouble);
  925. }
  926. else if (constant->mTypeCode == BfTypeCode_Double)
  927. {
  928. return StrFormat("Constant %f", constant->mDouble);
  929. }
  930. else if (IsInt(constant->mTypeCode))
  931. {
  932. return StrFormat("Constant %lld", constant->mInt64);
  933. }
  934. else if (constant->mConstType == BfConstType_GlobalVar)
  935. {
  936. if (mBfIRCodeGen != NULL)
  937. {
  938. auto gvConst = (BfGlobalVar*)constant;
  939. auto val = mBfIRCodeGen->GetLLVMValue(gvConst->mStreamId);
  940. std::string outStr;
  941. llvm::raw_string_ostream strStream(outStr);
  942. val->print(strStream);
  943. strStream.flush();
  944. return outStr;
  945. }
  946. else
  947. return "???";
  948. }
  949. else if (constant->mConstType == BfConstType_BitCast)
  950. {
  951. auto bitcast = (BfConstantBitCast*)constant;
  952. BfIRValue targetConst(BfIRValueFlags_Const, bitcast->mTarget);
  953. return ToString(targetConst) + " BitCast to " + ToString(bitcast->mToType);
  954. }
  955. else if (constant->mConstType == BfConstType_GEP32_2)
  956. {
  957. auto gepConst = (BfConstantGEP32_2*)constant;
  958. BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
  959. return ToString(targetConst) + StrFormat(" Gep32 %d,%d", gepConst->mIdx0, gepConst->mIdx1);
  960. }
  961. else if (constant->mConstType == BfConstType_PtrToInt)
  962. {
  963. auto ptrToIntConst = (BfConstantPtrToInt*)constant;
  964. BfIRValue targetConst(BfIRValueFlags_Const, ptrToIntConst->mTarget);
  965. return ToString(targetConst) + StrFormat(" PtrToInt TypeCode:%d", ptrToIntConst->mToTypeCode);
  966. }
  967. else
  968. {
  969. BF_FATAL("Unhandled");
  970. }
  971. }
  972. else if ((irValue.mFlags & BfIRValueFlags_Arg) != 0)
  973. {
  974. return StrFormat("Arg %d in %s", irValue.mId, ActiveFuncToString().c_str());
  975. }
  976. else if (irValue.mFlags != 0)
  977. {
  978. if (mBfIRCodeGen != NULL)
  979. {
  980. auto val = mBfIRCodeGen->GetLLVMValue(irValue.mId);
  981. std::string outStr;
  982. llvm::raw_string_ostream strStream(outStr);
  983. val->print(strStream);
  984. strStream << "\n Type: ";
  985. val->getType()->print(strStream);
  986. strStream.flush();
  987. return outStr;
  988. }
  989. #ifdef BF_PLATFORM_WINDOWS
  990. else if (mBeIRCodeGen != NULL)
  991. {
  992. auto val = mBeIRCodeGen->GetBeValue(irValue.mId);
  993. String str;
  994. BeDumpContext dc;
  995. dc.mModule = mBeIRCodeGen->mBeModule;
  996. dc.ToString(str, val);
  997. return str;
  998. }
  999. #endif
  1000. else
  1001. return "???";
  1002. }
  1003. else
  1004. {
  1005. BF_ASSERT(irValue.mId == -1);
  1006. }
  1007. return "empty";
  1008. }
  1009. String BfIRBuilder::ToString(BfIRType irType)
  1010. {
  1011. if (mBfIRCodeGen != NULL)
  1012. {
  1013. llvm::Type* llvmType = NULL;
  1014. if (irType.mKind == BfIRTypeData::TypeKind_Stream)
  1015. {
  1016. llvmType = mBfIRCodeGen->GetLLVMType(irType.mId);
  1017. }
  1018. else
  1019. {
  1020. auto& typeEntry = mBfIRCodeGen->GetTypeEntry(irType.mId);
  1021. if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeId)
  1022. llvmType = typeEntry.mLLVMType;
  1023. else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeInstId)
  1024. llvmType = typeEntry.mInstLLVMType;
  1025. else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeInstPtrId)
  1026. llvmType = typeEntry.mInstLLVMType->getPointerTo();
  1027. }
  1028. if (llvmType == NULL)
  1029. return "null";
  1030. std::string outStr;
  1031. llvm::raw_string_ostream strStream(outStr);
  1032. llvmType->print(strStream);
  1033. if (auto pointerType = llvm::dyn_cast<llvm::PointerType>(llvmType))
  1034. {
  1035. strStream << "\n ElementType: ";
  1036. pointerType->getElementType()->print(strStream);
  1037. }
  1038. strStream.flush();
  1039. return outStr;
  1040. }
  1041. #ifdef BF_PLATFORM_WINDOWS
  1042. else if (mBeIRCodeGen != NULL)
  1043. {
  1044. BeType* beType;
  1045. if (irType.mKind == BfIRTypeData::TypeKind_Stream)
  1046. {
  1047. beType = mBeIRCodeGen->GetBeType(irType.mId);
  1048. }
  1049. else
  1050. {
  1051. auto& typeEntry = mBeIRCodeGen->GetTypeEntry(irType.mId);
  1052. if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeId)
  1053. beType = typeEntry.mBeType;
  1054. else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeInstId)
  1055. beType = typeEntry.mBeType;
  1056. else if (irType.mKind == BfIRType::TypeKind::TypeKind_TypeInstPtrId)
  1057. beType = mBeIRCodeGen->mBeContext->GetPointerTo(typeEntry.mInstBeType);
  1058. }
  1059. String str;
  1060. BeDumpContext dc;
  1061. dc.mModule = mBeIRCodeGen->mBeModule;
  1062. dc.ToString(str, beType);
  1063. return str;
  1064. }
  1065. #endif
  1066. else
  1067. {
  1068. return "???";
  1069. }
  1070. }
  1071. String BfIRBuilder::ToString(BfIRFunction irFunc)
  1072. {
  1073. if (mBfIRCodeGen != NULL)
  1074. {
  1075. auto val = mBfIRCodeGen->GetLLVMValue(irFunc.mId);
  1076. if (val == NULL)
  1077. return "null";
  1078. std::string outStr;
  1079. llvm::raw_string_ostream strStream(outStr);
  1080. val->print(strStream);
  1081. strStream.flush();
  1082. return outStr;
  1083. }
  1084. #ifdef BF_PLATFORM_WINDOWS
  1085. else if (mBeIRCodeGen != NULL)
  1086. {
  1087. auto val = mBeIRCodeGen->GetBeValue(irFunc.mId);
  1088. if (val == NULL)
  1089. return "null";
  1090. String str;
  1091. BeDumpContext dc;
  1092. dc.mModule = mBeIRCodeGen->mBeModule;
  1093. dc.ToString(str, val);
  1094. return str;
  1095. }
  1096. #endif
  1097. else
  1098. return "???";
  1099. }
  1100. String BfIRBuilder::ToString(BfIRFunctionType irType)
  1101. {
  1102. if (mBfIRCodeGen != NULL)
  1103. {
  1104. auto llvmType = mBfIRCodeGen->GetLLVMType(irType.mId);
  1105. if (llvmType == NULL)
  1106. return "null";
  1107. std::string outStr;
  1108. llvm::raw_string_ostream strStream(outStr);
  1109. llvmType->print(strStream);
  1110. strStream.flush();
  1111. return outStr;
  1112. }
  1113. else
  1114. return "???";
  1115. }
  1116. String BfIRBuilder::ToString(BfIRMDNode irMDNode)
  1117. {
  1118. if (mBfIRCodeGen != NULL)
  1119. {
  1120. auto md = mBfIRCodeGen->GetLLVMMetadata(irMDNode.mId);
  1121. if (md == NULL)
  1122. return "null";
  1123. std::string outStr;
  1124. llvm::raw_string_ostream strStream(outStr);
  1125. md->print(strStream);
  1126. strStream.flush();
  1127. return outStr;
  1128. }
  1129. #ifdef BF_PLATFORM_WINDOWS
  1130. else if (mBeIRCodeGen != NULL)
  1131. {
  1132. auto md = mBeIRCodeGen->GetBeMetadata(irMDNode.mId);
  1133. if (md == NULL)
  1134. return "null";
  1135. String str;
  1136. BeDumpContext dc;
  1137. dc.mModule = mBeIRCodeGen->mBeModule;
  1138. dc.ToString(str, md);
  1139. return str;
  1140. }
  1141. #endif
  1142. else
  1143. return "???";
  1144. }
  1145. String BfIRBuilder::ActiveFuncToString()
  1146. {
  1147. if (mBfIRCodeGen != NULL)
  1148. {
  1149. std::string outStr;
  1150. llvm::raw_string_ostream strStream(outStr);
  1151. mBfIRCodeGen->mActiveFunction->print(strStream);
  1152. return outStr;
  1153. }
  1154. else
  1155. return "???";
  1156. }
  1157. void BfIRBuilder::PrintActiveFunc()
  1158. {
  1159. pv(mActiveFunction);
  1160. }
  1161. void BfIRBuilder::pv(const BfIRValue& irValue)
  1162. {
  1163. OutputDebugStrF("%s\n", ToString(irValue).c_str());
  1164. }
  1165. void BfIRBuilder::pt(const BfIRType& irType)
  1166. {
  1167. OutputDebugStrF("%s\n", ToString(irType).c_str());
  1168. }
  1169. void pt(llvm::Type* t);
  1170. void BfIRBuilder::pbft(BfType* type)
  1171. {
  1172. OutputDebugStrF("%s\n", mModule->TypeToString(type).c_str());
  1173. //auto itr = mTypeMap.find(type);
  1174. //if (itr == mTypeMap.end())
  1175. BfIRPopulateType* populateType = NULL;
  1176. if (!mTypeMap.TryGetValue(type, &populateType))
  1177. {
  1178. OutputDebugStrF("Not in mTypeMap\n");
  1179. return;
  1180. }
  1181. OutputDebugStrF("mTypeMap DefState: %d\n", *populateType);
  1182. if (mBfIRCodeGen != NULL)
  1183. {
  1184. auto llvmType = mBfIRCodeGen->GetLLVMTypeById(type->mTypeId);
  1185. ::pt(llvmType);
  1186. }
  1187. }
  1188. void BfIRBuilder::pt(const BfIRFunction& irFunc)
  1189. {
  1190. OutputDebugStrF("%s\n", ToString(irFunc).c_str());
  1191. }
  1192. void BfIRBuilder::pft(const BfIRFunctionType& irType)
  1193. {
  1194. OutputDebugStrF("%s\n", ToString(irType).c_str());
  1195. }
  1196. void BfIRBuilder::pmd(const BfIRMDNode& irMDNode)
  1197. {
  1198. OutputDebugStrF("%s\n", ToString(irMDNode).c_str());
  1199. }
  1200. void BfIRBuilder::GetBufferData(Array<uint8>& outBuffer)
  1201. {
  1202. if (mStream.GetSize() == 0)
  1203. return;
  1204. outBuffer.ResizeRaw(mStream.GetSize());
  1205. mStream.SetReadPos(0);
  1206. mStream.Read(&outBuffer[0], mStream.GetSize());
  1207. }
  1208. void BfIRBuilder::ClearConstData()
  1209. {
  1210. mTempAlloc.Clear();
  1211. mConstMemMap.Clear();
  1212. mFunctionMap.Clear();
  1213. BF_ASSERT(mMethodTypeMap.GetCount() == 0);
  1214. BF_ASSERT(mTypeMap.GetCount() == 0);
  1215. BF_ASSERT(mDITemporaryTypes.size() == 0);
  1216. #ifdef BFIR_RENTRY_CHECK
  1217. BF_ASSERT(mDeclReentrySet.size() == 0);
  1218. BF_ASSERT(mDefReentrySet.size() == 0);
  1219. #endif
  1220. BF_ASSERT(mStream.GetSize() == 0);
  1221. }
  1222. void BfIRBuilder::Start(const StringImpl& moduleName, int ptrSize, bool isOptimized)
  1223. {
  1224. mHasStarted = true;
  1225. WriteCmd(BfIRCmd_Module_Start, moduleName, ptrSize, isOptimized);
  1226. NEW_CMD_INSERTED;
  1227. }
  1228. void BfIRBuilder::SetBackend(bool isBeefBackend)
  1229. {
  1230. mIsBeefBackend = isBeefBackend;
  1231. BF_ASSERT(mIRCodeGen == NULL);
  1232. if (mDbgVerifyCodeGen)
  1233. {
  1234. #ifdef BF_PLATFORM_WINDOWS
  1235. if (isBeefBackend)
  1236. {
  1237. mBeIRCodeGen = new BeIRCodeGen();
  1238. mBeIRCodeGen->mStream = &mStream;
  1239. mBeIRCodeGen->mBfIRBuilder = this;
  1240. mIRCodeGen = mBeIRCodeGen;
  1241. }
  1242. else
  1243. #endif
  1244. {
  1245. mBfIRCodeGen = new BfIRCodeGen();
  1246. mBfIRCodeGen->mStream = &mStream;
  1247. mBfIRCodeGen->mBfIRBuilder = this;
  1248. mIRCodeGen = mBfIRCodeGen;
  1249. }
  1250. mIRCodeGen->SetConfigConst(BfIRConfigConst_VirtualMethodOfs, 0);
  1251. mIRCodeGen->SetConfigConst(BfIRConfigConst_DynSlotOfs, 0);
  1252. while (mStream.GetReadPos() < mStream.GetSize())
  1253. mIRCodeGen->HandleNextCmd();
  1254. }
  1255. }
  1256. void BfIRBuilder::RemoveIRCodeGen()
  1257. {
  1258. if (mIRCodeGen != NULL)
  1259. {
  1260. mIRCodeGen->mStream = NULL;
  1261. delete mIRCodeGen;
  1262. mIRCodeGen = NULL;
  1263. }
  1264. }
  1265. void BfIRBuilder::WriteIR(const StringImpl& fileName)
  1266. {
  1267. WriteCmd(BfIRCmd_WriteIR, fileName);
  1268. NEW_CMD_INSERTED;
  1269. }
  1270. void BfIRBuilder::Module_SetTargetTriple(const StringImpl& targetTriple)
  1271. {
  1272. WriteCmd(BfIRCmd_Module_SetTargetTriple, targetTriple);
  1273. NEW_CMD_INSERTED;
  1274. }
  1275. void BfIRBuilder::Module_AddModuleFlag(const StringImpl& flag, int val)
  1276. {
  1277. WriteCmd(BfIRCmd_Module_AddModuleFlag, flag, val);
  1278. NEW_CMD_INSERTED;
  1279. }
  1280. #define GET_FROM(ptr, T) *((T*)(ptr += sizeof(T)) - 1)
  1281. void BfIRBuilder::WriteSLEB128(int64 value)
  1282. {
  1283. if ((value >= -0x40) && (value <= 0x3F))
  1284. {
  1285. mStream.Write(((uint8)value) & 0x7F);
  1286. return;
  1287. }
  1288. bool hasMore;
  1289. do
  1290. {
  1291. uint8 curByte = (uint8)(value & 0x7f);
  1292. value >>= 7;
  1293. hasMore = !((((value == 0) && ((curByte & 0x40) == 0)) ||
  1294. ((value == -1) && ((curByte & 0x40) != 0))));
  1295. if (hasMore)
  1296. curByte |= 0x80;
  1297. mStream.Write(curByte);
  1298. }
  1299. while (hasMore);
  1300. }
  1301. void BfIRBuilder::WriteSLEB128(int32 value)
  1302. {
  1303. if (value < 0)
  1304. {
  1305. // if (value >= -0x40)
  1306. // {
  1307. // mStream.Write((uint8)value);
  1308. // return;
  1309. // }
  1310. //
  1311. // if (value >= -0x2000)
  1312. // {
  1313. // uint16 val =
  1314. // (((uint16)(value << 1)) & 0x7F00) |
  1315. // (((uint16)value) & 0x7F) | 0x80;
  1316. // mStream.Write_2(val);
  1317. // return;
  1318. // }
  1319. //
  1320. // if (value >= -0x100000)
  1321. // {
  1322. // uint32 val =
  1323. // (((uint32)(value << 2)) & 0x7F0000) |
  1324. // (((uint32)(value << 1)) & 0x7F00) |
  1325. // (((uint32)value) & 0x7F) | 0x8080;
  1326. // mStream.Write_3(val);
  1327. // return;
  1328. // }
  1329. //
  1330. // if (value >= -0x8000000)
  1331. // {
  1332. // uint32 val =
  1333. // (((uint32)(value << 3)) & 0x7F000000) |
  1334. // (((uint32)(value << 2)) & 0x7F0000) |
  1335. // (((uint32)(value << 1)) & 0x7F00) |
  1336. // (((uint32)value) & 0x7F) | 0x808080;
  1337. // mStream.Write_4(val);
  1338. // return;
  1339. // }
  1340. }
  1341. else
  1342. {
  1343. if (value <= 0x3F)
  1344. {
  1345. mStream.Write((uint8)value);
  1346. return;
  1347. }
  1348. if (value <= 0x1FFF)
  1349. {
  1350. uint16 val =
  1351. (((uint16)(value << 1)) & 0x7F00) |
  1352. (((uint16)value) & 0x7F) | 0x80;
  1353. mStream.Write_2(val);
  1354. return;
  1355. }
  1356. //
  1357. // if (value <= 0x0FFFFF)
  1358. // {
  1359. // uint32 val =
  1360. // (((uint32)(value << 2)) & 0x7F0000) |
  1361. // (((uint32)(value << 1)) & 0x7F00) |
  1362. // (((uint32)value) & 0x7F) | 0x8080;
  1363. // mStream.Write_3(val);
  1364. // return;
  1365. // }
  1366. //
  1367. // if (value <= 0x7FFFFF)
  1368. // {
  1369. // uint32 val =
  1370. // (((uint32)(value << 3)) & 0x7F000000) |
  1371. // (((uint32)(value << 2)) & 0x7F0000) |
  1372. // (((uint32)(value << 1)) & 0x7F00) |
  1373. // (((uint32)value) & 0x7F) | 0x808080;
  1374. // mStream.Write_4(val);
  1375. // return;
  1376. // }
  1377. }
  1378. bool hasMore;
  1379. do
  1380. {
  1381. uint8 curByte = (uint8)(value & 0x7f);
  1382. value >>= 7;
  1383. hasMore = !((((value == 0) && ((curByte & 0x40) == 0)) ||
  1384. ((value == -1) && ((curByte & 0x40) != 0))));
  1385. if (hasMore)
  1386. curByte |= 0x80;
  1387. mStream.Write(curByte);
  1388. } while (hasMore);
  1389. }
  1390. void BfIRBuilder::Write(uint8 val)
  1391. {
  1392. mStream.Write(val);
  1393. }
  1394. void BfIRBuilder::Write(bool val)
  1395. {
  1396. mStream.Write(val ? 1 : 0);
  1397. }
  1398. void BfIRBuilder::Write(int intVal)
  1399. {
  1400. WriteSLEB128(intVal);
  1401. }
  1402. void BfIRBuilder::Write(int64 intVal)
  1403. {
  1404. WriteSLEB128(intVal);
  1405. }
  1406. void BfIRBuilder::Write(const StringImpl&str)
  1407. {
  1408. WriteSLEB128((int)str.length());
  1409. mStream.Write(str.c_str(), (int)str.length());
  1410. }
  1411. void BfIRBuilder::Write(const BfIRValue& irValue)
  1412. {
  1413. if ((irValue.mFlags & BfIRValueFlags_Const) != 0)
  1414. {
  1415. auto constant = GetConstantById(irValue.mId);
  1416. mStream.Write(BfIRParamType_Const);
  1417. mStream.Write((uint8)constant->mTypeCode);
  1418. switch ((int)constant->mTypeCode)
  1419. {
  1420. case (int)BfTypeCode_Single:
  1421. {
  1422. float f = (float)constant->mDouble;
  1423. mStream.Write(&f, sizeof(float));
  1424. }
  1425. break;
  1426. case (int)BfTypeCode_Double:
  1427. {
  1428. mStream.Write(&constant->mDouble, sizeof(double));
  1429. }
  1430. break;
  1431. case (int)BfTypeCode_Int8:
  1432. case (int)BfTypeCode_UInt8:
  1433. case (int)BfTypeCode_Int16:
  1434. case (int)BfTypeCode_UInt16:
  1435. case (int)BfTypeCode_Int32:
  1436. case (int)BfTypeCode_UInt32:
  1437. case (int)BfTypeCode_Int64:
  1438. case (int)BfTypeCode_UInt64:
  1439. case (int)BfTypeCode_IntPtr:
  1440. case (int)BfTypeCode_UIntPtr:
  1441. case (int)BfTypeCode_IntUnknown:
  1442. case (int)BfTypeCode_UIntUnknown:
  1443. case (int)BfTypeCode_Char8:
  1444. case (int)BfTypeCode_Char16:
  1445. case (int)BfTypeCode_Char32:
  1446. {
  1447. WriteSLEB128(constant->mInt64);
  1448. }
  1449. break;
  1450. case (int)BfTypeCode_Boolean:
  1451. {
  1452. Write(constant->mBool);
  1453. }
  1454. break;
  1455. case (int)BfTypeCode_NullPtr:
  1456. {
  1457. Write(constant->mIRType);
  1458. }
  1459. break;
  1460. case (int)BfTypeCode_None:
  1461. {
  1462. // No param needed
  1463. }
  1464. break;
  1465. case (int)BfConstType_GlobalVar:
  1466. {
  1467. auto gvConst = (BfGlobalVar*)constant;
  1468. WriteSLEB128(gvConst->mStreamId);
  1469. if (gvConst->mStreamId == -1)
  1470. {
  1471. int streamId = mCmdCount++;
  1472. gvConst->mStreamId = streamId;
  1473. Write(gvConst->mType);
  1474. Write(gvConst->mIsConst);
  1475. Write((uint8)gvConst->mLinkageType);
  1476. Write(gvConst->mInitializer);
  1477. Write(String(gvConst->mName));
  1478. Write(gvConst->mIsTLS);
  1479. }
  1480. }
  1481. break;
  1482. case (int)BfConstType_BitCast:
  1483. case (int)BfConstType_BitCastNull:
  1484. {
  1485. auto bitcast = (BfConstantBitCast*)constant;
  1486. BfIRValue targetConst(BfIRValueFlags_Const, bitcast->mTarget);
  1487. Write(targetConst);
  1488. Write(bitcast->mToType);
  1489. }
  1490. break;
  1491. case (int)BfConstType_GEP32_2:
  1492. {
  1493. auto gepConst = (BfConstantGEP32_2*)constant;
  1494. BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
  1495. Write(targetConst);
  1496. Write(gepConst->mIdx0);
  1497. Write(gepConst->mIdx1);
  1498. }
  1499. break;
  1500. case (int)BfConstType_PtrToInt:
  1501. {
  1502. auto ptrToIntConst = (BfConstantPtrToInt*)constant;
  1503. BfIRValue targetConst(BfIRValueFlags_Const, ptrToIntConst->mTarget);
  1504. Write(targetConst);
  1505. Write(ptrToIntConst->mToTypeCode);
  1506. }
  1507. break;
  1508. case (int)BfConstType_AggZero:
  1509. {
  1510. Write(constant->mIRType);
  1511. }
  1512. break;
  1513. case (int)BfConstType_Array:
  1514. {
  1515. auto arrayConst = (BfConstantArray*)constant;
  1516. Write(arrayConst->mType);
  1517. Write(arrayConst->mValues);
  1518. }
  1519. break;
  1520. default:
  1521. {
  1522. BF_FATAL("Unhandled");
  1523. }
  1524. break;
  1525. }
  1526. }
  1527. else if ((irValue.mFlags & BfIRValueFlags_Arg) != 0)
  1528. {
  1529. mStream.Write(BfIRParamType_Arg);
  1530. WriteSLEB128(irValue.mId);
  1531. }
  1532. else if (irValue.mFlags != 0)
  1533. {
  1534. BF_ASSERT(irValue.mId >= 0);
  1535. if (irValue.mId < 0x100)
  1536. {
  1537. mStream.Write(BfIRParamType_StreamId_Abs8);
  1538. mStream.Write(irValue.mId);
  1539. }
  1540. else
  1541. {
  1542. int offset = mCmdCount - irValue.mId;
  1543. BF_ASSERT(offset > 0);
  1544. if (offset <= BfIRParamType_StreamId_Back_LAST - BfIRParamType_StreamId_Back1)
  1545. {
  1546. mStream.Write(BfIRParamType_StreamId_Back1 + (offset - 1));
  1547. }
  1548. else
  1549. {
  1550. mStream.Write(BfIRParamType_StreamId_Rel);
  1551. WriteSLEB128(offset);
  1552. }
  1553. }
  1554. }
  1555. else
  1556. {
  1557. BF_ASSERT(irValue.mId == -1);
  1558. mStream.Write(BfIRParamType_None);
  1559. }
  1560. }
  1561. void BfIRBuilder::Write(BfTypeCode typeCode)
  1562. {
  1563. mStream.Write((uint8)typeCode);
  1564. }
  1565. void BfIRBuilder::Write(BfIRTypeData type)
  1566. {
  1567. mStream.Write((uint8)type.mKind);
  1568. if (type.mKind != BfIRTypeData::TypeKind_None)
  1569. WriteSLEB128(type.mId);
  1570. }
  1571. void BfIRBuilder::Write(BfIRFunctionType funcType)
  1572. {
  1573. WriteSLEB128(funcType.mId);
  1574. }
  1575. void BfIRBuilder::Write(BfIRFunction func)
  1576. {
  1577. //auto funcData = mFunctionVec[func.mId];
  1578. //WriteSLEB128(bufPtr, funcData->mStreamId);
  1579. WriteSLEB128(func.mId);
  1580. }
  1581. void BfIRBuilder::Write(BfIRBlock block)
  1582. {
  1583. WriteSLEB128(block.mId);
  1584. }
  1585. void BfIRBuilder::Write(BfIRMDNode node)
  1586. {
  1587. BF_ASSERT(node.mId >= -1);
  1588. WriteSLEB128(node.mId);
  1589. }
  1590. BfIRValue BfIRBuilder::WriteCmd(BfIRCmd cmd)
  1591. {
  1592. if (mIgnoreWrites)
  1593. return GetFakeVal();
  1594. mStream.Write((uint8)cmd);
  1595. return BfIRValue(BfIRValueFlags_Value, mCmdCount++);
  1596. }
  1597. void BfIRBuilder::NewCmdInserted()
  1598. {
  1599. BF_ASSERT(mIgnoreWrites || mHasStarted);
  1600. if (mIgnoreWrites)
  1601. return;
  1602. if (mIRCodeGen == NULL)
  1603. return;
  1604. // This is for debug only - for checking the stream after each command. Useful when debugging IR generation errors
  1605. BF_ASSERT(mStream.GetReadPos() < mStream.GetSize());
  1606. mIRCodeGen->HandleNextCmd();
  1607. BF_ASSERT(mStream.GetReadPos() == mStream.GetSize());
  1608. }
  1609. BfIRMDNode BfIRBuilder::CreateNamespaceScope(BfType* type, BfIRMDNode fileDIScope)
  1610. {
  1611. BfIRMDNode curDIScope = fileDIScope;
  1612. auto typeInstance = type->ToTypeInstance();
  1613. if (mModule->mCompiler->mOptions.IsCodeView())
  1614. {
  1615. curDIScope = DbgCreateNameSpace(curDIScope, "_bf",
  1616. fileDIScope, 0);
  1617. }
  1618. if (typeInstance != NULL)
  1619. {
  1620. auto typeDef = typeInstance->mTypeDef;
  1621. if (!typeInstance->IsBoxed())
  1622. {
  1623. BfAtomComposite curNamespace;
  1624. if (typeInstance->IsArray())
  1625. {
  1626. auto arrayType = (BfArrayType*)typeInstance;
  1627. auto innerTypeInst = arrayType->GetUnderlyingType()->ToTypeInstance();
  1628. if (innerTypeInst != NULL)
  1629. curNamespace = innerTypeInst->mTypeDef->mNamespace;
  1630. }
  1631. else
  1632. curNamespace = typeDef->mNamespace;
  1633. for (int partCount = 0; partCount < curNamespace.GetPartsCount(); partCount++)
  1634. {
  1635. curDIScope = DbgCreateNameSpace(curDIScope, curNamespace.mParts[partCount]->ToString(),
  1636. fileDIScope, 0);
  1637. }
  1638. }
  1639. }
  1640. return curDIScope;
  1641. }
  1642. String BfIRBuilder::GetDebugTypeName(BfTypeInstance* typeInstance, bool includeOuterTypeName)
  1643. {
  1644. String typeName;
  1645. if (typeInstance->IsBoxed())
  1646. {
  1647. BfBoxedType* boxedType = (BfBoxedType*)typeInstance;
  1648. typeName = mModule->TypeToString(boxedType->mElementType, (BfTypeNameFlags)(BfTypeNameFlag_ResolveGenericParamNames | BfTypeNameFlag_AddGlobalContainerName));
  1649. if (boxedType->IsBoxedStructPtr())
  1650. typeName += "*";
  1651. typeName = "Box<" + typeName + ">";
  1652. }
  1653. else if (includeOuterTypeName)
  1654. {
  1655. typeName = mModule->TypeToString(typeInstance, (BfTypeNameFlags)(BfTypeNameFlag_ResolveGenericParamNames | BfTypeNameFlag_OmitNamespace | BfTypeNameFlag_AddGlobalContainerName));
  1656. }
  1657. else
  1658. {
  1659. typeName = mModule->TypeToString(typeInstance, (BfTypeNameFlags)(BfTypeNameFlag_ResolveGenericParamNames | BfTypeNameFlag_OmitNamespace | BfTypeNameFlag_AddGlobalContainerName | BfTypeNameFlag_OmitOuterType));
  1660. }
  1661. for (int i = 0; i < (int)typeName.length(); i++)
  1662. {
  1663. if (typeName[i] == '.')
  1664. {
  1665. typeName[i] = ':';
  1666. typeName.Insert(i, ":");
  1667. }
  1668. }
  1669. //DbgAddPrefix(typeName);
  1670. return typeName;
  1671. }
  1672. #ifdef BFIR_RENTRY_CHECK
  1673. struct ReEntryCheck
  1674. {
  1675. public:
  1676. std::set<BfType*>* mSet;
  1677. BfType* mType;
  1678. public:
  1679. ReEntryCheck(std::set<BfType*>* set, BfType* type)
  1680. {
  1681. mSet = set;
  1682. mType = type;
  1683. auto pair = mSet->insert(type);
  1684. BF_ASSERT(pair.second);
  1685. }
  1686. ~ReEntryCheck()
  1687. {
  1688. mSet->erase(mType);
  1689. }
  1690. };
  1691. #endif
  1692. void BfIRBuilder::CreateTypeDeclaration(BfType* type, bool forceDbgDefine)
  1693. {
  1694. bool wantDIData = DbgHasInfo() && (!type->IsUnspecializedType());
  1695. // Types that don't have a proper 'defining module' need to be defined in every module they are used
  1696. bool wantsDIForwardDecl = (type->GetModule() != mModule) && (!type->IsFunction());
  1697. // Forward declarations of valuetypes don't work in LLVM backend for Win32.....
  1698. //TODO: Why was this commented out?
  1699. if ((!mIsBeefBackend) && (type->IsValueType()))
  1700. wantsDIForwardDecl = false;
  1701. if (mModule->mExtensionCount != 0)
  1702. wantsDIForwardDecl = true;
  1703. if (forceDbgDefine)
  1704. wantsDIForwardDecl = false;
  1705. bool isPrimEnum = (type->IsEnum()) && (type->IsTypedPrimitive());
  1706. if (!type->IsDeclared())
  1707. mModule->PopulateType(type, BfPopulateType_Declaration);
  1708. BF_ASSERT(type->IsDeclared());
  1709. #ifdef BFIR_RENTRY_CHECK
  1710. ReEntryCheck reEntryCheck(&mDeclReentrySet, type);
  1711. #endif
  1712. BfIRType irType;
  1713. BfIRMDNode diType;
  1714. bool trackDIType = false;
  1715. if (type->IsPointer())
  1716. {
  1717. BfPointerType* pointerType = (BfPointerType*)type;
  1718. mModule->PopulateType(pointerType->mElementType, BfPopulateType_Data);
  1719. if (pointerType->mElementType->IsValuelessType())
  1720. {
  1721. irType = GetPrimitiveType(BfTypeCode_NullPtr);
  1722. }
  1723. else
  1724. {
  1725. irType = GetPointerTo(MapType(pointerType->mElementType));
  1726. }
  1727. if (wantDIData)
  1728. {
  1729. diType = DbgCreatePointerType(DbgGetType(pointerType->mElementType, BfIRPopulateType_Declaration));
  1730. trackDIType = true;
  1731. }
  1732. }
  1733. else if (type->IsRef())
  1734. {
  1735. BfRefType* refType = (BfRefType*)type;
  1736. if (refType->mElementType->IsValuelessType())
  1737. irType = GetPrimitiveType(BfTypeCode_NullPtr);
  1738. else
  1739. {
  1740. //mModule->PopulateType(refType->mElementType, BfPopulateType_Declaration);
  1741. irType = GetPointerTo(MapType(refType->mElementType));
  1742. }
  1743. if ((wantDIData) && (!type->IsUnspecializedType()))
  1744. {
  1745. diType = DbgCreateReferenceType(DbgGetType(refType->mElementType));
  1746. trackDIType = true;
  1747. }
  1748. }
  1749. else if ((type->IsGenericParam()) || (type->IsRetTypeType()))
  1750. {
  1751. //mModule->PopulateType(mModule->mContext->mBfObjectType, BfPopulateType_Declaration);
  1752. irType = MapType(mModule->mContext->mBfObjectType);
  1753. if (wantDIData)
  1754. diType = DbgGetType(mModule->mContext->mBfObjectType);
  1755. }
  1756. else if (type->IsConcreteInterfaceType())
  1757. {
  1758. BfConcreteInterfaceType* concreteInterfaceType = (BfConcreteInterfaceType*)type;
  1759. irType = MapType(concreteInterfaceType->mInterface);
  1760. if (wantDIData)
  1761. {
  1762. diType = DbgGetType(concreteInterfaceType->mInterface);
  1763. }
  1764. }
  1765. else if (type->IsMethodRef())
  1766. {
  1767. auto methodRefType = (BfMethodRefType*)type;
  1768. BfMethodInstance* methodInstance = methodRefType->mMethodRef;
  1769. String name = "_BF_MethodRef_";
  1770. name += BfTypeUtils::HashEncode64(methodInstance->mIdHash).c_str();
  1771. if (wantDIData)
  1772. {
  1773. auto bfFileInstance = mModule->GetFileFromNode(methodInstance->GetOwner()->mTypeDef->mTypeDeclaration);
  1774. auto namespaceScope = DbgCreateNameSpace(bfFileInstance->mDIFile, "_bf", bfFileInstance->mDIFile, 0);
  1775. StringT<128> mangledName;
  1776. BfMangler::Mangle(mangledName, mModule->mCompiler->GetMangleKind(), methodInstance);
  1777. int captureSize = 0;
  1778. int captureAlign = 0;
  1779. BfIRMDNode derivedFrom;
  1780. Array<BfIRMDNode> elements;
  1781. diType = DbgCreateReplaceableCompositeType(llvm::dwarf::DW_TAG_structure_type, name, namespaceScope, bfFileInstance->mDIFile, 0, captureSize * 8, captureAlign * 8, 0);
  1782. auto int64Type = mModule->GetPrimitiveType(BfTypeCode_Int64);
  1783. auto memberType = DbgCreateMemberType(diType, mangledName, bfFileInstance->mDIFile, 0, 0, 0, -1, 0, DbgGetType(int64Type));
  1784. elements.Add(memberType);
  1785. int offset = 0;
  1786. int implicitParamCount = methodInstance->GetImplicitParamCount();
  1787. for (int paramIdx = methodInstance->HasThis() ? -1 : 0; paramIdx < implicitParamCount; paramIdx++)
  1788. {
  1789. BfType* paramType = methodInstance->GetParamType(paramIdx);
  1790. offset = BF_ALIGN(offset, paramType->mAlign);
  1791. String memberName = methodInstance->GetParamName(paramIdx);
  1792. if (memberName == "this")
  1793. memberName = "__this";
  1794. auto diMember = DbgCreateMemberType(diType, memberName, bfFileInstance->mDIFile, 0, paramType->mSize * 8, paramType->mAlign * 8, offset * 8, 0, DbgGetType(paramType));
  1795. elements.Add(diMember);
  1796. offset += paramType->mSize;
  1797. }
  1798. offset = BF_ALIGN(offset, methodRefType->mAlign);
  1799. BF_ASSERT(offset == methodRefType->mSize);
  1800. DbgMakePermanent(diType, derivedFrom, elements);
  1801. }
  1802. Array<BfIRType> members;
  1803. for (int dataIdx = 0; dataIdx < methodRefType->GetCaptureDataCount(); dataIdx++)
  1804. {
  1805. BfType* paramType = methodRefType->GetCaptureType(dataIdx);
  1806. if (paramType->IsValueType())
  1807. PopulateType(paramType, BfIRPopulateType_Eventually_Full);
  1808. members.Add(MapType(paramType));
  1809. }
  1810. irType = CreateStructType(name);
  1811. StructSetBody(irType, members, false);
  1812. }
  1813. else if (type->IsSizedArray())
  1814. {
  1815. BfSizedArrayType* arrayType = (BfSizedArrayType*)type;
  1816. auto elementType = arrayType->mElementType;
  1817. BfIRType elementIrType;
  1818. if (elementType->IsValueType())
  1819. {
  1820. //mModule->PopulateType(arrayType->mElementType, BfPopulateType_Data);
  1821. elementIrType = MapType(arrayType->mElementType, BfIRPopulateType_Eventually_Full);
  1822. }
  1823. else
  1824. {
  1825. //mModule->PopulateType(arrayType->mElementType, BfPopulateType_Declaration);
  1826. elementIrType = MapType(arrayType->mElementType);
  1827. }
  1828. if (arrayType->mElementType->IsValuelessType())
  1829. irType = elementIrType;
  1830. else if (arrayType->mElementType->IsSizeAligned())
  1831. irType = GetSizedArrayType(MapType(arrayType->mElementType), arrayType->mElementCount);
  1832. else
  1833. irType = GetSizedArrayType(MapType(mModule->GetPrimitiveType(BfTypeCode_Int8)), arrayType->mSize);
  1834. if (wantDIData)
  1835. diType = DbgCreateArrayType((int64)arrayType->mSize * 8, arrayType->mAlign * 8, DbgGetType(arrayType->mElementType), arrayType->mElementCount);
  1836. }
  1837. else if (type->IsPrimitiveType())
  1838. {
  1839. auto primType = (BfPrimitiveType*)type;
  1840. auto typeCode = primType->mTypeDef->mTypeCode;
  1841. if ((typeCode == BfTypeCode_Var) || (typeCode == BfTypeCode_Let) || (typeCode == BfTypeCode_Self))
  1842. {
  1843. //mModule->PopulateType(mModule->mContext->mBfObjectType, BfPopulateType_Declaration);
  1844. irType = MapType(mModule->mContext->mBfObjectType);
  1845. if (wantDIData)
  1846. diType = DbgGetType(mModule->mContext->mBfObjectType);
  1847. }
  1848. else if (typeCode == BfTypeCode_NullPtr)
  1849. {
  1850. irType = GetPrimitiveType(typeCode);
  1851. if (wantDIData)
  1852. {
  1853. auto voidType = DbgGetType(mModule->GetPrimitiveType(BfTypeCode_None));
  1854. diType = DbgCreatePointerType(voidType);
  1855. }
  1856. }
  1857. else
  1858. {
  1859. irType = GetPrimitiveType(typeCode);
  1860. if (wantDIData)
  1861. {
  1862. int dwarfType = 0;
  1863. switch (typeCode)
  1864. {
  1865. case BfTypeCode_None:
  1866. dwarfType = llvm::dwarf::DW_ATE_address;
  1867. break;
  1868. case BfTypeCode_Boolean:
  1869. dwarfType = llvm::dwarf::DW_ATE_boolean;
  1870. break;
  1871. case BfTypeCode_Int8:
  1872. case BfTypeCode_Int16:
  1873. case BfTypeCode_Int32:
  1874. case BfTypeCode_Int64:
  1875. case BfTypeCode_IntPtr:
  1876. dwarfType = llvm::dwarf::DW_ATE_signed;
  1877. break;
  1878. case BfTypeCode_UInt8:
  1879. case BfTypeCode_UInt16:
  1880. case BfTypeCode_UInt32:
  1881. case BfTypeCode_UInt64:
  1882. case BfTypeCode_UIntPtr:
  1883. dwarfType = llvm::dwarf::DW_ATE_unsigned;
  1884. break;
  1885. case BfTypeCode_Char8:
  1886. case BfTypeCode_Char16:
  1887. case BfTypeCode_Char32:
  1888. dwarfType = llvm::dwarf::DW_ATE_unsigned_char;
  1889. break;
  1890. case BfTypeCode_Single:
  1891. case BfTypeCode_Double:
  1892. dwarfType = llvm::dwarf::DW_ATE_float;
  1893. break;
  1894. default:
  1895. BF_FATAL("Unhandled");
  1896. }
  1897. diType = DbgCreateBasicType(primType->mTypeDef->mName->ToString(), primType->mSize * 8, primType->mAlign * 8, dwarfType);
  1898. }
  1899. }
  1900. }
  1901. else if (type->IsTypeInstance())
  1902. {
  1903. BfTypeInstance* typeInstance = type->ToTypeInstance();
  1904. auto typeDef = typeInstance->mTypeDef;
  1905. BfIRMDNode diForwardDecl;
  1906. if (wantDIData)
  1907. {
  1908. BfFileInstance* bfFileInstance;
  1909. // Why did we bother setting the actual type declaration location?
  1910. bfFileInstance = mModule->GetFileFromNode(mModule->mContext->mBfObjectType->mTypeDef->mTypeDeclaration);
  1911. BfIRMDNode fileDIScope;
  1912. if (bfFileInstance != NULL)
  1913. fileDIScope = bfFileInstance->mDIFile;
  1914. BfIRMDNode curDIScope;
  1915. auto checkType = type;
  1916. if (checkType->IsArray())
  1917. {
  1918. BfArrayType* arrayType = (BfArrayType*)checkType;
  1919. checkType = arrayType->mTypeGenericArguments[0];
  1920. }
  1921. BfTypeInstance* outerType = NULL;
  1922. if (!checkType->IsBoxed())
  1923. outerType = mModule->GetOuterType(checkType);
  1924. if (outerType != NULL)
  1925. curDIScope = DbgGetTypeInst(outerType);
  1926. else if (checkType->IsNullable())
  1927. curDIScope = CreateNamespaceScope(checkType->GetUnderlyingType(), fileDIScope);
  1928. else
  1929. curDIScope = CreateNamespaceScope(checkType, fileDIScope);
  1930. String typeName = GetDebugTypeName(typeInstance, false);
  1931. if (wantsDIForwardDecl)
  1932. {
  1933. if (type->IsInterface())
  1934. {
  1935. int flags = 0;
  1936. diForwardDecl = DbgCreateReplaceableCompositeType(llvm::dwarf::DW_TAG_structure_type,
  1937. typeName, curDIScope, fileDIScope, 0, (int64)0 * 8, (int64)0 * 8, flags);
  1938. auto derivedFrom = DbgGetTypeInst(mModule->mContext->mBfObjectType);
  1939. llvm::SmallVector<BfIRMDNode, 8> diFieldTypes;
  1940. auto inheritanceType = DbgCreateInheritance(diForwardDecl, derivedFrom, 0, llvm::DINode::FlagPublic);
  1941. diFieldTypes.push_back(inheritanceType);
  1942. DbgMakePermanent(diForwardDecl, derivedFrom, diFieldTypes);
  1943. }
  1944. else
  1945. {
  1946. diForwardDecl = DbgCreateSizedForwardDecl(llvm::dwarf::DW_TAG_structure_type,
  1947. typeName, curDIScope, fileDIScope, 0, (int64)BF_ALIGN(type->mSize, type->mAlign) * 8, (int64)type->mAlign * 8);
  1948. }
  1949. }
  1950. else
  1951. {
  1952. // Will fill in later (during definition phase)
  1953. int flags = 0;
  1954. diForwardDecl = DbgCreateReplaceableCompositeType(llvm::dwarf::DW_TAG_structure_type,
  1955. typeName, curDIScope, fileDIScope, 0, (int64)BF_ALIGN(typeInstance->mInstSize, typeInstance->mInstAlign) * 8, (int64)typeInstance->mInstAlign * 8, flags);
  1956. mDITemporaryTypes.push_back(typeInstance);
  1957. if (!type->IsUnspecializedType())
  1958. {
  1959. BF_ASSERT(!mDeferredDbgTypeDefs.Contains(type));
  1960. mDeferredDbgTypeDefs.Add(type);
  1961. }
  1962. }
  1963. DbgSetInstType(type, diForwardDecl);
  1964. BfIRMDNode diType;
  1965. if (type->IsValueType())
  1966. diType = diForwardDecl;
  1967. else
  1968. diType = DbgCreatePointerType(diForwardDecl);
  1969. DbgSetType(type, diType);
  1970. }
  1971. if (type->IsTypedPrimitive())
  1972. {
  1973. auto underlyingType = type->GetUnderlyingType();
  1974. irType = MapType(underlyingType);
  1975. SetType(type, irType);
  1976. SetInstType(type, irType);
  1977. }
  1978. else
  1979. {
  1980. String prefix = typeDef->mProject->mName + ".";
  1981. StringT<128> mangledName;
  1982. mangledName += prefix;
  1983. BfMangler::Mangle(mangledName, mModule->mCompiler->GetMangleKind(), typeInstance, typeInstance->mModule);
  1984. BfIRType irStructType = CreateStructType(mangledName);
  1985. if (type->IsObjectOrInterface())
  1986. {
  1987. BfIRType ptrToStructType = GetPointerTo(irStructType);
  1988. SetType(type, ptrToStructType);
  1989. SetInstType(type, irStructType);
  1990. }
  1991. else
  1992. {
  1993. SetType(type, irStructType);
  1994. SetInstType(type, irStructType);
  1995. }
  1996. }
  1997. return;
  1998. }
  1999. if (irType)
  2000. SetType(type, irType);
  2001. if (diType)
  2002. {
  2003. DbgSetType(type, diType);
  2004. //if (trackDIType)
  2005. //DbgTrackDITypes(type);
  2006. }
  2007. }
  2008. void BfIRBuilder::CreateDbgTypeDefinition(BfType* type)
  2009. {
  2010. BP_ZONE("BfIRBuilder::CreateDbgTypeDefinition");
  2011. auto typeInstance = type->ToTypeInstance();
  2012. bool isPrimEnum = (type->IsEnum()) && (type->IsTypedPrimitive());
  2013. auto typeDef = typeInstance->mTypeDef;
  2014. bool wantDIData = true;
  2015. BfModuleOptions moduleOptions = mModule->GetModuleOptions();
  2016. bool isOptimized = (moduleOptions.mOptLevel != BfOptLevel_O0) && (moduleOptions.mOptLevel != BfOptLevel_OgPlus);
  2017. BfIRMDNode fileDIScope;
  2018. if (wantDIData)
  2019. {
  2020. BfFileInstance* bfFileInstance;
  2021. if (typeDef->mTypeDeclaration != NULL)
  2022. bfFileInstance = mModule->GetFileFromNode(typeDef->mTypeDeclaration);
  2023. else
  2024. bfFileInstance = mModule->GetFileFromNode(mModule->mCompiler->mBfObjectTypeDef->mTypeDeclaration);
  2025. if (bfFileInstance != NULL)
  2026. fileDIScope = bfFileInstance->mDIFile;
  2027. }
  2028. #ifdef BFIR_RENTRY_CHECK
  2029. ReEntryCheck reEntryCheck(&mDefReentrySet, type);
  2030. #endif
  2031. String typeName = GetDebugTypeName(typeInstance, false);
  2032. bool isGlobalContainer = typeDef->IsGlobalsContainer();
  2033. bool isDefiningModule = ((type->GetModule() == mModule) || (type->IsFunction()));
  2034. auto diForwardDecl = DbgGetTypeInst(typeInstance);
  2035. //BF_ASSERT(WantsDbgDefinition(type));
  2036. llvm::SmallVector<BfIRMDNode, 8> diFieldTypes;
  2037. bool isPacked = false;
  2038. bool isUnion = false;
  2039. bool isCRepr = false;
  2040. if (typeInstance->IsBoxed())
  2041. {
  2042. }
  2043. else
  2044. {
  2045. isCRepr = typeInstance->mIsCRepr;
  2046. isPacked = typeInstance->mIsPacked;
  2047. isUnion = typeInstance->mIsUnion;
  2048. }
  2049. BfSizedVector<BfFieldInstance*, 8> orderedFields;
  2050. if ((type->IsUnion()) && (!type->IsEnum()))
  2051. {
  2052. int lineNum = 0;
  2053. int flags = llvm::DINode::FlagPublic;
  2054. auto fieldType = typeInstance->GetUnionInnerType();
  2055. auto resolvedFieldDIType = DbgGetType(fieldType);
  2056. String fieldName = "__bfunion";
  2057. auto memberType = DbgCreateMemberType(diForwardDecl, fieldName, fileDIScope, lineNum,
  2058. fieldType->mSize * 8, fieldType->mAlign * 8, 0,
  2059. flags, resolvedFieldDIType);
  2060. diFieldTypes.push_back(memberType);
  2061. }
  2062. bool isPayloadEnum = (typeInstance->IsEnum()) && (!typeInstance->IsTypedPrimitive());
  2063. for (auto& fieldInstanceRef : typeInstance->mFieldInstances)
  2064. {
  2065. auto fieldInstance = &fieldInstanceRef;
  2066. if (!fieldInstance->mFieldIncluded)
  2067. continue;
  2068. auto fieldDef = fieldInstance->GetFieldDef();
  2069. if ((fieldInstance->mResolvedType == NULL) || (typeInstance->IsBoxed()))
  2070. continue;
  2071. auto resolvedFieldType = fieldInstance->GetResolvedType();
  2072. mModule->PopulateType(resolvedFieldType, BfPopulateType_Declaration);
  2073. BfIRType resolvedFieldIRType = MapType(resolvedFieldType);
  2074. BfIRMDNode resolvedFieldDIType;
  2075. if ((fieldDef != NULL) && (!fieldDef->mIsStatic) && (resolvedFieldType->IsStruct()))
  2076. PopulateType(resolvedFieldType, BfIRPopulateType_Eventually_Full);
  2077. resolvedFieldDIType = DbgGetType(resolvedFieldType);
  2078. if ((fieldDef == NULL) && (typeInstance->IsPayloadEnum()))
  2079. {
  2080. orderedFields.push_back(fieldInstance);
  2081. int lineNum = 0;
  2082. int flags = llvm::DINode::FlagPublic;
  2083. auto fieldType = fieldInstance->mResolvedType;
  2084. String fieldName = "__bftag";
  2085. auto memberType = DbgCreateMemberType(diForwardDecl, fieldName, fileDIScope, lineNum,
  2086. resolvedFieldType->mSize * 8, resolvedFieldType->mAlign * 8, fieldInstance->mDataOffset * 8,
  2087. flags, resolvedFieldDIType);
  2088. diFieldTypes.push_back(memberType);
  2089. }
  2090. if ((!typeInstance->IsBoxed()) && (fieldDef != NULL))
  2091. {
  2092. if (fieldDef->mIsConst)
  2093. {
  2094. if (isDefiningModule)
  2095. {
  2096. if ((isPayloadEnum) && (fieldDef->IsEnumCaseEntry()))
  2097. {
  2098. auto payloadType = fieldInstance->mResolvedType;
  2099. if (payloadType == NULL)
  2100. payloadType = mModule->CreateTupleType(BfTypeVector(), Array<String>());
  2101. String fieldName = StrFormat("_%d_%s", -fieldInstance->mDataIdx - 1, fieldDef->mName.c_str());
  2102. int flags = 0;
  2103. auto memberType = DbgCreateMemberType(diForwardDecl, fieldName, fileDIScope, 0,
  2104. BF_ALIGN(payloadType->mSize, payloadType->mAlign) * 8, payloadType->mAlign * 8, 0,
  2105. flags, DbgGetType(payloadType));
  2106. diFieldTypes.push_back(memberType);
  2107. }
  2108. else
  2109. {
  2110. BfConstant* constant = NULL;
  2111. BfIRValue staticValue;
  2112. if (fieldInstance->mConstIdx != -1)
  2113. {
  2114. constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
  2115. staticValue = mModule->ConstantToCurrent(constant, typeInstance->mConstHolder, resolvedFieldType);
  2116. }
  2117. BfIRMDNode constDIType;
  2118. if (resolvedFieldType->IsTypedPrimitive())
  2119. constDIType = DbgGetType(resolvedFieldType->GetUnderlyingType());//resolvedFieldType->GetUnderlyingType()->mDIType ;
  2120. else
  2121. constDIType = DbgCreateConstType(resolvedFieldDIType);
  2122. if ((fieldDef->mIsExtern) && (resolvedFieldType->IsPointer()))
  2123. {
  2124. auto underlyingType = resolvedFieldType->GetUnderlyingType();
  2125. auto staticTypedValue = mModule->ReferenceStaticField(fieldInstance);
  2126. staticValue = staticTypedValue.mValue;
  2127. int flags = 0;
  2128. auto memberType = DbgCreateStaticMemberType(diForwardDecl, fieldDef->mName, fileDIScope, 0,
  2129. constDIType, flags, CreateConst(BfTypeCode_Int32, 0));
  2130. diFieldTypes.push_back(memberType);
  2131. StringT<128> staticVarName;
  2132. BfMangler::Mangle(staticVarName, mModule->mCompiler->GetMangleKind(), fieldInstance);
  2133. String fieldName = DbgGetStaticFieldName(fieldInstance);
  2134. DbgCreateGlobalVariable(diForwardDecl, fieldName, staticVarName, fileDIScope, 0,
  2135. constDIType, false, staticValue, memberType);
  2136. }
  2137. else if ((resolvedFieldType->IsObjectOrInterface()) || (resolvedFieldType->IsPointer()) || (resolvedFieldType->IsSizedArray()))
  2138. {
  2139. bool useIntConstant = false;
  2140. bool wasMadeAddr = false;
  2141. String fieldName = fieldDef->mName;
  2142. BfIRValue intConstant;
  2143. if (constant != NULL)
  2144. {
  2145. // This constant debug info causes linking errors on Linux
  2146. if (isOptimized)
  2147. continue;
  2148. if (constant->mConstType == BfConstType_Array)
  2149. {
  2150. staticValue = ConstToMemory(staticValue);
  2151. wasMadeAddr = true;
  2152. }
  2153. else
  2154. {
  2155. int stringId = constant->mInt32;
  2156. const StringImpl& str = mModule->mContext->mStringObjectIdMap[stringId].mString;
  2157. staticValue = mModule->GetStringObjectValue(str);
  2158. }
  2159. }
  2160. StringT<128> staticVarName;
  2161. BfMangler::Mangle(staticVarName, mModule->mCompiler->GetMangleKind(), fieldInstance);
  2162. if (!useIntConstant)
  2163. {
  2164. auto useType = resolvedFieldType;
  2165. if (wasMadeAddr)
  2166. useType = mModule->CreatePointerType(useType);
  2167. staticValue = CreateGlobalVariable(mModule->mBfIRBuilder->MapType(useType), true, BfIRLinkageType_Internal, staticValue, staticVarName);
  2168. }
  2169. int flags = 0;
  2170. auto memberType = DbgCreateStaticMemberType(diForwardDecl, fieldName, fileDIScope, 0,
  2171. constDIType, flags, useIntConstant ? intConstant : BfIRValue());
  2172. diFieldTypes.push_back(memberType);
  2173. if (staticValue)
  2174. {
  2175. String qualifiedName = DbgGetStaticFieldName(fieldInstance);
  2176. DbgCreateGlobalVariable(diForwardDecl, qualifiedName, staticVarName, fileDIScope, 0,
  2177. constDIType, false, staticValue, memberType);
  2178. }
  2179. }
  2180. else if (mModule->mCompiler->mOptions.IsCodeView())
  2181. {
  2182. int flags = 0;
  2183. String fieldName = fieldDef->mName;
  2184. if (constant != NULL)
  2185. {
  2186. int64 writeVal = constant->mInt64;
  2187. if (constant->mTypeCode == BfTypeCode_Single)
  2188. {
  2189. // We need to do this because Singles are stored in mDouble, so we need to reduce here
  2190. float floatVal = (float)constant->mDouble;
  2191. writeVal = *(uint32*)&floatVal;
  2192. }
  2193. if (writeVal < 0)
  2194. fieldName += StrFormat("$_%llu", -writeVal);
  2195. else
  2196. fieldName += StrFormat("$%llu", writeVal);
  2197. }
  2198. auto memberType = DbgCreateStaticMemberType(diForwardDecl, fieldName, fileDIScope, 0,
  2199. constDIType, flags, staticValue);
  2200. diFieldTypes.push_back(memberType);
  2201. }
  2202. else
  2203. {
  2204. int flags = 0;
  2205. String fieldName = DbgGetStaticFieldName(fieldInstance);
  2206. auto memberType = DbgCreateStaticMemberType(diForwardDecl, fieldName, fileDIScope, 0,
  2207. constDIType, flags, staticValue);
  2208. diFieldTypes.push_back(memberType);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. else if (fieldDef->mIsStatic)
  2214. {
  2215. if (isDefiningModule)
  2216. {
  2217. int flags = 0;
  2218. auto memberType = DbgCreateStaticMemberType(diForwardDecl, fieldDef->mName, fileDIScope, 0,
  2219. resolvedFieldDIType, flags, BfIRValue());
  2220. diFieldTypes.push_back(memberType);
  2221. auto staticValue = mModule->ReferenceStaticField(fieldInstance);
  2222. if (staticValue.mValue)
  2223. {
  2224. StringT<128> staticVarName;
  2225. BfMangler::Mangle(staticVarName, mModule->mCompiler->GetMangleKind(), fieldInstance);
  2226. String fieldName = DbgGetStaticFieldName(fieldInstance);
  2227. DbgCreateGlobalVariable(diForwardDecl, fieldName, staticVarName, fileDIScope, 0,
  2228. resolvedFieldDIType, false, staticValue.mValue, memberType);
  2229. }
  2230. }
  2231. }
  2232. else
  2233. {
  2234. bool useForUnion = false;
  2235. BF_ASSERT(!fieldInstance->mIsEnumPayloadCase);
  2236. if (wantDIData)
  2237. {
  2238. int lineNum = 0;
  2239. int flags = 0;
  2240. String fieldName = fieldDef->mName;
  2241. if (fieldDef->mHasMultiDefs)
  2242. fieldName += "$" + fieldDef->mDeclaringType->mProject->mName;
  2243. auto memberType = DbgCreateMemberType(diForwardDecl, fieldName, fileDIScope, lineNum,
  2244. resolvedFieldType->mSize * 8, resolvedFieldType->mAlign * 8, fieldInstance->mDataOffset * 8,
  2245. flags, resolvedFieldDIType);
  2246. diFieldTypes.push_back(memberType);
  2247. }
  2248. }
  2249. }
  2250. }
  2251. int dataPos = 0;
  2252. if (typeInstance->mBaseType != NULL)
  2253. dataPos = typeInstance->mBaseType->mInstSize;
  2254. int unionSize = 0;
  2255. if (typeInstance->mIsUnion)
  2256. {
  2257. auto unionInnerType = typeInstance->GetUnionInnerType();
  2258. unionSize = unionInnerType->mSize;
  2259. dataPos += unionSize;
  2260. }
  2261. bool wantsMethods = true;
  2262. // We can't directly call boxed methods from the debugger
  2263. if (type->IsBoxed())
  2264. wantsMethods = false;
  2265. if (!isDefiningModule)
  2266. wantsMethods = false;
  2267. if (wantsMethods)
  2268. {
  2269. for (int methodIdx = 0; methodIdx < (int)typeInstance->mMethodInstanceGroups.size(); methodIdx++)
  2270. {
  2271. auto& methodGroup = typeInstance->mMethodInstanceGroups[methodIdx];
  2272. auto methodInstance = methodGroup.mDefault;
  2273. // We're only adding non-generic methods at the moment
  2274. if ((methodInstance == NULL) || (methodInstance->mIsUnspecialized))
  2275. continue;
  2276. if (type->IsGenericTypeInstance())
  2277. {
  2278. // We don't force method declarations for generic type instances, so don't add any unreified methods here
  2279. // for the purpose of keeping debug type info the same between rebuilds
  2280. if (!methodInstance->mIsReified)
  2281. continue;
  2282. }
  2283. auto methodDef = methodInstance->mMethodDef;
  2284. if ((methodDef->mMethodType == BfMethodType_Operator) || (methodDef->mMethodType == BfMethodType_Mixin) || (methodDef->mMethodType == BfMethodType_Ignore))
  2285. continue;
  2286. if (methodDef->mIsOverride)
  2287. continue;
  2288. llvm::SmallVector<BfIRMDNode, 8> diParams;
  2289. diParams.push_back(DbgGetType(methodInstance->mReturnType));
  2290. BfType* thisType = NULL;
  2291. if (!methodDef->mIsStatic)
  2292. {
  2293. BfType* thisType;
  2294. thisType = typeInstance;
  2295. if (!thisType->IsValuelessType())
  2296. {
  2297. BfType* thisPtrType = thisType;
  2298. auto diType = DbgGetType(thisPtrType);
  2299. if ((thisType->IsComposite()) && (!methodInstance->GetParamIsSplat(-1)))
  2300. {
  2301. diType = DbgCreatePointerType(diType);
  2302. diType = DbgCreateArtificialType(diType);
  2303. }
  2304. diParams.push_back(diType);
  2305. }
  2306. }
  2307. for (int paramIdx = 0; paramIdx < methodInstance->GetParamCount(); paramIdx++)
  2308. {
  2309. bool isParamSkipped = methodInstance->IsParamSkipped(paramIdx);
  2310. if (!isParamSkipped)
  2311. {
  2312. auto resolvedType = methodInstance->GetParamType(paramIdx);
  2313. diParams.push_back(DbgGetType(resolvedType));
  2314. }
  2315. }
  2316. BfIRMDNode diFuncType = DbgCreateSubroutineType(diParams);
  2317. int defLine = 0;
  2318. StringT<128> mangledName;
  2319. int flags = 0;
  2320. BfMangler::Mangle(mangledName, mModule->mCompiler->GetMangleKind(), methodInstance);
  2321. BfIRMDNode funcScope = diForwardDecl;
  2322. if (methodDef->mProtection == BfProtection_Public)
  2323. flags = llvm::DINode::FlagPublic;
  2324. else if (methodDef->mProtection == BfProtection_Protected)
  2325. flags = llvm::DINode::FlagProtected;
  2326. else
  2327. flags = llvm::DINode::FlagPrivate;
  2328. if (methodDef->mIsOverride)
  2329. {
  2330. //flags |= llvm::DINode::FlagVirtual;
  2331. }
  2332. else if (methodDef->mIsVirtual)
  2333. flags |= llvm::DINode::FlagIntroducedVirtual;
  2334. if ((methodDef->mIsStatic) || (methodDef->mMethodType == BfMethodType_Mixin))
  2335. flags |= llvm::DINode::FlagStaticMember;
  2336. else
  2337. {
  2338. if (typeInstance->IsValuelessType())
  2339. flags |= llvm::DINode::FlagStaticMember;
  2340. }
  2341. flags |= llvm::DINode::FlagPrototyped;
  2342. if (methodDef->mMethodType == BfMethodType_Ctor)
  2343. {
  2344. flags |= llvm::DINode::FlagArtificial;
  2345. }
  2346. String methodName = methodDef->mName;
  2347. llvm::SmallVector<BfIRMDNode, 1> genericArgs;
  2348. llvm::SmallVector<BfIRValue, 1> genericConstValueArgs;
  2349. auto diFunction = DbgCreateMethod(funcScope, methodName, mangledName, fileDIScope,
  2350. defLine + 1, diFuncType, false, false,
  2351. (methodInstance->mVirtualTableIdx != -1) ? 1 : 0,
  2352. (methodInstance->mVirtualTableIdx != -1) ? methodInstance->DbgGetVirtualMethodNum() : 0,
  2353. funcScope, flags, /*IsOptimized()*/false, BfIRValue(), genericArgs, genericConstValueArgs);
  2354. diFieldTypes.push_back(diFunction);
  2355. if (methodInstance->mVirtualTableIdx != -1)
  2356. {
  2357. BF_ASSERT(mModule->mCompiler->mMaxInterfaceSlots != -1);
  2358. }
  2359. }
  2360. }
  2361. BfType* declaredBaseType = typeInstance->mBaseType;
  2362. if (typeInstance->IsTypedPrimitive())
  2363. declaredBaseType = typeInstance->GetUnderlyingType();
  2364. /*if (isPrimEnum)
  2365. {
  2366. // Handled below
  2367. }
  2368. else*/ if (type->IsBoxed())
  2369. {
  2370. auto underlyingType = ((BfBoxedType*)type)->GetModifiedElementType();
  2371. if (!underlyingType->IsValuelessType())
  2372. {
  2373. auto fieldInstance = &typeInstance->mFieldInstances.back();
  2374. auto resolvedFieldType = fieldInstance->GetResolvedType();
  2375. auto inheritanceType = DbgCreateInheritance(diForwardDecl, DbgGetTypeInst(mModule->mContext->mBfObjectType), 0, llvm::DINode::FlagPublic);
  2376. diFieldTypes.push_back(inheritanceType);
  2377. int lineNum = 0;
  2378. int flags = llvm::DINode::FlagPublic;
  2379. auto memberType = DbgCreateMemberType(fileDIScope, "val", fileDIScope, lineNum,
  2380. resolvedFieldType->mSize * 8, resolvedFieldType->mAlign * 8, fieldInstance->mDataOffset * 8,
  2381. flags, DbgGetType(underlyingType));
  2382. diFieldTypes.push_back(memberType);
  2383. }
  2384. }
  2385. else
  2386. {
  2387. auto baseType = typeInstance->mBaseType;
  2388. if (baseType != NULL)
  2389. {
  2390. auto baseTypeInst = baseType->ToTypeInstance();
  2391. BfIRMDNode inheritanceType;
  2392. if (baseTypeInst != NULL)
  2393. inheritanceType = DbgCreateInheritance(diForwardDecl, DbgGetTypeInst(baseTypeInst), 0, llvm::DINode::FlagPublic);
  2394. else
  2395. inheritanceType = DbgCreateInheritance(diForwardDecl, DbgGetType(baseType), 0, llvm::DINode::FlagPublic);
  2396. diFieldTypes.push_back(inheritanceType);
  2397. }
  2398. // Typed primitives are expressed as multiple inheritance
  2399. if ((typeInstance->IsTypedPrimitive()) && (typeInstance->mBaseType != NULL) && (!typeInstance->mBaseType->IsTypedPrimitive()))
  2400. {
  2401. auto underlyingType = typeInstance->GetUnderlyingType();
  2402. // auto inheritanceType = DbgCreateInheritance(diForwardDecl, DbgGetType(underlyingType), 0, llvm::DINode::FlagPublic);
  2403. // diFieldTypes.push_back(inheritanceType);
  2404. int lineNum = 0;
  2405. int flags = llvm::DINode::FlagPublic;
  2406. auto memberType = DbgCreateMemberType(fileDIScope, "$prim", fileDIScope, lineNum,
  2407. underlyingType->mSize * 8, underlyingType->mAlign * 8, 0,
  2408. flags, DbgGetType(underlyingType));
  2409. diFieldTypes.push_back(memberType);
  2410. }
  2411. }
  2412. BfIRMDNode derivedFrom;
  2413. if (typeInstance->IsInterface())
  2414. {
  2415. derivedFrom = DbgGetTypeInst(mModule->mContext->mBfObjectType);
  2416. auto inheritanceType = DbgCreateInheritance(diForwardDecl, derivedFrom, 0, llvm::DINode::FlagPublic);
  2417. diFieldTypes.push_back(inheritanceType);
  2418. }
  2419. else if (typeInstance->mBaseType != NULL)
  2420. {
  2421. derivedFrom = DbgGetTypeInst(typeInstance->mBaseType);
  2422. }
  2423. BfIRMDNode curDIScope;
  2424. BfTypeInstance* outerType = NULL;
  2425. if (!type->IsBoxed())
  2426. outerType = mModule->GetOuterType(type);
  2427. if (outerType != NULL)
  2428. curDIScope = DbgGetTypeInst(outerType);
  2429. else if (type->IsNullable())
  2430. curDIScope = CreateNamespaceScope(type->GetUnderlyingType(), fileDIScope);
  2431. else
  2432. curDIScope = CreateNamespaceScope(type, fileDIScope);
  2433. //TODO: Set DI Flags
  2434. int diFlags = 0;
  2435. BfIRMDNode diType;
  2436. /*if ((typeInstance->IsEnum()) && (typeInstance->IsTypedPrimitive()))
  2437. {
  2438. llvm::SmallVector<BfIRMDNode, 8> diEnumValues;
  2439. for (auto& fieldInst : typeInstance->mFieldInstances)
  2440. {
  2441. if (!fieldInst.mFieldIncluded)
  2442. continue;
  2443. auto fieldDef = fieldInst.GetFieldDef();
  2444. if ((fieldInst.mConstIdx != -1) && (fieldDef->IsEnumCaseEntry()))
  2445. {
  2446. auto constant = typeInstance->mConstHolder->GetConstantById(fieldInst.mConstIdx);
  2447. auto enumValue = DbgCreateEnumerator(fieldInst.GetFieldDef()->mName, constant->mInt64);
  2448. diEnumValues.push_back(enumValue);
  2449. }
  2450. }
  2451. diType = DbgMakePermanent(diForwardDecl, DbgGetType(declaredBaseType), diEnumValues);
  2452. DbgSetType(type, diType);
  2453. DbgSetInstType(type, diType);
  2454. }
  2455. else*/
  2456. {
  2457. BfIRMDNode diCompositeType = DbgMakePermanent(diForwardDecl, BfIRMDNode(), diFieldTypes);
  2458. diType = diCompositeType;
  2459. }
  2460. }
  2461. bool BfIRBuilder::WantsDbgDefinition(BfType* type)
  2462. {
  2463. if ((type->GetModule() == mModule) || (type->IsFunction()))
  2464. return true;
  2465. // Forward declarations of valuetypes doesn't work in LLVM backend
  2466. // if ((!mIsBeefBackend) && (type->IsValueType()))
  2467. // return true;
  2468. return false;
  2469. }
  2470. void BfIRBuilder::CreateTypeDefinition(BfType* type, bool forceDbgDefine)
  2471. {
  2472. // This PopulateType is generally NOT needed, but here is a scenario in which it is:
  2473. // ClassB derives from ClassA. ClassC uses ClassB. A method inside ClassA gets modified,
  2474. // marking ClassA as incomplete, and then ClassC rebuilds and calls MapType on ClassB.
  2475. // "ClassB" itself is still populated, but its base class (ClassA) is not -- until we call
  2476. // this PopulateType below.
  2477. if (type->IsDataIncomplete())
  2478. mModule->PopulateType(type, BfPopulateType_Data);
  2479. bool isDefiningModule = ((type->GetModule() == mModule) || (type->IsFunction()));
  2480. if (mModule->mExtensionCount != 0)
  2481. isDefiningModule = false;
  2482. // if (mModule->mModuleName == "vdata")
  2483. // isDefiningModule = true;
  2484. if ((isDefiningModule) || (type->IsValueType()))
  2485. mModule->PopulateType(type, BfPopulateType_DataAndMethods);
  2486. if ((!isDefiningModule) && (!type->IsUnspecializedType()) && (type->IsValueType()) && (mHasDebugInfo))
  2487. {
  2488. DbgSetTypeSize(DbgGetType(type), BF_ALIGN(type->mSize, type->mAlign) * 8, type->mAlign * 8);
  2489. }
  2490. bool isPrimEnum = (type->IsEnum()) && (type->IsTypedPrimitive());
  2491. auto typeInstance = type->ToTypeInstance();
  2492. if (typeInstance == NULL)
  2493. return;
  2494. auto typeDef = typeInstance->mTypeDef;
  2495. #ifdef BFIR_RENTRY_CHECK
  2496. ReEntryCheck reEntryCheck(&mDefReentrySet, type);
  2497. #endif
  2498. bool isGlobalContainer = typeDef->IsGlobalsContainer();
  2499. auto diForwardDecl = DbgGetTypeInst(typeInstance);
  2500. llvm::SmallVector<BfIRType, 8> irFieldTypes;
  2501. if ((!typeInstance->IsTypedPrimitive()) && (typeInstance->mBaseType != NULL))
  2502. {
  2503. irFieldTypes.push_back(MapTypeInst(typeInstance->mBaseType, BfIRPopulateType_Eventually_Full));
  2504. }
  2505. llvm::SmallVector<BfIRMDNode, 8> diFieldTypes;
  2506. bool isPacked = false;
  2507. bool isUnion = false;
  2508. bool isCRepr = false;
  2509. if (typeInstance->IsBoxed())
  2510. {
  2511. auto boxedType = (BfBoxedType*)typeInstance;
  2512. BF_ASSERT(!boxedType->mFieldInstances.IsEmpty());
  2513. auto& fieldInst = boxedType->mFieldInstances.back();
  2514. auto elementType = fieldInst.mResolvedType;
  2515. if (!elementType->IsValuelessType())
  2516. {
  2517. irFieldTypes.push_back(MapType(elementType, elementType->IsValueType() ? BfIRPopulateType_Eventually_Full : BfIRPopulateType_Declaration));
  2518. }
  2519. }
  2520. else
  2521. {
  2522. isCRepr = typeInstance->mIsCRepr;
  2523. isPacked = typeInstance->mIsPacked;
  2524. isUnion = typeInstance->mIsUnion;
  2525. }
  2526. BfSizedVector<BfFieldInstance*, 8> orderedFields;
  2527. bool isPayloadEnum = (typeInstance->IsEnum()) && (!typeInstance->IsTypedPrimitive());
  2528. for (auto& fieldInstanceRef : typeInstance->mFieldInstances)
  2529. {
  2530. auto fieldInstance = &fieldInstanceRef;
  2531. if (!fieldInstance->mFieldIncluded)
  2532. continue;
  2533. auto fieldDef = fieldInstance->GetFieldDef();
  2534. if ((fieldInstance->mResolvedType == NULL) || (typeInstance->IsBoxed()))
  2535. continue;
  2536. if ((fieldDef != NULL) && (fieldDef->mIsStatic))
  2537. continue; // Just create type first - we may contain a static reference to ourselves, creating an dependency loop
  2538. auto resolvedFieldType = fieldInstance->GetResolvedType();
  2539. mModule->PopulateType(resolvedFieldType, BfPopulateType_Declaration);
  2540. BfIRType resolvedFieldIRType = MapType(resolvedFieldType);
  2541. BfIRMDNode resolvedFieldDIType;
  2542. if ((fieldDef != NULL) && (resolvedFieldType->IsStruct()))
  2543. PopulateType(resolvedFieldType, BfIRPopulateType_Eventually_Full);
  2544. if ((fieldDef == NULL) && (typeInstance->IsPayloadEnum()))
  2545. {
  2546. orderedFields.push_back(fieldInstance);
  2547. }
  2548. if ((!typeInstance->IsBoxed()) && (fieldDef != NULL))
  2549. {
  2550. bool useForUnion = false;
  2551. BF_ASSERT(!fieldInstance->mIsEnumPayloadCase);
  2552. if ((!fieldDef->mIsStatic) && (!resolvedFieldType->IsValuelessType()))
  2553. {
  2554. if (!isUnion)
  2555. {
  2556. BF_ASSERT(resolvedFieldIRType.mId != -1);
  2557. //irFieldTypes.push_back(resolvedFieldIRType);
  2558. if (fieldInstance->mDataIdx != -1)
  2559. {
  2560. while (fieldInstance->mDataIdx >= (int)orderedFields.size())
  2561. orderedFields.push_back(NULL);
  2562. orderedFields[fieldInstance->mDataIdx] = fieldInstance;
  2563. }
  2564. }
  2565. }
  2566. }
  2567. }
  2568. int dataPos = 0;
  2569. if (typeInstance->mBaseType != NULL)
  2570. dataPos = typeInstance->mBaseType->mInstSize;
  2571. int unionSize = 0;
  2572. if (typeInstance->mIsUnion)
  2573. {
  2574. auto unionInnerType = typeInstance->GetUnionInnerType();
  2575. irFieldTypes.push_back(MapType(unionInnerType, unionInnerType->IsValueType() ? BfIRPopulateType_Eventually_Full : BfIRPopulateType_Declaration));
  2576. unionSize = unionInnerType->mSize;
  2577. dataPos += unionSize;
  2578. }
  2579. for (int fieldIdx = 0; fieldIdx < (int)orderedFields.size(); fieldIdx++)
  2580. {
  2581. auto fieldInstance = orderedFields[fieldIdx];
  2582. if (fieldInstance == NULL)
  2583. continue;
  2584. auto resolvedFieldType = fieldInstance->GetResolvedType();
  2585. BfIRType resolvedFieldIRType = MapType(resolvedFieldType);
  2586. bool needsExplicitAlignment = !isCRepr || resolvedFieldType->NeedsExplicitAlignment();
  2587. if (!needsExplicitAlignment)
  2588. {
  2589. int alignSize = resolvedFieldType->mAlign;
  2590. dataPos = (dataPos + (alignSize - 1)) & ~(alignSize - 1);
  2591. }
  2592. if (fieldInstance->mDataOffset > dataPos)
  2593. {
  2594. int fillSize = fieldInstance->mDataOffset - dataPos;
  2595. auto byteType = mModule->GetPrimitiveType(BfTypeCode_Int8);
  2596. auto arrType = GetSizedArrayType(MapType(byteType), fillSize);
  2597. irFieldTypes.push_back(arrType);
  2598. dataPos = fieldInstance->mDataOffset;
  2599. }
  2600. dataPos += fieldInstance->mDataSize;
  2601. BF_ASSERT((int)irFieldTypes.size() == fieldInstance->mDataIdx);
  2602. irFieldTypes.push_back(resolvedFieldIRType);
  2603. if ((isCRepr) && (needsExplicitAlignment) && (fieldIdx == (int)orderedFields.size() - 1))
  2604. {
  2605. // Add explicit padding at end to enforce the "aligned size"
  2606. int endPad = typeInstance->mInstSize - dataPos;
  2607. if (endPad > 0)
  2608. {
  2609. auto byteType = mModule->GetPrimitiveType(BfTypeCode_Int8);
  2610. auto arrType = GetSizedArrayType(MapType(byteType), endPad);
  2611. irFieldTypes.push_back(arrType);
  2612. dataPos += endPad;
  2613. }
  2614. }
  2615. }
  2616. if (typeInstance->mIsUnion)
  2617. {
  2618. }
  2619. else if ((typeInstance->IsEnum()) && (typeInstance->IsStruct()))
  2620. {
  2621. BF_FATAL("Shouldn't happen");
  2622. // Just an empty placeholder
  2623. auto byteType = GetPrimitiveType(BfTypeCode_UInt8);
  2624. auto rawDataType = GetSizedArrayType(byteType, 0);
  2625. irFieldTypes.push_back(rawDataType);
  2626. }
  2627. if (!typeInstance->IsTypedPrimitive())
  2628. StructSetBody(MapTypeInst(typeInstance), irFieldTypes, isPacked || !isCRepr);
  2629. if (typeInstance->IsNullable())
  2630. {
  2631. BF_ASSERT(irFieldTypes.size() <= 3);
  2632. }
  2633. for (auto& fieldInstanceRef : typeInstance->mFieldInstances)
  2634. {
  2635. auto fieldInstance = &fieldInstanceRef;
  2636. if (!fieldInstance->mFieldIncluded)
  2637. continue;
  2638. auto fieldDef = fieldInstance->GetFieldDef();
  2639. if ((fieldInstance->mResolvedType == NULL) || (typeInstance->IsBoxed()))
  2640. continue;
  2641. if ((fieldDef == NULL) || (!fieldDef->mIsStatic))
  2642. continue; // Already handled non-statics
  2643. auto resolvedFieldType = fieldInstance->GetResolvedType();
  2644. mModule->PopulateType(resolvedFieldType, BfPopulateType_Declaration);
  2645. BfIRType resolvedFieldIRType = MapType(resolvedFieldType);
  2646. BfIRMDNode resolvedFieldDIType;
  2647. //if ((fieldDef != NULL) && (!fieldDef->mIsStatic) && (resolvedFieldType->IsStruct()))
  2648. //PopulateType(resolvedFieldType, BfIRPopulateType_Eventually_Full);
  2649. if ((!typeInstance->IsBoxed()) && (fieldDef != NULL))
  2650. {
  2651. if (fieldDef->mIsConst)
  2652. {
  2653. if ((isDefiningModule) && (fieldDef->mIsExtern) && (resolvedFieldType->IsPointer()))
  2654. {
  2655. if (!resolvedFieldType->IsVoid())
  2656. mModule->CreateStaticField(fieldInstance);
  2657. }
  2658. }
  2659. else if (fieldDef->mIsStatic)
  2660. {
  2661. if (isDefiningModule)
  2662. {
  2663. fieldInstance->mIsThreadLocal = mModule->IsThreadLocal(fieldInstance);
  2664. if (!resolvedFieldType->IsVoid())
  2665. mModule->CreateStaticField(fieldInstance, fieldInstance->mIsThreadLocal);
  2666. }
  2667. }
  2668. }
  2669. }
  2670. }
  2671. void BfIRBuilder::ReplaceDITemporaryTypes()
  2672. {
  2673. //for (auto typeInstance : mDITemporaryTypes)
  2674. for (int i = 0; i < (int)mDITemporaryTypes.size(); i++)
  2675. {
  2676. auto typeInstance = mDITemporaryTypes[i];
  2677. auto populateType = mTypeMap[typeInstance];
  2678. if (populateType == BfIRPopulateType_Full)
  2679. continue;
  2680. mTypeMap[typeInstance] = BfIRPopulateType_Eventually_Full;
  2681. CreateTypeDefinition(typeInstance, false);
  2682. mTypeMap[typeInstance] = BfIRPopulateType_Full;
  2683. }
  2684. mDITemporaryTypes.Clear();
  2685. }
  2686. void BfIRBuilder::PushDbgLoc(BfTypeInstance* typeInst)
  2687. {
  2688. }
  2689. BfIRPopulateType BfIRBuilder::GetPopulateTypeState(BfType* type)
  2690. {
  2691. BfIRPopulateType* populateTypePtr = NULL;
  2692. if (mTypeMap.TryGetValue(type, &populateTypePtr))
  2693. return *populateTypePtr;
  2694. return BfIRPopulateType_Identity;
  2695. }
  2696. void BfIRBuilder::PopulateType(BfType* type, BfIRPopulateType populateType)
  2697. {
  2698. if (mIgnoreWrites)
  2699. return;
  2700. BF_ASSERT(!mModule->mIsScratchModule);
  2701. if (populateType == BfIRPopulateType_Identity)
  2702. return;
  2703. auto curPopulateType = BfIRPopulateType_Identity;
  2704. BfIRPopulateType* populateTypePtr = NULL;
  2705. if (mTypeMap.TryGetValue(type, &populateTypePtr))
  2706. curPopulateType = *populateTypePtr;
  2707. if (curPopulateType >= populateType)
  2708. return;
  2709. if (curPopulateType == BfIRPopulateType_Full)
  2710. return;
  2711. auto typeInst = type->ToTypeInstance();
  2712. if ((curPopulateType < BfIRPopulateType_Declaration) && (populateType >= BfIRPopulateType_Declaration))
  2713. {
  2714. CreateTypeDeclaration(type, populateType == BfIRPopulateType_Full_ForceDefinition);
  2715. mTypeMap[type] = BfIRPopulateType_Declaration;
  2716. }
  2717. if ((curPopulateType < populateType) && (populateType >= BfIRPopulateType_Eventually_Full))
  2718. {
  2719. mTypeMap[type] = BfIRPopulateType_Eventually_Full;
  2720. CreateTypeDefinition(type, populateType == BfIRPopulateType_Full_ForceDefinition);
  2721. mTypeMap[type] = BfIRPopulateType_Full;
  2722. }
  2723. }
  2724. void BfIRBuilder::SetType(BfType* type, BfIRType irType)
  2725. {
  2726. WriteCmd(BfIRCmd_SetType, type->mTypeId, irType);
  2727. NEW_CMD_INSERTED;
  2728. }
  2729. void BfIRBuilder::SetInstType(BfType* type, BfIRType irType)
  2730. {
  2731. WriteCmd(BfIRCmd_SetInstType, type->mTypeId, irType);
  2732. NEW_CMD_INSERTED;
  2733. }
  2734. int BfIRBuilder::GetFakeId()
  2735. {
  2736. int fakeId = mCurFakeId;
  2737. mCurFakeId--;
  2738. if (mCurFakeId >= 0)
  2739. mCurFakeId = -2;
  2740. return fakeId;
  2741. }
  2742. BfIRValue BfIRBuilder::GetFakeVal()
  2743. {
  2744. BfIRValue irValue;
  2745. irValue.mFlags = BfIRValueFlags_Value;
  2746. irValue.mId = GetFakeId();
  2747. return irValue;
  2748. }
  2749. BfIRValue BfIRBuilder::GetFakeConst()
  2750. {
  2751. BfIRValue irValue;
  2752. irValue.mFlags = BfIRValueFlags_Const;
  2753. irValue.mId = GetFakeId();
  2754. return irValue;
  2755. }
  2756. BfIRType BfIRBuilder::GetFakeType()
  2757. {
  2758. BfIRType type;
  2759. type.mId = GetFakeId();
  2760. return type;
  2761. }
  2762. BfIRType BfIRBuilder::GetFakeBlock()
  2763. {
  2764. BfIRBlock block;
  2765. block.mFlags = BfIRValueFlags_Block;
  2766. block.mId = GetFakeId();
  2767. return block;
  2768. }
  2769. BfIRFunctionType BfIRBuilder::GetFakeFunctionType()
  2770. {
  2771. BfIRFunctionType funcType;
  2772. funcType.mId = GetFakeId();
  2773. return funcType;
  2774. }
  2775. BfIRFunction BfIRBuilder::GetFakeFunction()
  2776. {
  2777. BfIRFunction func;
  2778. //func.mFlags = BfIRValueFlags_Func;
  2779. func.mId = GetFakeId();
  2780. return func;
  2781. }
  2782. BfIRType BfIRBuilder::GetPrimitiveType(BfTypeCode typeCode)
  2783. {
  2784. FixTypeCode(typeCode);
  2785. BfIRType retType = WriteCmd(BfIRCmd_PrimitiveType, typeCode);
  2786. NEW_CMD_INSERTED_IRTYPE;
  2787. return retType;
  2788. }
  2789. BfIRType BfIRBuilder::CreateStructType(const StringImpl& name)
  2790. {
  2791. BfIRType retType = WriteCmd(BfIRCmd_CreateStruct, name);
  2792. NEW_CMD_INSERTED_IRTYPE;
  2793. return retType;
  2794. }
  2795. void BfIRBuilder::StructSetBody(BfIRType type, const BfSizedArray<BfIRType>& memberTypes, bool isPacked)
  2796. {
  2797. WriteCmd(BfIRCmd_StructSetBody, type, memberTypes, isPacked);
  2798. NEW_CMD_INSERTED;
  2799. }
  2800. BfIRType BfIRBuilder::MapType(BfType* type, BfIRPopulateType populateType)
  2801. {
  2802. if (!mIgnoreWrites)
  2803. {
  2804. PopulateType(type, populateType);
  2805. }
  2806. BfIRType retType;
  2807. retType.mKind = BfIRType::TypeKind_TypeId;
  2808. retType.mId = type->mTypeId;
  2809. return retType;
  2810. }
  2811. BfIRType BfIRBuilder::MapTypeInst(BfTypeInstance* typeInst, BfIRPopulateType populateType)
  2812. {
  2813. if (!mIgnoreWrites)
  2814. {
  2815. PopulateType(typeInst, populateType);
  2816. }
  2817. if (!mIgnoreWrites)
  2818. BF_ASSERT(mTypeMap.ContainsKey(typeInst));
  2819. BfIRType retType;
  2820. retType.mKind = BfIRType::TypeKind_TypeInstId;
  2821. retType.mId = typeInst->mTypeId;
  2822. return retType;
  2823. }
  2824. BfIRType BfIRBuilder::MapTypeInstPtr(BfTypeInstance* typeInst)
  2825. {
  2826. if (!mIgnoreWrites)
  2827. {
  2828. PopulateType(typeInst, BfIRPopulateType_Declaration);
  2829. }
  2830. BfIRType retType;
  2831. retType.mKind = BfIRType::TypeKind_TypeInstPtrId;
  2832. retType.mId = typeInst->mTypeId;
  2833. return retType;
  2834. }
  2835. BfIRType BfIRBuilder::GetType(BfIRValue val)
  2836. {
  2837. if (mIgnoreWrites)
  2838. return GetFakeType();
  2839. BfIRType retType = WriteCmd(BfIRCmd_GetType, val);
  2840. NEW_CMD_INSERTED_IRTYPE;
  2841. return retType;
  2842. }
  2843. BfIRType BfIRBuilder::GetPointerTo(BfIRFunctionType funcType)
  2844. {
  2845. BfIRType retType = WriteCmd(BfIRCmd_GetPointerToFuncType, funcType);
  2846. NEW_CMD_INSERTED_IRTYPE;
  2847. return retType;
  2848. }
  2849. BfIRType BfIRBuilder::GetPointerTo(BfIRType type)
  2850. {
  2851. BfIRType retType = WriteCmd(BfIRCmd_GetPointerToType, type);
  2852. NEW_CMD_INSERTED_IRTYPE;
  2853. return retType;
  2854. }
  2855. BfIRType BfIRBuilder::GetSizedArrayType(BfIRType elementType, int length)
  2856. {
  2857. BfIRType retType = WriteCmd(BfIRCmd_GetSizedArrayType, elementType, length);
  2858. NEW_CMD_INSERTED_IRTYPE;
  2859. return retType;
  2860. }
  2861. BfIRValue BfIRBuilder::CreateConstStruct(BfIRType type, const BfSizedArray<BfIRValue>& values)
  2862. {
  2863. BfIRValue retVal = WriteCmd(BfIRCmd_CreateConstStruct, type, values);
  2864. NEW_CMD_INSERTED_IRVALUE;
  2865. return retVal;
  2866. }
  2867. /*BfIRValue BfIRBuilder::CreateConstStructZero(BfIRType type)
  2868. {
  2869. BfIRValue retVal = WriteCmd(BfIRCmd_CreateConstStructZero, type);
  2870. NEW_CMD_INSERTED_IRVALUE;
  2871. return retVal;
  2872. }
  2873. BfIRValue BfIRBuilder::CreateConstArray(BfIRType type, const BfSizedArray<BfIRValue>& values)
  2874. {
  2875. BfIRValue retVal = WriteCmd(BfIRCmd_CreateConstArray, type, values);
  2876. NEW_CMD_INSERTED_IRVALUE;
  2877. return retVal;
  2878. }*/
  2879. BfIRValue BfIRBuilder::CreateConstString(const StringImpl& str)
  2880. {
  2881. BfIRValue retVal = WriteCmd(BfIRCmd_CreateConstString, str);
  2882. NEW_CMD_INSERTED_IRVALUE;
  2883. return retVal;
  2884. }
  2885. BfIRValue BfIRBuilder::ConstToMemory(BfIRValue constVal)
  2886. {
  2887. auto constant = GetConstant(constVal);
  2888. BfIRValue* value = NULL;
  2889. if (mConstMemMap.TryGetValue(constVal.mId, &value))
  2890. return *value;
  2891. BfIRType constType;
  2892. if (constant->mConstType == BfConstType_Array)
  2893. constType = ((BfConstantArray*)constant)->mType;
  2894. else if (constant->mConstType == BfConstType_AggZero)
  2895. constType = constant->mIRType;
  2896. else
  2897. BF_FATAL("Invalid const type for ConstToMemory");
  2898. auto memVal = CreateGlobalVariable(constType, true, BfIRLinkageType_Internal, constVal, StrFormat("__constMem%d", constVal.mId));
  2899. mConstMemMap[constVal.mId] = memVal;
  2900. return memVal;
  2901. }
  2902. BfIRValue BfIRBuilder::GetConfigConst(BfIRConfigConst constType, BfTypeCode typeCode)
  2903. {
  2904. BfIRValue retVal = WriteCmd(BfIRCmd_ConfigConst, (int)constType, typeCode);
  2905. NEW_CMD_INSERTED_IRVALUE;
  2906. return retVal;
  2907. }
  2908. BfIRValue BfIRBuilder::GetArgument(int argIdx)
  2909. {
  2910. BfIRValue retVal(BfIRValueFlags_Arg, argIdx);
  2911. return retVal;
  2912. }
  2913. void BfIRBuilder::SetName(BfIRValue val, const StringImpl& name)
  2914. {
  2915. WriteCmd(BfIRCmd_SetName, val, name);
  2916. NEW_CMD_INSERTED;
  2917. }
  2918. BfIRValue BfIRBuilder::CreateUndefValue(BfIRType type)
  2919. {
  2920. BfIRValue retVal = WriteCmd(BfIRCmd_CreateUndefValue, type);
  2921. NEW_CMD_INSERTED_IRVALUE;
  2922. return retVal;
  2923. }
  2924. BfIRValue BfIRBuilder::CreateNumericCast(BfIRValue val, bool valIsSigned, BfTypeCode typeCode)
  2925. {
  2926. FixTypeCode(typeCode);
  2927. if (val.IsConst())
  2928. {
  2929. auto constVal = GetConstantById(val.mId);
  2930. // ? -> Int
  2931. if (IsInt(typeCode))
  2932. {
  2933. uint64 val = 0;
  2934. if ((typeCode == BfTypeCode_IntPtr) || (typeCode == BfTypeCode_UIntPtr))
  2935. {
  2936. if (mModule->mSystem->mPtrSize == 4)
  2937. typeCode = (typeCode == BfTypeCode_IntPtr) ? BfTypeCode_Int32 : BfTypeCode_UInt32;
  2938. else
  2939. typeCode = (typeCode == BfTypeCode_IntPtr) ? BfTypeCode_Int64 : BfTypeCode_UInt64;
  2940. }
  2941. // Int -> Int
  2942. if (IsInt(constVal->mTypeCode))
  2943. {
  2944. switch (typeCode)
  2945. {
  2946. case BfTypeCode_Int8: val = (int8)constVal->mInt64; break;
  2947. case BfTypeCode_Char8:
  2948. case BfTypeCode_UInt8: val = (uint8)constVal->mInt64; break;
  2949. case BfTypeCode_Int16: val = (int16)constVal->mInt64; break;
  2950. case BfTypeCode_Char16:
  2951. case BfTypeCode_UInt16: val = (uint16)constVal->mInt64; break;
  2952. case BfTypeCode_Int32: val = (int32)constVal->mInt64; break;
  2953. case BfTypeCode_Char32:
  2954. case BfTypeCode_UInt32: val = (uint32)constVal->mInt64; break;
  2955. case BfTypeCode_Int64: val = (uint64)(int64)constVal->mInt64; break;
  2956. case BfTypeCode_UInt64: val = (uint64)constVal->mUInt64; break;
  2957. default: break;
  2958. }
  2959. }
  2960. else // Float -> Int
  2961. {
  2962. switch (typeCode)
  2963. {
  2964. case BfTypeCode_Int8: val = (int8)constVal->mDouble; break;
  2965. case BfTypeCode_Char8:
  2966. case BfTypeCode_UInt8: val = (uint8)constVal->mDouble; break;
  2967. case BfTypeCode_Int16: val = (int16)constVal->mDouble; break;
  2968. case BfTypeCode_Char16:
  2969. case BfTypeCode_UInt16: val = (uint16)constVal->mDouble; break;
  2970. case BfTypeCode_Int32: val = (int32)constVal->mDouble; break;
  2971. case BfTypeCode_Char32:
  2972. case BfTypeCode_UInt32: val = (uint32)constVal->mDouble; break;
  2973. case BfTypeCode_Int64: val = (uint64)(int64)constVal->mDouble; break;
  2974. case BfTypeCode_UInt64: val = (uint64)constVal->mDouble; break;
  2975. default: break;
  2976. }
  2977. }
  2978. return CreateConst(typeCode, val);
  2979. }
  2980. // Int -> Float
  2981. if (IsInt(constVal->mTypeCode))
  2982. {
  2983. double val = 0;
  2984. if (IsSigned(constVal->mTypeCode))
  2985. val = (double)constVal->mInt64;
  2986. else
  2987. val = (double)constVal->mUInt64;
  2988. return CreateConst(typeCode, val);
  2989. }
  2990. // Float -> Float
  2991. return CreateConst(typeCode, constVal->mDouble);
  2992. }
  2993. auto retVal = WriteCmd(BfIRCmd_NumericCast, val, valIsSigned, typeCode);
  2994. NEW_CMD_INSERTED_IRVALUE;
  2995. return retVal;
  2996. }
  2997. BfIRValue BfIRBuilder::CreateCmpEQ(BfIRValue lhs, BfIRValue rhs)
  2998. {
  2999. if ((lhs.IsConst()) && (rhs.IsConst()))
  3000. {
  3001. CMP_APPLY(lhs, rhs, ==);
  3002. if ((constLHS->mConstType == BfConstType_GlobalVar) ||
  3003. (constRHS->mConstType == BfConstType_GlobalVar))
  3004. {
  3005. return CreateConst(BfTypeCode_Boolean, (constLHS == constRHS) ? (uint64)1 : (uint64)0);
  3006. }
  3007. }
  3008. auto retVal = WriteCmd(BfIRCmd_CmpEQ, lhs, rhs);
  3009. NEW_CMD_INSERTED_IRVALUE;
  3010. return retVal;
  3011. }
  3012. BfIRValue BfIRBuilder::CreateCmpNE(BfIRValue lhs, BfIRValue rhs)
  3013. {
  3014. if ((lhs.IsConst()) && (rhs.IsConst()))
  3015. {
  3016. CMP_APPLY(lhs, rhs, !=);
  3017. if ((constLHS->mConstType == BfConstType_GlobalVar) ||
  3018. (constRHS->mConstType == BfConstType_GlobalVar))
  3019. {
  3020. return CreateConst(BfTypeCode_Boolean, (constLHS != constRHS) ? (uint64)1 : (uint64)0);
  3021. }
  3022. }
  3023. auto retVal = WriteCmd(BfIRCmd_CmpNE, lhs, rhs);
  3024. NEW_CMD_INSERTED_IRVALUE;
  3025. return retVal;
  3026. }
  3027. BfIRValue BfIRBuilder::CreateCmpLT(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3028. {
  3029. if ((lhs.IsConst()) && (rhs.IsConst()))
  3030. {
  3031. CMP_APPLY(lhs, rhs, <);
  3032. }
  3033. else if ((!isSigned) && (rhs.IsConst()))
  3034. {
  3035. // "unsigned < 0" is always false
  3036. auto constant = GetConstant(rhs);
  3037. if ((IsInt(constant->mTypeCode)) && (constant->mUInt64 == 0))
  3038. return CreateConst(BfTypeCode_Boolean, 0);
  3039. }
  3040. auto retVal = WriteCmd(isSigned ? BfIRCmd_CmpSLT : BfIRCmd_CmpULT, lhs, rhs);
  3041. NEW_CMD_INSERTED_IRVALUE;
  3042. return retVal;
  3043. }
  3044. BfIRValue BfIRBuilder::CreateCmpLTE(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3045. {
  3046. if ((lhs.IsConst()) && (rhs.IsConst()))
  3047. {
  3048. CMP_APPLY(lhs, rhs, <=);
  3049. }
  3050. auto retVal = WriteCmd(isSigned ? BfIRCmd_CmpSLE : BfIRCmd_CmpULE, lhs, rhs);
  3051. NEW_CMD_INSERTED;
  3052. return retVal;
  3053. }
  3054. BfIRValue BfIRBuilder::CreateCmpGT(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3055. {
  3056. if ((lhs.IsConst()) && (rhs.IsConst()))
  3057. {
  3058. CMP_APPLY(lhs, rhs, >);
  3059. }
  3060. auto retVal = WriteCmd(isSigned ? BfIRCmd_CmpSGT : BfIRCmd_CmpUGT, lhs, rhs);
  3061. NEW_CMD_INSERTED_IRVALUE;
  3062. return retVal;
  3063. }
  3064. BfIRValue BfIRBuilder::CreateCmpGTE(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3065. {
  3066. if ((lhs.IsConst()) && (rhs.IsConst()))
  3067. {
  3068. CMP_APPLY(lhs, rhs, >=);
  3069. }
  3070. else if ((!isSigned) && (lhs.IsConst()))
  3071. {
  3072. // "0 >= unsigned" is always true
  3073. auto constant = GetConstant(lhs);
  3074. if ((IsInt(constant->mTypeCode)) && (constant->mUInt64 == 0))
  3075. return CreateConst(BfTypeCode_Boolean, 1);
  3076. }
  3077. auto retVal = WriteCmd(isSigned ? BfIRCmd_CmpSGE : BfIRCmd_CmpUGE, lhs, rhs);
  3078. NEW_CMD_INSERTED_IRVALUE;
  3079. return retVal;
  3080. }
  3081. BfIRValue BfIRBuilder::CreateAdd(BfIRValue lhs, BfIRValue rhs)
  3082. {
  3083. mOpFailed = false;
  3084. if ((lhs.IsConst()) && (rhs.IsConst()))
  3085. {
  3086. auto constLHS = GetConstantById(lhs.mId);
  3087. if (constLHS->mConstType != BfConstType_PtrToInt)
  3088. {
  3089. BINOPFUNC_APPLY(lhs, rhs, CheckedAdd);
  3090. }
  3091. }
  3092. auto retVal = WriteCmd(BfIRCmd_Add, lhs, rhs);
  3093. NEW_CMD_INSERTED_IRVALUE;
  3094. return retVal;
  3095. }
  3096. BfIRValue BfIRBuilder::CreateSub(BfIRValue lhs, BfIRValue rhs)
  3097. {
  3098. mOpFailed = false;
  3099. if ((lhs.IsConst()) && (rhs.IsConst()))
  3100. {
  3101. BINOPFUNC_APPLY(lhs, rhs, CheckedSub);
  3102. }
  3103. auto retVal = WriteCmd(BfIRCmd_Sub, lhs, rhs);
  3104. NEW_CMD_INSERTED;
  3105. return retVal;
  3106. }
  3107. BfIRValue BfIRBuilder::CreateMul(BfIRValue lhs, BfIRValue rhs)
  3108. {
  3109. mOpFailed = false;
  3110. if ((lhs.IsConst()) && (rhs.IsConst()))
  3111. {
  3112. BINOPFUNC_APPLY(lhs, rhs, CheckedMul);
  3113. }
  3114. auto retVal = WriteCmd(BfIRCmd_Mul, lhs, rhs);
  3115. NEW_CMD_INSERTED_IRVALUE;
  3116. return retVal;
  3117. }
  3118. BfIRValue BfIRBuilder::CreateDiv(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3119. {
  3120. if ((lhs.IsConst()) && (rhs.IsConst()))
  3121. {
  3122. BINOP_APPLY(lhs, rhs, /);
  3123. }
  3124. auto retVal = WriteCmd(isSigned ? BfIRCmd_SDiv : BfIRCmd_UDiv, lhs, rhs);
  3125. NEW_CMD_INSERTED_IRVALUE;
  3126. return retVal;
  3127. }
  3128. BfIRValue BfIRBuilder::CreateRem(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3129. {
  3130. if ((lhs.IsConst()) && (rhs.IsConst()))
  3131. {
  3132. auto constLHS = GetConstantById(lhs.mId);
  3133. auto constRHS = GetConstantById(rhs.mId);
  3134. INT_BINOP_APPLY(constLHS, constRHS, %);
  3135. double fVal = fmod(constLHS->mDouble, constRHS->mDouble);
  3136. return CreateConst(constLHS->mTypeCode, fVal);
  3137. }
  3138. auto retVal = WriteCmd(isSigned ? BfIRCmd_SRem : BfIRCmd_URem, lhs, rhs);
  3139. NEW_CMD_INSERTED_IRVALUE;
  3140. return retVal;
  3141. }
  3142. BfIRValue BfIRBuilder::CreateAnd(BfIRValue lhs, BfIRValue rhs)
  3143. {
  3144. if ((lhs.IsConst()) && (rhs.IsConst()))
  3145. {
  3146. auto constLHS = GetConstantById(lhs.mId);
  3147. auto constRHS = GetConstantById(rhs.mId);
  3148. INT_BINOP_APPLY(constLHS, constRHS, &);
  3149. }
  3150. auto retVal = WriteCmd(BfIRCmd_And, lhs, rhs);
  3151. NEW_CMD_INSERTED_IRVALUE;
  3152. return retVal;
  3153. }
  3154. BfIRValue BfIRBuilder::CreateOr(BfIRValue lhs, BfIRValue rhs)
  3155. {
  3156. if ((lhs.IsConst()) && (rhs.IsConst()))
  3157. {
  3158. auto constLHS = GetConstantById(lhs.mId);
  3159. auto constRHS = GetConstantById(rhs.mId);
  3160. INT_BINOP_APPLY(constLHS, constRHS, |);
  3161. }
  3162. auto retVal = WriteCmd(BfIRCmd_Or, lhs, rhs);
  3163. NEW_CMD_INSERTED_IRVALUE;
  3164. return retVal;
  3165. }
  3166. BfIRValue BfIRBuilder::CreateXor(BfIRValue lhs, BfIRValue rhs)
  3167. {
  3168. if ((lhs.IsConst()) && (rhs.IsConst()))
  3169. {
  3170. auto constLHS = GetConstantById(lhs.mId);
  3171. auto constRHS = GetConstantById(rhs.mId);
  3172. INT_BINOP_APPLY(constLHS, constRHS, ^);
  3173. }
  3174. auto retVal = WriteCmd(BfIRCmd_Xor, lhs, rhs);
  3175. NEW_CMD_INSERTED_IRVALUE;
  3176. return retVal;
  3177. }
  3178. BfIRValue BfIRBuilder::CreateShl(BfIRValue lhs, BfIRValue rhs)
  3179. {
  3180. mOpFailed = false;
  3181. if ((lhs.IsConst()) && (rhs.IsConst()))
  3182. {
  3183. INT_BINOPFUNC_APPLY(lhs, rhs, CheckedShl);
  3184. }
  3185. auto retVal = WriteCmd(BfIRCmd_Shl, lhs, rhs);
  3186. NEW_CMD_INSERTED_IRVALUE;
  3187. return retVal;
  3188. }
  3189. BfIRValue BfIRBuilder::CreateShr(BfIRValue lhs, BfIRValue rhs, bool isSigned)
  3190. {
  3191. if ((lhs.IsConst()) && (rhs.IsConst()))
  3192. {
  3193. auto constLHS = GetConstantById(lhs.mId);
  3194. auto constRHS = GetConstantById(rhs.mId);
  3195. uint64 val;
  3196. if (isSigned)
  3197. val = (uint64)(constLHS->mInt64 >> constRHS->mInt32);
  3198. else
  3199. val = constLHS->mUInt64 >> constRHS->mInt32;
  3200. return CreateConst(constLHS->mTypeCode, val);
  3201. }
  3202. auto retVal = WriteCmd(isSigned ? BfIRCmd_AShr : BfIRCmd_LShr, lhs, rhs);
  3203. NEW_CMD_INSERTED_IRVALUE;
  3204. return retVal;
  3205. }
  3206. BfIRValue BfIRBuilder::CreateNeg(BfIRValue val)
  3207. {
  3208. if (val.IsConst())
  3209. {
  3210. UNARYOP_APPLY(val, -);
  3211. }
  3212. auto retVal = WriteCmd(BfIRCmd_Neg, val);
  3213. NEW_CMD_INSERTED_IRVALUE;
  3214. return retVal;
  3215. }
  3216. BfIRValue BfIRBuilder::CreateNot(BfIRValue val)
  3217. {
  3218. if (val.IsConst())
  3219. {
  3220. auto constVal = GetConstantById(val.mId);
  3221. uint64 newVal = 0;
  3222. if (constVal->mTypeCode == BfTypeCode_Boolean)
  3223. newVal = constVal->mBool ? 0 : 1;
  3224. else
  3225. newVal = ~constVal->mUInt64;
  3226. return CreateConst(constVal->mTypeCode, newVal);
  3227. }
  3228. auto retVal = WriteCmd(BfIRCmd_Not, val);
  3229. NEW_CMD_INSERTED_IRVALUE;
  3230. return retVal;
  3231. }
  3232. BfIRValue BfIRBuilder::CreateBitCast(BfIRValue val, BfIRType type)
  3233. {
  3234. if (val.IsConst())
  3235. {
  3236. auto constVal = GetConstant(val);
  3237. auto bitCast = mTempAlloc.Alloc<BfConstantBitCast>();
  3238. if (constVal->IsNull())
  3239. bitCast->mConstType = BfConstType_BitCastNull;
  3240. else
  3241. bitCast->mConstType = BfConstType_BitCast;
  3242. bitCast->mTarget = val.mId;
  3243. bitCast->mToType = type;
  3244. BfIRValue castedVal(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(bitCast));
  3245. #ifdef CHECK_CONSTHOLDER
  3246. castedVal.mHolder = this;
  3247. #endif
  3248. BF_ASSERT((void*)GetConstant(castedVal) == (void*)bitCast);
  3249. return castedVal;
  3250. }
  3251. auto retVal = WriteCmd(BfIRCmd_BitCast, val, type);
  3252. NEW_CMD_INSERTED_IRVALUE;
  3253. return retVal;
  3254. }
  3255. BfIRValue BfIRBuilder::CreatePtrToInt(BfIRValue val, BfTypeCode typeCode)
  3256. {
  3257. FixTypeCode(typeCode);
  3258. if (val.IsConst())
  3259. {
  3260. auto ptrToInt = mTempAlloc.Alloc<BfConstantPtrToInt>();
  3261. ptrToInt->mConstType = BfConstType_PtrToInt;
  3262. ptrToInt->mTarget = val.mId;
  3263. ptrToInt->mToTypeCode = typeCode;
  3264. BfIRValue castedVal(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(ptrToInt));
  3265. #ifdef CHECK_CONSTHOLDER
  3266. castedVal.mHolder = this;
  3267. #endif
  3268. return castedVal;
  3269. }
  3270. auto retVal = WriteCmd(BfIRCmd_PtrToInt, val, typeCode);
  3271. NEW_CMD_INSERTED_IRVALUE;
  3272. return retVal;
  3273. }
  3274. BfIRValue BfIRBuilder::CreateIntToPtr(BfIRValue val, BfIRType type)
  3275. {
  3276. BfIRValue retVal = WriteCmd(BfIRCmd_IntToPtr, val, type);
  3277. NEW_CMD_INSERTED_IRVALUE;
  3278. return retVal;
  3279. }
  3280. BfIRValue BfIRBuilder::CreateInBoundsGEP(BfIRValue val, int idx0)
  3281. {
  3282. BfIRValue retVal = WriteCmd(BfIRCmd_InboundsGEP1_32, val, idx0);
  3283. NEW_CMD_INSERTED_IRVALUE;
  3284. return retVal;
  3285. }
  3286. BfIRValue BfIRBuilder::CreateInBoundsGEP(BfIRValue val, int idx0, int idx1)
  3287. {
  3288. if (val.IsConst())
  3289. {
  3290. auto constGEP = mTempAlloc.Alloc<BfConstantGEP32_2>();
  3291. constGEP->mConstType = BfConstType_GEP32_2;
  3292. constGEP->mTarget = val.mId;
  3293. constGEP->mIdx0 = idx0;
  3294. constGEP->mIdx1 = idx1;
  3295. BfIRValue retVal;
  3296. retVal.mFlags = BfIRValueFlags_Const;
  3297. retVal.mId = mTempAlloc.GetChunkedId(constGEP);
  3298. #ifdef CHECK_CONSTHOLDER
  3299. retVal.mHolder = this;
  3300. #endif
  3301. return retVal;
  3302. }
  3303. auto retVal = WriteCmd(BfIRCmd_InboundsGEP2_32, val, idx0, idx1);
  3304. NEW_CMD_INSERTED_IRVALUE;
  3305. return retVal;
  3306. }
  3307. BfIRValue BfIRBuilder::CreateInBoundsGEP(BfIRValue val, BfIRValue idx0)
  3308. {
  3309. BfIRValue retVal = WriteCmd(BfIRCmd_InBoundsGEP1, val, idx0);
  3310. NEW_CMD_INSERTED_IRVALUE;
  3311. return retVal;
  3312. }
  3313. BfIRValue BfIRBuilder::CreateInBoundsGEP(BfIRValue val, BfIRValue idx0, BfIRValue idx1)
  3314. {
  3315. BfIRValue retVal = WriteCmd(BfIRCmd_InBoundsGEP2, val, idx0, idx1);
  3316. NEW_CMD_INSERTED_IRVALUE;
  3317. return retVal;
  3318. }
  3319. BfIRValue BfIRBuilder::CreateIsNull(BfIRValue val)
  3320. {
  3321. BfIRValue retVal = WriteCmd(BfIRCmd_IsNull, val);
  3322. NEW_CMD_INSERTED_IRVALUE;
  3323. return retVal;
  3324. }
  3325. BfIRValue BfIRBuilder::CreateIsNotNull(BfIRValue val)
  3326. {
  3327. BfIRValue retVal = WriteCmd(BfIRCmd_IsNotNull, val);
  3328. NEW_CMD_INSERTED_IRVALUE;
  3329. return retVal;
  3330. }
  3331. BfIRValue BfIRBuilder::CreateExtractValue(BfIRValue val, int idx)
  3332. {
  3333. auto aggConstant = GetConstant(val);
  3334. if (aggConstant != NULL)
  3335. {
  3336. if (aggConstant->mConstType == BfConstType_Array)
  3337. {
  3338. auto arrayConstant = (BfConstantArray*)aggConstant;
  3339. return arrayConstant->mValues[idx];
  3340. }
  3341. }
  3342. BfIRValue retVal = WriteCmd(BfIRCmd_ExtractValue, val, idx);
  3343. NEW_CMD_INSERTED_IRVALUE;
  3344. return retVal;
  3345. }
  3346. BfIRValue BfIRBuilder::CreateExtractValue(BfIRValue val, BfIRValue idx)
  3347. {
  3348. auto idxConst = GetConstant(idx);
  3349. if (idxConst != NULL)
  3350. {
  3351. BF_ASSERT(IsInt(idxConst->mTypeCode));
  3352. return CreateExtractValue(val, idxConst->mInt32);
  3353. }
  3354. if (mIgnoreWrites)
  3355. return GetFakeVal();
  3356. // We allow looking up into a const array with a non-const index by creating memory backing for the array
  3357. auto arrConst = GetConstant(val);
  3358. if (arrConst != NULL)
  3359. {
  3360. if ((arrConst->mConstType == BfConstType_Array) || (arrConst->mConstType == BfConstType_AggZero))
  3361. {
  3362. BfIRValue arrMemVal = ConstToMemory(val);
  3363. auto valAddr = CreateInBoundsGEP(arrMemVal, CreateConst(BfTypeCode_IntPtr, 0), idx);
  3364. return CreateLoad(valAddr);
  3365. }
  3366. }
  3367. BF_FATAL("Invalid extract value");
  3368. return BfIRValue();
  3369. }
  3370. BfIRValue BfIRBuilder::CreateInsertValue(BfIRValue agg, BfIRValue val, int idx)
  3371. {
  3372. BfIRValue retVal = WriteCmd(BfIRCmd_InsertValue, agg, val, idx);
  3373. NEW_CMD_INSERTED_IRVALUE;
  3374. return retVal;
  3375. }
  3376. BfIRValue BfIRBuilder::CreateAlloca(BfIRType type)
  3377. {
  3378. BfIRValue retVal = WriteCmd(BfIRCmd_Alloca, type);
  3379. NEW_CMD_INSERTED_IRVALUE;
  3380. return retVal;
  3381. }
  3382. BfIRValue BfIRBuilder::CreateAlloca(BfIRType type, BfIRValue arraySize)
  3383. {
  3384. BfIRValue retVal = WriteCmd(BfIRCmd_AllocaArray, type, arraySize);
  3385. NEW_CMD_INSERTED_IRVALUE;
  3386. return retVal;
  3387. }
  3388. void BfIRBuilder::SetAllocaAlignment(BfIRValue val, int alignment)
  3389. {
  3390. BfIRValue retVal = WriteCmd(BfIRCmd_SetAllocaAlignment, val, alignment);
  3391. NEW_CMD_INSERTED;
  3392. }
  3393. void BfIRBuilder::SetAllocaNoChkStkHint(BfIRValue val)
  3394. {
  3395. BfIRValue retVal = WriteCmd(BfIRCmd_SetAllocaNoChkStkHint, val);
  3396. NEW_CMD_INSERTED;
  3397. }
  3398. void BfIRBuilder::SetAllocaForceMem(BfIRValue val)
  3399. {
  3400. BfIRValue retVal = WriteCmd(BfIRCmd_SetAllocaForceMem, val);
  3401. NEW_CMD_INSERTED;
  3402. }
  3403. BfIRValue BfIRBuilder::CreateAliasValue(BfIRValue val)
  3404. {
  3405. BfIRValue retVal = WriteCmd(BfIRCmd_AliasValue, val);
  3406. NEW_CMD_INSERTED;
  3407. return retVal;
  3408. }
  3409. BfIRValue BfIRBuilder::CreateLifetimeStart(BfIRValue val)
  3410. {
  3411. BfIRValue retVal = WriteCmd(BfIRCmd_LifetimeStart, val);
  3412. NEW_CMD_INSERTED;
  3413. return retVal;
  3414. }
  3415. BfIRValue BfIRBuilder::CreateLifetimeEnd(BfIRValue val)
  3416. {
  3417. BfIRValue retVal = WriteCmd(BfIRCmd_LifetimeEnd, val);
  3418. NEW_CMD_INSERTED;
  3419. return retVal;
  3420. }
  3421. BfIRValue BfIRBuilder::CreateLifetimeExtend(BfIRValue val)
  3422. {
  3423. BfIRValue retVal = WriteCmd(BfIRCmd_LifetimeExtend, val);
  3424. NEW_CMD_INSERTED;
  3425. return retVal;
  3426. }
  3427. BfIRValue BfIRBuilder::CreateValueScopeStart()
  3428. {
  3429. BfIRValue retVal = WriteCmd(BfIRCmd_ValueScopeStart);
  3430. NEW_CMD_INSERTED;
  3431. return retVal;
  3432. }
  3433. void BfIRBuilder::CreateValueScopeRetain(BfIRValue val)
  3434. {
  3435. BfIRValue retVal = WriteCmd(BfIRCmd_ValueScopeRetain, val);
  3436. NEW_CMD_INSERTED;
  3437. }
  3438. void BfIRBuilder::CreateValueScopeSoftEnd(BfIRValue scopeStart)
  3439. {
  3440. BfIRValue retVal = WriteCmd(BfIRCmd_ValueScopeSoftEnd, scopeStart);
  3441. NEW_CMD_INSERTED;
  3442. }
  3443. void BfIRBuilder::CreateValueScopeHardEnd(BfIRValue scopeStart)
  3444. {
  3445. BfIRValue retVal = WriteCmd(BfIRCmd_ValueScopeHardEnd, scopeStart);
  3446. NEW_CMD_INSERTED;
  3447. }
  3448. BfIRValue BfIRBuilder::CreateLoad(BfIRValue val, bool isVolatile)
  3449. {
  3450. BfIRValue retVal = WriteCmd(BfIRCmd_Load, val, isVolatile);
  3451. NEW_CMD_INSERTED_IRVALUE;
  3452. return retVal;
  3453. }
  3454. BfIRValue BfIRBuilder::CreateAlignedLoad(BfIRValue val, int align, bool isVolatile)
  3455. {
  3456. BfIRValue retVal = WriteCmd(BfIRCmd_AlignedLoad, val, align, isVolatile);
  3457. NEW_CMD_INSERTED_IRVALUE;
  3458. return retVal;
  3459. }
  3460. BfIRValue BfIRBuilder::CreateStore(BfIRValue val, BfIRValue ptr, bool isVolatile)
  3461. {
  3462. BfIRValue retVal = WriteCmd(BfIRCmd_Store, val, ptr, isVolatile);
  3463. NEW_CMD_INSERTED_IRVALUE;
  3464. return retVal;
  3465. }
  3466. BfIRValue BfIRBuilder::CreateAlignedStore(BfIRValue val, BfIRValue ptr, int align, bool isVolatile)
  3467. {
  3468. BfIRValue retVal = WriteCmd(BfIRCmd_AlignedStore, val, ptr, align, isVolatile);
  3469. NEW_CMD_INSERTED_IRVALUE;
  3470. return retVal;
  3471. }
  3472. BfIRValue BfIRBuilder::CreateMemSet(BfIRValue addr, BfIRValue val, BfIRValue size, int align)
  3473. {
  3474. BfIRValue retVal = WriteCmd(BfIRCmd_MemSet, addr, val, size, align);
  3475. NEW_CMD_INSERTED_IRVALUE;
  3476. return retVal;
  3477. }
  3478. void BfIRBuilder::CreateFence(BfIRFenceType fenceType)
  3479. {
  3480. BfIRValue retVal = WriteCmd(BfIRCmd_Fence, (uint8)fenceType);
  3481. NEW_CMD_INSERTED;
  3482. }
  3483. BfIRValue BfIRBuilder::CreateStackSave()
  3484. {
  3485. BfIRValue retVal = WriteCmd(BfIRCmd_StackSave);
  3486. NEW_CMD_INSERTED;
  3487. return retVal;
  3488. }
  3489. BfIRValue BfIRBuilder::CreateStackRestore(BfIRValue stackVal)
  3490. {
  3491. BfIRValue retVal = WriteCmd(BfIRCmd_StackRestore, stackVal);
  3492. NEW_CMD_INSERTED;
  3493. return retVal;
  3494. }
  3495. BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
  3496. {
  3497. BF_ASSERT(varType);
  3498. auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
  3499. int chunkId = mTempAlloc.GetChunkedId(constGV);
  3500. constGV->mStreamId = -1;
  3501. constGV->mConstType = BfConstType_GlobalVar;
  3502. constGV->mType = varType;
  3503. constGV->mIsConst = isConstant;
  3504. constGV->mLinkageType = linkageType;
  3505. constGV->mInitializer = initializer;
  3506. constGV->mName = AllocStr(name);
  3507. constGV->mIsTLS = isTLS;
  3508. if (!mIgnoreWrites)
  3509. {
  3510. if (initializer)
  3511. mHasGlobalDefs = true;
  3512. BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVariable, varType, isConstant, (uint8)linkageType, initializer, name, isTLS);
  3513. constGV->mStreamId = retVal.mId;
  3514. retVal.mFlags = BfIRValueFlags_Const;
  3515. #ifdef CHECK_CONSTHOLDER
  3516. retVal.mHolder = this;
  3517. #endif
  3518. retVal.mId = chunkId;
  3519. NEW_CMD_INSERTED_IRVALUE;
  3520. return retVal;
  3521. }
  3522. auto irValue = BfIRValue(BfIRValueFlags_Const, chunkId);
  3523. return irValue;
  3524. }
  3525. void BfIRBuilder::GlobalVar_SetUnnamedAddr(BfIRValue val, bool unnamedAddr)
  3526. {
  3527. BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVar_SetUnnamedAddr, val, unnamedAddr);
  3528. NEW_CMD_INSERTED;
  3529. }
  3530. void BfIRBuilder::GlobalVar_SetInitializer(BfIRValue globalVar, BfIRValue initVal)
  3531. {
  3532. BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVar_SetInitializer, globalVar, initVal);
  3533. NEW_CMD_INSERTED;
  3534. }
  3535. void BfIRBuilder::GlobalVar_SetAlignment(BfIRValue globalVar, int alignment)
  3536. {
  3537. BF_ASSERT(alignment != -1);
  3538. BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVar_SetAlignment, globalVar, alignment);
  3539. NEW_CMD_INSERTED;
  3540. }
  3541. BfIRValue BfIRBuilder::CreateGlobalStringPtr(const StringImpl& str)
  3542. {
  3543. BfIRValue retVal = WriteCmd(BfIRCmd_GlobalStringPtr, str);
  3544. NEW_CMD_INSERTED_IRVALUE;
  3545. return retVal;
  3546. }
  3547. BfIRBlock BfIRBuilder::CreateBlock(const StringImpl& name, bool addNow)
  3548. {
  3549. if (addNow)
  3550. mActiveFunctionHasBody = true;
  3551. mBlockCount++;
  3552. BfIRBlock retBlock = WriteCmd(BfIRCmd_CreateBlock, name, addNow);
  3553. NEW_CMD_INSERTED_IRBLOCK;
  3554. return retBlock;
  3555. }
  3556. BfIRBlock BfIRBuilder::MaybeChainNewBlock(const StringImpl& name)
  3557. {
  3558. BfIRBlock retBlock = WriteCmd(BfIRCmd_MaybeChainNewBlock, name);
  3559. NEW_CMD_INSERTED_IRBLOCK;
  3560. if (!mIgnoreWrites)
  3561. {
  3562. BF_ASSERT(!retBlock.IsFake());
  3563. mActualInsertBlock = retBlock;
  3564. }
  3565. mInsertBlock = retBlock;
  3566. return retBlock;
  3567. }
  3568. void BfIRBuilder::AddBlock(BfIRBlock block)
  3569. {
  3570. mActiveFunctionHasBody = true;
  3571. BfIRValue retVal = WriteCmd(BfIRCmd_AddBlock, block);
  3572. NEW_CMD_INSERTED;
  3573. }
  3574. void BfIRBuilder::DropBlocks(BfIRBlock startingBlock)
  3575. {
  3576. WriteCmd(BfIRCmd_DropBlocks, startingBlock);
  3577. NEW_CMD_INSERTED;
  3578. }
  3579. void BfIRBuilder::MergeBlockDown(BfIRBlock fromBlock, BfIRBlock intoBlock)
  3580. {
  3581. WriteCmd(BfIRCmd_MergeBlockDown, fromBlock, intoBlock);
  3582. NEW_CMD_INSERTED;
  3583. }
  3584. void BfIRBuilder::SetInsertPoint(BfIRValue value)
  3585. {
  3586. BfIRValue retVal = WriteCmd(BfIRCmd_SetInsertPoint, value);
  3587. NEW_CMD_INSERTED;
  3588. }
  3589. void BfIRBuilder::SetInsertPoint(BfIRBlock block)
  3590. {
  3591. BfIRValue retVal = WriteCmd(BfIRCmd_SetInsertPoint, block);
  3592. if (!mIgnoreWrites)
  3593. {
  3594. BF_ASSERT(!block.IsFake());
  3595. mActualInsertBlock = block;
  3596. }
  3597. mInsertBlock = block;
  3598. NEW_CMD_INSERTED;
  3599. }
  3600. void BfIRBuilder::SetInsertPointAtStart(BfIRBlock block)
  3601. {
  3602. BfIRValue retVal = WriteCmd(BfIRCmd_SetInsertPointAtStart, block);
  3603. if (!mIgnoreWrites)
  3604. {
  3605. BF_ASSERT(!block.IsFake());
  3606. mActualInsertBlock = block;
  3607. }
  3608. mInsertBlock = block;
  3609. NEW_CMD_INSERTED;
  3610. }
  3611. void BfIRBuilder::EraseFromParent(BfIRBlock block)
  3612. {
  3613. BfIRValue retVal = WriteCmd(BfIRCmd_EraseFromParent, block);
  3614. NEW_CMD_INSERTED;
  3615. }
  3616. void BfIRBuilder::DeleteBlock(BfIRBlock block)
  3617. {
  3618. BfIRValue retVal = WriteCmd(BfIRCmd_DeleteBlock, block);
  3619. NEW_CMD_INSERTED;
  3620. }
  3621. void BfIRBuilder::EraseInstFromParent(BfIRValue val)
  3622. {
  3623. BfIRValue retVal = WriteCmd(BfIRCmd_EraseInstFromParent, val);
  3624. NEW_CMD_INSERTED;
  3625. }
  3626. BfIRValue BfIRBuilder::CreateBr(BfIRBlock block)
  3627. {
  3628. BfIRValue retVal = WriteCmd(BfIRCmd_CreateBr, block);
  3629. NEW_CMD_INSERTED;
  3630. return retVal;
  3631. }
  3632. BfIRValue BfIRBuilder::CreateBr_Fake(BfIRBlock block)
  3633. {
  3634. BfIRValue retVal = WriteCmd(BfIRCmd_CreateBr_Fake, block);
  3635. NEW_CMD_INSERTED;
  3636. return retVal;
  3637. }
  3638. BfIRValue BfIRBuilder::CreateBr_NoCollapse(BfIRBlock block)
  3639. {
  3640. BfIRValue retVal = WriteCmd(BfIRCmd_CreateBr_NoCollapse, block);
  3641. NEW_CMD_INSERTED;
  3642. return retVal;
  3643. }
  3644. void BfIRBuilder::CreateCondBr(BfIRValue val, BfIRBlock trueBlock, BfIRBlock falseBlock)
  3645. {
  3646. BfIRValue retVal = WriteCmd(BfIRCmd_CreateCondBr, val, trueBlock, falseBlock);
  3647. NEW_CMD_INSERTED;
  3648. }
  3649. BfIRBlock BfIRBuilder::GetInsertBlock()
  3650. {
  3651. if (!mIgnoreWrites)
  3652. {
  3653. BF_ASSERT(!mActualInsertBlock.IsFake());
  3654. return mActualInsertBlock;
  3655. }
  3656. return mInsertBlock;
  3657. }
  3658. void BfIRBuilder::MoveBlockToEnd(BfIRBlock block)
  3659. {
  3660. BfIRValue retVal = WriteCmd(BfIRCmd_MoveBlockToEnd, block);
  3661. NEW_CMD_INSERTED;
  3662. }
  3663. BfIRValue BfIRBuilder::CreateSwitch(BfIRValue value, BfIRBlock dest, int numCases)
  3664. {
  3665. BfIRValue retVal = WriteCmd(BfIRCmd_CreateSwitch, value, dest, numCases);
  3666. NEW_CMD_INSERTED_IRVALUE;
  3667. return retVal;
  3668. }
  3669. BfIRValue BfIRBuilder::AddSwitchCase(BfIRValue switchVal, BfIRValue caseVal, BfIRBlock caseBlock)
  3670. {
  3671. BfIRValue retVal = WriteCmd(BfIRCmd_AddSwitchCase, switchVal, caseVal, caseBlock);
  3672. BF_ASSERT(caseVal.IsConst());
  3673. NEW_CMD_INSERTED;
  3674. return retVal;
  3675. }
  3676. void BfIRBuilder::SetSwitchDefaultDest(BfIRValue switchVal, BfIRBlock caseBlock)
  3677. {
  3678. WriteCmd(BfIRCmd_SetSwitchDefaultDest, switchVal, caseBlock);
  3679. NEW_CMD_INSERTED;
  3680. }
  3681. BfIRValue BfIRBuilder::CreatePhi(BfIRType type, int incomingCount)
  3682. {
  3683. BfIRValue retVal = WriteCmd(BfIRCmd_CreatePhi, type, incomingCount);
  3684. NEW_CMD_INSERTED_IRVALUE;
  3685. return retVal;
  3686. }
  3687. void BfIRBuilder::AddPhiIncoming(BfIRValue phi, BfIRValue value, BfIRBlock comingFrom)
  3688. {
  3689. BfIRValue retVal = WriteCmd(BfIRCmd_AddPhiIncoming, phi, value, comingFrom);
  3690. NEW_CMD_INSERTED;
  3691. }
  3692. BfIRFunction BfIRBuilder::GetIntrinsic(int intrinId, const BfSizedArray<BfIRType>& paramTypes)
  3693. {
  3694. BfIRValue retVal = WriteCmd(BfIRCmd_GetIntrinsic, intrinId, paramTypes);
  3695. NEW_CMD_INSERTED;
  3696. return retVal;
  3697. }
  3698. BfIRFunctionType BfIRBuilder::MapMethod(BfMethodInstance* methodInstance)
  3699. {
  3700. if (mIgnoreWrites)
  3701. return GetFakeFunctionType();
  3702. bool useCache = (!mModule->mIsSpecialModule) && (methodInstance->mMethodDef->mIdx >= 0);
  3703. if (useCache)
  3704. {
  3705. BfIRFunctionType* funcType = NULL;
  3706. if (mMethodTypeMap.TryGetValue(methodInstance, &funcType))
  3707. return *funcType;
  3708. }
  3709. BfIRType retType;
  3710. SizedArray<BfIRType, 8> paramTypes;
  3711. methodInstance->GetIRFunctionInfo(mModule, retType, paramTypes);
  3712. auto funcType = CreateFunctionType(retType, paramTypes, false);
  3713. if (useCache)
  3714. mMethodTypeMap[methodInstance] = funcType;
  3715. return funcType;
  3716. }
  3717. BfIRFunctionType BfIRBuilder::CreateFunctionType(BfIRType resultType, const BfSizedArray<BfIRType>& paramTypes, bool isVarArg)
  3718. {
  3719. BfIRFunctionType retType = WriteCmd(BfIRCmd_CreateFunctionType, resultType, paramTypes, isVarArg);
  3720. NEW_CMD_INSERTED_IRFUNCTYPE;
  3721. return retType;
  3722. }
  3723. BfIRFunction BfIRBuilder::CreateFunction(BfIRFunctionType funcType, BfIRLinkageType linkageType, const StringImpl& name)
  3724. {
  3725. if (mIgnoreWrites)
  3726. {
  3727. auto fakeVal = GetFakeVal();
  3728. return fakeVal;
  3729. }
  3730. BF_ASSERT(mModule->mIsModuleMutable);
  3731. BfIRFunction retVal = WriteCmd(BfIRCmd_CreateFunction, funcType, (uint8)linkageType, name);
  3732. NEW_CMD_INSERTED_IRVALUE;
  3733. mFunctionMap[name] = retVal;
  3734. //BfLogSys(mModule->mSystem, "BfIRBuilder::CreateFunction: %d %s Module:%p\n", retVal.mId, name.c_str(), mModule);
  3735. return retVal;
  3736. }
  3737. void BfIRBuilder::EnsureFunctionPatchable()
  3738. {
  3739. BfIRValue retVal = WriteCmd(BfIRCmd_EnsureFunctionPatchable);
  3740. NEW_CMD_INSERTED_IRVALUE;
  3741. }
  3742. BfIRValue BfIRBuilder::RemapBindFunction(BfIRValue func)
  3743. {
  3744. BfIRValue retVal = WriteCmd(BfIRCmd_RemapBindFunction, func);
  3745. NEW_CMD_INSERTED_IRVALUE;
  3746. return retVal;
  3747. }
  3748. void BfIRBuilder::SetActiveFunction(BfIRFunction func)
  3749. {
  3750. //BfLogSys(mModule->mSystem, "BfIRBuilder::SetActiveFunction: %d\n", func.mId);
  3751. if (mActiveFunctionHasBody)
  3752. mNumFunctionsWithBodies++;
  3753. WriteCmd(BfIRCmd_SetActiveFunction, func);
  3754. mActiveFunction = func;
  3755. mActiveFunctionHasBody = false;
  3756. NEW_CMD_INSERTED;
  3757. }
  3758. BfIRFunction BfIRBuilder::GetActiveFunction()
  3759. {
  3760. return mActiveFunction;
  3761. }
  3762. BfIRFunction BfIRBuilder::GetFunction(const StringImpl& name)
  3763. {
  3764. BfIRFunction* funcPtr = NULL;
  3765. if (mFunctionMap.TryGetValue(name, &funcPtr))
  3766. return *funcPtr;
  3767. return BfIRFunction();
  3768. }
  3769. BfIRValue BfIRBuilder::CreateCall(BfIRValue func, const BfSizedArray<BfIRValue>& args)
  3770. {
  3771. BfIRValue retVal = WriteCmd(BfIRCmd_CreateCall, func, args);
  3772. NEW_CMD_INSERTED_IRVALUE;
  3773. return retVal;
  3774. }
  3775. void BfIRBuilder::SetCallCallingConv(BfIRValue callInst, BfIRCallingConv callingConv)
  3776. {
  3777. if (callingConv == BfIRCallingConv_CDecl)
  3778. return;
  3779. WriteCmd(BfIRCmd_SetCallCallingConv, callInst, (uint8)callingConv);
  3780. NEW_CMD_INSERTED;
  3781. }
  3782. void BfIRBuilder::SetFuncCallingConv(BfIRFunction func, BfIRCallingConv callingConv)
  3783. {
  3784. if (callingConv == BfIRCallingConv_CDecl)
  3785. return;
  3786. WriteCmd(BfIRCmd_SetFuncCallingConv, func, (uint8)callingConv);
  3787. NEW_CMD_INSERTED;
  3788. }
  3789. void BfIRBuilder::SetTailCall(BfIRValue callInst)
  3790. {
  3791. WriteCmd(BfIRCmd_SetTailCall, callInst);
  3792. NEW_CMD_INSERTED;
  3793. }
  3794. void BfIRBuilder::SetCallAttribute(BfIRValue callInst, int paramIdx, BfIRAttribute attribute)
  3795. {
  3796. WriteCmd(BfIRCmd_SetCallAttribute, callInst, paramIdx, attribute);
  3797. NEW_CMD_INSERTED;
  3798. }
  3799. BfIRValue BfIRBuilder::CreateRet(BfIRValue val)
  3800. {
  3801. BfIRValue retVal = WriteCmd(BfIRCmd_CreateRet, val);
  3802. NEW_CMD_INSERTED;
  3803. return retVal;
  3804. }
  3805. void BfIRBuilder::CreateRetVoid()
  3806. {
  3807. WriteCmd(BfIRCmd_CreateRetVoid);
  3808. NEW_CMD_INSERTED;
  3809. }
  3810. void BfIRBuilder::CreateUnreachable()
  3811. {
  3812. WriteCmd(BfIRCmd_CreateUnreachable);
  3813. NEW_CMD_INSERTED;
  3814. }
  3815. void BfIRBuilder::Call_AddAttribute(BfIRValue callInst, int argIdx, BfIRAttribute attr)
  3816. {
  3817. WriteCmd(BfIRCmd_Call_AddAttribute, callInst, argIdx, attr);
  3818. NEW_CMD_INSERTED;
  3819. }
  3820. void BfIRBuilder::Call_AddAttribute(BfIRValue callInst, int argIdx, BfIRAttribute attr, int arg)
  3821. {
  3822. WriteCmd(BfIRCmd_Call_AddAttribute1, callInst, argIdx, attr, arg);
  3823. NEW_CMD_INSERTED;
  3824. }
  3825. void BfIRBuilder::Func_AddAttribute(BfIRFunction func, int argIdx, BfIRAttribute attr)
  3826. {
  3827. WriteCmd(BfIRCmd_Func_AddAttribute, func, argIdx, attr);
  3828. NEW_CMD_INSERTED;
  3829. }
  3830. void BfIRBuilder::Func_AddAttribute(BfIRFunction func, int argIdx, BfIRAttribute attr, int arg)
  3831. {
  3832. WriteCmd(BfIRCmd_Func_AddAttribute1, func, argIdx, attr, arg);
  3833. NEW_CMD_INSERTED;
  3834. }
  3835. void BfIRBuilder::Func_SetParamName(BfIRFunction func, int argIdx, const StringImpl& name)
  3836. {
  3837. WriteCmd(BfIRCmd_Func_SetParamName, func, argIdx, name);
  3838. NEW_CMD_INSERTED;
  3839. }
  3840. void BfIRBuilder::Func_DeleteBody(BfIRFunction func)
  3841. {
  3842. if (mActiveFunction == func)
  3843. mActiveFunctionHasBody = false;
  3844. WriteCmd(BfIRCmd_Func_DeleteBody, func);
  3845. NEW_CMD_INSERTED;
  3846. }
  3847. void BfIRBuilder::Func_EraseFromParent(BfIRFunction func)
  3848. {
  3849. // Refuse to erase from parent
  3850. /*WriteCmd(BfIRCmd_Func_EraseFromParent, func);
  3851. // We don't actually remove it from the named map. It doesn't matter for us.
  3852. {
  3853. auto llvmFunc = llvm::dyn_cast<llvm::Function>(func.mLLVMValue);
  3854. llvmFunc->eraseFromParent();
  3855. }
  3856. NEW_CMD_INSERTED;*/
  3857. }
  3858. void BfIRBuilder::Func_SetLinkage(BfIRFunction func, BfIRLinkageType linkage)
  3859. {
  3860. WriteCmd(BfIRCmd_Func_SetLinkage, func, (uint8)linkage);
  3861. NEW_CMD_INSERTED;
  3862. }
  3863. void BfIRBuilder::SaveDebugLocation()
  3864. {
  3865. if (!mIgnoreWrites)
  3866. {
  3867. mSavedDebugLocs.push_back(mModule->mCurFilePosition);
  3868. WriteCmd(BfIRCmd_SaveDebugLocation);
  3869. NEW_CMD_INSERTED;
  3870. }
  3871. }
  3872. void BfIRBuilder::RestoreDebugLocation()
  3873. {
  3874. if (!mIgnoreWrites)
  3875. {
  3876. mModule->mCurFilePosition = mSavedDebugLocs.back();
  3877. mSavedDebugLocs.pop_back();
  3878. WriteCmd(BfIRCmd_RestoreDebugLocation);
  3879. mHasDebugLoc = true;
  3880. NEW_CMD_INSERTED;
  3881. }
  3882. }
  3883. bool BfIRBuilder::HasDebugLocation()
  3884. {
  3885. return mHasDebugLoc;
  3886. }
  3887. void BfIRBuilder::ClearDebugLocation()
  3888. {
  3889. WriteCmd(BfIRCmd_ClearDebugLocation);
  3890. mHasDebugLoc = false;
  3891. NEW_CMD_INSERTED;
  3892. }
  3893. void BfIRBuilder::ClearDebugLocation(BfIRValue inst)
  3894. {
  3895. WriteCmd(BfIRCmd_ClearDebugLocationInst, inst);
  3896. NEW_CMD_INSERTED;
  3897. }
  3898. void BfIRBuilder::ClearDebugLocation_Last()
  3899. {
  3900. WriteCmd(BfIRCmd_ClearDebugLocationInstLast);
  3901. NEW_CMD_INSERTED;
  3902. }
  3903. void BfIRBuilder::UpdateDebugLocation(BfIRValue inst)
  3904. {
  3905. WriteCmd(BfIRCmd_UpdateDebugLocation, inst);
  3906. NEW_CMD_INSERTED;
  3907. }
  3908. void BfIRBuilder::SetCurrentDebugLocation(int line, int column, BfIRMDNode diScope, BfIRMDNode diInlinedAt)
  3909. {
  3910. if (mDbgVerifyCodeGen && gDebugDbgLoc)
  3911. {
  3912. OutputDebugStrF("SetCurrentDebugLocation %d %d:%d\n", diScope.mId, line, column);
  3913. }
  3914. WriteCmd(BfIRCmd_SetCurrentDebugLocation, line, column, diScope, diInlinedAt);
  3915. mHasDebugLoc = true;
  3916. NEW_CMD_INSERTED;
  3917. }
  3918. void BfIRBuilder::CreateNop()
  3919. {
  3920. WriteCmd(BfIRCmd_Nop);
  3921. NEW_CMD_INSERTED;
  3922. }
  3923. void BfIRBuilder::CreateEnsureInstructionAt()
  3924. {
  3925. WriteCmd(BfIRCmd_EnsureInstructionAt);
  3926. NEW_CMD_INSERTED;
  3927. }
  3928. void BfIRBuilder::CreateStatementStart()
  3929. {
  3930. WriteCmd(BfIRCmd_StatementStart);
  3931. NEW_CMD_INSERTED;
  3932. }
  3933. void BfIRBuilder::CreateObjectAccessCheck(BfIRValue value, bool useAsm)
  3934. {
  3935. auto retBlock = WriteCmd(BfIRCmd_ObjectAccessCheck, value, useAsm);
  3936. NEW_CMD_INSERTED_IRBLOCK;
  3937. if (!mIgnoreWrites)
  3938. {
  3939. BF_ASSERT(!retBlock.IsFake());
  3940. mActualInsertBlock = retBlock;
  3941. }
  3942. mInsertBlock = retBlock;
  3943. }
  3944. void BfIRBuilder::DbgInit()
  3945. {
  3946. mHasDebugInfo = true;
  3947. WriteCmd(BfIRCmd_DbgInit);
  3948. NEW_CMD_INSERTED;
  3949. }
  3950. void BfIRBuilder::DbgFinalize()
  3951. {
  3952. while ((!mDeferredDbgTypeDefs.IsEmpty()) || (!mDITemporaryTypes.IsEmpty()))
  3953. {
  3954. //for (auto deferredType : mDeferredDbgTypeDefs)
  3955. for (int i = 0; i < (int)mDeferredDbgTypeDefs.size(); i++)
  3956. CreateDbgTypeDefinition(mDeferredDbgTypeDefs[i]);
  3957. mDeferredDbgTypeDefs.Clear();
  3958. ReplaceDITemporaryTypes();
  3959. }
  3960. WriteCmd(BfIRCmd_DbgFinalize);
  3961. NEW_CMD_INSERTED;
  3962. }
  3963. bool BfIRBuilder::DbgHasInfo()
  3964. {
  3965. return mHasDebugInfo;
  3966. }
  3967. String BfIRBuilder::DbgGetStaticFieldName(BfFieldInstance* fieldInstance)
  3968. {
  3969. String fieldName;
  3970. auto fieldDef = fieldInstance->GetFieldDef();
  3971. auto typeInstance = fieldInstance->mOwner;
  3972. auto typeDef = typeInstance->mTypeDef;
  3973. if (mModule->mCompiler->mOptions.IsCodeView())
  3974. {
  3975. fieldName += "_bf";
  3976. for (int partIdx = 0; partIdx < typeInstance->mTypeDef->mNamespace.GetPartsCount(); partIdx++)
  3977. {
  3978. auto atom = typeInstance->mTypeDef->mNamespace.mParts[partIdx];
  3979. if (!fieldName.empty())
  3980. fieldName += "::";
  3981. fieldName += atom->ToString();
  3982. }
  3983. if (!fieldName.empty())
  3984. fieldName += "::";
  3985. fieldName += GetDebugTypeName(typeInstance, true);
  3986. fieldName += "::";
  3987. fieldName += fieldDef->mName;
  3988. }
  3989. else
  3990. {
  3991. fieldName += fieldDef->mName;
  3992. }
  3993. return fieldName;
  3994. }
  3995. void BfIRBuilder::DbgAddPrefix(String & name)
  3996. {
  3997. if (mModule->mCompiler->mOptions.IsCodeView())
  3998. name.Insert(0, "bf__");
  3999. }
  4000. BfIRMDNode BfIRBuilder::DbgCreateCompileUnit(int lang, const StringImpl& fileName, const StringImpl& directory, const StringImpl& producer, bool isOptimized, const StringImpl& flags, int runtimeVer, bool linesOnly)
  4001. {
  4002. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateCompileUnit, lang, fileName, directory, producer, isOptimized, flags, runtimeVer, linesOnly);
  4003. NEW_CMD_INSERTED_IRMD;
  4004. return retVal;
  4005. }
  4006. BfIRMDNode BfIRBuilder::DbgCreateFile(const StringImpl& fileName, const StringImpl& directory)
  4007. {
  4008. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateFile, fileName, directory);
  4009. NEW_CMD_INSERTED_IRMD;
  4010. if (mDbgVerifyCodeGen && gDebugDbgLoc)
  4011. {
  4012. OutputDebugStrF("DbgCreateFile %s %d\n", fileName.c_str(), retVal.mId);
  4013. }
  4014. return retVal;
  4015. }
  4016. BfIRMDNode BfIRBuilder::DbgGetCurrentLocation()
  4017. {
  4018. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgGetCurrentLocation);
  4019. NEW_CMD_INSERTED_IRMD;
  4020. return retVal;
  4021. }
  4022. void BfIRBuilder::DbgSetType(BfType* type, BfIRMDNode diType)
  4023. {
  4024. WriteCmd(BfIRCmd_DbgSetType, type->mTypeId, diType);
  4025. NEW_CMD_INSERTED;
  4026. }
  4027. void BfIRBuilder::DbgSetInstType(BfType* type, BfIRMDNode diType)
  4028. {
  4029. WriteCmd(BfIRCmd_DbgSetInstType, type->mTypeId, diType);
  4030. NEW_CMD_INSERTED;
  4031. }
  4032. BfIRMDNode BfIRBuilder::DbgCreateConstValue(int64 val)
  4033. {
  4034. auto retVal = WriteCmd(BfIRCmd_ConstValueI64, val);
  4035. NEW_CMD_INSERTED;
  4036. return retVal;
  4037. }
  4038. BfIRMDNode BfIRBuilder::DbgGetType(BfType* type, BfIRPopulateType populateType)
  4039. {
  4040. if (mIgnoreWrites)
  4041. return BfIRMDNode();
  4042. PopulateType(type, populateType);
  4043. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgGetType, type->mTypeId);
  4044. NEW_CMD_INSERTED_IRMD;
  4045. return retVal;
  4046. }
  4047. BfIRMDNode BfIRBuilder::DbgGetTypeInst(BfTypeInstance* typeInst, BfIRPopulateType populateType)
  4048. {
  4049. if (mIgnoreWrites)
  4050. return BfIRMDNode();
  4051. PopulateType(typeInst, populateType);
  4052. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgGetTypeInst, typeInst->mTypeId);
  4053. NEW_CMD_INSERTED_IRMD;
  4054. return retVal;
  4055. }
  4056. void BfIRBuilder::DbgTrackDITypes(BfType* type)
  4057. {
  4058. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgTrackDITypes, type->mTypeId);
  4059. NEW_CMD_INSERTED;
  4060. }
  4061. BfIRMDNode BfIRBuilder::DbgCreateNameSpace(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNum)
  4062. {
  4063. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateNamespace, scope, name, file, lineNum);
  4064. NEW_CMD_INSERTED_IRMD;
  4065. return retVal;
  4066. }
  4067. BfIRMDNode BfIRBuilder::DbgCreateImportedModule(BfIRMDNode context, BfIRMDNode namespaceNode, int line)
  4068. {
  4069. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateImportedModule, context, namespaceNode, line);
  4070. NEW_CMD_INSERTED_IRMD;
  4071. return retVal;
  4072. }
  4073. BfIRMDNode BfIRBuilder::DbgCreateBasicType(const StringImpl& name, int64 sizeInBits, int64 alignInBits, int encoding)
  4074. {
  4075. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateBasicType, name, (int32)sizeInBits, (int32)alignInBits, encoding);
  4076. NEW_CMD_INSERTED_IRMD;
  4077. return retVal;
  4078. }
  4079. BfIRMDNode BfIRBuilder::DbgCreateStructType(BfIRMDNode context, const StringImpl& name, BfIRMDNode file, int lineNum, int64 sizeInBits, int64 alignInBits, int flags, BfIRMDNode derivedFrom, const BfSizedArray<BfIRMDNode>& elements)
  4080. {
  4081. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateStructType, context, name, file, lineNum, (int32)sizeInBits, (int32)alignInBits, flags, derivedFrom, elements);
  4082. NEW_CMD_INSERTED_IRMD;
  4083. return retVal;
  4084. }
  4085. BfIRMDNode BfIRBuilder::DbgCreateEnumerationType(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNumber, int64 sizeInBits, int64 alignInBits, const BfSizedArray<BfIRMDNode>& elements, BfIRMDNode underlyingType)
  4086. {
  4087. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateEnumerationType, scope, name, file, lineNumber, (int32)sizeInBits, (int32)alignInBits, elements, underlyingType);
  4088. NEW_CMD_INSERTED_IRMD;
  4089. return retVal;
  4090. }
  4091. BfIRMDNode BfIRBuilder::DbgCreatePointerType(BfIRMDNode diType)
  4092. {
  4093. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreatePointerType, diType);
  4094. NEW_CMD_INSERTED_IRMD;
  4095. return retVal;
  4096. }
  4097. BfIRMDNode BfIRBuilder::DbgCreateReferenceType(BfIRMDNode diType)
  4098. {
  4099. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateReferenceType, diType);
  4100. NEW_CMD_INSERTED_IRMD;
  4101. return retVal;
  4102. }
  4103. BfIRMDNode BfIRBuilder::DbgCreateConstType(BfIRMDNode diType)
  4104. {
  4105. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateConstType, diType);
  4106. NEW_CMD_INSERTED_IRMD;
  4107. return retVal;
  4108. }
  4109. BfIRMDNode BfIRBuilder::DbgCreateArtificialType(BfIRMDNode diType)
  4110. {
  4111. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateArtificialType, diType);
  4112. NEW_CMD_INSERTED_IRMD;
  4113. return retVal;
  4114. }
  4115. BfIRMDNode BfIRBuilder::DbgCreateArrayType(int64 sizeInBits, int64 alignInBits, BfIRMDNode elementType, int64 numElements)
  4116. {
  4117. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateArrayType, sizeInBits, alignInBits, elementType, numElements);
  4118. NEW_CMD_INSERTED_IRMD;
  4119. return retVal;
  4120. }
  4121. BfIRMDNode BfIRBuilder::DbgCreateReplaceableCompositeType(int tag, const StringImpl& name, BfIRMDNode scope, BfIRMDNode file, int line, int64 sizeInBits, int64 alignInBits, int flags)
  4122. {
  4123. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateReplaceableCompositeType, tag, name, scope, file, line, (int32)sizeInBits, (int32)alignInBits, flags);
  4124. NEW_CMD_INSERTED_IRMD;
  4125. return retVal;
  4126. }
  4127. void BfIRBuilder::DbgSetTypeSize(BfIRMDNode diType, int64 sizeInBits, int alignInBits)
  4128. {
  4129. BF_ASSERT(diType);
  4130. BfIRMDNode retVal = WriteCmd(BeIRCmd_DbgSetTypeSize, diType, sizeInBits, alignInBits);
  4131. NEW_CMD_INSERTED_IRMD;
  4132. }
  4133. BfIRMDNode BfIRBuilder::DbgCreateForwardDecl(int tag, const StringImpl& name, BfIRMDNode scope, BfIRMDNode file, int line)
  4134. {
  4135. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateForwardDecl, tag, name, scope, file, line);
  4136. NEW_CMD_INSERTED_IRMD;
  4137. return retVal;
  4138. }
  4139. BfIRMDNode BfIRBuilder::DbgCreateSizedForwardDecl(int tag, const StringImpl& name, BfIRMDNode scope, BfIRMDNode file, int line, int64 sizeInBits, int64 alignInBits)
  4140. {
  4141. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateSizedForwardDecl, tag, name, scope, file, line, (int32)sizeInBits, (int32)alignInBits);
  4142. NEW_CMD_INSERTED_IRMD;
  4143. return retVal;
  4144. }
  4145. BfIRMDNode BfIRBuilder::DbgReplaceAllUses(BfIRMDNode diPrevNode, BfIRMDNode diNewNode)
  4146. {
  4147. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgReplaceAllUses, diPrevNode, diNewNode);
  4148. NEW_CMD_INSERTED;
  4149. return retVal;
  4150. }
  4151. void BfIRBuilder::DbgDeleteTemporary(BfIRMDNode diNode)
  4152. {
  4153. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgDeleteTemporary, diNode);
  4154. NEW_CMD_INSERTED;
  4155. }
  4156. BfIRMDNode BfIRBuilder::DbgMakePermanent(BfIRMDNode diNode, BfIRMDNode diBaseType, const BfSizedArray<BfIRMDNode>& elements)
  4157. {
  4158. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgMakePermanent, diNode, diBaseType, elements);
  4159. NEW_CMD_INSERTED;
  4160. return retVal;
  4161. }
  4162. BfIRMDNode BfIRBuilder::DbgCreateEnumerator(const StringImpl& name, int64 val)
  4163. {
  4164. BfIRMDNode retVal = WriteCmd(BfIRCmd_CreateEnumerator, name, val);
  4165. NEW_CMD_INSERTED;
  4166. return retVal;
  4167. }
  4168. BfIRMDNode BfIRBuilder::DbgCreateMemberType(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNumber, int64 sizeInBits, int64 alignInBits, int64 offsetInBits, int flags, BfIRMDNode type)
  4169. {
  4170. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateMemberType, scope, name, file, lineNumber, (int32)sizeInBits, (int32)alignInBits, (int32)offsetInBits, flags, type);
  4171. NEW_CMD_INSERTED_IRMD;
  4172. return retVal;
  4173. }
  4174. BfIRMDNode BfIRBuilder::DbgCreateStaticMemberType(BfIRMDNode scope, const StringImpl&name, BfIRMDNode file, int lineNumber, BfIRMDNode type, int flags, BfIRValue val)
  4175. {
  4176. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgStaticCreateMemberType, scope, name, file, lineNumber, type, flags, val);
  4177. NEW_CMD_INSERTED_IRMD;
  4178. return retVal;
  4179. }
  4180. BfIRMDNode BfIRBuilder::DbgCreateInheritance(BfIRMDNode type, BfIRMDNode baseType, int64 baseOffset, int flags)
  4181. {
  4182. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateInheritance, type, baseType, (int32)baseOffset, flags);
  4183. NEW_CMD_INSERTED_IRMD;
  4184. return retVal;
  4185. }
  4186. BfIRMDNode BfIRBuilder::DbgCreateMethod(BfIRMDNode context, const StringImpl& name, const StringImpl& linkageName, BfIRMDNode file, int lineNum, BfIRMDNode type, bool isLocalToUnit, bool isDefinition, int vk, int vIndex, BfIRMDNode vTableHolder, int flags,
  4187. bool isOptimized, BfIRValue fn, const BfSizedArray<BfIRMDNode>& genericArgs, const BfSizedArray<BfIRValue>& genericConstValueArgs)
  4188. {
  4189. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateMethod, context, name, linkageName, file, lineNum, type, isLocalToUnit, isDefinition, vk, vIndex, vTableHolder, flags, isOptimized, fn, genericArgs, genericConstValueArgs);
  4190. NEW_CMD_INSERTED_IRMD;
  4191. // if (mDbgVerifyCodeGen && gDebugDbgLoc)
  4192. // {
  4193. // OutputDebugStrF("DbgCreateFunction Context:%d name:%s = %d\n", context.mId, name.c_str(), retVal.mId);
  4194. // }
  4195. return retVal;
  4196. }
  4197. BfIRMDNode BfIRBuilder::DbgCreateFunction(BfIRMDNode context, const StringImpl& name, const StringImpl& linkageName, BfIRMDNode file, int lineNum, BfIRMDNode type, bool isLocalToUnit, bool isDefinition, int scopeLine, int flags, bool isOptimized, BfIRValue fn)
  4198. {
  4199. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateFunction, context, name, linkageName, file, lineNum, type, isLocalToUnit, isDefinition, scopeLine, flags, isOptimized, fn);
  4200. NEW_CMD_INSERTED_IRMD;
  4201. // if (mDbgVerifyCodeGen && gDebugDbgLoc)
  4202. // {
  4203. // OutputDebugStrF("DbgCreateFunction Context:%d name:%s = %d\n", context.mId, name.c_str(), retVal.mId);
  4204. // }
  4205. return retVal;
  4206. }
  4207. BfIRMDNode BfIRBuilder::DbgCreateParameterVariable(BfIRMDNode scope, const StringImpl& name, int argNo, BfIRMDNode file, int lineNum, BfIRMDNode type, bool alwaysPreserve, int flags)
  4208. {
  4209. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateParameterVariable, scope, name, argNo, file, lineNum, type, alwaysPreserve, flags);
  4210. NEW_CMD_INSERTED_IRMD;
  4211. return retVal;
  4212. }
  4213. BfIRMDNode BfIRBuilder::DbgCreateSubroutineType(const BfSizedArray<BfIRMDNode>& elements)
  4214. {
  4215. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateSubroutineType, elements);
  4216. NEW_CMD_INSERTED_IRMD;
  4217. return retVal;
  4218. }
  4219. BfIRMDNode BfIRBuilder::DbgCreateAutoVariable(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNo, BfIRMDNode type, BfIRInitType initType)
  4220. {
  4221. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateAutoVariable, scope, name, file, lineNo, type, (int)initType);
  4222. NEW_CMD_INSERTED_IRMD;
  4223. return retVal;
  4224. }
  4225. BfIRValue BfIRBuilder::DbgInsertValueIntrinsic(BfIRValue val, BfIRMDNode varInfo)
  4226. {
  4227. BfIRValue retVal = WriteCmd(BfIRCmd_DbgInsertValueIntrinsic, val, varInfo);
  4228. NEW_CMD_INSERTED;
  4229. return retVal;
  4230. }
  4231. BfIRValue BfIRBuilder::DbgInsertDeclare(BfIRValue val, BfIRMDNode varInfo, BfIRValue declareBefore)
  4232. {
  4233. BfIRValue retVal = WriteCmd(BfIRCmd_DbgInsertDeclare, val, varInfo, declareBefore);
  4234. NEW_CMD_INSERTED_IRVALUE;
  4235. return retVal;
  4236. }
  4237. BfIRValue BfIRBuilder::DbgLifetimeEnd(BfIRMDNode varInfo)
  4238. {
  4239. BfIRValue retVal = WriteCmd(BfIRCmd_DbgLifetimeEnd, varInfo);
  4240. NEW_CMD_INSERTED_IRVALUE;
  4241. return retVal;
  4242. }
  4243. void BfIRBuilder::DbgCreateGlobalVariable(BfIRMDNode context, const StringImpl& name, const StringImpl& linkageName, BfIRMDNode file, int lineNumber, BfIRMDNode type, bool isLocalToUnit, BfIRValue val, BfIRMDNode decl)
  4244. {
  4245. WriteCmd(BfIRCmd_DbgCreateGlobalVariable, context, name, linkageName, file, lineNumber, type, isLocalToUnit, val, decl);
  4246. NEW_CMD_INSERTED;
  4247. }
  4248. BfIRMDNode BfIRBuilder::DbgCreateLexicalBlock(BfIRMDNode scope, BfIRMDNode file, int line, int col)
  4249. {
  4250. BfIRMDNode retVal = WriteCmd(BfIRCmd_DbgCreateLexicalBlock, scope, file, line, col);
  4251. NEW_CMD_INSERTED;
  4252. if (mDbgVerifyCodeGen && gDebugDbgLoc)
  4253. {
  4254. OutputDebugStrF("DbgCreateLexicalBlock Scope:%d File:%d = %d\n", scope.mId, file.mId, retVal.mId);
  4255. }
  4256. return retVal;
  4257. }
  4258. void BfIRBuilder::DbgCreateAnnotation(BfIRMDNode scope, const StringImpl& name, BfIRValue value)
  4259. {
  4260. WriteCmd(BfIRCmd_DbgCreateAnnotation, scope, name, value);
  4261. NEW_CMD_INSERTED;
  4262. }