cc_amd64.s 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399
  1. ; Copyright (C) 2016 Jeremiah Orians
  2. ; This file is part of stage0.
  3. ;
  4. ; stage0 is free software: you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation, either version 3 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; stage0 is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. ;; A Minimal C Compiler
  17. ;; type Cells are in the following form:
  18. ;; NEXT (0), SIZE (4), OFFSET (8), INDIRECT (12), MEMBERS (16), TYPE (20), NAME (24)
  19. ;; token_list Cells are in the following form:
  20. ;; NEXT (0), LOCALS/PREV (4), S (8), TYPE/FILENAME (12), ARGUMENTS/DEPTH/LINENUMBER (16)
  21. ;; Each being the length of a register [32bits]
  22. ;;
  23. ;; STACK space: End of program -> 512KB (0x80000) [Could be reduced]
  24. ;; HEAP space: 512KB -> End of Memory
  25. ;; R15 is the STACK pointer
  26. ;; R14 is the HEAP pointer
  27. :start
  28. ;; Prep TAPE_02
  29. LOADUI R0 0x1101
  30. FOPEN_WRITE
  31. ;; Prep TAPE_01
  32. LOADUI R0 0x1100
  33. FOPEN_READ
  34. :main
  35. LOADUI R0 0x1100 ; Pass Tape_01 for reading
  36. LOADR32 R14 @HEAP ; Setup Initial HEAP
  37. LOADUI R15 $STACK ; Setup Initial STACK
  38. CALLI R15 @read_all_tokens ; Read all Tokens in Tape_01
  39. CALLI R15 @reverse_list ; Fix Token Order
  40. ; CALLI R15 @debug_list ; Lets try to debug token errors
  41. MOVE R13 R0 ; Set global_token for future reading
  42. FALSE R12 ; Set struct token_list* out to NULL
  43. FALSE R11 ; Set struct token_list* list_strings to NULL
  44. FALSE R10 ; Set struct token_list* globals_list to NULL
  45. CALLI R15 @program ; Build our output
  46. LOADUI R0 $header_string1 ; Using our first header string
  47. LOADUI R1 0x1101 ; Using Tape_02
  48. CALLI R15 @file_print ; Write string
  49. MOVE R0 R12 ; using Contents of output_list
  50. CALLI R15 @recursive_output ; Recursively write
  51. LOADUI R0 $header_string2 ; Using our second header string
  52. CALLI R15 @file_print ; Write string
  53. MOVE R0 R10 ; using Contents of globals_list
  54. CALLI R15 @recursive_output ; Recursively write
  55. LOADUI R0 $header_string3 ; Using our third header string
  56. CALLI R15 @file_print ; Write string
  57. MOVE R0 R11 ; using Contents of strings_list
  58. CALLI R15 @recursive_output ; Recursively write
  59. LOADUI R0 $header_string4 ; Using our final header string
  60. CALLI R15 @file_print ; Write string
  61. HALT ; We have completed compiling our input
  62. ;; Symbol lists
  63. :global_constant_list
  64. NOP
  65. :global_symbol_list
  66. NOP
  67. :global_function_list
  68. NOP
  69. ;; Pointer to initial HEAP ADDRESS
  70. :HEAP
  71. '00080000'
  72. ;; Output strings
  73. :header_string1
  74. "
  75. # Core program
  76. "
  77. :header_string2
  78. "
  79. # Program global variables
  80. "
  81. :header_string3
  82. "
  83. # Program strings
  84. "
  85. :header_string4
  86. "
  87. :ELF_end
  88. "
  89. ;; clearWhiteSpace function
  90. ;; Receives a character in R0 and FILE* in R1 and line_num in R11
  91. ;; Returns first non-whitespace character in R0
  92. :clearWhiteSpace
  93. CMPSKIPI.NE R0 32 ; Check for a Space
  94. JUMP @clearWhiteSpace_reset ; Looks like we need to remove a space
  95. CMPSKIPI.NE R0 9 ; Check for a tab
  96. JUMP @clearWhiteSpace_reset ; Looks like we need to remove a tab
  97. CMPSKIPI.E R0 10 ; Check for a newline
  98. RET R15 ; Looks we found a non-whitespace
  99. ADDUI R11 R11 1 ; Increment line number
  100. ;; Fall through to iterate to next char
  101. :clearWhiteSpace_reset
  102. FGETC ; Get next char
  103. JUMP @clearWhiteSpace ; Iterate
  104. ;; consume_byte function
  105. ;; Receives a char in R0, FILE* in R1 and index in R13
  106. ;; Returns next char in R0
  107. :consume_byte
  108. STOREX8 R0 R14 R13 ; Put char onto HEAP
  109. ADDUI R13 R13 1 ; Increment index
  110. FGETC ; Get next char
  111. RET R15
  112. ;; consume_word function
  113. ;; Receives a char in R0, FILE* in R1, FREQUENT in R2 and index in R13
  114. ;; Returns next char in R0
  115. :consume_word
  116. PUSHR R3 R15 ; Protect R3
  117. FALSE R3 ; ESCAPE is FALSE
  118. :consume_word_reset
  119. JUMP.NZ R3 @consume_word_iter1
  120. CMPSKIPI.NE R0 92 ; If \
  121. TRUE R3 ; Looks like we are in an escape
  122. JUMP @consume_word_iter2
  123. :consume_word_iter1
  124. FALSE R3 ; Looks like we are no longer in an escape
  125. :consume_word_iter2
  126. CALLI R15 @consume_byte ; Store the char
  127. JUMP.NZ R3 @consume_word_reset ; If escape loop
  128. CMPJUMPI.NE R0 R2 @consume_word_reset ; if not matching frequent loop
  129. FGETC ; Get a new char to return
  130. POPR R3 R15 ; Restore R3
  131. RET R15
  132. ;; fixup_label function
  133. ;; Receives nothing (But uses R14 as HEAP pointer)
  134. ;; Returns 32 in R0 and no other registers altered
  135. :fixup_label
  136. PUSHR R1 R15 ; Protect R1 from change
  137. PUSHR R2 R15 ; Protect R2 from change
  138. LOADUI R0 58 ; Set HOLD to :
  139. FALSE R2 ; Set I to 0
  140. :fixup_label_reset
  141. MOVE R1 R0 ; Set PREV = HOLD
  142. LOADXU8 R0 R14 R2 ; Read hold_string[I] into HOLD
  143. STOREX8 R1 R14 R2 ; Set hold_string[I] = PREV
  144. ADDUI R2 R2 1 ; increment I
  145. JUMP.NZ R0 @fixup_label_reset ; Loop until we hit a NULL
  146. ;; clean up
  147. ADDUI R2 R2 1 ; increment I
  148. LOADUI R0 32 ; Put 32 in R0
  149. POPR R2 R15 ; Restore R2
  150. POPR R1 R15 ; Restore R1
  151. RET R15
  152. ;; in_set2 function
  153. ;; Receives a Char in R0, FILE* in R1, char* in R2 and index in R13
  154. ;; Return result in R2
  155. :in_set2
  156. PUSHR R3 R15 ; Protect R3 from changes
  157. :in_set2_reset
  158. LOADU8 R3 R2 0 ; Get char from list
  159. JUMP.Z R3 @in_set2_fail ; Stop when 0 == s[0]
  160. CMPJUMPI.E R0 R3 @in_set2_done ; We found a match
  161. ADDUI R2 R2 1 ; Increment to next char
  162. JUMP.NZ R3 @in_set2_reset ; Iterate if not NULL
  163. :in_set2_fail
  164. ;; Looks like not found
  165. FALSE R2 ; Return FALSE
  166. :in_set2_done
  167. CMPSKIPI.E R2 0 ; Provided not FALSE
  168. TRUE R2 ; The result is true
  169. POPR R3 R15 ; Restore R3
  170. RET R15
  171. ;; in_set function
  172. ;; Receives a Char in R0, char* in R1
  173. ;; Return result in R0
  174. :in_set
  175. PUSHR R2 R15 ; Protect R3 from changes
  176. :in_set_reset
  177. LOADU8 R2 R1 0 ; Get char from list
  178. JUMP.Z R2 @in_set_fail ; Stop when 0 == s[0]
  179. CMPJUMPI.E R0 R2 @in_set_done ; We found a match
  180. ADDUI R1 R1 1 ; Increment to next char
  181. JUMP.NZ R2 @in_set_reset ; Iterate if not NULL
  182. :in_set_fail
  183. ;; Looks like not found
  184. FALSE R1 ; Return FALSE
  185. :in_set_done
  186. CMPSKIPI.E R1 0 ; Provided not FALSE
  187. TRUE R2 ; The result is true
  188. MOVE R0 R2 ; Put result in correct place
  189. POPR R2 R15 ; Restore R3
  190. RET R15
  191. ;; Common in_set strings of interest
  192. ;; As Raw strings (") is forbidden and ' has some restrictions
  193. :nice_chars
  194. "
  195. !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  196. :keyword_chars
  197. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
  198. :variable_chars
  199. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
  200. :symbol_chars
  201. "<=>|&!-"
  202. :hex_chars
  203. "0123456789ABCDEF"
  204. :digit_chars
  205. "0123456789"
  206. :whitespace_chars
  207. "
  208. "
  209. ;; preserve_keyword function
  210. ;; Receives a Char in R0, FILE* in R1 and index in R13
  211. ;; Overwrites R2
  212. ;; Returns next CHAR
  213. :preserve_keyword
  214. LOADUI R2 $keyword_chars ; Using keyword list of chars
  215. CALLI R15 @in_set2 ; Check if in list
  216. JUMP.Z R2 @preserve_keyword_label ; if not in set, stop iterating
  217. :preserve_keyword_reset
  218. CALLI R15 @consume_byte ; Consume another byte
  219. JUMP @preserve_keyword ; Iterate
  220. :preserve_keyword_label
  221. CMPSKIPI.NE R0 58 ; Check for label (:)
  222. CALLI R15 @fixup_label ; Looks like we found one
  223. RET R15
  224. ;; preserve_symbol function
  225. ;; Receives a Char in R0, FILE* in R1 and index in R13
  226. ;; Overwrites R2
  227. ;; Returns next CHAR
  228. :preserve_symbol
  229. LOADUI R2 $symbol_chars ; Using symbol list of chars
  230. CALLI R15 @in_set2 ; Check if in list
  231. JUMP.NZ R2 @preserve_symbol_reset
  232. ;; Looks we didn't find anything we wanted to preserve
  233. RET R15
  234. :preserve_symbol_reset
  235. CALLI R15 @consume_byte ; Consume another byte
  236. JUMP @preserve_symbol ; Iterate
  237. ;; purge_macro function
  238. ;; Receives a Char in R0, FILE* in R1 and index in R13
  239. ;; Returns next CHAR via jumping to get_token_reset
  240. :purge_macro
  241. CMPSKIPI.NE R0 10 ; Check for Line Feed
  242. JUMP @get_token_reset ; Looks like we found it, call it done
  243. FGETC ; Looks like we need another CHAR
  244. JUMP @purge_macro ; Keep looping
  245. ;; get_token function
  246. ;; Receives a Char in R0, FILE* in R1, line_num in R11 and TOKEN in R10
  247. ;; sets index in R13 and current in R12
  248. ;; Overwrites R2
  249. ;; Returns next CHAR
  250. :get_token
  251. PUSHR R12 R15 ; Preserve R12
  252. PUSHR R13 R15 ; Preserve R13
  253. COPY R12 R14 ; Save CURRENT's Address
  254. ADDUI R14 R14 20 ; Update Malloc to free space for string
  255. :get_token_reset
  256. FALSE R13 ; Reset string_index to 0
  257. CALLI R15 @clearWhiteSpace ; Clear any leading whitespace
  258. CMPSKIPI.NE R0 35 ; Deal with # line macros
  259. JUMP @purge_macro ; Returns at get_token_reset
  260. ;; Check for keywords
  261. LOADUI R2 $keyword_chars ; Using keyword list
  262. CALLI R15 @in_set2 ; Check if keyword
  263. JUMP.Z R2 @get_token_symbol ; if not a keyword
  264. CALLI R15 @preserve_keyword ; Yep its a keyword
  265. JUMP @get_token_done ; Be done with token
  266. ;; Check for symbols
  267. :get_token_symbol
  268. LOADUI R2 $symbol_chars ; Using symbol list
  269. CALLI R15 @in_set2 ; Check if symbol
  270. JUMP.Z R2 @get_token_char ; If not a symbol
  271. CALLI R15 @preserve_symbol ; Yep its a symbol
  272. JUMP @get_token_done ; Be done with token
  273. ;; Check for char
  274. :get_token_char
  275. CMPSKIPI.E R0 39 ; Check if '
  276. JUMP @get_token_string ; Not a '
  277. COPY R2 R0 ; Prepare for consume_word
  278. CALLI R15 @consume_word ; Call it
  279. JUMP @get_token_done ; Be done with token
  280. ;; Check for string
  281. :get_token_string
  282. CMPSKIPI.E R0 34 ; Check if "
  283. JUMP @get_token_EOF ; Not a "
  284. COPY R2 R0 ; Prepare for consume_word
  285. CALLI R15 @consume_word ; Call it
  286. JUMP @get_token_done ; Be done with token
  287. ;; Check for EOF
  288. :get_token_EOF
  289. CMPSKIPI.L R0 0 ; If c < 0
  290. JUMP @get_token_comment ; If not EOF
  291. POPR R13 R15 ; Restore R13
  292. POPR R12 R15 ; Restore R12
  293. RET R15 ; Otherwise just return the EOF
  294. ;; Check for C comments
  295. :get_token_comment
  296. CMPSKIPI.E R0 47 ; Deal with non-comments
  297. JUMP @get_token_else ; immediately
  298. CALLI R15 @consume_byte ; Deal with another byte
  299. CMPSKIPI.NE R0 42 ; if * make it a block comment
  300. JUMP @get_token_comment_block ; and purge it all
  301. CMPSKIPI.E R0 47 ; Check if not //
  302. JUMP @get_token_done ; Finish off the token
  303. ;; Looks like it was //
  304. FGETC ; Get next char
  305. JUMP @get_token_reset ; Try again
  306. ;; Deal with the mess that is C block comments
  307. :get_token_comment_block
  308. FGETC ; Get next char
  309. :get_token_comment_block_outer
  310. CMPSKIPI.NE R0 47 ; Check for closing /
  311. JUMP @get_token_comment_block_outer_done ; Yep has closing /
  312. :get_token_comment_block_inner
  313. CMPSKIPI.NE R0 42 ; Check for preclosing *
  314. JUMP @get_token_comment_block_inner_done ; Yep has *
  315. ;; Otherwise we are just consuming
  316. FGETC ; Remove another CHAR
  317. CMPSKIPI.NE R0 10 ; Check for Line Feed
  318. ADDUI R11 R11 1 ; Found one, updating line number
  319. JUMP @get_token_comment_block_inner
  320. :get_token_comment_block_inner_done
  321. FGETC ; Remove another CHAR
  322. CMPSKIPI.NE R0 10 ; Check for Line Feed
  323. ADDUI R11 R11 1 ; Found one, updating line number
  324. JUMP @get_token_comment_block_outer
  325. :get_token_comment_block_outer_done
  326. FGETC ; Remove another CHAR
  327. JUMP @get_token_reset ; And Try again
  328. ;; Deal with default case
  329. :get_token_else
  330. CALLI R15 @consume_byte ; Consume the byte and be done
  331. :get_token_done
  332. ADDUI R13 R13 2 ; Pad with NULL the string
  333. STORE32 R14 R12 8 ; Set CURRENT->S to String
  334. ADD R14 R14 R13 ; Add string length to HEAP
  335. STORE32 R10 R12 0 ; CURRENT->NEXT = TOKEN
  336. STORE32 R10 R12 4 ; CURRENT->PREV = TOKE
  337. STORE32 R11 R12 16 ; CURRENT->LINENUM = LINE_NUM
  338. MOVE R10 R12 ; SET TOKEN to CURRENT
  339. POPR R13 R15 ; Restore R13
  340. POPR R12 R15 ; Restore R12
  341. RET R15
  342. ;; reverse_list function
  343. ;; Receives a Token_list in R0
  344. ;; Returns List in Reverse order in R0
  345. :reverse_list
  346. PUSHR R1 R15 ; Protect R1
  347. PUSHR R2 R15 ; Protect R2
  348. FALSE R1 ; Set ROOT to NULL
  349. CMPJUMPI.E R0 R1 @reverse_list_done ; ABORT if given a NULL
  350. :reverse_list_reset
  351. LOAD32 R2 R0 0 ; SET next to HEAD->NEXT
  352. STORE32 R1 R0 0 ; SET HEAD->NEXT to ROOT
  353. MOVE R1 R0 ; SET ROOT to HEAD
  354. MOVE R0 R2 ; SET HEAD to NEXT
  355. JUMP.NZ R0 @reverse_list_reset ; Iterate if HEAD not NULL
  356. :reverse_list_done
  357. MOVE R0 R1 ; SET Result to ROOT
  358. POPR R2 R15 ; Restore R2
  359. POPR R1 R15 ; Restore R1
  360. RET R15
  361. ;; read_all_tokens function
  362. ;; Receives a FILE* in R0
  363. ;; sets line_num in R11 and TOKEN in R10
  364. ;; Overwrites R2
  365. ;; Returns struct token_list* in R0
  366. :read_all_tokens
  367. PUSHR R1 R15 ; Protect R1
  368. PUSHR R2 R15 ; Protect R2
  369. PUSHR R10 R15 ; Protect R10
  370. PUSHR R11 R15 ; Protect R11
  371. MOVE R1 R0 ; Set R1 as FILE*
  372. FGETC ; Read our first CHAR
  373. LOADUI R11 1 ; Start line_num at 1
  374. FALSE R10 ; First token is NULL
  375. :read_all_tokens_reset
  376. JUMP.NP R0 @read_all_tokens_done
  377. CALLI R15 @get_token
  378. JUMP @read_all_tokens_reset
  379. :read_all_tokens_done
  380. MOVE R0 R10 ; Return the Token
  381. POPR R11 R15 ; Restore R11
  382. POPR R10 R15 ; Restore R10
  383. POPR R2 R15 ; Restore R2
  384. POPR R1 R15 ; Restore R1
  385. RET R15
  386. ;; parse_string function
  387. ;; Receives char* string in R0
  388. ;; R14 is HEAP Pointer
  389. ;; Returns char* in R0
  390. :parse_string
  391. PUSHR R1 R15 ; Protect R1
  392. COPY R1 R0 ; Make a copy of STRING
  393. CALLI R15 @weird ; Check if string is weird
  394. SWAP R0 R1
  395. JUMP.Z R1 @parse_string_regular ; Deal with regular strings
  396. ;; Looks like we have a weirdo
  397. CALLI R15 @collect_weird_string ; Create our weird string
  398. JUMP @parse_string_done ; Simply return what was created
  399. :parse_string_regular
  400. CALLI R15 @collect_regular_string
  401. :parse_string_done
  402. POPR R1 R15 ; Restore R1
  403. RET R15
  404. ;; weird function
  405. ;; Analyze string to determine if it's output would be weird for mescc-tools
  406. ;; Receives char* in R0
  407. ;; Returns BOOL in R0
  408. :weird
  409. PUSHR R1 R15 ; Protect R1
  410. PUSHR R2 R15 ; Protect R2
  411. PUSHR R3 R15 ; Protect R3
  412. PUSHR R4 R15 ; Protect R4
  413. FALSE R2 ; Assume FALSE
  414. ADDUI R3 R0 1 ; STRING = STRING + 1
  415. :weird_iter
  416. JUMP.NZ R2 @weird_done ; Stop if TRUE
  417. LOADU8 R4 R3 0 ; C = STRING[0]
  418. JUMP.Z R4 @weird_done ; Be done at NULL Termination
  419. CMPSKIPI.E R4 92 ; If not '\\'
  420. JUMP @weird_post_escape ; Looks like no escape analysis
  421. ;; Deal with the mess
  422. COPY R0 R3 ; Using STRING
  423. CALLI R15 @escape_lookup ; Get our CHAR
  424. MOVE R4 R0 ; C = ESCAPE_LOOKUP(STRING)
  425. LOADU8 R0 R3 1 ; STRING[1]
  426. CMPSKIPI.NE R0 120 ; if 'x' == STRING[1]
  427. ADDUI R3 R3 2 ; STRING = STRING + 2
  428. ADDUI R3 R3 1 ; STRING = STRING + 1
  429. :weird_post_escape
  430. LOADUI R1 $nice_chars ; using list of nice CHARS
  431. COPY R0 R4 ; using copy of C
  432. CALLI R15 @in_set ; Use in_set
  433. CMPSKIPI.NE R0 0 ; IF TRUE
  434. TRUE R2 ; Return TRUE
  435. ADDUI R3 R3 1 ; STRING = STRING + 1
  436. LOADUI R1 $whitespace_chars ; Check Whitespace Chars
  437. COPY R0 R4 ; Using copy of C
  438. CALLI R15 @in_set ; Use in_set
  439. JUMP.Z R0 @weird_iter ; If False simply loop
  440. LOADU8 R0 R3 0 ; STRING[1]
  441. CMPSKIPI.NE R0 58 ; If ':' == STRING[1]
  442. TRUE R2 ; Flip flag
  443. JUMP @weird_iter ; Keep trying to find an answer
  444. :weird_done
  445. MOVE R0 R2 ; Whatever is in R2 is the answer
  446. POPR R4 R15 ; Restore R4
  447. POPR R3 R15 ; Restore R3
  448. POPR R2 R15 ; Restore R2
  449. POPR R1 R15 ; Restore R1
  450. RET R15
  451. ;; collect_weird_string function
  452. ;; Converts weird string into a form mescc-tools can handle cleanly
  453. ;; Receives char* in R0
  454. ;; R14 is HEAP Pointer and $hex_chars as the table
  455. ;; Returns char* in R0
  456. :collect_weird_string
  457. PUSHR R1 R15 ; Protect R1
  458. PUSHR R2 R15 ; Protect R2
  459. PUSHR R3 R15 ; Protect R3
  460. PUSHR R4 R15 ; Protect R4
  461. LOADUI R4 $hex_chars ; Pointer to TABLE
  462. COPY R3 R14 ; Get HOLD
  463. MOVE R2 R0 ; Put STRING in Place
  464. LOADUI R0 39 ; Prefix with '
  465. PUSH8 R0 R3 ; HOLD[0] = '\'' && HOLD = HOLD + 1
  466. :collect_weird_string_iter
  467. ADDUI R2 R2 1 ; STRING = STRING + 1
  468. LOADUI R0 32 ; Insert ' '
  469. PUSH8 R0 R3 ; HOLD[0] = ' ' && HOLD = HOLD + 1
  470. COPY R0 R2 ; copy STRING
  471. CALLI R15 @escape_lookup ; Get char value
  472. ANDI R1 R0 0x0F ; Save Bottom out of the way
  473. SR0I R0 4 ; Isolate Top
  474. LOADXU8 R0 R4 R0 ; Using Table
  475. LOADXU8 R1 R4 R1 ; Using Table
  476. PUSH8 R0 R3 ; HOLD[0] = TABLE[(TEMP >> 4)] && HOLD = HOLD + 1
  477. PUSH8 R1 R3 ; HOLD[0] = TABLE[(TEMP & 15)] && HOLD = HOLD + 1
  478. LOADU8 R0 R2 0 ; STRING[0]
  479. JUMP.Z R0 @collect_weird_string_done ; Stop if NULL
  480. CMPSKIPI.E R0 92 ; IF STRING[0] != '\\'
  481. JUMP @collect_weird_string_check ; Deal with iteration
  482. LOADU8 R0 R2 1 ; STRING[1]
  483. CMPSKIPI.NE R0 120 ; If STRING[1] == 'x'
  484. ADDUI R2 R2 2 ; STRING = STRING + 2
  485. ADDUI R2 R2 1 ; STRING = STRING + 1
  486. :collect_weird_string_check
  487. LOADU8 R0 R2 1 ; STRING[1]
  488. JUMP.NZ R0 @collect_weird_string_iter
  489. :collect_weird_string_done
  490. LOADUI R0 32 ; Insert ' '
  491. PUSH8 R0 R3 ; HOLD[0] = ' ' && HOLD = HOLD + 1
  492. LOADUI R0 48 ; Insert '0'
  493. PUSH8 R0 R3 ; HOLD[0] = '0' && HOLD = HOLD + 1
  494. PUSH8 R0 R3 ; HOLD[0] = '0' && HOLD = HOLD + 1
  495. LOADUI R0 39 ; Insert '\''
  496. PUSH8 R0 R3 ; HOLD[0] = '\'' && HOLD = HOLD + 1
  497. LOADUI R0 10 ; Insert '\n'
  498. PUSH8 R0 R3 ; HOLD[0] = '\n' && HOLD = HOLD + 1
  499. ADDUI R3 R3 1 ; NULL Terminate
  500. SWAP R3 R14 ; CALLOC HOLD
  501. MOVE R0 R3 ; Return HOLD
  502. POPR R4 R15 ; Restore R4
  503. POPR R3 R15 ; Restore R3
  504. POPR R2 R15 ; Restore R2
  505. POPR R1 R15 ; Restore R1
  506. RET R15
  507. ;; hex function
  508. ;; Receives Char in R0
  509. ;; Return Int in R0
  510. :hex
  511. SUBUI R0 R0 48 ; First shift
  512. CMPSKIPI.GE R0 10 ; If 0-9
  513. RET R15 ; Be done
  514. ;; Deal with A-F
  515. ANDI R0 R0 0xDF ; Unset high bit
  516. SUBUI R0 R0 7 ; Shift them down
  517. CMPSKIPI.GE R0 10 ; if between 9 and A
  518. JUMP @hex_error ; Throw an error
  519. CMPSKIPI.L R0 16 ; if > F
  520. JUMP @hex_error ; Throw an error
  521. RET R15
  522. :hex_error
  523. LOADUI R0 $hex_error_message ; Our message
  524. FALSE R1 ; For human
  525. CALLI R15 @file_print ; write it
  526. CALLI R15 @line_error ; More info
  527. HALT
  528. :hex_error_message
  529. "Tried to print non-hex number
  530. "
  531. ;; escape_lookup function
  532. ;; Receives char* in R0
  533. ;; Returns char in R0
  534. :escape_lookup
  535. PUSHR R1 R15 ; Protect R1
  536. PUSHR R2 R15 ; Protect R2
  537. MOVE R1 R0 ; Put C in the right spot
  538. FALSE R2 ; Our flag for done
  539. LOADU8 R0 R1 0 ; c[0]
  540. CMPSKIPI.E R0 92 ; If C[0] != '\\'
  541. JUMP @escape_lookup_none ; Deal with none case
  542. LOADU8 R0 R1 1 ; c[1]
  543. CMPSKIPI.NE R0 120 ; if \x??
  544. JUMP @escape_lookup_hex
  545. ;; Deal with \? escapes
  546. CMPSKIPI.NE R0 110 ; If \n
  547. LOADUI R2 10 ; return \n
  548. CMPSKIPI.NE R0 116 ; If \t
  549. LOADUI R2 9 ; return \t
  550. CMPSKIPI.NE R0 92 ; If \\
  551. LOADUI R2 92 ; return \\
  552. CMPSKIPI.NE R0 39 ; If \'
  553. LOADUI R2 39 ; return \'
  554. CMPSKIPI.NE R0 34 ; If \"
  555. LOADUI R2 34 ; return \"
  556. CMPSKIPI.NE R0 114 ; If \r
  557. LOADUI R2 13 ; return \r
  558. JUMP.Z R2 @escape_lookup_error ; Looks like we got something weird
  559. JUMP @escape_lookup_done ; Otherwise just use our R2
  560. :escape_lookup_none
  561. MOVE R2 R0 ; We just return the char at C[0]
  562. JUMP @escape_lookup_done ; Be done
  563. :escape_lookup_hex
  564. LOADU8 R0 R1 2 ; c[2]
  565. CALLI R15 @hex ; Get first char
  566. SL0I R0 4 ; Shift our first nybble
  567. MOVE R2 R0 ; Protect our top nybble
  568. LOADU8 R0 R1 3 ; c[3]
  569. CALLI R15 @hex ; Get second char
  570. ADD R2 R2 R0 ; \x?? => ? << 4 + ?
  571. :escape_lookup_done
  572. MOVE R0 R2 ; R2 has our answer
  573. POPR R2 R15 ; Restore R2
  574. POPR R1 R15 ; Restore R1
  575. RET R15
  576. :escape_lookup_error
  577. MOVE R2 R0 ; Protect Char that failed
  578. LOADUI R0 $escape_lookup_string0 ; Load message
  579. FALSE R1 ; We want the User to see
  580. CALLI R15 @file_print ; Write it
  581. MOVE R0 R2 ; Our CHAR
  582. FPUTC ; Write it
  583. LOADUI R0 10 ; '\n'
  584. FPUTC ; Write it
  585. CALLI R15 @line_error ; Provide some debug information
  586. HALT
  587. :escape_lookup_string0
  588. "Received invalid escape \\"
  589. ;; collect_regular_string function
  590. ;; Converts C string into a RAW string for mescc-tools
  591. ;; Receives char* in R0
  592. ;; R14 is HEAP Pointer
  593. ;; Returns char* in R0
  594. :collect_regular_string
  595. PUSHR R1 R15 ; Protect R1
  596. PUSHR R2 R15 ; Protect R2
  597. COPY R2 R14 ; MESSAGE
  598. MOVE R1 R0 ; Put STRING in the right place
  599. :collect_regular_string_iter
  600. LOADU8 R0 R1 0 ; STRING[0]
  601. JUMP.Z R0 @collect_regular_string_done ; End at NULL
  602. CMPSKIPI.NE R0 92 ; if STRING[0] == '\\'
  603. JUMP @collect_regular_string_escape ; deal with escapes
  604. ;; Deal with vannilla chars
  605. STORE8 R0 R2 0 ; MESSAGE[0] = STRING[0]
  606. ADDUI R2 R2 1 ; MESSAGE = MESSAGE + 1
  607. ADDUI R1 R1 1 ; STRING = STRING + 1
  608. JUMP @collect_regular_string_iter ; Loop
  609. :collect_regular_string_escape
  610. COPY R0 R1 ; Prepare for call
  611. CALLI R15 @escape_lookup ; Get what weird char we need
  612. STORE8 R0 R2 0 ; MESSAGE[0] = escape_lookup(string)
  613. ADDUI R2 R2 1 ; MESSAGE = MESSAGE + 1
  614. LOADU8 R0 R1 1 ; STRING[1]
  615. CMPSKIPI.NE R0 120 ; if \x??
  616. ADDUI R1 R1 2 ; STRING = STRING + 2
  617. ADDUI R1 R1 2 ; STRING = STRING + 2
  618. JUMP @collect_regular_string_iter ; Loop
  619. :collect_regular_string_done
  620. LOADUI R0 34 ; Using "
  621. STORE8 R0 R2 0 ; MESSAGE[0] = '"'
  622. LOADUI R0 10 ; Using '\n'
  623. STORE8 R0 R2 1 ; MESSAGE[1] = "\n"
  624. ADDUI R2 R2 3 ; Add extra NULL padding
  625. SWAP R2 R14 ; Update HEAP
  626. MOVE R0 R2 ; Put MESSAGE in the right Spot
  627. POPR R2 R15 ; Restore R2
  628. POPR R1 R15 ; Restore R1
  629. RET R15
  630. ;; unary_expr_sizeof function
  631. ;; Receives nothing
  632. ;; Returns nothing
  633. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  634. :unary_expr_sizeof
  635. PUSHR R0 R15 ; Protect R0
  636. PUSHR R1 R15 ; Protect R1
  637. PUSHR R2 R15 ; Protect R2
  638. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  639. LOADUI R0 $unary_expr_sizeof_string0 ; Our first error message
  640. LOADUI R1 $open_paren ; Using "("
  641. CALLI R15 @require_match ; Ensure a match
  642. CALLI R15 @type_name ; Get type_name
  643. MOVE R2 R0 ; Protect A
  644. LOADUI R0 $unary_expr_sizeof_string1 ; Our final error message
  645. LOADUI R1 $close_paren ; Using ")"
  646. CALLI R15 @require_match ; Ensure a match
  647. LOADUI R0 $unary_expr_sizeof_string2 ; Our header
  648. CALLI R15 @emit_out ; emit it
  649. LOAD32 R0 R2 4 ; A->SIZE
  650. CALLI R15 @numerate_number ; Convert to string
  651. CALLI R15 @emit_out ; emit it
  652. LOADUI R0 $newline ; Using "\n"
  653. CALLI R15 @emit_out ; emit it
  654. POPR R2 R15 ; Restore R2
  655. POPR R1 R15 ; Restore R1
  656. POPR R0 R15 ; Restore R0
  657. RET R15
  658. :unary_expr_sizeof_string0
  659. "ERROR in unary_expr
  660. Missing (
  661. "
  662. :unary_expr_sizeof_string1
  663. "ERROR in unary_expr
  664. Missing )
  665. "
  666. :unary_expr_sizeof_string2
  667. "LOAD_IMMEDIATE_rax %"
  668. ;; constant_load function
  669. ;; Receives struct token_list* a in R0
  670. ;; Returns nothing
  671. :constant_load
  672. PUSHR R0 R15 ; Protect R0
  673. LOADUI R0 $constant_load_string0 ; Our header
  674. CALLI R15 @emit_out ; emit it
  675. POPR R0 R15 ; Restore R0
  676. LOAD32 R0 R0 16 ; A->ARGUMENTS
  677. LOAD32 R0 R0 8 ; A->ARGUMENTS->S
  678. CALLI R15 @emit_out ; emit it
  679. LOADUI R0 $newline ; Using "\n"
  680. CALLI R15 @emit_out ; emit it
  681. RET R15
  682. :constant_load_string0
  683. "LOAD_IMMEDIATE_rax %"
  684. ;; variable_load function
  685. ;; Receives struct token_list* a in R0
  686. ;; and struct token_list* current_target in R8
  687. ;; Returns Nothing
  688. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  689. :variable_load
  690. PUSHR R1 R15 ; Protect R1
  691. PUSHR R2 R15 ; Protect R2
  692. MOVE R2 R0 ; Protect A
  693. ;; Check if function call
  694. LOADUI R0 $type_function_name ; Using "FUNCTION"
  695. LOAD32 R1 R2 12 ; A->TYPE
  696. LOAD32 R1 R1 24 ; A->TYPE->NAME
  697. CALLI R15 @match ; IF "FUNCTION" == A->TYPE->NAME
  698. JUMP.Z R0 @variable_load_regular ; Nope
  699. LOADUI R0 $open_paren ; Using "("
  700. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  701. CALLI R15 @match ; IF "(" == GLOBAL_TOKEN->S
  702. JUMP.Z R0 @variable_load_regular ; Nope
  703. ;; Deal with function call
  704. LOAD32 R0 R2 16 ; A->DEPTH
  705. CALLI R15 @numerate_number ; Convert to string
  706. TRUE R1 ; Passing TRUE
  707. CALLI R15 @function_call ; DO IT
  708. JUMP @variable_load_done ; Be done
  709. :variable_load_regular
  710. LOAD32 R8 R2 12 ; CURRENT_TARGET = A->TYPE
  711. LOADUI R0 $variable_load_string0 ; Our prefix
  712. CALLI R15 @emit_out ; emit it
  713. LOAD32 R0 R2 16 ; A->DEPTH
  714. CALLI R15 @numerate_number ; Convert to string
  715. CALLI R15 @emit_out ; emit it
  716. LOADUI R0 $newline ; Using "\n"
  717. CALLI R15 @emit_out ; emit it
  718. ;; check for special case 1
  719. LOADUI R0 $equal ; Using "="
  720. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  721. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  722. JUMP.NZ R0 @variable_load_done ; Be done
  723. ;; deal with the general case
  724. LOADUI R0 $variable_load_string1 ; Our postfix
  725. CALLI R15 @emit_out ; emit it
  726. :variable_load_done
  727. POPR R2 R15 ; Restore R2
  728. POPR R1 R15 ; Restore R1
  729. RET R15
  730. :variable_load_string0
  731. "LOAD_BASE_ADDRESS_rax %"
  732. :variable_load_string1
  733. "LOAD_INTEGER
  734. "
  735. ;; function_load function
  736. ;; Receives struct token_list* a in R0
  737. ;; Returns nothing
  738. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  739. :function_load
  740. PUSHR R1 R15 ; Protect R1
  741. LOAD32 R0 R0 8 ; A->S
  742. PUSHR R0 R15 ; Protect A->S
  743. LOADUI R0 $open_paren ; Using "("
  744. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  745. CALLI R15 @match ; If GLOBAL_TOKEN->S == "("
  746. JUMP.Z R0 @function_load_regular ; If not do the simple thing
  747. ;; Deal iwth function call
  748. POPR R0 R15 ; Restore A->S
  749. FALSE R1 ; FALSE
  750. CALLI R15 @function_call ; Do the function call
  751. JUMP @function_load_done ; Clean up
  752. :function_load_regular
  753. LOADUI R0 $function_load_string0 ; Using our header string
  754. CALLI R15 @emit_out ; emit it
  755. POPR R0 R15 ; Restore A->S
  756. CALLI R15 @emit_out ; emit it
  757. LOADUI R0 $newline ; Using "\n"
  758. CALLI R15 @emit_out ; emit it
  759. :function_load_done
  760. POPR R1 R15 ; Restore R1
  761. RET R15
  762. :function_load_string0
  763. "LOAD_IMMEDIATE_rax &FUNCTION_"
  764. ;; global_load function
  765. ;; Receives struct token_list* a in R0
  766. ;; and struct token_list* current_target in R8
  767. ;; Returns nothing
  768. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  769. :global_load
  770. PUSHR R0 R15 ; Protect A
  771. LOAD32 R8 R0 12 ; CURRENT_TARGET = A->TYPE
  772. LOADUI R0 $global_load_string0 ; Our header string
  773. CALLI R15 @emit_out ; emit it
  774. POPR R0 R15 ; Restore A
  775. LOAD32 R0 R0 8 ; A->S
  776. CALLI R15 @emit_out ; emit it
  777. LOADUI R0 $newline ; Using "\n"
  778. CALLI R15 @emit_out ; emit it
  779. PUSHR R1 R15 ; Protect R1
  780. LOADUI R0 $equal ; Using "="
  781. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  782. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  783. JUMP.NZ R0 @global_load_done ; Skip the following
  784. ;; Deal with non-assignment
  785. LOADUI R0 $global_load_string1 ; Our footer string
  786. CALLI R15 @emit_out ; emit it
  787. :global_load_done
  788. POPR R1 R15 ; Restore R1
  789. RET R15
  790. :global_load_string0
  791. "LOAD_IMMEDIATE_rax &GLOBAL_"
  792. :global_load_string1
  793. "LOAD_INTEGER
  794. "
  795. ;; primary_expr_failure function
  796. ;; Fails hard and fast
  797. ;; Receives nothing
  798. ;; HALTs and will trash registers
  799. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  800. :primary_expr_failure
  801. LOADUI R0 $primary_expr_failure_string0 ; Our first string
  802. FALSE R1 ; Display to User
  803. CALLI R15 @file_print ; Print it
  804. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  805. CALLI R15 @file_print ; Print it
  806. LOADUI R0 $primary_expr_failure_string1 ; Our last string
  807. CALLI R15 @file_print ; Print it
  808. CALLI R15 @line_error ; Make it a line error message too
  809. HALT
  810. :primary_expr_failure_string0
  811. "Received "
  812. :primary_expr_failure_string1
  813. " in primary_expr
  814. "
  815. ;; primary_expr_string function
  816. ;; Receives struct token_list* global_token in R13,
  817. ;; struct token_list* out in R12,
  818. ;; struct token_list* string_list in R11
  819. ;; struct token_list* global_list in R10
  820. ;; and struct token_list* FUNC in R9
  821. ;; and struct token_list* current_target in R8
  822. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  823. ;; Returns the token_lists modified
  824. :primary_expr_string
  825. PUSHR R0 R15 ; Protect R0
  826. PUSHR R1 R15 ; Protect R1
  827. PUSHR R2 R15 ; Protect R2
  828. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  829. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  830. STORER32 R1 @current_count ; Update CURRENT_COUNT
  831. CALLI R15 @numerate_number ; Convert to string
  832. MOVE R2 R0 ; Put string in safe place
  833. LOADUI R0 $primary_expr_string_string0 ; Our string prefix
  834. CALLI R15 @emit_out ; emit it
  835. LOAD32 R0 R9 8 ; FUNCTION->S
  836. COPY R1 R2 ; NUMBER_STRING
  837. CALLI R15 @uniqueID_out ; Make it unique
  838. ;; The target
  839. LOADUI R0 $primary_expr_string_string1
  840. COPY R1 R11 ; Using STRINGS_LIST
  841. CALLI R15 @emit ; emit it
  842. MOVE R1 R0 ; Put STRINGS_LIST in correct place
  843. LOAD32 R0 R9 8 ; Using FUNCTION->S
  844. CALLI R15 @uniqueID ; Make it unique
  845. MOVE R11 R0 ; Update STRINGS_LIST
  846. ;; Parse the string
  847. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  848. CALLI R15 @parse_string ; Parse it
  849. COPY R1 R11 ; Using STRINGS_LIST
  850. CALLI R15 @emit ; emit it
  851. MOVE R11 R0 ; Update STRINGS_LIST
  852. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  853. POPR R2 R15 ; Restore R2
  854. POPR R1 R15 ; Restore R1
  855. POPR R0 R15 ; Restore R0
  856. RET R15
  857. :primary_expr_string_string0
  858. "LOAD_IMMEDIATE_rax &STRING_"
  859. :primary_expr_string_string1
  860. ":STRING_"
  861. ;; primary_expr_char function
  862. ;; Receives struct token_list* global_token in R13,
  863. ;; struct token_list* out in R12,
  864. ;; struct token_list* string_list in R11
  865. ;; struct token_list* global_list in R10
  866. ;; and struct token_list* FUNC in R9
  867. ;; and struct token_list* current_target in R8
  868. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  869. ;; Returns the token_lists modified
  870. :primary_expr_char
  871. PUSHR R0 R15 ; Protect R0
  872. LOADUI R0 $primary_expr_char_string0 ; Using our header string
  873. CALLI R15 @emit_out ; emit it
  874. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  875. ADDUI R0 R0 1 ; GLOBAL_TOKEN->S + 1
  876. CALLI R15 @escape_lookup ; escape_lookup value
  877. CALLI R15 @numerate_number ; Make it a string
  878. CALLI R15 @emit_out ; emit it
  879. LOADUI R0 $newline ; Using "\n"
  880. CALLI R15 @emit_out ; emit it
  881. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  882. POPR R0 R15 ; Restore R0
  883. RET R15
  884. :primary_expr_char_string0
  885. "LOAD_IMMEDIATE_rax %"
  886. ;; primary_expr_number function
  887. ;; struct token_list* out in R12,
  888. ;; struct token_list* string_list in R11
  889. ;; struct token_list* global_list in R10
  890. ;; and struct token_list* FUNC in R9
  891. ;; and struct token_list* current_target in R8
  892. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  893. ;; Returns the token_lists modified
  894. :primary_expr_number
  895. LOADUI R0 $primary_expr_number_string0 ; Our header
  896. CALLI R15 @emit_out ; emit it
  897. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  898. CALLI R15 @emit_out ; emit it
  899. LOADUI R0 $newline ; Using "\n"
  900. CALLI R15 @emit_out ; emit it
  901. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  902. RET R15
  903. :primary_expr_number_string0
  904. "LOAD_IMMEDIATE_rax %"
  905. ;; primary_expr_variable function
  906. ;; struct token_list* out in R12,
  907. ;; struct token_list* string_list in R11
  908. ;; struct token_list* global_list in R10
  909. ;; and struct token_list* FUNC in R9
  910. ;; and struct token_list* current_target in R8
  911. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  912. ;; Returns the token_lists modified
  913. :primary_expr_variable
  914. PUSHR R0 R15 ; Protect R0
  915. PUSHR R1 R15 ; Protect R1
  916. PUSHR R2 R15 ; Protect R2
  917. LOAD32 R2 R13 8 ; S = GLOBAL_TOKEN->S
  918. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  919. COPY R0 R2 ; Using S
  920. LOADR32 R1 @global_constant_list
  921. CALLI R15 @sym_lookup ; Lookup S in CONSTANTS
  922. JUMP.Z R0 @primary_expr_variable_locals ; try Locals
  923. ;; Deal with Constants
  924. CALLI R15 @constant_load ; A is in R0 already
  925. JUMP @primary_expr_variable_done ; Moving on
  926. :primary_expr_variable_locals
  927. COPY R0 R2 ; Using S
  928. LOAD32 R1 R9 4 ; Using FUNCTION->LOCALS
  929. CALLI R15 @sym_lookup ; Lookup S in Locals
  930. JUMP.Z R0 @primary_expr_variable_arguments ; try arguments
  931. ;; Deal with Locals
  932. CALLI R15 @variable_load ; A is in R0 already
  933. JUMP @primary_expr_variable_done ; Moving on
  934. :primary_expr_variable_arguments
  935. COPY R0 R2 ; Using S
  936. LOAD32 R1 R9 16 ; Using FUNCTION->ARGUMENTS
  937. CALLI R15 @sym_lookup ; Lookup S in arguments
  938. JUMP.Z R0 @primary_expr_variable_function ; try Functions
  939. ;; Deal with argument
  940. CALLI R15 @variable_load ; A is in R0 already
  941. JUMP @primary_expr_variable_done ; Moving on
  942. :primary_expr_variable_function
  943. COPY R0 R2 ; Using S
  944. LOADR32 R1 @global_function_list ; Get current GLOBAL_FUNCTION_LIST
  945. CALLI R15 @sym_lookup ; Lookup S in GLOBAL_FUNCTION_LIST
  946. JUMP.Z R0 @primary_expr_variable_global ; try Globals
  947. ;; Deal with function
  948. CALLI R15 @function_load ; Dothe work
  949. JUMP @primary_expr_variable_done ; Moving on
  950. :primary_expr_variable_global
  951. COPY R0 R2 ; Using S
  952. LOADR32 R1 @global_symbol_list ; Get current GLOBAL_SYMBOL_LIST
  953. CALLI R15 @sym_lookup ; Lookup S in GLOBAL_SYMBOL_LIST
  954. JUMP.Z R0 @primary_expr_variable_failure ; Looks like it isn't anything we know
  955. ;; Deal with a global
  956. CALLI R15 @global_load
  957. :primary_expr_variable_done
  958. POPR R2 R15 ; Restore R2
  959. POPR R1 R15 ; Restore R1
  960. POPR R0 R15 ; Restore R0
  961. RET R15
  962. :primary_expr_variable_failure
  963. MOVE R0 R2 ; Using S
  964. FALSE R1 ; We want the user to see
  965. CALLI R15 @file_print ; Print it
  966. LOADUI R0 $primary_expr_variable_string0 ; Body
  967. CALLI R15 @file_print ; Print it
  968. CALLI R15 @line_error ; Provide useful error info
  969. HALT
  970. :primary_expr_variable_string0
  971. " is not a defined symbol
  972. "
  973. ;; promote_type function
  974. ;; Receives struct type* in R0 and struct type* in R1
  975. ;; Returns first match struct type* in R0
  976. :promote_type
  977. JUMP.Z R1 @promote_type_abort0 ; If B is NULL just abort
  978. PUSHR R1 R15 ; Protect R1
  979. SWAP R0 R1 ; Give A a try
  980. JUMP.Z R1 @promote_type_abort1 ; A is NULL just short abort
  981. ;; Looks like we have a bunch of work to do
  982. PUSHR R2 R15 ; Protect R2
  983. PUSHR R3 R15 ; Protect R3
  984. PUSHR R4 R15 ; Protect R4
  985. PUSHR R5 R15 ; Protect R5
  986. MOVE R5 R1 ; Put A in the right spot
  987. MOVE R4 R0 ; Put B in the right spot
  988. LOADR32 R3 @global_types ; I = GLOBAL_TYPES
  989. :promote_type_iter
  990. LOAD32 R1 R3 24 ; I->NAME
  991. LOAD32 R0 R5 24 ; A->NAME
  992. CMPJUMPI.E R0 R1 @promote_type_done ; break
  993. LOAD32 R0 R4 24 ; B->NAME
  994. CMPJUMPI.E R0 R1 @promote_type_done ; break
  995. LOAD32 R1 R3 12 ; I->INDIRECT
  996. LOAD32 R1 R1 24 ; I->INDIRECT->NAME
  997. LOAD32 R0 R5 24 ; A->NAME
  998. CMPJUMPI.E R0 R1 @promote_type_done ; break
  999. LOAD32 R0 R4 24 ; B->NAME
  1000. CMPJUMPI.E R0 R1 @promote_type_done ; break
  1001. LOAD32 R3 R3 0 ; I = I->NEXT
  1002. JUMP.NZ R3 @promote_type_iter ; Loop if not NULL
  1003. :promote_type_done
  1004. MOVE R0 R3 ; Return I
  1005. POPR R5 R15 ; Restore R5
  1006. POPR R4 R15 ; Restore R4
  1007. POPR R3 R15 ; Restore R3
  1008. POPR R2 R15 ; Restore R2
  1009. :promote_type_abort1
  1010. POPR R1 R15 ; Restore R1
  1011. :promote_type_abort0
  1012. RET R15
  1013. ;; common_recursion function
  1014. ;; Receives FUNCTION* in R0
  1015. ;; struct token_list* out in R12,
  1016. ;; struct token_list* string_list in R11
  1017. ;; struct token_list* global_list in R10
  1018. ;; and struct token_list* FUNC in R9
  1019. ;; and struct token_list* current_target in R8
  1020. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1021. ;; Returns the token_lists modified
  1022. :common_recursion
  1023. PUSHR R0 R15 ; Protect R0
  1024. PUSHR R1 R15 ; Protect R1
  1025. PUSHR R2 R15 ; Protect R2
  1026. MOVE R2 R0 ; Protect F
  1027. COPY R1 R8 ; LAST_TYPE = CURRENT_TARGET
  1028. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1029. LOADUI R0 $common_recursion_string0 ; Header string
  1030. CALLI R15 @emit_out ; Our header
  1031. CALL R2 R15 ; CALL F()
  1032. COPY R0 R8 ; Using CURRENT_TARGET
  1033. CALLI R15 @promote_type ; Promote type
  1034. MOVE R8 R0 ; update CURRENT_TARGET
  1035. LOADUI R0 $common_recursion_string1 ; Footer string
  1036. CALLI R15 @emit_out ; Our footer
  1037. POPR R2 R15 ; Restore R2
  1038. POPR R1 R15 ; Restore R1
  1039. POPR R0 R15 ; Restore R0
  1040. RET R15
  1041. :common_recursion_string0
  1042. "PUSH_RAX #_common_recursion
  1043. "
  1044. :common_recursion_string1
  1045. "POP_RBX # _common_recursion
  1046. "
  1047. ;; general_recursion function
  1048. ;; Receives FUNCTION F in R0, char* s in R1, char* name in R2
  1049. ;; and FUNCTION ITERATE in R3
  1050. ;; struct token_list* out in R12,
  1051. ;; struct token_list* string_list in R11
  1052. ;; struct token_list* global_list in R10
  1053. ;; and struct token_list* FUNC in R9
  1054. ;; and struct token_list* current_target in R8
  1055. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1056. ;; Returns nothing
  1057. :general_recursion
  1058. PUSHR R0 R15 ; Protect R0
  1059. PUSHR R1 R15 ; Protect S
  1060. PUSHR R0 R15 ; Protect F
  1061. COPY R0 R2 ; Using NAME
  1062. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1063. CALLI R15 @match ; IF GLOBAL_TOKEN->S == NAME
  1064. JUMP.Z R0 @general_recursion_done
  1065. ;; deal with case of match
  1066. POPR R0 R15 ; Restore F
  1067. CALLI R15 @common_recursion ; Recurse
  1068. POPR R1 R15 ; Restore S
  1069. COPY R0 R1 ; Put S in correct place
  1070. CALLI R15 @emit_out ; emit it
  1071. CALL R3 R15 ; CALL ITERATE()
  1072. POPR R0 R15 ; Restore R0
  1073. RET R15 ; Don't double pop
  1074. :general_recursion_done
  1075. POPR R0 R15 ; Restore F
  1076. POPR R1 R15 ; Restore S
  1077. POPR R0 R15 ; Restore R0
  1078. RET R15
  1079. ;; ceil_log2 function
  1080. ;; Receives INT A in R0
  1081. ;; Returns LOG2(A) in R0
  1082. :ceil_log2
  1083. PUSHR R1 R15 ; Protect R1
  1084. PUSHR R2 R15 ; Protect R2
  1085. FALSE R2 ; RESULT = 0
  1086. SUBI R1 R0 1 ; A - 1
  1087. AND R1 R1 R0 ; A & (A - 1)
  1088. CMPSKIPI.NE R1 0 ; IF (A & (A - 1)) == 0
  1089. LOADI R2 -1 ; RESULT = -1
  1090. :ceil_log2_iter
  1091. JUMP.Z R0 @ceil_log2_done ; IF A > 0
  1092. ADDI R2 R2 1 ; RESULT = RESULT + 1
  1093. SARI R0 1 ; A = A >> 1
  1094. JUMP @ceil_log2_iter ; Loop
  1095. :ceil_log2_done
  1096. MOVE R0 R2 ; Use RESULT
  1097. POPR R2 R15 ; Restore R2
  1098. POPR R1 R15 ; Restore R1
  1099. RET R15
  1100. ;; postfix_expr_arrow function
  1101. ;; Receives nothing
  1102. ;; struct token_list* out in R12,
  1103. ;; struct token_list* string_list in R11
  1104. ;; struct token_list* global_list in R10
  1105. ;; and struct token_list* FUNC in R9
  1106. ;; and struct token_list* current_target in R8
  1107. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1108. ;; Returns the token_lists modified
  1109. :postfix_expr_arrow
  1110. PUSHR R0 R15 ; Protect R0
  1111. PUSHR R1 R15 ; Protect R1
  1112. PUSHR R2 R15 ; Protect R2
  1113. LOADUI R0 $postfix_expr_arrow_string0 ; Our header string
  1114. CALLI R15 @emit_out ; Emit it
  1115. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1116. COPY R0 R8 ; Passing CURRENT_TARGET
  1117. LOAD32 R1 R13 8 ; Using GLOBAL_TOKEN->S
  1118. CALLI R15 @lookup_member ; Look it up
  1119. LOAD32 R2 R0 4 ; Protect I->SIZE
  1120. LOAD32 R8 R0 20 ; CURRENT_TARGET = I->TYPE
  1121. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1122. LOAD32 R1 R0 8 ; I->OFFSET
  1123. JUMP.Z R1 @postfix_expr_arrow_offset ; If no offset needed skip the work
  1124. ;; Deal with non-zero offsets
  1125. LOADUI R0 $postfix_expr_arrow_string1 ; Our first prefix
  1126. CALLI R15 @emit_out ; Emit it
  1127. LOADUI R0 $postfix_expr_arrow_string2 ; Our second prefix
  1128. CALLI R15 @emit_out ; Emit it
  1129. MOVE R0 R1 ; Put I->OFFSET in the right place
  1130. CALLI R15 @numerate_number ; Convert to string
  1131. CALLI R15 @emit_out ; Emit it
  1132. LOADUI R0 $postfix_expr_arrow_string3 ; Our postfix
  1133. CALLI R15 @emit_out ; Emit it
  1134. :postfix_expr_arrow_offset
  1135. LOADUI R0 $equal ; Using "="
  1136. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1137. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  1138. JUMP.NZ R0 @postfix_expr_arrow_done
  1139. LOADUI R0 8 ; Compare against 8
  1140. CMPJUMPI.L R0 R2 @postfix_expr_arrow_done
  1141. ;; Deal with special case
  1142. LOADUI R0 $postfix_expr_arrow_string4 ; Our final string
  1143. CALLI R15 @emit_out ; Emit it
  1144. :postfix_expr_arrow_done
  1145. POPR R2 R15 ; Restore R2
  1146. POPR R1 R15 ; Restore R1
  1147. POPR R0 R15 ; Restore R0
  1148. RET R15
  1149. :postfix_expr_arrow_string0
  1150. "# looking up offset
  1151. "
  1152. :postfix_expr_arrow_string1
  1153. "# -> offset calculation
  1154. "
  1155. :postfix_expr_arrow_string2
  1156. "LOAD_IMMEDIATE_rbx %"
  1157. :postfix_expr_arrow_string3
  1158. "
  1159. ADD_rbx_to_rax
  1160. "
  1161. :postfix_expr_arrow_string4
  1162. "LOAD_INTEGER
  1163. "
  1164. ;; postfix_expr_array function
  1165. ;; Receives nothing
  1166. ;; struct token_list* out in R12,
  1167. ;; struct token_list* string_list in R11
  1168. ;; struct token_list* global_list in R10
  1169. ;; and struct token_list* FUNC in R9
  1170. ;; and struct token_list* current_target in R8
  1171. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1172. ;; Returns the token_lists modified
  1173. :postfix_expr_array
  1174. PUSHR R0 R15 ; Protect R0
  1175. PUSHR R1 R15 ; Protect R1
  1176. PUSHR R2 R15 ; Protect R2
  1177. COPY R2 R8 ; ARRAY = CURRENT_TARGET
  1178. LOADUI R0 $expression ; Using EXPRESSION
  1179. CALLI R15 @common_recursion ; Recurse
  1180. MOVE R8 R2 ; CURRENT_TARGET = ARRAY
  1181. LOADUI R2 $postfix_expr_array_string0 ; ASSIGN = load integer
  1182. LOADUI R0 $type_char_indirect_name ; Using "char*"
  1183. LOAD32 R1 R8 24 ; CURRENT_TARGET->NAME
  1184. CALLI R15 @match ; IF CURRENT_TARGET->NAME == "char*"
  1185. CMPSKIPI.E R0 0 ; deal with Byte
  1186. LOADUI R2 $postfix_expr_array_string1 ; ASSIGN = load byte
  1187. JUMP.NZ R0 @postfix_expr_array_byte ; Skip if Byte
  1188. ;; Deal with larger than byte
  1189. LOADUI R0 $postfix_expr_array_string2 ; Our shift
  1190. CALLI R15 @emit_out ; emit it
  1191. LOAD32 R0 R8 12 ; CURRENT_TARGET->INDIRECT
  1192. LOAD32 R0 R0 4 ; CURRENT_TARGET->INDIRECT->SIZE
  1193. CALLI R15 @ceil_log2 ; LOG2(CURRENT_TARGET->INDIRECT->SIZE)
  1194. CALLI R15 @numerate_number ; Convert to string
  1195. CALLI R15 @emit_out ; emit it
  1196. LOADUI R0 $newline ; Using "\n"
  1197. CALLI R15 @emit_out ; emit it
  1198. :postfix_expr_array_byte
  1199. LOADUI R0 $postfix_expr_array_string3 ; Add the offset
  1200. CALLI R15 @emit_out ; emit it
  1201. LOADUI R0 $postfix_expr_array_string4 ; Our final error message
  1202. LOADUI R1 $close_bracket ; Using "]"
  1203. CALLI R15 @require_match ; Ensure match
  1204. LOADUI R0 $equal ; Using "="
  1205. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1206. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  1207. CMPSKIPI.E R0 0 ; If match
  1208. LOADUI R2 $postfix_expr_array_string5 ; empty string
  1209. MOVE R0 R2 ; What ever string survived
  1210. CALLI R15 @emit_out ; emit it
  1211. POPR R2 R15 ; Restore R2
  1212. POPR R1 R15 ; Restore R1
  1213. POPR R0 R15 ; Restore R0
  1214. RET R15
  1215. :postfix_expr_array_string0
  1216. "LOAD_INTEGER
  1217. "
  1218. :postfix_expr_array_string1
  1219. "LOAD_BYTE
  1220. "
  1221. :postfix_expr_array_string2
  1222. "SAL_rax_Immediate8 !"
  1223. :postfix_expr_array_string3
  1224. "ADD_rbx_to_rax
  1225. "
  1226. :postfix_expr_array_string4
  1227. "ERROR in postfix_expr
  1228. Missing ]
  1229. "
  1230. :postfix_expr_array_string5
  1231. ""
  1232. ;; postfix_expr_stub function
  1233. ;; Receives nothing
  1234. ;; struct token_list* out in R12,
  1235. ;; struct token_list* string_list in R11
  1236. ;; struct token_list* global_list in R10
  1237. ;; and struct token_list* FUNC in R9
  1238. ;; and struct token_list* current_target in R8
  1239. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1240. ;; Returns the token_lists modified
  1241. :postfix_expr_stub
  1242. PUSHR R0 R15 ; Protect R0
  1243. PUSHR R1 R15 ; Protect R1
  1244. LOADUI R0 $open_bracket ; Using "["
  1245. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1246. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "["
  1247. JUMP.Z R0 @postfix_expr_stub_next
  1248. ;; Deal with "[" case
  1249. CALLI R15 @postfix_expr_array ; process
  1250. CALLI R15 @postfix_expr_stub ; recurse
  1251. :postfix_expr_stub_next
  1252. LOADUI R0 $arrow_string ; Using "->"
  1253. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1254. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "->"
  1255. JUMP.Z R0 @postfix_expr_stub_done ; clean up
  1256. ;; Deal with "->" case
  1257. CALLI R15 @postfix_expr_arrow ; Process
  1258. CALLI R15 @postfix_expr_stub ; recurse
  1259. :postfix_expr_stub_done
  1260. POPR R1 R15 ; Restore R1
  1261. POPR R0 R15 ; Restore R0
  1262. RET R15
  1263. ;; postfix_expr function
  1264. ;; struct token_list* out in R12,
  1265. ;; struct token_list* string_list in R11
  1266. ;; struct token_list* global_list in R10
  1267. ;; and struct token_list* FUNC in R9
  1268. ;; and struct token_list* current_target in R8
  1269. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1270. ;; Returns the token_lists modified
  1271. :postfix_expr
  1272. CALLI R15 @primary_expr ; Walk up the tree
  1273. CALLI R15 @postfix_expr_stub ; Deal with nodes on this level
  1274. RET R15
  1275. ;; additive_expr_stub function
  1276. ;; receives nothing
  1277. ;; returns nothing
  1278. ;; Updates struct token_list*
  1279. :additive_expr_stub
  1280. PUSHR R0 R15 ; Protect R0
  1281. PUSHR R1 R15 ; Protect R1
  1282. PUSHR R2 R15 ; Protect R2
  1283. PUSHR R3 R15 ; Protect R3
  1284. ;; Fixed pieces
  1285. LOADUI R0 $postfix_expr ; Set First argument
  1286. LOADUI R3 $additive_expr_stub
  1287. ;; The + bit
  1288. LOADUI R1 $additive_expr_stub_string0 ; Our first operation
  1289. LOADUI R2 $plus_string ; Using "+"
  1290. CALLI R15 @general_recursion
  1291. ;; The - bit
  1292. LOADUI R1 $additive_expr_stub_string1 ; Our second operation
  1293. LOADUI R2 $minus_string ; Using "-"
  1294. CALLI R15 @general_recursion
  1295. ;; The * bit
  1296. LOADUI R1 $additive_expr_stub_string2 ; Our third operation
  1297. LOADUI R2 $multiply_string ; Using "*"
  1298. CALLI R15 @general_recursion
  1299. ;; The / bit
  1300. LOADUI R1 $additive_expr_stub_string3 ; Our fourth operation
  1301. LOADUI R2 $divide_string ; Using "/"
  1302. CALLI R15 @general_recursion
  1303. ;; The % bit
  1304. LOADUI R1 $additive_expr_stub_string4 ; Our fifth operation
  1305. LOADUI R2 $modulus_string ; Using "%"
  1306. CALLI R15 @general_recursion
  1307. ;; The << bit
  1308. LOADUI R1 $additive_expr_stub_string5 ; Our sixth operation
  1309. LOADUI R2 $left_shift_string ; Using "<<"
  1310. CALLI R15 @general_recursion
  1311. ;; The >> bit
  1312. LOADUI R1 $additive_expr_stub_string6 ; Our final operation
  1313. LOADUI R2 $right_shift_string ; Using ">>"
  1314. CALLI R15 @general_recursion
  1315. POPR R3 R15 ; Restore R3
  1316. POPR R2 R15 ; Restore R2
  1317. POPR R1 R15 ; Restore R1
  1318. POPR R0 R15 ; Restore R0
  1319. RET R15
  1320. :additive_expr_stub_string0
  1321. "ADD_rbx_to_rax
  1322. "
  1323. :additive_expr_stub_string1
  1324. "SUBTRACT_rax_from_rbx_into_rbx
  1325. MOVE_rbx_to_rax
  1326. "
  1327. :additive_expr_stub_string2
  1328. "MULTIPLY_rax_by_rbx_into_rax
  1329. "
  1330. :additive_expr_stub_string3
  1331. "XCHG_rax_rbx
  1332. LOAD_IMMEDIATE_rdx %0
  1333. DIVIDE_rax_by_rbx_into_rax
  1334. "
  1335. :additive_expr_stub_string4
  1336. "XCHG_rax_rbx
  1337. LOAD_IMMEDIATE_rdx %0
  1338. MODULUS_rax_from_rbx_into_rbx
  1339. MOVE_rdx_to_rax
  1340. "
  1341. :additive_expr_stub_string5
  1342. "COPY_rax_to_rcx
  1343. COPY_rbx_to_rax
  1344. SAL_rax_cl
  1345. "
  1346. :additive_expr_stub_string6
  1347. "COPY_rax_to_rcx
  1348. COPY_rbx_to_rax
  1349. SAR_rax_cl
  1350. "
  1351. ;; additive_expr function
  1352. ;; Receives struct token_list* global_token in R13,
  1353. ;; struct token_list* out in R12,
  1354. ;; struct token_list* string_list in R11
  1355. ;; struct token_list* global_list in R10
  1356. ;; and struct token_list* FUNC in R9
  1357. ;; and struct token_list* current_target in R8
  1358. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1359. ;; Returns the token_lists modified
  1360. :additive_expr
  1361. CALLI R15 @postfix_expr ; Walk up the tree
  1362. CALLI R15 @additive_expr_stub ; Deal with nodes at this level
  1363. RET R15
  1364. ;; relational_expr_stub function
  1365. ;; receives nothing
  1366. ;; returns nothing
  1367. ;; Updates struct token_list*
  1368. :relational_expr_stub
  1369. PUSHR R0 R15 ; Protect R0
  1370. PUSHR R1 R15 ; Protect R1
  1371. PUSHR R2 R15 ; Protect R2
  1372. PUSHR R3 R15 ; Protect R3
  1373. ;; Fixed pieces
  1374. LOADUI R0 $additive_expr ; Set First argument
  1375. LOADUI R3 $relational_expr_stub
  1376. ;; The < bit
  1377. LOADUI R1 $relational_expr_stub_string0 ; Our first operation
  1378. LOADUI R2 $less_than_string ; Using "<"
  1379. CALLI R15 @general_recursion
  1380. ;; The <= bit
  1381. LOADUI R1 $relational_expr_stub_string1 ; Our second operation
  1382. LOADUI R2 $less_than_equal_string ; Using "<="
  1383. CALLI R15 @general_recursion
  1384. ;; The >= bit
  1385. LOADUI R1 $relational_expr_stub_string2 ; Our third operation
  1386. LOADUI R2 $greater_than_equal_string ; Using ">="
  1387. CALLI R15 @general_recursion
  1388. ;; The > bit
  1389. LOADUI R1 $relational_expr_stub_string3 ; Our fourth operation
  1390. LOADUI R2 $greater_than_string ; Using ">"
  1391. CALLI R15 @general_recursion
  1392. ;; The == bit
  1393. LOADUI R1 $relational_expr_stub_string4 ; Our fifth operation
  1394. LOADUI R2 $equal_to_string ; Using "=="
  1395. CALLI R15 @general_recursion
  1396. ;; The != bit
  1397. LOADUI R1 $relational_expr_stub_string5 ; Our final operation
  1398. LOADUI R2 $not_equal_string ; Using "!="
  1399. CALLI R15 @general_recursion
  1400. POPR R3 R15 ; Restore R3
  1401. POPR R2 R15 ; Restore R2
  1402. POPR R1 R15 ; Restore R1
  1403. POPR R0 R15 ; Restore R0
  1404. RET R15
  1405. :relational_expr_stub_string0
  1406. "CMP
  1407. SETL
  1408. MOVEZX
  1409. "
  1410. :relational_expr_stub_string1
  1411. "CMP
  1412. SETLE
  1413. MOVEZX
  1414. "
  1415. :relational_expr_stub_string2
  1416. "CMP
  1417. SETGE
  1418. MOVEZX
  1419. "
  1420. :relational_expr_stub_string3
  1421. "CMP
  1422. SETG
  1423. MOVEZX
  1424. "
  1425. :relational_expr_stub_string4
  1426. "CMP
  1427. SETE
  1428. MOVEZX
  1429. "
  1430. :relational_expr_stub_string5
  1431. "CMP
  1432. SETNE
  1433. MOVEZX
  1434. "
  1435. ;; relational_expr function
  1436. ;; Receives struct token_list* global_token in R13,
  1437. ;; struct token_list* out in R12,
  1438. ;; struct token_list* string_list in R11
  1439. ;; struct token_list* global_list in R10
  1440. ;; and struct token_list* FUNC in R9
  1441. ;; and struct token_list* current_target in R8
  1442. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1443. ;; Returns the token_lists modified
  1444. :relational_expr
  1445. CALLI R15 @additive_expr ; Walk up the tree
  1446. CALLI R15 @relational_expr_stub ; Deal with nodes at this level
  1447. RET R15
  1448. ;; relational_expr_stub function
  1449. ;; receives nothing
  1450. ;; returns nothing
  1451. ;; Updates struct token_list*
  1452. :bitwise_expr_stub
  1453. PUSHR R0 R15 ; Protect R0
  1454. PUSHR R1 R15 ; Protect R1
  1455. PUSHR R2 R15 ; Protect R2
  1456. PUSHR R3 R15 ; Protect R3
  1457. ;; Fixed pieces
  1458. LOADUI R0 $relational_expr ; Set First argument
  1459. LOADUI R3 $bitwise_expr_stub
  1460. ;; The & bit
  1461. LOADUI R1 $bitwise_expr_stub_string0 ; Our first operation
  1462. LOADUI R2 $bitwise_and ; Using "&"
  1463. CALLI R15 @general_recursion
  1464. ;; The && bit
  1465. LOADUI R1 $bitwise_expr_stub_string0 ; Our first operation
  1466. LOADUI R2 $logical_and ; Using "&&"
  1467. CALLI R15 @general_recursion
  1468. ;; The | bit
  1469. LOADUI R1 $bitwise_expr_stub_string1 ; Our second operation
  1470. LOADUI R2 $bitwise_or ; Using "|"
  1471. CALLI R15 @general_recursion
  1472. ;; The || bit
  1473. LOADUI R1 $bitwise_expr_stub_string1 ; Our second operation
  1474. LOADUI R2 $logical_or ; Using "||"
  1475. CALLI R15 @general_recursion
  1476. ;; The ^ bit
  1477. LOADUI R1 $bitwise_expr_stub_string2 ; Our second operation
  1478. LOADUI R2 $bitwise_xor ; Using "^"
  1479. CALLI R15 @general_recursion
  1480. POPR R3 R15 ; Restore R3
  1481. POPR R2 R15 ; Restore R2
  1482. POPR R1 R15 ; Restore R1
  1483. POPR R0 R15 ; Restore R0
  1484. RET R15
  1485. :bitwise_expr_stub_string0
  1486. "AND_rax_rbx
  1487. "
  1488. :bitwise_expr_stub_string1
  1489. "OR_rax_rbx
  1490. "
  1491. :bitwise_expr_stub_string2
  1492. "XOR_rbx_rax_into_rax
  1493. "
  1494. ;; bitwise_expr function
  1495. ;; Receives struct token_list* global_token in R13,
  1496. ;; struct token_list* out in R12,
  1497. ;; struct token_list* string_list in R11
  1498. ;; struct token_list* global_list in R10
  1499. ;; and struct token_list* FUNC in R9
  1500. ;; and struct token_list* current_target in R8
  1501. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1502. ;; Returns the token_lists modified
  1503. :bitwise_expr
  1504. CALLI R15 @relational_expr ; Walk up the tree
  1505. CALLI R15 @bitwise_expr_stub ; Deal with nodes at this level
  1506. RET R15
  1507. ;; primary_expr function
  1508. ;; Receives struct token_list* global_token in R13,
  1509. ;; struct token_list* out in R12,
  1510. ;; struct token_list* string_list in R11
  1511. ;; struct token_list* global_list in R10
  1512. ;; and struct token_list* FUNC in R9
  1513. ;; and struct token_list* current_target in R8
  1514. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1515. ;; Returns the token_lists modified
  1516. :primary_expr
  1517. PUSHR R0 R15 ; Protect R0
  1518. PUSHR R1 R15 ; Protect R1
  1519. LOADUI R0 $sizeof_string ; Load "sizeof"
  1520. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1521. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "sizeof"
  1522. JUMP.Z R0 @primary_expr_negate ; Guess not
  1523. ;; Deal with sizeof expression
  1524. CALLI R15 @unary_expr_sizeof ; Do real work
  1525. JUMP @primary_expr_done ; Wrap up
  1526. :primary_expr_negate
  1527. LOADU8 R0 R1 0 ; GLOBAL_TOKEN->S[0]
  1528. CMPSKIPI.E R0 45 ; IF GLOBAL_TOKEN->S[0] == '-'
  1529. JUMP @primary_expr_bang ; If not try '!'
  1530. ;; Deal with -a and -4 expressions
  1531. LOADUI R0 $primary_expr_str0 ; Load HEADER
  1532. CALLI R15 @emit_out ; emit it
  1533. LOADUI R0 $primary_expr ; Using PRIMARY_EXPR
  1534. CALLI R15 @common_recursion ; Recurse
  1535. LOADUI R0 $primary_expr_str1 ; add footer
  1536. CALLI R15 @emit_out ; emit it
  1537. JUMP @primary_expr_done ; Wrap up
  1538. :primary_expr_bang
  1539. CMPSKIPI.E R0 33 ; IF GLOBAL_TOKEN->S[0] == "!"
  1540. JUMP @primary_expr_nested ; If not try '('
  1541. ;; deal with !a expressions
  1542. LOADUI R0 $primary_expr_str2 ; Load HEADER
  1543. CALLI R15 @emit_out ; emit it
  1544. LOADUI R0 $postfix_expr ; Using POSTFIX_EXPR
  1545. CALLI R15 @common_recursion ; Recurse
  1546. LOADUI R0 $primary_expr_str3 ; add footer
  1547. CALLI R15 @emit_out ; emit it
  1548. JUMP @primary_expr_done ; Wrap up
  1549. :primary_expr_nested
  1550. CMPSKIPI.E R0 40 ; IF GLOBAL_TOKEN->S[0] == '('
  1551. JUMP @primary_expr_ch ; If not try 'char'
  1552. ;; Deal with ( expr )
  1553. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1554. CALLI R15 @expression ; Recurse
  1555. LOADUI R0 $primary_expr_str4 ; Using error message
  1556. LOADUI R1 $close_paren ; Using ")"
  1557. CALLI R15 @require_match ; Make sure we have closing match
  1558. JUMP @primary_expr_done ; Wrap up
  1559. :primary_expr_ch
  1560. CMPSKIPI.E R0 39 ; IF GLOBAL_TOKEN->S[0] == '\''
  1561. JUMP @primary_expr_st ; If not try "string"
  1562. ;; Deal with 'char'
  1563. CALLI R15 @primary_expr_char ; Collect char
  1564. JUMP @primary_expr_done ; Wrap up
  1565. :primary_expr_st
  1566. CMPSKIPI.E R0 34 ; IF GLOBAL_TOKEN->S[0] == '"'
  1567. JUMP @primary_expr_var ; If not try variables
  1568. ;; deal with "string"
  1569. CALLI R15 @primary_expr_string ; Collect string
  1570. JUMP @primary_expr_done ; Wrap up
  1571. :primary_expr_var
  1572. LOADUI R1 $variable_chars ; Using a-z+A-Z+_
  1573. CALLI R15 @in_set ; IF GLOBAL_TOKEN->S[0] in a-z+A-Z+_
  1574. JUMP.Z R0 @primary_expr_num
  1575. ;; Deal with foo TODO
  1576. CALLI R15 @primary_expr_variable ; deal with names
  1577. JUMP @primary_expr_done ; Wrap up
  1578. :primary_expr_num
  1579. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  1580. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  1581. LOADUI R1 $digit_chars ; Using 0-9
  1582. CALLI R15 @in_set ; IF GLOBAL_TOKEN->S[0] in 0-9
  1583. JUMP.Z R0 @primary_expr_failure ; Fail HARD
  1584. ;; Deal with 5
  1585. CALLI R15 @primary_expr_number ; deal with number
  1586. :primary_expr_done
  1587. POPR R1 R15 ; Restore R1
  1588. POPR R0 R15 ; Restore R0
  1589. RET R15
  1590. :primary_expr_str0
  1591. "LOAD_IMMEDIATE_rax %0
  1592. "
  1593. :primary_expr_str1
  1594. "SUBTRACT_rax_from_rbx_into_rbx
  1595. MOVE_rbx_to_rax
  1596. "
  1597. :primary_expr_str2
  1598. "LOAD_IMMEDIATE_rax %1
  1599. "
  1600. :primary_expr_str3
  1601. "XOR_rbx_rax_into_rax
  1602. "
  1603. :primary_expr_str4
  1604. "Error in Primary expression
  1605. Didn't get )
  1606. "
  1607. ;; expression function
  1608. ;; Receives struct token_list* global_token in R13,
  1609. ;; struct token_list* out in R12,
  1610. ;; struct token_list* string_list in R11
  1611. ;; struct token_list* global_list in R10
  1612. ;; and struct token_list* FUNC in R9
  1613. ;; and struct token_list* current_target in R8
  1614. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1615. ;; Returns the token_lists modified
  1616. :expression
  1617. PUSHR R0 R15 ; Protect R0
  1618. PUSHR R1 R15 ; Protect R1
  1619. PUSHR R2 R15 ; Protect R2
  1620. PUSHR R3 R15 ; Protect R3
  1621. CALLI R15 @bitwise_expr ; Check for more primitives first
  1622. LOADUI R0 $equal ; Using "="
  1623. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1624. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  1625. JUMP.Z R0 @expression_done ; Be done
  1626. ;; Determine store type
  1627. LOADUI R3 $expression_string1 ; Assuming the default of STORE CHAR
  1628. ;; First possible reason for INT
  1629. LOADUI R0 $close_bracket ; Using "]"
  1630. LOAD32 R1 R13 4 ; GLOBAL_TOKEN->PREV
  1631. LOAD32 R1 R1 8 ; GLOBAL_TOKEN->PREV->S
  1632. CALLI R15 @match ; IF GLOBAL_TOKEN->PREV-> == "]"
  1633. CMPSKIPI.NE R0 0 ; IF FALSE
  1634. LOADUI R3 $expression_string0 ; STORE INTEGER
  1635. ;; Second possible reason for INTeger
  1636. LOADUI R0 $type_char_indirect_name ; Using "char*"
  1637. LOAD32 R1 R8 24 ; CURRENT_TARGET->NAME
  1638. CALLI R15 @match ; IF CURRENT_TARGET->NAME == "char*"
  1639. CMPSKIPI.NE R0 0 ; IF FALSE
  1640. LOADUI R3 $expression_string0 ; STORE INTEGER
  1641. ;; Recurse to evaluate expression being stored
  1642. LOADUI R0 $expression ; Using expression
  1643. CALLI R15 @common_recursion ; Perform common recursion
  1644. ;; Put our string and clean up
  1645. MOVE R0 R3 ; Using our STORED string
  1646. CALLI R15 @emit_out ; emit it
  1647. FALSE R8 ; CURRENT_TARGET = NULL
  1648. :expression_done
  1649. POPR R3 R15 ; Restore R3
  1650. POPR R2 R15 ; Restore R2
  1651. POPR R1 R15 ; Restore R1
  1652. POPR R0 R15 ; Restore R0
  1653. RET R15
  1654. :expression_string0
  1655. "STORE_INTEGER
  1656. "
  1657. :expression_string1
  1658. "STORE_CHAR
  1659. "
  1660. ;; process_if function
  1661. ;; struct token_list* out in R12,
  1662. ;; struct token_list* string_list in R11
  1663. ;; struct token_list* global_list in R10
  1664. ;; and struct token_list* FUNC in R9
  1665. ;; and struct token_list* current_target in R8
  1666. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1667. ;; Returns the token_lists modified
  1668. :process_if
  1669. PUSHR R0 R15 ; Protect R0
  1670. PUSHR R1 R15 ; Protect R1
  1671. PUSHR R2 R15 ; Protect R2
  1672. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  1673. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  1674. STORER32 R1 @current_count ; Update CURRENT_COUNT
  1675. CALLI R15 @numerate_number ; Convert CURRENT_COUNT to string
  1676. MOVE R2 R0 ; Protect our string
  1677. LOADUI R0 $process_if_string0 ; using first string
  1678. CALLI R15 @emit_out ; emit it
  1679. LOAD32 R0 R9 8 ; FUNCTION->S
  1680. COPY R1 R2 ; Using our current count string
  1681. CALLI R15 @uniqueID_out ; Add unique identifier
  1682. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1683. LOADUI R0 $process_if_string1 ; Our first error message
  1684. LOADUI R1 $open_paren ; Using "("
  1685. CALLI R15 @require_match ; Make sure we have what we need
  1686. CALLI R15 @expression ; Recurse to get our boolean expression
  1687. LOADUI R0 $process_if_string2 ; Our test and jump
  1688. CALLI R15 @emit_out ; emit it
  1689. LOAD32 R0 R9 8 ; FUNCTION->S
  1690. COPY R1 R2 ; Using our current count string
  1691. CALLI R15 @uniqueID_out ; Add unique identifier
  1692. LOADUI R0 $process_if_string3 ; Our second error message
  1693. LOADUI R1 $close_paren ; Using ")"
  1694. CALLI R15 @require_match ; Make sure we have what we need
  1695. CALLI R15 @statement ; Collect our if statement
  1696. LOADUI R0 $process_if_string4 ; Our jump over else
  1697. CALLI R15 @emit_out ; emit it
  1698. LOAD32 R0 R9 8 ; FUNCTION->S
  1699. COPY R1 R2 ; Using our current count string
  1700. CALLI R15 @uniqueID_out ; Add unique identifier
  1701. LOADUI R0 $process_if_string5 ; Our else label
  1702. CALLI R15 @emit_out ; emit it
  1703. LOAD32 R0 R9 8 ; FUNCTION->S
  1704. CALLI R15 @uniqueID_out ; Add unique identifier
  1705. LOADUI R0 $else_string ; Using "else"
  1706. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1707. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "else"
  1708. JUMP.Z R0 @process_if_else ; Looks like no else
  1709. ;; Deal with else
  1710. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1711. CALLI R15 @statement ; Grab else statement
  1712. :process_if_else
  1713. LOADUI R0 $process_if_string6 ; Our jump over else
  1714. CALLI R15 @emit_out ; emit it
  1715. LOAD32 R0 R9 8 ; FUNCTION->S
  1716. COPY R1 R2 ; Using our current count string
  1717. CALLI R15 @uniqueID_out ; Add unique identifier
  1718. POPR R2 R15 ; Restore R2
  1719. POPR R1 R15 ; Restore R1
  1720. POPR R0 R15 ; Restore R0
  1721. RET R15
  1722. :process_if_string0
  1723. "# IF_"
  1724. :process_if_string1
  1725. "ERROR in process_if
  1726. MISSING (
  1727. "
  1728. :process_if_string2
  1729. "TEST
  1730. JUMP_EQ %ELSE_"
  1731. :process_if_string3
  1732. "ERROR in process_if
  1733. MISSING )
  1734. "
  1735. :process_if_string4
  1736. "JUMP %_END_IF_"
  1737. :process_if_string5
  1738. ":ELSE_"
  1739. :process_if_string6
  1740. ":_END_IF_"
  1741. ;; save_break_frame microfunction
  1742. ;; Overwrites R0 and R1
  1743. ;; Saves break frame on stack
  1744. ;; Returns to caller
  1745. :save_break_frame
  1746. POPR R1 R15 ; Save return address
  1747. LOADR32 R0 @break_frame ; Obtain BREAK_FRAME
  1748. PUSHR R0 R15 ; Protect BREAK_FRAME
  1749. LOADR32 R0 @break_target_head ; obtain HEAD
  1750. PUSHR R0 R15 ; Protect HEAD
  1751. LOADR32 R0 @break_target_func ; obtain FUNC
  1752. PUSHR R0 R15 ; Protect FUNC
  1753. LOADR32 R0 @break_target_num ; obtain NUM
  1754. PUSHR R0 R15 ; Protect NUM
  1755. PUSHR R1 R15 ; Set where we are returning to
  1756. RET R15
  1757. ;; restore_break_frame microfunction
  1758. ;; Overwrites R0 and R1
  1759. ;; Restores break frame from stack
  1760. ;; Returns to caller
  1761. :restore_break_frame
  1762. POPR R1 R15 ; Save return address
  1763. POPR R0 R15 ; obtain NUM
  1764. STORER32 R0 @break_target_num ; Restore NUM
  1765. POPR R0 R15 ; obtain FUNC
  1766. STORER32 R0 @break_target_func ; Restore FUNC
  1767. POPR R0 R15 ; obtain HEAD
  1768. STORER32 R0 @break_target_head ; Restore HEAD
  1769. POPR R0 R15 ; obtain BREAK_FRAME
  1770. STORER32 R0 @break_frame ; Restore BREAK_FRAME
  1771. PUSHR R1 R15 ; Set where we are returning to
  1772. RET R15
  1773. ;; set_break_frame microfunction
  1774. ;; Receives char* num in R0, char* head in R1
  1775. ;; Overwrites R0
  1776. ;; Sets break frame using
  1777. ;; R9 holding FUNC
  1778. ;; Returns to calling function
  1779. :set_break_frame
  1780. STORER32 R1 @break_target_head ; update BREAK_TARGET_HEAD
  1781. STORER32 R0 @break_target_num ; Update BREAK_TARGET_NUM
  1782. LOAD32 R0 R9 4 ; Using FUNCTION->LOCALS
  1783. STORER32 R0 @break_frame ; update BREAK_FRAME
  1784. LOAD32 R0 R9 8 ; Using FUNCTION->S
  1785. STORER32 R0 @break_target_func ; update BREAK_TARGET_FUNC
  1786. RET R15
  1787. ;; process_for function
  1788. ;; struct token_list* out in R12,
  1789. ;; struct token_list* string_list in R11
  1790. ;; struct token_list* global_list in R10
  1791. ;; and struct token_list* FUNC in R9
  1792. ;; and struct token_list* current_target in R8
  1793. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1794. ;; Returns the token_lists modified
  1795. :process_for
  1796. PUSHR R2 R15 ; Protect R2
  1797. PUSHR R1 R15 ; Protect R1
  1798. PUSHR R0 R15 ; Protect R0
  1799. CALLI R15 @save_break_frame ; Save break frame
  1800. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  1801. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  1802. STORER32 R1 @current_count ; Update CURRENT_COUNT
  1803. CALLI R15 @numerate_number ; Convert to string
  1804. COPY R2 R0 ; Protect NUMBER_STRING
  1805. LOADUI R1 $process_for_string0 ; Get new HEAD
  1806. CALLI R15 @set_break_frame ; Set the break frame values
  1807. LOADUI R0 $process_for_string1 ; Our comment header
  1808. CALLI R15 @emit_out ; emit it
  1809. COPY R1 R2 ; Using NUMBER_STRING
  1810. LOAD32 R0 R9 8 ; FUNCTION->S
  1811. CALLI R15 @uniqueID_out ; emit it
  1812. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1813. LOADUI R0 $process_for_string2 ; Our first error message
  1814. LOADUI R1 $open_paren ; Using "("
  1815. CALLI R15 @require_match ; Verify match
  1816. LOADUI R0 $semicolon ; Using ";"
  1817. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1818. CALLI R15 @match ; IF GLOBAL_TOKEN->S -- ";"
  1819. CMPSKIPI.NE R0 0 ; If GLOBAL_TOKEN->S != ";"
  1820. CALLI R15 @expression ; Skip that step
  1821. LOADUI R0 $process_for_string3 ; Our comment header
  1822. CALLI R15 @emit_out ; emit it
  1823. COPY R1 R2 ; Using NUMBER_STRING
  1824. LOAD32 R0 R9 8 ; FUNCTION->S
  1825. CALLI R15 @uniqueID_out ; emit it
  1826. LOADUI R0 $process_for_string4 ; Our second error message
  1827. LOADUI R1 $semicolon ; Using ";"
  1828. CALLI R15 @require_match ; Verify match
  1829. CALLI R15 @expression ; TEST logic required
  1830. LOADUI R0 $process_for_string5 ; Our comment header
  1831. CALLI R15 @emit_out ; emit it
  1832. COPY R1 R2 ; Using NUMBER_STRING
  1833. LOAD32 R0 R9 8 ; FUNCTION->S
  1834. CALLI R15 @uniqueID_out ; emit it
  1835. LOADUI R0 $process_for_string6 ; Our comment header
  1836. CALLI R15 @emit_out ; emit it
  1837. LOAD32 R0 R9 8 ; FUNCTION->S
  1838. CALLI R15 @uniqueID_out ; emit it
  1839. LOADUI R0 $process_for_string7 ; Our comment header
  1840. CALLI R15 @emit_out ; emit it
  1841. LOAD32 R0 R9 8 ; FUNCTION->S
  1842. CALLI R15 @uniqueID_out ; emit it
  1843. LOADUI R0 $process_for_string8 ; Our third error message
  1844. LOADUI R1 $semicolon ; Using ";"
  1845. CALLI R15 @require_match ; Verify match
  1846. CALLI R15 @expression ; Iterator logic
  1847. LOADUI R0 $process_for_string9 ; Our comment header
  1848. CALLI R15 @emit_out ; emit it
  1849. COPY R1 R2 ; Using NUMBER_STRING
  1850. LOAD32 R0 R9 8 ; FUNCTION->S
  1851. CALLI R15 @uniqueID_out ; emit it
  1852. LOADUI R0 $process_for_string10 ; Our comment header
  1853. CALLI R15 @emit_out ; emit it
  1854. LOAD32 R0 R9 8 ; FUNCTION->S
  1855. CALLI R15 @uniqueID_out ; emit it
  1856. LOADUI R0 $process_for_string11 ; Our final error message
  1857. LOADUI R1 $close_paren ; Using ")"
  1858. CALLI R15 @require_match ; Verify match
  1859. CALLI R15 @statement ; Main body
  1860. LOADUI R0 $process_for_string12 ; Our comment header
  1861. CALLI R15 @emit_out ; emit it
  1862. COPY R1 R2 ; Using NUMBER_STRING
  1863. LOAD32 R0 R9 8 ; FUNCTION->S
  1864. CALLI R15 @uniqueID_out ; emit it
  1865. LOADUI R0 $process_for_string13 ; Our comment header
  1866. CALLI R15 @emit_out ; emit it
  1867. LOAD32 R0 R9 8 ; FUNCTION->S
  1868. CALLI R15 @uniqueID_out ; emit it
  1869. CALLI R15 @restore_break_frame ; Restore break frame
  1870. POPR R0 R15 ; Restore R0
  1871. POPR R1 R15 ; Restore R1
  1872. POPR R2 R15 ; Restore R2
  1873. RET R15
  1874. :process_for_string0
  1875. "FOR_END_"
  1876. :process_for_string1
  1877. "# FOR_initialization_"
  1878. :process_for_string2
  1879. "ERROR in process_for
  1880. MISSING (
  1881. "
  1882. :process_for_string3
  1883. ":FOR_"
  1884. :process_for_string4
  1885. "ERROR in process_for
  1886. MISSING ;1
  1887. "
  1888. :process_for_string5
  1889. "TEST
  1890. JUMP_EQ %FOR_END_"
  1891. :process_for_string6
  1892. "JUMP %FOR_THEN_"
  1893. :process_for_string7
  1894. ":FOR_ITER_"
  1895. :process_for_string8
  1896. "ERROR in process_for
  1897. MISSING ;2
  1898. "
  1899. :process_for_string9
  1900. "JUMP %FOR_"
  1901. :process_for_string10
  1902. ":FOR_THEN_"
  1903. :process_for_string11
  1904. "ERROR in process_for
  1905. MISSING )
  1906. "
  1907. :process_for_string12
  1908. "JUMP %FOR_ITER_"
  1909. :process_for_string13
  1910. ":FOR_END_"
  1911. ;; process_do function
  1912. ;; struct token_list* out in R12,
  1913. ;; struct token_list* string_list in R11
  1914. ;; struct token_list* global_list in R10
  1915. ;; and struct token_list* FUNC in R9
  1916. ;; and struct token_list* current_target in R8
  1917. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1918. ;; Returns the token_lists modified
  1919. :process_do
  1920. PUSHR R2 R15 ; Protect R2
  1921. PUSHR R1 R15 ; Protect R1
  1922. PUSHR R0 R15 ; Protect R0
  1923. CALLI R15 @save_break_frame ; Save break frame
  1924. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  1925. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  1926. STORER32 R1 @current_count ; Update CURRENT_COUNT
  1927. CALLI R15 @numerate_number ; Convert to string
  1928. COPY R2 R0 ; Protect NUMBER_STRING
  1929. LOADUI R1 $process_do_string0 ; Using our desired head
  1930. CALLI R15 @set_break_frame ; Set the break frame values
  1931. LOADUI R0 $process_do_string1 ; Our label
  1932. CALLI R15 @emit_out ; emit it
  1933. COPY R1 R2 ; Using NUMBER_STRING
  1934. LOAD32 R0 R9 8 ; FUNCTION->S
  1935. CALLI R15 @uniqueID_out ; emit it
  1936. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1937. CALLI R15 @statement ; Collect our Do statement
  1938. LOADUI R0 $process_do_string2 ; our first error message
  1939. LOADUI R1 $while_string ; Using "while"
  1940. CALLI R15 @require_match ; Check for match
  1941. LOADUI R0 $process_do_string3 ; our second error message
  1942. LOADUI R1 $open_paren ; Using "("
  1943. CALLI R15 @require_match ; Check for match
  1944. CALLI R15 @expression ; Our logical expression
  1945. LOADUI R0 $process_do_string4 ; our third error message
  1946. LOADUI R1 $close_paren ; Using ")"
  1947. CALLI R15 @require_match ; Check for match
  1948. LOADUI R0 $process_do_string5 ; our final error message
  1949. LOADUI R1 $semicolon ; Using ";"
  1950. CALLI R15 @require_match ; Check for match
  1951. LOADUI R0 $process_do_string6 ; Our test string
  1952. CALLI R15 @emit_out ; emit it
  1953. COPY R1 R2 ; Put NUMBER_STRING in right place
  1954. LOAD32 R0 R9 8 ; FUNCTION->S
  1955. CALLI R15 @uniqueID_out ; emit it
  1956. LOADUI R0 $process_do_string7 ; Our end label string
  1957. CALLI R15 @emit_out ; emit it
  1958. LOAD32 R0 R9 8 ; FUNCTION->S
  1959. CALLI R15 @uniqueID_out ; emit it
  1960. CALLI R15 @restore_break_frame ; Restore break frame
  1961. POPR R0 R15 ; Restore R0
  1962. POPR R1 R15 ; Restore R1
  1963. POPR R2 R15 ; Restore R2
  1964. RET R15
  1965. :process_do_string0
  1966. "DO_END_"
  1967. :process_do_string1
  1968. ":DO_"
  1969. :process_do_string2
  1970. "ERROR in process_do
  1971. MISSING while
  1972. "
  1973. :process_do_string3
  1974. "ERROR in process_do
  1975. MISSING (
  1976. "
  1977. :process_do_string4
  1978. "ERROR in process_do
  1979. MISSING )
  1980. "
  1981. :process_do_string5
  1982. "ERROR in process_do
  1983. MISSING ;
  1984. "
  1985. :process_do_string6
  1986. "TEST
  1987. JUMP_NE %DO_"
  1988. :process_do_string7
  1989. ":DO_END_"
  1990. ;; process_while function
  1991. ;; struct token_list* out in R12,
  1992. ;; struct token_list* string_list in R11
  1993. ;; struct token_list* global_list in R10
  1994. ;; and struct token_list* FUNC in R9
  1995. ;; and struct token_list* current_target in R8
  1996. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1997. ;; Returns the token_lists modified
  1998. :process_while
  1999. PUSHR R2 R15 ; Protect R2
  2000. PUSHR R1 R15 ; Protect R1
  2001. PUSHR R0 R15 ; Protect R0
  2002. CALLI R15 @save_break_frame ; Save break frame
  2003. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  2004. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  2005. STORER32 R1 @current_count ; Update CURRENT_COUNT
  2006. CALLI R15 @numerate_number ; Convert to string
  2007. COPY R2 R0 ; Protect NUMBER_STRING
  2008. LOADUI R1 $process_while_string0 ; Set HEAD
  2009. CALLI R15 @set_break_frame ; Set the break frame values
  2010. LOADUI R0 $process_while_string1 ; Our head label
  2011. CALLI R15 @emit_out ; emit it
  2012. COPY R1 R2 ; Using NUMBER_STRING
  2013. LOAD32 R0 R9 8 ; FUNCTION->S
  2014. CALLI R15 @uniqueID_out ; emit it
  2015. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2016. LOADUI R0 $process_while_string2 ; Our first error message
  2017. LOADUI R1 $open_paren ; Using "("
  2018. CALLI R15 @require_match ; Check for match
  2019. CALLI R15 @expression ; Collect test expression
  2020. LOADUI R0 $process_while_string3 ; Our test and jump
  2021. CALLI R15 @emit_out ; emit it
  2022. COPY R1 R2 ; Using NUMBER_STRING
  2023. LOAD32 R0 R9 8 ; FUNCTION->S
  2024. CALLI R15 @uniqueID_out ; emit it
  2025. LOADUI R0 $process_while_string4 ; Our trailing comment
  2026. CALLI R15 @emit_out ; emit it
  2027. LOAD32 R0 R9 8 ; FUNCTION->S
  2028. CALLI R15 @uniqueID_out ; emit it
  2029. LOADUI R0 $process_while_string5 ; Our first error message
  2030. LOADUI R1 $close_paren ; Using ")"
  2031. CALLI R15 @require_match ; Check for match
  2032. CALLI R15 @statement ; Collect our loop statement
  2033. LOADUI R0 $process_while_string6 ; Our test and jump
  2034. CALLI R15 @emit_out ; emit it
  2035. COPY R1 R2 ; Using NUMBER_STRING
  2036. LOAD32 R0 R9 8 ; FUNCTION->S
  2037. CALLI R15 @uniqueID_out ; emit it
  2038. LOADUI R0 $process_while_string7 ; Our trailing comment
  2039. CALLI R15 @emit_out ; emit it
  2040. LOAD32 R0 R9 8 ; FUNCTION->S
  2041. CALLI R15 @uniqueID_out ; emit it
  2042. CALLI R15 @restore_break_frame ; Restore break frame
  2043. POPR R0 R15 ; Restore R0
  2044. POPR R1 R15 ; Restore R1
  2045. POPR R2 R15 ; Restore R2
  2046. RET R15
  2047. :process_while_string0
  2048. "END_WHILE_"
  2049. :process_while_string1
  2050. ":WHILE_"
  2051. :process_while_string2
  2052. "ERROR in process_while
  2053. MISSING (
  2054. "
  2055. :process_while_string3
  2056. "TEST
  2057. JUMP_EQ %END_WHILE_"
  2058. :process_while_string4
  2059. "# THEN_while_"
  2060. :process_while_string5
  2061. "ERROR in process_while
  2062. MISSING )
  2063. "
  2064. :process_while_string6
  2065. "JUMP %WHILE_"
  2066. :process_while_string7
  2067. ":END_WHILE_"
  2068. ;; return_result function
  2069. ;; Receives nothing
  2070. ;; Returns nothing
  2071. ;; and struct token_list* FUNC in R9
  2072. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2073. :return_result
  2074. PUSHR R0 R15 ; Protect R0
  2075. PUSHR R1 R15 ; Protect R1
  2076. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2077. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2078. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2079. CMPSKIPI.E R0 59 ; IF GLOBAL_TOKEN->S[0] == ';'
  2080. CALLI R15 @expression ; Evaluate expression
  2081. LOADUI R0 $return_result_string0 ; Using or error message
  2082. LOADUI R1 $semicolon ; Using ";"
  2083. CALLI R15 @require_match ; Require a match to ";"
  2084. LOADUI R0 $return_result_string1 ; Our pop command
  2085. LOAD32 R1 R9 4 ; FUNCTION->LOCALS
  2086. :return_result_iter
  2087. JUMP.Z R1 @return_result_done ; Be done when we hit NULL
  2088. CALLI R15 @emit_out ; Put the string every iteration
  2089. LOAD32 R1 R1 0 ; I = I->NEXT
  2090. JUMP @return_result_iter ; Keep looping
  2091. :return_result_done
  2092. LOADUI R0 $return_result_string2 ; Our footer
  2093. CALLI R15 @emit_out ; emit it
  2094. POPR R1 R15 ; Restore R1
  2095. POPR R0 R15 ; Restore R0
  2096. RET R15
  2097. :return_result_string0
  2098. "ERROR in return_result
  2099. MISSING ;
  2100. "
  2101. :return_result_string1
  2102. "POP_RBX # _return_result_locals
  2103. "
  2104. :return_result_string2
  2105. "RETURN
  2106. "
  2107. ;; process_break function
  2108. ;; Receives nothing
  2109. ;; and struct token_list* FUNC in R9
  2110. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2111. ;; Returns the token_lists modified
  2112. :process_break
  2113. PUSHR R0 R15 ; Protect R0
  2114. PUSHR R1 R15 ; Protect R1
  2115. PUSHR R2 R15 ; Protect R2
  2116. LOADR32 R0 @break_target_head ; BREAK_TARGET_HEAD
  2117. JUMP.NZ R0 @process_break_NON_NULL
  2118. ;; Deal with NULL == BREAK_TARGET_HEAD
  2119. LOADUI R0 $process_break_string0 ; Our first error message
  2120. FALSE R1 ; Write for User
  2121. CALLI R15 @file_print ; write it
  2122. CALLI R15 @line_error ; Give useful info
  2123. LOADUI R0 $newline ; Using "\n"
  2124. CALLI R15 @file_print ; Print it
  2125. HALT
  2126. :process_break_NON_NULL
  2127. LOADR32 R2 @break_frame ; BREAK_FRAME
  2128. LOAD32 R1 R9 4 ; I = FUNCTION->LOCALS
  2129. LOADUI R0 $process_break_string1 ; Our pop string
  2130. :process_break_iter
  2131. CMPJUMPI.E R1 R2 @process_break_done ; IF I == BREAK_FRAME
  2132. JUMP.Z R1 @process_break_done ; IF NULL == I break
  2133. CALLI R15 @emit_out ; emit pop
  2134. LOAD32 R1 R1 0 ; I = I->NEXT
  2135. JUMP @process_break_iter ; Loop
  2136. :process_break_done
  2137. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2138. LOADUI R0 $process_break_string2 ; Our jump string
  2139. CALLI R15 @emit_out ; emit it
  2140. LOADR32 R0 @break_target_head ; Our HEAD string
  2141. CALLI R15 @emit_out ; emit it
  2142. LOADR32 R0 @break_target_func ; Our FUNC string
  2143. CALLI R15 @emit_out ; emit it
  2144. LOADUI R0 $underline ; Using "_"
  2145. CALLI R15 @emit_out ; emit it
  2146. LOADR32 R0 @break_target_num ; Our NUM string
  2147. CALLI R15 @emit_out ; emit it
  2148. LOADUI R0 $newline ; Using "\n"
  2149. CALLI R15 @emit_out ; emit it
  2150. LOADUI R0 $process_break_string3 ; Our final error string
  2151. LOADUI R1 $semicolon ; Using ";"
  2152. CALLI R15 @require_match ; Make sure we get that match
  2153. POPR R2 R15 ; Restore R2
  2154. POPR R1 R15 ; Restore R1
  2155. POPR R0 R15 ; Restore R0
  2156. RET R15
  2157. :process_break_string0
  2158. "Not inside of a loop or case statement
  2159. "
  2160. :process_break_string1
  2161. "POP_RBX # break_cleanup_locals
  2162. "
  2163. :process_break_string2
  2164. "JUMP %"
  2165. :process_break_string3
  2166. "ERROR in break statement
  2167. Missing ;
  2168. "
  2169. :break_frame
  2170. NOP
  2171. :break_target_head
  2172. NOP
  2173. :break_target_func
  2174. NOP
  2175. :break_target_num
  2176. NOP
  2177. ;; process_asm function
  2178. ;; Receives struct token_list* global_token in R13,
  2179. ;; struct token_list* out in R12,
  2180. ;; struct token_list* string_list in R11
  2181. ;; struct token_list* global_list in R10
  2182. ;; and struct token_list* FUNC in R9
  2183. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2184. ;; Returns the token_lists modified
  2185. :process_asm
  2186. PUSHR R0 R15 ; Protect R0
  2187. PUSHR R1 R15 ; Protect R1
  2188. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2189. ;; First required match
  2190. LOADUI R0 $process_asm_string0 ; Using our First error message
  2191. LOADUI R1 $open_paren ; Using "("
  2192. CALLI R15 @require_match ; Make sure of our required match
  2193. :process_asm_iter
  2194. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2195. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2196. CMPSKIPI.E R0 34 ; IF GLOBAL_TOKEN->S[0] == '"'
  2197. JUMP @process_asm_done ; Otherwise be done
  2198. ;; Add block of assembly
  2199. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2200. ADDUI R0 R0 1 ; GLOBAL_TOKEN->S + 1
  2201. CALLI R15 @emit_out ; emit it
  2202. LOADUI R0 $newline ; Using "\n"
  2203. CALLI R15 @emit_out ; emit it
  2204. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2205. JUMP @process_asm_iter
  2206. :process_asm_done
  2207. LOADUI R0 $process_asm_string1 ; Using our First error message
  2208. LOADUI R1 $close_paren ; Using ")"
  2209. CALLI R15 @require_match ; Make sure of our required match
  2210. LOADUI R0 $process_asm_string2 ; Using our First error message
  2211. LOADUI R1 $semicolon ; Using ";"
  2212. CALLI R15 @require_match ; Make sure of our required match
  2213. POPR R1 R15 ; Restore R1
  2214. POPR R0 R15 ; Restore R0
  2215. RET R15
  2216. :process_asm_string0
  2217. "ERROR in process_asm
  2218. MISSING (
  2219. "
  2220. :process_asm_string1
  2221. "ERROR in process_asm
  2222. MISSING )
  2223. "
  2224. :process_asm_string2
  2225. "ERROR in process_asm
  2226. MISSING ;
  2227. "
  2228. ;; recursive_statement function
  2229. ;; Receives struct token_list* global_token in R13,
  2230. ;; struct token_list* out in R12,
  2231. ;; struct token_list* string_list in R11
  2232. ;; struct token_list* global_list in R10
  2233. ;; and struct token_list* FUNC in R9
  2234. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2235. ;; Returns the token_lists modified
  2236. :recursive_statement
  2237. PUSHR R0 R15 ; Protect R0
  2238. PUSHR R1 R15 ; Protect R1
  2239. PUSHR R2 R15 ; Protect R2
  2240. PUSHR R3 R15 ; Protect R3
  2241. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2242. LOAD32 R3 R9 4 ; FRAME = FUNCTION->LOCALS
  2243. :recursive_statement_iter
  2244. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2245. LOADUI R0 $close_curly_brace ; '}'
  2246. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "}"
  2247. JUMP.NZ R0 @recursive_statement_cleanup
  2248. ;; Lets collect those statements
  2249. CALLI R15 @statement ; Collect next statement
  2250. JUMP @recursive_statement_iter ; Iterate
  2251. :recursive_statement_cleanup
  2252. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2253. LOAD32 R1 R12 8 ; OUT->S
  2254. LOADUI R0 $recursive_statement_string0 ; "RETURN\n"
  2255. CALLI R15 @match ; IF OUT->S == "RETURN\n"
  2256. JUMP.NZ R0 @recursive_statement_done ; Save some work
  2257. ;; Lets pop them all off
  2258. LOAD32 R2 R9 4 ; FUNC->LOCALS
  2259. LOADUI R0 $recursive_statement_string1 ; Our POP string
  2260. :recursive_statement_pop
  2261. CMPJUMPI.E R2 R3 @recursive_statement_done
  2262. CALLI R15 @emit_out ; emit it
  2263. LOAD32 R2 R2 0 ; I = I->NEXT
  2264. JUMP.NZ R2 @recursive_statement_pop ; Keep looping
  2265. :recursive_statement_done
  2266. STORE32 R3 R9 4 ; FUNC->LOCALS = FRAME
  2267. POPR R3 R15 ; Restore R3
  2268. POPR R2 R15 ; Restore R2
  2269. POPR R1 R15 ; Restore R1
  2270. POPR R0 R15 ; Restore R0
  2271. RET R15
  2272. :recursive_statement_string0
  2273. "RETURN
  2274. "
  2275. :recursive_statement_string1
  2276. "POP_RBX # _recursive_statement_locals
  2277. "
  2278. ;; statement function
  2279. ;; Receives struct token_list* global_token in R13,
  2280. ;; struct token_list* out in R12,
  2281. ;; struct token_list* string_list in R11
  2282. ;; struct token_list* global_list in R10
  2283. ;; and struct token_list* FUNC in R9
  2284. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2285. ;; Returns the token_lists modified
  2286. :statement
  2287. PUSHR R0 R15 ; Protect R0
  2288. PUSHR R1 R15 ; Protect R1
  2289. PUSHR R2 R15 ; Protect R2
  2290. LOAD32 R2 R13 8 ; GLOBAL_TOKEN->S
  2291. LOADU8 R0 R2 0 ; GLOBAL_TOKEN->S[0]
  2292. CMPSKIPI.E R0 123 ; If GLOBAL_TOKEN->S[0] != '{'
  2293. JUMP @statement_label ; Try next match
  2294. ;; Deal with { statements }
  2295. CALLI R15 @recursive_statement
  2296. JUMP @statement_done ; All done
  2297. :statement_label
  2298. CMPSKIPI.E R0 58 ; If GLOBAL_TOKEN->S[0] != ':'
  2299. JUMP @statement_collect_local ; Try next match
  2300. ;; Deal with :label
  2301. LOAD32 R0 R13 8 ; Using GLOBAL_TOKEN->S
  2302. CALLI R15 @emit_out ; emit it
  2303. LOADUI R0 $statement_string0 ; Using label string
  2304. CALLI R15 @emit_out ; emit it
  2305. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2306. JUMP @statement_done ; Move on to next thing
  2307. :statement_collect_local
  2308. LOADUI R0 $struct ; Using "struct"
  2309. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2310. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "struct"
  2311. JUMP.NZ R0 @statement_collect_local_0
  2312. ;; Otherwise check if it is a primitive
  2313. LOADUI R0 $prim_types ; Using the Primitive types list
  2314. SWAP R0 R1 ; Put in correct order
  2315. CALLI R15 @lookup_type ; Check if a primitive type
  2316. JUMP.Z R0 @statement_process_if ; If not try the next one
  2317. :statement_collect_local_0
  2318. CALLI R15 @collect_local ; Collect the local
  2319. JUMP @statement_done ; And move on
  2320. :statement_process_if
  2321. LOADUI R0 $if_string ; Using "if"
  2322. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2323. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "if"
  2324. JUMP.Z R0 @statement_process_do
  2325. CALLI R15 @process_if ; Collect that if statement
  2326. JUMP @statement_done ; Move on to next thing
  2327. :statement_process_do
  2328. LOADUI R0 $do_string ; Using "do"
  2329. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2330. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "do"
  2331. JUMP.Z R0 @statement_process_while
  2332. CALLI R15 @process_do ; Collect that do statement
  2333. JUMP @statement_done ; Move on to next thing
  2334. :statement_process_while
  2335. LOADUI R0 $while_string ; Using "while"
  2336. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2337. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "while"
  2338. JUMP.Z R0 @statement_process_for
  2339. CALLI R15 @process_while ; Collect that while statement
  2340. JUMP @statement_done ; Move on to next thing
  2341. :statement_process_for
  2342. LOADUI R0 $for_string ; Using "for"
  2343. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2344. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "for"
  2345. JUMP.Z R0 @statement_process_asm
  2346. CALLI R15 @process_for ; Collect that FOR statement
  2347. JUMP @statement_done ; Move on to next thing
  2348. :statement_process_asm
  2349. LOADUI R0 $asm_string ; Using "asm"
  2350. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2351. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "asm"
  2352. JUMP.Z R0 @statement_goto
  2353. CALLI R15 @process_asm ; Collect that ASM statement
  2354. JUMP @statement_done ; Move on to next thing
  2355. :statement_goto
  2356. LOADUI R0 $goto_string ; Using "goto"
  2357. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2358. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "goto"
  2359. JUMP.Z R0 @statement_return_result
  2360. ;; Deal with goto label:
  2361. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2362. LOADUI R0 $statement_string1 ; Using our JUMP string
  2363. CALLI R15 @emit_out ; emit it
  2364. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2365. CALLI R15 @emit_out ; emit it
  2366. LOADUI R0 $newline ; "\n"
  2367. CALLI R15 @emit_out ; emit it
  2368. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2369. LOADUI R0 $statement_string2 ; Using our error message
  2370. LOADUI R1 $semicolon ; Using ";"
  2371. CALLI R15 @require_match ; Make sure of our required match
  2372. JUMP @statement_done ; Move on
  2373. :statement_return_result
  2374. LOADUI R0 $return_string ; Using "return"
  2375. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2376. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "return"
  2377. JUMP.Z R0 @statement_break
  2378. ;; Deal with return statements in functions
  2379. CALLI R15 @return_result ; Do all of the work
  2380. JUMP @statement_done ; Move on to next
  2381. :statement_break
  2382. LOADUI R0 $break_string ; Using "break"
  2383. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2384. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "break"
  2385. JUMP.Z R0 @statement_continue
  2386. ;; Let break function deal with updating out
  2387. CALLI R15 @process_break ; Do all the work
  2388. JUMP @statement_done ; Move on to next
  2389. :statement_continue
  2390. LOADUI R0 $continue_string ; Using "continue"
  2391. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2392. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "continue"
  2393. JUMP.Z R0 @statement_expression
  2394. ;; Simple Continue compatibility
  2395. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2396. LOADUI R0 $statement_string3 ; Using our continue comment string
  2397. CALLI R15 @emit_out ; emit it
  2398. LOADUI R0 $statement_string2 ; Using our error message
  2399. LOADUI R1 $semicolon ; Using ";"
  2400. CALLI R15 @require_match ; Make sure of our required match
  2401. JUMP @statement_done ; Move on
  2402. :statement_expression
  2403. CALLI R15 @expression ; Do expression evaluation
  2404. LOADUI R0 $statement_string2 ; Load our error message
  2405. LOADUI R1 $semicolon ; use ";"
  2406. CALLI R15 @require_match ; Make sure GLOBAL_TOKEN-> == ";"
  2407. :statement_done
  2408. POPR R2 R15 ; Restore R2
  2409. POPR R1 R15 ; Restore R1
  2410. POPR R0 R15 ; Restore R0
  2411. RET R15
  2412. :statement_string0
  2413. " #C goto label
  2414. "
  2415. :statement_string1
  2416. "JUMP %"
  2417. :statement_string2
  2418. "ERROR in statement
  2419. MISSING ;
  2420. "
  2421. :statement_string3
  2422. "
  2423. #continue statement
  2424. "
  2425. ;; collect_local function
  2426. ;; Receives struct token_list* global_token in R13,
  2427. ;; struct token_list* out in R12,
  2428. ;; struct token_list* string_list in R11
  2429. ;; struct token_list* global_list in R10
  2430. ;; and struct token_list* FUNC in R9
  2431. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2432. ;; Returns the token_lists modified
  2433. :collect_local
  2434. PUSHR R0 R15 ; Protect R0
  2435. PUSHR R1 R15 ; Protect R1
  2436. PUSHR R2 R15 ; Protect R2
  2437. CALLI R15 @type_name ; Get it's type
  2438. MOVE R1 R0 ; Prepare for call
  2439. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2440. LOAD32 R2 R9 4 ; FUNC->LOCALS
  2441. CALLI R15 @sym_declare ; SET A
  2442. MOVE R2 R0 ; Protect A
  2443. ;; Figure out depth
  2444. LOADUI R0 $main_string ; Using "main"
  2445. LOAD32 R1 R9 8 ; FUNC->S
  2446. CALLI R15 @match ; IF FUNC->S == "main"
  2447. JUMP.Z R0 @collect_local_0 ; Try next
  2448. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2449. JUMP.NZ R0 @collect_local_0 ; Try next
  2450. LOADI R0 -40 ; The default depth for main
  2451. STORE32 R0 R2 16 ; A->DEPTH = -40
  2452. JUMP @collect_local_output ; Deal with header
  2453. :collect_local_0
  2454. LOAD32 R0 R9 16 ; FUNC->ARGS
  2455. JUMP.NZ R0 @collect_local_1 ; Try Next
  2456. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2457. JUMP.NZ R0 @collect_local_1 ; Try Next
  2458. LOADI R0 -16 ; The default depth for foo()
  2459. STORE32 R0 R2 16 ; A->DEPTH = -16
  2460. JUMP @collect_local_output ; Deal with header
  2461. :collect_local_1
  2462. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2463. JUMP.NZ R0 @collect_local_2 ; Try Next
  2464. LOAD32 R0 R9 16 ; FUNC->ARGS
  2465. LOAD32 R0 R0 16 ; FUNC->ARGS->DEPTH
  2466. SUBI R0 R0 16 ; DEPTH = FUNC->ARGS->DEPTH - 16
  2467. STORE32 R0 R2 16 ; A->DEPTH = DEPTH
  2468. JUMP @collect_local_output ; Deal with header
  2469. :collect_local_2
  2470. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2471. LOAD32 R0 R0 16 ; FUNC->LOCALS->DEPTH
  2472. SUBI R0 R0 8 ; DEPTH = FUNC->LOCALS->DEPTH - 8
  2473. STORE32 R0 R2 16 ; A->DEPTH = DEPTH
  2474. :collect_local_output
  2475. STORE32 R2 R9 4 ; FUNC->LOCALS = A
  2476. ;; Output header
  2477. LOADUI R0 $collect_local_string0 ; Starting with the comment
  2478. CALLI R15 @emit_out ; emit it
  2479. LOAD32 R0 R2 8 ; A->S
  2480. CALLI R15 @emit_out ; emit it
  2481. LOADUI R0 $newline ; Using "\n"
  2482. CALLI R15 @emit_out ; emit it
  2483. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2484. ;; Deal with possible assignment
  2485. LOADUI R0 $equal ; Using "="
  2486. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2487. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  2488. JUMP.Z R0 @collect_local_nonassign
  2489. ;; Deal with assignment of the local
  2490. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2491. CALLI R15 @expression ; Update OUT with the evaluation of the Expression
  2492. :collect_local_nonassign
  2493. LOADUI R0 $collect_local_string1 ; Our error message
  2494. LOADUI R1 $semicolon ; Using ";"
  2495. CALLI R15 @require_match ; Make sure GLOBAL_TOKEN->S == ";"
  2496. ;; Final Footer
  2497. LOADUI R0 $collect_local_string2 ; Add our PUSH statement
  2498. CALLI R15 @emit_out ; emit it
  2499. LOAD32 R0 R2 8 ; A->S
  2500. CALLI R15 @emit_out ; emit it
  2501. LOADUI R0 $newline ; Using "\n"
  2502. CALLI R15 @emit_out ; emit it
  2503. POPR R2 R15 ; Restore R2
  2504. POPR R1 R15 ; Restore R1
  2505. POPR R0 R15 ; Restore R0
  2506. RET R15
  2507. :collect_local_string0
  2508. "# Defining local "
  2509. :collect_local_string1
  2510. "ERROR in collect_local
  2511. Missing ;
  2512. "
  2513. :collect_local_string2
  2514. "PUSH_RAX #"
  2515. ;; collect_arguments function
  2516. ;; Receives struct token_list* global_token in R13,
  2517. ;; struct token_list* out in R12,
  2518. ;; struct token_list* string_list in R11
  2519. ;; struct token_list* global_list in R10
  2520. ;; and struct token_list* FUNC in R9
  2521. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2522. ;; Returns the token_lists modified
  2523. :collect_arguments
  2524. PUSHR R0 R15 ; Protect R0
  2525. PUSHR R1 R15 ; Protect R1
  2526. PUSHR R2 R15 ; Protect R2
  2527. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2528. :collect_arguments_iter
  2529. LOADUI R0 $close_paren ; Using ")"
  2530. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2531. CALLI R15 @match ; IF GLOBAL_TOKEN->S == ")"
  2532. JUMP.NZ R0 @collect_arguments_done ; Be done
  2533. ;; Collect the arguments
  2534. CALLI R15 @type_name ; Get what type we are working with
  2535. MOVE R1 R0 ; Put TYPE where it will be used
  2536. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2537. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2538. CMPSKIPI.NE R0 41 ; IF GLOBAL_TOKEN->S[0] == ')'
  2539. JUMP @collect_arguments_iter3 ; foo(int,char,void) doesn't need anything done
  2540. ;; Check for foo(int a,...)
  2541. CMPSKIPI.NE R0 41 ; IF GLOBAL_TOKEN->S[0] == ','
  2542. JUMP @collect_arguments_iter3 ; Looks like final case
  2543. ;; Deal with foo(int a, ...)
  2544. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2545. LOAD32 R2 R9 16 ; FUNC->ARGUMENTS
  2546. CALLI R15 @sym_declare ; Get A
  2547. MOVE R2 R0 ; Get A out of the way
  2548. :collect_arguments_func
  2549. LOAD32 R0 R9 16 ; FUNC->ARGS
  2550. CMPSKIPI.E R0 0 ; IF NULL == FUNC->ARGS
  2551. LOAD32 R0 R0 16 ; FUNC->ARGS->DEPTH
  2552. SUBI R0 R0 8 ; FUNC->ARGS->DEPTH - 8 or NULL - 8 (-8)
  2553. STORE32 R0 R2 16 ; A->DEPTH = VALUE
  2554. :collect_arguments_iter2
  2555. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2556. STORE32 R2 R9 16 ; FUNC->ARGUMENTS = A
  2557. :collect_arguments_iter3
  2558. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2559. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2560. CMPSKIPI.NE R0 44 ; IF GLOBAL_TOKEN->S[0] == ','
  2561. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2562. JUMP @collect_arguments_iter ; Keep looping
  2563. :collect_arguments_done
  2564. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2565. POPR R2 R15 ; Restore R2
  2566. POPR R1 R15 ; Restore R1
  2567. POPR R0 R15 ; Restore R0
  2568. RET R15
  2569. ;; declare_function function
  2570. ;; Receives struct token_list* global_token in R13,
  2571. ;; struct token_list* out in R12,
  2572. ;; struct token_list* string_list in R11
  2573. ;; and struct token_list* global_list in R10
  2574. ;; SETS R9 to struct token_list* FUNC
  2575. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2576. ;; Returns the token_lists modified
  2577. :declare_function
  2578. PUSHR R0 R15 ; Protect R0
  2579. PUSHR R1 R15 ; Protect R1
  2580. PUSHR R2 R15 ; Protect R2
  2581. FALSE R0 ; Using Zero
  2582. STORER32 R0 @current_count ; CURRENT_COUNT = 0
  2583. LOAD32 R0 R13 4 ; GLOBAL_TOKEN->PREV
  2584. LOAD32 R0 R0 8 ; GLOBAL_TOKEN->PREV->S
  2585. FALSE R1 ; Passing NULL
  2586. LOADR32 R2 @global_function_list ; where the global function list is located
  2587. CALLI R15 @sym_declare ; declare FUNC
  2588. STORER32 R0 @global_function_list ; GLOBAL_FUNCTION_LIST = FUNC
  2589. MOVE R9 R0 ; SETS FUNC
  2590. CALLI R15 @collect_arguments ; Collect function arguments
  2591. LOAD32 R2 R13 8 ; GLOBAL_TOKEN->S
  2592. LOADU8 R0 R2 0 ; GLOBAL_TOKEN->S[0]
  2593. CMPSKIPI.NE R0 59 ; IF GLOBAL_TOKEN->S[0] == ';'
  2594. JUMP @declare_function_prototype ; Don't waste time
  2595. ;; Looks like it is an actual function definition
  2596. LOADUI R0 $declare_function_string0 ; Using first string
  2597. CALLI R15 @emit_out ; emit it
  2598. LOAD32 R0 R9 8 ; Using FUNC->S
  2599. CALLI R15 @emit_out ; emit it
  2600. LOADUI R0 $newline ; Using "\n"
  2601. CALLI R15 @emit_out ; emit it
  2602. LOADUI R0 $declare_function_string1 ; Using second string
  2603. CALLI R15 @emit_out ; emit it
  2604. LOAD32 R0 R9 8 ; Using FUNC->S
  2605. CALLI R15 @emit_out ; emit it
  2606. LOADUI R0 $newline ; Using "\n"
  2607. CALLI R15 @emit_out ; emit it
  2608. :declare_function_nonmain
  2609. FALSE R1 ; Cleaning up before call
  2610. CALLI R15 @statement ; Collect the statement
  2611. ;; Prevent Duplicate Returns
  2612. LOAD32 R1 R12 8 ; OUT->S
  2613. LOADUI R0 $declare_function_string2 ; Our final string
  2614. CALLI R15 @match ; Check for Match
  2615. JUMP.NZ R0 @declare_function_done ; Clean up
  2616. ;; Deal with adding the return
  2617. LOADUI R0 $declare_function_string2 ; Our final string
  2618. CALLI R15 @emit_out ; emit it
  2619. :declare_function_done
  2620. POPR R2 R15 ; Restore R2
  2621. POPR R1 R15 ; Restore R1
  2622. POPR R0 R15 ; Restore R0
  2623. RET R15
  2624. :declare_function_prototype
  2625. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2626. JUMP @declare_function_done ; Clean up
  2627. :declare_function_string0
  2628. "# Defining function "
  2629. :declare_function_string1
  2630. ":FUNCTION_"
  2631. :declare_function_string2
  2632. "RETURN
  2633. "
  2634. :current_count
  2635. NOP
  2636. ;; program function
  2637. ;; Receives struct token_list* global_token in R13,
  2638. ;; struct token_list* out in R12,
  2639. ;; struct token_list* string_list in R11
  2640. ;; and struct token_list* global_list in R10
  2641. ;; and struct token_list* FUNC in R9
  2642. ;; and struct token_list* current_target in R8
  2643. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2644. ;; Returns the token_lists modified
  2645. :program
  2646. PUSHR R0 R15 ; Protect R0
  2647. PUSHR R1 R15 ; Protect R1
  2648. PUSHR R2 R15 ; Protect R2
  2649. PUSHR R3 R15 ; Protect R3
  2650. :program_iter
  2651. JUMP.Z R13 @program_done ; Looks like we read all the tokens
  2652. LOADUI R0 $constant ; Using the constant string
  2653. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2654. CALLI R15 @match ; Check if they match
  2655. JUMP.Z R0 @program_type ; Looks like not
  2656. ;; Deal with CONSTANT case
  2657. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2658. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2659. FALSE R1 ; Set NULL
  2660. LOADR32 R2 @global_constant_list ; GLOBAL_CONSTANTS_LIST
  2661. CALLI R15 @sym_declare ; Declare the global constant
  2662. STORER32 R0 @global_constant_list ; Update global constant
  2663. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2664. STORE32 R13 R0 16 ; GLOBAL_CONSTANT_LIST->ARGUMENTS = GLOBAL_TOKEN
  2665. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2666. JUMP @program_iter ; Loop again
  2667. :program_type
  2668. CALLI R15 @type_name ; Get the type
  2669. JUMP.Z R0 @program_iter ; If newly defined type iterate
  2670. ;; Looks like we got a defined type
  2671. MOVE R1 R0 ; Put the type where it can be used
  2672. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2673. LOADUI R3 $global_symbol_list ; Get address of global symbol list
  2674. LOAD32 R2 R3 0 ; GLOBAL_SYMBOLS_LIST
  2675. CALLI R15 @sym_declare ; Declare that global symbol
  2676. STORE32 R0 R3 0 ; Update global symbol list
  2677. LOAD32 R3 R13 8 ; GLOBAL_TOKEN->S
  2678. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2679. LOADUI R0 $semicolon ; Get semicolon string
  2680. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2681. CALLI R15 @match ; Check if they match
  2682. JUMP.Z R0 @program_function ; If not a match
  2683. ;; Deal with case of TYPE NAME;
  2684. COPY R1 R10 ; Using GLOBALS_LIST
  2685. LOADUI R0 $program_string0 ; Using the GLOBAL_ prefix
  2686. CALLI R15 @emit ; emit it
  2687. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2688. MOVE R0 R3 ; Use GLOBAL_TOKEN->PREV->S
  2689. CALLI R15 @emit ; emit it
  2690. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2691. LOADUI R0 $program_string1 ; Using the NOP postfix
  2692. CALLI R15 @emit ; emit it
  2693. MOVE R10 R0 ; Move new GLOBALS_LIST into Place
  2694. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2695. JUMP @program_iter
  2696. :program_function
  2697. LOADUI R0 $open_paren ; Get open paren string
  2698. CALLI R15 @match ; Check if they match
  2699. JUMP.Z R0 @program_assign ; If not a match
  2700. ;; Deal with case of TYPE NAME(...)
  2701. CALLI R15 @declare_function
  2702. JUMP @program_iter
  2703. :program_assign
  2704. LOADUI R0 $equal ; Get equal string
  2705. CALLI R15 @match ; Check if they match
  2706. JUMP.Z R0 @program_error ; If not a match
  2707. COPY R1 R10 ; Using GLOBALS_LIST
  2708. LOADUI R0 $program_string0 ; Using the GLOBAL_ prefix
  2709. CALLI R15 @emit ; emit it
  2710. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2711. MOVE R0 R3 ; Use GLOBAL_TOKEN->PREV->S
  2712. CALLI R15 @emit ; emit it
  2713. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2714. LOADUI R0 $newline ; Using the Newline postfix
  2715. CALLI R15 @emit ; emit it
  2716. MOVE R10 R0 ; Update GLOBALS_LIST
  2717. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2718. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2719. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2720. LOADUI R1 $digit_chars ; 0-9
  2721. CALLI R15 @in_set ; Figure out if in set
  2722. JUMP.Z R0 @program_assign_string ; If not in sets
  2723. ;; Looks like we have an int
  2724. COPY R1 R10 ; Using GLOBALS_LIST
  2725. LOADUI R0 $percent ; Using percent prefix
  2726. CALLI R15 @emit ; emit it
  2727. MOVE R1 R0 ; Put GLOBALS_LIST into Place
  2728. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2729. CALLI R15 @emit ; emit it
  2730. MOVE R1 R0 ; Put GLOBALS_LIST into Place
  2731. LOADUI R0 $newline ; Using newline postfix
  2732. CALLI R15 @emit ; emit it
  2733. MOVE R10 R0 ; Update GLOBALS_LIST
  2734. JUMP @program_assign_done ; Move on
  2735. :program_assign_string
  2736. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2737. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2738. CMPSKIPI.E R0 34 ; If GLOBAL_TOKEN->S[0] == '"'
  2739. JUMP @program_error ; If not we hit an error
  2740. ;; Looks like we have a string
  2741. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2742. CALLI R15 @parse_string ; Parse it into useful form
  2743. COPY R1 R10 ; GLOBALS_LIST
  2744. CALLI R15 @emit ; emit it
  2745. MOVE R10 R0 ; Update GLOBALS_LIST
  2746. :program_assign_done
  2747. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2748. LOADUI R0 $program_string4 ; Potential error message
  2749. LOADUI R1 $semicolon ; Checking for ;
  2750. CALLI R15 @require_match ; Catch those errors
  2751. JUMP @program_iter
  2752. :program_error
  2753. LOADUI R0 $program_string2 ; message part 1
  2754. FALSE R1 ; Show to user
  2755. CALLI R15 @file_print ; write
  2756. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2757. CALLI R15 @file_print ; write
  2758. LOADUI R0 $program_string3 ; message part 2
  2759. CALLI R15 @file_print ; write
  2760. CALLI R15 @line_error ; Provide a meaningful error message
  2761. HALT
  2762. :program_done
  2763. POPR R3 R15 ; Restore R3
  2764. POPR R2 R15 ; Restore R2
  2765. POPR R1 R15 ; Restore R1
  2766. POPR R0 R15 ; Restore R0
  2767. RET R15
  2768. :program_string0
  2769. ":GLOBAL_"
  2770. :program_string1
  2771. "
  2772. NULL
  2773. "
  2774. :program_string2
  2775. "Received "
  2776. :program_string3
  2777. " in program
  2778. "
  2779. :program_string4
  2780. "ERROR in Program
  2781. Missing ;
  2782. "
  2783. ;; sym_declare function
  2784. ;; Receives char* in R0, struct type* in R1, struct token_list* in R2
  2785. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2786. ;; Returns struct token_list* in R0
  2787. :sym_declare
  2788. STORE32 R2 R14 0 ; A->NEXT = LIST
  2789. STORE32 R0 R14 8 ; A->S = S
  2790. STORE32 R1 R14 12 ; A->TYPE = T
  2791. ADDUI R0 R14 20 ; CALLOC struct token_list
  2792. SWAP R0 R14 ; Prepare for Return
  2793. RET R15
  2794. ;; sym_lookup function
  2795. ;; Receives char* in R0 and struct token_list in R1
  2796. ;; Returns struct token_list* or NULL in R0
  2797. :sym_lookup
  2798. PUSHR R2 R15 ; Protect R2
  2799. MOVE R2 R1 ; Protect I
  2800. MOVE R1 R0 ; Put S in proper place
  2801. :sym_lookup_iter
  2802. JUMP.Z R2 @sym_lookup_done ; Stop if NULL
  2803. LOAD32 R0 R2 8 ; I->S
  2804. CALLI R15 @match ; if I->S == S
  2805. JUMP.NZ R0 @sym_lookup_done ; Stop if match
  2806. LOAD32 R2 R2 0 ; I = I->NEXT
  2807. JUMP @sym_lookup_iter ; Keep looping
  2808. :sym_lookup_done
  2809. MOVE R0 R2 ; Using R2 as our result
  2810. POPR R2 R15 ; Restore R2
  2811. RET R15
  2812. ;; function_call function
  2813. ;; Receives CHAR* S in R0 and INT BOOL in R1
  2814. ;; struct token_list* out in R12,
  2815. ;; struct token_list* string_list in R11
  2816. ;; and struct token_list* global_list in R10
  2817. ;; and struct token_list* FUNC in R9
  2818. ;; and struct token_list* current_target in R8
  2819. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2820. ;; Returns the token_lists modified
  2821. :function_call
  2822. PUSHR R0 R15 ; Protect R0
  2823. PUSHR R1 R15 ; Protect R1
  2824. PUSHR R2 R15 ; Protect R2
  2825. PUSHR R3 R15 ; Protect R3
  2826. PUSHR R4 R15 ; Protect R4
  2827. MOVE R2 R0 ; Protect S
  2828. MOVE R3 R1 ; Protect BOOL
  2829. LOADUI R0 $function_call_string0 ; Our first error message
  2830. LOADUI R1 $open_paren ; Using "("
  2831. CALLI R15 @require_match ; Make sure of a match
  2832. FALSE R4 ; PASSED = 0
  2833. LOADUI R0 $function_call_string1 ; Our first header
  2834. CALLI R15 @emit_out ; emit it
  2835. LOADUI R0 $function_call_string2 ; Our second header
  2836. CALLI R15 @emit_out ; emit it
  2837. LOADUI R0 $function_call_string3 ; Our third header
  2838. CALLI R15 @emit_out ; emit it
  2839. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2840. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2841. CMPSKIPI.NE R0 41 ; IF GLOBAL_TOKEN->S[0] != ')'
  2842. JUMP @function_call_collect_done ; looks like function()
  2843. ;; Collect arguments
  2844. CALLI R15 @expression ; Deal with first argument
  2845. LOADUI R0 $function_call_string4 ; Push it onto stack
  2846. CALLI R15 @emit_out ; emit it
  2847. ADDUI R4 R4 1 ; PASSED = 1
  2848. :function_call_collect_iter
  2849. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2850. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2851. CMPSKIPI.E R0 44 ; IF GLOBAL_TOKEN->S[0] != ','
  2852. JUMP @function_call_collect_done ; looks like we are done collecting arguments
  2853. ;; Collect another argument
  2854. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2855. CALLI R15 @expression ; Deal with Nth argument
  2856. LOADUI R0 $function_call_string5 ; Push it onto stack
  2857. CALLI R15 @emit_out ; emit it
  2858. ADDUI R4 R4 1 ; PASSED = PASSED + 1
  2859. JUMP @function_call_collect_iter ; Keep looping
  2860. :function_call_collect_done
  2861. LOADUI R0 $function_call_string6 ; Our second error message
  2862. LOADUI R1 $close_paren ; Using ")"
  2863. CALLI R15 @require_match ; Make sure of a match
  2864. JUMP.Z R3 @function_call_call_false ; if BOOL != TRUE
  2865. ;; Deal with TRUE == BOOL
  2866. LOADUI R0 $function_call_string7 ; Our first prefix
  2867. CALLI R15 @emit_out ; emit it
  2868. MOVE R0 R2 ; Using S
  2869. CALLI R15 @emit_out ; emit it
  2870. LOADUI R0 $function_call_string8 ; Our first postfix
  2871. CALLI R15 @emit_out ; emit it
  2872. LOADUI R0 $function_call_string9 ; Our second postfix
  2873. CALLI R15 @emit_out ; emit it
  2874. LOADUI R0 $function_call_string10 ; Our last postfix
  2875. CALLI R15 @emit_out ; emit it
  2876. JUMP @function_call_call_done ; Move on
  2877. :function_call_call_false
  2878. ;; Deal with FALSE == BOOL
  2879. LOADUI R0 $function_call_string11 ; Our first prefix
  2880. CALLI R15 @emit_out ; emit it
  2881. LOADUI R0 $function_call_string12 ; Our last prefix
  2882. CALLI R15 @emit_out ; emit it
  2883. MOVE R0 R2 ; Using S
  2884. CALLI R15 @emit_out ; emit it
  2885. LOADUI R0 $newline ; Using "\n"
  2886. CALLI R15 @emit_out ; emit it
  2887. :function_call_call_done
  2888. LOADUI R0 $function_call_string13 ; Our POP
  2889. :function_call_pop_iter
  2890. JUMP.Z R4 @function_call_pop_done ; Skip POP if out of args on Stack
  2891. CALLI R15 @emit_out ; emit our POP
  2892. SUBI R4 R4 1 ; PASSED = PASSED - 1
  2893. JUMP @function_call_pop_iter ; Loop
  2894. :function_call_pop_done
  2895. LOADUI R0 $function_call_string14 ; Our first postfix
  2896. CALLI R15 @emit_out ; emit it
  2897. LOADUI R0 $function_call_string15 ; Our final postfix
  2898. CALLI R15 @emit_out ; emit it
  2899. POPR R4 R15 ; Restore R4
  2900. POPR R3 R15 ; Restore R3
  2901. POPR R2 R15 ; Restore R2
  2902. POPR R1 R15 ; Restore R1
  2903. POPR R0 R15 ; Restore R0
  2904. RET R15
  2905. :function_call_string0
  2906. "ERROR in process_expression_list
  2907. No ( was found
  2908. "
  2909. :function_call_string1
  2910. "PUSH_RDI # Prevent overwriting in recursion
  2911. "
  2912. :function_call_string2
  2913. "PUSH_RBP # Protect the old base pointer
  2914. "
  2915. :function_call_string3
  2916. "COPY_RSP_to_RDI # Copy new base pointer
  2917. "
  2918. :function_call_string4
  2919. "PUSH_RAX #_process_expression1
  2920. "
  2921. :function_call_string5
  2922. "PUSH_RAX #_process_expression2
  2923. "
  2924. :function_call_string6
  2925. "ERROR in process_expression_list
  2926. No ) was found
  2927. "
  2928. :function_call_string7
  2929. "LOAD_BASE_ADDRESS_rax %"
  2930. :function_call_string8
  2931. "
  2932. LOAD_INTEGER
  2933. "
  2934. :function_call_string9
  2935. "COPY_rdi_to_rbp
  2936. "
  2937. :function_call_string10
  2938. "CALL_rax
  2939. "
  2940. :function_call_string11
  2941. "COPY_rdi_to_rbp
  2942. "
  2943. :function_call_string12
  2944. "CALL_IMMEDIATE %FUNCTION_"
  2945. :function_call_string13
  2946. "POP_RBX # _process_expression_locals
  2947. "
  2948. :function_call_string14
  2949. "POP_RBP # Restore old base pointer
  2950. "
  2951. :function_call_string15
  2952. "POP_RDI # Prevent overwrite
  2953. "
  2954. ;; emit function
  2955. ;; Receives char* in R0, struct token_list* in R1
  2956. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2957. ;; Returns struct token_list* in R0
  2958. :emit
  2959. PUSHR R2 R15 ; Protect R2
  2960. COPY R2 R14 ; Pointer to T
  2961. ADDUI R14 R14 20 ; CALLOC struct token_list
  2962. STORE32 R1 R2 0 ; T->NEXT = HEAD
  2963. STORE32 R0 R2 8 ; T->S = S
  2964. MOVE R0 R2 ; Put T in proper spot for return
  2965. POPR R2 R15 ; Restore R2
  2966. RET R15
  2967. ;; emit_out function
  2968. ;; Receives char* in R0
  2969. ;; struct token_list* out in R12,
  2970. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2971. ;; Returns struct token_list* in R0
  2972. :emit_out
  2973. STORE32 R12 R14 0 ; T->NEXT = OUT
  2974. ADDUI R12 R14 20 ; Get T
  2975. SWAP R12 R14 ; CALLOC struct token_list
  2976. STORE32 R0 R12 8 ; T->S = S
  2977. RET R15
  2978. ;; uniqueID function
  2979. ;; Receives char* in R0, struct token_list* in R1 and char* in R2
  2980. ;; Calls emit repeatedly
  2981. ;; Returns struct token_list* in R0
  2982. :uniqueID
  2983. CALLI R15 @emit ; emit S
  2984. MOVE R1 R0 ; Put L in the correct place
  2985. LOADUI R0 $underline ; Using "_"
  2986. CALLI R15 @emit ; emit it
  2987. MOVE R1 R0 ; Put L in the correct place
  2988. COPY R0 R2 ; Put NUM in the correct place
  2989. CALLI R15 @emit ; emit NUM
  2990. MOVE R1 R0 ; Put L in the correct place
  2991. LOADUI R0 $newline ; Using "\n"
  2992. CALLI R15 @emit ; emit it
  2993. RET R15
  2994. ;; uniqueID_out function
  2995. ;; Receives char* in R0, char* in R1
  2996. ;; Calls emit_out repeatedly
  2997. ;; Returns nothing
  2998. :uniqueID_out
  2999. CALLI R15 @emit_out ; emit S
  3000. LOADUI R0 $underline ; Using "_"
  3001. CALLI R15 @emit_out ; emit it
  3002. COPY R0 R1 ; Put NUM in the correct place
  3003. CALLI R15 @emit_out ; emit NUM
  3004. LOADUI R0 $newline ; Using "\n"
  3005. CALLI R15 @emit_out ; emit it
  3006. RET R15
  3007. ;; file_print function
  3008. ;; Receives pointer to string in R0 and FILE* in R1
  3009. ;; Returns nothing
  3010. :file_print
  3011. PUSHR R2 R15 ; Protect R2 from Overwrite
  3012. MOVE R2 R0 ; Put string pointer into place
  3013. :file_print_read
  3014. LOAD8 R0 R2 0 ; Get a char
  3015. JUMP.Z R0 @file_print_done ; If NULL be done
  3016. FPUTC ; Write the Char
  3017. ADDUI R2 R2 1 ; Point at next CHAR
  3018. JUMP @file_print_read ; Loop again
  3019. :file_print_done
  3020. POPR R2 R15 ; Restore R2
  3021. RET R15
  3022. ;; recursive_output function
  3023. ;; Receives token_list in R0 and FILE* in R1
  3024. ;; Returns nothing and alters nothing
  3025. :recursive_output
  3026. JUMP.Z R0 @recursive_output_abort ; Abort if NULL
  3027. PUSHR R2 R15 ; Preserve R2 from recursion
  3028. MOVE R2 R0 ; Preserve R0 from recursion
  3029. LOAD32 R0 R2 0 ; Using I->NEXT
  3030. CALLI R15 @recursive_output ; Recurse
  3031. LOAD32 R0 R2 8 ; Using I->S
  3032. CALLI R15 @file_print ; Write the string
  3033. MOVE R0 R2 ; Put R0 back
  3034. POPR R2 R15 ; Restore R0
  3035. :recursive_output_abort
  3036. RET R15
  3037. ;; match function
  3038. ;; Receives a CHAR* in R0, CHAR* in R1
  3039. ;; Returns Bool in R0 indicating if strings match
  3040. :match
  3041. PUSHR R1 R15 ; Protect R1
  3042. PUSHR R2 R15 ; Protect R2
  3043. PUSHR R3 R15 ; Protect R3
  3044. PUSHR R4 R15 ; Protect R4
  3045. MOVE R2 R0 ; Put First string in place
  3046. MOVE R3 R1 ; Put Second string in place
  3047. LOADUI R4 0 ; Set initial index of 0
  3048. :match_cmpbyte
  3049. LOADXU8 R0 R2 R4 ; Get a byte of our first string
  3050. LOADXU8 R1 R3 R4 ; Get a byte of our second string
  3051. ADDUI R4 R4 1 ; Prep for next loop
  3052. CMPSKIP.NE R1 R0 ; Compare the bytes
  3053. JUMP.NZ R1 @match_cmpbyte ; Loop if bytes are equal
  3054. ;; Done
  3055. FALSE R2 ; Default answer
  3056. CMPSKIP.NE R0 R1 ; If ended loop with everything matching
  3057. TRUE R2 ; Set as TRUE
  3058. MOVE R0 R2 ; Prepare for return
  3059. POPR R4 R15 ; Restore R4
  3060. POPR R3 R15 ; Restore R3
  3061. POPR R2 R15 ; Restore R2
  3062. POPR R1 R15 ; Restore R1
  3063. RET R15
  3064. ;; lookup_type function
  3065. ;; Receives a CHAR* in R0 and struct type* in R1
  3066. ;; Returns struct type* in R0 or NULL if no match
  3067. :lookup_type
  3068. PUSHR R1 R15 ; Protect R1
  3069. PUSHR R2 R15 ; Protect R2
  3070. MOVE R2 R1 ; Put START in correct place
  3071. MOVE R1 R0 ; Put S in correct place
  3072. :lookup_type_iter
  3073. LOAD32 R0 R2 24 ; Get I->NAME
  3074. CALLI R15 @match ; Check if I->NAME == S
  3075. JUMP.NZ R0 @lookup_type_done ; If match found be done
  3076. LOAD32 R2 R2 0 ; I = I->NEXT
  3077. JUMP.NZ R2 @lookup_type_iter ; Otherwise iterate until I == NULL
  3078. :lookup_type_done
  3079. MOVE R0 R2 ; Our answer (I or NULL)
  3080. POPR R2 R15 ; Restore R2
  3081. POPR R1 R15 ; Restore R1
  3082. RET R15
  3083. ;; lookup_member function
  3084. ;; Receives struct type* parent in R0 and char* name in R1
  3085. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3086. ;; Returns struct type* of member in R0 or aborts with error
  3087. :lookup_member
  3088. PUSHR R1 R15 ; Protect R1
  3089. PUSHR R2 R15 ; Protect R2
  3090. PUSHR R3 R15 ; Protect R3
  3091. LOAD32 R3 R0 24 ; PARENT->NAME for error
  3092. MOVE R2 R0 ; I = PARENT
  3093. :lookup_member_iter
  3094. LOAD32 R2 R2 16 ; I = I->MEMBERS
  3095. JUMP.Z R2 @lookup_member_error ; We failed hard
  3096. LOAD32 R0 R2 24 ; I->NAME
  3097. CALLI R15 @match ; IF I->NAME == NAME
  3098. JUMP.Z R0 @lookup_member_iter ; Loop again
  3099. :lookup_member_done
  3100. MOVE R0 R2 ; Put I in the correct place
  3101. POPR R3 R15 ; Restore R3
  3102. POPR R2 R15 ; Restore R2
  3103. POPR R1 R15 ; Restore R1
  3104. RET R15
  3105. :lookup_member_error
  3106. FALSE R1 ; Write to TTY
  3107. LOADUI R0 $lookup_member_string0 ; Our header string
  3108. CALLI R15 @file_print ; Print it
  3109. MOVE R0 R3 ; Using PARENT->NAME
  3110. CALLI R15 @file_print ; Print it
  3111. LOADUI R0 $arrow_string ; Using "->"
  3112. CALLI R15 @file_print ; Print it
  3113. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3114. CALLI R15 @file_print ; Print it
  3115. LOADUI R0 $lookup_member_string1 ; Our footer string
  3116. CALLI R15 @file_print ; Print it
  3117. CALLI R15 @line_error ; Give line info
  3118. LOADUI R0 $newline ; Our final addition
  3119. CALLI R15 @file_print ; Print it
  3120. HALT
  3121. :lookup_member_string0
  3122. "ERROR in lookup_member "
  3123. :lookup_member_string1
  3124. " does not exist
  3125. "
  3126. ;; build_member function
  3127. ;; Receives a struct type* in R0, int in R1 and int in R2
  3128. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3129. ;; Modifies R2 to current member_size
  3130. ;; Returns struct type* in R0
  3131. :build_member
  3132. PUSHR R3 R15 ; Protect R3
  3133. PUSHR R4 R15 ; Protect R4
  3134. PUSHR R5 R15 ; Protect R5
  3135. PUSHR R6 R15 ; Protect R6
  3136. MOVE R6 R1 ; Protect OFFSET
  3137. MOVE R4 R0 ; Protect LAST
  3138. CALLI R15 @type_name ; Get MEMBER_TYPE
  3139. MOVE R5 R0 ; Protect MEMBER_TYPE
  3140. ADDUI R3 R14 28 ; CALLOC struct type
  3141. SWAP R3 R14 ; SET I
  3142. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3143. STORE32 R0 R3 24 ; I->NAME = GLOBAL_TOKEN->S
  3144. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3145. STORE32 R4 R3 16 ; I->MEMBERS = LAST
  3146. LOADUI R0 $open_bracket ; Using "["
  3147. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  3148. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "["
  3149. JUMP.Z R0 @build_member_single
  3150. ;; Deal with type name [ number ] ;
  3151. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3152. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3153. CALLI R15 @numerate_string ; Convert string to int NUMBER
  3154. LOAD32 R1 R5 20 ; MEMBER_TYPE->TYPE
  3155. LOAD32 R1 R1 4 ; MEMBER_TYPE->TYPE->SIZE
  3156. MULU R0 R0 R1 ; MEMBER_TYPE->TYPE->SIZE * NUMBER
  3157. STORE32 R0 R3 4 ; I->SIZE = MEMBER_TYPE->TYPE->SIZE * NUMBER
  3158. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3159. LOADUI R0 $build_member_string0 ; Our error message
  3160. LOADUI R1 $close_bracket ; Using "]"
  3161. CALLI R15 @require_match ; Make sure it is right
  3162. JUMP @build_member_done ; Skip over single steps
  3163. :build_member_single
  3164. LOAD32 R0 R5 4 ; MEMBER_TYPE->SIZE
  3165. STORE32 R0 R3 4 ; I->SIZE = MEMBER_TYPE->SIZE
  3166. :build_member_done
  3167. LOAD32 R2 R3 4 ; MEMBER_SIZE = I->SIZE
  3168. STORE32 R5 R3 20 ; I->TYPE = MEMBER_TYPE
  3169. STORE32 R6 R3 8 ; I->OFFSET = OFFSET
  3170. MOVE R1 R6 ; Restore OFFSET
  3171. MOVE R0 R3 ; RETURN I in R0
  3172. POPR R6 R15 ; Restore R6
  3173. POPR R5 R15 ; Restore R5
  3174. POPR R4 R15 ; Restore R4
  3175. POPR R3 R15 ; Restore R3
  3176. RET R15
  3177. :build_member_string0
  3178. "Struct only supports [num] form
  3179. "
  3180. ;; build_union function
  3181. ;; Receives a struct type* in R0, int in R1 and int in R2
  3182. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3183. ;; Modifies R2 to current member_size
  3184. ;; Returns struct type* in R0
  3185. :build_union
  3186. PUSHR R3 R15 ; Protect R3
  3187. PUSHR R4 R15 ; Protect R4
  3188. PUSHR R5 R15 ; Protect R5
  3189. MOVE R4 R0 ; Protect LAST
  3190. MOVE R3 R1 ; Protect OFFSET
  3191. FALSE R5 ; SIZE = 0
  3192. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3193. LOADUI R0 $build_union_string0 ; ERROR MESSAGE
  3194. LOADUI R1 $open_curly_brace ; OPEN CURLY BRACE
  3195. CALLI R15 @require_match ; Ensure we have that curly brace
  3196. :build_union_iter
  3197. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3198. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  3199. LOADUI R1 125 ; numerical value of }
  3200. CMPJUMPI.E R0 R1 @build_union_done ; No more looping required
  3201. MOVE R0 R4 ; We are passing last to be overwritten
  3202. MOVE R1 R3 ; We are also passing OFFSET
  3203. CALLI R15 @build_member ; To build_member to get new LAST and new member_size
  3204. CMPSKIP.LE R2 R5 ; If MEMBER_SIZE > SIZE
  3205. COPY R5 R2 ; SIZE = MEMMER_SIZE
  3206. MOVE R4 R0 ; Protect LAST
  3207. MOVE R3 R1 ; Protect OFFSET
  3208. LOADUI R0 $build_union_string1 ; ERROR MESSAGE
  3209. LOADUI R1 $semicolon ; SEMICOLON
  3210. CALLI R15 @require_match ; Ensure we have that curly brace
  3211. JUMP @build_union_iter ; Loop until we get that closing curly brace
  3212. :build_union_done
  3213. MOVE R2 R5 ; Setting MEMBER_SIZE = SIZE
  3214. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3215. MOVE R1 R3 ; Restore OFFSET
  3216. MOVE R0 R4 ; Restore LAST as we are turning that
  3217. POPR R5 R15 ; Restore R5
  3218. POPR R4 R15 ; Restore R4
  3219. POPR R3 R15 ; Restore R3
  3220. RET R15
  3221. :build_union_string0
  3222. "ERROR in build_union
  3223. Missing {
  3224. "
  3225. :build_union_string1
  3226. "ERROR in build_union
  3227. Missing ;
  3228. "
  3229. ;; create_struct function
  3230. ;; Receives Nothing
  3231. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3232. ;; Returns Nothing
  3233. :create_struct
  3234. PUSHR R0 R15 ; Protect R0
  3235. PUSHR R1 R15 ; Protect R1
  3236. PUSHR R2 R15 ; Protect R2
  3237. PUSHR R3 R15 ; Protect R3
  3238. PUSHR R4 R15 ; Protect R4
  3239. PUSHR R5 R15 ; Protect R5
  3240. PUSHR R6 R15 ; Protect R6
  3241. FALSE R5 ; OFFSET = 0
  3242. FALSE R2 ; MEMBER_SIZE = 0
  3243. COPY R3 R14 ; SET HEAD
  3244. ADDUI R14 R14 28 ; CALLOC struct type
  3245. COPY R4 R14 ; SET I
  3246. ADDUI R14 R14 28 ; CALLOC struct type
  3247. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3248. STORE32 R0 R3 24 ; HEAD->NAME = GLOBAL_TOKEN->S
  3249. STORE32 R0 R4 24 ; I->NAME = GLOBAL_TOKEN->S
  3250. STORE32 R4 R3 12 ; HEAD->INDIRECT = I
  3251. STORE32 R3 R4 12 ; I->INDIRECT - HEAD
  3252. LOADR32 R0 @global_types ; Get Address of GLOBAL_TYPES
  3253. STORE R0 R3 0 ; HEAD->NEXT = GLOBAL_TYPES
  3254. STORER32 R3 @global_types ; GLOBAL_TYPES = HEAD
  3255. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3256. LOADUI R0 8 ; Standard Pointer SIZE
  3257. STORE32 R0 R4 4 ; I->SIZE = 8
  3258. LOADUI R0 $create_struct_string0 ; ERROR MESSAGE
  3259. LOADUI R1 $open_curly_brace ; OPEN CURLY BRACE
  3260. CALLI R15 @require_match ; Ensure we have that curly brace
  3261. FALSE R6 ; LAST = NULL
  3262. :create_struct_iter
  3263. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3264. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  3265. LOADUI R1 125 ; Numerical value of }
  3266. CMPJUMPI.E R0 R1 @create_struct_done ; Stop looping if match
  3267. LOADUI R1 $union ; Pointer to string UNION
  3268. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3269. CALLI R15 @match ; Check if they Match
  3270. SWAP R0 R6 ; Put LAST in place
  3271. MOVE R1 R5 ; Put OFFSET in place
  3272. JUMP.NZ R6 @create_struct_union ; Deal with union case
  3273. ;; Deal with standard member case
  3274. CALLI R15 @build_member ; Sets new LAST and MEMBER_SIZE
  3275. JUMP @create_struct_iter2 ; reset for loop
  3276. :create_struct_union
  3277. CALLI R15 @build_union
  3278. :create_struct_iter2
  3279. ADD R5 R1 R2 ; OFFSET = OFFSET + MEMBER_SIZE
  3280. SWAP R0 R6 ; Put LAST in place
  3281. LOADUI R0 $create_struct_string1 ; ERROR MESSAGE
  3282. LOADUI R1 $semicolon ; SEMICOLON
  3283. CALLI R15 @require_match ; Ensure we have that semicolon
  3284. JUMP @create_struct_iter ; Keep Looping
  3285. :create_struct_done
  3286. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3287. LOADUI R0 $create_struct_string1 ; ERROR MESSAGE
  3288. LOADUI R1 $semicolon ; SEMICOLON
  3289. CALLI R15 @require_match ; Ensure we have that semicolon
  3290. STORE32 R5 R3 4 ; HEAD->SIZE = OFFSET
  3291. STORE32 R6 R3 16 ; HEAD->MEMBERS = LAST
  3292. STORE32 R6 R4 16 ; I->MEMBERS = LAST
  3293. POPR R6 R15 ; Restore R6
  3294. POPR R5 R15 ; Restore R5
  3295. POPR R4 R15 ; Restore R4
  3296. POPR R3 R15 ; Restore R3
  3297. POPR R2 R15 ; Restore R2
  3298. POPR R1 R15 ; Restore R1
  3299. POPR R0 R15 ; Restore R0
  3300. RET R15
  3301. :create_struct_string0
  3302. "ERROR in create_struct
  3303. Missing {
  3304. "
  3305. :create_struct_string1
  3306. "ERROR in create_struct
  3307. Missing ;
  3308. "
  3309. ;; type_name function
  3310. ;; Receives Nothing
  3311. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3312. ;; Returns struct type* in R0
  3313. :type_name
  3314. PUSHR R1 R15 ; Protect R1
  3315. PUSHR R2 R15 ; Protect R2
  3316. LOADUI R0 $struct ; String for struct for comparison
  3317. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  3318. CALLI R15 @match ; Check if they match
  3319. CMPSKIPI.E R0 0 ; If STRUCTURE
  3320. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3321. LOAD32 R2 R13 8 ; GLOBAL_TOKEN->S
  3322. LOADR32 R1 @global_types ; Check using the GLOBAL TYPES LIST
  3323. SWAP R0 R2 ; Put GLOBAL_TOKEN->S in the right place
  3324. CALLI R15 @lookup_type ; RET = lookup_type(GLOBAL_TOKEN->S)
  3325. MOVE R1 R2 ; Put STRUCTURE in the right place
  3326. CMPSKIP.E R0 R1 ; If RET == NULL and !STRUCTURE
  3327. JUMP @type_name_struct ; Guess not
  3328. ;; Exit with useful error message
  3329. FALSE R1 ; We will want to be writing the error message for the Human
  3330. LOADUI R0 $type_name_string0 ; The first string
  3331. CALLI R15 @file_print ; Display it
  3332. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3333. CALLI R15 @file_print ; Display it
  3334. LOADUI R0 $newline ; Terminating linefeed
  3335. CALLI R15 @file_print ; Display it
  3336. CALLI R15 @line_error ; Give useful debug info
  3337. HALT ; Just exit
  3338. :type_name_struct
  3339. JUMP.NZ R0 @type_name_iter ; If was found
  3340. CALLI R15 @create_struct ; Otherwise create it
  3341. JUMP @type_name_done ; and be done
  3342. :type_name_iter
  3343. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3344. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  3345. LOADU8 R1 R1 0 ; GLOBAL_TOKEN->S[0]
  3346. CMPSKIPI.E R1 42 ; if GLOBAL_TOKEN->S[0] == '*'
  3347. JUMP @type_name_done ; Looks like Nope
  3348. LOAD32 R0 R0 12 ; RET = RET->INDIRECT
  3349. JUMP @type_name_iter ; Keep looping
  3350. :type_name_done
  3351. POPR R2 R15 ; Restore R2
  3352. POPR R1 R15 ; Restore R1
  3353. RET R15
  3354. :type_name_string0
  3355. "Unknown type "
  3356. ;; line_error function
  3357. ;; Receives Nothing
  3358. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3359. ;; Returns nothing
  3360. :line_error
  3361. PUSHR R0 R15 ; Protect R0
  3362. PUSHR R1 R15 ; Protect R1
  3363. LOADUI R0 $line_error_string0 ; Our leading string
  3364. FALSE R1 ; We want the user to see
  3365. CALLI R15 @file_print ; Print it
  3366. LOAD32 R0 R13 16 ; GLOBAL_TOKEN->LINENUMBER
  3367. CALLI R15 @numerate_number ; Get a string pointer for number
  3368. CALLI R15 @file_print ; And print it
  3369. POPR R1 R15 ; Restore R1
  3370. POPR R0 R15 ; Restore R0
  3371. RET R15
  3372. :line_error_string0
  3373. "In file: TTY1 On line: "
  3374. ;; require_match function
  3375. ;; Receives char* in R0 and char* in R1
  3376. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3377. ;; Returns Nothing
  3378. :require_match
  3379. PUSHR R0 R15 ; Protect R0
  3380. PUSHR R2 R15 ; Protect R2
  3381. MOVE R2 R0 ; Get MESSAGE out of the way
  3382. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3383. CALLI R15 @match ; Check if GLOBAL_TOKEN->S == REQUIRED
  3384. JUMP.NZ R0 @require_match_done ; Looks like it was a match
  3385. ;; Terminate with an error
  3386. MOVE R0 R2 ; Put MESSAGE in required spot
  3387. FALSE R1 ; We want to write for user
  3388. CALLI R15 @file_print ; Write it
  3389. CALLI R15 @line_error ; And provide some debug info
  3390. HALT ; Then Stop immediately
  3391. :require_match_done
  3392. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3393. POPR R2 R15 ; Restore R2
  3394. POPR R0 R15 ; Restore R0
  3395. RET R15
  3396. ;; numerate_number function
  3397. ;; Receives int in R0
  3398. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3399. ;; Returns pointer to string generated
  3400. :numerate_number
  3401. PUSHR R1 R15 ; Preserve R1
  3402. PUSHR R2 R15 ; Preserve R2
  3403. PUSHR R3 R15 ; Preserve R3
  3404. PUSHR R4 R15 ; Preserve R4
  3405. PUSHR R5 R15 ; Preserve R5
  3406. PUSHR R6 R15 ; Preserve R6
  3407. MOVE R3 R0 ; Move Integer out of the way
  3408. COPY R1 R14 ; Get pointer result
  3409. ADDUI R14 R14 16 ; CALLOC the 16 chars of space
  3410. FALSE R6 ; Set index to 0
  3411. JUMP.Z R3 @numerate_number_ZERO ; Deal with Special case of ZERO
  3412. JUMP.P R3 @numerate_number_Positive
  3413. LOADUI R0 45 ; Using -
  3414. STOREX8 R0 R1 R6 ; write leading -
  3415. ADDUI R6 R6 1 ; Increment by 1
  3416. NOT R3 R3 ; Flip into positive
  3417. ADDUI R3 R3 1 ; Adjust twos
  3418. :numerate_number_Positive
  3419. LOADR R2 @Max_Decimal ; Starting from the Top
  3420. LOADUI R5 10 ; We move down by 10
  3421. FALSE R4 ; Flag leading Zeros
  3422. :numerate_number_0
  3423. DIVIDE R0 R3 R3 R2 ; Break off top 10
  3424. CMPSKIPI.E R0 0 ; If Not Zero
  3425. TRUE R4 ; Flip the Flag
  3426. JUMP.Z R4 @numerate_number_1 ; Skip leading Zeros
  3427. ADDUI R0 R0 48 ; Shift into ASCII
  3428. STOREX8 R0 R1 R6 ; write digit
  3429. ADDUI R6 R6 1 ; Increment by 1
  3430. :numerate_number_1
  3431. DIV R2 R2 R5 ; Look at next 10
  3432. CMPSKIPI.E R2 0 ; If we reached the bottom STOP
  3433. JUMP @numerate_number_0 ; Otherwise keep looping
  3434. :numerate_number_done
  3435. FALSE R0 ; NULL Terminate
  3436. STOREX8 R0 R1 R6 ; write
  3437. MOVE R0 R1 ; Return pointer to our string
  3438. ;; Cleanup
  3439. POPR R6 R15 ; Restore R6
  3440. POPR R5 R15 ; Restore R5
  3441. POPR R4 R15 ; Restore R4
  3442. POPR R3 R15 ; Restore R3
  3443. POPR R2 R15 ; Restore R2
  3444. POPR R1 R15 ; Restore R1
  3445. RET R15
  3446. :numerate_number_ZERO
  3447. LOADUI R0 48 ; Using Zero
  3448. STOREX8 R0 R1 R6 ; write
  3449. ADDUI R6 R6 1 ; Increment by 1
  3450. JUMP @numerate_number_done ; Be done
  3451. :Max_Decimal
  3452. '3B9ACA00'
  3453. ;; numerate_string function
  3454. ;; Receives pointer To string in R0
  3455. ;; Returns number in R0 equal to value of string
  3456. ;; Or Zero in the event of invalid string
  3457. :numerate_string
  3458. PUSHR R1 R15 ; Protect R1
  3459. PUSHR R2 R15 ; Protect R2
  3460. PUSHR R3 R15 ; Protect R3
  3461. PUSHR R4 R15 ; Protect R4
  3462. ;; Initialize
  3463. MOVE R1 R0 ; Get Text pointer out of the way
  3464. FALSE R2 ; Set Negative flag to false
  3465. FALSE R3 ; Set current count to Zero
  3466. LOAD8 R0 R1 1 ; Get second byte
  3467. CMPSKIPI.NE R0 120 ; If the second byte is x
  3468. JUMP @numerate_string_hex ; treat string like hex
  3469. ;; Deal with Decimal input
  3470. LOADUI R4 10 ; Multiply by 10
  3471. LOAD8 R0 R1 0 ; Get a byte
  3472. CMPSKIPI.NE R0 45 ; If - toggle flag
  3473. TRUE R2 ; So that we know to negate
  3474. CMPSKIPI.E R2 0 ; If toggled
  3475. ADDUI R1 R1 1 ; Move to next
  3476. :numerate_string_dec
  3477. LOAD8 R0 R1 0 ; Get a byte
  3478. CMPSKIPI.NE R0 0 ; If NULL
  3479. JUMP @numerate_string_done ; Be done
  3480. MUL R3 R3 R4 ; Shift counter by 10
  3481. SUBI R0 R0 48 ; Convert ascii to number
  3482. CMPSKIPI.GE R0 0 ; If less than a number
  3483. JUMP @numerate_string_done ; Terminate NOW
  3484. CMPSKIPI.L R0 10 ; If more than a number
  3485. JUMP @numerate_string_done ; Terminate NOW
  3486. ADDU R3 R3 R0 ; Don't add to the count
  3487. ADDUI R1 R1 1 ; Move onto next byte
  3488. JUMP @numerate_string_dec
  3489. ;; Deal with Hex input
  3490. :numerate_string_hex
  3491. LOAD8 R0 R1 0 ; Get a byte
  3492. CMPSKIPI.E R0 48 ; All hex strings start with 0x
  3493. JUMP @numerate_string_done ; Be done if not a match
  3494. ADDUI R1 R1 2 ; Move to after leading 0x
  3495. :numerate_string_hex_0
  3496. LOAD8 R0 R1 0 ; Get a byte
  3497. CMPSKIPI.NE R0 0 ; If NULL
  3498. JUMP @numerate_string_done ; Be done
  3499. SL0I R3 4 ; Shift counter by 16
  3500. SUBI R0 R0 48 ; Convert ascii number to number
  3501. CMPSKIPI.L R0 10 ; If A-F
  3502. SUBI R0 R0 7 ; Shove into Range
  3503. CMPSKIPI.L R0 16 ; If a-f
  3504. SUBI R0 R0 32 ; Shove into Range
  3505. ADDU R3 R3 R0 ; Add to the count
  3506. ADDUI R1 R1 1 ; Get next Hex
  3507. JUMP @numerate_string_hex_0
  3508. ;; Clean up
  3509. :numerate_string_done
  3510. CMPSKIPI.E R2 0 ; If Negate flag has been set
  3511. NEG R3 R3 ; Make the number negative
  3512. MOVE R0 R3 ; Put number in R0
  3513. POPR R4 R15 ; Restore R4
  3514. POPR R3 R15 ; Restore R3
  3515. POPR R2 R15 ; Restore R2
  3516. POPR R1 R15 ; Restore R1
  3517. RET R15
  3518. ;; Keywords
  3519. :union
  3520. "union"
  3521. :struct
  3522. "struct"
  3523. :constant
  3524. "CONSTANT"
  3525. :main_string
  3526. "main"
  3527. :argc_string
  3528. "argc"
  3529. :argv_string
  3530. "argv"
  3531. :if_string
  3532. "if"
  3533. :else_string
  3534. "else"
  3535. :do_string
  3536. "do"
  3537. :while_string
  3538. "while"
  3539. :for_string
  3540. "for"
  3541. :asm_string
  3542. "asm"
  3543. :goto_string
  3544. "goto"
  3545. :return_string
  3546. "return"
  3547. :break_string
  3548. "break"
  3549. :continue_string
  3550. "continue"
  3551. :sizeof_string
  3552. "sizeof"
  3553. :plus_string
  3554. "+"
  3555. :minus_string
  3556. "-"
  3557. :multiply_string
  3558. "*"
  3559. :divide_string
  3560. "/"
  3561. :modulus_string
  3562. "%"
  3563. :left_shift_string
  3564. "<<"
  3565. :right_shift_string
  3566. ">>"
  3567. :less_than_string
  3568. "<"
  3569. :less_than_equal_string
  3570. "<="
  3571. :greater_than_equal_string
  3572. ">="
  3573. :greater_than_string
  3574. ">"
  3575. :equal_to_string
  3576. "=="
  3577. :not_equal_string
  3578. "!="
  3579. :bitwise_and
  3580. "&"
  3581. :logical_and
  3582. "&&"
  3583. :bitwise_or
  3584. "|"
  3585. :logical_or
  3586. "||"
  3587. :bitwise_xor
  3588. "^"
  3589. :arrow_string
  3590. "->"
  3591. ;; Frequently Used strings
  3592. ;; Generally used by require_match
  3593. :open_curly_brace
  3594. "{"
  3595. :close_curly_brace
  3596. "}"
  3597. :open_paren
  3598. "("
  3599. :close_paren
  3600. ")"
  3601. :open_bracket
  3602. "["
  3603. :close_bracket
  3604. "]"
  3605. :semicolon
  3606. ";"
  3607. :equal
  3608. "="
  3609. :percent
  3610. "%"
  3611. :newline
  3612. "
  3613. "
  3614. :underline
  3615. "_"
  3616. ;; Global types
  3617. ;; NEXT (0), SIZE (4), OFFSET (8), INDIRECT (12), MEMBERS (16), TYPE (20), NAME (24)
  3618. :global_types
  3619. &type_void
  3620. :prim_types
  3621. :type_void
  3622. &type_int ; NEXT
  3623. '00 00 00 08' ; SIZE
  3624. NOP ; OFFSET
  3625. &type_void ; INDIRECT
  3626. NOP ; MEMBERS
  3627. &type_void ; TYPE
  3628. &type_void_name ; NAME
  3629. :type_void_name
  3630. "void"
  3631. :type_int
  3632. &type_char ; NEXT
  3633. '00 00 00 08' ; SIZE
  3634. NOP ; OFFSET
  3635. &type_int ; INDIRECT
  3636. NOP ; MEMBERS
  3637. &type_int ; TYPE
  3638. &type_int_name ; NAME
  3639. :type_int_name
  3640. "int"
  3641. :type_char
  3642. &type_file ; NEXT
  3643. '00 00 00 01' ; SIZE
  3644. NOP ; OFFSET
  3645. &type_char_indirect ; INDIRECT
  3646. NOP ; MEMBERS
  3647. &type_char ; TYPE
  3648. &type_char_name ; NAME
  3649. :type_char_name
  3650. "char"
  3651. :type_char_indirect
  3652. &type_file ; NEXT
  3653. '00 00 00 08' ; SIZE
  3654. NOP ; OFFSET
  3655. &type_char_double_indirect ; INDIRECT
  3656. NOP ; MEMBERS
  3657. &type_char_indirect ; TYPE
  3658. &type_char_indirect_name ; NAME
  3659. :type_char_indirect_name
  3660. "char*"
  3661. :type_char_double_indirect
  3662. &type_file ; NEXT
  3663. '00 00 00 08' ; SIZE
  3664. NOP ; OFFSET
  3665. &type_char_double_indirect ; INDIRECT
  3666. NOP ; MEMBERS
  3667. &type_char_indirect ; TYPE
  3668. &type_char_double_indirect_name ; NAME
  3669. :type_char_double_indirect_name
  3670. "char**"
  3671. :type_file
  3672. &type_function ; NEXT
  3673. '00 00 00 08' ; SIZE
  3674. NOP ; OFFSET
  3675. &type_file ; INDIRECT
  3676. NOP ; MEMBERS
  3677. &type_file ; TYPE
  3678. &type_file_name ; NAME
  3679. :type_file_name
  3680. "FILE"
  3681. :type_function
  3682. &type_unsigned ; NEXT
  3683. '00 00 00 08' ; SIZE
  3684. NOP ; OFFSET
  3685. &type_function ; INDIRECT
  3686. NOP ; MEMBERS
  3687. &type_function ; TYPE
  3688. &type_function_name ; NAME
  3689. :type_function_name
  3690. "FUNCTION"
  3691. :type_unsigned
  3692. NOP ; NEXT (NULL)
  3693. '00 00 00 08' ; SIZE
  3694. NOP ; OFFSET
  3695. &type_unsigned ; INDIRECT
  3696. NOP ; MEMBERS
  3697. &type_unsigned ; TYPE
  3698. &type_unsigned_name ; NAME
  3699. :type_unsigned_name
  3700. "unsigned"
  3701. ;; debug_list function
  3702. ;; Receives struct token_list* in R0
  3703. ;; Prints contents of list and HALTS
  3704. ;; Does not return
  3705. :debug_list
  3706. MOVE R9 R0 ; Protect the list Pointer
  3707. FALSE R1 ; Write to TTY
  3708. :debug_list_iter
  3709. ;; Header
  3710. LOADUI R0 $debug_list_string0 ; Using our first string
  3711. CALLI R15 @file_print ; Print it
  3712. COPY R0 R9 ; Use address of pointer
  3713. CALLI R15 @numerate_number ; Convert it into a string
  3714. CALLI R15 @file_print ; Print it
  3715. ;; NEXT
  3716. LOADUI R0 $debug_list_string1 ; Using our second string
  3717. CALLI R15 @file_print ; Print it
  3718. LOAD32 R0 R9 0 ; Use address of pointer
  3719. CALLI R15 @numerate_number ; Convert it into a string
  3720. CALLI R15 @file_print ; Print it
  3721. ;; PREV
  3722. LOADUI R0 $debug_list_string2 ; Using our third string
  3723. CALLI R15 @file_print ; Print it
  3724. LOAD32 R0 R9 4 ; Use address of pointer
  3725. CALLI R15 @numerate_number ; Convert it into a string
  3726. CALLI R15 @file_print ; Print it
  3727. ;; S
  3728. LOADUI R0 $debug_list_string3 ; Using our fourth string
  3729. CALLI R15 @file_print ; Print it
  3730. LOAD32 R0 R9 8 ; Use address of pointer
  3731. CALLI R15 @numerate_number ; Convert it into a string
  3732. CALLI R15 @file_print ; Print it
  3733. ;; S Contents
  3734. LOADUI R0 $debug_list_string4 ; Using our Prefix string
  3735. CALLI R15 @file_print ; Print it
  3736. LOAD32 R0 R9 8 ; Use address of pointer
  3737. CMPSKIPI.NE R0 0 ; If NULL Pointer
  3738. LOADUI R0 $debug_list_string_null ; Give meaningful message instead
  3739. CALLI R15 @file_print ; Print it
  3740. ;; TYPE
  3741. LOADUI R0 $debug_list_string5 ; Using our fifth string
  3742. CALLI R15 @file_print ; Print it
  3743. LOAD32 R0 R9 12 ; Use address of pointer
  3744. CALLI R15 @numerate_number ; Convert it into a string
  3745. CALLI R15 @file_print ; Print it
  3746. ;; PREV
  3747. LOADUI R0 $debug_list_string6 ; Using our sixth string
  3748. CALLI R15 @file_print ; Print it
  3749. LOAD32 R0 R9 16 ; Use address of pointer
  3750. CALLI R15 @numerate_number ; Convert it into a string
  3751. CALLI R15 @file_print ; Print it
  3752. ;; Add some space
  3753. LOADUI R0 10 ; Using NEWLINE
  3754. FPUTC
  3755. FPUTC
  3756. ;; Iterate if next not NULL
  3757. LOAD32 R9 R9 0 ; TOKEN = TOKEN->NEXT
  3758. JUMP.NZ R9 @debug_list_iter
  3759. ;; Looks lke we are done, wrap it up
  3760. HALT
  3761. :debug_list_string0
  3762. "Token_list node at address: "
  3763. :debug_list_string1
  3764. "
  3765. NEXT address: "
  3766. :debug_list_string2
  3767. "
  3768. PREV address: "
  3769. :debug_list_string3
  3770. "
  3771. S address: "
  3772. :debug_list_string4
  3773. "
  3774. The contents of S are: "
  3775. :debug_list_string5
  3776. "
  3777. TYPE address: "
  3778. :debug_list_string6
  3779. "
  3780. ARGUMENTS address: "
  3781. :debug_list_string_null
  3782. ">::<NULL>::<"
  3783. :STACK