BfIRBuilder.cpp 131 KB

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