BfIRBuilder.cpp 132 KB

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