Compiler.mod 121 KB

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