12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909 |
- #include "CeMachine.h"
- #include "CeDebugger.h"
- #include "BfModule.h"
- #include "BfCompiler.h"
- #include "BfIRBuilder.h"
- #include "BfParser.h"
- #include "BfReducer.h"
- #include "BfExprEvaluator.h"
- #include "BfResolvePass.h"
- #include "BfMangler.h"
- #include "../Backend/BeIRCodeGen.h"
- #include "BeefySysLib/platform/PlatformHelper.h"
- #include "../DebugManager.h"
- #include "BeefySysLib/util/StackHelper.h"
- extern "C"
- {
- #include "BeefySysLib/third_party/utf8proc/utf8proc.h"
- }
- #define CE_ENABLE_HEAP
- USING_NS_BF;
- enum CeOpInfoFlag
- {
- CeOpInfoFlag_None,
- CeOpInfoFlag_SizeX,
- };
- struct CeOpInfo
- {
- const char* mName;
- CeOperandInfoKind mResultKind;
- CeOperandInfoKind mOperandA;
- CeOperandInfoKind mOperandB;
- CeOperandInfoKind mOperandC;
- CeOpInfoFlag mFlags;
- };
- #define CEOPINFO_SIZED_1(OPNAME, OPINFOA) \
- {OPNAME "_8", OPINFOA##8}, \
- {OPNAME "_16", OPINFOA##16}, \
- {OPNAME "_32", OPINFOA##32}, \
- {OPNAME "_64", OPINFOA##64}, \
- {OPNAME "_X", OPINFOA, CEOI_None, CEOI_None, CEOI_None, CeOpInfoFlag_SizeX}
- #define CEOPINFO_SIZED_2(OPNAME, OPINFOA, OPINFOB) \
- {OPNAME "_8", OPINFOA##8, OPINFOB##8}, \
- {OPNAME "_16", OPINFOA##16, OPINFOB##16}, \
- {OPNAME "_32", OPINFOA##32, OPINFOB##32}, \
- {OPNAME "_64", OPINFOA##64, OPINFOB##64}, \
- {OPNAME "_X", OPINFOA, OPINFOB, CEOI_None, CEOI_None, CeOpInfoFlag_SizeX}
- #define CEOPINFO_SIZED_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_8", OPINFOA##8, OPINFOB##8, OPINFOC##8}, \
- {OPNAME "_16", OPINFOA##16, OPINFOB##16, OPINFOC##16}, \
- {OPNAME "_32", OPINFOA##32, OPINFOB##32, OPINFOC##32}, \
- {OPNAME "_64", OPINFOA##64, OPINFOB##64, OPINFOC##64}, \
- {OPNAME "_X", OPINFOA, OPINFOB, OPINFOC, CEOI_None, CeOpInfoFlag_SizeX}
- #define CEOPINFO_SIZED_NUMERIC_2(OPNAME, OPINFOA, OPINFOB) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8}, \
- {OPNAME "_I16", OPINFOA##16, OPINFOB##16}, \
- {OPNAME "_I32", OPINFOA##32, OPINFOB##32}, \
- {OPNAME "_I64", OPINFOA##64, OPINFOB##64}
- #define CEOPINFO_SIZED_NUMERIC_PLUSF_2(OPNAME, OPINFOA, OPINFOB) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8}, \
- {OPNAME "_I16", OPINFOA##16, OPINFOB##16}, \
- {OPNAME "_I32", OPINFOA##32, OPINFOB##32}, \
- {OPNAME "_I64", OPINFOA##64, OPINFOB##64}, \
- {OPNAME "_F32", OPINFOA##F32, OPINFOB##F64}, \
- {OPNAME "_F64", OPINFOA##F64, OPINFOB##F64}
- #define CEOPINFO_SIZED_NUMERIC_PLUSF_2_RESULT8(OPNAME, OPINFOA, OPINFOB) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8}, \
- {OPNAME "_I16", OPINFOA##8, OPINFOB##16}, \
- {OPNAME "_I32", OPINFOA##8, OPINFOB##32}, \
- {OPNAME "_I64", OPINFOA##8, OPINFOB##64}, \
- {OPNAME "_F32", OPINFOA##8, OPINFOB##F64}, \
- {OPNAME "_F64", OPINFOA##8, OPINFOB##F64}
- #define CEOPINFO_SIZED_NUMERIC_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8, OPINFOC##8}, \
- {OPNAME "_I16", OPINFOA##16, OPINFOB##16, OPINFOC##16}, \
- {OPNAME "_I32", OPINFOA##32, OPINFOB##32, OPINFOC##32}, \
- {OPNAME "_I64", OPINFOA##64, OPINFOB##64, OPINFOC##64}
- #define CEOPINFO_SIZED_NUMERIC_3_RESULT8(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8, OPINFOC##8}, \
- {OPNAME "_I16", OPINFOA##8, OPINFOB##16, OPINFOC##16}, \
- {OPNAME "_I32", OPINFOA##8, OPINFOB##32, OPINFOC##32}, \
- {OPNAME "_I64", OPINFOA##8, OPINFOB##64, OPINFOC##64}
- #define CEOPINFO_SIZED_UNUMERIC_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_U8", OPINFOA##8, OPINFOB##8, OPINFOC##8}, \
- {OPNAME "_U16", OPINFOA##16, OPINFOB##16, OPINFOC##16}, \
- {OPNAME "_U32", OPINFOA##32, OPINFOB##32, OPINFOC##32}, \
- {OPNAME "_U64", OPINFOA##64, OPINFOB##64, OPINFOC##64}
- #define CEOPINFO_SIZED_NUMERIC_PLUSF_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8, OPINFOC##8}, \
- {OPNAME "_I16", OPINFOA##16, OPINFOB##16, OPINFOC##16}, \
- {OPNAME "_I32", OPINFOA##32, OPINFOB##32, OPINFOC##32}, \
- {OPNAME "_I64", OPINFOA##64, OPINFOB##64, OPINFOC##64}, \
- {OPNAME "_F32", OPINFOA##F32, OPINFOB##F32, OPINFOC##F32}, \
- {OPNAME "_F64", OPINFOA##F64, OPINFOB##F64, OPINFOC##F64}
- #define CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_I8", OPINFOA##8, OPINFOB##8, OPINFOC##8}, \
- {OPNAME "_I16", OPINFOA##8, OPINFOB##16, OPINFOC##16}, \
- {OPNAME "_I32", OPINFOA##8, OPINFOB##32, OPINFOC##32}, \
- {OPNAME "_I64", OPINFOA##8, OPINFOB##64, OPINFOC##64}, \
- {OPNAME "_F32", OPINFOA##8, OPINFOB##F32, OPINFOC##F32}, \
- {OPNAME "_F64", OPINFOA##8, OPINFOB##F64, OPINFOC##F64}
- #define CEOPINFO_SIZED_FLOAT_2(OPNAME, OPINFOA, OPINFOB) \
- {OPNAME "_F32", OPINFOA##F32, OPINFOB##F32}, \
- {OPNAME "_F64", OPINFOA##F64, OPINFOB##F64}
- #define CEOPINFO_SIZED_FLOAT_3(OPNAME, OPINFOA, OPINFOB, OPINFOC) \
- {OPNAME "_F32", OPINFOA##F32, OPINFOB##F32, OPINFOC##F32}, \
- {OPNAME "_F64", OPINFOA##F64, OPINFOB##F64, OPINFOC##F64}
- static CeOpInfo gOpInfo[] =
- {
- {"InvalidOp"},
- {"Nop"},
- {"DbgBreak"},
- {"Ret"},
- {"SetRet", CEOI_None, CEOI_IMM32},
- {"Jmp", CEOI_None, CEOI_JMPREL},
- {"JmpIf", CEOI_None, CEOI_JMPREL, CEOI_FrameRef8},
- {"JmpIfNot", CEOI_None, CEOI_JMPREL, CEOI_FrameRef8},
- {"Error", CEOI_None, CEOI_IMM32},
- {"DynamicCastCheck", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
- {"GetReflectType", CEOI_FrameRef, CEOI_IMM32},
- {"GetString", CEOI_FrameRef, CEOI_IMM32},
- {"Malloc", CEOI_FrameRef, CEOI_FrameRef},
- {"Free", CEOI_None, CEOI_FrameRef},
- {"MemSet", CEOI_None, CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef},
- {"MemSet_Const", CEOI_None, CEOI_FrameRef, CEOI_IMM8, CEOI_IMM32},
- {"MemCpy", CEOI_None, CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef},
- {"FrameAddr_32", CEOI_FrameRef, CEOI_FrameRef},
- {"FrameAddr_64", CEOI_FrameRef, CEOI_FrameRef},
- {"FrameAddrOfs_32", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
- {"ConstData", CEOI_FrameRef, CEOI_IMM32},
- {"ConstDataRef", CEOI_FrameRef, CEOI_IMM32},
- {"Zero", CEOI_None, CEOI_FrameRef, CEOI_IMM32},
- {"Const_8", CEOI_FrameRef8, CEOI_IMM8},
- {"Const_16", CEOI_FrameRef16, CEOI_IMM16},
- {"Const_32", CEOI_FrameRef32, CEOI_IMM32},
- {"Const_64", CEOI_FrameRef64, CEOI_IMM64},
- {"Const_X", CEOI_FrameRef, CEOI_IMM_VAR},
- CEOPINFO_SIZED_2("Load", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_3("Store", CEOI_None, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_3("Move", CEOI_None, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_2("Push", CEOI_None, CEOI_FrameRef),
- CEOPINFO_SIZED_1("Pop", CEOI_FrameRef),
- {"AdjustSP", CEOI_None, CEOI_FrameRef},
- {"AdjustSPNeg", CEOI_None, CEOI_FrameRef},
- {"AdjustSPConst", CEOI_None, CEOI_IMM32},
- {"CeOp_GetSP", CEOI_FrameRef},
- {"CeOp_SetSP", CEOI_None, CEOI_FrameRef},
- {"GetStaticField", CEOI_FrameRef, CEOI_IMM32},
- {"GetStaticField_Initializer", CEOI_FrameRef, CEOI_IMM32, CEOI_FrameRef},
- {"GetMethod", CEOI_FrameRef, CEOI_IMM32},
- {"GetMethod_Inner", CEOI_FrameRef, CEOI_IMM32},
- {"GetMethod_Virt", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32},
- {"GetMethod_IFace", CEOI_FrameRef, CEOI_FrameRef, CEOI_IMM32, CEOI_IMM32},
- {"Call", CEOI_None, CEOI_FrameRef},
- {"CeOp_Conv_I8_I16", CEOI_FrameRef16, CEOI_FrameRef8},
- {"CeOp_Conv_I8_I32", CEOI_FrameRef32, CEOI_FrameRef8},
- {"CeOp_Conv_I8_I64", CEOI_FrameRef64, CEOI_FrameRef8},
- {"CeOp_Conv_I8_F32", CEOI_FrameRefF32, CEOI_FrameRef8},
- {"CeOp_Conv_I8_F64", CEOI_FrameRefF64, CEOI_FrameRef8},
- {"CeOp_Conv_I16_I32", CEOI_FrameRef32, CEOI_FrameRef16},
- {"CeOp_Conv_I16_I64", CEOI_FrameRef64, CEOI_FrameRef16},
- {"CeOp_Conv_I16_F32", CEOI_FrameRefF32, CEOI_FrameRef16},
- {"CeOp_Conv_I16_F64", CEOI_FrameRefF64, CEOI_FrameRef16},
- {"CeOp_Conv_I32_I64", CEOI_FrameRef64, CEOI_FrameRef32},
- {"CeOp_Conv_I32_F32", CEOI_FrameRefF32, CEOI_FrameRef32},
- {"CeOp_Conv_I32_F64", CEOI_FrameRefF64, CEOI_FrameRef32},
- {"CeOp_Conv_I64_F32", CEOI_FrameRefF32, CEOI_FrameRef64},
- {"CeOp_Conv_I64_F64", CEOI_FrameRefF64, CEOI_FrameRef64},
- {"CeOp_Conv_U8_U16", CEOI_FrameRef16, CEOI_FrameRef8},
- {"CeOp_Conv_U8_U32", CEOI_FrameRef32, CEOI_FrameRef8},
- {"CeOp_Conv_U8_U64", CEOI_FrameRef64, CEOI_FrameRef8},
- {"CeOp_Conv_U8_F32", CEOI_FrameRefF32, CEOI_FrameRef8},
- {"CeOp_Conv_U8_F64", CEOI_FrameRefF64, CEOI_FrameRef8},
- {"CeOp_Conv_U16_U32", CEOI_FrameRef32, CEOI_FrameRef16},
- {"CeOp_Conv_U16_U64", CEOI_FrameRef64, CEOI_FrameRef16},
- {"CeOp_Conv_U16_F32", CEOI_FrameRefF32, CEOI_FrameRef16},
- {"CeOp_Conv_U16_F64", CEOI_FrameRefF64, CEOI_FrameRef16},
- {"CeOp_Conv_U32_U64", CEOI_FrameRef64, CEOI_FrameRef32},
- {"CeOp_Conv_U32_F32", CEOI_FrameRefF32, CEOI_FrameRef32},
- {"CeOp_Conv_U32_F64", CEOI_FrameRefF64, CEOI_FrameRef32},
- {"CeOp_Conv_U64_F32", CEOI_FrameRefF32, CEOI_FrameRef64},
- {"CeOp_Conv_U64_F64", CEOI_FrameRefF64, CEOI_FrameRef64},
- {"CeOp_Conv_F32_I8", CEOI_FrameRef8, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_I16", CEOI_FrameRef16, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_I32", CEOI_FrameRef32, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_I64", CEOI_FrameRef64, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_U8", CEOI_FrameRef8, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_U16", CEOI_FrameRef16, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_U32", CEOI_FrameRef32, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_U64", CEOI_FrameRef64, CEOI_FrameRefF32},
- {"CeOp_Conv_F32_F64", CEOI_FrameRefF64, CEOI_FrameRefF32},
- {"CeOp_Conv_F64_I8", CEOI_FrameRef8, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_I16", CEOI_FrameRef16, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_I32", CEOI_FrameRef32, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_I64", CEOI_FrameRef64, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_U8", CEOI_FrameRef8, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_U16", CEOI_FrameRef16, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_U32", CEOI_FrameRef32, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_U64", CEOI_FrameRef64, CEOI_FrameRefF64},
- {"CeOp_Conv_F64_F32", CEOI_FrameRefF32, CEOI_FrameRefF64},
- CEOPINFO_SIZED_NUMERIC_PLUSF_2("Abs", CEOI_FrameRef, CEOI_FrameRef),
- {"AddConst_I8", CEOI_FrameRef8, CEOI_FrameRef8, CEOI_IMM8},
- {"AddConst_I16", CEOI_FrameRef16, CEOI_FrameRef16, CEOI_IMM16},
- {"AddConst_I32", CEOI_FrameRef32, CEOI_FrameRef32, CEOI_IMM32},
- {"AddConst_I64", CEOI_FrameRef64, CEOI_FrameRef64, CEOI_IMM64},
- {"AddConst_F32", CEOI_FrameRefF32, CEOI_FrameRefF32, CEOI_IMMF32},
- {"AddConst_F64", CEOI_FrameRefF64, CEOI_FrameRefF64, CEOI_IMMF64},
- CEOPINFO_SIZED_NUMERIC_PLUSF_3("Add", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3("Sub", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3("Mul", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3("SDiv", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("UDiv", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3("SMod", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("UMod", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("And", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("Or", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("Xor", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("Shl", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3("Shr", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_UNUMERIC_3("Shr", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Acos", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Asin", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Atan", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_3("Atan2", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Ceiling", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Cos", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Cosh", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Exp", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Floor", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Log", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Log10", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_3("Pow", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Round", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Sin", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Sinh", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Sqrt", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Tan", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_FLOAT_2("Tanh", CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8("Cmp_EQ", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8("Cmp_NE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8("Cmp_SLT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3_RESULT8("Cmp_ULT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8("Cmp_SLE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3_RESULT8("Cmp_ULE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8("Cmp_SGT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3_RESULT8("Cmp_UGT", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_3_RESULT8("Cmp_SGE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_3_RESULT8("Cmp_UGE", CEOI_FrameRef, CEOI_FrameRef, CEOI_FrameRef),
- CEOPINFO_SIZED_NUMERIC_PLUSF_2_RESULT8("Neg", CEOI_FrameRef, CEOI_FrameRef),
- {"Not_I1", CEOI_FrameRef8, CEOI_FrameRef8},
- CEOPINFO_SIZED_NUMERIC_2("Not", CEOI_FrameRef, CEOI_FrameRef),
- };
- static_assert(BF_ARRAY_COUNT(gOpInfo) == (int)CeOp_COUNT, "gOpName incorrect size");
- //////////////////////////////////////////////////////////////////////////
- static int ToString(float d, char* outStr, bool roundTrip)
- {
- if (!roundTrip)
- {
- int digits;
- if (d > 100000)
- digits = 1;
- else if (d > 10000)
- digits = 2;
- else if (d > 1000)
- digits = 3;
- else if (d > 100)
- digits = 4;
- else if (d > 10)
- digits = 5;
- else
- digits = 6;
- sprintf(outStr, "%1.*f", digits, d);
- }
- else
- sprintf(outStr, "%1.9g", d);
- int len = (int)strlen(outStr);
- for (int i = 0; outStr[i] != 0; i++)
- {
- if (outStr[i] == '.')
- {
- int checkC = len - 1;
- while (true)
- {
- char c = outStr[checkC];
- if (c == '.')
- {
- return checkC;
- }
- else if (c == 'e')
- {
- return len;
- }
- else if (c != '0')
- {
- for (int j = i + 1; j <= checkC; j++)
- if (outStr[j] == 'e')
- return len;
- return checkC + 1;
- }
- checkC--;
- }
- }
- }
- if ((len == 3) && (outStr[0] == 'i'))
- {
- strcpy(outStr, "Infinity");
- return 8;
- }
- if ((len == 4) && (outStr[0] == '-') && (outStr[1] == 'i'))
- {
- strcpy(outStr, "-Infinity");
- return 9;
- }
- if ((len == 9) && (outStr[0] == '-') && (outStr[1] == 'n')) //-nan(xxx)
- {
- strcpy(outStr, "NaN");
- return 3;
- }
- return len;
- }
- static int ToString(double d, char* outStr, bool roundTrip)
- {
- if (!roundTrip)
- {
- int digits;
- if (d < 0)
- {
- if (d < -10000000000)
- {
- sprintf(outStr, "%g", d);
- }
- else
- {
- if (d < -1000000000)
- digits = 1;
- else if (d < -100000000)
- digits = 2;
- else if (d < -10000000)
- digits = 3;
- else if (d < -1000000)
- digits = 4;
- else if (d < -100000)
- digits = 5;
- else if (d < -10000)
- digits = 6;
- else if (d < -1000)
- digits = 7;
- else if (d < -100)
- digits = 8;
- else if (d < -10)
- digits = 9;
- else
- digits = 10;
- sprintf(outStr, "%1.*f", digits, d);
- }
- }
- else
- {
- if (d > 10000000000)
- {
- sprintf(outStr, "%g", d);
- }
- else
- {
- if (d > 1000000000)
- digits = 1;
- else if (d > 100000000)
- digits = 2;
- else if (d > 10000000)
- digits = 3;
- else if (d > 1000000)
- digits = 4;
- else if (d > 100000)
- digits = 5;
- else if (d > 10000)
- digits = 6;
- else if (d > 1000)
- digits = 7;
- else if (d > 100)
- digits = 8;
- else if (d > 10)
- digits = 9;
- else
- digits = 10;
- sprintf(outStr, "%1.*f", digits, d);
- }
- }
- }
- else
- sprintf(outStr, "%1.17g", d);
- int len = (int)strlen(outStr);
- for (int i = 0; outStr[i] != 0; i++)
- {
- if (outStr[i] == '.')
- {
- int checkC = len - 1;
- while (true)
- {
- char c = outStr[checkC];
- if (c == '.')
- {
- return checkC;
- }
- else if (c == 'e')
- {
- return len;
- }
- else if (c != '0')
- {
- for (int j = i + 1; j <= checkC; j++)
- if (outStr[j] == 'e')
- return len;
- return checkC + 1;
- }
- checkC--;
- }
- }
- }
- if ((len == 3) && (outStr[0] == 'i'))
- {
- strcpy(outStr, "Infinity");
- return 8;
- }
- if ((len == 4) && (outStr[0] == '-') && (outStr[1] == 'i'))
- {
- strcpy(outStr, "-Infinity");
- return 9;
- }
- if ((len == 9) && (outStr[0] == '-') && (outStr[1] == 'n')) //-nan(xxx)
- {
- strcpy(outStr, "NaN");
- return 3;
- }
- return len;
- }
- static int FloatToString(float d, char* outStr)
- {
- return ::ToString(d, outStr, false);
- }
- static int DoubleToString(double d, char* outStr)
- {
- return ::ToString(d, outStr, false);
- }
- //////////////////////////////////////////////////////////////////////////
- String CeDbgMethodRef::ToString()
- {
- if (!mMethodRef)
- return mNameMod;
- BfMethodInstance* methodInstance = mMethodRef;
- auto module = methodInstance->GetOwner()->mModule;
- String name = module->MethodToString(methodInstance);
- if (!mNameMod.IsEmpty())
- {
- for (int i = 1; i < (int)name.length(); i++)
- {
- if (name[i] == '(')
- {
- char prevC = name[i - 1];
- if ((::isalnum((uint8)prevC)) || (prevC == '_'))
- {
- name.Insert(i, mNameMod);
- break;
- }
- }
- }
- }
- return name;
- }
- //////////////////////////////////////////////////////////////////////////
- CeInternalData::~CeInternalData()
- {
- switch (mKind)
- {
- case Kind_File:
- BfpFile_Release(mFile);
- break;
- case Kind_FindFileData:
- BfpFindFileData_Release(mFindFileData);
- break;
- case Kind_Spawn:
- BfpSpawn_Release(mSpawn);
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- CeFunction::~CeFunction()
- {
- BF_ASSERT(mId == -1);
- for (auto innerFunc : mInnerFunctions)
- delete innerFunc;
- delete mCeInnerFunctionInfo;
- delete mDbgInfo;
- BfLogSys(mCeMachine->mCompiler->mSystem, "CeFunction::~CeFunction %p\n", this);
- }
- BfTypeInstance* CeFunction::GetOwner()
- {
- if (mCeFunctionInfo != NULL)
- return mCeFunctionInfo->GetOwner();
- if (mCeInnerFunctionInfo != NULL)
- return mCeInnerFunctionInfo->mOwner->GetOwner();
- return NULL;
- }
- void CeFunction::Print()
- {
- CeDumpContext dumpCtx;
- dumpCtx.mCeFunction = this;
- dumpCtx.mStart = &mCode[0];
- dumpCtx.mPtr = dumpCtx.mStart;
- dumpCtx.mEnd = dumpCtx.mPtr + mCode.mSize;
- dumpCtx.Dump();
- String methodName;
- if (mMethodInstance != NULL)
- methodName = mMethodInstance->GetOwner()->mModule->MethodToString(mMethodInstance);
- OutputDebugStrF("Code for %s:\n%s\n", methodName.c_str(), dumpCtx.mStr.c_str());
- }
- void CeFunction::UnbindBreakpoints()
- {
- for (auto kv : mBreakpoints)
- mCode[kv.mKey] = kv.mValue.mPrevOpCode;
- mBreakpoints.Clear();
- }
- CeEmitEntry* CeFunction::FindEmitEntry(int instIdx, int* entryIdx)
- {
- int i = 0;
- CeEmitEntry* emitEntry = NULL;
- if (!mCode.IsEmpty())
- {
- int lo = 0;
- int hi = mEmitTable.size() - 1;
- while (lo <= hi)
- {
- i = (lo + hi) / 2;
- emitEntry = &mEmitTable.mVals[i];
- //int c = midVal <=> value;
- if (emitEntry->mCodePos == instIdx) break;
- if (emitEntry->mCodePos < instIdx)
- lo = i + 1;
- else
- hi = i - 1;
- }
- if ((emitEntry != NULL) && (emitEntry->mCodePos > instIdx) && (i > 0))
- {
- emitEntry = &mEmitTable.mVals[i - 1];
- if (entryIdx != NULL)
- *entryIdx = i - 1;
- }
- else if (entryIdx != NULL)
- *entryIdx = i;
- }
- return emitEntry;
- }
- // This is for "safe" retrieval from within CeDebugger
- int CeFunction::SafeGetId()
- {
- #ifdef BF_PLATFORM_WINDOWS
- __try
- {
- return mId;
- }
- __except (EXCEPTION_EXECUTE_HANDLER)
- {
- }
- return 0;
- #else
- return mId;
- #endif
- }
- //////////////////////////////////////////////////////////////////////////
- CeFunctionInfo::~CeFunctionInfo()
- {
- delete mCeFunction;
- }
- //////////////////////////////////////////////////////////////////////////
- #define CE_GET(T) *((T*)(mPtr += sizeof(T)) - 1)
- void CeDumpContext::DumpOperandInfo(CeOperandInfoKind operandInfoKind)
- {
- switch (operandInfoKind)
- {
- case CEOI_FrameRef:
- case CEOI_FrameRef8:
- case CEOI_FrameRef16:
- case CEOI_FrameRef32:
- case CEOI_FrameRef64:
- case CEOI_FrameRefF32:
- case CEOI_FrameRefF64:
- {
- int32 addr = CE_GET(int32);
- if (mCeFunction->mDbgInfo != NULL)
- {
- CeDbgVariable* dbgVar = NULL;
- if (mVarMap.TryGetValue(addr, &dbgVar))
- {
- mStr += dbgVar->mName;
- mStr += "@";
- }
- }
- switch (operandInfoKind)
- {
- case CEOI_FrameRef8:
- mStr += "int8";
- break;
- case CEOI_FrameRef16:
- mStr += "int16";
- break;
- case CEOI_FrameRef32:
- mStr += "int32";
- break;
- case CEOI_FrameRef64:
- mStr += "int64";
- break;
- case CEOI_FrameRefF32:
- mStr += "float";
- break;
- case CEOI_FrameRefF64:
- mStr += "double";
- break;
- }
- char str[64];
- if (addr >= 0)
- sprintf(str, "[FR+0x%X]", addr);
- else
- sprintf(str, "[FR-0x%X]", -addr);
- mStr += str;
- }
- break;
- case CEOI_IMM8:
- {
- int32 val = CE_GET(int8);
- char str[64];
- sprintf(str, "%d", val);
- mStr += str;
- }
- break;
- case CEOI_IMM16:
- {
- int32 val = CE_GET(int16);
- char str[64];
- sprintf(str, "%d", val);
- mStr += str;
- }
- break;
- case CEOI_IMM32:
- {
- int32 val = CE_GET(int32);
- char str[64];
- sprintf(str, "%d", val);
- mStr += str;
- }
- break;
- case CEOI_IMM64:
- {
- int64 val = CE_GET(int64);
- char str[64];
- sprintf(str, "%lld", (long long)val);
- mStr += str;
- }
- break;
- case CEOI_IMM_VAR:
- {
- mStr += '[';
- int32 size = CE_GET(int32);
- for (int i = 0; i < size; i++)
- {
- if (i != 0)
- mStr += ", ";
- uint8 val = CE_GET(uint8);
- char str[64];
- sprintf(str, "%X", val);
- mStr += str;
- }
- mStr += ']';
- }
- break;
- case CEOI_JMPREL:
- {
- int32 val = CE_GET(int32);
- char str[64];
- sprintf(str, "JMP:%04X", (int32)(val + (mPtr - mStart)));
- mStr += str;
- mJmp = (int32)(val + (mPtr - mStart));
- }
- break;
- }
- }
- void CeDumpContext::Next()
- {
- CeOp op = CE_GET(CeOp);
- if (op == CeOp_DbgBreak)
- {
- int instIdx = mPtr - mCeFunction->mCode.mVals - 2;
- CeBreakpointBind* breakpointEntry = NULL;
- if (mCeFunction->mBreakpoints.TryGetValue(instIdx, &breakpointEntry))
- {
- op = breakpointEntry->mPrevOpCode;
- }
- }
- CeOpInfo& opInfo = gOpInfo[op];
- auto argPtr = mPtr;
- if (mCeFunction->mDbgInfo != NULL)
- {
- for (auto& dbgVar : mCeFunction->mDbgInfo->mVariables)
- {
- mVarMap[dbgVar.mValue.mFrameOfs] = &dbgVar;
- }
- }
- int32 sizeX = -1;
- if ((opInfo.mFlags & CeOpInfoFlag_SizeX) != 0)
- {
- sizeX = CE_GET(int);
- }
- if (opInfo.mResultKind != CEOI_None)
- {
- DumpOperandInfo(opInfo.mResultKind);
- mStr += " = ";
- }
- mStr += opInfo.mName;
- if (sizeX != -1)
- {
- mStr += StrFormat(":%d", sizeX);
- }
- if (opInfo.mOperandA != CEOI_None)
- {
- mStr += " ";
- DumpOperandInfo(opInfo.mOperandA);
- }
- if (opInfo.mOperandB != CEOI_None)
- {
- mStr += ", ";
- DumpOperandInfo(opInfo.mOperandB);
- }
- if (opInfo.mOperandC != CEOI_None)
- {
- mStr += ", ";
- DumpOperandInfo(opInfo.mOperandC);
- }
- auto endPtr = mPtr;
- if (op == CeOp_GetMethod)
- {
- mPtr = argPtr;
- CE_GET(int32);
- int methodIdx = CE_GET(int32);
- auto& callEntry = mCeFunction->mCallTable[methodIdx];
- auto methodInstance = callEntry.mFunctionInfo->mMethodInstance;
- auto ceModule = mCeFunction->mCeMachine->mCeModule;
- if (!callEntry.mFunctionInfo->mMethodRef.IsNull())
- {
- auto methodRef = callEntry.mFunctionInfo->mMethodRef;
- auto methodDef = methodRef.mTypeInstance->mTypeDef->mMethods[methodRef.mMethodNum];
- methodInstance = ceModule->GetMethodInstance(methodRef.mTypeInstance, methodDef,
- methodRef.mMethodGenericArguments).mMethodInstance;
- }
- if (methodInstance != NULL)
- mStr += StrFormat(" ; %s", ceModule->MethodToString(methodInstance).c_str());
- mPtr = endPtr;
- }
- }
- void CeDumpContext::Dump()
- {
- if (!mCeFunction->mGenError.IsEmpty())
- mStr += StrFormat("Gen Error: %s\n", mCeFunction->mGenError.c_str());
- mStr += StrFormat("Frame Size: %d\n", mCeFunction->mFrameSize);
- uint8* start = mPtr;
- int curEmitIdx = 0;
- CeEmitEntry* curEmitEntry = NULL;
- while (mPtr < mEnd)
- {
- int ofs = mPtr - start;
- while ((curEmitIdx < mCeFunction->mEmitTable.mSize - 1) && (ofs >= mCeFunction->mEmitTable[curEmitIdx + 1].mCodePos))
- curEmitIdx++;
- if (curEmitIdx < mCeFunction->mEmitTable.mSize)
- curEmitEntry = &mCeFunction->mEmitTable[curEmitIdx];
- mStr += StrFormat("%04X: ", ofs);
- Next();
- if ((curEmitEntry != NULL) && (curEmitEntry->mScope != -1))
- {
- mStr += StrFormat(" @%d[%s:%d]", curEmitIdx, GetFileName(mCeFunction->mDbgScopes[curEmitEntry->mScope].mFilePath).c_str(),
- curEmitEntry->mLine + 1, curEmitEntry->mColumn + 1);
- }
- mStr += "\n";
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CeBuilder::Fail(const StringImpl& str)
- {
- if (!mCeFunction->mGenError.IsEmpty())
- return;
- String errStr = StrFormat("Failure during comptime generation of %s: %s", mBeFunction->mName.c_str(), str.c_str());
- if (mCurDbgLoc != NULL)
- {
- String filePath;
- mCurDbgLoc->GetDbgFile()->GetFilePath(filePath);
- errStr += StrFormat(" at line %d:%d in %s", mCurDbgLoc->mLine + 1, mCurDbgLoc->mColumn + 1, filePath.c_str());
- }
- mCeFunction->mGenError = errStr;
- }
- void CeBuilder::Emit(uint8 val)
- {
- mCeFunction->mCode.Add((uint8)val);
- }
- void CeBuilder::Emit(CeOp val)
- {
- *(CeOp*)mCeFunction->mCode.GrowUninitialized(sizeof(CeOp)) = val;
- }
- void CeBuilder::EmitSizedOp(CeOp val, int size)
- {
- CeSizeClass sizeClass = GetSizeClass(size);
- Emit((CeOp)(val + sizeClass));
- if (sizeClass == CeSizeClass_X)
- Emit((int32)size);
- if ((CeOp)(val + sizeClass) == CeOp_AddConst_I64)
- {
- NOP;
- }
- }
- void CeBuilder::Emit(int32 val)
- {
- *(int32*)mCeFunction->mCode.GrowUninitialized(4) = val;
- }
- void CeBuilder::Emit(int64 val)
- {
- *(int64*)mCeFunction->mCode.GrowUninitialized(8) = val;
- }
- void CeBuilder::Emit(bool val)
- {
- BF_FATAL("Invalid emit");
- }
- void CeBuilder::Emit(void* ptr, int size)
- {
- memcpy(mCeFunction->mCode.GrowUninitialized(size), ptr, size);
- }
- void CeBuilder::EmitZeroes(int size)
- {
- for (int i = 0; i < size; i++)
- Emit((uint8)0);
- }
- void CeBuilder::EmitJump(CeOp op, const CeOperand& block)
- {
- BF_ASSERT(block.mKind == CeOperandKind_Block);
- Emit(op);
- CeJumpEntry jumpEntry;
- jumpEntry.mBlockIdx = block.mBlockIdx;
- jumpEntry.mEmitPos = GetCodePos();
- mJumpTable.Add(jumpEntry);
- Emit((int32)0);
- }
- void CeBuilder::EmitBinarySwitchSection(BeSwitchInst* switchInst, int startIdx, int endIdx)
- {
- // This is an empirically determined binary switching limit
- if (endIdx - startIdx >= 18)
- {
- // int gteLabel = mCurLabelIdx++;
- //
- // auto mcDefaultBlock = GetOperand(switchInst->mDefaultBlock);
- //
- // int midIdx = startIdx + (endIdx - startIdx) / 2;
- // auto& switchCase = switchInst->mCases[midIdx];
- // auto switchBlock = GetOperand(switchCase.mBlock);
- // auto mcValue = GetOperand(switchInst->mValue);
- // auto valueType = GetType(mcValue);
- //
- // AllocInst(BeMCInstKind_Cmp, mcValue, GetOperand(switchCase.mValue));
- // AllocInst(BeMCInstKind_CondBr, BeMCOperand::FromLabel(gteLabel), BeMCOperand::FromCmpKind(BeCmpKind_SGE));
- // switchBlock.mBlock->AddPred(mActiveBlock);
- //
- // CreateBinarySwitchSection(switchInst, startIdx, midIdx);
- // AllocInst(BeMCInstKind_Br, mcDefaultBlock);
- // CreateLabel(-1, gteLabel);
- // CreateBinarySwitchSection(switchInst, midIdx, endIdx);
- // return;
- }
- for (int caseIdx = startIdx; caseIdx < endIdx; caseIdx++)
- {
- auto& switchCase = switchInst->mCases[caseIdx];
- auto switchBlock = GetOperand(switchCase.mBlock);
- auto mcValue = GetOperand(switchInst->mValue);
- CeOperand result;
- EmitBinaryOp(CeOp_Cmp_EQ_I8, CeOp_Cmp_EQ_F32, mcValue, GetOperand(switchCase.mValue), result);
- EmitJump(CeOp_JmpIf, switchBlock);
- EmitFrameOffset(result);
- }
- }
- CeOperand CeBuilder::EmitLoad(CeOperand ceTarget, int loadRefCount)
- {
- CeOperand result;
- if (ceTarget.mKind == CeOperandKind_AllocaAddr)
- {
- if (loadRefCount <= 1)
- {
- result = ceTarget;
- result.mKind = CeOperandKind_FrameOfs;
- }
- else
- {
- ceTarget.mKind = CeOperandKind_FrameOfs;
- result = FrameAlloc(ceTarget.mType);
- EmitSizedOp(CeOp_Move_8, ceTarget, NULL, true);
- Emit((int32)result.mFrameOfs);
- }
- }
- else
- {
- BF_ASSERT(ceTarget.mType->IsPointer());
- auto pointerType = (BePointerType*)ceTarget.mType;
- auto elemType = pointerType->mElementType;
- CeOperand refOperand = ceTarget;
- refOperand.mType = elemType;
- EmitSizedOp(CeOp_Load_8, refOperand, &result, true);
- }
- return result;
- }
- int CeBuilder::GetCodePos()
- {
- return (int)mCeFunction->mCode.size();
- }
- void CeBuilder::EmitFrameOffset(const CeOperand& val)
- {
- BF_ASSERT(val.mKind == CeOperandKind_FrameOfs);
- Emit((int32)val.mFrameOfs);
- }
- void CeBuilder::FlushPhi(CeBlock* ceBlock, int targetBlockIdx)
- {
- for (int i = 0; i < (int)ceBlock->mPhiOutgoing.size(); i++)
- {
- auto& phiOutgoing = ceBlock->mPhiOutgoing[i];
- if (phiOutgoing.mPhiBlockIdx == targetBlockIdx)
- {
- auto targetPhi = GetOperand(phiOutgoing.mPhiInst);
- auto mcVal = GetOperand(phiOutgoing.mPhiValue);
- EmitSizedOp(CeOp_Move_8, mcVal, NULL, true);
- EmitFrameOffset(targetPhi);
- ceBlock->mPhiOutgoing.RemoveAt(i);
- break;
- }
- }
- }
- void CeBuilder::EmitBinaryOp(CeOp iOp, CeOp fOp, const CeOperand& lhs, const CeOperand& rhs, CeOperand& result)
- {
- CeOp op = iOp;
- if (lhs.mType->IsIntable())
- {
- if (lhs.mType->mSize == 1)
- op = iOp;
- else if (lhs.mType->mSize == 2)
- op = (CeOp)(iOp + 1);
- else if (lhs.mType->mSize == 4)
- op = (CeOp)(iOp + 2);
- else if (lhs.mType->mSize == 8)
- op = (CeOp)(iOp + 3);
- else
- Fail("Invalid int operand size");
- }
- else if (lhs.mType->IsFloat())
- {
- BF_ASSERT(fOp != CeOp_InvalidOp);
- if (lhs.mType->mSize == 4)
- op = fOp;
- else if (lhs.mType->mSize == 8)
- op = (CeOp)(fOp + 1);
- else
- Fail("Invalid float operand size");
- }
- else
- Fail("Invalid binary operand");
- Emit(op);
- if (!result)
- result = FrameAlloc(lhs.mType);
- EmitFrameOffset(result);
- EmitFrameOffset(lhs);
- EmitFrameOffset(rhs);
- }
- CeOperand CeBuilder::EmitNumericCast(const CeOperand& ceValue, BeType* toType, bool valSigned, bool toSigned)
- {
- if (ceValue.mKind == CeOperandKind_Immediate)
- {
- CeOperand newVal = ceValue;
- newVal.mType = toType;
- return newVal;
- }
- CeOperand result;
- auto fromType = ceValue.mType;
- if (fromType == toType)
- {
- // If it's just a sign change then leave it alone
- result = ceValue;
- }
- else
- {
- if ((toType->IsIntable()) && (fromType->IsIntable()) && (toType->mSize <= fromType->mSize))
- {
- // For truncating values, no actual instructions are needed
- // Note that a copy is not needed because of SSA rules
- result = ceValue;
- result.mType = toType;
- }
- else
- {
- result = FrameAlloc(toType);
- CeOp op = CeOp_InvalidOp;
- BeTypeCode fromTypeCode = fromType->mTypeCode;
- BeTypeCode toTypeCode = toType->mTypeCode;
- if ((valSigned) && (toSigned))
- {
- switch (fromTypeCode)
- {
- case BeTypeCode_Int8:
- switch (toTypeCode)
- {
- case BeTypeCode_Int16:
- op = CeOp_Conv_I8_I16;
- break;
- case BeTypeCode_Int32:
- op = CeOp_Conv_I8_I32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_I8_I64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_I8_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_I8_F64;
- break;
- }
- break;
- case BeTypeCode_Int16:
- switch (toTypeCode)
- {
- case BeTypeCode_Int32:
- op = CeOp_Conv_I16_I32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_I16_I64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_I16_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_I16_F64;
- break;
- }
- break;
- case BeTypeCode_Int32:
- switch (toTypeCode)
- {
- case BeTypeCode_Int64:
- op = CeOp_Conv_I32_I64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_I32_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_I32_F64;
- break;
- }
- break;
- case BeTypeCode_Int64:
- switch (toTypeCode)
- {
- case BeTypeCode_Float:
- op = CeOp_Conv_I64_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_I64_F64;
- break;
- }
- break;
- case BeTypeCode_Float:
- switch (toTypeCode)
- {
- case BeTypeCode_Int8:
- op = CeOp_Conv_F32_I8;
- break;
- case BeTypeCode_Int16:
- op = CeOp_Conv_F32_I16;
- break;
- case BeTypeCode_Int32:
- op = CeOp_Conv_F32_I32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_F32_I64;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_F32_F64;
- break;
- }
- break;
- case BeTypeCode_Double:
- switch (toTypeCode)
- {
- case BeTypeCode_Int8:
- op = CeOp_Conv_F64_I8;
- break;
- case BeTypeCode_Int16:
- op = CeOp_Conv_F64_I16;
- break;
- case BeTypeCode_Int32:
- op = CeOp_Conv_F64_I32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_F64_I64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_F64_F32;
- break;
- }
- break;
- }
- }
- else
- {
- switch (fromTypeCode)
- {
- case BeTypeCode_Int8:
- switch (toTypeCode)
- {
- case BeTypeCode_Int16:
- op = CeOp_Conv_U8_U16;
- break;
- case BeTypeCode_Int32:
- op = CeOp_Conv_U8_U32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_U8_U64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_U8_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_U8_F64;
- break;
- }
- break;
- case BeTypeCode_Int16:
- switch (toTypeCode)
- {
- case BeTypeCode_Int32:
- op = CeOp_Conv_U16_U32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_U16_U64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_U16_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_U16_F64;
- break;
- }
- break;
- case BeTypeCode_Int32:
- switch (toTypeCode)
- {
- case BeTypeCode_Int64:
- op = CeOp_Conv_U32_U64;
- break;
- case BeTypeCode_Float:
- op = CeOp_Conv_U32_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_U32_F64;
- break;
- }
- break;
- case BeTypeCode_Int64:
- switch (toTypeCode)
- {
- case BeTypeCode_Float:
- op = CeOp_Conv_I64_F32;
- break;
- case BeTypeCode_Double:
- op = CeOp_Conv_I64_F64;
- break;
- }
- break;
- case BeTypeCode_Float:
- switch (toTypeCode)
- {
- case BeTypeCode_Int8:
- op = CeOp_Conv_F32_U8;
- break;
- case BeTypeCode_Int16:
- op = CeOp_Conv_F32_U16;
- break;
- case BeTypeCode_Int32:
- op = CeOp_Conv_F32_U32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_F32_U64;
- break;
- }
- break;
- case BeTypeCode_Double:
- switch (toTypeCode)
- {
- case BeTypeCode_Int8:
- op = CeOp_Conv_F64_U8;
- break;
- case BeTypeCode_Int16:
- op = CeOp_Conv_F64_U16;
- break;
- case BeTypeCode_Int32:
- op = CeOp_Conv_F64_U32;
- break;
- case BeTypeCode_Int64:
- op = CeOp_Conv_F64_U64;
- break;
- }
- break;
- }
- }
- if (op == CeOp_InvalidOp)
- {
- Fail("Invalid conversion op");
- }
- else
- {
- Emit(op);
- EmitFrameOffset(result);
- EmitFrameOffset(ceValue);
- }
- }
- }
- return result;
- }
- void CeBuilder::EmitUnaryOp(CeOp iOp, CeOp fOp, const CeOperand& val, CeOperand& result)
- {
- CeOp op = iOp;
- if (val.mType->IsIntable())
- {
- if (val.mType->mSize == 1)
- op = iOp;
- else if (val.mType->mSize == 2)
- op = (CeOp)(iOp + 1);
- else if (val.mType->mSize == 4)
- op = (CeOp)(iOp + 2);
- else if (val.mType->mSize == 8)
- op = (CeOp)(iOp + 3);
- else
- Fail("Invalid int operand size");
- }
- else if (val.mType->IsFloat())
- {
- if (val.mType->mSize == 4)
- op = fOp;
- else if (val.mType->mSize == 8)
- op = (CeOp)(fOp + 1);
- else
- Fail("Invalid float operand size");
- }
- else
- Fail("Invalid unary operand");
- Emit(op);
- result = FrameAlloc(val.mType);
- EmitFrameOffset(result);
- EmitFrameOffset(val);
- }
- void CeBuilder::EmitSizedOp(CeOp baseOp, const CeOperand& operand, CeOperand* outResult, bool allowNonStdSize)
- {
- bool isStdSize = true;
- CeOp op = CeOp_InvalidOp;
- if (operand.mType->mSize == 1)
- op = baseOp;
- else if (operand.mType->mSize == 2)
- op = (CeOp)(baseOp + 1);
- else if (operand.mType->mSize == 4)
- op = (CeOp)(baseOp + 2);
- else if (operand.mType->mSize == 8)
- op = (CeOp)(baseOp + 3);
- else
- {
- isStdSize = false;
- op = (CeOp)(baseOp + 4);
- }
- Emit(op);
- if (!isStdSize)
- {
- if (!allowNonStdSize)
- Fail("Invalid operand size");
- Emit((int32)operand.mType->mSize);
- }
- if (outResult != NULL)
- {
- *outResult = FrameAlloc(operand.mType);
- EmitFrameOffset(*outResult);
- }
- EmitFrameOffset(operand);
- }
- CeOperand CeBuilder::FrameAlloc(BeType* type)
- {
- mFrameSize += type->mSize;
- CeOperand result;
- result.mKind = CeOperandKind_FrameOfs;
- result.mFrameOfs = -mFrameSize;
- result.mType = type;
- return result;
- }
- CeOperand CeBuilder::EmitConst(int64 val, int size)
- {
- BeType* type = mIntPtrType;
- switch (size)
- {
- case 1:
- type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int8);
- break;
- case 2:
- type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int16);
- break;
- case 4:
- type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int32);
- break;
- case 8:
- type = mCeMachine->GetBeContext()->GetPrimitiveType(BeTypeCode_Int64);
- break;
- default:
- Fail("Bad const size");
- }
- auto result = FrameAlloc(type);
- EmitSizedOp(CeOp_Const_8, type->mSize);
- EmitFrameOffset(result);
- Emit(&val, size);
- return result;
- }
- int CeBuilder::GetCallTableIdx(BeFunction* beFunction, CeOperand* outOperand)
- {
- int* callIdxPtr = NULL;
- if (mFunctionMap.TryAdd(beFunction, NULL, &callIdxPtr))
- {
- CeFunctionInfo* ceFunctionInfo = NULL;
- mCeMachine->mNamedFunctionMap.TryGetValue(beFunction->mName, &ceFunctionInfo);
- if (ceFunctionInfo != NULL)
- ceFunctionInfo->mRefCount++;
- else
- {
- if (outOperand != NULL)
- {
- auto checkBuilder = this;
- if (checkBuilder->mParentBuilder != NULL)
- checkBuilder = checkBuilder->mParentBuilder;
- int innerFunctionIdx = 0;
- if (checkBuilder->mInnerFunctionMap.TryGetValue(beFunction, &innerFunctionIdx))
- {
- auto innerFunction = checkBuilder->mCeFunction->mInnerFunctions[innerFunctionIdx];
- if (innerFunction->mInitializeState < CeFunction::InitializeState_Initialized)
- mCeMachine->PrepareFunction(innerFunction, checkBuilder);
- CeOperand result = FrameAlloc(mCeMachine->GetBeContext()->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
- Emit(CeOp_GetMethod_Inner);
- EmitFrameOffset(result);
- Emit((int32)innerFunctionIdx);
- *outOperand = result;
- return -1;
- }
- }
- Fail(StrFormat("Unable to locate method %s", beFunction->mName.c_str()));
- }
- CeCallEntry callEntry;
- callEntry.mFunctionInfo = ceFunctionInfo;
- *callIdxPtr = (int)mCeFunction->mCallTable.size();
- mCeFunction->mCallTable.Add(callEntry);
- if ((ceFunctionInfo != NULL) && (mCeFunction->mCeFunctionInfo != NULL))
- {
- auto callerType = mCeFunction->GetOwner();
- auto calleeType = ceFunctionInfo->GetOwner();
- if ((callerType != NULL) && (calleeType != NULL))
- {
- // This will generally already be set, but there are some error cases (such as duplicate type names)
- // where this will not be set yet
- callerType->mModule->AddDependency(calleeType, callerType, BfDependencyMap::DependencyFlag_Calls);
- }
- }
- }
- return *callIdxPtr;
- }
- CeOperand CeBuilder::GetOperand(BeValue* value, bool allowAlloca, bool allowImmediate)
- {
- if (value == NULL)
- return CeOperand();
- BeType* errorType = mIntPtrType;
- CeErrorKind errorKind = CeErrorKind_None;
- switch (value->GetTypeId())
- {
- case BeGlobalVariable::TypeId:
- {
- auto globalVar = (BeGlobalVariable*)value;
- if (globalVar->mName.StartsWith("__bfStrObj"))
- {
- int stringId = atoi(globalVar->mName.c_str() + 10);
- int* stringTableIdxPtr = NULL;
- if (mStringMap.TryAdd(stringId, NULL, &stringTableIdxPtr))
- {
- *stringTableIdxPtr = (int)mCeFunction->mStringTable.size();
- CeStringEntry ceStringEntry;
- ceStringEntry.mStringId = stringId;
- mCeFunction->mStringTable.Add(ceStringEntry);
- }
- auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
- Emit(CeOp_GetString);
- EmitFrameOffset(result);
- Emit((int32)*stringTableIdxPtr);
- return result;
- }
- else if (globalVar->mName.StartsWith("__bfStrData"))
- {
- int stringId = atoi(globalVar->mName.c_str() + 11);
- int* stringTableIdxPtr = NULL;
- if (mStringMap.TryAdd(stringId, NULL, &stringTableIdxPtr))
- {
- *stringTableIdxPtr = (int)mCeFunction->mStringTable.size();
- CeStringEntry ceStringEntry;
- ceStringEntry.mStringId = stringId;
- mCeFunction->mStringTable.Add(ceStringEntry);
- }
- auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
- Emit(CeOp_GetString);
- EmitFrameOffset(result);
- Emit((int32)*stringTableIdxPtr);
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(
- mCeMachine->mCeModule->mCompiler->mStringTypeDef, BfPopulateType_Data);
- Emit(CeOp_AddConst_I32);
- EmitFrameOffset(result);
- EmitFrameOffset(result);
- Emit((int32)stringTypeInst->mInstSize);
- return result;
- }
- CeOperand initializerValue;
-
- if (globalVar->mIsConstant)
- {
- if (globalVar->mInitializer != NULL)
- {
- auto result = GetOperand(globalVar->mInitializer, false, true);
- if (result.mKind == CeOperandKind_ConstStructTableIdx)
- {
- auto& constTableEntry = mCeFunction->mConstStructTable[result.mStructTableIdx];
- auto ptrType = mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType);
- auto dataResult = FrameAlloc(ptrType);
- Emit(CeOp_ConstDataRef);
- EmitFrameOffset(dataResult);
- Emit((int32)result.mCallTableIdx);
- return dataResult;
- }
- return result;
- }
- }
- else
- {
- initializerValue = GetOperand(globalVar->mInitializer);
- BfFieldInstance** fieldInstancePtr = NULL;
- mStaticFieldInstanceMap.TryGetValue(globalVar->mName, &fieldInstancePtr);
- int* staticFieldTableIdxPtr = NULL;
- if (mStaticFieldMap.TryAdd(globalVar, NULL, &staticFieldTableIdxPtr))
- {
- CeStaticFieldEntry staticFieldEntry;
- if (fieldInstancePtr != NULL)
- staticFieldEntry.mTypeId = (*fieldInstancePtr)->mOwner->mTypeId;
- staticFieldEntry.mName = globalVar->mName;
- if (globalVar->mLinkageType == Beefy::BfIRLinkageType_Internal)
- {
- staticFieldEntry.mName += "@";
- staticFieldEntry.mName += mCeFunction->mMethodInstance->GetOwner()->mModule->mModuleName;
- }
- staticFieldEntry.mSize = globalVar->mType->mSize;
- *staticFieldTableIdxPtr = (int)mCeFunction->mStaticFieldTable.size();
- mCeFunction->mStaticFieldTable.Add(staticFieldEntry);
- }
- auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
- if (initializerValue)
- {
- Emit(CeOp_GetStaticField_Initializer);
- EmitFrameOffset(result);
- Emit((int32)*staticFieldTableIdxPtr);
- EmitFrameOffset(initializerValue);
- }
- else
- {
- Emit(CeOp_GetStaticField);
- EmitFrameOffset(result);
- Emit((int32)*staticFieldTableIdxPtr);
- }
- return result;
- }
- errorKind = CeErrorKind_GlobalVariable;
- errorType = mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType);
- }
- break;
- case BeCastConstant::TypeId:
- {
- auto constant = (BeCastConstant*)value;
- CeOperand mcOperand;
- auto result = GetOperand(constant->mTarget);
- result.mType = constant->mType;
- return result;
- }
- break;
- case BeUndefConstant::TypeId:
- case BeConstant::TypeId:
- {
- uint64 u64Val = 0;
- float fVal = 0;
- void* dataPtr = NULL;
- int dataSize = 0;
- auto constant = (BeConstant*)value;
- CeOperand mcOperand;
- switch (constant->mType->mTypeCode)
- {
- case BeTypeCode_Int8:
- case BeTypeCode_Int16:
- case BeTypeCode_Int32:
- case BeTypeCode_Int64:
- if (allowImmediate)
- {
- CeOperand result;
- result.mKind = CeOperandKind_Immediate;
- result.mImmediate = constant->mInt32;
- result.mType = constant->mType;
- return result;
- }
- case BeTypeCode_Boolean:
- case BeTypeCode_Double:
- dataPtr = &constant->mUInt64;
- dataSize = constant->mType->mSize;
- break;
- case BeTypeCode_Float:
- fVal = (float)constant->mDouble;
- dataPtr = &fVal;
- dataSize = 4;
- break;
- case BeTypeCode_Pointer:
- {
- if (constant->mTarget == NULL)
- {
- dataPtr = &u64Val;
- dataSize = mPtrSize;
- }
- else
- {
- auto relTo = GetOperand(constant->mTarget);
- if (relTo)
- {
- auto result = relTo;
- result.mType = constant->mType;
- return result;
- }
- // if (relTo.mKind == CeOperandKind_Immediate_Null)
- // {
- // mcOperand.mKind = CeOperandKind_Immediate_Null;
- // mcOperand.mType = constant->mType;
- // return mcOperand;
- // }
- //
- // mcOperand = AllocVirtualReg(constant->mType);
- // auto vregInfo = GetVRegInfo(mcOperand);
- // vregInfo->mDefOnFirstUse = true;
- // vregInfo->mRelTo = relTo;
- // vregInfo->mIsExpr = true;
- //
- //return mcOperand;
- }
- }
- break;
- case BeTypeCode_Struct:
- case BeTypeCode_SizedArray:
- case BeTypeCode_Vector:
- {
- auto beType = constant->mType;
- auto result = FrameAlloc(beType);
- Emit(CeOp_Zero);
- EmitFrameOffset(result);
- Emit((int32)beType->mSize);
- return result;
- }
- //mcOperand.mImmediate = constant->mInt64;
- //mcOperand.mKind = CeOperandKind_Immediate_i64;
- break;
- // default:
- // Fail("Unhandled constant type");
- }
- if (dataSize != 0)
- {
- auto beType = constant->mType;
- auto result = FrameAlloc(beType);
- CeSizeClass sizeClass = GetSizeClass(dataSize);
- Emit((CeOp)(CeOp_Const_8 + sizeClass));
- EmitFrameOffset(result);
- if (sizeClass == CeSizeClass_X)
- Emit((int32)dataSize);
- if (dataPtr != 0)
- Emit(dataPtr, dataSize);
- else
- {
- for (int i = 0; i < dataSize; i++)
- Emit((uint8)0);
- }
- return result;
- }
- }
- break;
- case BeStructConstant::TypeId:
- {
- int* constDataPtr = NULL;
- auto structConstant = (BeStructConstant*)value;
- if (mConstDataMap.TryAdd(structConstant, NULL, &constDataPtr))
- {
- CeConstStructData constStructData;
- constStructData.mQueueFixups = true;
- errorKind = mCeMachine->WriteConstant(constStructData, structConstant, NULL, this);
- if (errorKind == CeErrorKind_None)
- {
- *constDataPtr = (int)mCeFunction->mConstStructTable.size();
- mCeFunction->mConstStructTable.Add(constStructData);
- }
- else
- {
- *constDataPtr = -1;
- }
- }
- if (*constDataPtr != -1)
- {
- if (!allowImmediate)
- {
- auto result = FrameAlloc(structConstant->mType);
- Emit(CeOp_ConstData);
- EmitFrameOffset(result);
- Emit((int32)*constDataPtr);
- return result;
- }
- else
- {
- CeOperand result;
- result.mKind = CeOperandKind_ConstStructTableIdx;
- result.mCallTableIdx = *constDataPtr;
- result.mType = structConstant->mType;
- return result;
- }
- }
- else
- {
- errorKind = CeErrorKind_GlobalVariable;
- }
- }
- break;
- case BeGEP1Constant::TypeId:
- {
- auto gepConstant = (BeGEP1Constant*)value;
- auto mcVal = GetOperand(gepConstant->mTarget);
- BePointerType* ptrType = (BePointerType*)mcVal.mType;
- BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
- auto result = mcVal;
- // We assume we never do both an idx0 and idx1 at once. Fix if we change that.
- int64 byteOffset = 0;
- BeType* elementType = NULL;
- byteOffset += gepConstant->mIdx0 * ptrType->mElementType->mSize;
- result = FrameAlloc(ptrType);
- EmitSizedOp(CeOp_AddConst_I8, mPtrSize);
- EmitFrameOffset(result);
- EmitFrameOffset(mcVal);
- Emit(&byteOffset, mPtrSize);
- return result;
- }
- break;
- case BeGEP2Constant::TypeId:
- {
- auto gepConstant = (BeGEP2Constant*)value;
- auto mcVal = GetOperand(gepConstant->mTarget);
- BePointerType* ptrType = (BePointerType*)mcVal.mType;
- BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
- auto result = mcVal;
- // We assume we never do both an idx0 and idx1 at once. Fix if we change that.
- int64 byteOffset = 0;
- BeType* elementType = NULL;
- byteOffset += gepConstant->mIdx0 * ptrType->mElementType->mSize;
- if (ptrType->mElementType->mTypeCode == BeTypeCode_Struct)
- {
- BeStructType* structType = (BeStructType*)ptrType->mElementType;
- auto& structMember = structType->mMembers[gepConstant->mIdx1];
- elementType = structMember.mType;
- byteOffset = structMember.mByteOffset;
- }
- else
- {
- BF_ASSERT(ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray);
- auto arrayType = (BeSizedArrayType*)ptrType->mElementType;
- elementType = arrayType->mElementType;
- byteOffset = gepConstant->mIdx1 * elementType->GetStride();
- }
- auto elementPtrType = mCeMachine->GetBeContext()->GetPointerTo(elementType);
- result = FrameAlloc(elementPtrType);
- EmitSizedOp(CeOp_AddConst_I8, mPtrSize);
- EmitFrameOffset(result);
- EmitFrameOffset(mcVal);
- Emit(&byteOffset, mPtrSize);
- return result;
- }
- break;
- case BeExtractValueConstant::TypeId:
- {
- // Note: this only handles zero-aggregates
- auto extractConstant = (BeExtractValueConstant*)value;
- auto elementType = extractConstant->GetType();
- auto mcVal = GetOperand(extractConstant->mTarget);
- BeConstant beConstant;
- beConstant.mType = elementType;
- beConstant.mUInt64 = 0;
- return GetOperand(&beConstant);
- }
- break;
- case BeFunction::TypeId:
- {
- auto beFunction = (BeFunction*)value;
- CeOperand operand;
- int callIdx = GetCallTableIdx(beFunction, &operand);
- if (operand)
- return operand;
- if (allowImmediate)
- {
- CeOperand result;
- result.mKind = CeOperandKind_CallTableIdx;
- result.mCallTableIdx = callIdx;
- return result;
- }
- BF_ASSERT(callIdx <= mCeFunction->mCallTable.mSize);
- CeOperand result = FrameAlloc(mCeMachine->GetBeContext()->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
- Emit(CeOp_GetMethod);
- EmitFrameOffset(result);
- Emit((int32)callIdx);
- return result;
- }
- break;
- case BeCallInst::TypeId:
- {
- // auto callInst = (BeCallInst*)value;
- // if (callInst->mInlineResult != NULL)
- // return GetOperand(callInst->mInlineResult);
- }
- break;
- case BeTypeOfConstant::TypeId:
- {
- auto beTypeOf = (BeTypeOfConstant*)value;
- auto ptrType = mCeMachine->GetBeContext()->GetVoidPtrType();
- CeOperand result = FrameAlloc(ptrType);
- Emit(CeOp_GetReflectType);
- EmitFrameOffset(result);
- Emit((int32)beTypeOf->mBfTypeId);
- return result;
- }
- break;
- }
- CeOperand* operandPtr = NULL;
- mValueToOperand.TryGetValue(value, &operandPtr);
- if (errorKind != CeErrorKind_None)
- {
- Emit(CeOp_Error);
- Emit((int32)errorKind);
- }
- else
- {
- if (operandPtr == NULL)
- {
- BeDumpContext dumpCtx;
- String str;
- dumpCtx.ToString(str, value);
- Fail(StrFormat("Unable to find bevalue for operand: %s", str.c_str()));
- }
- }
- if (operandPtr == NULL)
- {
- return FrameAlloc(errorType);
- }
- auto operand = *operandPtr;
- if ((operand.mKind == CeOperandKind_AllocaAddr) && (!allowAlloca))
- {
- auto irCodeGen = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen;
- auto ptrType = mCeMachine->GetBeContext()->GetPointerTo(operand.mType);
- auto result = FrameAlloc(ptrType);
- Emit((mPtrSize == 4) ? CeOp_FrameAddr_32 : CeOp_FrameAddr_64);
- EmitFrameOffset(result);
- Emit((int32)operand.mFrameOfs);
- return result;
- }
- return operand;
- }
- CeSizeClass CeBuilder::GetSizeClass(int size)
- {
- switch (size)
- {
- case 1:
- return CeSizeClass_8;
- case 2:
- return CeSizeClass_16;
- case 4:
- return CeSizeClass_32;
- case 8:
- return CeSizeClass_64;
- default:
- return CeSizeClass_X;
- }
- }
- int CeBuilder::DbgCreateMethodRef(BfMethodInstance* methodInstance, const StringImpl& nameMod)
- {
- CeDbgMethodRef dbgMethodRef;
- dbgMethodRef.mNameMod = nameMod;
- dbgMethodRef.mMethodRef = methodInstance;
- int* valuePtr = NULL;
- if (mDbgMethodRefMap.TryAdd(dbgMethodRef, NULL, &valuePtr))
- {
- *valuePtr = mCeFunction->mDbgMethodRefTable.mSize;
- mCeFunction->mDbgMethodRefTable.Add(dbgMethodRef);
- }
- return *valuePtr;
- }
- void CeBuilder::HandleParams()
- {
- auto beModule = mBeFunction->mModule;
- // int regIdxOfs = 0;
- // int paramOfs = 0;
- auto retType = mBeFunction->GetFuncType()->mReturnType;
- int frameOffset = 0;
- if (mCeFunction->mMaxReturnSize > 0)
- {
- mReturnVal.mKind = CeOperandKind_AllocaAddr;
- mReturnVal.mFrameOfs = frameOffset;
- frameOffset += mCeFunction->mMaxReturnSize;
- }
- int paramOfs = 0;
- //for (int paramIdx = (int)mBeFunction->mParams.size() - 1; paramIdx >= 0; paramIdx--)
- for (int paramIdx = 0; paramIdx < mBeFunction->mParams.size(); paramIdx++)
- {
- auto funcType = mBeFunction->GetFuncType();
- auto& typeParam = funcType->mParams[paramIdx + paramOfs];
- auto& param = mBeFunction->mParams[paramIdx + paramOfs];
- auto beArg = beModule->GetArgument(paramIdx + paramOfs);
- auto paramType = typeParam.mType;
- CeOperand ceOperand;
- ceOperand.mKind = CeOperandKind_FrameOfs;
- ceOperand.mFrameOfs = frameOffset;
- ceOperand.mType = paramType;
- frameOffset += paramType->mSize;
- mValueToOperand[beArg] = ceOperand;
- }
- }
- void CeBuilder::ProcessMethod(BfMethodInstance* methodInstance, BfMethodInstance* dupMethodInstance, bool forceIRWrites)
- {
- SetAndRestoreValue<BfMethodState*> prevMethodStateInConstEval(mCeMachine->mCeModule->mCurMethodState, NULL);
- BfAutoComplete* prevAutoComplete = NULL;
- if (mCeMachine->mCeModule->mCompiler->mResolvePassData != NULL)
- {
- prevAutoComplete = mCeMachine->mCeModule->mCompiler->mResolvePassData->mAutoComplete;
- mCeMachine->mCeModule->mCompiler->mResolvePassData->mAutoComplete = NULL;
- }
- auto irCodeGen = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen;
- auto irBuilder = mCeMachine->mCeModule->mBfIRBuilder;
- auto beModule = irCodeGen->mBeModule;
- beModule->mCeMachine = mCeMachine;
- dupMethodInstance->mIsReified = true;
- dupMethodInstance->mInCEMachine = false; // Only have the original one
- // We can't use methodInstance->mMethodInstanceGroup because we may add foreign method instances which
- // would reallocate the methodInstanceGroup
- BfMethodInstanceGroup methodInstanceGroup;
- methodInstanceGroup.mOwner = methodInstance->mMethodInstanceGroup->mOwner;
- methodInstanceGroup.mDefault = methodInstance->mMethodInstanceGroup->mDefault;
- methodInstanceGroup.mMethodIdx = methodInstance->mMethodInstanceGroup->mMethodIdx;
- methodInstanceGroup.mOnDemandKind = BfMethodOnDemandKind_AlwaysInclude;
- dupMethodInstance->mMethodInstanceGroup = &methodInstanceGroup;
- mCeMachine->mCeModule->mHadBuildError = false;
- auto irState = irBuilder->GetState();
- auto beState = irCodeGen->GetState();
- mCeMachine->mCeModule->ProcessMethod(dupMethodInstance, true, forceIRWrites);
- irCodeGen->SetState(beState);
- irBuilder->SetState(irState);
- if (mCeMachine->mCeModule->mCompiler->mResolvePassData != NULL)
- mCeMachine->mCeModule->mCompiler->mResolvePassData->mAutoComplete = prevAutoComplete;
- methodInstanceGroup.mDefault = NULL;
- dupMethodInstance->mMethodInstanceGroup = methodInstance->mMethodInstanceGroup;
- }
- void CeBuilder::Build()
- {
- SetAndRestoreValue<CeDbgState*> prevDbgState;
- if (mCeMachine->mDebugger != NULL)
- prevDbgState.Init(mCeMachine->mDebugger->mCurDbgState, NULL);
-
- auto irCodeGen = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen;
- auto irBuilder = mCeMachine->mCeModule->mBfIRBuilder;
- auto beModule = irCodeGen->mBeModule;
- mCeFunction->mFailed = true;
- auto methodInstance = mCeFunction->mMethodInstance;
- if ((methodInstance != NULL) && (!mCeMachine->HasFailed()))
- {
- BfMethodInstance dupMethodInstance;
- dupMethodInstance.CopyFrom(methodInstance);
- auto methodDef = methodInstance->mMethodDef;
- bool isGenericVariation = (methodInstance->mIsUnspecializedVariation) || (methodInstance->GetOwner()->IsUnspecializedTypeVariation());
- int dependentGenericStartIdx = 0;
- if ((((methodInstance->mMethodInfoEx != NULL) && ((int)methodInstance->mMethodInfoEx->mMethodGenericArguments.size() > dependentGenericStartIdx)) ||
- ((methodInstance->GetOwner()->IsGenericTypeInstance()) && (!isGenericVariation) && (!methodInstance->mMethodDef->mIsLocalMethod) && (!methodInstance->mIsUnspecialized))))
- {
- auto unspecializedMethodInstance = mCeMachine->mCeModule->GetUnspecializedMethodInstance(methodInstance, !methodInstance->mMethodDef->mIsLocalMethod);
- if (!unspecializedMethodInstance->mHasBeenProcessed)
- {
- BfMethodInstance dupUnspecMethodInstance;
- dupUnspecMethodInstance.CopyFrom(unspecializedMethodInstance);
- ProcessMethod(unspecializedMethodInstance, &dupUnspecMethodInstance, false);
- if (dupUnspecMethodInstance.mMethodInfoEx != NULL)
- dupMethodInstance.GetMethodInfoEx()->mGenericTypeBindings = dupUnspecMethodInstance.mMethodInfoEx->mGenericTypeBindings;
- }
- }
- // Clear this so we can properly get QueueStaticField calls
- mCeMachine->mCeModule->mStaticFieldRefs.Clear();
- int startFunctionCount = (int)beModule->mFunctions.size();
- ProcessMethod(methodInstance, &dupMethodInstance, true);
- if (mCeFunction->mInitializeState == CeFunction::InitializeState_Initialized)
- return;
- if (!dupMethodInstance.mIRFunction)
- {
- mCeFunction->mFailed = true;
- return;
- }
- mBeFunction = (BeFunction*)irCodeGen->TryGetBeValue(dupMethodInstance.mIRFunction.mId);
- if (mBeFunction == NULL)
- {
- mCeFunction->mFailed = true;
- return;
- }
- mIntPtrType = irCodeGen->mBeContext->GetPrimitiveType((mPtrSize == 4) ? BeTypeCode_Int32 : BeTypeCode_Int64);
- for (int funcIdx = startFunctionCount; funcIdx < (int)beModule->mFunctions.size(); funcIdx++)
- {
- auto beFunction = beModule->mFunctions[funcIdx];
- if (beFunction == mBeFunction)
- continue;
- if (beFunction->mBlocks.IsEmpty())
- continue;
- CeFunction* innerFunction = new CeFunction();
- innerFunction->mCeMachine = mCeMachine;
- innerFunction->mIsVarReturn = beFunction->mIsVarReturn;
- innerFunction->mCeInnerFunctionInfo = new CeInnerFunctionInfo();
- innerFunction->mCeInnerFunctionInfo->mName = beFunction->mName;
- innerFunction->mCeInnerFunctionInfo->mBeFunction = beFunction;
- innerFunction->mCeInnerFunctionInfo->mOwner = mCeFunction;
- if (mCeMachine->mDebugger != NULL)
- innerFunction->mDbgInfo = new CeDbgFunctionInfo();
- mInnerFunctionMap[beFunction] = (int)mCeFunction->mInnerFunctions.size();
- mCeFunction->mInnerFunctions.Add(innerFunction);
- mCeMachine->MapFunctionId(innerFunction);
- }
- if (!mCeFunction->mCeFunctionInfo->mName.IsEmpty())
- {
- BF_ASSERT(mCeFunction->mCeFunctionInfo->mName == mBeFunction->mName);
- }
- else
- {
- mCeFunction->mCeFunctionInfo->mName = mBeFunction->mName;
- mCeMachine->mNamedFunctionMap[mCeFunction->mCeFunctionInfo->mName] = mCeFunction->mCeFunctionInfo;
- }
- if (mCeMachine->mCeModule->mHadBuildError)
- {
- mCeFunction->mGenError = "Method had errors";
- mCeMachine->mCeModule->mHadBuildError = false;
- return;
- }
- }
- else
- {
- BF_ASSERT(mCeFunction->mCeInnerFunctionInfo != NULL);
- mBeFunction = mCeFunction->mCeInnerFunctionInfo->mBeFunction;
- BF_ASSERT(mBeFunction != NULL);
- mCeFunction->mCeInnerFunctionInfo->mBeFunction = NULL;
- }
- SetAndRestoreValue<BeFunction*> prevBeFunction(beModule->mActiveFunction, mBeFunction);
- // Create blocks
- for (int blockIdx = 0; blockIdx < (int)mBeFunction->mBlocks.size(); blockIdx++)
- {
- auto beBlock = mBeFunction->mBlocks[blockIdx];
- CeBlock ceBlock;
- mBlocks.Add(ceBlock);
- CeOperand ceOperand;
- ceOperand.mKind = CeOperandKind_Block;
- ceOperand.mBlockIdx = blockIdx;
- mValueToOperand[beBlock] = ceOperand;
- }
- // Instruction pre-pass
- for (int blockIdx = 0; blockIdx < (int)mBeFunction->mBlocks.size(); blockIdx++)
- {
- auto beBlock = mBeFunction->mBlocks[blockIdx];
- auto& ceBlock = mBlocks[blockIdx];
- for (int instIdx = 0; instIdx < (int)beBlock->mInstructions.size(); instIdx++)
- {
- auto inst = beBlock->mInstructions[instIdx];
- int instType = inst->GetTypeId();
- switch (instType)
- {
- case BePhiInst::TypeId:
- {
- auto castedInst = (BePhiInst*)inst;
- auto resultType = castedInst->GetType();
- auto phiResult = FrameAlloc(resultType);
- mValueToOperand[castedInst] = phiResult;
- for (auto& phiIncoming : castedInst->mIncoming)
- {
- auto incomingBlockOpr = GetOperand(phiIncoming->mBlock);
- auto& incomingBlock = mBlocks[incomingBlockOpr.mBlockIdx];
- CePhiOutgoing phiOutgoing;
- phiOutgoing.mPhiValue = phiIncoming->mValue;
- phiOutgoing.mPhiInst = castedInst;
- phiOutgoing.mPhiBlockIdx = blockIdx;
- incomingBlock.mPhiOutgoing.Add(phiOutgoing);
- }
- }
- break;
- case BeRetInst::TypeId:
- case BeSetRetInst::TypeId:
- {
- auto castedInst = (BeRetInst*)inst;
- if (castedInst->mRetValue != NULL)
- {
- auto retType = castedInst->mRetValue->GetType();
- mCeFunction->mMaxReturnSize = BF_MAX(retType->mSize, mCeFunction->mMaxReturnSize);
- }
- }
- break;
- }
- }
- }
- int scopeIdx = -1;
- // Primary instruction pass
- BeDbgLoc* prevEmitDbgPos = NULL;
- bool inHeadAlloca = true;
- for (int blockIdx = 0; blockIdx < (int)mBeFunction->mBlocks.size(); blockIdx++)
- {
- auto beBlock = mBeFunction->mBlocks[blockIdx];
- auto ceBlock = &mBlocks[blockIdx];
- ceBlock->mEmitOfs = GetCodePos();
- if (blockIdx == 0)
- HandleParams();
- for (int instIdx = 0; instIdx < (int)beBlock->mInstructions.size(); instIdx++)
- {
- auto inst = beBlock->mInstructions[instIdx];
- CeOperand result;
- int startCodePos = GetCodePos();
- mCurDbgLoc = inst->mDbgLoc;
- if ((prevEmitDbgPos != mCurDbgLoc) && (mCurDbgLoc != NULL))
- {
- auto _GetScope = [&](BeMDNode* mdNode, int inlinedAt)
- {
- BeDbgFile* dbgFile;
- BeDbgFunction* dbgFunc = NULL;
- String nameAdd;
- while (auto dbgLexicalBlock = BeValueDynCast<BeDbgLexicalBlock>(mdNode))
- mdNode = dbgLexicalBlock->mScope;
- if (dbgFunc = BeValueDynCast<BeDbgFunction>(mdNode))
- {
- dbgFile = dbgFunc->mFile;
- }
- else if (auto dbgLoc = BeValueDynCast<BeDbgLoc>(mdNode))
- dbgFile = dbgLoc->GetDbgFile();
- else
- dbgFile = BeValueDynCast<BeDbgFile>(mdNode);
- int* valuePtr = NULL;
- CeDbgInlineLookup lookupPair(dbgFile, inlinedAt);
- if (mDbgScopeMap.TryAdd(lookupPair, NULL, &valuePtr))
- {
- int scopeIdx = (int)mCeFunction->mDbgScopes.size();
- String filePath = dbgFile->mDirectory;
- filePath.Append(DIR_SEP_CHAR);
- filePath += dbgFile->mFileName;
- CeDbgScope dbgScope;
- dbgScope.mFilePath = filePath;
- dbgScope.mInlinedAt = inlinedAt;
- dbgScope.mMethodVal = -1;
- if (dbgFunc != NULL)
- {
- if (dbgFunc->mValue == NULL)
- {
- if (!dbgFunc->mLinkageName.IsEmpty())
- {
- int methodRefIdx = atoi(dbgFunc->mLinkageName.c_str());
- dbgScope.mMethodVal = methodRefIdx | CeDbgScope::MethodValFlag_MethodRef;
- }
- else
- {
- CeDbgMethodRef dbgMethodRef;
- dbgMethodRef.mNameMod = dbgFunc->mName;
- int* valuePtr = NULL;
- if (mDbgMethodRefMap.TryAdd(dbgMethodRef, NULL, &valuePtr))
- {
- *valuePtr = mCeFunction->mDbgMethodRefTable.mSize;
- mCeFunction->mDbgMethodRefTable.Add(dbgMethodRef);
- }
- dbgScope.mMethodVal = *valuePtr | CeDbgScope::MethodValFlag_MethodRef;
- }
- }
- else if (dbgFunc->mValue != mBeFunction)
- dbgScope.mMethodVal = GetCallTableIdx(dbgFunc->mValue, NULL);
- }
- mCeFunction->mDbgScopes.Add(dbgScope);
- *valuePtr = scopeIdx;
- return scopeIdx;
- }
- else
- return *valuePtr;
- };
- std::function<int(BeDbgLoc*)> _GetInlinedScope = [&](BeDbgLoc* dbgLoc)
- {
- if (dbgLoc == NULL)
- return -1;
- int* valuePtr = NULL;
- if (mDbgInlineMap.TryAdd(dbgLoc, NULL, &valuePtr))
- {
- CeDbgInlineEntry inlineEntry;
- inlineEntry.mLine = dbgLoc->mLine;
- inlineEntry.mColumn = dbgLoc->mColumn;
- auto inlinedAt = _GetInlinedScope(dbgLoc->mDbgInlinedAt);
- inlineEntry.mScope = _GetScope(dbgLoc->mDbgScope, inlinedAt);
- *valuePtr = mCeFunction->mDbgInlineTable.mSize;
- mCeFunction->mDbgInlineTable.Add(inlineEntry);
- }
- return *valuePtr;
- };
- int inlinedAt = _GetInlinedScope(mCurDbgLoc->mDbgInlinedAt);
- scopeIdx = _GetScope(mCurDbgLoc->mDbgScope, inlinedAt);
- }
- int instType = inst->GetTypeId();
- switch (instType)
- {
- case BeAllocaInst::TypeId:
- case BeNumericCastInst::TypeId:
- case BeBitCastInst::TypeId:
- break;
- default:
- inHeadAlloca = false;
- break;
- }
- switch (instType)
- {
- case BeNopInst::TypeId:
- case BeLifetimeSoftEndInst::TypeId:
- case BeLifetimeStartInst::TypeId:
- case BeLifetimeExtendInst::TypeId:
- case BeValueScopeStartInst::TypeId:
- case BeValueScopeEndInst::TypeId:
- case BeValueScopeRetainInst::TypeId:
- case BeComptimeGetVirtualFunc::TypeId:
- case BeComptimeGetInterfaceFunc::TypeId:
- break;
- case BeUnreachableInst::TypeId:
- Emit(CeOp_InvalidOp);
- break;
- case BeUndefValueInst::TypeId:
- {
- auto castedInst = (BeUndefValueInst*)inst;
- result = FrameAlloc(castedInst->mType);
- }
- break;
- case BeAllocaInst::TypeId:
- {
- auto castedInst = (BeAllocaInst*)inst;
- CeOperand ceSize;
- ceSize.mKind = CeOperandKind_Immediate;
- ceSize.mImmediate = castedInst->mType->mSize;
- ceSize.mType = mIntPtrType;
- bool isAligned16 = false;
- int align = castedInst->mAlign;
- BeType* allocType = castedInst->mType;
- bool preservedVolatiles = false;
- if (castedInst->mArraySize != NULL)
- {
- auto mcArraySize = GetOperand(castedInst->mArraySize, false, true);
- if (mcArraySize.IsImmediate())
- {
- ceSize.mImmediate = ceSize.mImmediate * mcArraySize.mImmediate;
- }
- else
- {
- inHeadAlloca = false;
- if (ceSize.mImmediate == 1)
- {
- ceSize = mcArraySize;
- }
- else
- {
- ceSize = EmitConst(ceSize.mImmediate, mcArraySize.mType->mSize);
- EmitSizedOp(CeOp_Mul_I8, ceSize.mType->mSize);
- EmitFrameOffset(ceSize);
- EmitFrameOffset(ceSize);
- EmitFrameOffset(mcArraySize);
- }
- }
- }
- if (inHeadAlloca)
- {
- BF_ASSERT(ceSize.mKind == CeOperandKind_Immediate);
- mFrameSize += ceSize.mImmediate;
- result.mKind = CeOperandKind_AllocaAddr;
- result.mFrameOfs = -mFrameSize;
- result.mType = castedInst->mType;
- }
- else
- {
- if (ceSize.mKind == CeOperandKind_Immediate)
- {
- Emit(CeOp_AdjustSPConst);
- Emit((int32)-ceSize.mImmediate);
- }
- else
- {
- Emit(CeOp_AdjustSPNeg);
- EmitFrameOffset(ceSize);
- }
- auto ptrType = beModule->mContext->GetPointerTo(allocType);
- result = FrameAlloc(ptrType);
- Emit(CeOp_GetSP);
- EmitFrameOffset(result);
- }
- }
- break;
- case BeLoadInst::TypeId:
- {
- auto castedInst = (BeLoadInst*)inst;
- auto ceTarget = GetOperand(castedInst->mTarget, true);
- result = EmitLoad(ceTarget, inst->mRefCount);
- }
- break;
- case BeBinaryOpInst::TypeId:
- {
- auto castedInst = (BeBinaryOpInst*)inst;
- auto ceLHS = GetOperand(castedInst->mLHS);
- auto ceRHS = GetOperand(castedInst->mRHS);
- switch (castedInst->mOpKind)
- {
- case BeBinaryOpKind_Add:
- EmitBinaryOp(CeOp_Add_I8, CeOp_Add_F32, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_Subtract:
- EmitBinaryOp(CeOp_Sub_I8, CeOp_Sub_F32, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_Multiply:
- EmitBinaryOp(CeOp_Mul_I8, CeOp_Mul_F32, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_SDivide:
- EmitBinaryOp(CeOp_Div_I8, CeOp_Div_F32, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_UDivide:
- EmitBinaryOp(CeOp_Div_U8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_SModulus:
- EmitBinaryOp(CeOp_Mod_I8, CeOp_Mod_F32, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_UModulus:
- EmitBinaryOp(CeOp_Mod_U8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_BitwiseAnd:
- EmitBinaryOp(CeOp_And_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_BitwiseOr:
- EmitBinaryOp(CeOp_Or_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_ExclusiveOr:
- EmitBinaryOp(CeOp_Xor_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_LeftShift:
- EmitBinaryOp(CeOp_Shl_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_RightShift:
- EmitBinaryOp(CeOp_Shr_U8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- case BeBinaryOpKind_ARightShift:
- EmitBinaryOp(CeOp_Shr_I8, CeOp_InvalidOp, ceLHS, ceRHS, result);
- break;
- default:
- Fail("Invalid binary op");
- }
- }
- break;
- case BeBitCastInst::TypeId:
- {
- auto castedInst = (BeBitCastInst*)inst;
- auto mcValue = GetOperand(castedInst->mValue, false, true);
- if (castedInst->mToType->IsInt())
- {
- BF_ASSERT(castedInst->mToType->mSize == 8);
- }
- else
- BF_ASSERT(castedInst->mToType->IsPointer());
- auto toType = castedInst->mToType;
- if (mcValue.IsImmediate())
- {
- if (mcValue.mImmediate == 0)
- {
- CeOperand newImmediate;
- newImmediate.mKind = CeOperandKind_Immediate;
- newImmediate.mType = toType;
- result = newImmediate;
- }
- else
- {
- // Non-zero constant. Weird case, just do an actual MOV
- result = FrameAlloc(toType);
- EmitSizedOp(CeOp_Const_8, result, NULL, true);
- int64 val = mcValue.mImmediate;
- Emit(&val, toType->mSize);
- }
- }
- else
- {
- if (toType->mSize != mcValue.mType->mSize)
- Fail("Invalid bitcast");
- result = mcValue;
- result.mType = toType;
- }
- }
- break;
- case BeNumericCastInst::TypeId:
- {
- auto castedInst = (BeNumericCastInst*)inst;
- auto ceValue = GetOperand(castedInst->mValue);
- result = EmitNumericCast(ceValue, castedInst->mToType, castedInst->mValSigned, castedInst->mToSigned);
- }
- break;
- case BeStoreInst::TypeId:
- {
- auto castedInst = (BeStoreInst*)inst;
- auto mcVal = GetOperand(castedInst->mVal);
- auto mcPtr = GetOperand(castedInst->mPtr, true);
- if (mcPtr.mKind == CeOperandKind_AllocaAddr)
- {
- EmitSizedOp(CeOp_Move_8, mcVal, NULL, true);
- Emit((int32)mcPtr.mFrameOfs);
- }
- else
- {
- EmitSizedOp(CeOp_Store_8, mcVal, NULL, true);
- EmitFrameOffset(mcPtr);
- }
- }
- break;
- case BeRetInst::TypeId:
- case BeSetRetInst::TypeId:
- {
- auto castedInst = (BeRetInst*)inst;
- if (castedInst->mRetValue != NULL)
- {
- auto mcVal = GetOperand(castedInst->mRetValue);
- if (mcVal.mType->mSize > 0)
- {
- BF_ASSERT(mReturnVal.mKind == CeOperandKind_AllocaAddr);
- EmitSizedOp(CeOp_Move_8, mcVal, NULL, true);
- Emit((int32)mReturnVal.mFrameOfs);
- }
- }
- if (instType == BeRetInst::TypeId)
- Emit(CeOp_Ret);
- else
- {
- auto setRetInst = (BeSetRetInst*)inst;
- Emit(CeOp_SetRetType);
- Emit((int32)setRetInst->mReturnTypeId);
- }
- }
- break;
- case BeCmpInst::TypeId:
- {
- auto castedInst = (BeCmpInst*)inst;
- auto ceLHS = GetOperand(castedInst->mLHS);
- auto ceRHS = GetOperand(castedInst->mRHS);
- CeOp iOp = CeOp_InvalidOp;
- CeOp fOp = CeOp_InvalidOp;
- switch (castedInst->mCmpKind)
- {
- case BeCmpKind_EQ:
- iOp = CeOp_Cmp_EQ_I8;
- fOp = CeOp_Cmp_EQ_F32;
- break;
- case BeCmpKind_NE:
- iOp = CeOp_Cmp_NE_I8;
- fOp = CeOp_Cmp_NE_F32;
- break;
- case BeCmpKind_SLT:
- iOp = CeOp_Cmp_SLT_I8;
- fOp = CeOp_Cmp_SLT_F32;
- break;
- case BeCmpKind_ULT:
- iOp = CeOp_Cmp_ULT_I8;
- break;
- case BeCmpKind_SLE:
- iOp = CeOp_Cmp_SLE_I8;
- fOp = CeOp_Cmp_SLE_F32;
- break;
- case BeCmpKind_ULE:
- iOp = CeOp_Cmp_ULE_I8;
- break;
- case BeCmpKind_SGT:
- iOp = CeOp_Cmp_SGT_I8;
- fOp = CeOp_Cmp_SGT_F32;
- break;
- case BeCmpKind_UGT:
- iOp = CeOp_Cmp_UGT_I8;
- break;
- case BeCmpKind_SGE:
- iOp = CeOp_Cmp_SGE_I8;
- fOp = CeOp_Cmp_SGE_F32;
- break;
- case BeCmpKind_UGE:
- iOp = CeOp_Cmp_UGE_I8;
- break;
- }
- if (iOp == CeOp_InvalidOp)
- {
- Fail("Invalid cmp");
- break;
- }
- auto boolType = inst->GetType();
- result = FrameAlloc(boolType);
- EmitBinaryOp(iOp, fOp, ceLHS, ceRHS, result);
- // auto mcInst = AllocInst(BeMCInstKind_Cmp, mcLHS, mcRHS);
- //
- // auto cmpResultIdx = (int)mCmpResults.size();
- // BeCmpResult cmpResult;
- // cmpResult.mCmpKind = castedInst->mCmpKind;
- // mCmpResults.push_back(cmpResult);
- //
- // result.mKind = BeMCOperandKind_CmpResult;
- // result.mCmpResultIdx = cmpResultIdx;
- //
- // mcInst->mResult = result;
- }
- break;
- case BeGEPInst::TypeId:
- {
- auto castedInst = (BeGEPInst*)inst;
- auto ceVal = GetOperand(castedInst->mPtr);
- auto ceIdx0 = GetOperand(castedInst->mIdx0, false, true);
- if ((ceIdx0.mType != NULL) && (ceIdx0.mType->mSize < mPtrSize))
- ceIdx0 = EmitNumericCast(ceIdx0, mIntPtrType, true, true);
- BePointerType* ptrType = (BePointerType*)ceVal.mType;
- BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
- result = ceVal;
- if (castedInst->mIdx1 != NULL)
- {
- // We assume we never do both an idx0 and idx1 at once. Fix if we change that.
- BF_ASSERT(castedInst->mIdx0);
- auto ceIdx1 = GetOperand(castedInst->mIdx1, false, true);
- if ((ceIdx1.mType != NULL) && (ceIdx1.mType->mSize < mPtrSize))
- ceIdx1 = EmitNumericCast(ceIdx1, mIntPtrType, true, true);
- if (!ceIdx1.IsImmediate())
- {
- // This path is used when we have a const array that gets indexed by a non-const index value
- if (ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray)
- {
- auto arrayType = (BeSizedArrayType*)ptrType->mElementType;
- auto elementPtrType = beModule->mContext->GetPointerTo(arrayType->mElementType);
- if (ceIdx1.IsImmediate())
- {
- if (ceIdx1.mImmediate == 0)
- {
- result = ceVal;
- result.mType = elementPtrType;
- }
- else
- {
- auto ptrValue = FrameAlloc(elementPtrType);
- result = ptrValue;
- result = FrameAlloc(elementPtrType);
- EmitSizedOp(CeOp_AddConst_I8, mPtrSize);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- int32 byteOffset = (int32)(ceIdx1.mImmediate * arrayType->mElementType->GetStride());
- if (mPtrSize == 8)
- Emit((int64)byteOffset);
- else
- Emit((int32)byteOffset);
- }
- }
- else
- {
- auto ptrValue = FrameAlloc(elementPtrType);
- result = ptrValue;
- if (ceIdx1.mType->mSize < 4)
- {
- auto ceNewIdx = FrameAlloc(mIntPtrType);
- if (mIntPtrType->mSize == 8)
- {
- if (ceIdx1.mType->mSize == 1)
- Emit(CeOp_Conv_I8_I64);
- else
- Emit(CeOp_Conv_I16_I64);
- }
- else
- {
- if (ceIdx1.mType->mSize == 1)
- Emit(CeOp_Conv_I8_I32);
- else
- Emit(CeOp_Conv_I16_I32);
- }
- EmitFrameOffset(ceNewIdx);
- EmitFrameOffset(ceIdx1);
- ceIdx1 = ceNewIdx;
- }
- result = FrameAlloc(elementPtrType);
- if (mPtrSize == 4)
- {
- auto mcElementSize = FrameAlloc(mIntPtrType);
- Emit(CeOp_Const_32);
- EmitFrameOffset(mcElementSize);
- Emit((int32)arrayType->mElementType->GetStride());
- auto ofsValue = FrameAlloc(mIntPtrType);
- Emit(CeOp_Mul_I32);
- EmitFrameOffset(ofsValue);
- EmitFrameOffset(ceIdx1);
- EmitFrameOffset(mcElementSize);
- Emit(CeOp_Add_I32);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- EmitFrameOffset(ofsValue);
- }
- else
- {
- auto mcElementSize = FrameAlloc(mIntPtrType);
- Emit(CeOp_Const_64);
- EmitFrameOffset(mcElementSize);
- Emit((int64)arrayType->mElementType->GetStride());
- auto ofsValue = FrameAlloc(mIntPtrType);
- Emit(CeOp_Mul_I64);
- EmitFrameOffset(ofsValue);
- EmitFrameOffset(ceIdx1);
- EmitFrameOffset(mcElementSize);
- Emit(CeOp_Add_I64);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- EmitFrameOffset(ofsValue);
- }
- }
- }
- else
- Fail("Invalid GEP");
- }
- else
- {
- BF_ASSERT(ceIdx1.IsImmediate());
- int byteOffset = 0;
- BeType* elementType = NULL;
- if (ptrType->mElementType->mTypeCode == BeTypeCode_Struct)
- {
- BeStructType* structType = (BeStructType*)ptrType->mElementType;
- auto& structMember = structType->mMembers[ceIdx1.mImmediate];
- elementType = structMember.mType;
- byteOffset = structMember.mByteOffset;
- }
- else if (ptrType->mElementType->mTypeCode == BeTypeCode_SizedArray)
- {
- auto arrayType = (BeSizedArrayType*)ptrType->mElementType;
- elementType = arrayType->mElementType;
- byteOffset = ceIdx1.mImmediate * elementType->GetStride();
- }
- else if (ptrType->mElementType->mTypeCode == BeTypeCode_Vector)
- {
- auto arrayType = (BeVectorType*)ptrType->mElementType;
- elementType = arrayType->mElementType;
- byteOffset = ceIdx1.mImmediate * elementType->GetStride();
- }
- else
- {
- Fail("Invalid gep target");
- }
- auto elementPtrType = beModule->mContext->GetPointerTo(elementType);
- if (byteOffset != 0)
- {
- result = FrameAlloc(elementPtrType);
- EmitSizedOp(CeOp_AddConst_I8, mPtrSize);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- if (mPtrSize == 8)
- Emit((int64)byteOffset);
- else
- Emit((int32)byteOffset);
- }
- else
- {
- result.mType = elementPtrType;
- }
- }
- }
- else
- {
- CeOperand mcRelOffset;
- int relScale = 1;
- if (ceIdx0.IsImmediate())
- {
- int byteOffset = ceIdx0.mImmediate * ptrType->mElementType->GetStride();
- if (byteOffset != 0)
- {
- result = FrameAlloc(ptrType);
- EmitSizedOp(CeOp_AddConst_I8, mPtrSize);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- if (mPtrSize == 8)
- Emit((int64)byteOffset);
- else
- Emit((int32)byteOffset);
- }
- }
- else
- {
- result = FrameAlloc(ptrType);
- if (mPtrSize == 4)
- {
- auto mcElementSize = FrameAlloc(mIntPtrType);
- Emit(CeOp_Const_32);
- EmitFrameOffset(mcElementSize);
- Emit((int32)ptrType->mElementType->GetStride());
- auto ofsValue = FrameAlloc(mIntPtrType);
- Emit(CeOp_Mul_I32);
- EmitFrameOffset(ofsValue);
- EmitFrameOffset(ceIdx0);
- EmitFrameOffset(mcElementSize);
- Emit(CeOp_Add_I32);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- EmitFrameOffset(ofsValue);
- }
- else
- {
- auto mcElementSize = FrameAlloc(mIntPtrType);
- Emit(CeOp_Const_64);
- EmitFrameOffset(mcElementSize);
- Emit((int64)ptrType->mElementType->GetStride());
- auto ofsValue = FrameAlloc(mIntPtrType);
- Emit(CeOp_Mul_I64);
- EmitFrameOffset(ofsValue);
- EmitFrameOffset(ceIdx0);
- EmitFrameOffset(mcElementSize);
- Emit(CeOp_Add_I64);
- EmitFrameOffset(result);
- EmitFrameOffset(ceVal);
- EmitFrameOffset(ofsValue);
- }
- }
- }
- }
- break;
- case BeExtractValueInst::TypeId:
- {
- auto castedInst = (BeExtractValueInst*)inst;
- BeConstant* constant = BeValueDynCast<BeConstant>(castedInst->mAggVal);
- CeOperand mcAgg;
- if (constant != NULL)
- {
- result.mImmediate = 0;
- BeType* wantDefaultType = NULL;
- if (constant->mType->IsStruct())
- {
- BeStructType* structType = (BeStructType*)constant->mType;
- auto& member = structType->mMembers[castedInst->mIdx];
- wantDefaultType = member.mType;
- }
- else if (constant->mType->IsSizedArray())
- {
- BeSizedArrayType* arrayType = (BeSizedArrayType*)constant->mType;
- wantDefaultType = arrayType->mElementType;
- }
- if (wantDefaultType != NULL)
- {
- // switch (wantDefaultType->mTypeCode)
- // {
- // case BeTypeCode_Boolean:
- // case BeTypeCode_Int8:
- // result.mKind = BeMCOperandKind_Immediate_i8;
- // break;
- // case BeTypeCode_Int16:
- // result.mKind = BeMCOperandKind_Immediate_i16;
- // break;
- // case BeTypeCode_Int32:
- // result.mKind = BeMCOperandKind_Immediate_i32;
- // break;
- // case BeTypeCode_Int64:
- // result.mKind = BeMCOperandKind_Immediate_i64;
- // break;
- // case BeTypeCode_Float:
- // result.mKind = BeMCOperandKind_Immediate_f32;
- // break;
- // case BeTypeCode_Double:
- // result.mKind = BeMCOperandKind_Immediate_f64;
- // break;
- // case BeTypeCode_Pointer:
- // result.mKind = BeMCOperandKind_Immediate_Null;
- // result.mType = wantDefaultType;
- // break;
- // case BeTypeCode_Struct:
- // case BeTypeCode_SizedArray:
- // {
- // auto subConst = mAlloc.Alloc<BeConstant>();
- // subConst->mType = wantDefaultType;
- // result.mConstant = subConst;
- // result.mKind = BeMCOperandKind_ConstAgg;
- // }
- // break;
- // default:
- // NotImpl();
- // }
- Fail("Unhandled extract");
- }
- break;
- }
- else
- {
- mcAgg = GetOperand(castedInst->mAggVal);
- }
- auto aggType = mcAgg.mType;
- int byteOffset = 0;
- BeType* memberType = NULL;
- if (aggType->IsSizedArray())
- {
- auto sizedArray = (BeSizedArrayType*)aggType;
- memberType = sizedArray->mElementType;
- byteOffset = memberType->GetStride() * castedInst->mIdx;
- }
- else
- {
- BF_ASSERT(aggType->IsStruct());
- BeStructType* structType = (BeStructType*)aggType;
- auto& structMember = structType->mMembers[castedInst->mIdx];
- byteOffset = structMember.mByteOffset;
- memberType = structMember.mType;
- }
- if (byteOffset != 0)
- {
- auto ptrVal = FrameAlloc(beModule->mContext->GetPrimitiveType(BeTypeCode_Int32));
- Emit(CeOp_FrameAddrOfs_32);
- EmitFrameOffset(ptrVal);
- EmitFrameOffset(mcAgg);
- Emit((int32)byteOffset);
- result = FrameAlloc(memberType);
- EmitSizedOp(CeOp_Load_8, memberType->mSize);
- EmitFrameOffset(result);
- EmitFrameOffset(ptrVal);
- }
- else
- {
- result = mcAgg;
- result.mType = memberType;
- }
- }
- break;
- case BeBrInst::TypeId:
- {
- auto castedInst = (BeBrInst*)inst;
- auto targetBlock = GetOperand(castedInst->mTargetBlock);
- BF_ASSERT(targetBlock.mKind == CeOperandKind_Block);
- FlushPhi(ceBlock, targetBlock.mBlockIdx);
- if (targetBlock.mBlockIdx == blockIdx + 1)
- {
- // Do nothing - just continuing to next block
- break;
- }
- EmitJump(CeOp_Jmp, targetBlock);
- }
- break;
- case BeCondBrInst::TypeId:
- {
- auto castedInst = (BeCondBrInst*)inst;
- auto testVal = GetOperand(castedInst->mCond, true);
- auto trueBlock = GetOperand(castedInst->mTrueBlock);
- auto falseBlock = GetOperand(castedInst->mFalseBlock);
- FlushPhi(ceBlock, trueBlock.mBlockIdx);
- EmitJump(CeOp_JmpIf, trueBlock);
- EmitFrameOffset(testVal);
- FlushPhi(ceBlock, falseBlock.mBlockIdx);
- EmitJump(CeOp_Jmp, falseBlock);
- }
- break;
- case BePhiInst::TypeId:
- result = GetOperand(inst);
- BF_ASSERT(result);
- break;
- case BeNegInst::TypeId:
- {
- auto castedInst = (BeNegInst*)inst;
- auto ceValue = GetOperand(castedInst->mValue);
- EmitUnaryOp(CeOp_Neg_I8, CeOp_Neg_F32, ceValue, result);
- }
- break;
- case BeNotInst::TypeId:
- {
- auto castedInst = (BeNotInst*)inst;
- auto ceValue = GetOperand(castedInst->mValue);
- if (ceValue.mType->mTypeCode == BeTypeCode_Boolean)
- EmitUnaryOp(CeOp_Not_I1, CeOp_InvalidOp, ceValue, result);
- else
- EmitUnaryOp(CeOp_Not_I8, CeOp_InvalidOp, ceValue, result);
- }
- break;
- case BeSwitchInst::TypeId:
- {
- auto castedInst = (BeSwitchInst*)inst;
- std::stable_sort(castedInst->mCases.begin(), castedInst->mCases.end(), [&](const BeSwitchCase& lhs, const BeSwitchCase& rhs)
- {
- return lhs.mValue->mInt64 < rhs.mValue->mInt64;
- });
- int numVals = castedInst->mCases.size();
- if (numVals > 0)
- {
- EmitBinarySwitchSection(castedInst, 0, castedInst->mCases.size());
- }
- auto mcDefaultBlock = GetOperand(castedInst->mDefaultBlock);
- EmitJump(CeOp_Jmp, mcDefaultBlock);
- }
- break;
- case BeCallInst::TypeId:
- {
- auto castedInst = (BeCallInst*)inst;
- BeType* returnType = NULL;
- bool isVarArg = false;
- bool useAltArgs = false;
- CeOperand ceFunc;
- BeFunctionType* beFuncType = NULL;
- CeOperand virtTarget;
- int ifaceTypeId = -1;
- int virtualTableIdx = -1;
- if (auto intrin = BeValueDynCast<BeIntrinsic>(castedInst->mFunc))
- {
- switch (intrin->mKind)
- {
- case BfIRIntrinsic_Abs:
- EmitUnaryOp(CeOp_Abs_I8, CeOp_Abs_F32, GetOperand(castedInst->mArgs[0].mValue), result);
- break;
- case BfIRIntrinsic_Cast:
- {
- result = GetOperand(castedInst->mArgs[0].mValue);
- result.mType = intrin->mReturnType;
- }
- break;
- case BfIRIntrinsic_MemCpy:
- case BfIRIntrinsic_MemMove:
- {
- CeOperand ceDestPtr = GetOperand(castedInst->mArgs[0].mValue);
- CeOperand ceSrcPtr = GetOperand(castedInst->mArgs[1].mValue);
- CeOperand ceSize = GetOperand(castedInst->mArgs[2].mValue);
- Emit(CeOp_MemCpy);
- EmitFrameOffset(ceDestPtr);
- EmitFrameOffset(ceSrcPtr);
- EmitFrameOffset(ceSize);
- }
- break;
- case BfIRIntrinsic_MemSet:
- {
- CeOperand ceDestPtr = GetOperand(castedInst->mArgs[0].mValue);
- CeOperand ceValue = GetOperand(castedInst->mArgs[1].mValue);
- CeOperand ceSize = GetOperand(castedInst->mArgs[2].mValue);
- Emit(CeOp_MemSet);
- EmitFrameOffset(ceDestPtr);
- EmitFrameOffset(ceValue);
- EmitFrameOffset(ceSize);
- }
- break;
- case BfIRIntrinsic_AtomicFence:
- // Nothing to do
- break;
- case BfIRIntrinsic_AtomicAdd:
- EmitBinaryOp(CeOp_Add_I8, CeOp_Add_F32, GetOperand(castedInst->mArgs[0].mValue), GetOperand(castedInst->mArgs[1].mValue), result);
- break;
- case BfIRIntrinsic_AtomicOr:
- EmitBinaryOp(CeOp_Or_I8, CeOp_InvalidOp, GetOperand(castedInst->mArgs[0].mValue), GetOperand(castedInst->mArgs[1].mValue), result);
- break;
- case BfIRIntrinsic_AtomicSub:
- EmitBinaryOp(CeOp_Sub_I8, CeOp_Sub_F32, GetOperand(castedInst->mArgs[0].mValue), GetOperand(castedInst->mArgs[1].mValue), result);
- break;
- case BfIRIntrinsic_AtomicXor:
- EmitBinaryOp(CeOp_Xor_I8, CeOp_InvalidOp, GetOperand(castedInst->mArgs[0].mValue), GetOperand(castedInst->mArgs[1].mValue), result);
- break;
- case BfIRIntrinsic_DebugTrap:
- Emit(CeOp_DbgBreak);
- break;
- case BfIRIntrinsic_AtomicCmpXChg:
- {
- auto mcPtr = GetOperand(castedInst->mArgs[0].mValue, true);
- auto mcComparand = GetOperand(castedInst->mArgs[1].mValue);
- auto mcValue = GetOperand(castedInst->mArgs[2].mValue);
- CeOperand mcPrev = EmitLoad(mcPtr);
- CeOperand cmpResult;
- EmitBinaryOp(CeOp_Cmp_EQ_I8, CeOp_InvalidOp, mcPrev, mcComparand, cmpResult);
- Emit(CeOp_JmpIfNot);
- Emit((int32)14);
- EmitFrameOffset(cmpResult);
- if (mcPtr.mKind == CeOperandKind_AllocaAddr)
- {
- EmitSizedOp(CeOp_Move_8, mcValue, NULL, true);
- Emit((int32)mcPtr.mFrameOfs);
- }
- else
- {
- EmitSizedOp(CeOp_Store_8, mcValue, NULL, true);
- EmitFrameOffset(mcPtr);
- }
- result = mcPrev;
- }
- break;
- default:
- Emit(CeOp_Error);
- Emit((int32)CeErrorKind_Intrinsic);
- break;
- }
- }
- else if (auto beFunction = BeValueDynCast<BeFunction>(castedInst->mFunc))
- {
- beFuncType = beFunction->GetFuncType();
- CeFunctionInfo* ceFunctionInfo = NULL;
- mCeMachine->mNamedFunctionMap.TryGetValue(beFunction->mName, &ceFunctionInfo);
- if (ceFunctionInfo != NULL)
- {
- CeOp ceOp = CeOp_InvalidOp;
- if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_NotSet)
- mCeMachine->CheckFunctionKind(ceFunctionInfo->mCeFunction);
- if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Abs)
- ceOp = CeOp_Abs_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Acos)
- ceOp = CeOp_Acos_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Asin)
- ceOp = CeOp_Asin_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Atan)
- ceOp = CeOp_Atan_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Atan2)
- ceOp = CeOp_Atan2_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Ceiling)
- ceOp = CeOp_Ceiling_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Cos)
- ceOp = CeOp_Cos_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Cosh)
- ceOp = CeOp_Cosh_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Exp)
- ceOp = CeOp_Exp_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Floor)
- ceOp = CeOp_Floor_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Log)
- ceOp = CeOp_Log_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Log10)
- ceOp = CeOp_Log10_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Pow)
- ceOp = CeOp_Pow_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Round)
- ceOp = CeOp_Round_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Sin)
- ceOp = CeOp_Sin_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Sinh)
- ceOp = CeOp_Sinh_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Sqrt)
- ceOp = CeOp_Sqrt_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Tan)
- ceOp = CeOp_Tan_F32;
- else if (ceFunctionInfo->mCeFunction->mFunctionKind == CeFunctionKind_Math_Tanh)
- ceOp = CeOp_Tanh_F32;
- if (ceOp != CeOp_InvalidOp)
- {
- if (beFuncType->mReturnType->mSize == 8)
- ceOp = (CeOp)(ceOp + 1);
- result = FrameAlloc(beFuncType->mReturnType);
- if (beFuncType->mParams.size() == 1)
- {
- auto arg0 = GetOperand(castedInst->mArgs[0].mValue);
- Emit(ceOp);
- EmitFrameOffset(result);
- EmitFrameOffset(arg0);
- }
- else
- {
- auto arg0 = GetOperand(castedInst->mArgs[0].mValue);
- auto arg1 = GetOperand(castedInst->mArgs[1].mValue);
- Emit(ceOp);
- EmitFrameOffset(result);
- EmitFrameOffset(arg0);
- EmitFrameOffset(arg1);
- }
- break;
- }
- }
- if (beFunction->mName == "malloc")
- {
- result = FrameAlloc(beFuncType->mReturnType);
- auto ceSize = GetOperand(castedInst->mArgs[0].mValue);
- Emit(CeOp_Malloc);
- EmitFrameOffset(result);
- EmitFrameOffset(ceSize);
- break;
- }
- if (beFunction->mName == "free")
- {
- auto cePtr = GetOperand(castedInst->mArgs[0].mValue);
- Emit(CeOp_Free);
- EmitFrameOffset(cePtr);
- break;
- }
- ceFunc = GetOperand(beFunction, false, true);
- }
- else if (auto beGetVirtualFunc = BeValueDynCast<BeComptimeGetVirtualFunc>(castedInst->mFunc))
- {
- virtTarget = GetOperand(beGetVirtualFunc->mValue);
- virtualTableIdx = beGetVirtualFunc->mVirtualTableIdx;
- auto resultType = beGetVirtualFunc->GetType();
- BF_ASSERT(resultType->IsPointer());
- beFuncType = (BeFunctionType*)((BePointerType*)resultType)->mElementType;
- }
- else if (auto beGetInterfaceFunc = BeValueDynCast<BeComptimeGetInterfaceFunc>(castedInst->mFunc))
- {
- virtTarget = GetOperand(beGetInterfaceFunc->mValue);
- ifaceTypeId = beGetInterfaceFunc->mIFaceTypeId;
- virtualTableIdx = beGetInterfaceFunc->mMethodIdx;
- auto resultType = beGetInterfaceFunc->GetType();
- BF_ASSERT(resultType->IsPointer());
- beFuncType = (BeFunctionType*)((BePointerType*)resultType)->mElementType;
- }
- else
- {
- ceFunc = GetOperand(castedInst->mFunc, false, true);
- auto funcType = castedInst->mFunc->GetType();
- if (funcType->IsPointer())
- {
- auto ptrType = (BePointerType*)funcType;
- if (ptrType->mElementType->mTypeCode == BeTypeCode_Function)
- {
- beFuncType = (BeFunctionType*)ptrType->mElementType;
- }
- }
- }
- if ((ceFunc) || (virtualTableIdx != -1))
- {
- CeOperand thisOperand;
- int stackAdjust = 0;
- for (int argIdx = (int)castedInst->mArgs.size() - 1; argIdx >= 0; argIdx--)
- {
- auto& arg = castedInst->mArgs[argIdx];
- auto ceArg = GetOperand(arg.mValue);
- if (!ceArg)
- continue;
- if (argIdx == 0)
- thisOperand = ceArg;
- EmitSizedOp(CeOp_Push_8, ceArg, NULL, true);
- stackAdjust += ceArg.mType->mSize;
- }
- if (beFuncType->mReturnType->mSize > 0)
- {
- Emit(CeOp_AdjustSPConst);
- Emit((int32)-beFuncType->mReturnType->mSize);
- }
- if (!ceFunc)
- ceFunc = FrameAlloc(beModule->mContext->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
- if (ifaceTypeId != -1)
- {
- Emit(CeOp_GetMethod_IFace);
- EmitFrameOffset(ceFunc);
- EmitFrameOffset(thisOperand);
- Emit((int32)ifaceTypeId);
- Emit((int32)virtualTableIdx);
- }
- else if (virtualTableIdx != -1)
- {
- Emit(CeOp_GetMethod_Virt);
- EmitFrameOffset(ceFunc);
- EmitFrameOffset(thisOperand);
- Emit((int32)virtualTableIdx);
- }
- if (ceFunc.mKind == CeOperandKind_CallTableIdx)
- {
- CeOperand result = FrameAlloc(mCeMachine->GetBeContext()->GetPrimitiveType((sizeof(BfMethodInstance*) == 8) ? BeTypeCode_Int64 : BeTypeCode_Int32));
- Emit(CeOp_GetMethod);
- EmitFrameOffset(result);
- Emit((int32)ceFunc.mCallTableIdx);
- ceFunc = result;
- }
- Emit(CeOp_Call);
- EmitFrameOffset(ceFunc);
- if (beFuncType->mReturnType->mSize > 0)
- {
- result = FrameAlloc(beFuncType->mReturnType);
- EmitSizedOp(CeOp_Pop_8, result, NULL, true);
- }
- if (stackAdjust > 0)
- {
- Emit(CeOp_AdjustSPConst);
- Emit(stackAdjust);
- }
- }
- }
- break;
- case BeMemSetInst::TypeId:
- {
- auto castedInst = (BeMemSetInst*)inst;
- auto ceAddr = GetOperand(castedInst->mAddr);
- if (auto constVal = BeValueDynCast<BeConstant>(castedInst->mVal))
- {
- if (auto constSize = BeValueDynCast<BeConstant>(castedInst->mSize))
- {
- if (constVal->mUInt8 == 0)
- {
- Emit(CeOp_MemSet_Const);
- EmitFrameOffset(ceAddr);
- Emit((uint8)0);
- Emit((int32)constSize->mUInt32);
- break;
- }
- }
- }
- auto ceVal = GetOperand(castedInst->mVal);
- auto ceSize = GetOperand(castedInst->mSize);
- Emit(CeOp_MemSet);
- EmitFrameOffset(ceAddr);
- EmitFrameOffset(ceVal);
- EmitFrameOffset(ceSize);
- }
- break;
- case BeFenceInst::TypeId:
- break;
- case BeStackSaveInst::TypeId:
- {
- result = FrameAlloc(mIntPtrType);
- Emit(CeOp_GetSP);
- EmitFrameOffset(result);
- }
- break;
- case BeStackRestoreInst::TypeId:
- {
- auto castedInst = (BeStackRestoreInst*)inst;
- auto mcStackVal = GetOperand(castedInst->mStackVal);
- Emit(CeOp_SetSP);
- EmitFrameOffset(mcStackVal);
- }
- break;
- case BeComptimeError::TypeId:
- {
- auto castedInst = (BeComptimeError*)inst;
- Emit(CeOp_Error);
- Emit(castedInst->mError);
- }
- break;
- case BeComptimeGetType::TypeId:
- {
- auto castedInst = (BeComptimeGetType*)inst;
- result.mKind = CeOperandKind_Immediate;
- result.mImmediate = castedInst->mTypeId;
- result.mType = beModule->mContext->GetPrimitiveType(BeTypeCode_Int32);
- }
- break;
- case BeComptimeGetReflectType::TypeId:
- {
- auto castedInst = (BeComptimeGetReflectType*)inst;
- auto ptrType = beModule->mContext->GetVoidPtrType();
- result = FrameAlloc(ptrType);
- Emit(CeOp_GetReflectType);
- EmitFrameOffset(result);
- Emit((int32)castedInst->mTypeId);
- }
- break;
- case BeComptimeDynamicCastCheck::TypeId:
- {
- auto castedInst = (BeComptimeDynamicCastCheck*)inst;
- auto mcValue = GetOperand(castedInst->mValue);
- auto ptrType = beModule->mContext->GetVoidPtrType();
- result = FrameAlloc(ptrType);
- Emit(CeOp_DynamicCastCheck);
- EmitFrameOffset(result);
- EmitFrameOffset(mcValue);
- Emit((int32)castedInst->mTypeId);
- }
- break;
- case BeDbgDeclareInst::TypeId:
- {
- auto castedInst = (BeDbgDeclareInst*)inst;
- auto mcValue = GetOperand(castedInst->mValue, true);
- if (mCeFunction->mDbgInfo != NULL)
- {
- bool isConst = false;
- auto beType = castedInst->mDbgVar->mType;
- if (auto dbgConstType = BeValueDynCast<BeDbgConstType>(beType))
- {
- isConst = true;
- beType = dbgConstType->mElement;
- }
- if (auto dbgTypeId = BeValueDynCast<BeDbgTypeId>(beType))
- {
- mDbgVariableMap[castedInst->mValue] = mCeFunction->mDbgInfo->mVariables.mSize;
- CeDbgVariable dbgVariable;
- dbgVariable.mName = castedInst->mDbgVar->mName;
- dbgVariable.mValue = mcValue;
- dbgVariable.mType = mCeMachine->mCeModule->mContext->mTypes[dbgTypeId->mTypeId];
- dbgVariable.mScope = scopeIdx;
- dbgVariable.mIsConst = isConst;
- dbgVariable.mStartCodePos = mCeFunction->mCode.mSize;
- dbgVariable.mEndCodePos = -1;
- mCeFunction->mDbgInfo->mVariables.Add(dbgVariable);
- }
- }
- }
- break;
- case BeLifetimeEndInst::TypeId:
- {
- auto castedInst = (BeLifetimeEndInst*)inst;
- int varIdx = 0;
- if (mDbgVariableMap.TryGetValue(castedInst->mPtr, &varIdx))
- {
- auto dbgVar = &mCeFunction->mDbgInfo->mVariables[varIdx];
- dbgVar->mEndCodePos = mCeFunction->mCode.mSize;
- }
- }
- break;
- case BeEnsureInstructionAtInst::TypeId:
- {
- if (mCeMachine->mDebugger != NULL)
- {
- Emit(CeOp_Nop);
- }
- }
- break;
- default:
- Fail("Unhandled instruction");
- return;
- }
- if (result.mKind != CeOperandKind_None)
- mValueToOperand[inst] = result;
- if ((startCodePos != GetCodePos()) && (prevEmitDbgPos != mCurDbgLoc))
- {
- CeEmitEntry emitEntry;
- emitEntry.mCodePos = startCodePos;
- emitEntry.mScope = scopeIdx;
- if ((mCurDbgLoc != NULL) && (mCurDbgLoc->mLine != -1))
- {
- emitEntry.mLine = mCurDbgLoc->mLine;
- emitEntry.mColumn = mCurDbgLoc->mColumn;
- }
- else if (!mCeFunction->mEmitTable.IsEmpty())
- {
- auto& prevEmitEntry = mCeFunction->mEmitTable.back();
- emitEntry.mScope = prevEmitEntry.mScope;
- emitEntry.mLine = prevEmitEntry.mLine;
- emitEntry.mColumn = -1;
- }
- else
- {
- emitEntry.mLine = -1;
- emitEntry.mColumn = -1;
- }
- mCeFunction->mEmitTable.Add(emitEntry);
- prevEmitDbgPos = mCurDbgLoc;
- }
- }
- }
- if (mCeFunction->mDbgInfo != NULL)
- {
- for (auto& dbgVar : mCeFunction->mDbgInfo->mVariables)
- if (dbgVar.mEndCodePos == -1)
- dbgVar.mEndCodePos = mCeFunction->mCode.mSize;
- }
- for (auto& jumpEntry : mJumpTable)
- {
- auto& ceBlock = mBlocks[jumpEntry.mBlockIdx];
- *((int32*)(&mCeFunction->mCode[0] + jumpEntry.mEmitPos)) = ceBlock.mEmitOfs - jumpEntry.mEmitPos - 4;
- }
- if (irCodeGen->mFailed)
- {
- mCeMachine->Fail(StrFormat("IRCodeGen Failed: %s", irCodeGen->mErrorMsg.c_str()));
- irCodeGen->mFailed = false;
- }
- if (mCeMachine->HasFailed())
- {
- Fail("ConstEval machine failed");
- return;
- }
- if (mCeFunction->mCode.size() == 0)
- {
- Fail("No method definition available");
- return;
- }
- if (mCeFunction->mGenError.IsEmpty())
- mCeFunction->mFailed = false;
- mCeFunction->mFrameSize = mFrameSize;
- }
- //////////////////////////////////////////////////////////////////////////
- CeContext::CeContext()
- {
- mPrevContext = NULL;
- mCurEvalFlags = CeEvalFlags_None;
- mCeMachine = NULL;
- mReflectTypeIdOffset = -1;
- mExecuteId = -1;
- mStackSize = -1;
- mRecursiveDepth = -1;
- mCurCallSource = NULL;
- mHeap = new ContiguousHeap();
- mCurFrame = NULL;
- mCurModule = NULL;
- mCurMethodInstance = NULL;
- mCallerMethodInstance = NULL;
- mCallerTypeInstance = NULL;
- mCallerActiveTypeDef = NULL;
- mCurExpectingType = NULL;
- mCurEmitContext = NULL;
- mTypeDeclState = NULL;
- }
- CeContext::~CeContext()
- {
- delete mHeap;
- delete mTypeDeclState;
- BF_ASSERT(mInternalDataMap.IsEmpty());
- }
- BfError* CeContext::Fail(const StringImpl& error)
- {
- if (mCurModule == NULL)
- return NULL;
- if (mCurEmitContext != NULL)
- mCurEmitContext->mFailed = true;
- SetAndRestoreValue<BfTypeInstance*> prevTypeInst(mCurModule->mCurTypeInstance, mCallerTypeInstance);
- auto bfError = mCurModule->Fail(StrFormat("Unable to comptime %s", mCurModule->MethodToString(mCurMethodInstance).c_str()), mCurCallSource->mRefNode, (mCurEvalFlags & CeEvalFlags_PersistantError) != 0);
- if (bfError == NULL)
- return NULL;
- bool forceQueue = mCeMachine->mCompiler->GetAutoComplete() != NULL;
- if ((mCeMachine->mDebugger != NULL) && (mCeMachine->mDebugger->mCurDbgState != NULL))
- forceQueue = true;
- mCeMachine->mCompiler->mPassInstance->MoreInfo(error, forceQueue);
- return bfError;
- }
- BfError* CeContext::Fail(const CeFrame& curFrame, const StringImpl& str)
- {
- if (mCurEmitContext != NULL)
- mCurEmitContext->mFailed = true;
- SetAndRestoreValue<BfTypeInstance*> prevTypeInst(mCurModule->mCurTypeInstance, mCallerTypeInstance);
- auto bfError = mCurModule->Fail(StrFormat("Unable to comptime %s", mCurModule->MethodToString(mCurMethodInstance).c_str()), mCurCallSource->mRefNode,
- (mCurEvalFlags & CeEvalFlags_PersistantError) != 0,
- ((mCurEvalFlags & CeEvalFlags_DeferIfNotOnlyError) != 0) && !mCurModule->mHadBuildError);
- if (bfError == NULL)
- return NULL;
- auto passInstance = mCeMachine->mCompiler->mPassInstance;
- for (int stackIdx = mCallStack.size(); stackIdx >= 0; stackIdx--)
- {
- bool isHeadEntry = stackIdx == mCallStack.size();
- auto* ceFrame = (isHeadEntry) ? &curFrame : &mCallStack[stackIdx];
- auto ceFunction = ceFrame->mFunction;
- CeEmitEntry* emitEntry = ceFunction->FindEmitEntry(ceFrame->mInstPtr - ceFunction->mCode.mVals - 1);
- StringT<256> err;
- if (isHeadEntry)
- {
- err = str;
- err += " ";
- }
- auto contextMethodInstance = mCallerMethodInstance;
- auto contextTypeInstance = mCallerTypeInstance;
- if (stackIdx > 1)
- {
- auto func = mCallStack[stackIdx - 1].mFunction;
- contextMethodInstance = func->mCeFunctionInfo->mMethodInstance;
- contextTypeInstance = contextMethodInstance->GetOwner();
- }
- auto _AddCeMethodInstance = [&](BfMethodInstance* methodInstance)
- {
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, contextTypeInstance);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, contextMethodInstance);
- err += mCeMachine->mCeModule->MethodToString(methodInstance, BfMethodNameFlag_OmitParams);
- };
- auto _AddError = [&](const StringImpl& filePath, int line, int column)
- {
- err += StrFormat(" at line% d:%d in %s", line + 1, column + 1, filePath.c_str());
- auto moreInfo = passInstance->MoreInfo(err, mCeMachine->mCeModule->mCompiler->GetAutoComplete() != NULL);
- if ((moreInfo != NULL))
- {
- BfErrorLocation* location = new BfErrorLocation();
- location->mFile = filePath;
- location->mLine = line;
- location->mColumn = column;
- moreInfo->mLocation = location;
- }
- };
- if (emitEntry != NULL)
- {
- int scopeIdx = emitEntry->mScope;
- int prevInlineIdx = -1;
- while (scopeIdx != -1)
- {
- err += StrFormat("in comptime ");
- int line = emitEntry->mLine;
- int column = emitEntry->mColumn;
- String fileName;
- if (prevInlineIdx != -1)
- {
- auto dbgInlineInfo = &ceFunction->mDbgInlineTable[prevInlineIdx];
- line = dbgInlineInfo->mLine;
- column = dbgInlineInfo->mColumn;
- }
- CeDbgScope* ceScope = &ceFunction->mDbgScopes[scopeIdx];
- if (ceScope->mMethodVal == -1)
- {
- if (ceFunction->mMethodInstance != NULL)
- _AddCeMethodInstance(ceFunction->mMethodInstance);
- else
- _AddCeMethodInstance(ceFunction->mCeInnerFunctionInfo->mOwner->mMethodInstance);
- }
- else
- {
- if ((ceScope->mMethodVal & CeDbgScope::MethodValFlag_MethodRef) != 0)
- {
- auto dbgMethodRef = &ceFunction->mDbgMethodRefTable[ceScope->mMethodVal & CeDbgScope::MethodValFlag_IdxMask];
- err += dbgMethodRef->ToString();
- }
- else
- {
- auto callTableEntry = &ceFunction->mCallTable[ceScope->mMethodVal];
- _AddCeMethodInstance(callTableEntry->mFunctionInfo->mMethodInstance);
- }
- }
- _AddError(ceFunction->mDbgScopes[emitEntry->mScope].mFilePath, line, column);
- if (ceScope->mInlinedAt == -1)
- break;
- auto inlineInfo = &ceFrame->mFunction->mDbgInlineTable[ceScope->mInlinedAt];
- scopeIdx = inlineInfo->mScope;
- prevInlineIdx = ceScope->mInlinedAt;
- err.Clear();
- }
- }
- else
- {
- err += StrFormat("in comptime ");
- if (ceFunction->mMethodInstance != NULL)
- _AddCeMethodInstance(ceFunction->mMethodInstance);
- else
- _AddCeMethodInstance(ceFunction->mCeInnerFunctionInfo->mOwner->mMethodInstance);
- if ((emitEntry != NULL) && (emitEntry->mScope != -1))
- {
- _AddError(ceFunction->mDbgScopes[emitEntry->mScope].mFilePath, emitEntry->mLine, emitEntry->mColumn);
- }
- else
- {
- auto moreInfo = passInstance->MoreInfo(err, mCeMachine->mCeModule->mCompiler->GetAutoComplete() != NULL);
- }
- }
- }
- return bfError;
- }
- //////////////////////////////////////////////////////////////////////////
- void CeContext::CalcWorkingDir()
- {
- if (mWorkingDir.IsEmpty())
- {
- BfProject* activeProject = NULL;
- auto activeTypeDef = mCallerActiveTypeDef;
- if (activeTypeDef != NULL)
- activeProject = activeTypeDef->mProject;
- if (activeProject != NULL)
- mWorkingDir = activeProject->mDirectory;
- }
- }
- void CeContext::FixRelativePath(StringImpl& path)
- {
- CalcWorkingDir();
- if (!mWorkingDir.IsEmpty())
- path = GetAbsPath(path, mWorkingDir);
- }
- bool CeContext::AddRebuild(const CeRebuildKey& key, const CeRebuildValue& value)
- {
- if (mCurModule == NULL)
- return false;
- if (mCallerTypeInstance == NULL)
- return false;
- if ((mCurEvalFlags & CeEvalFlags_NoRebuild) != 0)
- return false;
- if (mCallerTypeInstance->mCeTypeInfo == NULL)
- mCallerTypeInstance->mCeTypeInfo = new BfCeTypeInfo();
- mCallerTypeInstance->mCeTypeInfo->mRebuildMap[key] = value;
- mCurModule->mCompiler->mHasComptimeRebuilds = true;
- return true;
- }
- void CeContext::AddFileRebuild(const StringImpl& path)
- {
- auto timeStamp = BfpFile_GetTime_LastWrite(path.c_str());
- if (timeStamp != 0)
- {
- String fixedPath = FixPathAndCase(path);
- CeRebuildKey rebuildKey;
- rebuildKey.mKind = CeRebuildKey::Kind_File;
- rebuildKey.mString = fixedPath;
- CeRebuildValue rebuildValue;
- rebuildValue.mInt = timeStamp;
- if (AddRebuild(rebuildKey, rebuildValue))
- mCurModule->mCompiler->mRebuildFileSet.Add(fixedPath);
- }
- }
- void CeContext::AddTypeSigRebuild(BfType* type)
- {
- mCurModule->AddDependency(type, mCurModule->mCurTypeInstance, BfDependencyMap::DependencyFlag_TypeSignature);
- }
- uint8* CeContext::CeMalloc(int size)
- {
- #ifdef CE_ENABLE_HEAP
- auto heapRef = mHeap->Alloc(size);
- auto ceAddr = mStackSize + heapRef;
- int sizeDelta = (ceAddr + size) - mMemory.mSize;
- if (sizeDelta > 0)
- mMemory.GrowUninitialized(sizeDelta);
- return mMemory.mVals + ceAddr;
- #else
- return mMemory.GrowUninitialized(size);
- #endif
- }
- uint8* CeContext::CeMallocZero(int size)
- {
- uint8* ptr = CeMalloc(size);
- memset(ptr, 0, size);
- return ptr;
- }
- bool CeContext::CeFree(addr_ce addr)
- {
- #ifdef CE_ENABLE_HEAP
- ContiguousHeap::AllocRef heapRef = addr - mStackSize;
- return mHeap->Free(heapRef);
- #else
- return true;
- #endif
- }
- addr_ce CeContext::CeAllocArray(BfArrayType* arrayType, int count, addr_ce& elemsAddr)
- {
- mCeMachine->mCeModule->PopulateType(arrayType);
- BfType* elemType = arrayType->GetUnderlyingType();
- auto countOffset = arrayType->mBaseType->mFieldInstances[0].mDataOffset;
- auto elemOffset = arrayType->mFieldInstances[0].mDataOffset;
- int allocSize = elemOffset + elemType->GetStride() * count;
- uint8* mem = CeMalloc(allocSize);
- memset(mem, 0, allocSize);
- *(int32*)(mem) = arrayType->mTypeId;
- *(int32*)(mem + countOffset) = count;
- elemsAddr = (addr_ce)(mem + elemOffset - mMemory.mVals);
- return (addr_ce)(mem - mMemory.mVals);
- }
- addr_ce CeContext::GetConstantData(BeConstant* constant)
- {
- auto writeConstant = constant;
- if (auto gvConstant = BeValueDynCast<BeGlobalVariable>(writeConstant))
- {
- if (gvConstant->mInitializer != NULL)
- writeConstant = gvConstant->mInitializer;
- }
- CeConstStructData structData;
- auto result = mCeMachine->WriteConstant(structData, writeConstant, this, NULL);
- BF_ASSERT(result == CeErrorKind_None);
- uint8* ptr = CeMallocZero(structData.mData.mSize);
- memcpy(ptr, structData.mData.mVals, structData.mData.mSize);
- return (addr_ce)(ptr - mMemory.mVals);
- }
- addr_ce CeContext::GetReflectTypeDecl(int typeId)
- {
- if (mTypeDeclState == NULL)
- mTypeDeclState = new CeTypeDeclState();
- if (mTypeDeclState->mReflectDeclMap.IsEmpty())
- {
- CeRebuildKey rebuildKey;
- rebuildKey.mKind = CeRebuildKey::Kind_TypeDeclListHash;
- CeRebuildValue rebuildValue;
- rebuildValue.mInt = mCeMachine->mCompiler->mSystem->GetTypeDeclListHash();
- AddRebuild(rebuildKey, rebuildValue);
- }
- addr_ce* addrPtr = NULL;
- if (!mTypeDeclState->mReflectDeclMap.TryAdd(typeId, NULL, &addrPtr))
- return *addrPtr;
- auto ceModule = mCeMachine->mCeModule;
- SetAndRestoreValue<bool> ignoreWrites(ceModule->mBfIRBuilder->mIgnoreWrites, false);
- if (ceModule->mContext->mBfTypeType == NULL)
- ceModule->mContext->ReflectInit();
- if ((uintptr)typeId >= (uintptr)mCeMachine->mCeModule->mContext->mTypes.mSize)
- return 0;
- auto bfType = mCeMachine->mCeModule->mContext->mTypes[typeId];
- if (bfType == NULL)
- return 0;
-
- if (bfType->mDefineState < BfTypeDefineState_HasCustomAttributes)
- ceModule->PopulateType(bfType, BfPopulateType_CustomAttributes);
-
- BfProject* curProject = NULL;
- auto activeTypeDef = mCurModule->GetActiveTypeDef();
- if (activeTypeDef != NULL)
- curProject = activeTypeDef->mProject;
- if (curProject == NULL)
- return 0;
- BfCreateTypeDataContext createTypeDataCtx;
- auto irData = ceModule->CreateTypeDeclData(bfType, curProject);
- BeValue* beValue = NULL;
- if (auto constant = mCeMachine->mCeModule->mBfIRBuilder->GetConstant(irData))
- {
- if (constant->mConstType == BfConstType_BitCast)
- {
- auto bitcast = (BfConstantBitCast*)constant;
- constant = mCeMachine->mCeModule->mBfIRBuilder->GetConstantById(bitcast->mTarget);
- }
- if (constant->mConstType == BfConstType_GlobalVar)
- {
- auto globalVar = (BfGlobalVar*)constant;
- beValue = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen->GetBeValue(globalVar->mStreamId);
- }
- }
- if (auto constant = BeValueDynCast<BeConstant>(beValue))
- *addrPtr = GetConstantData(constant);
- // We need to 'get' again because we might have resized
- return *addrPtr;
- }
- addr_ce CeContext::GetReflectType(int typeId)
- {
- addr_ce* addrPtr = NULL;
- if (!mReflectMap.TryAdd(typeId, NULL, &addrPtr))
- return *addrPtr;
- auto ceModule = mCeMachine->mCeModule;
- SetAndRestoreValue<bool> ignoreWrites(ceModule->mBfIRBuilder->mIgnoreWrites, false);
- if (ceModule->mContext->mBfTypeType == NULL)
- ceModule->mContext->ReflectInit();
- if ((uintptr)typeId >= (uintptr)mCeMachine->mCeModule->mContext->mTypes.mSize)
- return 0;
- auto bfType = mCeMachine->mCeModule->mContext->mTypes[typeId];
- if (bfType == NULL)
- return 0;
- if (bfType->mDefineState != BfTypeDefineState_CETypeInit)
- ceModule->PopulateType(bfType, BfPopulateType_DataAndMethods);
- BfCreateTypeDataContext createTypeDataCtx;
- auto irData = ceModule->CreateTypeData(bfType, createTypeDataCtx, true, true, true, false);
- BeValue* beValue = NULL;
- if (auto constant = mCeMachine->mCeModule->mBfIRBuilder->GetConstant(irData))
- {
- if (constant->mConstType == BfConstType_BitCast)
- {
- auto bitcast = (BfConstantBitCast*)constant;
- constant = mCeMachine->mCeModule->mBfIRBuilder->GetConstantById(bitcast->mTarget);
- }
- if (constant->mConstType == BfConstType_GlobalVar)
- {
- auto globalVar = (BfGlobalVar*)constant;
- beValue = mCeMachine->mCeModule->mBfIRBuilder->mBeIRCodeGen->GetBeValue(globalVar->mStreamId);
- }
- }
- if (auto constant = BeValueDynCast<BeConstant>(beValue))
- *addrPtr = GetConstantData(constant);
- // We need to 'get' again because we might have resized
- return *addrPtr;
- }
- addr_ce CeContext::GetReflectType(const String& typeName, bool useDeclaration)
- {
- if (mCeMachine->mTempParser == NULL)
- {
- mCeMachine->mTempPassInstance = new BfPassInstance(mCeMachine->mCompiler->mSystem);
- mCeMachine->mTempParser = new BfParser(mCeMachine->mCompiler->mSystem);
- mCeMachine->mTempParser->mIsEmitted = true;
- mCeMachine->mTempParser->SetSource(NULL, 4096);
- mCeMachine->mTempReducer = new BfReducer();
- mCeMachine->mTempReducer->mPassInstance = mCeMachine->mTempPassInstance;
- mCeMachine->mTempReducer->mSource = mCeMachine->mTempParser;
- mCeMachine->mTempReducer->mAlloc = mCeMachine->mTempParser->mAlloc;
- mCeMachine->mTempReducer->mSystem = mCeMachine->mCompiler->mSystem;
- }
- int copyLen = BF_MIN(typeName.mLength, 4096);
- memcpy((char*)mCeMachine->mTempParser->mSrc, typeName.c_str(), copyLen);
- ((char*)mCeMachine->mTempParser->mSrc)[copyLen] = 0;
- mCeMachine->mTempParser->mSrcLength = typeName.mLength;
- mCeMachine->mTempParser->mSrcIdx = 0;
- mCeMachine->mTempParser->Parse(mCeMachine->mTempPassInstance);
- BfType* type = NULL;
- if (mCeMachine->mTempParser->mRootNode->mChildArr.mSize > 0)
- {
- mCeMachine->mTempReducer->mVisitorPos = BfReducer::BfVisitorPos(mCeMachine->mTempParser->mRootNode);
- mCeMachine->mTempReducer->mVisitorPos.mReadPos = 0;
- auto typeRef = mCeMachine->mTempReducer->CreateTypeRef(mCeMachine->mTempParser->mRootNode->mChildArr[0]);
- if ((mCeMachine->mTempPassInstance->mErrors.mSize == 0) && (mCeMachine->mTempReducer->mVisitorPos.mReadPos == mCeMachine->mTempParser->mRootNode->mChildArr.mSize - 1))
- {
- SetAndRestoreValue<bool> prevIgnoreErrors(mCeMachine->mCeModule->mIgnoreErrors, true);
- type = mCeMachine->mCeModule->ResolveTypeRefAllowUnboundGenerics(typeRef, BfPopulateType_Identity);
- }
- }
- mCeMachine->mTempPassInstance->ClearErrors();
- if (type == NULL)
- return 0;
- return useDeclaration ? GetReflectTypeDecl(type->mTypeId) : GetReflectType(type->mTypeId);
- }
- int CeContext::GetTypeIdFromType(addr_ce typeAddr)
- {
- if (!CheckMemory(typeAddr, 8))
- return 0;
- if (mReflectTypeIdOffset == -1)
- {
- auto typeTypeInst = mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mTypeTypeDef)->ToTypeInstance();
- auto typeIdField = typeTypeInst->mTypeDef->GetFieldByName("mTypeId");
- mReflectTypeIdOffset = typeTypeInst->mFieldInstances[typeIdField->mIdx].mDataOffset;
- }
- return *(int32*)(mMemory.mVals + typeAddr + mReflectTypeIdOffset);
- }
- addr_ce CeContext::GetReflectSpecializedType(addr_ce unspecializedTypeAddr, addr_ce typeArgsSpanAddr)
- {
- BfType* unspecializedType = GetBfType(GetTypeIdFromType(unspecializedTypeAddr));
- if (unspecializedType == NULL)
- return 0;
- BfTypeInstance* unspecializedTypeInst = unspecializedType->ToGenericTypeInstance();
- if (unspecializedType == NULL)
- return 0;
- int ptrSize = mCeMachine->mCompiler->mSystem->mPtrSize;
- if (!CheckMemory(typeArgsSpanAddr, ptrSize * 2))
- return 0;
- addr_ce spanPtr = *(addr_ce*)(mMemory.mVals + typeArgsSpanAddr);
- int32 spanSize = *(int32*)(mMemory.mVals + typeArgsSpanAddr + ptrSize);
- if (spanSize < 0)
- return 0;
- if (!CheckMemory(spanPtr, spanSize * ptrSize))
- return 0;
- Array<BfType*> typeGenericArgs;
- for (int argIdx = 0; argIdx < spanSize; argIdx++)
- {
- addr_ce argPtr = *(addr_ce*)(mMemory.mVals + spanPtr + argIdx * ptrSize);
- BfType* typeGenericArg = GetBfType(GetTypeIdFromType(argPtr));
- if (typeGenericArg == NULL)
- return 0;
- typeGenericArgs.Add(typeGenericArg);
- }
- SetAndRestoreValue<bool> prevIgnoreErrors(mCeMachine->mCeModule->mIgnoreErrors, true);
- auto specializedType = mCeMachine->mCeModule->ResolveTypeDef(unspecializedTypeInst->mTypeDef, typeGenericArgs, BfPopulateType_Identity);
- if (specializedType == NULL)
- return 0;
- return GetReflectType(specializedType->mTypeId);
- }
- addr_ce CeContext::GetString(int stringId)
- {
- addr_ce* ceAddrPtr = NULL;
- if (!mStringMap.TryAdd(stringId, NULL, &ceAddrPtr))
- return *ceAddrPtr;
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- String str;
- BfStringPoolEntry* entry = NULL;
- if (mCeMachine->mCeModule->mContext->mStringObjectIdMap.TryGetValue(stringId, &entry))
- {
- entry->mLastUsedRevision = mCeMachine->mCompiler->mRevision;
- str = entry->mString;
- }
- int allocSize = stringTypeInst->mInstSize + (int)str.length() + 1;
- int charsOffset = stringTypeInst->mInstSize;
- uint8* mem = CeMallocZero(allocSize);
-
- auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize;
- auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset;
- auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset;
- auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset;
- // Write TypeId into there
- *(int32*)(mem) = stringTypeInst->mTypeId;
- *(int32*)(mem + lenOffset) = (int)str.length();
- if (lenByteCount == 4)
- *(int32*)(mem + allocSizeOffset) = 0x40000000 + (int)str.length() + 1;
- else
- *(int64*)(mem + allocSizeOffset) = 0x4000000000000000LL + (int)str.length() + 1;
- *(int32*)(mem + ptrOffset) = (mem + charsOffset) - mMemory.mVals;
- memcpy(mem + charsOffset, str.c_str(), str.length());
- *ceAddrPtr = mem - mMemory.mVals;
- return *ceAddrPtr;
- }
- addr_ce CeContext::GetString(const StringImpl& str)
- {
- int stringId = mCeMachine->mCeModule->mContext->GetStringLiteralId(str);
- return GetString(stringId);
- }
- BfType* CeContext::GetBfType(int typeId)
- {
- if ((uintptr)typeId < (uintptr)mCeMachine->mCeModule->mContext->mTypes.size())
- return mCeMachine->mCeModule->mContext->mTypes[typeId];
- return NULL;
- }
- BfType* CeContext::GetBfType(BfIRType irType)
- {
- if (irType.mKind == BfIRTypeData::TypeKind_TypeId)
- return GetBfType(irType.mId);
- return NULL;
- }
- void CeContext::PrepareConstStructEntry(CeConstStructData& constEntry)
- {
- if (constEntry.mHash.IsZero())
- {
- constEntry.mHash = Hash128(constEntry.mData.mVals, constEntry.mData.mSize);
- if (!constEntry.mFixups.IsEmpty())
- constEntry.mHash = Hash128(&constEntry.mFixups[0], constEntry.mFixups.mSize * sizeof(CeConstStructFixup), constEntry.mHash);
- }
- if (!constEntry.mFixups.IsEmpty())
- {
- if (constEntry.mFixedData.IsEmpty())
- constEntry.mFixedData = constEntry.mData;
- for (auto& fixup : constEntry.mFixups)
- {
- if (fixup.mKind == CeConstStructFixup::Kind_StringPtr)
- {
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- addr_ce addrPtr = GetString(fixup.mValue);
- *(addr_ce*)(constEntry.mFixedData.mVals + fixup.mOffset) = addrPtr;
- }
- else if (fixup.mKind == CeConstStructFixup::Kind_StringCharPtr)
- {
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- addr_ce addrPtr = GetString(fixup.mValue);
- *(addr_ce*)(constEntry.mFixedData.mVals + fixup.mOffset) = addrPtr + stringTypeInst->mInstSize;
- }
- }
- }
- constEntry.mBindExecuteId = mExecuteId;
- }
- bool CeContext::CheckMemory(addr_ce addr, int32 size)
- {
- if ((addr < 0x10000) || (addr + size > mMemory.mSize))
- return false;
- return true;
- }
- uint8* CeContext::GetMemoryPtr(addr_ce addr, int32 size)
- {
- if (CheckMemory(addr, size))
- return mMemory.mVals + addr;
- return NULL;
- }
- bool CeContext::GetStringFromAddr(addr_ce strInstAddr, StringImpl& str)
- {
- if (!CheckMemory(strInstAddr, 0))
- return false;
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize;
- auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset;
- auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset;
- auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset;
- uint8* strInst = (uint8*)(strInstAddr + mMemory.mVals);
- int32 lenVal = *(int32*)(strInst + lenOffset);
- char* charPtr = NULL;
- if (lenByteCount == 4)
- {
- int32 allocSizeVal = *(int32*)(strInst + allocSizeOffset);
- if ((allocSizeVal & 0x40000000) != 0)
- {
- int32 ptrVal = *(int32*)(strInst + ptrOffset);
- charPtr = (char*)(ptrVal + mMemory.mVals);
- }
- else
- {
- charPtr = (char*)(strInst + ptrOffset);
- }
- }
- else
- {
- int64 allocSizeVal = *(int64*)(strInst + allocSizeOffset);
- if ((allocSizeVal & 0x4000000000000000LL) != 0)
- {
- int32 ptrVal = *(int32*)(strInst + ptrOffset);
- charPtr = (char*)(ptrVal + mMemory.mVals);
- }
- else
- {
- charPtr = (char*)(strInst + ptrOffset);
- }
- }
- int32 ptrVal = *(int32*)(strInst + ptrOffset);
- if (charPtr != NULL)
- str.Insert(str.length(), charPtr, lenVal);
- return true;
- }
- bool CeContext::GetStringFromStringView(addr_ce addr, StringImpl& str)
- {
- int ptrSize = mCeMachine->mCeModule->mSystem->mPtrSize;
- if (!CheckMemory(addr, ptrSize * 2))
- return false;
- addr_ce charsPtr = *(addr_ce*)(mMemory.mVals + addr);
- int32 len = *(int32*)(mMemory.mVals + addr + ptrSize);
- if (len > 0)
- {
- if (!CheckMemory(charsPtr, len))
- return false;
- str.Append((const char*)(mMemory.mVals + charsPtr), len);
- }
- return true;
- }
- bool CeContext::GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeIdx, addr_ce resultAddr)
- {
- if (customAttributes == NULL)
- return false;
- auto customAttr = customAttributes->Get(attributeIdx);
- if (customAttr == NULL)
- return false;
- auto ceContext = mCeMachine->AllocContext();
- BfIRValue foreignValue = ceContext->CreateAttribute(mCurCallSource->mRefNode, module, constHolder, customAttr);
- auto foreignConstant = module->mBfIRBuilder->GetConstant(foreignValue);
- if (foreignConstant->mConstType == BfConstType_AggCE)
- {
- auto constAggData = (BfConstantAggCE*)foreignConstant;
- auto value = ceContext->CreateConstant(module, ceContext->mMemory.mVals + constAggData->mCEAddr, customAttr->mType);
- if (!value)
- Fail("Failed to encoded attribute");
- auto attrConstant = module->mBfIRBuilder->GetConstant(value);
- if ((attrConstant == NULL) || (!WriteConstant(module, resultAddr, attrConstant, customAttr->mType)))
- Fail("Failed to decode attribute");
- }
- mCeMachine->ReleaseContext(ceContext);
- return true;
- }
- BfType* CeContext::GetCustomAttributeType(BfCustomAttributes* customAttributes, int attributeIdx)
- {
- if (customAttributes == NULL)
- return NULL;
- auto customAttr = customAttributes->Get(attributeIdx);
- if (customAttr == NULL)
- return NULL;
- return customAttr->mType;
- }
- //#define CE_GETC(T) *((T*)(addr += sizeof(T)) - 1)
- #define CE_GETC(T) *(T*)(mMemory.mVals + addr)
- bool CeContext::WriteConstant(BfModule* module, addr_ce addr, BfConstant* constant, BfType* type, bool isParams)
- {
- int ptrSize = mCeMachine->mCeModule->mSystem->mPtrSize;
- switch (constant->mTypeCode)
- {
- case BfTypeCode_None:
- return true;
- case BfTypeCode_Int8:
- case BfTypeCode_UInt8:
- case BfTypeCode_Boolean:
- case BfTypeCode_Char8:
- CE_GETC(int8) = constant->mInt8;
- return true;
- case BfTypeCode_Int16:
- case BfTypeCode_UInt16:
- case BfTypeCode_Char16:
- CE_GETC(int16) = constant->mInt16;
- return true;
- case BfTypeCode_Int32:
- case BfTypeCode_UInt32:
- case BfTypeCode_Char32:
- CE_GETC(int32) = constant->mInt32;
- return true;
- case BfTypeCode_Int64:
- case BfTypeCode_UInt64:
- CE_GETC(int64) = constant->mInt64;
- return true;
- case BfTypeCode_NullPtr:
- if (ptrSize == 4)
- CE_GETC(int32) = 0;
- else
- CE_GETC(int64) = 0;
- return true;
- case BfTypeCode_Float:
- CE_GETC(float) = (float)constant->mDouble;
- return true;
- case BfTypeCode_Double:
- CE_GETC(double) = constant->mDouble;
- return true;
- }
- if (constant->mConstType == BfConstType_Agg)
- {
- if (type->IsPointer())
- {
- auto elementType = type->GetUnderlyingType();
- auto toPtr = CeMallocZero(elementType->mSize);
- addr_ce toAddr = (addr_ce)(toPtr - mMemory.mVals);
- if (ptrSize == 4)
- CE_GETC(int32) = (int32)toAddr;
- else
- CE_GETC(int64) = (int64)toAddr;
- return WriteConstant(module, toAddr, constant, elementType, isParams);
- }
- auto aggConstant = (BfConstantAgg*)constant;
- if (type->IsSizedArray())
- {
- auto sizedArrayType = (BfSizedArrayType*)type;
- for (int i = 0; i < sizedArrayType->mElementCount; i++)
- {
- auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[i]);
- if (fieldConstant == NULL)
- return false;
- if (!WriteConstant(module, addr + i * sizedArrayType->mElementType->mSize, fieldConstant, sizedArrayType->mElementType))
- return false;
- }
- return true;
- }
- else if (type->IsArray())
- {
- auto elemType = type->GetUnderlyingType();
- addr_ce elemsAddr = 0;
- addr_ce arrayAddr = CeAllocArray((BfArrayType*)type, aggConstant->mValues.size(), elemsAddr);
- for (int i = 0; i < (int)aggConstant->mValues.size(); i++)
- {
- auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[i]);
- if (fieldConstant == NULL)
- return false;
- if (!WriteConstant(module, elemsAddr + i * elemType->GetStride(), fieldConstant, elemType))
- return false;
- }
- if (ptrSize == 4)
- CE_GETC(int32) = arrayAddr;
- else
- CE_GETC(int64) = arrayAddr;
- return true;
- }
- else if ((type->IsInstanceOf(module->mCompiler->mSpanTypeDef)) && (isParams))
- {
- auto elemType = type->GetUnderlyingType();
- addr_ce elemsAddr = CeMalloc(elemType->GetStride() * aggConstant->mValues.size()) - mMemory.mVals;
- for (int i = 0; i < (int)aggConstant->mValues.size(); i++)
- {
- auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[i]);
- if (fieldConstant == NULL)
- return false;
- if (!WriteConstant(module, elemsAddr + i * elemType->GetStride(), fieldConstant, elemType))
- return false;
- }
- if (ptrSize == 4)
- {
- CE_GETC(int32) = elemsAddr;
- addr += 4;
- CE_GETC(int32) = (int32)aggConstant->mValues.size();
- }
- else
- {
- CE_GETC(int32) = elemsAddr;
- addr += 8;
- CE_GETC(int64) = (int32)aggConstant->mValues.size();
- }
- }
- else
- {
- BF_ASSERT(type->IsStruct());
- module->PopulateType(type);
- auto typeInst = type->ToTypeInstance();
- int idx = 0;
- auto baseType = typeInst->GetBaseType(true);
- if (baseType != NULL)
- {
- auto baseConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[0]);
- if (!WriteConstant(module, addr, baseConstant, baseType))
- return false;
- }
- BfType* innerType = NULL;
- BfType* payloadType = NULL;
- if (typeInst->IsUnion())
- innerType = typeInst->GetUnionInnerType();
- if (typeInst->IsPayloadEnum())
- {
- auto& dscrFieldInstance = typeInst->mFieldInstances.back();
- auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[dscrFieldInstance.mDataIdx]);
- if (fieldConstant == NULL)
- return false;
- if (!WriteConstant(module, addr + dscrFieldInstance.mDataOffset, fieldConstant, dscrFieldInstance.mResolvedType))
- return false;
- for (auto& fieldInstance : typeInst->mFieldInstances)
- {
- auto fieldDef = fieldInstance.GetFieldDef();
- if (!fieldInstance.mIsEnumPayloadCase)
- continue;
- int tagIdx = -fieldInstance.mDataIdx - 1;
- if (fieldConstant->mInt32 == tagIdx)
- payloadType = fieldInstance.mResolvedType;
- }
- }
- if (typeInst->IsUnion())
- {
- if (!innerType->IsValuelessType())
- {
- BfIRValue dataVal = aggConstant->mValues[1];
- if ((payloadType != NULL) && (innerType != NULL))
- {
- Array<uint8> memArr;
- memArr.Resize(innerType->mSize);
- if (!module->mBfIRBuilder->WriteConstant(dataVal, memArr.mVals, innerType))
- return false;
- dataVal = module->mBfIRBuilder->ReadConstant(memArr.mVals, payloadType);
- if (!dataVal)
- return false;
- innerType = payloadType;
- }
- auto fieldConstant = module->mBfIRBuilder->GetConstant(dataVal);
- if (fieldConstant == NULL)
- return false;
- if (!WriteConstant(module, addr, fieldConstant, innerType))
- return false;
- }
- }
- if (!typeInst->IsUnion())
- {
- for (auto& fieldInstance : typeInst->mFieldInstances)
- {
- if (fieldInstance.mDataOffset < 0)
- continue;
- auto fieldConstant = module->mBfIRBuilder->GetConstant(aggConstant->mValues[fieldInstance.mDataIdx]);
- if (fieldConstant == NULL)
- return false;
- if (!WriteConstant(module, addr + fieldInstance.mDataOffset, fieldConstant, fieldInstance.mResolvedType))
- return false;
- }
- }
- }
- return true;
- }
- if (constant->mConstType == BfConstType_AggZero)
- {
- BF_ASSERT(type->IsComposite());
- memset(mMemory.mVals + addr, 0, type->mSize);
- return true;
- }
- if (constant->mConstType == BfConstType_ArrayZero8)
- {
- memset(mMemory.mVals + addr, 0, constant->mInt32);
- return true;
- }
- if (constant->mConstType == BfConstType_Undef)
- {
- memset(mMemory.mVals + addr, 0, type->mSize);
- return true;
- }
- if (constant->mConstType == BfConstType_AggCE)
- {
- auto constAggData = (BfConstantAggCE*)constant;
- if (type->IsPointer())
- {
- if (ptrSize == 4)
- CE_GETC(int32) = constAggData->mCEAddr;
- else
- CE_GETC(int64) = constAggData->mCEAddr;
- }
- else
- {
- BF_ASSERT(type->IsComposite());
- memcpy(mMemory.mVals + addr, mMemory.mVals + constAggData->mCEAddr, type->mSize);
- }
- return true;
- }
- if (constant->mConstType == BfConstType_BitCast)
- {
- auto constBitCast = (BfConstantBitCast*)constant;
- auto constTarget = module->mBfIRBuilder->GetConstantById(constBitCast->mTarget);
- return WriteConstant(module, addr, constTarget, type);
- }
- if (constant->mConstType == BfConstType_Box)
- {
- auto constBox = (BfConstantBox*)constant;
- auto boxedType = GetBfType(constBox->mToType);
- if (boxedType == NULL)
- return false;
- auto boxedTypeInst = boxedType->ToTypeInstance();
- if (boxedTypeInst == NULL)
- return false;
- module->PopulateType(boxedTypeInst);
- if (boxedTypeInst->mFieldInstances.IsEmpty())
- return false;
- auto& fieldInstance = boxedTypeInst->mFieldInstances.back();
- auto boxedMem = CeMalloc(boxedTypeInst->mInstSize);
- memset(boxedMem, 0, ptrSize*2);
- *(int32*)boxedMem = boxedTypeInst->mTypeId;
- auto constTarget = module->mBfIRBuilder->GetConstantById(constBox->mTarget);
- WriteConstant(module, boxedMem - mMemory.mVals + fieldInstance.mDataOffset, constTarget, fieldInstance.mResolvedType);
- if (ptrSize == 4)
- CE_GETC(int32) = (int32)(boxedMem - mMemory.mVals);
- else
- CE_GETC(int64) = (int64)(boxedMem - mMemory.mVals);
- return true;
- }
- if (constant->mConstType == BfConstType_PtrToInt)
- {
- auto ptrToIntConst = (BfConstantPtrToInt*)constant;
- auto constTarget = module->mBfIRBuilder->GetConstantById(ptrToIntConst->mTarget);
- return WriteConstant(module, addr, constTarget, type);
- }
- if (constant->mConstType == BfConstType_IntToPtr)
- {
- auto ptrToIntConst = (BfConstantIntToPtr*)constant;
- auto intType = mCeMachine->mCeModule->GetPrimitiveType(BfTypeCode_IntPtr);
- auto constTarget = module->mBfIRBuilder->GetConstantById(ptrToIntConst->mTarget);
- return WriteConstant(module, addr, constTarget, intType);
- }
- if (constant->mConstType == BfConstType_BitCastNull)
- {
- BF_ASSERT(type->IsPointer() || type->IsObjectOrInterface());
- memset(mMemory.mVals + addr, 0, type->mSize);
- return true;
- }
- if (constant->mConstType == BfConstType_GEP32_2)
- {
- auto gepConst = (BfConstantGEP32_2*)constant;
- auto constTarget = module->mBfIRBuilder->GetConstantById(gepConst->mTarget);
- if (constTarget->mConstType == BfConstType_GlobalVar)
- {
- auto globalVar = (BfGlobalVar*)constTarget;
- if (strncmp(globalVar->mName, "__bfStrData", 10) == 0)
- {
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- int stringId = atoi(globalVar->mName + 11);
- addr_ce strAddr = GetString(stringId) + stringTypeInst->mInstSize;
- if (ptrSize == 4)
- CE_GETC(int32) = strAddr;
- else
- CE_GETC(int64) = strAddr;
- return true;
- }
- }
- }
- if (constant->mConstType == BfConstType_GlobalVar)
- {
- auto globalVar = (BfGlobalVar*)constant;
- if (strncmp(globalVar->mName, "__bfStrObj", 10) == 0)
- {
- int stringId = atoi(globalVar->mName + 10);
- addr_ce strAddr = GetString(stringId);
- if (ptrSize == 4)
- CE_GETC(int32) = strAddr;
- else
- CE_GETC(int64) = strAddr;
- return true;
- }
- }
- if (constant->mTypeCode == BfTypeCode_StringId)
- {
- addr_ce strAddr = GetString(constant->mInt32);
- if (type->IsPointer())
- {
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)mCeMachine->mCeModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- strAddr += stringTypeInst->mInstSize;
- }
- if (ptrSize == 4)
- CE_GETC(int32) = strAddr;
- else
- CE_GETC(int64) = strAddr;
- return true;
- }
- if ((constant->mConstType == BfConstType_TypeOf) || (constant->mConstType == BfConstType_TypeOf_WithData))
- {
- auto constTypeOf = (BfTypeOf_Const*)constant;
- addr_ce typeAddr = GetReflectType(constTypeOf->mType->mTypeId);
- if (ptrSize == 4)
- CE_GETC(int32) = typeAddr;
- else
- CE_GETC(int64) = typeAddr;
- return true;
- }
- if (constant->mConstType == BfConstType_ExtractValue)
- {
- Array<BfConstantExtractValue*> extractStack;
- auto checkConstant = constant;
- while (true)
- {
- if (checkConstant == NULL)
- break;
- if (checkConstant->mConstType == BfConstType_ExtractValue)
- {
- auto gepConst = (BfConstantExtractValue*)constant;
- BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
- checkConstant = module->mBfIRBuilder->GetConstant(targetConst);
- extractStack.Add(gepConst);
- continue;
- }
- if (checkConstant->mConstType == BfConstType_AggCE)
- return WriteConstant(module, addr, checkConstant, type, isParams);
- // Unhandled
- break;
- }
- }
- return false;
- }
- #define CE_CREATECONST_CHECKPTR(PTR, SIZE) \
- if ((((uint8*)(PTR) - memStart) - 0x10000) + (SIZE) > (memSize - 0x10000)) \
- { \
- Fail("Access violation creating constant result"); \
- return BfIRValue(); \
- }
- BfIRValue CeContext::CreateConstant(BfModule* module, uint8* ptr, BfType* bfType, BfType** outType)
- {
- auto ceModule = mCeMachine->mCeModule;
- BfIRBuilder* irBuilder = module->mBfIRBuilder;
- int32 ptrSize = module->mSystem->mPtrSize;
- uint8* memStart = mMemory.mVals;
- int memSize = mMemory.mSize;
- if (bfType->IsPrimitiveType())
- {
- auto primType = (BfPrimitiveType*)bfType;
- auto typeCode = primType->mTypeDef->mTypeCode;
- if (typeCode == BfTypeCode_IntPtr)
- typeCode = (ceModule->mCompiler->mSystem->mPtrSize == 4) ? BfTypeCode_Int32 : BfTypeCode_Int64;
- else if (typeCode == BfTypeCode_UIntPtr)
- typeCode = (ceModule->mCompiler->mSystem->mPtrSize == 4) ? BfTypeCode_UInt32 : BfTypeCode_UInt64;
- switch (typeCode)
- {
- case BfTypeCode_None:
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, 0);
- case BfTypeCode_Int8:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(int8));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(int8*)ptr);
- case BfTypeCode_UInt8:
- case BfTypeCode_Boolean:
- case BfTypeCode_Char8:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(uint8));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(uint8*)ptr);
- case BfTypeCode_Int16:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(int16));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(int16*)ptr);
- case BfTypeCode_UInt16:
- case BfTypeCode_Char16:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(uint16));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(uint16*)ptr);
- case BfTypeCode_Int32:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(int32));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(int32*)ptr);
- case BfTypeCode_UInt32:
- case BfTypeCode_Char32:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(uint32));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, (uint64) * (uint32*)ptr);
- case BfTypeCode_Int64:
- case BfTypeCode_UInt64:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(int64));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(uint64*)ptr);
- case BfTypeCode_Float:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(float));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(float*)ptr);
- case BfTypeCode_Double:
- CE_CREATECONST_CHECKPTR(ptr, sizeof(double));
- return irBuilder->CreateConst(primType->mTypeDef->mTypeCode, *(double*)ptr);
- }
- return BfIRValue();
- }
- if (bfType->IsTypedPrimitive())
- return CreateConstant(module, ptr, bfType->GetUnderlyingType(), outType);
- if (bfType->IsGenericParam())
- return irBuilder->GetUndefConstValue(irBuilder->MapType(bfType));
- if (bfType->IsTypeInstance())
- {
- auto typeInst = bfType->ToTypeInstance();
- uint8* instData = ptr;
- // if ((typeInst->IsObject()) && (!isBaseType))
- // {
- // CE_CREATECONST_CHECKPTR(ptr, sizeof(addr_ce));
- // instData = mMemory.mVals + *(addr_ce*)ptr;
- // CE_CREATECONST_CHECKPTR(instData, typeInst->mInstSize);
- // }
- if (typeInst->IsObjectOrInterface())
- {
- addr_ce addr = *(addr_ce*)(ptr);
- if (addr == 0)
- {
- return irBuilder->CreateConstNull(irBuilder->MapType(typeInst));
- }
- instData = memStart + addr;
- if (typeInst->IsInstanceOf(mCeMachine->mCompiler->mStringTypeDef))
- {
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)ceModule->ResolveTypeDef(mCeMachine->mCompiler->mStringTypeDef, BfPopulateType_Data);
- module->PopulateType(stringTypeInst);
- auto lenByteCount = stringTypeInst->mFieldInstances[0].mResolvedType->mSize;
- auto lenOffset = stringTypeInst->mFieldInstances[0].mDataOffset;
- auto allocSizeOffset = stringTypeInst->mFieldInstances[1].mDataOffset;
- auto ptrOffset = stringTypeInst->mFieldInstances[2].mDataOffset;
- if (addr + ptrOffset + 4 > memSize)
- {
- // Memory error
- return irBuilder->CreateConstNull(irBuilder->MapType(typeInst));
- }
- int32 lenVal = *(int32*)(instData + lenOffset);
- char* charPtr = NULL;
- if (lenByteCount == 4)
- {
- int32 allocSizeVal = *(int32*)(instData + allocSizeOffset);
- if ((allocSizeVal & 0x40000000) != 0)
- {
- int32 ptrVal = *(int32*)(instData + ptrOffset);
- charPtr = (char*)(ptrVal + memStart);
- }
- else
- {
- charPtr = (char*)(instData + ptrOffset);
- }
- }
- else
- {
- int64 allocSizeVal = *(int64*)(instData + allocSizeOffset);
- if ((allocSizeVal & 0x4000000000000000LL) != 0)
- {
- int32 ptrVal = *(int32*)(instData + ptrOffset);
- charPtr = (char*)(ptrVal + memStart);
- }
- else
- {
- charPtr = (char*)(instData + ptrOffset);
- }
- }
- CE_CREATECONST_CHECKPTR(charPtr, lenVal);
- String str(charPtr, lenVal);
- return module->GetStringObjectValue(str);
- }
- }
- if (typeInst->IsInstanceOf(mCeMachine->mCompiler->mStringViewTypeDef))
- {
- char* charPtr = (char*)memStart + *(addr_ce*)(ptr);
- int32 lenVal = *(int32*)(ptr + ptrSize);
- CE_CREATECONST_CHECKPTR(charPtr, lenVal);
- String str(charPtr, lenVal);
- auto stringViewType = ceModule->ResolveTypeDef(mCeMachine->mCompiler->mStringViewTypeDef, BfPopulateType_Data)->ToTypeInstance();
- auto spanType = stringViewType->mBaseType;
- auto valueTypeType = spanType->mBaseType;
- SizedArray<BfIRValue, 1> valueTypeValues;
- BfIRValue valueTypeVal = irBuilder->CreateConstAgg(irBuilder->MapType(valueTypeType, BfIRPopulateType_Full), valueTypeValues);
- SizedArray<BfIRValue, 3> spanValues;
- spanValues.Add(valueTypeVal);
- spanValues.Add(module->GetStringCharPtr(str));
- spanValues.Add(irBuilder->CreateConst(BfTypeCode_IntPtr, lenVal));
- BfIRValue spanVal = irBuilder->CreateConstAgg(irBuilder->MapType(spanType, BfIRPopulateType_Full), spanValues);
- SizedArray<BfIRValue, 1> stringViewValues;
- stringViewValues.Add(spanVal);
- return irBuilder->CreateConstAgg(irBuilder->MapType(stringViewType, BfIRPopulateType_Full), stringViewValues);
- }
- SizedArray<BfIRValue, 8> fieldVals;
- if (typeInst->IsInstanceOf(ceModule->mCompiler->mSpanTypeDef))
- {
- if ((outType != NULL) && ((mCurExpectingType == NULL) || (mCurExpectingType->IsSizedArray())))
- {
- module->PopulateType(typeInst);
- auto ptrOffset = typeInst->mFieldInstances[0].mDataOffset;
- auto lenOffset = typeInst->mFieldInstances[1].mDataOffset;
- BfType* elemType = typeInst->GetUnderlyingType();
- CE_CREATECONST_CHECKPTR(instData, ceModule->mSystem->mPtrSize * 2);
- addr_ce addr = *(addr_ce*)(instData + ptrOffset);
- int32 lenVal = *(int32*)(instData + lenOffset);
- CE_CREATECONST_CHECKPTR(memStart + addr, lenVal);
- for (int i = 0; i < lenVal; i++)
- {
- auto result = CreateConstant(module, memStart + addr + i * elemType->GetStride(), elemType);
- if (!result)
- return BfIRValue();
- fieldVals.Add(result);
- }
- auto irArrayType = irBuilder->GetSizedArrayType(irBuilder->MapType(elemType, BfIRPopulateType_Full), lenVal);
- auto instResult = irBuilder->CreateConstAgg(irArrayType, fieldVals);
- *outType = module->CreateSizedArrayType(elemType, lenVal);
- return instResult;
- }
- if ((mCurEvalFlags & CeEvalFlags_IgnoreConstEncodeFailure) == 0)
- Fail(StrFormat("Span return type '%s' must be received by a sized array", module->TypeToString(typeInst).c_str()));
- return irBuilder->CreateConstAggZero(irBuilder->MapType(typeInst));
- }
- if (typeInst->IsInstanceOf(ceModule->mCompiler->mTypeTypeDef))
- {
- int typeId = GetTypeIdFromType(instData - mMemory.mVals);
- if (typeId <= 0)
- {
- Fail("Unable to locate return type type");
- return BfIRValue();
- }
- return module->CreateTypeDataRef(module->mContext->mTypes[typeId]);
- }
- if (typeInst == module->mContext->mBfObjectType)
- {
- // Allow boxing
- CE_CREATECONST_CHECKPTR(instData, ceModule->mSystem->mPtrSize);
- addr_ce typeId = *(int*)(instData);
- BfType* type = GetBfType(typeId);
- if (type == NULL)
- {
- Fail("Unable to locate type");
- return BfIRValue();
- }
- if (type->IsInstanceOf(mCeMachine->mCompiler->mStringTypeDef))
- {
- return CreateConstant(module, ptr, type, outType);
- }
- else if (type->IsBoxed())
- {
- auto underlyingType = type->GetUnderlyingType();
- module->PopulateType(type);
- auto boxedType = (BfBoxedType*)type;
- int dataOffset = boxedType->mFieldInstances.back().mDataOffset;
- auto origValue = CreateConstant(module, ptr + dataOffset, underlyingType, outType);
- if (origValue)
- {
- if (outType != NULL)
- *outType = typeInst;
- return irBuilder->CreateConstBox(origValue, irBuilder->MapType(boxedType));
- }
- }
- // else if (type->IsValueType())
- // {
- // auto origValue = CreateConstant(module, ptr, type, outType);
- // if (origValue)
- // {
- // auto boxedType = module->CreateBoxedType(type);
- // irBuilder->PopulateType(boxedType);
- // return irBuilder->CreateConstBox(origValue, irBuilder->MapType(boxedType));
- // }
- // }
- }
- if (typeInst->IsObjectOrInterface())
- {
- if ((mCurEvalFlags & CeEvalFlags_IgnoreConstEncodeFailure) == 0)
- Fail(StrFormat("Reference type '%s' return value not allowed", module->TypeToString(typeInst).c_str()));
- return irBuilder->CreateConstNull(irBuilder->MapType(typeInst));
- }
- auto baseType = typeInst->GetBaseType(true);
- if (baseType != NULL)
- {
- auto result = CreateConstant(module, instData, baseType);
- if (!result)
- return BfIRValue();
- fieldVals.Add(result);
- }
- if (typeInst->IsUnion())
- {
- auto innerType = typeInst->GetUnionInnerType();
- if (!innerType->IsValuelessType())
- {
- auto result = CreateConstant(module, instData, innerType);
- if (!result)
- return BfIRValue();
- fieldVals.Add(result);
- }
- }
- if ((!typeInst->IsUnion()) || (typeInst->IsPayloadEnum()))
- {
- for (int fieldIdx = 0; fieldIdx < typeInst->mFieldInstances.size(); fieldIdx++)
- {
- auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
- if (fieldInstance.mDataOffset < 0)
- continue;
- if ((fieldInstance.mDataOffset == 0) && (typeInst == mCeMachine->mCompiler->mContext->mBfObjectType))
- {
- auto vdataPtr = module->GetClassVDataPtr(typeInst);
- if (fieldInstance.mResolvedType->IsInteger())
- fieldVals.Add(irBuilder->CreatePtrToInt(vdataPtr, ((BfPrimitiveType*)fieldInstance.mResolvedType)->mTypeDef->mTypeCode));
- else
- fieldVals.Add(vdataPtr);
- continue;
- }
- auto result = CreateConstant(module, instData + fieldInstance.mDataOffset, fieldInstance.mResolvedType);
- if (!result)
- return BfIRValue();
- if (fieldInstance.mDataIdx == fieldVals.mSize)
- {
- fieldVals.Add(result);
- }
- else
- {
- while (fieldInstance.mDataIdx >= fieldVals.mSize)
- fieldVals.Add(BfIRValue());
- fieldVals[fieldInstance.mDataIdx] = result;
- }
- }
- }
- for (auto& fieldVal : fieldVals)
- {
- if (!fieldVal)
- fieldVal = irBuilder->CreateConstArrayZero(0);
- }
- auto instResult = irBuilder->CreateConstAgg(irBuilder->MapTypeInst(typeInst, BfIRPopulateType_Identity), fieldVals);
- return instResult;
- }
- if (bfType->IsPointer())
- {
- addr_ce addr = *(addr_ce*)(ptr);
- if ((addr != 0) && ((mCurEvalFlags & CeEvalFlags_IgnoreConstEncodeFailure) == 0))
- Fail(StrFormat("Pointer type '%s' return value not allowed", module->TypeToString(bfType).c_str()));
- return irBuilder->CreateConstNull(irBuilder->MapType(bfType));
- }
- if ((bfType->IsSizedArray()) && (!bfType->IsUnknownSizedArrayType()))
- {
- SizedArray<BfIRValue, 8> values;
- auto sizedArrayType = (BfSizedArrayType*)bfType;
- for (int i = 0; i < sizedArrayType->mElementCount; i++)
- {
- auto elemValue = CreateConstant(module, ptr + i * sizedArrayType->mElementType->GetStride(), sizedArrayType->mElementType);
- if (!elemValue)
- return BfIRValue();
- values.Add(elemValue);
- }
- return irBuilder->CreateConstAgg(irBuilder->MapType(sizedArrayType, BfIRPopulateType_Full), values);
- }
- return BfIRValue();
- }
- BfIRValue CeContext::CreateAttribute(BfAstNode* targetSrc, BfModule* module, BfIRConstHolder* constHolder, BfCustomAttribute* customAttribute, addr_ce ceAttrAddr)
- {
- SetAndRestoreValue<bool> prevIgnoreWrites(module->mBfIRBuilder->mIgnoreWrites, true);
- module->mContext->mUnreifiedModule->PopulateType(customAttribute->mType);
- if (ceAttrAddr == 0)
- ceAttrAddr = CeMallocZero(customAttribute->mType->mSize) - mMemory.mVals;
- BfIRValue ceAttrVal = module->mBfIRBuilder->CreateConstAggCE(module->mBfIRBuilder->MapType(customAttribute->mType, BfIRPopulateType_Identity), ceAttrAddr);
- BfTypedValue ceAttrTypedValue(ceAttrVal, customAttribute->mType);
- auto ctorMethodInstance = module->GetRawMethodInstance(customAttribute->mType, customAttribute->mCtor);
- if (ctorMethodInstance == NULL)
- {
- module->Fail("Attribute ctor failed", targetSrc);
- return ceAttrVal;
- }
- SizedArray<BfIRValue, 8> ctorArgs;
- if (!customAttribute->mType->IsValuelessType())
- ctorArgs.Add(ceAttrVal);
- int paramIdx = 0;
- for (auto& arg : customAttribute->mCtorArgs)
- {
- auto constant = constHolder->GetConstant(arg);
- if (!constant)
- {
- module->AssertErrorState();
- return ceAttrVal;
- }
- auto paramType = ctorMethodInstance->GetParamType(paramIdx);
- ctorArgs.Add(module->ConstantToCurrent(constant, constHolder, paramType, true));
- paramIdx++;
- }
- BfTypedValue retValue = Call(CeCallSource(targetSrc), module, ctorMethodInstance, ctorArgs, CeEvalFlags_None, NULL);
- if (!retValue)
- return ceAttrVal;
- for (auto& setProperty : customAttribute->mSetProperties)
- {
- BfExprEvaluator exprEvaluator(module);
- BfMethodDef* setMethodDef = exprEvaluator.GetPropertyMethodDef(setProperty.mPropertyRef, BfMethodType_PropertySetter, BfCheckedKind_NotSet, ceAttrTypedValue);
- BfMethodInstance* setMethodInstance = NULL;
- if (setMethodDef != NULL)
- setMethodInstance = module->GetRawMethodInstance(customAttribute->mType, setMethodDef);
- if ((setMethodInstance == NULL) || (!setProperty.mParam))
- {
- module->Fail("Attribute prop failed", targetSrc);
- return ceAttrVal;
- }
- SizedArray<BfIRValue, 1> setArgs;
- if (!customAttribute->mType->IsValuelessType())
- setArgs.Add(ceAttrVal);
- if (!setProperty.mParam.mType->IsValuelessType())
- {
- auto constant = constHolder->GetConstant(setProperty.mParam.mValue);
- if (!constant)
- {
- module->AssertErrorState();
- return ceAttrVal;
- }
- setArgs.Add(module->ConstantToCurrent(constant, constHolder, setProperty.mParam.mType, true));
- }
- BfTypedValue retValue = Call(targetSrc, module, setMethodInstance, setArgs, CeEvalFlags_None, NULL);
- if (!retValue)
- return ceAttrVal;
- }
- for (auto& setField : customAttribute->mSetField)
- {
- BfFieldInstance* fieldInstance = setField.mFieldRef;
- if (fieldInstance->mDataOffset < 0)
- continue;
- auto constant = constHolder->GetConstant(setField.mParam.mValue);
- WriteConstant(module, ceAttrAddr + fieldInstance->mDataOffset, constant, fieldInstance->mResolvedType);
- }
- return ceAttrVal;
- }
- BfTypedValue CeContext::Call(CeCallSource callSource, BfModule* module, BfMethodInstance* methodInstance, const BfSizedArray<BfIRValue>& args, CeEvalFlags flags, BfType* expectingType)
- {
- // DISABLED
- //return BfTypedValue();
- //
- {
- StackHelper stackHelper;
- if (!stackHelper.CanStackExpand(256 * 1024))
- {
- BfTypedValue result;
- if (!stackHelper.Execute([&]()
- {
- result = Call(callSource, module, methodInstance, args, flags, expectingType);
- }))
- {
- module->Fail("Stack exhausted in CeContext::Call", callSource.mRefNode);
- }
- return result;
- }
- }
- //auto ceModule = mCeMachine->mCeModule;
- AutoTimer autoTimer(mCeMachine->mRevisionExecuteTime);
- SetAndRestoreValue<CeContext*> curPrevContext(mPrevContext, mCeMachine->mCurContext);
- SetAndRestoreValue<CeContext*> prevContext(mCeMachine->mCurContext, this);
- SetAndRestoreValue<CeEvalFlags> prevEvalFlags(mCurEvalFlags, flags);
- SetAndRestoreValue<CeCallSource*> prevCallSource(mCurCallSource, &callSource);
- SetAndRestoreValue<BfModule*> prevModule(mCurModule, module);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurMethodInstance, methodInstance);
- SetAndRestoreValue<BfMethodInstance*> prevCallerMethodInstance(mCallerMethodInstance, module->mCurMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevCallerTypeInstance(mCallerTypeInstance, module->mCurTypeInstance);
- SetAndRestoreValue<BfTypeDef*> prevCallerActiveTypeDef(mCallerActiveTypeDef, module->GetActiveTypeDef());
- SetAndRestoreValue<BfType*> prevExpectingType(mCurExpectingType, expectingType);
- SetAndRestoreValue<bool> prevCtxResolvingVar(module->mContext->mResolvingVarField, false);
- SetAndRestoreValue<BfMethodInstance*> moduleCurMethodInstance(module->mCurMethodInstance, methodInstance);
- SetAndRestoreValue<BfTypeInstance*> moduleCurTypeInstance(module->mCurTypeInstance, methodInstance->GetOwner());
- SetAndRestoreValue<int> prevCurExecuteId(mCurModule->mCompiler->mCurCEExecuteId, mCeMachine->mExecuteId);
- // Reentrancy may occur as methods need defining
- //SetAndRestoreValue<BfMethodState*> prevMethodStateInConstEval(module->mCurMethodState, NULL);
- if (mCeMachine->mAppendAllocInfo != NULL)
- {
- if (mCeMachine->mAppendAllocInfo->mAppendSizeValue)
- {
- bool isConst = mCeMachine->mAppendAllocInfo->mAppendSizeValue.IsConst();
- if (isConst)
- {
- auto constant = module->mBfIRBuilder->GetConstant(mCeMachine->mAppendAllocInfo->mAppendSizeValue);
- if (constant->mConstType == BfConstType_Undef)
- isConst = false;
- }
- if (!isConst)
- {
- Fail("Non-constant append alloc");
- return BfTypedValue();
- }
- }
- }
- if (mCeMachine->HasFailed())
- {
- Fail("ConstEval machine failed");
- return BfTypedValue();
- }
- int thisArgIdx = -1;
- int appendAllocIdx = -1;
- bool hasAggData = false;
- if (!methodInstance->mMethodDef->mIsStatic)
- {
- if (!methodInstance->GetOwner()->IsValuelessType())
- {
- thisArgIdx = 0;
- auto checkConstant = module->mBfIRBuilder->GetConstant(args[0]);
- while (checkConstant != NULL)
- {
- if ((checkConstant != NULL) && (checkConstant->mConstType == BfConstType_AggCE))
- {
- hasAggData = true;
- break;
- }
- if (checkConstant->mConstType == BfConstType_ExtractValue)
- {
- auto gepConst = (BfConstantExtractValue*)checkConstant;
- BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
- checkConstant = module->mBfIRBuilder->GetConstant(targetConst);
- continue;
- }
- break;
- }
- }
- if ((methodInstance->GetParamCount() >= 1) && (methodInstance->GetParamKind(0) == BfParamKind_AppendIdx))
- appendAllocIdx = 1;
- }
- int paramCompositeSize = 0;
- int paramIdx = methodInstance->GetParamCount();
- for (int argIdx = (int)args.size() - 1; argIdx >= 0; argIdx--)
- {
- BfType* paramType = NULL;
- while (true)
- {
- paramIdx--;
- paramType = methodInstance->GetParamType(paramIdx);
- if (paramType->IsTypedPrimitive())
- paramType = paramType->GetUnderlyingType();
- if ((!paramType->IsValuelessType()) && (!paramType->IsVar()))
- break;
- }
- BfType* compositeType = paramType->IsComposite() ? paramType : NULL;
- auto arg = args[argIdx];
- bool isConst = arg.IsConst();
- if (isConst)
- {
- auto constant = module->mBfIRBuilder->GetConstant(arg);
- if (constant->mConstType == BfConstType_Undef)
- {
- if (paramType->IsInstanceOf(module->mCompiler->mTypeTypeDef))
- {
- args[argIdx] = module->CreateTypeDataRef(module->GetPrimitiveType(BfTypeCode_None));
- }
- // else
- // isConst = false;
- }
- else if (((constant->mConstType == BfConstType_AggZero) || (constant->mConstType == BfConstType_Agg)) &&
- ((paramType->IsPointer()) || (paramType->IsRef())))
- compositeType = paramType->GetUnderlyingType();
- }
- if (compositeType != NULL)
- {
- if ((paramType->IsPointer()) || (paramType->IsRef()))
- paramCompositeSize += paramType->GetUnderlyingType()->mSize;
- else
- paramCompositeSize += paramType->mSize;
- }
- if (!isConst)
- {
- if ((argIdx == thisArgIdx) && (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor))
- {
- // Allow non-const 'this' for ctor
- }
- else if (argIdx != appendAllocIdx)
- {
- Fail(StrFormat("Non-constant argument for param '%s'", methodInstance->GetParamName(paramIdx).c_str()));
- return BfTypedValue();
- }
- }
- }
- auto methodDef = methodInstance->mMethodDef;
- if (mCeMachine->mCeModule == NULL)
- mCeMachine->Init();
- auto ceModule = mCeMachine->mCeModule;
- bool added = false;
- CeFunction* ceFunction = mCeMachine->GetFunction(methodInstance, BfIRValue(), added);
- if (ceFunction->mInitializeState == CeFunction::InitializeState_Initializing_ReEntry)
- {
- String error = "Comptime method preparation recursion";
- auto curContext = this;
- while (curContext != NULL)
- {
- if (curContext->mCurMethodInstance != NULL)
- error += StrFormat("\n %s", module->MethodToString(curContext->mCurMethodInstance).c_str());
- curContext = curContext->mPrevContext;
- if ((curContext != NULL) && (curContext->mCurMethodInstance == mCurMethodInstance))
- break;
- }
- Fail(error);
- return BfTypedValue();
- }
- if (ceFunction->mInitializeState < CeFunction::InitializeState_Initialized)
- mCeMachine->PrepareFunction(ceFunction, NULL);
- Array<CeFrame> prevCallStack;
- auto stackPtr = &mMemory[0] + mStackSize;
- auto* memStart = &mMemory[0];
- if (!mCallStack.IsEmpty())
- {
- BF_ASSERT((flags & CeEvalFlags_DbgCall) != 0);
- prevCallStack = mCallStack;
- stackPtr = &mMemory[0] + mCallStack.back().mStackAddr;
- mCallStack.Clear();
- }
- BfTypeInstance* thisType = methodInstance->GetOwner();
- addr_ce allocThisInstAddr = 0;
- addr_ce allocThisAddr = 0;
- addr_ce thisAddr = 0;
- int allocThisSize = -1;
- if ((thisArgIdx != -1) && (!hasAggData))
- {
- allocThisSize = thisType->mInstSize;
- if ((mCeMachine->mAppendAllocInfo != NULL) && (mCeMachine->mAppendAllocInfo->mAppendSizeValue))
- {
- BF_ASSERT(mCeMachine->mAppendAllocInfo->mModule == module);
- BF_ASSERT(mCeMachine->mAppendAllocInfo->mAppendSizeValue.IsConst());
- auto appendSizeConstant = module->mBfIRBuilder->GetConstant(mCeMachine->mAppendAllocInfo->mAppendSizeValue);
- BF_ASSERT(module->mBfIRBuilder->IsInt(appendSizeConstant->mTypeCode));
- allocThisSize += appendSizeConstant->mInt32;
- }
- if (allocThisSize >= mStackSize / 4)
- {
- // Resize stack a reasonable size
- mStackSize = BF_ALIGN(allocThisSize, 0x100000) + BF_CE_DEFAULT_STACK_SIZE;
- int64 memSize = mStackSize + BF_CE_DEFAULT_HEAP_SIZE;
- if (memSize > BF_CE_MAX_MEMORY)
- {
- Fail("Return value too large (>2GB)");
- return BfTypedValue();
- }
- if (memSize > mMemory.mSize)
- mMemory.Resize(memSize);
- stackPtr = &mMemory[0] + mStackSize;
- memStart = &mMemory[0];
- }
- stackPtr -= allocThisSize;
- auto allocThisPtr = stackPtr;
- memset(allocThisPtr, 0, allocThisSize);
- if (thisType->IsObject())
- *(int32*)(allocThisPtr) = thisType->mTypeId;
- allocThisInstAddr = allocThisPtr - memStart;
- allocThisAddr = allocThisInstAddr;
- thisAddr = allocThisAddr;
- }
- addr_ce allocAppendIdxAddr = 0;
- if (appendAllocIdx != -1)
- {
- stackPtr -= ceModule->mSystem->mPtrSize;
- memset(stackPtr, 0, ceModule->mSystem->mPtrSize);
- *(addr_ce*)(stackPtr) = (addr_ce)(allocThisInstAddr + thisType->mInstSize);
- allocAppendIdxAddr = stackPtr - memStart;
- }
- auto _FixVariables = [&]()
- {
- intptr memOffset = &mMemory[0] - memStart;
- if (memOffset == 0)
- return;
- memStart += memOffset;
- stackPtr += memOffset;
- };
- addr_ce compositeStartAddr = stackPtr - memStart;
- stackPtr -= paramCompositeSize;
- addr_ce useCompositeAddr = compositeStartAddr;
- paramIdx = methodInstance->GetParamCount();
- for (int argIdx = (int)args.size() - 1; argIdx >= 0; argIdx--)
- {
- BfType* paramType = NULL;
- while (true)
- {
- paramIdx--;
- paramType = methodInstance->GetParamType(paramIdx);
- if (paramType->IsTypedPrimitive())
- paramType = paramType->GetUnderlyingType();
- if ((!paramType->IsValuelessType()) && (!paramType->IsVar()))
- break;
- }
- bool isParams = methodInstance->GetParamKind(paramIdx) == BfParamKind_Params;
- auto arg = args[argIdx];
- if (!arg.IsConst())
- {
- if (argIdx == thisArgIdx)
- {
- if (mCeMachine->mAppendAllocInfo != NULL)
- BF_ASSERT(mCeMachine->mAppendAllocInfo->mAllocValue == arg);
- stackPtr -= ceModule->mSystem->mPtrSize;
- int64 addr64 = allocThisAddr;
- memcpy(stackPtr, &addr64, ceModule->mSystem->mPtrSize);
- continue;
- }
- else if (argIdx == appendAllocIdx)
- {
- stackPtr -= ceModule->mSystem->mPtrSize;
- int64 addr64 = allocAppendIdxAddr;
- memcpy(stackPtr, &addr64, ceModule->mSystem->mPtrSize);
- continue;
- }
- else
- return BfTypedValue();
- }
- auto constant = module->mBfIRBuilder->GetConstant(arg);
- BfType* compositeType = paramType->IsComposite() ? paramType : NULL;
- if (((constant->mConstType == BfConstType_AggZero) || (constant->mConstType == BfConstType_Agg)) &&
- ((paramType->IsPointer()) || (paramType->IsRef())))
- compositeType = paramType->GetUnderlyingType();
- if (compositeType != NULL)
- {
- useCompositeAddr -= compositeType->mSize;
- if (!WriteConstant(module, useCompositeAddr, constant, compositeType, isParams))
- {
- Fail(StrFormat("Failed to process argument for param '%s'", methodInstance->GetParamName(paramIdx).c_str()));
- return BfTypedValue();
- }
- _FixVariables();
- stackPtr -= ceModule->mSystem->mPtrSize;
- int64 addr64 = useCompositeAddr;
- if (argIdx == thisArgIdx)
- thisAddr = addr64;
- memcpy(stackPtr, &addr64, ceModule->mSystem->mPtrSize);
- }
- else
- {
- stackPtr -= paramType->mSize;
- auto useCompositeAddr = stackPtr - memStart;
- if (!WriteConstant(module, useCompositeAddr, constant, paramType, isParams))
- {
- Fail(StrFormat("Failed to process argument for param '%s'", methodInstance->GetParamName(paramIdx).c_str()));
- return BfTypedValue();
- }
- _FixVariables();
- if (argIdx == thisArgIdx)
- {
- auto checkConstant = constant;
- while (checkConstant != NULL)
- {
- if ((checkConstant != NULL) && (checkConstant->mConstType == BfConstType_AggCE))
- {
- auto constAggData = (BfConstantAggCE*)checkConstant;
- if (paramType->IsPointer())
- thisAddr = constAggData->mCEAddr;
- else
- thisAddr = useCompositeAddr;
- break;
- }
- if (checkConstant->mConstType == BfConstType_ExtractValue)
- {
- auto gepConst = (BfConstantExtractValue*)checkConstant;
- BfIRValue targetConst(BfIRValueFlags_Const, gepConst->mTarget);
- checkConstant = module->mBfIRBuilder->GetConstant(targetConst);
- continue;
- }
- break;
- }
- }
- }
- }
- addr_ce retAddr = 0;
- if (ceFunction->mMaxReturnSize > 0)
- {
- int retSize = ceFunction->mMaxReturnSize;
- stackPtr -= retSize;
- retAddr = stackPtr - memStart;
- }
- delete mCeMachine->mAppendAllocInfo;
- mCeMachine->mAppendAllocInfo = NULL;
- BfType* returnType = NULL;
- BfType* castReturnType = NULL;
- bool success = Execute(ceFunction, stackPtr - ceFunction->mFrameSize, stackPtr, returnType, castReturnType);
- memStart = &mMemory[0];
- addr_ce retInstAddr = retAddr;
- if ((returnType->IsObject()) || (returnType->IsPointer()))
- {
- // Or pointer?
- retInstAddr = *(addr_ce*)(memStart + retAddr);
- }
- if ((flags & CeEvalFlags_ForceReturnThis) != 0)
- {
- returnType = thisType;
- retInstAddr = thisAddr;
- }
- BfTypedValue returnValue;
- if (success)
- {
- BfTypedValue retValue;
- if (returnType->IsObject())
- {
- BfType* usedReturnType = returnType;
- BfIRValue constVal = CreateConstant(module, (uint8*)&retInstAddr, returnType, &usedReturnType);
- if (constVal)
- returnValue = BfTypedValue(constVal, usedReturnType);
- else
- {
- Fail("Failed to encode return argument");
- }
- }
- else if ((retInstAddr != 0) || (allocThisInstAddr != 0))
- {
- auto* retPtr = memStart + retInstAddr;
- if ((allocThisInstAddr != 0) && (methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor))
- {
- retPtr = memStart + allocThisAddr;
- returnType = thisType;
- }
- BfType* usedReturnType = returnType;
- BfIRValue constVal;
- if (returnType->IsObject())
- {
- addr_ce retAddr = retPtr - memStart;
- constVal = CreateConstant(module, (uint8*)&retAddr, returnType, &usedReturnType);
- }
- else
- constVal = CreateConstant(module, retPtr, returnType, &usedReturnType);
- if (constVal)
- returnValue = BfTypedValue(constVal, usedReturnType);
- else
- {
- Fail("Failed to encode return argument");
- }
- }
- else if ((methodInstance->mMethodDef->mMethodType == BfMethodType_Ctor) && (thisType != NULL) && (thisType->IsValuelessType()))
- {
- returnValue = BfTypedValue(module->mBfIRBuilder->CreateConstAggZero(module->mBfIRBuilder->MapType(returnType, BfIRPopulateType_Identity)), thisType);
- }
- else if ((returnType->IsComposite()) || (returnType->IsValuelessType()))
- {
- returnValue = BfTypedValue(module->mBfIRBuilder->CreateConstAggZero(module->mBfIRBuilder->MapType(returnType, BfIRPopulateType_Identity)), returnType);
- }
- }
- mCallStack.Clear();
- moduleCurMethodInstance.Restore();
- moduleCurTypeInstance.Restore();
- module->AddDependency(methodInstance->GetOwner(), module->mCurTypeInstance, BfDependencyMap::DependencyFlag_ConstEval);
- if (!prevCallStack.IsEmpty())
- {
- BF_ASSERT((flags& CeEvalFlags_DbgCall) != 0);
- mCallStack = prevCallStack;
- }
- if ((castReturnType != NULL) && (returnValue))
- {
- auto castedReturnValue = module->Cast(callSource.mRefNode, returnValue, castReturnType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromComptimeReturn));
- if (castedReturnValue)
- return castedReturnValue;
- }
- return returnValue;
- }
- #define CE_CHECKSTACK() \
- if (stackPtr < memStart) \
- { \
- _Fail("Stack overflow"); \
- return false; \
- }
- #define CE_CHECKALLOC(SIZE) \
- if ((SIZE < 0) || (SIZE >= 0x80000000LL) || ((uintptr)memSize + (uintptr)SIZE > BF_CE_MAX_MEMORY)) \
- { \
- _Fail("Maximum memory size exceeded (2GB)"); \
- return false; \
- }
- // This check will fail for addresses < 64K (null pointer), or out-of-bounds
- #define CE_CHECKSIZE(SIZE) \
- if ((SIZE) < 0) \
- { \
- _Fail("Invalid memory size"); \
- return false; \
- }
- #define CE_CHECKADDR(ADDR, SIZE) \
- if (((ADDR) < 0x10000) || ((ADDR) + (SIZE) > memSize)) \
- { \
- _Fail("Access violation"); \
- return false; \
- }
- #define CE_CHECKADDR_STR(STRNAME, ADDR) \
- { \
- addr_ce checkAddr = ADDR; \
- while (true) \
- { \
- if ((uintptr)checkAddr >= (uintptr)memSize) \
- { \
- break; \
- } \
- if (memStart[checkAddr] == 0) \
- { \
- CE_CHECKADDR(ADDR, checkAddr - ADDR + 1); \
- STRNAME = String::MakeRef((char*)memStart + ADDR, checkAddr - ADDR + 1); \
- break; \
- } \
- checkAddr++; \
- } \
- }
- #define CE_GET_INTERNAL(VAR, ID, KIND) \
- if (!mInternalDataMap.TryGetValue((int)ID, &VAR)) \
- { \
- _Fail("Invalid internal resource id"); \
- return false; \
- } \
- if (VAR->mKind != KIND) \
- { \
- _Fail("Invalid internal resource kind"); \
- return false; \
- } \
- if (VAR->mReleased) \
- { \
- _Fail("Resource already released"); \
- return false; \
- }
- #define CE_REMOVE_INTERNAL(VAR, ID, KIND) \
- if (!mInternalDataMap.Remove((int)ID, &VAR)) \
- { \
- _Fail("Invalid internal resource id"); \
- return false; \
- } \
- if (VAR->mKind != KIND) \
- { \
- _Fail("Invalid internal resource kind"); \
- return false; \
- } \
- if (VAR->mReleased) \
- { \
- _Fail("Resource already released"); \
- return false; \
- }
- #define CE_GETINST(T) *((T*)(instPtr += sizeof(T)) - 1)
- #define CE_GETFRAME(T) *(T*)(framePtr + *((int32*)(instPtr += sizeof(int32)) - 1))
- #define CEOP_BIN(OP, T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto lhs = CE_GETFRAME(T); \
- auto rhs = CE_GETFRAME(T); \
- result = lhs OP rhs; \
- }
- #define CEOP_BIN_DIV(OP, T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto lhs = CE_GETFRAME(T); \
- auto rhs = CE_GETFRAME(T); \
- if (rhs == 0) \
- { \
- _Fail("Division by zero"); \
- return false; \
- } \
- result = lhs OP rhs; \
- }
- #define CEOP_BIN2(OP, TLHS, TRHS) \
- { \
- auto& result = CE_GETFRAME(TLHS); \
- auto lhs = CE_GETFRAME(TLHS); \
- auto rhs = CE_GETFRAME(TRHS); \
- result = lhs OP rhs; \
- }
- #define CEOP_BIN_CONST(OP, T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto lhs = CE_GETFRAME(T); \
- auto rhs = CE_GETINST(T); \
- result = lhs OP rhs; \
- }
- #define CEOP_UNARY_FUNC(OP, T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto lhs = CE_GETFRAME(T); \
- result = OP(lhs); \
- }
- #define CEOP_BIN_FUNC(OP, T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto lhs = CE_GETFRAME(T); \
- auto rhs = CE_GETFRAME(T); \
- result = OP(lhs, rhs); \
- }
- #define CEOP_UNARY(OP, T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto val = CE_GETFRAME(T); \
- result = OP val; \
- }
- #define CEOP_CMP(OP, T) \
- { \
- auto& result = CE_GETFRAME(bool); \
- auto lhs = CE_GETFRAME(T); \
- auto rhs = CE_GETFRAME(T); \
- result = lhs OP rhs; \
- }
- #define CE_CAST(TFROM, TTO) \
- { \
- auto& result = CE_GETFRAME(TTO); \
- auto val = CE_GETFRAME(TFROM); \
- result = (TTO)val; \
- }
- #define CE_LOAD(T) \
- { \
- auto& result = CE_GETFRAME(T); \
- auto ceAddr = CE_GETFRAME(addr_ce); \
- CE_CHECKADDR(ceAddr, sizeof(T)); \
- result = *(T*)(memStart + ceAddr); \
- }
- #define CE_STORE(T) \
- { \
- auto val = CE_GETFRAME(T); \
- auto ceAddr = CE_GETFRAME(addr_ce); \
- CE_CHECKADDR(ceAddr, sizeof(T)); \
- *(T*)(memStart + ceAddr) = val; \
- }
- #define CEOP_MOVE(T) \
- { \
- auto val = CE_GETFRAME(T); \
- auto& ptr = CE_GETFRAME(T); \
- ptr = val; \
- }
- #define CEOP_PUSH(T) \
- { \
- stackPtr -= sizeof(T); \
- auto val = CE_GETFRAME(T); \
- *((T*)stackPtr) = val; \
- CE_CHECKSTACK(); \
- }
- #define CEOP_POP(T) \
- { \
- auto& result = CE_GETFRAME(T); \
- result = *((T*)stackPtr); \
- stackPtr += sizeof(T); \
- }
- #define CE_CALL(CEFUNC) \
- if (CEFUNC == NULL) \
- { \
- _Fail("Unable to locate function entry"); \
- return false; \
- } \
- mCallStack.Add(_GetCurFrame()); \
- ceFunction = CEFUNC; \
- framePtr = stackPtr; \
- stackPtr -= ceFunction->mFrameSize; \
- if (isDebugging) \
- memset(stackPtr, 0, ceFunction->mFrameSize); \
- instPtr = &ceFunction->mCode[0]; \
- CE_CHECKSTACK();
- static void CeSetAddrVal(void* ptr, int64 val, int32 ptrSize)
- {
- if (ptrSize == 4)
- *(int32*)(ptr) = (int32)val;
- else
- *(int64*)(ptr) = (int64)val;
- }
- class CeAsyncOperation
- {
- public:
- CeInternalData* mInternalData;
- int mRefCount;
- uint8* mData;
- int mDataSize;
- int mReadSize;
- BfpFileResult mResult;
- public:
- CeAsyncOperation()
- {
- mInternalData = NULL;
- mRefCount = 1;
- mData = NULL;
- mDataSize = 0;
- mReadSize = 0;
- mResult = BfpFileResult_Ok;
- }
- ~CeAsyncOperation()
- {
- mInternalData->Release();
- delete mData;
- }
- void AddRef()
- {
- BfpSystem_InterlockedExchangeAdd32((uint32*)&mRefCount, 1);
- }
- void Release()
- {
- if (BfpSystem_InterlockedExchangeAdd32((uint32*)&mRefCount, (uint32)-1) == 1)
- delete this;
- }
- void Run()
- {
- mReadSize = BfpFile_Read(mInternalData->mFile, mData, mDataSize, -1, &mResult);
- Release();
- }
- static void RunProc(void* ptr)
- {
- ((CeAsyncOperation*)ptr)->Run();
- }
- };
- bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* startFramePtr, BfType*& returnType, BfType*& castReturnType)
- {
- auto ceModule = mCeMachine->mCeModule;
- SetAndRestoreValue<bool> ignoreWrites(ceModule->mBfIRBuilder->mIgnoreWrites, false);
- CeFunction* ceFunction = startFunction;
- returnType = startFunction->mMethodInstance->mReturnType;
- uint8* memStart = &mMemory[0];
- int memSize = mMemory.mSize;
- uint8* instPtr = (ceFunction->mCode.IsEmpty()) ? NULL : &ceFunction->mCode[0];
- uint8* stackPtr = startStackPtr;
- uint8* framePtr = startFramePtr;
- bool needsFunctionIds = ceModule->mSystem->mPtrSize != 8;
- int32 ptrSize = ceModule->mSystem->mPtrSize;
- bool isDebugging = mCeMachine->mDebugger != NULL;
- volatile bool* specialCheckPtr = &mCeMachine->mSpecialCheck;
- volatile bool* fastFinishPtr = &mCeMachine->mCompiler->mFastFinish;
- volatile bool* cancelingPtr = &mCeMachine->mCompiler->mCanceling;
- auto _GetCurFrame = [&]()
- {
- CeFrame ceFrame;
- ceFrame.mFunction = ceFunction;
- ceFrame.mReturnType = returnType;
- ceFrame.mFrameAddr = framePtr - memStart;
- ceFrame.mStackAddr = stackPtr - memStart;
- ceFrame.mInstPtr = instPtr;
- return ceFrame;
- };
- auto _FixVariables = [&]()
- {
- memSize = mMemory.mSize;
- intptr memOffset = &mMemory[0] - memStart;
- if (memOffset == 0)
- return;
- memStart += memOffset;
- stackPtr += memOffset;
- framePtr += memOffset;
- };
- auto _DbgPause = [&]()
- {
- int itr = 0;
- while (mCeMachine->mDebugger != NULL)
- {
- if (mCeMachine->mDbgPaused)
- {
- // This indicates a missed breakpoint, we should try to avoid this
- // Re-entrancy can cause this, from populating a type during cedebugger autocomplete
- OutputDebugStrF("CeMachine DbgPause reentry\n");
- return;
- }
- CePendingExpr* prevPendingExpr = NULL;
- ///
- {
- AutoCrit autoCrit(mCeMachine->mCritSect);
- if ((mCeMachine->mDebugger->mDebugPendingExpr != NULL) && (itr == 0))
- {
- // Abandon evaluating expression
- prevPendingExpr = mCeMachine->mDebugger->mDebugPendingExpr;
- mCeMachine->mDebugger->mDebugPendingExpr = NULL;
- }
- if (itr == 0)
- mCallStack.Add(_GetCurFrame());
- mCeMachine->mDbgPaused = true;
- }
- mCeMachine->mDebugEvent.WaitFor();
- CePendingExpr* pendingExpr = NULL;
- ///
- {
- AutoCrit autoCrit(mCeMachine->mCritSect);
- mCeMachine->mDbgPaused = false;
- if (mCeMachine->mStepState.mKind != CeStepState::Kind_Evaluate)
- {
- mCallStack.pop_back();
- _FixVariables();
- break;
- }
- mCeMachine->mStepState.mKind = CeStepState::Kind_None;
- String result;
- if (mCeMachine->mDebugger->mDebugPendingExpr != NULL)
- pendingExpr = mCeMachine->mDebugger->mDebugPendingExpr;
- }
- if (pendingExpr == NULL)
- continue;;
- pendingExpr->mResult = mCeMachine->mDebugger->DoEvaluate(pendingExpr, true);
- ///
- {
- AutoCrit autoCrit(mCeMachine->mCritSect);
- pendingExpr->mDone = true;
- if (pendingExpr != mCeMachine->mDebugger->mDebugPendingExpr)
- delete pendingExpr;
- }
- itr++;
- }
- };
- auto _Fail = [&](const StringImpl& error)
- {
- auto bfError = Fail(_GetCurFrame(), error);
- if ((bfError != NULL) && (mCeMachine->mDebugger != NULL))
- {
- mCeMachine->mDebugger->OutputRawMessage(StrFormat("error %s", error.c_str()));
- _DbgPause();
- }
- };
- auto _CheckFastFinish = [&]()
- {
- if (*fastFinishPtr)
- return true;
- if (mCeMachine->mDbgWantBreak)
- {
- mCeMachine->mDbgWantBreak = false;
- _DbgPause();
- }
- return false;
- };
- std::function<bool(CeFunction* checkFunction, bool& handled)> _CheckFunction = [&](CeFunction* checkFunction, bool& handled)
- {
- if (checkFunction == NULL)
- {
- Fail(_GetCurFrame(), "Const method not available");
- return false;
- }
- if (mCeMachine->mDebugger != NULL)
- {
- if (checkFunction->mBreakpointVersion != mCeMachine->mDebugger->mBreakpointVersion)
- mCeMachine->mDebugger->UpdateBreakpoints(checkFunction);
- }
- else if (checkFunction->mBreakpointVersion != 0)
- {
- checkFunction->UnbindBreakpoints();
- checkFunction->mBreakpointVersion = 0;
- }
- if (checkFunction->mFunctionKind != CeFunctionKind_Normal)
- {
- if (checkFunction->mFunctionKind == CeFunctionKind_OOB)
- {
- Fail(_GetCurFrame(), "Array out of bounds");
- return false;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_OOB)
- {
- Fail(_GetCurFrame(), "Object not initialized");
- return false;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Malloc)
- {
- int64 size;
- if (ptrSize == 4)
- size = *(int32*)((uint8*)stackPtr + 4);
- else
- size = *(int64*)((uint8*)stackPtr + 8);
- CE_CHECKALLOC(size);
- uint8* ptr = CeMalloc(size);
- CeSetAddrVal(stackPtr + 0, ptr - memStart, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Free)
- {
- addr_ce freeAddr = *(addr_ce*)((uint8*)stackPtr + 4);
- bool success = CeFree(freeAddr);
- if (!success)
- _Fail("Invalid heap address");
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_FatalError)
- {
- int32 strInstAddr = *(int32*)((uint8*)stackPtr + 0);
- int32 stackOffset = *(int32*)(stackPtr + ceModule->mSystem->mPtrSize);
- if (mCeMachine->mDebugger != NULL)
- mCeMachine->mDebugger->mPendingActiveFrameOffset = stackOffset;
- String error = "Fatal Error: ";
- GetStringFromAddr(strInstAddr, error);
- _Fail(error);
- return false;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_DynCheckFailed)
- {
- _Fail("Dynamic cast check failed");
- return false;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_DebugWrite)
- {
- int32 ptrVal = *(int32*)((uint8*)stackPtr + 0);
- auto size = *(int32*)(stackPtr + ceModule->mSystem->mPtrSize);
- CE_CHECKADDR(ptrVal, size);
- char* strPtr = (char*)(ptrVal + memStart);
- String str;
- str.Insert(0, strPtr, size);
- if (mCeMachine->mDebugger != NULL)
- mCeMachine->mDebugger->OutputMessage(str);
- else
- OutputDebugStr(str);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_DebugWrite_Int)
- {
- if (ceModule->mSystem->mPtrSize == 4)
- {
- int32 intVal = *(int32*)((uint8*)stackPtr + 0);
- OutputDebugStrF("Debug Val: %d %X\n", intVal, intVal);
- }
- else
- {
- int64 intVal = *(int64*)((uint8*)stackPtr + 0);
- OutputDebugStrF("Debug Val: %lld %llX\n", intVal, intVal);
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectTypeDeclById)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ceModule->mSystem->mPtrSize);
- auto reflectType = GetReflectTypeDecl(typeId);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectTypeDeclByName)
- {
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- String typeName;
- if (!GetStringFromStringView(strViewPtr, typeName))
- {
- _Fail("Invalid StringView");
- return false;
- }
- auto reflectType = GetReflectType(typeName, true);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectNextTypeDecl)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ceModule->mSystem->mPtrSize);
- addr_ce reflectType = 0;
- auto context = mCeMachine->mCeModule->mContext;
- if (mTypeDeclState == NULL)
- mTypeDeclState = new CeTypeDeclState();
- while (true)
- {
- typeId++;
- if (typeId >= mCeMachine->mCeModule->mContext->mTypes.mSize)
- {
- int foundTypeCount = 0;
- if (!mTypeDeclState->mCheckedAllTypeDefs)
- {
- mTypeDeclState->mCheckedAllTypeDefs = true;
- for (auto typeDef : ceModule->mSystem->mTypeDefs)
- {
- if ((typeDef->mIsPartial) && (!typeDef->mIsCombinedPartial))
- continue;
- if (typeDef->mTypeCode == BfTypeCode_TypeAlias)
- continue;
- if (typeDef->mTypeDeclaration == NULL)
- continue;
- if (mTypeDeclState->mIteratedTypeDefs.Contains(typeDef))
- continue;
- int lastTypeId = mCeMachine->mCompiler->mCurTypeId;
- auto resolvedType = mCeMachine->mCeModule->ResolveTypeDef(typeDef, BfPopulateType_Identity);
- if ((resolvedType != NULL) && (resolvedType->IsTypeInstance()))
- {
- if (resolvedType->mDefineState == BfTypeDefineState_Undefined)
- foundTypeCount++;
- }
- }
- }
- if (foundTypeCount > 0)
- typeId = 0;
- else
- break;
- }
- auto bfType = mCeMachine->mCeModule->mContext->mTypes[typeId];
- if (bfType != NULL)
- {
- if (bfType->IsOnDemand())
- continue;
- if (bfType->IsBoxed())
- continue;
-
- auto bfTypeInst = bfType->ToTypeInstance();
- if (bfTypeInst == NULL)
- continue;
-
- auto useTypeDef = bfTypeInst->mTypeDef;
- useTypeDef = useTypeDef->GetLatest();
- if (!mTypeDeclState->mCheckedAllTypeDefs)
- {
- mTypeDeclState->mIteratedTypeDefs.Add(useTypeDef);
- }
- else
- {
- if (mTypeDeclState->mIteratedTypeDefs.Contains(useTypeDef))
- continue;
- }
- if (bfTypeInst->IsGenericTypeInstance())
- {
- if (!bfTypeInst->IsUnspecializedType())
- continue;
- if (bfTypeInst->IsUnspecializedTypeVariation())
- continue;
- }
-
- reflectType = GetReflectTypeDecl(typeId);
- if (reflectType != 0)
- break;
- }
- }
-
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetBaseType)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + 4);
- int baseTypeId = 0;
- BfType* type = GetBfType(typeId);
- if (type != NULL)
- {
- AddTypeSigRebuild(type);
- if (auto typeInst = type->ToTypeInstance())
- {
- if (type->mDefineState < BfTypeDefineState_HasCustomAttributes)
- ceModule->PopulateType(type, BfPopulateType_CustomAttributes);
- if (typeInst->mBaseType != NULL)
- baseTypeId = typeInst->mBaseType->mTypeId;
- }
- }
- *(addr_ce*)(stackPtr + 0) = baseTypeId;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_HasDeclaredMember)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + 1);
- int32 memberKind = *(int32*)((uint8*)stackPtr + 1 + 4);
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 4 + 4);
- bool hasMember;
- String typeName;
- if (!GetStringFromStringView(strViewPtr, typeName))
- {
- _Fail("Invalid StringView");
- return false;
- }
- BfType* type = GetBfType(typeId);
- if ((type != NULL) && (type->IsTypeInstance()))
- {
- AddTypeSigRebuild(type);
- auto typeInst = type->ToTypeInstance();
- typeInst->mTypeDef->PopulateMemberSets();
- if (memberKind == 0) // Field
- hasMember = typeInst->mTypeDef->mFieldSet.ContainsWith((StringImpl&)typeName);
- else if (memberKind == 1) // Method
- hasMember = typeInst->mTypeDef->mMethodSet.ContainsWith((StringImpl&)typeName);
- else if (memberKind == 2) // Property
- hasMember = typeInst->mTypeDef->mPropertySet.ContainsWith((StringImpl&)typeName);
- }
- *(addr_ce*)(stackPtr + 0) = hasMember;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectType)
- {
- addr_ce objAddr = *(addr_ce*)((uint8*)stackPtr + ceModule->mSystem->mPtrSize);
- CE_CHECKADDR(objAddr, 4);
- int32 typeId = *(int32*)(objAddr + memStart);
- auto reflectType = GetReflectType(typeId);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectTypeById)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ceModule->mSystem->mPtrSize);
- auto reflectType = GetReflectType(typeId);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetWrappedType)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ceModule->mSystem->mPtrSize);
- BfType* type = GetBfType(typeId);
- bool success = false;
- if (type == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- addr_ce reflectType = NULL;
- type = ceModule->GetWrappedStructType(type);
- if (type != NULL)
- reflectType = GetReflectType(type->mTypeId);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectTypeByName)
- {
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- String typeName;
- if (!GetStringFromStringView(strViewPtr, typeName))
- {
- _Fail("Invalid StringView");
- return false;
- }
- auto reflectType = GetReflectType(typeName, false);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetReflectSpecializedType)
- {
- addr_ce typeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce typeSpan = *(addr_ce*)((uint8*)stackPtr + ptrSize * 2);
- auto reflectType = GetReflectSpecializedType(typeAddr, typeSpan);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Type_ToString)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- BfType* type = GetBfType(typeId);
- bool success = false;
- if (type == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance);
-
- bool simpleName = false;
- if ((type->IsUnspecializedType()) && (!type->IsUnspecializedTypeVariation()) && (!type->IsGenericParam()))
- simpleName = true;
- String typeName;
- if (simpleName)
- mCeMachine->mCeModule->DoTypeToString(typeName, type, BfTypeNameFlags_None);
- else
- typeName = mCeMachine->mCeModule->TypeToString(type);
-
- CeSetAddrVal(stackPtr + 0, GetString(typeName), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_TypeName_ToString)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- BfType* type = GetBfType(typeId);
- bool success = false;
- if (type == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- String str;
- if (auto typeInst = type->ToTypeInstance())
- str = typeInst->mTypeDef->mName->ToString();
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance);
- CeSetAddrVal(stackPtr + 0, GetString(str), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_TypeDocumentation_ToString)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- BfType* type = GetBfType(typeId);
- if (type == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- String str;
- if (auto typeInst = type->ToTypeInstance())
- if(typeInst->mTypeDef->mTypeDeclaration->mDocumentation != NULL)
- typeInst->mTypeDef->mTypeDeclaration->mDocumentation->GetDocString(str);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance);
- CeSetAddrVal(stackPtr + 0, GetString(str), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Namespace_ToString)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- BfType* type = GetBfType(typeId);
- bool success = false;
- if (type == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- String str;
- if (auto typeInst = type->ToTypeInstance())
- typeInst->mTypeDef->mNamespace.ToString(str);
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance);
- CeSetAddrVal(stackPtr + 0, GetString(str), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Type_GetCustomAttribute)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + 1);
- int32 attributeIdx = *(int32*)((uint8*)stackPtr + 1 + 4);
- addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 4 + 4);
- BfType* type = GetBfType(typeId);
- bool success = false;
- if (type != NULL)
- {
- AddTypeSigRebuild(type);
- auto typeInst = type->ToTypeInstance();
- if (typeInst != NULL)
- success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, typeInst->mCustomAttributes, attributeIdx, resultPtr);
- _FixVariables();
- }
- *(addr_ce*)(stackPtr + 0) = success;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetCustomAttribute)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + 1);
- int32 fieldIdx = *(int32*)((uint8*)stackPtr + 1 + 4);
- int32 attributeIdx = *(int32*)((uint8*)stackPtr + 1 + 4 + 4);
- addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 4 + 4 + 4);
- BfType* type = GetBfType(typeId);
- bool success = false;
- if (type != NULL)
- {
- AddTypeSigRebuild(type);
- auto typeInst = type->ToTypeInstance();
- if (typeInst != NULL)
- {
- if (typeInst->mDefineState < BfTypeDefineState_CETypeInit)
- mCurModule->PopulateType(typeInst);
- if ((fieldIdx >= 0) && (fieldIdx < typeInst->mFieldInstances.mSize))
- {
- auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
- success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, fieldInstance.mCustomAttributes, attributeIdx, resultPtr);
- _FixVariables();
- }
- else if (fieldIdx != -1)
- {
- _Fail("Invalid field");
- return false;
- }
- }
- }
- *(addr_ce*)(stackPtr + 0) = success;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetCustomAttribute)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr + 1);
- int32 attributeIdx = *(int32*)((uint8*)stackPtr + 1 + 8);
- addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 8 + 4);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- AddTypeSigRebuild(methodInstance->GetOwner());
- bool success = GetCustomAttribute(mCurModule, methodInstance->GetOwner()->mConstHolder, methodInstance->GetCustomAttributes(), attributeIdx, resultPtr);
- _FixVariables();
- *(addr_ce*)(stackPtr + 0) = success;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Type_GetCustomAttributeType)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- int32 attributeIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4);
- BfType* type = GetBfType(typeId);
- addr_ce reflectType = 0;
- if (type != NULL)
- {
- AddTypeSigRebuild(type);
- auto typeInst = type->ToTypeInstance();
- if (typeInst != NULL)
- {
- auto attrType = GetCustomAttributeType(typeInst->mCustomAttributes, attributeIdx);
- if (attrType != NULL)
- reflectType = GetReflectType(attrType->mTypeId);
- }
- _FixVariables();
- }
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetCustomAttributeType)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- int32 fieldIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4);
- int32 attributeIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4 + 4);
- BfType* type = GetBfType(typeId);
- addr_ce reflectType = 0;
- if (type != NULL)
- {
- AddTypeSigRebuild(type);
- auto typeInst = type->ToTypeInstance();
- if (typeInst != NULL)
- {
- if (typeInst->mDefineState < BfTypeDefineState_CETypeInit)
- mCurModule->PopulateType(typeInst);
- if ((fieldIdx >= 0) && (fieldIdx < typeInst->mFieldInstances.mSize))
- {
- auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
- auto attrType = GetCustomAttributeType(fieldInstance.mCustomAttributes, attributeIdx);
- if (attrType != NULL)
- reflectType = GetReflectType(attrType->mTypeId);
- _FixVariables();
- }
- else if (fieldIdx != -1)
- {
- _Fail("Invalid field");
- return false;
- }
- }
- }
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetCustomAttributeType)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize);
- int32 attributeIdx = *(int32*)((uint8*)stackPtr + ptrSize + 8);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- AddTypeSigRebuild(methodInstance->GetOwner());
- auto attrType = GetCustomAttributeType(methodInstance->GetCustomAttributes(), attributeIdx);
- if (attrType != NULL)
- CeSetAddrVal(stackPtr + 0, GetReflectType(attrType->mTypeId), ptrSize);
- else
- CeSetAddrVal(stackPtr + 0, 0, ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetMethodCount)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + 4);
- CeTypeInfo* typeInfo = mCeMachine->GetTypeInfo(GetBfType(typeId));
- if (typeInfo == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- *(int32*)(stackPtr + 0) = (int)typeInfo->mMethodInstances.size();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetMethod)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + 8);
- int32 methodIdx = *(int32*)((uint8*)stackPtr + 8+4);
- CeTypeInfo* typeInfo = mCeMachine->GetTypeInfo(GetBfType(typeId));
- if (typeInfo == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- if ((methodIdx < 0) || (methodIdx >= typeInfo->mMethodInstances.mSize))
- {
- *(int64*)(stackPtr + 0) = 0;
- }
- else
- *(int64*)(stackPtr + 0) = (int64)(intptr)typeInfo->mMethodInstances[methodIdx];
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_ToString)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- CeSetAddrVal(stackPtr + 0, GetString(mCeMachine->mCeModule->MethodToString(methodInstance)), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetName)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- CeSetAddrVal(stackPtr + 0, GetString(methodInstance->mMethodDef->GetReflectName()), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetDocumentation)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- String docs;
- if (auto decl = BfNodeDynCast<BfMethodDeclaration>(methodInstance->mMethodDef->mMethodDeclaration))
- if(decl->mDocumentation != NULL)
- decl->mDocumentation->GetDocString(docs);
- CeSetAddrVal(stackPtr + 0, GetString(docs), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetInfo)
- {
- // int32 mReturnType
- // int32 mParamCount
- // int32 mGenericArgCount
- // int32 mFlags
- // int8 ComptimeMethodFlags
- // int32 mMethodIdx
- int64 methodHandle = *(int64*)((uint8*)stackPtr + 4+4+4+4+1+4);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- AddTypeSigRebuild(methodInstance->GetOwner());
- int genericArgCount = 0;
- if (methodInstance->mMethodInfoEx != NULL)
- genericArgCount = methodInstance->mMethodInfoEx->mMethodGenericArguments.mSize;
- *(int32*)(stackPtr + 0) = methodInstance->mReturnType->mTypeId;
- *(int32*)(stackPtr + 4) = methodInstance->GetParamCount();
- *(int32*)(stackPtr + 4+4) = genericArgCount;
- *(int32*)(stackPtr + 4+4+4) = methodInstance->GetMethodFlags();
- *(int32*)(stackPtr + 4+4+4+4) = methodInstance->GetComptimeMethodFlags();
- *(int32*)(stackPtr + 4+4+4+4+1) = methodInstance->mMethodDef->mIdx;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetParamInfo)
- {
- // int32 mParamType
- // int16 mFlags
- // str mName
- int64 methodHandle = *(int64*)((uint8*)stackPtr + 4+2+ptrSize);
- int32 paramIdx = *(int32*)((uint8*)stackPtr + 4+2+ptrSize+8);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- if (paramIdx < 0 || paramIdx >= methodInstance->mParams.mSize)
- {
- _Fail("paramIdx is out of range");
- return false;
- }
- enum ParamFlags
- {
- ParamFlag_None = 0,
- ParamFlag_Splat = 1,
- ParamFlag_Implicit = 2,
- ParamFlag_AppendIdx = 4,
- ParamFlag_Params = 8
- };
- ParamFlags paramFlags = ParamFlag_None;
- if (methodInstance->GetParamIsSplat(paramIdx))
- paramFlags = (ParamFlags)(paramFlags | ParamFlag_Splat);
- if (methodInstance->GetParamKind(paramIdx) == BfParamKind_AppendIdx)
- paramFlags = (ParamFlags)(paramFlags | ParamFlag_Implicit | ParamFlag_AppendIdx);
- if (methodInstance->GetParamKind(paramIdx) == BfParamKind_Params)
- paramFlags = (ParamFlags)(paramFlags | ParamFlag_Params);
- addr_ce stringAddr = GetString(methodInstance->GetParamName(paramIdx));
- _FixVariables();
- *(int32*)(stackPtr + 0) = methodInstance->GetParamType(paramIdx)->mTypeId;
- *(int16*)(stackPtr + 4) = (int16)paramFlags;
- CeSetAddrVal(stackPtr + 4+2, stringAddr, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetGenericArg)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize);
- int32 genericArgIdx = *(int32*)((uint8*)stackPtr + ptrSize + 8);
- auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
- if (methodInstance == NULL)
- {
- _Fail("Invalid method instance");
- return false;
- }
- if ((methodInstance->mMethodInfoEx == NULL) || (genericArgIdx < 0) || (genericArgIdx >= methodInstance->mMethodInfoEx->mMethodGenericArguments.mSize))
- {
- _Fail("genericArgIdx is out of range");
- return false;
- }
- auto reflectType = GetReflectType(methodInstance->mMethodInfoEx->mMethodGenericArguments[genericArgIdx]->mTypeId);
- _FixVariables();
- CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetDocumentation)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- int32 fieldIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4);
- BfType* type = GetBfType(typeId);
- if (type == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- String docs;
- if (type != NULL)
- {
- if (auto typeInst = type->ToTypeInstance())
- {
- if (fieldIdx < 0 || fieldIdx >= typeInst->mFieldInstances.size())
- {
- _Fail("Invalid field");
- return false;
- }
- auto fieldInstance = typeInst->mFieldInstances[fieldIdx];
- if (auto decl = BfNodeDynCast<BfFieldDeclaration>(fieldInstance.GetFieldDef()->mFieldDeclaration))
- if(decl->mDocumentation != NULL)
- decl->mDocumentation->GetDocString(docs);
- }
- }
- CeSetAddrVal(stackPtr + 0, GetString(docs), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetStatic)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
- int32 fieldIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4);
- CeFunction* ctorCallFunction = NULL;
- BfType* bfType = GetBfType(typeId);
- bool success = false;
- if (bfType != NULL)
- {
- auto typeInst = bfType->ToTypeInstance();
- if (typeInst != NULL)
- {
- if (typeInst->mDefineState < BfTypeDefineState_CETypeInit)
- mCurModule->PopulateType(typeInst);
- if ((fieldIdx >= 0) && (fieldIdx < typeInst->mFieldInstances.mSize))
- {
- auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
- auto fieldType = fieldInstance.mResolvedType;
- ceModule->PopulateType(fieldType, BfPopulateType_Full_Force);
- int64 fieldId = ((int64)typeId << 32) | fieldIdx;
- CeStaticFieldInfo* staticFieldInfo = NULL;
- if (mStaticFieldIdMap.TryAdd(fieldId, NULL, &staticFieldInfo))
- {
- if (mStaticCtorExecSet.TryAdd(typeId, NULL))
- {
- BfTypeInstance* bfTypeInstance = NULL;
- if (bfType != NULL)
- bfTypeInstance = bfType->ToTypeInstance();
- if (bfTypeInstance == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- auto methodDef = bfTypeInstance->mTypeDef->GetMethodByName("__BfStaticCtor");
- if (methodDef == NULL)
- {
- _Fail("No static ctor found");
- return false;
- }
- auto moduleMethodInstance = ceModule->GetMethodInstance(bfTypeInstance, methodDef, BfTypeVector());
- if (!moduleMethodInstance)
- {
- _Fail("No static ctor instance found");
- return false;
- }
- bool added = false;
- ctorCallFunction = mCeMachine->GetFunction(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, added);
- if (ctorCallFunction->mInitializeState < CeFunction::InitializeState_Initialized)
- mCeMachine->PrepareFunction(ctorCallFunction, NULL);
- }
- _FixVariables();
- StringT<4096> staticVarName;
- BfMangler::Mangle(staticVarName, ceModule->mCompiler->GetMangleKind(), &fieldInstance);
- CeStaticFieldInfo* nameStaticFieldInfo = NULL;
- mStaticFieldMap.TryAdd(staticVarName, NULL, &nameStaticFieldInfo);
- if (nameStaticFieldInfo->mAddr == 0)
- {
- int fieldSize = fieldInstance.mResolvedType->mSize;
- CE_CHECKALLOC(fieldSize);
- uint8* ptr = CeMalloc(fieldSize);
- _FixVariables();
- if (fieldSize > 0)
- memset(ptr, 0, fieldSize);
- nameStaticFieldInfo->mAddr = (addr_ce)(ptr - memStart);
- }
- staticFieldInfo->mAddr = nameStaticFieldInfo->mAddr;
- }
- CeSetAddrVal(stackPtr + 0, staticFieldInfo->mAddr, ptrSize);
- }
- else if (fieldIdx != -1)
- {
- _Fail("Invalid field");
- return false;
- }
- }
- }
- if (ctorCallFunction != NULL)
- {
- bool handled = false;
- if (!_CheckFunction(ctorCallFunction, handled))
- return false;
- if (!handled)
- CE_CALL(ctorCallFunction);
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_SetReturnType)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr);
- if (returnType->IsVar())
- castReturnType = GetBfType(typeId);
- else
- _Fail("Comptime return types can only be set on methods declared with a 'var' return type");
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Align)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr);
- int32 align = *(int32*)((uint8*)stackPtr + sizeof(int32));
- if ((mCurEmitContext == NULL) || (mCurEmitContext->mType == NULL) || (mCurEmitContext->mType->mTypeId != typeId))
- {
- _Fail("This type cannot be modified in this context");
- return false;
- }
- mCurEmitContext->mAlign = BF_MAX(mCurEmitContext->mAlign, align);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_EmitTypeBody)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr);
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr + sizeof(int32));
- if ((mCurEmitContext == NULL) || (mCurEmitContext->mType == NULL) || (mCurEmitContext->mType->mTypeId != typeId))
- {
- _Fail("Code cannot be emitted for this type in this context");
- return false;
- }
- if (!GetStringFromStringView(strViewPtr, mCurEmitContext->mEmitData))
- {
- _Fail("Invalid StringView");
- return false;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_EmitAddInterface)
- {
- int32 typeId = *(int32*)((uint8*)stackPtr);
- int32 ifaceTypeId = *(int32*)((uint8*)stackPtr + sizeof(int32));
- if ((mCurEmitContext == NULL) || (mCurEmitContext->mType->mTypeId != typeId))
- {
- _Fail("Code cannot be emitted for this type in this context");
- return false;
- }
- mCurEmitContext->mInterfaces.Add(ifaceTypeId);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_EmitMethodEntry)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr);
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr + sizeof(int64));
- if ((mCurEmitContext == NULL) || (mCurEmitContext->mMethodInstance == NULL) ||
- (methodHandle != (int64)mCurEmitContext->mMethodInstance))
- {
- _Fail("Code cannot be emitted for this method in this context");
- return false;
- }
- if (!GetStringFromStringView(strViewPtr, mCurEmitContext->mEmitData))
- {
- _Fail("Invalid StringView");
- return false;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_EmitMethodExit)
- {
- int64 methodHandle = *(int64*)((uint8*)stackPtr);
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr + sizeof(int64));
- if ((mCurEmitContext == NULL) || (mCurEmitContext->mMethodInstance == NULL) ||
- (methodHandle != (int64)mCurEmitContext->mMethodInstance))
- {
- _Fail("Code cannot be emitted for this method in this context");
- return false;
- }
- if (!GetStringFromStringView(strViewPtr, mCurEmitContext->mExitEmitData))
- {
- _Fail("Invalid StringView");
- return false;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_EmitMixin)
- {
- SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurModule->mCurMethodInstance, mCallerMethodInstance);
- SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurModule->mCurTypeInstance, mCallerTypeInstance);
- SetAndRestoreValue<bool> emitIgnoreWrites(ceModule->mBfIRBuilder->mIgnoreWrites, ignoreWrites.mPrevVal);
- addr_ce strViewPtr = *(addr_ce*)((uint8*)stackPtr);
- String emitStr;
- if (!GetStringFromStringView(strViewPtr, emitStr))
- {
- _Fail("Invalid StringView");
- return false;
- }
- mCurModule->CEMixin(mCurCallSource->mRefNode, emitStr);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_GetStringById)
- {
- int32 stringId = *(int32*)((uint8*)stackPtr + ptrSize);
- String string = "";
- BfStringPoolEntry* valuePtr = NULL;
- mCurModule->mContext->mStringObjectIdMap.TryGetValue(stringId, &valuePtr);
- if (valuePtr != NULL)
- string = valuePtr->mString;
- CeSetAddrVal(stackPtr + 0, GetString(string), ptrSize);
- _FixVariables();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Sleep)
- {
- int32 sleepMS = *(int32*)((uint8*)stackPtr);
- while (sleepMS > 0)
- {
- if (_CheckFastFinish())
- break;
- if (sleepMS > 20)
- {
- BfpThread_Sleep(20);
- sleepMS -= 20;
- continue;
- }
- BfpThread_Sleep(sleepMS);
- break;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpSystem_GetTimeStamp)
- {
- int64& result = *(int64*)((uint8*)stackPtr + 0);
- result = BfpSystem_GetTimeStamp();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_ToLower)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 4);
- result = utf8proc_tolower(val);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_ToUpper)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 4);
- result = utf8proc_toupper(val);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsLower)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 1);
- result = utf8proc_category(val) == UTF8PROC_CATEGORY_LL;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsUpper)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 1);
- result = utf8proc_category(val) == UTF8PROC_CATEGORY_LU;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsWhiteSpace_EX)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 1);
- auto cat = utf8proc_category(val);
- result = (cat == UTF8PROC_CATEGORY_ZS) || (cat == UTF8PROC_CATEGORY_ZL) || (cat == UTF8PROC_CATEGORY_ZP);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsLetterOrDigit)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 1);
- auto cat = utf8proc_category(val);
- switch (cat)
- {
- case UTF8PROC_CATEGORY_LU:
- case UTF8PROC_CATEGORY_LL:
- case UTF8PROC_CATEGORY_LT:
- case UTF8PROC_CATEGORY_LM:
- case UTF8PROC_CATEGORY_LO:
- case UTF8PROC_CATEGORY_ND:
- case UTF8PROC_CATEGORY_NL:
- case UTF8PROC_CATEGORY_NO:
- result = true;
- break;
- default:
- result = false;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsLetter)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 1);
- auto cat = utf8proc_category(val);
- switch (cat)
- {
- case UTF8PROC_CATEGORY_LU:
- case UTF8PROC_CATEGORY_LL:
- case UTF8PROC_CATEGORY_LT:
- case UTF8PROC_CATEGORY_LM:
- case UTF8PROC_CATEGORY_LO:
- result = true;
- break;
- default:
- result = false;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Char32_IsNumber)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- int32 val = *(int32*)((uint8*)stackPtr + 1);
- auto cat = utf8proc_category(val);
- switch (cat)
- {
- case UTF8PROC_CATEGORY_ND:
- case UTF8PROC_CATEGORY_NL:
- case UTF8PROC_CATEGORY_NO:
- result = true;
- break;
- default:
- result = false;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Double_Strtod)
- {
- double& result = *(double*)((uint8*)stackPtr + 0);
- addr_ce strAddr = *(addr_ce*)((uint8*)stackPtr + 8);
- addr_ce endAddr = *(addr_ce*)((uint8*)stackPtr + 8 + ptrSize);
- addr_ce checkAddr = strAddr;
- while (true)
- {
- if ((uintptr)checkAddr >= (uintptr)memSize)
- {
- checkAddr++;
- break;
- }
- if (memStart[checkAddr] == 0)
- break;
- checkAddr++;
- }
- CE_CHECKADDR(strAddr, checkAddr - strAddr + 1);
- char* strPtr = (char*)(memStart + strAddr);
- char** endPtr = NULL;
- if (endAddr != 0)
- endPtr = (char**)(memStart + endAddr);
- result = strtod(strPtr, endPtr);
- if (endAddr != 0)
- {
- CE_CHECKADDR(endAddr, ptrSize);
- CeSetAddrVal(endPtr, (uint8*)endPtr - memStart, ptrSize);
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Double_Ftoa)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- float val = *(float*)((uint8*)stackPtr + 4);
- addr_ce strAddr = *(addr_ce*)((uint8*)stackPtr + 4 + 4);
- char str[256];
- int count = sprintf(str, "%1.9f", val);
- CE_CHECKADDR(strAddr, count + 1);
- memcpy(memStart + strAddr, str, count + 1);
- result = count;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Double_ToString)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- double val = *(double*)((uint8*)stackPtr + 4);
- addr_ce strAddr = *(addr_ce*)((uint8*)stackPtr + 4 + 8);
- char str[256];
- int count = DoubleToString(val, str);
- CE_CHECKADDR(strAddr, count + 1);
- memcpy(memStart + strAddr, str, count + 1);
- result = count;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_Float_ToString)
- {
- int32& result = *(int32*)((uint8*)stackPtr + 0);
- float val = *(float*)((uint8*)stackPtr + 4);
- addr_ce strAddr = *(addr_ce*)((uint8*)stackPtr + 4 + 4);
- char str[256];
- int count = FloatToString(val, str);
- CE_CHECKADDR(strAddr, count + 1);
- memcpy(memStart + strAddr, str, count + 1);
- result = count;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_Create)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- BfpDirectory_Create(path.c_str(), (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_Rename)
- {
- addr_ce srcAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce destAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String srcPath;
- CE_CHECKADDR_STR(srcPath, srcAddr);
- String destPath;
- CE_CHECKADDR_STR(destPath, destAddr);
- FixRelativePath(srcPath);
- FixRelativePath(destPath);
- BfpDirectory_Rename(srcPath.c_str(), destPath.c_str(), (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_Delete)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- BfpDirectory_Delete(path.c_str(), (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_GetCurrent)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- CalcWorkingDir();
- TryStringOut(mWorkingDir, namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_SetCurrent)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- if (::BfpDirectory_Exists(path.c_str()))
- {
- mWorkingDir = path;
- if (outResultAddr != 0)
- *(BfpFileResult*)(memStart + outResultAddr) = BfpFileResult_Ok;
- }
- else
- {
- if (outResultAddr != 0)
- *(BfpFileResult*)(memStart + outResultAddr) = BfpFileResult_NotFound;
- }
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_Exists)
- {
- bool& result = *(bool*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 1);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- result = BfpDirectory_Exists(path.c_str());
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpDirectory_GetSysDirectory)
- {
- BfpSysDirectoryKind sysDirKind = *(BfpSysDirectoryKind*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 4);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + 4 + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + 4 + ptrSize + ptrSize);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- BfpDirectory_GetSysDirectory(sysDirKind, namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Close)
- {
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- CE_CHECKADDR(outResultAddr, 4);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- BfpFile_Close(internalData->mFile, (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Create)
- {
- void* resultPtr = ((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- int createKind = *(int*)((uint8*)stackPtr + ptrSize + ptrSize);
- int createFlags = *(int*)((uint8*)stackPtr + ptrSize + ptrSize + 4);
- int createFileAttrs = *(int*)((uint8*)stackPtr + ptrSize + ptrSize + 4 + 4);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + 4 + 4 + 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- CE_CHECKADDR(outResultAddr, 4);
- FixRelativePath(path);
- auto bfpFile = BfpFile_Create(path.c_str(), (BfpFileCreateKind)createKind, (BfpFileCreateFlags)createFlags, (BfpFileAttributes)createFileAttrs, (BfpFileResult*)(memStart + outResultAddr));
- if (bfpFile != NULL)
- {
- if ((createKind == BfpFileCreateKind_OpenExisting) || (createKind == BfpFileCreateKind_OpenAlways))
- AddFileRebuild(path);
- CeInternalData* internalData = new CeInternalData();
- internalData->mKind = CeInternalData::Kind_File;
- internalData->mFile = bfpFile;
- mInternalDataMap[++mCurHandleId] = internalData;
- CeSetAddrVal(resultPtr, mCurHandleId, ptrSize);
- }
- else
- CeSetAddrVal(resultPtr, 0, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Flush)
- {
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + 0);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- BfpFile_Flush(internalData->mFile);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetFileSize)
- {
- int64& result = *(int64*)((uint8*)stackPtr + 0);
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + 8);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- result = BfpFile_GetFileSize(internalData->mFile);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Read)
- {
- void* resultPtr = ((uint8*)stackPtr + 0);
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce bufferPtr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- intptr bufferSize = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- int timeoutMS = *(int32*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize + ptrSize);
- CE_CHECKADDR(bufferPtr, bufferSize);
- CE_CHECKADDR(outResultAddr, 4);
- BfpFileResult fileResult = BfpFileResult_UnknownError;
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- int timeoutLeft = timeoutMS;
- int64 result = 0;
- CeAsyncOperation* asyncOperation = NULL;
- BfpThread* asyncThread = NULL;
- while (true)
- {
- if (*cancelingPtr)
- break;
- int useTimeout = timeoutLeft;
- if (useTimeout < 0)
- {
- useTimeout = 20;
- }
- else if (useTimeout > 20)
- {
- useTimeout = 20;
- timeoutLeft -= useTimeout;
- }
- else
- timeoutLeft = 0;
- if (asyncOperation != NULL)
- {
- if (BfpThread_WaitFor(asyncThread, useTimeout))
- {
- if (asyncOperation->mReadSize > 0)
- memcpy(memStart + bufferPtr, asyncOperation->mData, asyncOperation->mReadSize);
- result = asyncOperation->mReadSize;
- fileResult = asyncOperation->mResult;
- break;
- }
- continue;
- }
- result = BfpFile_Read(internalData->mFile, memStart + bufferPtr, bufferSize, useTimeout, &fileResult);
- if (fileResult == BfpFileResult_Timeout)
- {
- if (timeoutLeft > 0)
- continue;
- }
- if (fileResult != BfpFileResult_InvalidParameter)
- break;
- BF_ASSERT(asyncOperation == NULL);
- asyncOperation = new CeAsyncOperation();
- asyncOperation->mInternalData = internalData;
- asyncOperation->mInternalData->AddRef();
- asyncOperation->mData = new uint8[bufferSize];
- asyncOperation->mDataSize = bufferSize;
- asyncOperation->AddRef();
- asyncThread = BfpThread_Create(CeAsyncOperation::RunProc, asyncOperation);
- }
- if (asyncOperation != NULL)
- asyncOperation->Release();
- if (asyncThread != NULL)
- BfpThread_Release(asyncThread);
- if (outResultAddr != 0)
- *(BfpFileResult*)(memStart + outResultAddr) = fileResult;
- CeSetAddrVal(resultPtr, result, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Release)
- {
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + 0);
- CeInternalData* internalData = NULL;
- CE_REMOVE_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- internalData->Release();
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Seek)
- {
- int64& result = *(int64*)((uint8*)stackPtr + 0);
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + 8);
- int64 offset = *(int64*)((uint8*)stackPtr + 8 + ptrSize);
- int seekKind = *(int*)((uint8*)stackPtr + 8 + ptrSize + 8);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- result = BfpFile_Seek(internalData->mFile, offset, (BfpFileSeekKind)seekKind);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Truncate)
- {
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- CE_CHECKADDR(outResultAddr, 4);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- BfpFile_Truncate(internalData->mFile, (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Write)
- {
- void* resultPtr = ((uint8*)stackPtr + 0);
- addr_ce fileId = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce bufferPtr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- intptr bufferSize = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- int timeoutMS = *(int32*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize + ptrSize);
- CE_CHECKADDR(bufferPtr, bufferSize);
- CE_CHECKADDR(outResultAddr, 4);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)fileId, CeInternalData::Kind_File);
- int64 result = BfpFile_Write(internalData->mFile, memStart + bufferPtr, bufferSize, timeoutMS, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- CeSetAddrVal(resultPtr, result, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetTime_LastWrite)
- {
- BfpTimeStamp& result = *(BfpTimeStamp*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 8);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- AddFileRebuild(path);
- result = BfpFile_GetTime_LastWrite(path.c_str());
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetAttributes)
- {
- BfpFileAttributes& result = *(BfpFileAttributes*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 4);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + 4);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- result = BfpFile_GetAttributes(path.c_str(), (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_SetAttributes)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- BfpFileAttributes attribs = *(BfpFileAttributes*)((uint8*)stackPtr + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- BfpFile_SetAttributes(path.c_str(), attribs, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Copy)
- {
- addr_ce srcAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce destAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- BfpFileCopyKind fileCopyKind = *(BfpFileCopyKind*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + 4);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String srcPath;
- CE_CHECKADDR_STR(srcPath, srcAddr);
- String destPath;
- CE_CHECKADDR_STR(destPath, destAddr);
- FixRelativePath(srcPath);
- FixRelativePath(destPath);
- BfpFile_Copy(srcPath.c_str(), destPath.c_str(), fileCopyKind, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Rename)
- {
- addr_ce srcAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce destAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String srcPath;
- CE_CHECKADDR_STR(srcPath, srcAddr);
- String destPath;
- CE_CHECKADDR_STR(destPath, destAddr);
- FixRelativePath(srcPath);
- FixRelativePath(destPath);
- BfpFile_Rename(srcPath.c_str(), destPath.c_str(), (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Delete)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- BfpFile_Delete(path.c_str(), (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_Exists)
- {
- bool& result = *(bool*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 1);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- FixRelativePath(path);
- AddFileRebuild(path);
- result = BfpFile_Exists(path.c_str());
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetTempPath)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- BfpFile_GetTempPath(namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetTempFileName)
- {
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- BfpFile_GetTempFileName(namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetFullPath)
- {
- addr_ce srcAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- String srcPath;
- CE_CHECKADDR_STR(srcPath, srcAddr);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- FixRelativePath(srcPath);
- BfpFile_GetFullPath(srcPath.c_str(), namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFile_GetActualPath)
- {
- addr_ce srcAddr = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- String srcPath;
- CE_CHECKADDR_STR(srcPath, srcAddr);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- FixRelativePath(srcPath);
- BfpFile_GetActualPath(srcPath.c_str(), namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpSpawn_Create)
- {
- void* resultPtr = ((uint8*)stackPtr + 0);
- addr_ce targetPathAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce argsAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce workingDirAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- addr_ce envAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize);
- int flags = *(int*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize + ptrSize + ptrSize + 4);
- String targetPath;
- CE_CHECKADDR_STR(targetPath, targetPathAddr);
- String args;
- if (argsAddr != 0)
- CE_CHECKADDR_STR(args, argsAddr);
- String workingDir;
- if (workingDirAddr != 0)
- CE_CHECKADDR_STR(workingDir, workingDirAddr);
- String env;
- if (envAddr != 0)
- CE_CHECKADDR_STR(env, envAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- if ((targetPath.Contains('/')) || (targetPath.Contains('\\')))
- {
- FixRelativePath(targetPath);
- }
- auto bfpSpawn = BfpSpawn_Create(targetPath.c_str(),
- (argsAddr == 0) ? NULL : args.c_str(),
- (workingDirAddr == 0) ? NULL : workingDir.c_str(),
- (envAddr == 0) ? NULL : env.c_str(), (BfpSpawnFlags)flags, (outResultAddr == 0) ? NULL : (BfpSpawnResult*)(memStart + outResultAddr));
- if (bfpSpawn != NULL)
- {
- CeInternalData* internalData = new CeInternalData();
- internalData->mKind = CeInternalData::Kind_Spawn;
- internalData->mSpawn = bfpSpawn;
- mInternalDataMap[++mCurHandleId] = internalData;
- CeSetAddrVal(resultPtr, mCurHandleId, ptrSize);
- }
- else
- CeSetAddrVal(resultPtr, 0, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpSpawn_GetStdHandles)
- {
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce outStdInAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce outStdOutAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outStdErrAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- if (outStdInAddr != 0)
- CE_CHECKADDR(outStdInAddr, ptrSize);
- if (outStdOutAddr != 0)
- CE_CHECKADDR(outStdOutAddr, ptrSize);
- if (outStdErrAddr != 0)
- CE_CHECKADDR(outStdErrAddr, ptrSize);
- BfpFile* outStdIn = NULL;
- BfpFile* outStdOut = NULL;
- BfpFile* outStdErr = NULL;
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_Spawn);
- BfpSpawn_GetStdHandles(internalData->mSpawn,
- (outStdInAddr != 0) ? &outStdIn : NULL,
- (outStdOutAddr != 0) ? &outStdOut : NULL,
- (outStdErrAddr != 0) ? &outStdErr : NULL);
- auto _SetHandle = [&](addr_ce addr, BfpFile* file)
- {
- if (addr == 0)
- return;
- if (file != NULL)
- {
- CeInternalData* internalData = new CeInternalData();
- internalData->mKind = CeInternalData::Kind_File;
- internalData->mFile = file;
- mInternalDataMap[++mCurHandleId] = internalData;
- CeSetAddrVal(memStart + addr, mCurHandleId, ptrSize);
- }
- };
- if (outStdInAddr != 0)
- _SetHandle(outStdInAddr, outStdIn);
- if (outStdOutAddr != 0)
- _SetHandle(outStdOutAddr, outStdOut);
- if (outStdErrAddr != 0)
- _SetHandle(outStdErrAddr, outStdErr);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpSpawn_Kill)
- {
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 0);
- int exitCode = *(int*)((uint8*)stackPtr + ptrSize);
- int killFlags = *(int*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- CE_CHECKADDR(outResultAddr, 4);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_Spawn);
- BfpSpawn_Kill(internalData->mSpawn, exitCode, (BfpKillFlags)killFlags, (BfpSpawnResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpSpawn_Release)
- {
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 0);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_Spawn);
- internalData->mReleased = true;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpSpawn_WaitFor)
- {
- bool& result = *(bool*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 1);
- int waitMS = *(int*)((uint8*)stackPtr + 1 + ptrSize);
- addr_ce outExitCodeAddr = *(addr_ce*)((uint8*)stackPtr + 1 + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + 1 + ptrSize + ptrSize + ptrSize);
- CE_CHECKADDR(outExitCodeAddr, ptrSize);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_Spawn);
- int outExitCode = 0;
- int timeLeft = waitMS;
- do
- {
- if (_CheckFastFinish())
- {
- result = false;
- break;
- }
- int waitTime = 20;
- if (timeLeft >= 0)
- {
- waitTime = BF_MIN(timeLeft, 20);
- timeLeft -= waitTime;
- }
- result = BfpSpawn_WaitFor(internalData->mSpawn, waitTime, &outExitCode, (outResultAddr == 0) ? NULL : (BfpSpawnResult*)(memStart + outResultAddr));
- if (result)
- break;
- if (waitTime == 0)
- break;
- } while (true);
- *(int*)(memStart + outExitCodeAddr) = outExitCode;
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_FindFirstFile)
- {
- void* resultPtr = ((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- int flags = *(int*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + 4);
- String path;
- CE_CHECKADDR_STR(path, nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- FixRelativePath(path);
- auto bfpFindFileData = BfpFindFileData_FindFirstFile(path.c_str(), (BfpFindFileFlags)flags, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- if (bfpFindFileData != NULL)
- {
- String dir = GetFileDir(path);
- dir = FixPathAndCase(dir);
- dir.Append(DIR_SEP_CHAR);
- CeRebuildKey rebuildKey;
- rebuildKey.mKind = CeRebuildKey::Kind_Directory;
- rebuildKey.mString = dir;
- CeRebuildValue rebuildValue;
- if (AddRebuild(rebuildKey, rebuildValue))
- mCurModule->mCompiler->mRebuildFileSet.Add(dir);
- CeInternalData* internalData = new CeInternalData();
- internalData->mKind = CeInternalData::Kind_FindFileData;
- internalData->mFindFileData = bfpFindFileData;
- mInternalDataMap[++mCurHandleId] = internalData;
- CeSetAddrVal(resultPtr, mCurHandleId, ptrSize);
- }
- else
- CeSetAddrVal(resultPtr, 0, ptrSize);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_FindNextFile)
- {
- bool& result = *(bool*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 1);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- result = BfpFindFileData_FindNextFile(internalData->mFindFileData);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_GetFileName)
- {
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 0);
- addr_ce nameAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize);
- addr_ce sizeAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize);
- addr_ce outResultAddr = *(addr_ce*)((uint8*)stackPtr + ptrSize + ptrSize + ptrSize);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- CE_CHECKADDR(sizeAddr, 4);
- int& nameSize = *(int*)(memStart + sizeAddr);
- CE_CHECKADDR(nameAddr, nameSize);
- char* namePtr = (char*)(memStart + nameAddr);
- if (outResultAddr != 0)
- CE_CHECKADDR(outResultAddr, 4);
- BfpFindFileData_GetFileName(internalData->mFindFileData, namePtr, &nameSize, (outResultAddr == 0) ? NULL : (BfpFileResult*)(memStart + outResultAddr));
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_GetTime_LastWrite)
- {
- BfpTimeStamp& result = *(BfpTimeStamp*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 8);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- result = BfpFindFileData_GetTime_LastWrite(internalData->mFindFileData);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_GetTime_Created)
- {
- BfpTimeStamp& result = *(BfpTimeStamp*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 8);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- result = BfpFindFileData_GetTime_Created(internalData->mFindFileData);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_GetTime_Access)
- {
- BfpTimeStamp& result = *(BfpTimeStamp*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 8);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- result = BfpFindFileData_GetTime_Access(internalData->mFindFileData);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_GetFileAttributes)
- {
- BfpFileAttributes& result = *(BfpFileAttributes*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 4);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- result = BfpFindFileData_GetFileAttributes(internalData->mFindFileData);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_GetFileSize)
- {
- int64& result = *(int64*)((uint8*)stackPtr + 0);
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 8);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- result = BfpFindFileData_GetFileSize(internalData->mFindFileData);
- }
- else if (checkFunction->mFunctionKind == CeFunctionKind_BfpFindFileData_Release)
- {
- addr_ce spawnId = *(addr_ce*)((uint8*)stackPtr + 0);
- CeInternalData* internalData = NULL;
- CE_GET_INTERNAL(internalData, (int)spawnId, CeInternalData::Kind_FindFileData);
- internalData->mReleased = true;
- }
- else
- {
- Fail(_GetCurFrame(), StrFormat("Unable to invoke extern method '%s'", ceModule->MethodToString(checkFunction->mMethodInstance).c_str()));
- return false;
- }
- handled = true;
- return true;
- }
- if (!checkFunction->mFailed)
- return true;
- if ((mCeMachine->mDebugger != NULL) && (!mCallStack.IsEmpty()))
- _Fail(StrFormat("Attempting to call failed method '%s'", ceModule->MethodToString(checkFunction->mMethodInstance).c_str()));
- auto error = Fail(_GetCurFrame(), StrFormat("Method call preparation '%s' failed", ceModule->MethodToString(checkFunction->mMethodInstance).c_str()));
- if ((error != NULL) && (!checkFunction->mGenError.IsEmpty()))
- mCeMachine->mCompiler->mPassInstance->MoreInfo("Comptime method generation error: " + checkFunction->mGenError);
- return false;
- };
- //
- {
- bool handled = false;
- if (!_CheckFunction(ceFunction, handled))
- return false;
- if (handled)
- return true;
- }
- int callCount = 0;
- int instCount = 0;
- CE_CHECKSTACK();
- while (true)
- {
- ++instCount;
- CeOp op = CE_GETINST(CeOp);
- if (*specialCheckPtr)
- {
- SpecialCheck:
- if (*fastFinishPtr)
- {
- BfTypeInstance* rebuildType = NULL;
- if ((mCurModule != NULL) && (mCurModule->mCurTypeInstance != NULL))
- rebuildType = mCurModule->mCurTypeInstance;
- if ((mCurEmitContext != NULL) && (mCurEmitContext->mType != NULL))
- rebuildType = mCurEmitContext->mType->ToTypeInstance();
- if (rebuildType != NULL)
- {
- rebuildType->mRebuildFlags = (BfTypeRebuildFlags)(rebuildType->mRebuildFlags | BfTypeRebuildFlag_ConstEvalCancelled);
- mCurModule->DeferRebuildType(rebuildType);
- }
- if (*cancelingPtr)
- {
- if ((mCurModule == NULL) || (mCurModule->mCurTypeInstance == NULL))
- _Fail("Comptime evaluation canceled");
- }
- return false;
- }
- bool wantsStop = false;
- if (mCeMachine->mStepState.mKind != CeStepState::Kind_None)
- {
- int curDepth = mCallStack.mSize + 1;
- int instIdx = instPtr - ceFunction->mCode.mVals - 1;
- switch (mCeMachine->mStepState.mKind)
- {
- case CeStepState::Kind_StepInfo:
- if (curDepth != mCeMachine->mStepState.mStartDepth)
- wantsStop = true;
- else if (instIdx >= mCeMachine->mStepState.mNextInstIdx)
- wantsStop = true;
- break;
- case CeStepState::Kind_StepInfo_Asm:
- wantsStop = true;
- break;
- case CeStepState::Kind_StepOver:
- if (curDepth < mCeMachine->mStepState.mStartDepth)
- wantsStop = true;
- else if ((mCeMachine->mStepState.mStartDepth == curDepth) && (instIdx >= mCeMachine->mStepState.mNextInstIdx))
- wantsStop = true;
- break;
- case CeStepState::Kind_StepOver_Asm:
- if (curDepth < mCeMachine->mStepState.mStartDepth)
- wantsStop = true;
- else if (curDepth == mCeMachine->mStepState.mStartDepth)
- wantsStop = true;
- break;
- case CeStepState::Kind_StepOut:
- if (curDepth < mCeMachine->mStepState.mStartDepth)
- wantsStop = true;
- else if ((curDepth == mCeMachine->mStepState.mStartDepth) && (instIdx >= mCeMachine->mStepState.mNextInstIdx))
- wantsStop = true;
- break;
- case CeStepState::Kind_StepOut_Asm:
- if (curDepth <= mCeMachine->mStepState.mStartDepth)
- wantsStop = true;
- break;
- case CeStepState::Kind_Jmp:
- instPtr = &ceFunction->mCode[mCeMachine->mStepState.mNextInstIdx];
- op = CE_GETINST(CeOp);
- wantsStop = true;
- break;
- }
- }
- else if (mCeMachine->mDbgWantBreak)
- {
- wantsStop = true;
- }
- else if ((mCeMachine->mDebugger != NULL) && (mCeMachine->mDebugger->mBreakpointFramesDirty))
- {
- AutoCrit autoCrit(mCeMachine->mCritSect);
- mCallStack.Add(_GetCurFrame());
- mCeMachine->mDebugger->UpdateBreakpointFrames();
- mCallStack.pop_back();
- }
- else
- *specialCheckPtr = false;
- if (wantsStop)
- {
- mCeMachine->mDbgWantBreak = false;
- mCeMachine->mStepState.mKind = CeStepState::Kind_None;
- _DbgPause();
- if (mCeMachine->mStepState.mKind == CeStepState::Kind_Jmp)
- goto SpecialCheck;
- // We may have changed breakpoints so we need to re-read
- instPtr -= sizeof(CeOp);
- op = CE_GETINST(CeOp);
- }
- }
- OpSwitch:
- switch (op)
- {
- case CeOp_Nop:
- break;
- case CeOp_DbgBreak:
- {
- bool foundBreakpoint = false;
- bool skipInst = false;
- if (mCeMachine->mDebugger != NULL)
- {
- AutoCrit autoCrit(mCeMachine->mCritSect);
- int instIdx = instPtr - ceFunction->mCode.mVals - 2;
- CeBreakpointBind* breakpointEntry = NULL;
- if (ceFunction->mBreakpoints.TryGetValue(instIdx, &breakpointEntry))
- {
- bool doBreak = false;
- mCallStack.Add(_GetCurFrame());
- if (mCeMachine->mDebugger->CheckConditionalBreakpoint(breakpointEntry->mBreakpoint))
- doBreak = true;
- mCallStack.pop_back();
- op = breakpointEntry->mPrevOpCode;
- // Keep us from an infinite loop if we set a breakpoint on a manual Break
- skipInst = op == CeOp_DbgBreak;
- foundBreakpoint = true;
- if (!doBreak)
- {
- _FixVariables();
- if (skipInst)
- break;
- goto OpSwitch;
- }
- mCeMachine->mDebugger->mActiveBreakpoint = breakpointEntry->mBreakpoint;
- }
- }
- _DbgPause();
- if (mCeMachine->mStepState.mKind == CeStepState::Kind_Jmp)
- goto SpecialCheck;
- if (skipInst)
- break;
- if (foundBreakpoint)
- goto OpSwitch;
- }
- break;
- case CeOp_Ret:
- {
- if (mCallStack.mSize == 0)
- return true;
- auto& ceFrame = mCallStack.back();
- ceFunction = ceFrame.mFunction;
- instPtr = ceFrame.mInstPtr;
- stackPtr = memStart + ceFrame.mStackAddr;
- framePtr = memStart + ceFrame.mFrameAddr;
- returnType = ceFrame.mReturnType;
- mCallStack.pop_back();
- }
- break;
- case CeOp_SetRetType:
- {
- int typeId = CE_GETINST(int32);
- returnType = GetBfType(typeId);
- BF_ASSERT(returnType != NULL);
- }
- break;
- case CeOp_Jmp:
- {
- auto relOfs = CE_GETINST(int32);
- instPtr += relOfs;
- }
- break;
- case CeOp_JmpIf:
- {
- auto relOfs = CE_GETINST(int32);
- bool cond = CE_GETFRAME(bool);
- if (cond)
- instPtr += relOfs - 4;
- }
- break;
- case CeOp_JmpIfNot:
- {
- auto relOfs = CE_GETINST(int32);
- bool cond = CE_GETFRAME(bool);
- if (!cond)
- instPtr += relOfs - 4;
- }
- break;
- case CeOp_Error:
- {
- auto errorKind = (CeErrorKind)CE_GETINST(int32);
- switch (errorKind)
- {
- case CeErrorKind_GlobalVariable:
- _Fail("Global variable access not allowed");
- break;
- case CeErrorKind_FunctionPointer:
- _Fail("Function pointer calls not allowed");
- break;
- case CeErrorKind_Intrinsic:
- _Fail("Intrinsic not allowed");
- break;
- case CeErrorKind_ObjectDynCheckFailed:
- _Fail("Dynamic cast check failed");
- break;
- default:
- _Fail("Operation not allowed");
- break;
- }
- }
- break;
- case CeOp_DynamicCastCheck:
- {
- auto& result = CE_GETFRAME(uint32);
- auto valueAddr = CE_GETFRAME(addr_ce);
- int32 ifaceId = CE_GETINST(int32);
- if (valueAddr == 0)
- {
- CeSetAddrVal(&result, 0, ptrSize);
- }
- else
- {
- CE_CHECKADDR(valueAddr, sizeof(int32));
- auto wantType = GetBfType(ifaceId);
- int32 objTypeId = *(int32*)(memStart + valueAddr);
- auto valueType = GetBfType(objTypeId);
- if ((wantType == NULL) || (valueType == NULL))
- {
- _Fail("Invalid type in CeOp_DynamicCastCheck");
- return false;
- }
- bool matches = false;
- if (ceModule->TypeIsSubTypeOf(valueType->ToTypeInstance(), wantType->ToTypeInstance(), false))
- {
- matches = true;
- }
- else if ((valueType->IsDelegate()) && (wantType->IsDelegate()))
- {
- int valueSignatureId = ceModule->GetDelegateSignatureId(valueType->ToTypeInstance());
- int checkSignatureId = ceModule->GetDelegateSignatureId(wantType->ToTypeInstance());
- matches = valueSignatureId == checkSignatureId;
- }
-
- if (matches)
- CeSetAddrVal(&result, valueAddr, ptrSize);
- else
- CeSetAddrVal(&result, 0, ptrSize);
- }
- }
- break;
- case CeOp_GetReflectType:
- {
- auto frameOfs = CE_GETINST(int32);
- int32 typeId = CE_GETINST(int32);
- auto reflectType = GetReflectType(typeId);
- _FixVariables();
- CeSetAddrVal(framePtr + frameOfs, reflectType, ptrSize);
- }
- break;
- case CeOp_GetString:
- {
- auto frameOfs = CE_GETINST(int32);
- auto stringTableIdx = CE_GETINST(int32);
- auto& ceStringEntry = ceFunction->mStringTable[stringTableIdx];
- if (ceStringEntry.mBindExecuteId != mExecuteId)
- {
- ceStringEntry.mStringAddr = GetString(ceStringEntry.mStringId);
- _FixVariables();
- ceStringEntry.mBindExecuteId = mExecuteId;
- }
- CeSetAddrVal(framePtr + frameOfs, ceStringEntry.mStringAddr, ptrSize);
- }
- break;
- case CeOp_Malloc:
- {
- auto frameOfs = CE_GETINST(int32);
- int64 size;
- if (ptrSize == 4)
- size = CE_GETFRAME(int32);
- else
- size = CE_GETFRAME(int64);
- CE_CHECKALLOC(size);
- uint8* mem = CeMalloc(size);
- _FixVariables();
- CeSetAddrVal(framePtr + frameOfs, mem - memStart, ptrSize);
- }
- break;
- case CeOp_Free:
- {
- auto freeAddr = CE_GETFRAME(addr_ce);
- bool success = CeFree(freeAddr);
- if (!success)
- _Fail("Invalid heap address");
- }
- break;
- case CeOp_MemSet:
- {
- auto destAddr = CE_GETFRAME(addr_ce);
- uint8 setValue = CE_GETFRAME(uint8);
- int32 setSize = CE_GETFRAME(int32);
- CE_CHECKSIZE(setSize);
- CE_CHECKADDR(destAddr, setSize);
- memset(memStart + destAddr, setValue, setSize);
- }
- break;
- case CeOp_MemSet_Const:
- {
- auto destAddr = CE_GETFRAME(addr_ce);
- uint8 setValue = CE_GETINST(uint8);
- int32 setSize = CE_GETINST(int32);
- CE_CHECKSIZE(setSize);
- CE_CHECKADDR(destAddr, setSize);
- memset(memStart + destAddr, setValue, setSize);
- }
- break;
- case CeOp_MemCpy:
- {
- auto destAddr = CE_GETFRAME(addr_ce);
- auto srcAddr = CE_GETFRAME(addr_ce);
- int32 size = CE_GETFRAME(int32);
- CE_CHECKSIZE(size);
- CE_CHECKADDR(srcAddr, size);
- CE_CHECKADDR(destAddr, size);
- memmove(memStart + destAddr, memStart + srcAddr, size);
- }
- break;
- case CeOp_FrameAddr_32:
- {
- auto& result = CE_GETFRAME(int32);
- auto addr = &CE_GETFRAME(uint8);
- result = addr - memStart;
- }
- break;
- case CeOp_FrameAddr_64:
- {
- //if (instPtr - ceFunction->mCode.mVals == 0x9c1)
- auto& result = CE_GETFRAME(int64);
- auto addr = &CE_GETFRAME(uint8);
- result = addr - memStart;
- }
- break;
- case CeOp_FrameAddrOfs_32:
- {
- auto& result = CE_GETFRAME(int32);
- auto addr = &CE_GETFRAME(uint8);
- int32 ofs = CE_GETINST(int32);
- result = (int32)(addr - memStart + ofs);
- }
- break;
- case CeOp_ConstData:
- {
- auto frameOfs = CE_GETINST(int32);
- int32 constIdx = CE_GETINST(int32);
- auto& constEntry = ceFunction->mConstStructTable[constIdx];
- if (constEntry.mBindExecuteId != mExecuteId)
- {
- PrepareConstStructEntry(constEntry);
- _FixVariables();
- }
- auto& buff = (constEntry.mFixedData.mSize > 0) ? constEntry.mFixedData : constEntry.mData;
- memcpy(framePtr + frameOfs, buff.mVals, buff.mSize);
- }
- break;
- case CeOp_ConstDataRef:
- {
- auto frameOfs = CE_GETINST(int32);
- int32 constIdx = CE_GETINST(int32);
- auto& constEntry = ceFunction->mConstStructTable[constIdx];
- if (constEntry.mBindExecuteId != mExecuteId)
- {
- PrepareConstStructEntry(constEntry);
- _FixVariables();
- auto& buff = (constEntry.mFixedData.mSize > 0) ? constEntry.mFixedData : constEntry.mData;
- addr_ce* constAddrPtr = NULL;
- if (mConstDataMap.TryAdd(constEntry.mHash, NULL, &constAddrPtr))
- {
- uint8* data = CeMalloc(buff.mSize);
- _FixVariables();
- memcpy(data, &buff[0], buff.mSize);
- *constAddrPtr = (addr_ce)(data - memStart);
- }
- constEntry.mAddr = *constAddrPtr;
- constEntry.mBindExecuteId = mExecuteId;
- }
- *(addr_ce*)(framePtr + frameOfs) = constEntry.mAddr;
- }
- break;
- case CeOp_Zero:
- {
- auto resultPtr = &CE_GETFRAME(uint8);
- int32 constSize = CE_GETINST(int32);
- memset(resultPtr, 0, constSize);
- }
- break;
- case CeOp_Const_8:
- {
- auto& result = CE_GETFRAME(int8);
- result = CE_GETINST(int8);
- }
- break;
- case CeOp_Const_16:
- {
- auto& result = CE_GETFRAME(int16);
- result = CE_GETINST(int16);
- }
- break;
- case CeOp_Const_32:
- {
- auto& result = CE_GETFRAME(int32);
- result = CE_GETINST(int32);
- }
- break;
- case CeOp_Const_64:
- {
- auto& result = CE_GETFRAME(int64);
- result = CE_GETINST(int64);
- }
- break;
- case CeOp_Const_X:
- {
- int32 constSize = CE_GETINST(int32);
- auto resultPtr = &CE_GETFRAME(uint8);
- memcpy(resultPtr, instPtr, constSize);
- instPtr += constSize;
- }
- break;
- case CeOp_Load_8:
- CE_LOAD(uint8);
- break;
- case CeOp_Load_16:
- CE_LOAD(uint16);
- break;
- case CeOp_Load_32:
- CE_LOAD(uint32);
- break;
- case CeOp_Load_64:
- CE_LOAD(uint64);
- break;
- case CeOp_Load_X:
- {
- int32 size = CE_GETINST(int32);
- auto resultPtr = &CE_GETFRAME(uint8);
- auto ceAddr = CE_GETFRAME(addr_ce);
- CE_CHECKADDR(ceAddr, size);
- memcpy(resultPtr, memStart + ceAddr, size);
- }
- break;
- case CeOp_Store_8:
- CE_STORE(uint8);
- break;
- case CeOp_Store_16:
- CE_STORE(uint16);
- break;
- case CeOp_Store_32:
- CE_STORE(uint32);
- break;
- case CeOp_Store_64:
- CE_STORE(uint64);
- break;
- case CeOp_Store_X:
- {
- auto size = CE_GETINST(int32);
- auto srcPtr = &CE_GETFRAME(uint8);
- auto ceAddr = CE_GETFRAME(addr_ce);
- CE_CHECKADDR(ceAddr, size);
- memcpy(memStart + ceAddr, srcPtr, size);
- }
- break;
- case CeOp_Move_8:
- CEOP_MOVE(int8);
- break;
- case CeOp_Move_16:
- CEOP_MOVE(int16);
- break;
- case CeOp_Move_32:
- CEOP_MOVE(int32);
- break;
- case CeOp_Move_64:
- CEOP_MOVE(int64);
- break;
- case CeOp_Move_X:
- {
- int32 size = CE_GETINST(int32);
- auto valPtr = &CE_GETFRAME(uint8);
- auto destPtr = &CE_GETFRAME(uint8);
- memcpy(destPtr, valPtr, size);
- }
- break;
- case CeOp_Push_8:
- CEOP_PUSH(int8);
- break;
- case CeOp_Push_16:
- CEOP_PUSH(int16);
- break;
- case CeOp_Push_32:
- CEOP_PUSH(int32);
- break;
- case CeOp_Push_64:
- CEOP_PUSH(int64);
- break;
- case CeOp_Pop_8:
- CEOP_POP(int8);
- break;
- case CeOp_Pop_16:
- CEOP_POP(int16);
- break;
- case CeOp_Pop_32:
- CEOP_POP(int32);
- break;
- case CeOp_Pop_64:
- CEOP_POP(int64);
- break;
- case CeOp_Pop_X:
- {
- int32 size = CE_GETINST(int32);
- auto resultPtr = &CE_GETFRAME(uint8);
- memcpy(resultPtr, stackPtr, size);
- stackPtr += size;
- }
- break;
- case CeOp_AdjustSP:
- {
- int32 adjust = CE_GETFRAME(int32);
- stackPtr += adjust;
- }
- break;
- case CeOp_AdjustSPNeg:
- {
- int32 adjust = CE_GETFRAME(int32);
- stackPtr -= adjust;
- }
- break;
- case CeOp_AdjustSPConst:
- {
- int32 adjust = CE_GETINST(int32);
- stackPtr += adjust;
- }
- break;
- case CeOp_GetSP:
- {
- auto& result = CE_GETFRAME(int32);
- result = stackPtr - memStart;
- }
- break;
- case CeOp_SetSP:
- {
- auto addr = CE_GETFRAME(int32);
- stackPtr = memStart + addr;
- }
- break;
- case CeOp_GetStaticField:
- case CeOp_GetStaticField_Initializer:
- {
- auto frameOfs = CE_GETINST(int32);
- int32 tableIdx = CE_GETINST(int32);
- int32 initializerFrameOfs = 0;
- if (op == CeOp_GetStaticField_Initializer)
- {
- initializerFrameOfs = CE_GETINST(int32);;
- }
- CeFunction* ctorCallFunction = NULL;
- auto& ceStaticFieldEntry = ceFunction->mStaticFieldTable[tableIdx];
- if (ceStaticFieldEntry.mBindExecuteId != mExecuteId)
- {
- if ((ceStaticFieldEntry.mTypeId > 0) && (mStaticCtorExecSet.TryAdd(ceStaticFieldEntry.mTypeId, NULL)) && (!ceStaticFieldEntry.mName.StartsWith("#")))
- {
- auto bfType = GetBfType(ceStaticFieldEntry.mTypeId);
- BfTypeInstance* bfTypeInstance = NULL;
- if (bfType != NULL)
- bfTypeInstance = bfType->ToTypeInstance();
- if (bfTypeInstance == NULL)
- {
- _Fail("Invalid type");
- return false;
- }
- if (bfType->mDefineState == BfTypeDefineState_CETypeInit)
- {
- // Don't create circular references
- }
- else
- {
- auto methodDef = bfTypeInstance->mTypeDef->GetMethodByName("__BfStaticCtor");
- if (methodDef != NULL)
- {
- auto moduleMethodInstance = ceModule->GetMethodInstance(bfTypeInstance, methodDef, BfTypeVector());
- if (!moduleMethodInstance)
- {
- _Fail("No static ctor instance found");
- return false;
- }
- ceModule->PopulateType(bfTypeInstance, BfPopulateType_DataAndMethods);
- bool added = false;
- ctorCallFunction = mCeMachine->GetFunction(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc, added);
- if (ctorCallFunction->mInitializeState < CeFunction::InitializeState_Initialized)
- mCeMachine->PrepareFunction(ctorCallFunction, NULL);
- }
- }
- }
- CeStaticFieldInfo* staticFieldInfo = NULL;
- mStaticFieldMap.TryAdd(ceStaticFieldEntry.mName, NULL, &staticFieldInfo);
- if (staticFieldInfo->mAddr == 0)
- {
- if (ceStaticFieldEntry.mSize < 0)
- _Fail(StrFormat("Reference to unsized global variable '%s'", ceStaticFieldEntry.mName.c_str()));
- CE_CHECKALLOC(ceStaticFieldEntry.mSize);
- uint8* ptr = CeMalloc(ceStaticFieldEntry.mSize);
- _FixVariables();
- if (ceStaticFieldEntry.mSize > 0)
- memset(ptr, 0, ceStaticFieldEntry.mSize);
- staticFieldInfo->mAddr = (addr_ce)(ptr - memStart);
- if (op == CeOp_GetStaticField_Initializer)
- {
- void* initDataPtr = framePtr + initializerFrameOfs;
- memcpy(ptr, initDataPtr, ceStaticFieldEntry.mSize);
- }
- if (ceStaticFieldEntry.mName.StartsWith("#"))
- {
- addr_ce resultAddr = 0;
- if (ceStaticFieldEntry.mName == "#CallerLineNum")
- {
- *(int*)ptr = mCurModule->mCurFilePosition.mCurLine;
- }
- else if (ceStaticFieldEntry.mName == "#CallerFilePath")
- {
- String filePath;
- if (mCurModule->mCurFilePosition.mFileInstance != NULL)
- filePath = mCurModule->mCurFilePosition.mFileInstance->mParser->mFileName;
- resultAddr = GetString(filePath);
- }
- else if (ceStaticFieldEntry.mName == "#CallerFileName")
- {
- String filePath;
- if (mCurModule->mCurFilePosition.mFileInstance != NULL)
- filePath = mCurModule->mCurFilePosition.mFileInstance->mParser->mFileName;
- resultAddr = GetString(GetFileName(filePath));
- }
- else if (ceStaticFieldEntry.mName == "#CallerFileDir")
- {
- String filePath;
- if (mCurModule->mCurFilePosition.mFileInstance != NULL)
- filePath = mCurModule->mCurFilePosition.mFileInstance->mParser->mFileName;
- resultAddr = GetString(GetFileDir(filePath));
- }
- else if (ceStaticFieldEntry.mName == "#CallerTypeName")
- {
- String typeName = "";
- typeName = mCeMachine->mCeModule->TypeToString(mCallerTypeInstance);
- resultAddr = GetString(typeName);
- }
- else if (ceStaticFieldEntry.mName == "#CallerType")
- {
- addr_ce typeAddr = GetReflectType(mCallerTypeInstance->mTypeId);
- resultAddr = typeAddr;
- }
- else if (ceStaticFieldEntry.mName == "#CallerMemberName")
- {
- String memberName = mCeMachine->mCeModule->MethodToString(mCallerMethodInstance);
- resultAddr = GetString(memberName);
- }
- else if (ceStaticFieldEntry.mName == "#CallerProject")
- {
- BfProject* project = NULL;
- project = mCallerTypeInstance->mTypeDef->mProject;
- if (project != NULL)
- resultAddr = GetString(project->mName);
- }
- else if (ceStaticFieldEntry.mName == "#OrigCalleeType")
- {
- if (mCurCallSource->mOrigCalleeType != NULL)
- {
- addr_ce typeAddr = GetReflectType(mCurCallSource->mOrigCalleeType->mTypeId);
- resultAddr = typeAddr;
- }
- }
- if (resultAddr != 0)
- {
- _FixVariables();
- CeSetAddrVal(memStart + staticFieldInfo->mAddr, resultAddr, ptrSize);
- }
- }
- }
- ceStaticFieldEntry.mAddr = staticFieldInfo->mAddr;
- ceStaticFieldEntry.mBindExecuteId = mExecuteId;
- }
- *(addr_ce*)(framePtr + frameOfs) = ceStaticFieldEntry.mAddr;
- if (ctorCallFunction != NULL)
- {
- bool handled = false;
- if (!_CheckFunction(ctorCallFunction, handled))
- return false;
- if (handled)
- break;
- CE_CALL(ctorCallFunction);
- }
- }
- break;
- case CeOp_GetMethod:
- {
- BF_ASSERT(memStart == mMemory.mVals);
- auto resultFrameIdx = CE_GETINST(int32);
- int32 callIdx = CE_GETINST(int32);
- auto& callEntry = ceFunction->mCallTable[callIdx];
- if (callEntry.mBindRevision != mCeMachine->mMethodBindRevision)
- {
- callEntry.mFunction = NULL;
- //mNamedFunctionMap.TryGetValue(callEntry.mFunctionName, &callEntry.mFunction);
- if (callEntry.mFunctionInfo == NULL)
- {
- _Fail("Unable to locate function entry");
- return false;
- }
- if ((callEntry.mFunctionInfo->mCeFunction == NULL) && (!callEntry.mFunctionInfo->mMethodRef.IsNull()))
- {
- auto methodRef = callEntry.mFunctionInfo->mMethodRef;
- if (methodRef.mMethodNum >= methodRef.mTypeInstance->mTypeDef->mMethods.mSize)
- {
- // Must be a comptime-generated method
- ceModule->PopulateType(methodRef.mTypeInstance);
- }
- if (methodRef.mMethodNum >= methodRef.mTypeInstance->mTypeDef->mMethods.mSize)
- {
- _Fail("OOB method reference");
- return false;
- }
- auto methodDef = methodRef.mTypeInstance->mTypeDef->mMethods[methodRef.mMethodNum];
- auto moduleMethodInstance = ceModule->GetMethodInstance(methodRef.mTypeInstance, methodDef,
- methodRef.mMethodGenericArguments);
- if (moduleMethodInstance)
- {
- auto ceFunction = mCeMachine->QueueMethod(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc);
- if (ceFunction == NULL)
- {
- _Fail("Method generation failed");
- return false;
- }
- ceFunction->mCeFunctionInfo->mRefCount++;
- mCeMachine->DerefMethodInfo(callEntry.mFunctionInfo);
- callEntry.mFunctionInfo = ceFunction->mCeFunctionInfo;
- }
- }
- if (callEntry.mFunctionInfo->mCeFunction == NULL)
- {
- _Fail("Method not generated");
- return false;
- }
- callEntry.mFunction = callEntry.mFunctionInfo->mCeFunction;
- if (callEntry.mFunction->mInitializeState < CeFunction::InitializeState_Initialized)
- {
- auto curFrame = _GetCurFrame();
- SetAndRestoreValue<CeFrame*> prevFrame(mCurFrame, &curFrame);
- BF_ASSERT(callEntry.mFunction->mInitializeState < CeFunction::InitializeState_Initialized);
- mCeMachine->PrepareFunction(callEntry.mFunction, NULL);
- }
- if (callEntry.mFunction->mMethodInstance != NULL)
- {
- if (callEntry.mFunction->mMethodInstance->GetOwner()->IsDeleting())
- {
- _Fail("Calling method on deleted type");
- return false;
- }
- }
- callEntry.mBindRevision = mCeMachine->mMethodBindRevision;
- }
- BF_ASSERT(memStart == mMemory.mVals);
- auto callFunction = callEntry.mFunction;
- if (needsFunctionIds)
- *(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
- else
- *(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
- }
- break;
- case CeOp_GetMethod_Inner:
- {
- auto resultFrameIdx = CE_GETINST(int32);
- int32 innerIdx = CE_GETINST(int32);
- auto outerFunction = ceFunction;
- if (outerFunction->mCeInnerFunctionInfo != NULL)
- outerFunction = outerFunction->mCeInnerFunctionInfo->mOwner;
- auto callFunction = outerFunction->mInnerFunctions[innerIdx];
- if (needsFunctionIds)
- *(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
- else
- *(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
- }
- break;
- case CeOp_GetMethod_Virt:
- {
- auto resultFrameIdx = CE_GETINST(int32);
- auto valueAddr = CE_GETFRAME(addr_ce);
- int32 virtualIdx = CE_GETINST(int32);
- CE_CHECKADDR(valueAddr, sizeof(int32));
- int32 objTypeId = *(int32*)(memStart + valueAddr);
- BfType* bfType = GetBfType(objTypeId);
- if ((bfType == NULL) || (!bfType->IsObject()))
- {
- _Fail("Invalid virtual method target");
- return false;
- }
- auto valueType = bfType->ToTypeInstance();
- if (valueType->mVirtualMethodTable.IsEmpty())
- ceModule->PopulateType(valueType, BfPopulateType_Full_Force);
- if (valueType->mVirtualMethodTable.IsEmpty())
- {
- _Fail("Empty virtual table");
- return false;
- }
- auto& methodRef = valueType->mVirtualMethodTable[virtualIdx].mImplementingMethod;
- if (methodRef.mTypeInstance->mDefineState < BfTypeDefineState_DefinedAndMethodsSlotted)
- ceModule->PopulateType(methodRef.mTypeInstance, BfPopulateType_DataAndMethods);
- auto methodInstance = (BfMethodInstance*)methodRef;
- auto callFunction = mCeMachine->GetPreparedFunction(methodInstance);
- if (needsFunctionIds)
- *(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
- else
- *(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
- }
- break;
- case CeOp_GetMethod_IFace:
- {
- auto resultFrameIdx = CE_GETINST(int32);
- auto valueAddr = CE_GETFRAME(addr_ce);
- int32 ifaceId = CE_GETINST(int32);
- int32 methodIdx = CE_GETINST(int32);
- auto ifaceType = ceModule->mContext->mTypes[ifaceId]->ToTypeInstance();
- CE_CHECKADDR(valueAddr, sizeof(int32));
- int32 objTypeId = *(int32*)(memStart + valueAddr);
- auto bfObjectType = GetBfType(objTypeId);
- if ((bfObjectType == NULL) || (!bfObjectType->IsTypeInstance()))
- {
- _Fail("Invalid object");
- return false;
- }
- auto valueType = bfObjectType->ToTypeInstance();
- BfMethodInstance* methodInstance = NULL;
- if (valueType != NULL)
- {
- if (valueType->mVirtualMethodTable.IsEmpty())
- ceModule->PopulateType(valueType, BfPopulateType_DataAndMethods);
- auto checkType = valueType;
- while (checkType != NULL)
- {
- for (auto& iface : checkType->mInterfaces)
- {
- if (iface.mInterfaceType == ifaceType)
- {
- if (valueType->mInterfaceMethodTable.IsEmpty())
- ceModule->PopulateType(valueType, BfPopulateType_Full_Force);
- if (valueType->mInterfaceMethodTable.IsEmpty())
- {
- _Fail("Empty interface table");
- return false;
- }
- methodInstance = valueType->mInterfaceMethodTable[iface.mStartInterfaceTableIdx + methodIdx].mMethodRef;
- break;
- }
- }
- checkType = checkType->mBaseType;
- }
- }
- if (methodInstance == NULL)
- {
- _Fail("Failed to invoke interface method");
- return false;
- }
- auto callFunction = mCeMachine->GetPreparedFunction(methodInstance);
- if (needsFunctionIds)
- *(int32*)(framePtr + resultFrameIdx) = callFunction->mId;
- else
- *(CeFunction**)(framePtr + resultFrameIdx) = callFunction;
- }
- break;
- case CeOp_Call:
- {
- callCount++;
- CeFunction* callFunction;
- if (needsFunctionIds)
- {
- int32 functionId = CE_GETFRAME(int32);
- callFunction = mCeMachine->mFunctionIdMap[functionId];
- }
- else
- callFunction = CE_GETFRAME(CeFunction*);
- bool handled = false;
- if (!_CheckFunction(callFunction, handled))
- return false;
- if (callFunction->mIsVarReturn)
- _Fail("Illegal call to method with 'var' return.");
- if (handled)
- break;
- CE_CALL(callFunction);
- }
- break;
- case CeOp_Conv_I8_I16:
- CE_CAST(int8, int16);
- break;
- case CeOp_Conv_I8_I32:
- CE_CAST(int8, int32);
- break;
- case CeOp_Conv_I8_I64:
- CE_CAST(int8, int64);
- break;
- case CeOp_Conv_I8_F32:
- CE_CAST(int8, float);
- break;
- case CeOp_Conv_I8_F64:
- CE_CAST(int8, double);
- break;
- case CeOp_Conv_I16_I32:
- CE_CAST(int16, int32);
- break;
- case CeOp_Conv_I16_I64:
- CE_CAST(int16, int64);
- break;
- case CeOp_Conv_I16_F32:
- CE_CAST(int16, float);
- break;
- case CeOp_Conv_I16_F64:
- CE_CAST(int16, double);
- break;
- case CeOp_Conv_I32_I64:
- CE_CAST(int32, int64);
- break;
- case CeOp_Conv_I32_F32:
- CE_CAST(int32, float);
- break;
- case CeOp_Conv_I32_F64:
- CE_CAST(int32, double);
- break;
- case CeOp_Conv_I64_F32:
- CE_CAST(int64, float);
- break;
- case CeOp_Conv_I64_F64:
- CE_CAST(int64, double);
- break;
- case CeOp_Conv_U8_U16:
- CE_CAST(uint8, uint16);
- break;
- case CeOp_Conv_U8_U32:
- CE_CAST(uint8, uint32);
- break;
- case CeOp_Conv_U8_U64:
- CE_CAST(uint8, uint64);
- break;
- case CeOp_Conv_U8_F32:
- CE_CAST(uint8, float);
- break;
- case CeOp_Conv_U8_F64:
- CE_CAST(uint8, double);
- break;
- case CeOp_Conv_U16_U32:
- CE_CAST(uint16, uint32);
- break;
- case CeOp_Conv_U16_U64:
- CE_CAST(uint16, uint64);
- break;
- case CeOp_Conv_U16_F32:
- CE_CAST(uint16, float);
- break;
- case CeOp_Conv_U16_F64:
- CE_CAST(uint16, double);
- break;
- case CeOp_Conv_U32_U64:
- CE_CAST(uint32, uint64);
- break;
- case CeOp_Conv_U32_F32:
- CE_CAST(uint32, float);
- break;
- case CeOp_Conv_U32_F64:
- CE_CAST(uint32, double);
- break;
- case CeOp_Conv_U64_F32:
- CE_CAST(uint64, float);
- break;
- case CeOp_Conv_U64_F64:
- CE_CAST(uint64, double);
- break;
- case CeOp_Conv_F32_I8:
- CE_CAST(float, int8);
- break;
- case CeOp_Conv_F32_I16:
- CE_CAST(float, int16);
- break;
- case CeOp_Conv_F32_I32:
- CE_CAST(float, int32);
- break;
- case CeOp_Conv_F32_I64:
- CE_CAST(float, int64);
- break;
- case CeOp_Conv_F32_F64:
- CE_CAST(float, double);
- break;
- case CeOp_Conv_F64_I8:
- CE_CAST(double, int8);
- break;
- case CeOp_Conv_F64_I16:
- CE_CAST(double, int16);
- break;
- case CeOp_Conv_F64_I32:
- CE_CAST(double, int32);
- break;
- case CeOp_Conv_F64_I64:
- CE_CAST(double, int64);
- break;
- case CeOp_Conv_F64_F32:
- CE_CAST(double, float);
- break;
- case CeOp_Conv_F32_U8:
- CE_CAST(float, uint8);
- break;
- case CeOp_Conv_F32_U16:
- CE_CAST(float, uint16);
- break;
- case CeOp_Conv_F32_U32:
- CE_CAST(float, uint32);
- break;
- case CeOp_Conv_F32_U64:
- CE_CAST(float, uint64);
- break;
- case CeOp_Conv_F64_U8:
- CE_CAST(float, uint8);
- break;
- case CeOp_Conv_F64_U16:
- CE_CAST(float, uint16);
- break;
- case CeOp_Conv_F64_U32:
- CE_CAST(float, uint32);
- break;
- case CeOp_Conv_F64_U64:
- CE_CAST(float, uint64);
- break;
- case CeOp_Abs_I8:
- {
- auto& result = CE_GETFRAME(int8);
- auto val = CE_GETFRAME(int8);
- result = (val < 0) ? -val : val;
- }
- break;
- case CeOp_Abs_I16:
- {
- auto& result = CE_GETFRAME(int16);
- auto val = CE_GETFRAME(int16);
- result = (val < 0) ? -val : val;
- }
- break;
- case CeOp_Abs_I32:
- {
- auto& result = CE_GETFRAME(int32);
- auto val = CE_GETFRAME(int32);
- result = (val < 0) ? -val : val;
- }
- break;
- case CeOp_Abs_I64:
- {
- auto& result = CE_GETFRAME(int64);
- auto val = CE_GETFRAME(int64);
- result = (val < 0) ? -val : val;
- }
- break;
- case CeOp_Abs_F32:
- CEOP_UNARY_FUNC(fabs, float);
- break;
- case CeOp_Abs_F64:
- CEOP_UNARY_FUNC(fabs, double);
- break;
- case CeOp_AddConst_I8:
- CEOP_BIN_CONST(+, int8);
- break;
- case CeOp_AddConst_I16:
- CEOP_BIN_CONST(+, int16);
- break;
- case CeOp_AddConst_I32:
- CEOP_BIN_CONST(+, int32);
- break;
- case CeOp_AddConst_I64:
- CEOP_BIN_CONST(+, int64);
- break;
- case CeOp_Add_I8:
- CEOP_BIN(+, int8);
- break;
- case CeOp_Add_I16:
- CEOP_BIN(+, int16);
- break;
- case CeOp_Add_I32:
- CEOP_BIN(+, int32);
- break;
- case CeOp_Add_I64:
- CEOP_BIN(+, int64);
- break;
- case CeOp_Add_F32:
- CEOP_BIN(+, float);
- break;
- case CeOp_Add_F64:
- CEOP_BIN(+, double);
- break;
- case CeOp_Sub_I8:
- CEOP_BIN(-, int8);
- break;
- case CeOp_Sub_I16:
- CEOP_BIN(-, int16);
- break;
- case CeOp_Sub_I32:
- CEOP_BIN(-, int32);
- break;
- case CeOp_Sub_I64:
- CEOP_BIN(-, int64);
- break;
- case CeOp_Sub_F32:
- CEOP_BIN(-, float);
- break;
- case CeOp_Sub_F64:
- CEOP_BIN(-, double);
- break;
- case CeOp_Mul_I8:
- CEOP_BIN(*, int8);
- break;
- case CeOp_Mul_I16:
- CEOP_BIN(*, int16);
- break;
- case CeOp_Mul_I32:
- CEOP_BIN(*, int32);
- break;
- case CeOp_Mul_I64:
- CEOP_BIN(*, int64);
- break;
- case CeOp_Mul_F32:
- CEOP_BIN(*, float);
- break;
- case CeOp_Mul_F64:
- CEOP_BIN(*, double);
- break;
- case CeOp_Div_I8:
- CEOP_BIN_DIV(/ , int8);
- break;
- case CeOp_Div_I16:
- CEOP_BIN_DIV(/ , int16);
- break;
- case CeOp_Div_I32:
- CEOP_BIN_DIV(/ , int32);
- break;
- case CeOp_Div_I64:
- CEOP_BIN_DIV(/ , int64);
- break;
- case CeOp_Div_F32:
- CEOP_BIN_DIV(/ , float);
- break;
- case CeOp_Div_F64:
- CEOP_BIN_DIV(/ , double);
- break;
- case CeOp_Div_U8:
- CEOP_BIN_DIV(/ , uint8);
- break;
- case CeOp_Div_U16:
- CEOP_BIN_DIV(/ , uint16);
- break;
- case CeOp_Div_U32:
- CEOP_BIN_DIV(/ , uint32);
- break;
- case CeOp_Div_U64:
- CEOP_BIN_DIV(/ , uint64);
- break;
- case CeOp_Mod_I8:
- CEOP_BIN_DIV(%, int8);
- break;
- case CeOp_Mod_I16:
- CEOP_BIN_DIV(%, int16);
- break;
- case CeOp_Mod_I32:
- CEOP_BIN_DIV(%, int32);
- break;
- case CeOp_Mod_I64:
- CEOP_BIN_DIV(%, int64);
- break;
- case CeOp_Mod_F32:
- {
- auto& result = CE_GETFRAME(float);
- auto lhs = CE_GETFRAME(float);
- auto rhs = CE_GETFRAME(float);
- if (rhs == 0)
- {
- _Fail("Division by zero");
- return false;
- }
- result = fmodf(lhs, rhs);
- }
- break;
- case CeOp_Mod_F64:
- {
- auto& result = CE_GETFRAME(double);
- auto lhs = CE_GETFRAME(double);
- auto rhs = CE_GETFRAME(double);
- if (rhs == 0)
- {
- _Fail("Division by zero");
- return false;
- }
- result = fmod(lhs, rhs);
- }
- break;
- case CeOp_Mod_U8:
- CEOP_BIN_DIV(%, uint8);
- break;
- case CeOp_Mod_U16:
- CEOP_BIN_DIV(%, uint16);
- break;
- case CeOp_Mod_U32:
- CEOP_BIN_DIV(%, uint32);
- break;
- case CeOp_Mod_U64:
- CEOP_BIN_DIV(%, uint64);
- break;
- case CeOp_And_I8:
- CEOP_BIN(&, uint8);
- break;
- case CeOp_And_I16:
- CEOP_BIN(&, uint16);
- break;
- case CeOp_And_I32:
- CEOP_BIN(&, uint32);
- break;
- case CeOp_And_I64:
- CEOP_BIN(&, uint64);
- break;
- case CeOp_Or_I8:
- CEOP_BIN(| , uint8);
- break;
- case CeOp_Or_I16:
- CEOP_BIN(| , uint16);
- break;
- case CeOp_Or_I32:
- CEOP_BIN(| , uint32);
- break;
- case CeOp_Or_I64:
- CEOP_BIN(| , uint64);
- break;
- case CeOp_Xor_I8:
- CEOP_BIN(^, uint8);
- break;
- case CeOp_Xor_I16:
- CEOP_BIN(^, uint16);
- break;
- case CeOp_Xor_I32:
- CEOP_BIN(^, uint32);
- break;
- case CeOp_Xor_I64:
- CEOP_BIN(^, uint64);
- break;
- case CeOp_Shl_I8:
- CEOP_BIN2(<< , int8, uint8);
- break;
- case CeOp_Shl_I16:
- CEOP_BIN2(<< , int16, uint8);
- break;
- case CeOp_Shl_I32:
- CEOP_BIN2(<< , int32, uint8);
- break;
- case CeOp_Shl_I64:
- CEOP_BIN2(<< , int64, uint8);
- break;
- case CeOp_Shr_I8:
- CEOP_BIN2(>> , int8, uint8);
- break;
- case CeOp_Shr_I16:
- CEOP_BIN2(>> , int16, uint8);
- break;
- case CeOp_Shr_I32:
- CEOP_BIN2(>> , int32, uint8);
- break;
- case CeOp_Shr_I64:
- CEOP_BIN2(>> , int64, uint8);
- break;
- case CeOp_Shr_U8:
- CEOP_BIN2(>> , uint8, uint8);
- break;
- case CeOp_Shr_U16:
- CEOP_BIN2(>> , uint16, uint8);
- break;
- case CeOp_Shr_U32:
- CEOP_BIN2(>> , uint32, uint8);
- break;
- case CeOp_Shr_U64:
- CEOP_BIN2(>> , uint64, uint8);
- break;
- case CeOp_Acos_F32:
- CEOP_UNARY_FUNC(acosf, float);
- break;
- case CeOp_Acos_F64:
- CEOP_UNARY_FUNC(acos, double);
- break;
- case CeOp_Asin_F32:
- CEOP_UNARY_FUNC(asinf, float);
- break;
- case CeOp_Asin_F64:
- CEOP_UNARY_FUNC(asin, double);
- break;
- case CeOp_Atan_F32:
- CEOP_UNARY_FUNC(atanf, float);
- break;
- case CeOp_Atan_F64:
- CEOP_UNARY_FUNC(atan, double);
- break;
- case CeOp_Atan2_F32:
- CEOP_BIN_FUNC(atan2f, float);
- break;
- case CeOp_Atan2_F64:
- CEOP_BIN_FUNC(atan2, double);
- break;
- case CeOp_Ceiling_F32:
- CEOP_UNARY_FUNC(ceilf, float);
- break;
- case CeOp_Ceiling_F64:
- CEOP_UNARY_FUNC(ceil, double);
- break;
- case CeOp_Cos_F32:
- CEOP_UNARY_FUNC(cosf, float);
- break;
- case CeOp_Cos_F64:
- CEOP_UNARY_FUNC(cos, double);
- break;
- case CeOp_Cosh_F32:
- CEOP_UNARY_FUNC(coshf, float);
- break;
- case CeOp_Cosh_F64:
- CEOP_UNARY_FUNC(cosh, double);
- break;
- case CeOp_Exp_F32:
- CEOP_UNARY_FUNC(expf, float);
- break;
- case CeOp_Exp_F64:
- CEOP_UNARY_FUNC(exp, double);
- break;
- case CeOp_Floor_F32:
- CEOP_UNARY_FUNC(floorf, float);
- break;
- case CeOp_Floor_F64:
- CEOP_UNARY_FUNC(floor, double);
- break;
- case CeOp_Log_F32:
- CEOP_UNARY_FUNC(logf, float);
- break;
- case CeOp_Log_F64:
- CEOP_UNARY_FUNC(log, double);
- break;
- case CeOp_Log10_F32:
- CEOP_UNARY_FUNC(log10f, float);
- break;
- case CeOp_Log10_F64:
- CEOP_UNARY_FUNC(log10, double);
- break;
- case CeOp_Pow_F32:
- CEOP_BIN_FUNC(powf, float);
- break;
- case CeOp_Pow_F64:
- CEOP_BIN_FUNC(pow, double);
- break;
- case CeOp_Round_F32:
- CEOP_UNARY_FUNC(roundf, float);
- break;
- case CeOp_Round_F64:
- CEOP_UNARY_FUNC(round, double);
- break;
- case CeOp_Sin_F32:
- CEOP_UNARY_FUNC(sinf, float);
- break;
- case CeOp_Sin_F64:
- CEOP_UNARY_FUNC(sin, double);
- break;
- case CeOp_Sinh_F32:
- CEOP_UNARY_FUNC(sinhf, float);
- break;
- case CeOp_Sinh_F64:
- CEOP_UNARY_FUNC(sinh, double);
- break;
- case CeOp_Sqrt_F32:
- CEOP_UNARY_FUNC(sqrtf, float);
- break;
- case CeOp_Sqrt_F64:
- CEOP_UNARY_FUNC(sqrt, double);
- break;
- case CeOp_Tan_F32:
- CEOP_UNARY_FUNC(tanf, float);
- break;
- case CeOp_Tan_F64:
- CEOP_UNARY_FUNC(tan, double);
- break;
- case CeOp_Tanh_F32:
- CEOP_UNARY_FUNC(tanhf, float);
- break;
- case CeOp_Tanh_F64:
- CEOP_UNARY_FUNC(tanh, double);
- break;
- case CeOp_Cmp_NE_I8:
- CEOP_CMP(!= , int8);
- break;
- case CeOp_Cmp_NE_I16:
- CEOP_CMP(!= , int16);
- break;
- case CeOp_Cmp_NE_I32:
- CEOP_CMP(!= , int32);
- break;
- case CeOp_Cmp_NE_I64:
- CEOP_CMP(!= , int64);
- break;
- case CeOp_Cmp_NE_F32:
- CEOP_CMP(!= , float);
- break;
- case CeOp_Cmp_NE_F64:
- CEOP_CMP(!= , double);
- break;
- case CeOp_Cmp_EQ_I8:
- CEOP_CMP(== , int8);
- break;
- case CeOp_Cmp_EQ_I16:
- CEOP_CMP(== , int16);
- break;
- case CeOp_Cmp_EQ_I32:
- CEOP_CMP(== , int32);
- break;
- case CeOp_Cmp_EQ_I64:
- CEOP_CMP(== , int64);
- break;
- case CeOp_Cmp_EQ_F32:
- CEOP_CMP(== , float);
- break;
- case CeOp_Cmp_EQ_F64:
- CEOP_CMP(== , double);
- break;
- case CeOp_Cmp_SLT_I8:
- CEOP_CMP(< , int8);
- break;
- case CeOp_Cmp_SLT_I16:
- CEOP_CMP(< , int16);
- break;
- case CeOp_Cmp_SLT_I32:
- CEOP_CMP(< , int32);
- break;
- case CeOp_Cmp_SLT_I64:
- CEOP_CMP(< , int64);
- break;
- case CeOp_Cmp_SLT_F32:
- CEOP_CMP(< , float);
- break;
- case CeOp_Cmp_SLT_F64:
- CEOP_CMP(< , double);
- break;
- case CeOp_Cmp_ULT_I8:
- CEOP_CMP(< , uint8);
- break;
- case CeOp_Cmp_ULT_I16:
- CEOP_CMP(< , uint16);
- break;
- case CeOp_Cmp_ULT_I32:
- CEOP_CMP(< , uint32);
- break;
- case CeOp_Cmp_ULT_I64:
- CEOP_CMP(< , uint64);
- break;
- case CeOp_Cmp_SLE_I8:
- CEOP_CMP(<= , int8);
- break;
- case CeOp_Cmp_SLE_I16:
- CEOP_CMP(<= , int16);
- break;
- case CeOp_Cmp_SLE_I32:
- CEOP_CMP(<= , int32);
- break;
- case CeOp_Cmp_SLE_I64:
- CEOP_CMP(<= , int64);
- break;
- case CeOp_Cmp_SLE_F32:
- CEOP_CMP(<= , float);
- break;
- case CeOp_Cmp_SLE_F64:
- CEOP_CMP(<= , double);
- break;
- case CeOp_Cmp_ULE_I8:
- CEOP_CMP(<= , uint8);
- break;
- case CeOp_Cmp_ULE_I16:
- CEOP_CMP(<= , uint16);
- break;
- case CeOp_Cmp_ULE_I32:
- CEOP_CMP(<= , uint32);
- break;
- case CeOp_Cmp_ULE_I64:
- CEOP_CMP(<= , uint64);
- break;
- case CeOp_Cmp_SGT_I8:
- CEOP_CMP(> , int8);
- break;
- case CeOp_Cmp_SGT_I16:
- CEOP_CMP(> , int16);
- break;
- case CeOp_Cmp_SGT_I32:
- CEOP_CMP(> , int32);
- break;
- case CeOp_Cmp_SGT_I64:
- CEOP_CMP(> , int64);
- break;
- case CeOp_Cmp_SGT_F32:
- CEOP_CMP(> , float);
- break;
- case CeOp_Cmp_SGT_F64:
- CEOP_CMP(> , double);
- break;
- case CeOp_Cmp_UGT_I8:
- CEOP_CMP(> , uint8);
- break;
- case CeOp_Cmp_UGT_I16:
- CEOP_CMP(> , uint16);
- break;
- case CeOp_Cmp_UGT_I32:
- CEOP_CMP(> , uint32);
- break;
- case CeOp_Cmp_UGT_I64:
- CEOP_CMP(> , uint64);
- break;
- case CeOp_Cmp_SGE_I8:
- CEOP_CMP(>= , int8);
- break;
- case CeOp_Cmp_SGE_I16:
- CEOP_CMP(>= , int16);
- break;
- case CeOp_Cmp_SGE_I32:
- CEOP_CMP(>= , int32);
- break;
- case CeOp_Cmp_SGE_I64:
- CEOP_CMP(>= , int64);
- break;
- case CeOp_Cmp_SGE_F32:
- CEOP_CMP(>= , float);
- break;
- case CeOp_Cmp_SGE_F64:
- CEOP_CMP(>= , double);
- break;
- case CeOp_Cmp_UGE_I8:
- CEOP_CMP(>= , uint8);
- break;
- case CeOp_Cmp_UGE_I16:
- CEOP_CMP(>= , uint16);
- break;
- case CeOp_Cmp_UGE_I32:
- CEOP_CMP(>= , uint32);
- break;
- case CeOp_Cmp_UGE_I64:
- CEOP_CMP(>= , uint64);
- break;
- case CeOp_Neg_I8:
- CEOP_UNARY(-, int8);
- break;
- case CeOp_Neg_I16:
- CEOP_UNARY(-, int16);
- break;
- case CeOp_Neg_I32:
- CEOP_UNARY(-, int32);
- break;
- case CeOp_Neg_I64:
- CEOP_UNARY(-, int64);
- break;
- case CeOp_Neg_F32:
- CEOP_UNARY(-, float);
- break;
- case CeOp_Neg_F64:
- CEOP_UNARY(-, double);
- break;
- case CeOp_Not_I1:
- CEOP_UNARY(!, bool);
- break;
- case CeOp_Not_I8:
- CEOP_UNARY(~, int8);
- break;
- case CeOp_Not_I16:
- CEOP_UNARY(~, int16);
- break;
- case CeOp_Not_I32:
- CEOP_UNARY(~, int32);
- break;
- case CeOp_Not_I64:
- CEOP_UNARY(~, int64);
- break;
- default:
- _Fail("Unhandled op");
- return false;
- }
- //BF_ASSERT(_CrtCheckMemory() != 0);
- }
- return true;
- }
- //////////////////////////////////////////////////////////////////////////
- CeMachine::CeMachine(BfCompiler* compiler)
- {
- mCompiler = compiler;
- mCeModule = NULL;
- mRevision = 0;
- mMethodBindRevision = 0;
- mCurContext = NULL;
- mCurCallSource = NULL;
- mExecuteId = -1;
- mCurRecursiveDepth = 0;
- mCurFunctionId = 0;
- mRevisionExecuteTime = 0;
- mCurBuilder = NULL;
- mPreparingFunction = NULL;
- mCurEmitContext = NULL;
- mAppendAllocInfo = NULL;
- mTempParser = NULL;
- mTempReducer = NULL;
- mTempPassInstance = NULL;
- mDebugger = NULL;
- mDbgPaused = false;
- mDbgWantBreak = false;
- mSpecialCheck = false;
- BfLogSys(mCompiler->mSystem, "CeMachine::CeMachine %p\n", this);
- }
- CeMachine::~CeMachine()
- {
- BF_ASSERT(mDebugger == NULL);
- for (auto context : mContextList)
- delete context;
- delete mTempPassInstance;
- delete mTempParser;
- delete mTempReducer;
- delete mAppendAllocInfo;
- delete mCeModule;
- auto _RemoveFunctionInfo = [&](CeFunctionInfo* functionInfo)
- {
- if (functionInfo->mMethodInstance != NULL)
- functionInfo->mMethodInstance->mInCEMachine = false;
- if (functionInfo->mCeFunction != NULL)
- {
- // We don't need to actually unmap it at this point
- functionInfo->mCeFunction->mId = -1;
- for (auto innerFunction : functionInfo->mCeFunction->mInnerFunctions)
- innerFunction->mId = -1;
- }
- delete functionInfo;
- };
- for (auto kv : mNamedFunctionMap)
- {
- if (kv.mValue->mMethodInstance == NULL)
- _RemoveFunctionInfo(kv.mValue);
- }
- for (auto kv : mFunctions)
- {
- BF_ASSERT(kv.mValue != NULL);
- _RemoveFunctionInfo(kv.mValue);
- }
- }
- void CeMachine::Fail(const StringImpl& error)
- {
- if (mFailString.IsEmpty())
- mFailString = error;
- }
- bool CeMachine::HasFailed()
- {
- return !mFailString.IsEmpty();
- }
- void CeMachine::Init()
- {
- BF_ASSERT(mCeModule == NULL);
- mCeModule = new BfModule(mCompiler->mContext, "__constEval");
- mCeModule->mIsSpecialModule = true;
- //mCeModule->mIsScratchModule = true;
- mCeModule->mIsComptimeModule = true;
- //mCeModule->mIsReified = true;
- if (mCompiler->mIsResolveOnly)
- mCeModule->mIsReified = true;
- else
- mCeModule->mIsReified = false;
- mCeModule->Init();
- BF_ASSERT(mCeModule->mBfIRBuilder == NULL);
- mCeModule->mBfIRBuilder = new BfIRBuilder(mCeModule);
- mCeModule->mBfIRBuilder->mDbgVerifyCodeGen = true;
- mCeModule->FinishInit();
- mCeModule->mBfIRBuilder->mHasDebugInfo = true;
- mCeModule->mHasFullDebugInfo = mDebugger != NULL;
- mCeModule->mBfIRBuilder->mIgnoreWrites = false;
- mCeModule->mWantsIRIgnoreWrites = false;
- }
- BeContext* CeMachine::GetBeContext()
- {
- if (mCeModule == NULL)
- return NULL;
- return mCeModule->mBfIRBuilder->mBeIRCodeGen->mBeContext;
- }
- BeModule* CeMachine::GetBeModule()
- {
- if (mCeModule == NULL)
- return NULL;
- return mCeModule->mBfIRBuilder->mBeIRCodeGen->mBeModule;
- }
- void CeMachine::CompileStarted()
- {
- mRevisionExecuteTime = 0;
- mSpecialCheck = false;
- mRevision++;
- mMethodBindRevision++;
- mDbgWantBreak = false;
- mFailString.Clear();
- if (mCeModule != NULL)
- {
- delete mCeModule;
- mCeModule = NULL;
- }
- }
- void CeMachine::CompileDone()
- {
- // So things like deleted local methods get rechecked
- mRevision++;
- mMethodBindRevision++;
- mTypeInfoMap.Clear();
- mMethodInstanceSet.Clear();
- }
- #define CE_SIZE_GET(T) *((T*)(ptr += sizeof(T)) - 1)
- int CeMachine::GetInstSize(CeFunction* ceFunction, int instIdx)
- {
- auto ptr = &ceFunction->mCode[instIdx];
- auto startPtr = ptr;
- auto _HandleOperand = [&](CeOperandInfoKind kind)
- {
- switch (kind)
- {
- case CEOI_FrameRef:
- case CEOI_FrameRef8:
- case CEOI_FrameRef16:
- case CEOI_FrameRef32:
- case CEOI_FrameRef64:
- case CEOI_FrameRefF32:
- case CEOI_FrameRefF64:
- ptr += 4;
- break;
- case CEOI_IMM8:
- ptr += 1;
- break;
- case CEOI_IMM16:
- ptr += 2;
- break;
- case CEOI_IMM32:
- ptr += 4;
- break;
- case CEOI_IMM64:
- ptr += 8;
- break;
- case CEOI_IMM_VAR:
- {
- int32 size = CE_SIZE_GET(int32);
- ptr += size;
- }
- break;
- case CEOI_JMPREL:
- ptr += 4;
- break;
- default:
- BF_ASSERT("Unhandled");
- }
- };
- auto op = CE_SIZE_GET(CeOp);
- if (op == CeOp_DbgBreak)
- {
- CeBreakpointBind* breakpointEntry = NULL;
- if (ceFunction->mBreakpoints.TryGetValue(instIdx, &breakpointEntry))
- op = breakpointEntry->mPrevOpCode;
- }
- CeOpInfo& opInfo = gOpInfo[op];
- _HandleOperand(opInfo.mResultKind);
- if ((opInfo.mFlags & CeOpInfoFlag_SizeX) != 0)
- ptr += 4;
- _HandleOperand(opInfo.mOperandA);
- _HandleOperand(opInfo.mOperandB);
- _HandleOperand(opInfo.mOperandC);
- return (int)(ptr - startPtr);
- }
- void CeMachine::DerefMethodInfo(CeFunctionInfo* ceFunctionInfo)
- {
- ceFunctionInfo->mRefCount--;
- if (ceFunctionInfo->mRefCount > 0)
- return;
- BF_ASSERT(ceFunctionInfo->mMethodInstance == NULL);
- if (!ceFunctionInfo->mName.IsEmpty())
- {
- auto itr = mNamedFunctionMap.Find(ceFunctionInfo->mName);
- if (itr->mValue == ceFunctionInfo)
- mNamedFunctionMap.Remove(itr);
- }
- delete ceFunctionInfo;
- }
- void CeMachine::RemoveFunc(CeFunction* ceFunction)
- {
- mFunctionIdMap.Remove(ceFunction->mId);
- ceFunction->mId = -1;
- }
- void CeMachine::RemoveMethod(BfMethodInstance* methodInstance)
- {
- BfLogSys(methodInstance->GetOwner()->mModule->mSystem, "CeMachine::RemoveMethod %p\n", methodInstance);
- mMethodBindRevision++;
- auto itr = mFunctions.Find(methodInstance);
- auto ceFunctionInfo = itr->mValue;
- BF_ASSERT(itr != mFunctions.end());
- if (itr != mFunctions.end())
- {
- if (ceFunctionInfo->mMethodInstance == methodInstance)
- {
- auto ceFunction = ceFunctionInfo->mCeFunction;
- for (auto& callEntry : ceFunction->mCallTable)
- {
- if (callEntry.mFunctionInfo != NULL)
- DerefMethodInfo(callEntry.mFunctionInfo);
- }
- if (ceFunction->mId != -1)
- {
- RemoveFunc(ceFunction);
- for (auto innerFunction : ceFunction->mInnerFunctions)
- RemoveFunc(innerFunction);
- }
- delete ceFunction;
- ceFunctionInfo->mCeFunction = NULL;
- ceFunctionInfo->mMethodInstance = NULL;
- if (methodInstance->mMethodDef->mIsLocalMethod)
- {
- // We can't rebuild these anyway
- }
- else if (ceFunctionInfo->mRefCount > 1)
- {
- // Generate a methodref
- ceFunctionInfo->mMethodRef = methodInstance;
- }
- DerefMethodInfo(ceFunctionInfo);
- }
- mFunctions.Remove(itr);
- }
- methodInstance->mInCEMachine = false;
- }
- CeErrorKind CeMachine::WriteConstant(CeConstStructData& data, BeConstant* constVal, CeContext* ceContext, CeBuilder* ceBuilder)
- {
- auto ceModule = mCeModule;
- auto beType = constVal->GetType();
- if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constVal))
- {
- if (globalVar->mName.StartsWith("__bfStrObj"))
- {
- int stringId = atoi(globalVar->mName.c_str() + 10);
- addr_ce stringAddr;
- if (data.mQueueFixups)
- {
- stringAddr = 0;
- CeConstStructFixup fixup;
- fixup.mKind = CeConstStructFixup::Kind_StringPtr;
- fixup.mValue = stringId;
- fixup.mOffset = (int)data.mData.mSize;
- data.mFixups.Add(fixup);
- }
- else
- {
- stringAddr = ceContext->GetString(stringId);
- }
- auto ptr = data.mData.GrowUninitialized(ceModule->mSystem->mPtrSize);
- int64 addr64 = stringAddr;
- memcpy(ptr, &addr64, ceModule->mSystem->mPtrSize);
- return CeErrorKind_None;
- }
- if (globalVar->mInitializer == NULL)
- {
- auto ptr = data.mData.GrowUninitialized(ceModule->mSystem->mPtrSize);
- int64 addr64 = (addr_ce)0;
- memcpy(ptr, &addr64, ceModule->mSystem->mPtrSize);
- return CeErrorKind_None;
- //TODO: Add this global variable in there and fixup
- // CeStaticFieldInfo* staticFieldInfoPtr = NULL;
- // if (mCeMachine->mStaticFieldMap.TryGetValue(globalVar->mName, &staticFieldInfoPtr))
- // {
- // CeStaticFieldInfo* staticFieldInfo = staticFieldInfoPtr;
- //
- // int* staticFieldTableIdxPtr = NULL;
- // if (mStaticFieldMap.TryAdd(globalVar, NULL, &staticFieldTableIdxPtr))
- // {
- // CeStaticFieldEntry staticFieldEntry;
- // staticFieldEntry.mTypeId = staticFieldInfo->mFieldInstance->mOwner->mTypeId;
- // staticFieldEntry.mName = globalVar->mName;
- // staticFieldEntry.mSize = globalVar->mType->mSize;
- // *staticFieldTableIdxPtr = (int)mCeFunction->mStaticFieldTable.size();
- // mCeFunction->mStaticFieldTable.Add(staticFieldEntry);
- // }
- //
- // auto result = FrameAlloc(mCeMachine->GetBeContext()->GetPointerTo(globalVar->mType));
- //
- // Emit(CeOp_GetStaticField);
- // EmitFrameOffset(result);
- // Emit((int32)*staticFieldTableIdxPtr);
- //
- // return result;
- // }
- // return CeErrorKind_GlobalVariable;
- }
- BF_ASSERT(!data.mQueueFixups);
- CeConstStructData gvData;
- auto result = WriteConstant(gvData, globalVar->mInitializer, ceContext, NULL);
- if (result != CeErrorKind_None)
- return result;
- uint8* gvPtr = ceContext->CeMalloc(gvData.mData.mSize);
- memcpy(gvPtr, gvData.mData.mVals, gvData.mData.mSize);
- auto ptr = data.mData.GrowUninitialized(ceModule->mSystem->mPtrSize);
- int64 addr64 = (addr_ce)(gvPtr - ceContext->mMemory.mVals);
- memcpy(ptr, &addr64, mCeModule->mSystem->mPtrSize);
- return CeErrorKind_None;
- }
- else if (auto beTypeOfConst = BeValueDynCast<BeTypeOfConstant>(constVal))
- {
- if (ceBuilder != NULL)
- {
- // Fake it to not break debugging
- auto ptr = data.mData.GrowUninitialized(ceModule->mSystem->mPtrSize);
- int64 addr64 = beTypeOfConst->mBfTypeId;
- memcpy(ptr, &addr64, ceModule->mSystem->mPtrSize);
- }
- return CeErrorKind_None;
- return CeErrorKind_Error;
- }
- else if (auto beFunc = BeValueDynCast<BeFunction>(constVal))
- {
- return CeErrorKind_FunctionPointer;
- }
- else if (auto constStruct = BeValueDynCast<BeStructConstant>(constVal))
- {
- int startOfs = data.mData.mSize;
- if (constStruct->mType->mTypeCode == BeTypeCode_Struct)
- {
- BeStructType* structType = (BeStructType*)constStruct->mType;
- BF_ASSERT(structType->mMembers.size() == constStruct->mMemberValues.size());
- for (int memberIdx = 0; memberIdx < (int)constStruct->mMemberValues.size(); memberIdx++)
- {
- auto& member = structType->mMembers[memberIdx];
- // Do any per-member alignment
- int wantZeroes = member.mByteOffset - (data.mData.mSize - startOfs);
- if (wantZeroes > 0)
- data.mData.Insert(data.mData.size(), (uint8)0, wantZeroes);
- auto result = WriteConstant(data, constStruct->mMemberValues[memberIdx], ceContext, ceBuilder);
- if (result != CeErrorKind_None)
- return result;
- }
- // Do end padding
- data.mData.Insert(data.mData.size(), (uint8)0, structType->mSize - (data.mData.mSize - startOfs));
- }
- else if (constStruct->mType->mTypeCode == BeTypeCode_SizedArray)
- {
- for (auto& memberVal : constStruct->mMemberValues)
- {
- auto result = WriteConstant(data, memberVal, ceContext, ceBuilder);
- if (result != CeErrorKind_None)
- return result;
- }
- }
- else
- BF_FATAL("Invalid StructConst type");
- }
- else if (auto constStr = BeValueDynCast<BeStringConstant>(constVal))
- {
- data.mData.Insert(data.mData.mSize, (uint8*)constStr->mString.c_str(), (int)constStr->mString.length() + 1);
- }
- else if (auto constCast = BeValueDynCast<BeCastConstant>(constVal))
- {
- auto result = WriteConstant(data, constCast->mTarget, ceContext, ceBuilder);
- if (result != CeErrorKind_None)
- return result;
- }
- else if (auto constGep = BeValueDynCast<BeGEP2Constant>(constVal))
- {
- if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constGep->mTarget))
- {
- BF_ASSERT(constGep->mIdx0 == 0);
- int64 dataOfs = 0;
- if (globalVar->mType->mTypeCode == BeTypeCode_Struct)
- {
- auto structType = (BeStructType*)globalVar->mType;
- dataOfs = structType->mMembers[constGep->mIdx1].mByteOffset;
- }
- else if (globalVar->mType->mTypeCode == BeTypeCode_SizedArray)
- {
- auto arrayType = (BeSizedArrayType*)globalVar->mType;
- dataOfs = arrayType->mElementType->GetStride() * constGep->mIdx1;
- }
- else
- {
- BF_FATAL("Invalid GEP");
- }
- addr_ce addr = -1;
- if (globalVar->mName.StartsWith("__bfStrData"))
- {
- int stringId = atoi(globalVar->mName.c_str() + 11);
- if (data.mQueueFixups)
- {
- addr = 0;
- CeConstStructFixup fixup;
- fixup.mKind = CeConstStructFixup::Kind_StringCharPtr;
- fixup.mValue = stringId;
- fixup.mOffset = (int)data.mData.mSize;
- data.mFixups.Add(fixup);
- }
- else
- {
- addr_ce stringAddr = ceContext->GetString(stringId);
- BfTypeInstance* stringTypeInst = (BfTypeInstance*)ceModule->ResolveTypeDef(ceModule->mCompiler->mStringTypeDef, BfPopulateType_Data);
- addr = stringAddr + stringTypeInst->mInstSize;
- }
- }
- if (addr != -1)
- {
- auto ptr = data.mData.GrowUninitialized(ceModule->mSystem->mPtrSize);
- int64 addr64 = addr + dataOfs;
- memcpy(ptr, &addr64, ceModule->mSystem->mPtrSize);
- return CeErrorKind_None;
- }
- return CeErrorKind_GlobalVariable;
- // auto sym = GetSymbol(globalVar);
- //
- // BeMCRelocation reloc;
- // reloc.mKind = BeMCRelocationKind_ADDR64;
- // reloc.mOffset = sect.mData.GetPos();
- // reloc.mSymTableIdx = sym->mIdx;
- // sect.mRelocs.push_back(reloc);
- // sect.mData.Write((int64)dataOfs);
- }
- else
- {
- BF_FATAL("Invalid GEPConstant");
- }
- }
- /*else if ((beType->IsPointer()) && (constVal->mTarget != NULL))
- {
- auto result = WriteConstant(arr, constVal->mTarget);
- if (result != CeErrorKind_None)
- return result;
- }
- else if (beType->IsComposite())
- {
- BF_ASSERT(constVal->mInt64 == 0);
- int64 zero = 0;
- int sizeLeft = beType->mSize;
- while (sizeLeft > 0)
- {
- int writeSize = BF_MIN(sizeLeft, 8);
- auto ptr = arr.GrowUninitialized(writeSize);
- memset(ptr, 0, writeSize);
- sizeLeft -= writeSize;
- }
- }*/
- else if (BeValueDynCastExact<BeConstant>(constVal) != NULL)
- {
- if (constVal->mType->IsStruct())
- {
- if (constVal->mType->mSize > 0)
- {
- auto ptr = data.mData.GrowUninitialized(constVal->mType->mSize);
- memset(ptr, 0, constVal->mType->mSize);
- }
- }
- else
- {
- auto ptr = data.mData.GrowUninitialized(beType->mSize);
- memcpy(ptr, &constVal->mInt64, beType->mSize);
- }
- }
- else
- return CeErrorKind_Error;
- return CeErrorKind_None;
- }
- void CeMachine::CheckFunctionKind(CeFunction* ceFunction)
- {
- ceFunction->mFunctionKind = CeFunctionKind_Normal;
- if (ceFunction->mMethodInstance != NULL)
- {
- auto methodDef = ceFunction->mMethodInstance->mMethodDef;
- if (methodDef->mIsExtern)
- {
- ceFunction->mFunctionKind = CeFunctionKind_Extern;
- auto owner = ceFunction->mMethodInstance->GetOwner();
- if (owner == mCeModule->mContext->mBfObjectType)
- {
- if (methodDef->mName == "Comptime_GetType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectType;
- }
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mTypeTypeDef))
- {
- if (methodDef->mName == "Comptime_GetTypeDeclarationById")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectTypeDeclById;
- }
- if (methodDef->mName == "Comptime_GetTypeDeclarationByName")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectTypeDeclByName;
- }
- else if (methodDef->mName == "Comptime_GetNextTypeDeclaration")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectNextTypeDecl;
- }
- else if (methodDef->mName == "Comptime_Type_GetBaseType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetBaseType;
- }
- else if (methodDef->mName == "Comptime_Type_HasDeclaredMember")
- {
- ceFunction->mFunctionKind = CeFunctionKind_HasDeclaredMember;
- }
- else if (methodDef->mName == "Comptime_GetTypeById")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectTypeById;
- }
- else if (methodDef->mName == "Comptime_GetWrappedType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetWrappedType;
- }
- else if (methodDef->mName == "Comptime_GetTypeByName")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectTypeByName;
- }
- else if (methodDef->mName == "Comptime_GetSpecializedType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetReflectSpecializedType;
- }
- else if (methodDef->mName == "Comptime_Type_ToString")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Type_ToString;
- }
- else if (methodDef->mName == "Comptime_TypeName_ToString")
- {
- ceFunction->mFunctionKind = CeFunctionKind_TypeName_ToString;
- }
- else if (methodDef->mName == "Comptime_TypeDocumentation_ToString")
- {
- ceFunction->mFunctionKind = CeFunctionKind_TypeDocumentation_ToString;
- }
- else if (methodDef->mName == "Comptime_Namespace_ToString")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Namespace_ToString;
- }
- else if (methodDef->mName == "Comptime_Type_GetCustomAttribute")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Type_GetCustomAttribute;
- }
- else if (methodDef->mName == "Comptime_Field_GetCustomAttribute")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Field_GetCustomAttribute;
- }
- else if (methodDef->mName == "Comptime_Method_GetCustomAttribute")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetCustomAttribute;
- }
- else if (methodDef->mName == "Comptime_Type_GetCustomAttributeType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Type_GetCustomAttributeType;
- }
- else if (methodDef->mName == "Comptime_Field_GetCustomAttributeType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Field_GetCustomAttributeType;
- }
- else if (methodDef->mName == "Comptime_Method_GetCustomAttributeType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetCustomAttributeType;
- }
- else if (methodDef->mName == "Comptime_GetMethod")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetMethod;
- }
- else if (methodDef->mName == "Comptime_GetMethodCount")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetMethodCount;
- }
- else if (methodDef->mName == "Comptime_Method_ToString")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_ToString;
- }
- else if (methodDef->mName == "Comptime_Method_GetName")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetName;
- }
- else if (methodDef->mName == "Comptime_Method_GetDocumentation")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetDocumentation;
- }
- else if (methodDef->mName == "Comptime_Method_GetInfo")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetInfo;
- }
- else if (methodDef->mName == "Comptime_Method_GetParamInfo")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetParamInfo;
- }
- else if (methodDef->mName == "Comptime_Method_GetGenericArg")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Method_GetGenericArg;
- }
- else if (methodDef->mName == "Comptime_Field_GetDocumentation")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Field_GetDocumentation;
- }
- else if (methodDef->mName == "Comptime_Field_GetStatic")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Field_GetStatic;
- }
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mCompilerTypeDef))
- {
- if (methodDef->mName == "Comptime_SetReturnType")
- {
- ceFunction->mFunctionKind = CeFunctionKind_SetReturnType;
- }
- else if (methodDef->mName == "Comptime_Align")
- {
- ceFunction->mFunctionKind = CeFunctionKind_Align;
- }
- else if (methodDef->mName == "Comptime_EmitTypeBody")
- {
- ceFunction->mFunctionKind = CeFunctionKind_EmitTypeBody;
- }
- else if (methodDef->mName == "Comptime_EmitAddInterface")
- {
- ceFunction->mFunctionKind = CeFunctionKind_EmitAddInterface;
- }
- else if (methodDef->mName == "Comptime_EmitMethodEntry")
- {
- ceFunction->mFunctionKind = CeFunctionKind_EmitMethodEntry;
- }
- else if (methodDef->mName == "Comptime_EmitMethodExit")
- {
- ceFunction->mFunctionKind = CeFunctionKind_EmitMethodExit;
- }
- else if (methodDef->mName == "Comptime_EmitMixin")
- {
- ceFunction->mFunctionKind = CeFunctionKind_EmitMixin;
- }
- else if (methodDef->mName == "Comptime_GetStringById")
- {
- ceFunction->mFunctionKind = CeFunctionKind_GetStringById;
- }
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mDiagnosticsDebugTypeDef))
- {
- if (methodDef->mName == "Write")
- {
- if (ceFunction->mMethodInstance->GetParamCount() == 1)
- ceFunction->mFunctionKind = CeFunctionKind_DebugWrite_Int;
- else
- ceFunction->mFunctionKind = CeFunctionKind_DebugWrite;
- }
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mThreadTypeDef))
- {
- if (methodDef->mName == "SleepInternal")
- ceFunction->mFunctionKind = CeFunctionKind_Sleep;
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mInternalTypeDef))
- {
- if (methodDef->mName == "ThrowIndexOutOfRange")
- ceFunction->mFunctionKind = CeFunctionKind_OOB;
- else if (methodDef->mName == "ThrowObjectNotInitialized")
- ceFunction->mFunctionKind = CeFunctionKind_ObjectNotInitialized;
- else if (methodDef->mName == "FatalError")
- ceFunction->mFunctionKind = CeFunctionKind_FatalError;
- else if (methodDef->mName == "Dbg_RawAlloc")
- ceFunction->mFunctionKind = CeFunctionKind_Malloc;
- else if (methodDef->mName == "Dbg_RawFree")
- ceFunction->mFunctionKind = CeFunctionKind_Free;
- else if (methodDef->mName == "ObjectDynCheckFailed")
- ceFunction->mFunctionKind = CeFunctionKind_DynCheckFailed;
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mPlatformTypeDef))
- {
- if (methodDef->mName == "BfpDirectory_Create")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_Create;
- else if (methodDef->mName == "BfpDirectory_Rename")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_Rename;
- else if (methodDef->mName == "BfpDirectory_Delete")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_Delete;
- else if (methodDef->mName == "BfpDirectory_GetCurrent")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_GetCurrent;
- else if (methodDef->mName == "BfpDirectory_SetCurrent")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_SetCurrent;
- else if (methodDef->mName == "BfpDirectory_Exists")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_Exists;
- else if (methodDef->mName == "BfpDirectory_GetSysDirectory")
- ceFunction->mFunctionKind = CeFunctionKind_BfpDirectory_GetSysDirectory;
- else if (methodDef->mName == "BfpFile_Close")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Close;
- else if (methodDef->mName == "BfpFile_Create")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Create;
- else if (methodDef->mName == "BfpFile_Flush")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Flush;
- else if (methodDef->mName == "BfpFile_GetFileSize")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetFileSize;
- else if (methodDef->mName == "BfpFile_Read")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Read;
- else if (methodDef->mName == "BfpFile_Release")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Release;
- else if (methodDef->mName == "BfpFile_Seek")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Seek;
- else if (methodDef->mName == "BfpFile_Truncate")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Truncate;
- else if (methodDef->mName == "BfpFile_Write")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Write;
- else if (methodDef->mName == "BfpFile_GetTime_LastWrite")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetTime_LastWrite;
- else if (methodDef->mName == "BfpFile_GetAttributes")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetAttributes;
- else if (methodDef->mName == "BfpFile_SetAttributes")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_SetAttributes;
- else if (methodDef->mName == "BfpFile_Copy")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Copy;
- else if (methodDef->mName == "BfpFile_Rename")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Rename;
- else if (methodDef->mName == "BfpFile_Delete")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Delete;
- else if (methodDef->mName == "BfpFile_Exists")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_Exists;
- else if (methodDef->mName == "BfpFile_GetTempPath")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetTempPath;
- else if (methodDef->mName == "BfpFile_GetTempFileName")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetTempFileName;
- else if (methodDef->mName == "BfpFile_GetFullPath")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetFullPath;
- else if (methodDef->mName == "BfpFile_GetActualPath")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFile_GetActualPath;
- else if (methodDef->mName == "BfpSpawn_Create")
- ceFunction->mFunctionKind = CeFunctionKind_BfpSpawn_Create;
- else if (methodDef->mName == "BfpSpawn_GetStdHandles")
- ceFunction->mFunctionKind = CeFunctionKind_BfpSpawn_GetStdHandles;
- else if (methodDef->mName == "BfpSpawn_Kill")
- ceFunction->mFunctionKind = CeFunctionKind_BfpSpawn_Kill;
- else if (methodDef->mName == "BfpSpawn_Release")
- ceFunction->mFunctionKind = CeFunctionKind_BfpSpawn_Release;
- else if (methodDef->mName == "BfpSpawn_WaitFor")
- ceFunction->mFunctionKind = CeFunctionKind_BfpSpawn_WaitFor;
- else if (methodDef->mName == "BfpFindFileData_FindFirstFile")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_FindFirstFile;
- else if (methodDef->mName == "BfpFindFileData_FindNextFile")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_FindNextFile;
- else if (methodDef->mName == "BfpFindFileData_GetFileName")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_GetFileName;
- else if (methodDef->mName == "BfpFindFileData_GetTime_LastWrite")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_GetTime_LastWrite;
- else if (methodDef->mName == "BfpFindFileData_GetTime_Created")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_GetTime_Created;
- else if (methodDef->mName == "BfpFindFileData_GetTime_Access")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_GetTime_Access;
- else if (methodDef->mName == "BfpFindFileData_GetFileAttributes")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_GetFileAttributes;
- else if (methodDef->mName == "BfpFindFileData_GetFileSize")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_GetFileSize;
- else if (methodDef->mName == "BfpFindFileData_Release")
- ceFunction->mFunctionKind = CeFunctionKind_BfpFindFileData_Release;
- else if (methodDef->mName == "BfpSystem_GetTimeStamp")
- ceFunction->mFunctionKind = CeFunctionKind_BfpSystem_GetTimeStamp;
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mChar32TypeDef))
- {
- if (methodDef->mName == "get__ToLower")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_ToLower;
- else if (methodDef->mName == "get__ToUpper")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_ToUpper;
- else if (methodDef->mName == "get__IsLower")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_IsLower;
- else if (methodDef->mName == "get__IsUpper")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_IsUpper;
- else if (methodDef->mName == "get__IsWhiteSpace_EX")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_IsWhiteSpace_EX;
- else if (methodDef->mName == "get__IsLetter")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_IsLetter;
- else if (methodDef->mName == "get__IsLetterOrDigit")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_IsLetterOrDigit;
- else if (methodDef->mName == "get__IsNumer")
- ceFunction->mFunctionKind = CeFunctionKind_Char32_IsNumber;
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mDoubleTypeDef))
- {
- if (methodDef->mName == "strtod")
- ceFunction->mFunctionKind = CeFunctionKind_Double_Strtod;
- else if (methodDef->mName == "ftoa")
- ceFunction->mFunctionKind = CeFunctionKind_Double_Ftoa;
- if (methodDef->mName == "ToString")
- ceFunction->mFunctionKind = CeFunctionKind_Double_ToString;
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mFloatTypeDef))
- {
- if (methodDef->mName == "ftoa")
- ceFunction->mFunctionKind = CeFunctionKind_Double_Ftoa;
- if (methodDef->mName == "ToString")
- ceFunction->mFunctionKind = CeFunctionKind_Float_ToString;
- }
- else if (owner->IsInstanceOf(mCeModule->mCompiler->mMathTypeDef))
- {
- if (methodDef->mName == "Abs")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Abs;
- else if (methodDef->mName == "Acos")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Acos;
- else if (methodDef->mName == "Asin")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Asin;
- else if (methodDef->mName == "Atan")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Atan;
- else if (methodDef->mName == "Atan2")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Atan2;
- else if (methodDef->mName == "Ceiling")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Ceiling;
- else if (methodDef->mName == "Cos")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Cos;
- else if (methodDef->mName == "Cosh")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Cosh;
- else if (methodDef->mName == "Exp")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Exp;
- else if (methodDef->mName == "Floor")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Floor;
- else if (methodDef->mName == "Log")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Log;
- else if (methodDef->mName == "Log10")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Log10;
- else if (methodDef->mName == "Mod")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Mod;
- else if (methodDef->mName == "Pow")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Pow;
- else if (methodDef->mName == "Round")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Round;
- else if (methodDef->mName == "Sin")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Sin;
- else if (methodDef->mName == "Sinh")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Sinh;
- else if (methodDef->mName == "Sqrt")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Sqrt;
- else if (methodDef->mName == "Tan")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Tan;
- else if (methodDef->mName == "Tanh")
- ceFunction->mFunctionKind = CeFunctionKind_Math_Tanh;
- }
- ceFunction->mInitializeState = CeFunction::InitializeState_Initialized;
- return;
- }
- }
- }
- void CeMachine::PrepareFunction(CeFunction* ceFunction, CeBuilder* parentBuilder)
- {
- AutoTimer autoTimer(mRevisionExecuteTime);
- SetAndRestoreValue<CeFunction*> prevCEFunction(mPreparingFunction, ceFunction);
- BF_ASSERT(ceFunction->mInitializeState <= CeFunction::InitializeState_Initialized);
- if (ceFunction->mFunctionKind == CeFunctionKind_NotSet)
- {
- CheckFunctionKind(ceFunction);
- if (ceFunction->mInitializeState == CeFunction::InitializeState_Initialized)
- return;
- }
- BF_ASSERT(ceFunction->mInitializeState <= CeFunction::InitializeState_Initialized);
- if (ceFunction->mInitializeState == CeFunction::InitializeState_Initializing_ReEntry)
- {
- //Fail("Function generation re-entry");
- return;
- }
- if (ceFunction->mInitializeState == CeFunction::InitializeState_Initializing)
- ceFunction->mInitializeState = CeFunction::InitializeState_Initializing_ReEntry;
- else
- ceFunction->mInitializeState = CeFunction::InitializeState_Initializing;
- CeBuilder ceBuilder;
- SetAndRestoreValue<CeBuilder*> prevBuilder(mCurBuilder, &ceBuilder);
- ceBuilder.mParentBuilder = parentBuilder;
- ceBuilder.mPtrSize = mCeModule->mCompiler->mSystem->mPtrSize;
- ceBuilder.mCeMachine = this;
- ceBuilder.mCeFunction = ceFunction;
- SetAndRestoreValue<int> prevRecursiveDepth(mCurRecursiveDepth, mCurRecursiveDepth + 1);
- ceBuilder.mRecursiveDepth = mCurRecursiveDepth;
- ceBuilder.Build();
- ceFunction->mInitializeState = CeFunction::InitializeState_Initialized;
- /*if (!ceFunction->mCode.IsEmpty())
- {
- CeDumpContext dumpCtx;
- dumpCtx.mCeFunction = ceFunction;
- dumpCtx.mStart = &ceFunction->mCode[0];
- dumpCtx.mPtr = dumpCtx.mStart;
- dumpCtx.mEnd = dumpCtx.mPtr + ceFunction->mCode.mSize;
- dumpCtx.Dump();
- OutputDebugStrF("Code for %s:\n%s\n", ceBuilder.mBeFunction->mName.c_str(), dumpCtx.mStr.c_str());
- }*/
- }
- void CeMachine::MapFunctionId(CeFunction* ceFunction)
- {
- if ((mCeModule->mSystem->mPtrSize == 8) && (mDebugger == NULL))
- return;
- ceFunction->mId = ++mCurFunctionId;
- mFunctionIdMap[ceFunction->mId] = ceFunction;
- }
- CeFunction* CeMachine::GetFunction(BfMethodInstance* methodInstance, BfIRValue func, bool& added)
- {
- if (func)
- {
- if ((func.IsConst()) || (func.IsFake()))
- return NULL;
- auto funcVal = mCeModule->mBfIRBuilder->mBeIRCodeGen->TryGetBeValue(func.mId);
- if (funcVal == NULL)
- return NULL;
- }
- CeFunctionInfo** functionInfoPtr = NULL;
- CeFunctionInfo* ceFunctionInfo = NULL;
- CeFunction* ceFunction = NULL;
- if (!mFunctions.TryAdd(methodInstance, NULL, &functionInfoPtr))
- {
- ceFunctionInfo = *functionInfoPtr;
- BF_ASSERT(ceFunctionInfo->mCeFunction != NULL);
- return ceFunctionInfo->mCeFunction;
- }
- BF_ASSERT(!methodInstance->mInCEMachine);
- methodInstance->mInCEMachine = true;
- BfLogSys(mCeModule->mSystem, "CeMachine::GetFunction %p\n", methodInstance);
- if (!func)
- {
- ceFunctionInfo = new CeFunctionInfo();
- }
- else
- {
- auto funcVal = mCeModule->mBfIRBuilder->mBeIRCodeGen->GetBeValue(func.mId);
- if (auto function = BeValueDynCast<BeFunction>(funcVal))
- {
- String funcName = function->mName;
- if (funcName.EndsWith("__INLINE"))
- funcName.RemoveFromEnd(8);
- CeFunctionInfo** namedFunctionInfoPtr = NULL;
- if (mNamedFunctionMap.TryAdd(funcName, NULL, &namedFunctionInfoPtr))
- {
- ceFunctionInfo = new CeFunctionInfo();
- ceFunctionInfo->mName = funcName;
- *namedFunctionInfoPtr = ceFunctionInfo;
- }
- else
- {
- ceFunctionInfo = *namedFunctionInfoPtr;
- if ((ceFunctionInfo->mMethodInstance != NULL) && (ceFunctionInfo->mMethodInstance != methodInstance))
- {
- // This ceFunctionInfo is already taken - probably from a name mangling conflict
- ceFunctionInfo = new CeFunctionInfo();
- }
- }
- }
- else
- {
- ceFunctionInfo = new CeFunctionInfo();
- }
- }
- ceFunctionInfo->mRefCount++;
- *functionInfoPtr = ceFunctionInfo;
- if (ceFunctionInfo->mMethodInstance == NULL)
- {
- added = true;
- auto module = methodInstance->GetOwner()->mModule;
- BF_ASSERT(ceFunctionInfo->mCeFunction == NULL);
- ceFunction = new CeFunction();
- ceFunction->mCeMachine = this;
- ceFunction->mIsVarReturn = methodInstance->mReturnType->IsVar();
- ceFunction->mCeFunctionInfo = ceFunctionInfo;
- ceFunction->mMethodInstance = methodInstance;
- if (mDebugger != NULL)
- ceFunction->mDbgInfo = new CeDbgFunctionInfo();
- ceFunctionInfo->mMethodInstance = methodInstance;
- ceFunctionInfo->mCeFunction = ceFunction;
- MapFunctionId(ceFunction);
- }
- return ceFunction;
- }
- CeFunction* CeMachine::GetPreparedFunction(BfMethodInstance* methodInstance)
- {
- bool added = false;
- auto ceFunction = GetFunction(methodInstance, BfIRValue(), added);
- if (ceFunction == NULL)
- return NULL;
- if (ceFunction->mInitializeState < CeFunction::InitializeState_Initialized)
- PrepareFunction(ceFunction, NULL);
- return ceFunction;
- }
- CeTypeInfo* CeMachine::GetTypeInfo(BfType* type)
- {
- if (type == NULL)
- return NULL;
- auto typeInstance = type->ToTypeInstance();
- if (typeInstance == NULL)
- return NULL;
- CeTypeInfo* ceTypeInfo = NULL;
- if (!mTypeInfoMap.TryAdd(type, NULL, &ceTypeInfo))
- {
- if (ceTypeInfo->mRevision == typeInstance->mRevision)
- return ceTypeInfo;
- ceTypeInfo->mMethodInstances.Clear();
- }
- mCeModule->PopulateType(typeInstance, BfPopulateType_DataAndMethods);
- ceTypeInfo->mRevision = typeInstance->mRevision;
- for (auto& methodGroup : typeInstance->mMethodInstanceGroups)
- {
- if (methodGroup.mOnDemandKind == BfMethodOnDemandKind_NoDecl_AwaitingReference)
- {
- auto methodDef = typeInstance->mTypeDef->mMethods[methodGroup.mMethodIdx];
- auto flags = ((methodDef->mGenericParams.size() != 0) || (typeInstance->IsUnspecializedType())) ? BfGetMethodInstanceFlag_UnspecializedPass : BfGetMethodInstanceFlag_None;
- flags = (BfGetMethodInstanceFlags)(flags | BfGetMethodInstanceFlag_MethodInstanceOnly);
- mCeModule->GetMethodInstance(typeInstance, methodDef, BfTypeVector(), flags);
- }
- if (methodGroup.mDefault != NULL)
- {
- mMethodInstanceSet.Add(methodGroup.mDefault);
- ceTypeInfo->mMethodInstances.Add(methodGroup.mDefault);
- }
- if (methodGroup.mMethodSpecializationMap != NULL)
- {
- for (auto& kv : *methodGroup.mMethodSpecializationMap)
- {
- mMethodInstanceSet.Add(kv.mValue);
- ceTypeInfo->mMethodInstances.Add(kv.mValue);
- }
- }
- }
- return ceTypeInfo;
- }
- BfMethodInstance* CeMachine::GetMethodInstance(int64 methodHandle)
- {
- BfMethodInstance* methodInstance = (BfMethodInstance*)(intptr)methodHandle;
- if (!mMethodInstanceSet.Contains(methodInstance))
- return NULL;
- return methodInstance;
- }
- BfFieldInstance* CeMachine::GetFieldInstance(int64 fieldHandle)
- {
- BfFieldInstance* fieldInstance = (BfFieldInstance*)(intptr)fieldHandle;
- if (!mFieldInstanceSet.Contains(fieldInstance))
- return NULL;
- return fieldInstance;
- }
- CeFunction* CeMachine::QueueMethod(BfMethodInstance* methodInstance, BfIRValue func)
- {
- if (mPreparingFunction != NULL)
- {
- auto curOwner = mPreparingFunction->mMethodInstance->GetOwner();
- curOwner->mModule->AddDependency(methodInstance->GetOwner(), curOwner, BfDependencyMap::DependencyFlag_ConstEval);
- }
- bool added = false;
- return GetFunction(methodInstance, func, added);
- }
- void CeMachine::QueueMethod(BfModuleMethodInstance moduleMethodInstance)
- {
- QueueMethod(moduleMethodInstance.mMethodInstance, moduleMethodInstance.mFunc);
- }
- void CeMachine::QueueStaticField(BfFieldInstance* fieldInstance, const StringImpl& mangledFieldName)
- {
- if (mCurBuilder != NULL)
- mCurBuilder->mStaticFieldInstanceMap[mangledFieldName] = fieldInstance;
- }
- void CeMachine::ClearTypeData(BfTypeInstance* typeInstance)
- {
- if (mTypeInfoMap.Remove(typeInstance))
- {
- for (auto& methodGroup : typeInstance->mMethodInstanceGroups)
- {
- if (methodGroup.mDefault != NULL)
- mMethodInstanceSet.Remove(methodGroup.mDefault);
- if (methodGroup.mMethodSpecializationMap != NULL)
- {
- for (auto& kv : *methodGroup.mMethodSpecializationMap)
- mMethodInstanceSet.Remove(kv.mValue);
- }
- }
- }
- }
- void CeMachine::SetAppendAllocInfo(BfModule* module, BfIRValue allocValue, BfIRValue appendSizeValue)
- {
- delete mAppendAllocInfo;
- mAppendAllocInfo = new CeAppendAllocInfo();
- mAppendAllocInfo->mModule = module;
- mAppendAllocInfo->mAllocValue = allocValue;
- mAppendAllocInfo->mAppendSizeValue = appendSizeValue;
- }
- void CeMachine::ClearAppendAllocInfo()
- {
- delete mAppendAllocInfo;
- mAppendAllocInfo = NULL;
- }
- CeContext* CeMachine::AllocContext()
- {
- CeContext* ceContext = NULL;
- if (!mContextList.IsEmpty())
- {
- ceContext = mContextList.back();
- mContextList.pop_back();
- }
- else
- {
- ceContext = new CeContext();
- ceContext->mCeMachine = this;
- ceContext->mMemory.Reserve(BF_CE_INITIAL_MEMORY);
- memset(ceContext->mMemory.mVals, 0, BF_CE_INITIAL_MEMORY);
- }
- ceContext->mCurEmitContext = mCurEmitContext;
- mCurEmitContext = NULL;
- mExecuteId++;
- ceContext->mStackSize = BF_CE_DEFAULT_STACK_SIZE;
- ceContext->mMemory.ResizeRaw(ceContext->mStackSize);
- ceContext->mExecuteId = mExecuteId;
- ceContext->mCurHandleId = 0;
- return ceContext;
- }
- void CeMachine::ReleaseContext(CeContext* ceContext)
- {
- ceContext->mStringMap.Clear();
- delete ceContext->mTypeDeclState;
- ceContext->mTypeDeclState = NULL;
- ceContext->mReflectMap.Clear();
- ceContext->mConstDataMap.Clear();
- ceContext->mMemory.Clear();
- if (ceContext->mMemory.mAllocSize > BF_CE_MAX_CARRYOVER_MEMORY)
- ceContext->mMemory.Dispose();
- ceContext->mStaticCtorExecSet.Clear();
- ceContext->mStaticFieldMap.Clear();
- ceContext->mStaticFieldIdMap.Clear();
- ceContext->mHeap->Clear(BF_CE_MAX_CARRYOVER_HEAP);
- ceContext->mReflectTypeIdOffset = -1;
- mCurEmitContext = ceContext->mCurEmitContext;
- ceContext->mCurEmitContext = NULL;
- mContextList.Add(ceContext);
- for (auto kv : ceContext->mInternalDataMap)
- kv.mValue->Release();
- ceContext->mInternalDataMap.Clear();
- ceContext->mWorkingDir.Clear();
- }
- BfTypedValue CeMachine::Call(CeCallSource callSource, BfModule* module, BfMethodInstance* methodInstance, const BfSizedArray<BfIRValue>& args, CeEvalFlags flags, BfType* expectingType)
- {
- auto ceContext = AllocContext();
- SetAndRestoreValue<int> prevRecursiveDepth(mCurRecursiveDepth, mCurRecursiveDepth + 1);
- ceContext->mRecursiveDepth = mCurRecursiveDepth;
- auto result = ceContext->Call(callSource, module, methodInstance, args, flags, expectingType);
- ReleaseContext(ceContext);
- return result;
- }
- BfError* CeMachine::FailCurrent(BfModule* srcModule, const StringImpl& error, BfAstNode* refNode)
- {
- BfError* bfError = NULL;
- if ((mCurBuilder != NULL) &&
- ((mCurContext != NULL) || (mCurBuilder->mRecursiveDepth > mCurContext->mRecursiveDepth)))
- {
- String useError = error;
- useError += StrFormat(" during const-eval generation of '%s'", srcModule->MethodToString(mCurBuilder->mCeFunction->mMethodInstance).c_str());
- bfError = srcModule->Fail(error, refNode);
- if (bfError != NULL)
- {
- auto filePos = mCurBuilder->mCeMachine->mCeModule->mCurFilePosition;
- auto parser = filePos.mFileInstance->mParser;
- if (parser != NULL)
- {
- srcModule->mCompiler->mPassInstance->MoreInfoAt(
- StrFormat("See comptime method '%s' processing location", srcModule->MethodToString(mCurBuilder->mCeFunction->mMethodInstance).c_str()),
- parser, filePos.mCurSrcPos, 1, BfFailFlag_None);
- }
- }
- }
- else
- {
- bfError = srcModule->Fail(error, refNode);
- }
- return bfError;
- }
- void CeMachine::FailCurrentMoreInfo(const StringImpl& error, BfAstNode* refNode)
- {
- }
|