cc_armv7l.s 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435
  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 $not ; Using "!"
  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 $unary_expr_sizeof_string2 ; Our header
  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. " R0 LOADI8_ALWAYS
  668. "
  669. ;; constant_load function
  670. ;; Receives struct token_list* a in R0
  671. ;; Returns nothing
  672. :constant_load
  673. PUSHR R0 R15 ; Protect R0
  674. LOADUI R0 $constant_load_string0 ; Our header
  675. CALLI R15 @emit_out ; emit it
  676. POPR R0 R15 ; Restore R0
  677. LOAD32 R0 R0 16 ; A->ARGUMENTS
  678. LOAD32 R0 R0 8 ; A->ARGUMENTS->S
  679. CALLI R15 @emit_out ; emit it
  680. LOADUI R0 $newline ; Using "\n"
  681. CALLI R15 @emit_out ; emit it
  682. RET R15
  683. :constant_load_string0
  684. "!0 R0 LOAD32 R15 MEMORY
  685. ~0 JUMP_ALWAYS
  686. %"
  687. ;; variable_load function
  688. ;; Receives struct token_list* a in R0
  689. ;; and struct token_list* current_target in R8
  690. ;; Returns Nothing
  691. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  692. :variable_load
  693. PUSHR R1 R15 ; Protect R1
  694. PUSHR R2 R15 ; Protect R2
  695. MOVE R2 R0 ; Protect A
  696. ;; Check if function call
  697. LOADUI R0 $type_function_name ; Using "FUNCTION"
  698. LOAD32 R1 R2 12 ; A->TYPE
  699. LOAD32 R1 R1 24 ; A->TYPE->NAME
  700. CALLI R15 @match ; IF "FUNCTION" == A->TYPE->NAME
  701. JUMP.Z R0 @variable_load_regular ; Nope
  702. LOADUI R0 $open_paren ; Using "("
  703. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  704. CALLI R15 @match ; IF "(" == GLOBAL_TOKEN->S
  705. JUMP.Z R0 @variable_load_regular ; Nope
  706. ;; Deal with function call
  707. LOAD32 R0 R2 16 ; A->DEPTH
  708. CALLI R15 @numerate_number ; Convert to string
  709. TRUE R1 ; Passing TRUE
  710. CALLI R15 @function_call ; DO IT
  711. JUMP @variable_load_done ; Be done
  712. :variable_load_regular
  713. LOAD32 R8 R2 12 ; CURRENT_TARGET = A->TYPE
  714. LOADUI R0 $not ; Using "!"
  715. CALLI R15 @emit_out ; emit it
  716. LOAD32 R0 R2 16 ; A->DEPTH
  717. CALLI R15 @numerate_number ; Convert to string
  718. CALLI R15 @emit_out ; emit it
  719. LOADUI R0 $variable_load_string0 ; Our prefix
  720. CALLI R15 @emit_out ; emit it
  721. ;; check for special case 1
  722. LOADUI R0 $equal ; Using "="
  723. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  724. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  725. JUMP.NZ R0 @variable_load_done ; Be done
  726. ;; deal with the general case
  727. LOADUI R0 $variable_load_string1 ; Our postfix
  728. CALLI R15 @emit_out ; emit it
  729. :variable_load_done
  730. POPR R2 R15 ; Restore R2
  731. POPR R1 R15 ; Restore R1
  732. RET R15
  733. :variable_load_string0
  734. " R0 SUB BP ARITH_ALWAYS
  735. "
  736. :variable_load_string1
  737. "!0 R0 LOAD32 R0 MEMORY
  738. "
  739. ;; function_load function
  740. ;; Receives struct token_list* a in R0
  741. ;; Returns nothing
  742. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  743. :function_load
  744. PUSHR R1 R15 ; Protect R1
  745. LOAD32 R0 R0 8 ; A->S
  746. PUSHR R0 R15 ; Protect A->S
  747. LOADUI R0 $open_paren ; Using "("
  748. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  749. CALLI R15 @match ; If GLOBAL_TOKEN->S == "("
  750. JUMP.Z R0 @function_load_regular ; If not do the simple thing
  751. ;; Deal iwth function call
  752. POPR R0 R15 ; Restore A->S
  753. FALSE R1 ; FALSE
  754. CALLI R15 @function_call ; Do the function call
  755. JUMP @function_load_done ; Clean up
  756. :function_load_regular
  757. LOADUI R0 $function_load_string0 ; Using our header string
  758. CALLI R15 @emit_out ; emit it
  759. POPR R0 R15 ; Restore A->S
  760. CALLI R15 @emit_out ; emit it
  761. LOADUI R0 $newline ; Using "\n"
  762. CALLI R15 @emit_out ; emit it
  763. :function_load_done
  764. POPR R1 R15 ; Restore R1
  765. RET R15
  766. :function_load_string0
  767. "!0 R0 LOAD32 R15 MEMORY
  768. ~0 JUMP_ALWAYS
  769. &FUNCTION_"
  770. ;; global_load function
  771. ;; Receives struct token_list* a in R0
  772. ;; and struct token_list* current_target in R8
  773. ;; Returns nothing
  774. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  775. :global_load
  776. PUSHR R0 R15 ; Protect A
  777. LOAD32 R8 R0 12 ; CURRENT_TARGET = A->TYPE
  778. LOADUI R0 $global_load_string0 ; Our header string
  779. CALLI R15 @emit_out ; emit it
  780. POPR R0 R15 ; Restore A
  781. LOAD32 R0 R0 8 ; A->S
  782. CALLI R15 @emit_out ; emit it
  783. LOADUI R0 $newline ; Using "\n"
  784. CALLI R15 @emit_out ; emit it
  785. PUSHR R1 R15 ; Protect R1
  786. LOADUI R0 $equal ; Using "="
  787. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  788. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  789. JUMP.NZ R0 @global_load_done ; Skip the following
  790. ;; Deal with non-assignment
  791. LOADUI R0 $global_load_string1 ; Our footer string
  792. CALLI R15 @emit_out ; emit it
  793. :global_load_done
  794. POPR R1 R15 ; Restore R1
  795. RET R15
  796. :global_load_string0
  797. "!0 R0 LOAD32 R15 MEMORY
  798. ~0 JUMP_ALWAYS
  799. &GLOBAL_"
  800. :global_load_string1
  801. "!0 R0 LOAD32 R0 MEMORY
  802. "
  803. ;; primary_expr_failure function
  804. ;; Fails hard and fast
  805. ;; Receives nothing
  806. ;; HALTs and will trash registers
  807. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  808. :primary_expr_failure
  809. LOADUI R0 $primary_expr_failure_string0 ; Our first string
  810. FALSE R1 ; Display to User
  811. CALLI R15 @file_print ; Print it
  812. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  813. CALLI R15 @file_print ; Print it
  814. LOADUI R0 $primary_expr_failure_string1 ; Our last string
  815. CALLI R15 @file_print ; Print it
  816. CALLI R15 @line_error ; Make it a line error message too
  817. HALT
  818. :primary_expr_failure_string0
  819. "Received "
  820. :primary_expr_failure_string1
  821. " in primary_expr
  822. "
  823. ;; primary_expr_string function
  824. ;; Receives struct token_list* global_token in R13,
  825. ;; struct token_list* out in R12,
  826. ;; struct token_list* string_list in R11
  827. ;; struct token_list* global_list in R10
  828. ;; and struct token_list* FUNC in R9
  829. ;; and struct token_list* current_target in R8
  830. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  831. ;; Returns the token_lists modified
  832. :primary_expr_string
  833. PUSHR R0 R15 ; Protect R0
  834. PUSHR R1 R15 ; Protect R1
  835. PUSHR R2 R15 ; Protect R2
  836. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  837. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  838. STORER32 R1 @current_count ; Update CURRENT_COUNT
  839. CALLI R15 @numerate_number ; Convert to string
  840. MOVE R2 R0 ; Put string in safe place
  841. LOADUI R0 $primary_expr_string_string0 ; Our string prefix
  842. CALLI R15 @emit_out ; emit it
  843. LOAD32 R0 R9 8 ; FUNCTION->S
  844. COPY R1 R2 ; NUMBER_STRING
  845. CALLI R15 @uniqueID_out ; Make it unique
  846. ;; The target
  847. LOADUI R0 $primary_expr_string_string1
  848. COPY R1 R11 ; Using STRINGS_LIST
  849. CALLI R15 @emit ; emit it
  850. MOVE R1 R0 ; Put STRINGS_LIST in correct place
  851. LOAD32 R0 R9 8 ; Using FUNCTION->S
  852. CALLI R15 @uniqueID ; Make it unique
  853. MOVE R11 R0 ; Update STRINGS_LIST
  854. ;; Parse the string
  855. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  856. CALLI R15 @parse_string ; Parse it
  857. COPY R1 R11 ; Using STRINGS_LIST
  858. CALLI R15 @emit ; emit it
  859. MOVE R11 R0 ; Update STRINGS_LIST
  860. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  861. POPR R2 R15 ; Restore R2
  862. POPR R1 R15 ; Restore R1
  863. POPR R0 R15 ; Restore R0
  864. RET R15
  865. :primary_expr_string_string0
  866. "!0 R0 LOAD32 R15 MEMORY
  867. ~0 JUMP_ALWAYS
  868. &STRING_"
  869. :primary_expr_string_string1
  870. ":STRING_"
  871. ;; primary_expr_char function
  872. ;; Receives struct token_list* global_token in R13,
  873. ;; struct token_list* out in R12,
  874. ;; struct token_list* string_list in R11
  875. ;; struct token_list* global_list in R10
  876. ;; and struct token_list* FUNC in R9
  877. ;; and struct token_list* current_target in R8
  878. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  879. ;; Returns the token_lists modified
  880. :primary_expr_char
  881. PUSHR R0 R15 ; Protect R0
  882. LOADUI R0 $not ; Using "!"
  883. CALLI R15 @emit_out ; emit it
  884. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  885. ADDUI R0 R0 1 ; GLOBAL_TOKEN->S + 1
  886. CALLI R15 @escape_lookup ; escape_lookup value
  887. CALLI R15 @numerate_number ; Make it a string
  888. CALLI R15 @emit_out ; emit it
  889. LOADUI R0 $primary_expr_char_string0 ; Using our header string
  890. CALLI R15 @emit_out ; emit it
  891. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  892. POPR R0 R15 ; Restore R0
  893. RET R15
  894. :primary_expr_char_string0
  895. " R0 LOADI8_ALWAYS
  896. "
  897. ;; primary_expr_number function
  898. ;; struct token_list* out in R12,
  899. ;; struct token_list* string_list in R11
  900. ;; struct token_list* global_list in R10
  901. ;; and struct token_list* FUNC in R9
  902. ;; and struct token_list* current_target in R8
  903. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  904. ;; Returns the token_lists modified
  905. :primary_expr_number
  906. LOADUI R0 $primary_expr_number_string0 ; Our header
  907. CALLI R15 @emit_out ; emit it
  908. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  909. CALLI R15 @emit_out ; emit it
  910. LOADUI R0 $newline ; Using "\n"
  911. CALLI R15 @emit_out ; emit it
  912. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  913. RET R15
  914. :primary_expr_number_string0
  915. "!0 R0 LOAD32 R15 MEMORY
  916. ~0 JUMP_ALWAYS
  917. %"
  918. ;; primary_expr_variable function
  919. ;; struct token_list* out in R12,
  920. ;; struct token_list* string_list in R11
  921. ;; struct token_list* global_list in R10
  922. ;; and struct token_list* FUNC in R9
  923. ;; and struct token_list* current_target in R8
  924. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  925. ;; Returns the token_lists modified
  926. :primary_expr_variable
  927. PUSHR R0 R15 ; Protect R0
  928. PUSHR R1 R15 ; Protect R1
  929. PUSHR R2 R15 ; Protect R2
  930. LOAD32 R2 R13 8 ; S = GLOBAL_TOKEN->S
  931. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  932. COPY R0 R2 ; Using S
  933. LOADR32 R1 @global_constant_list
  934. CALLI R15 @sym_lookup ; Lookup S in CONSTANTS
  935. JUMP.Z R0 @primary_expr_variable_locals ; try Locals
  936. ;; Deal with Constants
  937. CALLI R15 @constant_load ; A is in R0 already
  938. JUMP @primary_expr_variable_done ; Moving on
  939. :primary_expr_variable_locals
  940. COPY R0 R2 ; Using S
  941. LOAD32 R1 R9 4 ; Using FUNCTION->LOCALS
  942. CALLI R15 @sym_lookup ; Lookup S in Locals
  943. JUMP.Z R0 @primary_expr_variable_arguments ; try arguments
  944. ;; Deal with Locals
  945. CALLI R15 @variable_load ; A is in R0 already
  946. JUMP @primary_expr_variable_done ; Moving on
  947. :primary_expr_variable_arguments
  948. COPY R0 R2 ; Using S
  949. LOAD32 R1 R9 16 ; Using FUNCTION->ARGUMENTS
  950. CALLI R15 @sym_lookup ; Lookup S in arguments
  951. JUMP.Z R0 @primary_expr_variable_function ; try Functions
  952. ;; Deal with argument
  953. CALLI R15 @variable_load ; A is in R0 already
  954. JUMP @primary_expr_variable_done ; Moving on
  955. :primary_expr_variable_function
  956. COPY R0 R2 ; Using S
  957. LOADR32 R1 @global_function_list ; Get current GLOBAL_FUNCTION_LIST
  958. CALLI R15 @sym_lookup ; Lookup S in GLOBAL_FUNCTION_LIST
  959. JUMP.Z R0 @primary_expr_variable_global ; try Globals
  960. ;; Deal with function
  961. CALLI R15 @function_load ; Dothe work
  962. JUMP @primary_expr_variable_done ; Moving on
  963. :primary_expr_variable_global
  964. COPY R0 R2 ; Using S
  965. LOADR32 R1 @global_symbol_list ; Get current GLOBAL_SYMBOL_LIST
  966. CALLI R15 @sym_lookup ; Lookup S in GLOBAL_SYMBOL_LIST
  967. JUMP.Z R0 @primary_expr_variable_failure ; Looks like it isn't anything we know
  968. ;; Deal with a global
  969. CALLI R15 @global_load
  970. :primary_expr_variable_done
  971. POPR R2 R15 ; Restore R2
  972. POPR R1 R15 ; Restore R1
  973. POPR R0 R15 ; Restore R0
  974. RET R15
  975. :primary_expr_variable_failure
  976. MOVE R0 R2 ; Using S
  977. FALSE R1 ; We want the user to see
  978. CALLI R15 @file_print ; Print it
  979. LOADUI R0 $primary_expr_variable_string0 ; Body
  980. CALLI R15 @file_print ; Print it
  981. CALLI R15 @line_error ; Provide useful error info
  982. HALT
  983. :primary_expr_variable_string0
  984. " is not a defined symbol
  985. "
  986. ;; promote_type function
  987. ;; Receives struct type* in R0 and struct type* in R1
  988. ;; Returns first match struct type* in R0
  989. :promote_type
  990. JUMP.Z R1 @promote_type_abort0 ; If B is NULL just abort
  991. PUSHR R1 R15 ; Protect R1
  992. SWAP R0 R1 ; Give A a try
  993. JUMP.Z R1 @promote_type_abort1 ; A is NULL just short abort
  994. ;; Looks like we have a bunch of work to do
  995. PUSHR R2 R15 ; Protect R2
  996. PUSHR R3 R15 ; Protect R3
  997. PUSHR R4 R15 ; Protect R4
  998. PUSHR R5 R15 ; Protect R5
  999. MOVE R5 R1 ; Put A in the right spot
  1000. MOVE R4 R0 ; Put B in the right spot
  1001. LOADR32 R3 @global_types ; I = GLOBAL_TYPES
  1002. :promote_type_iter
  1003. LOAD32 R1 R3 24 ; I->NAME
  1004. LOAD32 R0 R5 24 ; A->NAME
  1005. CMPJUMPI.E R0 R1 @promote_type_done ; break
  1006. LOAD32 R0 R4 24 ; B->NAME
  1007. CMPJUMPI.E R0 R1 @promote_type_done ; break
  1008. LOAD32 R1 R3 12 ; I->INDIRECT
  1009. LOAD32 R1 R1 24 ; I->INDIRECT->NAME
  1010. LOAD32 R0 R5 24 ; A->NAME
  1011. CMPJUMPI.E R0 R1 @promote_type_done ; break
  1012. LOAD32 R0 R4 24 ; B->NAME
  1013. CMPJUMPI.E R0 R1 @promote_type_done ; break
  1014. LOAD32 R3 R3 0 ; I = I->NEXT
  1015. JUMP.NZ R3 @promote_type_iter ; Loop if not NULL
  1016. :promote_type_done
  1017. MOVE R0 R3 ; Return I
  1018. POPR R5 R15 ; Restore R5
  1019. POPR R4 R15 ; Restore R4
  1020. POPR R3 R15 ; Restore R3
  1021. POPR R2 R15 ; Restore R2
  1022. :promote_type_abort1
  1023. POPR R1 R15 ; Restore R1
  1024. :promote_type_abort0
  1025. RET R15
  1026. ;; common_recursion function
  1027. ;; Receives FUNCTION* in R0
  1028. ;; struct token_list* out in R12,
  1029. ;; struct token_list* string_list in R11
  1030. ;; struct token_list* global_list in R10
  1031. ;; and struct token_list* FUNC in R9
  1032. ;; and struct token_list* current_target in R8
  1033. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1034. ;; Returns the token_lists modified
  1035. :common_recursion
  1036. PUSHR R0 R15 ; Protect R0
  1037. PUSHR R1 R15 ; Protect R1
  1038. PUSHR R2 R15 ; Protect R2
  1039. MOVE R2 R0 ; Protect F
  1040. COPY R1 R8 ; LAST_TYPE = CURRENT_TARGET
  1041. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1042. LOADUI R0 $common_recursion_string0 ; Header string
  1043. CALLI R15 @emit_out ; Our header
  1044. CALL R2 R15 ; CALL F()
  1045. COPY R0 R8 ; Using CURRENT_TARGET
  1046. CALLI R15 @promote_type ; Promote type
  1047. MOVE R8 R0 ; update CURRENT_TARGET
  1048. LOADUI R0 $common_recursion_string1 ; Footer string
  1049. CALLI R15 @emit_out ; Our footer
  1050. POPR R2 R15 ; Restore R2
  1051. POPR R1 R15 ; Restore R1
  1052. POPR R0 R15 ; Restore R0
  1053. RET R15
  1054. :common_recursion_string0
  1055. "{R0} PUSH_ALWAYS #_common_recursion
  1056. "
  1057. :common_recursion_string1
  1058. "{R1} POP_ALWAYS # _common_recursion
  1059. "
  1060. ;; general_recursion function
  1061. ;; Receives FUNCTION F in R0, char* s in R1, char* name in R2
  1062. ;; and FUNCTION ITERATE in R3
  1063. ;; struct token_list* out in R12,
  1064. ;; struct token_list* string_list in R11
  1065. ;; struct token_list* global_list in R10
  1066. ;; and struct token_list* FUNC in R9
  1067. ;; and struct token_list* current_target in R8
  1068. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1069. ;; Returns nothing
  1070. :general_recursion
  1071. PUSHR R0 R15 ; Protect R0
  1072. PUSHR R1 R15 ; Protect S
  1073. PUSHR R0 R15 ; Protect F
  1074. COPY R0 R2 ; Using NAME
  1075. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1076. CALLI R15 @match ; IF GLOBAL_TOKEN->S == NAME
  1077. JUMP.Z R0 @general_recursion_done
  1078. ;; deal with case of match
  1079. POPR R0 R15 ; Restore F
  1080. CALLI R15 @common_recursion ; Recurse
  1081. POPR R1 R15 ; Restore S
  1082. COPY R0 R1 ; Put S in correct place
  1083. CALLI R15 @emit_out ; emit it
  1084. CALL R3 R15 ; CALL ITERATE()
  1085. POPR R0 R15 ; Restore R0
  1086. RET R15 ; Don't double pop
  1087. :general_recursion_done
  1088. POPR R0 R15 ; Restore F
  1089. POPR R1 R15 ; Restore S
  1090. POPR R0 R15 ; Restore R0
  1091. RET R15
  1092. ;; ceil_log2 function
  1093. ;; Receives INT A in R0
  1094. ;; Returns LOG2(A) in R0
  1095. :ceil_log2
  1096. PUSHR R1 R15 ; Protect R1
  1097. PUSHR R2 R15 ; Protect R2
  1098. FALSE R2 ; RESULT = 0
  1099. SUBI R1 R0 1 ; A - 1
  1100. AND R1 R1 R0 ; A & (A - 1)
  1101. CMPSKIPI.NE R1 0 ; IF (A & (A - 1)) == 0
  1102. LOADI R2 -1 ; RESULT = -1
  1103. :ceil_log2_iter
  1104. JUMP.Z R0 @ceil_log2_done ; IF A > 0
  1105. ADDI R2 R2 1 ; RESULT = RESULT + 1
  1106. SARI R0 1 ; A = A >> 1
  1107. JUMP @ceil_log2_iter ; Loop
  1108. :ceil_log2_done
  1109. MOVE R0 R2 ; Use RESULT
  1110. SARI R0 1 ; RESULT = RESULT >> 1
  1111. POPR R2 R15 ; Restore R2
  1112. POPR R1 R15 ; Restore R1
  1113. RET R15
  1114. ;; postfix_expr_arrow function
  1115. ;; Receives nothing
  1116. ;; struct token_list* out in R12,
  1117. ;; struct token_list* string_list in R11
  1118. ;; struct token_list* global_list in R10
  1119. ;; and struct token_list* FUNC in R9
  1120. ;; and struct token_list* current_target in R8
  1121. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1122. ;; Returns the token_lists modified
  1123. :postfix_expr_arrow
  1124. PUSHR R0 R15 ; Protect R0
  1125. PUSHR R1 R15 ; Protect R1
  1126. PUSHR R2 R15 ; Protect R2
  1127. LOADUI R0 $postfix_expr_arrow_string0 ; Our header string
  1128. CALLI R15 @emit_out ; Emit it
  1129. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1130. COPY R0 R8 ; Passing CURRENT_TARGET
  1131. LOAD32 R1 R13 8 ; Using GLOBAL_TOKEN->S
  1132. CALLI R15 @lookup_member ; Look it up
  1133. LOAD32 R2 R0 4 ; Protect I->SIZE
  1134. LOAD32 R8 R0 20 ; CURRENT_TARGET = I->TYPE
  1135. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1136. LOAD32 R1 R0 8 ; I->OFFSET
  1137. JUMP.Z R1 @postfix_expr_arrow_offset ; If no offset needed skip the work
  1138. ;; Deal with non-zero offsets
  1139. LOADUI R0 $postfix_expr_arrow_string1 ; Our first prefix
  1140. CALLI R15 @emit_out ; Emit it
  1141. LOADUI R0 $postfix_expr_arrow_string2 ; Our second prefix
  1142. CALLI R15 @emit_out ; Emit it
  1143. MOVE R0 R1 ; Put I->OFFSET in the right place
  1144. CALLI R15 @numerate_number ; Convert to string
  1145. CALLI R15 @emit_out ; Emit it
  1146. LOADUI R0 $postfix_expr_arrow_string3 ; Our postfix
  1147. CALLI R15 @emit_out ; Emit it
  1148. :postfix_expr_arrow_offset
  1149. LOADUI R0 $equal ; Using "="
  1150. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1151. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  1152. JUMP.NZ R0 @postfix_expr_arrow_done
  1153. LOADUI R0 4 ; Compare against 4
  1154. CMPJUMPI.L R0 R2 @postfix_expr_arrow_done
  1155. ;; Deal with special case
  1156. LOADUI R0 $postfix_expr_arrow_string4 ; Our final string
  1157. CALLI R15 @emit_out ; Emit it
  1158. :postfix_expr_arrow_done
  1159. POPR R2 R15 ; Restore R2
  1160. POPR R1 R15 ; Restore R1
  1161. POPR R0 R15 ; Restore R0
  1162. RET R15
  1163. :postfix_expr_arrow_string0
  1164. "# looking up offset
  1165. "
  1166. :postfix_expr_arrow_string1
  1167. "# -> offset calculation
  1168. "
  1169. :postfix_expr_arrow_string2
  1170. "!0 R1 LOAD32 R15 MEMORY
  1171. ~0 JUMP_ALWAYS
  1172. %"
  1173. :postfix_expr_arrow_string3
  1174. "
  1175. '0' R0 R0 ADD R1 ARITH2_ALWAYS
  1176. "
  1177. :postfix_expr_arrow_string4
  1178. "!0 R0 LOAD32 R0 MEMORY
  1179. "
  1180. ;; postfix_expr_array function
  1181. ;; Receives nothing
  1182. ;; struct token_list* out in R12,
  1183. ;; struct token_list* string_list in R11
  1184. ;; struct token_list* global_list in R10
  1185. ;; and struct token_list* FUNC in R9
  1186. ;; and struct token_list* current_target in R8
  1187. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1188. ;; Returns the token_lists modified
  1189. :postfix_expr_array
  1190. PUSHR R0 R15 ; Protect R0
  1191. PUSHR R1 R15 ; Protect R1
  1192. PUSHR R2 R15 ; Protect R2
  1193. COPY R2 R8 ; ARRAY = CURRENT_TARGET
  1194. LOADUI R0 $expression ; Using EXPRESSION
  1195. CALLI R15 @common_recursion ; Recurse
  1196. MOVE R8 R2 ; CURRENT_TARGET = ARRAY
  1197. LOADUI R2 $postfix_expr_array_string0 ; ASSIGN = load integer
  1198. LOADUI R0 $type_char_indirect_name ; Using "char*"
  1199. LOAD32 R1 R8 24 ; CURRENT_TARGET->NAME
  1200. CALLI R15 @match ; IF CURRENT_TARGET->NAME == "char*"
  1201. CMPSKIPI.E R0 0 ; deal with Byte
  1202. LOADUI R2 $postfix_expr_array_string1 ; ASSIGN = load byte
  1203. JUMP.NZ R0 @postfix_expr_array_byte ; Skip if Byte
  1204. ;; Deal with larger than byte
  1205. LOADUI R0 $postfix_expr_array_string2 ; Our shift
  1206. CALLI R15 @emit_out ; emit it
  1207. LOAD32 R0 R8 12 ; CURRENT_TARGET->INDIRECT
  1208. LOAD32 R0 R0 4 ; CURRENT_TARGET->INDIRECT->SIZE
  1209. CALLI R15 @ceil_log2 ; LOG2(CURRENT_TARGET->INDIRECT->SIZE)
  1210. CALLI R15 @numerate_number ; Convert to string
  1211. CALLI R15 @emit_out ; emit it
  1212. LOADUI R0 $postfix_expr_array_string6 ; Tail of shift
  1213. CALLI R15 @emit_out ; emit it
  1214. :postfix_expr_array_byte
  1215. LOADUI R0 $postfix_expr_array_string3 ; Add the offset
  1216. CALLI R15 @emit_out ; emit it
  1217. LOADUI R0 $postfix_expr_array_string4 ; Our final error message
  1218. LOADUI R1 $close_bracket ; Using "]"
  1219. CALLI R15 @require_match ; Ensure match
  1220. LOADUI R0 $equal ; Using "="
  1221. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1222. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  1223. CMPSKIPI.E R0 0 ; If match
  1224. LOADUI R2 $postfix_expr_array_string5 ; empty string
  1225. MOVE R0 R2 ; What ever string survived
  1226. CALLI R15 @emit_out ; emit it
  1227. POPR R2 R15 ; Restore R2
  1228. POPR R1 R15 ; Restore R1
  1229. POPR R0 R15 ; Restore R0
  1230. RET R15
  1231. :postfix_expr_array_string0
  1232. "!0 R0 LOAD32 R0 MEMORY
  1233. "
  1234. :postfix_expr_array_string1
  1235. "!0 R0 LOAD8 R0 MEMORY
  1236. "
  1237. :postfix_expr_array_string2
  1238. "'0' R0 R0 '"
  1239. :postfix_expr_array_string3
  1240. "'0' R0 R0 ADD R1 ARITH2_ALWAYS
  1241. "
  1242. :postfix_expr_array_string4
  1243. "ERROR in postfix_expr
  1244. Missing ]
  1245. "
  1246. :postfix_expr_array_string5
  1247. ""
  1248. :postfix_expr_array_string6
  1249. "' MOVE_ALWAYS
  1250. "
  1251. ;; postfix_expr_stub function
  1252. ;; Receives nothing
  1253. ;; struct token_list* out in R12,
  1254. ;; struct token_list* string_list in R11
  1255. ;; struct token_list* global_list in R10
  1256. ;; and struct token_list* FUNC in R9
  1257. ;; and struct token_list* current_target in R8
  1258. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1259. ;; Returns the token_lists modified
  1260. :postfix_expr_stub
  1261. PUSHR R0 R15 ; Protect R0
  1262. PUSHR R1 R15 ; Protect R1
  1263. LOADUI R0 $open_bracket ; Using "["
  1264. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1265. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "["
  1266. JUMP.Z R0 @postfix_expr_stub_next
  1267. ;; Deal with "[" case
  1268. CALLI R15 @postfix_expr_array ; process
  1269. CALLI R15 @postfix_expr_stub ; recurse
  1270. :postfix_expr_stub_next
  1271. LOADUI R0 $arrow_string ; Using "->"
  1272. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1273. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "->"
  1274. JUMP.Z R0 @postfix_expr_stub_done ; clean up
  1275. ;; Deal with "->" case
  1276. CALLI R15 @postfix_expr_arrow ; Process
  1277. CALLI R15 @postfix_expr_stub ; recurse
  1278. :postfix_expr_stub_done
  1279. POPR R1 R15 ; Restore R1
  1280. POPR R0 R15 ; Restore R0
  1281. RET R15
  1282. ;; postfix_expr function
  1283. ;; struct token_list* out in R12,
  1284. ;; struct token_list* string_list in R11
  1285. ;; struct token_list* global_list in R10
  1286. ;; and struct token_list* FUNC in R9
  1287. ;; and struct token_list* current_target in R8
  1288. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1289. ;; Returns the token_lists modified
  1290. :postfix_expr
  1291. CALLI R15 @primary_expr ; Walk up the tree
  1292. CALLI R15 @postfix_expr_stub ; Deal with nodes on this level
  1293. RET R15
  1294. ;; additive_expr_stub function
  1295. ;; receives nothing
  1296. ;; returns nothing
  1297. ;; Updates struct token_list*
  1298. :additive_expr_stub
  1299. PUSHR R0 R15 ; Protect R0
  1300. PUSHR R1 R15 ; Protect R1
  1301. PUSHR R2 R15 ; Protect R2
  1302. PUSHR R3 R15 ; Protect R3
  1303. ;; Fixed pieces
  1304. LOADUI R0 $postfix_expr ; Set First argument
  1305. LOADUI R3 $additive_expr_stub
  1306. ;; The + bit
  1307. LOADUI R1 $additive_expr_stub_string0 ; Our first operation
  1308. LOADUI R2 $plus_string ; Using "+"
  1309. CALLI R15 @general_recursion
  1310. ;; The - bit
  1311. LOADUI R1 $additive_expr_stub_string1 ; Our second operation
  1312. LOADUI R2 $minus_string ; Using "-"
  1313. CALLI R15 @general_recursion
  1314. ;; The * bit
  1315. LOADUI R1 $additive_expr_stub_string2 ; Our third operation
  1316. LOADUI R2 $multiply_string ; Using "*"
  1317. CALLI R15 @general_recursion
  1318. ;; The / bit
  1319. LOADUI R1 $additive_expr_stub_string3 ; Our fourth operation
  1320. LOADUI R2 $divide_string ; Using "/"
  1321. CALLI R15 @general_recursion
  1322. ;; The % bit
  1323. LOADUI R1 $additive_expr_stub_string4 ; Our fifth operation
  1324. LOADUI R2 $modulus_string ; Using "%"
  1325. CALLI R15 @general_recursion
  1326. ;; The << bit
  1327. LOADUI R1 $additive_expr_stub_string5 ; Our sixth operation
  1328. LOADUI R2 $left_shift_string ; Using "<<"
  1329. CALLI R15 @general_recursion
  1330. ;; The >> bit
  1331. LOADUI R1 $additive_expr_stub_string6 ; Our final operation
  1332. LOADUI R2 $right_shift_string ; Using ">>"
  1333. CALLI R15 @general_recursion
  1334. POPR R3 R15 ; Restore R3
  1335. POPR R2 R15 ; Restore R2
  1336. POPR R1 R15 ; Restore R1
  1337. POPR R0 R15 ; Restore R0
  1338. RET R15
  1339. :additive_expr_stub_string0
  1340. "'0' R0 R0 ADD R1 ARITH2_ALWAYS
  1341. "
  1342. :additive_expr_stub_string1
  1343. "'0' R0 R0 SUB R1 ARITH2_ALWAYS
  1344. "
  1345. :additive_expr_stub_string2
  1346. "'9' R0 '0' R1 MULS R0 ARITH2_ALWAYS
  1347. "
  1348. :additive_expr_stub_string3
  1349. "{LR} PUSH_ALWAYS
  1350. ^~divide CALL_ALWAYS
  1351. {LR} POP_ALWAYS
  1352. "
  1353. :additive_expr_stub_string4
  1354. "{LR} PUSH_ALWAYS
  1355. ^~modulus CALL_ALWAYS
  1356. {LR} POP_ALWAYS
  1357. "
  1358. :additive_expr_stub_string5
  1359. "LEFT R1 R0 R0 SHIFT AUX_ALWAYS
  1360. "
  1361. :additive_expr_stub_string6
  1362. "RIGHT R1 R0 R0 SHIFT AUX_ALWAYS
  1363. "
  1364. ;; additive_expr function
  1365. ;; Receives struct token_list* global_token in R13,
  1366. ;; struct token_list* out in R12,
  1367. ;; struct token_list* string_list in R11
  1368. ;; struct token_list* global_list in R10
  1369. ;; and struct token_list* FUNC in R9
  1370. ;; and struct token_list* current_target in R8
  1371. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1372. ;; Returns the token_lists modified
  1373. :additive_expr
  1374. CALLI R15 @postfix_expr ; Walk up the tree
  1375. CALLI R15 @additive_expr_stub ; Deal with nodes at this level
  1376. RET R15
  1377. ;; relational_expr_stub function
  1378. ;; receives nothing
  1379. ;; returns nothing
  1380. ;; Updates struct token_list*
  1381. :relational_expr_stub
  1382. PUSHR R0 R15 ; Protect R0
  1383. PUSHR R1 R15 ; Protect R1
  1384. PUSHR R2 R15 ; Protect R2
  1385. PUSHR R3 R15 ; Protect R3
  1386. ;; Fixed pieces
  1387. LOADUI R0 $additive_expr ; Set First argument
  1388. LOADUI R3 $relational_expr_stub
  1389. ;; The < bit
  1390. LOADUI R1 $relational_expr_stub_string0 ; Our first operation
  1391. LOADUI R2 $less_than_string ; Using "<"
  1392. CALLI R15 @general_recursion
  1393. ;; The <= bit
  1394. LOADUI R1 $relational_expr_stub_string1 ; Our second operation
  1395. LOADUI R2 $less_than_equal_string ; Using "<="
  1396. CALLI R15 @general_recursion
  1397. ;; The >= bit
  1398. LOADUI R1 $relational_expr_stub_string2 ; Our third operation
  1399. LOADUI R2 $greater_than_equal_string ; Using ">="
  1400. CALLI R15 @general_recursion
  1401. ;; The > bit
  1402. LOADUI R1 $relational_expr_stub_string3 ; Our fourth operation
  1403. LOADUI R2 $greater_than_string ; Using ">"
  1404. CALLI R15 @general_recursion
  1405. ;; The == bit
  1406. LOADUI R1 $relational_expr_stub_string4 ; Our fifth operation
  1407. LOADUI R2 $equal_to_string ; Using "=="
  1408. CALLI R15 @general_recursion
  1409. ;; The != bit
  1410. LOADUI R1 $relational_expr_stub_string5 ; Our final operation
  1411. LOADUI R2 $not_equal_string ; Using "!="
  1412. CALLI R15 @general_recursion
  1413. POPR R3 R15 ; Restore R3
  1414. POPR R2 R15 ; Restore R2
  1415. POPR R1 R15 ; Restore R1
  1416. POPR R0 R15 ; Restore R0
  1417. RET R15
  1418. :relational_expr_stub_string0
  1419. "'0' R0 CMP R1 AUX_ALWAYS
  1420. !0 R0 LOADI8_ALWAYS
  1421. !1 R0 LOADI8_L
  1422. "
  1423. :relational_expr_stub_string1
  1424. "'0' R0 CMP R1 AUX_ALWAYS
  1425. !0 R0 LOADI8_ALWAYS
  1426. !1 R0 LOADI8_LE
  1427. "
  1428. :relational_expr_stub_string2
  1429. "'0' R0 CMP R1 AUX_ALWAYS
  1430. !0 R0 LOADI8_ALWAYS
  1431. !1 R0 LOADI8_GE
  1432. "
  1433. :relational_expr_stub_string3
  1434. "'0' R0 CMP R1 AUX_ALWAYS
  1435. !0 R0 LOADI8_ALWAYS
  1436. !1 R0 LOADI8_G
  1437. "
  1438. :relational_expr_stub_string4
  1439. "'0' R0 CMP R1 AUX_ALWAYS
  1440. !0 R0 LOADI8_ALWAYS
  1441. !1 R0 LOADI8_EQUAL
  1442. "
  1443. :relational_expr_stub_string5
  1444. "'0' R0 CMP R1 AUX_ALWAYS
  1445. !0 R0 LOADI8_ALWAYS
  1446. !1 R0 LOADI8_NE
  1447. "
  1448. ;; relational_expr function
  1449. ;; Receives struct token_list* global_token in R13,
  1450. ;; struct token_list* out in R12,
  1451. ;; struct token_list* string_list in R11
  1452. ;; struct token_list* global_list in R10
  1453. ;; and struct token_list* FUNC in R9
  1454. ;; and struct token_list* current_target in R8
  1455. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1456. ;; Returns the token_lists modified
  1457. :relational_expr
  1458. CALLI R15 @additive_expr ; Walk up the tree
  1459. CALLI R15 @relational_expr_stub ; Deal with nodes at this level
  1460. RET R15
  1461. ;; relational_expr_stub function
  1462. ;; receives nothing
  1463. ;; returns nothing
  1464. ;; Updates struct token_list*
  1465. :bitwise_expr_stub
  1466. PUSHR R0 R15 ; Protect R0
  1467. PUSHR R1 R15 ; Protect R1
  1468. PUSHR R2 R15 ; Protect R2
  1469. PUSHR R3 R15 ; Protect R3
  1470. ;; Fixed pieces
  1471. LOADUI R0 $relational_expr ; Set First argument
  1472. LOADUI R3 $bitwise_expr_stub
  1473. ;; The & bit
  1474. LOADUI R1 $bitwise_expr_stub_string0 ; Our first operation
  1475. LOADUI R2 $bitwise_and ; Using "&"
  1476. CALLI R15 @general_recursion
  1477. ;; The && bit
  1478. LOADUI R1 $bitwise_expr_stub_string0 ; Our first operation
  1479. LOADUI R2 $logical_and ; Using "&&"
  1480. CALLI R15 @general_recursion
  1481. ;; The | bit
  1482. LOADUI R1 $bitwise_expr_stub_string1 ; Our second operation
  1483. LOADUI R2 $bitwise_or ; Using "|"
  1484. CALLI R15 @general_recursion
  1485. ;; The || bit
  1486. LOADUI R1 $bitwise_expr_stub_string1 ; Our second operation
  1487. LOADUI R2 $logical_or ; Using "||"
  1488. CALLI R15 @general_recursion
  1489. ;; The ^ bit
  1490. LOADUI R1 $bitwise_expr_stub_string2 ; Our second operation
  1491. LOADUI R2 $bitwise_xor ; Using "^"
  1492. CALLI R15 @general_recursion
  1493. POPR R3 R15 ; Restore R3
  1494. POPR R2 R15 ; Restore R2
  1495. POPR R1 R15 ; Restore R1
  1496. POPR R0 R15 ; Restore R0
  1497. RET R15
  1498. :bitwise_expr_stub_string0
  1499. "NO_SHIFT R0 R0 AND R1 ARITH2_ALWAYS
  1500. "
  1501. :bitwise_expr_stub_string1
  1502. "NO_SHIFT R0 R0 OR R1 AUX_ALWAYS
  1503. "
  1504. :bitwise_expr_stub_string2
  1505. "'0' R0 R0 XOR R1 ARITH2_ALWAYS
  1506. "
  1507. ;; bitwise_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. :bitwise_expr
  1517. CALLI R15 @relational_expr ; Walk up the tree
  1518. CALLI R15 @bitwise_expr_stub ; Deal with nodes at this level
  1519. RET R15
  1520. ;; primary_expr function
  1521. ;; Receives struct token_list* global_token in R13,
  1522. ;; struct token_list* out in R12,
  1523. ;; struct token_list* string_list in R11
  1524. ;; struct token_list* global_list in R10
  1525. ;; and struct token_list* FUNC in R9
  1526. ;; and struct token_list* current_target in R8
  1527. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1528. ;; Returns the token_lists modified
  1529. :primary_expr
  1530. PUSHR R0 R15 ; Protect R0
  1531. PUSHR R1 R15 ; Protect R1
  1532. LOADUI R0 $sizeof_string ; Load "sizeof"
  1533. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1534. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "sizeof"
  1535. JUMP.Z R0 @primary_expr_negate ; Guess not
  1536. ;; Deal with sizeof expression
  1537. CALLI R15 @unary_expr_sizeof ; Do real work
  1538. JUMP @primary_expr_done ; Wrap up
  1539. :primary_expr_negate
  1540. LOADU8 R0 R1 0 ; GLOBAL_TOKEN->S[0]
  1541. CMPSKIPI.E R0 45 ; IF GLOBAL_TOKEN->S[0] == '-'
  1542. JUMP @primary_expr_bang ; If not try '!'
  1543. ;; Deal with -a and -4 expressions
  1544. LOADUI R0 $primary_expr_str0 ; Load HEADER
  1545. CALLI R15 @emit_out ; emit it
  1546. LOADUI R0 $primary_expr ; Using PRIMARY_EXPR
  1547. CALLI R15 @common_recursion ; Recurse
  1548. LOADUI R0 $primary_expr_str1 ; add footer
  1549. CALLI R15 @emit_out ; emit it
  1550. JUMP @primary_expr_done ; Wrap up
  1551. :primary_expr_bang
  1552. CMPSKIPI.E R0 33 ; IF GLOBAL_TOKEN->S[0] == "!"
  1553. JUMP @primary_expr_nested ; If not try '('
  1554. ;; deal with !a expressions
  1555. LOADUI R0 $primary_expr_str2 ; Load HEADER
  1556. CALLI R15 @emit_out ; emit it
  1557. LOADUI R0 $postfix_expr ; Using POSTFIX_EXPR
  1558. CALLI R15 @common_recursion ; Recurse
  1559. LOADUI R0 $primary_expr_str3 ; add footer
  1560. CALLI R15 @emit_out ; emit it
  1561. JUMP @primary_expr_done ; Wrap up
  1562. :primary_expr_nested
  1563. CMPSKIPI.E R0 40 ; IF GLOBAL_TOKEN->S[0] == '('
  1564. JUMP @primary_expr_ch ; If not try 'char'
  1565. ;; Deal with ( expr )
  1566. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1567. CALLI R15 @expression ; Recurse
  1568. LOADUI R0 $primary_expr_str4 ; Using error message
  1569. LOADUI R1 $close_paren ; Using ")"
  1570. CALLI R15 @require_match ; Make sure we have closing match
  1571. JUMP @primary_expr_done ; Wrap up
  1572. :primary_expr_ch
  1573. CMPSKIPI.E R0 39 ; IF GLOBAL_TOKEN->S[0] == '\''
  1574. JUMP @primary_expr_st ; If not try "string"
  1575. ;; Deal with 'char'
  1576. CALLI R15 @primary_expr_char ; Collect char
  1577. JUMP @primary_expr_done ; Wrap up
  1578. :primary_expr_st
  1579. CMPSKIPI.E R0 34 ; IF GLOBAL_TOKEN->S[0] == '"'
  1580. JUMP @primary_expr_var ; If not try variables
  1581. ;; deal with "string"
  1582. CALLI R15 @primary_expr_string ; Collect string
  1583. JUMP @primary_expr_done ; Wrap up
  1584. :primary_expr_var
  1585. LOADUI R1 $variable_chars ; Using a-z+A-Z+_
  1586. CALLI R15 @in_set ; IF GLOBAL_TOKEN->S[0] in a-z+A-Z+_
  1587. JUMP.Z R0 @primary_expr_num
  1588. ;; Deal with foo TODO
  1589. CALLI R15 @primary_expr_variable ; deal with names
  1590. JUMP @primary_expr_done ; Wrap up
  1591. :primary_expr_num
  1592. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  1593. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  1594. LOADUI R1 $digit_chars ; Using 0-9
  1595. CALLI R15 @in_set ; IF GLOBAL_TOKEN->S[0] in 0-9
  1596. JUMP.Z R0 @primary_expr_failure ; Fail HARD
  1597. ;; Deal with 5
  1598. CALLI R15 @primary_expr_number ; deal with number
  1599. :primary_expr_done
  1600. POPR R1 R15 ; Restore R1
  1601. POPR R0 R15 ; Restore R0
  1602. RET R15
  1603. :primary_expr_str0
  1604. "!0 R0 LOADI8_ALWAYS
  1605. "
  1606. :primary_expr_str1
  1607. "'0' R0 R0 SUB R1 ARITH2_ALWAYS
  1608. "
  1609. :primary_expr_str2
  1610. "!1 R0 LOADI8_ALWAYS
  1611. "
  1612. :primary_expr_str3
  1613. "'0' R0 R0 XOR R1 ARITH2_ALWAYS
  1614. "
  1615. :primary_expr_str4
  1616. "Error in Primary expression
  1617. Didn't get )
  1618. "
  1619. ;; expression function
  1620. ;; Receives struct token_list* global_token in R13,
  1621. ;; struct token_list* out in R12,
  1622. ;; struct token_list* string_list in R11
  1623. ;; struct token_list* global_list in R10
  1624. ;; and struct token_list* FUNC in R9
  1625. ;; and struct token_list* current_target in R8
  1626. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1627. ;; Returns the token_lists modified
  1628. :expression
  1629. PUSHR R0 R15 ; Protect R0
  1630. PUSHR R1 R15 ; Protect R1
  1631. PUSHR R2 R15 ; Protect R2
  1632. PUSHR R3 R15 ; Protect R3
  1633. CALLI R15 @bitwise_expr ; Check for more primitives first
  1634. LOADUI R0 $equal ; Using "="
  1635. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1636. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  1637. JUMP.Z R0 @expression_done ; Be done
  1638. ;; Determine store type
  1639. LOADUI R3 $expression_string1 ; Assuming the default of STORE CHAR
  1640. ;; First possible reason for INT
  1641. LOADUI R0 $close_bracket ; Using "]"
  1642. LOAD32 R1 R13 4 ; GLOBAL_TOKEN->PREV
  1643. LOAD32 R1 R1 8 ; GLOBAL_TOKEN->PREV->S
  1644. CALLI R15 @match ; IF GLOBAL_TOKEN->PREV-> == "]"
  1645. CMPSKIPI.NE R0 0 ; IF FALSE
  1646. LOADUI R3 $expression_string0 ; STORE INTEGER
  1647. ;; Second possible reason for INTeger
  1648. LOADUI R0 $type_char_indirect_name ; Using "char*"
  1649. LOAD32 R1 R8 24 ; CURRENT_TARGET->NAME
  1650. CALLI R15 @match ; IF CURRENT_TARGET->NAME == "char*"
  1651. CMPSKIPI.NE R0 0 ; IF FALSE
  1652. LOADUI R3 $expression_string0 ; STORE INTEGER
  1653. ;; Recurse to evaluate expression being stored
  1654. LOADUI R0 $expression ; Using expression
  1655. CALLI R15 @common_recursion ; Perform common recursion
  1656. ;; Put our string and clean up
  1657. MOVE R0 R3 ; Using our STORED string
  1658. CALLI R15 @emit_out ; emit it
  1659. FALSE R8 ; CURRENT_TARGET = NULL
  1660. :expression_done
  1661. POPR R3 R15 ; Restore R3
  1662. POPR R2 R15 ; Restore R2
  1663. POPR R1 R15 ; Restore R1
  1664. POPR R0 R15 ; Restore R0
  1665. RET R15
  1666. :expression_string0
  1667. "!0 R0 STORE32 R1 MEMORY
  1668. "
  1669. :expression_string1
  1670. "!0 R0 STORE8 R1 MEMORY
  1671. "
  1672. ;; process_if function
  1673. ;; struct token_list* out in R12,
  1674. ;; struct token_list* string_list in R11
  1675. ;; struct token_list* global_list in R10
  1676. ;; and struct token_list* FUNC in R9
  1677. ;; and struct token_list* current_target in R8
  1678. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1679. ;; Returns the token_lists modified
  1680. :process_if
  1681. PUSHR R0 R15 ; Protect R0
  1682. PUSHR R1 R15 ; Protect R1
  1683. PUSHR R2 R15 ; Protect R2
  1684. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  1685. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  1686. STORER32 R1 @current_count ; Update CURRENT_COUNT
  1687. CALLI R15 @numerate_number ; Convert CURRENT_COUNT to string
  1688. MOVE R2 R0 ; Protect our string
  1689. LOADUI R0 $process_if_string0 ; using first string
  1690. CALLI R15 @emit_out ; emit it
  1691. LOAD32 R0 R9 8 ; FUNCTION->S
  1692. COPY R1 R2 ; Using our current count string
  1693. CALLI R15 @uniqueID_out ; Add unique identifier
  1694. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1695. LOADUI R0 $process_if_string1 ; Our first error message
  1696. LOADUI R1 $open_paren ; Using "("
  1697. CALLI R15 @require_match ; Make sure we have what we need
  1698. CALLI R15 @expression ; Recurse to get our boolean expression
  1699. LOADUI R0 $process_if_string2 ; Our test and jump
  1700. CALLI R15 @emit_out ; emit it
  1701. LOAD32 R0 R9 8 ; FUNCTION->S
  1702. COPY R1 R2 ; Using our current count string
  1703. CALLI R15 @uniqueID_out ; Add unique identifier
  1704. LOADUI R0 $process_if_string7 ; Our test and jump
  1705. CALLI R15 @emit_out ; emit it
  1706. LOADUI R0 $process_if_string3 ; Our second error message
  1707. LOADUI R1 $close_paren ; Using ")"
  1708. CALLI R15 @require_match ; Make sure we have what we need
  1709. CALLI R15 @statement ; Collect our if statement
  1710. LOADUI R0 $process_if_string4 ; Our jump over else
  1711. CALLI R15 @emit_out ; emit it
  1712. LOAD32 R0 R9 8 ; FUNCTION->S
  1713. COPY R1 R2 ; Using our current count string
  1714. CALLI R15 @uniqueID_out ; Add unique identifier
  1715. LOADUI R0 $process_if_string5 ; Our else label
  1716. CALLI R15 @emit_out ; emit it
  1717. LOAD32 R0 R9 8 ; FUNCTION->S
  1718. CALLI R15 @uniqueID_out ; Add unique identifier
  1719. LOADUI R0 $else_string ; Using "else"
  1720. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1721. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "else"
  1722. JUMP.Z R0 @process_if_else ; Looks like no else
  1723. ;; Deal with else
  1724. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1725. CALLI R15 @statement ; Grab else statement
  1726. :process_if_else
  1727. LOADUI R0 $process_if_string6 ; Our jump over else
  1728. CALLI R15 @emit_out ; emit it
  1729. LOAD32 R0 R9 8 ; FUNCTION->S
  1730. COPY R1 R2 ; Using our current count string
  1731. CALLI R15 @uniqueID_out ; Add unique identifier
  1732. POPR R2 R15 ; Restore R2
  1733. POPR R1 R15 ; Restore R1
  1734. POPR R0 R15 ; Restore R0
  1735. RET R15
  1736. :process_if_string0
  1737. "# IF_"
  1738. :process_if_string1
  1739. "ERROR in process_if
  1740. MISSING (
  1741. "
  1742. :process_if_string2
  1743. "!0 CMPI8 R0 IMM_ALWAYS
  1744. ^~ELSE_"
  1745. :process_if_string3
  1746. "ERROR in process_if
  1747. MISSING )
  1748. "
  1749. :process_if_string4
  1750. "^~_END_IF_"
  1751. :process_if_string5
  1752. " JUMP_ALWAYS
  1753. :ELSE_"
  1754. :process_if_string6
  1755. ":_END_IF_"
  1756. :process_if_string7
  1757. " JUMP_EQUAL
  1758. "
  1759. ;; save_break_frame microfunction
  1760. ;; Overwrites R0 and R1
  1761. ;; Saves break frame on stack
  1762. ;; Returns to caller
  1763. :save_break_frame
  1764. POPR R1 R15 ; Save return address
  1765. LOADR32 R0 @break_frame ; Obtain BREAK_FRAME
  1766. PUSHR R0 R15 ; Protect BREAK_FRAME
  1767. LOADR32 R0 @break_target_head ; obtain HEAD
  1768. PUSHR R0 R15 ; Protect HEAD
  1769. LOADR32 R0 @break_target_func ; obtain FUNC
  1770. PUSHR R0 R15 ; Protect FUNC
  1771. LOADR32 R0 @break_target_num ; obtain NUM
  1772. PUSHR R0 R15 ; Protect NUM
  1773. PUSHR R1 R15 ; Set where we are returning to
  1774. RET R15
  1775. ;; restore_break_frame microfunction
  1776. ;; Overwrites R0 and R1
  1777. ;; Restores break frame from stack
  1778. ;; Returns to caller
  1779. :restore_break_frame
  1780. POPR R1 R15 ; Save return address
  1781. POPR R0 R15 ; obtain NUM
  1782. STORER32 R0 @break_target_num ; Restore NUM
  1783. POPR R0 R15 ; obtain FUNC
  1784. STORER32 R0 @break_target_func ; Restore FUNC
  1785. POPR R0 R15 ; obtain HEAD
  1786. STORER32 R0 @break_target_head ; Restore HEAD
  1787. POPR R0 R15 ; obtain BREAK_FRAME
  1788. STORER32 R0 @break_frame ; Restore BREAK_FRAME
  1789. PUSHR R1 R15 ; Set where we are returning to
  1790. RET R15
  1791. ;; set_break_frame microfunction
  1792. ;; Receives char* num in R0, char* head in R1
  1793. ;; Overwrites R0
  1794. ;; Sets break frame using
  1795. ;; R9 holding FUNC
  1796. ;; Returns to calling function
  1797. :set_break_frame
  1798. STORER32 R1 @break_target_head ; update BREAK_TARGET_HEAD
  1799. STORER32 R0 @break_target_num ; Update BREAK_TARGET_NUM
  1800. LOAD32 R0 R9 4 ; Using FUNCTION->LOCALS
  1801. STORER32 R0 @break_frame ; update BREAK_FRAME
  1802. LOAD32 R0 R9 8 ; Using FUNCTION->S
  1803. STORER32 R0 @break_target_func ; update BREAK_TARGET_FUNC
  1804. RET R15
  1805. ;; process_for function
  1806. ;; struct token_list* out in R12,
  1807. ;; struct token_list* string_list in R11
  1808. ;; struct token_list* global_list in R10
  1809. ;; and struct token_list* FUNC in R9
  1810. ;; and struct token_list* current_target in R8
  1811. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1812. ;; Returns the token_lists modified
  1813. :process_for
  1814. PUSHR R2 R15 ; Protect R2
  1815. PUSHR R1 R15 ; Protect R1
  1816. PUSHR R0 R15 ; Protect R0
  1817. CALLI R15 @save_break_frame ; Save break frame
  1818. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  1819. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  1820. STORER32 R1 @current_count ; Update CURRENT_COUNT
  1821. CALLI R15 @numerate_number ; Convert to string
  1822. COPY R2 R0 ; Protect NUMBER_STRING
  1823. LOADUI R1 $process_for_string0 ; Get new HEAD
  1824. CALLI R15 @set_break_frame ; Set the break frame values
  1825. LOADUI R0 $process_for_string1 ; Our comment header
  1826. CALLI R15 @emit_out ; emit it
  1827. COPY R1 R2 ; Using NUMBER_STRING
  1828. LOAD32 R0 R9 8 ; FUNCTION->S
  1829. CALLI R15 @uniqueID_out ; emit it
  1830. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1831. LOADUI R0 $process_for_string2 ; Our first error message
  1832. LOADUI R1 $open_paren ; Using "("
  1833. CALLI R15 @require_match ; Verify match
  1834. LOADUI R0 $semicolon ; Using ";"
  1835. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  1836. CALLI R15 @match ; IF GLOBAL_TOKEN->S -- ";"
  1837. CMPSKIPI.NE R0 0 ; If GLOBAL_TOKEN->S != ";"
  1838. CALLI R15 @expression ; Skip that step
  1839. LOADUI R0 $process_for_string3 ; Our comment header
  1840. CALLI R15 @emit_out ; emit it
  1841. COPY R1 R2 ; Using NUMBER_STRING
  1842. LOAD32 R0 R9 8 ; FUNCTION->S
  1843. CALLI R15 @uniqueID_out ; emit it
  1844. LOADUI R0 $process_for_string4 ; Our second error message
  1845. LOADUI R1 $semicolon ; Using ";"
  1846. CALLI R15 @require_match ; Verify match
  1847. CALLI R15 @expression ; TEST logic required
  1848. LOADUI R0 $process_for_string5 ; Our comment header
  1849. CALLI R15 @emit_out ; emit it
  1850. COPY R1 R2 ; Using NUMBER_STRING
  1851. LOAD32 R0 R9 8 ; FUNCTION->S
  1852. CALLI R15 @uniqueID_out ; emit it
  1853. LOADUI R0 $process_for_string6 ; Our comment header
  1854. CALLI R15 @emit_out ; emit it
  1855. LOAD32 R0 R9 8 ; FUNCTION->S
  1856. CALLI R15 @uniqueID_out ; emit it
  1857. LOADUI R0 $process_for_string7 ; Our comment header
  1858. CALLI R15 @emit_out ; emit it
  1859. LOAD32 R0 R9 8 ; FUNCTION->S
  1860. CALLI R15 @uniqueID_out ; emit it
  1861. LOADUI R0 $process_for_string8 ; Our third error message
  1862. LOADUI R1 $semicolon ; Using ";"
  1863. CALLI R15 @require_match ; Verify match
  1864. CALLI R15 @expression ; Iterator logic
  1865. LOADUI R0 $process_for_string9 ; Our comment header
  1866. CALLI R15 @emit_out ; emit it
  1867. COPY R1 R2 ; Using NUMBER_STRING
  1868. LOAD32 R0 R9 8 ; FUNCTION->S
  1869. CALLI R15 @uniqueID_out ; emit it
  1870. LOADUI R0 $process_for_string10 ; Our comment header
  1871. CALLI R15 @emit_out ; emit it
  1872. LOAD32 R0 R9 8 ; FUNCTION->S
  1873. CALLI R15 @uniqueID_out ; emit it
  1874. LOADUI R0 $process_for_string11 ; Our final error message
  1875. LOADUI R1 $close_paren ; Using ")"
  1876. CALLI R15 @require_match ; Verify match
  1877. CALLI R15 @statement ; Main body
  1878. LOADUI R0 $process_for_string12 ; Our comment header
  1879. CALLI R15 @emit_out ; emit it
  1880. COPY R1 R2 ; Using NUMBER_STRING
  1881. LOAD32 R0 R9 8 ; FUNCTION->S
  1882. CALLI R15 @uniqueID_out ; emit it
  1883. LOADUI R0 $process_for_string13 ; Our comment header
  1884. CALLI R15 @emit_out ; emit it
  1885. LOAD32 R0 R9 8 ; FUNCTION->S
  1886. CALLI R15 @uniqueID_out ; emit it
  1887. CALLI R15 @restore_break_frame ; Restore break frame
  1888. POPR R0 R15 ; Restore R0
  1889. POPR R1 R15 ; Restore R1
  1890. POPR R2 R15 ; Restore R2
  1891. RET R15
  1892. :process_for_string0
  1893. "FOR_END_"
  1894. :process_for_string1
  1895. "# FOR_initialization_"
  1896. :process_for_string2
  1897. "ERROR in process_for
  1898. MISSING (
  1899. "
  1900. :process_for_string3
  1901. ":FOR_"
  1902. :process_for_string4
  1903. "ERROR in process_for
  1904. MISSING ;1
  1905. "
  1906. :process_for_string5
  1907. "!0 CMPI8 R0 IMM_ALWAYS
  1908. ^~FOR_END_"
  1909. :process_for_string6
  1910. " JUMP_EQUAL
  1911. ^~FOR_THEN_"
  1912. :process_for_string7
  1913. " JUMP_ALWAYS
  1914. :FOR_ITER_"
  1915. :process_for_string8
  1916. "ERROR in process_for
  1917. MISSING ;2
  1918. "
  1919. :process_for_string9
  1920. "^~FOR_"
  1921. :process_for_string10
  1922. " JUMP_ALWAYS
  1923. :FOR_THEN_"
  1924. :process_for_string11
  1925. "ERROR in process_for
  1926. MISSING )
  1927. "
  1928. :process_for_string12
  1929. "^~FOR_ITER_"
  1930. :process_for_string13
  1931. " JUMP_ALWAYS
  1932. :FOR_END_"
  1933. ;; process_do function
  1934. ;; struct token_list* out in R12,
  1935. ;; struct token_list* string_list in R11
  1936. ;; struct token_list* global_list in R10
  1937. ;; and struct token_list* FUNC in R9
  1938. ;; and struct token_list* current_target in R8
  1939. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  1940. ;; Returns the token_lists modified
  1941. :process_do
  1942. PUSHR R2 R15 ; Protect R2
  1943. PUSHR R1 R15 ; Protect R1
  1944. PUSHR R0 R15 ; Protect R0
  1945. CALLI R15 @save_break_frame ; Save break frame
  1946. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  1947. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  1948. STORER32 R1 @current_count ; Update CURRENT_COUNT
  1949. CALLI R15 @numerate_number ; Convert to string
  1950. COPY R2 R0 ; Protect NUMBER_STRING
  1951. LOADUI R1 $process_do_string0 ; Using our desired head
  1952. CALLI R15 @set_break_frame ; Set the break frame values
  1953. LOADUI R0 $process_do_string1 ; Our label
  1954. CALLI R15 @emit_out ; emit it
  1955. COPY R1 R2 ; Using NUMBER_STRING
  1956. LOAD32 R0 R9 8 ; FUNCTION->S
  1957. CALLI R15 @uniqueID_out ; emit it
  1958. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  1959. CALLI R15 @statement ; Collect our Do statement
  1960. LOADUI R0 $process_do_string2 ; our first error message
  1961. LOADUI R1 $while_string ; Using "while"
  1962. CALLI R15 @require_match ; Check for match
  1963. LOADUI R0 $process_do_string3 ; our second error message
  1964. LOADUI R1 $open_paren ; Using "("
  1965. CALLI R15 @require_match ; Check for match
  1966. CALLI R15 @expression ; Our logical expression
  1967. LOADUI R0 $process_do_string4 ; our third error message
  1968. LOADUI R1 $close_paren ; Using ")"
  1969. CALLI R15 @require_match ; Check for match
  1970. LOADUI R0 $process_do_string5 ; our final error message
  1971. LOADUI R1 $semicolon ; Using ";"
  1972. CALLI R15 @require_match ; Check for match
  1973. LOADUI R0 $process_do_string6 ; Our test string
  1974. CALLI R15 @emit_out ; emit it
  1975. COPY R1 R2 ; Put NUMBER_STRING in right place
  1976. LOAD32 R0 R9 8 ; FUNCTION->S
  1977. CALLI R15 @uniqueID_out ; emit it
  1978. LOADUI R0 $process_do_string7 ; Our end label string
  1979. CALLI R15 @emit_out ; emit it
  1980. LOAD32 R0 R9 8 ; FUNCTION->S
  1981. CALLI R15 @uniqueID_out ; emit it
  1982. CALLI R15 @restore_break_frame ; Restore break frame
  1983. POPR R0 R15 ; Restore R0
  1984. POPR R1 R15 ; Restore R1
  1985. POPR R2 R15 ; Restore R2
  1986. RET R15
  1987. :process_do_string0
  1988. "DO_END_"
  1989. :process_do_string1
  1990. ":DO_"
  1991. :process_do_string2
  1992. "ERROR in process_do
  1993. MISSING while
  1994. "
  1995. :process_do_string3
  1996. "ERROR in process_do
  1997. MISSING (
  1998. "
  1999. :process_do_string4
  2000. "ERROR in process_do
  2001. MISSING )
  2002. "
  2003. :process_do_string5
  2004. "ERROR in process_do
  2005. MISSING ;
  2006. "
  2007. :process_do_string6
  2008. "!0 CMPI8 R0 IMM_ALWAYS
  2009. ^~DO_"
  2010. :process_do_string7
  2011. " JUMP_NE
  2012. :DO_END_"
  2013. ;; process_while function
  2014. ;; struct token_list* out in R12,
  2015. ;; struct token_list* string_list in R11
  2016. ;; struct token_list* global_list in R10
  2017. ;; and struct token_list* FUNC in R9
  2018. ;; and struct token_list* current_target in R8
  2019. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2020. ;; Returns the token_lists modified
  2021. :process_while
  2022. PUSHR R2 R15 ; Protect R2
  2023. PUSHR R1 R15 ; Protect R1
  2024. PUSHR R0 R15 ; Protect R0
  2025. CALLI R15 @save_break_frame ; Save break frame
  2026. LOADR32 R0 @current_count ; Using CURRENT_COUNT
  2027. ADDUI R1 R0 1 ; CURRENT_COUNT = CURRENT_COUNT + 1
  2028. STORER32 R1 @current_count ; Update CURRENT_COUNT
  2029. CALLI R15 @numerate_number ; Convert to string
  2030. COPY R2 R0 ; Protect NUMBER_STRING
  2031. LOADUI R1 $process_while_string0 ; Set HEAD
  2032. CALLI R15 @set_break_frame ; Set the break frame values
  2033. LOADUI R0 $process_while_string1 ; Our head label
  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. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2039. LOADUI R0 $process_while_string2 ; Our first error message
  2040. LOADUI R1 $open_paren ; Using "("
  2041. CALLI R15 @require_match ; Check for match
  2042. CALLI R15 @expression ; Collect test expression
  2043. LOADUI R0 $process_while_string3 ; Our test and jump
  2044. CALLI R15 @emit_out ; emit it
  2045. COPY R1 R2 ; Using NUMBER_STRING
  2046. LOAD32 R0 R9 8 ; FUNCTION->S
  2047. CALLI R15 @uniqueID_out ; emit it
  2048. LOADUI R0 $process_while_string4 ; Our trailing comment
  2049. CALLI R15 @emit_out ; emit it
  2050. LOAD32 R0 R9 8 ; FUNCTION->S
  2051. CALLI R15 @uniqueID_out ; emit it
  2052. LOADUI R0 $process_while_string5 ; Our first error message
  2053. LOADUI R1 $close_paren ; Using ")"
  2054. CALLI R15 @require_match ; Check for match
  2055. CALLI R15 @statement ; Collect our loop statement
  2056. LOADUI R0 $process_while_string6 ; Our test and jump
  2057. CALLI R15 @emit_out ; emit it
  2058. COPY R1 R2 ; Using NUMBER_STRING
  2059. LOAD32 R0 R9 8 ; FUNCTION->S
  2060. CALLI R15 @uniqueID_out ; emit it
  2061. LOADUI R0 $process_while_string7 ; Our trailing comment
  2062. CALLI R15 @emit_out ; emit it
  2063. LOAD32 R0 R9 8 ; FUNCTION->S
  2064. CALLI R15 @uniqueID_out ; emit it
  2065. CALLI R15 @restore_break_frame ; Restore break frame
  2066. POPR R0 R15 ; Restore R0
  2067. POPR R1 R15 ; Restore R1
  2068. POPR R2 R15 ; Restore R2
  2069. RET R15
  2070. :process_while_string0
  2071. "END_WHILE_"
  2072. :process_while_string1
  2073. ":WHILE_"
  2074. :process_while_string2
  2075. "ERROR in process_while
  2076. MISSING (
  2077. "
  2078. :process_while_string3
  2079. "!0 CMPI8 R0 IMM_ALWAYS
  2080. ^~END_WHILE_"
  2081. :process_while_string4
  2082. " JUMP_EQUAL # THEN_while_"
  2083. :process_while_string5
  2084. "ERROR in process_while
  2085. MISSING )
  2086. "
  2087. :process_while_string6
  2088. "^~WHILE_"
  2089. :process_while_string7
  2090. " JUMP_ALWAYS
  2091. :END_WHILE_"
  2092. ;; return_result function
  2093. ;; Receives nothing
  2094. ;; Returns nothing
  2095. ;; and struct token_list* FUNC in R9
  2096. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2097. :return_result
  2098. PUSHR R0 R15 ; Protect R0
  2099. PUSHR R1 R15 ; Protect R1
  2100. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2101. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2102. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2103. CMPSKIPI.E R0 59 ; IF GLOBAL_TOKEN->S[0] == ';'
  2104. CALLI R15 @expression ; Evaluate expression
  2105. LOADUI R0 $return_result_string0 ; Using or error message
  2106. LOADUI R1 $semicolon ; Using ";"
  2107. CALLI R15 @require_match ; Require a match to ";"
  2108. LOADUI R0 $return_result_string1 ; Our pop command
  2109. LOAD32 R1 R9 4 ; FUNCTION->LOCALS
  2110. :return_result_iter
  2111. JUMP.Z R1 @return_result_done ; Be done when we hit NULL
  2112. CALLI R15 @emit_out ; Put the string every iteration
  2113. LOAD32 R1 R1 0 ; I = I->NEXT
  2114. JUMP @return_result_iter ; Keep looping
  2115. :return_result_done
  2116. LOADUI R0 $return_result_string2 ; Our footer
  2117. CALLI R15 @emit_out ; emit it
  2118. POPR R1 R15 ; Restore R1
  2119. POPR R0 R15 ; Restore R0
  2120. RET R15
  2121. :return_result_string0
  2122. "ERROR in return_result
  2123. MISSING ;
  2124. "
  2125. :return_result_string1
  2126. "{R1} POP_ALWAYS # _return_result_locals
  2127. "
  2128. :return_result_string2
  2129. "'1' LR RETURN
  2130. "
  2131. ;; process_break function
  2132. ;; Receives nothing
  2133. ;; and struct token_list* FUNC in R9
  2134. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2135. ;; Returns the token_lists modified
  2136. :process_break
  2137. PUSHR R0 R15 ; Protect R0
  2138. PUSHR R1 R15 ; Protect R1
  2139. PUSHR R2 R15 ; Protect R2
  2140. LOADR32 R0 @break_target_head ; BREAK_TARGET_HEAD
  2141. JUMP.NZ R0 @process_break_NON_NULL
  2142. ;; Deal with NULL == BREAK_TARGET_HEAD
  2143. LOADUI R0 $process_break_string0 ; Our first error message
  2144. FALSE R1 ; Write for User
  2145. CALLI R15 @file_print ; write it
  2146. CALLI R15 @line_error ; Give useful info
  2147. LOADUI R0 $newline ; Using "\n"
  2148. CALLI R15 @file_print ; Print it
  2149. HALT
  2150. :process_break_NON_NULL
  2151. LOADR32 R2 @break_frame ; BREAK_FRAME
  2152. LOAD32 R1 R9 4 ; I = FUNCTION->LOCALS
  2153. LOADUI R0 $process_break_string1 ; Our pop string
  2154. :process_break_iter
  2155. CMPJUMPI.E R1 R2 @process_break_done ; IF I == BREAK_FRAME
  2156. JUMP.Z R1 @process_break_done ; IF NULL == I break
  2157. CALLI R15 @emit_out ; emit pop
  2158. LOAD32 R1 R1 0 ; I = I->NEXT
  2159. JUMP @process_break_iter ; Loop
  2160. :process_break_done
  2161. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2162. LOADUI R0 $process_break_string2 ; Our jump string
  2163. CALLI R15 @emit_out ; emit it
  2164. LOADR32 R0 @break_target_head ; Our HEAD string
  2165. CALLI R15 @emit_out ; emit it
  2166. LOADR32 R0 @break_target_func ; Our FUNC string
  2167. CALLI R15 @emit_out ; emit it
  2168. LOADUI R0 $underline ; Using "_"
  2169. CALLI R15 @emit_out ; emit it
  2170. LOADR32 R0 @break_target_num ; Our NUM string
  2171. CALLI R15 @emit_out ; emit it
  2172. LOADUI R0 $process_break_string4 ; Using final jump string
  2173. CALLI R15 @emit_out ; emit it
  2174. LOADUI R0 $process_break_string3 ; Our final error string
  2175. LOADUI R1 $semicolon ; Using ";"
  2176. CALLI R15 @require_match ; Make sure we get that match
  2177. POPR R2 R15 ; Restore R2
  2178. POPR R1 R15 ; Restore R1
  2179. POPR R0 R15 ; Restore R0
  2180. RET R15
  2181. :process_break_string0
  2182. "Not inside of a loop or case statement
  2183. "
  2184. :process_break_string1
  2185. "{R1} POP_ALWAYS # break_cleanup_locals
  2186. "
  2187. :process_break_string2
  2188. "^~"
  2189. :process_break_string3
  2190. "ERROR in break statement
  2191. Missing ;
  2192. "
  2193. :process_break_string4
  2194. " JUMP_ALWAYS
  2195. "
  2196. :break_frame
  2197. NOP
  2198. :break_target_head
  2199. NOP
  2200. :break_target_func
  2201. NOP
  2202. :break_target_num
  2203. NOP
  2204. ;; process_asm function
  2205. ;; Receives struct token_list* global_token in R13,
  2206. ;; struct token_list* out in R12,
  2207. ;; struct token_list* string_list in R11
  2208. ;; struct token_list* global_list in R10
  2209. ;; and struct token_list* FUNC in R9
  2210. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2211. ;; Returns the token_lists modified
  2212. :process_asm
  2213. PUSHR R0 R15 ; Protect R0
  2214. PUSHR R1 R15 ; Protect R1
  2215. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2216. ;; First required match
  2217. LOADUI R0 $process_asm_string0 ; Using our First error message
  2218. LOADUI R1 $open_paren ; Using "("
  2219. CALLI R15 @require_match ; Make sure of our required match
  2220. :process_asm_iter
  2221. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2222. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2223. CMPSKIPI.E R0 34 ; IF GLOBAL_TOKEN->S[0] == '"'
  2224. JUMP @process_asm_done ; Otherwise be done
  2225. ;; Add block of assembly
  2226. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2227. ADDUI R0 R0 1 ; GLOBAL_TOKEN->S + 1
  2228. CALLI R15 @emit_out ; emit it
  2229. LOADUI R0 $newline ; Using "\n"
  2230. CALLI R15 @emit_out ; emit it
  2231. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2232. JUMP @process_asm_iter
  2233. :process_asm_done
  2234. LOADUI R0 $process_asm_string1 ; Using our First error message
  2235. LOADUI R1 $close_paren ; Using ")"
  2236. CALLI R15 @require_match ; Make sure of our required match
  2237. LOADUI R0 $process_asm_string2 ; Using our First error message
  2238. LOADUI R1 $semicolon ; Using ";"
  2239. CALLI R15 @require_match ; Make sure of our required match
  2240. POPR R1 R15 ; Restore R1
  2241. POPR R0 R15 ; Restore R0
  2242. RET R15
  2243. :process_asm_string0
  2244. "ERROR in process_asm
  2245. MISSING (
  2246. "
  2247. :process_asm_string1
  2248. "ERROR in process_asm
  2249. MISSING )
  2250. "
  2251. :process_asm_string2
  2252. "ERROR in process_asm
  2253. MISSING ;
  2254. "
  2255. ;; recursive_statement function
  2256. ;; Receives struct token_list* global_token in R13,
  2257. ;; struct token_list* out in R12,
  2258. ;; struct token_list* string_list in R11
  2259. ;; struct token_list* global_list in R10
  2260. ;; and struct token_list* FUNC in R9
  2261. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2262. ;; Returns the token_lists modified
  2263. :recursive_statement
  2264. PUSHR R0 R15 ; Protect R0
  2265. PUSHR R1 R15 ; Protect R1
  2266. PUSHR R2 R15 ; Protect R2
  2267. PUSHR R3 R15 ; Protect R3
  2268. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2269. LOAD32 R3 R9 4 ; FRAME = FUNCTION->LOCALS
  2270. :recursive_statement_iter
  2271. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2272. LOADUI R0 $close_curly_brace ; '}'
  2273. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "}"
  2274. JUMP.NZ R0 @recursive_statement_cleanup
  2275. ;; Lets collect those statements
  2276. CALLI R15 @statement ; Collect next statement
  2277. JUMP @recursive_statement_iter ; Iterate
  2278. :recursive_statement_cleanup
  2279. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2280. LOAD32 R1 R12 8 ; OUT->S
  2281. LOADUI R0 $recursive_statement_string0 ; "RETURN\n"
  2282. CALLI R15 @match ; IF OUT->S == "RETURN\n"
  2283. JUMP.NZ R0 @recursive_statement_done ; Save some work
  2284. ;; Lets pop them all off
  2285. LOAD32 R2 R9 4 ; FUNC->LOCALS
  2286. LOADUI R0 $recursive_statement_string1 ; Our POP string
  2287. :recursive_statement_pop
  2288. CMPJUMPI.E R2 R3 @recursive_statement_done
  2289. CALLI R15 @emit_out ; emit it
  2290. LOAD32 R2 R2 0 ; I = I->NEXT
  2291. JUMP.NZ R2 @recursive_statement_pop ; Keep looping
  2292. :recursive_statement_done
  2293. STORE32 R3 R9 4 ; FUNC->LOCALS = FRAME
  2294. POPR R3 R15 ; Restore R3
  2295. POPR R2 R15 ; Restore R2
  2296. POPR R1 R15 ; Restore R1
  2297. POPR R0 R15 ; Restore R0
  2298. RET R15
  2299. :recursive_statement_string0
  2300. "'1' LR RETURN
  2301. "
  2302. :recursive_statement_string1
  2303. "{R1} POP_ALWAYS # _recursive_statement_locals
  2304. "
  2305. ;; statement function
  2306. ;; Receives struct token_list* global_token in R13,
  2307. ;; struct token_list* out in R12,
  2308. ;; struct token_list* string_list in R11
  2309. ;; struct token_list* global_list in R10
  2310. ;; and struct token_list* FUNC in R9
  2311. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2312. ;; Returns the token_lists modified
  2313. :statement
  2314. PUSHR R0 R15 ; Protect R0
  2315. PUSHR R1 R15 ; Protect R1
  2316. PUSHR R2 R15 ; Protect R2
  2317. LOAD32 R2 R13 8 ; GLOBAL_TOKEN->S
  2318. LOADU8 R0 R2 0 ; GLOBAL_TOKEN->S[0]
  2319. CMPSKIPI.E R0 123 ; If GLOBAL_TOKEN->S[0] != '{'
  2320. JUMP @statement_label ; Try next match
  2321. ;; Deal with { statements }
  2322. CALLI R15 @recursive_statement
  2323. JUMP @statement_done ; All done
  2324. :statement_label
  2325. CMPSKIPI.E R0 58 ; If GLOBAL_TOKEN->S[0] != ':'
  2326. JUMP @statement_collect_local ; Try next match
  2327. ;; Deal with :label
  2328. LOAD32 R0 R13 8 ; Using GLOBAL_TOKEN->S
  2329. CALLI R15 @emit_out ; emit it
  2330. LOADUI R0 $statement_string0 ; Using label string
  2331. CALLI R15 @emit_out ; emit it
  2332. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2333. JUMP @statement_done ; Move on to next thing
  2334. :statement_collect_local
  2335. LOADUI R0 $struct ; Using "struct"
  2336. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2337. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "struct"
  2338. JUMP.NZ R0 @statement_collect_local_0
  2339. ;; Otherwise check if it is a primitive
  2340. LOADUI R0 $prim_types ; Using the Primitive types list
  2341. SWAP R0 R1 ; Put in correct order
  2342. CALLI R15 @lookup_type ; Check if a primitive type
  2343. JUMP.Z R0 @statement_process_if ; If not try the next one
  2344. :statement_collect_local_0
  2345. CALLI R15 @collect_local ; Collect the local
  2346. JUMP @statement_done ; And move on
  2347. :statement_process_if
  2348. LOADUI R0 $if_string ; Using "if"
  2349. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2350. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "if"
  2351. JUMP.Z R0 @statement_process_do
  2352. CALLI R15 @process_if ; Collect that if statement
  2353. JUMP @statement_done ; Move on to next thing
  2354. :statement_process_do
  2355. LOADUI R0 $do_string ; Using "do"
  2356. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2357. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "do"
  2358. JUMP.Z R0 @statement_process_while
  2359. CALLI R15 @process_do ; Collect that do statement
  2360. JUMP @statement_done ; Move on to next thing
  2361. :statement_process_while
  2362. LOADUI R0 $while_string ; Using "while"
  2363. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2364. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "while"
  2365. JUMP.Z R0 @statement_process_for
  2366. CALLI R15 @process_while ; Collect that while statement
  2367. JUMP @statement_done ; Move on to next thing
  2368. :statement_process_for
  2369. LOADUI R0 $for_string ; Using "for"
  2370. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2371. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "for"
  2372. JUMP.Z R0 @statement_process_asm
  2373. CALLI R15 @process_for ; Collect that FOR statement
  2374. JUMP @statement_done ; Move on to next thing
  2375. :statement_process_asm
  2376. LOADUI R0 $asm_string ; Using "asm"
  2377. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2378. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "asm"
  2379. JUMP.Z R0 @statement_goto
  2380. CALLI R15 @process_asm ; Collect that ASM statement
  2381. JUMP @statement_done ; Move on to next thing
  2382. :statement_goto
  2383. LOADUI R0 $goto_string ; Using "goto"
  2384. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2385. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "goto"
  2386. JUMP.Z R0 @statement_return_result
  2387. ;; Deal with goto label:
  2388. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2389. LOADUI R0 $statement_string1 ; Using our JUMP string
  2390. CALLI R15 @emit_out ; emit it
  2391. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2392. CALLI R15 @emit_out ; emit it
  2393. LOADUI R0 $statement_string4 ; Using jump terminator
  2394. CALLI R15 @emit_out ; emit it
  2395. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2396. LOADUI R0 $statement_string2 ; Using our error message
  2397. LOADUI R1 $semicolon ; Using ";"
  2398. CALLI R15 @require_match ; Make sure of our required match
  2399. JUMP @statement_done ; Move on
  2400. :statement_return_result
  2401. LOADUI R0 $return_string ; Using "return"
  2402. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2403. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "return"
  2404. JUMP.Z R0 @statement_break
  2405. ;; Deal with return statements in functions
  2406. CALLI R15 @return_result ; Do all of the work
  2407. JUMP @statement_done ; Move on to next
  2408. :statement_break
  2409. LOADUI R0 $break_string ; Using "break"
  2410. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2411. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "break"
  2412. JUMP.Z R0 @statement_continue
  2413. ;; Let break function deal with updating out
  2414. CALLI R15 @process_break ; Do all the work
  2415. JUMP @statement_done ; Move on to next
  2416. :statement_continue
  2417. LOADUI R0 $continue_string ; Using "continue"
  2418. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2419. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "continue"
  2420. JUMP.Z R0 @statement_expression
  2421. ;; Simple Continue compatibility
  2422. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2423. LOADUI R0 $statement_string3 ; Using our continue comment string
  2424. CALLI R15 @emit_out ; emit it
  2425. LOADUI R0 $statement_string2 ; Using our error message
  2426. LOADUI R1 $semicolon ; Using ";"
  2427. CALLI R15 @require_match ; Make sure of our required match
  2428. JUMP @statement_done ; Move on
  2429. :statement_expression
  2430. CALLI R15 @expression ; Do expression evaluation
  2431. LOADUI R0 $statement_string2 ; Load our error message
  2432. LOADUI R1 $semicolon ; use ";"
  2433. CALLI R15 @require_match ; Make sure GLOBAL_TOKEN-> == ";"
  2434. :statement_done
  2435. POPR R2 R15 ; Restore R2
  2436. POPR R1 R15 ; Restore R1
  2437. POPR R0 R15 ; Restore R0
  2438. RET R15
  2439. :statement_string0
  2440. " #C goto label
  2441. "
  2442. :statement_string1
  2443. "^~"
  2444. :statement_string2
  2445. "ERROR in statement
  2446. MISSING ;
  2447. "
  2448. :statement_string3
  2449. "
  2450. #continue statement
  2451. "
  2452. :statement_string4
  2453. " JUMP_ALWAYS
  2454. "
  2455. ;; collect_local function
  2456. ;; Receives struct token_list* global_token in R13,
  2457. ;; struct token_list* out in R12,
  2458. ;; struct token_list* string_list in R11
  2459. ;; struct token_list* global_list in R10
  2460. ;; and struct token_list* FUNC in R9
  2461. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2462. ;; Returns the token_lists modified
  2463. :collect_local
  2464. PUSHR R0 R15 ; Protect R0
  2465. PUSHR R1 R15 ; Protect R1
  2466. PUSHR R2 R15 ; Protect R2
  2467. CALLI R15 @type_name ; Get it's type
  2468. MOVE R1 R0 ; Prepare for call
  2469. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2470. LOAD32 R2 R9 4 ; FUNC->LOCALS
  2471. CALLI R15 @sym_declare ; SET A
  2472. MOVE R2 R0 ; Protect A
  2473. ;; Figure out depth
  2474. LOADUI R0 $main_string ; Using "main"
  2475. LOAD32 R1 R9 8 ; FUNC->S
  2476. CALLI R15 @match ; IF FUNC->S == "main"
  2477. JUMP.Z R0 @collect_local_0 ; Try next
  2478. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2479. JUMP.NZ R0 @collect_local_0 ; Try next
  2480. LOADI R0 16 ; The default depth for main
  2481. STORE32 R0 R2 16 ; A->DEPTH = 16
  2482. JUMP @collect_local_output ; Deal with header
  2483. :collect_local_0
  2484. LOAD32 R0 R9 16 ; FUNC->ARGS
  2485. JUMP.NZ R0 @collect_local_1 ; Try Next
  2486. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2487. JUMP.NZ R0 @collect_local_1 ; Try Next
  2488. LOADI R0 8 ; The default depth for foo()
  2489. STORE32 R0 R2 16 ; A->DEPTH = 8
  2490. JUMP @collect_local_output ; Deal with header
  2491. :collect_local_1
  2492. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2493. JUMP.NZ R0 @collect_local_2 ; Try Next
  2494. LOAD32 R0 R9 16 ; FUNC->ARGS
  2495. LOAD32 R0 R0 16 ; FUNC->ARGS->DEPTH
  2496. ADDI R0 R0 8 ; DEPTH = FUNC->ARGS->DEPTH + 8
  2497. STORE32 R0 R2 16 ; A->DEPTH = DEPTH
  2498. JUMP @collect_local_output ; Deal with header
  2499. :collect_local_2
  2500. LOAD32 R0 R9 4 ; FUNC->LOCALS
  2501. LOAD32 R0 R0 16 ; FUNC->LOCALS->DEPTH
  2502. ADDI R0 R0 4 ; DEPTH = FUNC->LOCALS->DEPTH + 4
  2503. STORE32 R0 R2 16 ; A->DEPTH = DEPTH
  2504. :collect_local_output
  2505. STORE32 R2 R9 4 ; FUNC->LOCALS = A
  2506. ;; Output header
  2507. LOADUI R0 $collect_local_string0 ; Starting with the comment
  2508. CALLI R15 @emit_out ; emit it
  2509. LOAD32 R0 R2 8 ; A->S
  2510. CALLI R15 @emit_out ; emit it
  2511. LOADUI R0 $newline ; Using "\n"
  2512. CALLI R15 @emit_out ; emit it
  2513. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2514. ;; Deal with possible assignment
  2515. LOADUI R0 $equal ; Using "="
  2516. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2517. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "="
  2518. JUMP.Z R0 @collect_local_nonassign
  2519. ;; Deal with assignment of the local
  2520. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2521. CALLI R15 @expression ; Update OUT with the evaluation of the Expression
  2522. :collect_local_nonassign
  2523. LOADUI R0 $collect_local_string1 ; Our error message
  2524. LOADUI R1 $semicolon ; Using ";"
  2525. CALLI R15 @require_match ; Make sure GLOBAL_TOKEN->S == ";"
  2526. ;; Final Footer
  2527. LOADUI R0 $collect_local_string2 ; Add our PUSH statement
  2528. CALLI R15 @emit_out ; emit it
  2529. LOAD32 R0 R2 8 ; A->S
  2530. CALLI R15 @emit_out ; emit it
  2531. LOADUI R0 $newline ; Using "\n"
  2532. CALLI R15 @emit_out ; emit it
  2533. POPR R2 R15 ; Restore R2
  2534. POPR R1 R15 ; Restore R1
  2535. POPR R0 R15 ; Restore R0
  2536. RET R15
  2537. :collect_local_string0
  2538. "# Defining local "
  2539. :collect_local_string1
  2540. "ERROR in collect_local
  2541. Missing ;
  2542. "
  2543. :collect_local_string2
  2544. "{R0} PUSH_ALWAYS #"
  2545. ;; collect_arguments function
  2546. ;; Receives struct token_list* global_token in R13,
  2547. ;; struct token_list* out in R12,
  2548. ;; struct token_list* string_list in R11
  2549. ;; struct token_list* global_list in R10
  2550. ;; and struct token_list* FUNC in R9
  2551. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2552. ;; Returns the token_lists modified
  2553. :collect_arguments
  2554. PUSHR R0 R15 ; Protect R0
  2555. PUSHR R1 R15 ; Protect R1
  2556. PUSHR R2 R15 ; Protect R2
  2557. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2558. :collect_arguments_iter
  2559. LOADUI R0 $close_paren ; Using ")"
  2560. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2561. CALLI R15 @match ; IF GLOBAL_TOKEN->S == ")"
  2562. JUMP.NZ R0 @collect_arguments_done ; Be done
  2563. ;; Collect the arguments
  2564. CALLI R15 @type_name ; Get what type we are working with
  2565. MOVE R1 R0 ; Put TYPE where it will be used
  2566. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2567. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2568. CMPSKIPI.NE R0 41 ; IF GLOBAL_TOKEN->S[0] == ')'
  2569. JUMP @collect_arguments_iter3 ; foo(int,char,void) doesn't need anything done
  2570. ;; Check for foo(int a,...)
  2571. CMPSKIPI.NE R0 41 ; IF GLOBAL_TOKEN->S[0] == ','
  2572. JUMP @collect_arguments_iter3 ; Looks like final case
  2573. ;; Deal with foo(int a, ...)
  2574. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2575. LOAD32 R2 R9 16 ; FUNC->ARGUMENTS
  2576. CALLI R15 @sym_declare ; Get A
  2577. MOVE R2 R0 ; Get A out of the way
  2578. :collect_arguments_func
  2579. LOAD32 R0 R9 16 ; FUNC->ARGS
  2580. CMPSKIPI.E R0 0 ; IF NULL == FUNC->ARGS
  2581. LOAD32 R0 R0 16 ; FUNC->ARGS->DEPTH
  2582. ADDI R0 R0 4 ; FUNC->ARGS->DEPTH + 4 or NULL + 4 (4)
  2583. STORE32 R0 R2 16 ; A->DEPTH = VALUE
  2584. :collect_arguments_iter2
  2585. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2586. STORE32 R2 R9 16 ; FUNC->ARGUMENTS = A
  2587. :collect_arguments_iter3
  2588. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2589. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2590. CMPSKIPI.NE R0 44 ; IF GLOBAL_TOKEN->S[0] == ','
  2591. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2592. JUMP @collect_arguments_iter ; Keep looping
  2593. :collect_arguments_done
  2594. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2595. POPR R2 R15 ; Restore R2
  2596. POPR R1 R15 ; Restore R1
  2597. POPR R0 R15 ; Restore R0
  2598. RET R15
  2599. ;; declare_function function
  2600. ;; Receives struct token_list* global_token in R13,
  2601. ;; struct token_list* out in R12,
  2602. ;; struct token_list* string_list in R11
  2603. ;; and struct token_list* global_list in R10
  2604. ;; SETS R9 to struct token_list* FUNC
  2605. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2606. ;; Returns the token_lists modified
  2607. :declare_function
  2608. PUSHR R0 R15 ; Protect R0
  2609. PUSHR R1 R15 ; Protect R1
  2610. PUSHR R2 R15 ; Protect R2
  2611. FALSE R0 ; Using Zero
  2612. STORER32 R0 @current_count ; CURRENT_COUNT = 0
  2613. LOAD32 R0 R13 4 ; GLOBAL_TOKEN->PREV
  2614. LOAD32 R0 R0 8 ; GLOBAL_TOKEN->PREV->S
  2615. FALSE R1 ; Passing NULL
  2616. LOADR32 R2 @global_function_list ; where the global function list is located
  2617. CALLI R15 @sym_declare ; declare FUNC
  2618. STORER32 R0 @global_function_list ; GLOBAL_FUNCTION_LIST = FUNC
  2619. MOVE R9 R0 ; SETS FUNC
  2620. CALLI R15 @collect_arguments ; Collect function arguments
  2621. LOAD32 R2 R13 8 ; GLOBAL_TOKEN->S
  2622. LOADU8 R0 R2 0 ; GLOBAL_TOKEN->S[0]
  2623. CMPSKIPI.NE R0 59 ; IF GLOBAL_TOKEN->S[0] == ';'
  2624. JUMP @declare_function_prototype ; Don't waste time
  2625. ;; Looks like it is an actual function definition
  2626. LOADUI R0 $declare_function_string0 ; Using first string
  2627. CALLI R15 @emit_out ; emit it
  2628. LOAD32 R0 R9 8 ; Using FUNC->S
  2629. CALLI R15 @emit_out ; emit it
  2630. LOADUI R0 $newline ; Using "\n"
  2631. CALLI R15 @emit_out ; emit it
  2632. LOADUI R0 $declare_function_string1 ; Using second string
  2633. CALLI R15 @emit_out ; emit it
  2634. LOAD32 R0 R9 8 ; Using FUNC->S
  2635. CALLI R15 @emit_out ; emit it
  2636. LOADUI R0 $newline ; Using "\n"
  2637. CALLI R15 @emit_out ; emit it
  2638. :declare_function_nonmain
  2639. FALSE R1 ; Cleaning up before call
  2640. CALLI R15 @statement ; Collect the statement
  2641. ;; Prevent Duplicate Returns
  2642. LOAD32 R1 R12 8 ; OUT->S
  2643. LOADUI R0 $declare_function_string2 ; Our final string
  2644. CALLI R15 @match ; Check for Match
  2645. JUMP.NZ R0 @declare_function_done ; Clean up
  2646. ;; Deal with adding the return
  2647. LOADUI R0 $declare_function_string2 ; Our final string
  2648. CALLI R15 @emit_out ; emit it
  2649. :declare_function_done
  2650. POPR R2 R15 ; Restore R2
  2651. POPR R1 R15 ; Restore R1
  2652. POPR R0 R15 ; Restore R0
  2653. RET R15
  2654. :declare_function_prototype
  2655. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2656. JUMP @declare_function_done ; Clean up
  2657. :declare_function_string0
  2658. "# Defining function "
  2659. :declare_function_string1
  2660. ":FUNCTION_"
  2661. :declare_function_string2
  2662. "'1' LR RETURN
  2663. "
  2664. :current_count
  2665. NOP
  2666. ;; program function
  2667. ;; Receives struct token_list* global_token in R13,
  2668. ;; struct token_list* out in R12,
  2669. ;; struct token_list* string_list in R11
  2670. ;; and struct token_list* global_list in R10
  2671. ;; and struct token_list* FUNC in R9
  2672. ;; and struct token_list* current_target in R8
  2673. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2674. ;; Returns the token_lists modified
  2675. :program
  2676. PUSHR R0 R15 ; Protect R0
  2677. PUSHR R1 R15 ; Protect R1
  2678. PUSHR R2 R15 ; Protect R2
  2679. PUSHR R3 R15 ; Protect R3
  2680. :program_iter
  2681. JUMP.Z R13 @program_done ; Looks like we read all the tokens
  2682. LOADUI R0 $constant ; Using the constant string
  2683. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2684. CALLI R15 @match ; Check if they match
  2685. JUMP.Z R0 @program_type ; Looks like not
  2686. ;; Deal with CONSTANT case
  2687. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2688. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2689. FALSE R1 ; Set NULL
  2690. LOADR32 R2 @global_constant_list ; GLOBAL_CONSTANTS_LIST
  2691. CALLI R15 @sym_declare ; Declare the global constant
  2692. STORER32 R0 @global_constant_list ; Update global constant
  2693. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2694. STORE32 R13 R0 16 ; GLOBAL_CONSTANT_LIST->ARGUMENTS = GLOBAL_TOKEN
  2695. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2696. JUMP @program_iter ; Loop again
  2697. :program_type
  2698. CALLI R15 @type_name ; Get the type
  2699. JUMP.Z R0 @program_iter ; If newly defined type iterate
  2700. ;; Looks like we got a defined type
  2701. MOVE R1 R0 ; Put the type where it can be used
  2702. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2703. LOADUI R3 $global_symbol_list ; Get address of global symbol list
  2704. LOAD32 R2 R3 0 ; GLOBAL_SYMBOLS_LIST
  2705. CALLI R15 @sym_declare ; Declare that global symbol
  2706. STORE32 R0 R3 0 ; Update global symbol list
  2707. LOAD32 R3 R13 8 ; GLOBAL_TOKEN->S
  2708. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2709. LOADUI R0 $semicolon ; Get semicolon string
  2710. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  2711. CALLI R15 @match ; Check if they match
  2712. JUMP.Z R0 @program_function ; If not a match
  2713. ;; Deal with case of TYPE NAME;
  2714. COPY R1 R10 ; Using GLOBALS_LIST
  2715. LOADUI R0 $program_string0 ; Using the GLOBAL_ prefix
  2716. CALLI R15 @emit ; emit it
  2717. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2718. MOVE R0 R3 ; Use GLOBAL_TOKEN->PREV->S
  2719. CALLI R15 @emit ; emit it
  2720. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2721. LOADUI R0 $program_string1 ; Using the NOP postfix
  2722. CALLI R15 @emit ; emit it
  2723. MOVE R10 R0 ; Move new GLOBALS_LIST into Place
  2724. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2725. JUMP @program_iter
  2726. :program_function
  2727. LOADUI R0 $open_paren ; Get open paren string
  2728. CALLI R15 @match ; Check if they match
  2729. JUMP.Z R0 @program_assign ; If not a match
  2730. ;; Deal with case of TYPE NAME(...)
  2731. CALLI R15 @declare_function
  2732. JUMP @program_iter
  2733. :program_assign
  2734. LOADUI R0 $equal ; Get equal string
  2735. CALLI R15 @match ; Check if they match
  2736. JUMP.Z R0 @program_error ; If not a match
  2737. COPY R1 R10 ; Using GLOBALS_LIST
  2738. LOADUI R0 $program_string0 ; Using the GLOBAL_ prefix
  2739. CALLI R15 @emit ; emit it
  2740. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2741. MOVE R0 R3 ; Use GLOBAL_TOKEN->PREV->S
  2742. CALLI R15 @emit ; emit it
  2743. MOVE R1 R0 ; Move new GLOBALS_LIST into Place
  2744. LOADUI R0 $newline ; Using the Newline postfix
  2745. CALLI R15 @emit ; emit it
  2746. MOVE R10 R0 ; Update GLOBALS_LIST
  2747. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2748. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2749. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2750. LOADUI R1 $digit_chars ; 0-9
  2751. CALLI R15 @in_set ; Figure out if in set
  2752. JUMP.Z R0 @program_assign_string ; If not in sets
  2753. ;; Looks like we have an int
  2754. COPY R1 R10 ; Using GLOBALS_LIST
  2755. LOADUI R0 $percent ; Using percent prefix
  2756. CALLI R15 @emit ; emit it
  2757. MOVE R1 R0 ; Put GLOBALS_LIST into Place
  2758. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2759. CALLI R15 @emit ; emit it
  2760. MOVE R1 R0 ; Put GLOBALS_LIST into Place
  2761. LOADUI R0 $newline ; Using newline postfix
  2762. CALLI R15 @emit ; emit it
  2763. MOVE R10 R0 ; Update GLOBALS_LIST
  2764. JUMP @program_assign_done ; Move on
  2765. :program_assign_string
  2766. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2767. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2768. CMPSKIPI.E R0 34 ; If GLOBAL_TOKEN->S[0] == '"'
  2769. JUMP @program_error ; If not we hit an error
  2770. ;; Looks like we have a string
  2771. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2772. CALLI R15 @parse_string ; Parse it into useful form
  2773. COPY R1 R10 ; GLOBALS_LIST
  2774. CALLI R15 @emit ; emit it
  2775. MOVE R10 R0 ; Update GLOBALS_LIST
  2776. :program_assign_done
  2777. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2778. LOADUI R0 $program_string4 ; Potential error message
  2779. LOADUI R1 $semicolon ; Checking for ;
  2780. CALLI R15 @require_match ; Catch those errors
  2781. JUMP @program_iter
  2782. :program_error
  2783. LOADUI R0 $program_string2 ; message part 1
  2784. FALSE R1 ; Show to user
  2785. CALLI R15 @file_print ; write
  2786. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2787. CALLI R15 @file_print ; write
  2788. LOADUI R0 $program_string3 ; message part 2
  2789. CALLI R15 @file_print ; write
  2790. CALLI R15 @line_error ; Provide a meaningful error message
  2791. HALT
  2792. :program_done
  2793. POPR R3 R15 ; Restore R3
  2794. POPR R2 R15 ; Restore R2
  2795. POPR R1 R15 ; Restore R1
  2796. POPR R0 R15 ; Restore R0
  2797. RET R15
  2798. :program_string0
  2799. ":GLOBAL_"
  2800. :program_string1
  2801. "
  2802. NULL
  2803. "
  2804. :program_string2
  2805. "Received "
  2806. :program_string3
  2807. " in program
  2808. "
  2809. :program_string4
  2810. "ERROR in Program
  2811. Missing ;
  2812. "
  2813. ;; sym_declare function
  2814. ;; Receives char* in R0, struct type* in R1, struct token_list* in R2
  2815. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2816. ;; Returns struct token_list* in R0
  2817. :sym_declare
  2818. STORE32 R2 R14 0 ; A->NEXT = LIST
  2819. STORE32 R0 R14 8 ; A->S = S
  2820. STORE32 R1 R14 12 ; A->TYPE = T
  2821. ADDUI R0 R14 20 ; CALLOC struct token_list
  2822. SWAP R0 R14 ; Prepare for Return
  2823. RET R15
  2824. ;; sym_lookup function
  2825. ;; Receives char* in R0 and struct token_list in R1
  2826. ;; Returns struct token_list* or NULL in R0
  2827. :sym_lookup
  2828. PUSHR R2 R15 ; Protect R2
  2829. MOVE R2 R1 ; Protect I
  2830. MOVE R1 R0 ; Put S in proper place
  2831. :sym_lookup_iter
  2832. JUMP.Z R2 @sym_lookup_done ; Stop if NULL
  2833. LOAD32 R0 R2 8 ; I->S
  2834. CALLI R15 @match ; if I->S == S
  2835. JUMP.NZ R0 @sym_lookup_done ; Stop if match
  2836. LOAD32 R2 R2 0 ; I = I->NEXT
  2837. JUMP @sym_lookup_iter ; Keep looping
  2838. :sym_lookup_done
  2839. MOVE R0 R2 ; Using R2 as our result
  2840. POPR R2 R15 ; Restore R2
  2841. RET R15
  2842. ;; function_call function
  2843. ;; Receives CHAR* S in R0 and INT BOOL in R1
  2844. ;; struct token_list* out in R12,
  2845. ;; struct token_list* string_list in R11
  2846. ;; and struct token_list* global_list in R10
  2847. ;; and struct token_list* FUNC in R9
  2848. ;; and struct token_list* current_target in R8
  2849. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2850. ;; Returns the token_lists modified
  2851. :function_call
  2852. PUSHR R0 R15 ; Protect R0
  2853. PUSHR R1 R15 ; Protect R1
  2854. PUSHR R2 R15 ; Protect R2
  2855. PUSHR R3 R15 ; Protect R3
  2856. PUSHR R4 R15 ; Protect R4
  2857. MOVE R2 R0 ; Protect S
  2858. MOVE R3 R1 ; Protect BOOL
  2859. LOADUI R0 $function_call_string0 ; Our first error message
  2860. LOADUI R1 $open_paren ; Using "("
  2861. CALLI R15 @require_match ; Make sure of a match
  2862. FALSE R4 ; PASSED = 0
  2863. LOADUI R0 $function_call_string1 ; Our first header
  2864. CALLI R15 @emit_out ; emit it
  2865. LOADUI R0 $function_call_string2 ; Our second header
  2866. CALLI R15 @emit_out ; emit it
  2867. LOADUI R0 $function_call_string3 ; Our third header
  2868. CALLI R15 @emit_out ; emit it
  2869. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2870. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2871. CMPSKIPI.NE R0 41 ; IF GLOBAL_TOKEN->S[0] != ')'
  2872. JUMP @function_call_collect_done ; looks like function()
  2873. ;; Collect arguments
  2874. CALLI R15 @expression ; Deal with first argument
  2875. LOADUI R0 $function_call_string4 ; Push it onto stack
  2876. CALLI R15 @emit_out ; emit it
  2877. ADDUI R4 R4 1 ; PASSED = 1
  2878. :function_call_collect_iter
  2879. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  2880. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  2881. CMPSKIPI.E R0 44 ; IF GLOBAL_TOKEN->S[0] != ','
  2882. JUMP @function_call_collect_done ; looks like we are done collecting arguments
  2883. ;; Collect another argument
  2884. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  2885. CALLI R15 @expression ; Deal with Nth argument
  2886. LOADUI R0 $function_call_string5 ; Push it onto stack
  2887. CALLI R15 @emit_out ; emit it
  2888. ADDUI R4 R4 1 ; PASSED = PASSED + 1
  2889. JUMP @function_call_collect_iter ; Keep looping
  2890. :function_call_collect_done
  2891. LOADUI R0 $function_call_string6 ; Our second error message
  2892. LOADUI R1 $close_paren ; Using ")"
  2893. CALLI R15 @require_match ; Make sure of a match
  2894. JUMP.Z R3 @function_call_call_false ; if BOOL != TRUE
  2895. ;; Deal with TRUE == BOOL
  2896. LOADUI R0 $function_call_string7 ; Our first prefix
  2897. CALLI R15 @emit_out ; emit it
  2898. MOVE R0 R2 ; Using S
  2899. CALLI R15 @emit_out ; emit it
  2900. LOADUI R0 $function_call_string8 ; Our first postfix
  2901. CALLI R15 @emit_out ; emit it
  2902. LOADUI R0 $function_call_string9 ; Our second postfix
  2903. CALLI R15 @emit_out ; emit it
  2904. LOADUI R0 $function_call_string10 ; Our last postfix
  2905. CALLI R15 @emit_out ; emit it
  2906. JUMP @function_call_call_done ; Move on
  2907. :function_call_call_false
  2908. ;; Deal with FALSE == BOOL
  2909. LOADUI R0 $function_call_string11 ; Our first prefix
  2910. CALLI R15 @emit_out ; emit it
  2911. LOADUI R0 $function_call_string12 ; Our last prefix
  2912. CALLI R15 @emit_out ; emit it
  2913. MOVE R0 R2 ; Using S
  2914. CALLI R15 @emit_out ; emit it
  2915. LOADUI R0 $function_call_string16 ; Using terminating prefix
  2916. CALLI R15 @emit_out ; emit it
  2917. :function_call_call_done
  2918. LOADUI R0 $function_call_string13 ; Our POP
  2919. :function_call_pop_iter
  2920. JUMP.Z R4 @function_call_pop_done ; Skip POP if out of args on Stack
  2921. CALLI R15 @emit_out ; emit our POP
  2922. SUBI R4 R4 1 ; PASSED = PASSED - 1
  2923. JUMP @function_call_pop_iter ; Loop
  2924. :function_call_pop_done
  2925. LOADUI R0 $function_call_string14 ; Our first postfix
  2926. CALLI R15 @emit_out ; emit it
  2927. LOADUI R0 $function_call_string15 ; Our final postfix
  2928. CALLI R15 @emit_out ; emit it
  2929. POPR R4 R15 ; Restore R4
  2930. POPR R3 R15 ; Restore R3
  2931. POPR R2 R15 ; Restore R2
  2932. POPR R1 R15 ; Restore R1
  2933. POPR R0 R15 ; Restore R0
  2934. RET R15
  2935. :function_call_string0
  2936. "ERROR in process_expression_list
  2937. No ( was found
  2938. "
  2939. :function_call_string1
  2940. "{R11} PUSH_ALWAYS # Prevent overwriting in recursion
  2941. "
  2942. :function_call_string2
  2943. "{BP} PUSH_ALWAYS # Protect the old base pointer
  2944. "
  2945. :function_call_string3
  2946. "'0' SP R11 NO_SHIFT MOVE_ALWAYS # Copy new base pointer
  2947. "
  2948. :function_call_string4
  2949. "{R0} PUSH_ALWAYS #_process_expression1
  2950. "
  2951. :function_call_string5
  2952. "{R0} PUSH_ALWAYS #_process_expression2
  2953. "
  2954. :function_call_string6
  2955. "ERROR in process_expression_list
  2956. No ) was found
  2957. "
  2958. :function_call_string7
  2959. "!"
  2960. :function_call_string8
  2961. " R0 SUB BP ARITH_ALWAYS
  2962. !0 R0 LOAD32 R0 MEMORY
  2963. {LR} PUSH_ALWAYS # Protect the old link register
  2964. "
  2965. :function_call_string9
  2966. "'0' R11 BP NO_SHIFT MOVE_ALWAYS
  2967. "
  2968. :function_call_string10
  2969. "'3' R0 CALL_REG_ALWAYS
  2970. {LR} POP_ALWAYS # Prevent overwrite
  2971. "
  2972. :function_call_string11
  2973. "{LR} PUSH_ALWAYS # Protect the old link register
  2974. '0' R11 BP NO_SHIFT MOVE_ALWAYS
  2975. "
  2976. :function_call_string12
  2977. "^~FUNCTION_"
  2978. :function_call_string13
  2979. "{R1} POP_ALWAYS # _process_expression_locals
  2980. "
  2981. :function_call_string14
  2982. "{BP} POP_ALWAYS # Restore old base pointer
  2983. "
  2984. :function_call_string15
  2985. "{R11} POP_ALWAYS # Prevent overwrite
  2986. "
  2987. :function_call_string16
  2988. " CALL_ALWAYS
  2989. {LR} POP_ALWAYS # Restore the old link register
  2990. "
  2991. ;; emit function
  2992. ;; Receives char* in R0, struct token_list* in R1
  2993. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  2994. ;; Returns struct token_list* in R0
  2995. :emit
  2996. PUSHR R2 R15 ; Protect R2
  2997. COPY R2 R14 ; Pointer to T
  2998. ADDUI R14 R14 20 ; CALLOC struct token_list
  2999. STORE32 R1 R2 0 ; T->NEXT = HEAD
  3000. STORE32 R0 R2 8 ; T->S = S
  3001. MOVE R0 R2 ; Put T in proper spot for return
  3002. POPR R2 R15 ; Restore R2
  3003. RET R15
  3004. ;; emit_out function
  3005. ;; Receives char* in R0
  3006. ;; struct token_list* out in R12,
  3007. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3008. ;; Returns struct token_list* in R0
  3009. :emit_out
  3010. STORE32 R12 R14 0 ; T->NEXT = OUT
  3011. ADDUI R12 R14 20 ; Get T
  3012. SWAP R12 R14 ; CALLOC struct token_list
  3013. STORE32 R0 R12 8 ; T->S = S
  3014. RET R15
  3015. ;; uniqueID function
  3016. ;; Receives char* in R0, struct token_list* in R1 and char* in R2
  3017. ;; Calls emit repeatedly
  3018. ;; Returns struct token_list* in R0
  3019. :uniqueID
  3020. CALLI R15 @emit ; emit S
  3021. MOVE R1 R0 ; Put L in the correct place
  3022. LOADUI R0 $underline ; Using "_"
  3023. CALLI R15 @emit ; emit it
  3024. MOVE R1 R0 ; Put L in the correct place
  3025. COPY R0 R2 ; Put NUM in the correct place
  3026. CALLI R15 @emit ; emit NUM
  3027. MOVE R1 R0 ; Put L in the correct place
  3028. LOADUI R0 $newline ; Using "\n"
  3029. CALLI R15 @emit ; emit it
  3030. RET R15
  3031. ;; uniqueID_out function
  3032. ;; Receives char* in R0, char* in R1
  3033. ;; Calls emit_out repeatedly
  3034. ;; Returns nothing
  3035. :uniqueID_out
  3036. CALLI R15 @emit_out ; emit S
  3037. LOADUI R0 $underline ; Using "_"
  3038. CALLI R15 @emit_out ; emit it
  3039. COPY R0 R1 ; Put NUM in the correct place
  3040. CALLI R15 @emit_out ; emit NUM
  3041. LOADUI R0 $newline ; Using "\n"
  3042. CALLI R15 @emit_out ; emit it
  3043. RET R15
  3044. ;; file_print function
  3045. ;; Receives pointer to string in R0 and FILE* in R1
  3046. ;; Returns nothing
  3047. :file_print
  3048. PUSHR R2 R15 ; Protect R2 from Overwrite
  3049. MOVE R2 R0 ; Put string pointer into place
  3050. :file_print_read
  3051. LOAD8 R0 R2 0 ; Get a char
  3052. JUMP.Z R0 @file_print_done ; If NULL be done
  3053. FPUTC ; Write the Char
  3054. ADDUI R2 R2 1 ; Point at next CHAR
  3055. JUMP @file_print_read ; Loop again
  3056. :file_print_done
  3057. POPR R2 R15 ; Restore R2
  3058. RET R15
  3059. ;; recursive_output function
  3060. ;; Receives token_list in R0 and FILE* in R1
  3061. ;; Returns nothing and alters nothing
  3062. :recursive_output
  3063. JUMP.Z R0 @recursive_output_abort ; Abort if NULL
  3064. PUSHR R2 R15 ; Preserve R2 from recursion
  3065. MOVE R2 R0 ; Preserve R0 from recursion
  3066. LOAD32 R0 R2 0 ; Using I->NEXT
  3067. CALLI R15 @recursive_output ; Recurse
  3068. LOAD32 R0 R2 8 ; Using I->S
  3069. CALLI R15 @file_print ; Write the string
  3070. MOVE R0 R2 ; Put R0 back
  3071. POPR R2 R15 ; Restore R0
  3072. :recursive_output_abort
  3073. RET R15
  3074. ;; match function
  3075. ;; Receives a CHAR* in R0, CHAR* in R1
  3076. ;; Returns Bool in R0 indicating if strings match
  3077. :match
  3078. PUSHR R1 R15 ; Protect R1
  3079. PUSHR R2 R15 ; Protect R2
  3080. PUSHR R3 R15 ; Protect R3
  3081. PUSHR R4 R15 ; Protect R4
  3082. MOVE R2 R0 ; Put First string in place
  3083. MOVE R3 R1 ; Put Second string in place
  3084. LOADUI R4 0 ; Set initial index of 0
  3085. :match_cmpbyte
  3086. LOADXU8 R0 R2 R4 ; Get a byte of our first string
  3087. LOADXU8 R1 R3 R4 ; Get a byte of our second string
  3088. ADDUI R4 R4 1 ; Prep for next loop
  3089. CMPSKIP.NE R1 R0 ; Compare the bytes
  3090. JUMP.NZ R1 @match_cmpbyte ; Loop if bytes are equal
  3091. ;; Done
  3092. FALSE R2 ; Default answer
  3093. CMPSKIP.NE R0 R1 ; If ended loop with everything matching
  3094. TRUE R2 ; Set as TRUE
  3095. MOVE R0 R2 ; Prepare for return
  3096. POPR R4 R15 ; Restore R4
  3097. POPR R3 R15 ; Restore R3
  3098. POPR R2 R15 ; Restore R2
  3099. POPR R1 R15 ; Restore R1
  3100. RET R15
  3101. ;; lookup_type function
  3102. ;; Receives a CHAR* in R0 and struct type* in R1
  3103. ;; Returns struct type* in R0 or NULL if no match
  3104. :lookup_type
  3105. PUSHR R1 R15 ; Protect R1
  3106. PUSHR R2 R15 ; Protect R2
  3107. MOVE R2 R1 ; Put START in correct place
  3108. MOVE R1 R0 ; Put S in correct place
  3109. :lookup_type_iter
  3110. LOAD32 R0 R2 24 ; Get I->NAME
  3111. CALLI R15 @match ; Check if I->NAME == S
  3112. JUMP.NZ R0 @lookup_type_done ; If match found be done
  3113. LOAD32 R2 R2 0 ; I = I->NEXT
  3114. JUMP.NZ R2 @lookup_type_iter ; Otherwise iterate until I == NULL
  3115. :lookup_type_done
  3116. MOVE R0 R2 ; Our answer (I or NULL)
  3117. POPR R2 R15 ; Restore R2
  3118. POPR R1 R15 ; Restore R1
  3119. RET R15
  3120. ;; lookup_member function
  3121. ;; Receives struct type* parent in R0 and char* name in R1
  3122. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3123. ;; Returns struct type* of member in R0 or aborts with error
  3124. :lookup_member
  3125. PUSHR R1 R15 ; Protect R1
  3126. PUSHR R2 R15 ; Protect R2
  3127. PUSHR R3 R15 ; Protect R3
  3128. LOAD32 R3 R0 24 ; PARENT->NAME for error
  3129. MOVE R2 R0 ; I = PARENT
  3130. :lookup_member_iter
  3131. LOAD32 R2 R2 16 ; I = I->MEMBERS
  3132. JUMP.Z R2 @lookup_member_error ; We failed hard
  3133. LOAD32 R0 R2 24 ; I->NAME
  3134. CALLI R15 @match ; IF I->NAME == NAME
  3135. JUMP.Z R0 @lookup_member_iter ; Loop again
  3136. :lookup_member_done
  3137. MOVE R0 R2 ; Put I in the correct place
  3138. POPR R3 R15 ; Restore R3
  3139. POPR R2 R15 ; Restore R2
  3140. POPR R1 R15 ; Restore R1
  3141. RET R15
  3142. :lookup_member_error
  3143. FALSE R1 ; Write to TTY
  3144. LOADUI R0 $lookup_member_string0 ; Our header string
  3145. CALLI R15 @file_print ; Print it
  3146. MOVE R0 R3 ; Using PARENT->NAME
  3147. CALLI R15 @file_print ; Print it
  3148. LOADUI R0 $arrow_string ; Using "->"
  3149. CALLI R15 @file_print ; Print it
  3150. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3151. CALLI R15 @file_print ; Print it
  3152. LOADUI R0 $lookup_member_string1 ; Our footer string
  3153. CALLI R15 @file_print ; Print it
  3154. CALLI R15 @line_error ; Give line info
  3155. LOADUI R0 $newline ; Our final addition
  3156. CALLI R15 @file_print ; Print it
  3157. HALT
  3158. :lookup_member_string0
  3159. "ERROR in lookup_member "
  3160. :lookup_member_string1
  3161. " does not exist
  3162. "
  3163. ;; build_member function
  3164. ;; Receives a struct type* in R0, int in R1 and int in R2
  3165. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3166. ;; Modifies R2 to current member_size
  3167. ;; Returns struct type* in R0
  3168. :build_member
  3169. PUSHR R3 R15 ; Protect R3
  3170. PUSHR R4 R15 ; Protect R4
  3171. PUSHR R5 R15 ; Protect R5
  3172. PUSHR R6 R15 ; Protect R6
  3173. MOVE R6 R1 ; Protect OFFSET
  3174. MOVE R4 R0 ; Protect LAST
  3175. CALLI R15 @type_name ; Get MEMBER_TYPE
  3176. MOVE R5 R0 ; Protect MEMBER_TYPE
  3177. ADDUI R3 R14 28 ; CALLOC struct type
  3178. SWAP R3 R14 ; SET I
  3179. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3180. STORE32 R0 R3 24 ; I->NAME = GLOBAL_TOKEN->S
  3181. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3182. STORE32 R4 R3 16 ; I->MEMBERS = LAST
  3183. LOADUI R0 $open_bracket ; Using "["
  3184. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  3185. CALLI R15 @match ; IF GLOBAL_TOKEN->S == "["
  3186. JUMP.Z R0 @build_member_single
  3187. ;; Deal with type name [ number ] ;
  3188. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3189. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3190. CALLI R15 @numerate_string ; Convert string to int NUMBER
  3191. LOAD32 R1 R5 20 ; MEMBER_TYPE->TYPE
  3192. LOAD32 R1 R1 4 ; MEMBER_TYPE->TYPE->SIZE
  3193. MULU R0 R0 R1 ; MEMBER_TYPE->TYPE->SIZE * NUMBER
  3194. STORE32 R0 R3 4 ; I->SIZE = MEMBER_TYPE->TYPE->SIZE * NUMBER
  3195. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3196. LOADUI R0 $build_member_string0 ; Our error message
  3197. LOADUI R1 $close_bracket ; Using "]"
  3198. CALLI R15 @require_match ; Make sure it is right
  3199. JUMP @build_member_done ; Skip over single steps
  3200. :build_member_single
  3201. LOAD32 R0 R5 4 ; MEMBER_TYPE->SIZE
  3202. STORE32 R0 R3 4 ; I->SIZE = MEMBER_TYPE->SIZE
  3203. :build_member_done
  3204. LOAD32 R2 R3 4 ; MEMBER_SIZE = I->SIZE
  3205. STORE32 R5 R3 20 ; I->TYPE = MEMBER_TYPE
  3206. STORE32 R6 R3 8 ; I->OFFSET = OFFSET
  3207. MOVE R1 R6 ; Restore OFFSET
  3208. MOVE R0 R3 ; RETURN I in R0
  3209. POPR R6 R15 ; Restore R6
  3210. POPR R5 R15 ; Restore R5
  3211. POPR R4 R15 ; Restore R4
  3212. POPR R3 R15 ; Restore R3
  3213. RET R15
  3214. :build_member_string0
  3215. "Struct only supports [num] form
  3216. "
  3217. ;; build_union function
  3218. ;; Receives a struct type* in R0, int in R1 and int in R2
  3219. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3220. ;; Modifies R2 to current member_size
  3221. ;; Returns struct type* in R0
  3222. :build_union
  3223. PUSHR R3 R15 ; Protect R3
  3224. PUSHR R4 R15 ; Protect R4
  3225. PUSHR R5 R15 ; Protect R5
  3226. MOVE R4 R0 ; Protect LAST
  3227. MOVE R3 R1 ; Protect OFFSET
  3228. FALSE R5 ; SIZE = 0
  3229. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3230. LOADUI R0 $build_union_string0 ; ERROR MESSAGE
  3231. LOADUI R1 $open_curly_brace ; OPEN CURLY BRACE
  3232. CALLI R15 @require_match ; Ensure we have that curly brace
  3233. :build_union_iter
  3234. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3235. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  3236. LOADUI R1 125 ; numerical value of }
  3237. CMPJUMPI.E R0 R1 @build_union_done ; No more looping required
  3238. MOVE R0 R4 ; We are passing last to be overwritten
  3239. MOVE R1 R3 ; We are also passing OFFSET
  3240. CALLI R15 @build_member ; To build_member to get new LAST and new member_size
  3241. CMPSKIP.LE R2 R5 ; If MEMBER_SIZE > SIZE
  3242. COPY R5 R2 ; SIZE = MEMMER_SIZE
  3243. MOVE R4 R0 ; Protect LAST
  3244. MOVE R3 R1 ; Protect OFFSET
  3245. LOADUI R0 $build_union_string1 ; ERROR MESSAGE
  3246. LOADUI R1 $semicolon ; SEMICOLON
  3247. CALLI R15 @require_match ; Ensure we have that curly brace
  3248. JUMP @build_union_iter ; Loop until we get that closing curly brace
  3249. :build_union_done
  3250. MOVE R2 R5 ; Setting MEMBER_SIZE = SIZE
  3251. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3252. MOVE R1 R3 ; Restore OFFSET
  3253. MOVE R0 R4 ; Restore LAST as we are turning that
  3254. POPR R5 R15 ; Restore R5
  3255. POPR R4 R15 ; Restore R4
  3256. POPR R3 R15 ; Restore R3
  3257. RET R15
  3258. :build_union_string0
  3259. "ERROR in build_union
  3260. Missing {
  3261. "
  3262. :build_union_string1
  3263. "ERROR in build_union
  3264. Missing ;
  3265. "
  3266. ;; create_struct function
  3267. ;; Receives Nothing
  3268. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3269. ;; Returns Nothing
  3270. :create_struct
  3271. PUSHR R0 R15 ; Protect R0
  3272. PUSHR R1 R15 ; Protect R1
  3273. PUSHR R2 R15 ; Protect R2
  3274. PUSHR R3 R15 ; Protect R3
  3275. PUSHR R4 R15 ; Protect R4
  3276. PUSHR R5 R15 ; Protect R5
  3277. PUSHR R6 R15 ; Protect R6
  3278. FALSE R5 ; OFFSET = 0
  3279. FALSE R2 ; MEMBER_SIZE = 0
  3280. COPY R3 R14 ; SET HEAD
  3281. ADDUI R14 R14 28 ; CALLOC struct type
  3282. COPY R4 R14 ; SET I
  3283. ADDUI R14 R14 28 ; CALLOC struct type
  3284. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3285. STORE32 R0 R3 24 ; HEAD->NAME = GLOBAL_TOKEN->S
  3286. STORE32 R0 R4 24 ; I->NAME = GLOBAL_TOKEN->S
  3287. STORE32 R4 R3 12 ; HEAD->INDIRECT = I
  3288. STORE32 R3 R4 12 ; I->INDIRECT - HEAD
  3289. LOADR32 R0 @global_types ; Get Address of GLOBAL_TYPES
  3290. STORE R0 R3 0 ; HEAD->NEXT = GLOBAL_TYPES
  3291. STORER32 R3 @global_types ; GLOBAL_TYPES = HEAD
  3292. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3293. LOADUI R0 4 ; Standard Pointer SIZE
  3294. STORE32 R0 R4 4 ; I->SIZE = 4
  3295. LOADUI R0 $create_struct_string0 ; ERROR MESSAGE
  3296. LOADUI R1 $open_curly_brace ; OPEN CURLY BRACE
  3297. CALLI R15 @require_match ; Ensure we have that curly brace
  3298. FALSE R6 ; LAST = NULL
  3299. :create_struct_iter
  3300. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3301. LOADU8 R0 R0 0 ; GLOBAL_TOKEN->S[0]
  3302. LOADUI R1 125 ; Numerical value of }
  3303. CMPJUMPI.E R0 R1 @create_struct_done ; Stop looping if match
  3304. LOADUI R1 $union ; Pointer to string UNION
  3305. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3306. CALLI R15 @match ; Check if they Match
  3307. SWAP R0 R6 ; Put LAST in place
  3308. MOVE R1 R5 ; Put OFFSET in place
  3309. JUMP.NZ R6 @create_struct_union ; Deal with union case
  3310. ;; Deal with standard member case
  3311. CALLI R15 @build_member ; Sets new LAST and MEMBER_SIZE
  3312. JUMP @create_struct_iter2 ; reset for loop
  3313. :create_struct_union
  3314. CALLI R15 @build_union
  3315. :create_struct_iter2
  3316. ADD R5 R1 R2 ; OFFSET = OFFSET + MEMBER_SIZE
  3317. SWAP R0 R6 ; Put LAST in place
  3318. LOADUI R0 $create_struct_string1 ; ERROR MESSAGE
  3319. LOADUI R1 $semicolon ; SEMICOLON
  3320. CALLI R15 @require_match ; Ensure we have that semicolon
  3321. JUMP @create_struct_iter ; Keep Looping
  3322. :create_struct_done
  3323. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3324. LOADUI R0 $create_struct_string1 ; ERROR MESSAGE
  3325. LOADUI R1 $semicolon ; SEMICOLON
  3326. CALLI R15 @require_match ; Ensure we have that semicolon
  3327. STORE32 R5 R3 4 ; HEAD->SIZE = OFFSET
  3328. STORE32 R6 R3 16 ; HEAD->MEMBERS = LAST
  3329. STORE32 R6 R4 16 ; I->MEMBERS = LAST
  3330. POPR R6 R15 ; Restore R6
  3331. POPR R5 R15 ; Restore R5
  3332. POPR R4 R15 ; Restore R4
  3333. POPR R3 R15 ; Restore R3
  3334. POPR R2 R15 ; Restore R2
  3335. POPR R1 R15 ; Restore R1
  3336. POPR R0 R15 ; Restore R0
  3337. RET R15
  3338. :create_struct_string0
  3339. "ERROR in create_struct
  3340. Missing {
  3341. "
  3342. :create_struct_string1
  3343. "ERROR in create_struct
  3344. Missing ;
  3345. "
  3346. ;; type_name function
  3347. ;; Receives Nothing
  3348. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3349. ;; Returns struct type* in R0
  3350. :type_name
  3351. PUSHR R1 R15 ; Protect R1
  3352. PUSHR R2 R15 ; Protect R2
  3353. LOADUI R0 $struct ; String for struct for comparison
  3354. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  3355. CALLI R15 @match ; Check if they match
  3356. CMPSKIPI.E R0 0 ; If STRUCTURE
  3357. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3358. LOAD32 R2 R13 8 ; GLOBAL_TOKEN->S
  3359. LOADR32 R1 @global_types ; Check using the GLOBAL TYPES LIST
  3360. SWAP R0 R2 ; Put GLOBAL_TOKEN->S in the right place
  3361. CALLI R15 @lookup_type ; RET = lookup_type(GLOBAL_TOKEN->S)
  3362. MOVE R1 R2 ; Put STRUCTURE in the right place
  3363. CMPSKIP.E R0 R1 ; If RET == NULL and !STRUCTURE
  3364. JUMP @type_name_struct ; Guess not
  3365. ;; Exit with useful error message
  3366. FALSE R1 ; We will want to be writing the error message for the Human
  3367. LOADUI R0 $type_name_string0 ; The first string
  3368. CALLI R15 @file_print ; Display it
  3369. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3370. CALLI R15 @file_print ; Display it
  3371. LOADUI R0 $newline ; Terminating linefeed
  3372. CALLI R15 @file_print ; Display it
  3373. CALLI R15 @line_error ; Give useful debug info
  3374. HALT ; Just exit
  3375. :type_name_struct
  3376. JUMP.NZ R0 @type_name_iter ; If was found
  3377. CALLI R15 @create_struct ; Otherwise create it
  3378. JUMP @type_name_done ; and be done
  3379. :type_name_iter
  3380. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3381. LOAD32 R1 R13 8 ; GLOBAL_TOKEN->S
  3382. LOADU8 R1 R1 0 ; GLOBAL_TOKEN->S[0]
  3383. CMPSKIPI.E R1 42 ; if GLOBAL_TOKEN->S[0] == '*'
  3384. JUMP @type_name_done ; Looks like Nope
  3385. LOAD32 R0 R0 12 ; RET = RET->INDIRECT
  3386. JUMP @type_name_iter ; Keep looping
  3387. :type_name_done
  3388. POPR R2 R15 ; Restore R2
  3389. POPR R1 R15 ; Restore R1
  3390. RET R15
  3391. :type_name_string0
  3392. "Unknown type "
  3393. ;; line_error function
  3394. ;; Receives Nothing
  3395. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3396. ;; Returns nothing
  3397. :line_error
  3398. PUSHR R0 R15 ; Protect R0
  3399. PUSHR R1 R15 ; Protect R1
  3400. LOADUI R0 $line_error_string0 ; Our leading string
  3401. FALSE R1 ; We want the user to see
  3402. CALLI R15 @file_print ; Print it
  3403. LOAD32 R0 R13 16 ; GLOBAL_TOKEN->LINENUMBER
  3404. CALLI R15 @numerate_number ; Get a string pointer for number
  3405. CALLI R15 @file_print ; And print it
  3406. POPR R1 R15 ; Restore R1
  3407. POPR R0 R15 ; Restore R0
  3408. RET R15
  3409. :line_error_string0
  3410. "In file: TTY1 On line: "
  3411. ;; require_match function
  3412. ;; Receives char* in R0 and char* in R1
  3413. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3414. ;; Returns Nothing
  3415. :require_match
  3416. PUSHR R0 R15 ; Protect R0
  3417. PUSHR R2 R15 ; Protect R2
  3418. MOVE R2 R0 ; Get MESSAGE out of the way
  3419. LOAD32 R0 R13 8 ; GLOBAL_TOKEN->S
  3420. CALLI R15 @match ; Check if GLOBAL_TOKEN->S == REQUIRED
  3421. JUMP.NZ R0 @require_match_done ; Looks like it was a match
  3422. ;; Terminate with an error
  3423. MOVE R0 R2 ; Put MESSAGE in required spot
  3424. FALSE R1 ; We want to write for user
  3425. CALLI R15 @file_print ; Write it
  3426. CALLI R15 @line_error ; And provide some debug info
  3427. HALT ; Then Stop immediately
  3428. :require_match_done
  3429. LOAD32 R13 R13 0 ; GLOBAL_TOKEN = GLOBAL_TOKEN->NEXT
  3430. POPR R2 R15 ; Restore R2
  3431. POPR R0 R15 ; Restore R0
  3432. RET R15
  3433. ;; numerate_number function
  3434. ;; Receives int in R0
  3435. ;; R13 Holds pointer to global_token, R14 is HEAP Pointer
  3436. ;; Returns pointer to string generated
  3437. :numerate_number
  3438. PUSHR R1 R15 ; Preserve R1
  3439. PUSHR R2 R15 ; Preserve R2
  3440. PUSHR R3 R15 ; Preserve R3
  3441. PUSHR R4 R15 ; Preserve R4
  3442. PUSHR R5 R15 ; Preserve R5
  3443. PUSHR R6 R15 ; Preserve R6
  3444. MOVE R3 R0 ; Move Integer out of the way
  3445. COPY R1 R14 ; Get pointer result
  3446. ADDUI R14 R14 16 ; CALLOC the 16 chars of space
  3447. FALSE R6 ; Set index to 0
  3448. JUMP.Z R3 @numerate_number_ZERO ; Deal with Special case of ZERO
  3449. JUMP.P R3 @numerate_number_Positive
  3450. LOADUI R0 45 ; Using -
  3451. STOREX8 R0 R1 R6 ; write leading -
  3452. ADDUI R6 R6 1 ; Increment by 1
  3453. NOT R3 R3 ; Flip into positive
  3454. ADDUI R3 R3 1 ; Adjust twos
  3455. :numerate_number_Positive
  3456. LOADR R2 @Max_Decimal ; Starting from the Top
  3457. LOADUI R5 10 ; We move down by 10
  3458. FALSE R4 ; Flag leading Zeros
  3459. :numerate_number_0
  3460. DIVIDE R0 R3 R3 R2 ; Break off top 10
  3461. CMPSKIPI.E R0 0 ; If Not Zero
  3462. TRUE R4 ; Flip the Flag
  3463. JUMP.Z R4 @numerate_number_1 ; Skip leading Zeros
  3464. ADDUI R0 R0 48 ; Shift into ASCII
  3465. STOREX8 R0 R1 R6 ; write digit
  3466. ADDUI R6 R6 1 ; Increment by 1
  3467. :numerate_number_1
  3468. DIV R2 R2 R5 ; Look at next 10
  3469. CMPSKIPI.E R2 0 ; If we reached the bottom STOP
  3470. JUMP @numerate_number_0 ; Otherwise keep looping
  3471. :numerate_number_done
  3472. FALSE R0 ; NULL Terminate
  3473. STOREX8 R0 R1 R6 ; write
  3474. MOVE R0 R1 ; Return pointer to our string
  3475. ;; Cleanup
  3476. POPR R6 R15 ; Restore R6
  3477. POPR R5 R15 ; Restore R5
  3478. POPR R4 R15 ; Restore R4
  3479. POPR R3 R15 ; Restore R3
  3480. POPR R2 R15 ; Restore R2
  3481. POPR R1 R15 ; Restore R1
  3482. RET R15
  3483. :numerate_number_ZERO
  3484. LOADUI R0 48 ; Using Zero
  3485. STOREX8 R0 R1 R6 ; write
  3486. ADDUI R6 R6 1 ; Increment by 1
  3487. JUMP @numerate_number_done ; Be done
  3488. :Max_Decimal
  3489. '3B9ACA00'
  3490. ;; numerate_string function
  3491. ;; Receives pointer To string in R0
  3492. ;; Returns number in R0 equal to value of string
  3493. ;; Or Zero in the event of invalid string
  3494. :numerate_string
  3495. PUSHR R1 R15 ; Protect R1
  3496. PUSHR R2 R15 ; Protect R2
  3497. PUSHR R3 R15 ; Protect R3
  3498. PUSHR R4 R15 ; Protect R4
  3499. ;; Initialize
  3500. MOVE R1 R0 ; Get Text pointer out of the way
  3501. FALSE R2 ; Set Negative flag to false
  3502. FALSE R3 ; Set current count to Zero
  3503. LOAD8 R0 R1 1 ; Get second byte
  3504. CMPSKIPI.NE R0 120 ; If the second byte is x
  3505. JUMP @numerate_string_hex ; treat string like hex
  3506. ;; Deal with Decimal input
  3507. LOADUI R4 10 ; Multiply by 10
  3508. LOAD8 R0 R1 0 ; Get a byte
  3509. CMPSKIPI.NE R0 45 ; If - toggle flag
  3510. TRUE R2 ; So that we know to negate
  3511. CMPSKIPI.E R2 0 ; If toggled
  3512. ADDUI R1 R1 1 ; Move to next
  3513. :numerate_string_dec
  3514. LOAD8 R0 R1 0 ; Get a byte
  3515. CMPSKIPI.NE R0 0 ; If NULL
  3516. JUMP @numerate_string_done ; Be done
  3517. MUL R3 R3 R4 ; Shift counter by 10
  3518. SUBI R0 R0 48 ; Convert ascii to number
  3519. CMPSKIPI.GE R0 0 ; If less than a number
  3520. JUMP @numerate_string_done ; Terminate NOW
  3521. CMPSKIPI.L R0 10 ; If more than a number
  3522. JUMP @numerate_string_done ; Terminate NOW
  3523. ADDU R3 R3 R0 ; Don't add to the count
  3524. ADDUI R1 R1 1 ; Move onto next byte
  3525. JUMP @numerate_string_dec
  3526. ;; Deal with Hex input
  3527. :numerate_string_hex
  3528. LOAD8 R0 R1 0 ; Get a byte
  3529. CMPSKIPI.E R0 48 ; All hex strings start with 0x
  3530. JUMP @numerate_string_done ; Be done if not a match
  3531. ADDUI R1 R1 2 ; Move to after leading 0x
  3532. :numerate_string_hex_0
  3533. LOAD8 R0 R1 0 ; Get a byte
  3534. CMPSKIPI.NE R0 0 ; If NULL
  3535. JUMP @numerate_string_done ; Be done
  3536. SL0I R3 4 ; Shift counter by 16
  3537. SUBI R0 R0 48 ; Convert ascii number to number
  3538. CMPSKIPI.L R0 10 ; If A-F
  3539. SUBI R0 R0 7 ; Shove into Range
  3540. CMPSKIPI.L R0 16 ; If a-f
  3541. SUBI R0 R0 32 ; Shove into Range
  3542. ADDU R3 R3 R0 ; Add to the count
  3543. ADDUI R1 R1 1 ; Get next Hex
  3544. JUMP @numerate_string_hex_0
  3545. ;; Clean up
  3546. :numerate_string_done
  3547. CMPSKIPI.E R2 0 ; If Negate flag has been set
  3548. NEG R3 R3 ; Make the number negative
  3549. MOVE R0 R3 ; Put number in R0
  3550. POPR R4 R15 ; Restore R4
  3551. POPR R3 R15 ; Restore R3
  3552. POPR R2 R15 ; Restore R2
  3553. POPR R1 R15 ; Restore R1
  3554. RET R15
  3555. ;; Keywords
  3556. :union
  3557. "union"
  3558. :struct
  3559. "struct"
  3560. :constant
  3561. "CONSTANT"
  3562. :main_string
  3563. "main"
  3564. :argc_string
  3565. "argc"
  3566. :argv_string
  3567. "argv"
  3568. :if_string
  3569. "if"
  3570. :else_string
  3571. "else"
  3572. :do_string
  3573. "do"
  3574. :while_string
  3575. "while"
  3576. :for_string
  3577. "for"
  3578. :asm_string
  3579. "asm"
  3580. :goto_string
  3581. "goto"
  3582. :return_string
  3583. "return"
  3584. :break_string
  3585. "break"
  3586. :continue_string
  3587. "continue"
  3588. :sizeof_string
  3589. "sizeof"
  3590. :plus_string
  3591. "+"
  3592. :minus_string
  3593. "-"
  3594. :multiply_string
  3595. "*"
  3596. :divide_string
  3597. "/"
  3598. :modulus_string
  3599. "%"
  3600. :left_shift_string
  3601. "<<"
  3602. :right_shift_string
  3603. ">>"
  3604. :less_than_string
  3605. "<"
  3606. :less_than_equal_string
  3607. "<="
  3608. :greater_than_equal_string
  3609. ">="
  3610. :greater_than_string
  3611. ">"
  3612. :equal_to_string
  3613. "=="
  3614. :not_equal_string
  3615. "!="
  3616. :bitwise_and
  3617. "&"
  3618. :logical_and
  3619. "&&"
  3620. :bitwise_or
  3621. "|"
  3622. :logical_or
  3623. "||"
  3624. :bitwise_xor
  3625. "^"
  3626. :arrow_string
  3627. "->"
  3628. ;; Frequently Used strings
  3629. ;; Generally used by require_match
  3630. :open_curly_brace
  3631. "{"
  3632. :close_curly_brace
  3633. "}"
  3634. :open_paren
  3635. "("
  3636. :close_paren
  3637. ")"
  3638. :open_bracket
  3639. "["
  3640. :close_bracket
  3641. "]"
  3642. :semicolon
  3643. ";"
  3644. :equal
  3645. "="
  3646. :percent
  3647. "%"
  3648. :newline
  3649. "
  3650. "
  3651. :not
  3652. "!"
  3653. :underline
  3654. "_"
  3655. ;; Global types
  3656. ;; NEXT (0), SIZE (4), OFFSET (8), INDIRECT (12), MEMBERS (16), TYPE (20), NAME (24)
  3657. :global_types
  3658. &type_void
  3659. :prim_types
  3660. :type_void
  3661. &type_int ; NEXT
  3662. '00 00 00 04' ; SIZE
  3663. NOP ; OFFSET
  3664. &type_void ; INDIRECT
  3665. NOP ; MEMBERS
  3666. &type_void ; TYPE
  3667. &type_void_name ; NAME
  3668. :type_void_name
  3669. "void"
  3670. :type_int
  3671. &type_char ; NEXT
  3672. '00 00 00 04' ; SIZE
  3673. NOP ; OFFSET
  3674. &type_int ; INDIRECT
  3675. NOP ; MEMBERS
  3676. &type_int ; TYPE
  3677. &type_int_name ; NAME
  3678. :type_int_name
  3679. "int"
  3680. :type_char
  3681. &type_file ; NEXT
  3682. '00 00 00 01' ; SIZE
  3683. NOP ; OFFSET
  3684. &type_char_indirect ; INDIRECT
  3685. NOP ; MEMBERS
  3686. &type_char ; TYPE
  3687. &type_char_name ; NAME
  3688. :type_char_name
  3689. "char"
  3690. :type_char_indirect
  3691. &type_file ; NEXT
  3692. '00 00 00 04' ; SIZE
  3693. NOP ; OFFSET
  3694. &type_char_double_indirect ; INDIRECT
  3695. NOP ; MEMBERS
  3696. &type_char_indirect ; TYPE
  3697. &type_char_indirect_name ; NAME
  3698. :type_char_indirect_name
  3699. "char*"
  3700. :type_char_double_indirect
  3701. &type_file ; NEXT
  3702. '00 00 00 04' ; SIZE
  3703. NOP ; OFFSET
  3704. &type_char_double_indirect ; INDIRECT
  3705. NOP ; MEMBERS
  3706. &type_char_indirect ; TYPE
  3707. &type_char_double_indirect_name ; NAME
  3708. :type_char_double_indirect_name
  3709. "char**"
  3710. :type_file
  3711. &type_function ; NEXT
  3712. '00 00 00 04' ; SIZE
  3713. NOP ; OFFSET
  3714. &type_file ; INDIRECT
  3715. NOP ; MEMBERS
  3716. &type_file ; TYPE
  3717. &type_file_name ; NAME
  3718. :type_file_name
  3719. "FILE"
  3720. :type_function
  3721. &type_unsigned ; NEXT
  3722. '00 00 00 04' ; SIZE
  3723. NOP ; OFFSET
  3724. &type_function ; INDIRECT
  3725. NOP ; MEMBERS
  3726. &type_function ; TYPE
  3727. &type_function_name ; NAME
  3728. :type_function_name
  3729. "FUNCTION"
  3730. :type_unsigned
  3731. NOP ; NEXT (NULL)
  3732. '00 00 00 04' ; SIZE
  3733. NOP ; OFFSET
  3734. &type_unsigned ; INDIRECT
  3735. NOP ; MEMBERS
  3736. &type_unsigned ; TYPE
  3737. &type_unsigned_name ; NAME
  3738. :type_unsigned_name
  3739. "unsigned"
  3740. ;; debug_list function
  3741. ;; Receives struct token_list* in R0
  3742. ;; Prints contents of list and HALTS
  3743. ;; Does not return
  3744. :debug_list
  3745. MOVE R9 R0 ; Protect the list Pointer
  3746. FALSE R1 ; Write to TTY
  3747. :debug_list_iter
  3748. ;; Header
  3749. LOADUI R0 $debug_list_string0 ; Using our first string
  3750. CALLI R15 @file_print ; Print it
  3751. COPY R0 R9 ; Use address of pointer
  3752. CALLI R15 @numerate_number ; Convert it into a string
  3753. CALLI R15 @file_print ; Print it
  3754. ;; NEXT
  3755. LOADUI R0 $debug_list_string1 ; Using our second string
  3756. CALLI R15 @file_print ; Print it
  3757. LOAD32 R0 R9 0 ; Use address of pointer
  3758. CALLI R15 @numerate_number ; Convert it into a string
  3759. CALLI R15 @file_print ; Print it
  3760. ;; PREV
  3761. LOADUI R0 $debug_list_string2 ; Using our third string
  3762. CALLI R15 @file_print ; Print it
  3763. LOAD32 R0 R9 4 ; Use address of pointer
  3764. CALLI R15 @numerate_number ; Convert it into a string
  3765. CALLI R15 @file_print ; Print it
  3766. ;; S
  3767. LOADUI R0 $debug_list_string3 ; Using our fourth string
  3768. CALLI R15 @file_print ; Print it
  3769. LOAD32 R0 R9 8 ; Use address of pointer
  3770. CALLI R15 @numerate_number ; Convert it into a string
  3771. CALLI R15 @file_print ; Print it
  3772. ;; S Contents
  3773. LOADUI R0 $debug_list_string4 ; Using our Prefix string
  3774. CALLI R15 @file_print ; Print it
  3775. LOAD32 R0 R9 8 ; Use address of pointer
  3776. CMPSKIPI.NE R0 0 ; If NULL Pointer
  3777. LOADUI R0 $debug_list_string_null ; Give meaningful message instead
  3778. CALLI R15 @file_print ; Print it
  3779. ;; TYPE
  3780. LOADUI R0 $debug_list_string5 ; Using our fifth string
  3781. CALLI R15 @file_print ; Print it
  3782. LOAD32 R0 R9 12 ; Use address of pointer
  3783. CALLI R15 @numerate_number ; Convert it into a string
  3784. CALLI R15 @file_print ; Print it
  3785. ;; PREV
  3786. LOADUI R0 $debug_list_string6 ; Using our sixth string
  3787. CALLI R15 @file_print ; Print it
  3788. LOAD32 R0 R9 16 ; Use address of pointer
  3789. CALLI R15 @numerate_number ; Convert it into a string
  3790. CALLI R15 @file_print ; Print it
  3791. ;; Add some space
  3792. LOADUI R0 10 ; Using NEWLINE
  3793. FPUTC
  3794. FPUTC
  3795. ;; Iterate if next not NULL
  3796. LOAD32 R9 R9 0 ; TOKEN = TOKEN->NEXT
  3797. JUMP.NZ R9 @debug_list_iter
  3798. ;; Looks lke we are done, wrap it up
  3799. HALT
  3800. :debug_list_string0
  3801. "Token_list node at address: "
  3802. :debug_list_string1
  3803. "
  3804. NEXT address: "
  3805. :debug_list_string2
  3806. "
  3807. PREV address: "
  3808. :debug_list_string3
  3809. "
  3810. S address: "
  3811. :debug_list_string4
  3812. "
  3813. The contents of S are: "
  3814. :debug_list_string5
  3815. "
  3816. TYPE address: "
  3817. :debug_list_string6
  3818. "
  3819. ARGUMENTS address: "
  3820. :debug_list_string_null
  3821. ">::<NULL>::<"
  3822. :STACK