CeMachine.cpp 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646
  1. #include "CeMachine.h"
  2. #include "BfModule.h"
  3. #include "BfCompiler.h"
  4. #include "BfIRBuilder.h"
  5. #include "../Backend/BeIRCodeGen.h"
  6. extern "C"
  7. {
  8. #include "BeefySysLib/third_party/utf8proc/utf8proc.h"
  9. }
  10. #define CE_ENABLE_HEAP
  11. USING_NS_BF;
  12. enum CeOpInfoFlag
  13. {
  14. CeOpInfoFlag_None,
  15. CeOpInfoFlag_SizeX,
  16. };
  17. struct CeOpInfo
  18. {
  19. const char* mName;
  20. CeOperandInfoKind mResultKind;
  21. CeOperandInfoKind mOperandA;
  22. CeOperandInfoKind mOperandB;
  23. CeOperandInfoKind mOperandC;
  24. CeOpInfoFlag mFlags;
  25. };
  26. #define CEOPINFO_SIZED_1(OPNAME, OPINFOA) \
  27. {OPNAME "_8", OPINFOA}, \
  28. {OPNAME "_16", OPINFOA}, \
  29. {OPNAME "_32", OPINFOA}, \
  30. {OPNAME "_64", OPINFOA}, \
  31. {OPNAME "_X", OPINFOA, CEOI_None, CEOI_None, CEOI_None, CeOpInfoFlag_SizeX}
  32. #define CEOPINFO_SIZED_2(OPNAME, OPINFOA, OPINFOB) \
  33. {OPNAME "_8", OPINFOA, OPINFOB}, \
  34. {OPNAME "_16", OPINFOA, OPINFOB}, \
  35. {OPNAME "_32", OPINFOA, OPINFOB}, \
  36. {OPNAME "_64", OPINFOA, OPINFOB}, \
  37. {OPNAME "_X", OPINFOA, OPINFOB, CEOI_None, CEOI_None, CeOpInfoFlag_SizeX}
  38. #define CEOPINFO_SIZED_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
  39. {OPNAME "_8", OPINFOA, OPINFOB, OPINFOC}, \
  40. {OPNAME "_16", OPINFOA, OPINFOB, OPINFOC}, \
  41. {OPNAME "_32", OPINFOA, OPINFOB, OPINFOC}, \
  42. {OPNAME "_64", OPINFOA, OPINFOB, OPINFOC}, \
  43. {OPNAME "_X", OPINFOA, OPINFOB, OPINFOC, CEOI_None, CeOpInfoFlag_SizeX}
  44. #define CEOPINFO_SIZED_NUMERIC_2(OPNAME, OPINFOA, OPINFOB) \
  45. {OPNAME "_I8", OPINFOA, OPINFOB}, \
  46. {OPNAME "_I16", OPINFOA, OPINFOB}, \
  47. {OPNAME "_I32", OPINFOA, OPINFOB}, \
  48. {OPNAME "_I64", OPINFOA, OPINFOB}
  49. #define CEOPINFO_SIZED_NUMERIC_PLUSF_2(OPNAME, OPINFOA, OPINFOB) \
  50. {OPNAME "_I8", OPINFOA, OPINFOB}, \
  51. {OPNAME "_I16", OPINFOA, OPINFOB}, \
  52. {OPNAME "_I32", OPINFOA, OPINFOB}, \
  53. {OPNAME "_I64", OPINFOA, OPINFOB}, \
  54. {OPNAME "_F32", OPINFOA, OPINFOB}, \
  55. {OPNAME "_F64", OPINFOA, OPINFOB}
  56. #define CEOPINFO_SIZED_NUMERIC_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
  57. {OPNAME "_I8", OPINFOA, OPINFOB, OPINFOC}, \
  58. {OPNAME "_I16", OPINFOA, OPINFOB, OPINFOC}, \
  59. {OPNAME "_I32", OPINFOA, OPINFOB, OPINFOC}, \
  60. {OPNAME "_I64", OPINFOA, OPINFOB, OPINFOC}
  61. #define CEOPINFO_SIZED_UNUMERIC_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
  62. {OPNAME "_U8", OPINFOA, OPINFOB, OPINFOC}, \
  63. {OPNAME "_U16", OPINFOA, OPINFOB, OPINFOC}, \
  64. {OPNAME "_U32", OPINFOA, OPINFOB, OPINFOC}, \
  65. {OPNAME "_U64", OPINFOA, OPINFOB, OPINFOC}
  66. #define CEOPINFO_SIZED_NUMERIC_PLUSF_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
  67. {OPNAME "_I8", OPINFOA, OPINFOB, OPINFOC}, \
  68. {OPNAME "_I16", OPINFOA, OPINFOB, OPINFOC}, \
  69. {OPNAME "_I32", OPINFOA, OPINFOB, OPINFOC}, \
  70. {OPNAME "_I64", OPINFOA, OPINFOB, OPINFOC}, \
  71. {OPNAME "_F32", OPINFOA, OPINFOB, OPINFOC}, \
  72. {OPNAME "_F64", OPINFOA, OPINFOB, OPINFOC}
  73. static CeOpInfo gOpInfo[] =
  74. {
  75. {"InvalidOp"},
  76. {"Ret"},
  77. {"Jmp", CEOI_None, CEOI_JMPREL},
  78. {"JmpIf", CEOI_None, CEOI_JMPREL, CEOI_FrameRef},
  79. {"JmpIfNot", CEOI_None, CEOI_JMPREL, CEOI_FrameRef},
  80. {"Error", CEOI_None, CEOI_IMM32},
  81. {"DynamicCastCheck", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
  82. {"GetReflectType", CEOI_FrameRef, CEOI_IMM32},
  83. {"GetString", CEOI_FrameRef, CEOI_IMM32},
  84. {"Malloc", CEOI_FrameRef, CEOI_FrameRef},
  85. {"Free", CEOI_None, CEOI_FrameRef},
  86. {"MemSet", CEOI_None, CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef},
  87. {"MemSet_Const", CEOI_None, CEOI_FrameRef, CEOI_IMM8, CEOI_IMM32},
  88. {"MemCpy", CEOI_None, CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef},
  89. {"FrameAddr_32", CEOI_FrameRef, CEOI_FrameRef},
  90. {"FrameAddr_64", CEOI_FrameRef, CEOI_FrameRef},
  91. {"FrameAddrOfs_32", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
  92. {"ConstData", CEOI_FrameRef, CEOI_IMM32},
  93. {"ConstDataRef", CEOI_FrameRef, CEOI_IMM32},
  94. {"Zero", CEOI_None, CEOI_FrameRef, CEOI_IMM32},
  95. {"Const_8", CEOI_FrameRef, CEOI_IMM8},
  96. {"Const_16", CEOI_FrameRef, CEOI_IMM16},
  97. {"Const_32", CEOI_FrameRef, CEOI_IMM32},
  98. {"Const_64", CEOI_FrameRef, CEOI_IMM64},
  99. {"Const_X", CEOI_FrameRef, CEOI_IMM_VAR},
  100. CEOPINFO_SIZED_2("Load", CEOI_FrameRef, CEOI_FrameRef),
  101. CEOPINFO_SIZED_3("Store", CEOI_None, CEOI_FrameRef, CEOI_FrameRef),
  102. CEOPINFO_SIZED_3("Move", CEOI_None, CEOI_FrameRef, CEOI_FrameRef),
  103. CEOPINFO_SIZED_2("Push", CEOI_None, CEOI_FrameRef),
  104. CEOPINFO_SIZED_1("Pop", CEOI_FrameRef),
  105. {"AdjustSP", CEOI_None, CEOI_FrameRef},
  106. {"AdjustSPNeg", CEOI_None, CEOI_FrameRef},
  107. {"AdjustSPConst", CEOI_None, CEOI_IMM32},
  108. {"CeOp_GetSP", CEOI_FrameRef},
  109. {"CeOp_SetSP", CEOI_None, CEOI_FrameRef},
  110. {"GetStaticField", CEOI_FrameRef, CEOI_IMM32},
  111. {"GetMethod", CEOI_FrameRef, CEOI_IMM32},
  112. {"GetMethod_Inner", CEOI_FrameRef, CEOI_IMM32},
  113. {"GetMethod_Virt", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
  114. {"GetMethod_IFace", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32, CEOI_IMM32},
  115. {"Call", CEOI_None, CEOI_FrameRef},
  116. {"CeOp_Conv_I8_I16", CEOI_FrameRef, CEOI_FrameRef},
  117. {"CeOp_Conv_I8_I32", CEOI_FrameRef, CEOI_FrameRef},
  118. {"CeOp_Conv_I8_I64", CEOI_FrameRef, CEOI_FrameRef},
  119. {"CeOp_Conv_I8_F32", CEOI_FrameRef, CEOI_FrameRef},
  120. {"CeOp_Conv_I8_F64", CEOI_FrameRef, CEOI_FrameRef},
  121. {"CeOp_Conv_I16_I32", CEOI_FrameRef, CEOI_FrameRef},
  122. {"CeOp_Conv_I16_I64", CEOI_FrameRef, CEOI_FrameRef},
  123. {"CeOp_Conv_I16_F32", CEOI_FrameRef, CEOI_FrameRef},
  124. {"CeOp_Conv_I16_F64", CEOI_FrameRef, CEOI_FrameRef},
  125. {"CeOp_Conv_I32_I64", CEOI_FrameRef, CEOI_FrameRef},
  126. {"CeOp_Conv_I32_F32", CEOI_FrameRef, CEOI_FrameRef},
  127. {"CeOp_Conv_I32_F64", CEOI_FrameRef, CEOI_FrameRef},
  128. {"CeOp_Conv_I64_F32", CEOI_FrameRef, CEOI_FrameRef},
  129. {"CeOp_Conv_I64_F64", CEOI_FrameRef, CEOI_FrameRef},
  130. {"CeOp_Conv_U8_U16", CEOI_FrameRef, CEOI_FrameRef},
  131. {"CeOp_Conv_U8_U32", CEOI_FrameRef, CEOI_FrameRef},
  132. {"CeOp_Conv_U8_U64", CEOI_FrameRef, CEOI_FrameRef},
  133. {"CeOp_Conv_U8_F32", CEOI_FrameRef, CEOI_FrameRef},
  134. {"CeOp_Conv_U8_F64", CEOI_FrameRef, CEOI_FrameRef},
  135. {"CeOp_Conv_U16_U32", CEOI_FrameRef, CEOI_FrameRef},
  136. {"CeOp_Conv_U16_U64", CEOI_FrameRef, CEOI_FrameRef},
  137. {"CeOp_Conv_U16_F32", CEOI_FrameRef, CEOI_FrameRef},
  138. {"CeOp_Conv_U16_F64", CEOI_FrameRef, CEOI_FrameRef},
  139. {"CeOp_Conv_U32_U64", CEOI_FrameRef, CEOI_FrameRef},
  140. {"CeOp_Conv_U32_F32", CEOI_FrameRef, CEOI_FrameRef},
  141. {"CeOp_Conv_U32_F64", CEOI_FrameRef, CEOI_FrameRef},
  142. {"CeOp_Conv_U64_F32", CEOI_FrameRef, CEOI_FrameRef},
  143. {"CeOp_Conv_U64_F64", CEOI_FrameRef, CEOI_FrameRef},
  144. {"CeOp_Conv_F32_I8", CEOI_FrameRef, CEOI_FrameRef},
  145. {"CeOp_Conv_F32_I16", CEOI_FrameRef, CEOI_FrameRef},
  146. {"CeOp_Conv_F32_I32", CEOI_FrameRef, CEOI_FrameRef},
  147. {"CeOp_Conv_F32_I64", CEOI_FrameRef, CEOI_FrameRef},
  148. {"CeOp_Conv_F32_F64", CEOI_FrameRef, CEOI_FrameRef},
  149. {"CeOp_Conv_F64_I8", CEOI_FrameRef, CEOI_FrameRef},
  150. {"CeOp_Conv_F64_I16", CEOI_FrameRef, CEOI_FrameRef},
  151. {"CeOp_Conv_F64_I32", CEOI_FrameRef, CEOI_FrameRef},
  152. {"CeOp_Conv_F64_I64", CEOI_FrameRef, CEOI_FrameRef},
  153. {"CeOp_Conv_F64_F32", CEOI_FrameRef, CEOI_FrameRef},
  154. {"AddConst_I8", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM8},
  155. {"AddConst_I16", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM16},
  156. {"AddConst_I32", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
  157. {"AddConst_I64", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM64},
  158. {"AddConst_F32", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMMF32},
  159. {"AddConst_F64", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMMF64},
  160. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Add", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  161. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Sub", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  162. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Mul", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  163. CEOPINFO_SIZED_NUMERIC_PLUSF_3("SDiv", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  164. CEOPINFO_SIZED_NUMERIC_3("UDiv", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  165. CEOPINFO_SIZED_NUMERIC_PLUSF_3("SMod", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  166. CEOPINFO_SIZED_NUMERIC_3("UMod", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  167. CEOPINFO_SIZED_NUMERIC_3("And", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  168. CEOPINFO_SIZED_NUMERIC_3("Or", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  169. CEOPINFO_SIZED_NUMERIC_3("Xor", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  170. CEOPINFO_SIZED_NUMERIC_3("Shl", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  171. CEOPINFO_SIZED_NUMERIC_3("Shr", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  172. CEOPINFO_SIZED_UNUMERIC_3("Shr", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  173. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Cmp_EQ", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  174. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Cmp_NE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  175. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Cmp_SLT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  176. CEOPINFO_SIZED_NUMERIC_3("Cmp_ULT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  177. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Cmp_SLE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  178. CEOPINFO_SIZED_NUMERIC_3("Cmp_ULE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  179. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Cmp_SGT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  180. CEOPINFO_SIZED_NUMERIC_3("Cmp_UGT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  181. CEOPINFO_SIZED_NUMERIC_PLUSF_3("Cmp_SGE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  182. CEOPINFO_SIZED_NUMERIC_3("Cmp_UGE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
  183. CEOPINFO_SIZED_NUMERIC_PLUSF_2("Neg", CEOI_FrameRef, CEOI_FrameRef),
  184. {"Not_I1", CEOI_FrameRef, CEOI_FrameRef},
  185. CEOPINFO_SIZED_NUMERIC_2("Not", CEOI_FrameRef, CEOI_FrameRef),
  186. };
  187. static_assert(BF_ARRAY_COUNT(gOpInfo) == (int)CeOp_COUNT, "gOpName incorrect size");
  188. //////////////////////////////////////////////////////////////////////////
  189. CeFunction::~CeFunction()
  190. {
  191. for (auto innerFunc : mInnerFunctions)
  192. delete innerFunc;
  193. }
  194. //////////////////////////////////////////////////////////////////////////
  195. CeFunctionInfo::~CeFunctionInfo()
  196. {
  197. delete mCeFunction;
  198. }
  199. //////////////////////////////////////////////////////////////////////////
  200. #define CE_GET(T) *((T*)(mPtr += sizeof(T)) - 1)
  201. void CeDumpContext::DumpOperandInfo(CeOperandInfoKind operandInfoKind)
  202. {
  203. switch (operandInfoKind)
  204. {
  205. case CEOI_FrameRef:
  206. {
  207. int32 addr = CE_GET(int32);
  208. char str[64];
  209. if (addr >= 0)
  210. sprintf(str, "FR+0x%X", addr);
  211. else
  212. sprintf(str, "FR-0x%X", -addr);
  213. mStr += str;
  214. }
  215. break;
  216. case CEOI_IMM8:
  217. {
  218. int32 val = CE_GET(int8);
  219. char str[64];
  220. sprintf(str, "%d", val);
  221. mStr += str;
  222. }
  223. break;
  224. case CEOI_IMM16:
  225. {
  226. int32 val = CE_GET(int16);
  227. char str[64];
  228. sprintf(str, "%d", val);
  229. mStr += str;
  230. }
  231. break;
  232. case CEOI_IMM32:
  233. {
  234. int32 val = CE_GET(int32);
  235. char str[64];
  236. sprintf(str, "%d", val);
  237. mStr += str;
  238. }
  239. break;
  240. case CEOI_IMM64:
  241. {
  242. int64 val = CE_GET(int64);
  243. char str[64];
  244. sprintf(str, "%lld", val);
  245. mStr += str;
  246. }
  247. break;
  248. case CEOI_IMM_VAR:
  249. {
  250. mStr += '[';
  251. int32 size = CE_GET(int32);
  252. for (int i = 0; i < size; i++)
  253. {
  254. if (i != 0)
  255. mStr += ", ";
  256. uint8 val = CE_GET(uint8);
  257. char str[64];
  258. sprintf(str, "%X", val);
  259. mStr += str;
  260. }
  261. mStr += ']';
  262. }
  263. break;
  264. case CEOI_JMPREL:
  265. {
  266. int32 val = CE_GET(int32);
  267. char str[64];
  268. sprintf(str, "JMP:%04X", (int32)(val + (mPtr - mStart)));
  269. mStr += str;
  270. }
  271. break;
  272. }
  273. }
  274. void CeDumpContext::Dump()
  275. {
  276. if (!mCeFunction->mGenError.IsEmpty())
  277. mStr += StrFormat("Gen Error: %s\n", mCeFunction->mGenError.c_str());
  278. mStr += StrFormat("Frame Size: %d\n", mCeFunction->mFrameSize);
  279. uint8* start = mPtr;
  280. int curEmitIdx = 0;
  281. CeEmitEntry* curEmitEntry = NULL;
  282. while (mPtr < mEnd)
  283. {
  284. int ofs = mPtr - start;
  285. while ((curEmitIdx < mCeFunction->mEmitTable.mSize - 1) && (ofs >= mCeFunction->mEmitTable[curEmitIdx + 1].mCodePos))
  286. curEmitIdx++;
  287. if (curEmitIdx < mCeFunction->mEmitTable.mSize)
  288. curEmitEntry = &mCeFunction->mEmitTable[curEmitIdx];
  289. CeOp op = CE_GET(CeOp);
  290. CeOpInfo& opInfo = gOpInfo[op];
  291. mStr += StrFormat("%04X: ", ofs);
  292. int32 sizeX = -1;
  293. if ((opInfo.mFlags & CeOpInfoFlag_SizeX) != 0)
  294. {
  295. sizeX = CE_GET(int);
  296. }
  297. if (opInfo.mResultKind != CEOI_None)
  298. {
  299. DumpOperandInfo(opInfo.mResultKind);
  300. mStr += " = ";
  301. }
  302. mStr += opInfo.mName;
  303. if (sizeX != -1)
  304. {
  305. mStr += StrFormat(":%d", sizeX);
  306. }
  307. if (opInfo.mOperandA != CEOI_None)
  308. {
  309. mStr += " ";
  310. DumpOperandInfo(opInfo.mOperandA);
  311. }
  312. if (opInfo.mOperandB != CEOI_None)
  313. {
  314. mStr += ", ";
  315. DumpOperandInfo(opInfo.mOperandB);
  316. }
  317. if (opInfo.mOperandC != CEOI_None)
  318. {
  319. mStr += ", ";
  320. DumpOperandInfo(opInfo.mOperandC);
  321. }
  322. if ((curEmitEntry != NULL) && (curEmitEntry->mFile != -1))
  323. {
  324. mStr += StrFormat(" @%d[%s:%d]", curEmitIdx, GetFileName(mCeFunction->mFiles[curEmitEntry->mFile]).c_str(),
  325. curEmitEntry->mLine + 1, curEmitEntry->mColumn + 1);
  326. }
  327. mStr += "\n";
  328. }
  329. }
  330. //////////////////////////////////////////////////////////////////////////
  331. void CeBuilder::Fail(const StringImpl& str)
  332. {
  333. if (!mCeFunction->mGenError.IsEmpty())
  334. return;
  335. String errStr = StrFormat("Failure during const code generation of %s: %s", mBeFunction->mName.c_str(), str.c_str());
  336. if (mCurDbgLoc != NULL)
  337. {
  338. String filePath;
  339. mCurDbgLoc->GetDbgFile()->GetFilePath(filePath);
  340. errStr += StrFormat(" at line %d:%d in %s", mCurDbgLoc->mLine + 1, mCurDbgLoc->mColumn + 1, filePath.c_str());
  341. }
  342. mCeFunction->mGenError = errStr;
  343. }
  344. void CeBuilder::Emit(uint8 val)
  345. {
  346. mCeFunction->mCode.Add((uint8)val);
  347. }
  348. void CeBuilder::Emit(CeOp val)
  349. {
  350. *(CeOp*)mCeFunction->mCode.GrowUninitialized(sizeof(CeOp)) = val;
  351. }
  352. void CeBuilder::EmitSizedOp(CeOp val, int size)
  353. {
  354. CeSizeClass sizeClass = GetSizeClass(size);
  355. Emit((CeOp)(val + sizeClass));
  356. if (sizeClass == CeSizeClass_X)
  357. Emit((int32)size);
  358. }
  359. void CeBuilder::Emit(int32 val)
  360. {
  361. *(int32*)mCeFunction->mCode.GrowUninitialized(4) = val;
  362. }
  363. void CeBuilder::Emit(int64 val)
  364. {
  365. *(int64*)mCeFunction->mCode.GrowUninitialized(8) = val;
  366. }
  367. void CeBuilder::Emit(bool val)
  368. {
  369. BF_FATAL("Invalid emit");
  370. }
  371. void CeBuilder::Emit(void* ptr, int size)
  372. {
  373. memcpy(mCeFunction->mCode.GrowUninitialized(size), ptr, size);
  374. }
  375. void CeBuilder::EmitZeroes(int size)
  376. {
  377. for (int i = 0; i < size; i++)
  378. Emit((uint8)0);
  379. }
  380. void CeBuilder::EmitJump(CeOp op, const CeOperand& block)
  381. {
  382. BF_ASSERT(block.mKind == CeOperandKind_Block);
  383. Emit(op);
  384. CeJumpEntry jumpEntry;
  385. jumpEntry.mBlockIdx = block.mBlockIdx;
  386. jumpEntry.mEmitPos = GetCodePos();
  387. mJumpTable.Add(jumpEntry);
  388. Emit((int32)0);
  389. }
  390. void CeBuilder::EmitBinarySwitchSection(BeSwitchInst* switchInst, int startIdx, int endIdx)
  391. {
  392. // This is an empirically determined binary switching limit
  393. if (endIdx - startIdx >= 18)
  394. {
  395. // int gteLabel = mCurLabelIdx++;
  396. //
  397. // auto mcDefaultBlock = GetOperand(switchInst->mDefaultBlock);
  398. //
  399. // int midIdx = startIdx + (endIdx - startIdx) / 2;
  400. // auto& switchCase = switchInst->mCases[midIdx];
  401. // auto switchBlock = GetOperand(switchCase.mBlock);
  402. // auto mcValue = GetOperand(switchInst->mValue);
  403. // auto valueType = GetType(mcValue);
  404. //
  405. // AllocInst(BeMCInstKind_Cmp, mcValue, GetOperand(switchCase.mValue));
  406. // AllocInst(BeMCInstKind_CondBr, BeMCOperand::FromLabel(gteLabel), BeMCOperand::FromCmpKind(BeCmpKind_SGE));
  407. // switchBlock.mBlock->AddPred(mActiveBlock);
  408. //
  409. // CreateBinarySwitchSection(switchInst, startIdx, midIdx);
  410. // AllocInst(BeMCInstKind_Br, mcDefaultBlock);
  411. // CreateLabel(-1, gteLabel);
  412. // CreateBinarySwitchSection(switchInst, midIdx, endIdx);
  413. // return;
  414. }
  415. for (int caseIdx = startIdx; caseIdx < endIdx; caseIdx++)
  416. {
  417. auto& switchCase = switchInst->mCases[caseIdx];
  418. auto switchBlock = GetOperand(switchCase.mBlock);
  419. auto mcValue = GetOperand(switchInst->mValue);
  420. CeOperand result;
  421. EmitBinaryOp(CeOp_Cmp_EQ_I8, CeOp_Cmp_EQ_F32, mcValue, GetOperand(switchCase.mValue), result);
  422. EmitJump(CeOp_JmpIf, switchBlock);
  423. EmitFrameOffset(result);
  424. }
  425. }
  426. int CeBuilder::GetCodePos()
  427. {
  428. return (int)mCeFunction->mCode.size();
  429. }
  430. void CeBuilder::EmitFrameOffset(const CeOperand& val)
  431. {
  432. BF_ASSERT(val.mKind == CeOperandKind_FrameOfs);
  433. Emit((int32)val.mFrameOfs);
  434. }
  435. void CeBuilder::FlushPhi(CeBlock* ceBlock, int targetBlockIdx)
  436. {
  437. for (int i = 0; i < (int)ceBlock->mPhiOutgoing.size(); i++)
  438. {
  439. auto& phiOutgoing = ceBlock->mPhiOutgoing[i];
  440. if (phiOutgoing.mPhiBlockIdx == targetBlockIdx)
  441. {
  442. auto targetPhi = GetOperand(phiOutgoing.mPhiInst);
  443. auto mcVal = GetOperand(phiOutgoing.mPhiValue);
  444. EmitSizedOp(CeOp_Move_8, mcVal, NULL, true);
  445. EmitFrameOffset(targetPhi);
  446. ceBlock->mPhiOutgoing.RemoveAt(i);
  447. break;
  448. }
  449. }
  450. }
  451. void CeBuilder::EmitBinaryOp(CeOp iOp, CeOp fOp, const CeOperand& lhs, const CeOperand& rhs, CeOperand& result)
  452. {
  453. CeOp op = iOp;
  454. if (lhs.mType->IsIntable())
  455. {
  456. if (lhs.mType->mSize == 1)
  457. op = iOp;
  458. else if (lhs.mType->mSize == 2)
  459. op = (CeOp)(iOp + 1);
  460. else if (lhs.mType->mSize == 4)
  461. op = (CeOp)(iOp + 2);
  462. else if (lhs.mType->mSize == 8)
  463. op = (CeOp)(iOp + 3);
  464. else
  465. Fail("Invalid int operand size");
  466. }
  467. else if (lhs.mType->IsFloat())
  468. {
  469. if (lhs.mType->mSize == 4)
  470. op = fOp;
  471. else if (lhs.mType->mSize == 8)
  472. op = (CeOp)(fOp + 1);
  473. else
  474. Fail("Invalid float operand size");
  475. }
  476. else
  477. Fail("Invalid binary operand");
  478. Emit(op);
  479. if (!result)
  480. result = FrameAlloc(lhs.mType);
  481. EmitFrameOffset(result);
  482. EmitFrameOffset(lhs);
  483. EmitFrameOffset(rhs);
  484. }
  485. void CeBuilder::EmitUnaryOp(CeOp iOp, CeOp fOp, const CeOperand& val, CeOperand& result)
  486. {
  487. CeOp op = iOp;
  488. if (val.mType->IsIntable())
  489. {
  490. if (val.mType->mSize == 1)
  491. op = iOp;
  492. else if (val.mType->mSize == 2)
  493. op = (CeOp)(iOp + 1);
  494. else if (val.mType->mSize == 4)
  495. op = (CeOp)(iOp + 2);
  496. else if (val.mType->mSize == 8)
  497. op = (CeOp)(iOp + 3);
  498. else
  499. Fail("Invalid int operand size");
  500. }
  501. else if (val.mType->IsFloat())
  502. {
  503. if (val.mType->mSize == 4)
  504. op = fOp;
  505. else if (val.mType->mSize == 8)
  506. op = (CeOp)(fOp + 1);
  507. else
  508. Fail("Invalid float operand size");
  509. }
  510. else
  511. Fail("Invalid unary operand");
  512. Emit(op);
  513. result = FrameAlloc(val.mType);
  514. EmitFrameOffset(result);
  515. EmitFrameOffset(val);
  516. }
  517. void CeBuilder::EmitSizedOp(CeOp baseOp, const CeOperand& operand, CeOperand* outResult, bool allowNonStdSize)
  518. {
  519. bool isStdSize = true;
  520. CeOp op = CeOp_InvalidOp;
  521. if (operand.mType->mSize == 1)
  522. op = baseOp;
  523. else if (operand.mType->mSize == 2)
  524. op = (CeOp)(baseOp + 1);
  525. else if (operand.mType->mSize == 4)
  526. op = (CeOp)(baseOp + 2);
  527. else if (operand.mType->mSize == 8)
  528. op = (CeOp)(baseOp + 3);
  529. else
  530. {
  531. isStdSize = false;
  532. op = (CeOp)(baseOp + 4);
  533. }
  534. Emit(op);
  535. if (!isStdSize)
  536. {
  537. if (!allowNonStdSize)
  538. Fail("Invalid operand size");
  539. Emit((int32)operand.mType->mSize);
  540. }
  541. if (outResult != NULL)
  542. {
  543. *outResult = FrameAlloc(operand.mType);
  544. EmitFrameOffset(*outResult);
  545. }
  546. EmitFrameOffset(operand);
  547. }
  548. CeOperand CeBuilder::FrameAlloc(BeType* type)
  549. {
  550. mFrameSize += type->mSize;
  551. CeOperand result;
  552. result.mKind = CeOperandKind_FrameOfs;
  553. result.mFrameOfs = -mFrameSize;
  554. result.mType = type;
  555. return result;
  556. }
  557. CeOperand CeBuilder::EmitConst(int64 val, int size)
  558. {
  559. BeType* type = mIntPtrType;
  560. switch (size)
  561. {
  562. case 1:
  563. type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int8);
  564. break;
  565. case 2:
  566. type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int16);
  567. break;
  568. case 4:
  569. type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int32);
  570. break;
  571. case 8:
  572. type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int64);
  573. break;
  574. default:
  575. Fail("Bad const size");
  576. }
  577. auto result = FrameAlloc(type);
  578. EmitSizedOp(CeOp_Const_8, type->mSize);
  579. EmitFrameOffset(result);
  580. Emit(&val, size);
  581. return result;
  582. }
  583. CeOperand CeBuilder::GetOperand(BeValue* value, bool allowAlloca, bool allowImmediate)
  584. {
  585. if (value == NULL)
  586. return CeOperand();
  587. BeType* errorType = mIntPtrType;
  588. CeErrorKind errorKind = CeErrorKind_None;
  589. switch (value->GetTypeId())
  590. {
  591. case BeGlobalVariable::TypeId:
  592. {
  593. auto globalVar = (BeGlobalVariable*)value;
  594. if (globalVar->mName.StartsWith("__bfStrObj"))
  595. {
  596. int stringId = atoi(globalVar->mName.c_str() + 10);
  597. int* stringTableIdxPtr = NULL;
  598. if (mStringMap.TryAdd(stringId, NULL, &stringTableIdxPtr))
  599. {
  600. *stringTableIdxPtr = (int)mCeFunction->mStringTable.size();
  601. CeStringEntry ceStringEntry;
  602. ceStringEntry.mStringId = stringId;
  603. mCeFunction->mStringTable.Add(ceStringEntry);
  604. }
  605. auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
  606. Emit(CeOp_GetString);
  607. EmitFrameOffset(result);
  608. Emit((int32)*stringTableIdxPtr);
  609. return result;
  610. }
  611. else if (globalVar->mName.StartsWith("__bfStrData"))
  612. {
  613. int stringId = atoi(globalVar->mName.c_str() + 11);
  614. int* stringTableIdxPtr = NULL;
  615. if (mStringMap.TryAdd(stringId, NULL, &stringTableIdxPtr))
  616. {
  617. *stringTableIdxPtr = (int)mCeFunction->mStringTable.size();
  618. CeStringEntry ceStringEntry;
  619. ceStringEntry.mStringId = stringId;
  620. mCeFunction->mStringTable.Add(ceStringEntry);
  621. }
  622. auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
  623. Emit(CeOp_GetString);
  624. EmitFrameOffset(result);
  625. Emit((int32)*stringTableIdxPtr);
  626. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(
  627. mCeMachine->mCeModule->mCompiler->mStringTypeDef, BfPopulateType_Data);
  628. Emit(CeOp_AddConst_I32);
  629. EmitFrameOffset(result);
  630. EmitFrameOffset(result);
  631. Emit((int32)stringTypeInst->mInstSize);
  632. return result;
  633. }
  634. CeStaticFieldInfo* staticFieldInfoPtr = NULL;
  635. if (mCeMachine->mStaticFieldMap.TryGetValue(globalVar->mName, &staticFieldInfoPtr))
  636. {
  637. CeStaticFieldInfo* staticFieldInfo = staticFieldInfoPtr;
  638. int* staticFieldTableIdxPtr = NULL;
  639. if (mStaticFieldMap.TryAdd(globalVar, NULL, &staticFieldTableIdxPtr))
  640. {
  641. CeStaticFieldEntry staticFieldEntry;
  642. staticFieldEntry.mTypeId = staticFieldInfo->mFieldInstance->mOwner->mTypeId;
  643. staticFieldEntry.mName = globalVar->mName;
  644. staticFieldEntry.mSize = globalVar->mType->mSize;
  645. *staticFieldTableIdxPtr = (int)mCeFunction->mStaticFieldTable.size();
  646. mCeFunction->mStaticFieldTable.Add(staticFieldEntry);
  647. }
  648. auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
  649. Emit(CeOp_GetStaticField);
  650. EmitFrameOffset(result);
  651. Emit((int32)*staticFieldTableIdxPtr);
  652. return result;
  653. }
  654. if (globalVar->mInitializer != NULL)
  655. {
  656. auto result = GetOperand(globalVar->mInitializer, false, true);
  657. if (result.mKind == CeOperandKind_ConstStructTableIdx)
  658. {
  659. auto& constTableEntry = mCeFunction->mConstStructTable[result.mStructTableIdx];
  660. auto ptrType = mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType);
  661. auto dataResult = FrameAlloc(ptrType);
  662. Emit(CeOp_ConstDataRef);
  663. EmitFrameOffset(dataResult);
  664. Emit((int32)result.mCallTableIdx);
  665. return dataResult;
  666. }
  667. return result;
  668. }
  669. errorKind = CeErrorKind_GlobalVariable;
  670. errorType = mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType);
  671. }
  672. break;
  673. case BeCastConstant::TypeId:
  674. {
  675. auto constant = (BeCastConstant*)value;
  676. CeOperand mcOperand;
  677. auto result = GetOperand(constant->mTarget);
  678. result.mType = constant->mType;
  679. return result;
  680. }
  681. break;
  682. case BeConstant::TypeId:
  683. {
  684. uint64 u64Val = 0;
  685. float fVal = 0;
  686. void* dataPtr = NULL;
  687. int dataSize = 0;
  688. auto constant = (BeConstant*)value;
  689. CeOperand mcOperand;
  690. switch (constant->mType->mTypeCode)
  691. {
  692. case BeTypeCode_Int8:
  693. case BeTypeCode_Int16:
  694. case BeTypeCode_Int32:
  695. case BeTypeCode_Int64:
  696. if (allowImmediate)
  697. {
  698. CeOperand result;
  699. result.mKind = CeOperandKind_Immediate;
  700. result.mImmediate = constant->mInt32;
  701. result.mType = constant->mType;
  702. return result;
  703. }
  704. case BeTypeCode_Boolean:
  705. case BeTypeCode_Double:
  706. dataPtr = &constant->mUInt64;
  707. dataSize = constant->mType->mSize;
  708. break;
  709. case BeTypeCode_Float:
  710. fVal = (float)constant->mDouble;
  711. dataPtr = &fVal;
  712. dataSize = 4;
  713. break;
  714. case BeTypeCode_Pointer:
  715. {
  716. if (constant->mTarget == NULL)
  717. {
  718. dataPtr = &u64Val;
  719. dataSize = mPtrSize;
  720. }
  721. else
  722. {
  723. auto relTo = GetOperand(constant->mTarget);
  724. if (relTo)
  725. {
  726. auto result = relTo;
  727. result.mType = constant->mType;
  728. return result;
  729. }
  730. // if (relTo.mKind == CeOperandKind_Immediate_Null)
  731. // {
  732. // mcOperand.mKind = CeOperandKind_Immediate_Null;
  733. // mcOperand.mType = constant->mType;
  734. // return mcOperand;
  735. // }
  736. //
  737. // mcOperand = AllocVirtualReg(constant->mType);
  738. // auto vregInfo = GetVRegInfo(mcOperand);
  739. // vregInfo->mDefOnFirstUse = true;
  740. // vregInfo->mRelTo = relTo;
  741. // vregInfo->mIsExpr = true;
  742. //
  743. //return mcOperand;
  744. }
  745. }
  746. break;
  747. case BeTypeCode_Struct:
  748. case BeTypeCode_SizedArray:
  749. case BeTypeCode_Vector:
  750. {
  751. auto beType = constant->mType;
  752. auto result = FrameAlloc(beType);
  753. Emit(CeOp_Zero);
  754. EmitFrameOffset(result);
  755. Emit((int32)beType->mSize);
  756. return result;
  757. }
  758. //mcOperand.mImmediate = constant->mInt64;
  759. //mcOperand.mKind = CeOperandKind_Immediate_i64;
  760. break;
  761. // default:
  762. // Fail("Unhandled constant type");
  763. }
  764. if (dataSize != 0)
  765. {
  766. auto beType = constant->mType;
  767. auto result = FrameAlloc(beType);
  768. CeSizeClass sizeClass = GetSizeClass(dataSize);
  769. Emit((CeOp)(CeOp_Const_8 + sizeClass));
  770. EmitFrameOffset(result);
  771. if (sizeClass == CeSizeClass_X)
  772. Emit((int32)dataSize);
  773. if (dataPtr != 0)
  774. Emit(dataPtr, dataSize);
  775. else
  776. {
  777. for (int i = 0; i < dataSize; i++)
  778. Emit((uint8)0);
  779. }
  780. return result;
  781. }
  782. }
  783. break;
  784. case BeStructConstant::TypeId:
  785. {
  786. int* constDataPtr = NULL;
  787. auto structConstant = (BeStructConstant*)value;
  788. if (mConstDataMap.TryAdd(structConstant, NULL, &constDataPtr))
  789. {
  790. CeConstStructData constStructData;
  791. constStructData.mQueueFixups = true;
  792. errorKind = mCeMachine->WriteConstant(constStructData, structConstant);
  793. if (errorKind == CeErrorKind_None)
  794. {
  795. *constDataPtr = (int)mCeFunction->mConstStructTable.size();
  796. mCeFunction->mConstStructTable.Add(constStructData);
  797. }
  798. else
  799. {
  800. *constDataPtr = -1;
  801. }
  802. }
  803. if (*constDataPtr != -1)
  804. {
  805. if (!allowImmediate)
  806. {
  807. auto result = FrameAlloc(structConstant->mType);
  808. Emit(CeOp_ConstData);
  809. EmitFrameOffset(result);
  810. Emit((int32)*constDataPtr);
  811. return result;
  812. }
  813. else
  814. {
  815. CeOperand result;
  816. result.mKind = CeOperandKind_ConstStructTableIdx;
  817. result.mCallTableIdx = *constDataPtr;
  818. result.mType = structConstant->mType;
  819. return result;
  820. }
  821. }
  822. else
  823. {
  824. errorKind = CeErrorKind_GlobalVariable;
  825. }
  826. }
  827. break;
  828. case BeGEPConstant::TypeId:
  829. {
  830. auto gepConstant = (BeGEPConstant*)value;
  831. auto mcVal = GetOperand(gepConstant->mTarget);
  832. BePointerType* ptrType = (BePointerType*)mcVal.mType;
  833. BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
  834. auto result = mcVal;
  835. // We assume we never do both an idx0 and idx1 at once. Fix if we change that.
  836. int64 byteOffset = 0;
  837. BeType* elementType = NULL;
  838. byteOffset += gepConstant->mIdx0 * ptrType->mElementType->mSize;
  839. if (ptrType->mElementType->mTypeCode == BeTypeCode_Struct)
  840. {
  841. BeStructType* structType = (BeStructType*)ptrType->mElementType;
  842. auto& structMember = structType->mMembers[gepConstant->mIdx1];
  843. elementType = structMember.mType;
  844. byteOffset = structMember.mByteOffset;
  845. }
  846. else
  847. {
  848. BF_ASSERT(ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray);
  849. auto arrayType = (BeSizedArrayType*)ptrType->mElementType;
  850. elementType = arrayType->mElementType;
  851. byteOffset = gepConstant->mIdx1 * elementType->mSize;
  852. }
  853. auto elementPtrType = mCeMachine->GetBeContext()->GetPointerTo(elementType);
  854. result = FrameAlloc(elementPtrType);
  855. EmitSizedOp(CeOp_AddConst_I8, mPtrSize);
  856. EmitFrameOffset(result);
  857. EmitFrameOffset(mcVal);
  858. Emit(&byteOffset, mPtrSize);
  859. // result = AllocRelativeVirtualReg(elementPtrType, result, GetImmediate(byteOffset), 1);
  860. // // The def is primary to create a single 'master location' for the GEP vreg to become legalized before use
  861. // auto vregInfo = GetVRegInfo(result);
  862. // vregInfo->mDefOnFirstUse = true;
  863. // result.mKind = CeOperandKind_VReg;
  864. return result;
  865. }
  866. break;
  867. case BeExtractValueConstant::TypeId:
  868. {
  869. // Note: this only handles zero-aggregates
  870. auto extractConstant = (BeExtractValueConstant*)value;
  871. auto elementType = extractConstant->GetType();
  872. auto mcVal = GetOperand(extractConstant->mTarget);
  873. BeConstant beConstant;
  874. beConstant.mType = elementType;
  875. beConstant.mUInt64 = 0;
  876. return GetOperand(&beConstant);
  877. }
  878. break;
  879. case BeFunction::TypeId:
  880. {
  881. auto beFunction = (BeFunction*)value;
  882. int* callIdxPtr = NULL;
  883. if (mFunctionMap.TryAdd(beFunction, NULL, &callIdxPtr))
  884. {
  885. CeFunctionInfo* ceFunctionInfo = NULL;
  886. mCeMachine->mNamedFunctionMap.TryGetValue(beFunction->mName, &ceFunctionInfo);
  887. if (ceFunctionInfo != NULL)
  888. ceFunctionInfo->mRefCount++;
  889. else
  890. {
  891. auto checkBuilder = this;
  892. if (checkBuilder->mParentBuilder != NULL)
  893. checkBuilder = checkBuilder->mParentBuilder;
  894. int innerFunctionIdx = 0;
  895. if (checkBuilder->mInnerFunctionMap.TryGetValue(beFunction, &innerFunctionIdx))
  896. {
  897. auto innerFunction = checkBuilder->mCeFunction->mInnerFunctions[innerFunctionIdx];
  898. if (!innerFunction->mInitialized)
  899. mCeMachine->PrepareFunction(innerFunction, checkBuilder);
  900. CeOperand result = FrameAlloc(mCeMachine->GetBeContext()->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
  901. Emit(CeOp_GetMethod_Inner);
  902. EmitFrameOffset(result);
  903. Emit((int32)innerFunctionIdx);
  904. return result;
  905. }
  906. Fail(StrFormat("Unable to locate method %s", beFunction->mName.c_str()));
  907. }
  908. CeCallEntry callEntry;
  909. callEntry.mFunctionInfo = ceFunctionInfo;
  910. *callIdxPtr = (int)mCeFunction->mCallTable.size();
  911. mCeFunction->mCallTable.Add(callEntry);
  912. }
  913. if (allowImmediate)
  914. {
  915. CeOperand result;
  916. result.mKind = CeOperandKind_CallTableIdx;
  917. result.mCallTableIdx = *callIdxPtr;
  918. return result;
  919. }
  920. CeOperand result = FrameAlloc(mCeMachine->GetBeContext()->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
  921. Emit(CeOp_GetMethod);
  922. EmitFrameOffset(result);
  923. Emit((int32)*callIdxPtr);
  924. return result;
  925. }
  926. break;
  927. case BeCallInst::TypeId:
  928. {
  929. // auto callInst = (BeCallInst*)value;
  930. // if (callInst->mInlineResult != NULL)
  931. // return GetOperand(callInst->mInlineResult);
  932. }
  933. break;
  934. }
  935. CeOperand* operandPtr = NULL;
  936. mValueToOperand.TryGetValue(value, &operandPtr);
  937. if (errorKind != CeErrorKind_None)
  938. {
  939. Emit(CeOp_Error);
  940. Emit((int32)errorKind);
  941. }
  942. else
  943. {
  944. if (operandPtr == NULL)
  945. {
  946. BeDumpContext dumpCtx;
  947. String str;
  948. dumpCtx.ToString(str, value);
  949. Fail(StrFormat("Unable to find bevalue for operand: %s", str.c_str()));
  950. }
  951. }
  952. if (operandPtr == NULL)
  953. {
  954. return FrameAlloc(errorType);
  955. }
  956. auto operand = *operandPtr;
  957. if ((operand.mKind == CeOperandKind_AllocaAddr) && (!allowAlloca))
  958. {
  959. auto irCodeGen = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen;
  960. auto ptrType = mCeMachine->GetBeContext()->GetPointerTo(operand.mType);
  961. auto result = FrameAlloc(ptrType);
  962. Emit((mPtrSize == 4) ? CeOp_FrameAddr_32 : CeOp_FrameAddr_64);
  963. EmitFrameOffset(result);
  964. Emit((int32)operand.mFrameOfs);
  965. return result;
  966. }
  967. return operand;
  968. }
  969. CeSizeClass CeBuilder::GetSizeClass(int size)
  970. {
  971. switch (size)
  972. {
  973. case 1:
  974. return CeSizeClass_8;
  975. case 2:
  976. return CeSizeClass_16;
  977. case 4:
  978. return CeSizeClass_32;
  979. case 8:
  980. return CeSizeClass_64;
  981. default:
  982. return CeSizeClass_X;
  983. }
  984. }
  985. void CeBuilder::HandleParams()
  986. {
  987. auto beModule = mBeFunction->mModule;
  988. // int regIdxOfs = 0;
  989. // int paramOfs = 0;
  990. auto retType = mBeFunction->GetFuncType()->mReturnType;
  991. int frameOffset = 0;
  992. if (retType->mSize > 0)
  993. {
  994. mReturnVal.mKind = CeOperandKind_AllocaAddr;
  995. mReturnVal.mFrameOfs = frameOffset;
  996. mReturnVal.mType = retType;
  997. frameOffset += retType->mSize;
  998. }
  999. int paramOfs = 0;
  1000. //for (int paramIdx = (int)mBeFunction->mParams.size() - 1; paramIdx >= 0; paramIdx--)
  1001. for (int paramIdx = 0; paramIdx < mBeFunction->mParams.size(); paramIdx++)
  1002. {
  1003. auto funcType = mBeFunction->GetFuncType();
  1004. auto& typeParam = funcType->mParams[paramIdx + paramOfs];
  1005. auto& param = mBeFunction->mParams[paramIdx + paramOfs];
  1006. auto beArg = beModule->GetArgument(paramIdx + paramOfs);
  1007. auto paramType = typeParam.mType;
  1008. CeOperand ceOperand;
  1009. ceOperand.mKind = CeOperandKind_FrameOfs;
  1010. ceOperand.mFrameOfs = frameOffset;
  1011. ceOperand.mType = paramType;
  1012. frameOffset += paramType->mSize;
  1013. mValueToOperand[beArg] = ceOperand;
  1014. }
  1015. }
  1016. void CeBuilder::Build()
  1017. {
  1018. auto irCodeGen = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen;
  1019. auto beModule = irCodeGen->mBeModule;
  1020. mCeFunction->mFailed = true;
  1021. auto methodInstance = mCeFunction->mMethodInstance;
  1022. if (methodInstance != NULL)
  1023. {
  1024. auto methodDef = methodInstance->mMethodDef;
  1025. BfMethodInstance dupMethodInstance;
  1026. dupMethodInstance.CopyFrom(methodInstance);
  1027. //dupMethodInstance.mIRFunction = workItem.mFunc;
  1028. dupMethodInstance.mIsReified = true;
  1029. dupMethodInstance.mInCEMachine = false; // Only have the original one
  1030. int startFunctionCount = (int)beModule->mFunctions.size();
  1031. //int startGlobalVariableCount = (int)beModule->mGlobalVariables.size();
  1032. mCeMachine->mCeModule->mHadBuildError = false;
  1033. mCeMachine->mCeModule->ProcessMethod(&dupMethodInstance, true);
  1034. if (!dupMethodInstance.mIRFunction)
  1035. {
  1036. mCeFunction->mFailed = true;
  1037. return;
  1038. }
  1039. mIntPtrType = irCodeGen->mBeContext->GetPrimitiveType((mPtrSize == 4) ? BeTypeCode_Int32 : BeTypeCode_Int64);
  1040. mBeFunction = (BeFunction*)irCodeGen->GetBeValue(dupMethodInstance.mIRFunction.mId);
  1041. for (int funcIdx = startFunctionCount; funcIdx < (int)beModule->mFunctions.size(); funcIdx++)
  1042. {
  1043. auto beFunction = beModule->mFunctions[funcIdx];
  1044. if (beFunction == mBeFunction)
  1045. continue;
  1046. if (beFunction->mBlocks.IsEmpty())
  1047. continue;
  1048. CeFunction* innerFunction = new CeFunction();
  1049. innerFunction->mCeInnerFunctionInfo = new CeInnerFunctionInfo();
  1050. innerFunction->mCeInnerFunctionInfo->mName = beFunction->mName;
  1051. innerFunction->mCeInnerFunctionInfo->mBeFunction = beFunction;
  1052. innerFunction->mCeInnerFunctionInfo->mOwner = mCeFunction;
  1053. mInnerFunctionMap[beFunction] = (int)mCeFunction->mInnerFunctions.size();
  1054. mCeFunction->mInnerFunctions.Add(innerFunction);
  1055. }
  1056. // for (int globalVarIdx = startGlobalVariableCount; globalVarIdx < (int)beModule->mGlobalVariables.size(); globalVarIdx++)
  1057. // {
  1058. // auto beGlobalVariable = beModule->mGlobalVariables[globalVarIdx];
  1059. // if (beGlobalVariable->mInitializer == NULL)
  1060. // continue;
  1061. //
  1062. // CeStaticFieldInfo* staticFieldInfoPtr = NULL;
  1063. // mCeMachine->mStaticFieldMap.TryAdd(beGlobalVariable->mName, NULL, &staticFieldInfoPtr);
  1064. // staticFieldInfoPtr->mBeConstant = beGlobalVariable;
  1065. // }
  1066. if (!mCeFunction->mCeFunctionInfo->mName.IsEmpty())
  1067. {
  1068. BF_ASSERT(mCeFunction->mCeFunctionInfo->mName == mBeFunction->mName);
  1069. }
  1070. else
  1071. {
  1072. mCeFunction->mCeFunctionInfo->mName = mBeFunction->mName;
  1073. mCeMachine->mNamedFunctionMap[mCeFunction->mCeFunctionInfo->mName] = mCeFunction->mCeFunctionInfo;
  1074. }
  1075. if (mCeMachine->mCeModule->mHadBuildError)
  1076. {
  1077. mCeFunction->mGenError = "Method had errors";
  1078. mCeMachine->mCeModule->mHadBuildError = false;
  1079. return;
  1080. }
  1081. }
  1082. else
  1083. {
  1084. BF_ASSERT(mCeFunction->mCeInnerFunctionInfo != NULL);
  1085. mBeFunction = mCeFunction->mCeInnerFunctionInfo->mBeFunction;
  1086. BF_ASSERT(mBeFunction != NULL);
  1087. mCeFunction->mCeInnerFunctionInfo->mBeFunction = NULL;
  1088. }
  1089. SetAndRestoreValue<BeFunction*> prevBeFunction(beModule->mActiveFunction, mBeFunction);
  1090. // Create blocks
  1091. for (int blockIdx = 0; blockIdx < (int)mBeFunction->mBlocks.size(); blockIdx++)
  1092. {
  1093. auto beBlock = mBeFunction->mBlocks[blockIdx];
  1094. CeBlock ceBlock;
  1095. mBlocks.Add(ceBlock);
  1096. CeOperand ceOperand;
  1097. ceOperand.mKind = CeOperandKind_Block;
  1098. ceOperand.mBlockIdx = blockIdx;
  1099. mValueToOperand[beBlock] = ceOperand;
  1100. }
  1101. // Instruction pre-pass
  1102. for (int blockIdx = 0; blockIdx < (int)mBeFunction->mBlocks.size(); blockIdx++)
  1103. {
  1104. auto beBlock = mBeFunction->mBlocks[blockIdx];
  1105. auto& ceBlock = mBlocks[blockIdx];
  1106. for (int instIdx = 0; instIdx < (int)beBlock->mInstructions.size(); instIdx++)
  1107. {
  1108. auto inst = beBlock->mInstructions[instIdx];
  1109. int instType = inst->GetTypeId();
  1110. switch (instType)
  1111. {
  1112. case BePhiInst::TypeId:
  1113. {
  1114. auto castedInst = (BePhiInst*)inst;
  1115. auto resultType = castedInst->GetType();
  1116. auto phiResult = FrameAlloc(resultType);
  1117. mValueToOperand[castedInst] = phiResult;
  1118. for (auto& phiIncoming : castedInst->mIncoming)
  1119. {
  1120. auto incomingBlockOpr = GetOperand(phiIncoming->mBlock);
  1121. auto& incomingBlock = mBlocks[incomingBlockOpr.mBlockIdx];
  1122. CePhiOutgoing phiOutgoing;
  1123. phiOutgoing.mPhiValue = phiIncoming->mValue;
  1124. phiOutgoing.mPhiInst = castedInst;
  1125. phiOutgoing.mPhiBlockIdx = blockIdx;
  1126. incomingBlock.mPhiOutgoing.Add(phiOutgoing);
  1127. }
  1128. }
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. // Primary instruction pass
  1134. BeDbgLoc* prevEmitDbgPos = NULL;
  1135. bool inHeadAlloca = true;
  1136. for (int blockIdx = 0; blockIdx < (int)mBeFunction->mBlocks.size(); blockIdx++)
  1137. {
  1138. auto beBlock = mBeFunction->mBlocks[blockIdx];
  1139. auto ceBlock = &mBlocks[blockIdx];
  1140. ceBlock->mEmitOfs = GetCodePos();
  1141. if (blockIdx == 0)
  1142. HandleParams();
  1143. for (int instIdx = 0; instIdx < (int)beBlock->mInstructions.size(); instIdx++)
  1144. {
  1145. auto inst = beBlock->mInstructions[instIdx];
  1146. CeOperand result;
  1147. int startCodePos = GetCodePos();
  1148. mCurDbgLoc = inst->mDbgLoc;
  1149. int instType = inst->GetTypeId();
  1150. switch (instType)
  1151. {
  1152. case BeAllocaInst::TypeId:
  1153. case BeNumericCastInst::TypeId:
  1154. case BeBitCastInst::TypeId:
  1155. break;
  1156. default:
  1157. inHeadAlloca = false;
  1158. break;
  1159. }
  1160. switch (instType)
  1161. {
  1162. case BeEnsureInstructionAtInst::TypeId:
  1163. case BeNopInst::TypeId:
  1164. case BeDbgDeclareInst::TypeId:
  1165. case BeLifetimeStartInst::TypeId:
  1166. case BeLifetimeEndInst::TypeId:
  1167. case BeLifetimeExtendInst::TypeId:
  1168. case BeValueScopeStartInst::TypeId:
  1169. case BeValueScopeEndInst::TypeId:
  1170. case BeValueScopeRetainInst::TypeId:
  1171. case BeConstEvalGetVirtualFunc::TypeId:
  1172. case BeConstEvalGetInterfaceFunc::TypeId:
  1173. break;
  1174. case BeUnreachableInst::TypeId:
  1175. Emit(CeOp_InvalidOp);
  1176. break;
  1177. case BeUndefValueInst::TypeId:
  1178. {
  1179. auto castedInst = (BeUndefValueInst*)inst;
  1180. result = FrameAlloc(castedInst->mType);
  1181. }
  1182. break;
  1183. case BeAllocaInst::TypeId:
  1184. {
  1185. auto castedInst = (BeAllocaInst*)inst;
  1186. CeOperand ceSize;
  1187. ceSize.mKind = CeOperandKind_Immediate;
  1188. ceSize.mImmediate = castedInst->mType->mSize;
  1189. ceSize.mType = mIntPtrType;
  1190. bool isAligned16 = false;
  1191. int align = castedInst->mAlign;
  1192. BeType* allocType = castedInst->mType;
  1193. bool preservedVolatiles = false;
  1194. if (castedInst->mArraySize != NULL)
  1195. {
  1196. auto mcArraySize = GetOperand(castedInst->mArraySize, false, true);
  1197. if (mcArraySize.IsImmediate())
  1198. {
  1199. ceSize.mImmediate = ceSize.mImmediate * mcArraySize.mImmediate;
  1200. }
  1201. else
  1202. {
  1203. inHeadAlloca = false;
  1204. if (ceSize.mImmediate == 1)
  1205. {
  1206. ceSize = mcArraySize;
  1207. }
  1208. else
  1209. {
  1210. ceSize = EmitConst(ceSize.mImmediate, mcArraySize.mType->mSize);
  1211. EmitSizedOp(CeOp_Mul_I8, ceSize.mType->mSize);
  1212. EmitFrameOffset(ceSize);
  1213. EmitFrameOffset(ceSize);
  1214. EmitFrameOffset(mcArraySize);
  1215. }
  1216. }
  1217. }
  1218. if (inHeadAlloca)
  1219. {
  1220. BF_ASSERT(ceSize.mKind == CeOperandKind_Immediate);
  1221. mFrameSize += ceSize.mImmediate;
  1222. result.mKind = CeOperandKind_AllocaAddr;
  1223. result.mFrameOfs = -mFrameSize;
  1224. result.mType = castedInst->mType;
  1225. }
  1226. else
  1227. {
  1228. if (ceSize.mKind == CeOperandKind_Immediate)
  1229. {
  1230. Emit(CeOp_AdjustSPConst);
  1231. Emit((int32)-ceSize.mImmediate);
  1232. }
  1233. else
  1234. {
  1235. Emit(CeOp_AdjustSPNeg);
  1236. EmitFrameOffset(ceSize);
  1237. }
  1238. auto ptrType = beModule->mContext->GetPointerTo(allocType);
  1239. result = FrameAlloc(ptrType);
  1240. Emit(CeOp_GetSP);
  1241. EmitFrameOffset(result);
  1242. }
  1243. }
  1244. break;
  1245. case BeLoadInst::TypeId:
  1246. {
  1247. auto castedInst = (BeLoadInst*)inst;
  1248. auto ceTarget = GetOperand(castedInst->mTarget, true);
  1249. if (ceTarget.mKind == CeOperandKind_AllocaAddr)
  1250. {
  1251. if (inst->mRefCount <= 1)
  1252. {
  1253. result = ceTarget;
  1254. result.mKind = CeOperandKind_FrameOfs;
  1255. }
  1256. else
  1257. {
  1258. ceTarget.mKind = CeOperandKind_FrameOfs;
  1259. result = FrameAlloc(ceTarget.mType);
  1260. EmitSizedOp(CeOp_Move_8, ceTarget, NULL, true);
  1261. Emit((int32)result.mFrameOfs);
  1262. }
  1263. }
  1264. else
  1265. {
  1266. BF_ASSERT(ceTarget.mType->IsPointer());
  1267. auto pointerType = (BePointerType*)ceTarget.mType;
  1268. auto elemType = pointerType->mElementType;
  1269. CeOperand refOperand = ceTarget;
  1270. refOperand.mType = elemType;
  1271. EmitSizedOp(CeOp_Load_8, refOperand, &result, true);
  1272. }
  1273. }
  1274. break;
  1275. case BeBinaryOpInst::TypeId:
  1276. {
  1277. auto castedInst = (BeBinaryOpInst*)inst;
  1278. auto ceLHS = GetOperand(castedInst->mLHS);
  1279. auto ceRHS = GetOperand(castedInst->mRHS);
  1280. switch (castedInst->mOpKind)
  1281. {
  1282. case BeBinaryOpKind_Add:
  1283. EmitBinaryOp(CeOp_Add_I8, CeOp_Add_F32, ceLHS, ceRHS, result);
  1284. break;
  1285. case BeBinaryOpKind_Subtract:
  1286. EmitBinaryOp(CeOp_Sub_I8, CeOp_Sub_F32, ceLHS, ceRHS, result);
  1287. break;
  1288. case BeBinaryOpKind_Multiply:
  1289. EmitBinaryOp(CeOp_Mul_I8, CeOp_Mul_F32, ceLHS, ceRHS, result);
  1290. break;
  1291. case BeBinaryOpKind_SDivide:
  1292. EmitBinaryOp(CeOp_Div_I8, CeOp_Div_F32, ceLHS, ceRHS, result);
  1293. break;
  1294. case BeBinaryOpKind_UDivide:
  1295. EmitBinaryOp(CeOp_Div_U8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1296. break;
  1297. case BeBinaryOpKind_SModulus:
  1298. EmitBinaryOp(CeOp_Mod_I8, CeOp_Mod_F32, ceLHS, ceRHS, result);
  1299. break;
  1300. case BeBinaryOpKind_UModulus:
  1301. EmitBinaryOp(CeOp_Mod_U8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1302. break;
  1303. case BeBinaryOpKind_BitwiseAnd:
  1304. EmitBinaryOp(CeOp_And_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1305. break;
  1306. case BeBinaryOpKind_BitwiseOr:
  1307. EmitBinaryOp(CeOp_Or_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1308. break;
  1309. case BeBinaryOpKind_ExclusiveOr:
  1310. EmitBinaryOp(CeOp_Xor_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1311. break;
  1312. case BeBinaryOpKind_LeftShift:
  1313. EmitBinaryOp(CeOp_Shl_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1314. break;
  1315. case BeBinaryOpKind_RightShift:
  1316. EmitBinaryOp(CeOp_Shr_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1317. break;
  1318. case BeBinaryOpKind_ARightShift:
  1319. EmitBinaryOp(CeOp_Shr_U8, CeOp_InvalidOp, ceLHS, ceRHS, result);
  1320. break;
  1321. default:
  1322. Fail("Invalid binary op");
  1323. }
  1324. }
  1325. break;
  1326. case BeBitCastInst::TypeId:
  1327. {
  1328. auto castedInst = (BeBitCastInst*)inst;
  1329. auto mcValue = GetOperand(castedInst->mValue, false, true);
  1330. if (castedInst->mToType->IsInt())
  1331. {
  1332. BF_ASSERT(castedInst->mToType->mSize == 8);
  1333. }
  1334. else
  1335. BF_ASSERT(castedInst->mToType->IsPointer());
  1336. auto toType = castedInst->mToType;
  1337. if (mcValue.IsImmediate())
  1338. {
  1339. if (mcValue.mImmediate == 0)
  1340. {
  1341. CeOperand newImmediate;
  1342. newImmediate.mKind = CeOperandKind_Immediate;
  1343. newImmediate.mType = toType;
  1344. result = newImmediate;
  1345. }
  1346. else
  1347. {
  1348. // Non-zero constant. Weird case, just do an actual MOV
  1349. result = FrameAlloc(toType);
  1350. EmitSizedOp(CeOp_Const_8, result, NULL, true);
  1351. int64 val = mcValue.mImmediate;
  1352. Emit(&val, toType->mSize);
  1353. }
  1354. }
  1355. else
  1356. {
  1357. if (toType->mSize != mcValue.mType->mSize)
  1358. Fail("Invalid bitcast");
  1359. result = mcValue;
  1360. result.mType = toType;
  1361. }
  1362. }
  1363. break;
  1364. case BeNumericCastInst::TypeId:
  1365. {
  1366. auto castedInst = (BeNumericCastInst*)inst;
  1367. auto ceValue = GetOperand(castedInst->mValue);
  1368. auto fromType = ceValue.mType;
  1369. if (fromType == castedInst->mToType)
  1370. {
  1371. // If it's just a sign change then leave it alone
  1372. result = ceValue;
  1373. }
  1374. else
  1375. {
  1376. auto toType = castedInst->mToType;
  1377. if ((toType->IsIntable()) && (fromType->IsIntable()) && (toType->mSize <= fromType->mSize))
  1378. {
  1379. // For truncating values, no actual instructions are needed
  1380. // Note that a copy is not needed because of SSA rules
  1381. result = ceValue;
  1382. result.mType = toType;
  1383. }
  1384. else
  1385. {
  1386. result = FrameAlloc(toType);
  1387. CeOp op = CeOp_InvalidOp;
  1388. BeTypeCode fromTypeCode = fromType->mTypeCode;
  1389. BeTypeCode toTypeCode = toType->mTypeCode;
  1390. if ((castedInst->mValSigned) && (castedInst->mToSigned))
  1391. {
  1392. switch (fromTypeCode)
  1393. {
  1394. case BeTypeCode_Int8:
  1395. switch (toTypeCode)
  1396. {
  1397. case BeTypeCode_Int16:
  1398. op = CeOp_Conv_I8_I16;
  1399. break;
  1400. case BeTypeCode_Int32:
  1401. op = CeOp_Conv_I8_I32;
  1402. break;
  1403. case BeTypeCode_Int64:
  1404. op = CeOp_Conv_I8_I64;
  1405. break;
  1406. case BeTypeCode_Float:
  1407. op = CeOp_Conv_I8_F32;
  1408. break;
  1409. case BeTypeCode_Double:
  1410. op = CeOp_Conv_I8_F64;
  1411. break;
  1412. }
  1413. break;
  1414. case BeTypeCode_Int16:
  1415. switch (toTypeCode)
  1416. {
  1417. case BeTypeCode_Int32:
  1418. op = CeOp_Conv_I16_I32;
  1419. break;
  1420. case BeTypeCode_Int64:
  1421. op = CeOp_Conv_I16_I64;
  1422. break;
  1423. case BeTypeCode_Float:
  1424. op = CeOp_Conv_I16_F32;
  1425. break;
  1426. case BeTypeCode_Double:
  1427. op = CeOp_Conv_I16_F64;
  1428. break;
  1429. }
  1430. break;
  1431. case BeTypeCode_Int32:
  1432. switch (toTypeCode)
  1433. {
  1434. case BeTypeCode_Int64:
  1435. op = CeOp_Conv_I32_I64;
  1436. break;
  1437. case BeTypeCode_Float:
  1438. op = CeOp_Conv_I32_F32;
  1439. break;
  1440. case BeTypeCode_Double:
  1441. op = CeOp_Conv_I32_F64;
  1442. break;
  1443. }
  1444. break;
  1445. case BeTypeCode_Int64:
  1446. switch (toTypeCode)
  1447. {
  1448. case BeTypeCode_Float:
  1449. op = CeOp_Conv_I64_F32;
  1450. break;
  1451. case BeTypeCode_Double:
  1452. op = CeOp_Conv_I64_F64;
  1453. break;
  1454. }
  1455. break;
  1456. case BeTypeCode_Float:
  1457. switch (toTypeCode)
  1458. {
  1459. case BeTypeCode_Int8:
  1460. op = CeOp_Conv_F32_I8;
  1461. break;
  1462. case BeTypeCode_Int16:
  1463. op = CeOp_Conv_F32_I16;
  1464. break;
  1465. case BeTypeCode_Int32:
  1466. op = CeOp_Conv_F32_I32;
  1467. break;
  1468. case BeTypeCode_Int64:
  1469. op = CeOp_Conv_F32_I64;
  1470. break;
  1471. case BeTypeCode_Double:
  1472. op = CeOp_Conv_F32_F64;
  1473. break;
  1474. }
  1475. break;
  1476. case BeTypeCode_Double:
  1477. switch (toTypeCode)
  1478. {
  1479. case BeTypeCode_Int8:
  1480. op = CeOp_Conv_F64_I8;
  1481. break;
  1482. case BeTypeCode_Int16:
  1483. op = CeOp_Conv_F64_I16;
  1484. break;
  1485. case BeTypeCode_Int32:
  1486. op = CeOp_Conv_F64_I32;
  1487. break;
  1488. case BeTypeCode_Int64:
  1489. op = CeOp_Conv_F64_I64;
  1490. break;
  1491. case BeTypeCode_Float:
  1492. op = CeOp_Conv_F64_F32;
  1493. break;
  1494. }
  1495. break;
  1496. }
  1497. }
  1498. else
  1499. {
  1500. switch (fromTypeCode)
  1501. {
  1502. case BeTypeCode_Int8:
  1503. switch (toTypeCode)
  1504. {
  1505. case BeTypeCode_Int16:
  1506. op = CeOp_Conv_U8_U16;
  1507. break;
  1508. case BeTypeCode_Int32:
  1509. op = CeOp_Conv_U8_U32;
  1510. break;
  1511. case BeTypeCode_Int64:
  1512. op = CeOp_Conv_U8_U64;
  1513. break;
  1514. case BeTypeCode_Float:
  1515. op = CeOp_Conv_I8_F32;
  1516. break;
  1517. case BeTypeCode_Double:
  1518. op = CeOp_Conv_I8_F64;
  1519. break;
  1520. }
  1521. break;
  1522. case BeTypeCode_Int16:
  1523. switch (toTypeCode)
  1524. {
  1525. case BeTypeCode_Int32:
  1526. op = CeOp_Conv_U16_U32;
  1527. break;
  1528. case BeTypeCode_Int64:
  1529. op = CeOp_Conv_U16_U64;
  1530. break;
  1531. case BeTypeCode_Float:
  1532. op = CeOp_Conv_U16_F32;
  1533. break;
  1534. case BeTypeCode_Double:
  1535. op = CeOp_Conv_U16_F64;
  1536. break;
  1537. }
  1538. break;
  1539. case BeTypeCode_Int32:
  1540. switch (toTypeCode)
  1541. {
  1542. case BeTypeCode_Int64:
  1543. op = CeOp_Conv_U32_U64;
  1544. break;
  1545. case BeTypeCode_Float:
  1546. op = CeOp_Conv_U32_F32;
  1547. break;
  1548. case BeTypeCode_Double:
  1549. op = CeOp_Conv_U32_F64;
  1550. break;
  1551. }
  1552. break;
  1553. case BeTypeCode_Int64:
  1554. switch (toTypeCode)
  1555. {
  1556. case BeTypeCode_Float:
  1557. op = CeOp_Conv_I64_F32;
  1558. break;
  1559. case BeTypeCode_Double:
  1560. op = CeOp_Conv_I64_F64;
  1561. break;
  1562. }
  1563. break;
  1564. }
  1565. }
  1566. if (op == CeOp_InvalidOp)
  1567. {
  1568. Fail("Invalid conversion op");
  1569. }
  1570. else
  1571. {
  1572. Emit(op);
  1573. EmitFrameOffset(result);
  1574. EmitFrameOffset(ceValue);
  1575. }
  1576. }
  1577. }
  1578. }
  1579. break;
  1580. case BeStoreInst::TypeId:
  1581. {
  1582. auto castedInst = (BeStoreInst*)inst;
  1583. auto mcVal = GetOperand(castedInst->mVal);
  1584. auto mcPtr = GetOperand(castedInst->mPtr, true);
  1585. if (mcPtr.mKind == CeOperandKind_AllocaAddr)
  1586. {
  1587. EmitSizedOp(CeOp_Move_8, mcVal, NULL, true);
  1588. Emit((int32)mcPtr.mFrameOfs);
  1589. }
  1590. else
  1591. {
  1592. EmitSizedOp(CeOp_Store_8, mcVal, NULL, true);
  1593. EmitFrameOffset(mcPtr);
  1594. }
  1595. }
  1596. break;
  1597. case BeRetInst::TypeId:
  1598. {
  1599. auto castedInst = (BeRetInst*)inst;
  1600. if (castedInst->mRetValue != NULL)
  1601. {
  1602. auto mcVal = GetOperand(castedInst->mRetValue);
  1603. BF_ASSERT(mReturnVal.mKind == CeOperandKind_AllocaAddr);
  1604. EmitSizedOp(CeOp_Move_8, mcVal, NULL, true);
  1605. Emit((int32)mReturnVal.mFrameOfs);
  1606. }
  1607. Emit(CeOp_Ret);
  1608. }
  1609. break;
  1610. case BeCmpInst::TypeId:
  1611. {
  1612. auto castedInst = (BeCmpInst*)inst;
  1613. auto ceLHS = GetOperand(castedInst->mLHS);
  1614. auto ceRHS = GetOperand(castedInst->mRHS);
  1615. CeOp iOp = CeOp_InvalidOp;
  1616. CeOp fOp = CeOp_InvalidOp;
  1617. switch (castedInst->mCmpKind)
  1618. {
  1619. case BeCmpKind_EQ:
  1620. iOp = CeOp_Cmp_EQ_I8;
  1621. fOp = CeOp_Cmp_EQ_F32;
  1622. break;
  1623. case BeCmpKind_NE:
  1624. iOp = CeOp_Cmp_NE_I8;
  1625. fOp = CeOp_Cmp_NE_F32;
  1626. break;
  1627. case BeCmpKind_SLT:
  1628. iOp = CeOp_Cmp_SLT_I8;
  1629. fOp = CeOp_Cmp_SLT_F32;
  1630. break;
  1631. case BeCmpKind_ULT:
  1632. iOp = CeOp_Cmp_ULT_I8;
  1633. break;
  1634. case BeCmpKind_SLE:
  1635. iOp = CeOp_Cmp_SLE_I8;
  1636. fOp = CeOp_Cmp_SLE_F32;
  1637. break;
  1638. case BeCmpKind_ULE:
  1639. iOp = CeOp_Cmp_ULE_I8;
  1640. break;
  1641. case BeCmpKind_SGT:
  1642. iOp = CeOp_Cmp_SGT_I8;
  1643. fOp = CeOp_Cmp_SGT_F32;
  1644. break;
  1645. case BeCmpKind_UGT:
  1646. iOp = CeOp_Cmp_UGT_I8;
  1647. break;
  1648. case BeCmpKind_SGE:
  1649. iOp = CeOp_Cmp_SGE_I8;
  1650. fOp = CeOp_Cmp_SGE_F32;
  1651. break;
  1652. case BeCmpKind_UGE:
  1653. iOp = CeOp_Cmp_UGE_I8;
  1654. break;
  1655. }
  1656. if (iOp == CeOp_InvalidOp)
  1657. {
  1658. Fail("Invalid cmp");
  1659. break;
  1660. }
  1661. auto boolType = inst->GetType();
  1662. result = FrameAlloc(boolType);
  1663. EmitBinaryOp(iOp, fOp, ceLHS, ceRHS, result);
  1664. // auto mcInst = AllocInst(BeMCInstKind_Cmp, mcLHS, mcRHS);
  1665. //
  1666. // auto cmpResultIdx = (int)mCmpResults.size();
  1667. // BeCmpResult cmpResult;
  1668. // cmpResult.mCmpKind = castedInst->mCmpKind;
  1669. // mCmpResults.push_back(cmpResult);
  1670. //
  1671. // result.mKind = BeMCOperandKind_CmpResult;
  1672. // result.mCmpResultIdx = cmpResultIdx;
  1673. //
  1674. // mcInst->mResult = result;
  1675. }
  1676. break;
  1677. case BeGEPInst::TypeId:
  1678. {
  1679. auto castedInst = (BeGEPInst*)inst;
  1680. auto ceVal = GetOperand(castedInst->mPtr);
  1681. auto ceIdx0 = GetOperand(castedInst->mIdx0, false, true);
  1682. BePointerType* ptrType = (BePointerType*)ceVal.mType;
  1683. BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
  1684. result = ceVal;
  1685. if (castedInst->mIdx1 != NULL)
  1686. {
  1687. // We assume we never do both an idx0 and idx1 at once. Fix if we change that.
  1688. BF_ASSERT(castedInst->mIdx0);
  1689. auto ceIdx1 = GetOperand(castedInst->mIdx1, false, true);
  1690. if (!ceIdx1.IsImmediate())
  1691. {
  1692. // This path is used when we have a const array that gets indexed by a non-const index value
  1693. if (ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray)
  1694. {
  1695. auto arrayType = (BeSizedArrayType*)ptrType->mElementType;
  1696. auto elementPtrType = beModule->mContext->GetPointerTo(arrayType->mElementType);
  1697. if (ceIdx1.IsImmediate())
  1698. {
  1699. if (ceIdx1.mImmediate == 0)
  1700. {
  1701. result = ceVal;
  1702. result.mType = elementPtrType;
  1703. }
  1704. else
  1705. {
  1706. auto ptrValue = FrameAlloc(elementPtrType);
  1707. result = ptrValue;
  1708. result = FrameAlloc(elementPtrType);
  1709. Emit((CeOp)(CeOp_AddConst_I32));
  1710. EmitFrameOffset(result);
  1711. EmitFrameOffset(ceVal);
  1712. Emit((int32)(ceIdx1.mImmediate * arrayType->mElementType->mSize));
  1713. }
  1714. }
  1715. else
  1716. {
  1717. auto ptrValue = FrameAlloc(elementPtrType);
  1718. result = ptrValue;
  1719. if (ceIdx1.mType->mSize < 4)
  1720. {
  1721. auto ceNewIdx = FrameAlloc(mIntPtrType);
  1722. if (mIntPtrType->mSize == 8)
  1723. {
  1724. if (ceIdx1.mType->mSize == 1)
  1725. Emit(CeOp_Conv_I8_I64);
  1726. else
  1727. Emit(CeOp_Conv_I16_I64);
  1728. }
  1729. else
  1730. {
  1731. if (ceIdx1.mType->mSize == 1)
  1732. Emit(CeOp_Conv_I8_I32);
  1733. else
  1734. Emit(CeOp_Conv_I16_I32);
  1735. }
  1736. EmitFrameOffset(ceNewIdx);
  1737. EmitFrameOffset(ceIdx1);
  1738. ceIdx1 = ceNewIdx;
  1739. }
  1740. result = FrameAlloc(elementPtrType);
  1741. if (mPtrSize == 4)
  1742. {
  1743. auto mcElementSize = FrameAlloc(mIntPtrType);
  1744. Emit(CeOp_Const_32);
  1745. EmitFrameOffset(mcElementSize);
  1746. Emit((int32)arrayType->mElementType->mSize);
  1747. auto ofsValue = FrameAlloc(mIntPtrType);
  1748. Emit(CeOp_Mul_I32);
  1749. EmitFrameOffset(ofsValue);
  1750. EmitFrameOffset(ceIdx1);
  1751. EmitFrameOffset(mcElementSize);
  1752. Emit(CeOp_Add_I32);
  1753. EmitFrameOffset(result);
  1754. EmitFrameOffset(ceVal);
  1755. EmitFrameOffset(ofsValue);
  1756. }
  1757. else
  1758. {
  1759. auto mcElementSize = FrameAlloc(mIntPtrType);
  1760. Emit(CeOp_Const_64);
  1761. EmitFrameOffset(mcElementSize);
  1762. Emit((int64)arrayType->mElementType->mSize);
  1763. auto ofsValue = FrameAlloc(mIntPtrType);
  1764. Emit(CeOp_Mul_I64);
  1765. EmitFrameOffset(ofsValue);
  1766. EmitFrameOffset(ceIdx1);
  1767. EmitFrameOffset(mcElementSize);
  1768. Emit(CeOp_Add_I64);
  1769. EmitFrameOffset(result);
  1770. EmitFrameOffset(ceVal);
  1771. EmitFrameOffset(ofsValue);
  1772. }
  1773. }
  1774. }
  1775. else
  1776. Fail("Invalid GEP");
  1777. }
  1778. else
  1779. {
  1780. BF_ASSERT(ceIdx1.IsImmediate());
  1781. int byteOffset = 0;
  1782. BeType* elementType = NULL;
  1783. if (ptrType->mElementType->mTypeCode == BeTypeCode_Struct)
  1784. {
  1785. BeStructType* structType = (BeStructType*)ptrType->mElementType;
  1786. auto& structMember = structType->mMembers[ceIdx1.mImmediate];
  1787. elementType = structMember.mType;
  1788. byteOffset = structMember.mByteOffset;
  1789. }
  1790. else if (ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray)
  1791. {
  1792. auto arrayType = (BeSizedArrayType*)ptrType->mElementType;
  1793. elementType = arrayType->mElementType;
  1794. byteOffset = ceIdx1.mImmediate * elementType->mSize;
  1795. }
  1796. else if (ptrType->mElementType->mTypeCode == BeTypeCode_Vector)
  1797. {
  1798. auto arrayType = (BeVectorType*)ptrType->mElementType;
  1799. elementType = arrayType->mElementType;
  1800. byteOffset = ceIdx1.mImmediate * elementType->mSize;
  1801. }
  1802. else
  1803. {
  1804. Fail("Invalid gep target");
  1805. }
  1806. auto elementPtrType = beModule->mContext->GetPointerTo(elementType);
  1807. if (byteOffset != 0)
  1808. {
  1809. result = FrameAlloc(elementPtrType);
  1810. Emit((CeOp)(CeOp_AddConst_I32));
  1811. EmitFrameOffset(result);
  1812. EmitFrameOffset(ceVal);
  1813. Emit((int32)byteOffset);
  1814. }
  1815. else
  1816. {
  1817. result.mType = elementPtrType;
  1818. }
  1819. }
  1820. }
  1821. else
  1822. {
  1823. CeOperand mcRelOffset;
  1824. int relScale = 1;
  1825. if (ceIdx0.IsImmediate())
  1826. {
  1827. int byteOffset = ceIdx0.mImmediate * ptrType->mElementType->mSize;
  1828. if (byteOffset != 0)
  1829. {
  1830. result = FrameAlloc(ptrType);
  1831. Emit((CeOp)(CeOp_AddConst_I32));
  1832. EmitFrameOffset(result);
  1833. EmitFrameOffset(ceVal);
  1834. Emit((int32)byteOffset);
  1835. }
  1836. }
  1837. else
  1838. {
  1839. result = FrameAlloc(ptrType);
  1840. if (mPtrSize == 4)
  1841. {
  1842. auto mcElementSize = FrameAlloc(mIntPtrType);
  1843. Emit(CeOp_Const_32);
  1844. EmitFrameOffset(mcElementSize);
  1845. Emit((int32)ptrType->mElementType->mSize);
  1846. auto ofsValue = FrameAlloc(mIntPtrType);
  1847. Emit(CeOp_Mul_I32);
  1848. EmitFrameOffset(ofsValue);
  1849. EmitFrameOffset(ceIdx0);
  1850. EmitFrameOffset(mcElementSize);
  1851. Emit(CeOp_Add_I32);
  1852. EmitFrameOffset(result);
  1853. EmitFrameOffset(ceVal);
  1854. EmitFrameOffset(ofsValue);
  1855. }
  1856. else
  1857. {
  1858. auto mcElementSize = FrameAlloc(mIntPtrType);
  1859. Emit(CeOp_Const_64);
  1860. EmitFrameOffset(mcElementSize);
  1861. Emit((int64)ptrType->mElementType->mSize);
  1862. auto ofsValue = FrameAlloc(mIntPtrType);
  1863. Emit(CeOp_Mul_I64);
  1864. EmitFrameOffset(ofsValue);
  1865. EmitFrameOffset(ceIdx0);
  1866. EmitFrameOffset(mcElementSize);
  1867. Emit(CeOp_Add_I64);
  1868. EmitFrameOffset(result);
  1869. EmitFrameOffset(ceVal);
  1870. EmitFrameOffset(ofsValue);
  1871. }
  1872. }
  1873. }
  1874. }
  1875. break;
  1876. case BeExtractValueInst::TypeId:
  1877. {
  1878. auto castedInst = (BeExtractValueInst*)inst;
  1879. BeConstant* constant = BeValueDynCast<BeConstant>(castedInst->mAggVal);
  1880. CeOperand mcAgg;
  1881. if (constant != NULL)
  1882. {
  1883. result.mImmediate = 0;
  1884. BeType* wantDefaultType = NULL;
  1885. if (constant->mType->IsStruct())
  1886. {
  1887. BeStructType* structType = (BeStructType*)constant->mType;
  1888. auto& member = structType->mMembers[castedInst->mIdx];
  1889. wantDefaultType = member.mType;
  1890. }
  1891. else if (constant->mType->IsSizedArray())
  1892. {
  1893. BeSizedArrayType* arrayType = (BeSizedArrayType*)constant->mType;
  1894. wantDefaultType = arrayType->mElementType;
  1895. }
  1896. if (wantDefaultType != NULL)
  1897. {
  1898. // switch (wantDefaultType->mTypeCode)
  1899. // {
  1900. // case BeTypeCode_Boolean:
  1901. // case BeTypeCode_Int8:
  1902. // result.mKind = BeMCOperandKind_Immediate_i8;
  1903. // break;
  1904. // case BeTypeCode_Int16:
  1905. // result.mKind = BeMCOperandKind_Immediate_i16;
  1906. // break;
  1907. // case BeTypeCode_Int32:
  1908. // result.mKind = BeMCOperandKind_Immediate_i32;
  1909. // break;
  1910. // case BeTypeCode_Int64:
  1911. // result.mKind = BeMCOperandKind_Immediate_i64;
  1912. // break;
  1913. // case BeTypeCode_Float:
  1914. // result.mKind = BeMCOperandKind_Immediate_f32;
  1915. // break;
  1916. // case BeTypeCode_Double:
  1917. // result.mKind = BeMCOperandKind_Immediate_f64;
  1918. // break;
  1919. // case BeTypeCode_Pointer:
  1920. // result.mKind = BeMCOperandKind_Immediate_Null;
  1921. // result.mType = wantDefaultType;
  1922. // break;
  1923. // case BeTypeCode_Struct:
  1924. // case BeTypeCode_SizedArray:
  1925. // {
  1926. // auto subConst = mAlloc.Alloc<BeConstant>();
  1927. // subConst->mType = wantDefaultType;
  1928. // result.mConstant = subConst;
  1929. // result.mKind = BeMCOperandKind_ConstAgg;
  1930. // }
  1931. // break;
  1932. // default:
  1933. // NotImpl();
  1934. // }
  1935. Fail("Unhandled extract");
  1936. }
  1937. break;
  1938. }
  1939. else
  1940. {
  1941. mcAgg = GetOperand(castedInst->mAggVal);
  1942. }
  1943. auto aggType = mcAgg.mType;
  1944. int byteOffset = 0;
  1945. BeType* memberType = NULL;
  1946. if (aggType->IsSizedArray())
  1947. {
  1948. auto sizedArray = (BeSizedArrayType*)aggType;
  1949. memberType = sizedArray->mElementType;
  1950. byteOffset = BF_ALIGN(memberType->mSize, memberType->mAlign) * castedInst->mIdx;
  1951. }
  1952. else
  1953. {
  1954. BF_ASSERT(aggType->IsStruct());
  1955. BeStructType* structType = (BeStructType*)aggType;
  1956. auto& structMember = structType->mMembers[castedInst->mIdx];
  1957. byteOffset = structMember.mByteOffset;
  1958. memberType = structMember.mType;
  1959. }
  1960. if (byteOffset != 0)
  1961. {
  1962. auto ptrVal = FrameAlloc(beModule->mContext->GetPrimitiveType(BeTypeCode_Int32));
  1963. Emit(CeOp_FrameAddrOfs_32);
  1964. EmitFrameOffset(ptrVal);
  1965. EmitFrameOffset(mcAgg);
  1966. Emit((int32)byteOffset);
  1967. result = FrameAlloc(memberType);
  1968. EmitSizedOp(CeOp_Load_8, memberType->mSize);
  1969. EmitFrameOffset(result);
  1970. EmitFrameOffset(ptrVal);
  1971. }
  1972. else
  1973. {
  1974. result = mcAgg;
  1975. result.mType = memberType;
  1976. }
  1977. }
  1978. break;
  1979. case BeBrInst::TypeId:
  1980. {
  1981. auto castedInst = (BeBrInst*)inst;
  1982. auto targetBlock = GetOperand(castedInst->mTargetBlock);
  1983. BF_ASSERT(targetBlock.mKind == CeOperandKind_Block);
  1984. FlushPhi(ceBlock, targetBlock.mBlockIdx);
  1985. if (targetBlock.mBlockIdx == blockIdx + 1)
  1986. {
  1987. // Do nothing - just continuing to next block
  1988. break;
  1989. }
  1990. EmitJump(CeOp_Jmp, targetBlock);
  1991. }
  1992. break;
  1993. case BeCondBrInst::TypeId:
  1994. {
  1995. auto castedInst = (BeCondBrInst*)inst;
  1996. auto testVal = GetOperand(castedInst->mCond, true);
  1997. auto trueBlock = GetOperand(castedInst->mTrueBlock);
  1998. auto falseBlock = GetOperand(castedInst->mFalseBlock);
  1999. FlushPhi(ceBlock, trueBlock.mBlockIdx);
  2000. EmitJump(CeOp_JmpIf, trueBlock);
  2001. EmitFrameOffset(testVal);
  2002. FlushPhi(ceBlock, falseBlock.mBlockIdx);
  2003. EmitJump(CeOp_Jmp, falseBlock);
  2004. }
  2005. break;
  2006. case BePhiInst::TypeId:
  2007. result = GetOperand(inst);
  2008. BF_ASSERT(result);
  2009. break;
  2010. case BeNegInst::TypeId:
  2011. {
  2012. auto castedInst = (BeNegInst*)inst;
  2013. auto ceValue = GetOperand(castedInst->mValue);
  2014. EmitUnaryOp(CeOp_Neg_I8, CeOp_Neg_F32, ceValue, result);
  2015. }
  2016. break;
  2017. case BeNotInst::TypeId:
  2018. {
  2019. auto castedInst = (BeNotInst*)inst;
  2020. auto ceValue = GetOperand(castedInst->mValue);
  2021. if (ceValue.mType->mTypeCode == BeTypeCode_Boolean)
  2022. EmitUnaryOp(CeOp_Not_I1, CeOp_InvalidOp, ceValue, result);
  2023. else
  2024. EmitUnaryOp(CeOp_Not_I8, CeOp_InvalidOp, ceValue, result);
  2025. }
  2026. break;
  2027. case BeSwitchInst::TypeId:
  2028. {
  2029. auto castedInst = (BeSwitchInst*)inst;
  2030. std::stable_sort(castedInst->mCases.begin(), castedInst->mCases.end(), [&](const BeSwitchCase& lhs, const BeSwitchCase& rhs)
  2031. {
  2032. return lhs.mValue->mInt64 < rhs.mValue->mInt64;
  2033. });
  2034. int numVals = castedInst->mCases.size();
  2035. if (numVals > 0)
  2036. {
  2037. EmitBinarySwitchSection(castedInst, 0, castedInst->mCases.size());
  2038. }
  2039. auto mcDefaultBlock = GetOperand(castedInst->mDefaultBlock);
  2040. EmitJump(CeOp_Jmp, mcDefaultBlock);
  2041. }
  2042. break;
  2043. case BeCallInst::TypeId:
  2044. {
  2045. auto castedInst = (BeCallInst*)inst;
  2046. BeType* returnType = NULL;
  2047. bool isVarArg = false;
  2048. bool useAltArgs = false;
  2049. CeOperand ceFunc;
  2050. BeFunctionType* beFuncType = NULL;
  2051. CeOperand virtTarget;
  2052. int ifaceTypeId = -1;
  2053. int virtualTableIdx = -1;
  2054. if (auto intrin = BeValueDynCast<BeIntrinsic>(castedInst->mFunc))
  2055. {
  2056. switch (intrin->mKind)
  2057. {
  2058. case BfIRIntrinsic_Cast:
  2059. {
  2060. result = GetOperand(castedInst->mArgs[0].mValue);
  2061. result.mType = intrin->mReturnType;
  2062. }
  2063. break;
  2064. case BfIRIntrinsic_MemCpy:
  2065. {
  2066. CeOperand ceDestPtr = GetOperand(castedInst->mArgs[0].mValue);
  2067. CeOperand ceSrcPtr = GetOperand(castedInst->mArgs[1].mValue);
  2068. CeOperand ceSize = GetOperand(castedInst->mArgs[2].mValue);
  2069. Emit(CeOp_MemCpy);
  2070. EmitFrameOffset(ceDestPtr);
  2071. EmitFrameOffset(ceSrcPtr);
  2072. EmitFrameOffset(ceSize);
  2073. }
  2074. break;
  2075. case BfIRIntrinsic_AtomicFence:
  2076. // Nothing to do
  2077. break;
  2078. default:
  2079. Emit(CeOp_Error);
  2080. Emit((int32)CeErrorKind_Intrinsic);
  2081. break;
  2082. }
  2083. }
  2084. else if (auto beFunction = BeValueDynCast<BeFunction>(castedInst->mFunc))
  2085. {
  2086. beFuncType = beFunction->GetFuncType();
  2087. if (beFunction->mName == "malloc")
  2088. {
  2089. result = FrameAlloc(beFuncType->mReturnType);
  2090. auto ceSize = GetOperand(castedInst->mArgs[0].mValue);
  2091. Emit(CeOp_Malloc);
  2092. EmitFrameOffset(result);
  2093. EmitFrameOffset(ceSize);
  2094. break;
  2095. }
  2096. if (beFunction->mName == "free")
  2097. {
  2098. auto cePtr = GetOperand(castedInst->mArgs[0].mValue);
  2099. Emit(CeOp_Free);
  2100. EmitFrameOffset(cePtr);
  2101. break;
  2102. }
  2103. ceFunc = GetOperand(beFunction, false, true);
  2104. }
  2105. else if (auto beGetVirtualFunc = BeValueDynCast<BeConstEvalGetVirtualFunc>(castedInst->mFunc))
  2106. {
  2107. virtTarget = GetOperand(beGetVirtualFunc->mValue);
  2108. virtualTableIdx = beGetVirtualFunc->mVirtualTableIdx;
  2109. auto resultType = beGetVirtualFunc->GetType();
  2110. BF_ASSERT(resultType->IsPointer());
  2111. beFuncType = (BeFunctionType*)((BePointerType*)resultType)->mElementType;
  2112. }
  2113. else if (auto beGetInterfaceFunc = BeValueDynCast<BeConstEvalGetInterfaceFunc>(castedInst->mFunc))
  2114. {
  2115. virtTarget = GetOperand(beGetInterfaceFunc->mValue);
  2116. ifaceTypeId = beGetInterfaceFunc->mIFaceTypeId;
  2117. virtualTableIdx = beGetInterfaceFunc->mMethodIdx;
  2118. auto resultType = beGetInterfaceFunc->GetType();
  2119. BF_ASSERT(resultType->IsPointer());
  2120. beFuncType = (BeFunctionType*)((BePointerType*)resultType)->mElementType;
  2121. }
  2122. else
  2123. {
  2124. ceFunc = GetOperand(castedInst->mFunc, false, true);
  2125. auto funcType = castedInst->mFunc->GetType();
  2126. if (funcType->IsPointer())
  2127. {
  2128. auto ptrType = (BePointerType*)funcType;
  2129. if (ptrType->mElementType->mTypeCode == BeTypeCode_Function)
  2130. {
  2131. beFuncType = (BeFunctionType*)ptrType->mElementType;
  2132. }
  2133. }
  2134. }
  2135. if ((ceFunc) || (virtualTableIdx != -1))
  2136. {
  2137. CeOperand thisOperand;
  2138. for (int argIdx = (int)castedInst->mArgs.size() - 1; argIdx >= 0; argIdx--)
  2139. {
  2140. auto& arg = castedInst->mArgs[argIdx];
  2141. auto ceArg = GetOperand(arg.mValue);
  2142. if (argIdx == 0)
  2143. thisOperand = ceArg;
  2144. EmitSizedOp(CeOp_Push_8, ceArg, NULL, true);
  2145. }
  2146. int stackAdjust = 0;
  2147. if (beFuncType->mReturnType->mSize > 0)
  2148. {
  2149. Emit(CeOp_AdjustSPConst);
  2150. Emit((int32)-beFuncType->mReturnType->mSize);
  2151. stackAdjust += beFuncType->mReturnType->mSize;
  2152. }
  2153. if (!ceFunc)
  2154. ceFunc = FrameAlloc(beModule->mContext->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
  2155. if (ifaceTypeId != -1)
  2156. {
  2157. Emit(CeOp_GetMethod_IFace);
  2158. EmitFrameOffset(ceFunc);
  2159. EmitFrameOffset(thisOperand);
  2160. Emit((int32)ifaceTypeId);
  2161. Emit((int32)virtualTableIdx);
  2162. }
  2163. else if (virtualTableIdx != -1)
  2164. {
  2165. Emit(CeOp_GetMethod_Virt);
  2166. EmitFrameOffset(ceFunc);
  2167. EmitFrameOffset(thisOperand);
  2168. Emit((int32)virtualTableIdx);
  2169. }
  2170. if (ceFunc.mKind == CeOperandKind_CallTableIdx)
  2171. {
  2172. CeOperand result = FrameAlloc(mCeMachine->GetBeContext()->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
  2173. Emit(CeOp_GetMethod);
  2174. EmitFrameOffset(result);
  2175. Emit((int32)ceFunc.mCallTableIdx);
  2176. ceFunc = result;
  2177. }
  2178. Emit(CeOp_Call);
  2179. EmitFrameOffset(ceFunc);
  2180. if (beFuncType->mReturnType->mSize > 0)
  2181. {
  2182. result = FrameAlloc(beFuncType->mReturnType);
  2183. EmitSizedOp(CeOp_Pop_8, result, NULL, true);
  2184. }
  2185. if (stackAdjust > 0)
  2186. {
  2187. Emit(CeOp_AdjustSPConst);
  2188. Emit(stackAdjust);
  2189. }
  2190. }
  2191. }
  2192. break;
  2193. case BeMemSetInst::TypeId:
  2194. {
  2195. auto castedInst = (BeMemSetInst*)inst;
  2196. auto ceAddr = GetOperand(castedInst->mAddr);
  2197. if (auto constVal = BeValueDynCast<BeConstant>(castedInst->mVal))
  2198. {
  2199. if (auto constSize = BeValueDynCast<BeConstant>(castedInst->mSize))
  2200. {
  2201. if (constVal->mUInt8 == 0)
  2202. {
  2203. Emit(CeOp_MemSet_Const);
  2204. EmitFrameOffset(ceAddr);
  2205. Emit((uint8)0);
  2206. Emit((int32)constSize->mUInt32);
  2207. break;
  2208. }
  2209. }
  2210. }
  2211. auto ceVal = GetOperand(castedInst->mVal);
  2212. auto ceSize = GetOperand(castedInst->mSize);
  2213. Emit(CeOp_MemSet);
  2214. EmitFrameOffset(ceAddr);
  2215. EmitFrameOffset(ceVal);
  2216. EmitFrameOffset(ceSize);
  2217. }
  2218. break;
  2219. case BeFenceInst::TypeId:
  2220. break;
  2221. case BeStackSaveInst::TypeId:
  2222. {
  2223. result = FrameAlloc(mIntPtrType);
  2224. Emit(CeOp_GetSP);
  2225. EmitFrameOffset(result);
  2226. }
  2227. break;
  2228. case BeStackRestoreInst::TypeId:
  2229. {
  2230. auto castedInst = (BeStackRestoreInst*)inst;
  2231. auto mcStackVal = GetOperand(castedInst->mStackVal);
  2232. Emit(CeOp_SetSP);
  2233. EmitFrameOffset(mcStackVal);
  2234. }
  2235. break;
  2236. case BeConstEvalGetType::TypeId:
  2237. {
  2238. auto castedInst = (BeConstEvalGetType*)inst;
  2239. result.mKind = CeOperandKind_Immediate;
  2240. result.mImmediate = castedInst->mTypeId;
  2241. result.mType = beModule->mContext->GetPrimitiveType(BeTypeCode_Int32);
  2242. }
  2243. break;
  2244. case BeConstEvalGetReflectType::TypeId:
  2245. {
  2246. auto castedInst = (BeConstEvalGetReflectType*)inst;
  2247. auto ptrType = beModule->mContext->GetVoidPtrType();
  2248. result = FrameAlloc(ptrType);
  2249. Emit(CeOp_GetReflectType);
  2250. EmitFrameOffset(result);
  2251. Emit((int32)castedInst->mTypeId);
  2252. }
  2253. break;
  2254. case BeConstEvalDynamicCastCheck::TypeId:
  2255. {
  2256. auto castedInst = (BeConstEvalDynamicCastCheck*)inst;
  2257. auto mcValue = GetOperand(castedInst->mValue);
  2258. auto ptrType = beModule->mContext->GetVoidPtrType();
  2259. result = FrameAlloc(ptrType);
  2260. Emit(CeOp_DynamicCastCheck);
  2261. EmitFrameOffset(result);
  2262. EmitFrameOffset(mcValue);
  2263. Emit((int32)castedInst->mTypeId);
  2264. }
  2265. break;
  2266. default:
  2267. Fail("Unhandled instruction");
  2268. return;
  2269. }
  2270. if (result.mKind != CeOperandKind_None)
  2271. mValueToOperand[inst] = result;
  2272. if ((startCodePos != GetCodePos()) && (prevEmitDbgPos != mCurDbgLoc))
  2273. {
  2274. prevEmitDbgPos = mCurDbgLoc;
  2275. int fileIdx = -1;
  2276. BeDbgFile* dbgFile = NULL;
  2277. if (mCurDbgLoc != NULL)
  2278. {
  2279. auto dbgFile = mCurDbgLoc->GetDbgFile();
  2280. int* valuePtr = NULL;
  2281. if (mDbgFileMap.TryAdd(dbgFile, NULL, &valuePtr))
  2282. {
  2283. fileIdx = (int)mCeFunction->mFiles.size();
  2284. String filePath = dbgFile->mDirectory;
  2285. filePath.Append(DIR_SEP_CHAR);
  2286. filePath += dbgFile->mFileName;
  2287. mCeFunction->mFiles.Add(filePath);
  2288. *valuePtr = fileIdx;
  2289. }
  2290. else
  2291. fileIdx = *valuePtr;
  2292. }
  2293. CeEmitEntry emitEntry;
  2294. emitEntry.mCodePos = startCodePos;
  2295. emitEntry.mFile = fileIdx;
  2296. if (mCurDbgLoc != NULL)
  2297. {
  2298. emitEntry.mLine = mCurDbgLoc->mLine;
  2299. emitEntry.mColumn = mCurDbgLoc->mColumn;
  2300. }
  2301. else
  2302. {
  2303. emitEntry.mLine = -1;
  2304. emitEntry.mColumn = -1;
  2305. }
  2306. mCeFunction->mEmitTable.Add(emitEntry);
  2307. }
  2308. }
  2309. }
  2310. for (auto& jumpEntry : mJumpTable)
  2311. {
  2312. auto& ceBlock = mBlocks[jumpEntry.mBlockIdx];
  2313. *((int32*)(&mCeFunction->mCode[0] + jumpEntry.mEmitPos)) = ceBlock.mEmitOfs - jumpEntry.mEmitPos - 4;
  2314. }
  2315. if (mCeFunction->mCode.size() == 0)
  2316. {
  2317. Fail("No method definition available");
  2318. return;
  2319. }
  2320. if (mCeFunction->mGenError.IsEmpty())
  2321. mCeFunction->mFailed = false;
  2322. mCeFunction->mFrameSize = mFrameSize;
  2323. }
  2324. //////////////////////////////////////////////////////////////////////////
  2325. CeMachine::CeMachine(BfCompiler* compiler)
  2326. {
  2327. mCompiler = compiler;
  2328. mCeModule = NULL;
  2329. mRevision = 0;
  2330. mExecuteId = 0;
  2331. mRevisionExecuteTime = 0;
  2332. mCurTargetSrc = NULL;
  2333. mCurModule = NULL;
  2334. mCurMethodInstance = NULL;
  2335. mCurExpectingType = NULL;
  2336. mHeap = NULL;
  2337. mStringCharsOffset = -1;
  2338. mAppendAllocInfo = NULL;
  2339. }
  2340. CeMachine::~CeMachine()
  2341. {
  2342. delete mAppendAllocInfo;
  2343. delete mCeModule;
  2344. delete mHeap;
  2345. for (auto kv : mFunctions)
  2346. {
  2347. auto functionInfo = kv.mValue;
  2348. delete functionInfo;
  2349. }
  2350. }
  2351. BfError* CeMachine::Fail(const StringImpl& error)
  2352. {
  2353. auto bfError = mCurModule->Fail(StrFormat("Unable to const-evaluate %s", mCurModule->MethodToString(mCurMethodInstance).c_str()), mCurTargetSrc);
  2354. if (bfError == NULL)
  2355. return NULL;
  2356. mCompiler->mPassInstance->MoreInfo(error);
  2357. return bfError;
  2358. }
  2359. BfError* CeMachine::Fail(const CeFrame& curFrame, const StringImpl& str)
  2360. {
  2361. auto bfError = mCurModule->Fail(StrFormat("Unable to const-evaluate %s", mCurModule->MethodToString(mCurMethodInstance).c_str()), mCurTargetSrc);
  2362. if (bfError == NULL)
  2363. return NULL;
  2364. auto passInstance = mCompiler->mPassInstance;
  2365. for (int stackIdx = mCallStack.size(); stackIdx >= 0; stackIdx--)
  2366. {
  2367. bool isHeadEntry = stackIdx == mCallStack.size();
  2368. auto* ceFrame = (isHeadEntry) ? &curFrame : &mCallStack[stackIdx];
  2369. auto ceFunction = ceFrame->mFunction;
  2370. int i = 0;
  2371. CeEmitEntry* emitEntry = NULL;
  2372. if (!ceFunction->mCode.IsEmpty())
  2373. {
  2374. int lo = 0;
  2375. int hi = ceFunction->mEmitTable.size() - 1;
  2376. int instIdx = ceFrame->mInstPtr - &ceFunction->mCode[0] - 1;
  2377. while (lo <= hi)
  2378. {
  2379. i = (lo + hi) / 2;
  2380. emitEntry = &ceFunction->mEmitTable.mVals[i];
  2381. //int c = midVal <=> value;
  2382. if (emitEntry->mCodePos == instIdx) break;
  2383. if (emitEntry->mCodePos < instIdx)
  2384. lo = i + 1;
  2385. else
  2386. hi = i - 1;
  2387. }
  2388. if ((emitEntry != NULL) && (emitEntry->mCodePos > instIdx) && (i > 0))
  2389. emitEntry = &ceFunction->mEmitTable.mVals[i - 1];
  2390. }
  2391. StringT<256> err;
  2392. if (isHeadEntry)
  2393. {
  2394. err = str;
  2395. err += " ";
  2396. }
  2397. err += StrFormat("in const evaluation of ");
  2398. if (ceFunction->mMethodInstance != NULL)
  2399. err += mCeModule->MethodToString(ceFunction->mMethodInstance, BfMethodNameFlag_OmitParams);
  2400. else
  2401. {
  2402. err += mCeModule->MethodToString(ceFunction->mCeInnerFunctionInfo->mOwner->mMethodInstance, BfMethodNameFlag_OmitParams);
  2403. }
  2404. if (emitEntry != NULL)
  2405. err += StrFormat(" at line% d:%d in %s", emitEntry->mLine + 1, emitEntry->mColumn + 1, ceFunction->mFiles[emitEntry->mFile].c_str());
  2406. auto moreInfo = passInstance->MoreInfo(err);
  2407. if ((moreInfo != NULL) && (emitEntry != NULL))
  2408. {
  2409. BfErrorLocation* location = new BfErrorLocation();
  2410. location->mFile = ceFunction->mFiles[emitEntry->mFile];
  2411. location->mLine = emitEntry->mLine;
  2412. location->mColumn = emitEntry->mColumn;
  2413. moreInfo->mLocation = location;
  2414. }
  2415. }
  2416. return bfError;
  2417. }
  2418. void CeMachine::Init()
  2419. {
  2420. mCeModule = new BfModule(mCompiler->mContext, "__constEval");
  2421. mCeModule->mIsSpecialModule = true;
  2422. //mCeModule->mIsScratchModule = true;
  2423. mCeModule->mIsConstModule = true;
  2424. //mCeModule->mIsReified = true;
  2425. if (mCompiler->mIsResolveOnly)
  2426. mCeModule->mIsReified = true;
  2427. else
  2428. mCeModule->mIsReified = false;
  2429. mCeModule->Init();
  2430. mCeModule->mBfIRBuilder = new BfIRBuilder(mCeModule);
  2431. mCeModule->mBfIRBuilder->mDbgVerifyCodeGen = true;
  2432. mCeModule->FinishInit();
  2433. mCeModule->mBfIRBuilder->mHasDebugInfo = false; // Only line info
  2434. mCeModule->mBfIRBuilder->mIgnoreWrites = false;
  2435. mCeModule->mWantsIRIgnoreWrites = false;
  2436. }
  2437. uint8* CeMachine::CeMalloc(int size)
  2438. {
  2439. #ifdef CE_ENABLE_HEAP
  2440. auto heapRef = mHeap->Alloc(size);
  2441. auto ceAddr = BF_CE_STACK_SIZE + heapRef;
  2442. int sizeDelta = (ceAddr + size) - mMemory.mSize;
  2443. if (sizeDelta > 0)
  2444. mMemory.GrowUninitialized(sizeDelta);
  2445. return mMemory.mVals + ceAddr;
  2446. #else
  2447. return mMemory.GrowUninitialized(size);
  2448. #endif
  2449. }
  2450. bool CeMachine::CeFree(addr_ce addr)
  2451. {
  2452. #ifdef CE_ENABLE_HEAP
  2453. ContiguousHeap::AllocRef heapRef = addr - BF_CE_STACK_SIZE;
  2454. return mHeap->Free(heapRef);
  2455. #else
  2456. return true;
  2457. #endif
  2458. }
  2459. addr_ce CeMachine::CeAllocArray(BfArrayType* arrayType, int count, addr_ce& elemsAddr)
  2460. {
  2461. mCeModule->PopulateType(arrayType);
  2462. BfType* elemType = arrayType->GetUnderlyingType();
  2463. auto countOffset = arrayType->mBaseType->mFieldInstances[0].mDataOffset;
  2464. auto elemOffset = arrayType->mFieldInstances[0].mDataOffset;
  2465. int allocSize = elemOffset + elemType->GetStride() * count;
  2466. uint8* mem = CeMalloc(allocSize);
  2467. memset(mem, 0, allocSize);
  2468. *(int32*)(mem) = arrayType->mTypeId;
  2469. *(int32*)(mem + countOffset) = count;
  2470. elemsAddr = (addr_ce)(mem + elemOffset - mMemory.mVals);
  2471. return (addr_ce)(mem - mMemory.mVals);
  2472. }
  2473. addr_ce CeMachine::GetConstantData(BeConstant* constant)
  2474. {
  2475. auto writeConstant = constant;
  2476. if (auto gvConstant = BeValueDynCast<BeGlobalVariable>(writeConstant))
  2477. {
  2478. if (gvConstant->mInitializer != NULL)
  2479. writeConstant = gvConstant->mInitializer;
  2480. }
  2481. CeConstStructData structData;
  2482. auto result = WriteConstant(structData, writeConstant);
  2483. BF_ASSERT(result == CeErrorKind_None);
  2484. uint8* ptr = CeMalloc(structData.mData.mSize);
  2485. memcpy(ptr, structData.mData.mVals, structData.mData.mSize);
  2486. return (addr_ce)(ptr - mMemory.mVals);
  2487. }
  2488. addr_ce CeMachine::GetReflectType(int typeId)
  2489. {
  2490. addr_ce* addrPtr = NULL;
  2491. if (!mReflectMap.TryAdd(typeId, NULL, &addrPtr))
  2492. return *addrPtr;
  2493. if (mCeModule->mContext->mBfTypeType == NULL)
  2494. mCeModule->mContext->ReflectInit();
  2495. if ((uintptr)typeId >= (uintptr)mCeModule->mContext->mTypes.mSize)
  2496. return 0;
  2497. auto bfType = mCeModule->mContext->mTypes[typeId];
  2498. if (bfType == NULL)
  2499. return 0;
  2500. mCeModule->PopulateType(bfType, BfPopulateType_DataAndMethods);
  2501. Dictionary<int, int> usedStringMap;
  2502. auto irData = mCeModule->CreateTypeData(bfType, usedStringMap, true, true, true, false);
  2503. BeValue* beValue = NULL;
  2504. if (auto constant = mCeModule->mBfIRBuilder->GetConstant(irData))
  2505. {
  2506. if (constant->mConstType == BfConstType_BitCast)
  2507. {
  2508. auto bitcast = (BfConstantBitCast*)constant;
  2509. constant = mCeModule->mBfIRBuilder->GetConstantById(bitcast->mTarget);
  2510. }
  2511. if (constant->mConstType == BfConstType_GlobalVar)
  2512. {
  2513. auto globalVar = (BfGlobalVar*)constant;
  2514. beValue = mCeModule->mBfIRBuilder->mBeIRCodeGen->GetBeValue(globalVar->mStreamId);
  2515. }
  2516. }
  2517. if (auto constant = BeValueDynCast<BeConstant>(beValue))
  2518. *addrPtr = GetConstantData(constant);
  2519. return *addrPtr;
  2520. }
  2521. addr_ce CeMachine::GetString(int stringId)
  2522. {
  2523. addr_ce* ceAddrPtr = NULL;
  2524. if (!mStringMap.TryAdd(stringId, NULL, &ceAddrPtr))
  2525. return *ceAddrPtr;
  2526. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCompiler->mStringTypeDef, BfPopulateType_Data);
  2527. String str;
  2528. BfStringPoolEntry* entry = NULL;
  2529. if (mCeModule->mContext->mStringObjectIdMap.TryGetValue(stringId, &entry))
  2530. {
  2531. str = entry->mString;
  2532. }
  2533. int allocSize = stringTypeInst->mInstSize + (int)str.length() + 1;
  2534. int charsOffset = stringTypeInst->mInstSize;
  2535. mStringCharsOffset = charsOffset;
  2536. uint8* mem = CeMalloc(allocSize);
  2537. memset(mem, 0, allocSize);
  2538. auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize;
  2539. auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset;
  2540. auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset;
  2541. auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset;
  2542. // Write TypeId into there
  2543. *(int32*)(mem) = stringTypeInst->mTypeId;
  2544. *(int32*)(mem + lenOffset) = (int)str.length();
  2545. if (lenByteCount == 4)
  2546. *(int32*)(mem + allocSizeOffset) = 0x40000000 + (int)str.length() + 1;
  2547. else
  2548. *(int64*)(mem + allocSizeOffset) = 0x4000000000000000LL + (int)str.length() + 1;
  2549. *(int32*)(mem + ptrOffset) = (mem + charsOffset) - mMemory.mVals;
  2550. memcpy(mem + charsOffset, str.c_str(), str.length());
  2551. *ceAddrPtr = mem - mMemory.mVals;
  2552. return *ceAddrPtr;
  2553. }
  2554. BfType* CeMachine::GetBfType(int typeId)
  2555. {
  2556. if ((uintptr)typeId < (uintptr)mCeModule->mContext->mTypes.size())
  2557. return mCeModule->mContext->mTypes[typeId];
  2558. return NULL;
  2559. }
  2560. void CeMachine::PrepareConstStructEntry(CeConstStructData& constEntry)
  2561. {
  2562. if (constEntry.mHash.IsZero())
  2563. {
  2564. constEntry.mHash = Hash128(&constEntry.mData[0], constEntry.mData.mSize);
  2565. if (!constEntry.mFixups.IsEmpty())
  2566. constEntry.mHash = Hash128(&constEntry.mFixups[0], constEntry.mFixups.mSize * sizeof(CeConstStructFixup), constEntry.mHash);
  2567. }
  2568. if (!constEntry.mFixups.IsEmpty())
  2569. {
  2570. if (constEntry.mFixedData.IsEmpty())
  2571. constEntry.mFixedData = constEntry.mData;
  2572. for (auto& fixup : constEntry.mFixups)
  2573. {
  2574. if (fixup.mKind == CeConstStructFixup::Kind_StringPtr)
  2575. {
  2576. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCompiler->mStringTypeDef, BfPopulateType_Data);
  2577. addr_ce addrPtr = GetString(fixup.mValue);
  2578. *(addr_ce*)(constEntry.mFixedData.mVals + fixup.mOffset) = addrPtr;
  2579. }
  2580. else if (fixup.mKind == CeConstStructFixup::Kind_StringCharPtr)
  2581. {
  2582. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCompiler->mStringTypeDef, BfPopulateType_Data);
  2583. addr_ce addrPtr = GetString(fixup.mValue);
  2584. *(addr_ce*)(constEntry.mFixedData.mVals + fixup.mOffset) = addrPtr + stringTypeInst->mInstSize;
  2585. }
  2586. }
  2587. }
  2588. constEntry.mBindExecuteId = mExecuteId;
  2589. }
  2590. BeContext* CeMachine::GetBeContext()
  2591. {
  2592. if (mCeModule == NULL)
  2593. return NULL;
  2594. return mCeModule->mBfIRBuilder->mBeIRCodeGen->mBeContext;
  2595. }
  2596. BeModule* CeMachine::GetBeModule()
  2597. {
  2598. if (mCeModule == NULL)
  2599. return NULL;
  2600. return mCeModule->mBfIRBuilder->mBeIRCodeGen->mBeModule;
  2601. }
  2602. void CeMachine::CompileStarted()
  2603. {
  2604. mRevisionExecuteTime = 0;
  2605. mRevision++;
  2606. if (mCeModule != NULL)
  2607. {
  2608. delete mCeModule;
  2609. mCeModule = NULL;
  2610. }
  2611. }
  2612. void CeMachine::DerefMethodInfo(CeFunctionInfo* ceFunctionInfo)
  2613. {
  2614. ceFunctionInfo->mRefCount--;
  2615. if (ceFunctionInfo->mRefCount > 0)
  2616. return;
  2617. BF_ASSERT(ceFunctionInfo->mMethodInstance == NULL);
  2618. auto itr = mNamedFunctionMap.Find(ceFunctionInfo->mName);
  2619. if (itr->mValue == ceFunctionInfo)
  2620. mNamedFunctionMap.Remove(itr);
  2621. delete ceFunctionInfo;
  2622. }
  2623. void CeMachine::RemoveMethod(BfMethodInstance* methodInstance)
  2624. {
  2625. BfLogSys(methodInstance->GetOwner()->mModule->mSystem, "CeMachine::RemoveMethod %p\n", methodInstance);
  2626. auto itr = mFunctions.Find(methodInstance);
  2627. auto ceFunctionInfo = itr->mValue;
  2628. BF_ASSERT(itr != mFunctions.end());
  2629. if (itr != mFunctions.end())
  2630. {
  2631. if (ceFunctionInfo->mMethodInstance == methodInstance)
  2632. {
  2633. auto ceFunction = ceFunctionInfo->mCeFunction;
  2634. for (auto& callEntry : ceFunction->mCallTable)
  2635. {
  2636. if (callEntry.mFunctionInfo != NULL)
  2637. DerefMethodInfo(callEntry.mFunctionInfo);
  2638. }
  2639. delete ceFunction;
  2640. ceFunctionInfo->mCeFunction = NULL;
  2641. ceFunctionInfo->mMethodInstance = NULL;
  2642. if (methodInstance->mMethodDef->mIsLocalMethod)
  2643. {
  2644. // We can't rebuild these anyway
  2645. }
  2646. else if (ceFunctionInfo->mRefCount > 1)
  2647. {
  2648. // Generate a methodref
  2649. ceFunctionInfo->mMethodRef = methodInstance;
  2650. }
  2651. DerefMethodInfo(ceFunctionInfo);
  2652. }
  2653. mFunctions.Remove(itr);
  2654. }
  2655. CheckFunctions();
  2656. }
  2657. //#define CE_GETC(T) *((T*)(addr += sizeof(T)) - 1)
  2658. #define CE_GETC(T) *(T*)(mMemory.mVals + addr)
  2659. bool CeMachine::WriteConstant(BfModule* module, addr_ce addr, BfConstant* constant, BfType* type, bool isParams)
  2660. {
  2661. switch (constant->mTypeCode)
  2662. {
  2663. case BfTypeCode_Int8:
  2664. case BfTypeCode_UInt8:
  2665. case BfTypeCode_Boolean:
  2666. case BfTypeCode_Char8:
  2667. CE_GETC(int8) = constant->mInt8;
  2668. return true;
  2669. case BfTypeCode_Int16:
  2670. case BfTypeCode_UInt16:
  2671. case BfTypeCode_Char16:
  2672. CE_GETC(int16) = constant->mInt16;
  2673. return true;
  2674. case BfTypeCode_Int32:
  2675. case BfTypeCode_UInt32:
  2676. case BfTypeCode_Char32:
  2677. CE_GETC(int32) = constant->mInt32;
  2678. return true;
  2679. case BfTypeCode_Int64:
  2680. case BfTypeCode_UInt64:
  2681. CE_GETC(int64) = constant->mInt64;
  2682. return true;
  2683. case BfTypeCode_NullPtr:
  2684. if (mCeModule->mSystem->mPtrSize == 4)
  2685. CE_GETC(int32) = 0;
  2686. else
  2687. CE_GETC(int64) = 0;
  2688. return true;
  2689. case BfTypeCode_Float:
  2690. CE_GETC(float) = (float)constant->mDouble;
  2691. return true;
  2692. case BfTypeCode_Double:
  2693. CE_GETC(double) = constant->mDouble;
  2694. return true;
  2695. }
  2696. if (constant->mConstType == BfConstType_Agg)
  2697. {
  2698. auto aggConstant = (BfConstantAgg*)constant;
  2699. if (type->IsSizedArray())
  2700. {
  2701. return false;
  2702. }
  2703. else if (type->IsArray())
  2704. {
  2705. auto elemType = type->GetUnderlyingType();
  2706. addr_ce elemsAddr = 0;
  2707. addr_ce arrayAddr = CeAllocArray((BfArrayType*)type, aggConstant->mValues.size(), elemsAddr);
  2708. for (int i = 0; i < (int)aggConstant->mValues.size(); i++)
  2709. {
  2710. auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[i]);
  2711. if (fieldConstant == NULL)
  2712. return false;
  2713. WriteConstant(module, elemsAddr + i * elemType->GetStride(), fieldConstant, elemType);
  2714. }
  2715. if (mCeModule->mSystem->mPtrSize == 4)
  2716. CE_GETC(int32) = arrayAddr;
  2717. else
  2718. CE_GETC(int64) = arrayAddr;
  2719. return true;
  2720. }
  2721. else if ((type->IsInstanceOf(module->mCompiler->mSpanTypeDef)) && (isParams))
  2722. {
  2723. auto elemType = type->GetUnderlyingType();
  2724. addr_ce elemsAddr = CeMalloc(elemType->GetStride() * aggConstant->mValues.size()) - mMemory.mVals;
  2725. for (int i = 0; i < (int)aggConstant->mValues.size(); i++)
  2726. {
  2727. auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[i]);
  2728. if (fieldConstant == NULL)
  2729. return false;
  2730. WriteConstant(module, elemsAddr + i * elemType->GetStride(), fieldConstant, elemType);
  2731. }
  2732. if (mCeModule->mSystem->mPtrSize == 4)
  2733. {
  2734. CE_GETC(int32) = elemsAddr;
  2735. addr += 4;
  2736. CE_GETC(int32) = (int32)aggConstant->mValues.size();
  2737. }
  2738. else
  2739. {
  2740. CE_GETC(int32) = elemsAddr;
  2741. addr += 8;
  2742. CE_GETC(int64) = (int32)aggConstant->mValues.size();
  2743. }
  2744. }
  2745. else
  2746. {
  2747. BF_ASSERT(type->IsStruct());
  2748. module->PopulateType(type);
  2749. auto typeInst = type->ToTypeInstance();
  2750. int idx = 0;
  2751. if (typeInst->mBaseType != NULL)
  2752. {
  2753. auto baseConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[0]);
  2754. WriteConstant(module, addr, baseConstant, typeInst->mBaseType);
  2755. }
  2756. for (auto& fieldInstance : typeInst->mFieldInstances)
  2757. {
  2758. if (fieldInstance.mDataOffset < 0)
  2759. continue;
  2760. auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[fieldInstance.mDataIdx]);
  2761. if (fieldConstant == NULL)
  2762. return false;
  2763. WriteConstant(module, addr + fieldInstance.mDataOffset, fieldConstant, fieldInstance.mResolvedType);
  2764. }
  2765. }
  2766. return true;
  2767. }
  2768. if (constant->mConstType == BfConstType_AggZero)
  2769. {
  2770. BF_ASSERT(type->IsComposite());
  2771. memset(mMemory.mVals + addr, 0, type->mSize);
  2772. return true;
  2773. }
  2774. if (constant->mConstType == BfConstType_BitCast)
  2775. {
  2776. auto constBitCast = (BfConstantBitCast*)constant;
  2777. auto constTarget = module->mBfIRBuilder->GetConstantById(constBitCast->mTarget);
  2778. return WriteConstant(module, addr, constTarget, type);
  2779. }
  2780. if (constant->mConstType == BfConstType_GEP32_2)
  2781. {
  2782. auto gepConst = (BfConstantGEP32_2*)constant;
  2783. auto constTarget = module->mBfIRBuilder->GetConstantById(gepConst->mTarget);
  2784. if (constTarget->mConstType == BfConstType_GlobalVar)
  2785. {
  2786. auto globalVar = (BfGlobalVar*)constTarget;
  2787. if (strncmp(globalVar->mName, "__bfStrData", 10) == 0)
  2788. {
  2789. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCompiler->mStringTypeDef, BfPopulateType_Data);
  2790. int stringId = atoi(globalVar->mName + 11);
  2791. addr_ce strAddr = GetString(stringId) + stringTypeInst->mInstSize;
  2792. if (mCeModule->mSystem->mPtrSize == 4)
  2793. CE_GETC(int32) = strAddr;
  2794. else
  2795. CE_GETC(int64) = strAddr;
  2796. return true;
  2797. }
  2798. }
  2799. }
  2800. if (constant->mConstType == BfConstType_GlobalVar)
  2801. {
  2802. auto globalVar = (BfGlobalVar*)constant;
  2803. if (strncmp(globalVar->mName, "__bfStrObj", 10) == 0)
  2804. {
  2805. int stringId = atoi(globalVar->mName + 10);
  2806. addr_ce strAddr = GetString(stringId);
  2807. if (mCeModule->mSystem->mPtrSize == 4)
  2808. CE_GETC(int32) = strAddr;
  2809. else
  2810. CE_GETC(int64) = strAddr;
  2811. return true;
  2812. }
  2813. }
  2814. return false;
  2815. }
  2816. CeErrorKind CeMachine::WriteConstant(CeConstStructData& data, BeConstant* constVal)
  2817. {
  2818. auto beType = constVal->GetType();
  2819. if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constVal))
  2820. {
  2821. if (globalVar->mName.StartsWith("__bfStrObj"))
  2822. {
  2823. int stringId = atoi(globalVar->mName.c_str() + 10);
  2824. addr_ce stringAddr = GetString(stringId);
  2825. auto ptr = data.mData.GrowUninitialized(mCeModule->mSystem->mPtrSize);
  2826. int64 addr64 = stringAddr;
  2827. memcpy(ptr, &addr64, mCeModule->mSystem->mPtrSize);
  2828. return CeErrorKind_None;
  2829. }
  2830. if (globalVar->mInitializer == NULL)
  2831. {
  2832. auto ptr = data.mData.GrowUninitialized(mCeModule->mSystem->mPtrSize);
  2833. int64 addr64 = (addr_ce)0;
  2834. memcpy(ptr, &addr64, mCeModule->mSystem->mPtrSize);
  2835. return CeErrorKind_None;
  2836. //TODO: Add this global variable in there and fixup
  2837. // CeStaticFieldInfo* staticFieldInfoPtr = NULL;
  2838. // if (mCeMachine->mStaticFieldMap.TryGetValue(globalVar->mName, &staticFieldInfoPtr))
  2839. // {
  2840. // CeStaticFieldInfo* staticFieldInfo = staticFieldInfoPtr;
  2841. //
  2842. // int* staticFieldTableIdxPtr = NULL;
  2843. // if (mStaticFieldMap.TryAdd(globalVar, NULL, &staticFieldTableIdxPtr))
  2844. // {
  2845. // CeStaticFieldEntry staticFieldEntry;
  2846. // staticFieldEntry.mTypeId = staticFieldInfo->mFieldInstance->mOwner->mTypeId;
  2847. // staticFieldEntry.mName = globalVar->mName;
  2848. // staticFieldEntry.mSize = globalVar->mType->mSize;
  2849. // *staticFieldTableIdxPtr = (int)mCeFunction->mStaticFieldTable.size();
  2850. // mCeFunction->mStaticFieldTable.Add(staticFieldEntry);
  2851. // }
  2852. //
  2853. // auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
  2854. //
  2855. // Emit(CeOp_GetStaticField);
  2856. // EmitFrameOffset(result);
  2857. // Emit((int32)*staticFieldTableIdxPtr);
  2858. //
  2859. // return result;
  2860. // }
  2861. // return CeErrorKind_GlobalVariable;
  2862. }
  2863. BF_ASSERT(!data.mQueueFixups);
  2864. CeConstStructData gvData;
  2865. auto result = WriteConstant(gvData, globalVar->mInitializer);
  2866. if (result != CeErrorKind_None)
  2867. return result;
  2868. uint8* gvPtr = CeMalloc(gvData.mData.mSize);
  2869. memcpy(gvPtr, gvData.mData.mVals, gvData.mData.mSize);
  2870. auto ptr = data.mData.GrowUninitialized(mCeModule->mSystem->mPtrSize);
  2871. int64 addr64 = (addr_ce)(gvPtr - mMemory.mVals);
  2872. memcpy(ptr, &addr64, mCeModule->mSystem->mPtrSize);
  2873. return CeErrorKind_None;
  2874. }
  2875. else if (auto beFunc = BeValueDynCast<BeFunction>(constVal))
  2876. {
  2877. return CeErrorKind_FunctionPointer;
  2878. }
  2879. else if (auto constStruct = BeValueDynCast<BeStructConstant>(constVal))
  2880. {
  2881. int startOfs = data.mData.mSize;
  2882. if (constStruct->mType->mTypeCode == BeTypeCode_Struct)
  2883. {
  2884. BeStructType* structType = (BeStructType*)constStruct->mType;
  2885. BF_ASSERT(structType->mMembers.size() == constStruct->mMemberValues.size());
  2886. for (int memberIdx = 0; memberIdx < (int)constStruct->mMemberValues.size(); memberIdx++)
  2887. {
  2888. auto& member = structType->mMembers[memberIdx];
  2889. // Do any per-member alignment
  2890. int wantZeroes = member.mByteOffset - (data.mData.mSize - startOfs);
  2891. if (wantZeroes > 0)
  2892. data.mData.Insert(data.mData.size(), (uint8)0, wantZeroes);
  2893. auto result = WriteConstant(data, constStruct->mMemberValues[memberIdx]);
  2894. if (result != CeErrorKind_None)
  2895. return result;
  2896. }
  2897. // Do end padding
  2898. data.mData.Insert(data.mData.size(), (uint8)0, structType->mSize - (data.mData.mSize - startOfs));
  2899. }
  2900. else if (constStruct->mType->mTypeCode == BeTypeCode_SizedArray)
  2901. {
  2902. for (auto& memberVal : constStruct->mMemberValues)
  2903. {
  2904. auto result = WriteConstant(data, memberVal);
  2905. if (result != CeErrorKind_None)
  2906. return result;
  2907. }
  2908. }
  2909. else
  2910. BF_FATAL("Invalid StructConst type");
  2911. }
  2912. else if (auto constStr = BeValueDynCast<BeStringConstant>(constVal))
  2913. {
  2914. data.mData.Insert(data.mData.mSize, (uint8*)constStr->mString.c_str(), (int)constStr->mString.length() + 1);
  2915. }
  2916. else if (auto constCast = BeValueDynCast<BeCastConstant>(constVal))
  2917. {
  2918. auto result = WriteConstant(data, constCast->mTarget);
  2919. if (result != CeErrorKind_None)
  2920. return result;
  2921. }
  2922. else if (auto constGep = BeValueDynCast<BeGEPConstant>(constVal))
  2923. {
  2924. if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constGep->mTarget))
  2925. {
  2926. BF_ASSERT(constGep->mIdx0 == 0);
  2927. int64 dataOfs = 0;
  2928. if (globalVar->mType->mTypeCode == BeTypeCode_Struct)
  2929. {
  2930. auto structType = (BeStructType*)globalVar->mType;
  2931. dataOfs = structType->mMembers[constGep->mIdx1].mByteOffset;
  2932. }
  2933. else if (globalVar->mType->mTypeCode == BeTypeCode_SizedArray)
  2934. {
  2935. auto arrayType = (BeSizedArrayType*)globalVar->mType;
  2936. dataOfs = arrayType->mElementType->mSize * constGep->mIdx1;
  2937. }
  2938. else
  2939. {
  2940. BF_FATAL("Invalid GEP");
  2941. }
  2942. addr_ce addr = -1;
  2943. if (globalVar->mName.StartsWith("__bfStrData"))
  2944. {
  2945. int stringId = atoi(globalVar->mName.c_str() + 11);
  2946. if (data.mQueueFixups)
  2947. {
  2948. addr = 0;
  2949. CeConstStructFixup fixup;
  2950. fixup.mKind = CeConstStructFixup::Kind_StringCharPtr;
  2951. fixup.mValue = stringId;
  2952. fixup.mOffset = (int)data.mData.mSize;
  2953. data.mFixups.Add(fixup);
  2954. }
  2955. else
  2956. {
  2957. addr_ce stringAddr = GetString(stringId);
  2958. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCeModule->mCompiler->mStringTypeDef, BfPopulateType_Data);
  2959. addr = stringAddr + stringTypeInst->mInstSize;
  2960. }
  2961. }
  2962. if (addr != -1)
  2963. {
  2964. auto ptr = data.mData.GrowUninitialized(mCeModule->mSystem->mPtrSize);
  2965. int64 addr64 = addr + dataOfs;
  2966. memcpy(ptr, &addr64, mCeModule->mSystem->mPtrSize);
  2967. return CeErrorKind_None;
  2968. }
  2969. return CeErrorKind_GlobalVariable;
  2970. // auto sym = GetSymbol(globalVar);
  2971. //
  2972. // BeMCRelocation reloc;
  2973. // reloc.mKind = BeMCRelocationKind_ADDR64;
  2974. // reloc.mOffset = sect.mData.GetPos();
  2975. // reloc.mSymTableIdx = sym->mIdx;
  2976. // sect.mRelocs.push_back(reloc);
  2977. // sect.mData.Write((int64)dataOfs);
  2978. }
  2979. else
  2980. {
  2981. BF_FATAL("Invalid GEPConstant");
  2982. }
  2983. }
  2984. /*else if ((beType->IsPointer()) && (constVal->mTarget != NULL))
  2985. {
  2986. auto result = WriteConstant(arr, constVal->mTarget);
  2987. if (result != CeErrorKind_None)
  2988. return result;
  2989. }
  2990. else if (beType->IsComposite())
  2991. {
  2992. BF_ASSERT(constVal->mInt64 == 0);
  2993. int64 zero = 0;
  2994. int sizeLeft = beType->mSize;
  2995. while (sizeLeft > 0)
  2996. {
  2997. int writeSize = BF_MIN(sizeLeft, 8);
  2998. auto ptr = arr.GrowUninitialized(writeSize);
  2999. memset(ptr, 0, writeSize);
  3000. sizeLeft -= writeSize;
  3001. }
  3002. }*/
  3003. else if (BeValueDynCastExact<BeConstant>(constVal) != NULL)
  3004. {
  3005. if (constVal->mType->IsStruct())
  3006. {
  3007. if (constVal->mType->mSize > 0)
  3008. {
  3009. auto ptr = data.mData.GrowUninitialized(constVal->mType->mSize);
  3010. memset(ptr, 0, constVal->mType->mSize);
  3011. }
  3012. }
  3013. else
  3014. {
  3015. auto ptr = data.mData.GrowUninitialized(beType->mSize);
  3016. memcpy(ptr, &constVal->mInt64, beType->mSize);
  3017. }
  3018. }
  3019. else
  3020. return CeErrorKind_Error;
  3021. return CeErrorKind_None;
  3022. }
  3023. #define CE_CREATECONST_CHECKPTR(PTR, SIZE) \
  3024. if ((((uint8*)(PTR) - memStart) - 0x10000) + (SIZE) > (memSize - 0x10000)) \
  3025. { \
  3026. Fail("Access violation creating constant result"); \
  3027. return BfIRValue(); \
  3028. }
  3029. BfIRValue CeMachine::CreateConstant(BfModule* module, uint8* ptr, BfType* bfType, BfType** outType)
  3030. {
  3031. BfIRBuilder* irBuilder = module->mBfIRBuilder;
  3032. uint8* memStart = mMemory.mVals;
  3033. int memSize = mMemory.mSize;
  3034. if (bfType->IsPrimitiveType())
  3035. {
  3036. auto primType = (BfPrimitiveType*)bfType;
  3037. auto typeCode = primType->mTypeDef->mTypeCode;
  3038. if (typeCode == BfTypeCode_IntPtr)
  3039. typeCode = (mCeModule->mCompiler->mSystem->mPtrSize == 4) ? BfTypeCode_Int32 : BfTypeCode_Int64;
  3040. else if (typeCode == BfTypeCode_UIntPtr)
  3041. typeCode = (mCeModule->mCompiler->mSystem->mPtrSize == 4) ? BfTypeCode_UInt32 : BfTypeCode_UInt64;
  3042. switch (typeCode)
  3043. {
  3044. case BfTypeCode_Int8:
  3045. CE_CREATECONST_CHECKPTR(ptr, sizeof(int8));
  3046. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(int8*)ptr);
  3047. case BfTypeCode_UInt8:
  3048. CE_CREATECONST_CHECKPTR(ptr, sizeof(uint8));
  3049. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(uint8*)ptr);
  3050. case BfTypeCode_Int16:
  3051. CE_CREATECONST_CHECKPTR(ptr, sizeof(int16));
  3052. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(int16*)ptr);
  3053. case BfTypeCode_UInt16:
  3054. CE_CREATECONST_CHECKPTR(ptr, sizeof(uint16));
  3055. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(uint16*)ptr);
  3056. case BfTypeCode_Int32:
  3057. CE_CREATECONST_CHECKPTR(ptr, sizeof(int32));
  3058. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(int32*)ptr);
  3059. case BfTypeCode_UInt32:
  3060. CE_CREATECONST_CHECKPTR(ptr, sizeof(uint32));
  3061. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, (uint64)*(uint32*)ptr);
  3062. case BfTypeCode_Int64:
  3063. case BfTypeCode_UInt64:
  3064. CE_CREATECONST_CHECKPTR(ptr, sizeof(int64));
  3065. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(uint64*)ptr);
  3066. case BfTypeCode_Float:
  3067. CE_CREATECONST_CHECKPTR(ptr, sizeof(float));
  3068. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(float*)ptr);
  3069. case BfTypeCode_Double:
  3070. CE_CREATECONST_CHECKPTR(ptr, sizeof(double));
  3071. return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(double*)ptr);
  3072. }
  3073. return BfIRValue();
  3074. }
  3075. if (bfType->IsTypeInstance())
  3076. {
  3077. auto typeInst = bfType->ToTypeInstance();
  3078. uint8* instData = ptr;
  3079. // if ((typeInst->IsObject()) && (!isBaseType))
  3080. // {
  3081. // CE_CREATECONST_CHECKPTR(ptr, sizeof(addr_ce));
  3082. // instData = mMemory.mVals + *(addr_ce*)ptr;
  3083. // CE_CREATECONST_CHECKPTR(instData, typeInst->mInstSize);
  3084. // }
  3085. if (typeInst->IsInstanceOf(mCompiler->mStringTypeDef))
  3086. {
  3087. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCompiler->mStringTypeDef, BfPopulateType_Data);
  3088. module->PopulateType(stringTypeInst);
  3089. auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize;
  3090. auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset;
  3091. auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset;
  3092. auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset;
  3093. int32 lenVal = *(int32*)(instData + lenOffset);
  3094. char* charPtr = NULL;
  3095. if (lenByteCount == 4)
  3096. {
  3097. int32 allocSizeVal = *(int32*)(instData + allocSizeOffset);
  3098. if ((allocSizeVal & 0x40000000) != 0)
  3099. {
  3100. int32 ptrVal = *(int32*)(instData + ptrOffset);
  3101. charPtr = (char*)(ptrVal + memStart);
  3102. }
  3103. else
  3104. {
  3105. charPtr = (char*)(instData + ptrOffset);
  3106. }
  3107. }
  3108. CE_CREATECONST_CHECKPTR(charPtr, lenVal);
  3109. String str(charPtr, lenVal);
  3110. return module->GetStringObjectValue(str);
  3111. }
  3112. SizedArray<BfIRValue, 8> fieldVals;
  3113. if (typeInst->IsInstanceOf(mCeModule->mCompiler->mSpanTypeDef))
  3114. {
  3115. if ((outType != NULL) && ((mCurExpectingType == NULL) || (mCurExpectingType->IsSizedArray())))
  3116. {
  3117. module->PopulateType(typeInst);
  3118. auto ptrOffset = typeInst->mFieldInstances[0].mDataOffset;
  3119. auto lenOffset = typeInst->mFieldInstances[1].mDataOffset;
  3120. BfType* elemType = typeInst->GetUnderlyingType();
  3121. CE_CREATECONST_CHECKPTR(instData, mCeModule->mSystem->mPtrSize * 2);
  3122. addr_ce addr = *(addr_ce*)(instData + ptrOffset);
  3123. int32 lenVal = *(int32*)(instData + lenOffset);
  3124. CE_CREATECONST_CHECKPTR(memStart + addr, lenVal);
  3125. for (int i = 0; i < lenVal; i++)
  3126. {
  3127. auto result = CreateConstant(module, memStart + addr + i * elemType->GetStride(), elemType);
  3128. if (!result)
  3129. return BfIRValue();
  3130. fieldVals.Add(result);
  3131. }
  3132. auto irArrayType = irBuilder->GetSizedArrayType(irBuilder->MapType(elemType, BfIRPopulateType_Full), lenVal);
  3133. auto instResult = irBuilder->CreateConstAgg(irArrayType, fieldVals);
  3134. *outType = module->CreateSizedArrayType(elemType, lenVal);
  3135. return instResult;
  3136. }
  3137. Fail(StrFormat("Span return type '%s' must be received by a sized array", module->TypeToString(typeInst).c_str()));
  3138. return BfIRValue();
  3139. }
  3140. if (typeInst->IsObjectOrInterface())
  3141. {
  3142. Fail(StrFormat("Reference type '%s' return value not allowed", module->TypeToString(typeInst).c_str()));
  3143. return BfIRValue();
  3144. }
  3145. if (typeInst->IsPointer())
  3146. {
  3147. Fail(StrFormat("Pointer type '%s' return value not allowed", module->TypeToString(typeInst).c_str()));
  3148. return BfIRValue();
  3149. }
  3150. if (typeInst->mBaseType != NULL)
  3151. {
  3152. auto result = CreateConstant(module, instData, typeInst->mBaseType);
  3153. if (!result)
  3154. return BfIRValue();
  3155. fieldVals.Add(result);
  3156. }
  3157. for (int fieldIdx = 0; fieldIdx < typeInst->mFieldInstances.size(); fieldIdx++)
  3158. {
  3159. auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
  3160. if (fieldInstance.mDataOffset < 0)
  3161. continue;
  3162. if ((fieldInstance.mDataOffset == 0) && (typeInst == mCompiler->mContext->mBfObjectType))
  3163. {
  3164. auto vdataPtr = module->GetClassVDataPtr(typeInst);
  3165. if (fieldInstance.mResolvedType->IsInteger())
  3166. fieldVals.Add(irBuilder->CreatePtrToInt(vdataPtr, ((BfPrimitiveType*)fieldInstance.mResolvedType)->mTypeDef->mTypeCode));
  3167. else
  3168. fieldVals.Add(vdataPtr);
  3169. continue;
  3170. }
  3171. auto result = CreateConstant(module, instData + fieldInstance.mDataOffset, fieldInstance.mResolvedType);
  3172. if (!result)
  3173. return BfIRValue();
  3174. if (fieldInstance.mDataIdx == fieldVals.mSize)
  3175. fieldVals.Add(result);
  3176. else
  3177. {
  3178. while (fieldInstance.mDataIdx >= fieldVals.mSize)
  3179. fieldVals.Add(BfIRValue());
  3180. fieldVals[fieldInstance.mDataIdx] = result;
  3181. }
  3182. }
  3183. auto instResult = irBuilder->CreateConstAgg(irBuilder->MapTypeInst(typeInst, BfIRPopulateType_Full), fieldVals);
  3184. return instResult;
  3185. }
  3186. return BfIRValue();
  3187. }
  3188. #define CE_CHECKSTACK() \
  3189. if (stackPtr < memStart) \
  3190. { \
  3191. _Fail("Stack overflow"); \
  3192. return false; \
  3193. }
  3194. #define CE_CHECKALLOC(SIZE) \
  3195. if ((uintptr)memSize + (uintptr)SIZE > BF_CE_MAX_MEMORY) \
  3196. { \
  3197. _Fail("Maximum memory size exceeded"); \
  3198. }
  3199. // This check will fail for addresses < 64K (null pointer), or out-of-bounds
  3200. #define CE_CHECKSIZE(SIZE) \
  3201. if ((SIZE) < 0) \
  3202. { \
  3203. _Fail("Invalid memory size"); \
  3204. return false; \
  3205. }
  3206. #define CE_CHECKADDR(ADDR, SIZE) \
  3207. if (((ADDR) - 0x10000) + (SIZE) > (memSize - 0x10000)) \
  3208. { \
  3209. _Fail("Access violation"); \
  3210. return false; \
  3211. }
  3212. #define CE_GETINST(T) *((T*)(instPtr += sizeof(T)) - 1)
  3213. #define CE_GETFRAME(T) *(T*)(framePtr + *((int32*)(instPtr += sizeof(int32)) - 1))
  3214. #define CEOP_BIN(OP, T) \
  3215. { \
  3216. auto& result = CE_GETFRAME(T); \
  3217. auto lhs = CE_GETFRAME(T); \
  3218. auto rhs = CE_GETFRAME(T); \
  3219. result = lhs OP rhs; \
  3220. }
  3221. #define CEOP_BIN_DIV(OP, T) \
  3222. { \
  3223. auto& result = CE_GETFRAME(T); \
  3224. auto lhs = CE_GETFRAME(T); \
  3225. auto rhs = CE_GETFRAME(T); \
  3226. if (rhs == 0) \
  3227. { \
  3228. _Fail("Division by zero"); \
  3229. return false; \
  3230. } \
  3231. result = lhs OP rhs; \
  3232. }
  3233. #define CEOP_BIN2(OP, TLHS, TRHS) \
  3234. { \
  3235. auto& result = CE_GETFRAME(TLHS); \
  3236. auto lhs = CE_GETFRAME(TLHS); \
  3237. auto rhs = CE_GETFRAME(TRHS); \
  3238. result = lhs OP rhs; \
  3239. }
  3240. #define CEOP_BIN_CONST(OP, T) \
  3241. { \
  3242. auto& result = CE_GETFRAME(T); \
  3243. auto lhs = CE_GETFRAME(T); \
  3244. auto rhs = CE_GETINST(T); \
  3245. result = lhs OP rhs; \
  3246. }
  3247. #define CEOP_UNARY(OP, T) \
  3248. { \
  3249. auto& result = CE_GETFRAME(T); \
  3250. auto val = CE_GETFRAME(T); \
  3251. result = OP val; \
  3252. }
  3253. #define CEOP_CMP(OP, T) \
  3254. { \
  3255. auto& result = CE_GETFRAME(bool); \
  3256. auto lhs = CE_GETFRAME(T); \
  3257. auto rhs = CE_GETFRAME(T); \
  3258. result = lhs OP rhs; \
  3259. }
  3260. #define CE_CAST(TFROM, TTO) \
  3261. { \
  3262. auto& result = CE_GETFRAME(TTO); \
  3263. auto val = CE_GETFRAME(TFROM); \
  3264. result = (TTO)val; \
  3265. }
  3266. #define CE_LOAD(T) \
  3267. { \
  3268. auto& result = CE_GETFRAME(T); \
  3269. auto ceAddr = CE_GETFRAME(addr_ce); \
  3270. CE_CHECKADDR(ceAddr, sizeof(T)); \
  3271. result = *(T*)(memStart + ceAddr); \
  3272. }
  3273. #define CE_STORE(T) \
  3274. { \
  3275. auto val = CE_GETFRAME(T); \
  3276. auto ceAddr = CE_GETFRAME(addr_ce); \
  3277. CE_CHECKADDR(ceAddr, sizeof(T)); \
  3278. *(T*)(memStart + ceAddr) = val; \
  3279. }
  3280. #define CEOP_MOVE(T) \
  3281. { \
  3282. auto val = CE_GETFRAME(T); \
  3283. auto& ptr = CE_GETFRAME(T); \
  3284. ptr = val; \
  3285. }
  3286. #define CEOP_PUSH(T) \
  3287. { \
  3288. stackPtr -= sizeof(T); \
  3289. auto val = CE_GETFRAME(T); \
  3290. *((T*)stackPtr) = val; \
  3291. CE_CHECKSTACK(); \
  3292. }
  3293. #define CEOP_POP(T) \
  3294. { \
  3295. auto& result = CE_GETFRAME(T); \
  3296. result = *((T*)stackPtr); \
  3297. stackPtr += sizeof(T); \
  3298. }
  3299. #define CE_CALL(CEFUNC) \
  3300. if (CEFUNC == NULL) \
  3301. { \
  3302. _Fail("Unable to locate function entry"); \
  3303. return false; \
  3304. } \
  3305. mCallStack.Add(_GetCurFrame()); \
  3306. ceFunction = CEFUNC; \
  3307. framePtr = stackPtr; \
  3308. stackPtr -= ceFunction->mFrameSize; \
  3309. instPtr = &ceFunction->mCode[0]; \
  3310. CE_CHECKSTACK();
  3311. bool CeMachine::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* startFramePtr)
  3312. {
  3313. mExecuteId++;
  3314. CeFunction* ceFunction = startFunction;
  3315. uint8* memStart = &mMemory[0];
  3316. int memSize = mMemory.mSize;
  3317. uint8* instPtr = (ceFunction->mCode.IsEmpty()) ? NULL : &ceFunction->mCode[0];
  3318. uint8* stackPtr = startStackPtr;
  3319. uint8* framePtr = startFramePtr;
  3320. volatile bool* cancelPtr = &mCompiler->mCanceling;
  3321. auto _GetCurFrame = [&]()
  3322. {
  3323. CeFrame ceFrame;
  3324. ceFrame.mFunction = ceFunction;
  3325. ceFrame.mFrameAddr = framePtr - memStart;
  3326. ceFrame.mStackAddr = stackPtr - memStart;
  3327. ceFrame.mInstPtr = instPtr;
  3328. return ceFrame;
  3329. };
  3330. auto _FixVariables = [&]()
  3331. {
  3332. memSize = mMemory.mSize;
  3333. intptr memOffset = &mMemory[0] - memStart;
  3334. if (memOffset == 0)
  3335. return;
  3336. memStart += memOffset;
  3337. stackPtr += memOffset;
  3338. framePtr += memOffset;
  3339. };
  3340. auto _Fail = [&](const StringImpl& error)
  3341. {
  3342. Fail(_GetCurFrame(), error);
  3343. };
  3344. auto _CheckFunction = [&](CeFunction* checkFunction, bool& handled)
  3345. {
  3346. if (checkFunction == NULL)
  3347. {
  3348. Fail(_GetCurFrame(), "Const method not available");
  3349. return false;
  3350. }
  3351. if (checkFunction->mFunctionKind != CeFunctionKind_Normal)
  3352. {
  3353. if (checkFunction->mFunctionKind == CeFunctionKind_OOB)
  3354. {
  3355. Fail(_GetCurFrame(), "Array out of bounds");
  3356. return false;
  3357. }
  3358. else if (checkFunction->mFunctionKind == CeFunctionKind_Malloc)
  3359. {
  3360. int32 size = *(int32*)((uint8*)stackPtr + 4);
  3361. CE_CHECKALLOC(size);
  3362. uint8* ptr = CeMalloc(size);
  3363. addr_ce& result = *(addr_ce*)((uint8*)stackPtr + 0);
  3364. result = (addr_ce)(ptr - memStart);
  3365. handled = true;
  3366. return true;
  3367. }
  3368. else if (checkFunction->mFunctionKind == CeFunctionKind_Free)
  3369. {
  3370. addr_ce freeAddr = *(addr_ce*)((uint8*)stackPtr + 4);
  3371. bool success = CeFree(freeAddr);
  3372. if (!success)
  3373. _Fail("Invalid heap address");
  3374. handled = true;
  3375. return true;
  3376. }
  3377. else if (checkFunction->mFunctionKind == CeFunctionKind_FatalError)
  3378. {
  3379. int32 strInstAddr = *(int32*)((uint8*)stackPtr + 0);
  3380. CE_CHECKADDR(strInstAddr, 0);
  3381. BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeModule->ResolveTypeDef(mCompiler->mStringTypeDef, BfPopulateType_Data);
  3382. auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize;
  3383. auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset;
  3384. auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset;
  3385. auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset;
  3386. uint8* strInst = (uint8*)(strInstAddr + memStart);
  3387. int32 lenVal = *(int32*)(strInst + lenOffset);
  3388. char* charPtr = NULL;
  3389. if (lenByteCount == 4)
  3390. {
  3391. int32 allocSizeVal = *(int32*)(strInst + allocSizeOffset);
  3392. if ((allocSizeVal & 0x40000000) != 0)
  3393. {
  3394. int32 ptrVal = *(int32*)(strInst + ptrOffset);
  3395. charPtr = (char*)(ptrVal + memStart);
  3396. }
  3397. else
  3398. {
  3399. charPtr = (char*)(strInst + ptrOffset);
  3400. }
  3401. }
  3402. int32 ptrVal = *(int32*)(strInst + ptrOffset);
  3403. String error = "Fatal Error: ";
  3404. if (charPtr != NULL)
  3405. error.Insert(error.length(), charPtr, lenVal);
  3406. _Fail(error);
  3407. return false;
  3408. }
  3409. else if (checkFunction->mFunctionKind == CeFunctionKind_DebugWrite)
  3410. {
  3411. int32 ptrVal = *(int32*)((uint8*)stackPtr + 0);
  3412. auto size = *(int32*)(stackPtr + mCeModule->mSystem->mPtrSize);
  3413. CE_CHECKADDR(ptrVal, size);
  3414. char* strPtr = (char*)(ptrVal + memStart);
  3415. String str;
  3416. str.Insert(0, strPtr, size);
  3417. OutputDebugStr(str);
  3418. handled = true;
  3419. return true;
  3420. }
  3421. else if (checkFunction->mFunctionKind == CeFunctionKind_DebugWrite_Int)
  3422. {
  3423. int32 intVal = *(int32*)((uint8*)stackPtr + 0);
  3424. OutputDebugStrF("Debug Val: %d\n", intVal);
  3425. handled = true;
  3426. return true;
  3427. }
  3428. else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectType)
  3429. {
  3430. addr_ce objAddr = *(addr_ce*)((uint8*)stackPtr + mCeModule->mSystem->mPtrSize);
  3431. CE_CHECKADDR(addr_ce, 4);
  3432. int32 typeId = *(int32*)(objAddr + memStart);
  3433. auto reflectType = GetReflectType(typeId);
  3434. _FixVariables();
  3435. *(addr_ce*)(stackPtr + 0) = reflectType;
  3436. handled = true;
  3437. return true;
  3438. }
  3439. else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectTypeById)
  3440. {
  3441. int32 typeId = *(int32*)((uint8*)stackPtr + mCeModule->mSystem->mPtrSize);
  3442. auto reflectType = GetReflectType(typeId);
  3443. _FixVariables();
  3444. *(addr_ce*)(stackPtr + 0) = reflectType;
  3445. handled = true;
  3446. return true;
  3447. }
  3448. else if (checkFunction->mFunctionKind == CeFunctionKind_Sleep)
  3449. {
  3450. int32 sleepMS = *(int32*)((uint8*)stackPtr);
  3451. while (sleepMS > 0)
  3452. {
  3453. if (*cancelPtr)
  3454. break;
  3455. if (sleepMS > 200)
  3456. {
  3457. Sleep(200);
  3458. sleepMS -= 200;
  3459. continue;
  3460. }
  3461. Sleep(sleepMS);
  3462. break;
  3463. }
  3464. handled = true;
  3465. return true;
  3466. }
  3467. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_ToLower)
  3468. {
  3469. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3470. int32 val = *(int32*)((uint8*)stackPtr + 4);
  3471. result = utf8proc_tolower(val);
  3472. handled = true;
  3473. return true;
  3474. }
  3475. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_ToUpper)
  3476. {
  3477. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3478. int32 val = *(int32*)((uint8*)stackPtr + 4);
  3479. result = utf8proc_toupper(val);
  3480. handled = true;
  3481. return true;
  3482. }
  3483. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsLower)
  3484. {
  3485. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3486. int32 val = *(int32*)((uint8*)stackPtr + 1);
  3487. result = utf8proc_category(val) == UTF8PROC_CATEGORY_LL;
  3488. handled = true;
  3489. return true;
  3490. }
  3491. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsUpper)
  3492. {
  3493. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3494. int32 val = *(int32*)((uint8*)stackPtr + 1);
  3495. result = utf8proc_category(val) == UTF8PROC_CATEGORY_LU;
  3496. handled = true;
  3497. return true;
  3498. }
  3499. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsWhiteSpace_EX)
  3500. {
  3501. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3502. int32 val = *(int32*)((uint8*)stackPtr + 1);
  3503. auto cat = utf8proc_category(val);
  3504. result = (cat == UTF8PROC_CATEGORY_ZS) || (cat == UTF8PROC_CATEGORY_ZL) || (cat == UTF8PROC_CATEGORY_ZP);
  3505. handled = true;
  3506. return true;
  3507. }
  3508. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsLetterOrDigit)
  3509. {
  3510. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3511. int32 val = *(int32*)((uint8*)stackPtr + 1);
  3512. auto cat = utf8proc_category(val);
  3513. switch (cat)
  3514. {
  3515. case UTF8PROC_CATEGORY_LU:
  3516. case UTF8PROC_CATEGORY_LL:
  3517. case UTF8PROC_CATEGORY_LT:
  3518. case UTF8PROC_CATEGORY_LM:
  3519. case UTF8PROC_CATEGORY_LO:
  3520. case UTF8PROC_CATEGORY_ND:
  3521. case UTF8PROC_CATEGORY_NL:
  3522. case UTF8PROC_CATEGORY_NO:
  3523. result = true;
  3524. break;
  3525. default:
  3526. result = false;
  3527. }
  3528. handled = true;
  3529. return true;
  3530. }
  3531. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsLetter)
  3532. {
  3533. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3534. int32 val = *(int32*)((uint8*)stackPtr + 1);
  3535. auto cat = utf8proc_category(val);
  3536. switch (cat)
  3537. {
  3538. case UTF8PROC_CATEGORY_LU:
  3539. case UTF8PROC_CATEGORY_LL:
  3540. case UTF8PROC_CATEGORY_LT:
  3541. case UTF8PROC_CATEGORY_LM:
  3542. case UTF8PROC_CATEGORY_LO:
  3543. result = true;
  3544. break;
  3545. default:
  3546. result = false;
  3547. }
  3548. handled = true;
  3549. return true;
  3550. }
  3551. else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsNumber)
  3552. {
  3553. int32& result = *(int32*)((uint8*)stackPtr + 0);
  3554. int32 val = *(int32*)((uint8*)stackPtr + 1);
  3555. auto cat = utf8proc_category(val);
  3556. switch (cat)
  3557. {
  3558. case UTF8PROC_CATEGORY_ND:
  3559. case UTF8PROC_CATEGORY_NL:
  3560. case UTF8PROC_CATEGORY_NO:
  3561. result = true;
  3562. break;
  3563. default:
  3564. result = false;
  3565. }
  3566. handled = true;
  3567. return true;
  3568. }
  3569. Fail(_GetCurFrame(), StrFormat("Unable to invoke extern method '%s'", mCeModule->MethodToString(checkFunction->mMethodInstance).c_str()));
  3570. return false;
  3571. }
  3572. if (!checkFunction->mFailed)
  3573. return true;
  3574. auto error = Fail(_GetCurFrame(), "Method call failed");
  3575. if ((error != NULL) && (!checkFunction->mGenError.IsEmpty()))
  3576. mCompiler->mPassInstance->MoreInfo("Const Method Generation Error: " + checkFunction->mGenError);
  3577. return false;
  3578. };
  3579. //
  3580. {
  3581. bool handled = false;
  3582. if (!_CheckFunction(ceFunction, handled))
  3583. return false;
  3584. if (handled)
  3585. return true;
  3586. }
  3587. int callCount = 0;
  3588. int instIdx = 0;
  3589. while (true)
  3590. {
  3591. if (*cancelPtr)
  3592. {
  3593. _Fail("Compilation cancelled");
  3594. return false;
  3595. }
  3596. ++instIdx;
  3597. CeOp op = CE_GETINST(CeOp);
  3598. switch (op)
  3599. {
  3600. case CeOp_Ret:
  3601. {
  3602. if (mCallStack.mSize == 0)
  3603. return true;
  3604. auto& ceFrame = mCallStack.back();
  3605. ceFunction = ceFrame.mFunction;
  3606. instPtr = ceFrame.mInstPtr;
  3607. stackPtr = memStart + ceFrame.mStackAddr;
  3608. framePtr = memStart + ceFrame.mFrameAddr;
  3609. mCallStack.pop_back();
  3610. }
  3611. break;
  3612. case CeOp_Jmp:
  3613. {
  3614. auto relOfs = CE_GETINST(int32);
  3615. instPtr += relOfs;
  3616. }
  3617. break;
  3618. case CeOp_JmpIf:
  3619. {
  3620. auto relOfs = CE_GETINST(int32);
  3621. bool cond = CE_GETFRAME(bool);
  3622. if (cond)
  3623. instPtr += relOfs - 4;
  3624. }
  3625. break;
  3626. case CeOp_JmpIfNot:
  3627. {
  3628. auto relOfs = CE_GETINST(int32);
  3629. bool cond = CE_GETFRAME(bool);
  3630. if (!cond)
  3631. instPtr += relOfs - 4;
  3632. }
  3633. break;
  3634. case CeOp_Error:
  3635. {
  3636. auto errorKind = (CeErrorKind)CE_GETINST(int32);
  3637. switch (errorKind)
  3638. {
  3639. case CeErrorKind_GlobalVariable:
  3640. _Fail("Global variable access not allowed");
  3641. break;
  3642. case CeErrorKind_FunctionPointer:
  3643. _Fail("Function pointer calls not allowed");
  3644. break;
  3645. case CeErrorKind_Intrinsic:
  3646. _Fail("Intrinsic not allowed");
  3647. break;
  3648. default:
  3649. _Fail("Operation not allowed");
  3650. break;
  3651. }
  3652. }
  3653. break;
  3654. case CeOp_DynamicCastCheck:
  3655. {
  3656. auto& result = CE_GETFRAME(uint32);
  3657. auto valueAddr = CE_GETFRAME(addr_ce);
  3658. int32 ifaceId = CE_GETINST(int32);
  3659. if (valueAddr == 0)
  3660. {
  3661. result = 0;
  3662. }
  3663. else
  3664. {
  3665. CE_CHECKADDR(valueAddr, sizeof(int32));
  3666. auto ifaceType = GetBfType(ifaceId);
  3667. int32 objTypeId = *(int32*)(memStart + valueAddr);
  3668. auto valueType = GetBfType(objTypeId);
  3669. if ((ifaceType == NULL) || (valueType == NULL))
  3670. {
  3671. _Fail("Invalid type");
  3672. return false;
  3673. }
  3674. if (mCeModule->TypeIsSubTypeOf(valueType->ToTypeInstance(), ifaceType->ToTypeInstance(), false))
  3675. result = valueAddr;
  3676. else
  3677. result = 0;
  3678. }
  3679. }
  3680. break;
  3681. case CeOp_GetReflectType:
  3682. {
  3683. auto frameOfs = CE_GETINST(int32);
  3684. int32 typeId = CE_GETINST(int32);
  3685. auto reflectType = GetReflectType(typeId);
  3686. _FixVariables();
  3687. *(addr_ce*)(framePtr + frameOfs) = reflectType;
  3688. }
  3689. break;
  3690. case CeOp_GetString:
  3691. {
  3692. auto frameOfs = CE_GETINST(int32);
  3693. auto stringTableIdx = CE_GETINST(int32);
  3694. auto& ceStringEntry = ceFunction->mStringTable[stringTableIdx];
  3695. if (ceStringEntry.mBindExecuteId != mExecuteId)
  3696. {
  3697. ceStringEntry.mStringAddr = GetString(ceStringEntry.mStringId);
  3698. _FixVariables();
  3699. ceStringEntry.mBindExecuteId = mExecuteId;
  3700. }
  3701. *(addr_ce*)(framePtr + frameOfs) = ceStringEntry.mStringAddr;
  3702. }
  3703. break;
  3704. case CeOp_Malloc:
  3705. {
  3706. auto frameOfs = CE_GETINST(int32);
  3707. int32 size = CE_GETFRAME(int32);
  3708. CE_CHECKALLOC(size);
  3709. uint8* mem = CeMalloc(size);
  3710. _FixVariables();
  3711. *(addr_ce*)(framePtr + frameOfs) = mem - memStart;
  3712. }
  3713. break;
  3714. case CeOp_Free:
  3715. {
  3716. auto freeAddr = CE_GETFRAME(addr_ce);
  3717. bool success = CeFree(freeAddr);
  3718. if (!success)
  3719. _Fail("Invalid heap address");
  3720. }
  3721. break;
  3722. case CeOp_MemSet:
  3723. {
  3724. auto destAddr = CE_GETFRAME(addr_ce);
  3725. uint8 setValue = CE_GETFRAME(uint8);
  3726. int32 setSize = CE_GETFRAME(int32);
  3727. CE_CHECKSIZE(setSize);
  3728. CE_CHECKADDR(destAddr, setSize);
  3729. memset(memStart + destAddr, setValue, setSize);
  3730. }
  3731. break;
  3732. case CeOp_MemSet_Const:
  3733. {
  3734. auto destAddr = CE_GETFRAME(addr_ce);
  3735. uint8 setValue = CE_GETINST(uint8);
  3736. int32 setSize = CE_GETINST(int32);
  3737. CE_CHECKSIZE(setSize);
  3738. CE_CHECKADDR(destAddr, setSize);
  3739. memset(memStart + destAddr, setValue, setSize);
  3740. }
  3741. break;
  3742. case CeOp_MemCpy:
  3743. {
  3744. auto destAddr = CE_GETFRAME(addr_ce);
  3745. auto srcAddr = CE_GETFRAME(addr_ce);
  3746. int32 size = CE_GETFRAME(int32);
  3747. CE_CHECKSIZE(size);
  3748. CE_CHECKADDR(srcAddr, size);
  3749. CE_CHECKADDR(destAddr, size);
  3750. memcpy(memStart + destAddr, memStart + srcAddr, size);
  3751. }
  3752. break;
  3753. case CeOp_FrameAddr_64:
  3754. {
  3755. auto& result = CE_GETFRAME(int64);
  3756. auto addr = &CE_GETFRAME(uint8);
  3757. result = addr - memStart;
  3758. }
  3759. break;
  3760. case CeOp_FrameAddrOfs_32:
  3761. {
  3762. auto& result = CE_GETFRAME(int32);
  3763. auto addr = &CE_GETFRAME(uint8);
  3764. int32 ofs = CE_GETINST(int32);
  3765. result = (int32)(addr - memStart + ofs);
  3766. }
  3767. break;
  3768. case CeOp_ConstData:
  3769. {
  3770. auto frameOfs = CE_GETINST(int32);
  3771. int32 constIdx = CE_GETINST(int32);
  3772. auto& constEntry = ceFunction->mConstStructTable[constIdx];
  3773. if (constEntry.mBindExecuteId != mExecuteId)
  3774. {
  3775. PrepareConstStructEntry(constEntry);
  3776. _FixVariables();
  3777. }
  3778. auto& buff = (constEntry.mFixedData.mSize > 0) ? constEntry.mFixedData : constEntry.mData;
  3779. memcpy(framePtr + frameOfs, buff.mVals, buff.mSize);
  3780. }
  3781. break;
  3782. case CeOp_ConstDataRef:
  3783. {
  3784. auto frameOfs = CE_GETINST(int32);
  3785. int32 constIdx = CE_GETINST(int32);
  3786. auto& constEntry = ceFunction->mConstStructTable[constIdx];
  3787. if (constEntry.mBindExecuteId != mExecuteId)
  3788. {
  3789. PrepareConstStructEntry(constEntry);
  3790. _FixVariables();
  3791. auto& buff = (constEntry.mFixedData.mSize > 0) ? constEntry.mFixedData : constEntry.mData;
  3792. addr_ce* constAddrPtr = NULL;
  3793. if (mConstDataMap.TryAdd(constEntry.mHash, NULL, &constAddrPtr))
  3794. {
  3795. uint8* data = CeMalloc(buff.mSize);
  3796. _FixVariables();
  3797. memcpy(data, &buff[0], buff.mSize);
  3798. *constAddrPtr = (addr_ce)(data - memStart);
  3799. }
  3800. constEntry.mAddr = *constAddrPtr;
  3801. constEntry.mBindExecuteId = mExecuteId;
  3802. }
  3803. *(addr_ce*)(framePtr + frameOfs) = constEntry.mAddr;
  3804. }
  3805. break;
  3806. case CeOp_Zero:
  3807. {
  3808. auto resultPtr = &CE_GETFRAME(uint8);
  3809. int32 constSize = CE_GETINST(int32);
  3810. memset(resultPtr, 0, constSize);
  3811. }
  3812. break;
  3813. case CeOp_Const_8:
  3814. {
  3815. auto& result = CE_GETFRAME(int8);
  3816. result = CE_GETINST(int8);
  3817. }
  3818. break;
  3819. case CeOp_Const_16:
  3820. {
  3821. auto& result = CE_GETFRAME(int16);
  3822. result = CE_GETINST(int16);
  3823. }
  3824. break;
  3825. case CeOp_Const_32:
  3826. {
  3827. auto& result = CE_GETFRAME(int32);
  3828. result = CE_GETINST(int32);
  3829. }
  3830. break;
  3831. case CeOp_Const_64:
  3832. {
  3833. auto& result = CE_GETFRAME(int64);
  3834. result = CE_GETINST(int64);
  3835. }
  3836. break;
  3837. case CeOp_Const_X:
  3838. {
  3839. int32 constSize = CE_GETINST(int32);
  3840. auto resultPtr = &CE_GETFRAME(uint8);
  3841. memcpy(resultPtr, instPtr, constSize);
  3842. instPtr += constSize;
  3843. }
  3844. break;
  3845. case CeOp_Load_8:
  3846. CE_LOAD(uint8);
  3847. break;
  3848. case CeOp_Load_16:
  3849. CE_LOAD(uint16);
  3850. break;
  3851. case CeOp_Load_32:
  3852. CE_LOAD(uint32);
  3853. break;
  3854. case CeOp_Load_64:
  3855. CE_LOAD(uint64);
  3856. break;
  3857. case CeOp_Load_X:
  3858. {
  3859. int32 size = CE_GETINST(int32);
  3860. auto resultPtr = &CE_GETFRAME(uint8);
  3861. auto ceAddr = CE_GETFRAME(addr_ce);
  3862. CE_CHECKADDR(ceAddr, size);
  3863. memcpy(resultPtr, memStart + ceAddr, size);
  3864. }
  3865. break;
  3866. case CeOp_Store_8:
  3867. CE_STORE(uint8);
  3868. break;
  3869. case CeOp_Store_16:
  3870. CE_STORE(uint16);
  3871. break;
  3872. case CeOp_Store_32:
  3873. CE_STORE(uint32);
  3874. break;
  3875. case CeOp_Store_64:
  3876. CE_STORE(uint64);
  3877. break;
  3878. case CeOp_Store_X:
  3879. {
  3880. auto size = CE_GETINST(int32);
  3881. auto srcPtr = &CE_GETFRAME(uint8);
  3882. auto ceAddr = CE_GETFRAME(addr_ce);
  3883. CE_CHECKADDR(ceAddr, size);
  3884. memcpy(memStart + ceAddr, srcPtr, size);
  3885. }
  3886. break;
  3887. case CeOp_Move_8:
  3888. CEOP_MOVE(int8);
  3889. break;
  3890. case CeOp_Move_16:
  3891. CEOP_MOVE(int16);
  3892. break;
  3893. case CeOp_Move_32:
  3894. CEOP_MOVE(int32);
  3895. break;
  3896. case CeOp_Move_64:
  3897. CEOP_MOVE(int64);
  3898. break;
  3899. case CeOp_Move_X:
  3900. {
  3901. int32 size = CE_GETINST(int32);
  3902. auto valPtr = &CE_GETFRAME(uint8);
  3903. auto destPtr = &CE_GETFRAME(uint8);
  3904. memcpy(destPtr, valPtr, size);
  3905. }
  3906. break;
  3907. case CeOp_Push_8:
  3908. CEOP_PUSH(int8);
  3909. break;
  3910. case CeOp_Push_16:
  3911. CEOP_PUSH(int16);
  3912. break;
  3913. case CeOp_Push_32:
  3914. CEOP_PUSH(int32);
  3915. break;
  3916. case CeOp_Push_64:
  3917. CEOP_PUSH(int64);
  3918. break;
  3919. case CeOp_Pop_8:
  3920. CEOP_POP(int8);
  3921. break;
  3922. case CeOp_Pop_16:
  3923. CEOP_POP(int16);
  3924. break;
  3925. case CeOp_Pop_32:
  3926. CEOP_POP(int32);
  3927. break;
  3928. case CeOp_Pop_64:
  3929. CEOP_POP(int64);
  3930. break;
  3931. case CeOp_Pop_X:
  3932. {
  3933. int32 size = CE_GETINST(int32);
  3934. auto resultPtr = &CE_GETFRAME(uint8);
  3935. memcpy(resultPtr, stackPtr, size);
  3936. stackPtr += size;
  3937. }
  3938. break;
  3939. case CeOp_AdjustSP:
  3940. {
  3941. int32 adjust = CE_GETFRAME(int32);
  3942. stackPtr += adjust;
  3943. }
  3944. break;
  3945. case CeOp_AdjustSPNeg:
  3946. {
  3947. int32 adjust = CE_GETFRAME(int32);
  3948. stackPtr -= adjust;
  3949. }
  3950. break;
  3951. case CeOp_AdjustSPConst:
  3952. {
  3953. int32 adjust = CE_GETINST(int32);
  3954. stackPtr += adjust;
  3955. }
  3956. break;
  3957. case CeOp_GetSP:
  3958. {
  3959. auto& result = CE_GETFRAME(int32);
  3960. result = stackPtr - memStart;
  3961. }
  3962. break;
  3963. case CeOp_SetSP:
  3964. {
  3965. auto addr = CE_GETFRAME(int32);
  3966. stackPtr = memStart + addr;
  3967. }
  3968. break;
  3969. case CeOp_GetStaticField:
  3970. {
  3971. auto frameOfs = CE_GETINST(int32);
  3972. int32 tableIdx = CE_GETINST(int32);
  3973. CeFunction* ctorCallFunction = NULL;
  3974. auto& ceStaticFieldEntry = ceFunction->mStaticFieldTable[tableIdx];
  3975. if (ceStaticFieldEntry.mBindExecuteId != mExecuteId)
  3976. {
  3977. if (mStaticCtorExecSet.TryAdd(ceStaticFieldEntry.mTypeId, NULL))
  3978. {
  3979. auto bfType = GetBfType(ceStaticFieldEntry.mTypeId);
  3980. BfTypeInstance* bfTypeInstance = NULL;
  3981. if (bfType != NULL)
  3982. bfTypeInstance = bfType->ToTypeInstance();
  3983. if (bfTypeInstance == NULL)
  3984. {
  3985. _Fail("Invalid type");
  3986. return false;
  3987. }
  3988. auto methodDef = bfTypeInstance->mTypeDef->GetMethodByName("__BfStaticCtor");
  3989. if (methodDef == NULL)
  3990. {
  3991. _Fail("No static ctor found");
  3992. return false;
  3993. }
  3994. auto moduleMethodInstance = mCeModule->GetMethodInstance(bfTypeInstance, methodDef, BfTypeVector());
  3995. if (!moduleMethodInstance)
  3996. {
  3997. _Fail("No static ctor instance found");
  3998. return false;
  3999. }
  4000. bool added = false;
  4001. ctorCallFunction = GetFunction(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, added);
  4002. if (!ctorCallFunction->mInitialized)
  4003. PrepareFunction(ctorCallFunction, NULL);
  4004. }
  4005. CeStaticFieldInfo* staticFieldInfo = NULL;
  4006. mStaticFieldMap.TryAdd(ceStaticFieldEntry.mName, NULL, &staticFieldInfo);
  4007. if (staticFieldInfo->mAddr == 0)
  4008. {
  4009. CE_CHECKALLOC(ceStaticFieldEntry.mSize);
  4010. uint8* ptr = CeMalloc(ceStaticFieldEntry.mSize);
  4011. _FixVariables();
  4012. memset(ptr, 0, ceStaticFieldEntry.mSize);
  4013. staticFieldInfo->mAddr = (addr_ce)(ptr - memStart);
  4014. }
  4015. ceStaticFieldEntry.mAddr = staticFieldInfo->mAddr;
  4016. ceStaticFieldEntry.mBindExecuteId = mExecuteId;
  4017. }
  4018. *(addr_ce*)(framePtr + frameOfs) = ceStaticFieldEntry.mAddr;
  4019. if (ctorCallFunction != NULL)
  4020. {
  4021. bool handled = false;
  4022. if (!_CheckFunction(ctorCallFunction, handled))
  4023. return false;
  4024. if (handled)
  4025. break;
  4026. CE_CALL(ctorCallFunction);
  4027. }
  4028. }
  4029. break;
  4030. case CeOp_GetMethod:
  4031. {
  4032. BF_ASSERT(memStart == mMemory.mVals);
  4033. auto& result = CE_GETFRAME(CeFunction*);
  4034. int32 callIdx = CE_GETINST(int32);
  4035. auto& callEntry = ceFunction->mCallTable[callIdx];
  4036. if (callEntry.mBindRevision != mRevision)
  4037. {
  4038. callEntry.mFunction = NULL;
  4039. //mNamedFunctionMap.TryGetValue(callEntry.mFunctionName, &callEntry.mFunction);
  4040. if (callEntry.mFunctionInfo == NULL)
  4041. {
  4042. _Fail("Unable to locate function entry");
  4043. return false;
  4044. }
  4045. if ((callEntry.mFunctionInfo->mCeFunction == NULL) && (!callEntry.mFunctionInfo->mMethodRef.IsNull()))
  4046. {
  4047. auto methodRef = callEntry.mFunctionInfo->mMethodRef;
  4048. auto methodDef = methodRef.mTypeInstance->mTypeDef->mMethods[methodRef.mMethodNum];
  4049. auto moduleMethodInstance = mCeModule->GetMethodInstance(methodRef.mTypeInstance, methodDef,
  4050. methodRef.mMethodGenericArguments);
  4051. if (moduleMethodInstance)
  4052. {
  4053. QueueMethod(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc);
  4054. }
  4055. }
  4056. if (callEntry.mFunctionInfo->mCeFunction == NULL)
  4057. {
  4058. _Fail("Method not generated");
  4059. return false;
  4060. }
  4061. callEntry.mFunction = callEntry.mFunctionInfo->mCeFunction;
  4062. if (!callEntry.mFunction->mInitialized)
  4063. {
  4064. PrepareFunction(callEntry.mFunction, NULL);
  4065. }
  4066. callEntry.mBindRevision = mRevision;
  4067. }
  4068. BF_ASSERT(memStart == mMemory.mVals);
  4069. result = callEntry.mFunction;
  4070. // if (callEntry.mFunction->mName.Contains("__static_dump"))
  4071. // {
  4072. // int32 val = *(int32*)(stackPtr);
  4073. // OutputDebugStrF("__static_dump: %d\n", val);
  4074. // }
  4075. }
  4076. break;
  4077. case CeOp_GetMethod_Inner:
  4078. {
  4079. auto& result = CE_GETFRAME(CeFunction*);
  4080. int32 innerIdx = CE_GETINST(int32);
  4081. auto outerFunction = ceFunction;
  4082. if (outerFunction->mCeInnerFunctionInfo != NULL)
  4083. outerFunction = outerFunction->mCeInnerFunctionInfo->mOwner;
  4084. auto& callEntry = outerFunction->mInnerFunctions[innerIdx];
  4085. result = callEntry;
  4086. }
  4087. break;
  4088. case CeOp_GetMethod_Virt:
  4089. {
  4090. auto& result = CE_GETFRAME(CeFunction*);
  4091. auto valueAddr = CE_GETFRAME(addr_ce);
  4092. int32 virtualIdx = CE_GETINST(int32);
  4093. CE_CHECKADDR(valueAddr, sizeof(int32));
  4094. int32 objTypeId = *(int32*)(memStart + valueAddr);
  4095. auto valueType = mCeModule->mContext->mTypes[objTypeId]->ToTypeInstance();
  4096. if (valueType->mVirtualMethodTable.IsEmpty())
  4097. mCeModule->PopulateType(valueType, BfPopulateType_DataAndMethods);
  4098. auto methodInstance = (BfMethodInstance*)valueType->mVirtualMethodTable[virtualIdx].mImplementingMethod;
  4099. auto callFunction = GetPreparedFunction(methodInstance);
  4100. result = callFunction;
  4101. }
  4102. break;
  4103. case CeOp_GetMethod_IFace:
  4104. {
  4105. auto& result = CE_GETFRAME(CeFunction*);
  4106. auto valueAddr = CE_GETFRAME(addr_ce);
  4107. int32 ifaceId = CE_GETINST(int32);
  4108. int32 methodIdx = CE_GETINST(int32);
  4109. auto ifaceType = mCeModule->mContext->mTypes[ifaceId]->ToTypeInstance();
  4110. CE_CHECKADDR(valueAddr, sizeof(int32));
  4111. int32 objTypeId = *(int32*)(memStart + valueAddr);
  4112. auto valueType = mCeModule->mContext->mTypes[objTypeId]->ToTypeInstance();
  4113. if (valueType->mVirtualMethodTable.IsEmpty())
  4114. mCeModule->PopulateType(valueType, BfPopulateType_DataAndMethods);
  4115. BfMethodInstance* methodInstance = NULL;
  4116. auto checkType = valueType;
  4117. while (checkType != NULL)
  4118. {
  4119. for (auto& iface : checkType->mInterfaces)
  4120. {
  4121. if (iface.mInterfaceType == ifaceType)
  4122. {
  4123. methodInstance = valueType->mInterfaceMethodTable[iface.mStartInterfaceTableIdx + methodIdx].mMethodRef;
  4124. break;
  4125. }
  4126. }
  4127. checkType = checkType->mBaseType;
  4128. }
  4129. if (methodInstance == NULL)
  4130. {
  4131. _Fail("Failed to invoke interface method");
  4132. return false;
  4133. }
  4134. auto callFunction = GetPreparedFunction(methodInstance);
  4135. result = callFunction;
  4136. }
  4137. break;
  4138. case CeOp_Call:
  4139. {
  4140. callCount++;
  4141. auto callFunction = CE_GETFRAME(CeFunction*);
  4142. bool handled = false;
  4143. if (!_CheckFunction(callFunction, handled))
  4144. return false;
  4145. if (handled)
  4146. break;
  4147. CE_CALL(callFunction);
  4148. }
  4149. break;
  4150. case CeOp_Conv_I8_I16:
  4151. CE_CAST(int8, int16);
  4152. break;
  4153. case CeOp_Conv_I8_I32:
  4154. CE_CAST(int8, int32);
  4155. break;
  4156. case CeOp_Conv_I8_I64:
  4157. CE_CAST(int8, int64);
  4158. break;
  4159. case CeOp_Conv_I8_F32:
  4160. CE_CAST(int8, float);
  4161. break;
  4162. case CeOp_Conv_I8_F64:
  4163. CE_CAST(int8, double);
  4164. break;
  4165. case CeOp_Conv_I16_I32:
  4166. CE_CAST(int16, int32);
  4167. break;
  4168. case CeOp_Conv_I16_I64:
  4169. CE_CAST(int16, int64);
  4170. break;
  4171. case CeOp_Conv_I16_F32:
  4172. CE_CAST(int16, float);
  4173. break;
  4174. case CeOp_Conv_I16_F64:
  4175. CE_CAST(int16, double);
  4176. break;
  4177. case CeOp_Conv_I32_I64:
  4178. CE_CAST(int32, int64);
  4179. break;
  4180. case CeOp_Conv_I32_F32:
  4181. CE_CAST(int32, float);
  4182. break;
  4183. case CeOp_Conv_I32_F64:
  4184. CE_CAST(int32, double);
  4185. break;
  4186. case CeOp_Conv_I64_F32:
  4187. CE_CAST(int64, float);
  4188. break;
  4189. case CeOp_Conv_I64_F64:
  4190. CE_CAST(int64, double);
  4191. break;
  4192. case CeOp_Conv_U8_U16:
  4193. CE_CAST(uint8, uint16);
  4194. break;
  4195. case CeOp_Conv_U8_U32:
  4196. CE_CAST(uint8, uint32);
  4197. break;
  4198. case CeOp_Conv_U8_U64:
  4199. CE_CAST(uint8, uint64);
  4200. break;
  4201. case CeOp_Conv_U8_F32:
  4202. CE_CAST(uint8, float);
  4203. break;
  4204. case CeOp_Conv_U8_F64:
  4205. CE_CAST(uint8, double);
  4206. break;
  4207. case CeOp_Conv_U16_U32:
  4208. CE_CAST(uint16, uint32);
  4209. break;
  4210. case CeOp_Conv_U16_U64:
  4211. CE_CAST(uint16, uint64);
  4212. break;
  4213. case CeOp_Conv_U16_F32:
  4214. CE_CAST(uint16, float);
  4215. break;
  4216. case CeOp_Conv_U16_F64:
  4217. CE_CAST(uint16, double);
  4218. break;
  4219. case CeOp_Conv_U32_U64:
  4220. CE_CAST(uint32, uint64);
  4221. break;
  4222. case CeOp_Conv_U32_F32:
  4223. CE_CAST(uint32, float);
  4224. break;
  4225. case CeOp_Conv_U32_F64:
  4226. CE_CAST(uint32, double);
  4227. break;
  4228. case CeOp_Conv_U64_F32:
  4229. CE_CAST(uint64, float);
  4230. break;
  4231. case CeOp_Conv_U64_F64:
  4232. CE_CAST(uint64, double);
  4233. break;
  4234. case CeOp_Conv_F32_I8:
  4235. CE_CAST(float, int8);
  4236. break;
  4237. case CeOp_Conv_F32_I16:
  4238. CE_CAST(float, int16);
  4239. break;
  4240. case CeOp_Conv_F32_I32:
  4241. CE_CAST(float, int32);
  4242. break;
  4243. case CeOp_Conv_F32_I64:
  4244. CE_CAST(float, int64);
  4245. break;
  4246. case CeOp_Conv_F32_F64:
  4247. CE_CAST(float, double);
  4248. break;
  4249. case CeOp_Conv_F64_I8:
  4250. CE_CAST(double, int8);
  4251. break;
  4252. case CeOp_Conv_F64_I16:
  4253. CE_CAST(double, int16);
  4254. break;
  4255. case CeOp_Conv_F64_I32:
  4256. CE_CAST(double, int32);
  4257. break;
  4258. case CeOp_Conv_F64_I64:
  4259. CE_CAST(double, int64);
  4260. break;
  4261. case CeOp_Conv_F64_F32:
  4262. CE_CAST(double, float);
  4263. break;
  4264. case CeOp_AddConst_I8:
  4265. CEOP_BIN_CONST(+, int8);
  4266. break;
  4267. case CeOp_AddConst_I16:
  4268. CEOP_BIN_CONST(+, int16);
  4269. break;
  4270. case CeOp_AddConst_I32:
  4271. CEOP_BIN_CONST(+, int32);
  4272. break;
  4273. case CeOp_AddConst_I64:
  4274. CEOP_BIN_CONST(+, int64);
  4275. break;
  4276. case CeOp_Add_I8:
  4277. CEOP_BIN(+, int8);
  4278. break;
  4279. case CeOp_Add_I16:
  4280. CEOP_BIN(+, int16);
  4281. break;
  4282. case CeOp_Add_I32:
  4283. CEOP_BIN(+, int32);
  4284. break;
  4285. case CeOp_Add_I64:
  4286. CEOP_BIN(+, int64);
  4287. break;
  4288. case CeOp_Add_F32:
  4289. CEOP_BIN(+, float);
  4290. break;
  4291. case CeOp_Add_F64:
  4292. CEOP_BIN(+, double);
  4293. break;
  4294. case CeOp_Sub_I8:
  4295. CEOP_BIN(-, int8);
  4296. break;
  4297. case CeOp_Sub_I16:
  4298. CEOP_BIN(-, int16);
  4299. break;
  4300. case CeOp_Sub_I32:
  4301. CEOP_BIN(-, int32);
  4302. break;
  4303. case CeOp_Sub_I64:
  4304. CEOP_BIN(-, int64);
  4305. break;
  4306. case CeOp_Sub_F32:
  4307. CEOP_BIN(-, float);
  4308. break;
  4309. case CeOp_Sub_F64:
  4310. CEOP_BIN(-, double);
  4311. break;
  4312. case CeOp_Mul_I8:
  4313. CEOP_BIN(*, int8);
  4314. break;
  4315. case CeOp_Mul_I16:
  4316. CEOP_BIN(*, int16);
  4317. break;
  4318. case CeOp_Mul_I32:
  4319. CEOP_BIN(*, int32);
  4320. break;
  4321. case CeOp_Mul_I64:
  4322. CEOP_BIN(*, int64);
  4323. break;
  4324. case CeOp_Mul_F32:
  4325. CEOP_BIN(*, float);
  4326. break;
  4327. case CeOp_Mul_F64:
  4328. CEOP_BIN(*, double);
  4329. break;
  4330. case CeOp_Div_I8:
  4331. CEOP_BIN_DIV(/, int8);
  4332. break;
  4333. case CeOp_Div_I16:
  4334. CEOP_BIN_DIV(/, int16);
  4335. break;
  4336. case CeOp_Div_I32:
  4337. CEOP_BIN_DIV(/, int32);
  4338. break;
  4339. case CeOp_Div_I64:
  4340. CEOP_BIN_DIV(/, int64);
  4341. break;
  4342. case CeOp_Div_F32:
  4343. CEOP_BIN_DIV(/, float);
  4344. break;
  4345. case CeOp_Div_F64:
  4346. CEOP_BIN_DIV(/, double);
  4347. break;
  4348. case CeOp_Div_U8:
  4349. CEOP_BIN_DIV(/, uint8);
  4350. break;
  4351. case CeOp_Div_U16:
  4352. CEOP_BIN_DIV(/, uint16);
  4353. break;
  4354. case CeOp_Div_U32:
  4355. CEOP_BIN_DIV(/, uint32);
  4356. break;
  4357. case CeOp_Div_U64:
  4358. CEOP_BIN_DIV(/, uint64);
  4359. break;
  4360. case CeOp_Mod_I8:
  4361. CEOP_BIN_DIV(%, int8);
  4362. break;
  4363. case CeOp_Mod_I16:
  4364. CEOP_BIN_DIV(%, int16);
  4365. break;
  4366. case CeOp_Mod_I32:
  4367. CEOP_BIN_DIV(%, int32);
  4368. break;
  4369. case CeOp_Mod_I64:
  4370. CEOP_BIN_DIV(%, int64);
  4371. break;
  4372. case CeOp_Mod_F32:
  4373. {
  4374. auto& result = CE_GETFRAME(float);
  4375. auto lhs = CE_GETFRAME(float);
  4376. auto rhs = CE_GETFRAME(float);
  4377. if (rhs == 0)
  4378. {
  4379. _Fail("Division by zero");
  4380. return false;
  4381. }
  4382. result = fmodf(lhs, rhs);
  4383. }
  4384. break;
  4385. case CeOp_Mod_F64:
  4386. {
  4387. auto& result = CE_GETFRAME(double);
  4388. auto lhs = CE_GETFRAME(double);
  4389. auto rhs = CE_GETFRAME(double);
  4390. if (rhs == 0)
  4391. {
  4392. _Fail("Division by zero");
  4393. return false;
  4394. }
  4395. result = fmod(lhs, rhs);
  4396. }
  4397. break;
  4398. case CeOp_Mod_U8:
  4399. CEOP_BIN_DIV(%, uint8);
  4400. break;
  4401. case CeOp_Mod_U16:
  4402. CEOP_BIN_DIV(%, uint16);
  4403. break;
  4404. case CeOp_Mod_U32:
  4405. CEOP_BIN_DIV(%, uint32);
  4406. break;
  4407. case CeOp_Mod_U64:
  4408. CEOP_BIN_DIV(%, uint64);
  4409. break;
  4410. case CeOp_And_I8:
  4411. CEOP_BIN(&, uint8);
  4412. break;
  4413. case CeOp_And_I16:
  4414. CEOP_BIN(&, uint16);
  4415. break;
  4416. case CeOp_And_I32:
  4417. CEOP_BIN(&, uint32);
  4418. break;
  4419. case CeOp_And_I64:
  4420. CEOP_BIN(&, uint64);
  4421. break;
  4422. case CeOp_Or_I8:
  4423. CEOP_BIN(|, uint8);
  4424. break;
  4425. case CeOp_Or_I16:
  4426. CEOP_BIN(|, uint16);
  4427. break;
  4428. case CeOp_Or_I32:
  4429. CEOP_BIN(|, uint32);
  4430. break;
  4431. case CeOp_Or_I64:
  4432. CEOP_BIN(|, uint64);
  4433. break;
  4434. case CeOp_Xor_I8:
  4435. CEOP_BIN(^, uint8);
  4436. break;
  4437. case CeOp_Xor_I16:
  4438. CEOP_BIN(^, uint16);
  4439. break;
  4440. case CeOp_Xor_I32:
  4441. CEOP_BIN(^, uint32);
  4442. break;
  4443. case CeOp_Xor_I64:
  4444. CEOP_BIN(^, uint64);
  4445. break;
  4446. case CeOp_Shl_I8:
  4447. CEOP_BIN2(<<, int8, uint8);
  4448. break;
  4449. case CeOp_Shl_I16:
  4450. CEOP_BIN2(<<, int16, uint8);
  4451. break;
  4452. case CeOp_Shl_I32:
  4453. CEOP_BIN2(<<, int32, uint8);
  4454. break;
  4455. case CeOp_Shl_I64:
  4456. CEOP_BIN2(<<, int64, uint8);
  4457. break;
  4458. case CeOp_Shr_I8:
  4459. CEOP_BIN2(>>, int8, uint8);
  4460. break;
  4461. case CeOp_Shr_I16:
  4462. CEOP_BIN2(>>, int16, uint8);
  4463. break;
  4464. case CeOp_Shr_I32:
  4465. CEOP_BIN2(>>, int32, uint8);
  4466. break;
  4467. case CeOp_Shr_I64:
  4468. CEOP_BIN2(>>, int64, uint8);
  4469. break;
  4470. case CeOp_Shr_U8:
  4471. CEOP_BIN2(>>, uint8, uint8);
  4472. break;
  4473. case CeOp_Shr_U16:
  4474. CEOP_BIN2(>>, uint16, uint8);
  4475. break;
  4476. case CeOp_Shr_U32:
  4477. CEOP_BIN2(>>, uint32, uint8);
  4478. break;
  4479. case CeOp_Shr_U64:
  4480. CEOP_BIN2(>>, uint64, uint8);
  4481. break;
  4482. case CeOp_Cmp_NE_I8:
  4483. CEOP_CMP(!= , int8);
  4484. break;
  4485. case CeOp_Cmp_NE_I16:
  4486. CEOP_CMP(!= , int16);
  4487. break;
  4488. case CeOp_Cmp_NE_I32:
  4489. CEOP_CMP(!=, int32);
  4490. break;
  4491. case CeOp_Cmp_NE_I64:
  4492. CEOP_CMP(!=, int64);
  4493. break;
  4494. case CeOp_Cmp_NE_F32:
  4495. CEOP_CMP(!= , float);
  4496. break;
  4497. case CeOp_Cmp_NE_F64:
  4498. CEOP_CMP(!= , double);
  4499. break;
  4500. case CeOp_Cmp_EQ_I8:
  4501. CEOP_CMP(==, int8);
  4502. break;
  4503. case CeOp_Cmp_EQ_I16:
  4504. CEOP_CMP(==, int16);
  4505. break;
  4506. case CeOp_Cmp_EQ_I32:
  4507. CEOP_CMP(==, int32);
  4508. break;
  4509. case CeOp_Cmp_EQ_I64:
  4510. CEOP_CMP(==, int64);
  4511. break;
  4512. case CeOp_Cmp_EQ_F32:
  4513. CEOP_CMP(== , float);
  4514. break;
  4515. case CeOp_Cmp_EQ_F64:
  4516. CEOP_CMP(== , double);
  4517. break;
  4518. case CeOp_Cmp_SLT_I8:
  4519. CEOP_CMP(< , int8);
  4520. break;
  4521. case CeOp_Cmp_SLT_I16:
  4522. CEOP_CMP(< , int16);
  4523. break;
  4524. case CeOp_Cmp_SLT_I32:
  4525. CEOP_CMP(<, int32);
  4526. break;
  4527. case CeOp_Cmp_SLT_I64:
  4528. CEOP_CMP(<, int64);
  4529. break;
  4530. case CeOp_Cmp_SLT_F32:
  4531. CEOP_CMP(<, float);
  4532. break;
  4533. case CeOp_Cmp_SLT_F64:
  4534. CEOP_CMP(< , double);
  4535. break;
  4536. case CeOp_Cmp_ULT_I8:
  4537. CEOP_CMP(<, uint8);
  4538. break;
  4539. case CeOp_Cmp_ULT_I16:
  4540. CEOP_CMP(<, uint16);
  4541. break;
  4542. case CeOp_Cmp_ULT_I32:
  4543. CEOP_CMP(<, uint32);
  4544. break;
  4545. case CeOp_Cmp_ULT_I64:
  4546. CEOP_CMP(<, uint64);
  4547. break;
  4548. case CeOp_Cmp_SLE_I8:
  4549. CEOP_CMP(<=, int8);
  4550. break;
  4551. case CeOp_Cmp_SLE_I16:
  4552. CEOP_CMP(<=, int16);
  4553. break;
  4554. case CeOp_Cmp_SLE_I32:
  4555. CEOP_CMP(<=, int32);
  4556. break;
  4557. case CeOp_Cmp_SLE_I64:
  4558. CEOP_CMP(<=, int64);
  4559. break;
  4560. case CeOp_Cmp_SLE_F32:
  4561. CEOP_CMP(<= , float);
  4562. break;
  4563. case CeOp_Cmp_SLE_F64:
  4564. CEOP_CMP(<= , double);
  4565. break;
  4566. case CeOp_Cmp_ULE_I8:
  4567. CEOP_CMP(<=, uint8);
  4568. break;
  4569. case CeOp_Cmp_ULE_I16:
  4570. CEOP_CMP(<=, uint16);
  4571. break;
  4572. case CeOp_Cmp_ULE_I32:
  4573. CEOP_CMP(<=, uint32);
  4574. break;
  4575. case CeOp_Cmp_ULE_I64:
  4576. CEOP_CMP(<=, uint64);
  4577. break;
  4578. case CeOp_Cmp_SGT_I8:
  4579. CEOP_CMP(>, int8);
  4580. break;
  4581. case CeOp_Cmp_SGT_I16:
  4582. CEOP_CMP(>, int16);
  4583. break;
  4584. case CeOp_Cmp_SGT_I32:
  4585. CEOP_CMP(>, int32);
  4586. break;
  4587. case CeOp_Cmp_SGT_I64:
  4588. CEOP_CMP(>, int64);
  4589. break;
  4590. case CeOp_Cmp_SGT_F32:
  4591. CEOP_CMP(>, float);
  4592. break;
  4593. case CeOp_Cmp_SGT_F64:
  4594. CEOP_CMP(>, double);
  4595. break;
  4596. case CeOp_Cmp_UGT_I8:
  4597. CEOP_CMP(>, uint8);
  4598. break;
  4599. case CeOp_Cmp_UGT_I16:
  4600. CEOP_CMP(>, uint16);
  4601. break;
  4602. case CeOp_Cmp_UGT_I32:
  4603. CEOP_CMP(>, uint32);
  4604. break;
  4605. case CeOp_Cmp_UGT_I64:
  4606. CEOP_CMP(>, uint64);
  4607. break;
  4608. case CeOp_Cmp_SGE_I8:
  4609. CEOP_CMP(>=, int8);
  4610. break;
  4611. case CeOp_Cmp_SGE_I16:
  4612. CEOP_CMP(>=, int16);
  4613. break;
  4614. case CeOp_Cmp_SGE_I32:
  4615. CEOP_CMP(>=, int32);
  4616. break;
  4617. case CeOp_Cmp_SGE_I64:
  4618. CEOP_CMP(>=, int64);
  4619. break;
  4620. case CeOp_Cmp_SGE_F32:
  4621. CEOP_CMP(>=, float);
  4622. break;
  4623. case CeOp_Cmp_SGE_F64:
  4624. CEOP_CMP(>=, double);
  4625. break;
  4626. case CeOp_Cmp_UGE_I8:
  4627. CEOP_CMP(>=, uint8);
  4628. break;
  4629. case CeOp_Cmp_UGE_I16:
  4630. CEOP_CMP(>=, uint16);
  4631. break;
  4632. case CeOp_Cmp_UGE_I32:
  4633. CEOP_CMP(>=, uint32);
  4634. break;
  4635. case CeOp_Cmp_UGE_I64:
  4636. CEOP_CMP(>=, uint64);
  4637. break;
  4638. case CeOp_Neg_I8:
  4639. CEOP_UNARY(-, int8);
  4640. break;
  4641. case CeOp_Neg_I16:
  4642. CEOP_UNARY(-, int16);
  4643. break;
  4644. case CeOp_Neg_I32:
  4645. CEOP_UNARY(-, int32);
  4646. break;
  4647. case CeOp_Neg_I64:
  4648. CEOP_UNARY(-, int64);
  4649. break;
  4650. case CeOp_Neg_F32:
  4651. CEOP_UNARY(-, float);
  4652. case CeOp_Neg_F64:
  4653. CEOP_UNARY(-, double);
  4654. break;
  4655. case CeOp_Not_I1:
  4656. CEOP_UNARY(!, bool);
  4657. break;
  4658. case CeOp_Not_I8:
  4659. CEOP_UNARY(~, int8);
  4660. break;
  4661. case CeOp_Not_I16:
  4662. CEOP_UNARY(~, int16);
  4663. break;
  4664. case CeOp_Not_I32:
  4665. CEOP_UNARY(~, int32);
  4666. break;
  4667. case CeOp_Not_I64:
  4668. CEOP_UNARY(~, int64);
  4669. break;
  4670. default:
  4671. _Fail("Unhandled op");
  4672. return false;
  4673. }
  4674. }
  4675. return true;
  4676. }
  4677. void CeMachine::PrepareFunction(CeFunction* ceFunction, CeBuilder* parentBuilder)
  4678. {
  4679. AutoTimer autoTimer(mRevisionExecuteTime);
  4680. if (mHeap == NULL)
  4681. mHeap = new ContiguousHeap();
  4682. if (ceFunction->mMethodInstance != NULL)
  4683. {
  4684. auto methodDef = ceFunction->mMethodInstance->mMethodDef;
  4685. if (methodDef->mIsExtern)
  4686. {
  4687. ceFunction->mFunctionKind = CeFunctionKind_Extern;
  4688. auto owner = ceFunction->mMethodInstance->GetOwner();
  4689. if (owner == mCeModule->mContext->mBfObjectType)
  4690. {
  4691. if (methodDef->mName == "ConstEval_GetType")
  4692. {
  4693. ceFunction->mFunctionKind = CeFunctionKind_GetReflectType;
  4694. }
  4695. }
  4696. else if (owner->IsInstanceOf(mCeModule->mCompiler->mTypeTypeDef))
  4697. {
  4698. if (methodDef->mName == "ConstEval_GetTypeById")
  4699. {
  4700. ceFunction->mFunctionKind = CeFunctionKind_GetReflectTypeById;
  4701. }
  4702. }
  4703. else if (owner->IsInstanceOf(mCeModule->mCompiler->mDiagnosticsDebugTypeDef))
  4704. {
  4705. if (methodDef->mName == "Write")
  4706. {
  4707. if (ceFunction->mMethodInstance->GetParamCount() == 1)
  4708. ceFunction->mFunctionKind = CeFunctionKind_DebugWrite_Int;
  4709. else
  4710. ceFunction->mFunctionKind = CeFunctionKind_DebugWrite;
  4711. }
  4712. }
  4713. else if (owner->IsInstanceOf(mCeModule->mCompiler->mThreadTypeDef))
  4714. {
  4715. if (methodDef->mName == "SleepInternal")
  4716. ceFunction->mFunctionKind = CeFunctionKind_Sleep;
  4717. }
  4718. else if (owner->IsInstanceOf(mCeModule->mCompiler->mInternalTypeDef))
  4719. {
  4720. if (methodDef->mName == "ThrowIndexOutOfRange")
  4721. ceFunction->mFunctionKind = CeFunctionKind_OOB;
  4722. else if (methodDef->mName == "FatalError")
  4723. ceFunction->mFunctionKind = CeFunctionKind_FatalError;
  4724. else if (methodDef->mName == "Dbg_RawAlloc")
  4725. ceFunction->mFunctionKind = CeFunctionKind_Malloc;
  4726. else if (methodDef->mName == "Dbg_RawFree")
  4727. ceFunction->mFunctionKind = CeFunctionKind_Free;
  4728. }
  4729. else if (owner->IsInstanceOf(mCeModule->mCompiler->mChar32TypeDef))
  4730. {
  4731. if (methodDef->mName == "get__ToLower")
  4732. ceFunction->mFunctionKind = CeFunctionKind_Char32_ToLower;
  4733. else if (methodDef->mName == "get__ToUpper")
  4734. ceFunction->mFunctionKind = CeFunctionKind_Char32_ToUpper;
  4735. else if (methodDef->mName == "get__IsLower")
  4736. ceFunction->mFunctionKind = CeFunctionKind_Char32_IsLower;
  4737. else if (methodDef->mName == "get__IsUpper")
  4738. ceFunction->mFunctionKind = CeFunctionKind_Char32_IsUpper;
  4739. else if (methodDef->mName == "get__IsWhiteSpace_EX")
  4740. ceFunction->mFunctionKind = CeFunctionKind_Char32_IsWhiteSpace_EX;
  4741. else if (methodDef->mName == "get__IsLetter")
  4742. ceFunction->mFunctionKind = CeFunctionKind_Char32_IsLetter;
  4743. else if (methodDef->mName == "get__IsLetterOrDigit")
  4744. ceFunction->mFunctionKind = CeFunctionKind_Char32_IsLetterOrDigit;
  4745. else if (methodDef->mName == "get__IsNumer")
  4746. ceFunction->mFunctionKind = CeFunctionKind_Char32_IsNumber;
  4747. }
  4748. ceFunction->mInitialized = true;
  4749. return;
  4750. }
  4751. }
  4752. BF_ASSERT(!ceFunction->mInitialized);
  4753. ceFunction->mInitialized = true;
  4754. CeBuilder ceBuilder;
  4755. ceBuilder.mParentBuilder = parentBuilder;
  4756. ceBuilder.mPtrSize = mCeModule->mCompiler->mSystem->mPtrSize;
  4757. ceBuilder.mCeMachine = this;
  4758. ceBuilder.mCeFunction = ceFunction;
  4759. ceBuilder.Build();
  4760. /*if (!ceFunction->mCode.IsEmpty())
  4761. {
  4762. CeDumpContext dumpCtx;
  4763. dumpCtx.mCeFunction = ceFunction;
  4764. dumpCtx.mStart = &ceFunction->mCode[0];
  4765. dumpCtx.mPtr = dumpCtx.mStart;
  4766. dumpCtx.mEnd = dumpCtx.mPtr + ceFunction->mCode.mSize;
  4767. dumpCtx.Dump();
  4768. OutputDebugStrF("Code for %s:\n%s\n", ceBuilder.mBeFunction->mName.c_str(), dumpCtx.mStr.c_str());
  4769. }*/
  4770. }
  4771. void CeMachine::CheckFunctions()
  4772. {
  4773. for (auto kv : mFunctions)
  4774. {
  4775. BF_ASSERT((((intptr)(void*)kv.mKey->mMethodDef) & 0xFF) != 0xDD);
  4776. }
  4777. }
  4778. CeFunction* CeMachine::GetFunction(BfMethodInstance* methodInstance, BfIRValue func, bool& added)
  4779. {
  4780. if (func)
  4781. {
  4782. if ((func.IsConst()) || (func.IsFake()))
  4783. return NULL;
  4784. }
  4785. CheckFunctions();
  4786. CeFunctionInfo** functionInfoPtr = NULL;
  4787. CeFunctionInfo* ceFunctionInfo = NULL;
  4788. CeFunction* ceFunction = NULL;
  4789. if (!mFunctions.TryAdd(methodInstance, NULL, &functionInfoPtr))
  4790. {
  4791. ceFunctionInfo = *functionInfoPtr;
  4792. BF_ASSERT(ceFunctionInfo->mCeFunction != NULL);
  4793. return ceFunctionInfo->mCeFunction;
  4794. }
  4795. BF_ASSERT(!methodInstance->mInCEMachine);
  4796. methodInstance->mInCEMachine = true;
  4797. BfLogSys(mCeModule->mSystem, "CeMachine::GetFunction %p\n", methodInstance);
  4798. if (!func)
  4799. {
  4800. ceFunctionInfo = new CeFunctionInfo();
  4801. }
  4802. else
  4803. {
  4804. auto funcVal = mCeModule->mBfIRBuilder->mBeIRCodeGen->GetBeValue(func.mId);
  4805. if (auto function = BeValueDynCast<BeFunction>(funcVal))
  4806. {
  4807. CeFunctionInfo** namedFunctionInfoPtr = NULL;
  4808. if (mNamedFunctionMap.TryAdd(function->mName, NULL, &namedFunctionInfoPtr))
  4809. {
  4810. ceFunctionInfo = new CeFunctionInfo();
  4811. ceFunctionInfo->mName = function->mName;
  4812. *namedFunctionInfoPtr = ceFunctionInfo;
  4813. }
  4814. else
  4815. {
  4816. ceFunctionInfo = *namedFunctionInfoPtr;
  4817. }
  4818. }
  4819. else
  4820. {
  4821. ceFunctionInfo = new CeFunctionInfo();
  4822. }
  4823. }
  4824. ceFunctionInfo->mRefCount++;
  4825. *functionInfoPtr = ceFunctionInfo;
  4826. if (ceFunctionInfo->mMethodInstance == NULL)
  4827. {
  4828. added = true;
  4829. auto module = methodInstance->GetOwner()->mModule;
  4830. BF_ASSERT(ceFunctionInfo->mCeFunction == NULL);
  4831. ceFunction = new CeFunction();
  4832. ceFunction->mCeFunctionInfo = ceFunctionInfo;
  4833. ceFunction->mMethodInstance = methodInstance;
  4834. ceFunctionInfo->mMethodInstance = methodInstance;
  4835. ceFunctionInfo->mCeFunction = ceFunction;
  4836. }
  4837. return ceFunction;
  4838. }
  4839. CeFunction* CeMachine::GetPreparedFunction(BfMethodInstance* methodInstance)
  4840. {
  4841. bool added = false;
  4842. auto ceFunction = GetFunction(methodInstance, BfIRValue(), added);
  4843. if (ceFunction == NULL)
  4844. return NULL;
  4845. if (!ceFunction->mInitialized)
  4846. PrepareFunction(ceFunction, NULL);
  4847. return ceFunction;
  4848. }
  4849. void CeMachine::QueueMethod(BfMethodInstance* methodInstance, BfIRValue func)
  4850. {
  4851. bool added = false;
  4852. auto ceFunction = GetFunction(methodInstance, func, added);
  4853. }
  4854. void CeMachine::QueueMethod(BfModuleMethodInstance moduleMethodInstance)
  4855. {
  4856. QueueMethod(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc);
  4857. }
  4858. void CeMachine::QueueStaticField(BfFieldInstance* fieldInstance, const StringImpl& mangledFieldName)
  4859. {
  4860. CeStaticFieldInfo staticFieldInfo;
  4861. staticFieldInfo.mFieldInstance = fieldInstance;
  4862. mStaticFieldMap[mangledFieldName] = staticFieldInfo;
  4863. }
  4864. void CeMachine::SetAppendAllocInfo(BfModule* module, BfIRValue allocValue, BfIRValue appendSizeValue)
  4865. {
  4866. delete mAppendAllocInfo;
  4867. mAppendAllocInfo = new CeAppendAllocInfo();
  4868. mAppendAllocInfo->mModule = module;
  4869. mAppendAllocInfo->mAllocValue = allocValue;
  4870. mAppendAllocInfo->mAppendSizeValue = appendSizeValue;
  4871. }
  4872. void CeMachine::ClearAppendAllocInfo()
  4873. {
  4874. delete mAppendAllocInfo;
  4875. mAppendAllocInfo = NULL;
  4876. }
  4877. BfTypedValue CeMachine::Call(BfAstNode* targetSrc, BfModule* module, BfMethodInstance* methodInstance, const BfSizedArray<BfIRValue>& args, CeEvalFlags flags, BfType* expectingType)
  4878. {
  4879. AutoTimer autoTimer(mRevisionExecuteTime);
  4880. // DISABLED
  4881. //return BfTypedValue();
  4882. SetAndRestoreValue<BfAstNode*> prevTargetSrc(mCurTargetSrc, targetSrc);
  4883. SetAndRestoreValue<BfModule*> prevModule(mCurModule, module);
  4884. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurMethodInstance, methodInstance);
  4885. SetAndRestoreValue<BfType*> prevExpectingType(mCurExpectingType, expectingType);
  4886. if (mAppendAllocInfo != NULL)
  4887. {
  4888. if ((mAppendAllocInfo->mAppendSizeValue) && (!mAppendAllocInfo->mAppendSizeValue.IsConst()))
  4889. {
  4890. Fail("Non-constant append alloc");
  4891. return BfTypedValue();
  4892. }
  4893. }
  4894. int thisArgIdx = -1;
  4895. int appendAllocIdx = -1;
  4896. if (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)
  4897. {
  4898. if (!methodInstance->GetOwner()->IsValuelessType())
  4899. thisArgIdx = 0;
  4900. if ((methodInstance->GetParamCount() >= 1) && (methodInstance->GetParamKind(0) == BfParamKind_AppendIdx))
  4901. appendAllocIdx = 1;
  4902. }
  4903. int paramCompositeSize = 0;
  4904. int paramIdx = methodInstance->GetParamCount();
  4905. for (int argIdx = (int)args.size() - 1; argIdx >= 0; argIdx--)
  4906. {
  4907. BfType* paramType = NULL;
  4908. while (true)
  4909. {
  4910. paramIdx--;
  4911. paramType = methodInstance->GetParamType(paramIdx);
  4912. if (paramType->IsTypedPrimitive())
  4913. paramType = paramType->GetUnderlyingType();
  4914. if (!paramType->IsValuelessType())
  4915. break;
  4916. }
  4917. if (paramType->IsComposite())
  4918. {
  4919. auto paramTypeInst = paramType->ToTypeInstance();
  4920. paramCompositeSize += paramTypeInst->mInstSize;
  4921. }
  4922. auto arg = args[argIdx];
  4923. if (!arg.IsConst())
  4924. {
  4925. if ((argIdx != thisArgIdx) && (argIdx != appendAllocIdx))
  4926. {
  4927. Fail(StrFormat("Non-constant argument for param '%s'", methodInstance->GetParamName(paramIdx).c_str()));
  4928. return BfTypedValue();
  4929. }
  4930. }
  4931. }
  4932. BF_ASSERT(mCallStack.IsEmpty());
  4933. auto methodDef = methodInstance->mMethodDef;
  4934. if (mCeModule == NULL)
  4935. Init();
  4936. bool added = false;
  4937. CeFunction* ceFunction = GetFunction(methodInstance, BfIRValue(), added);
  4938. if (!ceFunction->mInitialized)
  4939. PrepareFunction(ceFunction, NULL);
  4940. mMemory.Resize(BF_CE_STACK_SIZE);
  4941. auto stackPtr = &mMemory[0] + mMemory.mSize;
  4942. auto* memStart = &mMemory[0];
  4943. BfTypeInstance* thisType = methodInstance->GetOwner();
  4944. addr_ce allocThisInstAddr = 0;
  4945. addr_ce allocThisAddr = 0;
  4946. int allocThisSize = -1;
  4947. if (thisArgIdx != -1)
  4948. {
  4949. allocThisSize = thisType->mInstSize;
  4950. if ((mAppendAllocInfo != NULL) && (mAppendAllocInfo->mAppendSizeValue))
  4951. {
  4952. BF_ASSERT(mAppendAllocInfo->mModule == module);
  4953. BF_ASSERT(mAppendAllocInfo->mAppendSizeValue.IsConst());
  4954. auto appendSizeConstant = module->mBfIRBuilder->GetConstant(mAppendAllocInfo->mAppendSizeValue);
  4955. BF_ASSERT(module->mBfIRBuilder->IsInt(appendSizeConstant->mTypeCode));
  4956. allocThisSize += appendSizeConstant->mInt32;
  4957. }
  4958. stackPtr -= allocThisSize;
  4959. auto allocThisPtr = stackPtr;
  4960. memset(allocThisPtr, 0, allocThisSize);
  4961. if (thisType->IsObject())
  4962. *(int32*)(allocThisPtr) = thisType->mTypeId;
  4963. allocThisInstAddr = allocThisPtr - memStart;
  4964. allocThisAddr = allocThisInstAddr;
  4965. }
  4966. addr_ce allocAppendIdxAddr = 0;
  4967. if (appendAllocIdx != -1)
  4968. {
  4969. stackPtr -= mCeModule->mSystem->mPtrSize;
  4970. memset(stackPtr, 0, mCeModule->mSystem->mPtrSize);
  4971. allocAppendIdxAddr = stackPtr - memStart;
  4972. }
  4973. auto _FixVariables = [&]()
  4974. {
  4975. intptr memOffset = &mMemory[0] - memStart;
  4976. if (memOffset == 0)
  4977. return;
  4978. memStart += memOffset;
  4979. stackPtr += memOffset;
  4980. };
  4981. addr_ce compositeStartAddr = stackPtr - memStart;
  4982. stackPtr -= paramCompositeSize;
  4983. addr_ce useCompositeAddr = compositeStartAddr;
  4984. paramIdx = methodInstance->GetParamCount();
  4985. for (int argIdx = (int)args.size() - 1; argIdx >= 0; argIdx--)
  4986. {
  4987. BfType* paramType = NULL;
  4988. while (true)
  4989. {
  4990. paramIdx--;
  4991. paramType = methodInstance->GetParamType(paramIdx);
  4992. if (paramType->IsTypedPrimitive())
  4993. paramType = paramType->GetUnderlyingType();
  4994. if (!paramType->IsValuelessType())
  4995. break;
  4996. }
  4997. bool isParams = methodInstance->GetParamKind(paramIdx) == BfParamKind_Params;
  4998. auto arg = args[argIdx];
  4999. if (!arg.IsConst())
  5000. {
  5001. if (argIdx == thisArgIdx)
  5002. {
  5003. if (mAppendAllocInfo != NULL)
  5004. BF_ASSERT(mAppendAllocInfo->mAllocValue == arg);
  5005. stackPtr -= mCeModule->mSystem->mPtrSize;
  5006. int64 addr64 = allocThisAddr;
  5007. memcpy(stackPtr, &addr64, mCeModule->mSystem->mPtrSize);
  5008. continue;
  5009. }
  5010. else if (argIdx == appendAllocIdx)
  5011. {
  5012. stackPtr -= mCeModule->mSystem->mPtrSize;
  5013. int64 addr64 = allocAppendIdxAddr;
  5014. memcpy(stackPtr, &addr64, mCeModule->mSystem->mPtrSize);
  5015. continue;
  5016. }
  5017. else
  5018. return BfTypedValue();
  5019. }
  5020. auto constant = module->mBfIRBuilder->GetConstant(arg);
  5021. if (paramType->IsComposite())
  5022. {
  5023. auto paramTypeInst = paramType->ToTypeInstance();
  5024. useCompositeAddr -= paramTypeInst->mInstSize;
  5025. if (!WriteConstant(module, useCompositeAddr, constant, paramType, isParams))
  5026. {
  5027. Fail(StrFormat("Failed to process argument for param '%s'", methodInstance->GetParamName(paramIdx).c_str()));
  5028. return BfTypedValue();
  5029. }
  5030. _FixVariables();
  5031. stackPtr -= mCeModule->mSystem->mPtrSize;
  5032. int64 addr64 = useCompositeAddr;
  5033. memcpy(stackPtr, &addr64, mCeModule->mSystem->mPtrSize);
  5034. }
  5035. else
  5036. {
  5037. stackPtr -= paramType->mSize;
  5038. if (!WriteConstant(module, stackPtr - memStart, constant, paramType, isParams))
  5039. {
  5040. Fail(StrFormat("Failed to process argument for param '%s'", methodInstance->GetParamName(paramIdx).c_str()));
  5041. return BfTypedValue();
  5042. }
  5043. _FixVariables();
  5044. }
  5045. }
  5046. addr_ce retAddr = 0;
  5047. auto returnType = methodInstance->mReturnType;
  5048. if (!returnType->IsValuelessType())
  5049. {
  5050. int retSize = methodInstance->mReturnType->mSize;
  5051. stackPtr -= retSize;
  5052. retAddr = stackPtr - memStart;
  5053. }
  5054. bool success = Execute(ceFunction, stackPtr - ceFunction->mFrameSize, stackPtr);
  5055. memStart = &mMemory[0];
  5056. addr_ce retInstAddr = retAddr;
  5057. if ((returnType->IsObject()) || (returnType->IsPointer()))
  5058. {
  5059. // Or pointer?
  5060. retInstAddr = *(addr_ce*)(memStart + retAddr);
  5061. }
  5062. BfTypedValue returnValue;
  5063. if (success)
  5064. {
  5065. BfTypedValue retValue;
  5066. if ((retInstAddr != 0) || (allocThisInstAddr != 0))
  5067. {
  5068. auto* retPtr = memStart + retInstAddr;
  5069. if (allocThisInstAddr != 0)
  5070. {
  5071. retPtr = memStart + allocThisAddr;
  5072. returnType = thisType;
  5073. }
  5074. BfType* usedReturnType = returnType;
  5075. BfIRValue constVal = CreateConstant(module, retPtr, returnType, &usedReturnType);
  5076. if (constVal)
  5077. returnValue = BfTypedValue(constVal, usedReturnType);
  5078. else
  5079. {
  5080. Fail("Failed to encode return argument");
  5081. }
  5082. }
  5083. else
  5084. {
  5085. returnValue = BfTypedValue(module->mBfIRBuilder->GetFakeVal(), returnType);
  5086. }
  5087. }
  5088. mStringMap.Clear();
  5089. mReflectMap.Clear();
  5090. mConstDataMap.Clear();
  5091. mMemory.Clear();
  5092. if (mMemory.mAllocSize > BF_CE_MAX_CARRYOVER_MEMORY)
  5093. mMemory.Dispose();
  5094. mCallStack.Clear();
  5095. mStaticCtorExecSet.Clear();
  5096. mStaticFieldMap.Clear();
  5097. mHeap->Clear(BF_CE_MAX_CARRYOVER_HEAP);
  5098. return returnValue;
  5099. }