cc_core.c 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * Copyright (C) 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. * Copyright (C) 2020 deesix <deesix@tuta.io>
  4. * Copyright (C) 2021 Andrius Štikonas <andrius@stikonas.eu>
  5. * Copyright (C) 2025 Gtker
  6. * This file is part of M2-Planet.
  7. *
  8. * M2-Planet is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * M2-Planet is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "cc.h"
  22. #include "gcc_req.h"
  23. #include <stdint.h>
  24. /* Global lists */
  25. struct token_list* global_symbol_list;
  26. struct token_list* global_function_list;
  27. /* Core lists for this file */
  28. struct token_list* function;
  29. /* What we are currently working on */
  30. struct type* current_target;
  31. char* break_target_head;
  32. char* break_target_func;
  33. char* break_target_num;
  34. char* continue_target_head;
  35. struct token_list* break_frame;
  36. int current_count;
  37. int Address_of;
  38. /* Imported functions */
  39. char* int2str(int x, int base, int signed_p);
  40. int strtoint(char *a);
  41. char* parse_string(char* string);
  42. int escape_lookup(char* c);
  43. void require(int bool, char* error);
  44. struct token_list* reverse_list(struct token_list* head);
  45. struct type *mirror_type(struct type *source);
  46. struct type* new_function_pointer_typedef(char* name);
  47. struct type* add_primitive(struct type* a);
  48. int global_static_array(struct type*, char*);
  49. void declare_global_variable(struct type* type_size, struct token_list* variable);
  50. struct type* type_name(void);
  51. struct token_list* emit(char *s, struct token_list* head)
  52. {
  53. struct token_list* t = calloc(1, sizeof(struct token_list));
  54. require(NULL != t, "Exhausted memory while generating token to emit\n");
  55. t->next = head;
  56. t->s = s;
  57. return t;
  58. }
  59. void emit_out(char* s)
  60. {
  61. output_list = emit(s, output_list);
  62. }
  63. char* emit_string;
  64. int emit_string_index;
  65. void emit_to_string(char* s)
  66. {
  67. emit_string_index = emit_string_index + copy_string(emit_string + emit_string_index, s, MAX_STRING - emit_string_index);
  68. }
  69. void reset_emit_string(void)
  70. {
  71. /* Emitted strings are just added to a linked list so we need a new one each time. */
  72. emit_string = calloc(MAX_STRING, sizeof(char));
  73. emit_string_index = 0;
  74. }
  75. char* register_from_string(int reg)
  76. {
  77. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  78. {
  79. if(reg == REGISTER_ZERO) return "0";
  80. else if(reg == REGISTER_ONE) return "1";
  81. else if(reg == REGISTER_EMIT_TEMP) return "10";
  82. else if(reg == REGISTER_UNUSED2) return "11";
  83. else if(reg == REGISTER_LOCALS) return "12";
  84. else if(reg == REGISTER_TEMP) return "13";
  85. else if(reg == REGISTER_BASE) return "14";
  86. else if(reg == REGISTER_STACK) return "15";
  87. }
  88. else if(X86 == Architecture)
  89. {
  90. if(reg == REGISTER_ZERO) return "eax";
  91. else if(reg == REGISTER_ONE) return "ebx";
  92. else if(reg == REGISTER_TEMP) return "edi";
  93. else if(reg == REGISTER_BASE) return "ebp";
  94. else if(reg == REGISTER_STACK) return "esp";
  95. else if(reg == REGISTER_LOCALS) return "esi";
  96. else if(reg == REGISTER_EMIT_TEMP) return "ecx";
  97. else if(reg == REGISTER_UNUSED2) return "edx";
  98. }
  99. else if(AMD64 == Architecture)
  100. {
  101. if(reg == REGISTER_ZERO) return "rax";
  102. else if(reg == REGISTER_ONE) return "rbx";
  103. else if(reg == REGISTER_TEMP) return "rdi";
  104. else if(reg == REGISTER_BASE) return "rbp";
  105. else if(reg == REGISTER_STACK) return "rsp";
  106. else if(reg == REGISTER_LOCALS) return "r13";
  107. else if(reg == REGISTER_EMIT_TEMP) return "r14";
  108. else if(reg == REGISTER_UNUSED2) return "r15";
  109. }
  110. else if(ARMV7L == Architecture)
  111. {
  112. if(reg == REGISTER_ZERO) return "R0";
  113. else if(reg == REGISTER_ONE) return "R1";
  114. else if(reg == REGISTER_LOCALS) return "R8";
  115. else if(reg == REGISTER_EMIT_TEMP) return "R9";
  116. else if(reg == REGISTER_UNUSED2) return "R10";
  117. else if(reg == REGISTER_TEMP) return "R11";
  118. else if(reg == REGISTER_BASE) return "BP";
  119. else if(reg == REGISTER_RETURN) return "LR";
  120. else if(reg == REGISTER_STACK) return "SP";
  121. }
  122. else if(AARCH64 == Architecture)
  123. {
  124. if(reg == REGISTER_ZERO) return "X0";
  125. else if(reg == REGISTER_ONE) return "X1";
  126. else if(reg == REGISTER_LOCALS) return "X13";
  127. else if(reg == REGISTER_EMIT_TEMP) return "X14";
  128. else if(reg == REGISTER_UNUSED2) return "X15";
  129. else if(reg == REGISTER_TEMP) return "X16";
  130. else if(reg == REGISTER_BASE) return "BP";
  131. else if(reg == REGISTER_RETURN) return "LR";
  132. else if(reg == REGISTER_STACK) return "SP";
  133. }
  134. else if(RISCV32 == Architecture || RISCV64 == Architecture)
  135. {
  136. if(reg == REGISTER_ZERO) return "a0";
  137. else if(reg == REGISTER_ONE) return "a1";
  138. else if(reg == REGISTER_LOCALS) return "t3";
  139. else if(reg == REGISTER_EMIT_TEMP) return "t4";
  140. else if(reg == REGISTER_UNUSED2) return "t5";
  141. else if(reg == REGISTER_TEMP) return "tp";
  142. else if(reg == REGISTER_BASE) return "fp";
  143. else if(reg == REGISTER_RETURN) return "ra";
  144. else if(reg == REGISTER_STACK) return "sp";
  145. }
  146. fputs("PROGRAMMING ERROR: Invalid register passed to register_from_string: '", stderr);
  147. fputs(int2str(reg, 10, FALSE), stderr);
  148. fputs("' for architecture '", stderr);
  149. fputs(int2str(Architecture, 10, FALSE), stderr);
  150. fputs("'\n.", stderr);
  151. exit(EXIT_FAILURE);
  152. }
  153. void emit_load_named_immediate(int reg, char* prefix, char* name, char* note)
  154. {
  155. char* reg_name = register_from_string(reg);
  156. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  157. {
  158. emit_out("LOADR R");
  159. emit_out(reg_name);
  160. emit_out(" 4\nJUMP 4\n&");
  161. emit_out(prefix);
  162. emit_out(name);
  163. }
  164. else if(X86 == Architecture)
  165. {
  166. emit_out("mov_");
  167. emit_out(reg_name);
  168. emit_out(", &");
  169. emit_out(prefix);
  170. emit_out(name);
  171. }
  172. else if(AMD64 == Architecture)
  173. {
  174. emit_out("lea_");
  175. emit_out(reg_name);
  176. emit_out(",[rip+DWORD] %");
  177. emit_out(prefix);
  178. emit_out(name);
  179. }
  180. else if(ARMV7L == Architecture)
  181. {
  182. emit_out("!0 ");
  183. emit_out(reg_name);
  184. emit_out(" LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&");
  185. emit_out(prefix);
  186. emit_out(name);
  187. }
  188. else if(AARCH64 == Architecture)
  189. {
  190. emit_out("LOAD_W");
  191. /* Normal register starts with X for 64bit wide
  192. * but we need W. */
  193. emit_out(reg_name + 1);
  194. emit_out("_AHEAD\nSKIP_32_DATA\n&");
  195. emit_out(prefix);
  196. emit_out(name);
  197. }
  198. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  199. {
  200. emit_out("rd_");
  201. emit_out(reg_name);
  202. emit_out(" ~");
  203. emit_out(prefix);
  204. emit_out(name);
  205. emit_out(" auipc\n");
  206. emit_out("rd_");
  207. emit_out(reg_name);
  208. emit_out(" rs1_");
  209. emit_out(reg_name);
  210. emit_out(" !");
  211. emit_out(prefix);
  212. emit_out(name);
  213. emit_out(" addi");
  214. }
  215. if(note == NULL)
  216. {
  217. emit_out("\n");
  218. }
  219. else
  220. {
  221. emit_out(" # ");
  222. emit_out(note);
  223. emit_out("\n");
  224. }
  225. }
  226. void write_load_immediate(int reg, int value, char* note)
  227. {
  228. char* reg_name = register_from_string(reg);
  229. char* value_string = int2str(value, 10, TRUE);
  230. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  231. {
  232. if((32767 > value) && (value > -32768))
  233. {
  234. emit_to_string("LOADI R");
  235. emit_to_string(reg_name);
  236. emit_to_string(" ");
  237. emit_to_string(value_string);
  238. }
  239. else
  240. {
  241. emit_to_string("LOADR R");
  242. emit_to_string(reg_name);
  243. emit_to_string(" 4\nJUMP 4\n%");
  244. emit_to_string(int2str(value, 10, TRUE));
  245. }
  246. }
  247. else if(X86 == Architecture || AMD64 == Architecture)
  248. {
  249. if(value == 0)
  250. {
  251. /* This is the recommended way of zeroing a register on x86/amd64.
  252. * xor eax, eax (32 bit registers) for both x86 and amd64 since it
  253. * takes up a byte less and still zeros the register. */
  254. emit_to_string("xor_e");
  255. /* amd64 register starts with r but we need it to start with e */
  256. emit_to_string(reg_name + 1);
  257. emit_to_string(",e");
  258. emit_to_string(reg_name + 1);
  259. }
  260. else
  261. {
  262. emit_to_string("mov_");
  263. emit_to_string(reg_name);
  264. emit_to_string(", %");
  265. emit_to_string(value_string);
  266. }
  267. }
  268. else if(ARMV7L == Architecture)
  269. {
  270. if((127 >= value) && (value >= -128))
  271. {
  272. emit_to_string("!");
  273. emit_to_string(value_string);
  274. emit_to_string(" ");
  275. emit_to_string(reg_name);
  276. emit_to_string(" LOADI8_ALWAYS");
  277. }
  278. else
  279. {
  280. emit_to_string("!0 ");
  281. emit_to_string(reg_name);
  282. emit_to_string(" LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n%");
  283. emit_to_string(value_string);
  284. }
  285. }
  286. else if(AARCH64 == Architecture)
  287. {
  288. if((value == 0 && (reg == 0 || reg == 1)) || (value == 1 && reg == 0))
  289. {
  290. emit_to_string("SET_");
  291. emit_to_string(reg_name);
  292. emit_to_string("_TO_");
  293. emit_to_string(value_string);
  294. }
  295. else
  296. {
  297. emit_to_string("LOAD_W");
  298. /* Normal register starts with X for 64bit wide
  299. * but we need W. */
  300. emit_to_string(reg_name + 1);
  301. emit_to_string("_AHEAD\nSKIP_32_DATA\n%");
  302. emit_to_string(value_string);
  303. }
  304. }
  305. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  306. {
  307. if((2047 >= value) && (value >= -2048))
  308. {
  309. emit_to_string("rd_");
  310. emit_to_string(reg_name);
  311. emit_to_string(" !");
  312. emit_to_string(value_string);
  313. emit_to_string(" addi");
  314. }
  315. else if (0 == (value >> 30))
  316. {
  317. emit_to_string("rd_");
  318. emit_to_string(reg_name);
  319. emit_to_string(" ~");
  320. emit_to_string(value_string);
  321. emit_to_string(" lui\n");
  322. emit_to_string("rd_");
  323. emit_to_string(reg_name);
  324. emit_to_string(" rs1_");
  325. emit_to_string(reg_name);
  326. emit_to_string(" !");
  327. emit_to_string(value_string);
  328. emit_to_string(" addi");
  329. }
  330. else
  331. {
  332. int high = value >> 30;
  333. char* high_string = int2str(high, 10, TRUE);
  334. int low = ((value >> 30) << 30) ^ value;
  335. char* low_string = int2str(low, 10, TRUE);
  336. emit_to_string("rd_");
  337. emit_to_string(reg_name);
  338. emit_to_string(" ~");
  339. emit_to_string(high_string);
  340. emit_to_string(" lui\n");
  341. emit_to_string("rd_");
  342. emit_to_string(reg_name);
  343. emit_to_string(" rs1_");
  344. emit_to_string(reg_name);
  345. emit_to_string(" !");
  346. emit_to_string(high_string);
  347. emit_to_string(" addi\n");
  348. emit_to_string("rd_");
  349. emit_to_string(reg_name);
  350. emit_to_string(" rs1_");
  351. emit_to_string(reg_name);
  352. emit_to_string(" rs2_x30 slli\n");
  353. emit_to_string("rd_t1 ~");
  354. emit_to_string(low_string);
  355. emit_to_string(" lui\n");
  356. emit_to_string("rd_t1 rs1_t1 !");
  357. emit_to_string(low_string);
  358. emit_to_string(" addi\n");
  359. emit_to_string("rd_");
  360. emit_to_string(reg_name);
  361. emit_to_string(" rs1_");
  362. emit_to_string(reg_name);
  363. emit_to_string(" rs2_t1 or");
  364. }
  365. }
  366. if(note == NULL)
  367. {
  368. emit_to_string("\n");
  369. }
  370. else
  371. {
  372. emit_to_string(" # ");
  373. emit_to_string(note);
  374. emit_to_string("\n");
  375. }
  376. }
  377. void emit_load_immediate(int reg, int value, char* note)
  378. {
  379. reset_emit_string();
  380. write_load_immediate(reg, value, note);
  381. emit_out(emit_string);
  382. }
  383. /* Adds destination and source and places result in destination */
  384. void write_add(int destination_reg, int source_reg, char* note)
  385. {
  386. char* destination_name = register_from_string(destination_reg);
  387. char* source_name = register_from_string(source_reg);
  388. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  389. {
  390. emit_to_string("ADD R");
  391. emit_to_string(destination_name);
  392. emit_to_string(" R");
  393. emit_to_string(destination_name);
  394. emit_to_string(" R");
  395. emit_to_string(source_name);
  396. }
  397. else if(X86 == Architecture || AMD64 == Architecture)
  398. {
  399. emit_to_string("add_");
  400. emit_to_string(destination_name);
  401. emit_to_string(",");
  402. emit_to_string(source_name);
  403. }
  404. else if(ARMV7L == Architecture)
  405. {
  406. emit_to_string("'0' ");
  407. emit_to_string(destination_name);
  408. emit_to_string(" ");
  409. emit_to_string(destination_name);
  410. emit_to_string(" ADD ");
  411. emit_to_string(source_name);
  412. emit_to_string(" ARITH2_ALWAYS");
  413. }
  414. else if(AARCH64 == Architecture)
  415. {
  416. emit_to_string("ADD_");
  417. emit_to_string(destination_name);
  418. emit_to_string("_");
  419. emit_to_string(source_name);
  420. emit_to_string("_");
  421. emit_to_string(destination_name);
  422. }
  423. else if(RISCV32 == Architecture || RISCV64 == Architecture)
  424. {
  425. emit_to_string("rd_");
  426. emit_to_string(destination_name);
  427. emit_to_string(" rs1_");
  428. emit_to_string(source_name);
  429. emit_to_string(" rs2_");
  430. emit_to_string(destination_name);
  431. emit_to_string(" add");
  432. }
  433. if(note == NULL)
  434. {
  435. emit_to_string("\n");
  436. }
  437. else
  438. {
  439. emit_to_string(" # ");
  440. emit_to_string(note);
  441. emit_to_string("\n");
  442. }
  443. }
  444. void emit_add(int destination_reg, int source_reg, char* note)
  445. {
  446. reset_emit_string();
  447. write_add(destination_reg, source_reg, note);
  448. emit_out(emit_string);
  449. }
  450. void write_add_immediate(int reg, int value, char* note)
  451. {
  452. write_load_immediate(REGISTER_EMIT_TEMP, value, note);
  453. write_add(reg, REGISTER_EMIT_TEMP, note);
  454. }
  455. void emit_add_immediate(int reg, int value, char* note)
  456. {
  457. reset_emit_string();
  458. write_add_immediate(reg, value, note);
  459. emit_out(emit_string);
  460. }
  461. /* Subtracts destination and source and places result in destination */
  462. void write_sub(int destination_reg, int source_reg, char* note)
  463. {
  464. char* destination_name = register_from_string(destination_reg);
  465. char* source_name = register_from_string(source_reg);
  466. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  467. {
  468. emit_to_string("SUB R");
  469. emit_to_string(destination_name);
  470. emit_to_string(" R");
  471. emit_to_string(destination_name);
  472. emit_to_string(" R");
  473. emit_to_string(source_name);
  474. }
  475. else if(X86 == Architecture || AMD64 == Architecture)
  476. {
  477. emit_to_string("sub_");
  478. emit_to_string(destination_name);
  479. emit_to_string(",");
  480. emit_to_string(source_name);
  481. emit_to_string("\n");
  482. }
  483. else if(ARMV7L == Architecture)
  484. {
  485. emit_to_string("'0' ");
  486. emit_to_string(destination_name);
  487. emit_to_string(" ");
  488. emit_to_string(destination_name);
  489. emit_to_string(" SUB ");
  490. emit_to_string(source_name);
  491. emit_to_string(" ARITH2_ALWAYS");
  492. }
  493. else if(AARCH64 == Architecture)
  494. {
  495. emit_to_string("SUB_");
  496. emit_to_string(destination_name);
  497. emit_to_string("_");
  498. emit_to_string(destination_name);
  499. emit_to_string("_");
  500. emit_to_string(source_name);
  501. }
  502. else if(RISCV32 == Architecture || RISCV64 == Architecture)
  503. {
  504. emit_to_string("rd_");
  505. emit_to_string(destination_name);
  506. emit_to_string(" rs1_");
  507. emit_to_string(destination_name);
  508. emit_to_string(" rs2_");
  509. emit_to_string(source_name);
  510. emit_to_string(" sub");
  511. }
  512. if(note == NULL)
  513. {
  514. emit_to_string("\n");
  515. }
  516. else
  517. {
  518. emit_to_string(" # ");
  519. emit_to_string(note);
  520. emit_to_string("\n");
  521. }
  522. }
  523. void emit_sub(int destination_reg, int source_reg, char* note)
  524. {
  525. reset_emit_string();
  526. write_sub(destination_reg, source_reg, note);
  527. emit_out(emit_string);
  528. }
  529. void write_sub_immediate(int reg, int value, char* note)
  530. {
  531. write_load_immediate(REGISTER_EMIT_TEMP, value, note);
  532. write_sub(reg, REGISTER_EMIT_TEMP, note);
  533. }
  534. void emit_sub_immediate(int reg, int value, char* note)
  535. {
  536. reset_emit_string();
  537. write_sub_immediate(reg, value, note);
  538. emit_out(emit_string);
  539. }
  540. void emit_mul_into_register_zero(int reg, char* note)
  541. {
  542. char* reg_name = register_from_string(reg);
  543. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  544. {
  545. emit_out("MULU R0 R");
  546. emit_out(reg_name);
  547. emit_out(" R0");
  548. }
  549. else if(X86 == Architecture || AMD64 == Architecture)
  550. {
  551. emit_out("mul_");
  552. emit_out(reg_name);
  553. }
  554. else if(ARMV7L == Architecture)
  555. {
  556. emit_out("'9' R0 '0' ");
  557. emit_out(reg_name);
  558. emit_out(" MUL R0 ARITH2_ALWAYS");
  559. }
  560. else if(AARCH64 == Architecture)
  561. {
  562. emit_out("MUL_X0_");
  563. emit_out(reg_name);
  564. emit_out("_X0");
  565. }
  566. else if(RISCV32 == Architecture || RISCV64 == Architecture)
  567. {
  568. emit_out("rd_a0 rs1_");
  569. emit_out(reg_name);
  570. emit_out(" rs2_a0 mul");
  571. }
  572. if(note == NULL)
  573. {
  574. emit_out("\n");
  575. }
  576. else
  577. {
  578. emit_out(" # ");
  579. emit_out(note);
  580. emit_out("\n");
  581. }
  582. }
  583. void emit_mul_register_zero_with_immediate(int value, char* note)
  584. {
  585. emit_load_immediate(REGISTER_EMIT_TEMP, value, note);
  586. emit_mul_into_register_zero(REGISTER_EMIT_TEMP, note);
  587. }
  588. void emit_move(int destination_reg, int source_reg, char* note)
  589. {
  590. char* destination_name = register_from_string(destination_reg);
  591. char* source_name = register_from_string(source_reg);
  592. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  593. {
  594. emit_out("COPY R");
  595. emit_out(destination_name);
  596. emit_out(" R");
  597. emit_out(source_name);
  598. }
  599. else if(X86 == Architecture || AMD64 == Architecture)
  600. {
  601. emit_out("mov_");
  602. emit_out(destination_name);
  603. emit_out(",");
  604. emit_out(source_name);
  605. }
  606. else if(ARMV7L == Architecture)
  607. {
  608. emit_out("'0' ");
  609. emit_out(source_name);
  610. emit_out(" ");
  611. emit_out(destination_name);
  612. emit_out(" NO_SHIFT MOVE_ALWAYS");
  613. }
  614. else if(AARCH64 == Architecture)
  615. {
  616. emit_out("SET_");
  617. emit_out(destination_name);
  618. emit_out("_FROM_");
  619. emit_out(source_name);
  620. }
  621. else if(RISCV32 == Architecture || RISCV64 == Architecture)
  622. {
  623. emit_out("rd_");
  624. emit_out(destination_name);
  625. emit_out(" rs1_");
  626. emit_out(source_name);
  627. emit_out(" mv");
  628. }
  629. if(note == NULL)
  630. {
  631. emit_out("\n");
  632. }
  633. else
  634. {
  635. emit_out(" # ");
  636. emit_out(note);
  637. emit_out("\n");
  638. }
  639. }
  640. void emit_load_relative_to_register(int destination, int offset_register, int value, char* note)
  641. {
  642. char* destination_name = register_from_string(destination);
  643. char* offset_name = register_from_string(offset_register);
  644. char* value_string = int2str(value, 10, TRUE);
  645. int absolute_value = value;
  646. if(value < 0)
  647. {
  648. absolute_value = -absolute_value;
  649. }
  650. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  651. {
  652. emit_out("ADDI R");
  653. emit_out(destination_name);
  654. emit_out(" R");
  655. emit_out(offset_name);
  656. emit_out(" ");
  657. emit_out(value_string);
  658. }
  659. else if((X86 == Architecture) || (AMD64 == Architecture))
  660. {
  661. emit_out("lea_");
  662. emit_out(destination_name);
  663. emit_out(",[");
  664. emit_out(offset_name);
  665. emit_out("+DWORD] %");
  666. emit_out(value_string);
  667. }
  668. else if(ARMV7L == Architecture)
  669. {
  670. if((127 >= value) && (value >= -128))
  671. {
  672. emit_out("!");
  673. emit_out(value_string);
  674. emit_out(" ");
  675. emit_out(destination_name);
  676. emit_out(" SUB ");
  677. emit_out(offset_name);
  678. emit_out(" ARITH_ALWAYS\n");
  679. }
  680. else
  681. {
  682. emit_move(destination, offset_register, note);
  683. emit_sub_immediate(destination, value, note);
  684. }
  685. }
  686. else if(AARCH64 == Architecture)
  687. {
  688. emit_move(destination, offset_register, note);
  689. emit_sub_immediate(destination, absolute_value, note);
  690. }
  691. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  692. {
  693. if((2047 >= value) && (value >= -2048))
  694. {
  695. emit_out("rd_");
  696. emit_out(destination_name);
  697. emit_out(" rs1_");
  698. emit_out(offset_name);
  699. emit_out(" !");
  700. emit_out(value_string);
  701. emit_out(" addi");
  702. }
  703. else
  704. {
  705. emit_out("rd_");
  706. emit_out(destination_name);
  707. emit_out(" ~");
  708. emit_out(value_string);
  709. emit_out(" lui\n");
  710. emit_out("rd_");
  711. emit_out(destination_name);
  712. emit_out(" rs1_");
  713. emit_out(offset_name);
  714. emit_out(" !");
  715. emit_out(value_string);
  716. emit_out(" addi");
  717. }
  718. }
  719. if(note == NULL)
  720. {
  721. emit_out("\n");
  722. }
  723. else
  724. {
  725. emit_out(" # ");
  726. emit_out(note);
  727. emit_out("\n");
  728. }
  729. }
  730. void emit_dereference(int reg, char* note)
  731. {
  732. char* reg_name = register_from_string(reg);
  733. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  734. {
  735. emit_out("LOAD R");
  736. emit_out(reg_name);
  737. emit_out(" R");
  738. emit_out(reg_name);
  739. emit_out(" 0");
  740. }
  741. else if(X86 == Architecture || AMD64 == Architecture)
  742. {
  743. emit_out("mov_");
  744. emit_out(reg_name);
  745. emit_out(",[");
  746. emit_out(reg_name);
  747. emit_out("]");
  748. }
  749. else if(ARMV7L == Architecture)
  750. {
  751. emit_out("!0 ");
  752. emit_out(reg_name);
  753. emit_out(" LOAD32 ");
  754. emit_out(reg_name);
  755. emit_out(" MEMORY");
  756. }
  757. else if(AARCH64 == Architecture)
  758. {
  759. emit_out("DEREF_");
  760. emit_out(reg_name);
  761. }
  762. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  763. {
  764. emit_out("rd_");
  765. emit_out(reg_name);
  766. emit_out(" rs1_");
  767. emit_out(reg_name);
  768. if(RISCV32 == Architecture) emit_out(" lw");
  769. else emit_out(" ld");
  770. }
  771. if(note == NULL)
  772. {
  773. emit_out("\n");
  774. }
  775. else
  776. {
  777. emit_out(" # ");
  778. emit_out(note);
  779. emit_out("\n");
  780. }
  781. }
  782. void emit_push(int reg, char* note)
  783. {
  784. char* reg_name = register_from_string(reg);
  785. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  786. {
  787. emit_out("PUSHR R");
  788. emit_out(reg_name);
  789. emit_out(" R15");
  790. }
  791. else if(X86 == Architecture || AMD64 == Architecture)
  792. {
  793. emit_out("push_");
  794. emit_out(reg_name);
  795. }
  796. else if(ARMV7L == Architecture)
  797. {
  798. emit_out("{");
  799. emit_out(reg_name);
  800. emit_out("} PUSH_ALWAYS");
  801. }
  802. else if(AARCH64 == Architecture)
  803. {
  804. emit_out("PUSH_");
  805. emit_out(reg_name);
  806. }
  807. else if(RISCV32 == Architecture)
  808. {
  809. emit_out("rd_sp rs1_sp !-4 addi\nrs1_sp rs2_");
  810. emit_out(reg_name);
  811. emit_out(" sw");
  812. }
  813. else if(RISCV64 == Architecture)
  814. {
  815. emit_out("rd_sp rs1_sp !-8 addi\nrs1_sp rs2_");
  816. emit_out(reg_name);
  817. emit_out(" sd");
  818. }
  819. if(note == NULL)
  820. {
  821. emit_out("\n");
  822. }
  823. else
  824. {
  825. emit_out(" # ");
  826. emit_out(note);
  827. emit_out("\n");
  828. }
  829. }
  830. void emit_pop(int reg, char* note)
  831. {
  832. char* reg_name = register_from_string(reg);
  833. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  834. {
  835. emit_out("POPR R");
  836. emit_out(reg_name);
  837. emit_out(" R15");
  838. }
  839. else if(X86 == Architecture || AMD64 == Architecture)
  840. {
  841. emit_out("pop_");
  842. emit_out(reg_name);
  843. }
  844. else if(ARMV7L == Architecture)
  845. {
  846. emit_out("{");
  847. emit_out(reg_name);
  848. emit_out("} POP_ALWAYS");
  849. }
  850. else if(AARCH64 == Architecture)
  851. {
  852. emit_out("POP_");
  853. emit_out(reg_name);
  854. }
  855. else if(RISCV32 == Architecture)
  856. {
  857. emit_out("rd_");
  858. emit_out(reg_name);
  859. emit_out(" rs1_sp lw\nrd_sp rs1_sp !4 addi");
  860. }
  861. else if(RISCV64 == Architecture)
  862. {
  863. emit_out("rd_");
  864. emit_out(reg_name);
  865. emit_out(" rs1_sp ld\nrd_sp rs1_sp !8 addi");
  866. }
  867. if(note == NULL)
  868. {
  869. emit_out("\n");
  870. }
  871. else
  872. {
  873. emit_out(" # ");
  874. emit_out(note);
  875. emit_out("\n");
  876. }
  877. }
  878. int type_is_pointer(struct type* type_size)
  879. {
  880. return type_size->type != type_size || (type_size->options & TO_FUNCTION_POINTER);
  881. }
  882. int type_is_struct_or_union(struct type* type_size)
  883. {
  884. return type_size->members != NULL;
  885. }
  886. struct token_list* uniqueID(char* s, struct token_list* l, char* num)
  887. {
  888. l = emit("\n", emit(num, emit("_", emit(s, l))));
  889. return l;
  890. }
  891. void uniqueID_out(char* s, char* num)
  892. {
  893. output_list = uniqueID(s, output_list, num);
  894. }
  895. char* create_unique_id(char* prefix, char* s, char* num)
  896. {
  897. char* buf = calloc(MAX_STRING, sizeof(char));
  898. int written = copy_string(buf, prefix, MAX_STRING);
  899. written = copy_string(buf + written, s, MAX_STRING - written) + written;
  900. written = copy_string(buf + written, "_", MAX_STRING - written) + written;
  901. copy_string(buf + written, num, MAX_STRING - written);
  902. return buf;
  903. }
  904. struct token_list* sym_declare(char *s, struct type* t, struct token_list* list, int options)
  905. {
  906. struct token_list* a = calloc(1, sizeof(struct token_list));
  907. require(NULL != a, "Exhausted memory while attempting to declare a symbol\n");
  908. a->next = list;
  909. a->s = s;
  910. a->type = t;
  911. a->array_modifier = 1;
  912. a->options = options;
  913. return a;
  914. }
  915. struct token_list* sym_lookup(char *s, struct token_list* symbol_list)
  916. {
  917. struct token_list* i;
  918. for(i = symbol_list; NULL != i; i = i->next)
  919. {
  920. if(match(i->s, s)) return i;
  921. }
  922. return NULL;
  923. }
  924. struct token_list* static_variable_lookup(char* s)
  925. {
  926. struct static_variable_list* statics = function_static_variables_list;
  927. while(statics != NULL)
  928. {
  929. if(match(s, statics->local_variable_name))
  930. {
  931. return statics->global_variable;
  932. }
  933. statics = statics->next;
  934. }
  935. return NULL;
  936. }
  937. void line_error_token(struct token_list *token)
  938. {
  939. if(NULL == token)
  940. {
  941. fputs("EOF reached inside of line_error\n", stderr);
  942. fputs("problem at end of file\n", stderr);
  943. return;
  944. }
  945. fputs(token->filename, stderr);
  946. fputs(":", stderr);
  947. fputs(int2str(token->linenumber, 10, TRUE), stderr);
  948. fputs(":", stderr);
  949. }
  950. void line_error(void)
  951. {
  952. line_error_token(global_token);
  953. }
  954. /* Checks if current global_token is NULL and exits if it is. */
  955. void require_token(void)
  956. {
  957. if(NULL == global_token)
  958. {
  959. line_error_token(global_token);
  960. exit(EXIT_FAILURE);
  961. }
  962. }
  963. /* Advances token and checks for NULL. */
  964. void require_extra_token(void)
  965. {
  966. global_token = global_token->next;
  967. require_token();
  968. }
  969. void require_match(char* message, char* required)
  970. {
  971. if(NULL == global_token)
  972. {
  973. line_error();
  974. fputs("EOF reached inside of require match\n", stderr);
  975. fputs("problem at end of file\n", stderr);
  976. fputs(message, stderr);
  977. exit(EXIT_FAILURE);
  978. }
  979. if(!match(global_token->s, required))
  980. {
  981. line_error();
  982. fputs(message, stderr);
  983. exit(EXIT_FAILURE);
  984. }
  985. global_token = global_token->next;
  986. }
  987. void maybe_bootstrap_error(char* feature)
  988. {
  989. if (BOOTSTRAP_MODE)
  990. {
  991. line_error();
  992. fputs(feature, stderr);
  993. fputs(" is not supported in --bootstrap-mode\n", stderr);
  994. exit(EXIT_FAILURE);
  995. }
  996. }
  997. int unary_expr_sizeof(void);
  998. int constant_unary_expression(void)
  999. {
  1000. if('-' == global_token->s[0])
  1001. {
  1002. require_extra_token();
  1003. return -constant_unary_expression();
  1004. }
  1005. else if('+' == global_token->s[0])
  1006. {
  1007. require_extra_token();
  1008. return constant_unary_expression();
  1009. }
  1010. else if(match("sizeof", global_token->s))
  1011. {
  1012. return unary_expr_sizeof();
  1013. }
  1014. else if(global_token->s[0] == '\'')
  1015. {
  1016. int val = escape_lookup(global_token->s + 1);
  1017. global_token = global_token->next;
  1018. return val;
  1019. }
  1020. else if(global_token->s[0] == ':')
  1021. {
  1022. /* Switch labels have the : prepended to the front like labels.
  1023. * This is just a normal integer or constant.*/
  1024. struct token_list* lookup = sym_lookup(global_token->s + 1, global_constant_list);
  1025. if(lookup != NULL)
  1026. {
  1027. return strtoint(lookup->arguments->s);
  1028. }
  1029. else
  1030. {
  1031. return strtoint(global_token->s + 1);
  1032. }
  1033. }
  1034. else if(in_set(global_token->s[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"))
  1035. {
  1036. struct token_list* lookup = sym_lookup(global_token->s, global_constant_list);
  1037. if(lookup != NULL)
  1038. {
  1039. require_extra_token();
  1040. return strtoint(lookup->arguments->s);
  1041. }
  1042. else
  1043. {
  1044. line_error();
  1045. fputs("Unable to find symbol '", stderr);
  1046. fputs(global_token->s, stderr);
  1047. fputs("' for use in constant expression.\n", stderr);
  1048. exit(EXIT_FAILURE);
  1049. }
  1050. }
  1051. else if(in_set(global_token->s[0], "0123456789"))
  1052. {
  1053. require_extra_token();
  1054. return strtoint(global_token->prev->s);
  1055. }
  1056. line_error();
  1057. fputs("Invalid token '", stderr);
  1058. fputs(global_token->s, stderr);
  1059. fputs("' used in constant_expression_term.\n", stderr);
  1060. exit(EXIT_FAILURE);
  1061. }
  1062. /* global_token should start on the first part of the expression
  1063. * and it will end one token past the end of the expression. */
  1064. int constant_expression(void)
  1065. {
  1066. /* We go from the highest precedence operators down to the lowest */
  1067. int lhs = constant_unary_expression();
  1068. if(global_token->s[0] == '+')
  1069. {
  1070. require_extra_token();
  1071. return lhs + constant_expression();
  1072. }
  1073. else if(global_token->s[0] == '-')
  1074. {
  1075. require_extra_token();
  1076. return lhs - constant_expression();
  1077. }
  1078. else if(global_token->s[0] == ',' || global_token->s[0] == ']' || global_token->s[0] == ';' || global_token->s[0] == '}' || global_token->s[0] == ':')
  1079. {
  1080. return lhs;
  1081. }
  1082. line_error();
  1083. fputs("Invalid token '", stderr);
  1084. fputs(global_token->s, stderr);
  1085. fputs("' used in constant expression.\n", stderr);
  1086. exit(EXIT_FAILURE);
  1087. }
  1088. void expression(void);
  1089. void function_call(struct token_list* s, int is_function_pointer, int is_local)
  1090. {
  1091. require_match("ERROR in process_expression_list\nNo ( was found\n", "(");
  1092. require(NULL != global_token, "Improper function call\n");
  1093. emit_push(REGISTER_TEMP, "Protect temp register we are going to use");
  1094. if((AARCH64 == Architecture) || (RISCV64 == Architecture) || (RISCV32 == Architecture))
  1095. {
  1096. emit_push(REGISTER_RETURN, "Protect the old return pointer (link)");
  1097. }
  1098. emit_push(REGISTER_BASE, "Protect the old base pointer");
  1099. emit_push(REGISTER_LOCALS, "Protect the old locals pointer");
  1100. emit_move(REGISTER_TEMP, REGISTER_STACK, "Copy new base pointer");
  1101. int passed = 0;
  1102. while(global_token->s[0] != ')')
  1103. {
  1104. expression();
  1105. require(NULL != global_token, "incomplete function call, received EOF instead of )\n");
  1106. emit_push(REGISTER_ZERO, "function argument");
  1107. passed = passed + 1;
  1108. if(global_token->s[0] == ',')
  1109. {
  1110. require_extra_token();
  1111. }
  1112. }
  1113. require_match("ERROR in process_expression_list\nNo ) was found\n", ")");
  1114. if(TRUE == is_function_pointer)
  1115. {
  1116. int value = s->depth;
  1117. int reg = REGISTER_BASE;
  1118. if(is_local)
  1119. {
  1120. reg = REGISTER_LOCALS;
  1121. }
  1122. emit_load_relative_to_register(REGISTER_ZERO, reg, value, "function pointer call");
  1123. emit_dereference(REGISTER_ZERO, "function pointer call");
  1124. if(ARMV7L == Architecture)
  1125. {
  1126. emit_push(REGISTER_RETURN, "Protect the old link register");
  1127. }
  1128. emit_move(REGISTER_BASE, REGISTER_TEMP, "function pointer call");
  1129. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  1130. {
  1131. emit_out("CALL R0 R15\n");
  1132. }
  1133. else if(X86 == Architecture)
  1134. {
  1135. emit_out("call_eax\n");
  1136. }
  1137. else if(AMD64 == Architecture)
  1138. {
  1139. emit_out("call_rax\n");
  1140. }
  1141. else if(ARMV7L == Architecture)
  1142. {
  1143. emit_out("'3' R0 CALL_REG_ALWAYS\n");
  1144. emit_pop(REGISTER_RETURN, "Prevent overwrite");
  1145. }
  1146. else if(AARCH64 == Architecture)
  1147. {
  1148. emit_move(REGISTER_TEMP, REGISTER_ZERO, "function pointer call");
  1149. emit_out("BLR_X16\n");
  1150. }
  1151. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  1152. {
  1153. emit_out("rd_ra rs1_a0 jalr\n");
  1154. }
  1155. }
  1156. else
  1157. {
  1158. if(ARMV7L == Architecture)
  1159. {
  1160. emit_push(REGISTER_RETURN, "Protect the old link register");
  1161. }
  1162. emit_move(REGISTER_BASE, REGISTER_TEMP, "function call");
  1163. if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture))
  1164. {
  1165. emit_load_named_immediate(REGISTER_ZERO, "FUNCTION_", s->s, "function call");
  1166. emit_out("CALL R0 R15\n");
  1167. }
  1168. else if((X86 == Architecture) || (AMD64 == Architecture))
  1169. {
  1170. emit_out("call %FUNCTION_");
  1171. emit_out(s->s);
  1172. emit_out("\n");
  1173. }
  1174. else if(ARMV7L == Architecture)
  1175. {
  1176. emit_out("^~FUNCTION_");
  1177. emit_out(s->s);
  1178. emit_out(" CALL_ALWAYS\n");
  1179. emit_pop(REGISTER_RETURN, "Restore the old link register");
  1180. }
  1181. else if(AARCH64 == Architecture)
  1182. {
  1183. emit_load_named_immediate(REGISTER_TEMP, "FUNCTION_", s->s, "function call");
  1184. emit_out("BLR_X16\n");
  1185. }
  1186. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  1187. {
  1188. emit_out("rd_ra $FUNCTION_");
  1189. emit_out(s->s);
  1190. emit_out(" jal\n");
  1191. }
  1192. }
  1193. for(; passed > 0; passed = passed - 1)
  1194. {
  1195. emit_pop(REGISTER_ONE, "Clean up function arguments");
  1196. }
  1197. emit_pop(REGISTER_LOCALS, "Restore old locals pointer");
  1198. emit_pop(REGISTER_BASE, "Restore old base pointer");
  1199. if((AARCH64 == Architecture) || (RISCV64 == Architecture) || (RISCV32 == Architecture))
  1200. {
  1201. emit_pop(REGISTER_RETURN, "Restore old return pointer (link)");
  1202. }
  1203. emit_pop(REGISTER_TEMP, "Restore temp register");
  1204. }
  1205. char* load_value_signed(unsigned size)
  1206. {
  1207. if(size == 1)
  1208. {
  1209. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "LOAD8 R0 R0 0\n";
  1210. else if(X86 == Architecture) return "movsx_eax,BYTE_PTR_[eax]\n";
  1211. else if(AMD64 == Architecture) return "movsx_rax,BYTE_PTR_[rax]\n";
  1212. else if(ARMV7L == Architecture) return "LOADS8 R0 LOAD R0 HALF_MEMORY\n";
  1213. else if(AARCH64 == Architecture) return "LDRSB_X0_[X0]\n";
  1214. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) return "rd_a0 rs1_a0 lb\n";
  1215. }
  1216. else if(size == 2)
  1217. {
  1218. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "LOAD16 R0 R0 0\n";
  1219. else if(X86 == Architecture) return "movsx_eax,WORD_PTR_[eax]\n";
  1220. else if(AMD64 == Architecture) return "movsx_rax,WORD_PTR_[rax]\n";
  1221. else if(ARMV7L == Architecture) return "LOADS16 R0 LOAD R0 HALF_MEMORY\n";
  1222. else if(AARCH64 == Architecture) return "LDRSH_X0_[X0]\n";
  1223. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) return "rd_a0 rs1_a0 lh\n";
  1224. }
  1225. else if(size == 4)
  1226. {
  1227. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "LOAD R0 R0 0\n";
  1228. else if(X86 == Architecture) return "mov_eax,[eax]\n";
  1229. else if(AMD64 == Architecture) return "movsx_rax,DWORD_PTR_[rax]\n";
  1230. else if(ARMV7L == Architecture) return "!0 R0 LOAD32 R0 MEMORY\n";
  1231. else if(AARCH64 == Architecture) return "LDR_W0_[X0]\n";
  1232. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) return "rd_a0 rs1_a0 lw\n";
  1233. }
  1234. else if(size == 8)
  1235. {
  1236. if(AMD64 == Architecture) return "mov_rax,[rax]\n";
  1237. else if(AARCH64 == Architecture) return "DEREF_X0\n";
  1238. else if(RISCV64 == Architecture) return "rd_a0 rs1_a0 ld\n";
  1239. }
  1240. line_error();
  1241. fputs(" Got unsupported size ", stderr);
  1242. fputs(int2str(size, 10, TRUE), stderr);
  1243. fputs(" when trying to load value.\n", stderr);
  1244. exit(EXIT_FAILURE);
  1245. }
  1246. char* load_value_unsigned(unsigned size)
  1247. {
  1248. if(size == 1)
  1249. {
  1250. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "LOADU8 R0 R0 0\n";
  1251. else if(X86 == Architecture) return "movzx_eax,BYTE_PTR_[eax]\n";
  1252. else if(AMD64 == Architecture) return "movzx_rax,BYTE_PTR_[rax]\n";
  1253. else if(ARMV7L == Architecture) return "!0 R0 LOAD R0 MEMORY\n";
  1254. else if(AARCH64 == Architecture) return "DEREF_X0_BYTE\n";
  1255. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) return "rd_a0 rs1_a0 lbu\n";
  1256. }
  1257. else if(size == 2)
  1258. {
  1259. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "LOADU16 R0 R0 0\n";
  1260. else if(X86 == Architecture) return "movzx_eax,WORD_PTR_[eax]\n";
  1261. else if(AMD64 == Architecture) return "movzx_rax,WORD_PTR_[rax]\n";
  1262. else if(ARMV7L == Architecture) return "NO_OFFSET R0 LOAD R0 HALF_MEMORY\n";
  1263. else if(AARCH64 == Architecture) return "LDRH_W0_[X0]\n";
  1264. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) return "rd_a0 rs1_a0 lhu\n";
  1265. }
  1266. else if(size == 4)
  1267. {
  1268. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "LOAD R0 R0 0\n";
  1269. else if(X86 == Architecture) return "mov_eax,[eax]\n";
  1270. else if(AMD64 == Architecture) return "mov_eax,[rax]\n";
  1271. else if(ARMV7L == Architecture) return "!0 R0 LOAD32 R0 MEMORY\n";
  1272. else if(AARCH64 == Architecture) return "LDR_W0_[X0]\n";
  1273. else if(RISCV32 == Architecture) return "rd_a0 rs1_a0 lw\n";
  1274. else if(RISCV64 == Architecture) return "rd_a0 rs1_a0 lwu\n";
  1275. }
  1276. else if(size == 8)
  1277. {
  1278. if(AMD64 == Architecture) return "mov_rax,[rax]\n";
  1279. else if(AARCH64 == Architecture) return "DEREF_X0\n";
  1280. else if(RISCV64 == Architecture) return "rd_a0 rs1_a0 ld\n";
  1281. }
  1282. line_error();
  1283. fputs(" Got unsupported size ", stderr);
  1284. fputs(int2str(size, 10, TRUE), stderr);
  1285. fputs(" when trying to load value.\n", stderr);
  1286. exit(EXIT_FAILURE);
  1287. }
  1288. char* load_value(unsigned size, int is_signed)
  1289. {
  1290. if(is_signed) return load_value_signed(size);
  1291. return load_value_unsigned(size);
  1292. }
  1293. char* store_value(unsigned size)
  1294. {
  1295. if(size == 1)
  1296. {
  1297. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "STORE8 R0 R1 0\n";
  1298. else if(X86 == Architecture) return "mov_[ebx],al\n";
  1299. else if(AMD64 == Architecture) return "mov_[rbx],al\n";
  1300. else if(ARMV7L == Architecture) return "!0 R0 STORE8 R1 MEMORY\n";
  1301. else if(AARCH64 == Architecture) return "STR_BYTE_W0_[X1]\n";
  1302. else if(RISCV32 == Architecture || RISCV64 == Architecture) return "rs1_a1 rs2_a0 sb\n";
  1303. }
  1304. else if(size == 2)
  1305. {
  1306. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "STORE16 R0 R1 0\n";
  1307. else if(X86 == Architecture) return "mov_[ebx],ax\n";
  1308. else if(AMD64 == Architecture) return "mov_[rbx],ax\n";
  1309. else if(ARMV7L == Architecture) return "NO_OFFSET R0 STORE16 R1 HALF_MEMORY\n";
  1310. else if(AARCH64 == Architecture) return "STRH_W0_[X1]\n";
  1311. else if(RISCV32 == Architecture || RISCV64 == Architecture) return "rs1_a1 rs2_a0 sh\n";
  1312. }
  1313. else if(size == 4)
  1314. {
  1315. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) return "STORE R0 R1 0\n";
  1316. else if(X86 == Architecture) return "mov_[ebx],eax\n";
  1317. else if(AMD64 == Architecture) return "mov_[rbx],eax\n";
  1318. else if(ARMV7L == Architecture) return "!0 R0 STORE32 R1 MEMORY\n";
  1319. else if(AARCH64 == Architecture) return "STR_W0_[X1]\n";
  1320. else if(RISCV32 == Architecture || RISCV64 == Architecture) return "rs1_a1 rs2_a0 sw\n";
  1321. }
  1322. else if(size == 8)
  1323. {
  1324. if(AMD64 == Architecture) return "mov_[rbx],rax\n";
  1325. else if(AARCH64 == Architecture) return "STR_X0_[X1]\n";
  1326. else if(RISCV64 == Architecture) return "rs1_a1 rs2_a0 sd\n";
  1327. }
  1328. /* Should not happen but print error message. */
  1329. fputs("Got unsupported size ", stderr);
  1330. fputs(int2str(size, 10, TRUE), stderr);
  1331. fputs(" when storing number in register.\n", stderr);
  1332. line_error();
  1333. exit(EXIT_FAILURE);
  1334. }
  1335. int is_compound_assignment(char* token)
  1336. {
  1337. if(match("+=", token)) return TRUE;
  1338. else if(match("-=", token)) return TRUE;
  1339. else if(match("*=", token)) return TRUE;
  1340. else if(match("/=", token)) return TRUE;
  1341. else if(match("%=", token)) return TRUE;
  1342. else if(match("<<=", token)) return TRUE;
  1343. else if(match(">>=", token)) return TRUE;
  1344. else if(match("&=", token)) return TRUE;
  1345. else if(match("^=", token)) return TRUE;
  1346. else if(match("|=", token)) return TRUE;
  1347. return FALSE;
  1348. }
  1349. void postfix_expr_stub(void);
  1350. void function_load(struct token_list* a)
  1351. {
  1352. require(NULL != global_token, "incomplete function load\n");
  1353. if(match("(", global_token->s))
  1354. {
  1355. function_call(a, FALSE, FALSE);
  1356. return;
  1357. }
  1358. emit_load_named_immediate(REGISTER_ZERO, "FUNCTION_", a->s, "function load");
  1359. }
  1360. /*
  1361. * primary-expr:
  1362. * FAILURE
  1363. * "String"
  1364. * 'Char'
  1365. * [0-9]*
  1366. * [a-z,A-Z]*
  1367. * ( expression )
  1368. */
  1369. void primary_expr_failure(void)
  1370. {
  1371. require(NULL != global_token, "hit EOF when expecting primary expression\n");
  1372. line_error();
  1373. fputs("Received ", stderr);
  1374. fputs(global_token->s, stderr);
  1375. fputs(" in primary_expr\n", stderr);
  1376. exit(EXIT_FAILURE);
  1377. }
  1378. void primary_expr_string(void)
  1379. {
  1380. char* number_string = int2str(current_count, 10, TRUE);
  1381. current_count = current_count + 1;
  1382. char* unique_id = create_unique_id("STRING_", function->s, number_string);
  1383. emit_load_named_immediate(REGISTER_ZERO, "", unique_id, "primary expr string");
  1384. /* The target */
  1385. strings_list = emit("\n", emit(unique_id, emit(":", strings_list)));
  1386. /* catch case of just "foo" from segfaulting */
  1387. require(NULL != global_token->next, "a string by itself is not valid C\n");
  1388. /* Parse the string */
  1389. if('"' != global_token->next->s[0])
  1390. {
  1391. strings_list = emit(parse_string(global_token->s), strings_list);
  1392. require_extra_token();
  1393. }
  1394. else
  1395. {
  1396. char* s = calloc(MAX_STRING, sizeof(char));
  1397. /* prefix leading string */
  1398. s[0] = '"';
  1399. int i = 1;
  1400. int used_string_concatenation = FALSE;
  1401. int j;
  1402. while('"' == global_token->s[0])
  1403. {
  1404. if(used_string_concatenation) maybe_bootstrap_error("string literal concatenation");
  1405. /* Step past the leading '"' */
  1406. j = 1;
  1407. /* Copy the rest of the string as is */
  1408. while(0 != global_token->s[j])
  1409. {
  1410. require(i < MAX_STRING, "concat string exceeded max string length\n");
  1411. s[i] = global_token->s[j];
  1412. i = i + 1;
  1413. j = j + 1;
  1414. }
  1415. /* Move on to the next token */
  1416. require_extra_token();
  1417. used_string_concatenation = TRUE;
  1418. }
  1419. /* Now use it */
  1420. strings_list = emit(parse_string(s), strings_list);
  1421. }
  1422. }
  1423. void primary_expr_char(void)
  1424. {
  1425. emit_load_immediate(REGISTER_ZERO, escape_lookup(global_token->s + 1), "primary expr char");
  1426. require_extra_token();
  1427. }
  1428. void primary_expr_number(char* s)
  1429. {
  1430. emit_load_immediate(REGISTER_ZERO, strtoint(s), "primary expr number");
  1431. }
  1432. struct token_list* load_address_of_variable_into_register(int reg, char* s)
  1433. {
  1434. struct token_list* variable = static_variable_lookup(s);
  1435. if(NULL != variable)
  1436. {
  1437. current_target = variable->type;
  1438. emit_load_named_immediate(reg, "GLOBAL_", variable->s, "global load");
  1439. return variable;
  1440. }
  1441. variable = sym_lookup(s, function->locals);
  1442. if(NULL != variable)
  1443. {
  1444. require(NULL != global_token, "incomplete variable load received\n");
  1445. if((variable->type->options & TO_FUNCTION_POINTER) && match("(", global_token->s))
  1446. {
  1447. function_call(variable, TRUE, TRUE);
  1448. return NULL;
  1449. }
  1450. current_target = variable->type;
  1451. emit_load_relative_to_register(reg, REGISTER_LOCALS, variable->depth, "local variable load");
  1452. return variable;
  1453. }
  1454. variable = sym_lookup(s, function->arguments);
  1455. if(NULL != variable)
  1456. {
  1457. require(NULL != global_token, "incomplete variable load received\n");
  1458. if((variable->type->options & TO_FUNCTION_POINTER) && match("(", global_token->s))
  1459. {
  1460. function_call(variable, TRUE, FALSE);
  1461. return NULL;
  1462. }
  1463. current_target = variable->type;
  1464. emit_load_relative_to_register(reg, REGISTER_BASE, variable->depth, "function argument load");
  1465. return variable;
  1466. }
  1467. variable = sym_lookup(s, global_function_list);
  1468. if(NULL != variable)
  1469. {
  1470. function_load(variable);
  1471. return NULL;
  1472. }
  1473. variable = sym_lookup(s, global_symbol_list);
  1474. if(NULL != variable)
  1475. {
  1476. current_target = variable->type;
  1477. emit_load_named_immediate(reg, "GLOBAL_", variable->s, "global load");
  1478. return variable;
  1479. }
  1480. line_error();
  1481. fputs(s ,stderr);
  1482. fputs(" is not a defined symbol\n", stderr);
  1483. exit(EXIT_FAILURE);
  1484. }
  1485. void emit_va_start_intrinsic(void)
  1486. {
  1487. emit_out("# __va_start intrinsic\n");
  1488. require_match("Invalid token after __va_start, expected '('", "(");
  1489. require_token();
  1490. char* ap_name = global_token->s;
  1491. require_extra_token();
  1492. require_match("Invalid token in __va_start, expected ','", ",");
  1493. require_token();
  1494. char* variable_name = global_token->s;
  1495. require_extra_token();
  1496. require_match("Invalid token at end of __va_start, expected ')'", ")");
  1497. struct token_list* loaded = load_address_of_variable_into_register(REGISTER_ZERO, variable_name);
  1498. if(stack_direction == STACK_DIRECTION_PLUS)
  1499. {
  1500. emit_add_immediate(REGISTER_ZERO, loaded->type->size, "Add size of variable");
  1501. }
  1502. else
  1503. {
  1504. emit_sub_immediate(REGISTER_ZERO, loaded->type->size, "Subtract size of variable");
  1505. }
  1506. load_address_of_variable_into_register(REGISTER_ONE, ap_name);
  1507. /* Store REGISTER_ZERO in REGISTER_ONE deref */
  1508. emit_out(store_value(register_size));
  1509. emit_out("# __va_start intrinsic end\n");
  1510. }
  1511. void emit_va_arg_intrinsic(void)
  1512. {
  1513. emit_out("# __va_arg intrinsic\n");
  1514. require_match("Invalid token after __va_arg, expected '('", "(");
  1515. require_token();
  1516. char* ap_name = global_token->s;
  1517. require_extra_token();
  1518. require_match("Invalid token in __va_arg, expected ','", ",");
  1519. require_token();
  1520. struct type* type_size = type_name();
  1521. require_match("Invalid token at end of __va_start, expected ')'", ")");
  1522. emit_out("# REGISTER_ZERO = *ap\n");
  1523. load_address_of_variable_into_register(REGISTER_ZERO, ap_name);
  1524. emit_dereference(REGISTER_ZERO, "Deref ap");
  1525. emit_dereference(REGISTER_ZERO, "Deref ap");
  1526. emit_push(REGISTER_ZERO, "Dereffed va_arg");
  1527. emit_out("# ap = ap - sizeof(ty)\n");
  1528. load_address_of_variable_into_register(REGISTER_ZERO, ap_name);
  1529. emit_push(REGISTER_ZERO, "Push ap address");
  1530. emit_dereference(REGISTER_ZERO, "Deref ap for pointer to va_arg");
  1531. if(stack_direction == STACK_DIRECTION_PLUS)
  1532. {
  1533. emit_add_immediate(REGISTER_ZERO, type_size->size, "Add size of variable");
  1534. }
  1535. else
  1536. {
  1537. emit_sub_immediate(REGISTER_ZERO, type_size->size, "Subtract size of variable");
  1538. }
  1539. emit_pop(REGISTER_ONE, "Pop AP address");
  1540. /* Store REGISTER_ZERO in REGISTER_ONE deref */
  1541. emit_out(store_value(type_size->size));
  1542. emit_pop(REGISTER_ZERO, "Dereffed va_arg");
  1543. emit_out("# __va_arg intrinsic end\n");
  1544. }
  1545. void emit_va_end_intrinsic(void)
  1546. {
  1547. /* va_end is a noop for our impl */
  1548. require_match("Invalid token after __va_arg, expected '('", "(");
  1549. require_extra_token();
  1550. require_match("Invalid token at end of __va_start, expected ')'", ")");
  1551. }
  1552. int num_dereference_after_postfix;
  1553. void primary_expr_variable(void)
  1554. {
  1555. int num_dereference = 0;
  1556. while(global_token->s[0] == '*') {
  1557. require_extra_token();
  1558. num_dereference = num_dereference + 1;
  1559. }
  1560. char* s = global_token->s;
  1561. require_extra_token();
  1562. if(match("__va_start", s))
  1563. {
  1564. emit_va_start_intrinsic();
  1565. return;
  1566. }
  1567. else if(match("__va_arg", s))
  1568. {
  1569. emit_va_arg_intrinsic();
  1570. return;
  1571. }
  1572. else if(match("__va_end", s))
  1573. {
  1574. emit_va_end_intrinsic();
  1575. return;
  1576. }
  1577. else if(match("__va_copy", s))
  1578. {
  1579. emit_va_end_intrinsic();
  1580. return;
  1581. }
  1582. struct token_list* a = sym_lookup(s, global_constant_list);
  1583. if(NULL != a)
  1584. {
  1585. emit_load_immediate(REGISTER_ZERO, strtoint(a->arguments->s), "constant load");
  1586. return;
  1587. }
  1588. struct token_list* type = load_address_of_variable_into_register(REGISTER_ZERO, s);
  1589. if(TRUE == Address_of) return;
  1590. if(type == NULL) return;
  1591. int options = type->options;
  1592. if(match(".", global_token->s))
  1593. {
  1594. postfix_expr_stub();
  1595. return;
  1596. }
  1597. int is_assignment = match("=", global_token->s);
  1598. int is_compound_operator = is_compound_assignment(global_token->s);
  1599. int is_local_array = match("[", global_token->s) && (options & TLO_LOCAL_ARRAY);
  1600. int is_prefix_operator = (match("++", global_token->prev->prev->s) || match("--", global_token->prev->prev->s)) && (options != TLO_STATIC && options != TLO_GLOBAL);
  1601. int is_postfix_operator = (match("++", global_token->s) || match("--", global_token->s)) && (options != TLO_STATIC && options != TLO_GLOBAL);
  1602. int should_emit = !is_assignment && !is_compound_operator && !is_local_array && !is_postfix_operator && !is_prefix_operator;
  1603. int size = register_size;
  1604. if(options == TLO_LOCAL || options == TLO_ARGUMENT)
  1605. {
  1606. size = current_target->size;
  1607. }
  1608. if(should_emit)
  1609. {
  1610. emit_out(load_value(size, current_target->is_signed));
  1611. while (num_dereference > 0)
  1612. {
  1613. current_target = current_target->type;
  1614. emit_out(load_value(current_target->size, current_target->is_signed));
  1615. num_dereference = num_dereference - 1;
  1616. }
  1617. return;
  1618. }
  1619. num_dereference_after_postfix = num_dereference;
  1620. if(!is_postfix_operator)
  1621. {
  1622. while (num_dereference > 0)
  1623. {
  1624. emit_out(load_value(current_target->size, current_target->is_signed));
  1625. current_target = current_target->type;
  1626. num_dereference = num_dereference - 1;
  1627. }
  1628. }
  1629. }
  1630. void primary_expr(void);
  1631. struct type* promote_type(struct type* a, struct type* b)
  1632. {
  1633. require(NULL != b, "impossible case 1 in promote_type\n");
  1634. require(NULL != a, "impossible case 2 in promote_type\n");
  1635. if(a == b) return a;
  1636. struct type* i;
  1637. for(i = global_types; NULL != i; i = i->next)
  1638. {
  1639. if(a->name == i->name) break;
  1640. if(b->name == i->name) break;
  1641. if(a->name == i->indirect->name) break;
  1642. if(b->name == i->indirect->name) break;
  1643. if(a->name == i->indirect->indirect->name) break;
  1644. if(b->name == i->indirect->indirect->name) break;
  1645. }
  1646. require(NULL != i, "impossible case 3 in promote_type\n");
  1647. return i;
  1648. }
  1649. void common_recursion(FUNCTION f)
  1650. {
  1651. emit_push(REGISTER_ZERO, "_common_recursion");
  1652. struct type* last_type = current_target;
  1653. require_extra_token();
  1654. f();
  1655. current_target = promote_type(current_target, last_type);
  1656. emit_pop(REGISTER_ONE, "_common_recursion");
  1657. }
  1658. void general_recursion(FUNCTION f, char* s, char* name, FUNCTION iterate)
  1659. {
  1660. require(NULL != global_token, "Received EOF in general_recursion\n");
  1661. if(match(name, global_token->s))
  1662. {
  1663. common_recursion(f);
  1664. emit_out(s);
  1665. iterate();
  1666. }
  1667. }
  1668. void multiply_by_object_size(int object_size)
  1669. {
  1670. /* bootstrap mode can't depend upon on pointer arithmetic */
  1671. if(BOOTSTRAP_MODE) return;
  1672. if(object_size == 1)
  1673. {
  1674. /* No reason to multiply by one */
  1675. return;
  1676. }
  1677. emit_mul_register_zero_with_immediate(current_target->type->size, "pointer arithmetic");
  1678. }
  1679. void arithmetic_recursion(FUNCTION f, char* s1, char* s2, char* name, FUNCTION iterate)
  1680. {
  1681. require(NULL != global_token, "Received EOF in arithmetic_recursion\n");
  1682. if(match(name, global_token->s))
  1683. {
  1684. common_recursion(f);
  1685. if(NULL == current_target)
  1686. {
  1687. emit_out(s1);
  1688. }
  1689. else if(current_target->is_signed)
  1690. {
  1691. emit_out(s1);
  1692. }
  1693. else
  1694. {
  1695. emit_out(s2);
  1696. }
  1697. iterate();
  1698. }
  1699. }
  1700. /*
  1701. * postfix-expr:
  1702. * primary-expr
  1703. * postfix-expr [ expression ]
  1704. * postfix-expr ( expression-list-opt )
  1705. * postfix-expr -> member
  1706. * postfix-expr . member
  1707. */
  1708. struct type* lookup_member(struct type* parent, char* name);
  1709. void postfix_expr_arrow(void)
  1710. {
  1711. emit_out("# looking up offset\n");
  1712. require_extra_token();
  1713. struct type* i = lookup_member(current_target, global_token->s);
  1714. current_target = i->type;
  1715. require_extra_token();
  1716. if(0 != i->offset)
  1717. {
  1718. emit_add_immediate(REGISTER_ZERO, i->offset, "-> offset calculation");
  1719. }
  1720. if(global_token->s[0] == '.') return;
  1721. /* We don't yet support assigning structs to structs */
  1722. if((!match("=", global_token->s) && !is_compound_assignment(global_token->s) && (register_size >= i->size)))
  1723. {
  1724. emit_out(load_value(i->size, i->is_signed));
  1725. }
  1726. }
  1727. void postfix_expr_inc_or_dec(void)
  1728. {
  1729. int is_subtract = global_token->s[0] == '-';
  1730. require_extra_token();
  1731. emit_out("# postfix inc/dec\n");
  1732. emit_push(REGISTER_ONE, "Old register one value");
  1733. emit_push(REGISTER_ZERO, "Address of variable");
  1734. emit_dereference(REGISTER_ZERO, "Get value");
  1735. /* We need the address to be at the top of the stack and the value to be below it */
  1736. emit_pop(REGISTER_ONE, "Address of variable");
  1737. emit_push(REGISTER_ZERO, "Value before postfix operator");
  1738. emit_push(REGISTER_ONE, "Address of variable");
  1739. int value = 1;
  1740. if(type_is_pointer(current_target))
  1741. {
  1742. value = current_target->type->size;
  1743. }
  1744. if(is_subtract)
  1745. {
  1746. emit_sub_immediate(REGISTER_ZERO, value, "Subtract offset");
  1747. }
  1748. else
  1749. {
  1750. emit_add_immediate(REGISTER_ZERO, value, "Add offset");
  1751. }
  1752. emit_pop(REGISTER_ONE, "Address of variable");
  1753. /* Store REGISTER_ZERO in REGISTER_ONE deref */
  1754. emit_out(store_value(current_target->size));
  1755. emit_pop(REGISTER_ZERO, "Value before postfix operator");
  1756. emit_pop(REGISTER_ONE, "Previous value");
  1757. while (num_dereference_after_postfix > 0)
  1758. {
  1759. emit_out(load_value(current_target->type->size, current_target->type->is_signed));
  1760. current_target = current_target->type;
  1761. num_dereference_after_postfix = num_dereference_after_postfix - 1;
  1762. }
  1763. emit_out("# postfix inc/dec end\n");
  1764. }
  1765. void postfix_expr_dot(void)
  1766. {
  1767. maybe_bootstrap_error("Member access using .");
  1768. emit_out("# looking up offset\n");
  1769. require_extra_token();
  1770. struct type* i = lookup_member(current_target, global_token->s);
  1771. current_target = i->type;
  1772. require_extra_token();
  1773. if(0 != i->offset)
  1774. {
  1775. emit_add_immediate(REGISTER_ZERO, i->offset, ". offset calculation");
  1776. }
  1777. if(match("=", global_token->s) || is_compound_assignment(global_token->s)) return;
  1778. if(match("[", global_token->s) || match(".", global_token->s)) return;
  1779. emit_out(load_value(current_target->size, current_target->is_signed));
  1780. }
  1781. void postfix_expr_array(void)
  1782. {
  1783. char* prefix_operator = global_token->prev->prev->s;
  1784. struct type* array = current_target;
  1785. common_recursion(expression);
  1786. current_target = array;
  1787. require(NULL != current_target, "Arrays only apply to variables\n");
  1788. char* assign = load_value(register_size, current_target->is_signed);
  1789. /* Add support for Ints */
  1790. if(match("char*", current_target->name))
  1791. {
  1792. assign = load_value(1, TRUE);
  1793. }
  1794. else
  1795. {
  1796. emit_mul_register_zero_with_immediate(current_target->type->size, "primary expr array");
  1797. }
  1798. emit_add(REGISTER_ZERO, REGISTER_ONE, "primary expr array");
  1799. require_match("ERROR in postfix_expr\nMissing ]\n", "]");
  1800. require(NULL != global_token, "truncated array expression\n");
  1801. int is_prefix_operator = match("++", prefix_operator) || match("--", prefix_operator);
  1802. int is_postfix_operator = match("++", global_token->s) || match("--", global_token->s);
  1803. if(match("=", global_token->s) || is_compound_assignment(global_token->s) || match(".", global_token->s) || is_prefix_operator || is_postfix_operator)
  1804. {
  1805. assign = "";
  1806. }
  1807. if(match("[", global_token->s))
  1808. {
  1809. current_target = current_target->type;
  1810. }
  1811. emit_out(assign);
  1812. }
  1813. /*
  1814. * unary-expr:
  1815. * &postfix-expr
  1816. * - postfix-expr
  1817. * !postfix-expr
  1818. * sizeof ( type )
  1819. */
  1820. int unary_expr_sizeof(void)
  1821. {
  1822. require_extra_token();
  1823. require_match("ERROR in unary_expr\nMissing (\n", "(");
  1824. struct token_list* t = NULL;
  1825. int num_dereferences = 0;
  1826. if(!BOOTSTRAP_MODE)
  1827. {
  1828. if(global_token->s[0] == '\"')
  1829. {
  1830. /* string_length (strlen) doesn't include the null terminator but the
  1831. * token starts with a quotation mark so the count will be correct. */
  1832. int length = string_length(global_token->s);
  1833. require_extra_token();
  1834. require_match("ERROR in unary_expr\nMissing )\n", ")");
  1835. return length;
  1836. }
  1837. while(global_token->s[0] == '*')
  1838. {
  1839. num_dereferences = num_dereferences + 1;
  1840. require_extra_token();
  1841. }
  1842. t = static_variable_lookup(global_token->s);
  1843. if(NULL == t)
  1844. {
  1845. t = sym_lookup(global_token->s, global_constant_list);
  1846. if(NULL == t && NULL != function)
  1847. {
  1848. t = sym_lookup(global_token->s, function->locals);
  1849. if(NULL == t)
  1850. {
  1851. t = sym_lookup(global_token->s, function->arguments);
  1852. }
  1853. }
  1854. }
  1855. if(NULL == t)
  1856. {
  1857. t = sym_lookup(global_token->s, global_symbol_list);
  1858. }
  1859. }
  1860. int size = 0;
  1861. if(t != NULL)
  1862. {
  1863. require_extra_token();
  1864. struct type* a = t->type;
  1865. while(num_dereferences > 0)
  1866. {
  1867. a = a->type;
  1868. num_dereferences = num_dereferences - 1;
  1869. }
  1870. if(t->array_modifier != 0)
  1871. {
  1872. size = a->size * t->array_modifier;
  1873. }
  1874. else size = a->size;
  1875. }
  1876. else
  1877. {
  1878. if(num_dereferences != 0)
  1879. {
  1880. line_error();
  1881. fputs("Unable to dereference type for sizeof.\n", stderr);
  1882. exit(EXIT_FAILURE);
  1883. }
  1884. struct type* a = type_name();
  1885. size = a->size;
  1886. }
  1887. require_match("ERROR in unary_expr\nMissing )\n", ")");
  1888. return size;
  1889. }
  1890. void postfix_expr_stub(void)
  1891. {
  1892. require(NULL != global_token, "Unexpected EOF, improperly terminated primary expression\n");
  1893. if(match("[", global_token->s))
  1894. {
  1895. postfix_expr_array();
  1896. postfix_expr_stub();
  1897. }
  1898. if(match("->", global_token->s))
  1899. {
  1900. postfix_expr_arrow();
  1901. postfix_expr_stub();
  1902. }
  1903. if(match(".", global_token->s))
  1904. {
  1905. postfix_expr_dot();
  1906. postfix_expr_stub();
  1907. }
  1908. if(match("++", global_token->s) || match("--", global_token->s))
  1909. {
  1910. postfix_expr_inc_or_dec();
  1911. postfix_expr_stub();
  1912. }
  1913. }
  1914. void postfix_expr(void)
  1915. {
  1916. primary_expr();
  1917. postfix_expr_stub();
  1918. }
  1919. /*
  1920. * additive-expr:
  1921. * postfix-expr
  1922. * additive-expr * postfix-expr
  1923. * additive-expr / postfix-expr
  1924. * additive-expr % postfix-expr
  1925. * additive-expr + postfix-expr
  1926. * additive-expr - postfix-expr
  1927. * additive-expr << postfix-expr
  1928. * additive-expr >> postfix-expr
  1929. */
  1930. void additive_expr_stub_a(void)
  1931. {
  1932. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  1933. {
  1934. arithmetic_recursion(postfix_expr, "MUL R0 R1 R0\n", "MULU R0 R1 R0\n", "*", additive_expr_stub_a);
  1935. arithmetic_recursion(postfix_expr, "DIV R0 R1 R0\n", "DIVU R0 R1 R0\n", "/", additive_expr_stub_a);
  1936. arithmetic_recursion(postfix_expr, "MOD R0 R1 R0\n", "MODU R0 R1 R0\n", "%", additive_expr_stub_a);
  1937. }
  1938. else if(X86 == Architecture)
  1939. {
  1940. arithmetic_recursion(postfix_expr, "imul_ebx\n", "mul_ebx\n", "*", additive_expr_stub_a);
  1941. arithmetic_recursion(postfix_expr, "xchg_ebx,eax\ncdq\nidiv_ebx\n", "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\n", "/", additive_expr_stub_a);
  1942. arithmetic_recursion(postfix_expr, "xchg_ebx,eax\ncdq\nidiv_ebx\nmov_eax,edx\n", "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\nmov_eax,edx\n", "%", additive_expr_stub_a);
  1943. }
  1944. else if(AMD64 == Architecture)
  1945. {
  1946. arithmetic_recursion(postfix_expr, "imul_rbx\n", "mul_rbx\n", "*", additive_expr_stub_a);
  1947. arithmetic_recursion(postfix_expr, "xchg_rbx,rax\ncqo\nidiv_rbx\n", "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\n", "/", additive_expr_stub_a);
  1948. arithmetic_recursion(postfix_expr, "xchg_rbx,rax\ncqo\nidiv_rbx\nmov_rax,rdx\n", "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\nmov_rax,rdx\n", "%", additive_expr_stub_a);
  1949. }
  1950. else if(ARMV7L == Architecture)
  1951. {
  1952. arithmetic_recursion(postfix_expr, "'9' R0 '0' R1 MULS R0 ARITH2_ALWAYS\n", "'9' R0 '0' R1 MUL R0 ARITH2_ALWAYS\n", "*", additive_expr_stub_a);
  1953. arithmetic_recursion(postfix_expr, "{LR} PUSH_ALWAYS\n^~divides CALL_ALWAYS\n{LR} POP_ALWAYS\n", "{LR} PUSH_ALWAYS\n^~divide CALL_ALWAYS\n{LR} POP_ALWAYS\n", "/", additive_expr_stub_a);
  1954. arithmetic_recursion(postfix_expr, "{LR} PUSH_ALWAYS\n^~moduluss CALL_ALWAYS\n{LR} POP_ALWAYS\n", "{LR} PUSH_ALWAYS\n^~modulus CALL_ALWAYS\n{LR} POP_ALWAYS\n", "%", additive_expr_stub_a);
  1955. }
  1956. else if(AARCH64 == Architecture)
  1957. {
  1958. general_recursion(postfix_expr, "MUL_X0_X1_X0\n", "*", additive_expr_stub_a);
  1959. arithmetic_recursion(postfix_expr, "SDIV_X0_X1_X0\n", "UDIV_X0_X1_X0\n", "/", additive_expr_stub_a);
  1960. arithmetic_recursion(postfix_expr, "SDIV_X2_X1_X0\nMSUB_X0_X0_X2_X1\n", "UDIV_X2_X1_X0\nMSUB_X0_X0_X2_X1\n", "%", additive_expr_stub_a);
  1961. }
  1962. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  1963. {
  1964. general_recursion(postfix_expr, "rd_a0 rs1_a1 rs2_a0 mul\n", "*", additive_expr_stub_a);
  1965. arithmetic_recursion(postfix_expr, "rd_a0 rs1_a1 rs2_a0 div\n", "rd_a0 rs1_a1 rs2_a0 divu\n", "/", additive_expr_stub_a);
  1966. arithmetic_recursion(postfix_expr, "rd_a0 rs1_a1 rs2_a0 rem\n", "rd_a0 rs1_a1 rs2_a0 remu\n", "%", additive_expr_stub_a);
  1967. }
  1968. }
  1969. void additive_expr_a(void)
  1970. {
  1971. postfix_expr();
  1972. additive_expr_stub_a();
  1973. }
  1974. void additive_expr_stub_b(void)
  1975. {
  1976. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  1977. {
  1978. arithmetic_recursion(additive_expr_a, "ADD R0 R1 R0\n", "ADDU R0 R1 R0\n", "+", additive_expr_stub_b);
  1979. arithmetic_recursion(additive_expr_a, "SUB R0 R1 R0\n", "SUBU R0 R1 R0\n", "-", additive_expr_stub_b);
  1980. }
  1981. else if(X86 == Architecture)
  1982. {
  1983. arithmetic_recursion(additive_expr_a, "add_eax,ebx\n", "add_eax,ebx\n", "+", additive_expr_stub_b);
  1984. arithmetic_recursion(additive_expr_a, "sub_ebx,eax\nmov_eax,ebx\n", "sub_ebx,eax\nmov_eax,ebx\n", "-", additive_expr_stub_b);
  1985. }
  1986. else if(AMD64 == Architecture)
  1987. {
  1988. arithmetic_recursion(additive_expr_a, "add_rax,rbx\n", "add_rax,rbx\n", "+", additive_expr_stub_b);
  1989. arithmetic_recursion(additive_expr_a, "sub_rbx,rax\nmov_rax,rbx\n", "sub_rbx,rax\nmov_rax,rbx\n", "-", additive_expr_stub_b);
  1990. }
  1991. else if(ARMV7L == Architecture)
  1992. {
  1993. arithmetic_recursion(additive_expr_a, "'0' R0 R0 ADD R1 ARITH2_ALWAYS\n", "'0' R0 R0 ADD R1 ARITH2_ALWAYS\n", "+", additive_expr_stub_b);
  1994. arithmetic_recursion(additive_expr_a, "'0' R0 R0 SUB R1 ARITH2_ALWAYS\n", "'0' R0 R0 SUB R1 ARITH2_ALWAYS\n", "-", additive_expr_stub_b);
  1995. }
  1996. else if(AARCH64 == Architecture)
  1997. {
  1998. general_recursion(additive_expr_a, "ADD_X0_X1_X0\n", "+", additive_expr_stub_b);
  1999. general_recursion(additive_expr_a, "SUB_X0_X1_X0\n", "-", additive_expr_stub_b);
  2000. }
  2001. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2002. {
  2003. general_recursion(additive_expr_a, "rd_a0 rs1_a1 rs2_a0 add\n", "+", additive_expr_stub_b);
  2004. general_recursion(additive_expr_a, "rd_a0 rs1_a1 rs2_a0 sub\n", "-", additive_expr_stub_b);
  2005. }
  2006. }
  2007. void additive_expr_b(void)
  2008. {
  2009. additive_expr_a();
  2010. additive_expr_stub_b();
  2011. }
  2012. void additive_expr_stub_c(void)
  2013. {
  2014. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2015. {
  2016. arithmetic_recursion(additive_expr_b, "SAL R0 R1 R0\n", "SL0 R0 R1 R0\n", "<<", additive_expr_stub_c);
  2017. arithmetic_recursion(additive_expr_b, "SAR R0 R1 R0\n", "SR0 R0 R1 R0\n", ">>", additive_expr_stub_c);
  2018. }
  2019. else if(X86 == Architecture)
  2020. {
  2021. arithmetic_recursion(additive_expr_b, "mov_ecx,eax\nmov_eax,ebx\nsal_eax,cl\n", "mov_ecx,eax\nmov_eax,ebx\nshl_eax,cl\n", "<<", additive_expr_stub_c);
  2022. arithmetic_recursion(additive_expr_b, "mov_ecx,eax\nmov_eax,ebx\nsar_eax,cl\n", "mov_ecx,eax\nmov_eax,ebx\nshr_eax,cl\n", ">>", additive_expr_stub_c);
  2023. }
  2024. else if(AMD64 == Architecture)
  2025. {
  2026. arithmetic_recursion(additive_expr_b, "mov_rcx,rax\nmov_rax,rbx\nsal_rax,cl\n", "mov_rcx,rax\nmov_rax,rbx\nshl_rax,cl\n", "<<", additive_expr_stub_c);
  2027. arithmetic_recursion(additive_expr_b, "mov_rcx,rax\nmov_rax,rbx\nsar_rax,cl\n", "mov_rcx,rax\nmov_rax,rbx\nshr_rax,cl\n", ">>", additive_expr_stub_c);
  2028. }
  2029. else if(ARMV7L == Architecture)
  2030. {
  2031. arithmetic_recursion(additive_expr_b, "LEFT R1 R0 R0 SHIFT AUX_ALWAYS\n", "LEFT R1 R0 R0 SHIFT AUX_ALWAYS\n", "<<", additive_expr_stub_c);
  2032. arithmetic_recursion(additive_expr_b, "ARITH_RIGHT R1 R0 R0 SHIFT AUX_ALWAYS\n", "RIGHT R1 R0 R0 SHIFT AUX_ALWAYS\n", ">>", additive_expr_stub_c);
  2033. }
  2034. else if(AARCH64 == Architecture)
  2035. {
  2036. general_recursion(additive_expr_b, "LSHIFT_X0_X1_X0\n", "<<", additive_expr_stub_c);
  2037. arithmetic_recursion(additive_expr_b, "ARITH_RSHIFT_X0_X1_X0\n", "LOGICAL_RSHIFT_X0_X1_X0\n", ">>", additive_expr_stub_c);
  2038. }
  2039. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2040. {
  2041. general_recursion(additive_expr_b, "rd_a0 rs1_a1 rs2_a0 sll\n", "<<", additive_expr_stub_c);
  2042. arithmetic_recursion(additive_expr_b, "rd_a0 rs1_a1 rs2_a0 sra\n", "rd_a0 rs1_a1 rs2_a0 srl\n", ">>", additive_expr_stub_c);
  2043. }
  2044. }
  2045. void additive_expr_c(void)
  2046. {
  2047. additive_expr_b();
  2048. additive_expr_stub_c();
  2049. }
  2050. /*
  2051. * relational-expr:
  2052. * additive_expr
  2053. * relational-expr < additive_expr
  2054. * relational-expr <= additive_expr
  2055. * relational-expr >= additive_expr
  2056. * relational-expr > additive_expr
  2057. */
  2058. void relational_expr_stub(void)
  2059. {
  2060. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2061. {
  2062. arithmetic_recursion(additive_expr_c, "CMP R0 R1 R0\nSET.L R0 R0 1\n", "CMPU R0 R1 R0\nSET.L R0 R0 1\n", "<", relational_expr_stub);
  2063. arithmetic_recursion(additive_expr_c, "CMP R0 R1 R0\nSET.LE R0 R0 1\n", "CMPU R0 R1 R0\nSET.LE R0 R0 1\n", "<=", relational_expr_stub);
  2064. arithmetic_recursion(additive_expr_c, "CMP R0 R1 R0\nSET.GE R0 R0 1\n", "CMPU R0 R1 R0\nSET.GE R0 R0 1\n", ">=", relational_expr_stub);
  2065. arithmetic_recursion(additive_expr_c, "CMP R0 R1 R0\nSET.G R0 R0 1\n", "CMPU R0 R1 R0\nSET.G R0 R0 1\n", ">", relational_expr_stub);
  2066. arithmetic_recursion(additive_expr_c, "CMP R0 R1 R0\nSET.E R0 R0 1\n", "CMPU R0 R1 R0\nSET.E R0 R0 1\n", "==", relational_expr_stub);
  2067. arithmetic_recursion(additive_expr_c, "CMP R0 R1 R0\nSET.NE R0 R0 1\n", "CMPU R0 R1 R0\nSET.NE R0 R0 1\n", "!=", relational_expr_stub);
  2068. }
  2069. else if(X86 == Architecture)
  2070. {
  2071. arithmetic_recursion(additive_expr_c, "cmp\nsetl_al\nmovzx_eax,al\n", "cmp\nsetb_al\nmovzx_eax,al\n", "<", relational_expr_stub);
  2072. arithmetic_recursion(additive_expr_c, "cmp\nsetle_al\nmovzx_eax,al\n", "cmp\nsetbe_al\nmovzx_eax,al\n", "<=", relational_expr_stub);
  2073. arithmetic_recursion(additive_expr_c, "cmp\nsetge_al\nmovzx_eax,al\n", "cmp\nsetae_al\nmovzx_eax,al\n", ">=", relational_expr_stub);
  2074. arithmetic_recursion(additive_expr_c, "cmp\nsetg_al\nmovzx_eax,al\n", "cmp\nseta_al\nmovzx_eax,al\n", ">", relational_expr_stub);
  2075. general_recursion(additive_expr_c, "cmp\nsete_al\nmovzx_eax,al\n", "==", relational_expr_stub);
  2076. general_recursion(additive_expr_c, "cmp\nsetne_al\nmovzx_eax,al\n", "!=", relational_expr_stub);
  2077. }
  2078. else if(AMD64 == Architecture)
  2079. {
  2080. arithmetic_recursion(additive_expr_c, "cmp_rbx,rax\nsetl_al\nmovzx_rax,al\n", "cmp_rbx,rax\nsetb_al\nmovzx_rax,al\n", "<", relational_expr_stub);
  2081. arithmetic_recursion(additive_expr_c, "cmp_rbx,rax\nsetle_al\nmovzx_rax,al\n", "cmp_rbx,rax\nsetbe_al\nmovzx_rax,al\n", "<=", relational_expr_stub);
  2082. arithmetic_recursion(additive_expr_c, "cmp_rbx,rax\nsetge_al\nmovzx_rax,al\n", "cmp_rbx,rax\nsetae_al\nmovzx_rax,al\n", ">=", relational_expr_stub);
  2083. arithmetic_recursion(additive_expr_c, "cmp_rbx,rax\nsetg_al\nmovzx_rax,al\n", "cmp_rbx,rax\nseta_al\nmovzx_rax,al\n", ">", relational_expr_stub);
  2084. general_recursion(additive_expr_c, "cmp_rbx,rax\nsete_al\nmovzx_rax,al\n", "==", relational_expr_stub);
  2085. general_recursion(additive_expr_c, "cmp_rbx,rax\nsetne_al\nmovzx_rax,al\n", "!=", relational_expr_stub);
  2086. }
  2087. else if(ARMV7L == Architecture)
  2088. {
  2089. arithmetic_recursion(additive_expr_c, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_L\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_LO\n", "<", relational_expr_stub);
  2090. arithmetic_recursion(additive_expr_c, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_LE\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_LS\n", "<=", relational_expr_stub);
  2091. arithmetic_recursion(additive_expr_c, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_GE\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_HS\n", ">=", relational_expr_stub);
  2092. arithmetic_recursion(additive_expr_c, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_G\n", "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_HI\n", ">", relational_expr_stub);
  2093. general_recursion(additive_expr_c, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_EQUAL\n", "==", relational_expr_stub);
  2094. general_recursion(additive_expr_c, "'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_NE\n", "!=", relational_expr_stub);
  2095. }
  2096. else if(AARCH64 == Architecture)
  2097. {
  2098. arithmetic_recursion(additive_expr_c, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LT\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LO\nSET_X0_TO_0\n", "<", relational_expr_stub);
  2099. arithmetic_recursion(additive_expr_c, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LE\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_LS\nSET_X0_TO_0\n", "<=", relational_expr_stub);
  2100. arithmetic_recursion(additive_expr_c, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_GE\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_HS\nSET_X0_TO_0\n", ">=", relational_expr_stub);
  2101. arithmetic_recursion(additive_expr_c, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_GT\nSET_X0_TO_0\n", "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_HI\nSET_X0_TO_0\n", ">", relational_expr_stub);
  2102. general_recursion(additive_expr_c, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_EQ\nSET_X0_TO_0\n", "==", relational_expr_stub);
  2103. general_recursion(additive_expr_c, "CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_NE\nSET_X0_TO_0\n", "!=", relational_expr_stub);
  2104. }
  2105. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2106. {
  2107. arithmetic_recursion(additive_expr_c, "rd_a0 rs1_a1 rs2_a0 slt\n", "rd_a0 rs1_a1 rs2_a0 sltu\n", "<", relational_expr_stub);
  2108. arithmetic_recursion(additive_expr_c, "rd_a0 rs1_a0 rs2_a1 slt\nrd_a0 rs1_a0 !1 xori\n", "rd_a0 rs1_a0 rs2_a1 sltu\nrd_a0 rs1_a0 !1 xori\n", "<=", relational_expr_stub);
  2109. arithmetic_recursion(additive_expr_c, "rd_a0 rs1_a1 rs2_a0 slt\nrd_a0 rs1_a0 !1 xori\n", "rd_a0 rs1_a1 rs2_a0 sltu\nrd_a0 rs1_a0 !1 xori\n", ">=", relational_expr_stub);
  2110. arithmetic_recursion(additive_expr_c, "rd_a0 rs1_a0 rs2_a1 slt\n", "rd_a0 rs1_a0 rs2_a1 sltu\n", ">", relational_expr_stub);
  2111. general_recursion(additive_expr_c, "rd_a0 rs1_a0 rs2_a1 sub\nrd_a0 rs1_a0 !1 sltiu\n", "==", relational_expr_stub);
  2112. general_recursion(additive_expr_c, "rd_a0 rs1_a0 rs2_a1 sub\nrd_a0 rs2_a0 sltu\n", "!=", relational_expr_stub);
  2113. }
  2114. }
  2115. void relational_expr(void)
  2116. {
  2117. additive_expr_c();
  2118. relational_expr_stub();
  2119. }
  2120. /*
  2121. * bitwise-expr:
  2122. * relational-expr
  2123. * bitwise-expr & bitwise-expr
  2124. * bitwise-expr && bitwise-expr
  2125. * bitwise-expr | bitwise-expr
  2126. * bitwise-expr || bitwise-expr
  2127. * bitwise-expr ^ bitwise-expr
  2128. */
  2129. void bitwise_expr_stub(void)
  2130. {
  2131. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2132. {
  2133. general_recursion(relational_expr, "AND R0 R0 R1\n", "&", bitwise_expr_stub);
  2134. general_recursion(relational_expr, "AND R0 R0 R1\n", "&&", bitwise_expr_stub);
  2135. general_recursion(relational_expr, "OR R0 R0 R1\n", "|", bitwise_expr_stub);
  2136. general_recursion(relational_expr, "OR R0 R0 R1\n", "||", bitwise_expr_stub);
  2137. general_recursion(relational_expr, "XOR R0 R0 R1\n", "^", bitwise_expr_stub);
  2138. }
  2139. else if(X86 == Architecture)
  2140. {
  2141. general_recursion(relational_expr, "and_eax,ebx\n", "&", bitwise_expr_stub);
  2142. general_recursion(relational_expr, "and_eax,ebx\n", "&&", bitwise_expr_stub);
  2143. general_recursion(relational_expr, "or_eax,ebx\n", "|", bitwise_expr_stub);
  2144. general_recursion(relational_expr, "or_eax,ebx\n", "||", bitwise_expr_stub);
  2145. general_recursion(relational_expr, "xor_eax,ebx\n", "^", bitwise_expr_stub);
  2146. }
  2147. else if(AMD64 == Architecture)
  2148. {
  2149. general_recursion(relational_expr, "and_rax,rbx\n", "&", bitwise_expr_stub);
  2150. general_recursion(relational_expr, "and_rax,rbx\n", "&&", bitwise_expr_stub);
  2151. general_recursion(relational_expr, "or_rax,rbx\n", "|", bitwise_expr_stub);
  2152. general_recursion(relational_expr, "or_rax,rbx\n", "||", bitwise_expr_stub);
  2153. general_recursion(relational_expr, "xor_rax,rbx\n", "^", bitwise_expr_stub);
  2154. }
  2155. else if(ARMV7L == Architecture)
  2156. {
  2157. general_recursion(relational_expr, "NO_SHIFT R0 R0 AND R1 ARITH2_ALWAYS\n", "&", bitwise_expr_stub);
  2158. general_recursion(relational_expr, "NO_SHIFT R0 R0 AND R1 ARITH2_ALWAYS\n", "&&", bitwise_expr_stub);
  2159. general_recursion(relational_expr, "NO_SHIFT R0 R0 OR R1 AUX_ALWAYS\n", "|", bitwise_expr_stub);
  2160. general_recursion(relational_expr, "NO_SHIFT R0 R0 OR R1 AUX_ALWAYS\n", "||", bitwise_expr_stub);
  2161. general_recursion(relational_expr, "'0' R0 R0 XOR R1 ARITH2_ALWAYS\n", "^", bitwise_expr_stub);
  2162. }
  2163. else if(AARCH64 == Architecture)
  2164. {
  2165. general_recursion(relational_expr, "AND_X0_X1_X0\n", "&", bitwise_expr_stub);
  2166. general_recursion(relational_expr, "AND_X0_X1_X0\n", "&&", bitwise_expr_stub);
  2167. general_recursion(relational_expr, "OR_X0_X1_X0\n", "|", bitwise_expr_stub);
  2168. general_recursion(relational_expr, "OR_X0_X1_X0\n", "||", bitwise_expr_stub);
  2169. general_recursion(relational_expr, "XOR_X0_X1_X0\n", "^", bitwise_expr_stub);
  2170. }
  2171. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2172. {
  2173. general_recursion(relational_expr, "rd_a0 rs1_a1 rs2_a0 and\n", "&", bitwise_expr_stub);
  2174. general_recursion(relational_expr, "rd_a0 rs1_a1 rs2_a0 and\n", "&&", bitwise_expr_stub);
  2175. general_recursion(relational_expr, "rd_a0 rs1_a1 rs2_a0 or\n", "|", bitwise_expr_stub);
  2176. general_recursion(relational_expr, "rd_a0 rs1_a1 rs2_a0 or\n", "||", bitwise_expr_stub);
  2177. general_recursion(relational_expr, "rd_a0 rs1_a1 rs2_a0 xor\n", "^", bitwise_expr_stub);
  2178. }
  2179. }
  2180. void bitwise_expr(void)
  2181. {
  2182. relational_expr();
  2183. bitwise_expr_stub();
  2184. }
  2185. /*
  2186. * expression:
  2187. * bitwise-or-expr
  2188. * bitwise-or-expr = expression
  2189. */
  2190. void primary_expr(void)
  2191. {
  2192. require(NULL != global_token, "Received EOF where primary expression expected\n");
  2193. if(match("&", global_token->s))
  2194. {
  2195. Address_of = TRUE;
  2196. require_extra_token();
  2197. }
  2198. else
  2199. {
  2200. Address_of = FALSE;
  2201. }
  2202. if(match("sizeof", global_token->s)) emit_load_immediate(REGISTER_ZERO, unary_expr_sizeof(), "load sizeof");
  2203. else if(match("-", global_token->s))
  2204. {
  2205. if((KNIGHT_POSIX != Architecture) && (KNIGHT_NATIVE != Architecture))
  2206. {
  2207. emit_load_immediate(REGISTER_ZERO, 0, "primary expr");
  2208. }
  2209. common_recursion(primary_expr);
  2210. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("NEG R0 R0\n");
  2211. else if(X86 == Architecture) emit_out("sub_ebx,eax\nmov_eax,ebx\n");
  2212. else if(AMD64 == Architecture) emit_out("sub_rbx,rax\nmov_rax,rbx\n");
  2213. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 SUB R1 ARITH2_ALWAYS\n");
  2214. else if(AARCH64 == Architecture) emit_out("SUB_X0_X1_X0\n");
  2215. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 rs1_a1 rs2_a0 sub\n");
  2216. }
  2217. else if('!' == global_token->s[0])
  2218. {
  2219. if((RISCV32 != Architecture) && (RISCV64 != Architecture))
  2220. {
  2221. emit_load_immediate(REGISTER_ZERO, 1, "primary expr");
  2222. }
  2223. common_recursion(postfix_expr);
  2224. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("CMPU R0 R1 R0\nSET.G R0 R0 1\n");
  2225. else if(X86 == Architecture) emit_out("cmp\nseta_al\nmovzx_eax,al\n");
  2226. else if(AMD64 == Architecture) emit_out("cmp_rbx,rax\nseta_al\nmovzx_rax,al\n");
  2227. else if(ARMV7L == Architecture) emit_out("'0' R0 CMP R1 AUX_ALWAYS\n!0 R0 LOADI8_ALWAYS\n!1 R0 LOADI8_HI\n");
  2228. else if(AARCH64 == Architecture) emit_out("CMP_X1_X0\nSET_X0_TO_1\nSKIP_INST_HI\nSET_X0_TO_0\n");
  2229. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 rs1_a0 !1 sltiu\n");
  2230. }
  2231. else if('~' == global_token->s[0])
  2232. {
  2233. common_recursion(postfix_expr);
  2234. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("NOT R0 R0\n");
  2235. else if(X86 == Architecture) emit_out("not_eax\n");
  2236. else if(AMD64 == Architecture) emit_out("not_rax\n");
  2237. else if(ARMV7L == Architecture) emit_out("'0' R0 R0 MVN_ALWAYS\n");
  2238. else if(AARCH64 == Architecture) emit_out("MVN_X0\n");
  2239. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 rs1_a0 not\n");
  2240. }
  2241. else if(match("--", global_token->s) || match("++", global_token->s))
  2242. {
  2243. int is_subtract = global_token->s[0] == '-';
  2244. maybe_bootstrap_error("prefix operators --/++");
  2245. emit_out("# prefix inc/dec\n");
  2246. emit_push(REGISTER_ZERO, "Previous value");
  2247. require_extra_token();
  2248. postfix_expr();
  2249. emit_pop(REGISTER_ONE, "Restore previous value");
  2250. emit_push(REGISTER_ONE, "Previous value");
  2251. emit_push(REGISTER_ZERO, "Address of variable");
  2252. emit_dereference(REGISTER_ZERO, "Deref to get value");
  2253. int value = 1;
  2254. if(type_is_pointer(current_target))
  2255. {
  2256. value = current_target->type->size;
  2257. }
  2258. if(is_subtract)
  2259. {
  2260. emit_sub_immediate(REGISTER_ZERO, value, "Sub prefix from deref value");
  2261. }
  2262. else
  2263. {
  2264. emit_add_immediate(REGISTER_ZERO, value, "Add prefix to deref value");
  2265. }
  2266. emit_pop(REGISTER_ONE, "Address of variable");
  2267. /* Store REGISTER_ZERO in REGISTER_ONE deref */
  2268. emit_out(store_value(current_target->size));
  2269. emit_pop(REGISTER_ONE, "Previous value");
  2270. emit_out("# prefix inc/dec end\n");
  2271. }
  2272. else if(global_token->s[0] == '(')
  2273. {
  2274. require_extra_token();
  2275. expression();
  2276. require_match("Error in Primary expression\nDidn't get )\n", ")");
  2277. }
  2278. else if(global_token->s[0] == '\'') primary_expr_char();
  2279. else if(global_token->s[0] == '"') primary_expr_string();
  2280. else if(in_set(global_token->s[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_")) primary_expr_variable();
  2281. else if(global_token->s[0] == '*') primary_expr_variable();
  2282. else if(in_set(global_token->s[0], "0123456789"))
  2283. {
  2284. primary_expr_number(global_token->s);
  2285. require_extra_token();
  2286. }
  2287. else primary_expr_failure();
  2288. }
  2289. char* compound_operation(char* operator, int is_signed)
  2290. {
  2291. char* operation = "";
  2292. if(match("+=", operator))
  2293. {
  2294. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2295. {
  2296. if(is_signed) operation = "ADD R0 R1 R0\n";
  2297. else operation = "ADDU R0 R1 R0\n";
  2298. }
  2299. else emit_add(REGISTER_ZERO, REGISTER_ONE, "compound operation");
  2300. }
  2301. else if(match("-=", operator))
  2302. {
  2303. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2304. {
  2305. if(is_signed) operation = "SUB R0 R1 R0\n";
  2306. else operation = "SUBU R0 R1 R0\n";
  2307. }
  2308. else if(X86 == Architecture) operation = "sub_ebx,eax\nmov_eax,ebx\n";
  2309. else if(AMD64 == Architecture) operation = "sub_rbx,rax\nmov_rax,rbx\n";
  2310. else if(ARMV7L == Architecture) operation = "'0' R0 R0 SUB R1 ARITH2_ALWAYS\n";
  2311. else if(AARCH64 == Architecture) operation = "SUB_X0_X1_X0\n";
  2312. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) operation = "rd_a0 rs1_a1 rs2_a0 sub\n";
  2313. }
  2314. else if(match("*=", operator))
  2315. {
  2316. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2317. {
  2318. if(is_signed) operation = "MUL R0 R1 R0\n";
  2319. else operation = "MULU R0 R1 R0\n";
  2320. }
  2321. else if(X86 == Architecture)
  2322. {
  2323. if(is_signed) operation = "imul_ebx\n";
  2324. else operation = "mul_ebx\n";
  2325. }
  2326. else if(AMD64 == Architecture)
  2327. {
  2328. if(is_signed) operation = "imul_rbx\n";
  2329. else operation = "mul_rbx\n";
  2330. }
  2331. else if(ARMV7L == Architecture) operation = "'9' R0 '0' R1 MULS R0 ARITH2_ALWAYS\n";
  2332. else if(AARCH64 == Architecture) operation = "MUL_X0_X1_X0\n";
  2333. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) operation = "rd_a0 rs1_a1 rs2_a0 mul\n";
  2334. }
  2335. else if(match("/=", operator))
  2336. {
  2337. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2338. {
  2339. if(is_signed) operation = "DIV R0 R1 R0\n";
  2340. else operation = "DIVU R0 R1 R0\n";
  2341. }
  2342. else if(X86 == Architecture)
  2343. {
  2344. if (is_signed) operation = "xchg_ebx,eax\ncdq\nidiv_ebx\n";
  2345. else operation = "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\n";
  2346. }
  2347. else if(AMD64 == Architecture)
  2348. {
  2349. if(is_signed) operation = "xchg_rbx,rax\ncqo\nidiv_rbx\n";
  2350. else operation = "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\n";
  2351. }
  2352. else if(ARMV7L == Architecture)
  2353. {
  2354. if(is_signed) operation = "{LR} PUSH_ALWAYS\n^~divides CALL_ALWAYS\n{LR} POP_ALWAYS\n";
  2355. else operation = "{LR} PUSH_ALWAYS\n^~divide CALL_ALWAYS\n{LR} POP_ALWAYS\n";
  2356. }
  2357. else if(AARCH64 == Architecture)
  2358. {
  2359. if(is_signed) operation = "SDIV_X0_X1_X0\n";
  2360. else operation = "UDIV_X0_X1_X0\n";
  2361. }
  2362. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2363. {
  2364. if(is_signed) operation = "rd_a0 rs1_a1 rs2_a0 div\n";
  2365. else operation = "rd_a0 rs1_a1 rs2_a0 divu\n";
  2366. }
  2367. }
  2368. else if(match("%=", operator))
  2369. {
  2370. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2371. {
  2372. if(is_signed) operation = "MOD R0 R1 R0\n";
  2373. else operation = "MODU R0 R1 R0\n";
  2374. }
  2375. else if(X86 == Architecture)
  2376. {
  2377. if(is_signed) operation = "xchg_ebx,eax\ncdq\nidiv_ebx\nmov_eax,edx\n";
  2378. else operation = "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\nmov_eax,edx\n";
  2379. }
  2380. else if(AMD64 == Architecture)
  2381. {
  2382. if(is_signed) operation = "xchg_rbx,rax\ncqo\nidiv_rbx\nmov_rax,rdx\n";
  2383. else operation = "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\nmov_rax,rdx\n";
  2384. }
  2385. else if(ARMV7L == Architecture)
  2386. {
  2387. if(is_signed) operation = "{LR} PUSH_ALWAYS\n^~moduluss CALL_ALWAYS\n{LR} POP_ALWAYS\n";
  2388. else operation = "{LR} PUSH_ALWAYS\n^~modulus CALL_ALWAYS\n{LR} POP_ALWAYS\n";
  2389. }
  2390. else if(AARCH64 == Architecture)
  2391. {
  2392. if(is_signed) operation = "SDIV_X2_X1_X0\nMSUB_X0_X0_X2_X1\n";
  2393. else operation = "UDIV_X2_X1_X0\nMSUB_X0_X0_X2_X1\n";
  2394. }
  2395. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2396. {
  2397. if(is_signed) operation = "rd_a0 rs1_a1 rs2_a0 rem\n";
  2398. else operation = "rd_a0 rs1_a1 rs2_a0 remu\n";
  2399. }
  2400. }
  2401. else if(match("<<=", operator))
  2402. {
  2403. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2404. {
  2405. if(is_signed) operation = "SAL R0 R1 R0\n";
  2406. else operation = "SL0 R0 R1 R0\n";
  2407. }
  2408. else if(X86 == Architecture)
  2409. {
  2410. if(is_signed) operation = "mov_ecx,eax\nmov_eax,ebx\nsal_eax,cl\n";
  2411. else operation = "mov_ecx,eax\nmov_eax,ebx\nshl_eax,cl\n";
  2412. }
  2413. else if(AMD64 == Architecture)
  2414. {
  2415. if(is_signed) operation = "mov_rcx,rax\nmov_rax,rbx\nsal_rax,cl\n";
  2416. else operation = "mov_rcx,rax\nmov_rax,rbx\nshl_rax,cl\n";
  2417. }
  2418. else if(ARMV7L == Architecture) operation = "LEFT R1 R0 R0 SHIFT AUX_ALWAYS\n";
  2419. else if(AARCH64 == Architecture) operation = "LSHIFT_X0_X1_X0\n";
  2420. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) operation = "rd_a0 rs1_a1 rs2_a0 sll\n";
  2421. }
  2422. else if(match(">>=", operator))
  2423. {
  2424. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
  2425. {
  2426. if(is_signed) operation = "SAR R0 R1 R0\n";
  2427. else operation = "SR0 R0 R1 R0\n";
  2428. }
  2429. else if(X86 == Architecture)
  2430. {
  2431. if(is_signed) operation = "mov_ecx,eax\nmov_eax,ebx\nsar_eax,cl\n";
  2432. else operation = "mov_ecx,eax\nmov_eax,ebx\nshr_eax,cl\n";
  2433. }
  2434. else if(AMD64 == Architecture)
  2435. {
  2436. if(is_signed) operation = "mov_rcx,rax\nmov_rax,rbx\nsar_rax,cl\n";
  2437. else operation = "mov_rcx,rax\nmov_rax,rbx\nshr_rax,cl\n";
  2438. }
  2439. else if(ARMV7L == Architecture)
  2440. {
  2441. if(is_signed) operation = "ARITH_RIGHT R1 R0 R0 SHIFT AUX_ALWAYS\n";
  2442. else operation = "RIGHT R1 R0 R0 SHIFT AUX_ALWAYS\n";
  2443. }
  2444. else if(AARCH64 == Architecture)
  2445. {
  2446. if(is_signed) operation = "ARITH_RSHIFT_X0_X1_X0\n";
  2447. else operation = "LOGICAL_RSHIFT_X0_X1_X0\n";
  2448. }
  2449. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  2450. {
  2451. if(is_signed) operation = "rd_a0 rs1_a1 rs2_a0 sra\n";
  2452. else operation = "rd_a0 rs1_a1 rs2_a0 srl\n";
  2453. }
  2454. }
  2455. else if(match("&=", operator))
  2456. {
  2457. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) operation = "AND R0 R0 R1\n";
  2458. else if(X86 == Architecture) operation = "and_eax,ebx\n";
  2459. else if(AMD64 == Architecture) operation = "and_rax,rbx\n";
  2460. else if(ARMV7L == Architecture) operation = "NO_SHIFT R0 R0 AND R1 ARITH2_ALWAYS\n";
  2461. else if(AARCH64 == Architecture) operation = "AND_X0_X1_X0\n";
  2462. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) operation = "rd_a0 rs1_a1 rs2_a0 and\n";
  2463. }
  2464. else if(match("^=", operator))
  2465. {
  2466. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) operation = "XOR R0 R0 R1\n";
  2467. else if(X86 == Architecture) operation = "xor_eax,ebx\n";
  2468. else if(AMD64 == Architecture) operation = "xor_rax,rbx\n";
  2469. else if(ARMV7L == Architecture) operation = "'0' R0 R0 XOR R1 ARITH2_ALWAYS\n";
  2470. else if(AARCH64 == Architecture) operation = "XOR_X0_X1_X0\n";
  2471. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) operation = "rd_a0 rs1_a1 rs2_a0 xor\n";
  2472. }
  2473. else if(match("|=", operator))
  2474. {
  2475. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) operation = "OR R0 R0 R1\n";
  2476. else if(X86 == Architecture) operation = "or_eax,ebx\n";
  2477. else if(AMD64 == Architecture) operation = "or_rax,rbx\n";
  2478. else if(ARMV7L == Architecture) operation = "NO_SHIFT R0 R0 OR R1 AUX_ALWAYS\n";
  2479. else if(AARCH64 == Architecture) operation = "OR_X0_X1_X0\n";
  2480. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) operation = "rd_a0 rs1_a1 rs2_a0 or\n";
  2481. }
  2482. else
  2483. {
  2484. fputs("Found illegal compound assignment operator: ", stderr);
  2485. fputs(operator, stderr);
  2486. fputc('\n', stderr);
  2487. exit(EXIT_FAILURE);
  2488. }
  2489. return operation;
  2490. }
  2491. void expression(void)
  2492. {
  2493. bitwise_expr();
  2494. if(match("=", global_token->s))
  2495. {
  2496. char* store = "";
  2497. if(match("]", global_token->prev->s))
  2498. {
  2499. store = store_value(current_target->type->size);
  2500. }
  2501. else
  2502. {
  2503. store = store_value(current_target->size);
  2504. }
  2505. common_recursion(expression);
  2506. emit_out(store);
  2507. current_target = integer;
  2508. }
  2509. else if(is_compound_assignment(global_token->s))
  2510. {
  2511. maybe_bootstrap_error("compound operator");
  2512. char* load = "";
  2513. char* operation = "";
  2514. char* store = "";
  2515. struct type* last_type = current_target;
  2516. int is_array_indexed = match("]", global_token->prev->s);
  2517. if(!is_array_indexed || !match("char*", current_target->name))
  2518. {
  2519. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) load = "LOAD R1 R1 0\n";
  2520. else if(X86 == Architecture) load = "mov_ebx,[ebx]\n";
  2521. else if(AMD64 == Architecture) load = "mov_rbx,[rbx]\n";
  2522. else if(ARMV7L == Architecture) load = "!0 R1 LOAD32 R1 MEMORY\n";
  2523. else if(AARCH64 == Architecture) load = "DEREF_X1\n";
  2524. else if(RISCV32 == Architecture) load = "rd_a1 rs1_a1 lw\n";
  2525. else if(RISCV64 == Architecture) load = "rd_a1 rs1_a1 ld\n";
  2526. }
  2527. else
  2528. {
  2529. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) load = "LOAD8 R1 R1 0\n";
  2530. else if(X86 == Architecture) load = "movsx_ebx,BYTE_PTR_[ebx]\n";
  2531. else if(AMD64 == Architecture) load = "movsx_rbx,BYTE_PTR_[rbx]\n";
  2532. else if(ARMV7L == Architecture) load = "LOADU8 R1 LOAD R1 MEMORY\n";
  2533. else if(AARCH64 == Architecture) load = "DEREF_X1_BYTE\n";
  2534. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) load = "rd_a1 rs1_a1 lbu\n";
  2535. }
  2536. char *operator = global_token->s;
  2537. if(is_array_indexed)
  2538. {
  2539. store = store_value(current_target->type->size);
  2540. }
  2541. else
  2542. {
  2543. store = store_value(current_target->size);
  2544. }
  2545. int should_apply_pointer_arithmetic = type_is_pointer(current_target) && !is_array_indexed;
  2546. /* We need this before it's changed by the following expression */
  2547. int object_size = current_target->type->size;
  2548. common_recursion(expression);
  2549. current_target = promote_type(current_target, last_type);
  2550. emit_push(REGISTER_ONE, "compound operator");
  2551. emit_out(load);
  2552. if(should_apply_pointer_arithmetic) {
  2553. multiply_by_object_size(object_size);
  2554. }
  2555. operation = compound_operation(operator, current_target->is_signed);
  2556. emit_out(operation);
  2557. emit_pop(REGISTER_ONE, "compound operator");
  2558. emit_out(store);
  2559. current_target = integer;
  2560. }
  2561. }
  2562. int iskeywordp(char* s)
  2563. {
  2564. if(match("auto", s)) return TRUE;
  2565. if(match("_Bool", s)) return TRUE;
  2566. if(match("break", s)) return TRUE;
  2567. if(match("case", s)) return TRUE;
  2568. if(match("char", s)) return TRUE;
  2569. if(match("const", s)) return TRUE;
  2570. if(match("continue", s)) return TRUE;
  2571. if(match("default", s)) return TRUE;
  2572. if(match("do", s)) return TRUE;
  2573. if(match("double", s)) return TRUE;
  2574. if(match("else", s)) return TRUE;
  2575. if(match("enum", s)) return TRUE;
  2576. if(match("extern", s)) return TRUE;
  2577. if(match("float", s)) return TRUE;
  2578. if(match("for", s)) return TRUE;
  2579. if(match("goto", s)) return TRUE;
  2580. if(match("if", s)) return TRUE;
  2581. if(match("int", s)) return TRUE;
  2582. if(match("long", s)) return TRUE;
  2583. if(match("_Noreturn", s)) return TRUE;
  2584. if(match("register", s)) return TRUE;
  2585. if(match("restrict", s)) return TRUE;
  2586. if(match("return", s)) return TRUE;
  2587. if(match("short", s)) return TRUE;
  2588. if(match("signed", s)) return TRUE;
  2589. if(match("sizeof", s)) return TRUE;
  2590. if(match("static", s)) return TRUE;
  2591. if(match("struct", s)) return TRUE;
  2592. if(match("switch", s)) return TRUE;
  2593. if(match("typedef", s)) return TRUE;
  2594. if(match("union", s)) return TRUE;
  2595. if(match("unsigned", s)) return TRUE;
  2596. if(match("void", s)) return TRUE;
  2597. if(match("volatile", s)) return TRUE;
  2598. if(match("while", s)) return TRUE;
  2599. return FALSE;
  2600. }
  2601. /* Similar to integer division a / b but rounds up */
  2602. unsigned ceil_div(unsigned a, unsigned b)
  2603. {
  2604. return (a + b - 1) / b;
  2605. }
  2606. int locals_depth;
  2607. /* Process local variable */
  2608. void collect_local(void)
  2609. {
  2610. if(NULL != break_target_func)
  2611. {
  2612. maybe_bootstrap_error("Variable inside loop");
  2613. }
  2614. struct type* type_size = type_name();
  2615. if(type_size->size == NO_STRUCT_DEFINITION)
  2616. {
  2617. line_error();
  2618. fputs("Usage of struct '", stderr);
  2619. fputs(type_size->name, stderr);
  2620. fputs("' without definition (forward declaration only).\n", stderr);
  2621. exit(EXIT_FAILURE);
  2622. }
  2623. /* Declarations do not have the same pointer level so we'll need to find the actual type */
  2624. struct type* base_type = type_size->type->type;
  2625. struct type* current_type = type_size;
  2626. require(NULL != global_token, "Received EOF while collecting locals\n");
  2627. struct token_list* list_to_append_to = function->locals;
  2628. struct token_list* a;
  2629. unsigned struct_depth_adjustment;
  2630. char* name;
  2631. int function_depth_offset = 0;
  2632. do
  2633. {
  2634. if(global_token->s[0] == '(')
  2635. {
  2636. require_extra_token();
  2637. require_match("Required '*' after '(' in local function pointer.\n", "*");
  2638. require(NULL != global_token->s, "NULL token in local function pointer");
  2639. name = global_token->s;
  2640. require_extra_token();
  2641. require_match("Required ')' after name in local function pointer.\n", ")");
  2642. require_match("Required '(' after ')' in local function pointer.\n", "(");
  2643. while(global_token->s[0] != ')')
  2644. {
  2645. type_name();
  2646. if(global_token->s[0] == ',')
  2647. {
  2648. require_extra_token();
  2649. }
  2650. }
  2651. require_extra_token();
  2652. current_type = function_pointer;
  2653. }
  2654. else
  2655. {
  2656. name = global_token->s;
  2657. require_extra_token();
  2658. }
  2659. a = sym_declare(name, current_type, list_to_append_to, TLO_LOCAL);
  2660. list_to_append_to = a;
  2661. require(!in_set(name[0], "[{(<=>)}]|&!^%;:'\""), "forbidden character in local variable name\n");
  2662. require(!iskeywordp(name), "You are not allowed to use a keyword as a local variable name\n");
  2663. emit_out("# Defining local ");
  2664. emit_out(name);
  2665. emit_out("\n");
  2666. a->array_modifier = 1;
  2667. if(match("[", global_token->s))
  2668. {
  2669. maybe_bootstrap_error("array on the stack");
  2670. a->options = a->options | TLO_LOCAL_ARRAY;
  2671. require_extra_token();
  2672. a->array_modifier = constant_expression();
  2673. if(a->array_modifier == 0)
  2674. {
  2675. line_error();
  2676. fputs("Size zero is not supported for arrays on the stack\n", stderr);
  2677. exit(EXIT_FAILURE);
  2678. }
  2679. else if(a->array_modifier < 0)
  2680. {
  2681. line_error();
  2682. fputs("Negative values are not supported for arrays on the stack\n", stderr);
  2683. exit(EXIT_FAILURE);
  2684. }
  2685. else if(a->array_modifier > 0x100000)
  2686. {
  2687. line_error();
  2688. fputs("M2-Planet is very inefficient so you probably don't want to allocate over 1MB onto the stack\n", stderr);
  2689. exit(EXIT_FAILURE);
  2690. }
  2691. require_match("ERROR in collect_local\nMissing ] after local array size\n", "]");
  2692. }
  2693. if(NULL != function->locals)
  2694. {
  2695. function_depth_offset = function->locals->depth;
  2696. }
  2697. struct_depth_adjustment = ceil_div(a->type->size * a->array_modifier, register_size) * register_size;
  2698. if(stack_direction == STACK_DIRECTION_PLUS)
  2699. {
  2700. a->depth = function_depth_offset + struct_depth_adjustment;
  2701. }
  2702. else
  2703. {
  2704. a->depth = function_depth_offset - struct_depth_adjustment;
  2705. }
  2706. locals_depth = locals_depth + struct_depth_adjustment;
  2707. function->locals = a;
  2708. if(match("=", global_token->s))
  2709. {
  2710. if(a->array_modifier != 1)
  2711. {
  2712. line_error();
  2713. fputs("Stack arrays can not be initialized.\n", stderr);
  2714. exit(EXIT_FAILURE);
  2715. }
  2716. require_extra_token();
  2717. expression();
  2718. load_address_of_variable_into_register(REGISTER_ONE, name);
  2719. /* Store value of REGISTER_ZERO in REGISTER_ONE deref.
  2720. * The result of expression() will be in REGISTER_ZERO. */
  2721. emit_out(store_value(type_size->size));
  2722. }
  2723. if(global_token->s[0] == ',')
  2724. {
  2725. maybe_bootstrap_error("multiple variables per statement");
  2726. require_extra_token();
  2727. current_type = base_type;
  2728. while(global_token->s[0] == '*')
  2729. {
  2730. current_type = current_type->indirect;
  2731. require_extra_token();
  2732. }
  2733. }
  2734. }
  2735. while(global_token->s[0] != ';');
  2736. require_match("ERROR in collect_local\nMissing ;\n", ";");
  2737. }
  2738. void statement(void);
  2739. /* Evaluate if statements */
  2740. void process_if(void)
  2741. {
  2742. char* number_string = int2str(current_count, 10, TRUE);
  2743. current_count = current_count + 1;
  2744. emit_out("# IF_");
  2745. uniqueID_out(function->s, number_string);
  2746. global_token = global_token->next;
  2747. require_match("ERROR in process_if\nMISSING (\n", "(");
  2748. expression();
  2749. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.Z R0 @ELSE_");
  2750. else if(X86 == Architecture) emit_out("test_eax,eax\nje %ELSE_");
  2751. else if(AMD64 == Architecture) emit_out("test_rax,rax\nje %ELSE_");
  2752. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~ELSE_");
  2753. else if(AARCH64 == Architecture) emit_out("CBNZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&ELSE_");
  2754. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rs1_a0 @8 bnez\n$ELSE_");
  2755. uniqueID_out(function->s, number_string);
  2756. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\n");
  2757. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2758. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2759. require_match("ERROR in process_if\nMISSING )\n", ")");
  2760. statement();
  2761. require(NULL != global_token, "Reached EOF inside of function\n");
  2762. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @_END_IF_");
  2763. else if(X86 == Architecture) emit_out("jmp %_END_IF_");
  2764. else if(AMD64 == Architecture) emit_out("jmp %_END_IF_");
  2765. else if(ARMV7L == Architecture) emit_out("^~_END_IF_");
  2766. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&_END_IF_");
  2767. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$_END_IF_");
  2768. uniqueID_out(function->s, number_string);
  2769. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  2770. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2771. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2772. emit_out(":ELSE_");
  2773. uniqueID_out(function->s, number_string);
  2774. if(match("else", global_token->s))
  2775. {
  2776. require_extra_token();
  2777. statement();
  2778. require_token();
  2779. }
  2780. emit_out(":_END_IF_");
  2781. uniqueID_out(function->s, number_string);
  2782. }
  2783. void process_case(void)
  2784. {
  2785. process_case_iter:
  2786. require(NULL != global_token, "incomplete case statement\n");
  2787. if(match("case", global_token->s)) return;
  2788. if(match(":default", global_token->s)) return;
  2789. if(match("break", global_token->s))
  2790. {
  2791. statement();
  2792. require(NULL != global_token, "incomplete case statement\n");
  2793. }
  2794. else
  2795. {
  2796. statement();
  2797. require(NULL != global_token, "incomplete case statement\n");
  2798. goto process_case_iter;
  2799. }
  2800. }
  2801. void process_switch(void)
  2802. {
  2803. maybe_bootstrap_error("switch/case statements");
  2804. struct token_list* nested_locals = break_frame;
  2805. char* nested_break_head = break_target_head;
  2806. char* nested_break_func = break_target_func;
  2807. char* nested_break_num = break_target_num;
  2808. char* nested_continue_head = continue_target_head;
  2809. char* number_string = int2str(current_count, 10, TRUE);
  2810. current_count = current_count + 1;
  2811. break_target_head = "_SWITCH_END_";
  2812. continue_target_head = NULL; /* don't allow continue in switch statements */
  2813. break_target_num = number_string;
  2814. break_frame = function->locals;
  2815. break_target_func = function->s;
  2816. emit_out("# switch_");
  2817. uniqueID_out(function->s, number_string);
  2818. /* get what we are casing on */
  2819. global_token = global_token->next;
  2820. require_match("ERROR in process_switch\nMISSING (\n", "(");
  2821. expression();
  2822. require_match("ERROR in process_switch\nMISSING )\n", ")");
  2823. /* Put the value in R1 as it is currently in R0 */
  2824. emit_move(REGISTER_ONE, REGISTER_ZERO, "process switch");
  2825. /* Jump to the switch table */
  2826. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @_SWITCH_TABLE_");
  2827. else if(X86 == Architecture) emit_out("jmp %_SWITCH_TABLE_");
  2828. else if(AMD64 == Architecture) emit_out("jmp %_SWITCH_TABLE_");
  2829. else if(ARMV7L == Architecture) emit_out("^~_SWITCH_TABLE_");
  2830. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&_SWITCH_TABLE_");
  2831. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$_SWITCH_TABLE_");
  2832. uniqueID_out(function->s, number_string);
  2833. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  2834. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2835. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2836. /* must be switch (exp) {$STATEMENTS}; form */
  2837. require_match("ERROR in process_switch\nMISSING {\n", "{");
  2838. struct case_list* backtrack = NULL;
  2839. int value;
  2840. process_switch_iter:
  2841. require(NULL != global_token, "incomplete switch statement\n");
  2842. if(match("case", global_token->s))
  2843. {
  2844. require_extra_token();
  2845. value = constant_expression();
  2846. require_token();
  2847. if(':' == global_token->s[0])
  2848. {
  2849. struct case_list* c = calloc(1, sizeof(struct case_list));
  2850. c->next = backtrack;
  2851. c->value = int2str(value, 10, FALSE);
  2852. backtrack = c;
  2853. emit_out(":_SWITCH_CASE_");
  2854. emit_out(c->value);
  2855. emit_out("_");
  2856. uniqueID_out(function->s, number_string);
  2857. require_extra_token();
  2858. process_case();
  2859. require_token();
  2860. }
  2861. else line_error();
  2862. goto process_switch_iter;
  2863. }
  2864. else if(match(":default", global_token->s))
  2865. { /* because of how M2-Planet treats labels */
  2866. require_extra_token();
  2867. emit_out(":_SWITCH_DEFAULT_");
  2868. uniqueID_out(function->s, number_string);
  2869. require(NULL != global_token, "recieved EOF before switch closing }\n");
  2870. /* collect statements until } */
  2871. while(!match("}", global_token->s))
  2872. {
  2873. statement();
  2874. require(NULL != global_token, "recieved EOF before switch closing }\n");
  2875. }
  2876. /* jump over the switch table */
  2877. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @_SWITCH_END_");
  2878. else if(X86 == Architecture) emit_out("jmp %_SWITCH_END_");
  2879. else if(AMD64 == Architecture) emit_out("jmp %_SWITCH_END_");
  2880. else if(ARMV7L == Architecture) emit_out("^~_SWITCH_END_");
  2881. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&_SWITCH_END_");
  2882. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$_SWITCH_END_");
  2883. uniqueID_out(function->s, number_string);
  2884. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  2885. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2886. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2887. }
  2888. /* Switch statements must end with } */
  2889. require_match("ERROR in process_switch\nMISSING }\n", "}");
  2890. /* create the table */
  2891. emit_out(":_SWITCH_TABLE_");
  2892. uniqueID_out(function->s, number_string);
  2893. struct case_list* hold;
  2894. while(NULL != backtrack)
  2895. {
  2896. /* put case value in R0 as the switch (value) is in R1 */
  2897. primary_expr_number(backtrack->value);
  2898. hold = backtrack->next;
  2899. /* compare R0 and R1 and jump to case if equal */
  2900. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("CMPU R0 R0 R1\nJUMP.E R0 @_SWITCH_CASE_");
  2901. else if(X86 == Architecture) emit_out("cmp\nje %_SWITCH_CASE_");
  2902. else if(AMD64 == Architecture) emit_out("cmp_rbx,rax\nje %_SWITCH_CASE_");
  2903. else if(ARMV7L == Architecture) emit_out("'0' R0 CMP R1 AUX_ALWAYS\n^~_SWITCH_CASE_");
  2904. else if(AARCH64 == Architecture) emit_out("CMP_X1_X0\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&_SWITCH_CASE_");
  2905. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 rs1_a0 rs2_a1 sub\nrs1_a0 @8 bnez\n$_SWITCH_CASE_");
  2906. emit_out(backtrack->value);
  2907. emit_out("_");
  2908. uniqueID_out(function->s, number_string);
  2909. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\n");
  2910. else if(AARCH64 == Architecture) emit_out("\nSKIP_INST_NE\nBR_X16\n");
  2911. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2912. free(backtrack);
  2913. backtrack = hold;
  2914. }
  2915. /* Default to :default */
  2916. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @_SWITCH_DEFAULT_");
  2917. else if(X86 == Architecture) emit_out("jmp %_SWITCH_DEFAULT_");
  2918. else if(AMD64 == Architecture) emit_out("jmp %_SWITCH_DEFAULT_");
  2919. else if(ARMV7L == Architecture) emit_out("^~_SWITCH_DEFAULT_");
  2920. else if(AARCH64 == Architecture) emit_out("SKIP_32_DATA\n&_SWITCH_DEFAULT_");
  2921. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$_SWITCH_DEFAULT_");
  2922. uniqueID_out(function->s, number_string);
  2923. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  2924. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2925. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2926. /* put the exit of the switch */
  2927. emit_out(":_SWITCH_END_");
  2928. uniqueID_out(function->s, number_string);
  2929. break_target_head = nested_break_head;
  2930. break_target_func = nested_break_func;
  2931. break_target_num = nested_break_num;
  2932. continue_target_head = nested_continue_head;
  2933. break_frame = nested_locals;
  2934. }
  2935. void process_for(void)
  2936. {
  2937. struct token_list* nested_locals = break_frame;
  2938. char* nested_break_head = break_target_head;
  2939. char* nested_break_func = break_target_func;
  2940. char* nested_break_num = break_target_num;
  2941. char* nested_continue_head = continue_target_head;
  2942. char* number_string = int2str(current_count, 10, TRUE);
  2943. current_count = current_count + 1;
  2944. break_target_head = "FOR_END_";
  2945. continue_target_head = "FOR_ITER_";
  2946. break_target_num = number_string;
  2947. break_frame = function->locals;
  2948. break_target_func = function->s;
  2949. emit_out("# FOR_initialization_");
  2950. uniqueID_out(function->s, number_string);
  2951. require_extra_token();
  2952. require_match("ERROR in process_for\nMISSING (\n", "(");
  2953. if(!match(";",global_token->s))
  2954. {
  2955. expression();
  2956. }
  2957. emit_out(":FOR_");
  2958. uniqueID_out(function->s, number_string);
  2959. require_match("ERROR in process_for\nMISSING ;1\n", ";");
  2960. expression();
  2961. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.Z R0 @FOR_END_");
  2962. else if(X86 == Architecture) emit_out("test_eax,eax\nje %FOR_END_");
  2963. else if(AMD64 == Architecture) emit_out("test_rax,rax\nje %FOR_END_");
  2964. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~FOR_END_");
  2965. else if(AARCH64 == Architecture) emit_out("CBNZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_END_");
  2966. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rs1_a0 @8 bnez\n$FOR_END_");
  2967. uniqueID_out(function->s, number_string);
  2968. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\n");
  2969. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2970. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2971. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @FOR_THEN_");
  2972. else if(X86 == Architecture) emit_out("jmp %FOR_THEN_");
  2973. else if(AMD64 == Architecture) emit_out("jmp %FOR_THEN_");
  2974. else if(ARMV7L == Architecture) emit_out("^~FOR_THEN_");
  2975. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_THEN_");
  2976. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$FOR_THEN_");
  2977. uniqueID_out(function->s, number_string);
  2978. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  2979. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2980. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2981. emit_out(":FOR_ITER_");
  2982. uniqueID_out(function->s, number_string);
  2983. require_match("ERROR in process_for\nMISSING ;2\n", ";");
  2984. expression();
  2985. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @FOR_");
  2986. else if(X86 == Architecture) emit_out("jmp %FOR_");
  2987. else if(AMD64 == Architecture) emit_out("jmp %FOR_");
  2988. else if(ARMV7L == Architecture) emit_out("^~FOR_");
  2989. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_");
  2990. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$FOR_");
  2991. uniqueID_out(function->s, number_string);
  2992. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  2993. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  2994. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  2995. emit_out(":FOR_THEN_");
  2996. uniqueID_out(function->s, number_string);
  2997. require_match("ERROR in process_for\nMISSING )\n", ")");
  2998. statement();
  2999. require(NULL != global_token, "Reached EOF inside of function\n");
  3000. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @FOR_ITER_");
  3001. else if(X86 == Architecture) emit_out("jmp %FOR_ITER_");
  3002. else if(AMD64 == Architecture) emit_out("jmp %FOR_ITER_");
  3003. else if(ARMV7L == Architecture) emit_out("^~FOR_ITER_");
  3004. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&FOR_ITER_");
  3005. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$FOR_ITER_");
  3006. uniqueID_out(function->s, number_string);
  3007. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  3008. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  3009. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  3010. emit_out(":FOR_END_");
  3011. uniqueID_out(function->s, number_string);
  3012. break_target_head = nested_break_head;
  3013. break_target_func = nested_break_func;
  3014. break_target_num = nested_break_num;
  3015. continue_target_head = nested_continue_head;
  3016. break_frame = nested_locals;
  3017. }
  3018. /* Process Assembly statements */
  3019. void process_asm(void)
  3020. {
  3021. global_token = global_token->next;
  3022. require_match("ERROR in process_asm\nMISSING (\n", "(");
  3023. while('"' == global_token->s[0])
  3024. {
  3025. emit_out((global_token->s + 1));
  3026. emit_out("\n");
  3027. require_extra_token();
  3028. }
  3029. require_match("ERROR in process_asm\nMISSING )\n", ")");
  3030. require_match("ERROR in process_asm\nMISSING ;\n", ";");
  3031. }
  3032. /* Process do while loops */
  3033. void process_do(void)
  3034. {
  3035. struct token_list* nested_locals = break_frame;
  3036. char* nested_break_head = break_target_head;
  3037. char* nested_break_func = break_target_func;
  3038. char* nested_break_num = break_target_num;
  3039. char* nested_continue_head = continue_target_head;
  3040. char* number_string = int2str(current_count, 10, TRUE);
  3041. current_count = current_count + 1;
  3042. break_target_head = "DO_END_";
  3043. continue_target_head = "DO_TEST_";
  3044. break_target_num = number_string;
  3045. break_frame = function->locals;
  3046. break_target_func = function->s;
  3047. emit_out(":DO_");
  3048. uniqueID_out(function->s, number_string);
  3049. require_extra_token();
  3050. statement();
  3051. require_token();
  3052. emit_out(":DO_TEST_");
  3053. uniqueID_out(function->s, number_string);
  3054. require_match("ERROR in process_do\nMISSING while\n", "while");
  3055. require_match("ERROR in process_do\nMISSING (\n", "(");
  3056. expression();
  3057. require_match("ERROR in process_do\nMISSING )\n", ")");
  3058. require_match("ERROR in process_do\nMISSING ;\n", ";");
  3059. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.NZ R0 @DO_");
  3060. else if(X86 == Architecture) emit_out("test_eax,eax\njne %DO_");
  3061. else if(AMD64 == Architecture) emit_out("test_rax,rax\njne %DO_");
  3062. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~DO_");
  3063. else if(AARCH64 == Architecture) emit_out("CBZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&DO_");
  3064. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rs1_a0 @DO_END_");
  3065. uniqueID_out(function->s, number_string);
  3066. if(ARMV7L == Architecture) emit_out(" JUMP_NE\n");
  3067. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  3068. else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
  3069. {
  3070. emit_out("beqz\n$DO_");
  3071. uniqueID_out(function->s, number_string);
  3072. emit_out("jal\n");
  3073. }
  3074. emit_out(":DO_END_");
  3075. uniqueID_out(function->s, number_string);
  3076. break_frame = nested_locals;
  3077. break_target_head = nested_break_head;
  3078. break_target_func = nested_break_func;
  3079. break_target_num = nested_break_num;
  3080. continue_target_head = nested_continue_head;
  3081. }
  3082. /* Process while loops */
  3083. void process_while(void)
  3084. {
  3085. struct token_list* nested_locals = break_frame;
  3086. char* nested_break_head = break_target_head;
  3087. char* nested_break_func = break_target_func;
  3088. char* nested_break_num = break_target_num;
  3089. char* nested_continue_head = continue_target_head;
  3090. char* number_string = int2str(current_count, 10, TRUE);
  3091. current_count = current_count + 1;
  3092. break_target_head = "END_WHILE_";
  3093. continue_target_head = "WHILE_";
  3094. break_target_num = number_string;
  3095. break_frame = function->locals;
  3096. break_target_func = function->s;
  3097. emit_out(":WHILE_");
  3098. uniqueID_out(function->s, number_string);
  3099. global_token = global_token->next;
  3100. require_match("ERROR in process_while\nMISSING (\n", "(");
  3101. expression();
  3102. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP.Z R0 @END_WHILE_");
  3103. else if(X86 == Architecture) emit_out("test_eax,eax\nje %END_WHILE_");
  3104. else if(AMD64 == Architecture) emit_out("test_rax,rax\nje %END_WHILE_");
  3105. else if(ARMV7L == Architecture) emit_out("!0 CMPI8 R0 IMM_ALWAYS\n^~END_WHILE_");
  3106. else if(AARCH64 == Architecture) emit_out("CBNZ_X0_PAST_BR\nLOAD_W16_AHEAD\nSKIP_32_DATA\n&END_WHILE_");
  3107. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rs1_a0 @8 bnez\n$END_WHILE_");
  3108. uniqueID_out(function->s, number_string);
  3109. if(ARMV7L == Architecture) emit_out(" JUMP_EQUAL\t");
  3110. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  3111. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  3112. emit_out("# THEN_while_");
  3113. uniqueID_out(function->s, number_string);
  3114. require_match("ERROR in process_while\nMISSING )\n", ")");
  3115. statement();
  3116. require(NULL != global_token, "Reached EOF inside of function\n");
  3117. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @WHILE_");
  3118. else if(X86 == Architecture) emit_out("jmp %WHILE_");
  3119. else if(AMD64 == Architecture) emit_out("jmp %WHILE_");
  3120. else if(ARMV7L == Architecture) emit_out("^~WHILE_");
  3121. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&WHILE_");
  3122. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$WHILE_");
  3123. uniqueID_out(function->s, number_string);
  3124. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS\n");
  3125. else if(AARCH64 == Architecture) emit_out("\nBR_X16\n");
  3126. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("jal\n");
  3127. emit_out(":END_WHILE_");
  3128. uniqueID_out(function->s, number_string);
  3129. break_target_head = nested_break_head;
  3130. break_target_func = nested_break_func;
  3131. break_target_num = nested_break_num;
  3132. continue_target_head = nested_continue_head;
  3133. break_frame = nested_locals;
  3134. }
  3135. /* Ensure that functions return */
  3136. void return_result(void)
  3137. {
  3138. require_extra_token();
  3139. if(global_token->s[0] != ';') expression();
  3140. require_match("ERROR in return_result\nMISSING ;\n", ";");
  3141. emit_move(REGISTER_STACK, REGISTER_LOCALS, "Undo local variables");
  3142. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("RET R15\n");
  3143. else if(X86 == Architecture) emit_out("ret\n");
  3144. else if(AMD64 == Architecture) emit_out("ret\n");
  3145. else if(ARMV7L == Architecture) emit_out("'1' LR RETURN\n");
  3146. else if(AARCH64 == Architecture) emit_out("RETURN\n");
  3147. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("ret\n");
  3148. }
  3149. void process_break(void)
  3150. {
  3151. if(NULL == break_target_head)
  3152. {
  3153. line_error();
  3154. fputs("Not inside of a loop or case statement\n", stderr);
  3155. exit(EXIT_FAILURE);
  3156. }
  3157. require_extra_token();
  3158. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @");
  3159. else if(X86 == Architecture) emit_out("jmp %");
  3160. else if(AMD64 == Architecture) emit_out("jmp %");
  3161. else if(ARMV7L == Architecture) emit_out("^~");
  3162. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&");
  3163. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$");
  3164. emit_out(break_target_head);
  3165. emit_out(break_target_func);
  3166. emit_out("_");
  3167. emit_out(break_target_num);
  3168. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS");
  3169. else if(AARCH64 == Architecture) emit_out("\nBR_X16");
  3170. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out(" jal");
  3171. emit_out("\n");
  3172. require_match("ERROR in break statement\nMissing ;\n", ";");
  3173. }
  3174. void process_continue(void)
  3175. {
  3176. if(NULL == continue_target_head)
  3177. {
  3178. line_error();
  3179. fputs("Not inside of a loop\n", stderr);
  3180. exit(EXIT_FAILURE);
  3181. }
  3182. require_extra_token();
  3183. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @");
  3184. else if(X86 == Architecture) emit_out("jmp %");
  3185. else if(AMD64 == Architecture) emit_out("jmp %");
  3186. else if(ARMV7L == Architecture) emit_out("^~");
  3187. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&");
  3188. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$");
  3189. emit_out(continue_target_head);
  3190. emit_out(break_target_func);
  3191. emit_out("_");
  3192. emit_out(break_target_num);
  3193. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS");
  3194. else if(AARCH64 == Architecture) emit_out("\nBR_X16");
  3195. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out(" jal");
  3196. emit_out("\n");
  3197. require_match("ERROR in continue statement\nMissing ;\n", ";");
  3198. }
  3199. void recursive_statement(void)
  3200. {
  3201. require_extra_token();
  3202. struct token_list* frame = function->locals;
  3203. while(!match("}", global_token->s))
  3204. {
  3205. statement();
  3206. require(NULL != global_token, "Received EOF in recursive statement prior to }\n");
  3207. }
  3208. global_token = global_token->next;
  3209. function->locals = frame;
  3210. }
  3211. /* Variables inside loops are currently just global variables */
  3212. void process_static_variable(void)
  3213. {
  3214. maybe_bootstrap_error("static local variable");
  3215. struct type* type_size = type_name();
  3216. char* name = global_token->s;
  3217. struct static_variable_list* variable = calloc(1, sizeof(struct static_variable_list));
  3218. variable->next = function_static_variables_list;
  3219. function_static_variables_list = variable;
  3220. variable->local_variable_name = name;
  3221. char* new_name = calloc(MAX_STRING, sizeof(char));
  3222. int offset = copy_string(new_name, function->s, MAX_STRING);
  3223. offset = offset + copy_string(new_name + offset, "_", MAX_STRING - offset);
  3224. copy_string(new_name + offset, name, MAX_STRING - offset);
  3225. variable->global_variable = sym_declare(new_name, type_size, NULL, TLO_STATIC);
  3226. require_extra_token();
  3227. declare_global_variable(type_size, variable->global_variable);
  3228. require_match("Expected ; after static variable declarations\n", ";");
  3229. }
  3230. /*
  3231. * statement:
  3232. * { statement-list-opt }
  3233. * type-name identifier ;
  3234. * type-name identifier = expression;
  3235. * if ( expression ) statement
  3236. * if ( expression ) statement else statement
  3237. * do statement while ( expression ) ;
  3238. * while ( expression ) statement
  3239. * for ( expression ; expression ; expression ) statement
  3240. * asm ( "assembly" ... "assembly" ) ;
  3241. * goto label ;
  3242. * label:
  3243. * return ;
  3244. * break ;
  3245. * expr ;
  3246. */
  3247. struct type* lookup_primitive_type(void);
  3248. void statement(void)
  3249. {
  3250. require(NULL != global_token, "expected a C statement but received EOF\n");
  3251. /* Always an integer until told otherwise */
  3252. current_target = integer;
  3253. struct token_list* current_token = global_token;
  3254. if(global_token->s[0] == '{')
  3255. {
  3256. recursive_statement();
  3257. }
  3258. else if(':' == global_token->s[0])
  3259. {
  3260. emit_out(global_token->s);
  3261. emit_out("\t#C goto label\n");
  3262. require_extra_token();
  3263. }
  3264. else if((NULL != lookup_primitive_type()) ||
  3265. match("enum", global_token->s) ||
  3266. match("struct", global_token->s) ||
  3267. match("union", global_token->s) ||
  3268. match("const", global_token->s))
  3269. {
  3270. /* Multi token lookup may move the global_token, but collect_local does a lookup of its own. */
  3271. global_token = current_token;
  3272. collect_local();
  3273. }
  3274. else if(match("if", global_token->s))
  3275. {
  3276. process_if();
  3277. }
  3278. else if(match("switch", global_token->s))
  3279. {
  3280. process_switch();
  3281. }
  3282. else if(match("do", global_token->s))
  3283. {
  3284. process_do();
  3285. }
  3286. else if(match("while", global_token->s))
  3287. {
  3288. process_while();
  3289. }
  3290. else if(match("for", global_token->s))
  3291. {
  3292. process_for();
  3293. }
  3294. else if(match("asm", global_token->s))
  3295. {
  3296. process_asm();
  3297. }
  3298. else if(match("goto", global_token->s))
  3299. {
  3300. require_extra_token();
  3301. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) emit_out("JUMP @");
  3302. else if(X86 == Architecture) emit_out("jmp %");
  3303. else if(AMD64 == Architecture) emit_out("jmp %");
  3304. else if(ARMV7L == Architecture) emit_out("^~");
  3305. else if(AARCH64 == Architecture) emit_out("LOAD_W16_AHEAD\nSKIP_32_DATA\n&");
  3306. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("$");
  3307. emit_out(global_token->s);
  3308. if(ARMV7L == Architecture) emit_out(" JUMP_ALWAYS");
  3309. else if(AARCH64 == Architecture) emit_out("\nBR_X16");
  3310. else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out(" jal");
  3311. emit_out("\n");
  3312. global_token = global_token->next;
  3313. require_match("ERROR in statement\nMissing ;\n", ";");
  3314. }
  3315. else if(match("return", global_token->s))
  3316. {
  3317. return_result();
  3318. }
  3319. else if(match("break", global_token->s))
  3320. {
  3321. process_break();
  3322. }
  3323. else if(match("continue", global_token->s))
  3324. {
  3325. process_continue();
  3326. }
  3327. else if(match("static", global_token->s))
  3328. {
  3329. require_extra_token();
  3330. process_static_variable();
  3331. }
  3332. else
  3333. {
  3334. expression();
  3335. require_match("ERROR in statement\nMISSING ;\n", ";");
  3336. }
  3337. }
  3338. /* Collect function arguments */
  3339. void collect_arguments(void)
  3340. {
  3341. require_extra_token();
  3342. struct type* type_size;
  3343. struct token_list* a;
  3344. while(!match(")", global_token->s))
  3345. {
  3346. if(global_token->s[0] == '.')
  3347. {
  3348. /* Periods can only be in the argument list as a variadic parameter
  3349. * so if there is a period it's part of a variadic parameter */
  3350. require_match("Invalid token found in variadic arguments token", ".");
  3351. require_match("Invalid token found in variadic arguments token", ".");
  3352. require_match("Invalid token found in variadic arguments token", ".");
  3353. maybe_bootstrap_error("variadic functions");
  3354. break;
  3355. }
  3356. type_size = type_name();
  3357. require(NULL != global_token, "Received EOF when attempting to collect arguments\n");
  3358. require(NULL != type_size, "Must have non-null type\n");
  3359. if(global_token->s[0] == ')')
  3360. {
  3361. /* foo(int,char,void) doesn't need anything done */
  3362. continue;
  3363. }
  3364. else if(global_token->s[0] != ',')
  3365. {
  3366. /* deal with foo(int a, char b) */
  3367. require(!in_set(global_token->s[0], "[{(<=>)}]|&!^%;:'\""), "forbidden character in argument variable name\n");
  3368. require(!iskeywordp(global_token->s), "You are not allowed to use a keyword as a argument variable name\n");
  3369. a = sym_declare(global_token->s, type_size, function->arguments, TLO_ARGUMENT);
  3370. if(NULL == function->arguments)
  3371. {
  3372. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = 0;
  3373. else if(X86 == Architecture) a->depth = -4;
  3374. else if(AMD64 == Architecture) a->depth = -8;
  3375. else if(ARMV7L == Architecture) a->depth = 4;
  3376. else if(AARCH64 == Architecture) a->depth = register_size;
  3377. else if(RISCV32 == Architecture) a->depth = -4;
  3378. else if(RISCV64 == Architecture) a->depth = -8;
  3379. }
  3380. else
  3381. {
  3382. if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) a->depth = function->arguments->depth + register_size;
  3383. else if(X86 == Architecture) a->depth = function->arguments->depth - register_size;
  3384. else if(AMD64 == Architecture) a->depth = function->arguments->depth - register_size;
  3385. else if(ARMV7L == Architecture) a->depth = function->arguments->depth + register_size;
  3386. else if(AARCH64 == Architecture) a->depth = function->arguments->depth + register_size;
  3387. else if(RISCV32 == Architecture) a->depth = function->arguments->depth - register_size;
  3388. else if(RISCV64 == Architecture) a->depth = function->arguments->depth - register_size;
  3389. }
  3390. require_extra_token();
  3391. function->arguments = a;
  3392. }
  3393. /* ignore trailing comma (needed for foo(bar(), 1); expressions*/
  3394. if(global_token->s[0] == ',')
  3395. {
  3396. require_extra_token();
  3397. }
  3398. require(NULL != global_token, "Argument list never completed\n");
  3399. }
  3400. require_extra_token();
  3401. }
  3402. void declare_function(void)
  3403. {
  3404. current_count = 0;
  3405. function = sym_declare(global_token->prev->s, NULL, global_function_list, TLO_FUNCTION);
  3406. /* allow previously defined functions to be looked up */
  3407. global_function_list = function;
  3408. if((KNIGHT_NATIVE == Architecture) && match("main", function->s))
  3409. {
  3410. require_match("Impossible error ( vanished\n", "(");
  3411. require_match("Reality ERROR (USING KNIGHT-NATIVE)\nHardware does not support arguments\nthus neither can main on this architecture\ntry tape_01 and tape_02 instead\n", ")");
  3412. }
  3413. else collect_arguments();
  3414. require(NULL != global_token, "Function definitions either need to be prototypes or full\n");
  3415. /* If just a prototype don't waste time */
  3416. if(global_token->s[0] == ';') require_extra_token();
  3417. else
  3418. {
  3419. emit_out("# Defining function ");
  3420. emit_out(function->s);
  3421. emit_out("\n");
  3422. emit_out(":FUNCTION_");
  3423. emit_out(function->s);
  3424. emit_out("\n");
  3425. locals_depth = 0;
  3426. /* Save the current location of the stack pointer. */
  3427. emit_move(REGISTER_LOCALS, REGISTER_STACK, "Set locals pointer");
  3428. char* stack_reserve_string = calloc(MAX_STRING, sizeof(char));
  3429. emit_out(stack_reserve_string);
  3430. /* If we add any statics we don't want them globally available */
  3431. function_static_variables_list = NULL;
  3432. statement();
  3433. /* Just to be sure this doesn't escape the function somehow. */
  3434. function_static_variables_list = NULL;
  3435. if(locals_depth != 0)
  3436. {
  3437. reset_emit_string();
  3438. if(stack_direction == STACK_DIRECTION_PLUS)
  3439. {
  3440. write_add_immediate(REGISTER_STACK, locals_depth, "Reserve stack");
  3441. }
  3442. else
  3443. {
  3444. write_sub_immediate(REGISTER_STACK, locals_depth, "Reserve stack");
  3445. }
  3446. copy_string(stack_reserve_string, emit_string, MAX_STRING);
  3447. }
  3448. emit_move(REGISTER_STACK, REGISTER_LOCALS, "Undo local variables");
  3449. /* C99 5.1.2.2.3 Program termination
  3450. * [..] reaching the } that terminates the main function returns a value of 0.
  3451. * */
  3452. int is_main = match(function->s, "main");
  3453. /* Prevent duplicate RETURNS */
  3454. if(((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture)) && !match("RET R15\n", output_list->s))
  3455. {
  3456. if(is_main) emit_load_immediate(REGISTER_ZERO, 0, "declare function");
  3457. emit_out("RET R15\n");
  3458. }
  3459. else if((X86 == Architecture || AMD64 == Architecture || RISCV32 == Architecture || RISCV64 == Architecture)
  3460. && !match("ret\n", output_list->s))
  3461. {
  3462. if(is_main) emit_load_immediate(REGISTER_ZERO, 0, "declare function");
  3463. emit_out("ret\n");
  3464. }
  3465. else if((ARMV7L == Architecture) && !match("'1' LR RETURN\n", output_list->s))
  3466. {
  3467. if(is_main) emit_load_immediate(REGISTER_ZERO, 0, "declare function");
  3468. emit_out("'1' LR RETURN\n");
  3469. }
  3470. else if((AARCH64 == Architecture) && !match("RETURN\n", output_list->s))
  3471. {
  3472. if(is_main) emit_load_immediate(REGISTER_ZERO, 0, "declare function");
  3473. emit_out("RETURN\n");
  3474. }
  3475. }
  3476. }
  3477. void global_constant(void)
  3478. {
  3479. require_extra_token();
  3480. global_constant_list = sym_declare(global_token->s, NULL, global_constant_list, TLO_CONSTANT);
  3481. require(NULL != global_token->next, "CONSTANT lacks a value\n");
  3482. if(match("sizeof", global_token->next->s))
  3483. {
  3484. global_token = global_token->next->next;
  3485. require_match("ERROR in CONSTANT with sizeof\nMissing (\n", "(");
  3486. struct type* a = type_name();
  3487. require_match("ERROR in CONSTANT with sizeof\nMissing )\n", ")");
  3488. global_token->prev->s = int2str(a->size, 10, TRUE);
  3489. global_constant_list->arguments = global_token->prev;
  3490. }
  3491. else
  3492. {
  3493. global_constant_list->arguments = global_token->next;
  3494. require_extra_token();
  3495. require_extra_token();
  3496. }
  3497. }
  3498. struct type* typedef_function_pointer(void)
  3499. {
  3500. require_extra_token(); /* skip '(' */
  3501. require_match("Invalid token in function pointer parsing, expected '*'.\n", "*");
  3502. require(NULL != global_token, "Received EOF while reading typedef function pointer\n");
  3503. char* name = global_token->s;
  3504. require_extra_token();
  3505. require_match("Invalid token in function pointer parsing, expected ')'.\n", ")");
  3506. require_match("Invalid token in function pointer parsing, expected '('.\n", "(");
  3507. while(global_token->s[0] != ')')
  3508. {
  3509. type_name();
  3510. if(global_token->s[0] == ',')
  3511. {
  3512. require_extra_token();
  3513. }
  3514. }
  3515. require_extra_token(); /* skip ')' */
  3516. if(match(name, "FUNCTION"))
  3517. {
  3518. /* Don't create unnecessary duplicates of built-in types */
  3519. return function_pointer;
  3520. }
  3521. return new_function_pointer_typedef(name);
  3522. }
  3523. struct type* global_typedef(void)
  3524. {
  3525. require_extra_token(); /* skip 'typedef' */
  3526. struct type* type_size = type_name();
  3527. require(NULL != global_token, "Received EOF while reading typedef\n");
  3528. if(global_token->s[0] == '(')
  3529. {
  3530. typedef_function_pointer();
  3531. }
  3532. else
  3533. {
  3534. type_size = mirror_type(type_size);
  3535. global_token = global_token->next;
  3536. }
  3537. require_match("ERROR in typedef statement\nMissing ;\n", ";");
  3538. return type_size;
  3539. }
  3540. void global_variable_header(char* name)
  3541. {
  3542. globals_list = emit(":GLOBAL_", globals_list);
  3543. globals_list = emit(name, globals_list);
  3544. globals_list = emit("\n", globals_list);
  3545. }
  3546. void global_variable_zero_initialize(int size)
  3547. {
  3548. unsigned i = ceil_div(size, register_size);
  3549. while(i != 0)
  3550. {
  3551. /* NULLs are defined in the *_defs.M1 files to be register_size.
  3552. * This will round objects up to a multiple of register_size, but
  3553. * it will not have a negative effect.
  3554. * */
  3555. globals_list = emit("NULL\n", globals_list);
  3556. i = i - 1;
  3557. }
  3558. }
  3559. void global_value_output(int value, int size)
  3560. {
  3561. if(size == 1)
  3562. {
  3563. char* hex_table = "0123456789ABCDEF";
  3564. char* string;
  3565. string = calloc(6, sizeof(char));
  3566. string[0] = '\'';
  3567. string[1] = hex_table[value >> 4];
  3568. string[2] = hex_table[value & 15];
  3569. string[3] = '\'';
  3570. string[4] = ' ';
  3571. globals_list = emit(string, globals_list);
  3572. }
  3573. else if(size == 2)
  3574. {
  3575. if(value < 0 && Architecture != RISCV64 && Architecture != RISCV32)
  3576. {
  3577. globals_list = emit("@", globals_list);
  3578. }
  3579. else
  3580. {
  3581. globals_list = emit("$", globals_list);
  3582. }
  3583. globals_list = emit(int2str(value, 10, FALSE), globals_list);
  3584. globals_list = emit(" ", globals_list);
  3585. }
  3586. else if(size >= 4)
  3587. {
  3588. globals_list = emit("%", globals_list);
  3589. globals_list = emit(int2str(value, 10, FALSE), globals_list);
  3590. globals_list = emit(" ", globals_list);
  3591. if(size == 8)
  3592. {
  3593. globals_list = emit("%0 ", globals_list);
  3594. }
  3595. }
  3596. }
  3597. void global_pad_to_register_size(int bytes_written)
  3598. {
  3599. int remaining = (bytes_written % register_size);
  3600. if(remaining != 0)
  3601. {
  3602. int alignment_size = register_size - remaining;
  3603. while(alignment_size != 0)
  3604. {
  3605. globals_list = emit("'00' ", globals_list);
  3606. alignment_size = alignment_size - 1;
  3607. }
  3608. }
  3609. globals_list = emit("\n", globals_list);
  3610. }
  3611. void global_struct_initializer_list(struct type* type_size);
  3612. void global_value_selection(struct type* type_size)
  3613. {
  3614. if(type_is_pointer(type_size))
  3615. {
  3616. if(('"' == global_token->s[0]))
  3617. {
  3618. char* name = global_token->s + 1;
  3619. globals_list = emit("&GLOBAL_", globals_list);
  3620. globals_list = emit(name, globals_list);
  3621. globals_list = emit("_contents ", globals_list);
  3622. if(register_size == 8)
  3623. {
  3624. global_value_output(0, 4);
  3625. }
  3626. strings_list = emit(":GLOBAL_", strings_list);
  3627. strings_list = emit(name, strings_list);
  3628. strings_list = emit("_contents\n", strings_list);
  3629. strings_list = emit(parse_string(global_token->s), strings_list);
  3630. require_extra_token();
  3631. }
  3632. else if(match("0", global_token->s))
  3633. {
  3634. global_value_output(0, register_size);
  3635. require_extra_token();
  3636. }
  3637. else if(global_token->s[0] == '&')
  3638. {
  3639. require_extra_token();
  3640. char* name = global_token->s;
  3641. struct token_list* lookup_token = sym_lookup(name, global_function_list);
  3642. if(NULL != lookup_token)
  3643. {
  3644. globals_list = emit("&FUNCTION_", globals_list);
  3645. globals_list = emit(name, globals_list);
  3646. globals_list = emit(" ", globals_list);
  3647. }
  3648. else
  3649. {
  3650. lookup_token = sym_lookup(name, global_symbol_list);
  3651. if(NULL != lookup_token)
  3652. {
  3653. globals_list = emit("&GLOBAL_", globals_list);
  3654. globals_list = emit(name, globals_list);
  3655. globals_list = emit(" ", globals_list);
  3656. }
  3657. else
  3658. {
  3659. line_error();
  3660. fputs("Unable to find address of '", stderr);
  3661. fputs(name, stderr);
  3662. fputs("'.\n", stderr);
  3663. exit(EXIT_FAILURE);
  3664. }
  3665. }
  3666. if(register_size > 4)
  3667. {
  3668. globals_list = emit("%0 ", globals_list);
  3669. }
  3670. require_extra_token();
  3671. }
  3672. else
  3673. {
  3674. line_error();
  3675. fputs("Invalid initializer for global struct pointer member.\n", stderr);
  3676. exit(EXIT_FAILURE);
  3677. }
  3678. }
  3679. else if(type_is_struct_or_union(type_size))
  3680. {
  3681. global_struct_initializer_list(type_size);
  3682. }
  3683. else
  3684. {
  3685. int value = constant_expression();
  3686. global_value_output(value, type_size->size);
  3687. }
  3688. }
  3689. void global_struct_initializer_list(struct type* type_size)
  3690. {
  3691. require_match("Struct assignment initialization is invalid for globals.", "{");
  3692. require(NULL != global_token, "EOF in global struct initialization");
  3693. struct type* member = type_size->members;
  3694. do
  3695. {
  3696. if(member == NULL)
  3697. {
  3698. line_error();
  3699. fputs("Global struct initializer list has too many values.\n", stderr);
  3700. exit(EXIT_FAILURE);
  3701. }
  3702. global_value_selection(member->type);
  3703. member = member->members;
  3704. if(global_token->s[0] == ',')
  3705. {
  3706. require_extra_token();
  3707. }
  3708. }
  3709. while(global_token->s[0] != '}');
  3710. while(member != NULL)
  3711. {
  3712. global_value_output(0, member->size);
  3713. member = member->members;
  3714. }
  3715. globals_list = emit("\n", globals_list);
  3716. require_match("Struct assignment initialization is invalid for globals.", "}");
  3717. require(NULL != global_token, "EOF in global struct initialization");
  3718. }
  3719. int global_array_initializer_list(struct type* type_size, int array_modifier)
  3720. {
  3721. int amount_of_elements = 0;
  3722. do
  3723. {
  3724. if(amount_of_elements >= array_modifier && array_modifier != 0)
  3725. {
  3726. line_error();
  3727. fputs("Too many elements in initializer list.", stderr);
  3728. exit(EXIT_FAILURE);
  3729. }
  3730. global_value_selection(type_size);
  3731. amount_of_elements = amount_of_elements + 1;
  3732. if(global_token->s[0] == ',')
  3733. {
  3734. require_extra_token();
  3735. }
  3736. }
  3737. while (global_token->s[0] != '}');
  3738. require_extra_token();
  3739. if(array_modifier == 0)
  3740. {
  3741. if(amount_of_elements == 0)
  3742. {
  3743. line_error();
  3744. fputs("Array with initializer list can not have size zero.\n", stderr);
  3745. exit(EXIT_FAILURE);
  3746. }
  3747. array_modifier = amount_of_elements;
  3748. }
  3749. int size;
  3750. while(amount_of_elements < array_modifier)
  3751. {
  3752. size = type_size->size;
  3753. if(size == 1 || size == 4 || size == 8)
  3754. {
  3755. global_value_output(0, size);
  3756. }
  3757. else
  3758. {
  3759. while(size > 0)
  3760. {
  3761. if(size >= 4)
  3762. {
  3763. global_value_output(0, 4);
  3764. size = size - 4;
  3765. }
  3766. else
  3767. {
  3768. global_value_output(0, 1);
  3769. size = size - 1;
  3770. }
  3771. }
  3772. globals_list = emit("\n", globals_list);
  3773. }
  3774. amount_of_elements = amount_of_elements + 1;
  3775. }
  3776. return array_modifier;
  3777. }
  3778. int global_static_array(struct type* type_size, char* name)
  3779. {
  3780. global_variable_header(name);
  3781. if(global_token->s[0] == ';' || global_token->s[0] == ',')
  3782. {
  3783. global_variable_zero_initialize(type_size->size);
  3784. return 0;
  3785. }
  3786. else if(global_token->s[0] == '=')
  3787. {
  3788. require_extra_token();
  3789. global_value_selection(type_size);
  3790. global_pad_to_register_size(type_size->size);
  3791. return 0;
  3792. }
  3793. maybe_bootstrap_error("global array definitions");
  3794. globals_list = emit("&GLOBAL_STORAGE_", globals_list);
  3795. globals_list = emit(name, globals_list);
  3796. if (AARCH64 == Architecture || AMD64 == Architecture || RISCV64 == Architecture)
  3797. {
  3798. globals_list = emit(" %0", globals_list);
  3799. }
  3800. globals_list = emit("\n:GLOBAL_STORAGE_", globals_list);
  3801. globals_list = emit(name, globals_list);
  3802. globals_list = emit("\n", globals_list);
  3803. require_extra_token();
  3804. int array_modifier = 0;
  3805. int size = 0;
  3806. if(global_token->s[0] != ']')
  3807. {
  3808. array_modifier = constant_expression();
  3809. /* Make sure not negative */
  3810. if(array_modifier < 0)
  3811. {
  3812. line_error();
  3813. fputs("Negative values are not supported for allocated arrays\n", stderr);
  3814. exit(EXIT_FAILURE);
  3815. }
  3816. /* length */
  3817. size = array_modifier * type_size->size;
  3818. if(size == 0)
  3819. {
  3820. line_error();
  3821. fputs("Arrays with size of zero are not allowed.\n", stderr);
  3822. exit(EXIT_FAILURE);
  3823. }
  3824. /* Stop bad states */
  3825. if((size < 0) || (size > 0x100000))
  3826. {
  3827. line_error();
  3828. fputs("M2-Planet is very inefficient so you probably don't want to allocate over 1MB into your binary for NULLs\n", stderr);
  3829. exit(EXIT_FAILURE);
  3830. }
  3831. }
  3832. /* Ensure properly closed */
  3833. require_match("missing close bracket\n", "]");
  3834. if(global_token->s[0] == '=')
  3835. {
  3836. require_extra_token();
  3837. require_match("Missing { after = in global array", "{");
  3838. array_modifier = global_array_initializer_list(type_size, array_modifier);
  3839. global_pad_to_register_size(array_modifier * type_size->size);
  3840. }
  3841. else
  3842. {
  3843. if(size == 0)
  3844. {
  3845. line_error();
  3846. fputs("Array without size must have initializer list.", stderr);
  3847. exit(EXIT_FAILURE);
  3848. }
  3849. global_variable_zero_initialize(size);
  3850. }
  3851. return array_modifier;
  3852. }
  3853. void declare_global_variable(struct type* type_size, struct token_list* variable)
  3854. {
  3855. variable->array_modifier = global_static_array(type_size, variable->s);
  3856. }
  3857. /*
  3858. * program:
  3859. * declaration
  3860. * declaration program
  3861. *
  3862. * declaration:
  3863. * CONSTANT identifer value
  3864. * typedef identifer type;
  3865. * type-name identifier ;
  3866. * type-name identifier = value ;
  3867. * type-name identifier [ value ];
  3868. * type-name identifier ( parameter-list ) ;
  3869. * type-name identifier ( parameter-list ) statement
  3870. *
  3871. * parameter-list:
  3872. * parameter-declaration
  3873. * parameter-list, parameter-declaration
  3874. *
  3875. * parameter-declaration:
  3876. * type-name identifier-opt
  3877. */
  3878. void program(void)
  3879. {
  3880. function = NULL;
  3881. Address_of = FALSE;
  3882. struct type* type_size;
  3883. char* name;
  3884. new_type:
  3885. /* Deal with garbage input */
  3886. if (NULL == global_token) return;
  3887. require('#' != global_token->s[0], "unhandled macro directive\n");
  3888. require(!match("\n", global_token->s), "unexpected newline token\n");
  3889. /* Handle cc_* CONSTANT statements */
  3890. if(match("CONSTANT", global_token->s))
  3891. {
  3892. global_constant();
  3893. goto new_type;
  3894. }
  3895. /* Handle c typedef statements */
  3896. if(match("typedef", global_token->s))
  3897. {
  3898. type_size = global_typedef();
  3899. goto new_type;
  3900. }
  3901. /* declaration-specifiers can come in any order */
  3902. while(match("inline", global_token->s)
  3903. || match("static", global_token->s)
  3904. || match("_Noreturn", global_token->s))
  3905. {
  3906. require_extra_token();
  3907. }
  3908. type_size = type_name();
  3909. /* Deal with case of struct definitions */
  3910. if(global_token->s[0] == ';')
  3911. {
  3912. global_token = global_token->next;
  3913. goto new_type;
  3914. }
  3915. require(NULL != global_token->next, "Unterminated global\n");
  3916. if(global_token->s[0] == '('
  3917. || global_token->next->s[0] == ','
  3918. || global_token->next->s[0] == ';'
  3919. || global_token->next->s[0] == '='
  3920. || global_token->next->s[0] == '[')
  3921. {
  3922. do
  3923. {
  3924. if(global_token->s[0] == '(')
  3925. {
  3926. require_extra_token(); /* skip '(' */
  3927. require_match("Required '*' after '*' in global function pointer.\n", "*");
  3928. name = global_token->s;
  3929. require_extra_token();
  3930. require_match("Required ')' after name in global function pointer.\n", ")");
  3931. require_match("Required '(' after ')' in global function pointer.\n", "(");
  3932. while(global_token->s[0] != ')')
  3933. {
  3934. type_name();
  3935. if(global_token->s[0] == ',')
  3936. {
  3937. require_extra_token();
  3938. }
  3939. }
  3940. require_extra_token(); /* skip ')' */
  3941. type_size = function_pointer;
  3942. }
  3943. else
  3944. {
  3945. name = global_token->s;
  3946. require_extra_token();
  3947. }
  3948. global_symbol_list = sym_declare(name, type_size, global_symbol_list, TLO_GLOBAL);
  3949. declare_global_variable(type_size, global_symbol_list);
  3950. if(global_token->s[0] == ',')
  3951. {
  3952. require_extra_token();
  3953. }
  3954. }
  3955. while(global_token->s[0] != ';');
  3956. require_match("Expected ; after global declarations", ";");
  3957. goto new_type;
  3958. }
  3959. require_extra_token();
  3960. /* Deal with global functions */
  3961. if(match("(", global_token->s))
  3962. {
  3963. declare_function();
  3964. goto new_type;
  3965. }
  3966. /* Everything else is just an error */
  3967. line_error();
  3968. fputs("Received ", stderr);
  3969. fputs(global_token->s, stderr);
  3970. fputs(" in program\n", stderr);
  3971. exit(EXIT_FAILURE);
  3972. }
  3973. void recursive_output(struct token_list* head, FILE* out)
  3974. {
  3975. struct token_list* i = reverse_list(head);
  3976. while(NULL != i)
  3977. {
  3978. fputs(i->s, out);
  3979. i = i->next;
  3980. }
  3981. }
  3982. void output_tokens(struct token_list *i, FILE* out)
  3983. {
  3984. while(NULL != i)
  3985. {
  3986. fputs(i->s, out);
  3987. fputs(" ", out);
  3988. i = i->next;
  3989. }
  3990. }