123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114 |
- # This file is part of asmc, a bootstrapping OS with minimal seed
- # Copyright (C) 2018-2019 Giovanni Mascellani <gio@debian.org>
- # https://gitlab.com/giomasce/asmc
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- ifun cctx_emit 2
- fun escape_char 2 {
- $from
- $to
- $ctx
- @from 2 param = ;
- @to 1 param = ;
- @ctx 0 param = ;
- $emit
- @emit to 0 == = ;
- $discard
- @discard to 1 == = ;
- $data
- @data 0 = ;
- $data_ptr
- @data_ptr @data = ;
- if emit discard || {
- @to @data_ptr = ;
- }
- from ** **c 0 != "escape_char: unexpected null" assert_msg ;
- if from ** **c '\\' == {
- from from ** 1 + = ;
- $c
- @c from ** **c = ;
- c 0 != "escape_char: unexpected null" assert_msg ;
- $processed
- @processed 0 = ;
- if c 'n' == {
- to ** '\n' =c ;
- @processed 1 = ;
- }
- if c 'r' == {
- to ** '\r' =c ;
- @processed 1 = ;
- }
- if c 't' == {
- to ** '\t' =c ;
- @processed 1 = ;
- }
- if c 'v' == {
- to ** '\v' =c ;
- @processed 1 = ;
- }
- if c 'f' == {
- to ** '\f' =c ;
- @processed 1 = ;
- }
- if c 'a' == {
- to ** 0x07 =c ;
- @processed 1 = ;
- }
- if c 'b' == {
- to ** 0x08 =c ;
- @processed 1 = ;
- }
- if c '\\' == {
- to ** '\\' =c ;
- @processed 1 = ;
- }
- if c '\'' == {
- to ** '\'' =c ;
- @processed 1 = ;
- }
- if c '\"' == {
- to ** '\"' =c ;
- @processed 1 = ;
- }
- if '0' c <= c '7' <= && {
- # An octal escape sequence
- $value
- @value c '0' - = ;
- @c from ** 1 + **c = ;
- if '0' c <= c '7' <= && {
- from from ** 1 + = ;
- @value value 8 * c '0' - + = ;
- @c from ** 1 + **c = ;
- if '0' c <= c '7' <= && {
- from from ** 1 + = ;
- @value value 8 * c '0' - + = ;
- }
- }
- to ** value =c ;
- @processed 1 = ;
- }
- processed "escape_char: unknown escape sequence" from ** assert_msg_str ;
- from from ** 1 + = ;
- to to ** 1 + = ;
- } else {
- to ** from ** **c =c ;
- from from ** 1 + = ;
- to to ** 1 + = ;
- }
- if emit {
- data_ptr @data 1 + == "escape_char: error 1" assert_msg ;
- ctx data cctx_emit ;
- }
- }
- const TYPE_KIND_BASE 0
- const TYPE_KIND_POINTER 1
- const TYPE_KIND_FUNCTION 2
- const TYPE_KIND_ARRAY 3
- const TYPE_KIND_STRUCT 4
- const TYPE_KIND_UNION 5
- const TYPE_KIND_ENUM 6
- const TYPE_KIND 0
- const TYPE_BASE 4
- const TYPE_SIZE 8
- const TYPE_LENGTH 12
- const TYPE_ARGS 16
- const TYPE_ELLIPSIS 20
- const TYPE_FIELDS_OFFS 24
- const TYPE_FIELDS_TYPE_IDXS 28
- const TYPE_FIELDS_NAMES 32
- const SIZEOF_TYPE 36
- fun type_init 0 {
- $type
- @type SIZEOF_TYPE malloc = ;
- type TYPE_ARGS take_addr 4 vector_init = ;
- type TYPE_FIELDS_OFFS take_addr 4 vector_init = ;
- type TYPE_FIELDS_TYPE_IDXS take_addr 4 vector_init = ;
- type TYPE_FIELDS_NAMES take_addr 4 vector_init = ;
- type ret ;
- }
- fun type_destroy 1 {
- $type
- @type 0 param = ;
- type TYPE_ARGS take vector_destroy ;
- type TYPE_FIELDS_OFFS take vector_destroy ;
- type TYPE_FIELDS_TYPE_IDXS take vector_destroy ;
- type TYPE_FIELDS_NAMES take free_vect_of_ptrs ;
- type free ;
- }
- fun type_dump 1 {
- $type
- @type 0 param = ;
- $kind
- @kind type TYPE_KIND take = ;
- $base
- @base type TYPE_BASE take = ;
- if kind TYPE_KIND_BASE == {
- "Base type #" log ;
- base itoa log ;
- }
- if kind TYPE_KIND_POINTER == {
- "Pointer type to #" log ;
- base itoa log ;
- }
- if kind TYPE_KIND_FUNCTION == {
- "Function type returning #" log ;
- base itoa log ;
- $args
- @args type TYPE_ARGS take = ;
- $ellipsis
- @ellipsis type TYPE_ELLIPSIS take = ;
- if args vector_size 0 == ellipsis ! && {
- " taking no argument" log ;
- } else {
- " taking arguments" log ;
- $i
- @i 0 = ;
- while i args vector_size < {
- " #" log ;
- args i vector_at itoa log ;
- @i i 1 + = ;
- }
- if ellipsis {
- " ..." log ;
- }
- }
- }
- if kind TYPE_KIND_ARRAY == {
- "Array type of #" log ;
- base itoa log ;
- $length
- @length type TYPE_LENGTH take = ;
- if length 0xffffffff == {
- " of unspecified length" log ;
- } else {
- " of length " log ;
- length itoa log ;
- }
- }
- if kind TYPE_KIND_STRUCT == {
- "Struct type with fields" log ;
- $i
- @i 0 = ;
- $names
- $type_idxs
- $offs
- @names type TYPE_FIELDS_NAMES take = ;
- @type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
- @offs type TYPE_FIELDS_OFFS take = ;
- while i names vector_size < {
- " " log ;
- $name
- @name names i vector_at = ;
- if name **c '\0' == {
- @name "<anonymous>" = ;
- }
- name log ;
- " (@" log ;
- offs i vector_at itoa log ;
- " #" log ;
- type_idxs i vector_at itoa log ;
- ")" log ;
- @i i 1 + = ;
- }
- }
- if kind TYPE_KIND_UNION == {
- "Union type with fields" log ;
- $i
- @i 0 = ;
- $names
- $type_idxs
- $offs
- @names type TYPE_FIELDS_NAMES take = ;
- @type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
- @offs type TYPE_FIELDS_OFFS take = ;
- while i names vector_size < {
- " " log ;
- $name
- @name names i vector_at = ;
- if name **c '\0' == {
- @name "<anonymous>" = ;
- }
- name log ;
- " (@" log ;
- offs i vector_at itoa log ;
- " #" log ;
- type_idxs i vector_at itoa log ;
- ")" log ;
- @i i 1 + = ;
- }
- }
- $size
- @size type TYPE_SIZE take = ;
- if size 0xffffffff == {
- ", of undertermined size" log ;
- } else {
- ", of size " log ;
- size itoa log ;
- }
- }
- fun type_get_idx 2 {
- $type
- $name
- @type 1 param = ;
- @name 0 param = ;
- type TYPE_KIND take TYPE_KIND_STRUCT == type TYPE_KIND take TYPE_KIND_UNION == || "type_get_idx: type is not a struct or a union" assert_msg ;
- $i
- @i 0 = ;
- $names
- @names type TYPE_FIELDS_NAMES take = ;
- while i names vector_size < {
- if name names i vector_at strcmp 0 == {
- i ret ;
- }
- @i i 1 + = ;
- }
- 0xffffffff ret ;
- }
- const GLOBAL_TYPE_IDX 0
- const GLOBAL_LOC 4
- const GLOBAL_INITIALIZED 8
- const GLOBAL_EMITTED 12
- const SIZEOF_GLOBAL 16
- fun global_init 0 {
- $global
- @global SIZEOF_GLOBAL malloc = ;
- global ret ;
- }
- fun global_destroy 1 {
- $global
- @global 0 param = ;
- global free ;
- }
- fun global_dump 1 {
- $global
- @global 0 param = ;
- "has type #" log ;
- global GLOBAL_TYPE_IDX take itoa log ;
- " and is stored at " log ;
- global GLOBAL_LOC take itoa log ;
- }
- const CCTX_ASTINT_GET_TOKEN 0
- const CCTX_ASTINT_GET_TOKEN_OR_FAIL 4
- const CCTX_ASTINT_GIVE_BACK_TOKEN 8
- const CCTX_ASTINT_PARSE_TYPE 12
- const CCTX_ASTINT_CTX 16
- const SIZEOF_CCTX_ASTINT 20
- ifun cctx_get_token 1
- ifun cctx_give_back_token 1
- ifun cctx_get_token_or_fail 1
- ifun cctx_parse_type 1
- ifun cctx_parse_declarator 6
- fun cctx_astint_get_token 1 {
- $int
- @int 0 param = ;
- int CCTX_ASTINT_CTX take cctx_get_token ret ;
- }
- fun cctx_astint_get_token_or_fail 1 {
- $int
- @int 0 param = ;
- int CCTX_ASTINT_CTX take cctx_get_token_or_fail ret ;
- }
- fun cctx_astint_give_back_token 1 {
- $int
- @int 0 param = ;
- int CCTX_ASTINT_CTX take cctx_give_back_token ;
- }
- fun cctx_astint_parse_type 1 {
- $int
- @int 0 param = ;
- # Parse base type
- $base_type_idx
- @base_type_idx int CCTX_ASTINT_CTX take cctx_parse_type = ;
- if base_type_idx 0xffffffff == {
- base_type_idx ret ;
- }
- # Parse declarator
- $type_idx
- $name
- if int CCTX_ASTINT_CTX take 0 base_type_idx @type_idx @name 0 cctx_parse_declarator {
- type_idx ret ;
- } else {
- base_type_idx ret ;
- }
- }
- fun cctx_astint_init 1 {
- $ctx
- @ctx 0 param = ;
- $int
- @int SIZEOF_CCTX_ASTINT malloc = ;
- int CCTX_ASTINT_GET_TOKEN take_addr @cctx_astint_get_token = ;
- int CCTX_ASTINT_GET_TOKEN_OR_FAIL take_addr @cctx_astint_get_token_or_fail = ;
- int CCTX_ASTINT_GIVE_BACK_TOKEN take_addr @cctx_astint_give_back_token = ;
- int CCTX_ASTINT_PARSE_TYPE take_addr @cctx_astint_parse_type = ;
- int CCTX_ASTINT_CTX take_addr ctx = ;
- int ret ;
- }
- fun cctx_astint_destroy 1 {
- $int
- @int 0 param = ;
- int free ;
- }
- const CCTX_TYPES 0
- const CCTX_TYPENAMES 4
- const CCTX_GLOBALS 8
- const CCTX_TOKENS 12
- const CCTX_TOKENS_POS 16
- const CCTX_STAGE 20
- const CCTX_CURRENT_LOC 24
- const CCTX_LABEL_POS 28
- const CCTX_LABEL_BUF 32
- const CCTX_LABEL_NUM 36
- const CCTX_STRUCTS 40
- const CCTX_UNIONS 44
- const CCTX_ENUM_CONSTS 48
- const CCTX_HANDLES 52
- const CCTX_VERBOSE 56
- const CCTX_RUNTIME 60
- const CCTX_ASTINT 64
- const CCTX_ARRAY_LENS 68
- const CCTX_DEBUG 72
- const CCTX_DOT_POS 76
- const CCTX_DEBUG_AFTER 80
- const SIZEOF_CCTX 84
- fun cctx_init_types 1 {
- $ctx
- @ctx 0 param = ;
- ctx CCTX_TYPES take_addr 4 vector_init = ;
- ctx CCTX_TYPENAMES take_addr map_init = ;
- ctx CCTX_STRUCTS take_addr map_init = ;
- ctx CCTX_UNIONS take_addr map_init = ;
- ctx CCTX_ENUM_CONSTS take_addr map_init = ;
- }
- fun cctx_setup_handles 1 {
- $ctx
- @ctx 0 param = ;
- $handles
- @handles ctx CCTX_HANDLES take = ;
- handles @write vector_push_back ;
- handles @platform_setjmp vector_push_back ;
- handles @platform_longjmp vector_push_back ;
- handles @malloc vector_push_back ;
- handles @calloc vector_push_back ;
- handles @free vector_push_back ;
- handles @realloc vector_push_back ;
- handles @itoa vector_push_back ;
- handles @dump_stacktrace vector_push_back ;
- handles @vfs_open vector_push_back ;
- handles @vfs_close vector_push_back ;
- handles @vfs_read vector_push_back ;
- handles @vfs_write vector_push_back ;
- handles @vfs_truncate vector_push_back ;
- handles @vfs_seek vector_push_back ;
- handles @input_getc vector_push_back ;
- }
- fun cctx_setup_runtime 1 {
- $ctx
- @ctx 0 param = ;
- $fd
- @fd "/disk1/stdlib/int64.asm" vfs_open = ;
- ctx CCTX_RUNTIME take ASMCTX_VERBOSE take_addr 0 = ;
- ctx CCTX_RUNTIME take ASMCTX_DEBUG take_addr 0 = ;
- ctx CCTX_RUNTIME take fd asmctx_set_fd ;
- ctx CCTX_RUNTIME take asmctx_compile ;
- fd vfs_close ;
- }
- fun cctx_init 1 {
- $tokens
- @tokens 0 param = ;
- $ctx
- @ctx SIZEOF_CCTX malloc = ;
- ctx cctx_init_types ;
- ctx CCTX_GLOBALS take_addr map_init = ;
- ctx CCTX_TOKENS take_addr tokens = ;
- ctx CCTX_TOKENS_POS take_addr 0 = ;
- ctx CCTX_LABEL_NUM take_addr 0 = ;
- ctx CCTX_LABEL_BUF take_addr 32 malloc = ;
- ctx CCTX_LABEL_POS take_addr 4 vector_init = ;
- ctx CCTX_HANDLES take_addr 4 vector_init = ;
- ctx CCTX_VERBOSE take_addr 1 = ;
- ctx CCTX_DEBUG take_addr 1 = ;
- ctx CCTX_DOT_POS take_addr 0 = ;
- ctx CCTX_DEBUG_AFTER take_addr 0 1 - = ;
- ctx CCTX_RUNTIME take_addr asmctx_init = ;
- ctx CCTX_ASTINT take_addr ctx cctx_astint_init = ;
- ctx CCTX_ARRAY_LENS take_addr map_init = ;
- ctx cctx_setup_handles ;
- #ctx cctx_setup_runtime ;
- ctx ret ;
- }
- fun cctx_destroy_types 1 {
- $ctx
- @ctx 0 param = ;
- $types
- @types ctx CCTX_TYPES take = ;
- $i
- @i 0 = ;
- while i types vector_size < {
- types i vector_at type_destroy ;
- @i i 1 + = ;
- }
- types vector_destroy ;
- $typenames
- @typenames ctx CCTX_TYPENAMES take = ;
- typenames map_destroy ;
- $structs
- @structs ctx CCTX_STRUCTS take = ;
- structs map_destroy ;
- $unions
- @unions ctx CCTX_UNIONS take = ;
- unions map_destroy ;
- ctx CCTX_ENUM_CONSTS take map_destroy ;
- }
- fun global_destroy_closure 3 {
- $ctx
- $key
- $value
- @ctx 2 param = ;
- @key 1 param = ;
- @value 0 param = ;
- value global_destroy ;
- }
- fun cctx_destroy 1 {
- $ctx
- @ctx 0 param = ;
- ctx cctx_destroy_types ;
- $globals
- @globals ctx CCTX_GLOBALS take = ;
- globals @global_destroy_closure 0 map_foreach ;
- globals map_destroy ;
- ctx CCTX_LABEL_POS take vector_destroy ;
- ctx CCTX_LABEL_BUF take free ;
- ctx CCTX_HANDLES take vector_destroy ;
- ctx CCTX_RUNTIME take asmctx_destroy ;
- ctx CCTX_ASTINT take cctx_astint_destroy ;
- ctx CCTX_ARRAY_LENS take map_destroy ;
- ctx free ;
- }
- fun cctx_reset_types 1 {
- $ctx
- @ctx 0 param = ;
- ctx cctx_destroy_types ;
- ctx cctx_init_types ;
- }
- fun cctx_clean_globals_closure 3 {
- $ctx
- $key
- $value
- @ctx 2 param = ;
- @key 1 param = ;
- @value 0 param = ;
- value GLOBAL_EMITTED take_addr 0 = ;
- }
- fun cctx_clean_globals 1 {
- $ctx
- @ctx 0 param = ;
- $globals
- @globals ctx CCTX_GLOBALS take = ;
- globals @cctx_clean_globals_closure ctx map_foreach ;
- }
- fun cctx_get_type 2 {
- $ctx
- $type_idx
- @ctx 1 param = ;
- @type_idx 0 param = ;
- ctx CCTX_TYPES take type_idx vector_at ret ;
- }
- fun cctx_dump_types 1 {
- $ctx
- @ctx 0 param = ;
- $i
- @i 0 = ;
- $types
- @types ctx CCTX_TYPES take = ;
- while i types vector_size < {
- "#" log ;
- i itoa log ;
- ": " log ;
- types i vector_at type_dump ;
- "\n" log ;
- @i i 1 + = ;
- }
- }
- fun dump_typename_closure 3 {
- $ctx
- $key
- $value
- @ctx 2 param = ;
- @key 1 param = ;
- @value 0 param = ;
- "Typename " log ;
- key log ;
- ": #" log ;
- value itoa log ;
- "\n" log ;
- }
- fun cctx_dump_typenames 1 {
- $ctx
- @ctx 0 param = ;
- $typenames
- @typenames ctx CCTX_TYPENAMES take = ;
- typenames @dump_typename_closure 0 map_foreach ;
- }
- fun dump_global_closure 3 {
- $ctx
- $key
- $value
- @ctx 2 param = ;
- @key 1 param = ;
- @value 0 param = ;
- # Ignore globals starting with a dot, which are internal labels and
- # unlikely to give useful insights
- if key **c '.' == {
- ret ;
- }
- "Global " log ;
- key log ;
- ": " log ;
- value global_dump ;
- "\n" log ;
- }
- fun cctx_dump_globals 1 {
- $ctx
- @ctx 0 param = ;
- $globals
- @globals ctx CCTX_GLOBALS take = ;
- globals @dump_global_closure 0 map_foreach ;
- }
- fun cctx_print_stats 1 {
- $ctx
- @ctx 0 param = ;
- ctx CCTX_GLOBALS take map_size itoa log ;
- " globals were generated (including internal labels)\n" log ;
- ctx CCTX_TYPENAMES take map_size itoa log ;
- " type names were generated\n" log ;
- ctx CCTX_TYPES take vector_size itoa log ;
- " types were generated\n" log ;
- }
- ifun cctx_type_compare 3
- fun _cctx_type_compare 3 {
- $ctx
- $t1
- $t2
- @ctx 2 param = ;
- @t1 1 param = ;
- @t2 0 param = ;
- if t1 TYPE_KIND take t2 TYPE_KIND take != { 0 ret ; }
- if t1 TYPE_KIND take TYPE_KIND_BASE == {
- if t1 TYPE_BASE take t2 TYPE_BASE take != { 0 ret ; }
- 1 ret ;
- }
- if t1 TYPE_KIND take TYPE_KIND_POINTER == {
- if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare ! { 0 ret ; }
- 1 ret ;
- }
- if t1 TYPE_KIND take TYPE_KIND_FUNCTION == {
- if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare ! { 0 ret ; }
- if t1 TYPE_ELLIPSIS take t2 TYPE_ELLIPSIS take != { 0 ret ; }
- $args1
- $args2
- @args1 t1 TYPE_ARGS take = ;
- @args2 t2 TYPE_ARGS take = ;
- if args1 vector_size args2 vector_size != { 0 ret ; }
- $i
- @i 0 = ;
- while i args1 vector_size < {
- if ctx args1 i vector_at args2 i vector_at cctx_type_compare ! { 0 ret ; }
- @i i 1 + = ;
- }
- 1 ret ;
- }
- if t1 TYPE_KIND take TYPE_KIND_ARRAY == {
- if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare ! { 0 ret ; }
- if t1 TYPE_LENGTH take t2 TYPE_LENGTH take != { 0 ret ; }
- 1 ret ;
- }
- # structs and unions are always different unless they have the same
- # type index, which has already been checked
- if t1 TYPE_KIND take TYPE_KIND_STRUCT == {
- 0 ret ;
- }
- if t1 TYPE_KIND take TYPE_KIND_UNION == {
- 0 ret ;
- }
- 0 "_type_compare: not yet implemented" assert_msg ;
- }
- fun cctx_type_compare 3 {
- $ctx
- $ti1
- $ti2
- @ctx 2 param = ;
- @ti1 1 param = ;
- @ti2 0 param = ;
- if ti1 ti2 == { 1 ret ; }
- $t1
- $t2
- @t1 ctx CCTX_TYPES take ti1 vector_at = ;
- @t2 ctx CCTX_TYPES take ti2 vector_at = ;
- $res
- @res ctx t1 t2 _cctx_type_compare = ;
- if res {
- t1 TYPE_SIZE take t2 TYPE_SIZE take == "type_compare: type are equal, but have different size" assert_msg ;
- }
- res ret ;
- }
- fun cctx_add_type 2 {
- $ctx
- $type
- @ctx 1 param = ;
- @type 0 param = ;
- # Add the type to the list
- $types
- $idx
- @types ctx CCTX_TYPES take = ;
- @idx types vector_size = ;
- types type vector_push_back ;
- # Check if the new type already matches with another
- $i
- @i 0 = ;
- while i idx < {
- if ctx i idx cctx_type_compare {
- # Found a match, remove the new one and return the old one
- types vector_pop_back type_destroy ;
- i ret ;
- }
- @i i 1 + = ;
- }
- idx ret ;
- }
- fun cctx_get_pointer_type 2 {
- $ctx
- $type_idx
- @ctx 1 param = ;
- @type_idx 0 param = ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_POINTER = ;
- type TYPE_BASE take_addr type_idx = ;
- type TYPE_SIZE take_addr 4 = ;
- ctx type cctx_add_type ret ;
- }
- fun cctx_get_array_type 3 {
- $ctx
- $type_idx
- $length
- @ctx 2 param = ;
- @type_idx 1 param = ;
- @length 0 param = ;
- $base_type
- @base_type ctx CCTX_TYPES take type_idx vector_at = ;
- base_type TYPE_SIZE take 0xffffffff != "cctx_get_array_type: base type is invalid size" assert_msg ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_ARRAY = ;
- type TYPE_BASE take_addr type_idx = ;
- type TYPE_LENGTH take_addr length = ;
- type TYPE_SIZE take_addr length base_type TYPE_SIZE take * = ;
- # -1 is used when length is not specified
- if length 0xffffffff == {
- type TYPE_SIZE take_addr 0xffffffff = ;
- }
- ctx type cctx_add_type ret ;
- }
- fun cctx_get_function_type 4 {
- $ctx
- $type_idx
- $args
- $ellipsis
- @ctx 3 param = ;
- @type_idx 2 param = ;
- @args 1 param = ;
- @ellipsis 0 param = ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_FUNCTION = ;
- type TYPE_BASE take_addr type_idx = ;
- type TYPE_SIZE take_addr 0xffffffff = ;
- type TYPE_ARGS take vector_destroy ;
- type TYPE_ARGS take_addr args = ;
- type TYPE_ELLIPSIS take_addr ellipsis = ;
- ctx type cctx_add_type ret ;
- }
- ifun cctx_type_footprint 2
- fun cctx_construct_struct_type 3 {
- $ctx
- $type_idxs
- $names
- @ctx 2 param = ;
- @type_idxs 1 param = ;
- @names 0 param = ;
- type_idxs vector_size names vector_size == "cctx_construct_struct_type: inputs have different lengths" assert_msg ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_STRUCT = ;
- type TYPE_FIELDS_TYPE_IDXS take vector_destroy ;
- type TYPE_FIELDS_NAMES take vector_destroy ;
- type TYPE_FIELDS_TYPE_IDXS take_addr type_idxs = ;
- type TYPE_FIELDS_NAMES take_addr names = ;
- # Compute offsets and size
- $off
- @off 0 = ;
- $i
- @i 0 = ;
- $offs
- @offs type TYPE_FIELDS_OFFS take = ;
- while i names vector_size < {
- offs off vector_push_back ;
- $fp
- @fp ctx type_idxs i vector_at cctx_type_footprint = ;
- @off off fp + = ;
- @i i 1 + = ;
- }
- type TYPE_SIZE take_addr off = ;
- # Inherit labels from anonymous subtypes
- @i 0 = ;
- $orig_len
- @orig_len names vector_size = ;
- while i orig_len < {
- if names i vector_at **c '\0' == {
- $base_off
- @base_off offs i vector_at = ;
- $type_idx
- $type
- @type_idx type_idxs i vector_at = ;
- @type ctx type_idx cctx_get_type = ;
- type TYPE_KIND take TYPE_KIND_STRUCT == type TYPE_KIND take TYPE_KIND_UNION == || "cctx_construct_struct_type: anonymous field is not union or strucr" assert_msg ;
- $sub_type_idxs
- $sub_names
- $sub_offs
- @sub_type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
- @sub_names type TYPE_FIELDS_NAMES take = ;
- @sub_offs type TYPE_FIELDS_OFFS take = ;
- $j
- @j 0 = ;
- while j sub_names vector_size < {
- $sub_name
- @sub_name sub_names j vector_at = ;
- if sub_name **c '\0' != {
- names sub_name strdup vector_push_back ;
- type_idxs sub_type_idxs j vector_at vector_push_back ;
- offs sub_offs j vector_at base_off + vector_push_back ;
- }
- @j j 1 + = ;
- }
- }
- @i i 1 + = ;
- }
- type ret ;
- }
- fun cctx_get_incomplete_struct_type 1 {
- $ctx
- @ctx 0 param = ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_STRUCT = ;
- type TYPE_SIZE take_addr 0xffffffff = ;
- $r
- @r ctx type cctx_add_type = ;
- # To work around some deficiencies in the implementation,
- # immediately add the pointer type too
- ctx r cctx_get_pointer_type ;
- r ret ;
- }
- fun cctx_construct_union_type 3 {
- $ctx
- $type_idxs
- $names
- @ctx 2 param = ;
- @type_idxs 1 param = ;
- @names 0 param = ;
- type_idxs vector_size names vector_size == "cctx_construct_union_type: inputs have different lengths" assert_msg ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_UNION = ;
- type TYPE_FIELDS_TYPE_IDXS take vector_destroy ;
- type TYPE_FIELDS_NAMES take vector_destroy ;
- type TYPE_FIELDS_TYPE_IDXS take_addr type_idxs = ;
- type TYPE_FIELDS_NAMES take_addr names = ;
- # Compute size
- $size
- @size 0 = ;
- $i
- @i 0 = ;
- $offs
- @offs type TYPE_FIELDS_OFFS take = ;
- while i names vector_size < {
- offs 0 vector_push_back ;
- @size size ctx type_idxs i vector_at cctx_type_footprint max = ;
- @i i 1 + = ;
- }
- type TYPE_SIZE take_addr size = ;
- # Inherit labels from anonymous subtypes
- @i 0 = ;
- $orig_len
- @orig_len names vector_size = ;
- while i orig_len < {
- if names i vector_at **c '\0' == {
- $base_off
- @base_off offs i vector_at = ;
- $type_idx
- $type
- @type_idx type_idxs i vector_at = ;
- @type ctx type_idx cctx_get_type = ;
- type TYPE_KIND take TYPE_KIND_STRUCT == type TYPE_KIND take TYPE_KIND_UNION == || "cctx_construct_struct_type: anonymous field is not union or strucr" assert_msg ;
- $sub_type_idxs
- $sub_names
- $sub_offs
- @sub_type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
- @sub_names type TYPE_FIELDS_NAMES take = ;
- @sub_offs type TYPE_FIELDS_OFFS take = ;
- $j
- @j 0 = ;
- while j sub_names vector_size < {
- $sub_name
- @sub_name sub_names j vector_at = ;
- if sub_name **c '\0' != {
- names sub_name strdup vector_push_back ;
- type_idxs sub_type_idxs j vector_at vector_push_back ;
- offs sub_offs j vector_at base_off + vector_push_back ;
- }
- @j j 1 + = ;
- }
- }
- @i i 1 + = ;
- }
- type ret ;
- }
- fun cctx_get_incomplete_union_type 1 {
- $ctx
- @ctx 0 param = ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_UNION = ;
- type TYPE_SIZE take_addr 0xffffffff = ;
- ctx type cctx_add_type ret ;
- }
- const TYPE_VOID 0
- const TYPE_FIRST_INTEGER 1
- const TYPE_CHAR 1
- const TYPE_SCHAR 2
- const TYPE_UCHAR 3
- const TYPE_SHORT 4
- const TYPE_INT 5
- const TYPE_LONG 6
- const TYPE_USHORT 7
- const TYPE_UINT 8
- const TYPE_ULONG 9
- const TYPE_BOOL 10
- const TYPE_LAST_INTEGER 10
- # Floating point numbers are not supported, but at least the compiler
- # is able to tell their sizes
- const TYPE_FLOAT 11
- const TYPE_DOUBLE 12
- const TYPE_LDOUBLE 13
- # Other useful types
- const TYPE_CHAR_ARRAY 14
- const TYPE_VOID_PTR 15
- fun is_integer_type 1 {
- $idx
- @idx 0 param = ;
- TYPE_FIRST_INTEGER idx <= TYPE_LAST_INTEGER idx >= && ret ;
- }
- fun cctx_create_basic_type 3 {
- $ctx
- $idx
- $size
- @ctx 2 param = ;
- @idx 1 param = ;
- @size 0 param = ;
- $types
- @types ctx CCTX_TYPES take = ;
- idx types vector_size == "cctx_create_basic_type: error 1" assert_msg ;
- $type
- @type type_init = ;
- type TYPE_KIND take_addr TYPE_KIND_BASE = ;
- type TYPE_BASE take_addr idx = ;
- type TYPE_SIZE take_addr size = ;
- types type vector_push_back ;
- }
- fun cctx_create_basic_types 1 {
- $ctx
- @ctx 0 param = ;
- ctx TYPE_VOID 0xffffffff cctx_create_basic_type ;
- ctx TYPE_CHAR 1 cctx_create_basic_type ;
- ctx TYPE_SCHAR 1 cctx_create_basic_type ;
- ctx TYPE_UCHAR 1 cctx_create_basic_type ;
- ctx TYPE_SHORT 2 cctx_create_basic_type ;
- ctx TYPE_INT 4 cctx_create_basic_type ;
- ctx TYPE_LONG 8 cctx_create_basic_type ;
- ctx TYPE_USHORT 2 cctx_create_basic_type ;
- ctx TYPE_UINT 4 cctx_create_basic_type ;
- ctx TYPE_ULONG 8 cctx_create_basic_type ;
- ctx TYPE_BOOL 4 cctx_create_basic_type ;
- ctx TYPE_FLOAT 4 cctx_create_basic_type ;
- ctx TYPE_DOUBLE 8 cctx_create_basic_type ;
- ctx TYPE_LDOUBLE 12 cctx_create_basic_type ;
- ctx TYPE_CHAR 0xffffffff cctx_get_array_type TYPE_CHAR_ARRAY == "cctx_create_basic_types: error 1" assert_msg ;
- ctx TYPE_VOID cctx_get_pointer_type TYPE_VOID_PTR == "cctx_create_basic_types: error 2" assert_msg ;
- }
- fun cctx_has_global 2 {
- $ctx
- $name
- @ctx 1 param = ;
- @name 0 param = ;
- $globals
- @globals ctx CCTX_GLOBALS take = ;
- globals name map_has ret ;
- }
- fun cctx_get_global 2 {
- $ctx
- $name
- @ctx 1 param = ;
- @name 0 param = ;
- $globals
- @globals ctx CCTX_GLOBALS take = ;
- globals name map_has "cctx_get_global: global does not exist" name assert_msg_str ;
- $global
- @global globals name map_at = ;
- global ret ;
- }
- fun cctx_add_global 4 {
- $ctx
- $name
- $loc
- $type_idx
- @ctx 3 param = ;
- @name 2 param = ;
- @loc 1 param = ;
- @type_idx 0 param = ;
- $globals
- @globals ctx CCTX_GLOBALS take = ;
- $present
- @present globals name map_has = ;
- $global
- if present {
- @global globals name map_at = ;
- ctx global GLOBAL_TYPE_IDX take type_idx cctx_type_compare "cctx_add_global: types do not match" name assert_msg_str ;
- } else {
- ctx CCTX_STAGE take 0 == "cctx_add_global: error 1" assert_msg ;
- @global global_init = ;
- global GLOBAL_TYPE_IDX take_addr type_idx = ;
- global GLOBAL_LOC take_addr loc = ;
- global GLOBAL_INITIALIZED take_addr 0 = ;
- global GLOBAL_EMITTED take_addr 0 = ;
- globals name global map_set ;
- }
- if ctx CCTX_STAGE take 0 == {
- global GLOBAL_LOC take_addr 0xffffffff = ;
- } else {
- present "cctx_add_global: error 2" assert_msg ;
- }
- if ctx CCTX_STAGE take 1 == {
- if loc 0xffffffff != {
- global GLOBAL_LOC take 0xffffffff == "cctx_add_global: global is defined more than once" name assert_msg_str ;
- global GLOBAL_LOC take_addr loc = ;
- }
- }
- if ctx CCTX_STAGE take 2 == {
- if loc 0xffffffff != {
- global GLOBAL_LOC take loc == "cctx_add_global: error 3" name assert_msg_str ;
- if name **c '.' != {
- name loc resolve_symbol_add ;
- }
- }
- }
- type_idx ret ;
- }
- fun cctx_emit 2 {
- $ctx
- $byte
- @ctx 1 param = ;
- @byte 0 param = ;
- if ctx CCTX_STAGE take 2 == {
- ctx CCTX_CURRENT_LOC take byte =c ;
- }
- ctx CCTX_CURRENT_LOC take_addr ctx CCTX_CURRENT_LOC take 1 + = ;
- }
- fun cctx_emit16 2 {
- $ctx
- $word
- @ctx 1 param = ;
- @word 0 param = ;
- ctx word cctx_emit ;
- ctx word 8 >> cctx_emit ;
- }
- fun cctx_emit32 2 {
- $ctx
- $dword
- @ctx 1 param = ;
- @dword 0 param = ;
- ctx dword cctx_emit16 ;
- ctx dword 16 >> cctx_emit16 ;
- }
- fun cctx_emit_zeros 2 {
- $ctx
- $num
- @ctx 1 param = ;
- @num 0 param = ;
- $i
- @i 0 = ;
- while i num < {
- ctx 0 cctx_emit ;
- @i i 1 + = ;
- }
- }
- ifun cctx_gen_label 3
- ifun cctx_fix_label 4
- ifun cctx_gen_jump 3
- ifun cctx_gen_label_jump 5
- fun cctx_is_eof 1 {
- $ctx
- @ctx 0 param = ;
- ctx CCTX_TOKENS_POS take ctx CCTX_TOKENS take vector_size == ret ;
- }
- fun cctx_get_token 1 {
- $ctx
- @ctx 0 param = ;
- if ctx CCTX_TOKENS_POS take ctx CCTX_TOKENS take vector_size == {
- 0 ret ;
- } else {
- $tok
- @tok ctx CCTX_TOKENS take ctx CCTX_TOKENS_POS take vector_at = ;
- if ctx CCTX_DEBUG take {
- " " log ;
- tok log ;
- } else {
- if ctx CCTX_VERBOSE take {
- if ctx CCTX_TOKENS_POS take ctx CCTX_DEBUG_AFTER take == {
- ctx CCTX_DEBUG take_addr 1 = ;
- }
- if ctx CCTX_TOKENS_POS take ctx CCTX_DOT_POS take == {
- "." log ;
- ctx CCTX_DOT_POS take_addr ctx CCTX_DOT_POS take 1000 + = ;
- }
- }
- }
- ctx CCTX_TOKENS_POS take_addr ctx CCTX_TOKENS_POS take 1 + = ;
- tok ret ;
- }
- }
- fun cctx_give_back_token 1 {
- $ctx
- @ctx 0 param = ;
- if ctx CCTX_DEBUG take {
- " <gb>" log ;
- }
- ctx CCTX_TOKENS_POS take 0 > "cctx_give_back_token: error 1" assert_msg ;
- ctx CCTX_TOKENS_POS take_addr ctx CCTX_TOKENS_POS take 1 - = ;
- }
- fun cctx_save_token_pos 1 {
- $ctx
- @ctx 0 param = ;
- ctx CCTX_TOKENS_POS take ret ;
- }
- fun cctx_restore_token_pos 2 {
- $ctx
- $pos
- @ctx 1 param = ;
- @pos 0 param = ;
- ctx CCTX_TOKENS_POS take_addr pos = ;
- }
- fun cctx_get_token_or_fail 1 {
- $ctx
- @ctx 0 param = ;
- $tok
- @tok ctx cctx_get_token = ;
- tok 0 != "cctx_get_token_or_fail: unexpected end-of-file" assert_msg ;
- tok ret ;
- }
- fun cctx_go_to_matching 3 {
- $ctx
- $open
- $close
- @ctx 2 param = ;
- @open 1 param = ;
- @close 0 param = ;
- $level
- @level 1 = ;
- while level 0 > {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok open strcmp 0 == {
- @level level 1 + = ;
- }
- if tok close strcmp 0 == {
- @level level 1 - = ;
- }
- }
- }
- fun cctx_print_token_pos 1 {
- $ctx
- @ctx 0 param = ;
- "Token pos: " log ;
- ctx CCTX_TOKENS_POS take itoa log ;
- "\n" log ;
- }
- ifun cctx_parse_type 1
- fun cctx_parse_struct 3 {
- $ctx
- $type_idxs_ptr
- $names_ptr
- @ctx 2 param = ;
- @type_idxs_ptr 1 param = ;
- @names_ptr 0 param = ;
- $type_idxs
- $names
- @type_idxs 4 vector_init = ;
- @names 4 vector_init = ;
- $cont
- @cont 1 = ;
- while cont {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok "}" strcmp 0 == {
- @cont 0 = ;
- } else {
- ctx cctx_give_back_token ;
- $type_idx
- @type_idx ctx cctx_parse_type = ;
- type_idx 0xffffffff != "cctx_parse_struct: type expected" tok assert_msg_str ;
- $cont2
- @cont2 1 = ;
- while cont2 {
- $actual_type_idx
- $name
- if ctx 0 type_idx @actual_type_idx @name 0 cctx_parse_declarator ! {
- # If the declarator parsing fails, then we assume that this
- # is an annonymous struct or union, which for the moment we
- # just represent as having an empty name; names will be
- # fixed later, when the type is actually created
- @name "" = ;
- @actual_type_idx type_idx = ;
- }
- type_idxs actual_type_idx vector_push_back ;
- names name strdup vector_push_back ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok ":" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- @tok ctx cctx_get_token_or_fail = ;
- }
- if tok ";" strcmp 0 == {
- @cont2 0 = ;
- } else {
- tok "," strcmp 0 == "cctx_parse_struct: comma expected" assert_msg ;
- }
- }
- }
- }
- type_idxs_ptr type_idxs = ;
- names_ptr names = ;
- }
- ifun ast_eval_compile 3
- fun cctx_parse_ast1 2 {
- $ctx
- $term
- @ctx 1 param = ;
- @term 0 param = ;
- if ctx CCTX_DEBUG take {
- " <pa1>" log ;
- }
- $res
- @res ctx CCTX_ASTINT take term ast_parse1 = ;
- if ctx CCTX_DEBUG take {
- " </pa1>" log ;
- }
- res ret ;
- }
- fun cctx_parse_ast2 3 {
- $ctx
- $term1
- $term2
- @ctx 2 param = ;
- @term1 1 param = ;
- @term2 0 param = ;
- if ctx CCTX_DEBUG take {
- " <pa2>" log ;
- }
- $res
- @res ctx CCTX_ASTINT take term1 term2 ast_parse2 = ;
- if ctx CCTX_DEBUG take {
- " </pa2>" log ;
- }
- res ret ;
- }
- fun cctx_parse_ast3 4 {
- $ctx
- $term1
- $term2
- $term3
- @ctx 3 param = ;
- @term1 2 param = ;
- @term2 1 param = ;
- @term3 0 param = ;
- if ctx CCTX_DEBUG take {
- " <pa3>" log ;
- }
- $res
- @res ctx CCTX_ASTINT take term1 term2 term3 ast_parse3 = ;
- if ctx CCTX_DEBUG take {
- " </pa3>" log ;
- }
- res ret ;
- }
- fun cctx_parse_enum 1 {
- $ctx
- @ctx 0 param = ;
- $enum_consts
- @enum_consts ctx CCTX_ENUM_CONSTS take = ;
- $cont
- @cont 1 = ;
- $val
- @val 0 = ;
- while cont {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok "}" strcmp 0 == {
- @cont 0 = ;
- } else {
- $ident
- @ident tok = ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok "=" strcmp 0 == {
- $ast
- @ast ctx "}" "," cctx_parse_ast2 = ;
- @val ctx 0 ast ast_eval_compile = ;
- ast ast_destroy ;
- @tok ctx cctx_get_token_or_fail = ;
- }
- enum_consts ident map_has ! "cctx_parse_enum: constant is already defined" assert_msg ;
- enum_consts ident val map_set ;
- @val val 1 + = ;
- if tok "}" strcmp 0 == {
- @cont 0 = ;
- } else {
- tok "," strcmp 0 == "cctx_parse_enum: comma expected" assert_msg ;
- }
- }
- }
- }
- fun cctx_parse_type 1 {
- $ctx
- @ctx 0 param = ;
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- # Ignore constness
- if tok "const" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- }
- if tok "void" strcmp 0 == { TYPE_VOID ret ; }
- if tok "_Bool" strcmp 0 == { TYPE_BOOL ret ; }
- if tok "char" strcmp 0 == { TYPE_CHAR ret ; }
- if tok "short" strcmp 0 == { TYPE_SHORT ret ; }
- if tok "int" strcmp 0 == { TYPE_INT ret ; }
- if tok "float" strcmp 0 == { TYPE_FLOAT ret ; }
- if tok "double" strcmp 0 == { TYPE_DOUBLE ret ; }
- if tok "signed" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "char" strcmp 0 == { TYPE_SCHAR ret ; }
- if tok "short" strcmp 0 == { TYPE_SHORT ret ; }
- if tok "int" strcmp 0 == { TYPE_INT ret ; }
- if tok "long" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "int" strcmp 0 == { TYPE_INT ret ; }
- if tok "long" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "int" strcmp 0 == { TYPE_LONG ret ; }
- ctx cctx_give_back_token ;
- TYPE_LONG ret ;
- }
- ctx cctx_give_back_token ;
- TYPE_INT ret ;
- }
- ctx cctx_give_back_token ;
- TYPE_INT ret ;
- }
- if tok "unsigned" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "char" strcmp 0 == { TYPE_UCHAR ret ; }
- if tok "short" strcmp 0 == { TYPE_USHORT ret ; }
- if tok "int" strcmp 0 == { TYPE_UINT ret ; }
- if tok "long" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "int" strcmp 0 == { TYPE_UINT ret ; }
- if tok "long" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "int" strcmp 0 == { TYPE_ULONG ret ; }
- ctx cctx_give_back_token ;
- TYPE_ULONG ret ;
- }
- ctx cctx_give_back_token ;
- TYPE_UINT ret ;
- }
- ctx cctx_give_back_token ;
- TYPE_UINT ret ;
- }
- if tok "long" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "int" strcmp 0 == { TYPE_INT ret ; }
- if tok "double" strcmp 0 == { TYPE_LDOUBLE ret ; }
- if tok "long" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "int" strcmp 0 == { TYPE_LONG ret ; }
- ctx cctx_give_back_token ;
- TYPE_LONG ret ;
- }
- ctx cctx_give_back_token ;
- TYPE_INT ret ;
- }
- if tok "struct" strcmp 0 == {
- $tag
- $type_idxs
- $names
- @tag 0 = ;
- @type_idxs 0 = ;
- @tok ctx cctx_get_token_or_fail = ;
- $structs
- @structs ctx CCTX_STRUCTS take = ;
- $type_idx
- if tok "{" strcmp 0 != {
- @tag tok = ;
- @tok ctx cctx_get_token_or_fail = ;
- if structs tag map_has {
- @type_idx structs tag map_at = ;
- } else {
- @type_idx ctx cctx_get_incomplete_struct_type = ;
- structs tag type_idx map_set ;
- }
- } else {
- @type_idx ctx cctx_get_incomplete_struct_type = ;
- }
- if tok "{" strcmp 0 == {
- ctx @type_idxs @names cctx_parse_struct ;
- $type
- @type ctx type_idx cctx_get_type = ;
- type TYPE_SIZE take 0xffffffff == "cctx_parse_type: cannot define a struct twice" assert_msg ;
- $newtype
- @newtype ctx type_idxs names cctx_construct_struct_type = ;
- type type_destroy ;
- ctx CCTX_TYPES take type_idx vector_at_addr newtype = ;
- } else {
- ctx cctx_give_back_token ;
- tag 0 != "cctx_parse_type: struct without neither tag nor definition" assert_msg ;
- }
- type_idx ret ;
- }
- if tok "union" strcmp 0 == {
- $tag
- $type_idxs
- $names
- @tag 0 = ;
- @type_idxs 0 = ;
- @tok ctx cctx_get_token_or_fail = ;
- $unions
- @unions ctx CCTX_UNIONS take = ;
- $type_idx
- if tok "{" strcmp 0 != {
- @tag tok = ;
- @tok ctx cctx_get_token_or_fail = ;
- if unions tag map_has {
- @type_idx unions tag map_at = ;
- } else {
- @type_idx ctx cctx_get_incomplete_union_type = ;
- unions tag type_idx map_set ;
- }
- } else {
- @type_idx ctx cctx_get_incomplete_union_type = ;
- }
- if tok "{" strcmp 0 == {
- ctx @type_idxs @names cctx_parse_struct ;
- $type
- @type ctx type_idx cctx_get_type = ;
- type TYPE_SIZE take 0xffffffff == "cctx_parse_type: cannot define a union twice" assert_msg ;
- $newtype
- @newtype ctx type_idxs names cctx_construct_union_type = ;
- type type_destroy ;
- ctx CCTX_TYPES take type_idx vector_at_addr newtype = ;
- } else {
- ctx cctx_give_back_token ;
- tag 0 != "cctx_parse_type: union without neither tag nor definition" assert_msg ;
- }
- type_idx ret ;
- }
- if tok "enum" strcmp 0 == {
- $tag
- @tok ctx cctx_get_token_or_fail = ;
- if tok "{" strcmp 0 != {
- @tag tok = ;
- @tok ctx cctx_get_token_or_fail = ;
- }
- if tok "{" strcmp 0 == {
- ctx cctx_parse_enum ;
- } else {
- ctx cctx_give_back_token ;
- tag 0 != "cctx_parse_type: enum without neither tag nor definition" assert_msg ;
- }
- TYPE_UINT ret ;
- }
- $typenames
- @typenames ctx CCTX_TYPENAMES take = ;
- if typenames tok map_has {
- $idx
- @idx typenames tok map_at = ;
- idx ret ;
- } else {
- ctx cctx_give_back_token ;
- 0xffffffff ret ;
- }
- }
- fun _cctx_parse_function_arguments 3 {
- $ctx
- $ret_arg_names
- $ret_ellipsis
- @ctx 2 param = ;
- @ret_arg_names 1 param = ;
- @ret_ellipsis 0 param = ;
- ret_ellipsis 0 = ;
- $args
- @args 4 vector_init = ;
- while 1 {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok "void" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok ")" strcmp 0 == {
- args vector_size 0 == "_cctx_parse_function_arguments: void only valid in first position" assert_msg ;
- args ret ;
- } else {
- ctx cctx_give_back_token ;
- ctx cctx_give_back_token ;
- }
- } else {
- ctx cctx_give_back_token ;
- }
- $type_idx
- @type_idx ctx cctx_parse_type = ;
- if type_idx 0xffffffff == {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "..." strcmp 0 == {
- ret_ellipsis 1 = ;
- @tok ctx cctx_get_token_or_fail = ;
- }
- tok ")" strcmp 0 == "_cctx_parse_function_arguments: ) or type expected" assert_msg ;
- if args vector_size 0 == {
- ret_ellipsis 1 = ;
- }
- args vector_size 0 == ret_ellipsis ** || "_cctx_parse_function_arguments: unexpected )" assert_msg ;
- args ret ;
- }
- $name
- $actual_type_idx
- if ctx 0 type_idx @actual_type_idx @name 0 cctx_parse_declarator ! {
- @actual_type_idx type_idx = ;
- }
- args actual_type_idx vector_push_back ;
- if ret_arg_names 0 != {
- ret_arg_names name vector_push_back ;
- }
- @tok ctx cctx_get_token_or_fail = ;
- if tok ")" strcmp 0 == {
- args ret ;
- }
- tok "," strcmp 0 == "_cctx_parse_function_arguments: ) or , expected" assert_msg ;
- }
- }
- fun _cctx_parse_declarator 6 {
- $ctx
- $lctx
- $type_idx
- $ret_type_idx
- $ret_name
- $ret_arg_names
- @ctx 5 param = ;
- @lctx 4 param = ;
- @type_idx 3 param = ;
- @ret_type_idx 2 param = ;
- @ret_name 1 param = ;
- @ret_arg_names 0 param = ;
- #"_cctx_parse_declarator: entering\n" log ;
- #ctx cctx_print_token_pos ;
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- $processed
- @processed 0 = ;
- #"_cctx_parse_declarator: token is " log ;
- #tok log ;
- #"\n" log ;
- # Parse pointer declaration
- if tok "*" strcmp 0 == {
- @type_idx ctx type_idx cctx_get_pointer_type = ;
- if ctx lctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator ! {
- ret_type_idx type_idx = ;
- }
- @processed 1 = ;
- }
- # Parse function declaration and grouping parantheses
- if tok "(" strcmp 0 == {
- # Here the first problem is decide whether this is a function or
- # grouping parenthesis; if immediately after there is a type or a
- # closing parenthesis, we are in the first case; otherwise, we are
- # in the second case.
- $pos
- @pos ctx cctx_save_token_pos = ;
- $type
- @type ctx cctx_parse_type = ;
- ctx pos cctx_restore_token_pos ;
- $is_funct
- @is_funct 0 = ;
- if type 0xffffffff != {
- @is_funct 1 = ;
- }
- @tok ctx cctx_get_token_or_fail = ;
- if tok ")" strcmp 0 == {
- @is_funct 1 = ;
- }
- # Restore the content of tok, so that the program does not get
- # captured in later branches
- @tok "(" = ;
- ctx cctx_give_back_token ;
- if is_funct {
- # Function parenthesis
- $args
- $ellipsis
- @args ctx ret_arg_names @ellipsis _cctx_parse_function_arguments = ;
- if ctx lctx type_idx ret_type_idx ret_name 0 _cctx_parse_declarator {
- @type_idx ret_type_idx ** = ;
- }
- ret_type_idx ctx type_idx args ellipsis cctx_get_function_type = ;
- } else {
- # Grouping parenthesis
- $inside_pos
- $outside_pos
- $end_pos
- @inside_pos ctx cctx_save_token_pos = ;
- ctx "(" ")" cctx_go_to_matching ;
- @outside_pos ctx cctx_save_token_pos = ;
- if ctx lctx type_idx ret_type_idx ret_name 0 _cctx_parse_declarator {
- @type_idx ret_type_idx ** = ;
- }
- @end_pos ctx cctx_save_token_pos = ;
- ctx inside_pos cctx_restore_token_pos ;
- ctx lctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator "_cctx_parse_declarator: invalid syntax 1" assert_msg ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ")" strcmp 0 == "_cctx_parse_declarator: error 1" assert_msg ;
- outside_pos ctx cctx_save_token_pos == "_cctx_parse_declarator: invalid syntax 2" assert_msg ;
- ctx end_pos cctx_restore_token_pos ;
- }
- @processed 1 = ;
- }
- # Parse array declaration
- if tok "[" strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- $length
- if tok "]" strcmp 0 == {
- @length 0xffffffff = ;
- } else {
- ctx cctx_give_back_token ;
- $ast
- @ast ctx "]" cctx_parse_ast1 = ;
- @length ctx lctx ast ast_eval_compile = ;
- ast ast_destroy ;
- @tok ctx cctx_get_token_or_fail = ;
- }
- tok "]" strcmp 0 == "_cctx_parse_declarator: expected ] after array subscript" assert_msg ;
- if ctx lctx type_idx ret_type_idx ret_name 0 _cctx_parse_declarator {
- @type_idx ret_type_idx ** = ;
- }
- ret_type_idx ctx type_idx length cctx_get_array_type = ;
- @processed 1 = ;
- }
- # Parse the actual declarator identifier
- if tok is_valid_identifier {
- if ctx lctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator ! {
- ret_type_idx type_idx = ;
- }
- ret_name ** 0 == "_cctx_parse_declarator: more than one identifier found" assert_msg ;
- ret_name tok = ;
- @processed 1 = ;
- }
- if processed ! {
- ctx cctx_give_back_token ;
- #"_cctx_parse_declarator: failed\n" log ;
- 0 ret ;
- }
- #"_cctx_parse_declarator: success\n" log ;
- 1 ret ;
- }
- fun cctx_parse_declarator 6 {
- $ctx
- $lctx
- $type_idx
- $ret_type_idx
- $ret_name
- $ret_arg_names
- @ctx 5 param = ;
- @lctx 4 param = ;
- @type_idx 3 param = ;
- @ret_type_idx 2 param = ;
- @ret_name 1 param = ;
- @ret_arg_names 0 param = ;
- ret_name 0 = ;
- ctx lctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator ret ;
- }
- fun cctx_type_footprint 2 {
- $ctx
- $type_idx
- @ctx 1 param = ;
- @type_idx 0 param = ;
- $type
- @type ctx CCTX_TYPES take type_idx vector_at = ;
- $size
- @size type TYPE_SIZE take = ;
- size 0xffffffff != "cctx_type_footprint: type cannot be instantiated" assert_msg ;
- size 1 - 3 | 1 + ret ;
- }
- fun cctx_type_size 2 {
- $ctx
- $type_idx
- @ctx 1 param = ;
- @type_idx 0 param = ;
- $type
- @type ctx CCTX_TYPES take type_idx vector_at = ;
- $size
- @size type TYPE_SIZE take = ;
- size 0xffffffff != "cctx_type_size: type cannot be instantiated" assert_msg ;
- size ret ;
- }
- const STACK_ELEM_NAME 0
- const STACK_ELEM_TYPE_IDX 4
- const STACK_ELEM_LOC 8
- const SIZEOF_STACK_ELEM 12
- fun stack_elem_init 0 {
- $elem
- @elem SIZEOF_STACK_ELEM malloc = ;
- elem ret ;
- }
- fun stack_elem_destroy 1 {
- $elem
- @elem 0 param = ;
- elem free ;
- }
- const LCTX_STACK 0
- const LCTX_RETURN_TYPE_IDX 4
- const LCTX_RETURN_LABEL 8
- const LCTX_BREAK_LABEL 12
- const LCTX_CONTINUE_LABEL 16
- const LCTX_RETURNS_OBJ 20
- const LCTX_GOTO_LABELS 24
- const LCTX_DEFAULT_LABEL 28
- const LCTX_CASE_LABELS 32
- const SIZEOF_LCTX 36
- fun lctx_init 0 {
- $lctx
- @lctx SIZEOF_LCTX malloc = ;
- lctx LCTX_STACK take_addr 4 vector_init = ;
- lctx LCTX_GOTO_LABELS take_addr map_init = ;
- lctx LCTX_DEFAULT_LABEL take_addr 0xffffffff = ;
- lctx LCTX_CASE_LABELS take_addr 0 = ;
- lctx ret ;
- }
- fun lctx_destroy 1 {
- $lctx
- @lctx 0 param = ;
- $stack
- @stack lctx LCTX_STACK take = ;
- $i
- @i 0 = ;
- while i stack vector_size < {
- stack i vector_at stack_elem_destroy ;
- @i i 1 + = ;
- }
- lctx LCTX_STACK take vector_destroy ;
- lctx LCTX_GOTO_LABELS take map_destroy ;
- lctx free ;
- }
- fun lctx_stack_pos 1 {
- $lctx
- @lctx 0 param = ;
- $stack
- @stack lctx LCTX_STACK take = ;
- stack stack vector_size 1 - vector_at STACK_ELEM_LOC take ret ;
- }
- fun lctx_gen_label 2 {
- $lctx
- $ctx
- @lctx 1 param = ;
- @ctx 0 param = ;
- ctx 0xffffffff 0xffffffff cctx_gen_label ret ;
- }
- fun lctx_fix_label 3 {
- $lctx
- $ctx
- $idx
- @lctx 2 param = ;
- @ctx 1 param = ;
- @idx 0 param = ;
- $loc
- $pos
- @loc ctx CCTX_CURRENT_LOC take = ;
- if lctx 0 != {
- @pos lctx lctx_stack_pos = ;
- } else {
- @pos 0 = ;
- }
- ctx idx loc pos cctx_fix_label ;
- }
- fun lctx_get_variable 2 {
- $lctx
- $name
- @lctx 1 param = ;
- @name 0 param = ;
- # Begin scanning the stack from the end, so that inner variables
- # mask outer ones
- $stack
- @stack lctx LCTX_STACK take = ;
- $i
- @i stack vector_size 1 - = ;
- while i 0 >= {
- $elem
- @elem stack i vector_at = ;
- if elem STACK_ELEM_NAME take name strcmp 0 == {
- elem ret ;
- }
- @i i 1 - = ;
- }
- 0 ret ;
- }
- fun lctx_save_status 2 {
- $lctx
- $ctx
- @lctx 1 param = ;
- @ctx 0 param = ;
- lctx LCTX_STACK take vector_size ret ;
- }
- fun lctx_restore_status 3 {
- $lctx
- $ctx
- $status
- @lctx 2 param = ;
- @ctx 1 param = ;
- @status 0 param = ;
- $current_pos
- @current_pos lctx lctx_stack_pos = ;
- $stack
- @stack lctx LCTX_STACK take = ;
- status stack vector_size <= "lctx_restore_status: error 1" assert_msg ;
- $new_pos
- @new_pos stack status 1 - vector_at STACK_ELEM_LOC take = ;
- $rewind
- @rewind new_pos current_pos - = ;
- rewind 0 >= "lctx_restore_status: error 2" assert_msg ;
- # add esp, rewind
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx rewind cctx_emit32 ;
- # Drop enough stack elements in excess
- while stack vector_size status > {
- $elem
- @elem stack vector_pop_back = ;
- elem stack_elem_destroy ;
- }
- }
- fun lctx_push_var 3 {
- $lctx
- $ctx
- $type_idx
- $name
- @lctx 3 param = ;
- @ctx 2 param = ;
- @type_idx 1 param = ;
- @name 0 param = ;
- $footprint
- @footprint ctx type_idx cctx_type_footprint = ;
- $new_pos
- @new_pos lctx lctx_stack_pos footprint - = ;
- $elem
- @elem stack_elem_init = ;
- elem STACK_ELEM_NAME take_addr name = ;
- elem STACK_ELEM_TYPE_IDX take_addr type_idx = ;
- elem STACK_ELEM_LOC take_addr new_pos = ;
- lctx LCTX_STACK take elem vector_push_back ;
- # sub esp, footprint
- ctx 0x81 cctx_emit ;
- ctx 0xec cctx_emit ;
- ctx footprint cctx_emit32 ;
- }
- fun lctx_prime_stack 3 {
- $lctx
- $ctx
- $type_idx
- $arg_names
- @lctx 3 param = ;
- @ctx 2 param = ;
- @type_idx 1 param = ;
- @arg_names 0 param = ;
- $type
- @type ctx type_idx cctx_get_type = ;
- type TYPE_KIND take TYPE_KIND_FUNCTION == "lctx_prime_stack: type is not a function" assert_msg ;
- $args
- @args type TYPE_ARGS take = ;
- args vector_size arg_names vector_size == "lctx_prime_stack: error 1" assert_msg ;
- $stack
- @stack lctx LCTX_STACK take = ;
- $return_type_idx
- @return_type_idx type TYPE_BASE take = ;
- $return_type
- @return_type ctx return_type_idx cctx_get_type = ;
- lctx LCTX_RETURNS_OBJ take_addr return_type TYPE_KIND take TYPE_KIND_STRUCT == return_type TYPE_KIND take TYPE_KIND_UNION == || = ;
- return_type_idx TYPE_VOID == return_type_idx is_integer_type || return_type TYPE_KIND take TYPE_KIND_POINTER == || return_type TYPE_KIND take TYPE_KIND_STRUCT == || return_type TYPE_KIND take TYPE_KIND_UNION == || "cctx_compile_function: return type must be void, integer, pointer, struct or union" assert_msg ;
- # Base footprint contains the saved EBP and return value; if the
- # function returns an object, it contains also the returned value
- # address
- $base_footprint
- @base_footprint 8 = ;
- if return_type TYPE_KIND take TYPE_KIND_UNION == return_type TYPE_KIND take TYPE_KIND_STRUCT == || {
- @base_footprint base_footprint 4 + = ;
- }
- $i
- @i 0 = ;
- $total_footprint
- @total_footprint 0 = ;
- while i args vector_size < {
- @total_footprint total_footprint ctx args i vector_at cctx_type_footprint + = ;
- @i i 1 + = ;
- }
- $loc
- @loc total_footprint base_footprint + = ;
- @i args vector_size 1 - = ;
- while i 0 >= {
- $this_type_idx
- @this_type_idx args i vector_at = ;
- $name
- @name arg_names i vector_at = ;
- name 0 != "lctx_prime_stack: name cannot be empty" assert_msg ;
- @loc loc ctx this_type_idx cctx_type_footprint - = ;
- $elem
- @elem stack_elem_init = ;
- elem STACK_ELEM_NAME take_addr name = ;
- elem STACK_ELEM_TYPE_IDX take_addr this_type_idx = ;
- elem STACK_ELEM_LOC take_addr loc = ;
- stack elem vector_push_back ;
- @i i 1 - = ;
- }
- loc base_footprint == "lctx_prime_stack: error 2" assert_msg ;
- # Add a fictious element to mark the beginning of local variables
- $elem
- @elem stack_elem_init = ;
- elem STACK_ELEM_NAME take_addr "" = ;
- elem STACK_ELEM_TYPE_IDX take_addr 0 = ;
- elem STACK_ELEM_LOC take_addr 0 = ;
- stack elem vector_push_back ;
- }
- fun lctx_gen_prologue 2 {
- $lctx
- $ctx
- @lctx 1 param = ;
- @ctx 0 param = ;
- # push ebp; mov ebp, esp
- ctx 0x55 cctx_emit ;
- ctx 0x89 cctx_emit ;
- ctx 0xe5 cctx_emit ;
- }
- fun lctx_gen_epilogue 2 {
- $lctx
- $ctx
- @lctx 1 param = ;
- @ctx 0 param = ;
- if lctx LCTX_RETURNS_OBJ take {
- # mov eax, [ebp+8]; pop ebp; ret 4
- ctx 0x8b cctx_emit ;
- ctx 0x45 cctx_emit ;
- ctx 0x08 cctx_emit ;
- ctx 0x5d cctx_emit ;
- ctx 0xc2 cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x00 cctx_emit ;
- } else {
- # pop ebp; ret
- ctx 0x5d cctx_emit ;
- ctx 0xc3 cctx_emit ;
- }
- }
- fun cctx_write_label 2 {
- $ctx
- $idx
- @ctx 1 param = ;
- @idx 0 param = ;
- $buf
- @buf ctx CCTX_LABEL_BUF take = ;
- buf '.' =c ;
- buf 1 + 'L' =c ;
- idx itoa buf 2 + strcpy ;
- buf ret ;
- }
- fun cctx_gen_label 3 {
- $ctx
- $loc
- $pos
- @ctx 2 param = ;
- @loc 1 param = ;
- @pos 0 param = ;
- $label_pos
- @label_pos ctx CCTX_LABEL_POS take = ;
- $idx
- @idx ctx CCTX_LABEL_NUM take = ;
- $name
- @name ctx idx cctx_write_label = ;
- if ctx CCTX_STAGE take 0 == {
- idx label_pos vector_size == "cctx_gen_label: error 2" assert_msg ;
- label_pos pos vector_push_back ;
- ctx name loc TYPE_VOID cctx_add_global ;
- } else {
- idx label_pos vector_size < "cctx_gen_label: error 1" assert_msg ;
- if pos 0xffffffff != {
- label_pos idx vector_at pos == "cctx_gen_label: error 3" assert_msg ;
- }
- if ctx CCTX_STAGE take 2 == {
- # Check that in the end the label position has been set
- # (otherwise the label was never actually defined)
- label_pos idx vector_at 0xffffffff != "cctx_gen_label: label never defined" assert_msg ;
- }
- ctx name loc TYPE_VOID cctx_add_global ;
- }
- ctx CCTX_LABEL_NUM take_addr idx 1 + = ;
- idx ret ;
- }
- fun cctx_fix_label 4 {
- $ctx
- $idx
- $loc
- $pos
- @ctx 3 param = ;
- @idx 2 param = ;
- @loc 1 param = ;
- @pos 0 param = ;
- $label_pos
- @label_pos ctx CCTX_LABEL_POS take = ;
- $name
- @name ctx idx cctx_write_label = ;
- ctx name loc TYPE_VOID cctx_add_global ;
- if ctx CCTX_STAGE take 0 == {
- label_pos idx vector_at_addr pos = ;
- } else {
- label_pos idx vector_at pos == "cctx_fix_label: error 1" assert_msg ;
- }
- }
- const JUMP_TYPE_JMP 0
- const JUMP_TYPE_CALL 1
- const JUMP_TYPE_JZ 2
- const JUMP_TYPE_JNZ 3
- fun cctx_gen_jump_loc 3 {
- $ctx
- $target_loc
- $type
- @ctx 2 param = ;
- @target_loc 1 param = ;
- @type 0 param = ;
- if type JUMP_TYPE_JMP == {
- # jmp rel
- ctx 0xe9 cctx_emit ;
- } else {
- if type JUMP_TYPE_CALL == {
- # call rel
- ctx 0xe8 cctx_emit ;
- } else {
- if type JUMP_TYPE_JZ == {
- # jz rel
- ctx 0x0f cctx_emit ;
- ctx 0x84 cctx_emit ;
- } else {
- if type JUMP_TYPE_JNZ == {
- # jnz rel
- ctx 0x0f cctx_emit ;
- ctx 0x85 cctx_emit ;
- } else {
- 0 "cctx_gen_dump: error 1" assert_msg ;
- }
- }
- }
- }
- $current_loc
- @current_loc ctx CCTX_CURRENT_LOC take 4 + = ;
- $rel
- @rel target_loc current_loc - = ;
- ctx rel cctx_emit32 ;
- }
- fun cctx_gen_jump 3 {
- $ctx
- $name
- $type
- @ctx 2 param = ;
- @name 1 param = ;
- @type 0 param = ;
- $global
- @global ctx name cctx_get_global = ;
- $target_loc
- @target_loc global GLOBAL_LOC take = ;
- ctx target_loc type cctx_gen_jump_loc ;
- }
- fun cctx_gen_label_jump 5 {
- $ctx
- $lctx
- $idx
- $type
- $rewind
- @ctx 4 param = ;
- @lctx 3 param = ;
- @idx 2 param = ;
- @type 1 param = ;
- @rewind 0 param = ;
- $current_pos
- if lctx 0 != {
- @current_pos lctx lctx_stack_pos = ;
- } else {
- @current_pos 0 = ;
- }
- $new_pos
- @new_pos ctx CCTX_LABEL_POS take idx vector_at = ;
- $pos_diff
- @pos_diff new_pos current_pos - = ;
- $name
- @name ctx idx cctx_write_label = ;
- # When rewinding on a conditional jump, the add will modify the
- # flags, so we have to back them up
- if rewind {
- if type JUMP_TYPE_JZ == type JUMP_TYPE_JNZ == || {
- # lahf
- ctx 0x9f cctx_emit ;
- }
- # add esp, pos_diff; cctx_gen_jump
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx pos_diff cctx_emit32 ;
- if type JUMP_TYPE_JZ == type JUMP_TYPE_JNZ == || {
- # sahf
- ctx 0x9e cctx_emit ;
- }
- }
- ctx name type cctx_gen_jump ;
- }
- fun cctx_get_label_addr 3 {
- $ctx
- $lctx
- $idx
- @ctx 2 param = ;
- @lctx 1 param = ;
- @idx 0 param = ;
- $name
- @name ctx idx cctx_write_label = ;
- $global
- @global ctx name cctx_get_global = ;
- global GLOBAL_LOC take ret ;
- }
- fun string_unescaped_len 1 {
- $s
- @s 0 param = ;
- $i
- @i 0 = ;
- s **c '\"' == "string_unescaped_len: not a string literal" assert_msg ;
- @s s 1 + = ;
- while s **c '\"' != {
- @s 1 0 escape_char ;
- @i i 1 + = ;
- }
- @s s 1 + = ;
- s **c '\0' == "string_unescaped_len: illegal string literal" assert_msg ;
- i ret ;
- }
- fun cctx_gen_string 3 {
- $ctx
- $lctx
- $name
- @ctx 2 param = ;
- @lctx 1 param = ;
- @name 0 param = ;
- $str_label
- $label
- @str_label lctx ctx lctx_gen_label = ;
- @label lctx ctx lctx_gen_label = ;
- ctx lctx label JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- lctx ctx str_label lctx_fix_label ;
- $from
- @from name 1 + = ;
- while from **c '\"' != {
- @from 0 ctx escape_char ;
- }
- from 1 + **c 0 == "cctx_gen_string: illegal string literal" assert_msg ;
- ctx 0 cctx_emit ;
- lctx ctx label lctx_fix_label ;
- str_label ret ;
- }
- ifun ast_eval_compile_ext 2
- fun ast_eval_compile 3 {
- $ctx
- $lctx
- $ast
- @ctx 2 param = ;
- @lctx 1 param = ;
- @ast 0 param = ;
- $extctx
- @extctx 8 malloc = ;
- extctx ctx = ;
- extctx 4 + lctx = ;
- $value
- @value ast @ast_eval_compile_ext extctx ast_eval = ;
- value "ast_eval_compile: failed" assert_msg ;
- extctx free ;
- value ** ret ;
- }
- ifun ast_strtoll 1
- ifun ast_eval_type 3
- fun ast_eval_compile_ext 2 {
- $extctx
- $ast
- @extctx 1 param = ;
- @ast 0 param = ;
- $ctx
- $lctx
- @ctx extctx ** = ;
- @lctx extctx 4 + ** = ;
- $name
- @name ast AST_NAME take = ;
- # "ast_eval_compile_ext: " log ;
- # name log ;
- # "\n" log ;
- if ast AST_TYPE take 0 == {
- $value
- @value i64_init = ;
- # Operand
- if name is_valid_identifier {
- $enum_consts
- @enum_consts ctx CCTX_ENUM_CONSTS take = ;
- if enum_consts name map_has {
- value enum_consts name map_at i64_from_32 ;
- } else {
- $global
- @global ctx name cctx_get_global = ;
- $loc
- @loc global GLOBAL_LOC take = ;
- $type_idx
- @type_idx global GLOBAL_TYPE_IDX take = ;
- $type
- @type ctx type_idx cctx_get_type = ;
- if type TYPE_KIND take TYPE_KIND_ARRAY == type TYPE_KIND take TYPE_KIND_FUNCTION == || {
- value loc i64_from_u32 ;
- } else {
- ctx type_idx cctx_type_footprint 4 == "ast_eval_compile_ext: unsupported global" assert_msg ;
- value loc ** i64_from_u32 ;
- }
- }
- } else {
- if name **c '\"' == {
- $str_label
- @str_label ctx 0 name cctx_gen_string = ;
- value ctx 0 str_label cctx_get_label_addr i64_from_u32 ;
- } else {
- value i64_destroy ;
- ast ast_strtoll ;
- @value ast AST_VALUE take = ;
- }
- }
- value ret ;
- } else {
- # Operator
- if name "sizeof_PRE" strcmp 0 == {
- $value
- @value i64_init = ;
- $sub_type_idx
- @sub_type_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
- if ast AST_RIGHT take AST_ORIG_TYPE_IDX take 0xffffffff != {
- @sub_type_idx ast AST_RIGHT take AST_ORIG_TYPE_IDX take = ;
- }
- value ctx sub_type_idx cctx_type_size i64_from_u32 ;
- value ret ;
- }
- if name "(_PRE" strcmp 0 == {
- $type_idx
- @type_idx ast AST_CAST_TYPE_IDX take = ;
- $type
- @type ctx type_idx cctx_get_type = ;
- if type TYPE_KIND take TYPE_KIND_POINTER == {
- @type_idx TYPE_UINT = ;
- }
- type_idx is_integer_type "ast_eval_compile_ext: invalid cast to non-integer type" assert_msg ;
- $value
- @value i64_init = ;
- value ast AST_RIGHT take @ast_eval_compile_ext extctx ast_eval i64_copy ;
- $size
- @size ctx type_idx cctx_type_size = ;
- if size 1 == {
- value i64_cast_to_u8 ;
- } else {
- if size 2 == {
- value i64_cast_to_u16 ;
- } else {
- if size 4 == {
- value i64_cast_to_u32 ;
- } else {
- if size 8 == {
- # Nothing to do here...
- } else {
- 0 "ast_eval_compile_ext: error 1" assert_msg ;
- }
- }
- }
- }
- value ret ;
- }
- # Implement the special case &(((type*)x)->y) which is used in macro
- # offsetof()
- if name "&_PRE" strcmp 0 == {
- $ast2
- @ast2 ast AST_RIGHT take = ;
- $name2
- @name2 ast2 AST_NAME take = ;
- name2 "->" strcmp 0 == "ast_eval_compile_ext: & not taken on ->" name2 assert_msg_str ;
- $field
- @field ast2 AST_RIGHT take AST_NAME take = ;
- $ast3
- @ast3 ast2 AST_LEFT take = ;
- $name3
- @name3 ast3 AST_NAME take = ;
- name3 "(_PRE" strcmp 0 == "ast_eval_compile_ext: & not taken on cast" name3 assert_msg_str ;
- $type_idx
- @type_idx ast3 AST_CAST_TYPE_IDX take = ;
- $type
- @type ctx type_idx cctx_get_type = ;
- type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_compile_ext: & not taken on cast to pointer" assert_msg ;
- $base_type
- @base_type ctx type TYPE_BASE take cctx_get_type = ;
- base_type TYPE_KIND take TYPE_KIND_STRUCT == base_type TYPE_KIND take TYPE_KIND_UNION == || "ast_eval_compile_ext: & not taken on cast to pointer to struct" assert_msg ;
- $idx
- @idx base_type field type_get_idx = ;
- $value2
- @value2 i64_init = ;
- value2 base_type TYPE_FIELDS_OFFS take idx vector_at i64_from_u32 ;
- $value
- @value i64_init = ;
- value ast3 @ast_eval_compile_ext extctx ast_eval i64_copy ;
- value value2 i64_add ;
- value2 i64_destroy ;
- value ret ;
- }
- 0 ret ;
- }
- 0 "ast_eval_compile_ext: should not arrive here" assert_msg ;
- }
- fun promote_integer_type 1 {
- $type
- @type 0 param = ;
- if type TYPE_BOOL ==
- type TYPE_CHAR == ||
- type TYPE_SCHAR == ||
- type TYPE_UCHAR == ||
- type TYPE_SHORT == ||
- type TYPE_INT == ||
- type TYPE_USHORT == || {
- TYPE_INT ret ;
- }
- if type TYPE_UINT == {
- TYPE_UINT ret ;
- }
- if type TYPE_LONG == {
- TYPE_LONG ret ;
- }
- if type TYPE_ULONG == {
- TYPE_ULONG ret ;
- }
- 0 "promote_integer_type: not an integer type" assert_msg ;
- }
- fun ast_arith_conv 4 {
- $ast1
- $ast2
- $ctx
- $lctx
- @ast1 3 param = ;
- @ast2 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $type1
- $type2
- @type1 ast1 ctx lctx ast_eval_type = ;
- @type2 ast2 ctx lctx ast_eval_type = ;
- # As an exception to the standard, automatically convert every
- # pointer to an unsigned int
- if ctx type1 cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == {
- @type1 TYPE_UINT = ;
- }
- if ctx type2 cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == {
- @type2 TYPE_UINT = ;
- }
- @type1 type1 promote_integer_type = ;
- @type2 type2 promote_integer_type = ;
- if type1 type2 == {
- type1 ret ;
- }
- if type1 TYPE_UINT == type2 TYPE_INT == &&
- type2 TYPE_UINT == type1 TYPE_INT == && || {
- TYPE_UINT ret ;
- }
- if type1 TYPE_ULONG == type2 TYPE_LONG == &&
- type2 TYPE_ULONG == type1 TYPE_LONG == && || {
- TYPE_ULONG ret ;
- }
- if type1 TYPE_UINT == type2 TYPE_ULONG == &&
- type2 TYPE_UINT == type1 TYPE_ULONG == && || {
- TYPE_ULONG ret ;
- }
- if type1 TYPE_INT == type2 TYPE_LONG == &&
- type2 TYPE_INT == type1 TYPE_LONG == && || {
- TYPE_LONG ret ;
- }
- if type1 TYPE_INT == type2 TYPE_ULONG == &&
- type2 TYPE_INT == type1 TYPE_ULONG == && || {
- TYPE_ULONG ret ;
- }
- if type1 TYPE_UINT == type2 TYPE_LONG == &&
- type2 TYPE_UINT == type1 TYPE_LONG == && || {
- TYPE_LONG ret ;
- }
- 0 "ast_arith_conv: error 1" type1 type2 assert_msg_int_int ;
- }
- fun i64_fits_in 2 {
- $i
- $type_idx
- @i 1 param = ;
- @type_idx 0 param = ;
- if type_idx TYPE_ULONG == { 1 ret ; }
- if type_idx TYPE_LONG == { i i64_to_upper32 0x80000000 & 0 == ret ; }
- if type_idx TYPE_UINT == { i i64_to_upper32 0 == ret ; }
- if type_idx TYPE_INT == { i i64_to_upper32 0 == i i64_to_32 0x80000000 & 0 == && ret ; }
- 0 "i64_fits_in: unsupported type" assert_msg ;
- }
- fun ast_strtoll 1 {
- $ast
- @ast 0 param = ;
- $name
- @name ast AST_NAME take = ;
- if name **c '\'' == {
- $data
- $from
- $to
- @data 0 = ;
- @from name 1 + = ;
- @to @data = ;
- @from @to 0 escape_char ;
- to @data 1 + == "ast_push_value: invalid character literal 1" assert_msg ;
- from **c '\'' == "ast_push_value: invalid character literal 2" assert_msg ;
- from 1 + **c 0 == "ast_push_value: invalid character literal 3" assert_msg ;
- $value
- @value i64_init = ;
- value data i64_from_32 ;
- ast AST_VALUE take_addr value = ;
- ast AST_TYPE_IDX take_addr TYPE_INT = ;
- ret ;
- }
- $suffix
- $value
- @value name @suffix c_strtoll = ;
- # FIXME Why is it called twice on the same AST?
- if ast AST_VALUE take 0 != {
- ast AST_VALUE take i64_destroy ;
- }
- ast AST_VALUE take_addr value = ;
- # Decode suffix
- $u_num
- $l_num
- @u_num 0 = ;
- @l_num 0 = ;
- while suffix **c '\0' != {
- $c
- @c suffix **c = ;
- if c 'u' == c 'U' == || {
- @u_num u_num 1 + = ;
- } else {
- if c 'l' == c 'L' == || {
- @l_num l_num 1 + = ;
- } else {
- 0 "ast_strtoll: invalid suffix letter" name assert_msg_str ;
- }
- }
- @suffix suffix 1 + = ;
- }
- 0 u_num <= u_num 1 <= && "ast_strtoll: too many U suffixes" assert_msg ;
- 0 l_num <= l_num 2 <= && "ast_strtoll: too many L suffixes" assert_msg ;
- # (Re)decode prefix
- $dec
- @dec ast AST_NAME take **c '0' != = ;
- # Ok, now we are ready to assign a type to the expression
- $type_idx
- if u_num 1 == {
- if l_num 2 == {
- @type_idx TYPE_ULONG = ;
- } else {
- if value TYPE_UINT i64_fits_in {
- @type_idx TYPE_UINT = ;
- } else {
- @type_idx TYPE_ULONG = ;
- }
- }
- } else {
- if l_num 2 == {
- if dec {
- @type_idx TYPE_LONG = ;
- } else {
- if value TYPE_LONG i64_fits_in {
- @type_idx TYPE_LONG = ;
- } else {
- @type_idx TYPE_ULONG = ;
- }
- }
- } else {
- if dec {
- if value TYPE_INT i64_fits_in {
- @type_idx TYPE_INT = ;
- } else {
- if value TYPE_LONG i64_fits_in {
- @type_idx TYPE_LONG = ;
- } else {
- @type_idx TYPE_ULONG = ;
- }
- }
- } else {
- if value TYPE_INT i64_fits_in {
- @type_idx TYPE_INT = ;
- } else {
- if value TYPE_UINT i64_fits_in {
- @type_idx TYPE_UINT = ;
- } else {
- if value TYPE_LONG i64_fits_in {
- @type_idx TYPE_LONG = ;
- } else {
- @type_idx TYPE_ULONG = ;
- }
- }
- }
- }
- }
- }
- ast AST_TYPE_IDX take_addr type_idx = ;
- }
- fun ast_eval_type 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- if ast AST_TYPE_IDX take 0xffffffff != {
- ast AST_TYPE_IDX take ret ;
- }
- $name
- @name ast AST_NAME take = ;
- $type_idx
- $common_type_idx
- @common_type_idx 0xffffffff = ;
- if ast AST_TYPE take 0 == {
- # Operand
- if name is_valid_identifier {
- # Search among enum constants
- $enum_consts
- @enum_consts ctx CCTX_ENUM_CONSTS take = ;
- if enum_consts name map_has name "sizeof" strcmp 0 == || {
- @type_idx TYPE_INT = ;
- } else {
- # Search in local stack and among globals
- $elem
- if lctx 0 != {
- @elem lctx name lctx_get_variable = ;
- } else {
- @elem 0 = ;
- }
- if elem {
- @type_idx elem STACK_ELEM_TYPE_IDX take = ;
- } else {
- $global
- @global ctx name cctx_get_global = ;
- @type_idx global GLOBAL_TYPE_IDX take = ;
- }
- }
- } else {
- if name **c '\"' == {
- @type_idx ctx TYPE_CHAR name string_unescaped_len 1 + cctx_get_array_type = ;
- } else {
- ast ast_strtoll ;
- @type_idx ast AST_TYPE_IDX take = ;
- }
- }
- } else {
- # Operator
- $processed
- @processed 0 = ;
- $sum
- $subtract
- $assign
- @sum name "+" strcmp 0 == name "+=" strcmp 0 == || = ;
- @subtract name "-" strcmp 0 == name "-=" strcmp 0 == || = ;
- @assign name "+=" strcmp 0 == name "-=" strcmp 0 == || = ;
- if sum subtract ||
- processed ! && {
- $left_idx
- $right_idx
- @left_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @right_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
- $left_ptr
- $right_ptr
- @left_ptr ctx left_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
- @right_ptr ctx right_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
- $left_size
- $right_size
- if left_ptr {
- # Allow void to be considered of size 1
- if ctx left_idx cctx_get_type TYPE_BASE take TYPE_VOID == {
- @left_size 1 = ;
- } else {
- @left_size ctx ctx left_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
- }
- }
- if right_ptr {
- if ctx right_idx cctx_get_type TYPE_BASE take TYPE_VOID == {
- @right_size 1 = ;
- } else {
- @right_size ctx ctx right_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
- }
- }
- if sum {
- left_ptr right_ptr && ! "ast_eval_type: cannot take sum of two pointers" assert_msg ;
- if left_ptr {
- right_idx is_integer_type "ast_eval_type: cannot add pointer and non-integer" assert_msg ;
- @type_idx left_idx = ;
- @processed 1 = ;
- }
- if right_ptr {
- assign ! "ast_eval_type: right of sum-assign must be integer" assert_msg ;
- left_idx is_integer_type "ast_eval_type: cannot add pointer and non-integer" assert_msg ;
- @type_idx right_idx = ;
- @processed 1 = ;
- }
- } else {
- left_ptr ! right_ptr && ! "ast_eval_type: cannot take different of a non-pointer and a pointer" assert_msg ;
- if left_ptr {
- if right_ptr {
- assign ! "ast_eval_type: right of subtract-assign must be integer" assert_msg ;
- left_size right_size == "ast_eval_type: cannot take difference of pointers to types of different size" assert_msg ;
- @type_idx TYPE_INT = ;
- } else {
- right_idx is_integer_type "ast_eval_type: cannot subtract pointer and non-integer" assert_msg ;
- @type_idx left_idx = ;
- }
- @processed 1 = ;
- }
- }
- }
- if name "*" strcmp 0 ==
- name "/" strcmp 0 == ||
- name "%" strcmp 0 == ||
- name "+" strcmp 0 == ||
- name "-" strcmp 0 == ||
- name "&" strcmp 0 == ||
- name "^" strcmp 0 == ||
- name "|" strcmp 0 == ||
- name "*=" strcmp 0 == ||
- name "/=" strcmp 0 == ||
- name "%=" strcmp 0 == ||
- name "+=" strcmp 0 == ||
- name "-=" strcmp 0 == ||
- name "&=" strcmp 0 == ||
- name "^=" strcmp 0 == ||
- name "|=" strcmp 0 == ||
- processed ! && {
- @type_idx ast AST_LEFT take ast AST_RIGHT take ctx lctx ast_arith_conv = ;
- @common_type_idx type_idx = ;
- @processed 1 = ;
- }
- if name "<<" strcmp 0 ==
- name ">>" strcmp 0 == ||
- name "<<=" strcmp 0 == ||
- name ">>=" strcmp 0 == ||
- processed ! && {
- $type1
- $type2
- @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
- @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
- @type1 type1 promote_integer_type = ;
- @type2 type2 promote_integer_type = ;
- @type_idx type1 = ;
- @common_type_idx type_idx = ;
- @processed 1 = ;
- }
- if name "+_PRE" strcmp 0 ==
- name "-_PRE" strcmp 0 == ||
- name "~_PRE" strcmp 0 == ||
- processed ! && {
- @type_idx ast AST_RIGHT take ctx lctx ast_eval_type promote_integer_type = ;
- @common_type_idx type_idx = ;
- @processed 1 = ;
- }
- if name "++_PRE" strcmp 0 ==
- name "++_POST" strcmp 0 == ||
- name "--_PRE" strcmp 0 == ||
- name "--_POST" strcmp 0 == ||
- processed ! && {
- $sub_type_idx
- if name "++_PRE" strcmp 0 == name "--_PRE" strcmp 0 == || {
- @sub_type_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
- } else {
- @sub_type_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- }
- if sub_type_idx is_integer_type {
- @type_idx sub_type_idx promote_integer_type = ;
- } else {
- ctx sub_type_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: argument must be integer or pointer" assert_msg ;
- @type_idx sub_type_idx = ;
- }
- @common_type_idx type_idx = ;
- @processed 1 = ;
- }
- if name "<" strcmp 0 ==
- name ">" strcmp 0 == ||
- name "<=" strcmp 0 == ||
- name ">=" strcmp 0 == ||
- name "==" strcmp 0 == ||
- name "!=" strcmp 0 == ||
- name "&&" strcmp 0 == ||
- name "||" strcmp 0 == ||
- processed ! && {
- $type1
- $type2
- @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
- @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
- type1 is_integer_type ctx type1 cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == || "ast_eval_type: left is neither integer nor pointer" assert_msg ;
- type2 is_integer_type ctx type2 cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == || "ast_eval_type: left is neither integer nor pointer" assert_msg ;
- @type_idx TYPE_INT = ;
- @common_type_idx ast AST_LEFT take ast AST_RIGHT take ctx lctx ast_arith_conv = ;
- @processed 1 = ;
- }
- if name "!_PRE" strcmp 0 ==
- processed ! && {
- $type
- @type ast AST_RIGHT take ctx lctx ast_eval_type = ;
- type is_integer_type ctx type cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == || "ast_eval_type: operand is neither integer nor pointer" assert_msg ;
- @type_idx TYPE_INT = ;
- @common_type_idx type = ;
- if ctx type cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == {
- @common_type_idx TYPE_UINT = ;
- }
- @processed 1 = ;
- }
- if name "=" strcmp 0 == {
- @type_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- $orig_type_idx
- @orig_type_idx ast AST_LEFT take AST_ORIG_TYPE_IDX take = ;
- if orig_type_idx 0xffffffff != {
- ctx orig_type_idx cctx_get_type TYPE_KIND take TYPE_KIND_ARRAY != "ast_eval_type: cannot assign to array" assert_msg ;
- }
- @processed 1 = ;
- }
- if name "(" strcmp 0 == {
- $fun_ptr_idx
- $fun_ptr_type
- $fun_idx
- $fun_type
- @fun_ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @fun_ptr_type ctx fun_ptr_idx cctx_get_type = ;
- fun_ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: left is not a pointer" assert_msg ;
- @fun_idx fun_ptr_type TYPE_BASE take = ;
- @fun_type ctx fun_idx cctx_get_type = ;
- fun_type TYPE_KIND take TYPE_KIND_FUNCTION == "ast_eval_type: left is not a pointer to function" assert_msg ;
- @type_idx fun_type TYPE_BASE take = ;
- @processed 1 = ;
- }
- if name "*_PRE" strcmp 0 == name "[" strcmp 0 == || {
- $ptr_idx
- $ptr_type
- $ptr_ast
- @ptr_ast ast AST_RIGHT take = ;
- if name "[" strcmp 0 == {
- @ptr_idx ptr_ast ctx lctx ast_eval_type = ;
- @ptr_type ctx ptr_idx cctx_get_type = ;
- if ptr_type TYPE_KIND take TYPE_KIND_POINTER != {
- @ptr_ast ast AST_LEFT take = ;
- }
- }
- @ptr_idx ptr_ast ctx lctx ast_eval_type = ;
- @ptr_type ctx ptr_idx cctx_get_type = ;
- ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: arg not a pointer" assert_msg ;
- @type_idx ptr_type TYPE_BASE take = ;
- @processed 1 = ;
- }
- if name "&_PRE" strcmp 0 == {
- $orig_idx
- @orig_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
- @type_idx ctx orig_idx cctx_get_pointer_type = ;
- @processed 1 = ;
- }
- if name "." strcmp 0 == {
- $struct_idx
- $struct_type
- @struct_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @struct_type ctx struct_idx cctx_get_type = ;
- ast AST_RIGHT take AST_TYPE take 0 == "ast_eval_type: right is not a plain name" assert_msg ;
- $name
- @name ast AST_RIGHT take AST_NAME take = ;
- $field
- @field struct_type name type_get_idx = ;
- field 0xffffffff != "ast_eval_type: specified field does not exist" assert_msg ;
- @type_idx struct_type TYPE_FIELDS_TYPE_IDXS take field vector_at = ;
- @processed 1 = ;
- }
- if name "->" strcmp 0 == {
- $ptr_idx
- $ptr_type
- $struct_idx
- $struct_type
- @ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @ptr_type ctx ptr_idx cctx_get_type = ;
- ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: right is not a pointer" assert_msg ;
- @struct_idx ptr_type TYPE_BASE take = ;
- @struct_type ctx struct_idx cctx_get_type = ;
- ast AST_RIGHT take AST_TYPE take 0 == "ast_eval_type: right is not a plain name" assert_msg ;
- $name
- @name ast AST_RIGHT take AST_NAME take = ;
- $field
- @field struct_type name type_get_idx = ;
- field 0xffffffff != "ast_eval_type: specified field does not exist" assert_msg ;
- @type_idx struct_type TYPE_FIELDS_TYPE_IDXS take field vector_at = ;
- @processed 1 = ;
- }
- if name "?" strcmp 0 == {
- $processed2
- @processed2 0 = ;
- $type1
- $type2
- @type1 ast AST_CENTER take ctx lctx ast_eval_type = ;
- @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
- # Always allow ternary operator between two things of the same type
- if type1 type2 == {
- @type_idx type1 = ;
- @processed2 1 = ;
- }
- if type1 is_integer_type type2 is_integer_type && {
- @type_idx ast AST_CENTER take ast AST_RIGHT take ctx lctx ast_arith_conv = ;
- @processed2 1 = ;
- }
- $t1
- $t2
- @t1 ctx type1 cctx_get_type = ;
- @t2 ctx type2 cctx_get_type = ;
- if t1 TYPE_KIND take TYPE_KIND_POINTER == t2 TYPE_KIND take TYPE_KIND_POINTER == && {
- if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare {
- @type_idx type1 = ;
- } else {
- # If the pointed types are different, do not bother too much
- # and just return void*
- @type_idx TYPE_VOID_PTR = ;
- }
- @processed2 1 = ;
- }
- processed2 "ast_eval_type: not implemented form of ternary operator" type1 type2 assert_msg_int_int ;
- @processed 1 = ;
- }
- if name "(_PRE" strcmp 0 == {
- @type_idx ast AST_CAST_TYPE_IDX take = ;
- @processed 1 = ;
- }
- if name "sizeof_PRE" strcmp 0 == {
- @type_idx TYPE_INT = ;
- @processed 1 = ;
- }
- if name "," strcmp 0 == {
- $type1
- $type2
- @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
- @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
- @type_idx type2 = ;
- @processed 1 = ;
- }
- processed "ast_eval_type: not implemented" name assert_msg_str ;
- }
- # Process decaying
- $type
- @type ctx type_idx cctx_get_type = ;
- $orig_type_idx
- @orig_type_idx 0xffffffff = ;
- if type TYPE_KIND take TYPE_KIND_ARRAY == {
- @orig_type_idx type_idx = ;
- $base_idx
- @base_idx type TYPE_BASE take = ;
- @type_idx ctx base_idx cctx_get_pointer_type = ;
- }
- if type TYPE_KIND take TYPE_KIND_FUNCTION == {
- @orig_type_idx type_idx = ;
- @type_idx ctx type_idx cctx_get_pointer_type = ;
- }
- @type ctx type_idx cctx_get_type = ;
- # Sanity check
- type_idx TYPE_VOID == type TYPE_SIZE take 0xffffffff != || "ast_eval_type: invalid expression type" assert_msg ;
- ast AST_TYPE_IDX take_addr type_idx = ;
- ast AST_ORIG_TYPE_IDX take_addr orig_type_idx = ;
- ast AST_COMMON_TYPE_IDX take_addr common_type_idx = ;
- type_idx ret ;
- }
- ifun ast_push_value 3
- ifun ast_push_value_ptr 3
- fun ast_push_addr 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $name
- @name ast AST_NAME take = ;
- $type_idx
- @type_idx ast ctx lctx ast_eval_type = ;
- if ast AST_TYPE take 0 == {
- # Operand
- if name is_valid_identifier {
- # Search in local stack and among globals
- $elem
- @elem lctx name lctx_get_variable = ;
- if elem {
- # lea eax, [ebp+loc]; push eax
- ctx 0x8d cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx elem STACK_ELEM_LOC take cctx_emit32 ;
- ctx 0x50 cctx_emit ;
- } else {
- $global
- @global ctx name cctx_get_global = ;
- # push loc
- ctx 0x68 cctx_emit ;
- ctx global GLOBAL_LOC take cctx_emit32 ;
- }
- } else {
- if name **c '\"' == {
- $str_label
- @str_label ctx lctx name cctx_gen_string = ;
- # push str_label
- ctx 0x68 cctx_emit ;
- ctx ctx lctx str_label cctx_get_label_addr cctx_emit32 ;
- } else {
- 0 "ast_push_addr: cannot take the address of an immediate" assert_msg ;
- }
- }
- } else {
- # Operator
- $processed
- @processed 0 = ;
- if name "*_PRE" strcmp 0 == {
- ast AST_RIGHT take ctx lctx ast_push_value ;
- @processed 1 = ;
- }
- if name "." strcmp 0 == {
- $struct_idx
- $struct_type
- @struct_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @struct_type ctx struct_idx cctx_get_type = ;
- ast AST_RIGHT take AST_TYPE take 0 == "ast_push_addr: right is not a plain name" assert_msg ;
- $name
- @name ast AST_RIGHT take AST_NAME take = ;
- $field
- @field struct_type name type_get_idx = ;
- field 0xffffffff != "ast_push_addr: specified field does not exist" assert_msg ;
- $off
- @off struct_type TYPE_FIELDS_OFFS take field vector_at = ;
- # ast_push_addr; pop eax; add eax, off; push eax
- ast AST_LEFT take ctx lctx ast_push_addr ;
- ctx 0x58 cctx_emit ;
- ctx 0x05 cctx_emit ;
- ctx off cctx_emit32 ;
- ctx 0x50 cctx_emit ;
- @processed 1 = ;
- }
- if name "->" strcmp 0 == {
- $ptr_idx
- $ptr_type
- $struct_idx
- $struct_type
- @ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @ptr_type ctx ptr_idx cctx_get_type = ;
- ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_push_addr: right is not a pointer" assert_msg ;
- @struct_idx ptr_type TYPE_BASE take = ;
- @struct_type ctx struct_idx cctx_get_type = ;
- ast AST_RIGHT take AST_TYPE take 0 == "ast_push_addr: right is not a plain name" assert_msg ;
- $name
- @name ast AST_RIGHT take AST_NAME take = ;
- $field
- @field struct_type name type_get_idx = ;
- field 0xffffffff != "ast_push_addr: specified field does not exist" assert_msg ;
- $off
- @off struct_type TYPE_FIELDS_OFFS take field vector_at = ;
- # ast_push_value; pop eax; add eax, off; push eax
- ast AST_LEFT take ctx lctx ast_push_value ;
- ctx 0x58 cctx_emit ;
- ctx 0x05 cctx_emit ;
- ctx off cctx_emit32 ;
- ctx 0x50 cctx_emit ;
- @processed 1 = ;
- }
- if name "[" strcmp 0 == {
- @processed ast ctx lctx ast_push_value_ptr = ;
- }
- processed "ast_push_addr: not implemented" assert_msg ;
- }
- }
- fun lctx_int_convert 4 {
- $lctx
- $ctx
- $from_idx
- $to_idx
- @lctx 3 param = ;
- @ctx 2 param = ;
- @from_idx 1 param = ;
- @to_idx 0 param = ;
- # Treat pointers as integers
- if ctx from_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == {
- @from_idx TYPE_INT = ;
- }
- from_idx is_integer_type "lctx_int_convert: source is not an integer type" from_idx to_idx assert_msg_int_int ;
- to_idx is_integer_type "lctx_int_convert: target is not an integer type" from_idx to_idx assert_msg_int_int ;
- if ctx from_idx cctx_type_footprint 4 == {
- # pop eax
- ctx 0x58 cctx_emit ;
- } else {
- if ctx from_idx cctx_type_footprint 8 == {
- # pop eax; pop edx
- ctx 0x58 cctx_emit ;
- ctx 0x5a cctx_emit ;
- } else {
- 0 "lctx_int_convert: error 1" assert_msg ;
- }
- }
- if from_idx TYPE_CHAR == from_idx TYPE_SCHAR == || {
- # movsx eax, al; cdq
- ctx 0x0f cctx_emit ;
- ctx 0xbe cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0x99 cctx_emit ;
- } else {
- if from_idx TYPE_UCHAR == {
- # movzx eax, al; xor edx, edx
- ctx 0x0f cctx_emit ;
- ctx 0xb6 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0x31 cctx_emit ;
- ctx 0xd2 cctx_emit ;
- } else {
- if from_idx TYPE_SHORT == {
- # movsx eax, ax; cdq
- ctx 0x0f cctx_emit ;
- ctx 0xbf cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0x99 cctx_emit ;
- } else {
- if from_idx TYPE_USHORT == {
- # movzx eax, ax; xor edx, edx
- ctx 0x0f cctx_emit ;
- ctx 0xb7 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0x31 cctx_emit ;
- ctx 0xd2 cctx_emit ;
- } else {
- if from_idx TYPE_INT == {
- # cdq
- ctx 0x99 cctx_emit ;
- } else {
- if from_idx TYPE_UINT == from_idx TYPE_BOOL == || {
- # xor edx, edx
- ctx 0x31 cctx_emit ;
- ctx 0xd2 cctx_emit ;
- } else {
- from_idx TYPE_LONG == from_idx TYPE_ULONG == || "lctx_int_convert: error 2" assert_msg ;
- }
- }
- }
- }
- }
- }
- if to_idx TYPE_BOOL == {
- # or eax, edx; setne al; movzx eax, al
- ctx 0x09 cctx_emit ;
- ctx 0xd0 cctx_emit ;
- ctx 0x0f cctx_emit ;
- ctx 0x95 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0x0f cctx_emit ;
- ctx 0xb6 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- }
- if ctx to_idx cctx_type_footprint 4 == {
- # push eax
- ctx 0x50 cctx_emit ;
- } else {
- if ctx to_idx cctx_type_footprint 8 == {
- # push edx; push eax
- ctx 0x52 cctx_emit ;
- ctx 0x50 cctx_emit ;
- } else {
- 0 "lctx_int_convert: error 3" assert_msg ;
- }
- }
- }
- fun lctx_convert_stack 4 {
- $lctx
- $ctx
- $from_idx
- $to_idx
- @lctx 3 param = ;
- @ctx 2 param = ;
- @from_idx 1 param = ;
- @to_idx 0 param = ;
- if ctx from_idx to_idx cctx_type_compare {
- ret ;
- }
- $from_type
- $to_type
- @from_type ctx from_idx cctx_get_type = ;
- @to_type ctx to_idx cctx_get_type = ;
- # Permit all conversions between integers and pointers, just to make
- # it easy
- if from_type TYPE_KIND take TYPE_KIND_POINTER == {
- @from_idx TYPE_UINT = ;
- }
- if to_type TYPE_KIND take TYPE_KIND_POINTER == {
- @to_idx TYPE_UINT = ;
- }
- if from_idx is_integer_type to_idx is_integer_type && {
- lctx ctx from_idx to_idx lctx_int_convert ;
- ret ;
- }
- 0 "lctx_convert_stack: not implemented" from_idx to_idx assert_msg_int_int ;
- }
- fun ast_push_value_logic 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $name
- @name ast AST_NAME take = ;
- $and
- @and name "&&" strcmp 0 == = ;
- and name "||" strcmp 0 == ^ "ast_push_value_logic: not a binary logic operator" assert_msg ;
- $type1
- $type2
- $type_idx
- @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
- @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
- @type_idx ast ctx lctx ast_eval_type = ;
- # Evaluate first operand
- ast AST_LEFT take ctx lctx ast_push_value ;
- # Try short-circuit evaluation
- $end_lab
- @end_lab lctx ctx lctx_gen_label = ;
- # pop eax; test eax, eax; mov eax, ?; cctx_gen_label_jump
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- if and {
- ctx 0 cctx_emit32 ;
- ctx lctx end_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
- } else {
- ctx 1 cctx_emit32 ;
- ctx lctx end_lab JUMP_TYPE_JNZ 0 cctx_gen_label_jump ;
- }
- # Evaluate second operand
- ast AST_RIGHT take ctx lctx ast_push_value ;
- # Reduce result to 0 or 1
- # pop eax; test eax, eax; mov eax, 0; setnz al
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x95 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- # Push result
- lctx ctx end_lab lctx_fix_label ;
- # push eax
- ctx 0x50 cctx_emit ;
- }
- fun ast_push_value_arith32 4 {
- $ctx
- $name
- $type_idx
- $is_prefix
- @ctx 3 param = ;
- @name 2 param = ;
- @type_idx 1 param = ;
- @is_prefix 0 param = ;
- # Pop right result and store in ECX
- # pop ecx
- ctx 0x59 cctx_emit ;
- # Pop left result and store in EAX
- if is_prefix ! {
- # pop eax
- ctx 0x58 cctx_emit ;
- }
- # Invoke the specific operation code
- $processed
- @processed 0 = ;
- if name "+" strcmp 0 == {
- # add eax, ecx
- ctx 0x01 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- @processed 1 = ;
- }
- if name "-" strcmp 0 == {
- # sub eax, ecx
- ctx 0x29 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- @processed 1 = ;
- }
- if name "&" strcmp 0 == {
- # and eax, ecx
- ctx 0x21 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- @processed 1 = ;
- }
- if name "|" strcmp 0 == {
- # or eax, ecx
- ctx 0x09 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- @processed 1 = ;
- }
- if name "^" strcmp 0 == {
- # xor eax, ecx
- ctx 0x31 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- @processed 1 = ;
- }
- if name "<<" strcmp 0 == {
- # shl eax, cl
- ctx 0xd3 cctx_emit ;
- ctx 0xe0 cctx_emit ;
- @processed 1 = ;
- }
- if name ">>" strcmp 0 == type_idx TYPE_UINT == && {
- # shr eax, cl
- ctx 0xd3 cctx_emit ;
- ctx 0xe8 cctx_emit ;
- @processed 1 = ;
- }
- if name ">>" strcmp 0 == type_idx TYPE_INT == && {
- # sar eax, cl
- ctx 0xd3 cctx_emit ;
- ctx 0xf8 cctx_emit ;
- @processed 1 = ;
- }
- if name "+_PRE" strcmp 0 == {
- # mov eax, ecx
- ctx 0x89 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- @processed 1 = ;
- }
- if name "-_PRE" strcmp 0 == {
- # mov eax, ecx; neg eax
- ctx 0x89 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xd8 cctx_emit ;
- @processed 1 = ;
- }
- if name "~_PRE" strcmp 0 == {
- # mov eax, ecx; not eax
- ctx 0x89 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xd0 cctx_emit ;
- @processed 1 = ;
- }
- if name "!_PRE" strcmp 0 == {
- # test ecx, ecx; mov eax, 0; sete al
- ctx 0x85 cctx_emit ;
- ctx 0xc9 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x94 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name "*" strcmp 0 == {
- # imul ecx
- ctx 0xf7 cctx_emit ;
- ctx 0xe9 cctx_emit ;
- @processed 1 = ;
- }
- if name "/" strcmp 0 == type_idx TYPE_UINT == && {
- # xor edx, edx; div ecx
- ctx 0x31 cctx_emit ;
- ctx 0xd2 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xf1 cctx_emit ;
- @processed 1 = ;
- }
- if name "/" strcmp 0 == type_idx TYPE_INT == && {
- # cdq; idiv ecx
- ctx 0x99 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xf9 cctx_emit ;
- @processed 1 = ;
- }
- if name "%" strcmp 0 == type_idx TYPE_UINT == && {
- # xor edx, edx; div ecx; mov eax, edx
- ctx 0x31 cctx_emit ;
- ctx 0xd2 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xf1 cctx_emit ;
- ctx 0x89 cctx_emit ;
- ctx 0xd0 cctx_emit ;
- @processed 1 = ;
- }
- if name "%" strcmp 0 == type_idx TYPE_INT == && {
- # cdq; idiv ecx; mov eax, edx
- ctx 0x99 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xf9 cctx_emit ;
- ctx 0x89 cctx_emit ;
- ctx 0xd0 cctx_emit ;
- @processed 1 = ;
- }
- if name "==" strcmp 0 == {
- # cmp eax, ecx; mov eax, 0; sete al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x94 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name "!=" strcmp 0 == {
- # cmp eax, ecx; mov eax, 0; setne al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x95 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name "<" strcmp 0 == type_idx TYPE_UINT == && {
- # cmp eax, ecx; mov eax, 0; setb al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x92 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name "<=" strcmp 0 == type_idx TYPE_UINT == && {
- # cmp eax, ecx; mov eax, 0; setbe al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x96 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name ">" strcmp 0 == type_idx TYPE_UINT == && {
- # cmp eax, ecx; mov eax, 0; seta al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x97 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name ">=" strcmp 0 == type_idx TYPE_UINT == && {
- # cmp eax, ecx; mov eax, 0; setae al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x93 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name "<" strcmp 0 == type_idx TYPE_INT == && {
- # cmp eax, ecx; mov eax, 0; setl al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x9c cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name "<=" strcmp 0 == type_idx TYPE_INT == && {
- # cmp eax, ecx; mov eax, 0; setle al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x9e cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name ">" strcmp 0 == type_idx TYPE_INT == && {
- # cmp eax, ecx; mov eax, 0; setg al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x9f cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- if name ">=" strcmp 0 == type_idx TYPE_INT == && {
- # cmp eax, ecx; mov eax, 0; setge al
- ctx 0x39 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx 0xb8 cctx_emit ;
- ctx 0 cctx_emit32 ;
- ctx 0x0f cctx_emit ;
- ctx 0x9d cctx_emit ;
- ctx 0xc0 cctx_emit ;
- @processed 1 = ;
- }
- processed "ast_push_value_arith32: not implemented" name assert_msg_str ;
- # push eax
- ctx 0x50 cctx_emit ;
- }
- fun ast_push_value_arith64 4 {
- $ctx
- $name
- $type_idx
- $is_prefix
- @ctx 3 param = ;
- @name 2 param = ;
- @type_idx 1 param = ;
- @is_prefix 0 param = ;
- # Push arguments addresses on the stack, so that we can call i64_* routines
- if is_prefix {
- # lea edx, [esp]; push edx
- ctx 0x8d cctx_emit ;
- ctx 0x14 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x52 cctx_emit ;
- } else {
- # lea eax, [esp+8]; lea edx, [esp]; push eax; push edx
- ctx 0x8d cctx_emit ;
- ctx 0x44 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x08 cctx_emit ;
- ctx 0x8d cctx_emit ;
- ctx 0x14 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x50 cctx_emit ;
- ctx 0x52 cctx_emit ;
- }
- # Invoke the specific operation code
- $processed
- @processed 0 = ;
- if name "+" strcmp 0 == {
- ctx _i64_add JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "-" strcmp 0 == {
- ctx _i64_sub JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "&" strcmp 0 == {
- ctx _i64_and JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "|" strcmp 0 == {
- ctx _i64_or JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "^" strcmp 0 == {
- ctx _i64_xor JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "<<" strcmp 0 == {
- ctx _i64_shl JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name ">>" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx _i64_shr JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name ">>" strcmp 0 == type_idx TYPE_LONG == && {
- ctx _i64_sar JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "+_PRE" strcmp 0 == {
- @processed 1 = ;
- }
- if name "-_PRE" strcmp 0 == {
- ctx _i64_neg JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "~_PRE" strcmp 0 == {
- ctx _i64_not JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "!_PRE" strcmp 0 == {
- ctx _i64_lnot JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "*" strcmp 0 == {
- ctx _i64_mul JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "/" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx @i64_udiv JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "/" strcmp 0 == type_idx TYPE_LONG == && {
- ctx @i64_sdiv JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "%" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx @i64_umod JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "%" strcmp 0 == type_idx TYPE_LONG == && {
- ctx @i64_smod JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "==" strcmp 0 == {
- ctx _i64_eq JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "!=" strcmp 0 == {
- ctx _i64_neq JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "<" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx _i64_ul JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "<=" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx _i64_ule JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name ">" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx _i64_ug JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name ">=" strcmp 0 == type_idx TYPE_ULONG == && {
- ctx _i64_uge JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "<" strcmp 0 == type_idx TYPE_LONG == && {
- ctx _i64_l JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name "<=" strcmp 0 == type_idx TYPE_LONG == && {
- ctx _i64_le JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name ">" strcmp 0 == type_idx TYPE_LONG == && {
- ctx _i64_g JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- if name ">=" strcmp 0 == type_idx TYPE_LONG == && {
- ctx _i64_ge JUMP_TYPE_CALL cctx_gen_jump_loc ;
- @processed 1 = ;
- }
- processed "ast_push_value_arith64: not implemented" name assert_msg_str ;
- # Discard the addresses and possibly the second operand
- if is_prefix {
- # add esp, 4
- ctx 0x83 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx 0x04 cctx_emit ;
- } else {
- # add esp, 16
- ctx 0x83 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx 0x10 cctx_emit ;
- }
- }
- ifun cctx_gen_push_data 2
- ifun cctx_gen_move_data 2
- fun ast_push_value_arith 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $name
- @name ast AST_NAME take = ;
- $is_prefix
- $is_postfix
- @is_prefix name "+_PRE" strcmp 0 ==
- name "-_PRE" strcmp 0 == ||
- name "~_PRE" strcmp 0 == ||
- name "!_PRE" strcmp 0 == ||
- name "++_PRE" strcmp 0 == ||
- name "--_PRE" strcmp 0 == || = ;
- @is_postfix name "++_POST" strcmp 0 ==
- name "--_POST" strcmp 0 == || = ;
- $assign
- $incdec
- @assign name "*=" strcmp 0 ==
- name "/=" strcmp 0 == ||
- name "%=" strcmp 0 == ||
- name "+=" strcmp 0 == ||
- name "-=" strcmp 0 == ||
- name "&=" strcmp 0 == ||
- name "^=" strcmp 0 == ||
- name "|=" strcmp 0 == ||
- name "<<=" strcmp 0 == ||
- name ">>=" strcmp 0 == || = ;
- @incdec name "++_PRE" strcmp 0 ==
- name "--_PRE" strcmp 0 == ||
- name "++_POST" strcmp 0 == ||
- name "--_POST" strcmp 0 == || = ;
- @name name strdup = ;
- if assign {
- is_prefix ! "ast_push_value_arith: error 2" assert_msg ;
- name name strlen + 1 - '\0' =c ;
- }
- if incdec {
- name 1 + '\0' =c ;
- is_prefix is_postfix || "ast_push_value_arith: error 6" assert_msg ;
- is_prefix ! is_postfix ! || "ast_push_value_arith: error 7" assert_msg ;
- assign ! "ast_push_value_arith: error 8" assert_msg ;
- }
- if is_postfix {
- incdec "ast_push_value_arith: error 9" assert_msg ;
- assign ! "ast_push_value_arith: error 10" assert_msg ;
- }
- $type1
- $type2
- $type_idx
- $common_type_idx
- if is_prefix ! {
- @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
- }
- if is_postfix ! {
- @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
- }
- if incdec {
- if is_prefix {
- @type1 type2 = ;
- } else {
- @type2 type1 = ;
- }
- }
- @type_idx ast ctx lctx ast_eval_type = ;
- @common_type_idx ast AST_COMMON_TYPE_IDX take = ;
- # Recursively evalute both operands
- if is_prefix ! {
- if assign incdec || {
- # See the comment for sum-assigning
- # ast_push_addr; mov eax, [esp], cctx_gen_push_data
- ast AST_LEFT take ctx lctx ast_push_addr ;
- ctx 0x8b cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx ctx type1 cctx_type_footprint cctx_gen_push_data ;
- } else {
- ast AST_LEFT take ctx lctx ast_push_value ;
- }
- lctx ctx type1 common_type_idx lctx_int_convert ;
- }
- if is_postfix ! {
- if incdec {
- # See the comment for sum-assigning
- # ast_push_addr; mov eax, [esp], cctx_gen_push_data
- ast AST_RIGHT take ctx lctx ast_push_addr ;
- ctx 0x8b cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx ctx type2 cctx_type_footprint cctx_gen_push_data ;
- } else {
- ast AST_RIGHT take ctx lctx ast_push_value ;
- }
- lctx ctx type2 common_type_idx lctx_int_convert ;
- }
- # If postincrement or postdecrement, put a further copy of the
- # operand on the stack, which will be returned in the end; so the
- # stack is: value to be returned, address to be assigned, operand
- if incdec is_postfix && {
- if ctx common_type_idx cctx_type_footprint 4 == {
- # pop eax; pop ecx; push eax; push ecx; push eax
- ctx 0x58 cctx_emit ;
- ctx 0x59 cctx_emit ;
- ctx 0x50 cctx_emit ;
- ctx 0x51 cctx_emit ;
- ctx 0x50 cctx_emit ;
- } else {
- # pop eax; pop edx; pop ecx; push edx; push eax; push ecx; push edx; push eax
- ctx 0x58 cctx_emit ;
- ctx 0x5a cctx_emit ;
- ctx 0x59 cctx_emit ;
- ctx 0x52 cctx_emit ;
- ctx 0x50 cctx_emit ;
- ctx 0x51 cctx_emit ;
- ctx 0x52 cctx_emit ;
- ctx 0x50 cctx_emit ;
- }
- }
- # If increment or decrement, then push 1 of the appropriate type
- if incdec {
- type1 type2 == "ast_push_value_arith: error 11" assert_msg ;
- # push 1
- ctx 0x6a cctx_emit ;
- ctx 0x01 cctx_emit ;
- lctx ctx TYPE_INT common_type_idx lctx_int_convert ;
- }
- # Incdec operators must not be considered as prefix, because we have
- # pushed a second operand on the stack
- if ctx common_type_idx cctx_type_footprint 4 == {
- ctx name common_type_idx is_prefix incdec ! && ast_push_value_arith32 ;
- } else {
- ctx common_type_idx cctx_type_footprint 8 == "ast_push_value_arith: error 1" assert_msg ;
- ctx name common_type_idx is_prefix incdec ! && ast_push_value_arith64 ;
- }
- if type_idx common_type_idx != {
- assign ! "ast_push_value_arith: error 3" assert_msg ;
- lctx ctx common_type_idx type_idx lctx_int_convert ;
- }
- # Do the assignment if needed
- if assign incdec || {
- ctx type1 cctx_type_size ctx type_idx cctx_type_size <= "ast_push_value_arith: error 5" assert_msg ;
- type_idx common_type_idx == "ast_push_value_arith: error 4" assert_msg ;
- # First remove the address from the stack (it is not on the top of
- # the stack, though)
- # pop eax
- ctx 0x58 cctx_emit ;
- if ctx type_idx cctx_type_footprint 8 == {
- # pop edx
- ctx 0x5a cctx_emit ;
- }
- # pop ecx
- ctx 0x59 cctx_emit ;
- if ctx type_idx cctx_type_footprint 8 == {
- # push edx
- ctx 0x52 cctx_emit ;
- }
- # push eax
- ctx 0x50 cctx_emit ;
- # Then do the assignment
- # mov eax, ecx; cctx_gen_move_data
- ctx 0x89 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- ctx ctx type1 cctx_type_size cctx_gen_move_data ;
- }
- # If postincrement or postdecrement, remove the result of the
- # operation, because the copy of the operand already pushed on the
- # stack will be returned
- if incdec is_postfix && {
- # add esp, footprint
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx ctx common_type_idx cctx_type_footprint cctx_emit32 ;
- }
- name free ;
- }
- fun cctx_gen_push_data 2 {
- $ctx
- $size
- @ctx 1 param = ;
- @size 0 param = ;
- size 4 % 0 == "cctx_gen_push_data: size is not multiple of 4" assert_msg ;
- $i
- @i size 4 - = ;
- while i 0 >= {
- # push [eax+off]
- ctx 0xff cctx_emit ;
- ctx 0xb0 cctx_emit ;
- ctx i cctx_emit32 ;
- @i i 4 - = ;
- }
- }
- fun cctx_gen_pop_data 2 {
- $ctx
- $size
- @ctx 1 param = ;
- @size 0 param = ;
- size 4 % 0 == "cctx_gen_pop_data: size is not multiple of 4" assert_msg ;
- $i
- @i 0 = ;
- while i size < {
- # pop [eax+off]
- ctx 0x8f cctx_emit ;
- ctx 0x80 cctx_emit ;
- ctx i cctx_emit32 ;
- @i i 4 + = ;
- }
- }
- fun cctx_gen_move_data 2 {
- $ctx
- $size
- @ctx 1 param = ;
- @size 0 param = ;
- if size 1 == {
- # mov dl, [esp]; mov [eax], dl
- ctx 0x8a cctx_emit ;
- ctx 0x14 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x88 cctx_emit ;
- ctx 0x10 cctx_emit ;
- ret ;
- }
- if size 2 == {
- # mov dx, [esp]; mov [eax], dx
- ctx 0x66 cctx_emit ;
- ctx 0x8b cctx_emit ;
- ctx 0x14 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x66 cctx_emit ;
- ctx 0x89 cctx_emit ;
- ctx 0x10 cctx_emit ;
- ret ;
- }
- size 4 % 0 == "cctx_gen_move_data: size is not multiple of 4" assert_msg ;
- $i
- @i 0 = ;
- while i size < {
- # mov edx, [esp+off]; mov [eax+off], edx
- ctx 0x8b cctx_emit ;
- ctx 0x94 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx i cctx_emit32 ;
- ctx 0x89 cctx_emit ;
- ctx 0x90 cctx_emit ;
- ctx i cctx_emit32 ;
- @i i 4 + = ;
- }
- }
- fun cctx_gen_move_data_backward 2 {
- $ctx
- $size
- @ctx 1 param = ;
- @size 0 param = ;
- size 4 % 0 == "cctx_gen_move_data: size is not multiple of 4" assert_msg ;
- $i
- @i size 4 - = ;
- while i 0 >= {
- # mov edx, [esp+off]; mov [eax+off], edx
- ctx 0x8b cctx_emit ;
- ctx 0x94 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx i cctx_emit32 ;
- ctx 0x89 cctx_emit ;
- ctx 0x90 cctx_emit ;
- ctx i cctx_emit32 ;
- @i i 4 - = ;
- }
- }
- fun cctx_default_promotion 2 {
- $ctx
- $type_idx
- @ctx 1 param = ;
- @type_idx 0 param = ;
- if type_idx TYPE_CHAR ==
- type_idx TYPE_SCHAR == ||
- type_idx TYPE_SHORT == ||
- type_idx TYPE_INT == || {
- TYPE_INT ret ;
- }
- if type_idx TYPE_UCHAR ==
- type_idx TYPE_USHORT == ||
- type_idx TYPE_UINT == || {
- TYPE_UINT ret ;
- }
- type_idx ret ;
- }
- fun ast_gen_function_call 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $left
- $right
- @left ast AST_LEFT take = ;
- @right ast AST_RIGHT take = ;
- $type_idx
- @type_idx ast ctx lctx ast_eval_type = ;
- $fun_ptr_idx
- $fun_ptr_type
- $fun_idx
- $fun_type
- $return_type_idx
- $return_type
- @fun_ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @fun_ptr_type ctx fun_ptr_idx cctx_get_type = ;
- fun_ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_gen_function_call: left is not a pointer" assert_msg ;
- @fun_idx fun_ptr_type TYPE_BASE take = ;
- @fun_type ctx fun_idx cctx_get_type = ;
- fun_type TYPE_KIND take TYPE_KIND_FUNCTION == "ast_gen_function_call: left is not a pointer to function" assert_msg ;
- @return_type_idx fun_type TYPE_BASE take = ;
- @return_type ctx return_type_idx cctx_get_type = ;
- $args
- @args fun_type TYPE_ARGS take = ;
- # If the function will return an object, immediately allocate it
- $returns_obj
- @returns_obj return_type TYPE_KIND take TYPE_KIND_STRUCT == return_type TYPE_KIND take TYPE_KIND_UNION == || = ;
- if returns_obj {
- # sub esp, footprint
- ctx 0x81 cctx_emit ;
- ctx 0xec cctx_emit ;
- ctx ctx return_type_idx cctx_type_footprint cctx_emit32 ;
- }
- # Passed arguments are stored in reverse order
- $passed_args
- @passed_args 4 vector_init = ;
- if right AST_NAME take 0 != {
- $cont
- @cont 1 = ;
- while cont {
- if right AST_NAME take "," strcmp 0 == {
- passed_args right AST_RIGHT take vector_push_back ;
- @right right AST_LEFT take = ;
- } else {
- passed_args right vector_push_back ;
- @cont 0 = ;
- }
- }
- }
- # Push arguments on the stack, right to left
- $ellipsis
- @ellipsis fun_type TYPE_ELLIPSIS take = ;
- if ellipsis {
- passed_args vector_size args vector_size >= "ast_gen_function_call: too few arguments" assert_msg ;
- } else {
- passed_args vector_size args vector_size == "ast_gen_function_call: arguments number does not match" assert_msg ;
- }
- $i
- @i 0 = ;
- $rewind
- @rewind 0 = ;
- $excess_args
- @excess_args passed_args vector_size args vector_size - = ;
- while i passed_args vector_size < {
- $passed_arg
- @passed_arg passed_args i vector_at = ;
- passed_arg ctx lctx ast_push_value ;
- $from_type
- $to_type
- @from_type passed_arg ctx lctx ast_eval_type = ;
- if i excess_args < {
- @to_type ctx from_type cctx_default_promotion = ;
- } else {
- $arg
- @arg args args vector_size i excess_args - - 1 - vector_at = ;
- @to_type arg = ;
- }
- lctx ctx from_type to_type lctx_convert_stack ;
- @rewind rewind ctx to_type cctx_type_footprint + = ;
- @i i 1 + = ;
- }
- # If the function will return an object, push the returned value address
- if returns_obj {
- # lea eax, [esp+rewind]; push eax
- ctx 0x8d cctx_emit ;
- ctx 0x84 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx rewind cctx_emit32 ;
- ctx 0x50 cctx_emit ;
- }
- # Call function
- # ast_push_value; pop eax; call eax
- left ctx lctx ast_push_value ;
- ctx 0x58 cctx_emit ;
- ctx 0xff cctx_emit ;
- ctx 0xd0 cctx_emit ;
- # Clean up stack
- # add esp, rewind
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx rewind cctx_emit32 ;
- # Push result if there is one and it is not an object
- if type_idx TYPE_VOID != returns_obj ! && {
- $res_footprint
- @res_footprint ctx ast ctx lctx ast_eval_type cctx_type_footprint = ;
- res_footprint 4 == res_footprint 0 == || res_footprint 8 == || "ast_gen_function_call: return type is not scalar" assert_msg ;
- if res_footprint 4 >= {
- if res_footprint 8 == {
- # push edx
- ctx 0x52 cctx_emit ;
- }
- # push eax
- ctx 0x50 cctx_emit ;
- }
- }
- passed_args vector_destroy ;
- }
- fun ast_push_value_ptr 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $name
- @name ast AST_NAME take = ;
- $sum
- $is_prefix
- $is_postfix
- @is_prefix name "++_PRE" strcmp 0 ==
- name "--_PRE" strcmp 0 == || = ;
- @is_postfix name "++_POST" strcmp 0 ==
- name "--_POST" strcmp 0 == || = ;
- $subtract
- $assign
- $incdec
- @sum name "+" strcmp 0 == name "[" strcmp 0 == || name "+=" strcmp 0 == || = ;
- @subtract name "-" strcmp 0 == name "-=" strcmp 0 == || = ;
- @assign name "+=" strcmp 0 == name "-=" strcmp 0 == || = ;
- @incdec name "++_PRE" strcmp 0 ==
- name "--_PRE" strcmp 0 == ||
- name "++_POST" strcmp 0 == ||
- name "--_POST" strcmp 0 == || = ;
- sum subtract || incdec || "ast_push_value_ptr: not a sum or a subtraction" assert_msg ;
- if incdec {
- is_prefix is_postfix || "ast_push_value_ptr: error 1" assert_msg ;
- is_prefix ! is_postfix ! || "ast_push_value_ptr: error 2" assert_msg ;
- assign ! "ast_push_value_ptr: error 3" assert_msg ;
- } else {
- is_prefix ! is_postfix ! && "ast_push_value_ptr: error 4" assert_msg ;
- }
- $left_idx
- $right_idx
- $left_ptr
- $right_ptr
- $left_size
- $right_size
- if is_prefix ! {
- @left_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @left_ptr ctx left_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
- if left_ptr {
- if ctx left_idx cctx_get_type TYPE_BASE take TYPE_VOID == {
- @left_size 1 = ;
- } else {
- @left_size ctx ctx left_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
- }
- }
- }
- if is_postfix ! {
- @right_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
- @right_ptr ctx right_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
- if right_ptr {
- if ctx right_idx cctx_get_type TYPE_BASE take TYPE_VOID == {
- @right_size 1 = ;
- } else {
- @right_size ctx ctx right_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
- }
- }
- }
- $processed
- @processed 0 = ;
- if incdec {
- processed ! "ast_push_value_ptr: error 5" assert_msg ;
- if is_prefix right_ptr && {
- # ast_push_addr; pop eax
- ast AST_RIGHT take ctx lctx ast_push_addr ;
- ctx 0x58 cctx_emit ;
- if name **c '+' == {
- # add [eax], size
- ctx 0x81 cctx_emit ;
- ctx 0x00 cctx_emit ;
- ctx right_size cctx_emit32 ;
- } else {
- # sub [eax], size
- ctx 0x81 cctx_emit ;
- ctx 0x28 cctx_emit ;
- ctx right_size cctx_emit32 ;
- }
- # push [eax]
- ctx 0xff cctx_emit ;
- ctx 0x30 cctx_emit ;
- @processed 1 = ;
- }
- if is_postfix left_ptr && {
- # ast_push_addr; pop eax; mov edx, [eax]
- ast AST_LEFT take ctx lctx ast_push_addr ;
- ctx 0x58 cctx_emit ;
- ctx 0x8b cctx_emit ;
- ctx 0x10 cctx_emit ;
- if name **c '+' == {
- # add [eax], size
- ctx 0x81 cctx_emit ;
- ctx 0x00 cctx_emit ;
- ctx left_size cctx_emit32 ;
- } else {
- # sub [eax], size
- ctx 0x81 cctx_emit ;
- ctx 0x28 cctx_emit ;
- ctx left_size cctx_emit32 ;
- }
- # push edx
- ctx 0x52 cctx_emit ;
- @processed 1 = ;
- }
- }
- if sum {
- processed ! "ast_push_value_ptr: error 6" assert_msg ;
- left_ptr right_ptr && ! "ast_push_value_ptr: cannot take sum of two pointers" assert_msg ;
- if left_ptr {
- if assign {
- # When sum-assigning, push both the address and the value on
- # the stack: first we use the value to do the computation,
- # then the address to assign
- # ast_push_addr; mov eax, [esp]; push [eax]
- ast AST_LEFT take ctx lctx ast_push_addr ;
- ctx 0x8b cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0xff cctx_emit ;
- ctx 0x30 cctx_emit ;
- } else {
- # ast_push_value
- ast AST_LEFT take ctx lctx ast_push_value ;
- }
- # ast_push_value; lctx_convert_stack; push size
- ast AST_RIGHT take ctx lctx ast_push_value ;
- lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type TYPE_UINT lctx_convert_stack ;
- ctx 0x68 cctx_emit ;
- ctx left_size cctx_emit32 ;
- @processed 1 = ;
- }
- if right_ptr {
- assign ! "ast_push_value_ptr: right of sum-assign must be integer" assert_msg ;
- # ast_push_value; ast_push_value; lctx_convert_stack; push size
- ast AST_RIGHT take ctx lctx ast_push_value ;
- ast AST_LEFT take ctx lctx ast_push_value ;
- lctx ctx ast AST_LEFT take ctx lctx ast_eval_type TYPE_UINT lctx_convert_stack ;
- ctx 0x68 cctx_emit ;
- ctx right_size cctx_emit32 ;
- @processed 1 = ;
- }
- if processed {
- # pop eax; pop edx; imul edx; pop ecx; add eax, ecx
- ctx 0x58 cctx_emit ;
- ctx 0x5a cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xea cctx_emit ;
- ctx 0x59 cctx_emit ;
- ctx 0x01 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- if assign {
- # pop edx; mov [edx], eax
- ctx 0x5a cctx_emit ;
- ctx 0x89 cctx_emit ;
- ctx 0x02 cctx_emit ;
- }
- # push eax
- ctx 0x50 cctx_emit ;
- }
- }
- if subtract {
- processed ! "ast_push_value_ptr: error 7" assert_msg ;
- left_ptr ! right_ptr && ! "ast_push_value_ptr: cannot take different of a non-pointer and a pointer" assert_msg ;
- if left_ptr {
- if right_ptr {
- assign ! "ast_push_value_ptr: right of subtract-assign must be integer" assert_msg ;
- left_size right_size == "ast_push_value_ptr: cannot take difference of pointers to types of different size" assert_msg ;
- # push size; ast_push_value; ast_push_value
- ctx 0x68 cctx_emit ;
- ctx left_size cctx_emit32 ;
- ast AST_LEFT take ctx lctx ast_push_value ;
- ast AST_RIGHT take ctx lctx ast_push_value ;
- # pop edx; pop eax; sub eax, edx; pop ecx; cdq; idiv ecx; push eax
- ctx 0x5a cctx_emit ;
- ctx 0x58 cctx_emit ;
- ctx 0x29 cctx_emit ;
- ctx 0xd0 cctx_emit ;
- ctx 0x59 cctx_emit ;
- ctx 0x99 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xf9 cctx_emit ;
- ctx 0x50 cctx_emit ;
- } else {
- if assign {
- # See the comment for sum-assigning
- # ast_push_addr; mov eax, [esp]; push [eax]
- ast AST_LEFT take ctx lctx ast_push_addr ;
- ctx 0x8b cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0xff cctx_emit ;
- ctx 0x30 cctx_emit ;
- } else {
- # ast_push_value
- ast AST_LEFT take ctx lctx ast_push_value ;
- }
- # ast_push_value; lctx_convert_stack; push size
- ast AST_RIGHT take ctx lctx ast_push_value ;
- lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type TYPE_UINT lctx_convert_stack ;
- ctx 0x68 cctx_emit ;
- ctx left_size cctx_emit32 ;
- # pop eax; pop edx; imul edx; pop ecx; neg eax; add eax, ecx
- ctx 0x58 cctx_emit ;
- ctx 0x5a cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xea cctx_emit ;
- ctx 0x59 cctx_emit ;
- ctx 0xf7 cctx_emit ;
- ctx 0xd8 cctx_emit ;
- ctx 0x01 cctx_emit ;
- ctx 0xc8 cctx_emit ;
- if assign {
- # pop edx; mov [edx], eax
- ctx 0x5a cctx_emit ;
- ctx 0x89 cctx_emit ;
- ctx 0x02 cctx_emit ;
- }
- # push eax
- ctx 0x50 cctx_emit ;
- }
- @processed 1 = ;
- }
- }
- processed ret ;
- }
- fun ast_push_value 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- $name
- @name ast AST_NAME take = ;
- $type_idx
- @type_idx ast ctx lctx ast_eval_type = ;
- # In case of type decaying, return the address
- $orig_type_idx
- @orig_type_idx ast AST_ORIG_TYPE_IDX take = ;
- if orig_type_idx 0xffffffff != {
- $orig_type
- @orig_type ctx orig_type_idx cctx_get_type = ;
- $orig_kind
- @orig_kind orig_type TYPE_KIND take = ;
- if orig_kind TYPE_KIND_FUNCTION == orig_kind TYPE_KIND_ARRAY == || {
- ast ctx lctx ast_push_addr ;
- ret ;
- }
- }
- if ast AST_TYPE take 0 == {
- # Operand
- if name is_valid_identifier {
- $enum_consts
- @enum_consts ctx CCTX_ENUM_CONSTS take = ;
- if name "sizeof" strcmp 0 == {
- # push type_size
- ctx 0x68 cctx_emit ;
- ctx ctx ast AST_CAST_TYPE_IDX take cctx_type_size cctx_emit32 ;
- } else {
- if enum_consts name map_has {
- $value
- @value enum_consts name map_at = ;
- ctx 0x68 cctx_emit ;
- ctx value cctx_emit32 ;
- } else {
- # Push the address
- ast ctx lctx ast_push_addr ;
- # pop eax
- ctx 0x58 cctx_emit ;
- ctx ctx type_idx cctx_type_footprint cctx_gen_push_data ;
- }
- }
- } else {
- if name **c '\"' == {
- 0 "ast_push_value: error 1" assert_msg ;
- } else {
- # Value is already encoded in the AST
- }
- if ctx type_idx cctx_type_footprint 8 == {
- # push upper32
- ctx 0x68 cctx_emit ;
- ctx ast AST_VALUE take i64_to_upper32 cctx_emit32 ;
- } else {
- ctx type_idx cctx_type_footprint 4 == "ast_push_value: error 2" assert_msg ;
- }
- # push lower32
- ctx 0x68 cctx_emit ;
- ctx ast AST_VALUE take i64_to_32 cctx_emit32 ;
- }
- } else {
- # Operator
- $processed
- @processed 0 = ;
- if name "+" strcmp 0 ==
- name "-" strcmp 0 == ||
- name "+=" strcmp 0 == ||
- name "-=" strcmp 0 == ||
- name "++_PRE" strcmp 0 == ||
- name "--_PRE" strcmp 0 == ||
- name "++_POST" strcmp 0 == ||
- name "--_POST" strcmp 0 == ||
- processed ! && {
- @processed ast ctx lctx ast_push_value_ptr = ;
- }
- if name "*" strcmp 0 ==
- name "/" strcmp 0 == ||
- name "%" strcmp 0 == ||
- name "+" strcmp 0 == ||
- name "-" strcmp 0 == ||
- name "&" strcmp 0 == ||
- name "^" strcmp 0 == ||
- name "|" strcmp 0 == ||
- name "<<" strcmp 0 == ||
- name ">>" strcmp 0 == ||
- name "*=" strcmp 0 == ||
- name "/=" strcmp 0 == ||
- name "%=" strcmp 0 == ||
- name "+=" strcmp 0 == ||
- name "-=" strcmp 0 == ||
- name "&=" strcmp 0 == ||
- name "^=" strcmp 0 == ||
- name "|=" strcmp 0 == ||
- name "<<=" strcmp 0 == ||
- name ">>=" strcmp 0 == ||
- name "<" strcmp 0 == ||
- name ">" strcmp 0 == ||
- name "<=" strcmp 0 == ||
- name ">=" strcmp 0 == ||
- name "==" strcmp 0 == ||
- name "!=" strcmp 0 == ||
- name "+_PRE" strcmp 0 == ||
- name "-_PRE" strcmp 0 == ||
- name "~_PRE" strcmp 0 == ||
- name "!_PRE" strcmp 0 == ||
- name "++_PRE" strcmp 0 == ||
- name "--_PRE" strcmp 0 == ||
- name "++_POST" strcmp 0 == ||
- name "--_POST" strcmp 0 == ||
- processed ! && {
- ast ctx lctx ast_push_value_arith ;
- @processed 1 = ;
- }
- if name "&&" strcmp 0 ==
- name "||" strcmp 0 == ||
- processed ! && {
- ast ctx lctx ast_push_value_logic ;
- @processed 1 = ;
- }
- if name "=" strcmp 0 == {
- ast AST_RIGHT take ctx lctx ast_push_value ;
- lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type ast ctx lctx ast_eval_type lctx_convert_stack ;
- ast AST_LEFT take ctx lctx ast_push_addr ;
- # pop eax; cctx_gen_move_data
- ctx 0x58 cctx_emit ;
- ctx ctx ast ctx lctx ast_eval_type cctx_type_size cctx_gen_move_data ;
- @processed 1 = ;
- }
- if name "(" strcmp 0 == {
- ast ctx lctx ast_gen_function_call ;
- @processed 1 = ;
- }
- if name "*_PRE" strcmp 0 == name "[" strcmp 0 == || {
- # Push the address
- ast ctx lctx ast_push_addr ;
- # pop eax
- ctx 0x58 cctx_emit ;
- ctx ctx type_idx cctx_type_footprint cctx_gen_push_data ;
- @processed 1 = ;
- }
- if name "&_PRE" strcmp 0 == {
- ast AST_RIGHT take ctx lctx ast_push_addr ;
- @processed 1 = ;
- }
- if name "." strcmp 0 == {
- $struct_idx
- $struct_type
- $struct_size
- @struct_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
- @struct_type ctx struct_idx cctx_get_type = ;
- @struct_size ctx struct_idx cctx_type_footprint = ;
- ast AST_RIGHT take AST_TYPE take 0 == "ast_push_value: right is not a plain name" assert_msg ;
- $name
- @name ast AST_RIGHT take AST_NAME take = ;
- $field
- @field struct_type name type_get_idx = ;
- field 0xffffffff != "ast_push_value: specified field does not exist" assert_msg ;
- $res_idx
- $res_size
- $off
- @res_idx ast ctx lctx ast_eval_type = ;
- @res_size ctx res_idx cctx_type_footprint = ;
- @off struct_type TYPE_FIELDS_OFFS take field vector_at = ;
- # ast_push_value; lea eax, [esp+struct_size-res_size]; add esp, off; cctx_gen_move_data_backward; mov esp, eax
- ast AST_LEFT take ctx lctx ast_push_value ;
- ctx 0x8d cctx_emit ;
- ctx 0x84 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx struct_size res_size - cctx_emit32 ;
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx off cctx_emit32 ;
- ctx res_size cctx_gen_move_data_backward ;
- ctx 0x89 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- @processed 1 = ;
- }
- if name "->" strcmp 0 == {
- # Push the address
- ast ctx lctx ast_push_addr ;
- # pop eax
- ctx 0x58 cctx_emit ;
- ctx ctx type_idx cctx_type_footprint cctx_gen_push_data ;
- @processed 1 = ;
- }
- if name "?" strcmp 0 == {
- $else_lab
- $end_lab
- @else_lab lctx ctx lctx_gen_label = ;
- @end_lab lctx ctx lctx_gen_label = ;
- # Evaluate guard expression
- ast AST_LEFT take ctx lctx ast_push_value ;
- lctx ctx ast AST_LEFT take ctx lctx ast_eval_type TYPE_BOOL lctx_convert_stack ;
- # pop eax; test eax, eax; cctx_gen_label_jump
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx lctx else_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
- # Evaluate center expression
- ast AST_CENTER take ctx lctx ast_push_value ;
- lctx ctx ast AST_CENTER take ctx lctx ast_eval_type type_idx lctx_convert_stack ;
- # cctx_gen_label_jump
- ctx lctx end_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- # Evaluate right expression
- lctx ctx else_lab lctx_fix_label ;
- ast AST_RIGHT take ctx lctx ast_push_value ;
- lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type type_idx lctx_convert_stack ;
- lctx ctx end_lab lctx_fix_label ;
- @processed 1 = ;
- }
- if name "(_PRE" strcmp 0 == {
- ast AST_RIGHT take ctx lctx ast_push_value ;
- if type_idx TYPE_VOID == {
- # Cast to void, discard the return value
- $right_type
- @right_type ast AST_RIGHT take ctx lctx ast_eval_type = ;
- if right_type TYPE_VOID != {
- # add esp, footprint
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx ctx right_type cctx_type_footprint cctx_emit32 ;
- }
- } else {
- lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type type_idx lctx_convert_stack ;
- }
- @processed 1 = ;
- }
- if name "sizeof_PRE" strcmp 0 == {
- $sub_type_idx
- @sub_type_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
- if ast AST_RIGHT take AST_ORIG_TYPE_IDX take 0xffffffff != {
- @sub_type_idx ast AST_RIGHT take AST_ORIG_TYPE_IDX take = ;
- }
- # push type_size
- ctx 0x68 cctx_emit ;
- ctx ctx sub_type_idx cctx_type_size cctx_emit32 ;
- @processed 1 = ;
- }
- if name "," strcmp 0 == {
- # Push left and discard
- ast AST_LEFT take ctx lctx ast_push_value ;
- if ast AST_LEFT take ctx lctx ast_eval_type TYPE_VOID != {
- $footprint
- @footprint ctx ast AST_LEFT take ctx lctx ast_eval_type cctx_type_footprint = ;
- # add esp, footprint
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx footprint cctx_emit32 ;
- }
- # Push right
- ast AST_RIGHT take ctx lctx ast_push_value ;
- @processed 1 = ;
- }
- processed "ast_push_value: not implemented" name assert_msg_str ;
- }
- }
- fun ast_c_eval 3 {
- $ast
- $ctx
- $lctx
- @ast 2 param = ;
- @ctx 1 param = ;
- @lctx 0 param = ;
- # First push value
- ast ctx lctx ast_push_value ;
- # Then pop and discard it (if it is not void)
- $type_idx
- @type_idx ast ctx lctx ast_eval_type = ;
- if type_idx TYPE_VOID != {
- $footprint
- @footprint ctx type_idx cctx_type_footprint = ;
- # add esp, footprint
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx footprint cctx_emit32 ;
- }
- }
- fun cctx_compile_expression 2 {
- $ctx
- $lctx
- $target_type_idx
- $end_tok
- @ctx 3 param = ;
- @lctx 2 param = ;
- @target_type_idx 1 param = ;
- @end_tok 0 param = ;
- $ast
- @ast ctx end_tok cctx_parse_ast1 = ;
- #ast ctx lctx ast_eval_type ;
- #ast ast_dump ;
- if target_type_idx TYPE_VOID == {
- ast ctx lctx ast_c_eval ;
- } else {
- ast ctx lctx ast_push_value ;
- lctx ctx ast ctx lctx ast_eval_type target_type_idx lctx_convert_stack ;
- }
- ast ast_destroy ;
- }
- ifun cctx_compile_statement_or_block 2
- ifun cctx_compile_block 2
- fun cctx_compile_statement 2 {
- $ctx
- $lctx
- @ctx 1 param = ;
- @lctx 0 param = ;
- $processed
- @processed 0 = ;
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- # Check if we found the closing brace
- if tok "}" strcmp 0 == processed ! && {
- @processed 1 = ;
- 0 ret ;
- }
- $expect_semicolon
- @expect_semicolon 0 = ;
- # Allow and ignore empty statements
- if tok ";" strcmp 0 == processed ! && {
- @processed 1 = ;
- }
- # Allow a block to be began at every point (with this
- # cctx_compile_statement_or_block could be simplified)
- if tok "{" strcmp 0 == processed ! && {
- ctx lctx cctx_compile_block ;
- @processed 1 = ;
- }
- # Parse return
- if tok "return" strcmp 0 == processed ! && {
- $ret_type
- @ret_type lctx LCTX_RETURN_TYPE_IDX take = ;
- if ret_type TYPE_VOID != {
- ctx lctx ret_type ";" cctx_compile_expression ;
- if lctx LCTX_RETURNS_OBJ take {
- # mov eax, [ebp+8]; cctx_gen_move_data; add esp, size
- ctx 0x8b cctx_emit ;
- ctx 0x45 cctx_emit ;
- ctx 0x08 cctx_emit ;
- ctx ctx ret_type cctx_type_size cctx_gen_move_data ;
- ctx 0x81 cctx_emit ;
- ctx 0xc4 cctx_emit ;
- ctx ctx ret_type cctx_type_footprint cctx_emit32 ;
- } else {
- # pop eax
- ctx 0x58 cctx_emit ;
- if ctx ret_type cctx_type_footprint 8 == {
- # pop edx
- ctx 0x5a cctx_emit ;
- } else {
- ctx ret_type cctx_type_footprint 4 == "cctx_compile_statement: error 2" assert_msg ;
- }
- }
- }
- ctx lctx lctx LCTX_RETURN_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
- @processed 1 = ;
- @expect_semicolon 1 = ;
- }
- # Parse break
- if tok "break" strcmp 0 == processed ! && {
- ctx lctx lctx LCTX_BREAK_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
- @processed 1 = ;
- @expect_semicolon 1 = ;
- }
- # Parse continue
- if tok "continue" strcmp 0 == processed ! && {
- ctx lctx lctx LCTX_CONTINUE_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
- @processed 1 = ;
- @expect_semicolon 1 = ;
- }
- # Parse if
- if tok "if" strcmp 0 == processed ! && {
- $else_lab
- $end_lab
- @else_lab lctx ctx lctx_gen_label = ;
- @end_lab lctx ctx lctx_gen_label = ;
- # Compile guard expression
- @tok ctx cctx_get_token_or_fail = ;
- tok "(" strcmp 0 == "cctx_compile_statement: ( expected" assert_msg ;
- ctx lctx TYPE_BOOL ")" cctx_compile_expression ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ")" strcmp 0 == "cctx_compile_statement: ) expected" assert_msg ;
- # pop eax; test eax, eax; cctx_gen_label_jump
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx lctx else_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
- # Compile body
- ctx lctx cctx_compile_statement_or_block ;
- # cctx_gen_label_jump
- ctx lctx end_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- # Compile else
- lctx ctx else_lab lctx_fix_label ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok "else" strcmp 0 == {
- ctx lctx cctx_compile_statement_or_block ;
- } else {
- ctx cctx_give_back_token ;
- }
- lctx ctx end_lab lctx_fix_label ;
- @processed 1 = ;
- }
- # Parse for
- if tok "for" strcmp 0 == processed ! && {
- # Set up labels
- $continue_lab
- $break_lab
- $restart_lab
- $body_lab
- @continue_lab lctx ctx lctx_gen_label = ;
- @break_lab lctx ctx lctx_gen_label = ;
- @restart_lab lctx ctx lctx_gen_label = ;
- @body_lab lctx ctx lctx_gen_label = ;
- # Compile initialization expression
- @tok ctx cctx_get_token_or_fail = ;
- tok "(" strcmp 0 == "cctx_compile_statement: ( expected after for" assert_msg ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok ";" strcmp 0 != {
- ctx cctx_give_back_token ;
- ctx lctx TYPE_VOID ";" cctx_compile_expression ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ";" strcmp 0 == "cctx_compile_statement: ; expected after for" assert_msg ;
- }
- # Compile guard expression
- lctx ctx restart_lab lctx_fix_label ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok ";" strcmp 0 != {
- ctx cctx_give_back_token ;
- ctx lctx TYPE_BOOL ";" cctx_compile_expression ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ";" strcmp 0 == "cctx_compile_statement: second ; expected after for" assert_msg ;
- } else {
- # push 1
- ctx 0x6a cctx_emit ;
- ctx 0x01 cctx_emit ;
- }
- # pop eax; test eax, eax; cctx_gen_label_jump; cctx_gen_label_jump
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx lctx break_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
- ctx lctx body_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- # Compile iteration expression
- lctx ctx continue_lab lctx_fix_label ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok ")" strcmp 0 != {
- ctx cctx_give_back_token ;
- ctx lctx TYPE_VOID ")" cctx_compile_expression ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ")" strcmp 0 == "cctx_compile_statement: ) expected after for" assert_msg ;
- }
- # cctx_gen_label_jump
- ctx lctx restart_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- # Compile body
- $old_break_lab
- $old_continue_lab
- @old_break_lab lctx LCTX_BREAK_LABEL take = ;
- @old_continue_lab lctx LCTX_CONTINUE_LABEL take = ;
- lctx LCTX_BREAK_LABEL take_addr break_lab = ;
- lctx LCTX_CONTINUE_LABEL take_addr continue_lab = ;
- lctx ctx body_lab lctx_fix_label ;
- ctx lctx cctx_compile_statement_or_block ;
- lctx LCTX_BREAK_LABEL take_addr old_break_lab = ;
- lctx LCTX_CONTINUE_LABEL take_addr old_continue_lab = ;
- # cctx_gen_label_jump
- ctx lctx continue_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- lctx ctx break_lab lctx_fix_label ;
- @processed 1 = ;
- }
- # Parse while
- if tok "while" strcmp 0 == processed ! && {
- # Set up labels
- $continue_lab
- $break_lab
- @continue_lab lctx ctx lctx_gen_label = ;
- @break_lab lctx ctx lctx_gen_label = ;
- # Compile guard expression
- lctx ctx continue_lab lctx_fix_label ;
- @tok ctx cctx_get_token_or_fail = ;
- tok "(" strcmp 0 == "cctx_compile_statement: ( expected after while" assert_msg ;
- ctx lctx TYPE_BOOL ")" cctx_compile_expression ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ")" strcmp 0 == "cctx_compile_statement: ) expected after while" assert_msg ;
- # pop eax; test eax, eax; cctx_gen_label_jump
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx lctx break_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
- # Compile body
- $old_break_lab
- $old_continue_lab
- @old_break_lab lctx LCTX_BREAK_LABEL take = ;
- @old_continue_lab lctx LCTX_CONTINUE_LABEL take = ;
- lctx LCTX_BREAK_LABEL take_addr break_lab = ;
- lctx LCTX_CONTINUE_LABEL take_addr continue_lab = ;
- ctx lctx cctx_compile_statement_or_block ;
- lctx LCTX_BREAK_LABEL take_addr old_break_lab = ;
- lctx LCTX_CONTINUE_LABEL take_addr old_continue_lab = ;
- # cctx_gen_label_jump
- ctx lctx continue_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- lctx ctx break_lab lctx_fix_label ;
- @processed 1 = ;
- }
- # Parse do ... while
- if tok "do" strcmp 0 == processed ! && {
- # Set up labels
- $continue_lab
- $break_lab
- $restart_lab
- @continue_lab lctx ctx lctx_gen_label = ;
- @break_lab lctx ctx lctx_gen_label = ;
- @restart_lab lctx ctx lctx_gen_label = ;
- # Compile body
- lctx ctx restart_lab lctx_fix_label ;
- $old_break_lab
- $old_continue_lab
- @old_break_lab lctx LCTX_BREAK_LABEL take = ;
- @old_continue_lab lctx LCTX_CONTINUE_LABEL take = ;
- lctx LCTX_BREAK_LABEL take_addr break_lab = ;
- lctx LCTX_CONTINUE_LABEL take_addr continue_lab = ;
- ctx lctx cctx_compile_statement_or_block ;
- lctx LCTX_BREAK_LABEL take_addr old_break_lab = ;
- lctx LCTX_CONTINUE_LABEL take_addr old_continue_lab = ;
- # Except while
- @tok ctx cctx_get_token_or_fail = ;
- tok "while" strcmp 0 == "cctx_compile_statement: while expected after do" assert_msg ;
- # Compile guard expression
- lctx ctx continue_lab lctx_fix_label ;
- @tok ctx cctx_get_token_or_fail = ;
- tok "(" strcmp 0 == "cctx_compile_statement: ( expected after while after do" assert_msg ;
- ctx lctx TYPE_BOOL ")" cctx_compile_expression ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ")" strcmp 0 == "cctx_compile_statement: ) expected after while after do" assert_msg ;
- # pop eax; test eax, eax; cctx_gen_label_jump
- ctx 0x58 cctx_emit ;
- ctx 0x85 cctx_emit ;
- ctx 0xc0 cctx_emit ;
- ctx lctx break_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
- # cctx_gen_label_jump
- ctx lctx restart_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- lctx ctx break_lab lctx_fix_label ;
- @expect_semicolon 1 = ;
- @processed 1 = ;
- }
- # Parse switch
- if tok "switch" strcmp 0 == processed ! && {
- $break_lab
- $logic_lab
- @break_lab lctx ctx lctx_gen_label = ;
- @logic_lab lctx ctx lctx_gen_label = ;
- # Compile switch expression
- @tok ctx cctx_get_token_or_fail = ;
- tok "(" strcmp 0 == "cctx_compile_statement: ( expected after switch" assert_msg ;
- $ast
- @ast ctx ")" cctx_parse_ast1 = ;
- $type_idx
- @type_idx ast ctx lctx ast_eval_type promote_integer_type = ;
- ast ctx lctx ast_push_value ;
- lctx ctx ast ctx lctx ast_eval_type type_idx lctx_convert_stack ;
- lctx ctx type_idx TYPE_ULONG lctx_convert_stack ;
- ast ast_destroy ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ")" strcmp 0 == "cctx_compile_statement: ) expected after switch" assert_msg ;
- # Jump to the switch logic, which is at the end (because for the
- # moment we have no idea of which labels will appear)
- ctx lctx logic_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- # Compile the body
- $old_break_lab
- $old_default_lab
- $old_case_labs
- @old_break_lab lctx LCTX_BREAK_LABEL take = ;
- @old_default_lab lctx LCTX_DEFAULT_LABEL take = ;
- @old_case_labs lctx LCTX_CASE_LABELS take = ;
- lctx LCTX_BREAK_LABEL take_addr break_lab = ;
- lctx LCTX_DEFAULT_LABEL take_addr 0xffffffff = ;
- lctx LCTX_CASE_LABELS take_addr 8 vector_init = ;
- ctx lctx cctx_compile_statement_or_block ;
- # Jump to the break, to avoid doing the switch logic again
- ctx lctx break_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
- # Switch logic
- lctx ctx logic_lab lctx_fix_label ;
- $i
- @i 0 = ;
- $labs
- @labs lctx LCTX_CASE_LABELS take = ;
- while i labs vector_size < {
- $ast
- $lab
- @ast labs i vector_at_addr ** = ;
- @lab labs i vector_at_addr 4 + ** = ;
- ast ctx lctx ast_push_value ;
- lctx ctx ast ctx lctx ast_eval_type type_idx lctx_convert_stack ;
- lctx ctx type_idx TYPE_ULONG lctx_convert_stack ;
- # cctx_gen_label_jump with conditional jump and stack rewind
- # modifies EAX
- # pop eax; pop edx; sub eax, [esp]; sub edx, [esp+4]; or eax, edx; pop ecx; pop edx; cctx_gen_label_jump; push edx; push ecx
- ctx 0x58 cctx_emit ;
- ctx 0x5a cctx_emit ;
- ctx 0x2b cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x2b cctx_emit ;
- ctx 0x54 cctx_emit ;
- ctx 0x24 cctx_emit ;
- ctx 0x04 cctx_emit ;
- ctx 0x09 cctx_emit ;
- ctx 0xd0 cctx_emit ;
- ctx 0x59 cctx_emit ;
- ctx 0x5a cctx_emit ;
- ctx lctx lab JUMP_TYPE_JZ 1 cctx_gen_label_jump ;
- ctx 0x52 cctx_emit ;
- ctx 0x51 cctx_emit ;
- ast ast_destroy ;
- @i i 1 + = ;
- }
- # Discard the switch value
- # pop eax; pop edx
- ctx 0x58 cctx_emit ;
- ctx 0x5a cctx_emit ;
- if lctx LCTX_DEFAULT_LABEL take 0xffffffff != {
- # cctx_gen_label_jump
- ctx lctx lctx LCTX_DEFAULT_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
- }
- lctx LCTX_CASE_LABELS take vector_destroy ;
- lctx LCTX_BREAK_LABEL take_addr old_break_lab = ;
- lctx LCTX_DEFAULT_LABEL take_addr old_default_lab = ;
- lctx LCTX_CASE_LABELS take_addr old_case_labs = ;
- # Finally, fix break label
- lctx ctx break_lab lctx_fix_label ;
- @processed 1 = ;
- }
- # Parse case
- if tok "case" strcmp 0 == processed ! && {
- $labs
- @labs lctx LCTX_CASE_LABELS take = ;
- $ast
- @ast ctx ":" cctx_parse_ast1 = ;
- # Evaluate the AST to check that it is constant
- ctx lctx ast ast_eval_compile ;
- @tok ctx cctx_get_token_or_fail = ;
- tok ":" strcmp 0 == "cctx_compile_statement: expected : after case" assert_msg ;
- labs 0 != "cctx_compile_statement: not in a switch block" assert_msg ;
- $lab
- @lab lctx ctx lctx_gen_label = ;
- lctx ctx lab lctx_fix_label ;
- labs ast vector_push_back ;
- labs labs vector_size 1 - vector_at_addr 4 + lab = ;
- ctx lctx cctx_compile_statement_or_block ;
- @expect_semicolon 0 = ;
- @processed 1 = ;
- }
- # Parse default
- if tok "default" strcmp 0 == processed ! && {
- @tok ctx cctx_get_token_or_fail = ;
- tok ":" strcmp 0 == "cctx_compile_statement: expected : after default" assert_msg ;
- lctx LCTX_CASE_LABELS take 0 != "cctx_compile_statement: not in a switch block" assert_msg ;
- lctx LCTX_DEFAULT_LABEL take 0xffffffff == "cctx_compile_statement: default label already seen" assert_msg ;
- $lab
- @lab lctx ctx lctx_gen_label = ;
- lctx ctx lab lctx_fix_label ;
- lctx LCTX_DEFAULT_LABEL take_addr lab = ;
- ctx lctx cctx_compile_statement_or_block ;
- @expect_semicolon 0 = ;
- @processed 1 = ;
- }
- # Parse goto
- if tok "goto" strcmp 0 == processed ! && {
- @tok ctx cctx_get_token_or_fail = ;
- $lab
- if lctx LCTX_GOTO_LABELS take tok map_has {
- @lab lctx LCTX_GOTO_LABELS take tok map_at = ;
- } else {
- @lab lctx ctx lctx_gen_label = ;
- lctx LCTX_GOTO_LABELS take tok lab map_set ;
- }
- # cctx_gen_label_jump
- ctx lctx lab JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
- @processed 1 = ;
- @expect_semicolon 1 = ;
- }
- # Consider one more token to see if it is a goto label
- if processed ! {
- $tok2
- @tok2 ctx cctx_get_token_or_fail = ;
- if tok2 ":" strcmp 0 == {
- $lab
- if lctx LCTX_GOTO_LABELS take tok map_has {
- @lab lctx LCTX_GOTO_LABELS take tok map_at = ;
- } else {
- @lab lctx ctx lctx_gen_label = ;
- lctx LCTX_GOTO_LABELS take tok lab map_set ;
- }
- lctx ctx lab lctx_fix_label ;
- ctx lctx cctx_compile_statement_or_block ;
- @expect_semicolon 0 = ;
- @processed 1 = ;
- } else {
- ctx cctx_give_back_token ;
- }
- }
- if processed ! {
- ctx cctx_give_back_token ;
- # Try to parse a type, in which case we have a variable declaration
- $type_idx
- @type_idx ctx cctx_parse_type = ;
- if type_idx 0xffffffff != {
- # There is a type, so we have a variable declaration
- $cont
- @cont 1 = ;
- while cont {
- $actual_type_idx
- $name
- ctx lctx type_idx @actual_type_idx @name 0 cctx_parse_declarator "cctx_compile_statement: error 1" assert_msg ;
- name 0 != "cctx_compile_statement: cannot instantiate variable without name" assert_msg ;
- lctx ctx actual_type_idx name lctx_push_var ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok "=" strcmp 0 == {
- $right_ast
- $left_ast
- $ast
- @right_ast ctx "," ";" cctx_parse_ast2 = ;
- @left_ast ast_init = ;
- left_ast AST_TYPE take_addr 0 = ;
- left_ast AST_NAME take_addr name strdup = ;
- @ast ast_init = ;
- ast AST_TYPE take_addr 1 = ;
- ast AST_NAME take_addr "=" strdup = ;
- ast AST_RIGHT take_addr right_ast = ;
- ast AST_LEFT take_addr left_ast = ;
- ast ctx lctx ast_c_eval ;
- ast ast_destroy ;
- } else {
- ctx cctx_give_back_token ;
- }
- @tok ctx cctx_get_token_or_fail = ;
- if tok ";" strcmp 0 == {
- ctx cctx_give_back_token ;
- @cont 0 = ;
- } else {
- tok "," strcmp 0 == "cctx_compile_statement: comma expected" assert_msg ;
- }
- }
- } else {
- # No type, so this is an expression
- ctx lctx TYPE_VOID ";" cctx_compile_expression ;
- }
- @expect_semicolon 1 = ;
- }
- # Expect and consume the semicolon
- if expect_semicolon {
- @tok ctx cctx_get_token_or_fail = ;
- tok ";" strcmp 0 == "cctx_compile_statement: ; expected" assert_msg ;
- }
- 1 ret ;
- }
- fun cctx_compile_block 2 {
- $ctx
- $lctx
- @ctx 1 param = ;
- @lctx 0 param = ;
- $saved_pos
- @saved_pos lctx ctx lctx_save_status = ;
- $cont
- @cont 1 = ;
- while cont {
- @cont ctx lctx cctx_compile_statement = ;
- }
- lctx ctx saved_pos lctx_restore_status ;
- }
- fun cctx_compile_statement_or_block 2 {
- $ctx
- $lctx
- @ctx 1 param = ;
- @lctx 0 param = ;
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok "{" strcmp 0 == {
- ctx lctx cctx_compile_block ;
- } else {
- ctx cctx_give_back_token ;
- ctx lctx cctx_compile_statement ;
- }
- }
- fun cctx_compile_function 3 {
- $ctx
- $type_idx
- $arg_names
- @ctx 2 param = ;
- @type_idx 1 param = ;
- @arg_names 0 param = ;
- # Costruct the local context
- $lctx
- @lctx lctx_init = ;
- lctx LCTX_RETURN_LABEL take_addr lctx ctx lctx_gen_label = ;
- lctx LCTX_RETURN_TYPE_IDX take_addr ctx type_idx cctx_get_type TYPE_BASE take = ;
- lctx ctx type_idx arg_names lctx_prime_stack ;
- lctx ctx lctx_gen_prologue ;
- ctx lctx cctx_compile_block ;
- lctx ctx lctx LCTX_RETURN_LABEL take lctx_fix_label ;
- lctx ctx lctx_gen_epilogue ;
- lctx lctx_destroy ;
- }
- fun cctx_mangle_function_type 2 {
- $ctx
- $type_idx
- @ctx 1 param = ;
- @type_idx 0 param = ;
- $type
- @type ctx type_idx cctx_get_type = ;
- type TYPE_KIND take TYPE_KIND_FUNCTION == "cctx_mangle_function_type: not a function type" assert_msg ;
- $base_idx
- @base_idx type TYPE_BASE take = ;
- $args
- @args type TYPE_ARGS take = ;
- $new_args
- @new_args 4 vector_init = ;
- $ellipsis
- @ellipsis type TYPE_ELLIPSIS take = ;
- $i
- @i 0 = ;
- while i args vector_size < {
- $arg_idx
- @arg_idx args i vector_at = ;
- $arg_type
- @arg_type ctx arg_idx cctx_get_type = ;
- if arg_type TYPE_KIND take TYPE_KIND_ARRAY == {
- $arg_base
- @arg_base arg_type TYPE_BASE take = ;
- @arg_idx ctx arg_base cctx_get_pointer_type = ;
- }
- if arg_type TYPE_KIND take TYPE_KIND_FUNCTION == {
- @arg_idx ctx arg_idx cctx_get_pointer_type = ;
- }
- new_args arg_idx vector_push_back ;
- @i i 1 + = ;
- }
- ctx base_idx new_args ellipsis cctx_get_function_type ret ;
- }
- fun cctx_assign_with_size 4 {
- $ctx
- $loc
- $value
- $size
- @ctx 3 param = ;
- @loc 2 param = ;
- @value 1 param = ;
- @size 0 param = ;
- if ctx CCTX_STAGE take 2 != {
- ret ;
- }
- if size 1 == {
- loc value ** =c ;
- ret ;
- }
- if size 2 == {
- loc value ** =c ;
- loc 1 + value ** 8 >> =c ;
- ret ;
- }
- if size 4 == {
- loc value ** = ;
- ret ;
- }
- if size 8 == {
- loc value ** = ;
- loc 4 + value 4 + ** = ;
- ret ;
- }
- 0 "cctx_assign_with_size: invalid size" assert_msg ;
- }
- fun cctx_parse_initializer 3 {
- $ctx
- $type_idx
- $loc
- @ctx 2 param = ;
- @type_idx 1 param = ;
- @loc 0 param = ;
- $type
- @type ctx type_idx cctx_get_type = ;
- if type_idx is_integer_type type TYPE_KIND take TYPE_KIND_POINTER == || {
- $ast
- @ast ctx "," "}" ";" cctx_parse_ast3 = ;
- #ast ast_dump ;
- ctx 0 ast ast_eval_compile ;
- $size
- @size ctx type_idx cctx_type_size = ;
- ctx loc ast AST_VALUE take size cctx_assign_with_size ;
- ast ast_destroy ;
- 0xffffffff ret ;
- }
- if type TYPE_KIND take TYPE_KIND_STRUCT == {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- tok "{" strcmp 0 == "cctx_parse_initializer: { expected" assert_msg ;
- $i
- @i 0 = ;
- $offs
- @offs type TYPE_FIELDS_OFFS take = ;
- $type_idxs
- @type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
- while i offs vector_size < {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "}" strcmp 0 == {
- @i offs vector_size = ;
- } else {
- ctx cctx_give_back_token ;
- ctx type_idxs i vector_at loc offs i vector_at + cctx_parse_initializer ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok "}" strcmp 0 == {
- @i offs vector_size = ;
- } else {
- tok "," strcmp 0 == "cctx_parse_initializer: , or } expected" assert_msg ;
- @i i 1 + = ;
- }
- }
- }
- tok "}" strcmp 0 == "cctx_parse_initializer: initializer has too many entries" assert_msg ;
- 0xffffffff ret ;
- }
- if type TYPE_KIND take TYPE_KIND_ARRAY == type TYPE_BASE take TYPE_CHAR == && {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok **c '\"' == {
- @tok tok 1 + = ;
- $i
- @i 0 = ;
- $len
- @len type TYPE_LENGTH take = ;
- while i len 1 - < len 0xffffffff == || {
- if tok **c '\"' == {
- tok 1 + **c '\0' == "cctx_parse_initializer: illegal string literal" assert_msg ;
- # Add the final blank
- if ctx CCTX_STAGE take 0 != {
- loc '\0' =c ;
- }
- if len 0xffffffff == {
- @len i 1 + = ;
- }
- @i len 1 - = ;
- } else {
- if ctx CCTX_STAGE take 0 == {
- @tok 1 ctx escape_char ;
- } else {
- @tok @loc ctx escape_char ;
- }
- @i i 1 + = ;
- }
- }
- tok **c '\"' == "cctx_parse_initializer: initializer string is too long" assert_msg ;
- tok 1 + **c '\0' == "cctx_parse_initializer: illegal string literal" assert_msg ;
- len ret ;
- } else {
- ctx cctx_give_back_token ;
- }
- }
- if type TYPE_KIND take TYPE_KIND_ARRAY == {
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- tok "{" strcmp 0 == "cctx_parse_initializer: { expected" assert_msg ;
- $i
- @i 0 = ;
- $len
- @len type TYPE_LENGTH take = ;
- $base_type_idx
- @base_type_idx type TYPE_BASE take = ;
- $base_size
- @base_size ctx base_type_idx cctx_type_size = ;
- while i len < len 0xffffffff == || {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "}" strcmp 0 == {
- if len 0xffffffff == {
- @len i = ;
- }
- @i len = ;
- } else {
- ctx cctx_give_back_token ;
- ctx base_type_idx loc i base_size * + cctx_parse_initializer ;
- @tok ctx cctx_get_token_or_fail = ;
- @i i 1 + = ;
- if tok "}" strcmp 0 == {
- if len 0xffffffff == {
- @len i = ;
- }
- @i len = ;
- } else {
- tok "," strcmp 0 == "cctx_parse_initializer: , or } expected" assert_msg ;
- }
- }
- }
- if tok "," strcmp 0 == {
- @tok ctx cctx_get_token_or_fail = ;
- }
- tok "}" strcmp 0 == "cctx_parse_initializer: initializer has too many entries" assert_msg ;
- len ret ;
- }
- 0 "cctx_parse_initializer: not implemented" assert_msg ;
- }
- fun cctx_compile_line 1 {
- $ctx
- @ctx 0 param = ;
- $tok
- @tok ctx cctx_get_token_or_fail = ;
- if tok "typedef" strcmp 0 == {
- $type_idx
- @type_idx ctx cctx_parse_type = ;
- type_idx 0xffffffff != "cctx_compile_line: type expected after typedef" assert_msg ;
- $cont
- @cont 1 = ;
- while cont {
- $actual_type_idx
- $name
- ctx 0 type_idx @actual_type_idx @name 0 cctx_parse_declarator "cctx_compile_line: could not parse declarator after typedef" assert_msg ;
- name 0 != "cctx_compile_line: cannot define type without name" assert_msg ;
- $typenames
- @typenames ctx CCTX_TYPENAMES take = ;
- typenames name map_has ! "cctx_compile_line: type name already defined" assert_msg ;
- typenames name actual_type_idx map_set ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok ";" strcmp 0 == {
- @cont 0 = ;
- } else {
- tok "," strcmp 0 == "cctx_compile_line: comma expected after typedef" assert_msg ;
- }
- }
- ret ;
- } else {
- ctx cctx_give_back_token ;
- }
- $extern
- $static
- @extern 0 = ;
- @static 0 = ;
- $cont
- @cont 1 = ;
- while cont {
- @tok ctx cctx_get_token_or_fail = ;
- if tok "extern" strcmp 0 == {
- @extern 1 = ;
- } else {
- if tok "static" strcmp 0 == {
- @static 1 = ;
- } else {
- ctx cctx_give_back_token ;
- @cont 0 = ;
- }
- }
- }
- $type_idx
- @type_idx ctx cctx_parse_type = ;
- type_idx 0xffffffff != "cctx_compile: type expected" tok assert_msg_str ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok ";" strcmp 0 == {
- @cont 0 = ;
- } else {
- ctx cctx_give_back_token ;
- @cont 1 = ;
- }
- while cont {
- $actual_type_idx
- $name
- $arg_names
- @arg_names 4 vector_init = ;
- ctx 0 type_idx @actual_type_idx @name arg_names cctx_parse_declarator "cctx_compile_line: could not parse declarator" assert_msg ;
- $type
- @type ctx actual_type_idx cctx_get_type = ;
- name 0 != "cctx_compile_line: cannot instantiate variable without name" assert_msg ;
- if type TYPE_KIND take TYPE_KIND_FUNCTION == {
- # If it is a function, first mangle its parameters' types
- @actual_type_idx ctx actual_type_idx cctx_mangle_function_type = ;
- @type ctx actual_type_idx cctx_get_type = ;
- # Then check if it has a body
- @tok ctx cctx_get_token_or_fail = ;
- if tok "{" strcmp 0 == {
- # There is the body, register the global and compile the body
- ctx name ctx CCTX_CURRENT_LOC take actual_type_idx cctx_add_global ;
- ctx actual_type_idx arg_names cctx_compile_function ;
- @cont 0 = ;
- } else {
- # No body, register the global with a fictious location
- ctx cctx_give_back_token ;
- ctx name 0xffffffff actual_type_idx cctx_add_global ;
- }
- } else {
- if extern {
- ctx name 0xffffffff actual_type_idx cctx_add_global ;
- } else {
- # If it is anything else, register it and allocate its size
- $loc
- @loc ctx CCTX_CURRENT_LOC take = ;
- $initializer
- @initializer 0 = ;
- @tok ctx cctx_get_token_or_fail = ;
- if tok "=" strcmp 0 == {
- @initializer 1 = ;
- } else {
- ctx cctx_give_back_token ;
- }
- if ctx CCTX_STAGE take 0 == {
- # During stage 0, if there is an initializer parse it
- # immediately, because it might encode the length of an
- # array
- if initializer {
- $actual_len
- @actual_len ctx actual_type_idx loc cctx_parse_initializer = ;
- if type TYPE_KIND take TYPE_KIND_ARRAY == type TYPE_LENGTH take 0xffffffff == && {
- actual_len 0xffffffff != "cctx_compile_line: error 1" assert_msg ;
- ctx CCTX_ARRAY_LENS take name actual_len map_set ;
- @actual_type_idx ctx type TYPE_BASE take actual_len cctx_get_array_type = ;
- @type ctx actual_type_idx cctx_get_type = ;
- }
- }
- #type type_dump ;
- $found
- @found 0 = ;
- if static ctx name cctx_has_global && {
- @found 1 = ;
- $global
- @global ctx name cctx_get_global = ;
- ctx global GLOBAL_TYPE_IDX take actual_type_idx cctx_type_compare "cctx_compile_line: global redefined with another type" name assert_msg_str ;
- if initializer {
- global GLOBAL_INITIALIZED take ! "cctx_compile_line: global initialized more than once" name assert_msg_str ;
- global GLOBAL_INITIALIZED take_addr 1 = ;
- }
- }
- if found ! {
- ctx name loc actual_type_idx cctx_add_global ;
- ctx ctx actual_type_idx cctx_type_footprint cctx_emit_zeros ;
- }
- } else {
- if initializer type TYPE_KIND take TYPE_KIND_ARRAY == && type TYPE_LENGTH take 0xffffffff == && {
- $actual_len
- @actual_len ctx CCTX_ARRAY_LENS take name map_at = ;
- @actual_type_idx ctx type TYPE_BASE take actual_len cctx_get_array_type = ;
- @type ctx actual_type_idx cctx_get_type = ;
- }
- #type type_dump ;
- $emit
- @emit 1 = ;
- if static {
- $global
- @global ctx name cctx_get_global = ;
- if global GLOBAL_EMITTED take ! {
- global GLOBAL_EMITTED take_addr 1 = ;
- } else {
- @emit 0 = ;
- @loc global GLOBAL_LOC take = ;
- }
- }
- if emit {
- ctx name loc actual_type_idx cctx_add_global ;
- ctx ctx actual_type_idx cctx_type_footprint cctx_emit_zeros ;
- }
- if initializer {
- ctx actual_type_idx loc cctx_parse_initializer ;
- }
- }
- }
- }
- arg_names vector_destroy ;
- if cont {
- @tok ctx cctx_get_token_or_fail = ;
- if tok ";" strcmp 0 == {
- @cont 0 = ;
- } else {
- tok "," strcmp 0 == "cctx_compile_line: comma expected" assert_msg ;
- }
- }
- }
- }
- fun cctx_compile 1 {
- $ctx
- @ctx 0 param = ;
- ctx CCTX_STAGE take_addr 0 = ;
- $start_loc
- @start_loc 0 = ;
- $size
- while ctx CCTX_STAGE take 3 < {
- if ctx CCTX_VERBOSE take {
- "Compilation stage " log ;
- ctx CCTX_STAGE take 1 + itoa log ;
- }
- ctx CCTX_CURRENT_LOC take_addr start_loc = ;
- ctx CCTX_TOKENS_POS take_addr 0 = ;
- ctx CCTX_DOT_POS take_addr 0 = ;
- ctx CCTX_LABEL_NUM take_addr 0 = ;
- ctx cctx_reset_types ;
- ctx cctx_clean_globals ;
- ctx cctx_create_basic_types ;
- ctx "__builtin_handles" ctx CCTX_HANDLES take vector_data TYPE_VOID_PTR cctx_add_global ;
- while ctx cctx_is_eof ! {
- ctx cctx_compile_line ;
- }
- if ctx CCTX_VERBOSE take {
- "\n" log ;
- }
- if ctx CCTX_STAGE take 0 == {
- @size ctx CCTX_CURRENT_LOC take start_loc - = ;
- @start_loc size platform_allocate = ;
- } else {
- ctx CCTX_CURRENT_LOC take start_loc - size == "cctx_compile: error 1" assert_msg ;
- }
- ctx CCTX_STAGE take_addr ctx CCTX_STAGE take 1 + = ;
- }
- if ctx CCTX_VERBOSE take {
- "Compiled program has size " log ;
- size itoa log ;
- " and starts at " log ;
- start_loc itoa log ;
- "\n" log ;
- }
- if ctx CCTX_DEBUG take {
- "Compiled dump:\n" log ;
- start_loc size dump_mem ;
- "\n" log ;
- }
- }
- fun parse_c 1 {
- $filename
- @filename 0 param = ;
- # Preprocessing
- $ctx
- @ctx ppctx_init = ;
- ctx filename ppctx_set_base_filename ;
- $tokens
- @tokens 4 vector_init = ;
- tokens ctx filename preproc_file ;
- @tokens tokens remove_whites = ;
- @tokens tokens collapse_strings = ;
- "Finished preprocessing\n" log ;
- #tokens print_token_list ;
- # Compilation
- $cctx
- @cctx tokens cctx_init = ;
- cctx CCTX_DEBUG take_addr 0 = ;
- cctx cctx_compile ;
- # Debug output
- # "TYPES TABLE\n" log ;
- # cctx cctx_dump_types ;
- # "TYPE NAMES TABLE\n" log ;
- # cctx cctx_dump_typenames ;
- # "GLOBALS TABLE\n" log ;
- # cctx cctx_dump_globals ;
- cctx cctx_print_stats ;
- # Try to execute the code
- "Executing compiled code...\n" log ;
- $main_global
- @main_global cctx "_start" cctx_get_global = ;
- $main_addr
- @main_addr main_global GLOBAL_LOC take = ;
- $arg
- @arg "_main" = ;
- $res
- @res @arg 1 main_addr \2 = ;
- "It returned " log ;
- res itoa log ;
- "\n" log ;
- # Cleanup
- tokens free_vect_of_ptrs ;
- cctx cctx_destroy ;
- ctx ppctx_destroy ;
- }
|