Compiler.mod 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712
  1. IMPLEMENTATION MODULE Compiler ;
  2. (* Turbo Pascal 3-style single-pass Pascal -> 8086 compiler in
  3. GNU Modula-2 (-fiso), following the structure of the original
  4. TPSRC6 'turbo' entry / TPSRC7-10:
  5. Inittur reset state, pre-defined types, scratch temporaries
  6. Skip lexer: blanks, comments { } and (* *), directives {$ }
  7. GetWord/WddTok/MatchKey word lexing with a keyword table (kName/kTk)
  8. PeekKw lookahead keyword check WITHOUT consuming (via saved
  9. srcPos) - needed because declarations and compound
  10. statements peek at END/ELSE/etc
  11. RdIntConst/RdConst integer, hex and char constants
  12. Search symbol table lookup filtered by lexical level
  13. ParseExpr -> ParseCmp -> ParseAdd -> ParseMul -> ParseNeg
  14. -> ParseAtom precedence climb (TPSRC9)
  15. Statmnt statements: if/while/repeat/for/case/goto/exit/begin
  16. assignment and calls (TPSRC8)
  17. ParseType/Decls ARRAY, STRING, scalar/subrange types; variable,
  18. constant, label and procedure/function definitions
  19. Compile driver: optional PROGRAM header, DefPart, progpart,
  20. final '.', header size patch, patch resolution.
  21. Ebyte/Eword/Ecall/Ejmp + patch list code emission (TPSRC10).
  22. The emitted image is a byte array (mode word, CS/DS, size words,
  23. CALL initmem, MOV BP,SP, then generated code). Forward labels and
  24. forward procedure calls resolve through a patch list (ptc records).
  25. Working subset (v0.4): integer/char/boolean/byte scalars, constants
  26. with folding, globals, locals, value parameters, procedures and
  27. scalar-result functions, ARRAY[const..const] with constant indexing,
  28. control flow, GOTO/EXIT, and the standard procedures WRITE, WRITELN,
  29. READ, READLN, HALT (DefBuiltins + IoCall).
  30. The standard procedures are dispatched per argument, as the original
  31. does (TPSRC8 pwriteln/pwrloop inspects each argument's class and emits
  32. a different call per type), so the runtime is handed a value and never
  33. a descriptor.
  34. Still not implemented: real/set/record/file and the string runtime
  35. raise Err (ENoLib) - the original's "not implemented" path. The
  36. runtime blob itself, the linker that rebases the TU_* entry offsets
  37. by the runtime's size, and CmdRun (the interpreter) are still pending,
  38. so a compiled image cannot be executed yet. *)
  39. FROM TextBuf IMPORT Length, CharAt ;
  40. FROM SYSTEM IMPORT BYTE ;
  41. FROM Runtime IMPORT RT_Build, RT_Size, RT_Byte, RT_Entry, LoadBias ;
  42. (* The runtime is copied to the front of the code buffer and pc/dc start past
  43. it, so every emitted address is image-absolute and no relocation pass is
  44. needed. See Inittur.
  45. `LoadBias' comes from Runtime because it is the same constant on both
  46. sides of the image: Runtime adds it to every data address it bakes into its
  47. own code (FixUp, kind 2), and this module adds it to every ABSOLUTE address
  48. it bakes into the program's. It is deliberately ONE constant in ONE place
  49. rather than 0100h written out at six sites, because getting it wrong at one
  50. site is invisible - see the note on LoadBias in Runtime.mod. Relative
  51. encodings (the entry JMP, every CALL and JMP) must NOT get it: both
  52. operands shift together and the +0100h cancels. *)
  53. (* ---------------------------------------------------------------- *)
  54. (* constants *)
  55. (* ---------------------------------------------------------------- *)
  56. CONST
  57. MaxLine = 128 ;
  58. MaxName = 31 ;
  59. (* Size of the entry jump at image offset 0: E9 lo hi. The jump's
  60. displacement is relative to the END of the jump, so every offset in the
  61. image is EntSize further along than it was before the jump existed. *)
  62. EntSize = 3 ;
  63. MaxCode = 24000 ;
  64. MaxSym = 3000 ;
  65. MaxPatch = 2000 ;
  66. MaxPend = 400 ;
  67. (* type codes (TP3 vartp) *)
  68. TNone = 0 ; TArray = 1 ; TRecord = 2 ; TSet = 3 ;
  69. TPtr = 4 ; TFile = 5 ; TText = 6 ; TUntyp = 7 ;
  70. TString = 8 ; TReal = 9 ; TScalar = 10 ; TBool = 11 ;
  71. (* CHAR needs a class of its own. It used to be registered as TScalar,
  72. which made a CHAR variable indistinguishable from an INTEGER one: the
  73. class is all IoCall has to dispatch on, so `write(c)` called wrint (the
  74. value 65 became the *address* 65 and it printed whatever lived at 0x41)
  75. and `readln(c)` called rdint (which stores a 16-bit result, so it wrote
  76. two bytes into a one-byte variable). TP3 TPSRC8 prdtyped/pwriteln
  77. dispatches on the type identifier for exactly this reason. BYTE stays
  78. TScalar: a BYTE is written as an integer, as in TP3. *)
  79. TChar = 12 ;
  80. (* symbol kinds *)
  81. KLabel = 100H ; KConst = 200H ; KType = 300H ;
  82. KVar = 400H ; KProc = 500H ; KFunc = 600H ;
  83. KBuiltin = 700H ; (* standard procedure, see BI_* below *)
  84. (* which standard procedure a KBuiltin symbol denotes *)
  85. BI_Write = 0 ; BI_WriteLn = 1 ; BI_Read = 2 ;
  86. BI_ReadLn = 3 ; BI_Halt = 4 ;
  87. (* keyword tokens *)
  88. TkNone = 0 ; TkProgram = 1 ; TkBegin = 2 ; TkEnd = 3 ;
  89. TkIf = 4 ; TkThen = 5 ; TkElse = 6 ; TkWhile = 7 ;
  90. TkDo = 8 ; TkRepeat = 9 ; TkUntil = 10 ; TkFor = 11 ;
  91. TkTo = 12 ; TkDownto = 13 ; TkCase = 14 ; TkOf = 15 ;
  92. TkGoto = 16 ; TkExit = 17 ; TkWith = 18 ; TkVar = 19 ;
  93. TkConst= 20 ; TkType = 21 ; TkLabel = 22 ; TkProcedure = 23 ;
  94. TkFunction = 24 ; TkNil = 25 ; TkAnd = 26 ; TkOr = 27 ;
  95. TkNot = 28 ; TkDiv = 29 ; TkMod = 30 ; TkIn = 31 ;
  96. TkFile = 32 ; TkText = 33 ; TkRecord = 34 ; TkArray = 35 ;
  97. TkSet = 36 ; TkPacked = 37 ; TkForward = 38 ; TkExternal = 39 ;
  98. TkAbsolute = 40 ; TkOverlay = 41 ; TkString = 42 ;
  99. (* Codes for the SYMBOL operators, as BinOpEmit numbers them. The word
  100. operators carry their own Tk* token and need no code here.
  101. These are named, not bare numbers, because every precedence level's
  102. parser picks its own code out of the same set and BinOpEmit cannot see
  103. which level called it. ParseAdd chose 1 for '+' and ParseMul chose 1
  104. for '*', so every multiplication dispatched to EmAddAxCx and a * b
  105. compiled to a + b. Only the constant-folding path was right, which is
  106. why n * n with n a CONST was correct and a * b with a a variable was
  107. not. *)
  108. OpAdd = 1 ; OpSub = 2 ; OpMul = 3 ;
  109. (* TP3 error numbers *)
  110. ENoSemi = 1 ; EPointExp = 10 ; ESimpType = 30 ;
  111. EUnknown = 41 ; EConstRange = 45 ; EMemOvf = 98 ;
  112. ECompOvf = 99 ; ENoLib = 102 ; ETypeErr = 56 ;
  113. AposC = 39 ; (* ORD ("'") - avoids quote-in-quote *)
  114. (* ---------------------------------------------------------------- *)
  115. (* types *)
  116. (* ---------------------------------------------------------------- *)
  117. TYPE
  118. SymEntry =
  119. RECORD
  120. name : ARRAY [0..MaxName] OF CHAR ;
  121. tag : CARDINAL ;
  122. cls : CARDINAL ;
  123. size : CARDINAL ;
  124. elem : CARDINAL ;
  125. off : CARDINAL ;
  126. lval : LONGINT ;
  127. level : CARDINAL ;
  128. local : BOOLEAN ;
  129. resvar : CARDINAL ;
  130. goPos : CARDINAL ;
  131. defnd : BOOLEAN ;
  132. fwd : BOOLEAN ;
  133. END ;
  134. PatchRec =
  135. RECORD
  136. place : CARDINAL ;
  137. target : CARDINAL ;
  138. filled : BOOLEAN ;
  139. END ;
  140. PendRec =
  141. RECORD
  142. kind : CARDINAL ; (* 0 goto, 1 call *)
  143. who : CARDINAL ;
  144. place : CARDINAL ; (* patch slot index *)
  145. END ;
  146. ERes =
  147. RECORD
  148. cls : CARDINAL ;
  149. kind : CARDINAL ; (* 0 const, 1 var, 2 value in AX,
  150. 3 string literal (cls = TString) *)
  151. imm : LONGINT ;
  152. idx : CARDINAL ;
  153. boff : CARDINAL ; (* constant fold-in for subscripts *)
  154. chr : BOOLEAN ; (* single-quoted literal, e.g. 'a' *)
  155. strx : CARDINAL ; (* string literal: index into strPool *)
  156. END ;
  157. DirRec = RECORD rng, chk : BOOLEAN END ;
  158. (* ---------------------------------------------------------------- *)
  159. (* state *)
  160. (* ---------------------------------------------------------------- *)
  161. VAR
  162. srcPos, srcLen : CARDINAL ;
  163. wrd : ARRAY [0..MaxName] OF CHAR ;
  164. (* String-literal pool.
  165. TP3 does not put a literal in the data segment at all: it emits the
  166. literal *inline in the code stream* as <length byte><characters>, and
  167. the runtime entry "wrtinl" reads the length from the return address and
  168. returns to just past the last character (TPSRC4 xwrtinl, TPSRC10
  169. estring). So nothing here ends up in the image as data - the pool only
  170. has to survive from the moment the literal is scanned until IoCall
  171. decides to emit it, because by then the parser has moved on. *)
  172. strPool : ARRAY [0..4095] OF CHAR ;
  173. strOff : ARRAY [0..255] OF CARDINAL ;
  174. strLen : ARRAY [0..255] OF CARDINAL ;
  175. strTop : CARDINAL ; (* next free byte in strPool *)
  176. strCnt : CARDINAL ; (* literals collected so far *)
  177. rdStrX : CARDINAL ; (* pool index of the literal RdConst just read *)
  178. symtab : ARRAY [0..MaxSym - 1] OF SymEntry ;
  179. symTop : CARDINAL ;
  180. patches : ARRAY [0..MaxPatch - 1] OF PatchRec ;
  181. nPatch : CARDINAL ;
  182. pend : ARRAY [0..MaxPend - 1] OF PendRec ;
  183. nPend : CARDINAL ;
  184. exitPatch : ARRAY [0..63] OF CARDINAL ;
  185. exitCnt : CARDINAL ;
  186. brkSave : ARRAY [0..15] OF CARDINAL ;
  187. loopTy : ARRAY [0..15] OF CARDINAL ; (* 1 while/repeat, 2 for *)
  188. brkN : CARDINAL ;
  189. caseJmp : ARRAY [0..63] OF CARDINAL ;
  190. caseN : CARDINAL ;
  191. pc, dc : CARDINAL ;
  192. varspc : CARDINAL ;
  193. cbuf : ARRAY [0..MaxCode - 1] OF BYTE ;
  194. codeSz, dataSz : CARDINAL ;
  195. (* Image layout, all image-absolute. rtSz is where the runtime ends and
  196. the program header begins; dataBase is where the data area begins
  197. (rtSz + 1000H, a fixed 4 KiB above the code). codeSz and dataSz are
  198. PROGRAM sizes - the runtime is excluded - so the numbers the fixture
  199. table pins keep meaning what they meant before the runtime was
  200. prepended. *)
  201. rtSz, dataBase : CARDINAL ;
  202. (* Image offset of the entry jump's rel16 operand, patched at the end of
  203. Compile. The jump is at image offset 0, so its displacement is simply
  204. the program code's end - 3. *)
  205. entRel, prologAt : CARDINAL ;
  206. (* Runtime entry offsets as IMAGE-ABSOLUTE addresses, which is what
  207. EmCall and EmJmp want. They are derived from Runtime.RT_Entry in
  208. Inittur (after RT_Build, since RT_Entry only knows where the code
  209. landed once the blob is assembled) rather than written down, so a moved
  210. entry cannot leave the compiler calling the old address. Not a CONST
  211. block because RT_Entry is a function.
  212. Standard-procedure entries: TP3 does NOT pass a descriptor -
  213. TPSRC8 pwriteln/pwrloop inspects each argument's class in CL and emits
  214. a *different* call per type, so the type is fixed at compile time and
  215. the runtime needs only the value. Mirrored here. *)
  216. TU_InitMem : CARDINAL ;
  217. TU_ProgEnd : CARDINAL ;
  218. TU_StackChk : CARDINAL ;
  219. TU_WrInt : CARDINAL ; TU_WrChar : CARDINAL ; TU_WrBool : CARDINAL ;
  220. TU_WrReal : CARDINAL ; TU_WrLn : CARDINAL ;
  221. TU_RdInt : CARDINAL ; TU_RdChar : CARDINAL ; TU_RdBool : CARDINAL ;
  222. TU_RdLn : CARDINAL ; TU_Halt : CARDINAL ;
  223. TU_WrInl : CARDINAL ; (* inline string literal; NO stack argument *)
  224. abortFac : BOOLEAN ;
  225. errNum : CARDINAL ; (* NOT "errNo": Compile's formal of that
  226. name would shadow it, and the caller's
  227. errNo would never be filled in *)
  228. txerrPos : CARDINAL ;
  229. lexnest : CARDINAL ;
  230. curIsFunc : BOOLEAN ;
  231. resultVar : CARDINAL ;
  232. locFree : CARDINAL ; (* next local slot (BP-relative, 8-bit) *)
  233. locBytes : CARDINAL ; (* frame size for SUB SP *)
  234. parmOff : CARDINAL ; (* provisional parameter slot, in declaration
  235. order; remapped to TP3's BP+4 rule when the
  236. list has been read (see ProcFunc) *)
  237. dirs : DirRec ;
  238. tmpA, tmpB : CARDINAL ; (* global scratch word addresses *)
  239. hdrFlag, hdrCS, hdrDS, hdrHeap, hdrMax : CARDINAL ;
  240. kName : ARRAY [0..42] OF ARRAY [0..15] OF CHAR ;
  241. kTk : ARRAY [0..42] OF CARDINAL ;
  242. (* ---------------------------------------------------------------- *)
  243. (* small char helpers *)
  244. (* ---------------------------------------------------------------- *)
  245. PROCEDURE CurCh () : CHAR ;
  246. BEGIN
  247. IF srcPos >= srcLen THEN
  248. RETURN 0C
  249. END ;
  250. RETURN CharAt (srcPos)
  251. END CurCh ;
  252. PROCEDURE GetCh () : CHAR ;
  253. VAR ch : CHAR ;
  254. BEGIN
  255. ch := CurCh () ;
  256. IF srcPos < srcLen THEN
  257. INC (srcPos)
  258. END ;
  259. RETURN ch
  260. END GetCh ;
  261. PROCEDURE PeekAhead (k : CARDINAL) : CHAR ;
  262. BEGIN
  263. IF srcPos + k >= srcLen THEN
  264. RETURN 0C
  265. END ;
  266. RETURN CharAt (srcPos + k)
  267. END PeekAhead ;
  268. PROCEDURE Digit (ch : CHAR) : BOOLEAN ;
  269. BEGIN
  270. RETURN (ch >= '0') AND (ch <= '9')
  271. END Digit ;
  272. PROCEDURE Alpha (ch : CHAR) : BOOLEAN ;
  273. BEGIN
  274. RETURN ((ch >= 'A') AND (ch <= 'Z')) OR ((ch >= 'a') AND (ch <= 'z'))
  275. OR (ch = '_')
  276. END Alpha ;
  277. PROCEDURE AlphaNum (ch : CHAR) : BOOLEAN ;
  278. BEGIN
  279. RETURN (Alpha (ch)) OR (Digit (ch))
  280. END AlphaNum ;
  281. PROCEDURE IsHexCh (ch : CHAR) : BOOLEAN ;
  282. BEGIN
  283. RETURN (Digit (ch)) OR ((ch >= 'A') AND (ch <= 'F'))
  284. OR ((ch >= 'a') AND (ch <= 'f'))
  285. END IsHexCh ;
  286. PROCEDURE Upper (ch : CHAR) : CHAR ;
  287. BEGIN
  288. IF (ch >= 'a') AND (ch <= 'z') THEN
  289. RETURN CHR (ORD (ch) - ORD ('a') + ORD ('A'))
  290. END ;
  291. RETURN ch
  292. END Upper ;
  293. PROCEDURE W16 (x : LONGINT) : CARDINAL ;
  294. (* fold x modulo 10000H, handling negatives (no negative MOD) *)
  295. VAR m : CARDINAL ;
  296. BEGIN
  297. IF x >= 0 THEN
  298. RETURN VAL (CARDINAL, x MOD 10000H)
  299. END ;
  300. m := VAL (CARDINAL, (0 - x) MOD 10000H) ;
  301. RETURN (10000H - m) MOD 10000H
  302. END W16 ;
  303. PROCEDURE DropCh (v : CHAR) ;
  304. BEGIN
  305. END DropCh ;
  306. PROCEDURE DropB (v : BOOLEAN) ;
  307. BEGIN
  308. END DropB ;
  309. PROCEDURE DropC (v : CARDINAL) ;
  310. BEGIN
  311. END DropC ;
  312. PROCEDURE BitAnd (a, b : LONGINT) : LONGINT ;
  313. BEGIN
  314. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, W16 (a))
  315. * VAL (BITSET, W16 (b))))
  316. END BitAnd ;
  317. PROCEDURE BitOr (a, b : LONGINT) : LONGINT ;
  318. BEGIN
  319. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, W16 (a))
  320. + VAL (BITSET, W16 (b))))
  321. END BitOr ;
  322. PROCEDURE BitNot (a : LONGINT) : LONGINT ;
  323. BEGIN
  324. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, 0FFFFH)
  325. - VAL (BITSET, W16 (a))))
  326. END BitNot ;
  327. (* ---------------------------------------------------------------- *)
  328. (* errors *)
  329. (* ---------------------------------------------------------------- *)
  330. PROCEDURE Err (n : CARDINAL) ;
  331. BEGIN
  332. IF NOT abortFac THEN
  333. abortFac := TRUE ;
  334. errNum := n ;
  335. txerrPos := srcPos
  336. END
  337. END Err ;
  338. PROCEDURE OK () : BOOLEAN ;
  339. BEGIN
  340. RETURN NOT abortFac
  341. END OK ;
  342. (* ---------------------------------------------------------------- *)
  343. (* emission : ebyte / eword / ecall / ejump *)
  344. (* ---------------------------------------------------------------- *)
  345. PROCEDURE Ebyte (b : BYTE) ;
  346. BEGIN
  347. IF pc >= MaxCode THEN
  348. Err (EMemOvf)
  349. ELSE
  350. cbuf [pc] := b ;
  351. INC (pc)
  352. END
  353. END Ebyte ;
  354. PROCEDURE Eword (w : CARDINAL) ;
  355. BEGIN
  356. Ebyte (VAL (BYTE, w MOD 100H)) ;
  357. Ebyte (VAL (BYTE, (w DIV 100H) MOD 100H))
  358. END Eword ;
  359. PROCEDURE PatchWord (at, w : CARDINAL) ;
  360. (* Store a 16-bit word into cbuf at an absolute offset.
  361. A helper, because writing this inline got it wrong in all four header
  362. words: the low byte was (w DIV 16) MOD 100H, which is a NIBBLE shift, not
  363. the byte shift (w MOD 100H). So 1181h - the data base - was stored as
  364. 0118h = 280. It was invisible for as long as nothing read those words,
  365. which is exactly what "write it inline once and trust it" buys you. *)
  366. BEGIN
  367. cbuf [at] := VAL (BYTE, w MOD 100H) ;
  368. cbuf [at + 1] := VAL (BYTE, (w DIV 100H) MOD 100H)
  369. END PatchWord ;
  370. PROCEDURE AddPatch (place, target : CARDINAL) ;
  371. BEGIN
  372. IF nPatch < MaxPatch THEN
  373. patches [nPatch].place := place ;
  374. patches [nPatch].target := target ;
  375. patches [nPatch].filled := FALSE ;
  376. INC (nPatch)
  377. ELSE
  378. Err (ECompOvf)
  379. END
  380. END AddPatch ;
  381. PROCEDURE SetPatTgt (idx, t : CARDINAL) ;
  382. BEGIN
  383. IF idx < nPatch THEN
  384. patches [idx].target := t
  385. END
  386. END SetPatTgt ;
  387. PROCEDURE EmCall (target : CARDINAL) : CARDINAL ;
  388. (* E8 rel16 near call; target = 0 => forward (patched later).
  389. Returns the patch slot, or 0 when resolved directly. *)
  390. VAR rel, p : CARDINAL ;
  391. BEGIN
  392. Ebyte (0E8H) ;
  393. IF target = 0 THEN
  394. Eword (0) ;
  395. p := nPatch ;
  396. AddPatch (pc - 2, 0) ;
  397. RETURN p
  398. END ;
  399. (* rel16 is measured from the END of the instruction. Here pc already
  400. points past the opcode(s) and at the displacement field, so the
  401. instruction ends at pc+2 - the same convention ResolvePatches uses
  402. with "place + 2". Omitting the +2 lands every direct call/jump 2 bytes
  403. past its target. *)
  404. rel := (target + 10000H - (pc + 2)) MOD 10000H ;
  405. Eword (rel) ;
  406. RETURN 0
  407. END EmCall ;
  408. PROCEDURE EmJmpNear (target : CARDINAL) : CARDINAL ;
  409. (* E9 rel16; target = 0 => forward. Returns patch slot or 0. *)
  410. VAR rel, p : CARDINAL ;
  411. BEGIN
  412. Ebyte (0E9H) ;
  413. IF target = 0 THEN
  414. Eword (0) ;
  415. p := nPatch ;
  416. AddPatch (pc - 2, 0) ;
  417. RETURN p
  418. END ;
  419. rel := (target + 10000H - (pc + 2)) MOD 10000H ; (* see EmCall *)
  420. Eword (rel) ;
  421. RETURN 0
  422. END EmJmpNear ;
  423. PROCEDURE JccShort (cc : BYTE) : BYTE ;
  424. (* The 8086 SHORT Jcc opcode for a condition nibble. 70h..7Fh is exactly
  425. 70h + nibble: 70 JO 71 JNO 72 JB 73 JAE 74 JE 75 JNE 76 JBE 77 JA
  426. 78 JS 79 JNS 7A JP 7B JNP 7C JL 7D JGE 7E JLE 7F JG.
  427. So 70H + cc is the same condition the 386-only `0F 8x rel16' (for a Jcc) or
  428. `0F 9x' (for a SETcc) encoded, which is what lets the seven EmJcc sites and
  429. the six EmSetcc arms go on passing the low byte they always passed. *)
  430. VAR n : CARDINAL ;
  431. BEGIN
  432. n := VAL (CARDINAL, cc) MOD 10H ;
  433. RETURN VAL (BYTE, 70H + n)
  434. END JccShort ;
  435. PROCEDURE JccShortInv (cc : BYTE) : BYTE ;
  436. (* The same, for a jump that is taken when the condition does NOT hold.
  437. EmJcc needs this one and EmSetcc needs the other, and the difference is the
  438. whole bug, so it is worth being explicit about where it comes from: the low
  439. bit of a Jcc code IS the negation bit. 4/5, C/D, E/F, 2/3, 6/7, A/9, B/8 and
  440. 0/1 are the eight (condition, its negation) pairs, so negating a condition is
  441. `n XOR 1' and nothing more - `JE' and `JNE' are 0x74 and 0x75.
  442. Gm2 under -fiso has no XOR on integers at all: BITAND and BAND are both
  443. syntax errors, and arithmetic on a BYTE operand is rejected too, which is
  444. why every operand here goes through VAL. n + 1 - 2*(n MOD 2) is XOR 1 for a
  445. four-bit n and it lives in one named place rather than open-coded, because
  446. an open-coded negation at two call sites is how they end up disagreeing. *)
  447. VAR n : CARDINAL ;
  448. BEGIN
  449. n := VAL (CARDINAL, cc) MOD 10H ;
  450. RETURN VAL (BYTE, 70H + n + 1 - 2 * (n MOD 2))
  451. END JccShortInv ;
  452. PROCEDURE EmJcc (cc : BYTE ; target : CARDINAL) : CARDINAL ;
  453. (* A conditional branch, 8086 style. `cc' is the condition nibble, and it is
  454. exactly the low byte of the `0F 8x rel16' this used to emit.
  455. That was a real fault and nothing in the build could see it: `0F' is a
  456. 386-and-later opcode prefix and the 8086 has none, so EVERY conditional
  457. branch in EVERY compiled program was an illegal instruction on the machine
  458. TP3 targets. FCML decoded it happily, because FCML's -m16 mode is 386 --
  459. and FCML is this project's independent disassembler, so the one tool that
  460. could have objected was the one guaranteed to agree. qemu-system-i386 has
  461. no 8086 model either; its lowest is 486. So the compile succeeded, the
  462. .COM linked, the layout checked, the golden held and all 30 fixtures ran to
  463. the right answers, all at once, with the bug in.
  464. TPSRC8 lays IF, WHILE and REPEAT out as
  465. MOV AL,brnchop ; MOV AH,#$03 ; CALL eword ; PUSH pc ; CALL ejump
  466. i.e. a SHORT Jcc of displacement 3, stepping over a 3-byte EJMP. That is
  467. the shape here too, and EmJmpNear already owns the displacement arithmetic
  468. and the patch slot, so it is three lines and there is no second copy of
  469. that rule.
  470. The one thing that is NOT the same as TPSRC8, and cost a round of "every
  471. conditional is inverted" (t09_if printed pos/nonpos/lt for a program that
  472. must print nonpos/pos/ge): TP3's brnchop is the branch taken when the
  473. condition is TRUE, and TP3 steps over the EJMP when it is taken. Here `cc'
  474. is the branch taken when the condition is FALSE -- IF's `EmJcc (84H)' is
  475. JZ, patched to the ELSE, so it must fire when the test failed. EmJcc jumps
  476. to the target, it does not step over it, so stepping over an EJMP and then
  477. falling into the destination is the wrong way round: the byte has to be
  478. JccShortInv, not JccShort. The control flow that comes out is identical to
  479. the `0F 8x' form this replaces; only which of the pair is spelled differs.
  480. The flags survive, and the FOR test needs them to: it emits CMP and then
  481. Jcc with nothing in between, so anything that wrote a flag here would
  482. break the loop. Jcc and EJMP both leave the flags alone. *)
  483. BEGIN
  484. Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)
  485. Ebyte (03H) ; (* rel8: step over the 3-byte EJMP *)
  486. RETURN EmJmpNear (target) (* target = 0 => forward, see above *)
  487. END EmJcc ;
  488. PROCEDURE ResolvePatches () ;
  489. VAR i : CARDINAL ;
  490. rel : CARDINAL ;
  491. BEGIN
  492. i := 0 ;
  493. WHILE i < nPatch DO
  494. IF NOT patches [i].filled THEN
  495. rel := (patches [i].target + 10000H - (patches [i].place + 2))
  496. MOD 10000H ;
  497. cbuf [patches [i].place] := VAL (BYTE, rel MOD 100H) ;
  498. cbuf [patches [i].place + 1] := VAL (BYTE, (rel DIV 100H) MOD 100H) ;
  499. patches [i].filled := TRUE
  500. END ;
  501. INC (i)
  502. END
  503. END ResolvePatches ;
  504. (* 1-byte const emitters *)
  505. PROCEDURE EmMovAxi (imm : CARDINAL) ;
  506. BEGIN
  507. Ebyte (0B8H) ; Eword (imm)
  508. END EmMovAxi ;
  509. PROCEDURE EmMovBpSp () ;
  510. BEGIN
  511. Ebyte (8BH) ; Ebyte (0ECH)
  512. END EmMovBpSp ;
  513. PROCEDURE EmMovAh0 () ;
  514. BEGIN
  515. Ebyte (0B4H) ; Ebyte (0H)
  516. END EmMovAh0 ;
  517. PROCEDURE EmMovAxSp () ;
  518. (* Read the top of the stack into AX, leaving the stack unchanged.
  519. The obvious encoding, MOV AX,[SP], DOES NOT EXIST on the 8086. There is
  520. no encoding of [SP] as a memory operand: SIB bytes, which is how [ESP]
  521. would be written, did not exist until the 386, and mod=00 / rm=100 is
  522. [SI], not [SP]. The first version of this emitted 8B 44 24 00 - mod=01,
  523. rm=100, SIB=24h, disp8=0 - which is correct only on a 386 and above. Two
  524. of this project's oracles agree that it is wrong: fcml in 16-bit mode
  525. decodes it as MOV AX,[SI+0x24h], and so does qemu executing it, because
  526. qemu follows the CPU's rules for the encoding it is given rather than
  527. guessing. It was not the encoder's fault that the bytes were well formed;
  528. they were, and they read SI+24h.
  529. The observable effect was that CASE compiled to no branches at all: each
  530. label test loaded a garbage address, every comparison failed, and the
  531. program fell straight past the whole statement and exited without printing.
  532. A CASE fixture caught it. Nothing else could have - the encoding is
  533. valid, the size is right, and the byte-level checks have no way to know
  534. what register was meant.
  535. So: POP then PUSH the same value. Two bytes, no SIB, correct on every
  536. 8086, and observationally identical to peeking - the stack pointer ends
  537. where it started, holding the same value. *)
  538. BEGIN
  539. Ebyte (58H) ; (* POP AX *)
  540. Ebyte (50H) (* PUSH AX *)
  541. END EmMovAxSp ;
  542. PROCEDURE EmMovCxSp () ;
  543. (* The same, for CX - the FOR loop's bound, pushed by the FOR statement and
  544. re-read on every iteration. This one was emitting 8B 0C and nothing else,
  545. which is MOV CX,[SI] with the SIB slot missing: the *next* instruction was
  546. consumed as the SIB byte and the displacement. Same root cause, same fix,
  547. and it had not been noticed only because no FOR fixture is executed yet. *)
  548. BEGIN
  549. Ebyte (59H) ; (* POP CX *)
  550. Ebyte (51H) (* PUSH CX *)
  551. END EmMovCxSp ;
  552. PROCEDURE EmPushAx () ;
  553. BEGIN
  554. Ebyte (50H)
  555. END EmPushAx ;
  556. PROCEDURE EmPopCx () ;
  557. BEGIN
  558. Ebyte (59H)
  559. END EmPopCx ;
  560. PROCEDURE EmPopDx () ;
  561. BEGIN
  562. Ebyte (5AH)
  563. END EmPopDx ;
  564. (* 91 = XCHG AX,CX, and NOT 93. LoadPair hands the operator the left
  565. operand in CX and the right in AX (in the original, and still the
  566. commonest, shape: it pushes the left, loads the right, then pops the
  567. left into CX), so the exchange is what puts LEFT in AX for the
  568. operation to act on.
  569. Without it, `a - b` computes `b - a`; with the wrong register, `a + b`
  570. computes `AX' + a` where AX' is whatever BX happened to hold.
  571. This emitted 93H = XCHG BX,AX for its entire life, which is the same class
  572. of mistake as MovSiBx = 89 DC in Runtime.mod: the right opcode, the wrong
  573. ModRM, decoding cleanly. Byte counts were right, the compile matrix was
  574. green, and no exec fixture did arithmetic on two variables - the first one
  575. to do so, `c := a + b` with a=7 b=5, printed 263 = 0100h+7, where 0100h
  576. was the caller's leftover BX. The name was the only thing wrong, and
  577. nothing read the name: audit_helpers.py swept Runtime.mod and not
  578. Compiler.mod, which is where most of these emitters live. It does both
  579. modules now. *)
  580. PROCEDURE EmXchgAxCx () ;
  581. BEGIN
  582. Ebyte (91H)
  583. END EmXchgAxCx ;
  584. PROCEDURE EmXorAxAx () ;
  585. BEGIN
  586. Ebyte (33H) ; Ebyte (0C0H)
  587. END EmXorAxAx ;
  588. PROCEDURE EmAddAxCx () ;
  589. BEGIN
  590. Ebyte (3H) ; Ebyte (0C1H)
  591. END EmAddAxCx ;
  592. PROCEDURE EmSubAxCx () ;
  593. BEGIN
  594. Ebyte (2BH) ; Ebyte (0C1H)
  595. END EmSubAxCx ;
  596. PROCEDURE EmMulAxCx () ;
  597. BEGIN
  598. Ebyte (0F7H) ; Ebyte (0E9H)
  599. END EmMulAxCx ;
  600. PROCEDURE EmIDivAxCx () ;
  601. BEGIN
  602. Ebyte (99H) ; Ebyte (0F7H) ; Ebyte (0F9H)
  603. END EmIDivAxCx ;
  604. PROCEDURE EmAndAxCx () ;
  605. BEGIN
  606. Ebyte (23H) ; Ebyte (0C1H)
  607. END EmAndAxCx ;
  608. PROCEDURE EmOrAxCx () ;
  609. BEGIN
  610. Ebyte (0BH) ; Ebyte (0C1H)
  611. END EmOrAxCx ;
  612. PROCEDURE EmNegAx () ;
  613. BEGIN
  614. Ebyte (0F7H) ; Ebyte (0D8H)
  615. END EmNegAx ;
  616. PROCEDURE EmNotAx () ;
  617. BEGIN
  618. Ebyte (0F7H) ; Ebyte (0D0H)
  619. END EmNotAx ;
  620. PROCEDURE EmXorAl01 () ;
  621. (* XOR AL,#01 - TPSRC9 neglevel's *boolean* NOT:
  622. CALL loadatom ; CALL ecode ; B $02,$34,$01
  623. neglevel tests the type first (CMP CL,#$0A -> integer -> NOT AX,
  624. CMP CL,#$0B -> boolean -> XOR AL,#01) and reports error 47 for anything
  625. else. Emitting NOT AX for a boolean leaves 0FFFEh or 0FFFFh, and the
  626. runtime's wrbool tests [BP+4] <> 0, so `not (a = a)' came out TRUE. *)
  627. BEGIN
  628. Ebyte (34H) ; Ebyte (01H)
  629. END EmXorAl01 ;
  630. PROCEDURE EmCmpAxCx () ;
  631. BEGIN
  632. Ebyte (3BH) ; Ebyte (0C1H)
  633. END EmCmpAxCx ;
  634. PROCEDURE EmCmpAxi (imm : CARDINAL) ;
  635. BEGIN
  636. Ebyte (03DH) ; Eword (imm)
  637. END EmCmpAxi ;
  638. PROCEDURE EmSetcc (cc : BYTE) ;
  639. (* Flags -> a Boolean in AX, on an 8086. `cc' is the SETcc opcode's low byte
  640. (94H = E, 95H = NE, 9CH = L, 9DH = GE, 9EH = LE, 9FH = G), i.e. the same
  641. condition nibble EmJcc takes.
  642. This used to emit `0F cc C0' - SETcc - which is 386-and-later, and then a
  643. MOV AH,0. The 8086 cannot read its flags as a value at all, so there was
  644. nothing else to fall back on.
  645. TPSRC9's flgbool is the fallback, and emits exactly this for exactly this
  646. case (CH = 04h, a comparison whose result is wanted as a value rather than
  647. as a branch):
  648. CALL ecode ; B $03,$B8,$01,$00 -> MOV AX,#0001
  649. MOV AL,brnchop ; CALL ebyte -> JNZ +1
  650. CALL ecode ; B $02,$01,$48 -> DEC AX
  651. AX stays 1 because the DEC was stepped over, and becomes 0 because it ran.
  652. So the shape is one MOV, one short Jcc whose displacement is the length of
  653. the DEC, and the DEC - which is EmJcc's shape with a different displacement,
  654. and the reason both are two instructions and a byte.
  655. The polarity is the opposite of EmJcc's, and deliberately so: here the jump
  656. must be taken when the comparison is TRUE, because what is being asked is
  657. "is this comparison true", and the nibble the six ParseCmp arms pass is the
  658. comparison's own opcode. So this is JccShort and EmJcc is JccShortInv --
  659. see EmJcc for why the difference is there at all. TP3's flgbool writes JNZ
  660. for the same reason; in the one case IT reaches flgbool from, the boolean is
  661. sitting in AX rather than in the flags, so JNZ is how it says "AX is
  662. non-zero".
  663. AH comes out 0 for free, which is why the EmMovAh0 this used to end with is
  664. gone: 6 bytes here where the old sequence was 5. The FLAGS do not survive,
  665. which the old SETcc did - and nothing reads them. Every conditional branch
  666. in the compiler is preceded by its own CMP (see EmJcc's note on the FOR
  667. test), and a comparison's value is consumed either as an AX operand or by
  668. the test that follows it; the 30 executed fixtures are what holds that
  669. down, not this comment. *)
  670. BEGIN
  671. Ebyte (0B8H) ; Eword (1) ; (* MOV AX,#0001 *)
  672. Ebyte (JccShort (cc)) ; (* taken when the comparison HOLDS *)
  673. Ebyte (01H) ; (* rel8: step over the DEC AX *)
  674. Ebyte (48H) (* DEC AX *)
  675. END EmSetcc ;
  676. PROCEDURE EmIncAx () ;
  677. BEGIN
  678. Ebyte (40H)
  679. END EmIncAx ;
  680. PROCEDURE EmDecAx () ;
  681. BEGIN
  682. Ebyte (48H)
  683. END EmDecAx ;
  684. PROCEDURE EmBpDisp (off : CARDINAL) ;
  685. (* Emit the ModR/M byte and displacement for a [BP+off] operand, picking the
  686. encoding from the size of off. This is the ONE place that choice is made,
  687. because getting it wrong is invisible: 8B 46 d8 and 8B 86 lo hi are both
  688. well-formed MOVs, both decode cleanly, and only one of them reads the
  689. variable the symbol table names. So the two are chosen here, once, rather
  690. than re-derived at each of the four call sites. See the ModR/M table in
  691. Runtime.mod.
  692. off <= 127 -> mod=01 rm=110 -> 46 <disp8> 3 bytes with the opcode
  693. otherwise -> mod=10 rm=110 -> 86 <disp16> 4 bytes with the opcode
  694. Both displacements are SIGNED, and that is the whole subtlety:
  695. - `off` is a 16-bit value and locals are allocated DOWNWARD from
  696. 0FFFEh (locFree starts there and is decremented per declaration), so
  697. the first local of a procedure sits at off = 0FFFC, which is -4. The
  698. disp16 form reads those same two bytes as a signed value and addresses
  699. [BP-4] correctly. There is no overflow case: all 65536 values of `off`
  700. are representable, and a frame larger than 64K is a different problem.
  701. - The old code took `off MOD 100H` and always emitted disp8. That is
  702. the correct low byte for every displacement, so it was accidentally
  703. right across -32768..+127, which is where locals actually live. It
  704. went wrong at +128, where disp8 80h is -128 and not +128. So this
  705. changes no existing program's bytes and fixes the one case that was
  706. broken -- a bug nobody had hit yet, which is exactly why it wanted a
  707. test rather than an argument. *)
  708. BEGIN
  709. IF off <= 127 THEN
  710. Ebyte (46H) ; Ebyte (VAL (BYTE, off))
  711. ELSE
  712. Ebyte (86H) ; Eword (off)
  713. END
  714. END EmBpDisp ;
  715. PROCEDURE EmLoadVar (local : BOOLEAN ; off, nbytes : CARDINAL) ;
  716. (* A local is [BP+off] and `off' is already a frame displacement, so it needs
  717. no bias. A global is [off] with a DIRECT displacement, i.e. an absolute
  718. address, and that is the image offset + LoadBias - see Runtime.LoadBias. *)
  719. BEGIN
  720. IF nbytes = 1 THEN
  721. IF local THEN
  722. Ebyte (8AH) ; EmBpDisp (off)
  723. ELSE
  724. Ebyte (0A0H) ; Eword ((off + LoadBias) MOD 10000H)
  725. END
  726. ELSE
  727. IF local THEN
  728. Ebyte (8BH) ; EmBpDisp (off)
  729. ELSE
  730. Ebyte (0A1H) ; Eword ((off + LoadBias) MOD 10000H)
  731. END
  732. END
  733. END EmLoadVar ;
  734. PROCEDURE EmStoreVar (local : BOOLEAN ; off, nbytes : CARDINAL) ;
  735. BEGIN
  736. IF nbytes = 1 THEN
  737. IF local THEN
  738. Ebyte (88H) ; EmBpDisp (off)
  739. ELSE
  740. Ebyte (0A2H) ; Eword ((off + LoadBias) MOD 10000H)
  741. END
  742. ELSE
  743. IF local THEN
  744. Ebyte (89H) ; EmBpDisp (off)
  745. ELSE
  746. Ebyte (0A3H) ; Eword ((off + LoadBias) MOD 10000H)
  747. END
  748. END
  749. END EmStoreVar ;
  750. PROCEDURE EmPushVarAddr (local : BOOLEAN ; off : CARDINAL) ;
  751. (* LEA AX,[BP+disp] / LEA AX,[off] then PUSH AX - READ passes the address of
  752. a variable, not its value. 8D 46 disp is LEA AX,[BP+disp8] and
  753. 8D 86 lo hi is LEA AX,[BP+disp16]; 8D 06 off is LEA AX,[off]
  754. (mod=00 rm=110 = the direct disp16 form). All three are 8086-legal.
  755. The [off] form is absolute and so carries LoadBias; the [BP+disp] forms
  756. are displacements and so do not. *)
  757. BEGIN
  758. IF local THEN
  759. Ebyte (8DH) ; EmBpDisp (off)
  760. ELSE
  761. Ebyte (8DH) ; Ebyte (06H) ; Eword ((off + LoadBias) MOD 10000H)
  762. END ;
  763. EmPushAx ()
  764. END EmPushVarAddr ;
  765. PROCEDURE EmSubSp (n : CARDINAL) ;
  766. BEGIN
  767. Ebyte (81H) ; Ebyte (0ECH) ; Eword (n MOD 10000H)
  768. END EmSubSp ;
  769. PROCEDURE EmAddSp (n : CARDINAL) ;
  770. BEGIN
  771. IF n <= 126 THEN
  772. Ebyte (83H) ; Ebyte (0C4H) ; Ebyte (VAL (BYTE, n))
  773. ELSE
  774. Ebyte (81H) ; Ebyte (0C4H) ; Eword (n MOD 10000H)
  775. END
  776. END EmAddSp ;
  777. PROCEDURE EmPushBp () ;
  778. BEGIN
  779. Ebyte (55H)
  780. END EmPushBp ;
  781. PROCEDURE EmLeave () ;
  782. BEGIN
  783. Ebyte (0C9H)
  784. END EmLeave ;
  785. PROCEDURE EmRet () ;
  786. BEGIN
  787. Ebyte (0C3H)
  788. END EmRet ;
  789. (* ---------------------------------------------------------------- *)
  790. (* symbol table *)
  791. (* ---------------------------------------------------------------- *)
  792. PROCEDURE NameEq (a : ARRAY OF CHAR ; b : ARRAY OF CHAR) : BOOLEAN ;
  793. VAR i : CARDINAL ;
  794. BEGIN
  795. i := 0 ;
  796. LOOP
  797. IF i > HIGH (a) THEN
  798. RETURN FALSE
  799. END ;
  800. IF i > HIGH (b) THEN
  801. RETURN FALSE
  802. END ;
  803. IF a [i] # b [i] THEN
  804. RETURN FALSE
  805. END ;
  806. IF a [i] = 0C THEN
  807. RETURN TRUE
  808. END ;
  809. INC (i)
  810. END
  811. END NameEq ;
  812. PROCEDURE CopyStr (VAR dst : ARRAY OF CHAR ; src : ARRAY OF CHAR) ;
  813. VAR i : CARDINAL ;
  814. BEGIN
  815. i := 0 ;
  816. LOOP
  817. IF i > HIGH (dst) THEN
  818. dst [HIGH (dst)] := 0C ;
  819. RETURN
  820. END ;
  821. IF i > HIGH (src) THEN
  822. dst [i] := 0C ;
  823. RETURN
  824. END ;
  825. dst [i] := src [i] ;
  826. IF src [i] = 0C THEN
  827. RETURN
  828. END ;
  829. INC (i)
  830. END
  831. END CopyStr ;
  832. PROCEDURE CopyWord (name : ARRAY OF CHAR) ;
  833. (* stash current word into global wrd (uppercased) *)
  834. VAR i : CARDINAL ;
  835. BEGIN
  836. i := 0 ;
  837. LOOP
  838. IF i > HIGH (name) THEN
  839. wrd [i] := 0C ;
  840. RETURN
  841. END ;
  842. IF i > MaxName THEN
  843. wrd [MaxName] := 0C ;
  844. RETURN
  845. END ;
  846. IF name [i] = 0C THEN
  847. wrd [i] := 0C ;
  848. RETURN
  849. END ;
  850. wrd [i] := Upper (name [i]) ;
  851. INC (i)
  852. END
  853. END CopyWord ;
  854. PROCEDURE SaveWord (VAR dst : ARRAY OF CHAR) ;
  855. (* copy wrd into dst *)
  856. VAR i : CARDINAL ;
  857. BEGIN
  858. i := 0 ;
  859. LOOP
  860. IF i > HIGH (dst) THEN
  861. dst [HIGH (dst)] := 0C ;
  862. RETURN
  863. END ;
  864. IF i > MaxName THEN
  865. dst [MaxName] := 0C ;
  866. RETURN
  867. END ;
  868. dst [i] := wrd [i] ;
  869. IF wrd [i] = 0C THEN
  870. RETURN
  871. END ;
  872. INC (i)
  873. END
  874. END SaveWord ;
  875. PROCEDURE NumToName (n : CARDINAL ; VAR dst : ARRAY OF CHAR) ;
  876. VAR buf : ARRAY [0..9] OF CHAR ;
  877. i, j : CARDINAL ;
  878. BEGIN
  879. IF n = 0 THEN
  880. dst [0] := '0' ;
  881. dst [1] := 0C ;
  882. RETURN
  883. END ;
  884. i := 0 ;
  885. WHILE n > 0 DO
  886. IF i <= 9 THEN
  887. buf [i] := CHR (ORD ('0') + (n MOD 10)) ;
  888. INC (i)
  889. END ;
  890. n := n DIV 10
  891. END ;
  892. j := 0 ;
  893. WHILE i > 0 DO
  894. DEC (i) ;
  895. IF j <= HIGH (dst) THEN
  896. dst [j] := buf [i] ;
  897. INC (j)
  898. END
  899. END ;
  900. IF j <= HIGH (dst) THEN
  901. dst [j] := 0C
  902. END
  903. END NumToName ;
  904. PROCEDURE NewSym (name : ARRAY OF CHAR ; tag : CARDINAL ;
  905. cls, size, elem, off : CARDINAL ; v : LONGINT ;
  906. local : BOOLEAN) : CARDINAL ;
  907. VAR e : SymEntry ;
  908. BEGIN
  909. IF symTop >= MaxSym THEN
  910. Err (ECompOvf) ;
  911. RETURN 0
  912. END ;
  913. CopyWord (name) ;
  914. CopyStr (e.name, wrd) ;
  915. e.tag := tag ;
  916. e.cls := cls ;
  917. e.size := size ;
  918. e.elem := elem ;
  919. e.off := off ;
  920. e.lval := v ;
  921. e.level := lexnest ;
  922. e.local := local ;
  923. e.resvar := 0 ;
  924. e.goPos := 0 ;
  925. e.defnd := FALSE ;
  926. e.fwd := FALSE ;
  927. symtab [symTop] := e ;
  928. INC (symTop) ;
  929. RETURN symTop - 1
  930. END NewSym ;
  931. PROCEDURE Search (nm : ARRAY OF CHAR ; VAR idx : CARDINAL) : BOOLEAN ;
  932. (* find nm among symbols visible at the current lexical level *)
  933. VAR p : CARDINAL ;
  934. BEGIN
  935. CopyWord (nm) ;
  936. p := symTop ;
  937. WHILE p > 0 DO
  938. DEC (p) ;
  939. IF symtab [p].level <= lexnest THEN
  940. IF NameEq (symtab [p].name, wrd) THEN
  941. idx := p ;
  942. RETURN TRUE
  943. END
  944. END
  945. END ;
  946. RETURN FALSE
  947. END Search ;
  948. PROCEDURE DupTest (nm : ARRAY OF CHAR) ;
  949. VAR i : CARDINAL ;
  950. BEGIN
  951. IF Search (nm, i) THEN
  952. Err (EUnknown)
  953. END
  954. END DupTest ;
  955. PROCEDURE HideLocals (from : CARDINAL) ;
  956. (* Make every symbol from index `from' up invisible to everything outside the
  957. procedure that declared it.
  958. Search accepts a symbol when its level is <= lexnest, and every procedure
  959. body is compiled at the same depth (lexnest 1), so a finished procedure's
  960. parameters stayed visible to the NEXT procedure: a second `a : integer'
  961. was a duplicate (err 41) and an unqualified `a' inside procedure two read
  962. procedure one's argument. Level 0FFFFH fails `level <= lexnest' at every
  963. depth a later procedure can be at, and by the time this runs the body that
  964. could still legitimately see them is finished.
  965. The symbols are relabelled, not popped: symtab[old].resvar holds an INDEX,
  966. and a function's result variable is one of the entries being hidden. *)
  967. VAR p : CARDINAL ;
  968. BEGIN
  969. p := from ;
  970. WHILE p < symTop DO
  971. symtab [p].level := 0FFFFH ;
  972. INC (p)
  973. END
  974. END HideLocals ;
  975. (* ---------------------------------------------------------------- *)
  976. (* lexer *)
  977. (* ---------------------------------------------------------------- *)
  978. PROCEDURE InitKeys () ;
  979. VAR i : CARDINAL ;
  980. BEGIN
  981. FOR i := 0 TO 42 DO
  982. kTk [i] := 0 ;
  983. kName [i] [0] := 0C
  984. END ;
  985. kName [1] := "PROGRAM" ; kTk [1] := TkProgram ;
  986. kName [2] := "BEGIN" ; kTk [2] := TkBegin ;
  987. kName [3] := "END" ; kTk [3] := TkEnd ;
  988. kName [4] := "IF" ; kTk [4] := TkIf ;
  989. kName [5] := "THEN" ; kTk [5] := TkThen ;
  990. kName [6] := "ELSE" ; kTk [6] := TkElse ;
  991. kName [7] := "WHILE" ; kTk [7] := TkWhile ;
  992. kName [8] := "DO" ; kTk [8] := TkDo ;
  993. kName [9] := "REPEAT" ; kTk [9] := TkRepeat ;
  994. kName [10] := "UNTIL" ; kTk [10] := TkUntil ;
  995. kName [11] := "FOR" ; kTk [11] := TkFor ;
  996. kName [12] := "TO" ; kTk [12] := TkTo ;
  997. kName [13] := "DOWNTO" ; kTk [13] := TkDownto ;
  998. kName [14] := "CASE" ; kTk [14] := TkCase ;
  999. kName [15] := "OF" ; kTk [15] := TkOf ;
  1000. kName [16] := "GOTO" ; kTk [16] := TkGoto ;
  1001. kName [17] := "EXIT" ; kTk [17] := TkExit ;
  1002. kName [18] := "WITH" ; kTk [18] := TkWith ;
  1003. kName [19] := "VAR" ; kTk [19] := TkVar ;
  1004. kName [20] := "CONST" ; kTk [20] := TkConst ;
  1005. kName [21] := "TYPE" ; kTk [21] := TkType ;
  1006. kName [22] := "LABEL" ; kTk [22] := TkLabel ;
  1007. kName [23] := "PROCEDURE" ; kTk [23] := TkProcedure ;
  1008. kName [24] := "FUNCTION" ; kTk [24] := TkFunction ;
  1009. kName [25] := "NIL" ; kTk [25] := TkNil ;
  1010. kName [26] := "AND" ; kTk [26] := TkAnd ;
  1011. kName [27] := "OR" ; kTk [27] := TkOr ;
  1012. kName [28] := "NOT" ; kTk [28] := TkNot ;
  1013. kName [29] := "DIV" ; kTk [29] := TkDiv ;
  1014. kName [30] := "MOD" ; kTk [30] := TkMod ;
  1015. kName [31] := "IN" ; kTk [31] := TkIn ;
  1016. kName [32] := "FILE" ; kTk [32] := TkFile ;
  1017. kName [33] := "TEXT" ; kTk [33] := TkText ;
  1018. kName [34] := "RECORD" ; kTk [34] := TkRecord ;
  1019. kName [35] := "ARRAY" ; kTk [35] := TkArray ;
  1020. kName [36] := "SET" ; kTk [36] := TkSet ;
  1021. kName [37] := "PACKED" ; kTk [37] := TkPacked ;
  1022. kName [38] := "FORWARD" ; kTk [38] := TkForward ;
  1023. kName [39] := "EXTERNAL" ; kTk [39] := TkExternal ;
  1024. kName [40] := "ABSOLUTE" ; kTk [40] := TkAbsolute ;
  1025. kName [41] := "OVERLAY" ; kTk [41] := TkOverlay ;
  1026. kName [42] := "STRING" ; kTk [42] := TkString
  1027. END InitKeys ;
  1028. PROCEDURE IsBlank (ch : CHAR) : BOOLEAN ;
  1029. BEGIN
  1030. RETURN (ch = ' ') OR (ORD (ch) = 09H) OR (ORD (ch) = 0DH) OR (ORD (ch) = 0AH) OR (ORD (ch) = 0CH)
  1031. END IsBlank ;
  1032. PROCEDURE Skip () ;
  1033. (* blanks, comments { } and (* *), compiler directives {$ } / (*$ *)
  1034. letter + sign toggles rng/chk *)
  1035. VAR ch : CHAR ;
  1036. letter : CHAR ;
  1037. BEGIN
  1038. WHILE NOT abortFac DO
  1039. WHILE IsBlank (CurCh ()) DO
  1040. ch := GetCh ()
  1041. END ;
  1042. IF CurCh () = '{' THEN
  1043. ch := GetCh () ;
  1044. IF CurCh () = '$' THEN
  1045. ch := GetCh () ;
  1046. letter := GetCh () ;
  1047. ch := GetCh () ;
  1048. IF ch = '+' THEN
  1049. IF letter = 'R' THEN dirs.rng := TRUE END ;
  1050. IF letter = 'I' THEN dirs.chk := TRUE END
  1051. ELSIF ch = '-' THEN
  1052. IF letter = 'R' THEN dirs.rng := FALSE END ;
  1053. IF letter = 'I' THEN dirs.chk := FALSE END
  1054. END
  1055. END ;
  1056. WHILE (CurCh () # '}') AND (CurCh () # 0C) DO
  1057. ch := GetCh ()
  1058. END ;
  1059. IF CurCh () = '}' THEN
  1060. ch := GetCh ()
  1061. END
  1062. ELSIF (CurCh () = '(') AND (PeekAhead (1) = '*') THEN
  1063. ch := GetCh () ;
  1064. ch := GetCh () ;
  1065. IF CurCh () = '$' THEN
  1066. ch := GetCh () ;
  1067. letter := GetCh () ;
  1068. ch := GetCh () ;
  1069. IF ch = '+' THEN
  1070. IF letter = 'R' THEN dirs.rng := TRUE END
  1071. ELSIF ch = '-' THEN
  1072. IF letter = 'R' THEN dirs.rng := FALSE END
  1073. END
  1074. END ;
  1075. LOOP
  1076. IF (CurCh () = '*') AND (PeekAhead (1) = ')') THEN
  1077. ch := GetCh () ;
  1078. ch := GetCh () ;
  1079. EXIT
  1080. END ;
  1081. IF CurCh () = 0C THEN
  1082. EXIT
  1083. END ;
  1084. ch := GetCh ()
  1085. END
  1086. ELSE
  1087. RETURN
  1088. END
  1089. END
  1090. END Skip ;
  1091. PROCEDURE GetWord () ;
  1092. (* read identifier into wrd (uppercased); next char must be alpha *)
  1093. VAR i : CARDINAL ;
  1094. ch : CHAR ;
  1095. BEGIN
  1096. i := 0 ;
  1097. ch := GetCh () ;
  1098. LOOP
  1099. IF i > MaxName THEN
  1100. wrd [MaxName] := 0C ;
  1101. RETURN
  1102. END ;
  1103. wrd [i] := Upper (ch) ;
  1104. INC (i) ;
  1105. ch := CurCh () ;
  1106. IF NOT AlphaNum (ch) THEN
  1107. wrd [i] := 0C ;
  1108. RETURN
  1109. END ;
  1110. ch := GetCh ()
  1111. END
  1112. END GetWord ;
  1113. PROCEDURE WddTok () : CARDINAL ;
  1114. (* map wrd -> keyword token *)
  1115. VAR i : CARDINAL ;
  1116. BEGIN
  1117. i := 1 ;
  1118. WHILE i <= 42 DO
  1119. IF kName [i] [0] # 0C THEN
  1120. IF NameEq (wrd, kName [i]) THEN
  1121. RETURN kTk [i]
  1122. END
  1123. END ;
  1124. INC (i)
  1125. END ;
  1126. RETURN TkNone
  1127. END WddTok ;
  1128. PROCEDURE DeclaresProc () : BOOLEAN ;
  1129. (* Does the REST of the source declare a PROCEDURE or a FUNCTION?
  1130. Needed because the declaration part is compiled BEFORE the main statement
  1131. part, so a procedure's code lands between the program prologue and the
  1132. main body - and nothing jumps over it. A program with a procedure
  1133. therefore ran off the end of the prologue, straight into the first
  1134. procedure, which read its argument out of an uninitialised frame and
  1135. returned to address 0. Every Pascal program containing a procedure was
  1136. broken; `t13_proc` compiled and was never executed, so nothing saw it.
  1137. The jump that fixes it has to be emitted BEFORE the declaration part, but
  1138. whether one is needed is only known AFTER - so the only honest options are
  1139. to emit it unconditionally (3 dead bytes in every program, and every code
  1140. size in expected.tsv moves) or to know the answer in advance. This is the
  1141. second: it scans ahead and puts srcPos back.
  1142. That is safe because the whole program is already in `src` and `srcPos` is
  1143. a plain index into it - the same trick PeekKw and KwAhead use. The scan
  1144. looks for the keywords anywhere in the remainder rather than tracking the
  1145. nesting of `begin`s, which is deliberately loose: a program with no
  1146. procedures that merely mentions the word in a string literal would get a
  1147. 3-byte jump to the next instruction, which is harmless, whereas tracking
  1148. the main `begin` against a procedure's `begin` would be a second parser
  1149. to get wrong. *)
  1150. VAR save : CARDINAL ;
  1151. found : BOOLEAN ;
  1152. tk : CARDINAL ;
  1153. ch : CHAR ;
  1154. BEGIN
  1155. save := srcPos ;
  1156. found := FALSE ;
  1157. tk := TkNone ; (* so the answer is defined if src is empty *)
  1158. (* Step over delimiters as well as blanks. Stopping at the first
  1159. non-letter looked reasonable and was wrong: `var x : integer ;` is full
  1160. of ':' and ';', so the scan gave up inside the variable section and
  1161. never reached the PROCEDURE. The loop ends at the end of the source,
  1162. not at the first punctuation. *)
  1163. WHILE (NOT found) AND (srcPos < srcLen) DO
  1164. Skip () ;
  1165. IF Alpha (CurCh ()) THEN
  1166. GetWord () ;
  1167. tk := WddTok () ;
  1168. IF (tk = TkProcedure) OR (tk = TkFunction) THEN
  1169. found := TRUE
  1170. END
  1171. ELSE
  1172. ch := GetCh () (* a ':' or ';' - step over it *)
  1173. END
  1174. END ;
  1175. srcPos := save ;
  1176. RETURN (tk = TkProcedure) OR (tk = TkFunction)
  1177. END DeclaresProc ;
  1178. PROCEDURE KwAhead (word : ARRAY OF CHAR) : BOOLEAN ;
  1179. (* does the next token (past blanks/comments) equal the keyword 'word',
  1180. without consuming it? srcPos is saved and restored. *)
  1181. VAR save : CARDINAL ;
  1182. k : BOOLEAN ;
  1183. BEGIN
  1184. save := srcPos ;
  1185. Skip () ;
  1186. k := FALSE ;
  1187. IF Alpha (CurCh ()) THEN
  1188. GetWord () ;
  1189. k := NameEq (wrd, word)
  1190. END ;
  1191. srcPos := save ;
  1192. RETURN k
  1193. END KwAhead ;
  1194. PROCEDURE PeekKw (VAR tok : CARDINAL) ;
  1195. (* peek at the next keyword token without consuming it *)
  1196. VAR i : CARDINAL ;
  1197. BEGIN
  1198. tok := TkNone ;
  1199. i := 1 ;
  1200. WHILE i <= 42 DO
  1201. IF kName [i] [0] # 0C THEN
  1202. IF KwAhead (kName [i]) THEN
  1203. tok := kTk [i] ;
  1204. RETURN
  1205. END
  1206. END ;
  1207. INC (i)
  1208. END
  1209. END PeekKw ;
  1210. PROCEDURE MatchKey (VAR tok : CARDINAL) : BOOLEAN ;
  1211. (* skip; if next symbol is a word, read it into wrd and set its token.
  1212. Returns TRUE when a word was read (tok = TkNone for plain ids). *)
  1213. BEGIN
  1214. tok := TkNone ;
  1215. Skip () ;
  1216. IF NOT Alpha (CurCh ()) THEN
  1217. RETURN FALSE
  1218. END ;
  1219. GetWord () ;
  1220. tok := WddTok () ;
  1221. RETURN TRUE
  1222. END MatchKey ;
  1223. PROCEDURE MatchDelim (ch : CHAR) : BOOLEAN ;
  1224. BEGIN
  1225. Skip () ;
  1226. IF CurCh () = ch THEN
  1227. DropCh (GetCh ()) ;
  1228. RETURN TRUE
  1229. END ;
  1230. RETURN FALSE
  1231. END MatchDelim ;
  1232. PROCEDURE MatchAssign () : BOOLEAN ;
  1233. BEGIN
  1234. Skip () ;
  1235. IF (CurCh () = ':') AND (PeekAhead (1) = '=') THEN
  1236. DropCh (GetCh ()) ;
  1237. DropCh (GetCh ()) ;
  1238. RETURN TRUE
  1239. END ;
  1240. RETURN FALSE
  1241. END MatchAssign ;
  1242. PROCEDURE MatchRange () : BOOLEAN ;
  1243. BEGIN
  1244. Skip () ;
  1245. IF (CurCh () = '.') AND (PeekAhead (1) = '.') THEN
  1246. DropCh (GetCh ()) ;
  1247. DropCh (GetCh ()) ;
  1248. RETURN TRUE
  1249. END ;
  1250. RETURN FALSE
  1251. END MatchRange ;
  1252. PROCEDURE ExpectDelim (ch : CHAR ; n : CARDINAL) ;
  1253. BEGIN
  1254. Skip () ;
  1255. IF CurCh () = ch THEN
  1256. DropCh (GetCh ())
  1257. ELSE
  1258. Err (n)
  1259. END
  1260. END ExpectDelim ;
  1261. PROCEDURE HexVal (ch : CHAR) : CARDINAL ;
  1262. BEGIN
  1263. IF (ch >= '0') AND (ch <= '9') THEN
  1264. RETURN ORD (ch) - ORD ('0')
  1265. ELSIF (ch >= 'A') AND (ch <= 'F') THEN
  1266. RETURN ORD (ch) - ORD ('A') + 10
  1267. END ;
  1268. RETURN ORD (ch) - ORD ('a') + 10
  1269. END HexVal ;
  1270. PROCEDURE RdIntConst (VAR v : LONGINT) ;
  1271. (* bare integer constant; current char is digit or '$' *)
  1272. VAR acc : LONGINT ;
  1273. BEGIN
  1274. acc := 0 ;
  1275. IF CurCh () = '$' THEN
  1276. DropCh (GetCh ()) ;
  1277. WHILE IsHexCh (CurCh ()) DO
  1278. acc := acc * 16 + VAL (LONGINT, HexVal (CurCh ())) ;
  1279. DropCh (GetCh ())
  1280. END
  1281. ELSE
  1282. WHILE Digit (CurCh ()) DO
  1283. acc := acc * 10 + VAL (LONGINT, ORD (CurCh ()) - ORD ('0')) ;
  1284. DropCh (GetCh ())
  1285. END
  1286. END ;
  1287. v := acc
  1288. END RdIntConst ;
  1289. PROCEDURE StrNew (first : CARDINAL ; hasFirst : BOOLEAN ) : CARDINAL ;
  1290. (* Open a pool slot for a string literal, optionally pre-seeded with its
  1291. first character.
  1292. RdConst has to read the first character before it can tell a one-character
  1293. literal from a longer one - the "is the next character another quote?"
  1294. test only makes sense once something has been read - so the seeding has to
  1295. happen here and has to advance strTop. Leaving strTop alone and letting the
  1296. caller write the character by hand is a trap: strTop is the next FREE byte,
  1297. so the first StrPut lands on top of the seeded character and overwrites it.
  1298. (That bug shipped the literal 'hi' as 69 00 - 'i' then NUL.) *)
  1299. VAR x : CARDINAL ;
  1300. BEGIN
  1301. IF strCnt > HIGH (strOff) THEN
  1302. Err (ECompOvf) ; (* too many literals in one unit *)
  1303. RETURN 0
  1304. END ;
  1305. x := strCnt ;
  1306. strOff [x] := strTop ;
  1307. strLen [x] := 0 ;
  1308. INC (strCnt) ;
  1309. IF hasFirst THEN
  1310. strPool [strTop] := CHR (first) ;
  1311. INC (strTop) ;
  1312. strLen [x] := 1
  1313. END ;
  1314. rdStrX := x ;
  1315. RETURN x
  1316. END StrNew ;
  1317. PROCEDURE StrPut (x : CARDINAL ) ;
  1318. (* append the current source character to pool slot x *)
  1319. BEGIN
  1320. IF x > HIGH (strOff) THEN
  1321. RETURN
  1322. END ;
  1323. IF strTop > HIGH (strPool) THEN
  1324. Err (ECompOvf) ; (* literal longer than the pool *)
  1325. RETURN
  1326. END ;
  1327. strPool [strTop] := CurCh () ;
  1328. INC (strTop) ;
  1329. INC (strLen [x])
  1330. END StrPut ;
  1331. PROCEDURE RdConst (VAR v : LONGINT ; VAR cls : CARDINAL ;
  1332. VAR isStr : BOOLEAN) ;
  1333. (* scalar or string constant. A string literal's text is collected into the
  1334. pool and its slot index left in rdStrX; a single-character literal stays a
  1335. TScalar holding its character code, which is what "c := 'a'" wants. *)
  1336. CONST q = AposC ;
  1337. BEGIN
  1338. isStr := FALSE ;
  1339. cls := TScalar ;
  1340. v := 0 ;
  1341. Skip () ;
  1342. IF CurCh () = '$' THEN
  1343. RdIntConst (v) ;
  1344. cls := TScalar
  1345. ELSIF Digit (CurCh ()) THEN
  1346. RdIntConst (v) ;
  1347. IF (CurCh () = '.') AND (Digit (PeekAhead (1))) THEN
  1348. cls := TReal ;
  1349. DropCh (GetCh ())
  1350. END ;
  1351. IF (CurCh () = 'E') OR (CurCh () = 'e') THEN
  1352. cls := TReal ;
  1353. DropCh (GetCh ())
  1354. END ;
  1355. IF cls = TReal THEN
  1356. WHILE AlphaNum (CurCh ()) OR (CurCh () = '.') OR (CurCh () = '-')
  1357. OR (CurCh () = '+') DO
  1358. DropCh (GetCh ())
  1359. END
  1360. END
  1361. ELSIF ORD (CurCh ()) = q THEN
  1362. DropCh (GetCh ()) ;
  1363. IF ORD (CurCh ()) = q THEN
  1364. (* '' - the empty string. It used to be reported as the scalar 39,
  1365. so writeln('') printed a single quote mark. It is a string of
  1366. length zero, and an inline zero-length literal is exactly what the
  1367. runtime's JCXZ path is for. *)
  1368. DropCh (GetCh ()) ;
  1369. isStr := TRUE ;
  1370. cls := TString ;
  1371. v := 0 ;
  1372. rdStrX := StrNew (0, FALSE)
  1373. ELSIF (CurCh () = 0C) OR ((ORD (CurCh ()) = 0DH)) OR ((ORD (CurCh ()) = 0AH)) THEN
  1374. Err (EUnknown)
  1375. ELSE
  1376. v := VAL (LONGINT, ORD (GetCh ())) ;
  1377. cls := TScalar ;
  1378. IF ORD (CurCh ()) = q THEN
  1379. DropCh (GetCh ())
  1380. ELSE
  1381. isStr := TRUE ;
  1382. cls := TString ;
  1383. (* Scan to the closing quote. NB: the loop condition must test
  1384. the CURRENT character and only the current character. The
  1385. obvious-looking "while CurCh # quote" with a
  1386. "if PeekAhead(1) = quote then consume two" body is wrong:
  1387. consuming the quote moves the cursor past it, so the next
  1388. condition test sees the character AFTER the literal, is
  1389. satisfied, and the scan runs on to end-of-buffer - which
  1390. silently eats the rest of the program and makes every later
  1391. error point at end-of-file. Stop on the quote itself, and
  1392. treat a doubled quote as one embedded quote character.
  1393. The first character is already gone - it was read into v above -
  1394. so the pool slot is pre-seeded with it. *)
  1395. rdStrX := StrNew (VAL (CARDINAL, v), TRUE) ;
  1396. LOOP
  1397. IF ORD (CurCh ()) = q THEN
  1398. IF ORD (PeekAhead (1)) = q THEN
  1399. StrPut (rdStrX) ; (* '' inside *)
  1400. DropCh (GetCh ()) ; DropCh (GetCh ())
  1401. ELSE
  1402. EXIT (* closing quote *)
  1403. END
  1404. ELSIF (CurCh () = 0C) OR (ORD (CurCh ()) = 0DH) THEN
  1405. Err (EUnknown) ; (* unterminated *)
  1406. EXIT
  1407. ELSE
  1408. StrPut (rdStrX) ;
  1409. DropCh (GetCh ())
  1410. END
  1411. END ;
  1412. IF ORD (CurCh ()) = q THEN
  1413. DropCh (GetCh ())
  1414. END
  1415. END
  1416. END
  1417. ELSE
  1418. Err (EUnknown)
  1419. END
  1420. END RdConst ;
  1421. (* ---------------------------------------------------------------- *)
  1422. (* forward declarations *)
  1423. (* ---------------------------------------------------------------- *)
  1424. PROCEDURE ParseExpr (VAR r : ERes) ; FORWARD ;
  1425. PROCEDURE ParseAdd (VAR r : ERes) ; FORWARD ;
  1426. PROCEDURE ParseMul (VAR r : ERes) ; FORWARD ;
  1427. PROCEDURE ParseNeg (VAR r : ERes) ; FORWARD ;
  1428. PROCEDURE ParseAtom (VAR r : ERes) ; FORWARD ;
  1429. PROCEDURE ParseType (VAR cls, size, elem : CARDINAL) ; FORWARD ;
  1430. PROCEDURE Statmnt () ; FORWARD ;
  1431. PROCEDURE ParseCall (idx : CARDINAL) ; FORWARD ;
  1432. PROCEDURE ParseCallArgs (idx : CARDINAL) ; FORWARD ;
  1433. PROCEDURE EmCallMost (idx, nk : CARDINAL) ; FORWARD ;
  1434. (* ---------------------------------------------------------------- *)
  1435. (* expressions (TPSRC9) *)
  1436. (* ---------------------------------------------------------------- *)
  1437. PROCEDURE LoadAtom (VAR r : ERes) ;
  1438. (* load value of r into AX (folding constants).
  1439. A string literal is refused here, and this is the single place that has to:
  1440. assignment, IF, WHILE, FOR, REPEAT, CASE, array subscripts and every
  1441. operator all reach their operand through LoadAtom, and none of them can use
  1442. a counted string where a 16-bit word is expected. Reporting it here rather
  1443. than in the parser means writeln('hi') still works - IoCall handles a
  1444. literal before it ever calls LoadAtom. *)
  1445. BEGIN
  1446. IF r.cls = TString THEN
  1447. Err (ENoLib) ; (* string value used as a number *)
  1448. r.kind := 2 ;
  1449. RETURN
  1450. END ;
  1451. IF r.kind = 0 THEN
  1452. EmMovAxi (W16 (r.imm)) ;
  1453. r.kind := 2
  1454. ELSIF r.kind = 1 THEN
  1455. IF symtab [r.idx].size > 2 THEN
  1456. Err (ENoLib)
  1457. ELSE
  1458. EmLoadVar (symtab [r.idx].local,
  1459. (symtab [r.idx].off + r.boff) MOD 10000H,
  1460. symtab [r.idx].size) ;
  1461. IF symtab [r.idx].size = 1 THEN
  1462. EmMovAh0 ()
  1463. END ;
  1464. r.kind := 2
  1465. END
  1466. END
  1467. END LoadAtom ;
  1468. PROCEDURE ParseSub (VAR r : ERes) ;
  1469. (* consume '[' constExpr ']' while present, folding the index into the
  1470. base offset (constant indexing only) *)
  1471. VAR t : ERes ;
  1472. BEGIN
  1473. LOOP
  1474. Skip () ;
  1475. IF CurCh () # '[' THEN
  1476. RETURN
  1477. END ;
  1478. DropCh (GetCh ()) ;
  1479. ParseExpr (t) ;
  1480. IF OK () THEN
  1481. IF t.kind # 0 THEN
  1482. Err (ENoLib) ;
  1483. RETURN
  1484. END ;
  1485. IF symtab [r.idx].cls = TArray THEN
  1486. r.boff := W16 (VAL (LONGINT, r.boff)
  1487. + t.imm * VAL (LONGINT, symtab [r.idx].elem))
  1488. ELSE
  1489. Err (ESimpType) ;
  1490. RETURN
  1491. END
  1492. END ;
  1493. ExpectDelim (']', ENoSemi)
  1494. END
  1495. END ParseSub ;
  1496. PROCEDURE ParseVar (VAR r : ERes) ;
  1497. (* name [ '[' constExpr ']' ]* ; a function name maps to its result var *)
  1498. VAR idx : CARDINAL ;
  1499. BEGIN
  1500. IF NOT Search (wrd, idx) THEN
  1501. Err (EUnknown) ;
  1502. RETURN
  1503. END ;
  1504. r.idx := idx ;
  1505. r.kind := 1 ;
  1506. r.boff := 0 ;
  1507. r.cls := symtab [idx].cls ;
  1508. IF symtab [idx].tag = KFunc THEN
  1509. idx := symtab [idx].resvar ;
  1510. r.idx := idx ;
  1511. r.cls := symtab [idx].cls
  1512. ELSIF (symtab [idx].tag # KVar) AND (symtab [idx].tag # KType) THEN
  1513. Err (EUnknown) ;
  1514. RETURN
  1515. END ;
  1516. ParseSub (r)
  1517. END ParseVar ;
  1518. PROCEDURE ConstAdd (a, b : LONGINT) : LONGINT ;
  1519. BEGIN
  1520. RETURN VAL (LONGINT, W16 (a + b))
  1521. END ConstAdd ;
  1522. PROCEDURE ConstSub (a, b : LONGINT) : LONGINT ;
  1523. BEGIN
  1524. RETURN VAL (LONGINT, W16 (a - b))
  1525. END ConstSub ;
  1526. PROCEDURE ConstMul (a, b : LONGINT) : LONGINT ;
  1527. BEGIN
  1528. RETURN VAL (LONGINT, W16 (a * b))
  1529. END ConstMul ;
  1530. PROCEDURE EmXchgAxDx () ;
  1531. (* 92h = XCHG AX,DX. Named for what it EMITS, which is the point of the
  1532. whole naming convention: this used to be called EmMoveAxDx, which is what
  1533. somebody would expect the opcode to be, and it is not - 89 D8 is
  1534. MOV AX,DX, 92h is the exchange. Here the exchange is what is wanted, so
  1535. the name is the only thing that was wrong, and it was wrong in the exact
  1536. way this file's names are not allowed to be: reading as "a move" when it
  1537. is a swap. After EmIDivAxCx the remainder is in DX and `mod` wants it in
  1538. AX; an exchange gets it there in one byte where a move also would, so the
  1539. behaviour is identical either way and only the name lied. *)
  1540. BEGIN
  1541. Ebyte (92H)
  1542. END EmXchgAxDx ;
  1543. PROCEDURE BinOpEmit (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  1544. (* binary operation at one precedence level; folds constant operands *)
  1545. VAR f : LONGINT ;
  1546. okc : BOOLEAN ;
  1547. BEGIN
  1548. IF op = TkAnd THEN
  1549. IF (left.kind = 0) AND (right.kind = 0) THEN
  1550. res.kind := 0 ;
  1551. res.imm := BitAnd (left.imm, right.imm) ;
  1552. res.cls := TBool ;
  1553. RETURN
  1554. END ;
  1555. LoadPair (left, right) ;
  1556. EmAndAxCx () ;
  1557. res.kind := 2 ; res.cls := TBool ;
  1558. RETURN
  1559. END ;
  1560. IF op = TkOr THEN
  1561. IF (left.kind = 0) AND (right.kind = 0) THEN
  1562. res.kind := 0 ;
  1563. res.imm := BitOr (left.imm, right.imm) ;
  1564. res.cls := TBool ;
  1565. RETURN
  1566. END ;
  1567. LoadPair (left, right) ;
  1568. EmOrAxCx () ;
  1569. res.kind := 2 ; res.cls := TBool ;
  1570. RETURN
  1571. END ;
  1572. IF (left.kind = 0) AND (right.kind = 0) THEN
  1573. okc := FALSE ;
  1574. CASE op OF
  1575. OpAdd : f := ConstAdd (left.imm, right.imm) ; okc := TRUE ;
  1576. | OpSub : f := ConstSub (left.imm, right.imm) ; okc := TRUE ;
  1577. | OpMul : f := ConstMul (left.imm, right.imm) ; okc := TRUE ;
  1578. | TkDiv : okc := (right.imm # 0) AND (right.imm > 0)
  1579. AND (left.imm >= 0) ;
  1580. IF okc THEN f := left.imm DIV right.imm END ;
  1581. | TkMod : okc := (right.imm # 0) AND (right.imm > 0)
  1582. AND (left.imm >= 0) ;
  1583. IF okc THEN f := left.imm MOD right.imm END ;
  1584. ELSE
  1585. okc := FALSE
  1586. END ;
  1587. IF okc THEN
  1588. res.kind := 0 ;
  1589. res.imm := VAL (LONGINT, W16 (f)) ;
  1590. res.cls := left.cls ;
  1591. RETURN
  1592. ELSIF op = TkDiv THEN
  1593. Err (EConstRange) ;
  1594. RETURN
  1595. END
  1596. END ;
  1597. LoadPair (left, right) ;
  1598. CASE op OF
  1599. OpAdd : EmAddAxCx ;
  1600. | OpSub : EmSubAxCx ;
  1601. | OpMul : EmMulAxCx ;
  1602. | TkDiv : EmIDivAxCx ;
  1603. | TkMod : EmIDivAxCx ; EmXchgAxDx ;
  1604. ELSE
  1605. Err (ETypeErr)
  1606. END ;
  1607. res.kind := 2 ;
  1608. res.cls := left.cls
  1609. END BinOpEmit ;
  1610. PROCEDURE ConstCmp (op : CARDINAL ; a, b : LONGINT ; VAR f : LONGINT)
  1611. : BOOLEAN ;
  1612. VAR a16, b16 : CARDINAL ;
  1613. BEGIN
  1614. a16 := W16 (a) ;
  1615. b16 := W16 (b) ;
  1616. f := 0 ;
  1617. CASE op OF
  1618. 1 : f := VAL (LONGINT, ORD (a16 = b16)) ;
  1619. | 2 : f := VAL (LONGINT, ORD (a16 # b16)) ;
  1620. | 3 : f := VAL (LONGINT, ORD (a16 < b16)) ;
  1621. | 4 : f := VAL (LONGINT, ORD (a16 >= b16)) ;
  1622. | 5 : f := VAL (LONGINT, ORD (a16 > b16)) ;
  1623. | 6 : f := VAL (LONGINT, ORD (a16 <= b16)) ;
  1624. ELSE
  1625. RETURN FALSE
  1626. END ;
  1627. RETURN TRUE
  1628. END ConstCmp ;
  1629. (* ---------------------------------------------------------------- *)
  1630. (* keeping an operand alive across the parse of the other one *)
  1631. (* ---------------------------------------------------------------- *)
  1632. PROCEDURE SaveLeft (VAR left : ERes) ;
  1633. (* Called the moment the operator has been recognised, i.e. *before* the
  1634. right-hand operand is parsed.
  1635. Every binary level in this parser has the shape
  1636. left := r ; ParseXxx (right) ; BinOpEmit (op, left, right, r) ;
  1637. so the right-hand operand's code is emitted between those two lines, and
  1638. the left-hand operand has to survive that. For kind 0 (a constant) and
  1639. kind 1 (a named variable) there is nothing to survive: LoadAtom re-emits
  1640. the MOV or the load at the operator site, and no code has been emitted
  1641. for the left yet. A kind-2 value is different - its code is already in
  1642. the image and its value exists only in AX, which the right-hand parse is
  1643. about to overwrite. The value had to be parked on the stack instead.
  1644. TPSRC9 does exactly this in the other order: addptr and mulptr CALL
  1645. pushres (which calls loadatom and sets flgpshax) *before* CALL mullevel,
  1646. so PUSH AX lands in the image ahead of the right-hand operand's code.
  1647. Without this, `(p > q) or (q > p)` emitted both comparisons and then
  1648. ORed the *second* comparison's result with itself - which is why it
  1649. printed FALSE for TRUE or FALSE. *)
  1650. BEGIN
  1651. IF left.kind = 2 THEN
  1652. EmPushAx () ;
  1653. left.kind := 4 (* 4 = on the top of the stack *)
  1654. END
  1655. END SaveLeft ;
  1656. PROCEDURE LoadPair (VAR left, right : ERes) ;
  1657. (* Materialise both operands of a binary operator so that, once this
  1658. returns, AX holds the LEFT one and CX the RIGHT one - which is the state
  1659. EmAddAxCx/EmSubAxCx/EmMulAxCx/EmIDivAxCx/EmAndAxCx/EmOrAxCx/EmCmpAxCx
  1660. all expect (see EmXchgAxCx).
  1661. Three shapes, and only three:
  1662. left.kind = 4 SaveLeft pushed it before the right operand was
  1663. parsed, so the right value is in AX and the left one
  1664. is on the stack: load the right (a no-op for kind 2),
  1665. pop the left into CX, then swap.
  1666. right.kind = 2 The right value is in AX, and loading the left would
  1667. destroy it: push the right, load the left, then pop
  1668. the right straight into CX. No swap needed.
  1669. neither Both can still be materialised from scratch, and
  1670. neither is at risk, so this is the original sequence.
  1671. Whichever shape is taken, exactly one push is matched by exactly one
  1672. pop. *)
  1673. BEGIN
  1674. IF left.kind = 4 THEN
  1675. LoadAtom (right) ;
  1676. EmPopCx () ;
  1677. EmXchgAxCx ()
  1678. ELSIF right.kind = 2 THEN
  1679. EmPushAx () ;
  1680. LoadAtom (left) ;
  1681. EmPopCx ()
  1682. ELSE
  1683. LoadAtom (left) ; EmPushAx () ;
  1684. LoadAtom (right) ; EmPopCx () ;
  1685. EmXchgAxCx ()
  1686. END
  1687. END LoadPair ;
  1688. PROCEDURE ParseCmp (VAR r : ERes) ;
  1689. (* "=" | "<>" | "<" | "<=" | ">" | ">=" *)
  1690. VAR op : CARDINAL ;
  1691. left, right : ERes ;
  1692. f : LONGINT ;
  1693. BEGIN
  1694. ParseAdd (r) ;
  1695. LOOP
  1696. op := 0 ;
  1697. Skip () ;
  1698. IF CurCh () = '=' THEN
  1699. op := 1 ; DropCh (GetCh ())
  1700. ELSIF (CurCh () = '<') AND (PeekAhead (1) = '>') THEN
  1701. op := 2 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  1702. ELSIF CurCh () = '<' THEN
  1703. IF PeekAhead (1) = '=' THEN
  1704. op := 6 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  1705. ELSE
  1706. op := 3 ; DropCh (GetCh ())
  1707. END
  1708. ELSIF CurCh () = '>' THEN
  1709. IF PeekAhead (1) = '=' THEN
  1710. op := 5 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  1711. ELSE
  1712. op := 4 ; DropCh (GetCh ())
  1713. END
  1714. END ;
  1715. IF op = 0 THEN
  1716. RETURN
  1717. END ;
  1718. left := r ;
  1719. SaveLeft (left) ;
  1720. ParseAdd (right) ;
  1721. IF (left.kind = 0) AND (right.kind = 0) THEN
  1722. IF ConstCmp (op, left.imm, right.imm, f) THEN
  1723. r.kind := 0 ;
  1724. r.imm := f ;
  1725. r.cls := TBool
  1726. ELSE
  1727. r.kind := 0 ;
  1728. r.imm := 0 ;
  1729. r.cls := TBool
  1730. END
  1731. ELSE
  1732. LoadPair (left, right) ;
  1733. EmCmpAxCx () ;
  1734. (* The mnemonic is written next to every opcode on purpose. `op' is
  1735. a number, so the arm for ">" and the arm for ">=" differed only
  1736. by two hex digits that are each one letter from the other
  1737. meaning -- SETG (9FH) and SETGE (9DH). They were swapped, which
  1738. made a > b mean a >= b and a >= b mean a > b. Only the equality
  1739. boundary could see it: 6>5, 5<5, -1>-2 and every other case I
  1740. tried were already right. An opcode on its own does not say
  1741. which comparison it is the answer to. *)
  1742. CASE op OF
  1743. 1 : EmSetcc (94H) ; (* = SETE *)
  1744. | 2 : EmSetcc (95H) ; (* <> SETNE *)
  1745. | 3 : EmSetcc (9CH) ; (* < SETL *)
  1746. | 4 : EmSetcc (9FH) ; (* > SETG *)
  1747. | 5 : EmSetcc (9DH) ; (* >= SETGE *)
  1748. | 6 : EmSetcc (9EH) (* <= SETLE *)
  1749. END ;
  1750. r.kind := 2 ;
  1751. r.cls := TBool
  1752. END
  1753. END
  1754. END ParseCmp ;
  1755. PROCEDURE ParseAdd (VAR r : ERes) ;
  1756. VAR op : CARDINAL ;
  1757. left, right : ERes ;
  1758. BEGIN
  1759. ParseMul (r) ;
  1760. LOOP
  1761. op := 0 ;
  1762. Skip () ;
  1763. IF CurCh () = '+' THEN
  1764. op := OpAdd ; DropCh (GetCh ())
  1765. ELSIF CurCh () = '-' THEN
  1766. op := OpSub ; DropCh (GetCh ())
  1767. ELSIF KwAhead ("OR") THEN
  1768. GetWord () ;
  1769. op := TkOr
  1770. ELSE
  1771. RETURN
  1772. END ;
  1773. left := r ;
  1774. SaveLeft (left) ;
  1775. ParseMul (right) ;
  1776. BinOpEmit (op, left, right, r)
  1777. END
  1778. END ParseAdd ;
  1779. PROCEDURE ParseMul (VAR r : ERes) ;
  1780. VAR op : CARDINAL ;
  1781. left, right : ERes ;
  1782. BEGIN
  1783. ParseNeg (r) ;
  1784. LOOP
  1785. op := 0 ;
  1786. Skip () ;
  1787. IF CurCh () = '*' THEN
  1788. op := OpMul ; DropCh (GetCh ()) (* OpAdd here meant a*b -> a+b *)
  1789. ELSIF CurCh () = '/' THEN
  1790. op := 2 ; DropCh (GetCh ()) ; Err (ENoLib)
  1791. ELSIF KwAhead ("DIV") THEN
  1792. GetWord () ; op := TkDiv
  1793. ELSIF KwAhead ("MOD") THEN
  1794. GetWord () ; op := TkMod
  1795. ELSIF KwAhead ("AND") THEN
  1796. GetWord () ; op := TkAnd
  1797. ELSE
  1798. RETURN
  1799. END ;
  1800. IF op = 2 THEN
  1801. RETURN
  1802. END ;
  1803. left := r ;
  1804. SaveLeft (left) ;
  1805. ParseNeg (right) ;
  1806. BinOpEmit (op, left, right, r)
  1807. END
  1808. END ParseMul ;
  1809. PROCEDURE ParseNeg (VAR r : ERes) ;
  1810. BEGIN
  1811. Skip () ;
  1812. IF CurCh () = '+' THEN
  1813. DropCh (GetCh ()) ;
  1814. ParseNeg (r) ;
  1815. RETURN
  1816. ELSIF CurCh () = '-' THEN
  1817. DropCh (GetCh ()) ;
  1818. ParseNeg (r) ;
  1819. IF r.kind = 0 THEN
  1820. r.imm := VAL (LONGINT, W16 (0 - r.imm))
  1821. ELSE
  1822. LoadAtom (r) ;
  1823. EmNegAx () ;
  1824. r.kind := 2
  1825. END ;
  1826. RETURN
  1827. ELSIF KwAhead ("NOT") THEN
  1828. GetWord () ;
  1829. ParseNeg (r) ;
  1830. (* TPSRC9 neglevel branches on the operand's type before emitting
  1831. anything: CMP CL,#$0A (integer) -> NOT AX, CMP CL,#$0B (boolean)
  1832. -> XOR AL,#01, anything else -> error. This used to emit NOT AX
  1833. for both, so `not (a = a)' produced 0FFFEh and the runtime's
  1834. wrbool test ([BP+4] <> 0) read it as TRUE.
  1835. The integer constant fold stays: the 16 bits are the same either
  1836. way. A boolean constant must not go through BitNot - 0FFFFH-1 is
  1837. 0FFFEh, which wrbool also reads as TRUE - so it is loaded and
  1838. xor-ed, exactly as neglevel does it. *)
  1839. IF r.cls = TBool THEN
  1840. LoadAtom (r) ;
  1841. EmXorAl01 () ;
  1842. r.kind := 2
  1843. ELSIF r.cls = TScalar THEN
  1844. IF r.kind = 0 THEN
  1845. r.imm := BitNot (r.imm)
  1846. ELSE
  1847. LoadAtom (r) ;
  1848. EmNotAx () ;
  1849. r.kind := 2
  1850. END
  1851. ELSE
  1852. Err (ETypeErr)
  1853. END ;
  1854. RETURN
  1855. END ;
  1856. ParseAtom (r)
  1857. END ParseNeg ;
  1858. PROCEDURE ParseAtom (VAR r : ERes) ;
  1859. (* const | variable | func(params) | '(' expr ')' *)
  1860. VAR idx : CARDINAL ;
  1861. strf : BOOLEAN ;
  1862. quoted : BOOLEAN ;
  1863. BEGIN
  1864. r.chr := FALSE ; (* default: not a quoted char literal *)
  1865. Skip () ;
  1866. IF CurCh () = '(' THEN
  1867. DropCh (GetCh ()) ;
  1868. ParseExpr (r) ;
  1869. ExpectDelim (')', ENoSemi) ;
  1870. RETURN
  1871. END ;
  1872. IF (CurCh () = '$') OR (Digit (CurCh ())) OR (ORD (CurCh ()) = AposC) THEN
  1873. (* remember that this was a quoted literal BEFORE RdConst consumes it:
  1874. RdConst reports a 1-character literal as TScalar (its char code),
  1875. which is right for "c := 'a'" but would make writeln('a') print 97.
  1876. Mark it so the writer picks the char entry, not the integer one. *)
  1877. quoted := (ORD (CurCh ()) = AposC) ;
  1878. RdConst (r.imm, r.cls, strf) ;
  1879. r.chr := quoted AND (r.cls = TScalar) AND NOT strf ;
  1880. IF r.cls = TReal THEN
  1881. Err (ENoLib) ;
  1882. r.kind := 2 ;
  1883. RETURN
  1884. END ;
  1885. IF strf THEN
  1886. (* A string literal is legal here as a *value* - it is not rejected
  1887. at this point, because writeln('hi') needs it and IoCall is the
  1888. only place that knows how to emit one. Everywhere else the
  1889. literal has to end up as a machine word, and that is caught by
  1890. LoadAtom, which refuses a TString. *)
  1891. r.strx := rdStrX ;
  1892. r.kind := 3 ;
  1893. RETURN
  1894. END ;
  1895. r.kind := 0 ;
  1896. RETURN
  1897. END ;
  1898. IF NOT Alpha (CurCh ()) THEN
  1899. Err (EUnknown) ;
  1900. RETURN
  1901. END ;
  1902. GetWord () ;
  1903. IF NOT Search (wrd, idx) THEN
  1904. Err (EUnknown) ;
  1905. RETURN
  1906. END ;
  1907. IF symtab [idx].tag = KConst THEN
  1908. r.kind := 0 ;
  1909. r.imm := symtab [idx].lval ;
  1910. r.cls := symtab [idx].cls ;
  1911. RETURN
  1912. ELSIF symtab [idx].tag = KFunc THEN
  1913. ParseCall (idx) ;
  1914. r.kind := 2 ;
  1915. r.cls := symtab [idx].cls ;
  1916. RETURN
  1917. ELSIF (symtab [idx].tag = KVar) OR (symtab [idx].tag = KType) THEN
  1918. ParseVar (r) ;
  1919. r.boff := 0 ;
  1920. RETURN
  1921. ELSE
  1922. Err (EUnknown)
  1923. END
  1924. END ParseAtom ;
  1925. PROCEDURE AddPend (kind, who, place : CARDINAL) ;
  1926. BEGIN
  1927. IF nPend < MaxPend THEN
  1928. pend [nPend].kind := kind ;
  1929. pend [nPend].who := who ;
  1930. pend [nPend].place := place ;
  1931. INC (nPend)
  1932. ELSE
  1933. Err (ECompOvf)
  1934. END
  1935. END AddPend ;
  1936. PROCEDURE EmCallMost (idx, nk : CARDINAL) ;
  1937. (* emit the call to sym 'idx' and clean up nk value arguments *)
  1938. VAR p : CARDINAL ;
  1939. BEGIN
  1940. IF symtab [idx].defnd THEN
  1941. DropC (EmCall (symtab [idx].goPos))
  1942. ELSE
  1943. p := EmCall (0) ;
  1944. AddPend (1, idx, p)
  1945. END ;
  1946. IF nk > 0 THEN
  1947. EmAddSp (2 * nk)
  1948. END
  1949. END EmCallMost ;
  1950. PROCEDURE ParseCallArgs (idx : CARDINAL) ;
  1951. (* '(' already consumed: read args ')' then call.
  1952. Each argument is loaded and pushed the moment it has been parsed, which is
  1953. what TPSRC8 cproc/cprlp1/cprlp2 do - CALL exprsave, CALL epushax, one per
  1954. argument, then the CALL. A computed argument (kind 2) exists only in AX,
  1955. so parsing the argument after it would destroy it before the push loop
  1956. ever reached it: `p (a > b, x)' passed x twice.
  1957. Pushing in parse order is also the order the callee is laid out for: the
  1958. first argument goes on first and so ends up farthest from BP, and the LAST
  1959. declared parameter is the one at BP+4 (RESUME-TP3.md 3.11). *)
  1960. VAR arg : ERes ;
  1961. nArgs : CARDINAL ;
  1962. BEGIN
  1963. nArgs := 0 ;
  1964. IF CurCh () = ')' THEN
  1965. DropCh (GetCh ())
  1966. ELSE
  1967. LOOP
  1968. IF nArgs >= 16 THEN
  1969. Err (ECompOvf) ;
  1970. EXIT
  1971. END ;
  1972. ParseExpr (arg) ;
  1973. LoadAtom (arg) ;
  1974. EmPushAx () ;
  1975. INC (nArgs) ;
  1976. IF NOT MatchDelim (',') THEN
  1977. EXIT
  1978. END
  1979. END ;
  1980. ExpectDelim (')', ENoSemi)
  1981. END ;
  1982. EmCallMost (idx, nArgs)
  1983. END ParseCallArgs ;
  1984. PROCEDURE ParseCall (idx : CARDINAL) ;
  1985. (* procedure/function call; '(' optional - same parse-then-push as above,
  1986. reached from ParseAtom when a FUNCTION is called in an expression *)
  1987. VAR arg : ERes ;
  1988. nArgs : CARDINAL ;
  1989. BEGIN
  1990. nArgs := 0 ;
  1991. IF MatchDelim ('(') THEN
  1992. IF CurCh () # ')' THEN
  1993. LOOP
  1994. IF nArgs >= 16 THEN
  1995. Err (ECompOvf) ;
  1996. EXIT
  1997. END ;
  1998. ParseExpr (arg) ;
  1999. LoadAtom (arg) ;
  2000. EmPushAx () ;
  2001. INC (nArgs) ;
  2002. IF NOT MatchDelim (',') THEN
  2003. EXIT
  2004. END
  2005. END ;
  2006. ExpectDelim (')', ENoSemi)
  2007. ELSE
  2008. DropCh (GetCh ())
  2009. END
  2010. END ;
  2011. EmCallMost (idx, nArgs)
  2012. END ParseCall ;
  2013. PROCEDURE ParseExpr (VAR r : ERes) ;
  2014. BEGIN
  2015. ParseCmp (r)
  2016. END ParseExpr ;
  2017. (* ---------------------------------------------------------------- *)
  2018. (* statements (TPSRC8) *)
  2019. (* ---------------------------------------------------------------- *)
  2020. PROCEDURE ParseLabelStmt () ;
  2021. (* numeric label definition 'n :' *)
  2022. VAR n : CARDINAL ;
  2023. nm : ARRAY [0..9] OF CHAR ;
  2024. idx : CARDINAL ;
  2025. i : CARDINAL ;
  2026. BEGIN
  2027. n := 0 ;
  2028. WHILE Digit (CurCh ()) DO
  2029. n := n * 10 + (ORD (CurCh ()) - ORD ('0')) ;
  2030. DropCh (GetCh ())
  2031. END ;
  2032. ExpectDelim (':', ENoSemi) ;
  2033. NumToName (n, nm) ;
  2034. IF Search (nm, idx) THEN
  2035. IF symtab [idx].tag = KLabel THEN
  2036. symtab [idx].defnd := TRUE ;
  2037. symtab [idx].goPos := pc ;
  2038. i := 0 ;
  2039. WHILE i < nPend DO
  2040. IF (pend [i].kind = 0) AND (pend [i].who = idx) THEN
  2041. SetPatTgt (pend [i].place, pc) ;
  2042. pend [i].kind := 99
  2043. END ;
  2044. INC (i)
  2045. END
  2046. ELSE
  2047. Err (EUnknown)
  2048. END
  2049. ELSE
  2050. idx := NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE) ;
  2051. symtab [idx].defnd := TRUE ;
  2052. symtab [idx].goPos := pc
  2053. END
  2054. END ParseLabelStmt ;
  2055. PROCEDURE Assignment (r : ERes) ;
  2056. (* ':=' already consumed by the caller; store expression into r *)
  2057. VAR src : ERes ;
  2058. BEGIN
  2059. IF r.kind # 1 THEN
  2060. Err (EUnknown) ;
  2061. RETURN
  2062. END ;
  2063. IF symtab [r.idx].size > 2 THEN
  2064. Err (ENoLib) ;
  2065. RETURN
  2066. END ;
  2067. ParseExpr (src) ;
  2068. LoadAtom (src) ;
  2069. EmStoreVar (symtab [r.idx].local,
  2070. (symtab [r.idx].off + r.boff) MOD 10000H,
  2071. symtab [r.idx].size)
  2072. END Assignment ;
  2073. PROCEDURE Compound () ;
  2074. (* BEGIN statement ';' ... END; END is consumed here *)
  2075. VAR tok : CARDINAL ;
  2076. BEGIN
  2077. LOOP
  2078. PeekKw (tok) ;
  2079. IF tok = TkEnd THEN
  2080. DropB (MatchKey (tok)) ;
  2081. RETURN
  2082. END ;
  2083. Statmnt () ;
  2084. IF NOT OK () THEN
  2085. RETURN
  2086. END ;
  2087. IF NOT MatchDelim (';') THEN
  2088. PeekKw (tok) ;
  2089. IF tok = TkEnd THEN
  2090. DropB (MatchKey (tok)) ;
  2091. RETURN
  2092. END ;
  2093. Err (ENoSemi) ;
  2094. RETURN
  2095. END
  2096. END
  2097. END Compound ;
  2098. PROCEDURE IoCall (idx : CARDINAL) ;
  2099. (* WRITE / WRITELN / READ / READLN / HALT.
  2100. TP3 (TPSRC8 pwriteln, pwrloop, prdtyped) does not pass a descriptor to
  2101. the runtime: it looks at each argument's class and emits a *different*
  2102. call per type, so the formatting is fixed at compile time. Mirrored
  2103. here - one call per argument, then a final call for the line break.
  2104. WRITE/WRITELN push the value; READ/READLN push the address, so the
  2105. runtime can store. As everywhere else in this compiler the caller
  2106. cleans the argument off the stack. *)
  2107. VAR arg : ERes ;
  2108. nArgs, ent, which, acls : CARDINAL ;
  2109. reading : BOOLEAN ;
  2110. pushed : BOOLEAN ;
  2111. n : CARDINAL ;
  2112. dummy : ERes ;
  2113. BEGIN
  2114. which := symtab [idx].cls ; (* BI_* *)
  2115. IF which = BI_Halt THEN
  2116. IF MatchDelim ('(') THEN (* halt(0) - code ignored *)
  2117. ParseExpr (dummy) ;
  2118. ExpectDelim (')', ENoSemi)
  2119. END ;
  2120. DropC (EmCall (TU_Halt)) ;
  2121. RETURN
  2122. END ;
  2123. reading := (which = BI_Read) OR (which = BI_ReadLn) ;
  2124. nArgs := 0 ;
  2125. IF MatchDelim ('(') THEN
  2126. IF CurCh () # ')' THEN
  2127. LOOP
  2128. IF nArgs >= 16 THEN
  2129. Err (ECompOvf) ;
  2130. EXIT
  2131. END ;
  2132. ParseExpr (arg) ;
  2133. (* One argument at a time: parse it, give it its own push or its
  2134. own runtime call, and only then read the next one. TPSRC8
  2135. pwrloop does exactly this, and it has to - a computed argument
  2136. exists only in AX, so parsing the argument after it, and the
  2137. call made for THIS one, are both free to destroy it. Reading
  2138. the whole list first and emitting afterwards printed the first
  2139. argument as the last argument's value, and pushed an argument
  2140. that an earlier inline string's call had already overwritten. *)
  2141. pushed := TRUE ; (* default: value is on the stack -> call + pop *)
  2142. IF reading THEN
  2143. IF arg.kind # 1 THEN
  2144. Err (ETypeErr) ; (* READ needs a variable *)
  2145. RETURN
  2146. END ;
  2147. acls := symtab [arg.idx].cls ;
  2148. IF acls = TString THEN
  2149. Err (ENoLib) ; (* string runtime pending *)
  2150. RETURN
  2151. END ;
  2152. EmPushVarAddr (symtab [arg.idx].local, symtab [arg.idx].off) ;
  2153. IF acls = TReal THEN
  2154. ent := TU_RdInt (* real reads: not yet *)
  2155. ELSIF acls = TBool THEN
  2156. ent := TU_RdBool
  2157. ELSIF acls = TChar THEN
  2158. ent := TU_RdChar (* one byte, not a word *)
  2159. ELSIF acls = TScalar THEN
  2160. ent := TU_RdInt
  2161. ELSE
  2162. Err (ETypeErr) ;
  2163. RETURN
  2164. END
  2165. ELSE
  2166. acls := arg.cls ;
  2167. IF (acls = TString) AND (arg.kind = 3) THEN
  2168. (* An inline string literal. TP3 TPSRC8 pwrinlin special-cases a
  2169. literal that is followed directly by ',' or ')' - i.e. an
  2170. argument, not an expression - and emits
  2171. CALL wrtinl <length byte> <characters...>
  2172. with no stack argument at all; wrtinl reads the length from the
  2173. return address and returns to just past the last character.
  2174. Mirrored exactly, so the literal costs only its own characters
  2175. in the code stream and nothing in the data segment. *)
  2176. IF strLen [arg.strx] > 255 THEN
  2177. (* The length is one byte, so a literal of 256 characters or
  2178. more would wrap: 300 characters emitted behind a length of
  2179. 44, and the runtime would print 44 of them and silently drop
  2180. the rest. TP3 strings are at most 255 characters, so refuse
  2181. rather than truncate. *)
  2182. Err (EConstRange) ;
  2183. RETURN
  2184. END ;
  2185. DropC (EmCall (TU_WrInl)) ;
  2186. Ebyte (VAL (BYTE, strLen [arg.strx])) ;
  2187. n := 0 ;
  2188. WHILE n < strLen [arg.strx] DO
  2189. Ebyte (VAL (BYTE, ORD (strPool [strOff [arg.strx] + n]))) ;
  2190. INC (n)
  2191. END ;
  2192. pushed := FALSE (* nothing was pushed for this one *)
  2193. ELSE
  2194. IF acls = TString THEN
  2195. (* A string *variable*. Not emitted rather than emitted wrongly:
  2196. EmPushVarAddr's local form is still wrong (see the note on
  2197. that procedure), and a wrong address here would print
  2198. garbage instead of failing. *)
  2199. Err (ENoLib) ;
  2200. RETURN
  2201. END ;
  2202. LoadAtom (arg) ;
  2203. EmPushAx () ;
  2204. IF arg.chr THEN
  2205. ent := TU_WrChar (* 'a' - one char, not 97 *)
  2206. ELSIF acls = TReal THEN
  2207. ent := TU_WrReal
  2208. ELSIF acls = TBool THEN
  2209. ent := TU_WrBool
  2210. ELSIF acls = TChar THEN
  2211. ent := TU_WrChar (* c : char - one char *)
  2212. ELSIF acls = TScalar THEN
  2213. ent := TU_WrInt
  2214. ELSE
  2215. Err (ETypeErr) ;
  2216. RETURN
  2217. END
  2218. END
  2219. END ;
  2220. IF pushed THEN
  2221. DropC (EmCall (ent)) ;
  2222. EmAddSp (2) (* one 16-bit argument *)
  2223. END ;
  2224. INC (nArgs) ;
  2225. IF NOT MatchDelim (',') THEN
  2226. EXIT
  2227. END
  2228. END ;
  2229. IF NOT MatchDelim (')') THEN
  2230. Err (ENoSemi) ;
  2231. RETURN
  2232. END
  2233. ELSE
  2234. DropCh (GetCh ())
  2235. END
  2236. END ;
  2237. IF reading AND (nArgs = 0) THEN
  2238. (* readln with no variable: just skip to the next line *)
  2239. DropC (EmCall (TU_RdLn)) ;
  2240. RETURN
  2241. END ;
  2242. IF which = BI_WriteLn THEN
  2243. DropC (EmCall (TU_WrLn))
  2244. ELSIF which = BI_ReadLn THEN
  2245. DropC (EmCall (TU_RdLn))
  2246. END
  2247. END IoCall ;
  2248. PROCEDURE Statmnt () ;
  2249. VAR tok : CARDINAL ;
  2250. idx, i2 : CARDINAL ;
  2251. t, src : ERes ;
  2252. L1, zj, zj2, exj : CARDINAL ;
  2253. lo, hi, v : LONGINT ;
  2254. clso : CARDINAL ;
  2255. i : CARDINAL ;
  2256. nm : ARRAY [0..MaxName] OF CHAR ;
  2257. strf : BOOLEAN ;
  2258. dow : BOOLEAN ;
  2259. BEGIN
  2260. Skip () ;
  2261. IF Digit (CurCh ()) THEN
  2262. ParseLabelStmt () ; (* consumed 'n' ':' *)
  2263. Statmnt () ; (* 'n : statement' - the statement follows
  2264. the label directly, with no ';' between *)
  2265. RETURN
  2266. END ;
  2267. IF NOT Alpha (CurCh ()) THEN
  2268. ExpectDelim (';', ENoSemi) ;
  2269. RETURN
  2270. END ;
  2271. DropB (MatchKey (tok)) ;
  2272. IF tok = TkBegin THEN
  2273. Compound ()
  2274. ELSIF tok = TkIf THEN
  2275. ParseExpr (t) ;
  2276. LoadAtom (t) ;
  2277. EmCmpAxi (0) ;
  2278. zj := EmJcc (84H, 0) ; (* JZ -> else/end *)
  2279. IF NOT (MatchKey (tok) AND (tok = TkThen)) THEN
  2280. Err (ENoSemi)
  2281. END ;
  2282. Statmnt () ;
  2283. (* Peek the ELSE, do not match it. `MatchKey (tok) AND (tok = TkElse)`
  2284. consumes whatever the next keyword is even when the AND fails, so an
  2285. `if` that was the LAST statement of a BEGIN..END block ate the block's
  2286. own END: Compound then found neither ';' nor END and raised ENoSemi.
  2287. Any `if` as the last statement of a compound was unparseable - not
  2288. in a loop, not anywhere - and no fixture had one, so nothing noticed.
  2289. The visible symptom was a parse error at the statement AFTER the
  2290. block, which points at entirely the wrong piece of source. *)
  2291. PeekKw (tok) ;
  2292. IF tok = TkElse THEN
  2293. DropB (MatchKey (tok)) ;
  2294. exj := EmJmpNear (0) ;
  2295. SetPatTgt (zj, pc) ;
  2296. Statmnt () ;
  2297. SetPatTgt (exj, pc)
  2298. ELSE
  2299. SetPatTgt (zj, pc)
  2300. END
  2301. ELSIF tok = TkWhile THEN
  2302. L1 := pc ;
  2303. ParseExpr (t) ;
  2304. LoadAtom (t) ;
  2305. EmCmpAxi (0) ;
  2306. zj := EmJcc (84H, 0) ; (* JZ -> end *)
  2307. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  2308. Err (ENoSemi)
  2309. END ;
  2310. brkSave [brkN] := exitCnt ;
  2311. loopTy [brkN] := 1 ;
  2312. INC (brkN) ;
  2313. Statmnt () ;
  2314. DEC (brkN) ;
  2315. i := brkSave [brkN] ;
  2316. WHILE i < exitCnt DO
  2317. SetPatTgt (exitPatch [i], pc) ;
  2318. INC (i)
  2319. END ;
  2320. exitCnt := brkSave [brkN] ;
  2321. DropC (EmJmpNear (L1)) ;
  2322. SetPatTgt (zj, pc)
  2323. ELSIF tok = TkRepeat THEN
  2324. L1 := pc ;
  2325. brkSave [brkN] := exitCnt ;
  2326. loopTy [brkN] := 1 ;
  2327. INC (brkN) ;
  2328. Statmnt () ;
  2329. IF NOT (MatchKey (tok) AND (tok = TkUntil)) THEN
  2330. Err (ENoSemi)
  2331. END ;
  2332. ParseExpr (t) ;
  2333. LoadAtom (t) ;
  2334. EmCmpAxi (0) ;
  2335. (* UNTIL exits when the condition is TRUE, so the body runs again when
  2336. it is FALSE. The condition is a 0/1 in AX and EmCmpAxi (0) has just
  2337. compared it with 0, so ZF=1 means "condition false" -- which is
  2338. exactly the case that loops, hence JZ.
  2339. This was JNZ, which compiled repeat-until as while-until: the body
  2340. ran once, the condition was tested, and it stopped. t12_repeat is
  2341. `i:=0; repeat i:=i+1 until i>5' and it printed 1.
  2342. No patch slot: L1 is backwards and already known, so EmJcc returns
  2343. 0 and there is nothing to SetPatTgt. *)
  2344. DropC (EmJcc (84H, L1)) ; (* JZ -> body again *)
  2345. DEC (brkN) ;
  2346. i := brkSave [brkN] ;
  2347. WHILE i < exitCnt DO
  2348. SetPatTgt (exitPatch [i], pc) ;
  2349. INC (i)
  2350. END ;
  2351. exitCnt := brkSave [brkN]
  2352. ELSIF tok = TkFor THEN
  2353. (* control variable *)
  2354. Skip () ; (* after the FOR keyword: skip blanks *)
  2355. IF NOT Alpha (CurCh ()) THEN
  2356. Err (EUnknown) ;
  2357. RETURN
  2358. END ;
  2359. GetWord () ;
  2360. IF NOT Search (wrd, idx) THEN
  2361. Err (EUnknown) ;
  2362. RETURN
  2363. END ;
  2364. IF symtab [idx].size > 2 THEN
  2365. Err (ENoLib) ;
  2366. RETURN
  2367. END ;
  2368. IF NOT MatchAssign () THEN
  2369. Err (ENoSemi)
  2370. END ;
  2371. ParseExpr (src) ;
  2372. LoadAtom (src) ;
  2373. EmStoreVar (symtab [idx].local, symtab [idx].off,
  2374. symtab [idx].size) ;
  2375. IF NOT MatchKey (tok) THEN
  2376. Err (ESimpType) ;
  2377. RETURN
  2378. END ;
  2379. IF (tok = TkTo) OR (tok = TkDownto) THEN
  2380. dow := (tok = TkDownto)
  2381. ELSE
  2382. Err (ESimpType) ;
  2383. RETURN
  2384. END ;
  2385. ParseExpr (t) ;
  2386. LoadAtom (t) ;
  2387. EmPushAx () ; (* loop bound on the stack *)
  2388. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  2389. Err (ENoSemi)
  2390. END ;
  2391. brkSave [brkN] := exitCnt ;
  2392. loopTy [brkN] := 2 ;
  2393. INC (brkN) ;
  2394. L1 := pc ; (* Ltest: the test comes FIRST *)
  2395. (* The test is emitted before the body, not after it. It used to be
  2396. emitted after, which is a post-test loop and runs the body one time
  2397. too many: with `for i := 1 to 5`, the sequence of i at the test is
  2398. 1,2,3,4,5,6 - the test at i=5 is `5 > 5`, which is false, so the
  2399. body ran a sixth time with i=6. `for i := 1 to 5 do s := s + i`
  2400. printed 21. The size was right and the shape was right; only the
  2401. order was wrong, and no byte check can see an order.
  2402. The bound stays on the stack for the whole loop, so EmMovCxSp has to
  2403. re-read it every iteration - which is also what makes the bound a
  2404. *variable* rather than a constant. [SP] cannot be encoded on the
  2405. 8086, so EmMovCxSp is POP CX ; PUSH CX, an observational no-op that
  2406. leaves the bound in place. *)
  2407. EmMovCxSp () ;
  2408. EmLoadVar (symtab [idx].local, symtab [idx].off,
  2409. symtab [idx].size) ;
  2410. EmCmpAxCx () ;
  2411. IF dow THEN
  2412. zj := EmJcc (8CH, 0) (* JL -> done *)
  2413. ELSE
  2414. zj := EmJcc (8FH, 0) (* JG -> done *)
  2415. END ;
  2416. Statmnt () ;
  2417. DEC (brkN) ;
  2418. (* A FOR's exits are NOT patched here, even though `done` is not known
  2419. yet. For a WHILE or REPEAT, "just after the body" is a correct
  2420. target: the jump back to the test re-evaluates the condition and
  2421. leaves. For a FOR there is a STEP between the body and `done`, so
  2422. an EXIT that jumped here would increment the control variable and
  2423. jump back to the test - and if the incremented value still satisfied
  2424. the bound, it would run the body AGAIN. `exit` did not exit.
  2425. The fix needs no new bookkeeping: `brkSave [brkN] .. exitCnt` still
  2426. names exactly this loop's exits, because Statmnt may have added more
  2427. and nothing has reset exitCnt. So they are patched at `done`, below.
  2428. A WHILE nested inside the FOR saves and restores its own range and
  2429. leaves this one intact. *)
  2430. (* step *)
  2431. EmLoadVar (symtab [idx].local, symtab [idx].off,
  2432. symtab [idx].size) ;
  2433. IF dow THEN
  2434. EmDecAx ()
  2435. ELSE
  2436. EmIncAx ()
  2437. END ;
  2438. EmStoreVar (symtab [idx].local, symtab [idx].off,
  2439. symtab [idx].size) ;
  2440. DropC (EmJmpNear (L1)) ;
  2441. SetPatTgt (zj, pc) ; (* done: *)
  2442. (* A WHILE loop here, not a FOR over the exit range. The range is
  2443. usually EMPTY - most loops have no `exit` - and `exitCnt` is a
  2444. CARDINAL, so `TO exitCnt - 1` with exitCnt = 0 is `TO 65535`: the
  2445. loop does not terminate, it wraps, and it walks exitPatch [0..65535]
  2446. off the end of a 64-element array. `for i := 1 to 10 do i := i` has
  2447. no exit, so this is the ORDINARY case, and it faulted with
  2448. "invalid address referenced" on every FOR loop without an exit. *)
  2449. i := brkSave [brkN] ;
  2450. WHILE i < exitCnt DO
  2451. SetPatTgt (exitPatch [i], pc) ;
  2452. INC (i)
  2453. END ;
  2454. exitCnt := brkSave [brkN] ;
  2455. EmAddSp (2) (* drop the loop bound *)
  2456. ELSIF tok = TkCase THEN
  2457. ParseExpr (t) ;
  2458. LoadAtom (t) ;
  2459. EmPushAx () ; (* selector on the stack *)
  2460. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  2461. Err (ENoSemi)
  2462. END ;
  2463. caseN := 0 ;
  2464. LOOP
  2465. Skip () ;
  2466. IF MatchDelim (';') THEN
  2467. Skip ()
  2468. END ;
  2469. PeekKw (tok) ;
  2470. IF (tok = TkEnd) OR (tok = TkElse) THEN
  2471. EXIT
  2472. END ;
  2473. (* case label : constant identifier or literal *)
  2474. IF Alpha (CurCh ()) THEN
  2475. GetWord () ;
  2476. IF Search (wrd, i2) AND (symtab [i2].tag = KConst) THEN
  2477. lo := symtab [i2].lval
  2478. ELSE
  2479. Err (EUnknown) ;
  2480. EXIT
  2481. END
  2482. ELSE
  2483. RdConst (lo, clso, strf)
  2484. END ;
  2485. IF MatchRange () THEN
  2486. RdConst (hi, clso, strf)
  2487. ELSE
  2488. hi := lo
  2489. END ;
  2490. ExpectDelim (':', ENoSemi) ;
  2491. EmMovAxSp () ;
  2492. EmCmpAxi (W16 (lo)) ;
  2493. zj := EmJcc (85H, 0) ; (* JNZ -> next *)
  2494. IF hi # lo THEN
  2495. EmCmpAxi (W16 (hi)) ;
  2496. zj2 := EmJcc (85H, 0)
  2497. ELSE
  2498. zj2 := 0
  2499. END ;
  2500. Statmnt () ;
  2501. IF caseN >= 64 THEN
  2502. Err (ECompOvf) ;
  2503. EXIT
  2504. END ;
  2505. caseJmp [caseN] := EmJmpNear (0) ;
  2506. INC (caseN) ;
  2507. SetPatTgt (zj, pc) ;
  2508. IF zj2 # 0 THEN
  2509. SetPatTgt (zj2, pc)
  2510. END
  2511. END ;
  2512. IF tok = TkElse THEN
  2513. DropB (MatchKey (tok)) ;
  2514. Statmnt () ;
  2515. IF NOT (MatchKey (tok) AND (tok = TkEnd)) THEN
  2516. Err (ENoSemi)
  2517. END
  2518. ELSE
  2519. DropB (MatchKey (tok))
  2520. END ;
  2521. EmAddSp (2) ;
  2522. FOR i := 0 TO caseN - 1 DO
  2523. SetPatTgt (caseJmp [i], pc)
  2524. END
  2525. ELSIF tok = TkGoto THEN
  2526. v := 0 ;
  2527. Skip () ; (* after the GOTO keyword: skip blanks *)
  2528. IF Digit (CurCh ()) THEN
  2529. RdIntConst (v) ;
  2530. NumToName (W16 (v), nm) ;
  2531. IF Search (nm, idx) THEN
  2532. IF symtab [idx].tag = KLabel THEN
  2533. IF symtab [idx].defnd THEN
  2534. DropC (EmJmpNear (symtab [idx].goPos))
  2535. ELSE
  2536. zj := EmJmpNear (0) ;
  2537. AddPend (0, idx, zj)
  2538. END
  2539. ELSE
  2540. Err (EUnknown)
  2541. END
  2542. ELSE
  2543. idx := NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE) ;
  2544. zj := EmJmpNear (0) ;
  2545. AddPend (0, idx, zj)
  2546. END
  2547. ELSE
  2548. Err (EUnknown)
  2549. END
  2550. ELSIF tok = TkExit THEN
  2551. IF brkN = 0 THEN
  2552. Err (EUnknown)
  2553. ELSE
  2554. (* No EmAddSp (2) here, even inside a FOR. The FOR's `done` label
  2555. drops the bound, so an EXIT that jumped to `done` would drop it a
  2556. second time - 4 bytes off a stack that only had 2 to give, which
  2557. silently corrupts the caller's frame. It used to do exactly
  2558. that, and it was doubly wrong: the exits were patched to the STEP
  2559. rather than to `done`, so the EXIT also incremented the control
  2560. variable and jumped back into the test. *)
  2561. zj := EmJmpNear (0) ;
  2562. IF exitCnt < 64 THEN
  2563. exitPatch [exitCnt] := zj ;
  2564. INC (exitCnt)
  2565. END
  2566. END
  2567. ELSIF tok = TkWith THEN
  2568. Err (ENoLib)
  2569. ELSE
  2570. (* identifier statement: assignment or call *)
  2571. IF NOT Search (wrd, idx) THEN
  2572. Err (EUnknown) ;
  2573. RETURN
  2574. END ;
  2575. IF symtab [idx].tag = KBuiltin THEN
  2576. IoCall (idx) ;
  2577. RETURN
  2578. END ;
  2579. IF symtab [idx].tag = KProc THEN
  2580. IF MatchDelim ('(') THEN
  2581. ParseCallArgs (idx)
  2582. ELSE
  2583. ParseCall (idx)
  2584. END ;
  2585. RETURN
  2586. END ;
  2587. IF (symtab [idx].tag = KVar) OR (symtab [idx].tag = KFunc) THEN
  2588. t.idx := idx ;
  2589. t.kind := 1 ;
  2590. t.boff := 0 ;
  2591. t.cls := symtab [idx].cls ;
  2592. IF symtab [idx].tag = KFunc THEN
  2593. t.idx := symtab [idx].resvar ;
  2594. t.cls := symtab [t.idx].cls
  2595. END ;
  2596. ParseSub (t) ;
  2597. IF MatchAssign () THEN
  2598. Assignment (t) ;
  2599. RETURN
  2600. END ;
  2601. Err (ENoSemi) ;
  2602. RETURN
  2603. END ;
  2604. Err (ENoSemi)
  2605. END
  2606. END Statmnt ;
  2607. (* ---------------------------------------------------------------- *)
  2608. (* types and declarations (TPSRC7) *)
  2609. (* ---------------------------------------------------------------- *)
  2610. PROCEDURE ParseType (VAR cls, size, elem : CARDINAL) ;
  2611. VAR tok : CARDINAL ;
  2612. idx : CARDINAL ;
  2613. lo, hi : LONGINT ;
  2614. s2, e2 : CARDINAL ;
  2615. subcls : CARDINAL ;
  2616. strf : BOOLEAN ;
  2617. consumed : BOOLEAN ;
  2618. BEGIN
  2619. cls := TNone ; size := 0 ; elem := 0 ;
  2620. consumed := FALSE ;
  2621. Skip () ; (* after ':' / '=' : skip blanks *)
  2622. IF Alpha (CurCh ()) THEN
  2623. DropB (MatchKey (tok)) ;
  2624. consumed := TRUE
  2625. ELSE
  2626. tok := TkNone
  2627. END ;
  2628. IF tok = TkArray THEN
  2629. ExpectDelim ('[', ENoSemi) ;
  2630. RdConst (lo, subcls, strf) ;
  2631. IF NOT MatchRange () THEN
  2632. Err (ESimpType)
  2633. END ;
  2634. RdConst (hi, subcls, strf) ;
  2635. ExpectDelim (']', ENoSemi) ;
  2636. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  2637. Err (ENoSemi)
  2638. END ;
  2639. ParseType (cls, s2, e2) ;
  2640. cls := TArray ;
  2641. elem := s2 ;
  2642. size := s2 * (W16 (VAL (LONGINT, W16 (hi))
  2643. - VAL (LONGINT, W16 (lo)) + 1))
  2644. ELSIF tok = TkString THEN
  2645. cls := TString ;
  2646. size := 256 ;
  2647. elem := 1 ;
  2648. IF MatchDelim ('[') THEN
  2649. RdConst (hi, subcls, strf) ;
  2650. ExpectDelim (']', ENoSemi) ;
  2651. size := W16 (hi) + 1
  2652. END
  2653. ELSIF tok = TkSet THEN
  2654. Err (ENoLib) ;
  2655. (* Unreachable, and it would be wrong even if it were reached: a
  2656. speculative `MatchKey (tok) AND (tok = TkOf)` consumes the token it
  2657. rejects. See the note in the IF handler. *)
  2658. IF MatchKey (tok) AND (tok = TkOf) THEN
  2659. ParseType (cls, s2, e2)
  2660. END
  2661. ELSIF tok = TkRecord THEN
  2662. Err (ENoLib) ;
  2663. LOOP
  2664. PeekKw (tok) ;
  2665. IF tok = TkEnd THEN
  2666. DropB (MatchKey (tok)) ;
  2667. EXIT
  2668. END ;
  2669. IF CurCh () = 0C THEN
  2670. EXIT
  2671. END ;
  2672. Skip () ;
  2673. IF Alpha (CurCh ()) THEN
  2674. DropCh (GetCh ())
  2675. ELSE
  2676. DropCh (GetCh ())
  2677. END
  2678. END
  2679. ELSIF (tok = TkFile) OR (tok = TkText) THEN
  2680. cls := TFile ;
  2681. size := 0 ;
  2682. elem := 0 ;
  2683. Err (ENoLib)
  2684. ELSE
  2685. IF consumed THEN
  2686. IF Search (wrd, idx) AND (symtab [idx].tag = KType) THEN
  2687. cls := symtab [idx].cls ;
  2688. size := symtab [idx].size ;
  2689. elem := symtab [idx].size
  2690. ELSE
  2691. Err (EUnknown)
  2692. END
  2693. ELSE
  2694. (* subrange lo .. hi *)
  2695. RdConst (lo, subcls, strf) ;
  2696. IF NOT MatchRange () THEN
  2697. Err (ESimpType) ;
  2698. RETURN
  2699. END ;
  2700. RdConst (hi, subcls, strf) ;
  2701. cls := TScalar ;
  2702. size := 2 ;
  2703. elem := 2
  2704. END
  2705. END
  2706. END ParseType ;
  2707. PROCEDURE DefVar () ;
  2708. (* 'name' (',' name)* ':' type [ABSOLUTE addr] ; group repeated until a
  2709. declaration keyword appears *)
  2710. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  2711. cls, size, elem : CARDINAL ;
  2712. tok : CARDINAL ;
  2713. v : LONGINT ;
  2714. off : CARDINAL ;
  2715. BEGIN
  2716. LOOP
  2717. Skip () ; (* after the VAR keyword: skip blanks *)
  2718. IF NOT Alpha (CurCh ()) THEN
  2719. Err (EUnknown) ;
  2720. RETURN
  2721. END ;
  2722. LOOP
  2723. GetWord () ;
  2724. SaveWord (nm) ;
  2725. DupTest (nm) ;
  2726. IF NOT MatchDelim (':') THEN
  2727. Err (ENoSemi)
  2728. END ;
  2729. ParseType (cls, size, elem) ;
  2730. off := 0 ;
  2731. IF lexnest = 0 THEN
  2732. IF size > 2 THEN
  2733. Err (ENoLib) ;
  2734. RETURN
  2735. END ;
  2736. PeekKw (tok) ;
  2737. IF tok = TkAbsolute THEN
  2738. DropB (MatchKey (tok)) ;
  2739. IF (CurCh () = '$') OR (Digit (CurCh ())) THEN
  2740. RdIntConst (v) ;
  2741. off := W16 (v)
  2742. ELSE
  2743. Err (EUnknown)
  2744. END
  2745. ELSE
  2746. off := dc ;
  2747. dc := dc + size
  2748. END ;
  2749. DropC (NewSym (nm, KVar, cls, size, elem, off, 0, FALSE)) ;
  2750. varspc := varspc + size
  2751. ELSE
  2752. IF size > 2 THEN
  2753. Err (ENoLib) ;
  2754. RETURN
  2755. END ;
  2756. DropC (NewSym (nm, KVar, cls, size, elem, locFree, 0, TRUE)) ;
  2757. locFree := (locFree - size) MOD 10000H ;
  2758. locBytes := locBytes + size
  2759. END ;
  2760. IF NOT MatchDelim (',') THEN
  2761. EXIT
  2762. END
  2763. END ;
  2764. IF NOT MatchDelim (';') THEN
  2765. Err (ENoSemi) ;
  2766. RETURN
  2767. END ;
  2768. PeekKw (tok) ;
  2769. IF tok # TkNone THEN
  2770. RETURN
  2771. END
  2772. END
  2773. END DefVar ;
  2774. PROCEDURE DefConst () ;
  2775. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  2776. v : LONGINT ;
  2777. cls : CARDINAL ;
  2778. isStr : BOOLEAN ;
  2779. tok : CARDINAL ;
  2780. BEGIN
  2781. LOOP
  2782. PeekKw (tok) ;
  2783. IF tok # TkNone THEN
  2784. RETURN
  2785. END ;
  2786. Skip () ; (* after the CONST keyword: skip blanks *)
  2787. IF NOT Alpha (CurCh ()) THEN
  2788. Err (EUnknown) ;
  2789. RETURN
  2790. END ;
  2791. GetWord () ;
  2792. SaveWord (nm) ;
  2793. DupTest (nm) ;
  2794. ExpectDelim ('=', ENoSemi) ;
  2795. RdConst (v, cls, isStr) ;
  2796. IF isStr THEN
  2797. Err (ENoLib)
  2798. END ;
  2799. DropC (NewSym (nm, KConst, cls, 0, 0, 0, v, FALSE)) ;
  2800. IF NOT MatchDelim (';') THEN
  2801. Err (ENoSemi) ;
  2802. RETURN
  2803. END
  2804. END
  2805. END DefConst ;
  2806. PROCEDURE DefLabelPart () ;
  2807. VAR nm : ARRAY [0..9] OF CHAR ;
  2808. n : CARDINAL ;
  2809. BEGIN
  2810. LOOP
  2811. Skip () ;
  2812. IF NOT Digit (CurCh ()) THEN
  2813. Err (EUnknown) ;
  2814. RETURN
  2815. END ;
  2816. n := 0 ;
  2817. WHILE Digit (CurCh ()) DO
  2818. n := n * 10 + (ORD (CurCh ()) - ORD ('0')) ;
  2819. DropCh (GetCh ())
  2820. END ;
  2821. NumToName (n, nm) ;
  2822. IF NOT Search (nm, n) THEN
  2823. DropC (NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE))
  2824. END ;
  2825. IF NOT MatchDelim (',') THEN
  2826. IF MatchDelim (';') THEN
  2827. RETURN
  2828. END ;
  2829. Err (ENoSemi) ;
  2830. RETURN
  2831. END
  2832. END
  2833. END DefLabelPart ;
  2834. PROCEDURE IfMatchSemi () ;
  2835. BEGIN
  2836. IF NOT MatchDelim (';') THEN
  2837. Err (ENoSemi)
  2838. END
  2839. END IfMatchSemi ;
  2840. PROCEDURE SymEpi () ;
  2841. (* function result: AX := result var *)
  2842. BEGIN
  2843. IF curIsFunc THEN
  2844. IF OK () THEN
  2845. EmLoadVar (TRUE, symtab [resultVar].off, symtab [resultVar].size)
  2846. END
  2847. END
  2848. END SymEpi ;
  2849. PROCEDURE ProcFunc () ;
  2850. (* PROCEDURE name (params) ; body | FUNCTION name (params) : type ; body *)
  2851. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  2852. idx, old : CARDINAL ;
  2853. tok, tok2 : CARDINAL ;
  2854. parmNm : ARRAY [0..MaxName] OF CHAR ;
  2855. cls, size, elem : CARDINAL ;
  2856. isFunc : BOOLEAN ;
  2857. saveNest, saveLoc, saveRes, saveF : CARDINAL ;
  2858. saveLB, savePO : CARDINAL ;
  2859. nestMark : CARDINAL ; (* symTop just inside this procedure *)
  2860. i : CARDINAL ;
  2861. BEGIN
  2862. isFunc := curIsFunc ;
  2863. Skip () ; (* after the PROCEDURE/FUNCTION keyword *)
  2864. IF NOT Alpha (CurCh ()) THEN
  2865. Err (EUnknown) ;
  2866. RETURN
  2867. END ;
  2868. GetWord () ;
  2869. SaveWord (nm) ;
  2870. IF Search (nm, idx) AND (symtab [idx].tag = KProc)
  2871. AND (symtab [idx].fwd) THEN
  2872. old := idx
  2873. ELSIF Search (nm, idx) THEN
  2874. Err (EUnknown) ;
  2875. RETURN
  2876. ELSE
  2877. old := NewSym (nm, KProc, TNone, 0, 0, 0, 0, FALSE)
  2878. END ;
  2879. IF isFunc THEN
  2880. symtab [old].tag := KFunc
  2881. END ;
  2882. saveNest := lexnest ;
  2883. saveLoc := locFree ;
  2884. saveRes := resultVar ;
  2885. saveF := VAL (CARDINAL, ORD (curIsFunc)) ;
  2886. saveLB := locBytes ;
  2887. savePO := parmOff ;
  2888. INC (lexnest) ;
  2889. nestMark := symTop ; (* after the proc's own name, before its params *)
  2890. locFree := 0FFFEH ;
  2891. locBytes := 0 ;
  2892. parmOff := 4 ;
  2893. IF MatchDelim ('(') THEN
  2894. IF CurCh () # ')' THEN
  2895. LOOP
  2896. (* PeekKw, not MatchKey: MatchKey CONSUMES the word it reads, so
  2897. using it to test for VAR would eat the parameter's name. *)
  2898. PeekKw (tok) ;
  2899. IF tok = TkVar THEN
  2900. (* VAR parameter recorded as value in this milestone *)
  2901. DropB (MatchKey (tok))
  2902. END ;
  2903. Skip () ; (* blanks before the parameter name *)
  2904. IF NOT Alpha (CurCh ()) THEN
  2905. Err (EUnknown) ;
  2906. RETURN
  2907. END ;
  2908. GetWord () ;
  2909. SaveWord (parmNm) ;
  2910. DupTest (parmNm) ;
  2911. ExpectDelim (':', ENoSemi) ; (* formal is 'name : type' *)
  2912. ParseType (cls, size, elem) ;
  2913. IF size > 2 THEN
  2914. Err (ENoLib) ;
  2915. RETURN
  2916. END ;
  2917. DropC (NewSym (parmNm, KVar, cls, size, elem, parmOff, 0, TRUE)) ;
  2918. parmOff := parmOff + 2 ;
  2919. IF NOT MatchDelim (',') THEN
  2920. IF MatchDelim (')') THEN
  2921. EXIT
  2922. END ;
  2923. Err (ENoSemi) ;
  2924. EXIT
  2925. END
  2926. END
  2927. ELSE
  2928. DropCh (GetCh ())
  2929. END
  2930. END ;
  2931. (* TP3 lays a parameter list out BACKWARDS from BP+4: the LAST declared
  2932. parameter sits at BP+4 and each earlier one is 2 bytes farther from BP
  2933. (RESUME-TP3.md 3.11 - "le dernier paramètre déclaré est le plus proche
  2934. (BP+4)" - and the Code Generation Internals stack frame, "BP+4: last
  2935. parameter"). This is the other half of pushing the arguments as they
  2936. are parsed: the first argument is pushed first and so ends up farthest
  2937. from BP, which has to be the FIRST declared parameter's slot.
  2938. During the loop the offsets were provisional, in declaration order
  2939. (parmOff starts at 4 and grows by 2 per parameter), so parameter k of n
  2940. sat at 4 + 2*(k-1). The map `off := parmOff + 2 - off' sends that to
  2941. 4 + 2*(n-k) - k = 1 to the far end, k = n to BP+4 - which is the layout
  2942. above, with no offset invented and none lost.
  2943. The range is exactly the parameters: the only symbol created while the
  2944. list is read is the parameter itself (NewSym inside the loop),
  2945. ParseType creates no symbols, the procedure's own name predates
  2946. nestMark, and a function's result variable is created after this point. *)
  2947. i := nestMark ;
  2948. WHILE i < symTop DO
  2949. symtab [i].off := parmOff + 2 - symtab [i].off ;
  2950. INC (i)
  2951. END ;
  2952. IF isFunc THEN
  2953. IF MatchDelim (':') THEN
  2954. ParseType (cls, size, elem)
  2955. ELSE
  2956. cls := TScalar ;
  2957. size := 2 ;
  2958. elem := 2
  2959. END ;
  2960. IF size > 2 THEN
  2961. Err (ENoLib) ;
  2962. RETURN
  2963. END ;
  2964. symtab [old].cls := cls ;
  2965. symtab [old].size := size ;
  2966. resultVar := NewSym (nm, KVar, cls, size, elem, locFree, 0, TRUE) ;
  2967. symtab [old].resvar := resultVar ;
  2968. locFree := (locFree - size) MOD 10000H ;
  2969. locBytes := locBytes + size
  2970. END ;
  2971. IfMatchSemi () ;
  2972. PeekKw (tok2) ;
  2973. IF (tok2 = TkForward) OR (tok2 = TkExternal) THEN
  2974. DropB (MatchKey (tok2)) ;
  2975. symtab [old].fwd := TRUE ;
  2976. symtab [old].defnd := (tok2 = TkExternal) ;
  2977. IfMatchSemi () ;
  2978. HideLocals (nestMark) ; (* a FORWARD's parameters are not the
  2979. caller's to see either *)
  2980. lexnest := saveNest ;
  2981. locFree := saveLoc ;
  2982. resultVar := saveRes ;
  2983. curIsFunc := (saveF # 0) ;
  2984. locBytes := saveLB ;
  2985. parmOff := savePO ;
  2986. RETURN
  2987. END ;
  2988. (* body *)
  2989. symtab [old].goPos := pc ;
  2990. symtab [old].defnd := TRUE ;
  2991. EmPushBp () ;
  2992. EmMovBpSp () ;
  2993. DefPart () ; (* nested declarations; stops at BEGIN *)
  2994. IF locBytes > 0 THEN
  2995. EmSubSp (locBytes)
  2996. END ;
  2997. DropC (EmCall (TU_StackChk)) ;
  2998. Statmnt () ; (* body *)
  2999. IF OK () THEN
  3000. SymEpi () ;
  3001. EmLeave () ;
  3002. EmRet ()
  3003. END ;
  3004. (* patch pending forward calls to this proc *)
  3005. i := 0 ;
  3006. WHILE i < nPend DO
  3007. IF (pend [i].kind = 1) AND (pend [i].who = old) THEN
  3008. SetPatTgt (pend [i].place, symtab [old].goPos) ;
  3009. pend [i].kind := 99
  3010. END ;
  3011. INC (i)
  3012. END ;
  3013. HideLocals (nestMark) ; (* parameters and locals stop here *)
  3014. lexnest := saveNest ;
  3015. locFree := saveLoc ;
  3016. resultVar := saveRes ;
  3017. curIsFunc := (saveF # 0) ;
  3018. locBytes := saveLB ;
  3019. parmOff := savePO
  3020. END ProcFunc ;
  3021. PROCEDURE DefType () ;
  3022. (* 'name' '=' typeDef ; ... until a declaration keyword appears *)
  3023. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  3024. cls, size, elem : CARDINAL ;
  3025. tok : CARDINAL ;
  3026. BEGIN
  3027. LOOP
  3028. PeekKw (tok) ;
  3029. IF tok # TkNone THEN
  3030. RETURN
  3031. END ;
  3032. Skip () ; (* after the TYPE keyword: skip blanks *)
  3033. IF NOT Alpha (CurCh ()) THEN
  3034. Err (EUnknown) ;
  3035. RETURN
  3036. END ;
  3037. GetWord () ;
  3038. SaveWord (nm) ;
  3039. DupTest (nm) ;
  3040. ExpectDelim ('=', ENoSemi) ;
  3041. ParseType (cls, size, elem) ;
  3042. IF NOT OK () THEN
  3043. RETURN
  3044. END ;
  3045. DropC (NewSym (nm, KType, cls, size, elem, 0, 0, FALSE)) ;
  3046. IF NOT MatchDelim (';') THEN
  3047. Err (ENoSemi) ;
  3048. RETURN
  3049. END
  3050. END
  3051. END DefType ;
  3052. PROCEDURE DefPart () ;
  3053. (* LABEL / CONST / TYPE / VAR / OVERLAY / PROC / FUNCTION / BEGIN *)
  3054. VAR tok : CARDINAL ;
  3055. BEGIN
  3056. LOOP
  3057. IF MatchDelim (';') THEN
  3058. (* separator between declarations *)
  3059. ELSE
  3060. PeekKw (tok) ;
  3061. IF tok = TkBegin THEN
  3062. RETURN
  3063. END ;
  3064. IF NOT MatchKey (tok) THEN
  3065. Err (EUnknown) ;
  3066. RETURN
  3067. END ;
  3068. CASE tok OF
  3069. TkLabel : DefLabelPart () ;
  3070. | TkConst : DefConst () ;
  3071. | TkType : DefType () ;
  3072. | TkVar : DefVar () ;
  3073. | TkOverlay :
  3074. LOOP
  3075. Skip () ;
  3076. IF CurCh () = ';' THEN
  3077. DropCh (GetCh ()) ;
  3078. EXIT
  3079. END ;
  3080. IF CurCh () = 0C THEN
  3081. Err (ENoSemi) ;
  3082. EXIT
  3083. END ;
  3084. DropCh (GetCh ())
  3085. END ;
  3086. | TkProcedure : curIsFunc := FALSE ; ProcFunc () ;
  3087. | TkFunction : curIsFunc := TRUE ; ProcFunc () ;
  3088. ELSE
  3089. Err (EUnknown) ;
  3090. RETURN
  3091. END ;
  3092. IF NOT OK () THEN
  3093. RETURN
  3094. END
  3095. END
  3096. END
  3097. END DefPart ;
  3098. (* ---------------------------------------------------------------- *)
  3099. (* driver (TPSRC7 compile) *)
  3100. (* ---------------------------------------------------------------- *)
  3101. PROCEDURE DefBuiltins () ;
  3102. (* The standard procedures. Without these, WRITELN is absent from the
  3103. symbol table, Statmnt's identifier branch fails its Search and every
  3104. program that prints anything dies with EUnknown (41) on the '(' after the
  3105. call name - the single remaining cause of failure in the fixture matrix.
  3106. Tagged KBuiltin (not KProc) because these are not called generically:
  3107. WRITE/WRITELN/READ/READLN need TP3's per-argument type dispatch
  3108. (IoCall), and HALT takes no argument at all. defnd is TRUE because the
  3109. entry point is known - there is no forward reference to patch. *)
  3110. VAR i : CARDINAL ;
  3111. BEGIN
  3112. i := NewSym ("WRITE" , KBuiltin, BI_Write , 0, 0, 0, 0, FALSE) ;
  3113. symtab [i].defnd := TRUE ;
  3114. symtab [i].goPos := TU_WrInt ;
  3115. i := NewSym ("WRITELN", KBuiltin, BI_WriteLn, 0, 0, 0, 0, FALSE) ;
  3116. symtab [i].defnd := TRUE ;
  3117. symtab [i].goPos := TU_WrInt ;
  3118. i := NewSym ("READ" , KBuiltin, BI_Read , 0, 0, 0, 0, FALSE) ;
  3119. symtab [i].defnd := TRUE ;
  3120. symtab [i].goPos := TU_RdInt ;
  3121. i := NewSym ("READLN" , KBuiltin, BI_ReadLn , 0, 0, 0, 0, FALSE) ;
  3122. symtab [i].defnd := TRUE ;
  3123. symtab [i].goPos := TU_RdInt ;
  3124. i := NewSym ("HALT" , KBuiltin, BI_Halt , 0, 0, 0, 0, FALSE) ;
  3125. symtab [i].defnd := TRUE ;
  3126. symtab [i].goPos := TU_Halt
  3127. END DefBuiltins ;
  3128. PROCEDURE Inittur () ;
  3129. (* reset compiler state and define the standard types *)
  3130. VAR i, rt : CARDINAL ;
  3131. BEGIN
  3132. abortFac := FALSE ;
  3133. errNum := 0 ;
  3134. txerrPos := 0 ;
  3135. srcPos := 0 ;
  3136. srcLen := Length () ;
  3137. (* The image layout is
  3138. [JMP rel16][runtime][program header][program code]
  3139. The runtime is copied to the front, which is what the original does:
  3140. TPSRC7 "copyrt" runs REPZ MOVSB with SI=DI=0 and then "MOV pc,#$2D7C".
  3141. Because the runtime sits at (near) offset 0, every address the compiler
  3142. emits is already image-absolute - the data symbols' offsets, the TU_*
  3143. call targets and the rel16 displacements all need no relocation pass.
  3144. (The base shift would in fact cancel in EmCall's arithmetic, since both
  3145. sides of a CALL move together; making the offsets absolute just means
  3146. the linker has nothing to do but copy bytes.)
  3147. The JMP is new, and it is not cosmetic. A DOS .COM is entered at
  3148. CS:0100, i.e. FILE offset 0, and for a long time offset 0 held the
  3149. runtime's first bytes - so a .COM built by this compiler started by
  3150. executing initmem with AX holding whatever the loader left in it. Every
  3151. test up to that point checked bytes and never ran the thing, so it could
  3152. not see this. The jump is the program's entry and the runtime is
  3153. ordinary data to it; keeping the runtime at the front is what preserves
  3154. the no-relocation property, so the jump goes in front of the runtime
  3155. rather than the runtime being moved behind the program.
  3156. dc is put a fixed 4 KiB above the end of the program so that data cannot
  3157. collide with code in a single 64 KiB .COM segment. LIMITATION: a
  3158. program whose code exceeds 4 KiB overruns its own data area. TP3 had
  3159. overlay segments for this; we do not, and the check belongs where the
  3160. limit is documented rather than as a silent truncation. *)
  3161. RT_Build (EntSize) ;
  3162. rt := RT_Size () ;
  3163. IF rt >= MaxCode THEN
  3164. Err (EMemOvf) ; (* cannot happen: rt is 436 *)
  3165. RETURN
  3166. END ;
  3167. (* The entry jump, at image offset 0. See the layout note above: a DOS
  3168. .COM is entered at CS:0100, which is file offset 0, so whatever sits
  3169. at offset 0 is the program's first executed instruction. *)
  3170. (* The jump's three bytes are written out longhand rather than through
  3171. Eword, because Eword writes at pc and advances it, and pc is stale at
  3172. this point -- the operand landed wherever the last compile left pc. *)
  3173. cbuf [0] := 0E9H ; (* JMP rel16 *)
  3174. entRel := 1 ;
  3175. cbuf [entRel] := 0 ;
  3176. cbuf [entRel + 1] := 0 ;
  3177. i := EntSize ;
  3178. WHILE i - EntSize < rt DO
  3179. cbuf [i] := RT_Byte (i - EntSize) ;
  3180. INC (i)
  3181. END ;
  3182. pc := rt + EntSize ;
  3183. rtSz := rt + EntSize ;
  3184. dataBase := rtSz + 1000H ;
  3185. dc := dataBase ;
  3186. strTop := 0 ;
  3187. strCnt := 0 ;
  3188. rdStrX := 0 ;
  3189. varspc := 0 ;
  3190. symTop := 0 ;
  3191. nPatch := 0 ;
  3192. nPend := 0 ;
  3193. exitCnt := 0 ;
  3194. brkN := 0 ;
  3195. caseN := 0 ;
  3196. lexnest := 0 ;
  3197. curIsFunc := FALSE ;
  3198. resultVar := 0 ;
  3199. locFree := 0FFFEH ;
  3200. locBytes := 0 ;
  3201. parmOff := 4 ;
  3202. dirs.rng := TRUE ;
  3203. dirs.chk := TRUE ;
  3204. InitKeys () ;
  3205. DropC (NewSym ("INTEGER", KType, TScalar, 2, 2, 0, 0, FALSE)) ;
  3206. DropC (NewSym ("BYTE" , KType, TScalar, 1, 1, 0, 0, FALSE)) ;
  3207. DropC (NewSym ("CHAR" , KType, TChar, 1, 1, 0, 0, FALSE)) ;
  3208. DropC (NewSym ("BOOLEAN", KType, TBool , 1, 1, 0, 0, FALSE)) ;
  3209. DropC (NewSym ("REAL" , KType, TReal , 6, 6, 0, 0, FALSE)) ;
  3210. DropC (NewSym ("STRING" , KType, TString, 256, 1, 0, 0, FALSE)) ;
  3211. DropC (NewSym ("TRUE" , KConst, TBool, 1, 1, 0, 1, FALSE)) ;
  3212. DropC (NewSym ("FALSE" , KConst, TBool, 1, 1, 0, 0, FALSE)) ;
  3213. DefBuiltins () ;
  3214. tmpA := NewSym ("@@T1", KVar, TScalar, 2, 2, dc, 0, FALSE) ;
  3215. dc := dc + 2 ;
  3216. tmpB := NewSym ("@@T2", KVar, TScalar, 2, 2, dc, 0, FALSE) ;
  3217. dc := dc + 2 ;
  3218. (* Runtime entry offsets, derived from the blob rather than assumed. This
  3219. has to happen after RT_Build, since RT_Entry only knows where the code
  3220. landed once the blob is assembled.
  3221. RT_Entry returns an IMAGE-ABSOLUTE address, already biased by the base
  3222. RT_Build was given, so nothing here has to know where the runtime
  3223. landed. It used to return a blob-relative offset, and the bias was
  3224. applied here instead - three bytes' worth, for the entry jump. With
  3225. that line missing, every CALL landed three bytes short, in the middle of
  3226. a neighbouring runtime entry, and a CALL into the middle of wrtin's
  3227. `INT 21h' behaves perfectly plausibly: the program runs, prints nothing
  3228. and hangs. Only running it finds that. *)
  3229. IF (RT_Entry (13) = 0) OR (RT_Entry (11) = 0) OR (RT_Entry (3) = 0) THEN
  3230. (* RT_Entry returns 0 for an unknown selector. initmem sits at 0
  3231. legitimately, so it cannot appear in this test - but wrtinl, rdln
  3232. and wrint never can, so catching them is enough to catch a runtime
  3233. that failed to build or a selector that went stale. This test is on
  3234. 0 is not a usable address here, since RT_Entry returns an
  3235. image-absolute address and the base is EntSize. *)
  3236. Err (EMemOvf)
  3237. END ;
  3238. TU_InitMem := RT_Entry (0) ;
  3239. TU_ProgEnd := RT_Entry (1) ;
  3240. TU_StackChk := RT_Entry (2) ;
  3241. TU_WrInt := RT_Entry (3) ;
  3242. TU_WrChar := RT_Entry (4) ;
  3243. TU_WrBool := RT_Entry (5) ;
  3244. TU_WrReal := RT_Entry (6) ;
  3245. TU_WrLn := RT_Entry (7) ;
  3246. TU_RdInt := RT_Entry (8) ;
  3247. TU_RdChar := RT_Entry (9) ;
  3248. TU_RdBool := RT_Entry (10) ;
  3249. TU_RdLn := RT_Entry (11) ;
  3250. TU_Halt := RT_Entry (12) ;
  3251. TU_WrInl := RT_Entry (13) ; (* inline string literal *)
  3252. END Inittur ;
  3253. PROCEDURE HeadWord (VAR slot : CARDINAL) ;
  3254. BEGIN
  3255. slot := pc ;
  3256. Eword (0)
  3257. END HeadWord ;
  3258. PROCEDURE Compile (VAR errNo, errPos : CARDINAL) : BOOLEAN ;
  3259. VAR tok : CARDINAL ;
  3260. overProc : CARDINAL ;
  3261. hasProc : BOOLEAN ;
  3262. BEGIN
  3263. Inittur () ;
  3264. IF OK () THEN
  3265. (* program prologue: header words, CALL initmem, MOV BP,SP *)
  3266. HeadWord (hdrFlag) ;
  3267. HeadWord (hdrCS) ;
  3268. HeadWord (hdrDS) ;
  3269. HeadWord (hdrHeap) ;
  3270. HeadWord (hdrMax) ;
  3271. Eword (16) ; (* max open files *)
  3272. Eword (0) ; (* input buffer word *)
  3273. Eword (0) ; (* output buffer word *)
  3274. (* Here, and not one line earlier, is the first instruction of the
  3275. program: everything above is the header, which is DATA. The entry
  3276. jump has to land exactly here. Recorded rather than assumed, so that
  3277. a header that grows a word moves the target with it. *)
  3278. prologAt := pc ;
  3279. (* TU_InitMem takes the header offset in AX, not on the stack, so the
  3280. AX load has to precede the call. Previously the prologue called
  3281. offset 8 - which in this layout is the hdrMax word - and that was
  3282. coherent only because the runtime was not there. Now it is the
  3283. real header. *)
  3284. EmMovAxi ((rtSz + LoadBias) MOD 10000H) ;
  3285. DropC (EmCall (TU_InitMem)) ;
  3286. EmMovBpSp () ;
  3287. IF MatchKey (tok) AND (tok = TkProgram) THEN
  3288. (* MatchKey stops right after "PROGRAM", so the optional program
  3289. name normally follows blanks. Skip them before testing for the
  3290. name: otherwise Alpha sees the blank, the name is never consumed
  3291. and IfMatchSemi reports ENoSemi at the name. *)
  3292. Skip () ;
  3293. IF Alpha (CurCh ()) THEN
  3294. GetWord ()
  3295. END ;
  3296. IF MatchDelim ('(') THEN
  3297. WHILE NOT MatchDelim (')') DO
  3298. IF Alpha (CurCh ()) THEN
  3299. GetWord ()
  3300. END ;
  3301. IF CurCh () = ',' THEN
  3302. DropCh (GetCh ())
  3303. END
  3304. END
  3305. END ;
  3306. IfMatchSemi ()
  3307. END ;
  3308. IF OK () THEN
  3309. (* Jump over the procedure bodies, if there are any. DefPart
  3310. compiles them HERE, between the prologue and the main statement
  3311. part, and there was no jump - so a program with a procedure fell
  3312. off the end of the prologue into the first procedure. See
  3313. DeclaresProc for why the condition is asked before DefPart runs
  3314. and not after. *)
  3315. hasProc := DeclaresProc () ;
  3316. IF hasProc THEN
  3317. overProc := EmJmpNear (0)
  3318. END ;
  3319. DefPart () ;
  3320. IF OK () THEN
  3321. (* A separate flag, NOT `overProc # 0`. EmJmpNear returns a
  3322. patch SLOT, and slot 0 is a perfectly ordinary slot - the
  3323. first forward jump in a program is slot 0. So a zero test
  3324. cannot tell "no forward jump" from "forward jump in slot 0",
  3325. it just skips the first patch, and the jump keeps its
  3326. placeholder target of 0. The program then jumped to image
  3327. offset 0, i.e. back to the entry jump, and ran the runtime
  3328. and the whole program again, forever. The slot is only
  3329. valid together with a boolean saying a slot was taken. *)
  3330. IF hasProc THEN
  3331. SetPatTgt (overProc, pc)
  3332. END ;
  3333. IF MatchKey (tok) AND (tok = TkBegin) THEN
  3334. Compound () ;
  3335. IF OK () THEN
  3336. EmXorAxAx () ;
  3337. DropC (EmCall (TU_ProgEnd)) ;
  3338. ResolvePatches () ;
  3339. (* Program-only sizes. The runtime is not part of the
  3340. program's code, and the fixture table has always meant
  3341. "the program's own code", so subtract it here rather
  3342. than making every expectation in expected.tsv wrong. *)
  3343. codeSz := pc - rtSz ;
  3344. dataSz := dc - dataBase ;
  3345. (* Header words. The layout is ours (the original's is
  3346. bigger and serves a real overlay loader), but
  3347. Runtime.EmitInitMem reads +4 and +8, so hdrDS and
  3348. hdrHeap must be the data base and the data end. *)
  3349. (* The entry jump's displacement. A .COM is entered at
  3350. CS:0100 = file offset 0, so the jump is the only thing
  3351. that decides where execution starts, and it has to land
  3352. on the START of the program code - the prologue, which is
  3353. at rtSz - not on pc, which is the END of it. (Patching
  3354. pc - EntSize, i.e. the end, lands one byte past the last
  3355. instruction, in the zero-filled code/data gap, where the
  3356. CPU slides through `ADD [BX+SI],AL' until it faults.)
  3357. The displacement is measured from the END of the jump,
  3358. and both addresses are image-absolute, so the load
  3359. segment cancels. *)
  3360. (* The entry jump's displacement. A .COM is entered at
  3361. CS:0100 = file offset 0, so this jump is the only thing
  3362. that decides where execution starts.
  3363. The target is prologAt - where the prologue ACTUALLY
  3364. began, recorded before the header words were emitted,
  3365. and the header is 16 bytes long, so this is rtSz + 16 and
  3366. NOT rtSz. Landing on rtSz lands on the HEADER, which is
  3367. data, and the CPU then decodes sixteen bytes of it as
  3368. instructions. That failure is spectacularly
  3369. non-deterministic across programs: 01 00 is
  3370. `ADD [BX+SI],AX' and is harmless, so writeln('hi') ran
  3371. fine by sliding through the header into the prologue,
  3372. while t07's hdrHeap word 90 12 decodes as a LOCK-prefixed
  3373. ADD whose displacement crosses a page and faults, and the
  3374. program hung with no output at all. Both looked like
  3375. "the jump is in the right area". Recording the position
  3376. rather than assuming it means a future header that grows
  3377. a word cannot silently reintroduce this. *)
  3378. PatchWord (entRel, (prologAt - EntSize) MOD 10000H) ;
  3379. PatchWord (hdrFlag, 1) ;
  3380. (* Every OFFSET field in the header is a segment offset,
  3381. i.e. an image offset plus LoadBias - one convention for
  3382. the whole structure, so that nobody has to remember
  3383. which of these five words is numbered which way.
  3384. hdrFlag 1 set, so a loader can recognise the header
  3385. hdrCS end of the generated code
  3386. hdrDS first byte of the data area <- read by initmem
  3387. hdrHeap one past the last <- read by initmem
  3388. hdrMax 0 (no overlay loader yet)
  3389. hdrDS and hdrHeap are the two that are CONSUMED, and
  3390. omitting the bias there is a silent no-op: initmem would
  3391. clear a range starting 0100h below the data, off the
  3392. front of the image, and never reach the globals at the
  3393. end. Nothing crashes, and the globals keep whatever the
  3394. loader left in them. *)
  3395. PatchWord (hdrCS, pc + LoadBias) ;
  3396. PatchWord (hdrDS, dataBase + LoadBias) ;
  3397. PatchWord (hdrHeap, dc + LoadBias) ;
  3398. PatchWord (hdrMax, 0)
  3399. END
  3400. ELSE
  3401. Err (EUnknown)
  3402. END
  3403. END
  3404. END
  3405. END ;
  3406. IF NOT MatchDelim ('.') THEN
  3407. Err (EPointExp)
  3408. END ;
  3409. IF abortFac THEN
  3410. errNo := errNum ; (* was "errNo := errNo": a self-assignment,
  3411. because the formal shadowed the module
  3412. variable, so the error code always
  3413. reached the caller as 0 *)
  3414. errPos := txerrPos ;
  3415. RETURN FALSE
  3416. END ;
  3417. errNo := 0 ;
  3418. errPos := 0 ;
  3419. RETURN TRUE
  3420. END Compile ;
  3421. PROCEDURE CodeBytes () : CARDINAL ;
  3422. BEGIN
  3423. RETURN codeSz
  3424. END CodeBytes ;
  3425. PROCEDURE DataBytes () : CARDINAL ;
  3426. BEGIN
  3427. RETURN dataSz
  3428. END DataBytes ;
  3429. PROCEDURE ImageBytes () : CARDINAL ;
  3430. (* Total linked image size: rtSz (the runtime) + CodeBytes (the program).
  3431. The program is NOT padded out to the data base here - the linker does
  3432. that, and only it knows the .COM's final size. *)
  3433. BEGIN
  3434. RETURN rtSz + codeSz
  3435. END ImageBytes ;
  3436. PROCEDURE DataBase () : CARDINAL ;
  3437. (* image-absolute offset at which the data area begins (rtSz + 1000H). The
  3438. linker must place the program's data here and zero-fill from the end of
  3439. the code up to it. *)
  3440. BEGIN
  3441. RETURN dataBase
  3442. END DataBase ;
  3443. PROCEDURE ImageByteAt (i : CARDINAL) : BYTE ;
  3444. (* i-th byte of the WHOLE image, runtime included, so a test can check the
  3445. real thing a .COM would contain. Returns 0 past the end. *)
  3446. BEGIN
  3447. IF i >= rtSz + codeSz THEN
  3448. RETURN 0
  3449. END ;
  3450. RETURN cbuf [i]
  3451. END ImageByteAt ;
  3452. PROCEDURE CodeByteAt (i : CARDINAL) : BYTE ;
  3453. (* i-th byte of the emitted image, for test harnesses that need to check
  3454. the generated 8086 code rather than just its size. Returns 0 past the
  3455. end of the image. *)
  3456. BEGIN
  3457. IF i >= codeSz THEN
  3458. RETURN 0
  3459. END ;
  3460. RETURN cbuf [i]
  3461. END CodeByteAt ;
  3462. END Compiler.