BfIRBuilder.cpp 161 KB

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