c_compile.g 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365
  1. # This file is part of asmc, a bootstrapping OS with minimal seed
  2. # Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. # https://gitlab.com/giomasce/asmc
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. ifun cctx_emit 2
  15. fun escape_char 2 {
  16. $from
  17. $to
  18. $ctx
  19. @from 2 param = ;
  20. @to 1 param = ;
  21. @ctx 0 param = ;
  22. $emit
  23. @emit to 0 == = ;
  24. $data
  25. @data 0 = ;
  26. $data_ptr
  27. @data_ptr @data = ;
  28. if emit {
  29. @to @data_ptr = ;
  30. }
  31. from ** **c 0 != "escape_char: unexpected null" assert_msg ;
  32. if from ** **c '\\' == {
  33. from from ** 1 + = ;
  34. $c
  35. @c from ** **c = ;
  36. c 0 != "escape_char: unexpected null" assert_msg ;
  37. $processed
  38. @processed 0 = ;
  39. if c '0' == {
  40. to ** 0 =c ;
  41. @processed 1 = ;
  42. }
  43. if c 'n' == {
  44. to ** '\n' =c ;
  45. @processed 1 = ;
  46. }
  47. if c 'r' == {
  48. to ** '\r' =c ;
  49. @processed 1 = ;
  50. }
  51. if c 't' == {
  52. to ** '\t' =c ;
  53. @processed 1 = ;
  54. }
  55. if c 'v' == {
  56. to ** '\v' =c ;
  57. @processed 1 = ;
  58. }
  59. if c 'f' == {
  60. to ** '\f' =c ;
  61. @processed 1 = ;
  62. }
  63. if c '\\' == {
  64. to ** '\\' =c ;
  65. @processed 1 = ;
  66. }
  67. if c '\'' == {
  68. to ** '\'' =c ;
  69. @processed 1 = ;
  70. }
  71. if c '\"' == {
  72. to ** '\"' =c ;
  73. @processed 1 = ;
  74. }
  75. processed "escape_char: unknown escape sequence" assert_msg ;
  76. from from ** 1 + = ;
  77. to to ** 1 + = ;
  78. } else {
  79. to ** from ** **c =c ;
  80. from from ** 1 + = ;
  81. to to ** 1 + = ;
  82. }
  83. if emit {
  84. data_ptr @data 1 + == "escape_char: error 1" assert_msg ;
  85. ctx data cctx_emit ;
  86. }
  87. }
  88. const TYPE_KIND_BASE 0
  89. const TYPE_KIND_POINTER 1
  90. const TYPE_KIND_FUNCTION 2
  91. const TYPE_KIND_ARRAY 3
  92. const TYPE_KIND_STRUCT 4
  93. const TYPE_KIND_UNION 5
  94. const TYPE_KIND_ENUM 6
  95. const TYPE_KIND 0
  96. const TYPE_BASE 4
  97. const TYPE_SIZE 8
  98. const TYPE_LENGTH 12
  99. const TYPE_ARGS 16
  100. const TYPE_ELLIPSIS 20
  101. const TYPE_FIELDS_OFFS 24
  102. const TYPE_FIELDS_TYPE_IDXS 28
  103. const TYPE_FIELDS_NAMES 32
  104. const SIZEOF_TYPE 36
  105. fun type_init 0 {
  106. $type
  107. @type SIZEOF_TYPE malloc = ;
  108. type TYPE_ARGS take_addr 4 vector_init = ;
  109. type TYPE_FIELDS_OFFS take_addr 4 vector_init = ;
  110. type TYPE_FIELDS_TYPE_IDXS take_addr 4 vector_init = ;
  111. type TYPE_FIELDS_NAMES take_addr 4 vector_init = ;
  112. type ret ;
  113. }
  114. fun type_destroy 1 {
  115. $type
  116. @type 0 param = ;
  117. type TYPE_ARGS take vector_destroy ;
  118. type TYPE_FIELDS_OFFS take vector_destroy ;
  119. type TYPE_FIELDS_TYPE_IDXS take vector_destroy ;
  120. type TYPE_FIELDS_NAMES take free_vect_of_ptrs ;
  121. type free ;
  122. }
  123. fun type_dump 1 {
  124. $type
  125. @type 0 param = ;
  126. $kind
  127. @kind type TYPE_KIND take = ;
  128. $base
  129. @base type TYPE_BASE take = ;
  130. if kind TYPE_KIND_BASE == {
  131. "Base type #" 1 platform_log ;
  132. base itoa 1 platform_log ;
  133. }
  134. if kind TYPE_KIND_POINTER == {
  135. "Pointer type to #" 1 platform_log ;
  136. base itoa 1 platform_log ;
  137. }
  138. if kind TYPE_KIND_FUNCTION == {
  139. "Function type returning #" 1 platform_log ;
  140. base itoa 1 platform_log ;
  141. $args
  142. @args type TYPE_ARGS take = ;
  143. $ellipsis
  144. @ellipsis type TYPE_ELLIPSIS take = ;
  145. if args vector_size 0 == ellipsis ! && {
  146. " taking no argument" 1 platform_log ;
  147. } else {
  148. " taking arguments" 1 platform_log ;
  149. $i
  150. @i 0 = ;
  151. while i args vector_size < {
  152. " #" 1 platform_log ;
  153. args i vector_at itoa 1 platform_log ;
  154. @i i 1 + = ;
  155. }
  156. if ellipsis {
  157. " ..." 1 platform_log ;
  158. }
  159. }
  160. }
  161. if kind TYPE_KIND_ARRAY == {
  162. "Array type of #" 1 platform_log ;
  163. base itoa 1 platform_log ;
  164. $length
  165. @length type TYPE_LENGTH take = ;
  166. if length 0xffffffff == {
  167. " of unspecified length" 1 platform_log ;
  168. } else {
  169. " of length " 1 platform_log ;
  170. length itoa 1 platform_log ;
  171. }
  172. }
  173. if kind TYPE_KIND_STRUCT == {
  174. "Struct type with fields" 1 platform_log ;
  175. $i
  176. @i 0 = ;
  177. $names
  178. $type_idxs
  179. $offs
  180. @names type TYPE_FIELDS_NAMES take = ;
  181. @type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
  182. @offs type TYPE_FIELDS_OFFS take = ;
  183. while i names vector_size < {
  184. " " 1 platform_log ;
  185. names i vector_at 1 platform_log ;
  186. " (@" 1 platform_log ;
  187. offs i vector_at itoa 1 platform_log ;
  188. " #" 1 platform_log ;
  189. type_idxs i vector_at itoa 1 platform_log ;
  190. ")" 1 platform_log ;
  191. @i i 1 + = ;
  192. }
  193. }
  194. if kind TYPE_KIND_UNION == {
  195. "Union type with fields" 1 platform_log ;
  196. $i
  197. @i 0 = ;
  198. $names
  199. $type_idxs
  200. @names type TYPE_FIELDS_NAMES take = ;
  201. @type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
  202. while i names vector_size < {
  203. " " 1 platform_log ;
  204. names i vector_at 1 platform_log ;
  205. " (#" 1 platform_log ;
  206. type_idxs i vector_at itoa 1 platform_log ;
  207. ")" 1 platform_log ;
  208. @i i 1 + = ;
  209. }
  210. }
  211. $size
  212. @size type TYPE_SIZE take = ;
  213. if size 0xffffffff == {
  214. ", of undertermined size" 1 platform_log ;
  215. } else {
  216. ", of size " 1 platform_log ;
  217. size itoa 1 platform_log ;
  218. }
  219. }
  220. fun type_get_idx 2 {
  221. $type
  222. $name
  223. @type 1 param = ;
  224. @name 0 param = ;
  225. 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 ;
  226. $i
  227. @i 0 = ;
  228. $names
  229. @names type TYPE_FIELDS_NAMES take = ;
  230. while i names vector_size < {
  231. if name names i vector_at strcmp 0 == {
  232. i ret ;
  233. }
  234. @i i 1 + = ;
  235. }
  236. 0xffffffff ret ;
  237. }
  238. const GLOBAL_TYPE_IDX 0
  239. const GLOBAL_LOC 4
  240. const SIZEOF_GLOBAL 8
  241. fun global_init 0 {
  242. $global
  243. @global SIZEOF_GLOBAL malloc = ;
  244. global ret ;
  245. }
  246. fun global_destroy 1 {
  247. $global
  248. @global 0 param = ;
  249. global free ;
  250. }
  251. fun global_dump 1 {
  252. $global
  253. @global 0 param = ;
  254. "has type #" 1 platform_log ;
  255. global GLOBAL_TYPE_IDX take itoa 1 platform_log ;
  256. " and is stored at " 1 platform_log ;
  257. global GLOBAL_LOC take itoa 1 platform_log ;
  258. }
  259. const CCTX_TYPES 0
  260. const CCTX_TYPENAMES 4
  261. const CCTX_GLOBALS 8
  262. const CCTX_TOKENS 12
  263. const CCTX_TOKENS_POS 16
  264. const CCTX_STAGE 20
  265. const CCTX_CURRENT_LOC 24
  266. const CCTX_LABEL_POS 28
  267. const CCTX_LABEL_BUF 32
  268. const CCTX_LABEL_NUM 36
  269. const CCTX_STRUCTS 40
  270. const CCTX_UNIONS 44
  271. const CCTX_ENUM_CONSTS 48
  272. const CCTX_HANDLES 52
  273. const CCTX_VERBOSE 56
  274. const SIZEOF_CCTX 60
  275. fun cctx_init_types 1 {
  276. $ctx
  277. @ctx 0 param = ;
  278. ctx CCTX_TYPES take_addr 4 vector_init = ;
  279. ctx CCTX_TYPENAMES take_addr map_init = ;
  280. ctx CCTX_STRUCTS take_addr map_init = ;
  281. ctx CCTX_UNIONS take_addr map_init = ;
  282. ctx CCTX_ENUM_CONSTS take_addr map_init = ;
  283. }
  284. fun cctx_setup_handles 1 {
  285. $ctx
  286. @ctx 0 param = ;
  287. $handles
  288. @handles ctx CCTX_HANDLES take = ;
  289. handles @platform_write_char vector_push_back ;
  290. handles @platform_setjmp vector_push_back ;
  291. handles @platform_longjmp vector_push_back ;
  292. handles @malloc vector_push_back ;
  293. handles @calloc vector_push_back ;
  294. handles @free vector_push_back ;
  295. handles @realloc vector_push_back ;
  296. handles @itoa vector_push_back ;
  297. }
  298. fun cctx_init 1 {
  299. $tokens
  300. @tokens 0 param = ;
  301. $ctx
  302. @ctx SIZEOF_CCTX malloc = ;
  303. ctx cctx_init_types ;
  304. ctx CCTX_GLOBALS take_addr map_init = ;
  305. ctx CCTX_TOKENS take_addr tokens = ;
  306. ctx CCTX_TOKENS_POS take_addr 0 = ;
  307. ctx CCTX_LABEL_NUM take_addr 0 = ;
  308. ctx CCTX_LABEL_BUF take_addr 32 malloc = ;
  309. ctx CCTX_LABEL_POS take_addr 4 vector_init = ;
  310. ctx CCTX_HANDLES take_addr 4 vector_init = ;
  311. ctx CCTX_VERBOSE take_addr 1 = ;
  312. ctx cctx_setup_handles ;
  313. ctx ret ;
  314. }
  315. fun cctx_destroy_types 1 {
  316. $ctx
  317. @ctx 0 param = ;
  318. $types
  319. @types ctx CCTX_TYPES take = ;
  320. $i
  321. @i 0 = ;
  322. while i types vector_size < {
  323. types i vector_at type_destroy ;
  324. @i i 1 + = ;
  325. }
  326. types vector_destroy ;
  327. $typenames
  328. @typenames ctx CCTX_TYPENAMES take = ;
  329. typenames map_destroy ;
  330. $structs
  331. @structs ctx CCTX_STRUCTS take = ;
  332. structs map_destroy ;
  333. $unions
  334. @unions ctx CCTX_UNIONS take = ;
  335. unions map_destroy ;
  336. ctx CCTX_ENUM_CONSTS take map_destroy ;
  337. }
  338. fun global_destroy_closure 3 {
  339. $ctx
  340. $key
  341. $value
  342. @ctx 2 param = ;
  343. @key 1 param = ;
  344. @value 0 param = ;
  345. value global_destroy ;
  346. }
  347. fun cctx_destroy 1 {
  348. $ctx
  349. @ctx 0 param = ;
  350. ctx cctx_destroy_types ;
  351. $globals
  352. @globals ctx CCTX_GLOBALS take = ;
  353. globals @global_destroy_closure 0 map_foreach ;
  354. globals map_destroy ;
  355. ctx CCTX_LABEL_POS take vector_destroy ;
  356. ctx CCTX_LABEL_BUF take free ;
  357. ctx CCTX_HANDLES take vector_destroy ;
  358. ctx free ;
  359. }
  360. fun cctx_reset_types 1 {
  361. $ctx
  362. @ctx 0 param = ;
  363. ctx cctx_destroy_types ;
  364. ctx cctx_init_types ;
  365. }
  366. fun cctx_get_type 2 {
  367. $ctx
  368. $type_idx
  369. @ctx 1 param = ;
  370. @type_idx 0 param = ;
  371. ctx CCTX_TYPES take type_idx vector_at ret ;
  372. }
  373. fun cctx_dump_types 1 {
  374. $ctx
  375. @ctx 0 param = ;
  376. $i
  377. @i 0 = ;
  378. $types
  379. @types ctx CCTX_TYPES take = ;
  380. while i types vector_size < {
  381. "#" 1 platform_log ;
  382. i itoa 1 platform_log ;
  383. ": " 1 platform_log ;
  384. types i vector_at type_dump ;
  385. "\n" 1 platform_log ;
  386. @i i 1 + = ;
  387. }
  388. }
  389. fun dump_typename_closure 3 {
  390. $ctx
  391. $key
  392. $value
  393. @ctx 2 param = ;
  394. @key 1 param = ;
  395. @value 0 param = ;
  396. "Typename " 1 platform_log ;
  397. key 1 platform_log ;
  398. ": #" 1 platform_log ;
  399. value itoa 1 platform_log ;
  400. "\n" 1 platform_log ;
  401. }
  402. fun cctx_dump_typenames 1 {
  403. $ctx
  404. @ctx 0 param = ;
  405. $typenames
  406. @typenames ctx CCTX_TYPENAMES take = ;
  407. typenames @dump_typename_closure 0 map_foreach ;
  408. }
  409. fun dump_global_closure 3 {
  410. $ctx
  411. $key
  412. $value
  413. @ctx 2 param = ;
  414. @key 1 param = ;
  415. @value 0 param = ;
  416. "Global " 1 platform_log ;
  417. key 1 platform_log ;
  418. ": " 1 platform_log ;
  419. value global_dump ;
  420. "\n" 1 platform_log ;
  421. }
  422. fun cctx_dump_globals 1 {
  423. $ctx
  424. @ctx 0 param = ;
  425. $globals
  426. @globals ctx CCTX_GLOBALS take = ;
  427. globals @dump_global_closure 0 map_foreach ;
  428. }
  429. ifun cctx_type_compare 3
  430. fun _cctx_type_compare 3 {
  431. $ctx
  432. $t1
  433. $t2
  434. @ctx 2 param = ;
  435. @t1 1 param = ;
  436. @t2 0 param = ;
  437. if t1 TYPE_KIND take t2 TYPE_KIND take != { 0 ret ; }
  438. if t1 TYPE_KIND take TYPE_KIND_BASE == {
  439. if t1 TYPE_BASE take t2 TYPE_BASE take != { 0 ret ; }
  440. 1 ret ;
  441. }
  442. if t1 TYPE_KIND take TYPE_KIND_POINTER == {
  443. if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare ! { 0 ret ; }
  444. 1 ret ;
  445. }
  446. if t1 TYPE_KIND take TYPE_KIND_FUNCTION == {
  447. if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare ! { 0 ret ; }
  448. if t1 TYPE_ELLIPSIS take t2 TYPE_ELLIPSIS take != { 0 ret ; }
  449. $args1
  450. $args2
  451. @args1 t1 TYPE_ARGS take = ;
  452. @args2 t2 TYPE_ARGS take = ;
  453. if args1 vector_size args2 vector_size != { 0 ret ; }
  454. $i
  455. @i 0 = ;
  456. while i args1 vector_size < {
  457. if ctx args1 i vector_at args2 i vector_at cctx_type_compare ! { 0 ret ; }
  458. @i i 1 + = ;
  459. }
  460. 1 ret ;
  461. }
  462. if t1 TYPE_KIND take TYPE_KIND_ARRAY == {
  463. if ctx t1 TYPE_BASE take t2 TYPE_BASE take cctx_type_compare ! { 0 ret ; }
  464. if t1 TYPE_LENGTH take t2 TYPE_LENGTH take != { 0 ret ; }
  465. 1 ret ;
  466. }
  467. # structs and unions are always different unless they have the same
  468. # type index, which has already been checked
  469. if t1 TYPE_KIND take TYPE_KIND_STRUCT == {
  470. 0 ret ;
  471. }
  472. if t1 TYPE_KIND take TYPE_KIND_UNION == {
  473. 0 ret ;
  474. }
  475. 0 "_type_compare: not yet implemented" assert_msg ;
  476. }
  477. fun cctx_type_compare 3 {
  478. $ctx
  479. $ti1
  480. $ti2
  481. @ctx 2 param = ;
  482. @ti1 1 param = ;
  483. @ti2 0 param = ;
  484. if ti1 ti2 == { 1 ret ; }
  485. $t1
  486. $t2
  487. @t1 ctx CCTX_TYPES take ti1 vector_at = ;
  488. @t2 ctx CCTX_TYPES take ti2 vector_at = ;
  489. $res
  490. @res ctx t1 t2 _cctx_type_compare = ;
  491. if res {
  492. t1 TYPE_SIZE take t2 TYPE_SIZE take == "type_compare: type are equal, but have different size" assert_msg ;
  493. }
  494. res ret ;
  495. }
  496. fun cctx_add_type 2 {
  497. $ctx
  498. $type
  499. @ctx 1 param = ;
  500. @type 0 param = ;
  501. # Add the type to the list
  502. $types
  503. $idx
  504. @types ctx CCTX_TYPES take = ;
  505. @idx types vector_size = ;
  506. types type vector_push_back ;
  507. # Check if the new type already matches with another
  508. $i
  509. @i 0 = ;
  510. while i idx < {
  511. if ctx i idx cctx_type_compare {
  512. # Found a match, remove the new one and return the old one
  513. types vector_pop_back type_destroy ;
  514. i ret ;
  515. }
  516. @i i 1 + = ;
  517. }
  518. idx ret ;
  519. }
  520. fun cctx_get_pointer_type 2 {
  521. $ctx
  522. $type_idx
  523. @ctx 1 param = ;
  524. @type_idx 0 param = ;
  525. $type
  526. @type type_init = ;
  527. type TYPE_KIND take_addr TYPE_KIND_POINTER = ;
  528. type TYPE_BASE take_addr type_idx = ;
  529. type TYPE_SIZE take_addr 4 = ;
  530. ctx type cctx_add_type ret ;
  531. }
  532. fun cctx_get_array_type 3 {
  533. $ctx
  534. $type_idx
  535. $length
  536. @ctx 2 param = ;
  537. @type_idx 1 param = ;
  538. @length 0 param = ;
  539. $base_type
  540. @base_type ctx CCTX_TYPES take type_idx vector_at = ;
  541. base_type TYPE_SIZE take 0xffffffff != "cctx_get_array_type: base type is invalid size" assert_msg ;
  542. $type
  543. @type type_init = ;
  544. type TYPE_KIND take_addr TYPE_KIND_ARRAY = ;
  545. type TYPE_BASE take_addr type_idx = ;
  546. type TYPE_LENGTH take_addr length = ;
  547. type TYPE_SIZE take_addr length base_type TYPE_SIZE take * = ;
  548. # -1 is used when length is not specified
  549. if length 0xffffffff == {
  550. type TYPE_SIZE take_addr 0xffffffff = ;
  551. }
  552. ctx type cctx_add_type ret ;
  553. }
  554. fun cctx_get_function_type 4 {
  555. $ctx
  556. $type_idx
  557. $args
  558. $ellipsis
  559. @ctx 3 param = ;
  560. @type_idx 2 param = ;
  561. @args 1 param = ;
  562. @ellipsis 0 param = ;
  563. $type
  564. @type type_init = ;
  565. type TYPE_KIND take_addr TYPE_KIND_FUNCTION = ;
  566. type TYPE_BASE take_addr type_idx = ;
  567. type TYPE_SIZE take_addr 0xffffffff = ;
  568. type TYPE_ARGS take vector_destroy ;
  569. type TYPE_ARGS take_addr args = ;
  570. type TYPE_ELLIPSIS take_addr ellipsis = ;
  571. ctx type cctx_add_type ret ;
  572. }
  573. ifun cctx_type_footprint 2
  574. fun cctx_construct_struct_type 3 {
  575. $ctx
  576. $type_idxs
  577. $names
  578. @ctx 2 param = ;
  579. @type_idxs 1 param = ;
  580. @names 0 param = ;
  581. type_idxs vector_size names vector_size == "cctx_construct_struct_type: inputs have different lengths" assert_msg ;
  582. $type
  583. @type type_init = ;
  584. type TYPE_KIND take_addr TYPE_KIND_STRUCT = ;
  585. type TYPE_FIELDS_TYPE_IDXS take vector_destroy ;
  586. type TYPE_FIELDS_NAMES take vector_destroy ;
  587. type TYPE_FIELDS_TYPE_IDXS take_addr type_idxs = ;
  588. type TYPE_FIELDS_NAMES take_addr names = ;
  589. # Compute offsets and size
  590. $off
  591. @off 0 = ;
  592. $i
  593. @i 0 = ;
  594. $offs
  595. @offs type TYPE_FIELDS_OFFS take = ;
  596. while i names vector_size < {
  597. offs off vector_push_back ;
  598. $fp
  599. @fp ctx type_idxs i vector_at cctx_type_footprint = ;
  600. @off off fp + = ;
  601. @i i 1 + = ;
  602. }
  603. type TYPE_SIZE take_addr off = ;
  604. type ret ;
  605. }
  606. fun cctx_get_incomplete_struct_type 1 {
  607. $ctx
  608. @ctx 0 param = ;
  609. $type
  610. @type type_init = ;
  611. type TYPE_KIND take_addr TYPE_KIND_STRUCT = ;
  612. type TYPE_SIZE take_addr 0xffffffff = ;
  613. ctx type cctx_add_type ret ;
  614. }
  615. fun cctx_construct_union_type 3 {
  616. $ctx
  617. $type_idxs
  618. $names
  619. @ctx 2 param = ;
  620. @type_idxs 1 param = ;
  621. @names 0 param = ;
  622. type_idxs vector_size names vector_size == "cctx_construct_union_type: inputs have different lengths" assert_msg ;
  623. $type
  624. @type type_init = ;
  625. type TYPE_KIND take_addr TYPE_KIND_UNION = ;
  626. type TYPE_FIELDS_TYPE_IDXS take vector_destroy ;
  627. type TYPE_FIELDS_NAMES take vector_destroy ;
  628. type TYPE_FIELDS_TYPE_IDXS take_addr type_idxs = ;
  629. type TYPE_FIELDS_NAMES take_addr names = ;
  630. # Compute size
  631. $size
  632. @size 0 = ;
  633. $i
  634. @i 0 = ;
  635. $offs
  636. @offs type TYPE_FIELDS_OFFS take = ;
  637. while i names vector_size < {
  638. offs 0 vector_push_back ;
  639. @size size ctx type_idxs i vector_at cctx_type_footprint max = ;
  640. @i i 1 + = ;
  641. }
  642. type TYPE_SIZE take_addr size = ;
  643. type ret ;
  644. }
  645. fun cctx_get_incomplete_union_type 1 {
  646. $ctx
  647. @ctx 0 param = ;
  648. $type
  649. @type type_init = ;
  650. type TYPE_KIND take_addr TYPE_KIND_UNION = ;
  651. type TYPE_SIZE take_addr 0xffffffff = ;
  652. ctx type cctx_add_type ret ;
  653. }
  654. const TYPE_VOID 0
  655. const TYPE_CHAR 1
  656. const TYPE_SCHAR 2
  657. const TYPE_UCHAR 3
  658. const TYPE_SHORT 4
  659. const TYPE_INT 5
  660. const TYPE_LONG 6
  661. const TYPE_USHORT 7
  662. const TYPE_UINT 8
  663. const TYPE_ULONG 9
  664. const TYPE_CHAR_ARRAY 10
  665. const TYPE_VOID_PTR 11
  666. fun is_integer_type 1 {
  667. $idx
  668. @idx 0 param = ;
  669. TYPE_CHAR idx <= TYPE_ULONG idx >= && ret ;
  670. }
  671. fun cctx_create_basic_type 3 {
  672. $ctx
  673. $idx
  674. $size
  675. @ctx 2 param = ;
  676. @idx 1 param = ;
  677. @size 0 param = ;
  678. $types
  679. @types ctx CCTX_TYPES take = ;
  680. idx types vector_size == "cctx_create_basic_type: error 1" assert_msg ;
  681. $type
  682. @type type_init = ;
  683. type TYPE_KIND take_addr TYPE_KIND_BASE = ;
  684. type TYPE_BASE take_addr idx = ;
  685. type TYPE_SIZE take_addr size = ;
  686. types type vector_push_back ;
  687. }
  688. fun cctx_create_basic_types 1 {
  689. $ctx
  690. @ctx 0 param = ;
  691. ctx TYPE_VOID 0xffffffff cctx_create_basic_type ;
  692. ctx TYPE_CHAR 1 cctx_create_basic_type ;
  693. ctx TYPE_SCHAR 1 cctx_create_basic_type ;
  694. ctx TYPE_UCHAR 1 cctx_create_basic_type ;
  695. ctx TYPE_SHORT 2 cctx_create_basic_type ;
  696. ctx TYPE_INT 4 cctx_create_basic_type ;
  697. ctx TYPE_LONG 8 cctx_create_basic_type ;
  698. ctx TYPE_USHORT 2 cctx_create_basic_type ;
  699. ctx TYPE_UINT 4 cctx_create_basic_type ;
  700. ctx TYPE_ULONG 8 cctx_create_basic_type ;
  701. ctx TYPE_CHAR 0xffffffff cctx_get_array_type TYPE_CHAR_ARRAY == "cctx_create_basic_types: error 1" assert_msg ;
  702. ctx TYPE_VOID cctx_get_pointer_type TYPE_VOID_PTR == "cctx_create_basic_types: error 2" assert_msg ;
  703. }
  704. fun cctx_has_global 2 {
  705. $ctx
  706. $name
  707. @ctx 1 param = ;
  708. @name 0 param = ;
  709. $globals
  710. @globals ctx CCTX_GLOBALS take = ;
  711. globals name map_has ret ;
  712. }
  713. fun cctx_get_global 2 {
  714. $ctx
  715. $name
  716. @ctx 1 param = ;
  717. @name 0 param = ;
  718. $globals
  719. @globals ctx CCTX_GLOBALS take = ;
  720. globals name map_has "cctx_get_global: global does not exist" assert_msg ;
  721. $global
  722. @global globals name map_at = ;
  723. global ret ;
  724. }
  725. fun cctx_add_global 4 {
  726. $ctx
  727. $name
  728. $loc
  729. $type_idx
  730. @ctx 3 param = ;
  731. @name 2 param = ;
  732. @loc 1 param = ;
  733. @type_idx 0 param = ;
  734. $globals
  735. @globals ctx CCTX_GLOBALS take = ;
  736. $present
  737. @present globals name map_has = ;
  738. $global
  739. if present {
  740. @global globals name map_at = ;
  741. ctx global GLOBAL_TYPE_IDX take type_idx cctx_type_compare "cctx_add_global: types do not match" assert_msg ;
  742. } else {
  743. ctx CCTX_STAGE take 0 == "cctx_add_global: error 1" assert_msg ;
  744. @global global_init = ;
  745. global GLOBAL_TYPE_IDX take_addr type_idx = ;
  746. global GLOBAL_LOC take_addr loc = ;
  747. globals name global map_set ;
  748. }
  749. if ctx CCTX_STAGE take 0 == {
  750. global GLOBAL_LOC take_addr 0xffffffff = ;
  751. } else {
  752. present "cctx_add_global: error 2" assert_msg ;
  753. }
  754. if ctx CCTX_STAGE take 1 == {
  755. if loc 0xffffffff != {
  756. global GLOBAL_LOC take 0xffffffff == "cctx_add_global: global is defined more than once" assert_msg ;
  757. global GLOBAL_LOC take_addr loc = ;
  758. }
  759. }
  760. if ctx CCTX_STAGE take 2 == {
  761. if loc 0xffffffff != {
  762. global GLOBAL_LOC take loc == "cctx_add_global: error 3" assert_msg ;
  763. }
  764. }
  765. }
  766. fun cctx_emit 2 {
  767. $ctx
  768. $byte
  769. @ctx 1 param = ;
  770. @byte 0 param = ;
  771. if ctx CCTX_STAGE take 2 == {
  772. ctx CCTX_CURRENT_LOC take byte =c ;
  773. }
  774. ctx CCTX_CURRENT_LOC take_addr ctx CCTX_CURRENT_LOC take 1 + = ;
  775. }
  776. fun cctx_emit16 2 {
  777. $ctx
  778. $word
  779. @ctx 1 param = ;
  780. @word 0 param = ;
  781. ctx word cctx_emit ;
  782. ctx word 8 >> cctx_emit ;
  783. }
  784. fun cctx_emit32 2 {
  785. $ctx
  786. $dword
  787. @ctx 1 param = ;
  788. @dword 0 param = ;
  789. ctx dword cctx_emit16 ;
  790. ctx dword 16 >> cctx_emit16 ;
  791. }
  792. fun cctx_emit_zeros 2 {
  793. $ctx
  794. $num
  795. @ctx 1 param = ;
  796. @num 0 param = ;
  797. $i
  798. @i 0 = ;
  799. while i num < {
  800. ctx 0 cctx_emit ;
  801. @i i 1 + = ;
  802. }
  803. }
  804. ifun cctx_gen_label 3
  805. ifun cctx_fix_label 4
  806. ifun cctx_gen_jump 3
  807. ifun cctx_gen_label_jump 5
  808. fun cctx_is_eof 1 {
  809. $ctx
  810. @ctx 0 param = ;
  811. ctx CCTX_TOKENS_POS take ctx CCTX_TOKENS take vector_size == ret ;
  812. }
  813. fun cctx_get_token 1 {
  814. $ctx
  815. @ctx 0 param = ;
  816. if ctx CCTX_TOKENS_POS take ctx CCTX_TOKENS take vector_size == {
  817. 0 ret ;
  818. } else {
  819. $tok
  820. @tok ctx CCTX_TOKENS take ctx CCTX_TOKENS_POS take vector_at = ;
  821. ctx CCTX_TOKENS_POS take_addr ctx CCTX_TOKENS_POS take 1 + = ;
  822. if ctx CCTX_VERBOSE take {
  823. " " 1 platform_log ;
  824. tok 1 platform_log ;
  825. }
  826. tok ret ;
  827. }
  828. }
  829. fun cctx_give_back_token 1 {
  830. $ctx
  831. @ctx 0 param = ;
  832. if ctx CCTX_VERBOSE take {
  833. " <gb>" 1 platform_log ;
  834. }
  835. ctx CCTX_TOKENS_POS take 0 > "cctx_give_back_token: error 1" assert_msg ;
  836. ctx CCTX_TOKENS_POS take_addr ctx CCTX_TOKENS_POS take 1 - = ;
  837. }
  838. fun cctx_save_token_pos 1 {
  839. $ctx
  840. @ctx 0 param = ;
  841. ctx CCTX_TOKENS_POS take ret ;
  842. }
  843. fun cctx_restore_token_pos 2 {
  844. $ctx
  845. $pos
  846. @ctx 1 param = ;
  847. @pos 0 param = ;
  848. ctx CCTX_TOKENS_POS take_addr pos = ;
  849. }
  850. fun cctx_get_token_or_fail 1 {
  851. $ctx
  852. @ctx 0 param = ;
  853. $tok
  854. @tok ctx cctx_get_token = ;
  855. tok 0 != "cctx_get_token_or_fail: unexpected end-of-file" assert_msg ;
  856. tok ret ;
  857. }
  858. fun cctx_go_to_matching 3 {
  859. $ctx
  860. $open
  861. $close
  862. @ctx 2 param = ;
  863. @open 1 param = ;
  864. @close 0 param = ;
  865. $level
  866. @level 1 = ;
  867. while level 0 > {
  868. $tok
  869. @tok ctx cctx_get_token_or_fail = ;
  870. if tok open strcmp 0 == {
  871. @level level 1 + = ;
  872. }
  873. if tok close strcmp 0 == {
  874. @level level 1 - = ;
  875. }
  876. }
  877. }
  878. fun cctx_print_token_pos 1 {
  879. $ctx
  880. @ctx 0 param = ;
  881. "Token pos: " 1 platform_log ;
  882. ctx CCTX_TOKENS_POS take itoa 1 platform_log ;
  883. "\n" 1 platform_log ;
  884. }
  885. ifun cctx_parse_type 1
  886. ifun cctx_parse_declarator 5
  887. fun cctx_parse_struct 3 {
  888. $ctx
  889. $type_idxs_ptr
  890. $names_ptr
  891. @ctx 2 param = ;
  892. @type_idxs_ptr 1 param = ;
  893. @names_ptr 0 param = ;
  894. $type_idxs
  895. $names
  896. @type_idxs 4 vector_init = ;
  897. @names 4 vector_init = ;
  898. $cont
  899. @cont 1 = ;
  900. while cont {
  901. $tok
  902. @tok ctx cctx_get_token_or_fail = ;
  903. if tok "}" strcmp 0 == {
  904. @cont 0 = ;
  905. } else {
  906. ctx cctx_give_back_token ;
  907. $type_idx
  908. @type_idx ctx cctx_parse_type = ;
  909. type_idx 0xffffffff != "cctx_parse_struct: type expected" assert_msg ;
  910. $cont2
  911. @cont2 1 = ;
  912. while cont2 {
  913. $actual_type_idx
  914. $name
  915. ctx type_idx @actual_type_idx @name 0 cctx_parse_declarator "cctx_parse_struct: could not parse declarator" assert_msg ;
  916. type_idxs actual_type_idx vector_push_back ;
  917. names name strdup vector_push_back ;
  918. @tok ctx cctx_get_token_or_fail = ;
  919. if tok ":" strcmp 0 == {
  920. @tok ctx cctx_get_token_or_fail = ;
  921. @tok ctx cctx_get_token_or_fail = ;
  922. }
  923. if tok ";" strcmp 0 == {
  924. @cont2 0 = ;
  925. } else {
  926. tok "," strcmp 0 == "cctx_parse_struct: comma expected" assert_msg ;
  927. }
  928. }
  929. }
  930. }
  931. type_idxs_ptr type_idxs = ;
  932. names_ptr names = ;
  933. }
  934. ifun ast_eval_compile 2
  935. fun cctx_parse_ast1 2 {
  936. $ctx
  937. $term
  938. @ctx 1 param = ;
  939. @term 0 param = ;
  940. if ctx CCTX_VERBOSE take {
  941. " <pa>" 1 platform_log ;
  942. }
  943. # Bad hack to fix ast_parse interface
  944. ctx cctx_give_back_token ;
  945. ctx CCTX_TOKENS take ctx CCTX_TOKENS_POS take_addr term ast_parse ret ;
  946. }
  947. fun cctx_parse_ast2 3 {
  948. $ctx
  949. $term1
  950. $term2
  951. @ctx 2 param = ;
  952. @term1 1 param = ;
  953. @term2 0 param = ;
  954. if ctx CCTX_VERBOSE take {
  955. " <pa>" 1 platform_log ;
  956. }
  957. # Bad hack to fix ast_parse interface
  958. ctx cctx_give_back_token ;
  959. ctx CCTX_TOKENS take ctx CCTX_TOKENS_POS take_addr term1 term2 ast_parse3 ret ;
  960. }
  961. fun cctx_parse_ast3 4 {
  962. $ctx
  963. $term1
  964. $term2
  965. $term3
  966. @ctx 3 param = ;
  967. @term1 2 param = ;
  968. @term2 1 param = ;
  969. @term3 0 param = ;
  970. if ctx CCTX_VERBOSE take {
  971. " <pa>" 1 platform_log ;
  972. }
  973. # Bad hack to fix ast_parse interface
  974. ctx cctx_give_back_token ;
  975. ctx CCTX_TOKENS take ctx CCTX_TOKENS_POS take_addr term1 term2 term3 ast_parse4 ret ;
  976. }
  977. fun cctx_parse_enum 1 {
  978. $ctx
  979. @ctx 0 param = ;
  980. $enum_consts
  981. @enum_consts ctx CCTX_ENUM_CONSTS take = ;
  982. $cont
  983. @cont 1 = ;
  984. $val
  985. @val 0 = ;
  986. while cont {
  987. $tok
  988. @tok ctx cctx_get_token_or_fail = ;
  989. if tok "}" strcmp 0 == {
  990. @cont 0 = ;
  991. } else {
  992. $ident
  993. @ident tok = ;
  994. @tok ctx cctx_get_token_or_fail = ;
  995. if tok "=" strcmp 0 == {
  996. $ast
  997. @ast ctx "}" "," cctx_parse_ast2 = ;
  998. @val ctx ast ast_eval_compile = ;
  999. ast ast_destroy ;
  1000. @tok ctx cctx_get_token_or_fail = ;
  1001. }
  1002. enum_consts ident map_has ! "cctx_parse_enum: constant is already defined" assert_msg ;
  1003. enum_consts ident val map_set ;
  1004. @val val 1 + = ;
  1005. if tok "}" strcmp 0 == {
  1006. @cont 0 = ;
  1007. } else {
  1008. tok "," strcmp 0 == "cctx_parse_enum: comma expected" assert_msg ;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. fun cctx_parse_type 1 {
  1014. $ctx
  1015. @ctx 0 param = ;
  1016. $tok
  1017. @tok ctx cctx_get_token_or_fail = ;
  1018. # Ignore constness
  1019. if tok "const" strcmp 0 == {
  1020. @tok ctx cctx_get_token_or_fail = ;
  1021. }
  1022. if tok "void" strcmp 0 == { TYPE_VOID ret ; }
  1023. if tok "char" strcmp 0 == { TYPE_CHAR ret ; }
  1024. if tok "short" strcmp 0 == { TYPE_SHORT ret ; }
  1025. if tok "int" strcmp 0 == { TYPE_INT ret ; }
  1026. if tok "signed" strcmp 0 == {
  1027. @tok ctx cctx_get_token_or_fail = ;
  1028. if tok "char" strcmp 0 == { TYPE_SCHAR ret ; }
  1029. if tok "short" strcmp 0 == { TYPE_SHORT ret ; }
  1030. if tok "int" strcmp 0 == { TYPE_INT ret ; }
  1031. if tok "long" strcmp 0 == {
  1032. @tok ctx cctx_get_token_or_fail = ;
  1033. if tok "int" strcmp 0 == { TYPE_LONG ret ; }
  1034. if tok "long" strcmp 0 == {
  1035. @tok ctx cctx_get_token_or_fail = ;
  1036. if tok "int" strcmp 0 == { TYPE_LONG ret ; }
  1037. ctx cctx_give_back_token ;
  1038. TYPE_LONG ret ;
  1039. }
  1040. ctx cctx_give_back_token ;
  1041. TYPE_LONG ret ;
  1042. }
  1043. ctx cctx_give_back_token ;
  1044. TYPE_INT ret ;
  1045. }
  1046. if tok "unsigned" strcmp 0 == {
  1047. @tok ctx cctx_get_token_or_fail = ;
  1048. if tok "char" strcmp 0 == { TYPE_UCHAR ret ; }
  1049. if tok "short" strcmp 0 == { TYPE_USHORT ret ; }
  1050. if tok "int" strcmp 0 == { TYPE_UINT ret ; }
  1051. if tok "long" strcmp 0 == {
  1052. @tok ctx cctx_get_token_or_fail = ;
  1053. if tok "int" strcmp 0 == { TYPE_ULONG ret ; }
  1054. if tok "long" strcmp 0 == {
  1055. @tok ctx cctx_get_token_or_fail = ;
  1056. if tok "int" strcmp 0 == { TYPE_ULONG ret ; }
  1057. ctx cctx_give_back_token ;
  1058. TYPE_ULONG ret ;
  1059. }
  1060. ctx cctx_give_back_token ;
  1061. TYPE_ULONG ret ;
  1062. }
  1063. ctx cctx_give_back_token ;
  1064. TYPE_UINT ret ;
  1065. }
  1066. if tok "long" strcmp 0 == {
  1067. @tok ctx cctx_get_token_or_fail = ;
  1068. if tok "long" strcmp 0 == { TYPE_LONG ret ; }
  1069. ctx cctx_give_back_token ;
  1070. TYPE_LONG ret ;
  1071. }
  1072. if tok "struct" strcmp 0 == {
  1073. $tag
  1074. $type_idxs
  1075. $names
  1076. @tag 0 = ;
  1077. @type_idxs 0 = ;
  1078. @tok ctx cctx_get_token_or_fail = ;
  1079. $structs
  1080. @structs ctx CCTX_STRUCTS take = ;
  1081. $type_idx
  1082. if tok "{" strcmp 0 != {
  1083. @tag tok = ;
  1084. @tok ctx cctx_get_token_or_fail = ;
  1085. if structs tag map_has {
  1086. @type_idx structs tag map_at = ;
  1087. } else {
  1088. @type_idx ctx cctx_get_incomplete_struct_type = ;
  1089. structs tag type_idx map_set ;
  1090. }
  1091. } else {
  1092. @type_idx ctx cctx_get_incomplete_struct_type = ;
  1093. }
  1094. if tok "{" strcmp 0 == {
  1095. ctx @type_idxs @names cctx_parse_struct ;
  1096. $type
  1097. @type ctx type_idx cctx_get_type = ;
  1098. type TYPE_SIZE take 0xffffffff == "cctx_parse_type: cannot define a struct twice" assert_msg ;
  1099. $newtype
  1100. @newtype ctx type_idxs names cctx_construct_struct_type = ;
  1101. type type_destroy ;
  1102. ctx CCTX_TYPES take type_idx vector_at_addr newtype = ;
  1103. } else {
  1104. ctx cctx_give_back_token ;
  1105. tag 0 != "cctx_parse_type: struct without neither tag nor definition" assert_msg ;
  1106. }
  1107. type_idx ret ;
  1108. }
  1109. if tok "union" strcmp 0 == {
  1110. $tag
  1111. $type_idxs
  1112. $names
  1113. @tag 0 = ;
  1114. @type_idxs 0 = ;
  1115. @tok ctx cctx_get_token_or_fail = ;
  1116. $unions
  1117. @unions ctx CCTX_UNIONS take = ;
  1118. $type_idx
  1119. if tok "{" strcmp 0 != {
  1120. @tag tok = ;
  1121. @tok ctx cctx_get_token_or_fail = ;
  1122. if unions tag map_has {
  1123. @type_idx unions tag map_at = ;
  1124. } else {
  1125. @type_idx ctx cctx_get_incomplete_union_type = ;
  1126. unions tag type_idx map_set ;
  1127. }
  1128. } else {
  1129. @type_idx ctx cctx_get_incomplete_union_type = ;
  1130. }
  1131. if tok "{" strcmp 0 == {
  1132. ctx @type_idxs @names cctx_parse_struct ;
  1133. $type
  1134. @type ctx type_idx cctx_get_type = ;
  1135. type TYPE_SIZE take 0xffffffff == "cctx_parse_type: cannot define a union twice" assert_msg ;
  1136. $newtype
  1137. @newtype ctx type_idxs names cctx_construct_union_type = ;
  1138. type type_destroy ;
  1139. ctx CCTX_TYPES take type_idx vector_at_addr newtype = ;
  1140. } else {
  1141. ctx cctx_give_back_token ;
  1142. tag 0 != "cctx_parse_type: union without neither tag nor definition" assert_msg ;
  1143. }
  1144. type_idx ret ;
  1145. }
  1146. if tok "enum" strcmp 0 == {
  1147. $tag
  1148. @tok ctx cctx_get_token_or_fail = ;
  1149. if tok "{" strcmp 0 != {
  1150. @tag tok = ;
  1151. @tok ctx cctx_get_token_or_fail = ;
  1152. }
  1153. if tok "{" strcmp 0 == {
  1154. ctx cctx_parse_enum ;
  1155. } else {
  1156. ctx cctx_give_back_token ;
  1157. tag 0 != "cctx_parse_type: enum without neither tag nor definition" assert_msg ;
  1158. }
  1159. TYPE_UINT ret ;
  1160. }
  1161. $typenames
  1162. @typenames ctx CCTX_TYPENAMES take = ;
  1163. if typenames tok map_has {
  1164. $idx
  1165. @idx typenames tok map_at = ;
  1166. idx ret ;
  1167. } else {
  1168. ctx cctx_give_back_token ;
  1169. 0xffffffff ret ;
  1170. }
  1171. }
  1172. ifun cctx_parse_declarator 5
  1173. fun _cctx_parse_function_arguments 3 {
  1174. $ctx
  1175. $ret_arg_names
  1176. $ret_ellipsis
  1177. @ctx 2 param = ;
  1178. @ret_arg_names 1 param = ;
  1179. @ret_ellipsis 0 param = ;
  1180. ret_ellipsis 0 = ;
  1181. $args
  1182. @args 4 vector_init = ;
  1183. while 1 {
  1184. $type_idx
  1185. @type_idx ctx cctx_parse_type = ;
  1186. if type_idx 0xffffffff == {
  1187. $tok
  1188. @tok ctx cctx_get_token_or_fail = ;
  1189. if tok "..." strcmp 0 == {
  1190. ret_ellipsis 1 = ;
  1191. @tok ctx cctx_get_token_or_fail = ;
  1192. }
  1193. tok ")" strcmp 0 == "_cctx_parse_function_arguments: ) or type expected" assert_msg ;
  1194. args vector_size 0 == ret_ellipsis ** || "_cctx_parse_function_arguments: unexpected )" assert_msg ;
  1195. args ret ;
  1196. }
  1197. $name
  1198. $actual_type_idx
  1199. if ctx type_idx @actual_type_idx @name 0 cctx_parse_declarator ! {
  1200. @actual_type_idx type_idx = ;
  1201. }
  1202. args actual_type_idx vector_push_back ;
  1203. if ret_arg_names 0 != {
  1204. ret_arg_names name vector_push_back ;
  1205. }
  1206. $tok
  1207. @tok ctx cctx_get_token_or_fail = ;
  1208. if tok ")" strcmp 0 == {
  1209. args ret ;
  1210. }
  1211. tok "," strcmp 0 == "_cctx_parse_function_arguments: ) or , expected" assert_msg ;
  1212. }
  1213. }
  1214. fun _cctx_parse_declarator 5 {
  1215. $ctx
  1216. $type_idx
  1217. $ret_type_idx
  1218. $ret_name
  1219. $ret_arg_names
  1220. @ctx 4 param = ;
  1221. @type_idx 3 param = ;
  1222. @ret_type_idx 2 param = ;
  1223. @ret_name 1 param = ;
  1224. @ret_arg_names 0 param = ;
  1225. #"_cctx_parse_declarator: entering\n" 1 platform_log ;
  1226. #ctx cctx_print_token_pos ;
  1227. $tok
  1228. @tok ctx cctx_get_token_or_fail = ;
  1229. $processed
  1230. @processed 0 = ;
  1231. #"_cctx_parse_declarator: token is " 1 platform_log ;
  1232. #tok 1 platform_log ;
  1233. #"\n" 1 platform_log ;
  1234. # Parse pointer declaration
  1235. if tok "*" strcmp 0 == {
  1236. @type_idx ctx type_idx cctx_get_pointer_type = ;
  1237. if ctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator ! {
  1238. ret_type_idx type_idx = ;
  1239. }
  1240. @processed 1 = ;
  1241. }
  1242. # Parse function declaration and grouping parantheses
  1243. if tok "(" strcmp 0 == {
  1244. # Here the first problem is decide whether this is a function or
  1245. # grouping parenthesis; if immediately after there is a type or a
  1246. # closing parenthesis, we are in the first case; otherwise, we are
  1247. # in the second case.
  1248. $pos
  1249. @pos ctx cctx_save_token_pos = ;
  1250. $type
  1251. @type ctx cctx_parse_type = ;
  1252. ctx pos cctx_restore_token_pos ;
  1253. $is_funct
  1254. @is_funct 0 = ;
  1255. if type 0xffffffff != {
  1256. @is_funct 1 = ;
  1257. }
  1258. @tok ctx cctx_get_token_or_fail = ;
  1259. if tok ")" strcmp 0 == {
  1260. @is_funct 1 = ;
  1261. }
  1262. # Restore the content of tok, so that the program does not get
  1263. # captured in later branches
  1264. @tok "(" = ;
  1265. ctx cctx_give_back_token ;
  1266. if is_funct {
  1267. # Function parenthesis
  1268. $args
  1269. $ellipsis
  1270. @args ctx ret_arg_names @ellipsis _cctx_parse_function_arguments = ;
  1271. if ctx type_idx ret_type_idx ret_name 0 _cctx_parse_declarator {
  1272. @type_idx ret_type_idx ** = ;
  1273. }
  1274. ret_type_idx ctx type_idx args ellipsis cctx_get_function_type = ;
  1275. } else {
  1276. # Grouping parenthesis
  1277. $inside_pos
  1278. $outside_pos
  1279. $end_pos
  1280. @inside_pos ctx cctx_save_token_pos = ;
  1281. ctx "(" ")" cctx_go_to_matching ;
  1282. @outside_pos ctx cctx_save_token_pos = ;
  1283. if ctx type_idx ret_type_idx ret_name 0 _cctx_parse_declarator {
  1284. @type_idx ret_type_idx ** = ;
  1285. }
  1286. @end_pos ctx cctx_save_token_pos = ;
  1287. ctx inside_pos cctx_restore_token_pos ;
  1288. ctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator "_cctx_parse_declarator: invalid syntax 1" assert_msg ;
  1289. @tok ctx cctx_get_token_or_fail = ;
  1290. tok ")" strcmp 0 == "_cctx_parse_declarator: error 1" assert_msg ;
  1291. outside_pos ctx cctx_save_token_pos == "_cctx_parse_declarator: invalid syntax 2" assert_msg ;
  1292. ctx end_pos cctx_restore_token_pos ;
  1293. }
  1294. @processed 1 = ;
  1295. }
  1296. # Parse array declaration
  1297. if tok "[" strcmp 0 == {
  1298. @tok ctx cctx_get_token_or_fail = ;
  1299. $length
  1300. if tok "]" strcmp 0 == {
  1301. @length 0xffffffff = ;
  1302. } else {
  1303. ctx cctx_give_back_token ;
  1304. $ast
  1305. @ast ctx "]" cctx_parse_ast1 = ;
  1306. @length ctx ast ast_eval_compile = ;
  1307. ast ast_destroy ;
  1308. @tok ctx cctx_get_token_or_fail = ;
  1309. }
  1310. tok "]" strcmp 0 == "_cctx_parse_declarator: expected ] after array subscript" assert_msg ;
  1311. if ctx type_idx ret_type_idx ret_name 0 _cctx_parse_declarator {
  1312. @type_idx ret_type_idx ** = ;
  1313. }
  1314. ret_type_idx ctx type_idx length cctx_get_array_type = ;
  1315. @processed 1 = ;
  1316. }
  1317. # Parse the actual declarator identifier
  1318. if tok is_valid_identifier {
  1319. if ctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator ! {
  1320. ret_type_idx type_idx = ;
  1321. }
  1322. ret_name ** 0 == "_cctx_parse_declarator: more than one identifier found" assert_msg ;
  1323. ret_name tok = ;
  1324. @processed 1 = ;
  1325. }
  1326. if processed ! {
  1327. ctx cctx_give_back_token ;
  1328. #"_cctx_parse_declarator: failed\n" 1 platform_log ;
  1329. 0 ret ;
  1330. }
  1331. #"_cctx_parse_declarator: success\n" 1 platform_log ;
  1332. 1 ret ;
  1333. }
  1334. fun cctx_parse_declarator 5 {
  1335. $ctx
  1336. $type_idx
  1337. $ret_type_idx
  1338. $ret_name
  1339. $ret_arg_names
  1340. @ctx 4 param = ;
  1341. @type_idx 3 param = ;
  1342. @ret_type_idx 2 param = ;
  1343. @ret_name 1 param = ;
  1344. @ret_arg_names 0 param = ;
  1345. ret_name 0 = ;
  1346. ctx type_idx ret_type_idx ret_name ret_arg_names _cctx_parse_declarator ret ;
  1347. }
  1348. fun cctx_type_footprint 2 {
  1349. $ctx
  1350. $type_idx
  1351. @ctx 1 param = ;
  1352. @type_idx 0 param = ;
  1353. $type
  1354. @type ctx CCTX_TYPES take type_idx vector_at = ;
  1355. $size
  1356. @size type TYPE_SIZE take = ;
  1357. size 0xffffffff != "cctx_type_footprint: type cannot be instantiated" assert_msg ;
  1358. size 1 - 3 | 1 + ret ;
  1359. }
  1360. fun cctx_type_size 2 {
  1361. $ctx
  1362. $type_idx
  1363. @ctx 1 param = ;
  1364. @type_idx 0 param = ;
  1365. $type
  1366. @type ctx CCTX_TYPES take type_idx vector_at = ;
  1367. $size
  1368. @size type TYPE_SIZE take = ;
  1369. size 0xffffffff != "cctx_type_size: type cannot be instantiated" assert_msg ;
  1370. size ret ;
  1371. }
  1372. const STACK_ELEM_NAME 0
  1373. const STACK_ELEM_TYPE_IDX 4
  1374. const STACK_ELEM_LOC 8
  1375. const SIZEOF_STACK_ELEM 12
  1376. fun stack_elem_init 0 {
  1377. $elem
  1378. @elem SIZEOF_STACK_ELEM malloc = ;
  1379. elem ret ;
  1380. }
  1381. fun stack_elem_destroy 1 {
  1382. $elem
  1383. @elem 0 param = ;
  1384. elem free ;
  1385. }
  1386. const LCTX_STACK 0
  1387. const LCTX_RETURN_TYPE_IDX 4
  1388. const LCTX_RETURN_LABEL 8
  1389. const LCTX_BREAK_LABEL 12
  1390. const LCTX_CONTINUE_LABEL 16
  1391. const SIZEOF_LCTX 20
  1392. fun lctx_init 0 {
  1393. $lctx
  1394. @lctx SIZEOF_LCTX malloc = ;
  1395. lctx LCTX_STACK take_addr 4 vector_init = ;
  1396. lctx ret ;
  1397. }
  1398. fun lctx_destroy 1 {
  1399. $lctx
  1400. @lctx 0 param = ;
  1401. $stack
  1402. @stack lctx LCTX_STACK take = ;
  1403. $i
  1404. @i 0 = ;
  1405. while i stack vector_size < {
  1406. stack i vector_at stack_elem_destroy ;
  1407. @i i 1 + = ;
  1408. }
  1409. lctx LCTX_STACK take vector_destroy ;
  1410. lctx free ;
  1411. }
  1412. fun lctx_stack_pos 1 {
  1413. $lctx
  1414. @lctx 0 param = ;
  1415. $stack
  1416. @stack lctx LCTX_STACK take = ;
  1417. stack stack vector_size 1 - vector_at STACK_ELEM_LOC take ret ;
  1418. }
  1419. fun lctx_gen_label 2 {
  1420. $lctx
  1421. $ctx
  1422. @lctx 1 param = ;
  1423. @ctx 0 param = ;
  1424. ctx 0xffffffff 0xffffffff cctx_gen_label ret ;
  1425. }
  1426. fun lctx_fix_label 3 {
  1427. $lctx
  1428. $ctx
  1429. $idx
  1430. @lctx 2 param = ;
  1431. @ctx 1 param = ;
  1432. @idx 0 param = ;
  1433. $loc
  1434. $pos
  1435. @loc ctx CCTX_CURRENT_LOC take = ;
  1436. if lctx 0 != {
  1437. @pos lctx lctx_stack_pos = ;
  1438. } else {
  1439. @pos 0 = ;
  1440. }
  1441. ctx idx loc pos cctx_fix_label ;
  1442. }
  1443. fun lctx_get_variable 2 {
  1444. $lctx
  1445. $name
  1446. @lctx 1 param = ;
  1447. @name 0 param = ;
  1448. # Begin scanning the stack from the end, so that inner variables
  1449. # mask outer ones
  1450. $stack
  1451. @stack lctx LCTX_STACK take = ;
  1452. $i
  1453. @i stack vector_size 1 - = ;
  1454. while i 0 >= {
  1455. $elem
  1456. @elem stack i vector_at = ;
  1457. if elem STACK_ELEM_NAME take name strcmp 0 == {
  1458. elem ret ;
  1459. }
  1460. @i i 1 - = ;
  1461. }
  1462. 0 ret ;
  1463. }
  1464. fun lctx_save_status 2 {
  1465. $lctx
  1466. $ctx
  1467. @lctx 1 param = ;
  1468. @ctx 0 param = ;
  1469. lctx LCTX_STACK take vector_size ret ;
  1470. }
  1471. fun lctx_restore_status 3 {
  1472. $lctx
  1473. $ctx
  1474. $status
  1475. @lctx 2 param = ;
  1476. @ctx 1 param = ;
  1477. @status 0 param = ;
  1478. $current_pos
  1479. @current_pos lctx lctx_stack_pos = ;
  1480. $stack
  1481. @stack lctx LCTX_STACK take = ;
  1482. status stack vector_size <= "lctx_restore_status: error 1" assert_msg ;
  1483. $new_pos
  1484. @new_pos stack status 1 - vector_at STACK_ELEM_LOC take = ;
  1485. $rewind
  1486. @rewind new_pos current_pos - = ;
  1487. rewind 0 >= "lctx_restore_status: error 2" assert_msg ;
  1488. # add esp, rewind
  1489. ctx 0x81 cctx_emit ;
  1490. ctx 0xc4 cctx_emit ;
  1491. ctx rewind cctx_emit32 ;
  1492. # Drop enough stack elements in excess
  1493. while stack vector_size status > {
  1494. $elem
  1495. @elem stack vector_pop_back = ;
  1496. elem stack_elem_destroy ;
  1497. }
  1498. }
  1499. fun lctx_push_var 3 {
  1500. $lctx
  1501. $ctx
  1502. $type_idx
  1503. $name
  1504. @lctx 3 param = ;
  1505. @ctx 2 param = ;
  1506. @type_idx 1 param = ;
  1507. @name 0 param = ;
  1508. $footprint
  1509. @footprint ctx type_idx cctx_type_footprint = ;
  1510. $new_pos
  1511. @new_pos lctx lctx_stack_pos footprint - = ;
  1512. $elem
  1513. @elem stack_elem_init = ;
  1514. elem STACK_ELEM_NAME take_addr name = ;
  1515. elem STACK_ELEM_TYPE_IDX take_addr type_idx = ;
  1516. elem STACK_ELEM_LOC take_addr new_pos = ;
  1517. lctx LCTX_STACK take elem vector_push_back ;
  1518. # sub esp, footprint
  1519. ctx 0x81 cctx_emit ;
  1520. ctx 0xec cctx_emit ;
  1521. ctx footprint cctx_emit32 ;
  1522. }
  1523. fun lctx_prime_stack 3 {
  1524. $lctx
  1525. $ctx
  1526. $type_idx
  1527. $arg_names
  1528. @lctx 3 param = ;
  1529. @ctx 2 param = ;
  1530. @type_idx 1 param = ;
  1531. @arg_names 0 param = ;
  1532. $type
  1533. @type ctx type_idx cctx_get_type = ;
  1534. type TYPE_KIND take TYPE_KIND_FUNCTION == "lctx_prime_stack: type is not a function" assert_msg ;
  1535. $args
  1536. @args type TYPE_ARGS take = ;
  1537. args vector_size arg_names vector_size == "lctx_prime_stack: error 1" assert_msg ;
  1538. $stack
  1539. @stack lctx LCTX_STACK take = ;
  1540. $i
  1541. @i 0 = ;
  1542. $total_footprint
  1543. @total_footprint 0 = ;
  1544. while i args vector_size < {
  1545. @total_footprint total_footprint ctx args i vector_at cctx_type_footprint + = ;
  1546. @i i 1 + = ;
  1547. }
  1548. $loc
  1549. @loc total_footprint 8 + = ;
  1550. @i args vector_size 1 - = ;
  1551. while i 0 >= {
  1552. $this_type_idx
  1553. @this_type_idx args i vector_at = ;
  1554. $name
  1555. @name arg_names i vector_at = ;
  1556. name 0 != "lctx_prime_stack: name cannot be empty" assert_msg ;
  1557. @loc loc ctx this_type_idx cctx_type_footprint - = ;
  1558. $elem
  1559. @elem stack_elem_init = ;
  1560. elem STACK_ELEM_NAME take_addr name = ;
  1561. elem STACK_ELEM_TYPE_IDX take_addr this_type_idx = ;
  1562. elem STACK_ELEM_LOC take_addr loc = ;
  1563. stack elem vector_push_back ;
  1564. @i i 1 - = ;
  1565. }
  1566. loc 8 == "lctx_prime_stack: error 2" assert_msg ;
  1567. # Add a fictious element to mark the beginning of local variables
  1568. $elem
  1569. @elem stack_elem_init = ;
  1570. elem STACK_ELEM_NAME take_addr "" = ;
  1571. elem STACK_ELEM_TYPE_IDX take_addr 0 = ;
  1572. elem STACK_ELEM_LOC take_addr 0 = ;
  1573. stack elem vector_push_back ;
  1574. }
  1575. fun lctx_gen_prologue 2 {
  1576. $lctx
  1577. $ctx
  1578. @lctx 1 param = ;
  1579. @ctx 0 param = ;
  1580. # push ebp; mov ebp, esp
  1581. ctx 0x55 cctx_emit ;
  1582. ctx 0x89 cctx_emit ;
  1583. ctx 0xe5 cctx_emit ;
  1584. }
  1585. fun lctx_gen_epilogue 2 {
  1586. $lctx
  1587. $ctx
  1588. @lctx 1 param = ;
  1589. @ctx 0 param = ;
  1590. # pop ebp; ret
  1591. ctx 0x5d cctx_emit ;
  1592. ctx 0xc3 cctx_emit ;
  1593. }
  1594. fun cctx_write_label 2 {
  1595. $ctx
  1596. $idx
  1597. @ctx 1 param = ;
  1598. @idx 0 param = ;
  1599. $buf
  1600. @buf ctx CCTX_LABEL_BUF take = ;
  1601. buf '.' =c ;
  1602. buf 1 + 'L' =c ;
  1603. idx itoa buf 2 + strcpy ;
  1604. buf ret ;
  1605. }
  1606. fun cctx_gen_label 3 {
  1607. $ctx
  1608. $loc
  1609. $pos
  1610. @ctx 2 param = ;
  1611. @loc 1 param = ;
  1612. @pos 0 param = ;
  1613. $label_pos
  1614. @label_pos ctx CCTX_LABEL_POS take = ;
  1615. $idx
  1616. @idx ctx CCTX_LABEL_NUM take = ;
  1617. $name
  1618. @name ctx idx cctx_write_label = ;
  1619. if ctx CCTX_STAGE take 0 == {
  1620. idx label_pos vector_size == "cctx_gen_label: error 2" assert_msg ;
  1621. label_pos pos vector_push_back ;
  1622. ctx name loc TYPE_VOID cctx_add_global ;
  1623. } else {
  1624. idx label_pos vector_size < "cctx_gen_label: error 1" assert_msg ;
  1625. if pos 0xffffffff != {
  1626. label_pos idx vector_at pos == "cctx_gen_label: error 3" assert_msg ;
  1627. }
  1628. ctx name loc TYPE_VOID cctx_add_global ;
  1629. }
  1630. ctx CCTX_LABEL_NUM take_addr idx 1 + = ;
  1631. idx ret ;
  1632. }
  1633. fun cctx_fix_label 4 {
  1634. $ctx
  1635. $idx
  1636. $loc
  1637. $pos
  1638. @ctx 3 param = ;
  1639. @idx 2 param = ;
  1640. @loc 1 param = ;
  1641. @pos 0 param = ;
  1642. $label_pos
  1643. @label_pos ctx CCTX_LABEL_POS take = ;
  1644. $name
  1645. @name ctx idx cctx_write_label = ;
  1646. ctx name loc TYPE_VOID cctx_add_global ;
  1647. if ctx CCTX_STAGE take 0 == {
  1648. label_pos idx vector_at_addr pos = ;
  1649. } else {
  1650. label_pos idx vector_at pos == "cctx_fix_label: error 1" assert_msg ;
  1651. }
  1652. }
  1653. const JUMP_TYPE_JMP 0
  1654. const JUMP_TYPE_CALL 1
  1655. const JUMP_TYPE_JZ 2
  1656. const JUMP_TYPE_JNZ 3
  1657. fun cctx_gen_jump 3 {
  1658. $ctx
  1659. $name
  1660. $type
  1661. @ctx 2 param = ;
  1662. @name 1 param = ;
  1663. @type 0 param = ;
  1664. if type JUMP_TYPE_JMP == {
  1665. # jmp rel
  1666. ctx 0xe9 cctx_emit ;
  1667. } else {
  1668. if type JUMP_TYPE_CALL == {
  1669. # call rel
  1670. ctx 0xe8 cctx_emit ;
  1671. } else {
  1672. if type JUMP_TYPE_JZ == {
  1673. # jz rel
  1674. ctx 0x0f cctx_emit ;
  1675. ctx 0x84 cctx_emit ;
  1676. } else {
  1677. if type JUMP_TYPE_JNZ == {
  1678. # jnz rel
  1679. ctx 0x0f cctx_emit ;
  1680. ctx 0x85 cctx_emit ;
  1681. } else {
  1682. 0 "cctx_gen_dump: error 1" assert_msg ;
  1683. }
  1684. }
  1685. }
  1686. }
  1687. $global
  1688. @global ctx name cctx_get_global = ;
  1689. $target_loc
  1690. @target_loc global GLOBAL_LOC take = ;
  1691. $current_loc
  1692. @current_loc ctx CCTX_CURRENT_LOC take 4 + = ;
  1693. $rel
  1694. @rel target_loc current_loc - = ;
  1695. ctx rel cctx_emit32 ;
  1696. }
  1697. fun cctx_gen_label_jump 5 {
  1698. $ctx
  1699. $lctx
  1700. $idx
  1701. $type
  1702. $rewind
  1703. @ctx 4 param = ;
  1704. @lctx 3 param = ;
  1705. @idx 2 param = ;
  1706. @type 1 param = ;
  1707. @rewind 0 param = ;
  1708. $current_pos
  1709. if lctx 0 != {
  1710. @current_pos lctx lctx_stack_pos = ;
  1711. } else {
  1712. @current_pos 0 = ;
  1713. }
  1714. $new_pos
  1715. @new_pos ctx CCTX_LABEL_POS take idx vector_at = ;
  1716. $pos_diff
  1717. @pos_diff new_pos current_pos - = ;
  1718. $name
  1719. @name ctx idx cctx_write_label = ;
  1720. # add esp, pos_diff; cctx_gen_jump
  1721. if rewind {
  1722. ctx 0x81 cctx_emit ;
  1723. ctx 0xc4 cctx_emit ;
  1724. ctx pos_diff cctx_emit32 ;
  1725. }
  1726. ctx name type cctx_gen_jump ;
  1727. }
  1728. fun cctx_get_label_addr 3 {
  1729. $ctx
  1730. $lctx
  1731. $idx
  1732. @ctx 2 param = ;
  1733. @lctx 1 param = ;
  1734. @idx 0 param = ;
  1735. $name
  1736. @name ctx idx cctx_write_label = ;
  1737. $global
  1738. @global ctx name cctx_get_global = ;
  1739. global GLOBAL_LOC take ret ;
  1740. }
  1741. fun cctx_gen_string 3 {
  1742. $ctx
  1743. $lctx
  1744. $name
  1745. @ctx 2 param = ;
  1746. @lctx 1 param = ;
  1747. @name 0 param = ;
  1748. $str_label
  1749. $label
  1750. @str_label lctx ctx lctx_gen_label = ;
  1751. @label lctx ctx lctx_gen_label = ;
  1752. ctx lctx label JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
  1753. lctx ctx str_label lctx_fix_label ;
  1754. $from
  1755. @from name 1 + = ;
  1756. while from **c '\"' != {
  1757. @from 0 ctx escape_char ;
  1758. }
  1759. from 1 + **c 0 == "cctx_gen_string: illegal string literal" assert_msg ;
  1760. ctx 0 cctx_emit ;
  1761. lctx ctx label lctx_fix_label ;
  1762. str_label ret ;
  1763. }
  1764. fun ast_eval_compile 2 {
  1765. $ctx
  1766. $ast
  1767. @ctx 1 param = ;
  1768. @ast 0 param = ;
  1769. $name
  1770. @name ast AST_NAME take = ;
  1771. if ast AST_TYPE take 0 == {
  1772. # Operand
  1773. if name is_valid_identifier {
  1774. $enum_consts
  1775. @enum_consts ctx CCTX_ENUM_CONSTS take = ;
  1776. if enum_consts name map_has {
  1777. enum_consts name map_at ret ;
  1778. } else {
  1779. $global
  1780. @global ctx name cctx_get_global = ;
  1781. $loc
  1782. @loc global GLOBAL_LOC take = ;
  1783. $type_idx
  1784. @type_idx global GLOBAL_TYPE_IDX take = ;
  1785. $type
  1786. @type ctx type_idx cctx_get_type = ;
  1787. if type TYPE_KIND take TYPE_KIND_ARRAY == type TYPE_KIND take TYPE_KIND_FUNCTION == || {
  1788. loc ret ;
  1789. } else {
  1790. ctx type_idx cctx_type_footprint 4 == "ast_eval_compile: unsupported global" assert_msg ;
  1791. loc ** ret ;
  1792. }
  1793. }
  1794. } else {
  1795. if name **c '\"' == {
  1796. $str_label
  1797. @str_label ctx 0 name cctx_gen_string = ;
  1798. ctx 0 str_label cctx_get_label_addr ret ;
  1799. } else {
  1800. name atoi_c ret ;
  1801. }
  1802. }
  1803. } else {
  1804. # Operator
  1805. ast AST_TYPE take 1 == "ast_eval_compile: error 1" assert_msg ;
  1806. if name "&&" strcmp 0 == {
  1807. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile && ret ;
  1808. }
  1809. if name "||" strcmp 0 == {
  1810. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile || ret ;
  1811. }
  1812. if name "&" strcmp 0 == {
  1813. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile & ret ;
  1814. }
  1815. if name "|" strcmp 0 == {
  1816. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile | ret ;
  1817. }
  1818. if name "==" strcmp 0 == {
  1819. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile == ret ;
  1820. }
  1821. if name "!=" strcmp 0 == {
  1822. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile != ret ;
  1823. }
  1824. if name ">=" strcmp 0 == {
  1825. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile >= ret ;
  1826. }
  1827. if name "<=" strcmp 0 == {
  1828. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile <= ret ;
  1829. }
  1830. if name ">" strcmp 0 == {
  1831. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile > ret ;
  1832. }
  1833. if name "<" strcmp 0 == {
  1834. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile < ret ;
  1835. }
  1836. if name "+" strcmp 0 == {
  1837. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile + ret ;
  1838. }
  1839. if name "-" strcmp 0 == {
  1840. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile - ret ;
  1841. }
  1842. if name "*" strcmp 0 == {
  1843. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile * ret ;
  1844. }
  1845. if name "/" strcmp 0 == {
  1846. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile / ret ;
  1847. }
  1848. if name "%" strcmp 0 == {
  1849. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile % ret ;
  1850. }
  1851. if name "^" strcmp 0 == {
  1852. ctx ast AST_LEFT take ast_eval_compile ctx ast AST_RIGHT take ast_eval_compile ^ ret ;
  1853. }
  1854. if name "!_PRE" strcmp 0 == {
  1855. ctx ast AST_RIGHT take ast_eval_compile ! ret ;
  1856. }
  1857. if name "-_PRE" strcmp 0 == {
  1858. 0 ctx ast AST_RIGHT take ast_eval_compile - ret ;
  1859. }
  1860. "Please implement " 1 platform_log ;
  1861. name 1 platform_log ;
  1862. "\n" 1 platform_log ;
  1863. 0 "ast_eval_compile: unsupported operation" assert_msg ;
  1864. }
  1865. }
  1866. ifun ast_eval_type 3
  1867. fun promote_integer_type 1 {
  1868. $type
  1869. @type 0 param = ;
  1870. if type TYPE_CHAR ==
  1871. type TYPE_SCHAR == ||
  1872. type TYPE_UCHAR == ||
  1873. type TYPE_SHORT == ||
  1874. type TYPE_INT == ||
  1875. type TYPE_USHORT == || {
  1876. TYPE_INT ret ;
  1877. }
  1878. if type TYPE_UINT == {
  1879. TYPE_UINT ret ;
  1880. }
  1881. if type TYPE_LONG == {
  1882. TYPE_LONG ret ;
  1883. }
  1884. if type TYPE_ULONG == {
  1885. TYPE_ULONG ret ;
  1886. }
  1887. 0 "promote_integer_type: not an integer type" assert_msg ;
  1888. }
  1889. fun ast_arith_conv 3 {
  1890. $ast
  1891. $ctx
  1892. $lctx
  1893. @ast 2 param = ;
  1894. @ctx 1 param = ;
  1895. @lctx 0 param = ;
  1896. $type1
  1897. $type2
  1898. @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
  1899. @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
  1900. @type1 type1 promote_integer_type = ;
  1901. @type2 type2 promote_integer_type = ;
  1902. if type1 TYPE_UINT == type2 TYPE_UINT == || {
  1903. TYPE_UINT ret ;
  1904. } else {
  1905. TYPE_INT ret ;
  1906. }
  1907. }
  1908. fun ast_eval_type 3 {
  1909. $ast
  1910. $ctx
  1911. $lctx
  1912. @ast 2 param = ;
  1913. @ctx 1 param = ;
  1914. @lctx 0 param = ;
  1915. if ast AST_TYPE_IDX take 0xffffffff != {
  1916. ast AST_TYPE_IDX take ret ;
  1917. }
  1918. $name
  1919. @name ast AST_NAME take = ;
  1920. $type_idx
  1921. if ast AST_TYPE take 0 == {
  1922. # Operand
  1923. if name is_valid_identifier {
  1924. # Search among enum constants
  1925. $enum_consts
  1926. @enum_consts ctx CCTX_ENUM_CONSTS take = ;
  1927. if enum_consts name map_has {
  1928. TYPE_UINT ret ;
  1929. } else {
  1930. # Search in local stack and among globals
  1931. $elem
  1932. @elem lctx name lctx_get_variable = ;
  1933. if elem {
  1934. @type_idx elem STACK_ELEM_TYPE_IDX take = ;
  1935. } else {
  1936. $global
  1937. @global ctx name cctx_get_global = ;
  1938. @type_idx global GLOBAL_TYPE_IDX take = ;
  1939. }
  1940. }
  1941. } else {
  1942. if name **c '\"' == {
  1943. @type_idx TYPE_CHAR_ARRAY = ;
  1944. } else {
  1945. if name **c '\'' == {
  1946. @type_idx TYPE_INT = ;
  1947. } else {
  1948. @type_idx TYPE_INT = ;
  1949. }
  1950. }
  1951. }
  1952. } else {
  1953. # Operator
  1954. $processed
  1955. @processed 0 = ;
  1956. $sum
  1957. $subtract
  1958. @sum name "+" strcmp 0 == = ;
  1959. @subtract name "-" strcmp 0 == = ;
  1960. if sum subtract || {
  1961. $left_idx
  1962. $right_idx
  1963. @left_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  1964. @right_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
  1965. $left_ptr
  1966. $right_ptr
  1967. @left_ptr ctx left_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
  1968. @right_ptr ctx right_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
  1969. $left_size
  1970. $right_size
  1971. if left_ptr {
  1972. @left_size ctx ctx left_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
  1973. }
  1974. if right_ptr {
  1975. @right_size ctx ctx right_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
  1976. }
  1977. if sum {
  1978. left_ptr right_ptr && ! "ast_eval_type: cannot take sum of two pointers" assert_msg ;
  1979. if left_ptr {
  1980. @type_idx left_idx = ;
  1981. @processed 1 = ;
  1982. }
  1983. if right_ptr {
  1984. @type_idx right_idx = ;
  1985. @processed 1 = ;
  1986. }
  1987. } else {
  1988. left_ptr ! right_ptr && ! "ast_eval_type: cannot take different of a non-pointer and a pointer" assert_msg ;
  1989. if left_ptr {
  1990. if right_ptr {
  1991. left_size right_size == "ast_eval_type: cannot take difference of pointers to types of different size" assert_msg ;
  1992. @type_idx TYPE_INT = ;
  1993. } else {
  1994. @type_idx left_idx = ;
  1995. }
  1996. @processed 1 = ;
  1997. }
  1998. }
  1999. }
  2000. if name "*" strcmp 0 ==
  2001. name "/" strcmp 0 == ||
  2002. name "%" strcmp 0 == ||
  2003. name "+" strcmp 0 == ||
  2004. name "-" strcmp 0 == ||
  2005. name "&" strcmp 0 == ||
  2006. name "^" strcmp 0 == ||
  2007. name "|" strcmp 0 == ||
  2008. processed ! && {
  2009. @type_idx ast ctx lctx ast_arith_conv = ;
  2010. @processed 1 = ;
  2011. }
  2012. if name "<<" strcmp 0 ==
  2013. name ">>" strcmp 0 == ||
  2014. processed ! && {
  2015. $type1
  2016. $type2
  2017. @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
  2018. @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2019. @type1 type1 promote_integer_type = ;
  2020. @type2 type2 promote_integer_type = ;
  2021. @type_idx type1 = ;
  2022. @processed 1 = ;
  2023. }
  2024. if name "+_PRE" strcmp 0 ==
  2025. name "-_PRE" strcmp 0 == ||
  2026. name "~_PRE" strcmp 0 == ||
  2027. processed ! && {
  2028. @type_idx ast AST_RIGHT take ctx lctx ast_eval_type promote_integer_type = ;
  2029. @processed 1 = ;
  2030. }
  2031. if name "<" strcmp 0 ==
  2032. name ">" strcmp 0 == ||
  2033. name "<=" strcmp 0 == ||
  2034. name ">=" strcmp 0 == ||
  2035. name "==" strcmp 0 == ||
  2036. name "!=" strcmp 0 == ||
  2037. name "&&" strcmp 0 == ||
  2038. name "||" strcmp 0 == ||
  2039. processed ! && {
  2040. $type1
  2041. $type2
  2042. @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
  2043. @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2044. 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 ;
  2045. 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 ;
  2046. @type_idx TYPE_INT = ;
  2047. @processed 1 = ;
  2048. }
  2049. if name "!_PRE" strcmp 0 ==
  2050. processed ! && {
  2051. $type
  2052. @type ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2053. 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 ;
  2054. @type_idx TYPE_INT = ;
  2055. @processed 1 = ;
  2056. }
  2057. if name "=" strcmp 0 == {
  2058. @type_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2059. @processed 1 = ;
  2060. }
  2061. if name "(" strcmp 0 == {
  2062. $fun_ptr_idx
  2063. $fun_ptr_type
  2064. $fun_idx
  2065. $fun_type
  2066. @fun_ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2067. @fun_ptr_type ctx fun_ptr_idx cctx_get_type = ;
  2068. fun_ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: left is not a pointer" assert_msg ;
  2069. @fun_idx fun_ptr_type TYPE_BASE take = ;
  2070. @fun_type ctx fun_idx cctx_get_type = ;
  2071. fun_type TYPE_KIND take TYPE_KIND_FUNCTION == "ast_eval_type: left is not a pointer to function" assert_msg ;
  2072. @type_idx fun_type TYPE_BASE take = ;
  2073. @processed 1 = ;
  2074. }
  2075. if name "*_PRE" strcmp 0 == {
  2076. $ptr_idx
  2077. $ptr_type
  2078. @ptr_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2079. @ptr_type ctx ptr_idx cctx_get_type = ;
  2080. ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: right is not a pointer" assert_msg ;
  2081. @type_idx ptr_type TYPE_BASE take = ;
  2082. @processed 1 = ;
  2083. }
  2084. if name "&_PRE" strcmp 0 == {
  2085. $orig_idx
  2086. @orig_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2087. @type_idx ctx orig_idx cctx_get_pointer_type = ;
  2088. @processed 1 = ;
  2089. }
  2090. if name "." strcmp 0 == {
  2091. $struct_idx
  2092. $struct_type
  2093. @struct_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2094. @struct_type ctx struct_idx cctx_get_type = ;
  2095. ast AST_RIGHT take AST_TYPE take 0 == "ast_eval_type: right is not a plain name" assert_msg ;
  2096. $name
  2097. @name ast AST_RIGHT take AST_NAME take = ;
  2098. $field
  2099. @field struct_type name type_get_idx = ;
  2100. field 0xffffffff != "ast_eval_type: specified field does not exist" assert_msg ;
  2101. @type_idx struct_type TYPE_FIELDS_TYPE_IDXS take field vector_at = ;
  2102. @processed 1 = ;
  2103. }
  2104. if name "->" strcmp 0 == {
  2105. $ptr_idx
  2106. $ptr_type
  2107. $struct_idx
  2108. $struct_type
  2109. @ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2110. @ptr_type ctx ptr_idx cctx_get_type = ;
  2111. ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_eval_type: right is not a pointer" assert_msg ;
  2112. @struct_idx ptr_type TYPE_BASE take = ;
  2113. @struct_type ctx struct_idx cctx_get_type = ;
  2114. ast AST_RIGHT take AST_TYPE take 0 == "ast_eval_type: right is not a plain name" assert_msg ;
  2115. $name
  2116. @name ast AST_RIGHT take AST_NAME take = ;
  2117. $field
  2118. @field struct_type name type_get_idx = ;
  2119. field 0xffffffff != "ast_eval_type: specified field does not exist" assert_msg ;
  2120. @type_idx struct_type TYPE_FIELDS_TYPE_IDXS take field vector_at = ;
  2121. @processed 1 = ;
  2122. }
  2123. processed "ast_eval_type: not implemented" assert_msg ;
  2124. }
  2125. # Process decaying
  2126. $type
  2127. @type ctx type_idx cctx_get_type = ;
  2128. $orig_type_idx
  2129. @orig_type_idx 0xffffffff = ;
  2130. if type TYPE_KIND take TYPE_KIND_ARRAY == {
  2131. @orig_type_idx type_idx = ;
  2132. $base_idx
  2133. @base_idx type TYPE_BASE take = ;
  2134. @type_idx ctx base_idx cctx_get_pointer_type = ;
  2135. }
  2136. if type TYPE_KIND take TYPE_KIND_FUNCTION == {
  2137. @orig_type_idx type_idx = ;
  2138. @type_idx ctx type_idx cctx_get_pointer_type = ;
  2139. }
  2140. @type ctx type_idx cctx_get_type = ;
  2141. # Sanity check
  2142. type_idx TYPE_VOID == type TYPE_SIZE take 0xffffffff != || "ast_eval_type: invalid expression type" assert_msg ;
  2143. ast AST_TYPE_IDX take_addr type_idx = ;
  2144. ast AST_ORIG_TYPE_IDX take_addr orig_type_idx = ;
  2145. type_idx ret ;
  2146. }
  2147. ifun ast_push_value 3
  2148. fun ast_push_addr 3 {
  2149. $ast
  2150. $ctx
  2151. $lctx
  2152. @ast 2 param = ;
  2153. @ctx 1 param = ;
  2154. @lctx 0 param = ;
  2155. $name
  2156. @name ast AST_NAME take = ;
  2157. $type_idx
  2158. @type_idx ast ctx lctx ast_eval_type = ;
  2159. if ast AST_TYPE take 0 == {
  2160. # Operand
  2161. if name is_valid_identifier {
  2162. # Search in local stack and among globals
  2163. $elem
  2164. @elem lctx name lctx_get_variable = ;
  2165. if elem {
  2166. # lea eax, [ebp+loc]; push eax
  2167. ctx 0x8d cctx_emit ;
  2168. ctx 0x85 cctx_emit ;
  2169. ctx elem STACK_ELEM_LOC take cctx_emit32 ;
  2170. ctx 0x50 cctx_emit ;
  2171. } else {
  2172. $global
  2173. @global ctx name cctx_get_global = ;
  2174. # push loc
  2175. ctx 0x68 cctx_emit ;
  2176. ctx global GLOBAL_LOC take cctx_emit32 ;
  2177. }
  2178. } else {
  2179. if name **c '\"' == {
  2180. $str_label
  2181. @str_label ctx lctx name cctx_gen_string = ;
  2182. # push str_label
  2183. ctx 0x68 cctx_emit ;
  2184. ctx ctx lctx str_label cctx_get_label_addr cctx_emit32 ;
  2185. } else {
  2186. 0 "ast_push_addr: cannot take the address of an immediate" assert_msg ;
  2187. }
  2188. }
  2189. } else {
  2190. # Operator
  2191. $processed
  2192. @processed 0 = ;
  2193. if name "*_PRE" strcmp 0 == {
  2194. ast AST_RIGHT take ctx lctx ast_push_value ;
  2195. @processed 1 = ;
  2196. }
  2197. if name "." strcmp 0 == {
  2198. $struct_idx
  2199. $struct_type
  2200. @struct_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2201. @struct_type ctx struct_idx cctx_get_type = ;
  2202. ast AST_RIGHT take AST_TYPE take 0 == "ast_push_addr: right is not a plain name" assert_msg ;
  2203. $name
  2204. @name ast AST_RIGHT take AST_NAME take = ;
  2205. $field
  2206. @field struct_type name type_get_idx = ;
  2207. field 0xffffffff != "ast_push_addr: specified field does not exist" assert_msg ;
  2208. $off
  2209. @off struct_type TYPE_FIELDS_OFFS take field vector_at = ;
  2210. # ast_push_addr; pop eax; add eax, off; push eax
  2211. ast AST_LEFT take ctx lctx ast_push_addr ;
  2212. ctx 0x58 cctx_emit ;
  2213. ctx 0x05 cctx_emit ;
  2214. ctx off cctx_emit32 ;
  2215. ctx 0x50 cctx_emit ;
  2216. @processed 1 = ;
  2217. }
  2218. if name "->" strcmp 0 == {
  2219. $ptr_idx
  2220. $ptr_type
  2221. $struct_idx
  2222. $struct_type
  2223. @ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2224. @ptr_type ctx ptr_idx cctx_get_type = ;
  2225. ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_push_addr: right is not a pointer" assert_msg ;
  2226. @struct_idx ptr_type TYPE_BASE take = ;
  2227. @struct_type ctx struct_idx cctx_get_type = ;
  2228. ast AST_RIGHT take AST_TYPE take 0 == "ast_push_addr: right is not a plain name" assert_msg ;
  2229. $name
  2230. @name ast AST_RIGHT take AST_NAME take = ;
  2231. $field
  2232. @field struct_type name type_get_idx = ;
  2233. field 0xffffffff != "ast_push_addr: specified field does not exist" assert_msg ;
  2234. $off
  2235. @off struct_type TYPE_FIELDS_OFFS take field vector_at = ;
  2236. # ast_push_value; pop eax; add eax, off; push eax
  2237. ast AST_LEFT take ctx lctx ast_push_value ;
  2238. ctx 0x58 cctx_emit ;
  2239. ctx 0x05 cctx_emit ;
  2240. ctx off cctx_emit32 ;
  2241. ctx 0x50 cctx_emit ;
  2242. @processed 1 = ;
  2243. }
  2244. processed "ast_push_addr: not implemented" assert_msg ;
  2245. }
  2246. }
  2247. fun lctx_int_convert 4 {
  2248. $lctx
  2249. $ctx
  2250. $from_idx
  2251. $to_idx
  2252. @lctx 3 param = ;
  2253. @ctx 2 param = ;
  2254. @from_idx 1 param = ;
  2255. @to_idx 0 param = ;
  2256. # Treat pointers as integers
  2257. if ctx from_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == {
  2258. @from_idx TYPE_INT = ;
  2259. }
  2260. from_idx is_integer_type "lctx_int_convert: source is not an integer type" assert_msg ;
  2261. to_idx is_integer_type "lctx_int_convert: target is not an integer type" assert_msg ;
  2262. if ctx from_idx cctx_type_footprint 4 == {
  2263. # pop eax
  2264. ctx 0x58 cctx_emit ;
  2265. } else {
  2266. if ctx from_idx cctx_type_footprint 8 == {
  2267. # pop eax; pop edx
  2268. ctx 0x58 cctx_emit ;
  2269. ctx 0x5a cctx_emit ;
  2270. } else {
  2271. 0 "lctx_int_convert: error 1" assert_msg ;
  2272. }
  2273. }
  2274. if from_idx TYPE_CHAR == from_idx TYPE_SCHAR == || {
  2275. # movsx eax, al; cdq
  2276. ctx 0x0f cctx_emit ;
  2277. ctx 0xbe cctx_emit ;
  2278. ctx 0xc0 cctx_emit ;
  2279. ctx 0x99 cctx_emit ;
  2280. } else {
  2281. if from_idx TYPE_UCHAR == {
  2282. # movzx eax, al; xor edx, edx
  2283. ctx 0x0f cctx_emit ;
  2284. ctx 0xb6 cctx_emit ;
  2285. ctx 0xc0 cctx_emit ;
  2286. ctx 0x31 cctx_emit ;
  2287. ctx 0xd2 cctx_emit ;
  2288. } else {
  2289. if from_idx TYPE_SHORT == {
  2290. # movsx eax, ax; cdq
  2291. ctx 0x0f cctx_emit ;
  2292. ctx 0xbf cctx_emit ;
  2293. ctx 0xc0 cctx_emit ;
  2294. ctx 0x99 cctx_emit ;
  2295. } else {
  2296. if from_idx TYPE_USHORT == {
  2297. # movzx eax, ax; xor edx, edx
  2298. ctx 0x0f cctx_emit ;
  2299. ctx 0xb7 cctx_emit ;
  2300. ctx 0xc0 cctx_emit ;
  2301. ctx 0x31 cctx_emit ;
  2302. ctx 0xd2 cctx_emit ;
  2303. } else {
  2304. if from_idx TYPE_INT == {
  2305. # cdq
  2306. ctx 0x99 cctx_emit ;
  2307. } else {
  2308. if from_idx TYPE_UINT == {
  2309. # xor edx, edx
  2310. ctx 0x31 cctx_emit ;
  2311. ctx 0xd2 cctx_emit ;
  2312. } else {
  2313. from_idx TYPE_LONG == from_idx TYPE_ULONG == || "lctx_int_convert: error 2" assert_msg ;
  2314. }
  2315. }
  2316. }
  2317. }
  2318. }
  2319. }
  2320. if ctx to_idx cctx_type_footprint 4 == {
  2321. # push eax
  2322. ctx 0x50 cctx_emit ;
  2323. } else {
  2324. if ctx to_idx cctx_type_footprint 8 == {
  2325. # push edx; push eax
  2326. ctx 0x52 cctx_emit ;
  2327. ctx 0x50 cctx_emit ;
  2328. } else {
  2329. 0 "lctx_int_convert: error 3" assert_msg ;
  2330. }
  2331. }
  2332. }
  2333. fun lctx_convert_stack 4 {
  2334. $lctx
  2335. $ctx
  2336. $from_idx
  2337. $to_idx
  2338. @lctx 3 param = ;
  2339. @ctx 2 param = ;
  2340. @from_idx 1 param = ;
  2341. @to_idx 0 param = ;
  2342. if ctx from_idx to_idx cctx_type_compare {
  2343. ret ;
  2344. }
  2345. if from_idx is_integer_type to_idx is_integer_type && {
  2346. lctx ctx from_idx to_idx lctx_int_convert ;
  2347. ret ;
  2348. }
  2349. $from_type
  2350. $to_type
  2351. @from_type ctx from_idx cctx_get_type = ;
  2352. @to_type ctx to_idx cctx_get_type = ;
  2353. if from_type TYPE_KIND take TYPE_KIND_POINTER == to_type TYPE_KIND take TYPE_KIND_POINTER == && {
  2354. # Permit any implicit conversion between any two pointers of any type
  2355. ctx from_idx cctx_type_footprint 4 == "lctx_convert_stack: error 3" assert_msg ;
  2356. ctx to_idx cctx_type_footprint 4 == "lctx_convert_stack: error 4" assert_msg ;
  2357. ret ;
  2358. }
  2359. if from_idx is_integer_type to_type TYPE_KIND take TYPE_KIND_POINTER == && {
  2360. # Permit any implicit converstion of integers to pointers
  2361. ctx from_idx cctx_type_footprint 4 == "lctx_convert_stack: error 5" assert_msg ;
  2362. ctx to_idx cctx_type_footprint 4 == "lctx_convert_stack: error 6" assert_msg ;
  2363. ret ;
  2364. }
  2365. 0 "lctx_convert_stack: not implemented" assert_msg ;
  2366. }
  2367. fun ast_push_value_logic 3 {
  2368. $ast
  2369. $ctx
  2370. $lctx
  2371. @ast 2 param = ;
  2372. @ctx 1 param = ;
  2373. @lctx 0 param = ;
  2374. $name
  2375. @name ast AST_NAME take = ;
  2376. $and
  2377. @and name "&&" strcmp 0 == = ;
  2378. and name "||" strcmp 0 == ^ "ast_push_value_logic: not a binary logic operator" assert_msg ;
  2379. $type1
  2380. $type2
  2381. $type_idx
  2382. @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
  2383. @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2384. @type_idx ast ctx lctx ast_eval_type = ;
  2385. # Evaluate first operand
  2386. ast AST_LEFT take ctx lctx ast_push_value ;
  2387. # Try short-circuit evaluation
  2388. $end_lab
  2389. @end_lab lctx ctx lctx_gen_label = ;
  2390. # pop eax; test eax, eax; mov eax, ?; cctx_gen_label_jump
  2391. ctx 0x58 cctx_emit ;
  2392. ctx 0x85 cctx_emit ;
  2393. ctx 0xc0 cctx_emit ;
  2394. ctx 0xb8 cctx_emit ;
  2395. if and {
  2396. ctx 0 cctx_emit32 ;
  2397. ctx lctx end_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
  2398. } else {
  2399. ctx 1 cctx_emit32 ;
  2400. ctx lctx end_lab JUMP_TYPE_JNZ 0 cctx_gen_label_jump ;
  2401. }
  2402. # Evaluate second operand
  2403. ast AST_RIGHT take ctx lctx ast_push_value ;
  2404. # Reduce result to 0 or 1
  2405. # pop eax; test eax, eax; mov eax, 0; setnz al
  2406. ctx 0x58 cctx_emit ;
  2407. ctx 0x85 cctx_emit ;
  2408. ctx 0xc0 cctx_emit ;
  2409. ctx 0xb8 cctx_emit ;
  2410. ctx 0 cctx_emit32 ;
  2411. ctx 0x0f cctx_emit ;
  2412. ctx 0x95 cctx_emit ;
  2413. ctx 0xc0 cctx_emit ;
  2414. # Push result
  2415. lctx ctx end_lab lctx_fix_label ;
  2416. # push eax
  2417. ctx 0x50 cctx_emit ;
  2418. }
  2419. fun ast_push_value_arith 3 {
  2420. $ast
  2421. $ctx
  2422. $lctx
  2423. @ast 2 param = ;
  2424. @ctx 1 param = ;
  2425. @lctx 0 param = ;
  2426. $name
  2427. @name ast AST_NAME take = ;
  2428. $is_prefix
  2429. @is_prefix name "+_PRE" strcmp 0 ==
  2430. name "-_PRE" strcmp 0 == ||
  2431. name "~_PRE" strcmp 0 == ||
  2432. name "!_PRE" strcmp 0 == || = ;
  2433. $type1
  2434. $type2
  2435. $type_idx
  2436. $large_idx
  2437. if is_prefix ! {
  2438. @type1 ast AST_LEFT take ctx lctx ast_eval_type = ;
  2439. }
  2440. @type2 ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2441. @type_idx ast ctx lctx ast_eval_type = ;
  2442. if type_idx TYPE_LONG == type_idx TYPE_ULONG == || {
  2443. @large_idx type_idx = ;
  2444. } else {
  2445. @large_idx TYPE_LONG = ;
  2446. }
  2447. ctx large_idx cctx_type_footprint 8 == "ast_push_value_arith: error 1" assert_msg ;
  2448. # push ebx
  2449. ctx 0x53 cctx_emit ;
  2450. # Recursively evalute both operands
  2451. if is_prefix ! {
  2452. ast AST_LEFT take ctx lctx ast_push_value ;
  2453. lctx ctx type1 large_idx lctx_int_convert ;
  2454. }
  2455. ast AST_RIGHT take ctx lctx ast_push_value ;
  2456. lctx ctx type2 large_idx lctx_int_convert ;
  2457. # Pop right result and store in EBX:ECX
  2458. # pop ecx; pop ebx
  2459. ctx 0x59 cctx_emit ;
  2460. ctx 0x5b cctx_emit ;
  2461. # Pop left result and store in EDX:EAX
  2462. if is_prefix ! {
  2463. # pop eax; pop edx
  2464. ctx 0x58 cctx_emit ;
  2465. ctx 0x5a cctx_emit ;
  2466. }
  2467. # Invoke the operation specific operation
  2468. $processed
  2469. @processed 0 = ;
  2470. if name "+" strcmp 0 == {
  2471. # add eax, ecx
  2472. ctx 0x01 cctx_emit ;
  2473. ctx 0xc8 cctx_emit ;
  2474. @processed 1 = ;
  2475. }
  2476. if name "-" strcmp 0 == {
  2477. # sub eax, ecx
  2478. ctx 0x29 cctx_emit ;
  2479. ctx 0xc8 cctx_emit ;
  2480. @processed 1 = ;
  2481. }
  2482. if name "&" strcmp 0 == {
  2483. # and eax, ecx
  2484. ctx 0x21 cctx_emit ;
  2485. ctx 0xc8 cctx_emit ;
  2486. @processed 1 = ;
  2487. }
  2488. if name "|" strcmp 0 == {
  2489. # or eax, ecx
  2490. ctx 0x09 cctx_emit ;
  2491. ctx 0xc8 cctx_emit ;
  2492. @processed 1 = ;
  2493. }
  2494. if name "^" strcmp 0 == {
  2495. # xor eax, ecx
  2496. ctx 0x01 cctx_emit ;
  2497. ctx 0x31 cctx_emit ;
  2498. @processed 1 = ;
  2499. }
  2500. if name "<<" strcmp 0 == {
  2501. # shl eax, cl
  2502. ctx 0xd3 cctx_emit ;
  2503. ctx 0xe0 cctx_emit ;
  2504. @processed 1 = ;
  2505. }
  2506. if name ">>" strcmp 0 == type_idx TYPE_UINT == && {
  2507. # shr eax, cl
  2508. ctx 0xd3 cctx_emit ;
  2509. ctx 0xe8 cctx_emit ;
  2510. @processed 1 = ;
  2511. }
  2512. if name ">>" strcmp 0 == type_idx TYPE_INT == && {
  2513. # sar eax, cl
  2514. ctx 0xd3 cctx_emit ;
  2515. ctx 0xf8 cctx_emit ;
  2516. @processed 1 = ;
  2517. }
  2518. if name "+_PRE" strcmp 0 == {
  2519. # mov eax, ecx
  2520. ctx 0x89 cctx_emit ;
  2521. ctx 0xc8 cctx_emit ;
  2522. @processed 1 = ;
  2523. }
  2524. if name "-_PRE" strcmp 0 == {
  2525. # mov eax, ecx; neg eax
  2526. ctx 0x89 cctx_emit ;
  2527. ctx 0xc8 cctx_emit ;
  2528. ctx 0xf7 cctx_emit ;
  2529. ctx 0xd8 cctx_emit ;
  2530. @processed 1 = ;
  2531. }
  2532. if name "~_PRE" strcmp 0 == {
  2533. # mov eax, ecx; not eax
  2534. ctx 0x89 cctx_emit ;
  2535. ctx 0xc8 cctx_emit ;
  2536. ctx 0xf7 cctx_emit ;
  2537. ctx 0xd0 cctx_emit ;
  2538. @processed 1 = ;
  2539. }
  2540. if name "!_PRE" strcmp 0 == {
  2541. # test ecx, ecx; mov eax, 0; sete al
  2542. ctx 0x85 cctx_emit ;
  2543. ctx 0xc9 cctx_emit ;
  2544. ctx 0xb8 cctx_emit ;
  2545. ctx 0 cctx_emit32 ;
  2546. ctx 0x0f cctx_emit ;
  2547. ctx 0x94 cctx_emit ;
  2548. ctx 0xc0 cctx_emit ;
  2549. @processed 1 = ;
  2550. }
  2551. if name "*" strcmp 0 == type_idx TYPE_UINT == && {
  2552. # mul ecx
  2553. ctx 0xf7 cctx_emit ;
  2554. ctx 0xe1 cctx_emit ;
  2555. @processed 1 = ;
  2556. }
  2557. if name "*" strcmp 0 == type_idx TYPE_INT == && {
  2558. # imul ecx
  2559. ctx 0xf7 cctx_emit ;
  2560. ctx 0xe9 cctx_emit ;
  2561. @processed 1 = ;
  2562. }
  2563. if name "/" strcmp 0 == type_idx TYPE_UINT == && {
  2564. # xor edx, edx; div ecx
  2565. ctx 0x31 cctx_emit ;
  2566. ctx 0xd2 cctx_emit ;
  2567. ctx 0xf7 cctx_emit ;
  2568. ctx 0xf1 cctx_emit ;
  2569. @processed 1 = ;
  2570. }
  2571. if name "/" strcmp 0 == type_idx TYPE_INT == && {
  2572. # cdq; idiv ecx
  2573. ctx 0x99 cctx_emit ;
  2574. ctx 0xf7 cctx_emit ;
  2575. ctx 0xf9 cctx_emit ;
  2576. @processed 1 = ;
  2577. }
  2578. if name "%" strcmp 0 == type_idx TYPE_UINT == && {
  2579. # xor edx, edx; div ecx; mov eax, edx
  2580. ctx 0x31 cctx_emit ;
  2581. ctx 0xd2 cctx_emit ;
  2582. ctx 0xf7 cctx_emit ;
  2583. ctx 0xf1 cctx_emit ;
  2584. ctx 0x89 cctx_emit ;
  2585. ctx 0xd0 cctx_emit ;
  2586. @processed 1 = ;
  2587. }
  2588. if name "%" strcmp 0 == type_idx TYPE_INT == && {
  2589. # cdq; idiv ecx; mov eax, edx
  2590. ctx 0x99 cctx_emit ;
  2591. ctx 0xf7 cctx_emit ;
  2592. ctx 0xf9 cctx_emit ;
  2593. ctx 0x89 cctx_emit ;
  2594. ctx 0xd0 cctx_emit ;
  2595. @processed 1 = ;
  2596. }
  2597. if name "==" strcmp 0 == {
  2598. # cmp eax, ecx; mov eax, 0; sete al
  2599. ctx 0x39 cctx_emit ;
  2600. ctx 0xc8 cctx_emit ;
  2601. ctx 0xb8 cctx_emit ;
  2602. ctx 0 cctx_emit32 ;
  2603. ctx 0x0f cctx_emit ;
  2604. ctx 0x94 cctx_emit ;
  2605. ctx 0xc0 cctx_emit ;
  2606. @processed 1 = ;
  2607. }
  2608. if name "!=" strcmp 0 == {
  2609. # cmp eax, ecx; mov eax, 0; setne al
  2610. ctx 0x39 cctx_emit ;
  2611. ctx 0xc8 cctx_emit ;
  2612. ctx 0xb8 cctx_emit ;
  2613. ctx 0 cctx_emit32 ;
  2614. ctx 0x0f cctx_emit ;
  2615. ctx 0x95 cctx_emit ;
  2616. ctx 0xc0 cctx_emit ;
  2617. @processed 1 = ;
  2618. }
  2619. if name "<" strcmp 0 == type_idx TYPE_UINT == && {
  2620. # cmp eax, ecx; mov eax, 0; setb al
  2621. ctx 0x39 cctx_emit ;
  2622. ctx 0xc8 cctx_emit ;
  2623. ctx 0xb8 cctx_emit ;
  2624. ctx 0 cctx_emit32 ;
  2625. ctx 0x0f cctx_emit ;
  2626. ctx 0x92 cctx_emit ;
  2627. ctx 0xc0 cctx_emit ;
  2628. @processed 1 = ;
  2629. }
  2630. if name "<=" strcmp 0 == type_idx TYPE_UINT == && {
  2631. # cmp eax, ecx; mov eax, 0; setbe al
  2632. ctx 0x39 cctx_emit ;
  2633. ctx 0xc8 cctx_emit ;
  2634. ctx 0xb8 cctx_emit ;
  2635. ctx 0 cctx_emit32 ;
  2636. ctx 0x0f cctx_emit ;
  2637. ctx 0x96 cctx_emit ;
  2638. ctx 0xc0 cctx_emit ;
  2639. @processed 1 = ;
  2640. }
  2641. if name ">" strcmp 0 == type_idx TYPE_UINT == && {
  2642. # cmp eax, ecx; mov eax, 0; seta al
  2643. ctx 0x39 cctx_emit ;
  2644. ctx 0xc8 cctx_emit ;
  2645. ctx 0xb8 cctx_emit ;
  2646. ctx 0 cctx_emit32 ;
  2647. ctx 0x0f cctx_emit ;
  2648. ctx 0x97 cctx_emit ;
  2649. ctx 0xc0 cctx_emit ;
  2650. @processed 1 = ;
  2651. }
  2652. if name ">=" strcmp 0 == type_idx TYPE_UINT == && {
  2653. # cmp eax, ecx; mov eax, 0; setae al
  2654. ctx 0x39 cctx_emit ;
  2655. ctx 0xc8 cctx_emit ;
  2656. ctx 0xb8 cctx_emit ;
  2657. ctx 0 cctx_emit32 ;
  2658. ctx 0x0f cctx_emit ;
  2659. ctx 0x93 cctx_emit ;
  2660. ctx 0xc0 cctx_emit ;
  2661. @processed 1 = ;
  2662. }
  2663. if name "<" strcmp 0 == type_idx TYPE_INT == && {
  2664. # cmp eax, ecx; mov eax, 0; setl al
  2665. ctx 0x39 cctx_emit ;
  2666. ctx 0xc8 cctx_emit ;
  2667. ctx 0xb8 cctx_emit ;
  2668. ctx 0 cctx_emit32 ;
  2669. ctx 0x0f cctx_emit ;
  2670. ctx 0x9c cctx_emit ;
  2671. ctx 0xc0 cctx_emit ;
  2672. @processed 1 = ;
  2673. }
  2674. if name "<=" strcmp 0 == type_idx TYPE_INT == && {
  2675. # cmp eax, ecx; mov eax, 0; setle al
  2676. ctx 0x39 cctx_emit ;
  2677. ctx 0xc8 cctx_emit ;
  2678. ctx 0xb8 cctx_emit ;
  2679. ctx 0 cctx_emit32 ;
  2680. ctx 0x0f cctx_emit ;
  2681. ctx 0x9e cctx_emit ;
  2682. ctx 0xc0 cctx_emit ;
  2683. @processed 1 = ;
  2684. }
  2685. if name ">" strcmp 0 == type_idx TYPE_INT == && {
  2686. # cmp eax, ecx; mov eax, 0; setg al
  2687. ctx 0x39 cctx_emit ;
  2688. ctx 0xc8 cctx_emit ;
  2689. ctx 0xb8 cctx_emit ;
  2690. ctx 0 cctx_emit32 ;
  2691. ctx 0x0f cctx_emit ;
  2692. ctx 0x9f cctx_emit ;
  2693. ctx 0xc0 cctx_emit ;
  2694. @processed 1 = ;
  2695. }
  2696. if name ">=" strcmp 0 == type_idx TYPE_INT == && {
  2697. # cmp eax, ecx; mov eax, 0; setge al
  2698. ctx 0x39 cctx_emit ;
  2699. ctx 0xc8 cctx_emit ;
  2700. ctx 0xb8 cctx_emit ;
  2701. ctx 0 cctx_emit32 ;
  2702. ctx 0x0f cctx_emit ;
  2703. ctx 0x9d cctx_emit ;
  2704. ctx 0xc0 cctx_emit ;
  2705. @processed 1 = ;
  2706. }
  2707. processed "ast_push_value_arith: not implemented" assert_msg ;
  2708. # pop ebx
  2709. ctx 0x5b cctx_emit ;
  2710. # Push result stored in EDX:EAX
  2711. # push edx; push eax
  2712. ctx 0x52 cctx_emit ;
  2713. ctx 0x50 cctx_emit ;
  2714. # Convert result to the output type
  2715. lctx ctx large_idx type_idx lctx_int_convert ;
  2716. }
  2717. fun cctx_gen_push_data 2 {
  2718. $ctx
  2719. $size
  2720. @ctx 1 param = ;
  2721. @size 0 param = ;
  2722. size 4 % 0 == "cctx_gen_push_data: size is not multiple of 4" assert_msg ;
  2723. $i
  2724. @i size 4 - = ;
  2725. while i 0 >= {
  2726. # push [eax+off]
  2727. ctx 0xff cctx_emit ;
  2728. ctx 0xb0 cctx_emit ;
  2729. ctx i cctx_emit32 ;
  2730. @i i 4 - = ;
  2731. }
  2732. }
  2733. fun cctx_gen_pop_data 2 {
  2734. $ctx
  2735. $size
  2736. @ctx 1 param = ;
  2737. @size 0 param = ;
  2738. size 4 % 0 == "cctx_gen_pop_data: size is not multiple of 4" assert_msg ;
  2739. $i
  2740. @i 0 = ;
  2741. while i size < {
  2742. # pop [eax+off]
  2743. ctx 0x8f cctx_emit ;
  2744. ctx 0x80 cctx_emit ;
  2745. ctx i cctx_emit32 ;
  2746. @i i 4 + = ;
  2747. }
  2748. }
  2749. fun cctx_gen_move_data 2 {
  2750. $ctx
  2751. $size
  2752. @ctx 1 param = ;
  2753. @size 0 param = ;
  2754. if size 1 == {
  2755. # mov dl, [esp]; mov [eax], dl
  2756. ctx 0x8a cctx_emit ;
  2757. ctx 0x14 cctx_emit ;
  2758. ctx 0x24 cctx_emit ;
  2759. ctx 0x88 cctx_emit ;
  2760. ctx 0x10 cctx_emit ;
  2761. ret ;
  2762. }
  2763. if size 2 == {
  2764. # mov dx, [esp]; mov [eax], dx
  2765. ctx 0x66 cctx_emit ;
  2766. ctx 0x8b cctx_emit ;
  2767. ctx 0x14 cctx_emit ;
  2768. ctx 0x24 cctx_emit ;
  2769. ctx 0x66 cctx_emit ;
  2770. ctx 0x89 cctx_emit ;
  2771. ctx 0x10 cctx_emit ;
  2772. ret ;
  2773. }
  2774. size 4 % 0 == "cctx_gen_move_data: size is not multiple of 4" assert_msg ;
  2775. $i
  2776. @i 0 = ;
  2777. while i size < {
  2778. # mov edx, [esp+off]; mov [eax+off], edx
  2779. ctx 0x8b cctx_emit ;
  2780. ctx 0x94 cctx_emit ;
  2781. ctx 0x24 cctx_emit ;
  2782. ctx i cctx_emit32 ;
  2783. ctx 0x89 cctx_emit ;
  2784. ctx 0x90 cctx_emit ;
  2785. ctx i cctx_emit32 ;
  2786. @i i 4 + = ;
  2787. }
  2788. }
  2789. fun cctx_gen_move_data_backward 2 {
  2790. $ctx
  2791. $size
  2792. @ctx 1 param = ;
  2793. @size 0 param = ;
  2794. size 4 % 0 == "cctx_gen_move_data: size is not multiple of 4" assert_msg ;
  2795. $i
  2796. @i size 4 - = ;
  2797. while i 0 >= {
  2798. # mov edx, [esp+off]; mov [eax+off], edx
  2799. ctx 0x8b cctx_emit ;
  2800. ctx 0x94 cctx_emit ;
  2801. ctx 0x24 cctx_emit ;
  2802. ctx i cctx_emit32 ;
  2803. ctx 0x89 cctx_emit ;
  2804. ctx 0x90 cctx_emit ;
  2805. ctx i cctx_emit32 ;
  2806. @i i 4 - = ;
  2807. }
  2808. }
  2809. fun cctx_default_promotion 2 {
  2810. $ctx
  2811. $type_idx
  2812. @ctx 1 param = ;
  2813. @type_idx 0 param = ;
  2814. if type_idx TYPE_CHAR ==
  2815. type_idx TYPE_SCHAR == ||
  2816. type_idx TYPE_SHORT == ||
  2817. type_idx TYPE_INT == || {
  2818. TYPE_INT ret ;
  2819. }
  2820. if type_idx TYPE_UCHAR ==
  2821. type_idx TYPE_USHORT == ||
  2822. type_idx TYPE_UINT == || {
  2823. TYPE_UINT ret ;
  2824. }
  2825. type_idx ret ;
  2826. }
  2827. fun ast_gen_function_call 3 {
  2828. $ast
  2829. $ctx
  2830. $lctx
  2831. @ast 2 param = ;
  2832. @ctx 1 param = ;
  2833. @lctx 0 param = ;
  2834. $left
  2835. $right
  2836. @left ast AST_LEFT take = ;
  2837. @right ast AST_RIGHT take = ;
  2838. $type_idx
  2839. @type_idx ast ctx lctx ast_eval_type = ;
  2840. $fun_ptr_idx
  2841. $fun_ptr_type
  2842. $fun_idx
  2843. $fun_type
  2844. @fun_ptr_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2845. @fun_ptr_type ctx fun_ptr_idx cctx_get_type = ;
  2846. fun_ptr_type TYPE_KIND take TYPE_KIND_POINTER == "ast_gen_function_call: left is not a pointer" assert_msg ;
  2847. @fun_idx fun_ptr_type TYPE_BASE take = ;
  2848. @fun_type ctx fun_idx cctx_get_type = ;
  2849. fun_type TYPE_KIND take TYPE_KIND_FUNCTION == "ast_gen_function_call: left is not a pointer to function" assert_msg ;
  2850. $args
  2851. @args fun_type TYPE_ARGS take = ;
  2852. # Passed arguments are stored in reverse order
  2853. $passed_args
  2854. @passed_args 4 vector_init = ;
  2855. if right AST_NAME take 0 != {
  2856. $cont
  2857. @cont 1 = ;
  2858. while cont {
  2859. if right AST_NAME take "," strcmp 0 == {
  2860. passed_args right AST_RIGHT take vector_push_back ;
  2861. @right right AST_LEFT take = ;
  2862. } else {
  2863. passed_args right vector_push_back ;
  2864. @cont 0 = ;
  2865. }
  2866. }
  2867. }
  2868. # Push arguments on the stack, right to left
  2869. $ellipsis
  2870. @ellipsis fun_type TYPE_ELLIPSIS take = ;
  2871. if ellipsis {
  2872. passed_args vector_size args vector_size >= "ast_gen_function_call: too few arguments" assert_msg ;
  2873. } else {
  2874. passed_args vector_size args vector_size == "ast_gen_function_call: arguments number does not match" assert_msg ;
  2875. }
  2876. $i
  2877. @i 0 = ;
  2878. $rewind
  2879. @rewind 0 = ;
  2880. $excess_args
  2881. @excess_args passed_args vector_size args vector_size - = ;
  2882. while i passed_args vector_size < {
  2883. $passed_arg
  2884. @passed_arg passed_args i vector_at = ;
  2885. passed_arg ctx lctx ast_push_value ;
  2886. $from_type
  2887. $to_type
  2888. @from_type passed_arg ctx lctx ast_eval_type = ;
  2889. if i excess_args < {
  2890. @to_type ctx from_type cctx_default_promotion = ;
  2891. } else {
  2892. $arg
  2893. @arg args args vector_size i excess_args - - 1 - vector_at = ;
  2894. @to_type arg = ;
  2895. }
  2896. lctx ctx from_type to_type lctx_convert_stack ;
  2897. @rewind rewind ctx to_type cctx_type_footprint + = ;
  2898. @i i 1 + = ;
  2899. }
  2900. # Call function
  2901. # ast_push_value; pop eax; call eax
  2902. left ctx lctx ast_push_value ;
  2903. ctx 0x58 cctx_emit ;
  2904. ctx 0xff cctx_emit ;
  2905. ctx 0xd0 cctx_emit ;
  2906. # Clean up stack
  2907. # add esp, rewind
  2908. ctx 0x81 cctx_emit ;
  2909. ctx 0xc4 cctx_emit ;
  2910. ctx rewind cctx_emit32 ;
  2911. # Push result
  2912. if type_idx TYPE_VOID != {
  2913. $res_footprint
  2914. @res_footprint ctx ast ctx lctx ast_eval_type cctx_type_footprint = ;
  2915. res_footprint 4 == res_footprint 0 == || "ast_gen_function_call: return type is not scalar" assert_msg ;
  2916. if res_footprint 4 == {
  2917. # push eax
  2918. ctx 0x50 cctx_emit ;
  2919. }
  2920. }
  2921. passed_args vector_destroy ;
  2922. }
  2923. fun ast_push_value_ptr 3 {
  2924. $ast
  2925. $ctx
  2926. $lctx
  2927. @ast 2 param = ;
  2928. @ctx 1 param = ;
  2929. @lctx 0 param = ;
  2930. $name
  2931. @name ast AST_NAME take = ;
  2932. $sum
  2933. $subtract
  2934. @sum name "+" strcmp 0 == = ;
  2935. @subtract name "-" strcmp 0 == = ;
  2936. sum subtract || "ast_push_value_ptr: not a sum or a subtraction" assert_msg ;
  2937. $processed
  2938. @processed 0 = ;
  2939. $left_idx
  2940. $right_idx
  2941. @left_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  2942. @right_idx ast AST_RIGHT take ctx lctx ast_eval_type = ;
  2943. $left_ptr
  2944. $right_ptr
  2945. @left_ptr ctx left_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
  2946. @right_ptr ctx right_idx cctx_get_type TYPE_KIND take TYPE_KIND_POINTER == = ;
  2947. $left_size
  2948. $right_size
  2949. if left_ptr {
  2950. @left_size ctx ctx left_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
  2951. }
  2952. if right_ptr {
  2953. @right_size ctx ctx right_idx cctx_get_type TYPE_BASE take cctx_type_size = ;
  2954. }
  2955. if sum {
  2956. left_ptr right_ptr && ! "ast_eval_type: cannot take sum of two pointers" assert_msg ;
  2957. if left_ptr {
  2958. # ast_push_value; ast_push_value; push size
  2959. ast AST_LEFT take ctx lctx ast_push_value ;
  2960. ast AST_RIGHT take ctx lctx ast_push_value ;
  2961. lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type TYPE_UINT lctx_convert_stack ;
  2962. ctx 0x68 cctx_emit ;
  2963. ctx left_size cctx_emit32 ;
  2964. @processed 1 = ;
  2965. }
  2966. if right_ptr {
  2967. # ast_push_value; ast_push_value; push size
  2968. ast AST_RIGHT take ctx lctx ast_push_value ;
  2969. ast AST_LEFT take ctx lctx ast_push_value ;
  2970. lctx ctx ast AST_LEFT take ctx lctx ast_eval_type TYPE_UINT lctx_convert_stack ;
  2971. ctx 0x68 cctx_emit ;
  2972. ctx right_size cctx_emit32 ;
  2973. @processed 1 = ;
  2974. }
  2975. if processed {
  2976. # pop eax; pop edx; imul edx; pop ecx; add eax, ecx; push eax
  2977. ctx 0x58 cctx_emit ;
  2978. ctx 0x5a cctx_emit ;
  2979. ctx 0xf7 cctx_emit ;
  2980. ctx 0xea cctx_emit ;
  2981. ctx 0x59 cctx_emit ;
  2982. ctx 0x01 cctx_emit ;
  2983. ctx 0xc8 cctx_emit ;
  2984. ctx 0x50 cctx_emit ;
  2985. }
  2986. } else {
  2987. left_ptr ! right_ptr && ! "ast_eval_type: cannot take different of a non-pointer and a pointer" assert_msg ;
  2988. if left_ptr {
  2989. if right_ptr {
  2990. left_size right_size == "ast_eval_type: cannot take difference of pointers to types of different size" assert_msg ;
  2991. # push size; ast_push_value; ast_push_value
  2992. ctx 0x68 cctx_emit ;
  2993. ctx left_size cctx_emit32 ;
  2994. ast AST_LEFT take ctx lctx ast_push_value ;
  2995. ast AST_RIGHT take ctx lctx ast_push_value ;
  2996. # pop edx; pop eax; sub eax, edx; pop ecx; cdq; idiv ecx; push eax
  2997. ctx 0x5a cctx_emit ;
  2998. ctx 0x58 cctx_emit ;
  2999. ctx 0x29 cctx_emit ;
  3000. ctx 0xd0 cctx_emit ;
  3001. ctx 0x59 cctx_emit ;
  3002. ctx 0x99 cctx_emit ;
  3003. ctx 0xf7 cctx_emit ;
  3004. ctx 0xf9 cctx_emit ;
  3005. ctx 0x50 cctx_emit ;
  3006. } else {
  3007. # ast_push_value; ast_push_value; push size
  3008. ast AST_LEFT take ctx lctx ast_push_value ;
  3009. ast AST_RIGHT take ctx lctx ast_push_value ;
  3010. lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type TYPE_UINT lctx_convert_stack ;
  3011. ctx 0x68 cctx_emit ;
  3012. ctx left_size cctx_emit32 ;
  3013. # pop eax; pop edx; imul edx; pop ecx; neg eax; add eax, ecx; push eax
  3014. ctx 0x58 cctx_emit ;
  3015. ctx 0x5a cctx_emit ;
  3016. ctx 0xf7 cctx_emit ;
  3017. ctx 0xea cctx_emit ;
  3018. ctx 0x59 cctx_emit ;
  3019. ctx 0xf7 cctx_emit ;
  3020. ctx 0xd8 cctx_emit ;
  3021. ctx 0x01 cctx_emit ;
  3022. ctx 0xc8 cctx_emit ;
  3023. ctx 0x50 cctx_emit ;
  3024. }
  3025. @processed 1 = ;
  3026. }
  3027. }
  3028. processed ret ;
  3029. }
  3030. fun ast_push_value 3 {
  3031. $ast
  3032. $ctx
  3033. $lctx
  3034. @ast 2 param = ;
  3035. @ctx 1 param = ;
  3036. @lctx 0 param = ;
  3037. $name
  3038. @name ast AST_NAME take = ;
  3039. $type_idx
  3040. @type_idx ast ctx lctx ast_eval_type = ;
  3041. # In case of type decaying, return the address
  3042. $orig_type_idx
  3043. @orig_type_idx ast AST_ORIG_TYPE_IDX take = ;
  3044. $orig_type
  3045. @orig_type ctx orig_type_idx cctx_get_type = ;
  3046. $orig_kind
  3047. @orig_kind orig_type TYPE_KIND take = ;
  3048. if orig_kind TYPE_KIND_FUNCTION == orig_kind TYPE_KIND_ARRAY == || {
  3049. ast ctx lctx ast_push_addr ;
  3050. ret ;
  3051. }
  3052. if ast AST_TYPE take 0 == {
  3053. # Operand
  3054. if name is_valid_identifier {
  3055. $enum_consts
  3056. @enum_consts ctx CCTX_ENUM_CONSTS take = ;
  3057. if enum_consts name map_has {
  3058. $value
  3059. @value enum_consts name map_at = ;
  3060. ctx 0x68 cctx_emit ;
  3061. ctx value cctx_emit32 ;
  3062. } else {
  3063. # Push the address
  3064. ast ctx lctx ast_push_addr ;
  3065. # pop eax
  3066. ctx 0x58 cctx_emit ;
  3067. ctx ctx type_idx cctx_type_footprint cctx_gen_push_data ;
  3068. }
  3069. } else {
  3070. $value
  3071. if name **c '\"' == {
  3072. 0 "ast_push_value: error 1" assert_msg ;
  3073. } else {
  3074. if name **c '\'' == {
  3075. $data
  3076. $from
  3077. $to
  3078. @data 0 = ;
  3079. @from name 1 + = ;
  3080. @to @data = ;
  3081. @from @to 0 escape_char ;
  3082. to @data 1 + == "ast_push_value: invalid character literal 1" assert_msg ;
  3083. from **c '\'' == "ast_push_value: invalid character literal 2" assert_msg ;
  3084. from 1 + **c 0 == "ast_push_value: invalid character literal 3" assert_msg ;
  3085. @value data = ;
  3086. } else {
  3087. @value name atoi_c = ;
  3088. }
  3089. }
  3090. # push value
  3091. ctx 0x68 cctx_emit ;
  3092. ctx value cctx_emit32 ;
  3093. }
  3094. } else {
  3095. # Operator
  3096. $processed
  3097. @processed 0 = ;
  3098. $sum
  3099. $subtract
  3100. @sum name "+" strcmp 0 == = ;
  3101. @subtract name "-" strcmp 0 == = ;
  3102. if sum subtract || {
  3103. @processed ast ctx lctx ast_push_value_ptr = ;
  3104. }
  3105. if name "*" strcmp 0 ==
  3106. name "/" strcmp 0 == ||
  3107. name "%" strcmp 0 == ||
  3108. name "+" strcmp 0 == ||
  3109. name "-" strcmp 0 == ||
  3110. name "<" strcmp 0 == ||
  3111. name ">" strcmp 0 == ||
  3112. name "<=" strcmp 0 == ||
  3113. name ">=" strcmp 0 == ||
  3114. name "==" strcmp 0 == ||
  3115. name "!=" strcmp 0 == ||
  3116. name "&" strcmp 0 == ||
  3117. name "^" strcmp 0 == ||
  3118. name "|" strcmp 0 == ||
  3119. name "<<" strcmp 0 == ||
  3120. name ">>" strcmp 0 == ||
  3121. name "+_PRE" strcmp 0 == ||
  3122. name "-_PRE" strcmp 0 == ||
  3123. name "~_PRE" strcmp 0 == ||
  3124. name "!_PRE" strcmp 0 == ||
  3125. processed ! && {
  3126. ast ctx lctx ast_push_value_arith ;
  3127. @processed 1 = ;
  3128. }
  3129. if name "&&" strcmp 0 ==
  3130. name "||" strcmp 0 == ||
  3131. processed ! && {
  3132. ast ctx lctx ast_push_value_logic ;
  3133. @processed 1 = ;
  3134. }
  3135. if name "=" strcmp 0 == {
  3136. ast AST_RIGHT take ctx lctx ast_push_value ;
  3137. lctx ctx ast AST_RIGHT take ctx lctx ast_eval_type ast ctx lctx ast_eval_type lctx_convert_stack ;
  3138. ast AST_LEFT take ctx lctx ast_push_addr ;
  3139. # pop eax; cctx_gen_move_data
  3140. ctx 0x58 cctx_emit ;
  3141. ctx ctx ast ctx lctx ast_eval_type cctx_type_size cctx_gen_move_data ;
  3142. @processed 1 = ;
  3143. }
  3144. if name "(" strcmp 0 == {
  3145. ast ctx lctx ast_gen_function_call ;
  3146. @processed 1 = ;
  3147. }
  3148. if name "*_PRE" strcmp 0 == {
  3149. # Push the address
  3150. ast ctx lctx ast_push_addr ;
  3151. # pop eax
  3152. ctx 0x58 cctx_emit ;
  3153. ctx ctx type_idx cctx_type_footprint cctx_gen_push_data ;
  3154. @processed 1 = ;
  3155. }
  3156. if name "&_PRE" strcmp 0 == {
  3157. ast AST_RIGHT take ctx lctx ast_push_addr ;
  3158. @processed 1 = ;
  3159. }
  3160. if name "." strcmp 0 == {
  3161. $struct_idx
  3162. $struct_type
  3163. $struct_size
  3164. @struct_idx ast AST_LEFT take ctx lctx ast_eval_type = ;
  3165. @struct_type ctx struct_idx cctx_get_type = ;
  3166. @struct_size ctx struct_idx cctx_type_footprint = ;
  3167. ast AST_RIGHT take AST_TYPE take 0 == "ast_push_value: right is not a plain name" assert_msg ;
  3168. $name
  3169. @name ast AST_RIGHT take AST_NAME take = ;
  3170. $field
  3171. @field struct_type name type_get_idx = ;
  3172. field 0xffffffff != "ast_push_value: specified field does not exist" assert_msg ;
  3173. $res_idx
  3174. $res_size
  3175. $off
  3176. @res_idx ast ctx lctx ast_eval_type = ;
  3177. @res_size ctx res_idx cctx_type_footprint = ;
  3178. @off struct_type TYPE_FIELDS_OFFS take field vector_at = ;
  3179. # ast_push_value; lea eax, [esp+struct_size-res_size]; add esp, off; cctx_gen_move_data_backward; mov esp, eax
  3180. ast AST_LEFT take ctx lctx ast_push_value ;
  3181. ctx 0x8d cctx_emit ;
  3182. ctx 0x84 cctx_emit ;
  3183. ctx 0x24 cctx_emit ;
  3184. ctx struct_size res_size - cctx_emit32 ;
  3185. ctx 0x81 cctx_emit ;
  3186. ctx 0xc4 cctx_emit ;
  3187. ctx off cctx_emit32 ;
  3188. ctx res_size cctx_gen_move_data_backward ;
  3189. ctx 0x89 cctx_emit ;
  3190. ctx 0xc4 cctx_emit ;
  3191. @processed 1 = ;
  3192. }
  3193. if name "->" strcmp 0 == {
  3194. # Push the address
  3195. ast ctx lctx ast_push_addr ;
  3196. # pop eax
  3197. ctx 0x58 cctx_emit ;
  3198. ctx ctx type_idx cctx_type_footprint cctx_gen_push_data ;
  3199. @processed 1 = ;
  3200. }
  3201. processed "ast_push_value: not implemented" assert_msg ;
  3202. }
  3203. }
  3204. fun ast_eval 3 {
  3205. $ast
  3206. $ctx
  3207. $lctx
  3208. @ast 2 param = ;
  3209. @ctx 1 param = ;
  3210. @lctx 0 param = ;
  3211. # First push value
  3212. ast ctx lctx ast_push_value ;
  3213. # Then pop and discard it (if it is not void)
  3214. $type_idx
  3215. @type_idx ast ctx lctx ast_eval_type = ;
  3216. if type_idx TYPE_VOID != {
  3217. $footprint
  3218. @footprint ctx type_idx cctx_type_footprint = ;
  3219. # add esp, footprint
  3220. ctx 0x81 cctx_emit ;
  3221. ctx 0xc4 cctx_emit ;
  3222. ctx footprint cctx_emit32 ;
  3223. }
  3224. }
  3225. fun cctx_compile_expression 2 {
  3226. $ctx
  3227. $lctx
  3228. $target_type_idx
  3229. $end_tok
  3230. @ctx 3 param = ;
  3231. @lctx 2 param = ;
  3232. @target_type_idx 1 param = ;
  3233. @end_tok 0 param = ;
  3234. $ast
  3235. @ast ctx end_tok cctx_parse_ast1 = ;
  3236. #ast ctx lctx ast_eval_type ;
  3237. if target_type_idx TYPE_VOID == {
  3238. ast ctx lctx ast_eval ;
  3239. } else {
  3240. ast ctx lctx ast_push_value ;
  3241. lctx ctx ast ctx lctx ast_eval_type target_type_idx lctx_convert_stack ;
  3242. }
  3243. #ast ast_dump ;
  3244. ast ast_destroy ;
  3245. }
  3246. ifun cctx_compile_statement_or_block 2
  3247. fun cctx_compile_statement 2 {
  3248. $ctx
  3249. $lctx
  3250. @ctx 1 param = ;
  3251. @lctx 0 param = ;
  3252. $processed
  3253. @processed 0 = ;
  3254. $tok
  3255. @tok ctx cctx_get_token_or_fail = ;
  3256. # Check if we found the closing brace
  3257. if tok "}" strcmp 0 == processed ! && {
  3258. @processed 1 = ;
  3259. 0 ret ;
  3260. }
  3261. $expect_semicolon
  3262. @expect_semicolon 0 = ;
  3263. # Parse return
  3264. if tok "return" strcmp 0 == processed ! && {
  3265. $ret_type
  3266. @ret_type lctx LCTX_RETURN_TYPE_IDX take = ;
  3267. if ret_type TYPE_VOID != {
  3268. ctx ret_type cctx_type_footprint 4 == "cctx_compile_statement: only returned scalar types are supported" assert_msg ;
  3269. ctx lctx ret_type ";" cctx_compile_expression ;
  3270. # pop eax
  3271. ctx 0x58 cctx_emit ;
  3272. }
  3273. ctx lctx lctx LCTX_RETURN_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
  3274. @processed 1 = ;
  3275. @expect_semicolon 1 = ;
  3276. }
  3277. # Parse break
  3278. if tok "break" strcmp 0 == processed ! && {
  3279. ctx lctx lctx LCTX_BREAK_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
  3280. @processed 1 = ;
  3281. @expect_semicolon 1 = ;
  3282. }
  3283. # Parse continue
  3284. if tok "continue" strcmp 0 == processed ! && {
  3285. ctx lctx lctx LCTX_CONTINUE_LABEL take JUMP_TYPE_JMP 1 cctx_gen_label_jump ;
  3286. @processed 1 = ;
  3287. @expect_semicolon 1 = ;
  3288. }
  3289. # Parse if
  3290. if tok "if" strcmp 0 == processed ! && {
  3291. $else_lab
  3292. $end_lab
  3293. @else_lab lctx ctx lctx_gen_label = ;
  3294. @end_lab lctx ctx lctx_gen_label = ;
  3295. # Compile guard expression
  3296. @tok ctx cctx_get_token_or_fail = ;
  3297. tok "(" strcmp 0 == "cctx_compile_statement: ( expected" assert_msg ;
  3298. ctx lctx TYPE_UINT ")" cctx_compile_expression ;
  3299. @tok ctx cctx_get_token_or_fail = ;
  3300. tok ")" strcmp 0 == "cctx_compile_statement: ) expected" assert_msg ;
  3301. # pop eax; test eax, eax; cctx_gen_label_jump
  3302. ctx 0x58 cctx_emit ;
  3303. ctx 0x85 cctx_emit ;
  3304. ctx 0xc0 cctx_emit ;
  3305. ctx lctx else_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
  3306. # Compile body
  3307. ctx lctx cctx_compile_statement_or_block ;
  3308. # cctx_gen_label_jump
  3309. ctx lctx end_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
  3310. # Compile else
  3311. lctx ctx else_lab lctx_fix_label ;
  3312. @tok ctx cctx_get_token_or_fail = ;
  3313. if tok "else" strcmp 0 == {
  3314. ctx lctx cctx_compile_statement_or_block ;
  3315. } else {
  3316. ctx cctx_give_back_token ;
  3317. }
  3318. lctx ctx end_lab lctx_fix_label ;
  3319. @processed 1 = ;
  3320. }
  3321. # Parse for
  3322. if tok "for" strcmp 0 == processed ! && {
  3323. # Set up labels
  3324. $continue_lab
  3325. $break_lab
  3326. $restart_lab
  3327. $body_lab
  3328. @continue_lab lctx ctx lctx_gen_label = ;
  3329. @break_lab lctx ctx lctx_gen_label = ;
  3330. @restart_lab lctx ctx lctx_gen_label = ;
  3331. @body_lab lctx ctx lctx_gen_label = ;
  3332. # Compile initialization expression
  3333. @tok ctx cctx_get_token_or_fail = ;
  3334. tok "(" strcmp 0 == "cctx_compile_statement: ( expected after for" assert_msg ;
  3335. ctx lctx TYPE_VOID ";" cctx_compile_expression ;
  3336. @tok ctx cctx_get_token_or_fail = ;
  3337. tok ";" strcmp 0 == "cctx_compile_statement: ; expected after for" assert_msg ;
  3338. # Compile guard expression
  3339. lctx ctx restart_lab lctx_fix_label ;
  3340. ctx lctx TYPE_UINT ";" cctx_compile_expression ;
  3341. @tok ctx cctx_get_token_or_fail = ;
  3342. tok ";" strcmp 0 == "cctx_compile_statement: second ; expected after for" assert_msg ;
  3343. # pop eax; test eax, eax; cctx_gen_label_jump; cctx_gen_label_jump
  3344. ctx 0x58 cctx_emit ;
  3345. ctx 0x85 cctx_emit ;
  3346. ctx 0xc0 cctx_emit ;
  3347. ctx lctx break_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
  3348. ctx lctx body_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
  3349. # Compile iteration expression
  3350. lctx ctx continue_lab lctx_fix_label ;
  3351. ctx lctx TYPE_VOID ")" cctx_compile_expression ;
  3352. @tok ctx cctx_get_token_or_fail = ;
  3353. tok ")" strcmp 0 == "cctx_compile_statement: ) expected after for" assert_msg ;
  3354. # cctx_gen_label_jump
  3355. ctx lctx restart_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
  3356. # Compile body
  3357. $old_break_lab
  3358. $old_continue_lab
  3359. @old_break_lab lctx LCTX_BREAK_LABEL take = ;
  3360. @old_continue_lab lctx LCTX_CONTINUE_LABEL take = ;
  3361. lctx LCTX_BREAK_LABEL take_addr break_lab = ;
  3362. lctx LCTX_CONTINUE_LABEL take_addr continue_lab = ;
  3363. lctx ctx body_lab lctx_fix_label ;
  3364. ctx lctx cctx_compile_statement_or_block ;
  3365. lctx LCTX_BREAK_LABEL take_addr old_break_lab = ;
  3366. lctx LCTX_CONTINUE_LABEL take_addr old_continue_lab = ;
  3367. # cctx_gen_label_jump
  3368. ctx lctx continue_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
  3369. lctx ctx break_lab lctx_fix_label ;
  3370. @processed 1 = ;
  3371. }
  3372. # Parse while
  3373. if tok "while" strcmp 0 == processed ! && {
  3374. # Set up labels
  3375. $continue_lab
  3376. $break_lab
  3377. @continue_lab lctx ctx lctx_gen_label = ;
  3378. @break_lab lctx ctx lctx_gen_label = ;
  3379. # Compile guard expression
  3380. lctx ctx continue_lab lctx_fix_label ;
  3381. @tok ctx cctx_get_token_or_fail = ;
  3382. tok "(" strcmp 0 == "cctx_compile_statement: ( expected after while" assert_msg ;
  3383. ctx lctx TYPE_UINT ")" cctx_compile_expression ;
  3384. @tok ctx cctx_get_token_or_fail = ;
  3385. tok ")" strcmp 0 == "cctx_compile_statement: ) expected after while" assert_msg ;
  3386. # pop eax; test eax, eax; cctx_gen_label_jump
  3387. ctx 0x58 cctx_emit ;
  3388. ctx 0x85 cctx_emit ;
  3389. ctx 0xc0 cctx_emit ;
  3390. ctx lctx break_lab JUMP_TYPE_JZ 0 cctx_gen_label_jump ;
  3391. # Compile body
  3392. $old_break_lab
  3393. $old_continue_lab
  3394. @old_break_lab lctx LCTX_BREAK_LABEL take = ;
  3395. @old_continue_lab lctx LCTX_CONTINUE_LABEL take = ;
  3396. lctx LCTX_BREAK_LABEL take_addr break_lab = ;
  3397. lctx LCTX_CONTINUE_LABEL take_addr continue_lab = ;
  3398. ctx lctx cctx_compile_statement_or_block ;
  3399. lctx LCTX_BREAK_LABEL take_addr old_break_lab = ;
  3400. lctx LCTX_CONTINUE_LABEL take_addr old_continue_lab = ;
  3401. # cctx_gen_label_jump
  3402. ctx lctx continue_lab JUMP_TYPE_JMP 0 cctx_gen_label_jump ;
  3403. lctx ctx break_lab lctx_fix_label ;
  3404. @processed 1 = ;
  3405. }
  3406. if processed ! {
  3407. ctx cctx_give_back_token ;
  3408. # Try to parse a type, in which case we have a variable declaration
  3409. $type_idx
  3410. @type_idx ctx cctx_parse_type = ;
  3411. if type_idx 0xffffffff != {
  3412. # There is a type, so we have a variable declaration
  3413. $cont
  3414. @cont 1 = ;
  3415. while cont {
  3416. $actual_type_idx
  3417. $name
  3418. ctx type_idx @actual_type_idx @name 0 cctx_parse_declarator "cctx_compile_statement: error 1" assert_msg ;
  3419. name 0 != "cctx_compile_statement: cannot instantiate variable without name" assert_msg ;
  3420. lctx ctx actual_type_idx name lctx_push_var ;
  3421. @tok ctx cctx_get_token_or_fail = ;
  3422. if tok "=" strcmp 0 == {
  3423. $right_ast
  3424. $left_ast
  3425. $ast
  3426. @right_ast ctx ";" cctx_parse_ast1 = ;
  3427. @left_ast ast_init = ;
  3428. left_ast AST_TYPE take_addr 0 = ;
  3429. left_ast AST_NAME take_addr name strdup = ;
  3430. @ast ast_init = ;
  3431. ast AST_TYPE take_addr 1 = ;
  3432. ast AST_NAME take_addr "=" strdup = ;
  3433. ast AST_RIGHT take_addr right_ast = ;
  3434. ast AST_LEFT take_addr left_ast = ;
  3435. ast ctx lctx ast_eval ;
  3436. ast ast_destroy ;
  3437. } else {
  3438. ctx cctx_give_back_token ;
  3439. }
  3440. @tok ctx cctx_get_token_or_fail = ;
  3441. if tok ";" strcmp 0 == {
  3442. ctx cctx_give_back_token ;
  3443. @cont 0 = ;
  3444. } else {
  3445. tok "," strcmp 0 == "cctx_compile_statement: comma expected" assert_msg ;
  3446. }
  3447. }
  3448. } else {
  3449. # No type, so this is an expression
  3450. ctx lctx TYPE_VOID ";" cctx_compile_expression ;
  3451. }
  3452. @expect_semicolon 1 = ;
  3453. }
  3454. # Expect and consume the semicolon
  3455. if expect_semicolon {
  3456. @tok ctx cctx_get_token_or_fail = ;
  3457. tok ";" strcmp 0 == "cctx_compile_statement: ; expected" assert_msg ;
  3458. }
  3459. 1 ret ;
  3460. }
  3461. fun cctx_compile_block 2 {
  3462. $ctx
  3463. $lctx
  3464. @ctx 1 param = ;
  3465. @lctx 0 param = ;
  3466. $saved_pos
  3467. @saved_pos lctx ctx lctx_save_status = ;
  3468. $cont
  3469. @cont 1 = ;
  3470. while cont {
  3471. @cont ctx lctx cctx_compile_statement = ;
  3472. }
  3473. lctx ctx saved_pos lctx_restore_status ;
  3474. }
  3475. fun cctx_compile_statement_or_block 2 {
  3476. $ctx
  3477. $lctx
  3478. @ctx 1 param = ;
  3479. @lctx 0 param = ;
  3480. $tok
  3481. @tok ctx cctx_get_token_or_fail = ;
  3482. if tok "{" strcmp 0 == {
  3483. ctx lctx cctx_compile_block ;
  3484. } else {
  3485. ctx cctx_give_back_token ;
  3486. ctx lctx cctx_compile_statement ;
  3487. }
  3488. }
  3489. fun cctx_compile_function 3 {
  3490. $ctx
  3491. $type_idx
  3492. $arg_names
  3493. @ctx 2 param = ;
  3494. @type_idx 1 param = ;
  3495. @arg_names 0 param = ;
  3496. # Costruct the local context
  3497. $lctx
  3498. @lctx lctx_init = ;
  3499. lctx LCTX_RETURN_LABEL take_addr lctx ctx lctx_gen_label = ;
  3500. lctx ctx type_idx arg_names lctx_prime_stack ;
  3501. $return_type_idx
  3502. @return_type_idx ctx type_idx cctx_get_type TYPE_BASE take = ;
  3503. lctx LCTX_RETURN_TYPE_IDX take_addr return_type_idx = ;
  3504. lctx ctx lctx_gen_prologue ;
  3505. ctx lctx cctx_compile_block ;
  3506. lctx ctx lctx LCTX_RETURN_LABEL take lctx_fix_label ;
  3507. lctx ctx lctx_gen_epilogue ;
  3508. lctx lctx_destroy ;
  3509. }
  3510. fun cctx_mangle_function_type 2 {
  3511. $ctx
  3512. $type_idx
  3513. @ctx 1 param = ;
  3514. @type_idx 0 param = ;
  3515. $type
  3516. @type ctx type_idx cctx_get_type = ;
  3517. type TYPE_KIND take TYPE_KIND_FUNCTION == "cctx_mangle_function_type: not a function type" assert_msg ;
  3518. $base_idx
  3519. @base_idx type TYPE_BASE take = ;
  3520. $args
  3521. @args type TYPE_ARGS take = ;
  3522. $new_args
  3523. @new_args 4 vector_init = ;
  3524. $ellipsis
  3525. @ellipsis type TYPE_ELLIPSIS take = ;
  3526. $i
  3527. @i 0 = ;
  3528. while i args vector_size < {
  3529. $arg_idx
  3530. @arg_idx args i vector_at = ;
  3531. $arg_type
  3532. @arg_type ctx arg_idx cctx_get_type = ;
  3533. if arg_type TYPE_KIND take TYPE_KIND_ARRAY == {
  3534. $arg_base
  3535. @arg_base arg_type TYPE_BASE take = ;
  3536. @arg_idx ctx arg_base cctx_get_pointer_type = ;
  3537. }
  3538. if arg_type TYPE_KIND take TYPE_KIND_FUNCTION == {
  3539. @arg_idx ctx arg_idx cctx_get_pointer_type = ;
  3540. }
  3541. new_args arg_idx vector_push_back ;
  3542. @i i 1 + = ;
  3543. }
  3544. ctx base_idx new_args ellipsis cctx_get_function_type ret ;
  3545. }
  3546. fun assign_with_size 3 {
  3547. $loc
  3548. $value
  3549. $size
  3550. @loc 2 param = ;
  3551. @value 1 param = ;
  3552. @size 0 param = ;
  3553. if size 1 == {
  3554. loc value =c ;
  3555. ret ;
  3556. }
  3557. if size 2 == {
  3558. loc value =c ;
  3559. loc 1 + value 8 >> =c ;
  3560. ret ;
  3561. }
  3562. if size 4 == {
  3563. loc value = ;
  3564. ret ;
  3565. }
  3566. 0 "assign_with_size: invalid size" assert_msg ;
  3567. }
  3568. fun cctx_parse_initializer 3 {
  3569. $ctx
  3570. $type_idx
  3571. $loc
  3572. @ctx 2 param = ;
  3573. @type_idx 1 param = ;
  3574. @loc 0 param = ;
  3575. # Check that the type is complete
  3576. ctx type_idx cctx_type_footprint ;
  3577. $type
  3578. @type ctx type_idx cctx_get_type = ;
  3579. if type_idx is_integer_type type TYPE_KIND take TYPE_KIND_POINTER == || {
  3580. $ast
  3581. @ast ctx "," "}" ";" cctx_parse_ast3 = ;
  3582. $value
  3583. @value ctx ast ast_eval_compile = ;
  3584. ast ast_destroy ;
  3585. $size
  3586. @size ctx type_idx cctx_type_size = ;
  3587. loc value size assign_with_size ;
  3588. ret ;
  3589. }
  3590. if type TYPE_KIND take TYPE_KIND_STRUCT == {
  3591. $tok
  3592. @tok ctx cctx_get_token_or_fail = ;
  3593. tok "{" strcmp 0 == "cctx_parse_initializer: { expected" assert_msg ;
  3594. $i
  3595. @i 0 = ;
  3596. $offs
  3597. @offs type TYPE_FIELDS_OFFS take = ;
  3598. $type_idxs
  3599. @type_idxs type TYPE_FIELDS_TYPE_IDXS take = ;
  3600. while i offs vector_size < {
  3601. @tok ctx cctx_get_token_or_fail = ;
  3602. if tok "}" strcmp 0 == {
  3603. @i offs vector_size = ;
  3604. } else {
  3605. ctx cctx_give_back_token ;
  3606. ctx type_idxs i vector_at loc offs i vector_at + cctx_parse_initializer ;
  3607. @tok ctx cctx_get_token_or_fail = ;
  3608. if tok "}" strcmp 0 == {
  3609. @i offs vector_size = ;
  3610. } else {
  3611. tok "," strcmp 0 == "cctx_parse_initializer: , or } expected" assert_msg ;
  3612. @i i 1 + = ;
  3613. }
  3614. }
  3615. }
  3616. tok "}" strcmp 0 == "cctx_parse_initializer: initializer has too many entries" assert_msg ;
  3617. ret ;
  3618. }
  3619. if type TYPE_KIND take TYPE_KIND_ARRAY == {
  3620. $tok
  3621. @tok ctx cctx_get_token_or_fail = ;
  3622. tok "{" strcmp 0 == "cctx_parse_initializer: { expected" assert_msg ;
  3623. $i
  3624. @i 0 = ;
  3625. $len
  3626. @len type TYPE_LENGTH take = ;
  3627. $base_type_idx
  3628. @base_type_idx type TYPE_BASE take = ;
  3629. $base_size
  3630. @base_size ctx base_type_idx cctx_type_size = ;
  3631. while i len < {
  3632. @tok ctx cctx_get_token_or_fail = ;
  3633. if tok "}" strcmp 0 == {
  3634. @i len = ;
  3635. } else {
  3636. ctx cctx_give_back_token ;
  3637. ctx base_type_idx loc i base_size * + cctx_parse_initializer ;
  3638. @tok ctx cctx_get_token_or_fail = ;
  3639. if tok "}" strcmp 0 == {
  3640. @i len = ;
  3641. } else {
  3642. tok "," strcmp 0 == "cctx_parse_initializer: , or } expected" assert_msg ;
  3643. @i i 1 + = ;
  3644. }
  3645. }
  3646. }
  3647. tok "}" strcmp 0 == "cctx_parse_initializer: initializer has too many entries" assert_msg ;
  3648. ret ;
  3649. }
  3650. 0 "cctx_parse_initializer: not implemented" assert_msg ;
  3651. }
  3652. fun cctx_compile_line 1 {
  3653. $ctx
  3654. @ctx 0 param = ;
  3655. $tok
  3656. @tok ctx cctx_get_token_or_fail = ;
  3657. if tok "typedef" strcmp 0 == {
  3658. $type_idx
  3659. @type_idx ctx cctx_parse_type = ;
  3660. type_idx 0xffffffff != "cctx_compile_line: type expected after typedef" assert_msg ;
  3661. $cont
  3662. @cont 1 = ;
  3663. while cont {
  3664. $actual_type_idx
  3665. $name
  3666. ctx type_idx @actual_type_idx @name 0 cctx_parse_declarator "cctx_compile_line: could not parse declarator after typedef" assert_msg ;
  3667. name 0 != "cctx_compile_line: cannot define type without name" assert_msg ;
  3668. $typenames
  3669. @typenames ctx CCTX_TYPENAMES take = ;
  3670. typenames name map_has ! "cctx_compile_line: type name already defined" assert_msg ;
  3671. typenames name actual_type_idx map_set ;
  3672. @tok ctx cctx_get_token_or_fail = ;
  3673. if tok ";" strcmp 0 == {
  3674. @cont 0 = ;
  3675. } else {
  3676. tok "," strcmp 0 == "cctx_compile_line: comma expected after typedef" assert_msg ;
  3677. }
  3678. }
  3679. ret ;
  3680. } else {
  3681. ctx cctx_give_back_token ;
  3682. }
  3683. $extern
  3684. $static
  3685. @extern 0 = ;
  3686. @static 0 = ;
  3687. $cont
  3688. @cont 1 = ;
  3689. while cont {
  3690. @tok ctx cctx_get_token_or_fail = ;
  3691. if tok "extern" strcmp 0 == {
  3692. @extern 1 = ;
  3693. } else {
  3694. if tok "static" strcmp 0 == {
  3695. @static 1 = ;
  3696. } else {
  3697. ctx cctx_give_back_token ;
  3698. @cont 0 = ;
  3699. }
  3700. }
  3701. }
  3702. $type_idx
  3703. @type_idx ctx cctx_parse_type = ;
  3704. type_idx 0xffffffff != "cctx_compile: type expected" assert_msg ;
  3705. @tok ctx cctx_get_token_or_fail = ;
  3706. if tok ";" strcmp 0 == {
  3707. @cont 0 = ;
  3708. } else {
  3709. ctx cctx_give_back_token ;
  3710. @cont 1 = ;
  3711. }
  3712. while cont {
  3713. $actual_type_idx
  3714. $name
  3715. $arg_names
  3716. @arg_names 4 vector_init = ;
  3717. ctx type_idx @actual_type_idx @name arg_names cctx_parse_declarator "cctx_compile_line: could not parse declarator" assert_msg ;
  3718. $type
  3719. @type ctx actual_type_idx cctx_get_type = ;
  3720. name 0 != "cctx_compile_line: cannot instantiate variable without name" assert_msg ;
  3721. if type TYPE_KIND take TYPE_KIND_FUNCTION == {
  3722. # If it is a function, first mangle its parameters' types
  3723. @actual_type_idx ctx actual_type_idx cctx_mangle_function_type = ;
  3724. @type ctx actual_type_idx cctx_get_type = ;
  3725. # Then check if it has a body
  3726. @tok ctx cctx_get_token_or_fail = ;
  3727. if tok "{" strcmp 0 == {
  3728. # There is the body, register the global and compile the body
  3729. ctx name ctx CCTX_CURRENT_LOC take actual_type_idx cctx_add_global ;
  3730. ctx actual_type_idx arg_names cctx_compile_function ;
  3731. @cont 0 = ;
  3732. } else {
  3733. # No body, register the global with a fictious location
  3734. ctx cctx_give_back_token ;
  3735. ctx name 0xffffffff actual_type_idx cctx_add_global ;
  3736. }
  3737. } else {
  3738. if extern {
  3739. ctx name 0xffffffff actual_type_idx cctx_add_global ;
  3740. } else {
  3741. # If it is anything else, register it and allocate its size
  3742. $loc
  3743. @loc ctx CCTX_CURRENT_LOC take = ;
  3744. ctx name loc actual_type_idx cctx_add_global ;
  3745. ctx ctx actual_type_idx cctx_type_footprint cctx_emit_zeros ;
  3746. # Check if there is an initializer
  3747. @tok ctx cctx_get_token_or_fail = ;
  3748. if tok "=" strcmp 0 == {
  3749. ctx actual_type_idx loc cctx_parse_initializer ;
  3750. } else {
  3751. ctx cctx_give_back_token ;
  3752. }
  3753. }
  3754. }
  3755. arg_names vector_destroy ;
  3756. if cont {
  3757. @tok ctx cctx_get_token_or_fail = ;
  3758. if tok ";" strcmp 0 == {
  3759. @cont 0 = ;
  3760. } else {
  3761. tok "," strcmp 0 == "cctx_compile_line: comma expected" assert_msg ;
  3762. }
  3763. }
  3764. }
  3765. }
  3766. fun cctx_compile 1 {
  3767. $ctx
  3768. @ctx 0 param = ;
  3769. ctx CCTX_STAGE take_addr 0 = ;
  3770. $start_loc
  3771. @start_loc 0 = ;
  3772. $size
  3773. while ctx CCTX_STAGE take 3 < {
  3774. if ctx CCTX_VERBOSE take {
  3775. "Compilation stage " 1 platform_log ;
  3776. ctx CCTX_STAGE take 1 + itoa 1 platform_log ;
  3777. "\n" 1 platform_log ;
  3778. }
  3779. ctx CCTX_CURRENT_LOC take_addr start_loc = ;
  3780. ctx CCTX_TOKENS_POS take_addr 0 = ;
  3781. ctx CCTX_LABEL_NUM take_addr 0 = ;
  3782. ctx cctx_reset_types ;
  3783. ctx cctx_create_basic_types ;
  3784. ctx "__builtin_handles" ctx CCTX_HANDLES take vector_data TYPE_VOID_PTR cctx_add_global ;
  3785. while ctx cctx_is_eof ! {
  3786. ctx cctx_compile_line ;
  3787. }
  3788. if ctx CCTX_STAGE take 0 == {
  3789. @size ctx CCTX_CURRENT_LOC take start_loc - = ;
  3790. @start_loc size platform_allocate = ;
  3791. } else {
  3792. ctx CCTX_CURRENT_LOC take start_loc - size == "cctx_compile: error 1" assert_msg ;
  3793. }
  3794. ctx CCTX_STAGE take_addr ctx CCTX_STAGE take 1 + = ;
  3795. }
  3796. if ctx CCTX_VERBOSE take {
  3797. "Compiled program has size " 1 platform_log ;
  3798. size itoa 1 platform_log ;
  3799. " and starts at " 1 platform_log ;
  3800. start_loc itoa 1 platform_log ;
  3801. "\n" 1 platform_log ;
  3802. "Compiled dump:\n" 1 platform_log ;
  3803. start_loc size dump_mem ;
  3804. "\n" 1 platform_log ;
  3805. }
  3806. }
  3807. fun parse_c 1 {
  3808. $filename
  3809. @filename 0 param = ;
  3810. # Preprocessing
  3811. $ctx
  3812. @ctx ppctx_init = ;
  3813. ctx filename ppctx_set_base_filename ;
  3814. $tokens
  3815. @tokens 4 vector_init = ;
  3816. tokens ctx filename preproc_file ;
  3817. @tokens tokens remove_whites = ;
  3818. "Finished preprocessing\n" 1 platform_log ;
  3819. tokens print_token_list ;
  3820. # Compilation
  3821. $cctx
  3822. @cctx tokens cctx_init = ;
  3823. cctx cctx_compile ;
  3824. # Debug output
  3825. "TYPES TABLE\n" 1 platform_log ;
  3826. cctx cctx_dump_types ;
  3827. "TYPE NAMES TABLE\n" 1 platform_log ;
  3828. cctx cctx_dump_typenames ;
  3829. "GLOBALS TABLE\n" 1 platform_log ;
  3830. cctx cctx_dump_globals ;
  3831. # Try to execute the code
  3832. "Executing compiled code...\n" 1 platform_log ;
  3833. $main_global
  3834. @main_global cctx "_start" cctx_get_global = ;
  3835. $main_addr
  3836. @main_addr main_global GLOBAL_LOC take = ;
  3837. $arg
  3838. @arg "_main" = ;
  3839. $res
  3840. @res @arg 1 main_addr \2 = ;
  3841. "It returned " 1 platform_log ;
  3842. res itoa 1 platform_log ;
  3843. "\n" 1 platform_log ;
  3844. # Cleanup
  3845. tokens free_vect_of_ptrs ;
  3846. cctx cctx_destroy ;
  3847. ctx ppctx_destroy ;
  3848. }