BfIRBuilder.cpp 129 KB

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