Compiler.mod 292 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911
  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 TextLimit, Length, CharAt, InsertStr, DeleteFromTo ;
  40. FROM SYSTEM IMPORT BYTE, ADR ;
  41. FROM Posix IMPORT open, read, close ;
  42. FROM Runtime IMPORT RT_Build, RT_Size, RT_Byte, RT_Entry, LoadBias ;
  43. (* The runtime is copied to the front of the code buffer and pc/dc start past
  44. it, so every emitted address is image-absolute and no relocation pass is
  45. needed. See Inittur.
  46. `LoadBias' comes from Runtime because it is the same constant on both
  47. sides of the image: Runtime adds it to every data address it bakes into its
  48. own code (FixUp, kind 2), and this module adds it to every ABSOLUTE address
  49. it bakes into the program's. It is deliberately ONE constant in ONE place
  50. rather than 0100h written out at six sites, because getting it wrong at one
  51. site is invisible - see the note on LoadBias in Runtime.mod. Relative
  52. encodings (the entry JMP, every CALL and JMP) must NOT get it: both
  53. operands shift together and the +0100h cancels. *)
  54. (* ---------------------------------------------------------------- *)
  55. (* constants *)
  56. (* ---------------------------------------------------------------- *)
  57. CONST
  58. MaxLine = 128 ;
  59. MaxName = 31 ;
  60. (* Size of the entry jump at image offset 0: E9 lo hi. The jump's
  61. displacement is relative to the END of the jump, so every offset in the
  62. image is EntSize further along than it was before the jump existed. *)
  63. EntSize = 3 ;
  64. MaxCode = 24000 ;
  65. (* Program layout, image offsets. The original puts the runtime and the
  66. program's code in CS and the globals in a SEPARATE DS segment, so the
  67. .COM contains no data and the code is not limited by a data gap. We
  68. have one flat segment (our boot loader), so the same property is
  69. obtained by putting the globals at a fixed high image offset: the .COM
  70. is code-only, hdrDS points at DataStart, and initmem zeroes it. The
  71. code must end below CodeMax, where the boot harness puts the stdin
  72. descriptor (memory 7000h). *)
  73. DataStart = 8000H ; (* image offset of the first global (memory 8100h) *)
  74. CodeMax = 6F00H ; (* code must end below memory 7000h *)
  75. MaxSym = 3000 ;
  76. MaxPatch = 2000 ;
  77. MaxPend = 400 ;
  78. MaxFields = 2000 ; (* record fields, in a table of their own: a
  79. field is scoped to its record, not global *)
  80. MaxArrs = 1000 ; (* array SHAPES, likewise: a symbol points at
  81. one, and 2-D indexing needs both ranges *)
  82. FileVarSize = 132 ; (* a file variable is our control block: the DOS
  83. handle (word), a mode word (0 closed, 1 read,
  84. 2 write), then a 128-byte NUL-terminated
  85. filename. TP3 keeps its own name buffer and
  86. a 128-byte data buffer here; we keep the name
  87. and do unbuffered INT 21h I/O, with the data
  88. buffer supplied by the caller. *)
  89. (* type codes (TP3 vartp) *)
  90. TNone = 0 ; TArray = 1 ; TRecord = 2 ; TSet = 3 ;
  91. TPtr = 4 ; TFile = 5 ; TText = 6 ; TUntyp = 7 ;
  92. TString = 8 ; TReal = 9 ; TScalar = 10 ; TBool = 11 ;
  93. (* CHAR needs a class of its own. It used to be registered as TScalar,
  94. which made a CHAR variable indistinguishable from an INTEGER one: the
  95. class is all IoCall has to dispatch on, so `write(c)` called wrint (the
  96. value 65 became the *address* 65 and it printed whatever lived at 0x41)
  97. and `readln(c)` called rdint (which stores a 16-bit result, so it wrote
  98. two bytes into a one-byte variable). TP3 TPSRC8 prdtyped/pwriteln
  99. dispatches on the type identifier for exactly this reason. BYTE stays
  100. TScalar: a BYTE is written as an integer, as in TP3 - but a READ of one
  101. is dispatched on the variable's size instead (TPSRC8 prdnum
  102. `CMP varsize,#$01 / JA -> rdint, else rdbyte'), so the one thing class
  103. alone cannot tell apart is told apart where the size is in hand. *)
  104. TChar = 12 ;
  105. TEnum = 13 ; (* an enumeration type: ordinal, size 1 (TP3 uses 1
  106. byte below 256 values), bounds in SymEntry.lo/hi *)
  107. (* symbol kinds *)
  108. KLabel = 100H ; KConst = 200H ; KType = 300H ;
  109. KVar = 400H ; KProc = 500H ; KFunc = 600H ;
  110. KBuiltin = 700H ; (* standard procedure, see BI_* below *)
  111. (* which standard procedure a KBuiltin symbol denotes *)
  112. BI_Write = 0 ; BI_WriteLn = 1 ; BI_Read = 2 ;
  113. BI_ReadLn = 3 ; BI_Halt = 4 ; BI_Length = 5 ;
  114. (* CRT identifiers. There is no unit system, so these are builtins. They
  115. are statements (except Length), dispatched in IoCall; each one that is
  116. not implemented yet is a loud ENoLib, never a silent no-op. *)
  117. BI_ClrScr = 6 ; BI_GotoXY = 7 ; BI_LowVideo = 8 ;
  118. BI_NormVideo = 9 ; BI_ClrEol = 10 ; BI_Delay = 11 ;
  119. BI_KeyPressed = 12 ; BI_UpCase = 13 ; BI_Succ = 14 ; BI_Pred = 15 ;
  120. (* File operations. TP3 has these as standard procedures/functions; our
  121. control block is Compiler.FileVarSize, so Assign stores the name for
  122. the later Reset/Rewrite. IOResult is an expression; EndOfFile is the
  123. next file increment. *)
  124. BI_Assign = 16 ; BI_Reset = 17 ; BI_Rewrite = 18 ;
  125. BI_Close = 19 ; BI_IoResult = 20 ; BI_Chr = 21 ; BI_Str = 22 ;
  126. BI_New = 23 ; BI_Dispose = 24 ; BI_Eof = 25 ; BI_Val = 26 ;
  127. (* the sub-codes EmCrtSeq takes *)
  128. CrtClr = 0 ; CrtDl = 1 ; CrtDh = 2 ; CrtCaret = 3 ; CrtEol = 4 ;
  129. CrtLow = 5 ; CrtNorm = 6 ; CrtDelay = 7 ; CrtKey = 8 ;
  130. (* keyword tokens *)
  131. TkNone = 0 ; TkProgram = 1 ; TkBegin = 2 ; TkEnd = 3 ;
  132. TkIf = 4 ; TkThen = 5 ; TkElse = 6 ; TkWhile = 7 ;
  133. TkDo = 8 ; TkRepeat = 9 ; TkUntil = 10 ; TkFor = 11 ;
  134. TkTo = 12 ; TkDownto = 13 ; TkCase = 14 ; TkOf = 15 ;
  135. TkGoto = 16 ; TkExit = 17 ; TkWith = 18 ; TkVar = 19 ;
  136. TkConst= 20 ; TkType = 21 ; TkLabel = 22 ; TkProcedure = 23 ;
  137. TkFunction = 24 ; TkNil = 25 ; TkAnd = 26 ; TkOr = 27 ;
  138. TkNot = 28 ; TkDiv = 29 ; TkMod = 30 ; TkIn = 31 ;
  139. TkFile = 32 ; TkText = 33 ; TkRecord = 34 ; TkArray = 35 ;
  140. TkSet = 36 ; TkPacked = 37 ; TkForward = 38 ; TkExternal = 39 ;
  141. TkAbsolute = 40 ; TkOverlay = 41 ; TkString = 42 ;
  142. (* Codes for the SYMBOL operators, as BinOpEmit numbers them. The word
  143. operators carry their own Tk* token and need no code here.
  144. These are named, not bare numbers, because every precedence level's
  145. parser picks its own code out of the same set and BinOpEmit cannot see
  146. which level called it. ParseAdd chose 1 for '+' and ParseMul chose 1
  147. for '*', so every multiplication dispatched to EmAddAxCx and a * b
  148. compiled to a + b. Only the constant-folding path was right, which is
  149. why n * n with n a CONST was correct and a * b with a a variable was
  150. not. *)
  151. OpAdd = 1 ; OpSub = 2 ; OpMul = 3 ;
  152. (* TP3 error numbers *)
  153. ENoSemi = 1 ; EPointExp = 10 ; ESimpType = 30 ;
  154. EUnknown = 41 ; EConstRange = 45 ; EMemOvf = 98 ;
  155. ECompOvf = 99 ; ENoLib = 102 ; ETypeErr = 56 ;
  156. EIoHere = 66 ; (* I/O not allowed here - TPSRC8 prderrtp *)
  157. EFileNF = 90 ; (* File not found - TPSRC10 cdincl, errb *)
  158. EInclNest = 96 ; (* Illegal nesting of include files - TPSRC10 cdincl *)
  159. AposC = 39 ; (* ORD ("'") - avoids quote-in-quote *)
  160. (* ---------------------------------------------------------------- *)
  161. (* types *)
  162. (* ---------------------------------------------------------------- *)
  163. TYPE
  164. SymEntry =
  165. RECORD
  166. name : ARRAY [0..MaxName] OF CHAR ;
  167. tag : CARDINAL ;
  168. cls : CARDINAL ;
  169. size : CARDINAL ;
  170. elem : CARDINAL ;
  171. off : CARDINAL ;
  172. lval : LONGINT ;
  173. lo, hi : LONGINT ; (* an ORDINAL type's bounds: an enumeration
  174. (0..n-1) or a subrange. Needed so
  175. `set of T' can size its base. *)
  176. fldFirst, fldLast : CARDINAL ; (* a RECORD type's field range in the
  177. field table (0,0 when not a record) *)
  178. arrIdx : CARDINAL ; (* an ARRAY type's shape in ArrRec (0 = no) *)
  179. parmFirst, parmCount : CARDINAL ; (* a PROCEDURE/FUNCTION's parameters:
  180. symtab indices, in declaration order *)
  181. isKbd : BOOLEAN ; (* the predefined KBD file: reads are console *)
  182. isVar : BOOLEAN ; (* a VAR parameter: the slot holds an address *)
  183. level : CARDINAL ;
  184. local : BOOLEAN ;
  185. resvar : CARDINAL ;
  186. goPos : CARDINAL ;
  187. defnd : BOOLEAN ;
  188. fwd : BOOLEAN ;
  189. END ;
  190. PatchRec =
  191. RECORD
  192. place : CARDINAL ;
  193. target : CARDINAL ;
  194. filled : BOOLEAN ;
  195. END ;
  196. PendRec =
  197. RECORD
  198. kind : CARDINAL ; (* 0 goto, 1 call *)
  199. who : CARDINAL ;
  200. place : CARDINAL ; (* patch slot index *)
  201. END ;
  202. ERes =
  203. RECORD
  204. cls : CARDINAL ;
  205. kind : CARDINAL ; (* 0 const, 1 var, 2 value in AX,
  206. 3 string literal (cls = TString),
  207. 4 value on the stack (SaveLeft),
  208. 5 string element, address in DI,
  209. 7 set value (cls = TSet) *)
  210. imm : LONGINT ;
  211. idx : CARDINAL ;
  212. boff : CARDINAL ; (* constant fold-in for subscripts *)
  213. chr : BOOLEAN ; (* single-quoted literal, e.g. 'a' *)
  214. strx : CARDINAL ; (* string literal: index into strPool *)
  215. setoff : CARDINAL ; (* SET value: image offset of the bytes *)
  216. setbase : CARDINAL ; (* ... the first byte's first element,
  217. a multiple of 8 (TP3's hybrid origin) *)
  218. setsize : CARDINAL ; (* ... and how many bytes there are *)
  219. sz : CARDINAL ; (* width of a kind-1 value: an element or
  220. field's size, not its variable's *)
  221. END ;
  222. DirRec = RECORD rng, chk : BOOLEAN END ;
  223. FldRec =
  224. RECORD
  225. name : ARRAY [0..MaxName] OF CHAR ;
  226. cls : CARDINAL ;
  227. size : CARDINAL ;
  228. elem : CARDINAL ;
  229. off : CARDINAL ; (* byte offset within the record *)
  230. lo, hi : LONGINT ; (* bounds, for a subrange/enum field *)
  231. END ;
  232. ArrRec =
  233. RECORD
  234. n : CARDINAL ; (* number of ranges (1 for array[1..5]) *)
  235. lo, hi : ARRAY [0..3] OF LONGINT ;
  236. elem : CARDINAL ; (* element size in bytes *)
  237. ecls : CARDINAL ; (* element CLASS, for a subscripted value *)
  238. END ;
  239. (* ---------------------------------------------------------------- *)
  240. (* state *)
  241. (* ---------------------------------------------------------------- *)
  242. VAR
  243. srcPos, srcLen : CARDINAL ;
  244. (* Include state. srcName is the path the buffer was loaded from, set by
  245. SetSource; only its directory is used, because a {$I file} directive
  246. names the file relative to the INCLUDING file. Includes are expanded by
  247. ExpandIncludes BEFORE parsing, so inclEnd is the end of the include
  248. region currently being scanned and inclActive says one is open - a
  249. nested {$I} inside that region is error 96, since TP3 permits only one
  250. level. incBuf holds one whole include file while it is spliced. *)
  251. srcName : ARRAY [0..255] OF CHAR ; inclName : ARRAY [0..127] OF CHAR ;
  252. inclPath : ARRAY [0..383] OF CHAR ;
  253. inclEnd : CARDINAL ;
  254. inclActive : BOOLEAN ;
  255. incBuf : ARRAY [0..TextLimit] OF CHAR ;
  256. enumLo, enumHi : LONGINT ; (* bounds of the enumeration ParseType just read,
  257. handed to DefType which owns the type NAME *)
  258. fields : ARRAY [0..MaxFields - 1] OF FldRec ;
  259. fldTop : CARDINAL ; (* next free field-table slot *)
  260. recFldFirst, recFldLast : CARDINAL ; (* field range ParseType just built *)
  261. arrs : ARRAY [0..MaxArrs - 1] OF ArrRec ;
  262. arrTop : CARDINAL ; (* next free array-shape slot *)
  263. newArrIdx : CARDINAL ; (* ArrRec index+1 for the type just parsed *)
  264. constBlob : ARRAY [0..511] OF CHAR ; (* bytes of a set/array typed constant
  265. while it is being built *)
  266. wrd : ARRAY [0..MaxName] OF CHAR ;
  267. (* String-literal pool.
  268. TP3 does not put a literal in the data segment at all: it emits the
  269. literal *inline in the code stream* as <length byte><characters>, and
  270. the runtime entry "wrtinl" reads the length from the return address and
  271. returns to just past the last character (TPSRC4 xwrtinl, TPSRC10
  272. estring). So nothing here ends up in the image as data - the pool only
  273. has to survive from the moment the literal is scanned until IoCall
  274. decides to emit it, because by then the parser has moved on. *)
  275. strPool : ARRAY [0..4095] OF CHAR ;
  276. strOff : ARRAY [0..255] OF CARDINAL ;
  277. strLen : ARRAY [0..255] OF CARDINAL ;
  278. strTop : CARDINAL ; (* next free byte in strPool *)
  279. strCnt : CARDINAL ; (* literals collected so far *)
  280. rdStrX : CARDINAL ; (* pool index of the literal RdConst just read *)
  281. symtab : ARRAY [0..MaxSym - 1] OF SymEntry ;
  282. symTop : CARDINAL ;
  283. patches : ARRAY [0..MaxPatch - 1] OF PatchRec ;
  284. nPatch : CARDINAL ;
  285. pend : ARRAY [0..MaxPend - 1] OF PendRec ;
  286. nPend : CARDINAL ;
  287. exitPatch : ARRAY [0..63] OF CARDINAL ;
  288. exitCnt : CARDINAL ;
  289. brkSave : ARRAY [0..15] OF CARDINAL ;
  290. loopTy : ARRAY [0..15] OF CARDINAL ; (* 1 while/repeat, 2 for *)
  291. brkN : CARDINAL ;
  292. caseJmp : ARRAY [0..63] OF CARDINAL ;
  293. caseN : CARDINAL ;
  294. (* the active WITH scopes, innermost last. A DI-based base (an array
  295. element addressed at run time) is parked in a two-byte data slot: the
  296. body may clobber DI, so each field access reloads it. *)
  297. withN : CARDINAL ;
  298. withIsDI : ARRAY [0..3] OF BOOLEAN ;
  299. withIdx : ARRAY [0..3] OF CARDINAL ;
  300. withBoff : ARRAY [0..3] OF CARDINAL ;
  301. withSlot : ARRAY [0..3] OF CARDINAL ;
  302. withFF : ARRAY [0..3] OF CARDINAL ;
  303. withFL : ARRAY [0..3] OF CARDINAL ;
  304. lblLo, lblHi : ARRAY [0..31] OF LONGINT ; (* one arm's label list *)
  305. lblJmp : ARRAY [0..63] OF CARDINAL ; (* its body jumps: 1 or 2 per label *)
  306. pc, dc : CARDINAL ;
  307. varspc : CARDINAL ;
  308. cbuf : ARRAY [0..MaxCode - 1] OF BYTE ;
  309. codeSz, dataSz : CARDINAL ;
  310. (* Image layout, all image-absolute. rtSz is where the runtime ends and
  311. the program header begins; dataBase is the fixed high global area
  312. (DataStart = 8000H image, 8100H memory), which is NOT part of the .COM
  313. image: initmem zeroes it at startup, as the original's separate DS
  314. segment is zeroed. codeSz and dataSz are PROGRAM sizes - the runtime is
  315. excluded. *)
  316. rtSz, dataBase : CARDINAL ;
  317. (* TRUE once Compile has computed rtSz/dataBase, i.e. once the code window
  318. has been measured. Ebyte refuses to write past CodeMax/MaxCode and
  319. needs to know the window is real before it can: imgReady is FALSE until
  320. then, and a zero CodeMax would abort on the very first byte. *)
  321. imgReady : BOOLEAN ;
  322. (* Image offset of the entry jump's rel16 operand, patched at the end of
  323. Compile. The jump is at image offset 0, so its displacement is simply
  324. the program code's end - 3. *)
  325. entRel, prologAt : CARDINAL ;
  326. (* Runtime entry offsets as IMAGE-ABSOLUTE addresses, which is what
  327. EmCall and EmJmp want. They are derived from Runtime.RT_Entry in
  328. Inittur (after RT_Build, since RT_Entry only knows where the code
  329. landed once the blob is assembled) rather than written down, so a moved
  330. entry cannot leave the compiler calling the old address. Not a CONST
  331. block because RT_Entry is a function.
  332. Standard-procedure entries: TP3 does NOT pass a descriptor -
  333. TPSRC8 pwriteln/pwrloop inspects each argument's class in CL and emits
  334. a *different* call per type, so the type is fixed at compile time and
  335. the runtime needs only the value. Mirrored here. *)
  336. TU_InitMem : CARDINAL ;
  337. TU_ProgEnd : CARDINAL ;
  338. TU_StackChk : CARDINAL ;
  339. TU_WrInt : CARDINAL ; TU_WrIntW : CARDINAL ;
  340. TU_WrChar : CARDINAL ; TU_WrBool : CARDINAL ;
  341. TU_WrReal : CARDINAL ; TU_RealWrite : CARDINAL ; TU_RealFmt : CARDINAL ;
  342. TU_WrLn : CARDINAL ;
  343. TU_RdInt : CARDINAL ; TU_RdChar : CARDINAL ; TU_RdBool : CARDINAL ;
  344. TU_RdLn : CARDINAL ; TU_Halt : CARDINAL ;
  345. TU_RdByte : CARDINAL ; (* one-byte variable reads; see IoCall *)
  346. TU_WrInl : CARDINAL ; (* inline string literal; NO stack argument *)
  347. TU_WrStr : CARDINAL ; (* string variable; address is the stack argument *)
  348. TU_RdStr : CARDINAL ; (* readln(s)/read(s); address on the stack, limit
  349. in CX *)
  350. TU_StrStore : CARDINAL ; (* s := t; dest and source addresses on the stack,
  351. limit in CX *)
  352. TU_SetOp : CARDINAL ; (* runtime set operations, entries 31..33 *)
  353. TU_SetCmp : CARDINAL ;
  354. TU_SetCopy : CARDINAL ;
  355. TU_FileAssign : CARDINAL ; (* file control block operations, entries 34..38 *)
  356. TU_FileReset : CARDINAL ;
  357. TU_FileRewrite : CARDINAL ;
  358. TU_FileClose : CARDINAL ;
  359. TU_IoResult : CARDINAL ;
  360. TU_IoStore : CARDINAL ; (* save AX as the last I/O status *)
  361. TU_HeapNew : CARDINAL ; (* bump allocation for New *)
  362. TU_FileEof : CARDINAL ; (* text-file end of file *)
  363. TU_FileReadln : CARDINAL ; (* text-file line read *)
  364. TU_IntFmt : CARDINAL ; (* integer -> counted string *)
  365. TU_ValInt : CARDINAL ; (* counted string -> integer *)
  366. TU_CmpStr : CARDINAL ; (* string comparison; two addresses on the stack,
  367. signed -1/0/+1 in AX *)
  368. TU_StrCat : CARDINAL ; (* string concatenation; dest and source addresses
  369. on the stack, limit in CX; exit code 16 if the
  370. result would be too long *)
  371. abortFac : BOOLEAN ;
  372. errNum : CARDINAL ; (* NOT "errNo": Compile's formal of that
  373. name would shadow it, and the caller's
  374. errNo would never be filled in *)
  375. txerrPos : CARDINAL ;
  376. lexnest : CARDINAL ;
  377. curIsFunc : BOOLEAN ;
  378. resultVar : CARDINAL ;
  379. resultIsReal : BOOLEAN ; (* the CURRENT function returns TReal *)
  380. resultPtrOff : CARDINAL ; (* hidden local: caller's 6-byte result dest *)
  381. callScratch : CARDINAL ; (* per-site real-result scratch symbol *)
  382. locFree : CARDINAL ; (* next local slot (BP-relative, 8-bit) *)
  383. locBytes : CARDINAL ; (* frame size for SUB SP *)
  384. parmOff : CARDINAL ; (* provisional parameter slot, in declaration
  385. order; remapped to TP3's BP+4 rule when the
  386. list has been read (see ProcFunc) *)
  387. dirs : DirRec ;
  388. tmpA, tmpB : CARDINAL ; (* global scratch word addresses *)
  389. crtAttrOff : CARDINAL ; (* image offset of the text attribute byte that
  390. LowVideo/NormVideo set and ClrScr/ClrEol use *)
  391. hdrFlag, hdrCS, hdrDS, hdrHeap, hdrMax : CARDINAL ;
  392. kName : ARRAY [0..42] OF ARRAY [0..15] OF CHAR ;
  393. kTk : ARRAY [0..42] OF CARDINAL ;
  394. (* ---------------------------------------------------------------- *)
  395. (* small char helpers *)
  396. (* ---------------------------------------------------------------- *)
  397. PROCEDURE CurCh () : CHAR ;
  398. BEGIN
  399. IF srcPos >= srcLen THEN
  400. RETURN 0C
  401. END ;
  402. RETURN CharAt (srcPos)
  403. END CurCh ;
  404. PROCEDURE GetCh () : CHAR ;
  405. VAR ch : CHAR ;
  406. BEGIN
  407. ch := CurCh () ;
  408. IF srcPos < srcLen THEN
  409. INC (srcPos)
  410. END ;
  411. RETURN ch
  412. END GetCh ;
  413. PROCEDURE PeekAhead (k : CARDINAL) : CHAR ;
  414. BEGIN
  415. IF srcPos + k >= srcLen THEN
  416. RETURN 0C
  417. END ;
  418. RETURN CharAt (srcPos + k)
  419. END PeekAhead ;
  420. PROCEDURE Digit (ch : CHAR) : BOOLEAN ;
  421. BEGIN
  422. RETURN (ch >= '0') AND (ch <= '9')
  423. END Digit ;
  424. PROCEDURE Alpha (ch : CHAR) : BOOLEAN ;
  425. BEGIN
  426. RETURN ((ch >= 'A') AND (ch <= 'Z')) OR ((ch >= 'a') AND (ch <= 'z'))
  427. OR (ch = '_')
  428. END Alpha ;
  429. PROCEDURE AlphaNum (ch : CHAR) : BOOLEAN ;
  430. BEGIN
  431. RETURN (Alpha (ch)) OR (Digit (ch))
  432. END AlphaNum ;
  433. PROCEDURE IsHexCh (ch : CHAR) : BOOLEAN ;
  434. BEGIN
  435. RETURN (Digit (ch)) OR ((ch >= 'A') AND (ch <= 'F'))
  436. OR ((ch >= 'a') AND (ch <= 'f'))
  437. END IsHexCh ;
  438. PROCEDURE Upper (ch : CHAR) : CHAR ;
  439. BEGIN
  440. IF (ch >= 'a') AND (ch <= 'z') THEN
  441. RETURN CHR (ORD (ch) - ORD ('a') + ORD ('A'))
  442. END ;
  443. RETURN ch
  444. END Upper ;
  445. PROCEDURE W16 (x : LONGINT) : CARDINAL ;
  446. (* fold x modulo 10000H, handling negatives (no negative MOD) *)
  447. VAR m : CARDINAL ;
  448. BEGIN
  449. IF x >= 0 THEN
  450. RETURN VAL (CARDINAL, x MOD 10000H)
  451. END ;
  452. m := VAL (CARDINAL, (0 - x) MOD 10000H) ;
  453. RETURN (10000H - m) MOD 10000H
  454. END W16 ;
  455. PROCEDURE DropCh (v : CHAR) ;
  456. BEGIN
  457. END DropCh ;
  458. PROCEDURE DropB (v : BOOLEAN) ;
  459. BEGIN
  460. END DropB ;
  461. PROCEDURE DropC (v : CARDINAL) ;
  462. BEGIN
  463. END DropC ;
  464. PROCEDURE BitAnd (a, b : LONGINT) : LONGINT ;
  465. BEGIN
  466. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, W16 (a))
  467. * VAL (BITSET, W16 (b))))
  468. END BitAnd ;
  469. PROCEDURE BitOr (a, b : LONGINT) : LONGINT ;
  470. BEGIN
  471. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, W16 (a))
  472. + VAL (BITSET, W16 (b))))
  473. END BitOr ;
  474. PROCEDURE BitNot (a : LONGINT) : LONGINT ;
  475. BEGIN
  476. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, 0FFFFH)
  477. - VAL (BITSET, W16 (a))))
  478. END BitNot ;
  479. (* ---------------------------------------------------------------- *)
  480. (* errors *)
  481. (* ---------------------------------------------------------------- *)
  482. PROCEDURE Err (n : CARDINAL) ;
  483. BEGIN
  484. IF NOT abortFac THEN
  485. abortFac := TRUE ;
  486. errNum := n ;
  487. txerrPos := srcPos
  488. END
  489. END Err ;
  490. PROCEDURE OK () : BOOLEAN ;
  491. BEGIN
  492. RETURN NOT abortFac
  493. END OK ;
  494. (* ---------------------------------------------------------------- *)
  495. (* {$I file} include *)
  496. (* ---------------------------------------------------------------- *)
  497. PROCEDURE SetSource (path : ARRAY OF CHAR) ;
  498. (* remember the path the buffer was loaded from; MakeIncludePath keeps only its
  499. directory *)
  500. VAR i : CARDINAL ;
  501. BEGIN
  502. i := 0 ;
  503. WHILE (i < 255) AND (i <= HIGH (path)) AND (path [i] # 0C) DO
  504. srcName [i] := path [i] ;
  505. INC (i)
  506. END ;
  507. srcName [i] := 0C
  508. END SetSource ;
  509. PROCEDURE ReadWholeFile (path : ARRAY OF CHAR ; VAR n : CARDINAL) : BOOLEAN ;
  510. (* Read path into incBuf with the same CR/LF normalisation as LoadWorkFile,
  511. and report its length in n. FALSE if it could not be opened. ^Z ends the
  512. text, as it does for every other reader here. *)
  513. VAR fd : INTEGER ;
  514. k : LONGINT ;
  515. b : BYTE ;
  516. ch : CHAR ;
  517. prevCR, emit : BOOLEAN ;
  518. BEGIN
  519. n := 0 ;
  520. fd := open (ADR (path), 0, 0) ;
  521. IF fd < 0 THEN
  522. RETURN FALSE
  523. END ;
  524. prevCR := FALSE ;
  525. LOOP
  526. k := read (fd, ADR (b), 1) ;
  527. IF k # 1 THEN EXIT END ;
  528. IF b = 26 THEN EXIT END ; (* ^Z *)
  529. emit := TRUE ;
  530. IF b = 10 THEN
  531. IF prevCR THEN
  532. emit := FALSE
  533. ELSE
  534. ch := CHR (13) (* lone LF -> CR *)
  535. END ;
  536. prevCR := FALSE
  537. ELSIF b = 13 THEN
  538. ch := CHR (13) ;
  539. prevCR := TRUE
  540. ELSE
  541. ch := CHR (ORD (b)) ;
  542. prevCR := FALSE
  543. END ;
  544. IF emit AND (n <= TextLimit) THEN
  545. incBuf [n] := ch ;
  546. INC (n)
  547. END
  548. END ;
  549. k := close (fd) ;
  550. RETURN TRUE
  551. END ReadWholeFile ;
  552. PROCEDURE MakeIncludePath () ;
  553. (* inclPath := directory (srcName) + inclName, defaulting the extension to
  554. .PAS when the name has none (TPSRC10 kpasext). *)
  555. VAR i, j, last : CARDINAL ; dot : BOOLEAN ;
  556. BEGIN
  557. dot := FALSE ;
  558. i := 0 ;
  559. WHILE (i < 127) AND (inclName [i] # 0C) DO
  560. IF inclName [i] = '.' THEN dot := TRUE END ;
  561. INC (i)
  562. END ;
  563. IF (NOT dot) AND (i <= 123) THEN
  564. inclName [i] := '.' ;
  565. inclName [i + 1] := 'P' ;
  566. inclName [i + 2] := 'A' ;
  567. inclName [i + 3] := 'S' ;
  568. inclName [i + 4] := 0C
  569. END ;
  570. last := 0 ;
  571. i := 0 ;
  572. WHILE (i < 256) AND (srcName [i] # 0C) DO
  573. IF srcName [i] = '/' THEN last := i + 1 END ;
  574. INC (i)
  575. END ;
  576. j := 0 ;
  577. WHILE j < last DO
  578. inclPath [j] := srcName [j] ;
  579. INC (j)
  580. END ;
  581. i := 0 ;
  582. WHILE (i < 127) AND (inclName [i] # 0C) DO
  583. inclPath [j] := inclName [i] ;
  584. INC (j) ;
  585. INC (i)
  586. END ;
  587. inclPath [j] := 0C
  588. END MakeIncludePath ;
  589. PROCEDURE ExpandIncludes () ;
  590. (* Replace every {$I name} directive in the buffer with the named file's text,
  591. BEFORE parsing.
  592. This is a pre-pass, and it has to be. Skip is called by lookahead routines
  593. (DeclaresProc, PeekKw, KwAhead) that SAVE AND RESTORE srcPos; an include
  594. performed inside Skip would run once during the lookahead and once for real,
  595. and the second pass would see the first splice's text and report error 96.
  596. A splice also grows the buffer, which a rewind cannot undo. So the whole
  597. expansion is done here, in one forward scan with no lookahead, and the
  598. parser only ever sees the finished text.
  599. The scan has to recognise exactly what the lexer does, or it would expand a
  600. directive that the lexer would have read as a string or a comment: a quoted
  601. literal is skipped, then star-paren comments (which is why {$I} written
  602. inside one is inert - t56_include.inc relies on that), then brace comments,
  603. and only then a brace directive.
  604. Divergence from the original: TP3 STREAMS the include file line by line and
  605. switches back to the outer file at EOF (TPSRC10 fileline/getincl/fileeof);
  606. we hold the whole source in TextBuf and address it by index, so the include
  607. is spliced in place. A nested {$I} therefore cannot switch back on its own
  608. - which TP3 also refuses, and that is the error 96 reported here. *)
  609. VAR i, d0, d1, k, ni, n : CARDINAL ;
  610. c, letter : CHAR ;
  611. isInc : BOOLEAN ;
  612. BEGIN
  613. i := 0 ;
  614. inclActive := FALSE ;
  615. inclEnd := 0 ;
  616. WHILE (i < Length ()) AND OK () DO
  617. c := CharAt (i) ;
  618. IF c = "'" THEN
  619. INC (i) ;
  620. WHILE (i < Length ()) AND (CharAt (i) # "'") DO INC (i) END ;
  621. IF i < Length () THEN INC (i) END
  622. ELSIF (c = '(') AND (i + 1 < Length ()) AND (CharAt (i + 1) = '*') THEN
  623. INC (i, 2) ;
  624. WHILE (i < Length ()) AND NOT ((CharAt (i) = '*')
  625. AND (i + 1 < Length ()) AND (CharAt (i + 1) = ')')) DO
  626. INC (i)
  627. END ;
  628. IF i < Length () THEN INC (i, 2) END
  629. ELSIF (c = '{') AND (i + 1 < Length ()) AND (CharAt (i + 1) = '$') THEN
  630. d0 := i ;
  631. d1 := i + 2 ;
  632. WHILE (d1 < Length ()) AND (CharAt (d1) # '}') DO INC (d1) END ;
  633. IF i + 2 < Length () THEN
  634. letter := Upper (CharAt (i + 2))
  635. ELSE
  636. letter := 0C
  637. END ;
  638. isInc := FALSE ;
  639. IF letter = 'I' THEN
  640. k := i + 3 ;
  641. WHILE (k < d1) AND ((CharAt (k) = ' ') OR (CharAt (k) = CHR (9))) DO
  642. INC (k)
  643. END ;
  644. IF (k >= d1) OR ((CharAt (k) # '+') AND (CharAt (k) # '-')) THEN
  645. isInc := TRUE
  646. END
  647. END ;
  648. IF isInc THEN
  649. IF inclActive AND (d0 < inclEnd) THEN
  650. srcPos := d0 ;
  651. Err (EInclNest) ; (* 96 *)
  652. RETURN
  653. END ;
  654. ni := 0 ;
  655. WHILE (k < d1) AND (ni < 127) DO
  656. inclName [ni] := CharAt (k) ;
  657. INC (k) ;
  658. INC (ni)
  659. END ;
  660. WHILE (ni > 0) AND ((inclName [ni - 1] = ' ')
  661. OR (inclName [ni - 1] = CHR (9))) DO
  662. DEC (ni)
  663. END ;
  664. inclName [ni] := 0C ;
  665. MakeIncludePath () ;
  666. IF NOT ReadWholeFile (inclPath, n) THEN
  667. srcPos := d0 ;
  668. Err (EFileNF) ; (* 90 *)
  669. RETURN
  670. END ;
  671. DeleteFromTo (d0, d1 - d0 + 1) ;
  672. IF NOT InsertStr (d0, incBuf, n) THEN
  673. srcPos := d0 ;
  674. Err (EMemOvf) ;
  675. RETURN
  676. END ;
  677. inclEnd := d0 + n ;
  678. inclActive := TRUE ;
  679. i := d0 (* scan the new text too *)
  680. ELSE
  681. i := d1 + 1 (* ordinary directive: leave it *)
  682. END
  683. ELSIF c = '{' THEN
  684. INC (i) ;
  685. WHILE (i < Length ()) AND (CharAt (i) # '}') DO INC (i) END ;
  686. IF i < Length () THEN INC (i) END
  687. ELSE
  688. INC (i)
  689. END
  690. END ;
  691. srcLen := Length () ;
  692. srcPos := 0
  693. END ExpandIncludes ;
  694. (* ---------------------------------------------------------------- *)
  695. (* emission : ebyte / eword / ecall / ejump *)
  696. (* ---------------------------------------------------------------- *)
  697. PROCEDURE Ebyte (b : BYTE) ;
  698. BEGIN
  699. IF (pc >= MaxCode) OR (imgReady AND (pc >= CodeMax)) THEN
  700. (* The image buffer bound (MaxCode) and the boot harness's code ceiling
  701. (CodeMax). Globals no longer sit immediately above the code - they
  702. live at DataStart and initmem zeroes them - so this is purely a
  703. capacity check: cbuf cannot hold another byte, or the image would
  704. reach the stdin descriptor at memory 7000h. Reported as TP3's error
  705. 98 (memory overflow) rather than a silent truncation. *)
  706. Err (EMemOvf)
  707. ELSE
  708. cbuf [pc] := b ;
  709. INC (pc)
  710. END
  711. END Ebyte ;
  712. PROCEDURE Eword (w : CARDINAL) ;
  713. BEGIN
  714. Ebyte (VAL (BYTE, w MOD 100H)) ;
  715. Ebyte (VAL (BYTE, (w DIV 100H) MOD 100H))
  716. END Eword ;
  717. PROCEDURE PatchWord (at, w : CARDINAL) ;
  718. (* Store a 16-bit word into cbuf at an absolute offset.
  719. A helper, because writing this inline got it wrong in all four header
  720. words: the low byte was (w DIV 16) MOD 100H, which is a NIBBLE shift, not
  721. the byte shift (w MOD 100H). So 1181h - the data base - was stored as
  722. 0118h = 280. It was invisible for as long as nothing read those words,
  723. which is exactly what "write it inline once and trust it" buys you. *)
  724. BEGIN
  725. cbuf [at] := VAL (BYTE, w MOD 100H) ;
  726. cbuf [at + 1] := VAL (BYTE, (w DIV 100H) MOD 100H)
  727. END PatchWord ;
  728. PROCEDURE AddPatch (place, target : CARDINAL) ;
  729. BEGIN
  730. IF nPatch < MaxPatch THEN
  731. patches [nPatch].place := place ;
  732. patches [nPatch].target := target ;
  733. patches [nPatch].filled := FALSE ;
  734. INC (nPatch)
  735. ELSE
  736. Err (ECompOvf)
  737. END
  738. END AddPatch ;
  739. PROCEDURE SetPatTgt (idx, t : CARDINAL) ;
  740. BEGIN
  741. IF idx < nPatch THEN
  742. patches [idx].target := t
  743. END
  744. END SetPatTgt ;
  745. PROCEDURE EmCall (target : CARDINAL) : CARDINAL ;
  746. (* E8 rel16 near call; target = 0 => forward (patched later).
  747. Returns the patch slot, or 0 when resolved directly. *)
  748. VAR rel, p : CARDINAL ;
  749. BEGIN
  750. Ebyte (0E8H) ;
  751. IF target = 0 THEN
  752. Eword (0) ;
  753. p := nPatch ;
  754. AddPatch (pc - 2, 0) ;
  755. RETURN p
  756. END ;
  757. (* rel16 is measured from the END of the instruction. Here pc already
  758. points past the opcode(s) and at the displacement field, so the
  759. instruction ends at pc+2 - the same convention ResolvePatches uses
  760. with "place + 2". Omitting the +2 lands every direct call/jump 2 bytes
  761. past its target. *)
  762. rel := (target + 10000H - (pc + 2)) MOD 10000H ;
  763. Eword (rel) ;
  764. RETURN 0
  765. END EmCall ;
  766. PROCEDURE EmJmpNear (target : CARDINAL) : CARDINAL ;
  767. (* E9 rel16; target = 0 => forward. Returns patch slot or 0. *)
  768. VAR rel, p : CARDINAL ;
  769. BEGIN
  770. Ebyte (0E9H) ;
  771. IF target = 0 THEN
  772. Eword (0) ;
  773. p := nPatch ;
  774. AddPatch (pc - 2, 0) ;
  775. RETURN p
  776. END ;
  777. rel := (target + 10000H - (pc + 2)) MOD 10000H ; (* see EmCall *)
  778. Eword (rel) ;
  779. RETURN 0
  780. END EmJmpNear ;
  781. PROCEDURE JccShort (cc : BYTE) : BYTE ;
  782. (* The 8086 SHORT Jcc opcode for a condition nibble. 70h..7Fh is exactly
  783. 70h + nibble: 70 JO 71 JNO 72 JB 73 JAE 74 JE 75 JNE 76 JBE 77 JA
  784. 78 JS 79 JNS 7A JP 7B JNP 7C JL 7D JGE 7E JLE 7F JG.
  785. So 70H + cc is the same condition the 386-only `0F 8x rel16' (for a Jcc) or
  786. `0F 9x' (for a SETcc) encoded, which is what lets the seven EmJcc sites and
  787. the six EmSetcc arms go on passing the low byte they always passed. *)
  788. VAR n : CARDINAL ;
  789. BEGIN
  790. n := VAL (CARDINAL, cc) MOD 10H ;
  791. RETURN VAL (BYTE, 70H + n)
  792. END JccShort ;
  793. PROCEDURE JccShortInv (cc : BYTE) : BYTE ;
  794. (* The same, for a jump that is taken when the condition does NOT hold.
  795. EmJcc needs this one and EmSetcc needs the other, and the difference is the
  796. whole bug, so it is worth being explicit about where it comes from: the low
  797. bit of a Jcc code IS the negation bit. 4/5, C/D, E/F, 2/3, 6/7, A/9, B/8 and
  798. 0/1 are the eight (condition, its negation) pairs, so negating a condition is
  799. `n XOR 1' and nothing more - `JE' and `JNE' are 0x74 and 0x75.
  800. Gm2 under -fiso has no XOR on integers at all: BITAND and BAND are both
  801. syntax errors, and arithmetic on a BYTE operand is rejected too, which is
  802. why every operand here goes through VAL. n + 1 - 2*(n MOD 2) is XOR 1 for a
  803. four-bit n and it lives in one named place rather than open-coded, because
  804. an open-coded negation at two call sites is how they end up disagreeing. *)
  805. VAR n : CARDINAL ;
  806. BEGIN
  807. n := VAL (CARDINAL, cc) MOD 10H ;
  808. RETURN VAL (BYTE, 70H + n + 1 - 2 * (n MOD 2))
  809. END JccShortInv ;
  810. PROCEDURE EmJcc (cc : BYTE ; target : CARDINAL) : CARDINAL ;
  811. (* A conditional branch, 8086 style. `cc' is the condition nibble, and it is
  812. exactly the low byte of the `0F 8x rel16' this used to emit.
  813. That was a real fault and nothing in the build could see it: `0F' is a
  814. 386-and-later opcode prefix and the 8086 has none, so EVERY conditional
  815. branch in EVERY compiled program was an illegal instruction on the machine
  816. TP3 targets. FCML decoded it happily, because FCML's -m16 mode is 386 --
  817. and FCML is this project's independent disassembler, so the one tool that
  818. could have objected was the one guaranteed to agree. qemu-system-i386 has
  819. no 8086 model either; its lowest is 486. So the compile succeeded, the
  820. .COM linked, the layout checked, the golden held and all 30 fixtures ran to
  821. the right answers, all at once, with the bug in.
  822. TPSRC8 lays IF, WHILE and REPEAT out as
  823. MOV AL,brnchop ; MOV AH,#$03 ; CALL eword ; PUSH pc ; CALL ejump
  824. i.e. a SHORT Jcc of displacement 3, stepping over a 3-byte EJMP. That is
  825. the shape here too, and EmJmpNear already owns the displacement arithmetic
  826. and the patch slot, so it is three lines and there is no second copy of
  827. that rule.
  828. The one thing that is NOT the same as TPSRC8, and cost a round of "every
  829. conditional is inverted" (t09_if printed pos/nonpos/lt for a program that
  830. must print nonpos/pos/ge): TP3's brnchop is the branch taken when the
  831. condition is TRUE, and TP3 steps over the EJMP when it is taken. Here `cc'
  832. is the branch taken when the condition is FALSE -- IF's `EmJcc (84H)' is
  833. JZ, patched to the ELSE, so it must fire when the test failed. EmJcc jumps
  834. to the target, it does not step over it, so stepping over an EJMP and then
  835. falling into the destination is the wrong way round: the byte has to be
  836. JccShortInv, not JccShort. The control flow that comes out is identical to
  837. the `0F 8x' form this replaces; only which of the pair is spelled differs.
  838. The flags survive, and the FOR test needs them to: it emits CMP and then
  839. Jcc with nothing in between, so anything that wrote a flag here would
  840. break the loop. Jcc and EJMP both leave the flags alone. *)
  841. BEGIN
  842. Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)
  843. Ebyte (03H) ; (* rel8: step over the 3-byte EJMP *)
  844. RETURN EmJmpNear (target) (* target = 0 => forward, see above *)
  845. END EmJcc ;
  846. PROCEDURE ResolvePatches () ;
  847. VAR i : CARDINAL ;
  848. rel : CARDINAL ;
  849. BEGIN
  850. i := 0 ;
  851. WHILE i < nPatch DO
  852. IF NOT patches [i].filled THEN
  853. rel := (patches [i].target + 10000H - (patches [i].place + 2))
  854. MOD 10000H ;
  855. cbuf [patches [i].place] := VAL (BYTE, rel MOD 100H) ;
  856. cbuf [patches [i].place + 1] := VAL (BYTE, (rel DIV 100H) MOD 100H) ;
  857. patches [i].filled := TRUE
  858. END ;
  859. INC (i)
  860. END
  861. END ResolvePatches ;
  862. (* 1-byte const emitters *)
  863. PROCEDURE EmMovAxi (imm : CARDINAL) ;
  864. BEGIN
  865. Ebyte (0B8H) ; Eword (imm)
  866. END EmMovAxi ;
  867. PROCEDURE EmMovCxi (imm : CARDINAL) ;
  868. BEGIN
  869. Ebyte (0B9H) ; Eword (imm)
  870. END EmMovCxi ;
  871. PROCEDURE EmMovBpSp () ;
  872. BEGIN
  873. Ebyte (8BH) ; Ebyte (0ECH)
  874. END EmMovBpSp ;
  875. PROCEDURE EmMovAh0 () ;
  876. BEGIN
  877. Ebyte (0B4H) ; Ebyte (0H)
  878. END EmMovAh0 ;
  879. PROCEDURE EmMovAxSp () ;
  880. (* Read the top of the stack into AX, leaving the stack unchanged.
  881. The obvious encoding, MOV AX,[SP], DOES NOT EXIST on the 8086. There is
  882. no encoding of [SP] as a memory operand: SIB bytes, which is how [ESP]
  883. would be written, did not exist until the 386, and mod=00 / rm=100 is
  884. [SI], not [SP]. The first version of this emitted 8B 44 24 00 - mod=01,
  885. rm=100, SIB=24h, disp8=0 - which is correct only on a 386 and above. Two
  886. of this project's oracles agree that it is wrong: fcml in 16-bit mode
  887. decodes it as MOV AX,[SI+0x24h], and so does qemu executing it, because
  888. qemu follows the CPU's rules for the encoding it is given rather than
  889. guessing. It was not the encoder's fault that the bytes were well formed;
  890. they were, and they read SI+24h.
  891. The observable effect was that CASE compiled to no branches at all: each
  892. label test loaded a garbage address, every comparison failed, and the
  893. program fell straight past the whole statement and exited without printing.
  894. A CASE fixture caught it. Nothing else could have - the encoding is
  895. valid, the size is right, and the byte-level checks have no way to know
  896. what register was meant.
  897. So: POP then PUSH the same value. Two bytes, no SIB, correct on every
  898. 8086, and observationally identical to peeking - the stack pointer ends
  899. where it started, holding the same value. *)
  900. BEGIN
  901. Ebyte (58H) ; (* POP AX *)
  902. Ebyte (50H) (* PUSH AX *)
  903. END EmMovAxSp ;
  904. PROCEDURE EmMovCxSp () ;
  905. (* The same, for CX - the FOR loop's bound, pushed by the FOR statement and
  906. re-read on every iteration. This one was emitting 8B 0C and nothing else,
  907. which is MOV CX,[SI] with the SIB slot missing: the *next* instruction was
  908. consumed as the SIB byte and the displacement. Same root cause, same fix,
  909. and it had not been noticed only because no FOR fixture is executed yet. *)
  910. BEGIN
  911. Ebyte (59H) ; (* POP CX *)
  912. Ebyte (51H) (* PUSH CX *)
  913. END EmMovCxSp ;
  914. PROCEDURE EmPushAx () ;
  915. BEGIN
  916. Ebyte (50H)
  917. END EmPushAx ;
  918. PROCEDURE EmPopCx () ;
  919. BEGIN
  920. Ebyte (59H)
  921. END EmPopCx ;
  922. PROCEDURE EmPopDx () ;
  923. BEGIN
  924. Ebyte (5AH)
  925. END EmPopDx ;
  926. PROCEDURE EmPopSi () ;
  927. BEGIN
  928. Ebyte (5EH) (* POP SI *)
  929. END EmPopSi ;
  930. PROCEDURE EmPopAx () ;
  931. BEGIN
  932. Ebyte (58H) (* POP AX *)
  933. END EmPopAx ;
  934. PROCEDURE EmPushBx () ;
  935. BEGIN Ebyte (53H) END EmPushBx ;
  936. PROCEDURE EmPopBx () ;
  937. BEGIN Ebyte (5BH) END EmPopBx ;
  938. PROCEDURE EmMovAxCx () ;
  939. BEGIN Ebyte (89H) ; Ebyte (0C8H) END EmMovAxCx ; (* MOV AX,CX *)
  940. PROCEDURE EmMovChAl () ;
  941. BEGIN Ebyte (88H) ; Ebyte (0C5H) END EmMovChAl ; (* MOV CH,AL *)
  942. PROCEDURE EmMovChImm (v : CARDINAL) ;
  943. BEGIN Ebyte (0B5H) ; Ebyte (VAL (BYTE, v MOD 256)) END EmMovChImm ;
  944. (* 91 = XCHG AX,CX, and NOT 93. LoadPair hands the operator the left
  945. operand in CX and the right in AX (in the original, and still the
  946. commonest, shape: it pushes the left, loads the right, then pops the
  947. left into CX), so the exchange is what puts LEFT in AX for the
  948. operation to act on.
  949. Without it, `a - b` computes `b - a`; with the wrong register, `a + b`
  950. computes `AX' + a` where AX' is whatever BX happened to hold.
  951. This emitted 93H = XCHG BX,AX for its entire life, which is the same class
  952. of mistake as MovSiBx = 89 DC in Runtime.mod: the right opcode, the wrong
  953. ModRM, decoding cleanly. Byte counts were right, the compile matrix was
  954. green, and no exec fixture did arithmetic on two variables - the first one
  955. to do so, `c := a + b` with a=7 b=5, printed 263 = 0100h+7, where 0100h
  956. was the caller's leftover BX. The name was the only thing wrong, and
  957. nothing read the name: audit_helpers.py swept Runtime.mod and not
  958. Compiler.mod, which is where most of these emitters live. It does both
  959. modules now. *)
  960. PROCEDURE EmXchgAxCx () ;
  961. BEGIN
  962. Ebyte (91H)
  963. END EmXchgAxCx ;
  964. PROCEDURE EmXorAxAx () ;
  965. BEGIN
  966. Ebyte (33H) ; Ebyte (0C0H)
  967. END EmXorAxAx ;
  968. PROCEDURE EmAddAxCx () ;
  969. BEGIN
  970. Ebyte (3H) ; Ebyte (0C1H)
  971. END EmAddAxCx ;
  972. PROCEDURE EmSubAxCx () ;
  973. BEGIN
  974. Ebyte (2BH) ; Ebyte (0C1H)
  975. END EmSubAxCx ;
  976. PROCEDURE EmMulAxCx () ;
  977. BEGIN
  978. Ebyte (0F7H) ; Ebyte (0E9H)
  979. END EmMulAxCx ;
  980. PROCEDURE EmIDivAxCx () ;
  981. BEGIN
  982. Ebyte (99H) ; Ebyte (0F7H) ; Ebyte (0F9H)
  983. END EmIDivAxCx ;
  984. PROCEDURE EmAndAxCx () ;
  985. BEGIN
  986. Ebyte (23H) ; Ebyte (0C1H)
  987. END EmAndAxCx ;
  988. PROCEDURE EmOrAxCx () ;
  989. BEGIN
  990. Ebyte (0BH) ; Ebyte (0C1H)
  991. END EmOrAxCx ;
  992. PROCEDURE EmOrAxAx () ;
  993. (* 09 C0: OR AX,AX - the 8086's flag-only test of AX, and the one the string
  994. comparison needs, because cmpstr hands back a signed -1/0/+1 rather than
  995. flags. OR AX,AX sets SF and ZF from the value and clears OF and CF, which
  996. is exactly the flag state a CMP AX,0 would leave - see EmSetcc and
  997. ParseCmp's EmRelSetcc. TEST AX,AX (85 C0) does the same thing, but
  998. tests/audit_helpers.py's name grammar has no `Test' mnemonic, and an
  999. emitter that cannot be name-checked is worse than one that can.
  1000. It is NOT EmXorAxAx: XORing AX with itself zeroes it, which would throw
  1001. away the very value the flags are supposed to describe. *)
  1002. BEGIN
  1003. Ebyte (09H) ; Ebyte (0C0H)
  1004. END EmOrAxAx ;
  1005. PROCEDURE EmNegAx () ;
  1006. BEGIN
  1007. Ebyte (0F7H) ; Ebyte (0D8H)
  1008. END EmNegAx ;
  1009. PROCEDURE EmNotAx () ;
  1010. BEGIN
  1011. Ebyte (0F7H) ; Ebyte (0D0H)
  1012. END EmNotAx ;
  1013. PROCEDURE EmXorAl01 () ;
  1014. (* XOR AL,#01 - TPSRC9 neglevel's *boolean* NOT:
  1015. CALL loadatom ; CALL ecode ; B $02,$34,$01
  1016. neglevel tests the type first (CMP CL,#$0A -> integer -> NOT AX,
  1017. CMP CL,#$0B -> boolean -> XOR AL,#01) and reports error 47 for anything
  1018. else. Emitting NOT AX for a boolean leaves 0FFFEh or 0FFFFh, and the
  1019. runtime's wrbool tests [BP+4] <> 0, so `not (a = a)' came out TRUE. *)
  1020. BEGIN
  1021. Ebyte (34H) ; Ebyte (01H)
  1022. END EmXorAl01 ;
  1023. PROCEDURE EmCmpAxCx () ;
  1024. BEGIN
  1025. Ebyte (3BH) ; Ebyte (0C1H)
  1026. END EmCmpAxCx ;
  1027. PROCEDURE EmCmpAxi (imm : CARDINAL) ;
  1028. BEGIN
  1029. Ebyte (03DH) ; Eword (imm)
  1030. END EmCmpAxi ;
  1031. PROCEDURE EmSetcc (cc : BYTE) ;
  1032. (* Flags -> a Boolean in AX, on an 8086. `cc' is the SETcc opcode's low byte
  1033. (94H = E, 95H = NE, 9CH = L, 9DH = GE, 9EH = LE, 9FH = G), i.e. the same
  1034. condition nibble EmJcc takes.
  1035. This used to emit `0F cc C0' - SETcc - which is 386-and-later, and then a
  1036. MOV AH,0. The 8086 cannot read its flags as a value at all, so there was
  1037. nothing else to fall back on.
  1038. TPSRC9's flgbool is the fallback, and emits exactly this for exactly this
  1039. case (CH = 04h, a comparison whose result is wanted as a value rather than
  1040. as a branch):
  1041. CALL ecode ; B $03,$B8,$01,$00 -> MOV AX,#0001
  1042. MOV AL,brnchop ; CALL ebyte -> JNZ +1
  1043. CALL ecode ; B $02,$01,$48 -> DEC AX
  1044. AX stays 1 because the DEC was stepped over, and becomes 0 because it ran.
  1045. So the shape is one MOV, one short Jcc whose displacement is the length of
  1046. the DEC, and the DEC - which is EmJcc's shape with a different displacement,
  1047. and the reason both are two instructions and a byte.
  1048. The polarity is the opposite of EmJcc's, and deliberately so: here the jump
  1049. must be taken when the comparison is TRUE, because what is being asked is
  1050. "is this comparison true", and the nibble the six ParseCmp arms pass is the
  1051. comparison's own opcode. So this is JccShort and EmJcc is JccShortInv --
  1052. see EmJcc for why the difference is there at all. TP3's flgbool writes JNZ
  1053. for the same reason; in the one case IT reaches flgbool from, the boolean is
  1054. sitting in AX rather than in the flags, so JNZ is how it says "AX is
  1055. non-zero".
  1056. AH comes out 0 for free, which is why the EmMovAh0 this used to end with is
  1057. gone: 6 bytes here where the old sequence was 5. The FLAGS do not survive,
  1058. which the old SETcc did - and nothing reads them. Every conditional branch
  1059. in the compiler is preceded by its own CMP (see EmJcc's note on the FOR
  1060. test), and a comparison's value is consumed either as an AX operand or by
  1061. the test that follows it; the 30 executed fixtures are what holds that
  1062. down, not this comment. *)
  1063. BEGIN
  1064. Ebyte (0B8H) ; Eword (1) ; (* MOV AX,#0001 *)
  1065. Ebyte (JccShort (cc)) ; (* taken when the comparison HOLDS *)
  1066. Ebyte (01H) ; (* rel8: step over the DEC AX *)
  1067. Ebyte (48H) (* DEC AX *)
  1068. END EmSetcc ;
  1069. PROCEDURE EmRelSetcc (op : CARDINAL) ;
  1070. (* The relational operator -> SETcc table, in ONE place, because there are now
  1071. two callers: ParseCmp's numeric arm, whose flags come from EmCmpAxCx, and
  1072. its string arm, whose flags come from a cmpstr result put through
  1073. EmOrAxAx. It was factored out of ParseCmp when the string arm arrived, and
  1074. the note it carried is repeated here rather than referenced, because a
  1075. comment in the caller about a table in another routine is a comment nobody
  1076. reads.
  1077. The mnemonic is written next to every opcode on purpose. `op' is a number,
  1078. so the arm for ">" and the arm for ">=" differed only by two hex digits
  1079. that are each one letter from the other meaning -- SETG (9FH) and SETGE
  1080. (9DH). They were swapped once, which made a > b mean a >= b and a >= b
  1081. mean a > b. Only the equality boundary could see it: 6>5, 5<5, -1>-2 and
  1082. every other case I tried were already right. An opcode on its own does not
  1083. say which comparison it is the answer to.
  1084. `op' is ParseCmp's own numbering: 1 "=", 2 "<>", 3 "<", 4 ">", 5 ">=",
  1085. 6 "<=". The condition nibbles are the SIGNED ones (L/G/GE/LE), and both
  1086. callers hand it a value whose SIGN is the answer - ParseCmp's numeric arm
  1087. compares with CMP AX,CX on signed data, and cmpstr answers -1/0/+1 - so
  1088. the signed test is the right one in both places. One table also means that
  1089. swap cannot come back in one arm and not the other: tests/nonvacuity.sh
  1090. mutates exactly these two lines, and it now breaks the string comparison
  1091. at the same time as the numeric one. *)
  1092. BEGIN
  1093. CASE op OF
  1094. 1 : EmSetcc (94H) ; (* = SETE *)
  1095. | 2 : EmSetcc (95H) ; (* <> SETNE *)
  1096. | 3 : EmSetcc (9CH) ; (* < SETL *)
  1097. | 4 : EmSetcc (9FH) ; (* > SETG *)
  1098. | 5 : EmSetcc (9DH) ; (* >= SETGE *)
  1099. | 6 : EmSetcc (9EH) (* <= SETLE *)
  1100. END
  1101. END EmRelSetcc ;
  1102. PROCEDURE EmIncAx () ;
  1103. BEGIN
  1104. Ebyte (40H)
  1105. END EmIncAx ;
  1106. PROCEDURE EmDecAx () ;
  1107. BEGIN
  1108. Ebyte (48H)
  1109. END EmDecAx ;
  1110. PROCEDURE EmBpDisp (off, regf : CARDINAL) ;
  1111. (* Emit the ModR/M byte and displacement for a [BP+off] operand, picking the
  1112. encoding from the size of off. This is the ONE place that choice is made,
  1113. because getting it wrong is invisible: 8B 46 d8 and 8B 86 lo hi are both
  1114. well-formed MOVs, both decode cleanly, and only one of them reads the
  1115. variable the symbol table names. So the two are chosen here, once, rather
  1116. than re-derived at each of the four call sites. See the ModR/M table in
  1117. Runtime.mod.
  1118. off <= 127 -> mod=01 rm=110 -> 46 <disp8> 3 bytes with the opcode
  1119. otherwise -> mod=10 rm=110 -> 86 <disp16> 4 bytes with the opcode
  1120. Both displacements are SIGNED, and that is the whole subtlety:
  1121. - `off` is a 16-bit value and locals are packed top-anchored below BP:
  1122. locFree is the highest still-free byte (0FFFFh = -1 at entry), a local of
  1123. size n is based so its LAST byte sits on locFree, and locFree then drops
  1124. by n. So the first INTEGER local is at off = 0FFFEh = -2, the second at
  1125. 0FFFCh = -4, and so on - and a string local, which grows UP from its
  1126. base, lands wholly below BP instead of straddling it. The disp16 form
  1127. reads those same two bytes as a signed value and addresses the negative
  1128. slot correctly. There is no overflow case: all 65536 values of `off`
  1129. are representable, and a frame larger than 64K is a different problem.
  1130. - The old code took `off MOD 100H` and always emitted disp8. That is
  1131. the correct low byte for every displacement, so it was accidentally
  1132. right across -32768..+127, which is where locals actually live. It
  1133. went wrong at +128, where disp8 80h is -128 and not +128. So this
  1134. changes no existing program's bytes and fixes the one case that was
  1135. broken -- a bug nobody had hit yet, which is exactly why it wanted a
  1136. test rather than an argument. *)
  1137. BEGIN
  1138. (* mod=01 rm=110 -> 40H + regf*8 + 6H, mod=10 rm=110 -> 80H + regf*8 + 6H.
  1139. regf is the ModR/M reg field: 0 when the opcode's register is the
  1140. accumulator (every original caller), 7 for the DI forms EmLeaDi needs.
  1141. rm is 110 ([BP+disp]) in both. *)
  1142. IF off <= 127 THEN
  1143. Ebyte (VAL (BYTE, 40H + regf * 8 + 6H)) ; Ebyte (VAL (BYTE, off))
  1144. ELSE
  1145. Ebyte (VAL (BYTE, 80H + regf * 8 + 6H)) ; Eword (off)
  1146. END
  1147. END EmBpDisp ;
  1148. PROCEDURE EmLoadVar (local : BOOLEAN ; off, nbytes : CARDINAL) ;
  1149. (* A local is [BP+off] and `off' is already a frame displacement, so it needs
  1150. no bias. A global is [off] with a DIRECT displacement, i.e. an absolute
  1151. address, and that is the image offset + LoadBias - see Runtime.LoadBias. *)
  1152. BEGIN
  1153. IF nbytes = 1 THEN
  1154. IF local THEN
  1155. Ebyte (8AH) ; EmBpDisp (off, 0)
  1156. ELSE
  1157. Ebyte (0A0H) ; Eword ((off + LoadBias) MOD 10000H)
  1158. END
  1159. ELSE
  1160. IF local THEN
  1161. Ebyte (8BH) ; EmBpDisp (off, 0)
  1162. ELSE
  1163. Ebyte (0A1H) ; Eword ((off + LoadBias) MOD 10000H)
  1164. END
  1165. END
  1166. END EmLoadVar ;
  1167. PROCEDURE EmStoreVar (local : BOOLEAN ; off, nbytes : CARDINAL) ;
  1168. BEGIN
  1169. IF nbytes = 1 THEN
  1170. IF local THEN
  1171. Ebyte (88H) ; EmBpDisp (off, 0)
  1172. ELSE
  1173. Ebyte (0A2H) ; Eword ((off + LoadBias) MOD 10000H)
  1174. END
  1175. ELSE
  1176. IF local THEN
  1177. Ebyte (89H) ; EmBpDisp (off, 0)
  1178. ELSE
  1179. Ebyte (0A3H) ; Eword ((off + LoadBias) MOD 10000H)
  1180. END
  1181. END
  1182. END EmStoreVar ;
  1183. PROCEDURE EmPushVarAddr (local : BOOLEAN ; off : CARDINAL) ;
  1184. (* LEA AX,[BP+disp] / LEA AX,[off] then PUSH AX - READ passes the address of
  1185. a variable, not its value. 8D 46 disp is LEA AX,[BP+disp8] and
  1186. 8D 86 lo hi is LEA AX,[BP+disp16]; 8D 06 off is LEA AX,[off]
  1187. (mod=00 rm=110 = the direct disp16 form). All three are 8086-legal.
  1188. The [off] form is absolute and so carries LoadBias; the [BP+disp] forms
  1189. are displacements and so do not. *)
  1190. BEGIN
  1191. IF local THEN
  1192. Ebyte (8DH) ; EmBpDisp (off, 0)
  1193. ELSE
  1194. Ebyte (8DH) ; Ebyte (06H) ; Eword ((off + LoadBias) MOD 10000H)
  1195. END ;
  1196. EmPushAx ()
  1197. END EmPushVarAddr ;
  1198. PROCEDURE EmLeaDi (local : BOOLEAN ; off : CARDINAL) ;
  1199. (* LEA DI,[BP+disp] / LEA DI,[off] - the run-time address of an indexed
  1200. string's element, computed once in ParseSub and read by LoadAtom,
  1201. Assignment and IoCall.
  1202. It is the same three encodings EmPushVarAddr uses for AX - 8D 7E disp8,
  1203. 8D BE disp16, 8D 3E off - with the ModR/M reg field set to DI (111)
  1204. instead of the accumulator, which is why EmBpDisp now takes that field and
  1205. this is still the one place the disp8/disp16 choice is made. A local is
  1206. [BP+disp] with no bias; a global is [off] and carries LoadBias, absolute.
  1207. The later `MOV AL,[DI]` / `MOV [DI],AL` read through DS rather than SS.
  1208. That is correct here for the same reason TP3's own string ops use DI with
  1209. the default segment: a .COM loads with DS=SS=CS=ES, and nothing in this
  1210. compiler or its runtime ever reloads a segment register, so a BP-relative
  1211. frame address held in DI addresses the same byte. *)
  1212. BEGIN
  1213. IF local THEN
  1214. Ebyte (8DH) ; EmBpDisp (off, 7)
  1215. ELSE
  1216. Ebyte (8DH) ; Ebyte (3EH) ; Eword ((off + LoadBias) MOD 10000H)
  1217. END
  1218. END EmLeaDi ;
  1219. PROCEDURE EmAddDiAx () ;
  1220. BEGIN Ebyte (01H) ; Ebyte (0C7H) END EmAddDiAx ; (* 01 C7: DI := DI + AX *)
  1221. PROCEDURE EmAddDiImm (n : CARDINAL) ;
  1222. (* ADD DI,imm16: a record field's offset when the element's address is already
  1223. in DI (`Sheet[I,J].Field', `with Sheet[I,J] do'). *)
  1224. BEGIN
  1225. Ebyte (81H) ; Ebyte (0C7H) ; Eword (n MOD 10000H)
  1226. END EmAddDiImm ;
  1227. PROCEDURE EmLdDiGlob (off : CARDINAL) ;
  1228. (* MOV DI,[off]: recover the base address WITH saved in a scratch word. The
  1229. body of a WITH may clobber DI, so the address is parked in the data area
  1230. and reloaded for every field access. *)
  1231. BEGIN
  1232. Ebyte (8BH) ; Ebyte (3EH) ; Eword ((off + LoadBias) MOD 10000H)
  1233. END EmLdDiGlob ;
  1234. PROCEDURE EmStDiGlob (off : CARDINAL) ;
  1235. (* MOV [off],DI: the WITH statement's base-address scratch. *)
  1236. BEGIN
  1237. Ebyte (89H) ; Ebyte (3EH) ; Eword ((off + LoadBias) MOD 10000H)
  1238. END EmStDiGlob ;
  1239. PROCEDURE EmLdAlDi () ;
  1240. BEGIN Ebyte (8AH) ; Ebyte (05H) END EmLdAlDi ; (* 8A 05: AL := [DI] *)
  1241. PROCEDURE EmLdAlSi () ;
  1242. BEGIN Ebyte (8AH) ; Ebyte (04H) END EmLdAlSi ; (* 8A 04: AL := [SI] *)
  1243. PROCEDURE EmIncSi () ;
  1244. BEGIN Ebyte (46H) END EmIncSi ;
  1245. PROCEDURE EmIncDi () ;
  1246. BEGIN Ebyte (47H) END EmIncDi ;
  1247. PROCEDURE EmMovDiAx () ;
  1248. BEGIN Ebyte (89H) ; Ebyte (0C7H) END EmMovDiAx ; (* 89 C7: DI := AX *)
  1249. PROCEDURE EmMovAxDi () ;
  1250. BEGIN Ebyte (89H) ; Ebyte (0F8H) END EmMovAxDi ; (* 89 F8: AX := DI *)
  1251. PROCEDURE EmLdAxDi () ;
  1252. BEGIN Ebyte (8BH) ; Ebyte (05H) END EmLdAxDi ; (* 8B 05: AX := [DI] *)
  1253. PROCEDURE EmStDiAl () ;
  1254. BEGIN Ebyte (88H) ; Ebyte (05H) END EmStDiAl ; (* 88 05: [DI] := AL *)
  1255. PROCEDURE EmPushDi () ;
  1256. BEGIN Ebyte (57H) END EmPushDi ; (* PUSH DI *)
  1257. PROCEDURE EmPopDi () ;
  1258. BEGIN Ebyte (5FH) END EmPopDi ; (* POP DI *)
  1259. PROCEDURE EmLoadVarInd (off, nbytes : CARDINAL) ;
  1260. (* AX := the value of a VAR parameter. [BP+off] holds the ADDRESS; BX carries
  1261. it so the byte/word load has a base register. The address is a DS offset:
  1262. a .COM has one segment, which is why no segment register appears here. *)
  1263. BEGIN
  1264. EmLoadVar (TRUE, off, 2) ; (* AX := [BP+off], the address *)
  1265. Ebyte (89H) ; Ebyte (0C3H) ; (* MOV BX,AX *)
  1266. IF nbytes = 1 THEN
  1267. Ebyte (8AH) ; Ebyte (07H) ; (* MOV AL,[BX] *)
  1268. EmMovAh0 ()
  1269. ELSE
  1270. Ebyte (8BH) ; Ebyte (07H) (* MOV AX,[BX] *)
  1271. END
  1272. END EmLoadVarInd ;
  1273. PROCEDURE EmStoreVarIndPrep (off : CARDINAL) ;
  1274. (* DI := the address of a VAR parameter, then PUSH DI. The push protects it
  1275. across the parse of the right-hand side, which may use DI for a string
  1276. element of its own. EmStoreInd finishes the store after POP DI. *)
  1277. BEGIN
  1278. Ebyte (8BH) ; EmBpDisp (off, 7) ; (* MOV DI,[BP+off] *)
  1279. EmPushDi ()
  1280. END EmStoreVarIndPrep ;
  1281. PROCEDURE EmStDiImm (off, v : CARDINAL) ;
  1282. (* MOV byte [DI+off],imm8: one byte of a set constructor stored through a
  1283. DI-addressed record field. Sets are at most 32 bytes, so disp8 is enough. *)
  1284. BEGIN
  1285. Ebyte (0C6H) ; Ebyte (45H) ; Ebyte (VAL (BYTE, off)) ; Ebyte (VAL (BYTE, v))
  1286. END EmStDiImm ;
  1287. PROCEDURE EmStDiAlOff (off : CARDINAL) ;
  1288. (* MOV [DI+off],AL: one byte of a real copied through a DI-addressed field. *)
  1289. BEGIN
  1290. Ebyte (88H) ; Ebyte (45H) ; Ebyte (VAL (BYTE, off))
  1291. END EmStDiAlOff ;
  1292. PROCEDURE EmLdAlDiOff (off : CARDINAL) ;
  1293. (* MOV AL,[DI+off]: the byte of a set variable that holds an element's bit. *)
  1294. BEGIN
  1295. Ebyte (8AH) ; Ebyte (45H) ; Ebyte (VAL (BYTE, off))
  1296. END EmLdAlDiOff ;
  1297. PROCEDURE EmCmpAlImm (v : CARDINAL) ;
  1298. BEGIN Ebyte (3CH) ; Ebyte (VAL (BYTE, v)) END EmCmpAlImm ; (* CMP AL,imm8 *)
  1299. PROCEDURE EmSubAlImm (v : CARDINAL) ;
  1300. BEGIN Ebyte (2CH) ; Ebyte (VAL (BYTE, v)) END EmSubAlImm ; (* SUB AL,imm8 *)
  1301. PROCEDURE EmMovClAl () ;
  1302. BEGIN Ebyte (88H) ; Ebyte (0C1H) END EmMovClAl ; (* MOV CL,AL *)
  1303. PROCEDURE EmAndClImm (v : CARDINAL) ;
  1304. BEGIN Ebyte (80H) ; Ebyte (0E1H) ; Ebyte (VAL (BYTE, v)) END EmAndClImm ;
  1305. PROCEDURE EmShrAx1 () ;
  1306. BEGIN Ebyte (0D1H) ; Ebyte (0E8H) END EmShrAx1 ; (* SHR AX,1 *)
  1307. PROCEDURE EmMovBxi (v : CARDINAL) ;
  1308. BEGIN Ebyte (0BBH) ; Eword (v MOD 10000H) END EmMovBxi ; (* MOV BX,imm16 *)
  1309. PROCEDURE EmLeaBx (off : CARDINAL) ;
  1310. (* LEA BX,[BP+off]: the address of a local set. *)
  1311. BEGIN
  1312. Ebyte (8DH) ; EmBpDisp (off, 3)
  1313. END EmLeaBx ;
  1314. PROCEDURE EmMovBxDi () ;
  1315. BEGIN Ebyte (89H) ; Ebyte (0FBH) END EmMovBxDi ; (* MOV BX,DI *)
  1316. PROCEDURE EmAddBxAx () ;
  1317. BEGIN Ebyte (01H) ; Ebyte (0C3H) END EmAddBxAx ; (* ADD BX,AX *)
  1318. PROCEDURE EmLdAlBx () ;
  1319. BEGIN Ebyte (8AH) ; Ebyte (07H) END EmLdAlBx ; (* MOV AL,[BX] *)
  1320. PROCEDURE EmMovDlImm (v : CARDINAL) ;
  1321. BEGIN Ebyte (0B2H) ; Ebyte (VAL (BYTE, v)) END EmMovDlImm ;
  1322. PROCEDURE EmShlDlCl () ;
  1323. BEGIN Ebyte (0D2H) ; Ebyte (0E2H) END EmShlDlCl ; (* SHL DL,CL *)
  1324. PROCEDURE EmAndAlDl () ;
  1325. BEGIN Ebyte (20H) ; Ebyte (0D0H) END EmAndAlDl ; (* AND AL,DL *)
  1326. PROCEDURE EmAndAlImm (v : CARDINAL) ;
  1327. BEGIN Ebyte (24H) ; Ebyte (VAL (BYTE, v)) END EmAndAlImm ; (* AND AL,imm8 *)
  1328. PROCEDURE EmStoreInd (nbytes : CARDINAL) ;
  1329. (* [DI] := AL/WORD, after POP DI - the companion of EmStoreVarIndPrep. *)
  1330. BEGIN
  1331. IF nbytes = 1 THEN
  1332. EmStDiAl () (* MOV [DI],AL *)
  1333. ELSE
  1334. Ebyte (89H) ; Ebyte (05H) (* MOV [DI],AX *)
  1335. END
  1336. END EmStoreInd ;
  1337. PROCEDURE EmFileIo (reading : BOOLEAN ; fileLocal : BOOLEAN ; fileOff : CARDINAL ;
  1338. addrKind : CARDINAL ; addrLocal : BOOLEAN ;
  1339. addrOff, count : CARDINAL ; stringArg : BOOLEAN) ;
  1340. (* Typed-file block I/O: read/write `count' bytes at the data argument to the
  1341. open file in the file variable.
  1342. ABI to DOS: BX = handle, CX = byte count, DS:DX = buffer, AH = 3Fh read /
  1343. 40h write. The handle is the first word of the file control block. For a
  1344. DI-addressed record (`Sheet[I,J]') the address is already in DI; a plain
  1345. variable's address is formed by LEA. The DOS result is handed to the
  1346. io_store runtime entry, so IOResult sees it.
  1347. This is emitted directly rather than through a runtime entry because the
  1348. runtime has only ~45 bytes of probe headroom left; the code is two words
  1349. per call site and the compiler's code budget is the loose one. *)
  1350. BEGIN
  1351. IF addrKind = 5 THEN
  1352. (* DI already holds a kind-5 element's address. *)
  1353. ELSIF addrKind = 4 THEN
  1354. EmLeaDi (FALSE, addrOff) (* a code-resident blob *)
  1355. ELSIF addrKind = 1 THEN
  1356. EmLeaDi (addrLocal, addrOff)
  1357. ELSE
  1358. Err (ETypeErr) ;
  1359. RETURN
  1360. END ;
  1361. Ebyte (89H) ; Ebyte (0FAH) ; (* MOV DX,DI *)
  1362. IF stringArg THEN
  1363. (* DX points at the characters, CX = the length byte at [DI]. *)
  1364. Ebyte (42H) ; (* INC DX *)
  1365. EmLdAlDi () ;
  1366. EmMovClAl () ;
  1367. Ebyte (30H) ; Ebyte (0EDH) (* XOR CH,CH *)
  1368. ELSE
  1369. EmMovCxi (count)
  1370. END ;
  1371. EmLoadVar (fileLocal, fileOff, 2) ; (* AX := the DOS handle *)
  1372. Ebyte (89H) ; Ebyte (0C3H) ; (* MOV BX,AX *)
  1373. Ebyte (0B4H) ; (* MOV AH,3Fh/40h *)
  1374. IF reading THEN Ebyte (3FH) ELSE Ebyte (40H) END ;
  1375. Ebyte (0CDH) ; Ebyte (21H) ; (* INT 21h *)
  1376. DropC (EmCall (TU_IoStore))
  1377. END EmFileIo ;
  1378. PROCEDURE EmInlineCh (ch : CARDINAL) : CARDINAL ;
  1379. (* Emit a one-character string literal behind a JMP and return the image
  1380. offset of its length byte. Used by text-file Write(File, ' '). *)
  1381. VAR here : CARDINAL ;
  1382. BEGIN
  1383. Ebyte (0EBH) ; Ebyte (2) ;
  1384. here := pc ;
  1385. Ebyte (1) ; Ebyte (VAL (BYTE, ch)) ;
  1386. RETURN here
  1387. END EmInlineCh ;
  1388. PROCEDURE EmInlineCrLf () : CARDINAL ;
  1389. (* The CR LF pair every text-file Writeln emits. *)
  1390. VAR here : CARDINAL ;
  1391. BEGIN
  1392. Ebyte (0EBH) ; Ebyte (3) ;
  1393. here := pc ;
  1394. Ebyte (2) ; Ebyte (13) ; Ebyte (10) ;
  1395. RETURN here
  1396. END EmInlineCrLf ;
  1397. PROCEDURE EmSubSp (n : CARDINAL) ;
  1398. BEGIN
  1399. Ebyte (81H) ; Ebyte (0ECH) ; Eword (n MOD 10000H)
  1400. END EmSubSp ;
  1401. PROCEDURE EmAddSp (n : CARDINAL) ;
  1402. BEGIN
  1403. IF n <= 126 THEN
  1404. Ebyte (83H) ; Ebyte (0C4H) ; Ebyte (VAL (BYTE, n))
  1405. ELSE
  1406. Ebyte (81H) ; Ebyte (0C4H) ; Eword (n MOD 10000H)
  1407. END
  1408. END EmAddSp ;
  1409. PROCEDURE EmPushBp () ;
  1410. BEGIN
  1411. Ebyte (55H)
  1412. END EmPushBp ;
  1413. PROCEDURE EmLeave () ;
  1414. BEGIN
  1415. Ebyte (0C9H)
  1416. END EmLeave ;
  1417. PROCEDURE EmRet () ;
  1418. BEGIN
  1419. Ebyte (0C3H)
  1420. END EmRet ;
  1421. (* ---------------------------------------------------------------- *)
  1422. (* symbol table *)
  1423. (* ---------------------------------------------------------------- *)
  1424. PROCEDURE NameEq (a : ARRAY OF CHAR ; b : ARRAY OF CHAR) : BOOLEAN ;
  1425. VAR i : CARDINAL ;
  1426. BEGIN
  1427. i := 0 ;
  1428. LOOP
  1429. IF i > HIGH (a) THEN
  1430. RETURN FALSE
  1431. END ;
  1432. IF i > HIGH (b) THEN
  1433. RETURN FALSE
  1434. END ;
  1435. IF a [i] # b [i] THEN
  1436. RETURN FALSE
  1437. END ;
  1438. IF a [i] = 0C THEN
  1439. RETURN TRUE
  1440. END ;
  1441. INC (i)
  1442. END
  1443. END NameEq ;
  1444. PROCEDURE CopyStr (VAR dst : ARRAY OF CHAR ; src : ARRAY OF CHAR) ;
  1445. VAR i : CARDINAL ;
  1446. BEGIN
  1447. i := 0 ;
  1448. LOOP
  1449. IF i > HIGH (dst) THEN
  1450. dst [HIGH (dst)] := 0C ;
  1451. RETURN
  1452. END ;
  1453. IF i > HIGH (src) THEN
  1454. dst [i] := 0C ;
  1455. RETURN
  1456. END ;
  1457. dst [i] := src [i] ;
  1458. IF src [i] = 0C THEN
  1459. RETURN
  1460. END ;
  1461. INC (i)
  1462. END
  1463. END CopyStr ;
  1464. PROCEDURE CopyWord (name : ARRAY OF CHAR) ;
  1465. (* stash current word into global wrd (uppercased) *)
  1466. VAR i : CARDINAL ;
  1467. BEGIN
  1468. i := 0 ;
  1469. LOOP
  1470. IF i > HIGH (name) THEN
  1471. wrd [i] := 0C ;
  1472. RETURN
  1473. END ;
  1474. IF i > MaxName THEN
  1475. wrd [MaxName] := 0C ;
  1476. RETURN
  1477. END ;
  1478. IF name [i] = 0C THEN
  1479. wrd [i] := 0C ;
  1480. RETURN
  1481. END ;
  1482. wrd [i] := Upper (name [i]) ;
  1483. INC (i)
  1484. END
  1485. END CopyWord ;
  1486. PROCEDURE SaveWord (VAR dst : ARRAY OF CHAR) ;
  1487. (* copy wrd into dst *)
  1488. VAR i : CARDINAL ;
  1489. BEGIN
  1490. i := 0 ;
  1491. LOOP
  1492. IF i > HIGH (dst) THEN
  1493. dst [HIGH (dst)] := 0C ;
  1494. RETURN
  1495. END ;
  1496. IF i > MaxName THEN
  1497. dst [MaxName] := 0C ;
  1498. RETURN
  1499. END ;
  1500. dst [i] := wrd [i] ;
  1501. IF wrd [i] = 0C THEN
  1502. RETURN
  1503. END ;
  1504. INC (i)
  1505. END
  1506. END SaveWord ;
  1507. PROCEDURE NumToName (n : CARDINAL ; VAR dst : ARRAY OF CHAR) ;
  1508. VAR buf : ARRAY [0..9] OF CHAR ;
  1509. i, j : CARDINAL ;
  1510. BEGIN
  1511. IF n = 0 THEN
  1512. dst [0] := '0' ;
  1513. dst [1] := 0C ;
  1514. RETURN
  1515. END ;
  1516. i := 0 ;
  1517. WHILE n > 0 DO
  1518. IF i <= 9 THEN
  1519. buf [i] := CHR (ORD ('0') + (n MOD 10)) ;
  1520. INC (i)
  1521. END ;
  1522. n := n DIV 10
  1523. END ;
  1524. j := 0 ;
  1525. WHILE i > 0 DO
  1526. DEC (i) ;
  1527. IF j <= HIGH (dst) THEN
  1528. dst [j] := buf [i] ;
  1529. INC (j)
  1530. END
  1531. END ;
  1532. IF j <= HIGH (dst) THEN
  1533. dst [j] := 0C
  1534. END
  1535. END NumToName ;
  1536. PROCEDURE NewSym (name : ARRAY OF CHAR ; tag : CARDINAL ;
  1537. cls, size, elem, off : CARDINAL ; v : LONGINT ;
  1538. local : BOOLEAN) : CARDINAL ;
  1539. VAR e : SymEntry ;
  1540. BEGIN
  1541. IF symTop >= MaxSym THEN
  1542. Err (ECompOvf) ;
  1543. RETURN 0
  1544. END ;
  1545. CopyWord (name) ;
  1546. CopyStr (e.name, wrd) ;
  1547. e.tag := tag ;
  1548. e.cls := cls ;
  1549. e.size := size ;
  1550. e.elem := elem ;
  1551. e.off := off ;
  1552. e.lval := v ;
  1553. e.lo := 0 ;
  1554. e.hi := 0 ;
  1555. e.fldFirst := 0 ;
  1556. e.fldLast := 0 ;
  1557. e.arrIdx := 0 ;
  1558. e.parmFirst := 0 ;
  1559. e.parmCount := 0 ;
  1560. e.isKbd := FALSE ;
  1561. e.isVar := FALSE ;
  1562. e.level := lexnest ;
  1563. e.local := local ;
  1564. e.resvar := 0 ;
  1565. e.goPos := 0 ;
  1566. e.defnd := FALSE ;
  1567. e.fwd := FALSE ;
  1568. symtab [symTop] := e ;
  1569. INC (symTop) ;
  1570. RETURN symTop - 1
  1571. END NewSym ;
  1572. PROCEDURE Search (nm : ARRAY OF CHAR ; VAR idx : CARDINAL) : BOOLEAN ;
  1573. (* find nm among symbols visible at the current lexical level *)
  1574. VAR p : CARDINAL ;
  1575. BEGIN
  1576. CopyWord (nm) ;
  1577. p := symTop ;
  1578. WHILE p > 0 DO
  1579. DEC (p) ;
  1580. IF symtab [p].level <= lexnest THEN
  1581. IF NameEq (symtab [p].name, wrd) THEN
  1582. idx := p ;
  1583. RETURN TRUE
  1584. END
  1585. END
  1586. END ;
  1587. RETURN FALSE
  1588. END Search ;
  1589. PROCEDURE DupTest (nm : ARRAY OF CHAR) ;
  1590. (* A name may be declared twice only in the SAME scope. This used to reject
  1591. any name Search could see, so a parameter could not shadow a global - legal
  1592. ISO Pascal, and CALC.INC's `procedure IBMCh(var Ch: Char)' does exactly
  1593. that (`Ch' is a global). Same-level duplicates stay errors: two parameters
  1594. in one list, two locals, or two globals. *)
  1595. VAR i : CARDINAL ;
  1596. BEGIN
  1597. IF Search (nm, i) THEN
  1598. IF symtab [i].level = lexnest THEN
  1599. Err (EUnknown)
  1600. END
  1601. END
  1602. END DupTest ;
  1603. PROCEDURE HideLocals (from : CARDINAL) ;
  1604. (* Make every symbol from index `from' up invisible to everything outside the
  1605. procedure that declared it.
  1606. Search accepts a symbol when its level is <= lexnest, and every procedure
  1607. body is compiled at the same depth (lexnest 1), so a finished procedure's
  1608. parameters stayed visible to the NEXT procedure: a second `a : integer'
  1609. was a duplicate (err 41) and an unqualified `a' inside procedure two read
  1610. procedure one's argument. Level 0FFFFH fails `level <= lexnest' at every
  1611. depth a later procedure can be at, and by the time this runs the body that
  1612. could still legitimately see them is finished.
  1613. The symbols are relabelled, not popped: symtab[old].resvar holds an INDEX,
  1614. and a function's result variable is one of the entries being hidden. *)
  1615. VAR p : CARDINAL ;
  1616. BEGIN
  1617. p := from ;
  1618. WHILE p < symTop DO
  1619. symtab [p].level := 0FFFFH ;
  1620. INC (p)
  1621. END
  1622. END HideLocals ;
  1623. (* ---------------------------------------------------------------- *)
  1624. (* lexer *)
  1625. (* ---------------------------------------------------------------- *)
  1626. PROCEDURE InitKeys () ;
  1627. VAR i : CARDINAL ;
  1628. BEGIN
  1629. FOR i := 0 TO 42 DO
  1630. kTk [i] := 0 ;
  1631. kName [i] [0] := 0C
  1632. END ;
  1633. kName [1] := "PROGRAM" ; kTk [1] := TkProgram ;
  1634. kName [2] := "BEGIN" ; kTk [2] := TkBegin ;
  1635. kName [3] := "END" ; kTk [3] := TkEnd ;
  1636. kName [4] := "IF" ; kTk [4] := TkIf ;
  1637. kName [5] := "THEN" ; kTk [5] := TkThen ;
  1638. kName [6] := "ELSE" ; kTk [6] := TkElse ;
  1639. kName [7] := "WHILE" ; kTk [7] := TkWhile ;
  1640. kName [8] := "DO" ; kTk [8] := TkDo ;
  1641. kName [9] := "REPEAT" ; kTk [9] := TkRepeat ;
  1642. kName [10] := "UNTIL" ; kTk [10] := TkUntil ;
  1643. kName [11] := "FOR" ; kTk [11] := TkFor ;
  1644. kName [12] := "TO" ; kTk [12] := TkTo ;
  1645. kName [13] := "DOWNTO" ; kTk [13] := TkDownto ;
  1646. kName [14] := "CASE" ; kTk [14] := TkCase ;
  1647. kName [15] := "OF" ; kTk [15] := TkOf ;
  1648. kName [16] := "GOTO" ; kTk [16] := TkGoto ;
  1649. kName [17] := "EXIT" ; kTk [17] := TkExit ;
  1650. kName [18] := "WITH" ; kTk [18] := TkWith ;
  1651. kName [19] := "VAR" ; kTk [19] := TkVar ;
  1652. kName [20] := "CONST" ; kTk [20] := TkConst ;
  1653. kName [21] := "TYPE" ; kTk [21] := TkType ;
  1654. kName [22] := "LABEL" ; kTk [22] := TkLabel ;
  1655. kName [23] := "PROCEDURE" ; kTk [23] := TkProcedure ;
  1656. kName [24] := "FUNCTION" ; kTk [24] := TkFunction ;
  1657. kName [25] := "NIL" ; kTk [25] := TkNil ;
  1658. kName [26] := "AND" ; kTk [26] := TkAnd ;
  1659. kName [27] := "OR" ; kTk [27] := TkOr ;
  1660. kName [28] := "NOT" ; kTk [28] := TkNot ;
  1661. kName [29] := "DIV" ; kTk [29] := TkDiv ;
  1662. kName [30] := "MOD" ; kTk [30] := TkMod ;
  1663. kName [31] := "IN" ; kTk [31] := TkIn ;
  1664. kName [32] := "FILE" ; kTk [32] := TkFile ;
  1665. kName [33] := "TEXT" ; kTk [33] := TkText ;
  1666. kName [34] := "RECORD" ; kTk [34] := TkRecord ;
  1667. kName [35] := "ARRAY" ; kTk [35] := TkArray ;
  1668. kName [36] := "SET" ; kTk [36] := TkSet ;
  1669. kName [37] := "PACKED" ; kTk [37] := TkPacked ;
  1670. kName [38] := "FORWARD" ; kTk [38] := TkForward ;
  1671. kName [39] := "EXTERNAL" ; kTk [39] := TkExternal ;
  1672. kName [40] := "ABSOLUTE" ; kTk [40] := TkAbsolute ;
  1673. kName [41] := "OVERLAY" ; kTk [41] := TkOverlay ;
  1674. kName [42] := "STRING" ; kTk [42] := TkString
  1675. END InitKeys ;
  1676. PROCEDURE IsBlank (ch : CHAR) : BOOLEAN ;
  1677. BEGIN
  1678. RETURN (ch = ' ') OR (ORD (ch) = 09H) OR (ORD (ch) = 0DH) OR (ORD (ch) = 0AH) OR (ORD (ch) = 0CH)
  1679. END IsBlank ;
  1680. PROCEDURE Skip () ;
  1681. (* blanks, comments { } and (* *), compiler directives {$ } / (*$ *)
  1682. letter + sign toggles rng/chk. Include directives are already gone by the
  1683. time Skip runs: ExpandIncludes splices them into the buffer first, so the
  1684. {$I file} spelling never reaches here. {$I+}/{$I-} still do, and stay the
  1685. I/O-check toggle, exactly as TPSRC10 cdirec splits them on the sign. *)
  1686. VAR ch : CHAR ;
  1687. letter : CHAR ;
  1688. BEGIN
  1689. WHILE NOT abortFac DO
  1690. WHILE IsBlank (CurCh ()) DO
  1691. ch := GetCh ()
  1692. END ;
  1693. IF CurCh () = '{' THEN
  1694. ch := GetCh () ;
  1695. IF CurCh () = '$' THEN
  1696. ch := GetCh () ;
  1697. letter := Upper (GetCh ()) ;
  1698. IF CurCh () = '+' THEN
  1699. ch := GetCh () ;
  1700. IF letter = 'I' THEN dirs.chk := TRUE END ;
  1701. IF letter = 'R' THEN dirs.rng := TRUE END
  1702. ELSIF CurCh () = '-' THEN
  1703. ch := GetCh () ;
  1704. IF letter = 'I' THEN dirs.chk := FALSE END ;
  1705. IF letter = 'R' THEN dirs.rng := FALSE END
  1706. END
  1707. END ;
  1708. WHILE (CurCh () # '}') AND (CurCh () # 0C) DO
  1709. ch := GetCh ()
  1710. END ;
  1711. IF CurCh () = '}' THEN
  1712. ch := GetCh ()
  1713. END
  1714. ELSIF (CurCh () = '(') AND (PeekAhead (1) = '*') THEN
  1715. ch := GetCh () ;
  1716. ch := GetCh () ;
  1717. IF CurCh () = '$' THEN
  1718. ch := GetCh () ;
  1719. letter := Upper (GetCh ()) ;
  1720. IF CurCh () = '+' THEN
  1721. ch := GetCh () ;
  1722. IF letter = 'I' THEN dirs.chk := TRUE END ;
  1723. IF letter = 'R' THEN dirs.rng := TRUE END
  1724. ELSIF CurCh () = '-' THEN
  1725. ch := GetCh () ;
  1726. IF letter = 'I' THEN dirs.chk := FALSE END ;
  1727. IF letter = 'R' THEN dirs.rng := FALSE END
  1728. END
  1729. END ;
  1730. LOOP
  1731. IF (CurCh () = '*') AND (PeekAhead (1) = ')') THEN
  1732. ch := GetCh () ;
  1733. ch := GetCh () ;
  1734. EXIT
  1735. END ;
  1736. IF CurCh () = 0C THEN
  1737. EXIT
  1738. END ;
  1739. ch := GetCh ()
  1740. END
  1741. ELSE
  1742. RETURN
  1743. END
  1744. END
  1745. END Skip ;
  1746. PROCEDURE GetWord () ;
  1747. (* read identifier into wrd (uppercased); next char must be alpha *)
  1748. VAR i : CARDINAL ;
  1749. ch : CHAR ;
  1750. BEGIN
  1751. i := 0 ;
  1752. ch := GetCh () ;
  1753. LOOP
  1754. IF i > MaxName THEN
  1755. wrd [MaxName] := 0C ;
  1756. RETURN
  1757. END ;
  1758. wrd [i] := Upper (ch) ;
  1759. INC (i) ;
  1760. ch := CurCh () ;
  1761. IF NOT AlphaNum (ch) THEN
  1762. wrd [i] := 0C ;
  1763. RETURN
  1764. END ;
  1765. ch := GetCh ()
  1766. END
  1767. END GetWord ;
  1768. PROCEDURE WddTok () : CARDINAL ;
  1769. (* map wrd -> keyword token *)
  1770. VAR i : CARDINAL ;
  1771. BEGIN
  1772. i := 1 ;
  1773. WHILE i <= 42 DO
  1774. IF kName [i] [0] # 0C THEN
  1775. IF NameEq (wrd, kName [i]) THEN
  1776. RETURN kTk [i]
  1777. END
  1778. END ;
  1779. INC (i)
  1780. END ;
  1781. RETURN TkNone
  1782. END WddTok ;
  1783. PROCEDURE DeclaresProc () : BOOLEAN ;
  1784. (* Does the REST of the source declare a PROCEDURE or a FUNCTION?
  1785. Needed because the declaration part is compiled BEFORE the main statement
  1786. part, so a procedure's code lands between the program prologue and the
  1787. main body - and nothing jumps over it. A program with a procedure
  1788. therefore ran off the end of the prologue, straight into the first
  1789. procedure, which read its argument out of an uninitialised frame and
  1790. returned to address 0. Every Pascal program containing a procedure was
  1791. broken; `t13_proc` compiled and was never executed, so nothing saw it.
  1792. The jump that fixes it has to be emitted BEFORE the declaration part, but
  1793. whether one is needed is only known AFTER - so the only honest options are
  1794. to emit it unconditionally (3 dead bytes in every program, and every code
  1795. size in expected.tsv moves) or to know the answer in advance. This is the
  1796. second: it scans ahead and puts srcPos back.
  1797. That is safe because the whole program is already in `src` and `srcPos` is
  1798. a plain index into it - the same trick PeekKw and KwAhead use. The scan
  1799. looks for the keywords anywhere in the remainder rather than tracking the
  1800. nesting of `begin`s, which is deliberately loose: a program with no
  1801. procedures that merely mentions the word in a string literal would get a
  1802. 3-byte jump to the next instruction, which is harmless, whereas tracking
  1803. the main `begin` against a procedure's `begin` would be a second parser
  1804. to get wrong. *)
  1805. VAR save : CARDINAL ;
  1806. found : BOOLEAN ;
  1807. tk : CARDINAL ;
  1808. ch : CHAR ;
  1809. BEGIN
  1810. save := srcPos ;
  1811. found := FALSE ;
  1812. tk := TkNone ; (* so the answer is defined if src is empty *)
  1813. (* Step over delimiters as well as blanks. Stopping at the first
  1814. non-letter looked reasonable and was wrong: `var x : integer ;` is full
  1815. of ':' and ';', so the scan gave up inside the variable section and
  1816. never reached the PROCEDURE. The loop ends at the end of the source,
  1817. not at the first punctuation. *)
  1818. WHILE (NOT found) AND (srcPos < srcLen) DO
  1819. Skip () ;
  1820. IF Alpha (CurCh ()) THEN
  1821. GetWord () ;
  1822. tk := WddTok () ;
  1823. IF (tk = TkProcedure) OR (tk = TkFunction) THEN
  1824. found := TRUE
  1825. END
  1826. ELSE
  1827. ch := GetCh () (* a ':' or ';' - step over it *)
  1828. END
  1829. END ;
  1830. srcPos := save ;
  1831. RETURN (tk = TkProcedure) OR (tk = TkFunction)
  1832. END DeclaresProc ;
  1833. PROCEDURE KwAhead (word : ARRAY OF CHAR) : BOOLEAN ;
  1834. (* does the next token (past blanks/comments) equal the keyword 'word',
  1835. without consuming it? srcPos is saved and restored. *)
  1836. VAR save : CARDINAL ;
  1837. k : BOOLEAN ;
  1838. BEGIN
  1839. save := srcPos ;
  1840. Skip () ;
  1841. k := FALSE ;
  1842. IF Alpha (CurCh ()) THEN
  1843. GetWord () ;
  1844. k := NameEq (wrd, word)
  1845. END ;
  1846. srcPos := save ;
  1847. RETURN k
  1848. END KwAhead ;
  1849. PROCEDURE PeekKw (VAR tok : CARDINAL) ;
  1850. (* peek at the next keyword token without consuming it *)
  1851. VAR i : CARDINAL ;
  1852. BEGIN
  1853. tok := TkNone ;
  1854. i := 1 ;
  1855. WHILE i <= 42 DO
  1856. IF kName [i] [0] # 0C THEN
  1857. IF KwAhead (kName [i]) THEN
  1858. tok := kTk [i] ;
  1859. RETURN
  1860. END
  1861. END ;
  1862. INC (i)
  1863. END
  1864. END PeekKw ;
  1865. PROCEDURE MatchKey (VAR tok : CARDINAL) : BOOLEAN ;
  1866. (* skip; if next symbol is a word, read it into wrd and set its token.
  1867. Returns TRUE when a word was read (tok = TkNone for plain ids). *)
  1868. BEGIN
  1869. tok := TkNone ;
  1870. Skip () ;
  1871. IF NOT Alpha (CurCh ()) THEN
  1872. RETURN FALSE
  1873. END ;
  1874. GetWord () ;
  1875. tok := WddTok () ;
  1876. RETURN TRUE
  1877. END MatchKey ;
  1878. PROCEDURE MatchDelim (ch : CHAR) : BOOLEAN ;
  1879. BEGIN
  1880. Skip () ;
  1881. IF CurCh () = ch THEN
  1882. DropCh (GetCh ()) ;
  1883. RETURN TRUE
  1884. END ;
  1885. RETURN FALSE
  1886. END MatchDelim ;
  1887. PROCEDURE MatchAssign () : BOOLEAN ;
  1888. BEGIN
  1889. Skip () ;
  1890. IF (CurCh () = ':') AND (PeekAhead (1) = '=') THEN
  1891. DropCh (GetCh ()) ;
  1892. DropCh (GetCh ()) ;
  1893. RETURN TRUE
  1894. END ;
  1895. RETURN FALSE
  1896. END MatchAssign ;
  1897. PROCEDURE MatchRange () : BOOLEAN ;
  1898. BEGIN
  1899. Skip () ;
  1900. IF (CurCh () = '.') AND (PeekAhead (1) = '.') THEN
  1901. DropCh (GetCh ()) ;
  1902. DropCh (GetCh ()) ;
  1903. RETURN TRUE
  1904. END ;
  1905. RETURN FALSE
  1906. END MatchRange ;
  1907. PROCEDURE ExpectDelim (ch : CHAR ; n : CARDINAL) ;
  1908. BEGIN
  1909. Skip () ;
  1910. IF CurCh () = ch THEN
  1911. DropCh (GetCh ())
  1912. ELSE
  1913. Err (n)
  1914. END
  1915. END ExpectDelim ;
  1916. PROCEDURE HexVal (ch : CHAR) : CARDINAL ;
  1917. BEGIN
  1918. IF (ch >= '0') AND (ch <= '9') THEN
  1919. RETURN ORD (ch) - ORD ('0')
  1920. ELSIF (ch >= 'A') AND (ch <= 'F') THEN
  1921. RETURN ORD (ch) - ORD ('A') + 10
  1922. END ;
  1923. RETURN ORD (ch) - ORD ('a') + 10
  1924. END HexVal ;
  1925. PROCEDURE RdIntConst (VAR v : LONGINT) ;
  1926. (* bare integer constant; current char is digit or '$' *)
  1927. VAR acc : LONGINT ;
  1928. BEGIN
  1929. acc := 0 ;
  1930. IF CurCh () = '$' THEN
  1931. DropCh (GetCh ()) ;
  1932. WHILE IsHexCh (CurCh ()) DO
  1933. acc := acc * 16 + VAL (LONGINT, HexVal (CurCh ())) ;
  1934. DropCh (GetCh ())
  1935. END
  1936. ELSE
  1937. WHILE Digit (CurCh ()) DO
  1938. acc := acc * 10 + VAL (LONGINT, ORD (CurCh ()) - ORD ('0')) ;
  1939. DropCh (GetCh ())
  1940. END
  1941. END ;
  1942. v := acc
  1943. END RdIntConst ;
  1944. PROCEDURE StrNew (first : CARDINAL ; hasFirst : BOOLEAN ) : CARDINAL ;
  1945. (* Open a pool slot for a string literal, optionally pre-seeded with its
  1946. first character.
  1947. RdConst has to read the first character before it can tell a one-character
  1948. literal from a longer one - the "is the next character another quote?"
  1949. test only makes sense once something has been read - so the seeding has to
  1950. happen here and has to advance strTop. Leaving strTop alone and letting the
  1951. caller write the character by hand is a trap: strTop is the next FREE byte,
  1952. so the first StrPut lands on top of the seeded character and overwrites it.
  1953. (That bug shipped the literal 'hi' as 69 00 - 'i' then NUL.) *)
  1954. VAR x : CARDINAL ;
  1955. BEGIN
  1956. IF strCnt > HIGH (strOff) THEN
  1957. Err (ECompOvf) ; (* too many literals in one unit *)
  1958. RETURN 0
  1959. END ;
  1960. x := strCnt ;
  1961. strOff [x] := strTop ;
  1962. strLen [x] := 0 ;
  1963. INC (strCnt) ;
  1964. IF hasFirst THEN
  1965. strPool [strTop] := CHR (first) ;
  1966. INC (strTop) ;
  1967. strLen [x] := 1
  1968. END ;
  1969. rdStrX := x ;
  1970. RETURN x
  1971. END StrNew ;
  1972. PROCEDURE StrPut (x : CARDINAL ) ;
  1973. (* append the current source character to pool slot x *)
  1974. BEGIN
  1975. IF x > HIGH (strOff) THEN
  1976. RETURN
  1977. END ;
  1978. IF strTop > HIGH (strPool) THEN
  1979. Err (ECompOvf) ; (* literal longer than the pool *)
  1980. RETURN
  1981. END ;
  1982. strPool [strTop] := CurCh () ;
  1983. INC (strTop) ;
  1984. INC (strLen [x])
  1985. END StrPut ;
  1986. PROCEDURE RdConst (VAR v : LONGINT ; VAR cls : CARDINAL ;
  1987. VAR isStr : BOOLEAN) ;
  1988. (* scalar or string constant. A string literal's text is collected into the
  1989. pool and its slot index left in rdStrX; a single-character literal stays a
  1990. TScalar holding its character code, which is what "c := 'a'" wants. *)
  1991. CONST q = AposC ;
  1992. VAR ch : CHAR ;
  1993. idx : CARDINAL ;
  1994. BEGIN
  1995. isStr := FALSE ;
  1996. cls := TScalar ;
  1997. v := 0 ;
  1998. Skip () ;
  1999. IF CurCh () = '$' THEN
  2000. RdIntConst (v) ;
  2001. cls := TScalar
  2002. ELSIF Digit (CurCh ()) THEN
  2003. RdIntConst (v) ;
  2004. IF (CurCh () = '.') AND (Digit (PeekAhead (1))) THEN
  2005. cls := TReal ;
  2006. DropCh (GetCh ())
  2007. END ;
  2008. IF (CurCh () = 'E') OR (CurCh () = 'e') THEN
  2009. cls := TReal ;
  2010. DropCh (GetCh ())
  2011. END ;
  2012. IF cls = TReal THEN
  2013. WHILE AlphaNum (CurCh ()) OR (CurCh () = '.') OR (CurCh () = '-')
  2014. OR (CurCh () = '+') DO
  2015. DropCh (GetCh ())
  2016. END
  2017. END
  2018. ELSIF CurCh () = '^' THEN
  2019. (* TP3 caret character constant, unquoted: ^M is CR (13), ^[ is ESC
  2020. (27), ^@ is 0. The value is the upper-cased character minus 64, so
  2021. the +256-MOD-256 keeps ^@ and any control character from wrapping. *)
  2022. DropCh (GetCh ()) ;
  2023. IF CurCh () = 0C THEN
  2024. Err (EUnknown)
  2025. ELSE
  2026. ch := Upper (GetCh ()) ;
  2027. v := VAL (LONGINT, (ORD (ch) + 256 - 64) MOD 256) ;
  2028. cls := TScalar
  2029. END
  2030. ELSIF CurCh () = '#' THEN
  2031. (* TP3 decimal character constant: #27, #255. *)
  2032. DropCh (GetCh ()) ;
  2033. IF Digit (CurCh ()) THEN
  2034. RdIntConst (v) ;
  2035. cls := TScalar
  2036. ELSE
  2037. Err (EUnknown)
  2038. END
  2039. ELSIF Alpha (CurCh ()) THEN
  2040. (* TRUE / FALSE, or a NAMED constant - an enumerator (`[Constant,
  2041. Formula]'), or any earlier const. A constant expression may name a
  2042. constant, and set literals and `case' labels are full of enumerators. *)
  2043. GetWord () ;
  2044. IF NameEq (wrd, "TRUE") THEN
  2045. v := 1 ;
  2046. cls := TBool
  2047. ELSIF NameEq (wrd, "FALSE") THEN
  2048. v := 0 ;
  2049. cls := TBool
  2050. ELSIF Search (wrd, idx) AND (symtab [idx].tag = KConst) THEN
  2051. v := symtab [idx].lval ;
  2052. cls := symtab [idx].cls
  2053. ELSE
  2054. Err (EUnknown)
  2055. END
  2056. ELSIF ORD (CurCh ()) = q THEN
  2057. DropCh (GetCh ()) ;
  2058. IF ORD (CurCh ()) = q THEN
  2059. (* '' - the empty string. It used to be reported as the scalar 39,
  2060. so writeln('') printed a single quote mark. It is a string of
  2061. length zero, and an inline zero-length literal is exactly what the
  2062. runtime's JCXZ path is for. *)
  2063. DropCh (GetCh ()) ;
  2064. isStr := TRUE ;
  2065. cls := TString ;
  2066. v := 0 ;
  2067. rdStrX := StrNew (0, FALSE)
  2068. ELSIF (CurCh () = 0C) OR ((ORD (CurCh ()) = 0DH)) OR ((ORD (CurCh ()) = 0AH)) THEN
  2069. Err (EUnknown)
  2070. ELSE
  2071. v := VAL (LONGINT, ORD (GetCh ())) ;
  2072. cls := TScalar ;
  2073. IF ORD (CurCh ()) = q THEN
  2074. DropCh (GetCh ())
  2075. ELSE
  2076. isStr := TRUE ;
  2077. cls := TString ;
  2078. (* Scan to the closing quote. NB: the loop condition must test
  2079. the CURRENT character and only the current character. The
  2080. obvious-looking "while CurCh # quote" with a
  2081. "if PeekAhead(1) = quote then consume two" body is wrong:
  2082. consuming the quote moves the cursor past it, so the next
  2083. condition test sees the character AFTER the literal, is
  2084. satisfied, and the scan runs on to end-of-buffer - which
  2085. silently eats the rest of the program and makes every later
  2086. error point at end-of-file. Stop on the quote itself, and
  2087. treat a doubled quote as one embedded quote character.
  2088. The first character is already gone - it was read into v above -
  2089. so the pool slot is pre-seeded with it. *)
  2090. rdStrX := StrNew (VAL (CARDINAL, v), TRUE) ;
  2091. LOOP
  2092. IF ORD (CurCh ()) = q THEN
  2093. IF ORD (PeekAhead (1)) = q THEN
  2094. StrPut (rdStrX) ; (* '' inside *)
  2095. DropCh (GetCh ()) ; DropCh (GetCh ())
  2096. ELSE
  2097. EXIT (* closing quote *)
  2098. END
  2099. ELSIF (CurCh () = 0C) OR (ORD (CurCh ()) = 0DH) THEN
  2100. Err (EUnknown) ; (* unterminated *)
  2101. EXIT
  2102. ELSE
  2103. StrPut (rdStrX) ;
  2104. DropCh (GetCh ())
  2105. END
  2106. END ;
  2107. IF ORD (CurCh ()) = q THEN
  2108. DropCh (GetCh ())
  2109. END
  2110. END
  2111. END
  2112. ELSE
  2113. Err (EUnknown)
  2114. END
  2115. END RdConst ;
  2116. (* ---------------------------------------------------------------- *)
  2117. (* forward declarations *)
  2118. (* ---------------------------------------------------------------- *)
  2119. PROCEDURE ParseExpr (VAR r : ERes) ; FORWARD ;
  2120. PROCEDURE ParseAdd (VAR r : ERes) ; FORWARD ;
  2121. PROCEDURE ParseMul (VAR r : ERes) ; FORWARD ;
  2122. PROCEDURE ParseNeg (VAR r : ERes) ; FORWARD ;
  2123. PROCEDURE ParseAtom (VAR r : ERes) ; FORWARD ;
  2124. PROCEDURE ParseType (VAR cls, size, elem : CARDINAL) ; FORWARD ;
  2125. PROCEDURE Statmnt () ; FORWARD ;
  2126. PROCEDURE ParseCall (idx : CARDINAL) ; FORWARD ;
  2127. PROCEDURE ParseCallArgs (idx : CARDINAL) ; FORWARD ;
  2128. PROCEDURE EmCallMost (idx, cleanup : CARDINAL) ; FORWARD ;
  2129. PROCEDURE SlotSize (cls, size : CARDINAL) : CARDINAL ; FORWARD ;
  2130. PROCEDURE ParamSlotBytes (e : SymEntry) : CARDINAL ; FORWARD ;
  2131. (* ---------------------------------------------------------------- *)
  2132. (* expressions (TPSRC9) *)
  2133. (* ---------------------------------------------------------------- *)
  2134. PROCEDURE VarSize (r : ERes) : CARDINAL ;
  2135. (* The width of a kind-1 value. An element (`b[i]') or a field (`r.f') has a
  2136. size of its own, which is NOT its variable's; when the parser has recorded
  2137. one, it wins. Otherwise the variable's declared size is the answer. *)
  2138. BEGIN
  2139. IF r.sz > 0 THEN
  2140. RETURN r.sz
  2141. END ;
  2142. RETURN symtab [r.idx].size
  2143. END VarSize ;
  2144. PROCEDURE LoadAtom (VAR r : ERes) ;
  2145. (* load value of r into AX (folding constants).
  2146. A string literal is refused here, and this is the single place that has to:
  2147. assignment, IF, WHILE, FOR, REPEAT, CASE, array subscripts and every
  2148. operator all reach their operand through LoadAtom, and none of them can use
  2149. a counted string where a 16-bit word is expected. Reporting it here rather
  2150. than in the parser means writeln('hi') still works - IoCall handles a
  2151. literal before it ever calls LoadAtom. *)
  2152. VAR emsz : CARDINAL ;
  2153. BEGIN
  2154. IF r.cls = TString THEN
  2155. Err (ENoLib) ; (* string value used as a number *)
  2156. r.kind := 2 ;
  2157. RETURN
  2158. END ;
  2159. IF r.cls = TSet THEN
  2160. (* A set is bytes, not a 16-bit value. It is legal as the right operand
  2161. of `in' (and later as a whole-value operand of `+'/`*'/'='/assignment),
  2162. all of which handle it BEFORE LoadAtom - but never as a number, which
  2163. would otherwise load its first two bytes as if it were an integer. *)
  2164. Err (ENoLib) ;
  2165. r.kind := 2 ;
  2166. RETURN
  2167. END ;
  2168. IF r.kind = 5 THEN
  2169. (* An element whose address ParseSub computed into DI (LEA DI,[base] /
  2170. ADD DI,AX): a string character, or an array element of one or two
  2171. bytes. Zeroing AH keeps a one-byte value a clean 16-bit char. The
  2172. read must happen HERE, before anything else is emitted: the caller
  2173. reaches this immediately after parsing the subscript, or - for a
  2174. binary operator - SaveLeft has already materialised it, so DI is
  2175. still the element's address. *)
  2176. IF VarSize (r) > 2 THEN
  2177. Err (ENoLib) ; (* a whole record/array as a value *)
  2178. r.kind := 2 ;
  2179. RETURN
  2180. END ;
  2181. IF VarSize (r) = 2 THEN
  2182. EmLdAxDi ()
  2183. ELSE
  2184. EmLdAlDi () ;
  2185. EmMovAh0 ()
  2186. END ;
  2187. r.kind := 2 ;
  2188. RETURN
  2189. END ;
  2190. IF r.kind = 0 THEN
  2191. EmMovAxi (W16 (r.imm)) ;
  2192. r.kind := 2
  2193. ELSIF r.kind = 1 THEN
  2194. IF symtab [r.idx].isVar THEN
  2195. (* A VAR parameter: [BP+off] holds the address, and the value is one
  2196. level down. r.boff is necessarily 0 - a by-reference parameter is
  2197. a scalar in this increment, so it has nothing to subscript. *)
  2198. IF r.boff # 0 THEN
  2199. Err (ENoLib) ;
  2200. r.kind := 2 ;
  2201. RETURN
  2202. END ;
  2203. EmLoadVarInd (symtab [r.idx].off, VarSize (r)) ;
  2204. r.kind := 2 ;
  2205. RETURN
  2206. END ;
  2207. IF (VarSize (r) > 2) AND NOT
  2208. ((r.cls = TChar) AND (symtab [r.idx].cls = TString)) THEN
  2209. Err (ENoLib)
  2210. ELSE
  2211. (* s[i] indexes one byte of a string, not the string, so its width is
  2212. one whatever the variable's declared width is. *)
  2213. emsz := VarSize (r) ;
  2214. IF (r.cls = TChar) AND (symtab [r.idx].cls = TString) THEN
  2215. emsz := 1
  2216. END ;
  2217. EmLoadVar (symtab [r.idx].local,
  2218. (symtab [r.idx].off + r.boff) MOD 10000H,
  2219. emsz) ;
  2220. IF emsz = 1 THEN
  2221. EmMovAh0 ()
  2222. END ;
  2223. r.kind := 2
  2224. END
  2225. END
  2226. END LoadAtom ;
  2227. PROCEDURE ParseSub (VAR r : ERes) ;
  2228. (* consume '[' expr ']' while present.
  2229. A CONSTANT index folds into the base offset boff, exactly as before. A
  2230. VARIABLE index on a STRING cannot fold - off+i is a run-time value - so it
  2231. is computed once here into DI and r.kind drops to 5, which is how LoadAtom,
  2232. Assignment and IoCall are told the element's address is in DI. Arrays
  2233. still accept only a constant index: a variable array subscript needs the
  2234. element-size scaling that a one-byte string element does not, and it is
  2235. item 2. *)
  2236. VAR t : ERes ;
  2237. ai, fi, nd, k, st, p : CARDINAL ;
  2238. coff : LONGINT ;
  2239. accOnStack, haveVar, more : BOOLEAN ;
  2240. baseDI, found : BOOLEAN ;
  2241. BEGIN
  2242. LOOP
  2243. Skip () ;
  2244. IF (r.kind = 1) AND (symtab [r.idx].cls = TPtr) AND
  2245. (CurCh () = '^') THEN
  2246. (* Dereference in a statement/assignment path, which reaches ParseSub
  2247. without going through ParseVar. The pointer word is the base
  2248. address; r.idx becomes the pointee TYPE symbol for array/record
  2249. metadata, exactly as in ParseVar. *)
  2250. DropCh (GetCh ()) ;
  2251. p := symtab [r.idx].elem ;
  2252. EmLoadVar (symtab [r.idx].local, symtab [r.idx].off, 2) ;
  2253. EmMovDiAx () ;
  2254. r.idx := p ;
  2255. r.kind := 5 ;
  2256. r.boff := 0 ;
  2257. r.cls := symtab [p].cls ;
  2258. r.sz := symtab [p].size
  2259. ELSIF CurCh () = '.' THEN
  2260. (* FIELD selection: `r.f'. The field's offset adds to the record
  2261. base's, and the value's class and WIDTH become the field's - which
  2262. is the whole reason a width rides on the expression (VarSize). *)
  2263. DropCh (GetCh ()) ;
  2264. Skip () ;
  2265. IF NOT Alpha (CurCh ()) THEN
  2266. Err (EUnknown) ;
  2267. RETURN
  2268. END ;
  2269. GetWord () ;
  2270. IF symtab [r.idx].fldFirst >= symtab [r.idx].fldLast THEN
  2271. Err (ESimpType) ; (* not a record *)
  2272. RETURN
  2273. END ;
  2274. fi := symtab [r.idx].fldFirst ;
  2275. found := FALSE ;
  2276. WHILE (fi < symtab [r.idx].fldLast) AND NOT found DO
  2277. IF NameEq (fields [fi].name, wrd) THEN
  2278. found := TRUE
  2279. ELSE
  2280. INC (fi)
  2281. END
  2282. END ;
  2283. IF NOT found THEN
  2284. Err (EUnknown) ;
  2285. RETURN
  2286. END ;
  2287. IF r.kind = 5 THEN
  2288. (* The element's address is in DI: a field is a fixed offset from
  2289. it. The array symbol already carries the element record's
  2290. field range (DefVar copies recFldFirst/recFldLast), which is
  2291. why the lookup above works for `Sheet[I,J].Field' too. *)
  2292. IF fields [fi].off # 0 THEN
  2293. EmAddDiImm (fields [fi].off)
  2294. END
  2295. ELSE
  2296. r.boff := W16 (VAL (LONGINT, r.boff)
  2297. + VAL (LONGINT, fields [fi].off)) ;
  2298. END ;
  2299. r.cls := fields [fi].cls ;
  2300. r.sz := fields [fi].size ;
  2301. IF r.cls = TSet THEN
  2302. (* ParseVar set setoff only when the SYMBOL itself was a set; for
  2303. an array/record the class was still TArray/TRecord then, so the
  2304. base offset has to be recorded now. *)
  2305. r.setoff := symtab [r.idx].off ;
  2306. r.setbase := fields [fi].elem ;
  2307. r.setsize := fields [fi].size
  2308. END
  2309. ELSIF CurCh () = '[' THEN
  2310. DropCh (GetCh ()) ;
  2311. IF symtab [r.idx].cls = TString THEN
  2312. (* s[i]: a STRING is a length byte at s[0] followed by the
  2313. characters, so index i selects the one byte at off+i. A second
  2314. subscript on a character is not a type. *)
  2315. ParseExpr (t) ;
  2316. IF OK () THEN
  2317. IF r.cls = TChar THEN
  2318. Err (ESimpType) ;
  2319. RETURN
  2320. END ;
  2321. IF symtab [r.idx].isVar THEN
  2322. (* VAR string parameter: the base is the address in the
  2323. parameter slot, not LEA of the slot. Compute DI once for
  2324. both constant and variable indices, so LoadAtom and
  2325. Assignment see the same kind-5 character address they do
  2326. for an ordinary string. *)
  2327. EmLoadVar (TRUE, symtab [r.idx].off, 2) ;
  2328. EmMovDiAx () ;
  2329. IF t.kind = 0 THEN
  2330. EmAddDiImm ((r.boff + W16 (t.imm)) MOD 10000H)
  2331. ELSE
  2332. IF r.boff # 0 THEN
  2333. EmAddDiImm (r.boff)
  2334. END ;
  2335. LoadAtom (t) ;
  2336. EmAddDiAx ()
  2337. END ;
  2338. r.kind := 5
  2339. ELSIF t.kind = 0 THEN
  2340. (* constant index: fold it into boff, as an array's does, and
  2341. r.cls drops to TChar so every consumer below - LoadAtom,
  2342. Assignment, IoCall - sees a one-byte value instead of the
  2343. whole string. *)
  2344. r.boff := W16 (VAL (LONGINT, r.boff) + t.imm)
  2345. ELSE
  2346. (* Variable index. Compute the element address once, now:
  2347. the index is evaluated into AX (LoadAtom is a no-op for a
  2348. kind-2 value and re-reads [DI] for a nested subscript), then
  2349. DI := base + AX. This is TP3's indstr/ialoop shape (TPSRC9
  2350. 1100-1111, 1200-1229), which likewise holds the address in
  2351. DI until the byte is loaded or stored; a register is what
  2352. lets a later LoadAtom read [DI] and a later assignment PUSH
  2353. DI across its right-hand side. *)
  2354. LoadAtom (t) ;
  2355. EmLeaDi (symtab [r.idx].local,
  2356. (symtab [r.idx].off + r.boff) MOD 10000H) ;
  2357. EmAddDiAx () ;
  2358. r.kind := 5
  2359. END ;
  2360. r.cls := TChar ;
  2361. r.sz := 1
  2362. END ;
  2363. ExpectDelim (']', ENoSemi)
  2364. ELSIF symtab [r.idx].cls = TArray THEN
  2365. (* An array subscript LIST: one expression per dimension, comma
  2366. separated. The byte offset is
  2367. sum over k of (i[k] - lo[k]) * stride[k]
  2368. where stride[k] is the element size times the count of every
  2369. LATER dimension (row-major: the LAST dimension varies fastest).
  2370. A constant index folds into coff; a run-time one is scaled as
  2371. soon as it is parsed and summed on the STACK, because parsing
  2372. the next index may clobber AX and DI. *)
  2373. IF symtab [r.idx].arrIdx = 0 THEN
  2374. Err (ESimpType) ;
  2375. RETURN
  2376. END ;
  2377. baseDI := (r.kind = 5) ;
  2378. ai := symtab [r.idx].arrIdx - 1 ;
  2379. nd := 0 ;
  2380. coff := 0 ;
  2381. accOnStack := FALSE ;
  2382. haveVar := FALSE ;
  2383. LOOP
  2384. IF nd >= arrs [ai].n THEN
  2385. Err (ETypeErr) ;
  2386. RETURN
  2387. END ;
  2388. ParseExpr (t) ;
  2389. IF NOT OK () THEN
  2390. RETURN
  2391. END ;
  2392. st := arrs [ai].elem ;
  2393. k := nd + 1 ;
  2394. WHILE k < arrs [ai].n DO
  2395. st := st * W16 (arrs [ai].hi [k] - arrs [ai].lo [k] + 1) ;
  2396. INC (k)
  2397. END ;
  2398. Skip () ;
  2399. more := CurCh () = ',' ;
  2400. IF t.kind = 0 THEN
  2401. coff := coff + (t.imm - arrs [ai].lo [nd])
  2402. * VAL (LONGINT, st)
  2403. ELSE
  2404. LoadAtom (t) ;
  2405. IF arrs [ai].lo [nd] # 0 THEN
  2406. EmMovCxi (W16 (arrs [ai].lo [nd])) ;
  2407. EmSubAxCx ()
  2408. END ;
  2409. IF st # 1 THEN
  2410. EmMovCxi (st) ;
  2411. EmMulAxCx ()
  2412. END ;
  2413. IF more THEN
  2414. (* the next dimension's expression will clobber AX: park
  2415. the running sum. A SINGLE index skips this entirely,
  2416. so 1-D code is byte-identical to the old path. *)
  2417. IF accOnStack THEN
  2418. EmPopCx () ;
  2419. EmAddAxCx () ;
  2420. EmPushAx ()
  2421. ELSE
  2422. EmPushAx () ;
  2423. accOnStack := TRUE
  2424. END
  2425. ELSE
  2426. (* last dimension: AX is the total *)
  2427. IF accOnStack THEN
  2428. EmPopCx () ;
  2429. EmAddAxCx () ;
  2430. accOnStack := FALSE
  2431. END ;
  2432. haveVar := TRUE
  2433. END
  2434. END ;
  2435. INC (nd) ;
  2436. IF more THEN
  2437. DropCh (GetCh ())
  2438. ELSE
  2439. EXIT
  2440. END
  2441. END ;
  2442. IF nd # arrs [ai].n THEN
  2443. (* fewer subscripts than dimensions: the value would be a
  2444. sub-array, which has no scalar representation *)
  2445. Err (ETypeErr) ;
  2446. RETURN
  2447. END ;
  2448. ExpectDelim (']', ENoSemi) ;
  2449. IF accOnStack THEN
  2450. (* the last dimensions were constant: the parked sum is still
  2451. on the stack, and AX is the total *)
  2452. EmPopAx () ;
  2453. accOnStack := FALSE
  2454. END ;
  2455. IF baseDI THEN
  2456. (* The base address is already in DI: a pointer dereference
  2457. (`Buffer^[i]'). Add the computed byte offset to it; a
  2458. constant-only index can use ADD DI,imm16. *)
  2459. IF haveVar THEN
  2460. EmAddDiAx ()
  2461. ELSE
  2462. IF coff # 0 THEN
  2463. EmAddDiImm (W16 (coff))
  2464. END
  2465. END ;
  2466. r.kind := 5
  2467. ELSIF haveVar THEN
  2468. EmLeaDi (symtab [r.idx].local,
  2469. (symtab [r.idx].off + r.boff
  2470. + W16 (coff)) MOD 10000H) ;
  2471. EmAddDiAx () ;
  2472. r.kind := 5
  2473. ELSE
  2474. r.boff := W16 (VAL (LONGINT, r.boff) + coff)
  2475. END ;
  2476. r.sz := arrs [ai].elem ;
  2477. r.cls := arrs [ai].ecls
  2478. ELSE
  2479. ParseExpr (t) ;
  2480. IF OK () THEN
  2481. IF t.kind # 0 THEN
  2482. Err (ENoLib)
  2483. ELSE
  2484. Err (ESimpType)
  2485. END
  2486. END ;
  2487. ExpectDelim (']', ENoSemi)
  2488. END
  2489. ELSE
  2490. RETURN
  2491. END
  2492. END
  2493. END ParseSub ;
  2494. PROCEDURE ParseVar (VAR r : ERes) ;
  2495. (* name [ '[' constExpr ']' ]* ; a function name maps to its result var *)
  2496. VAR idx, p : CARDINAL ;
  2497. BEGIN
  2498. IF NOT Search (wrd, idx) THEN
  2499. Err (EUnknown) ;
  2500. RETURN
  2501. END ;
  2502. IF symtab [idx].cls = TPtr THEN
  2503. Skip () ;
  2504. IF CurCh () = '^' THEN
  2505. (* Dereference: the pointer word is the base address. r.idx becomes
  2506. the pointee TYPE symbol, because ParseSub reads an array/record's
  2507. metadata from the symbol, not from the expression. *)
  2508. DropCh (GetCh ()) ;
  2509. p := symtab [idx].elem ;
  2510. EmLoadVar (symtab [idx].local, symtab [idx].off, 2) ;
  2511. EmMovDiAx () ;
  2512. r.idx := p ;
  2513. r.kind := 5 ;
  2514. r.boff := 0 ;
  2515. r.cls := symtab [p].cls ;
  2516. r.sz := symtab [p].size ;
  2517. ParseSub (r) ;
  2518. RETURN
  2519. END
  2520. END ;
  2521. r.idx := idx ;
  2522. r.kind := 1 ;
  2523. r.boff := 0 ;
  2524. r.cls := symtab [idx].cls ;
  2525. IF symtab [idx].tag = KFunc THEN
  2526. idx := symtab [idx].resvar ;
  2527. r.idx := idx ;
  2528. r.cls := symtab [idx].cls
  2529. ELSIF (symtab [idx].tag # KVar) AND (symtab [idx].tag # KType) THEN
  2530. Err (EUnknown) ;
  2531. RETURN
  2532. END ;
  2533. IF r.cls = TSet THEN
  2534. (* a set value: carry where its bytes are so ParseCmp can fold `in' and
  2535. (later) compare. A set VARIABLE is still refused at its declaration,
  2536. so today this is a typed constant or an inline literal. *)
  2537. r.setoff := symtab [r.idx].off ;
  2538. r.setbase := symtab [r.idx].elem ;
  2539. r.setsize := symtab [r.idx].size
  2540. END ;
  2541. r.sz := symtab [r.idx].size ;
  2542. ParseSub (r)
  2543. END ParseVar ;
  2544. PROCEDURE ConstAdd (a, b : LONGINT) : LONGINT ;
  2545. BEGIN
  2546. RETURN VAL (LONGINT, W16 (a + b))
  2547. END ConstAdd ;
  2548. PROCEDURE ConstSub (a, b : LONGINT) : LONGINT ;
  2549. BEGIN
  2550. RETURN VAL (LONGINT, W16 (a - b))
  2551. END ConstSub ;
  2552. PROCEDURE ConstMul (a, b : LONGINT) : LONGINT ;
  2553. BEGIN
  2554. RETURN VAL (LONGINT, W16 (a * b))
  2555. END ConstMul ;
  2556. PROCEDURE EmXchgAxDx () ;
  2557. (* 92h = XCHG AX,DX. Named for what it EMITS, which is the point of the
  2558. whole naming convention: this used to be called EmMoveAxDx, which is what
  2559. somebody would expect the opcode to be, and it is not - 89 D8 is
  2560. MOV AX,DX, 92h is the exchange. Here the exchange is what is wanted, so
  2561. the name is the only thing that was wrong, and it was wrong in the exact
  2562. way this file's names are not allowed to be: reading as "a move" when it
  2563. is a swap. After EmIDivAxCx the remainder is in DX and `mod` wants it in
  2564. AX; an exchange gets it there in one byte where a move also would, so the
  2565. behaviour is identical either way and only the name lied. *)
  2566. BEGIN
  2567. Ebyte (92H)
  2568. END EmXchgAxDx ;
  2569. PROCEDURE StrConcat (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ; FORWARD ;
  2570. PROCEDURE StrOperand (r : ERes) : BOOLEAN ; FORWARD ;
  2571. PROCEDURE BinOpEmit (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  2572. (* binary operation at one precedence level; folds constant operands *)
  2573. VAR f : LONGINT ;
  2574. okc : BOOLEAN ;
  2575. BEGIN
  2576. IF StrOperand (left) OR StrOperand (right) THEN
  2577. (* A string OPERAND - a STRING or a CHAR, which is a one-character string
  2578. for this purpose - makes this a string operation, and `+' is the only
  2579. one ISO Modula-2 defines: every other binary operator reached here
  2580. (OR, and the multiplicative and additive numeric ops) has no meaning
  2581. over a STRING, and StrConcat rejects it.
  2582. The test is StrOperand and NOT `cls = TString' because of the two
  2583. CHAR operands. `'a' + 'b'' has no STRING in it at all, so a
  2584. TString-only test let it fall through to the integer path, where the
  2585. character codes were added and CHR(195) was printed - a silently
  2586. wrong program rather than an error (see t53_strcat_type). A CHAR
  2587. operand is still a string operand, which is also what makes `a + ch'
  2588. concatenate.
  2589. Handling it here, at the head of the one routine every binary
  2590. operator funnels through, is what keeps `a + b', `writeln(a + b)',
  2591. `(a + b) = c' and `s := a + b + c' all going through the same
  2592. emission. *)
  2593. StrConcat (op, left, right, res) ;
  2594. RETURN
  2595. END ;
  2596. IF op = TkAnd THEN
  2597. IF (left.kind = 0) AND (right.kind = 0) THEN
  2598. res.kind := 0 ;
  2599. res.imm := BitAnd (left.imm, right.imm) ;
  2600. res.cls := TBool ;
  2601. RETURN
  2602. END ;
  2603. LoadPair (left, right) ;
  2604. EmAndAxCx () ;
  2605. res.kind := 2 ; res.cls := TBool ;
  2606. RETURN
  2607. END ;
  2608. IF op = TkOr THEN
  2609. IF (left.kind = 0) AND (right.kind = 0) THEN
  2610. res.kind := 0 ;
  2611. res.imm := BitOr (left.imm, right.imm) ;
  2612. res.cls := TBool ;
  2613. RETURN
  2614. END ;
  2615. LoadPair (left, right) ;
  2616. EmOrAxCx () ;
  2617. res.kind := 2 ; res.cls := TBool ;
  2618. RETURN
  2619. END ;
  2620. IF (left.kind = 0) AND (right.kind = 0) THEN
  2621. okc := FALSE ;
  2622. CASE op OF
  2623. OpAdd : f := ConstAdd (left.imm, right.imm) ; okc := TRUE ;
  2624. | OpSub : f := ConstSub (left.imm, right.imm) ; okc := TRUE ;
  2625. | OpMul : f := ConstMul (left.imm, right.imm) ; okc := TRUE ;
  2626. | TkDiv : okc := (right.imm # 0) AND (right.imm > 0)
  2627. AND (left.imm >= 0) ;
  2628. IF okc THEN f := left.imm DIV right.imm END ;
  2629. | TkMod : okc := (right.imm # 0) AND (right.imm > 0)
  2630. AND (left.imm >= 0) ;
  2631. IF okc THEN f := left.imm MOD right.imm END ;
  2632. ELSE
  2633. okc := FALSE
  2634. END ;
  2635. IF okc THEN
  2636. res.kind := 0 ;
  2637. res.imm := VAL (LONGINT, W16 (f)) ;
  2638. res.cls := left.cls ;
  2639. RETURN
  2640. ELSIF op = TkDiv THEN
  2641. Err (EConstRange) ;
  2642. RETURN
  2643. END
  2644. END ;
  2645. LoadPair (left, right) ;
  2646. CASE op OF
  2647. OpAdd : EmAddAxCx ;
  2648. | OpSub : EmSubAxCx ;
  2649. | OpMul : EmMulAxCx ;
  2650. | TkDiv : EmIDivAxCx ;
  2651. | TkMod : EmIDivAxCx ; EmXchgAxDx ;
  2652. ELSE
  2653. Err (ETypeErr)
  2654. END ;
  2655. res.kind := 2 ;
  2656. res.cls := left.cls
  2657. END BinOpEmit ;
  2658. PROCEDURE ConstCmp (op : CARDINAL ; a, b : LONGINT ; VAR f : LONGINT)
  2659. : BOOLEAN ;
  2660. VAR a16, b16 : CARDINAL ;
  2661. BEGIN
  2662. a16 := W16 (a) ;
  2663. b16 := W16 (b) ;
  2664. f := 0 ;
  2665. CASE op OF
  2666. 1 : f := VAL (LONGINT, ORD (a16 = b16)) ;
  2667. | 2 : f := VAL (LONGINT, ORD (a16 # b16)) ;
  2668. | 3 : f := VAL (LONGINT, ORD (a16 < b16)) ;
  2669. | 4 : f := VAL (LONGINT, ORD (a16 >= b16)) ;
  2670. | 5 : f := VAL (LONGINT, ORD (a16 > b16)) ;
  2671. | 6 : f := VAL (LONGINT, ORD (a16 <= b16)) ;
  2672. ELSE
  2673. RETURN FALSE
  2674. END ;
  2675. RETURN TRUE
  2676. END ConstCmp ;
  2677. (* ---------------------------------------------------------------- *)
  2678. (* keeping an operand alive across the parse of the other one *)
  2679. (* ---------------------------------------------------------------- *)
  2680. PROCEDURE SaveLeft (VAR left : ERes) ;
  2681. (* Called the moment the operator has been recognised, i.e. *before* the
  2682. right-hand operand is parsed.
  2683. Every binary level in this parser has the shape
  2684. left := r ; ParseXxx (right) ; BinOpEmit (op, left, right, r) ;
  2685. so the right-hand operand's code is emitted between those two lines, and
  2686. the left-hand operand has to survive that. For kind 0 (a constant) and
  2687. kind 1 (a named variable) there is nothing to survive: LoadAtom re-emits
  2688. the MOV or the load at the operator site, and no code has been emitted
  2689. for the left yet. A kind-2 value is different - its code is already in
  2690. the image and its value exists only in AX, which the right-hand parse is
  2691. about to overwrite. The value had to be parked on the stack instead.
  2692. TPSRC9 does exactly this in the other order: addptr and mulptr CALL
  2693. pushres (which calls loadatom and sets flgpshax) *before* CALL mullevel,
  2694. so PUSH AX lands in the image ahead of the right-hand operand's code.
  2695. Without this, `(p > q) or (q > p)` emitted both comparisons and then
  2696. ORed the *second* comparison's result with itself - which is why it
  2697. printed FALSE for TRUE or FALSE. *)
  2698. BEGIN
  2699. IF left.kind = 2 THEN
  2700. EmPushAx () ;
  2701. left.kind := 4 (* 4 = on the top of the stack *)
  2702. ELSIF left.kind = 5 THEN
  2703. (* A string element whose address is in DI. This is the one place the
  2704. address must be spent BEFORE the right-hand operand is parsed: DI
  2705. cannot survive it, because the right operand may hold a subscript of
  2706. its own (which reuses DI) or call a routine (which may clobber it).
  2707. Reading [DI] now - which LoadAtom does, turning kind 5 into kind 2 -
  2708. and pushing the value is exactly what the kind-2 case above does
  2709. next, so the two branches converge on kind 4. A subscript on the
  2710. RIGHT is fine: it sets DI after this point and LoadPair reads it
  2711. immediately, with no parse in between. *)
  2712. LoadAtom (left) ; (* AL := [DI], AH := 0; kind := 2 *)
  2713. EmPushAx () ;
  2714. left.kind := 4
  2715. END
  2716. END SaveLeft ;
  2717. PROCEDURE LoadPair (VAR left, right : ERes) ;
  2718. (* Materialise both operands of a binary operator so that, once this
  2719. returns, AX holds the LEFT one and CX the RIGHT one - which is the state
  2720. EmAddAxCx/EmSubAxCx/EmMulAxCx/EmIDivAxCx/EmAndAxCx/EmOrAxCx/EmCmpAxCx
  2721. all expect (see EmXchgAxCx).
  2722. Three shapes, and only three:
  2723. left.kind = 4 SaveLeft pushed it before the right operand was
  2724. parsed, so the right value is in AX and the left one
  2725. is on the stack: load the right (a no-op for kind 2),
  2726. pop the left into CX, then swap.
  2727. right.kind = 2 The right value is in AX, and loading the left would
  2728. destroy it: push the right, load the left, then pop
  2729. the right straight into CX. No swap needed.
  2730. neither Both can still be materialised from scratch, and
  2731. neither is at risk, so this is the original sequence.
  2732. A kind-5 right-hand operand takes that last shape, and is safe there: the
  2733. only instruction emitted before its LoadAtom is the left's, and MOV AX,[var]
  2734. and MOV AX,imm do not touch DI, so [DI] still holds the element address.
  2735. Whichever shape is taken, exactly one push is matched by exactly one
  2736. pop. *)
  2737. BEGIN
  2738. IF left.kind = 4 THEN
  2739. LoadAtom (right) ;
  2740. EmPopCx () ;
  2741. EmXchgAxCx ()
  2742. ELSIF right.kind = 2 THEN
  2743. EmPushAx () ;
  2744. LoadAtom (left) ;
  2745. EmPopCx ()
  2746. ELSE
  2747. LoadAtom (left) ; EmPushAx () ;
  2748. LoadAtom (right) ; EmPopCx () ;
  2749. EmXchgAxCx ()
  2750. END
  2751. END LoadPair ;
  2752. PROCEDURE EmStrBlob (r : ERes) : CARDINAL ;
  2753. (* Emit a string literal's <len><chars> in the code stream behind a JMP and
  2754. return the image offset of the LENGTH byte. Shared by EmPushStrAddr (which
  2755. pushes that address) and EmPushStrSlot (which copies the bytes onto the
  2756. stack for a string value parameter). *)
  2757. VAR n, len, here : CARDINAL ;
  2758. BEGIN
  2759. len := strLen [r.strx] ;
  2760. IF len > 255 THEN
  2761. (* As in IoCall: the length is one byte, so a longer literal would wrap
  2762. and silently carry only its first (length MOD 256) characters.
  2763. TP3 strings are at most 255 characters. *)
  2764. Err (EConstRange) ;
  2765. RETURN 0
  2766. END ;
  2767. IF len + 1 <= 127 THEN
  2768. Ebyte (0EBH) ; Ebyte (VAL (BYTE, len + 1)) (* JMP rel8 over it *)
  2769. ELSE
  2770. Ebyte (0E9H) ; Eword (len + 1) (* JMP rel16 over it *)
  2771. END ;
  2772. here := pc ; (* the length byte: the literal's address *)
  2773. Ebyte (VAL (BYTE, len)) ;
  2774. n := 0 ;
  2775. WHILE n < len DO
  2776. Ebyte (VAL (BYTE, ORD (strPool [strOff [r.strx] + n]))) ;
  2777. INC (n)
  2778. END ;
  2779. RETURN here
  2780. END EmStrBlob ;
  2781. PROCEDURE EmPushSetAddr (r : ERes) ;
  2782. (* Push the ADDRESS of a set value: the runtime's set entries take addresses.
  2783. A set literal/typed constant is code-resident (kind 7), a set-expression
  2784. result is a data buffer (kind 8), a named set is symbol+boff, and a field
  2785. reached through DI is already addressed. *)
  2786. BEGIN
  2787. IF r.kind = 5 THEN
  2788. EmPushDi ()
  2789. ELSIF r.kind = 1 THEN
  2790. EmPushVarAddr (symtab [r.idx].local,
  2791. (symtab [r.idx].off + r.boff) MOD 10000H)
  2792. ELSIF (r.kind = 7) OR (r.kind = 8) THEN
  2793. EmPushVarAddr (FALSE, r.setoff)
  2794. ELSE
  2795. Err (ENoLib)
  2796. END
  2797. END EmPushSetAddr ;
  2798. PROCEDURE SetPairSize (left, right : ERes ; VAR size, base : CARDINAL) : BOOLEAN ;
  2799. (* The byte count for a set operation. An inline literal is a 32-byte
  2800. `set of Char' by default; when the other operand is a typed set (CALC's
  2801. one-byte `set of Attributes') that type wins. Two non-literals must have
  2802. the same origin and are taken at the smaller size; the runtime only ever
  2803. touches that many bytes. *)
  2804. BEGIN
  2805. IF left.setbase # right.setbase THEN
  2806. RETURN FALSE
  2807. END ;
  2808. base := left.setbase ;
  2809. IF left.kind = 7 THEN
  2810. IF right.kind = 7 THEN
  2811. size := left.setsize
  2812. ELSE
  2813. size := right.setsize
  2814. END
  2815. ELSIF right.kind = 7 THEN
  2816. size := left.setsize
  2817. ELSE
  2818. size := left.setsize ;
  2819. IF right.setsize < size THEN
  2820. size := right.setsize
  2821. END
  2822. END ;
  2823. RETURN (size > 0) AND (size <= 32)
  2824. END SetPairSize ;
  2825. PROCEDURE AllocSetScratch (size : CARDINAL) : CARDINAL ;
  2826. (* A fresh data buffer for one set expression. One per operation, so a nested
  2827. or repeated expression cannot overwrite the other side while it is still
  2828. being consumed. The bytes need no initialisation: every operation writes
  2829. all `size' of them before the value is read. *)
  2830. VAR off : CARDINAL ;
  2831. BEGIN
  2832. IF dc + size > 0F000H THEN
  2833. Err (EConstRange) ;
  2834. RETURN 0
  2835. END ;
  2836. off := dc ;
  2837. dc := dc + size ;
  2838. RETURN off
  2839. END AllocSetScratch ;
  2840. PROCEDURE SetBinEmit (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  2841. (* The caller has already pushed the LEFT address (before parsing the right
  2842. operand, so a DI-addressed field survives). Finish the argument list -
  2843. right, destination, (op<<8)|size - call the runtime, and return the buffer
  2844. as a kind-8 set value. *)
  2845. VAR size, base, dest : CARDINAL ;
  2846. BEGIN
  2847. IF NOT SetPairSize (left, right, size, base) THEN
  2848. Err (ENoLib) ;
  2849. RETURN
  2850. END ;
  2851. dest := AllocSetScratch (size) ;
  2852. IF NOT OK () THEN
  2853. RETURN
  2854. END ;
  2855. EmPushSetAddr (right) ;
  2856. EmPushVarAddr (FALSE, dest) ;
  2857. EmMovAxi (W16 (op * 256 + size)) ;
  2858. EmPushAx () ;
  2859. DropC (EmCall (TU_SetOp)) ;
  2860. EmAddSp (8) ;
  2861. res.kind := 8 ;
  2862. res.cls := TSet ;
  2863. res.chr := FALSE ;
  2864. res.setoff := dest ;
  2865. res.setbase := base ;
  2866. res.setsize := size
  2867. END SetBinEmit ;
  2868. PROCEDURE EmPushStrAddr (r : ERes) ;
  2869. (* Push the ADDRESS of a whole-string operand - the argument shape cmpstr
  2870. takes, one per operand.
  2871. A string VARIABLE's value already is its address, so this is the same push
  2872. READ makes (EmPushVarAddr), through symtab's local/off.
  2873. A string LITERAL has no storage, and the image carries no initialised data
  2874. to put one in (globals are left to the loader's zero fill), so the
  2875. characters have to travel in the code stream and the operand is the address
  2876. of those bytes. TP3 does the same in TPSRC9 fullvar (993): it builds the
  2877. `JMP' opcode in AL and the length in AH (`MOV AL,#$EB' / `MOV.B AH,CH'),
  2878. emits that word, and takes the position immediately after it as varofs,
  2879. with varseg := CS - the instruction stream steps over the constant instead
  2880. of running into it.
  2881. The difference is one byte, and it is deliberate. TP3's constant is an
  2882. ARRAY [1..n] OF CHAR, and `estr2' (TPSRC10 85) emits it WITHOUT a length
  2883. byte because the length travelled on the stack instead. Every string in
  2884. THIS runtime - variable or literal - is a `string': a length byte and then
  2885. the characters, which is what rdstr writes and what cmpstr reads. So a
  2886. literal operand has to look the same, and the jump carries one extra byte:
  2887. <len><chars>, with the ADDRESS pushed being the length byte itself. The
  2888. jump still exists only to keep the CPU from walking into the characters.
  2889. Nothing is added to the data segment and no runtime entry is involved in
  2890. reading the literal; LEA AX,[that] is absolute into the code segment, which
  2891. is where a .COM's DS already is.
  2892. A 1-character literal is NOT a TString here: RdConst reports it as TScalar
  2893. (its character code), so `s = 'a'` compares a string with a character and
  2894. is a type error rather than a length-1 string compare. That is deliberate
  2895. - it is the same rule `c := 'a'` relies on - and the caller rejects it. *)
  2896. BEGIN
  2897. IF r.kind = 5 THEN
  2898. (* a string element/field computed through DI - `Sheet[X,Y].Contents'
  2899. and the `with' form. DI holds the field's address, which is exactly
  2900. what cmpstr/strcat/strstore take on the stack, so push it. This used
  2901. to be ENoLib, and it was the CALC rung after Write(Value:FW:DEC):
  2902. `if Sheet[UFX,UFY].Contents <> '' then'. *)
  2903. EmPushDi ()
  2904. ELSIF r.kind = 1 THEN
  2905. IF symtab [r.idx].isVar THEN
  2906. (* A VAR string parameter: the slot at [BP+off] holds the address of
  2907. the caller's characters, so push its CONTENTS, not the address of
  2908. the slot. boff is zero for the whole string; an element/subscript
  2909. is kind 5 and handled above. *)
  2910. IF r.boff # 0 THEN
  2911. Err (ENoLib)
  2912. ELSE
  2913. EmLoadVar (TRUE, symtab [r.idx].off, 2) ;
  2914. EmPushAx ()
  2915. END
  2916. ELSE
  2917. EmPushVarAddr (symtab [r.idx].local,
  2918. (symtab [r.idx].off + r.boff) MOD 10000H)
  2919. END
  2920. ELSIF r.kind = 3 THEN
  2921. EmPushVarAddr (FALSE, EmStrBlob (r)) (* the literal travels inline *)
  2922. ELSE
  2923. Err (ENoLib) (* a computed string is not a value *)
  2924. END
  2925. END EmPushStrAddr ;
  2926. PROCEDURE EmPushRealAddr (r : ERes) ;
  2927. (* Push the address of a six-byte REAL actual: ordinary variable, VAR REAL
  2928. parameter (slot holds the address), or a kind-5 field/function result. *)
  2929. BEGIN
  2930. IF r.kind = 5 THEN
  2931. EmPushDi ()
  2932. ELSIF r.kind = 1 THEN
  2933. IF symtab [r.idx].isVar THEN
  2934. EmLoadVar (TRUE, symtab [r.idx].off, 2) ;
  2935. EmPushAx ()
  2936. ELSE
  2937. EmPushVarAddr (symtab [r.idx].local,
  2938. (symtab [r.idx].off + r.boff) MOD 10000H)
  2939. END
  2940. ELSE
  2941. Err (ENoLib)
  2942. END
  2943. END EmPushRealAddr ;
  2944. PROCEDURE EmPushRealSlot (r : ERes ; reserve : CARDINAL) ;
  2945. (* Push a six-byte value REAL argument's whole slot onto the stack. *)
  2946. BEGIN
  2947. EmPushRealAddr (r) ;
  2948. EmPopSi () ;
  2949. EmSubSp (reserve) ;
  2950. Ebyte (89H) ; Ebyte (0E7H) ; (* MOV DI,SP *)
  2951. EmMovCxi (reserve) ;
  2952. Ebyte (8AH) ; Ebyte (04H) ; (* MOV AL,[SI] *)
  2953. Ebyte (88H) ; Ebyte (05H) ; (* MOV [DI],AL *)
  2954. Ebyte (46H) ; Ebyte (47H) ; (* INC SI / INC DI *)
  2955. Ebyte (0E2H) ; Ebyte (0F8H) (* LOOP back *)
  2956. END EmPushRealSlot ;
  2957. PROCEDURE EmPushStrSlot (r : ERes ; reserve, copyN : CARDINAL) ;
  2958. (* Push a STRING VALUE argument's whole slot: reserve `reserve' bytes on the
  2959. stack and copy `copyN' of the source string's <len><chars> into them, so
  2960. that after the CALL the callee's BP+4 is the length byte and the characters
  2961. follow it.
  2962. <push the source address, then POP SI> ; local/global/literal alike
  2963. SUB SP,reserve ; MOV DI,SP ; MOV CX,copyN
  2964. loop: MOV AL,[SI] ; MOV [DI],AL ; INC SI ; INC DI ; LOOP loop
  2965. The address comes from EmPushStrAddr, which already knows how to form it for
  2966. a local ([BP+off]), a global (absolute) or a literal (inline blob) - so a
  2967. caller's LOCAL string can be passed too, not just a global.
  2968. A byte loop, not REP MOVSB, on purpose: MOVS/STOS address through ES, and a
  2969. .COM's ES happens to equal DS but nothing in this compiler establishes that
  2970. contract. Every access here is DS-relative, which is the one segment the
  2971. image already guarantees. `copyN' is at most `reserve' (a shorter actual
  2972. string than the formal leaves the tail of the slot untouched, harmless
  2973. because the length byte bounds every read). *)
  2974. BEGIN
  2975. EmPushStrAddr (r) ; (* push the source address *)
  2976. EmPopSi () ; (* SI := source *)
  2977. EmSubSp (reserve) ;
  2978. Ebyte (89H) ; Ebyte (0E7H) ; (* MOV DI,SP *)
  2979. EmMovCxi (copyN) ;
  2980. IF copyN > 0 THEN
  2981. Ebyte (8AH) ; Ebyte (04H) ; (* MOV AL,[SI] *)
  2982. Ebyte (88H) ; Ebyte (05H) ; (* MOV [DI],AL *)
  2983. Ebyte (46H) ; (* INC SI *)
  2984. Ebyte (47H) ; (* INC DI *)
  2985. Ebyte (0E2H) ; Ebyte (0F8H) (* LOOP -8: back to MOV AL,[SI] *)
  2986. END
  2987. END EmPushStrSlot ;
  2988. (* ---------------------------------------------------------------- *)
  2989. (* sets - type sizing, literals, compile-time membership *)
  2990. (* ---------------------------------------------------------------- *)
  2991. PROCEDURE SetRangeBits (VAR bytes : ARRAY OF CHAR ; size, base : CARDINAL ;
  2992. lo, hi : LONGINT) ;
  2993. (* Set the bits for every element in [lo, hi], using TP3's hybrid rule
  2994. (coeur-tp-ocr 4.7.2): element e is bit (e MOD 8) of byte
  2995. ((e DIV 8) - (base DIV 8)), and is IGNORED unless that byte is in
  2996. [0, size). A value that lands outside the used bytes is truncated away, as
  2997. TP3 does; one inside them but outside the DECLARED range still lands, which
  2998. is also TP3's behaviour. *)
  2999. VAR rel, first, last, bidx, bit, m, o : CARDINAL ;
  3000. BEGIN
  3001. IF (hi < 0) OR (lo > 255) THEN
  3002. RETURN
  3003. END ;
  3004. IF lo < 0 THEN first := 0 ELSE first := VAL (CARDINAL, lo) END ;
  3005. IF hi > 255 THEN last := 255 ELSE last := VAL (CARDINAL, hi) END ;
  3006. rel := first ;
  3007. WHILE rel <= last DO
  3008. IF rel >= base THEN
  3009. bidx := (rel - base) DIV 8 ;
  3010. IF bidx < size THEN
  3011. bit := (rel - base) MOD 8 ;
  3012. m := 1 ;
  3013. WHILE bit > 0 DO
  3014. m := m * 2 ; DEC (bit)
  3015. END ;
  3016. o := ORD (bytes [bidx]) ;
  3017. IF (o DIV m) MOD 2 = 0 THEN
  3018. bytes [bidx] := CHR (o + m)
  3019. END
  3020. END
  3021. END ;
  3022. INC (rel)
  3023. END
  3024. END SetRangeBits ;
  3025. PROCEDURE RdSetLiteral (base, size : CARDINAL ; VAR bytes : ARRAY OF CHAR) : BOOLEAN ;
  3026. (* Read a set constructor `[e, a..b, ...]' and fill `bytes' (already size
  3027. long). `[]' is the empty set. *)
  3028. VAR v, hi : LONGINT ; subcls : CARDINAL ; strf : BOOLEAN ; k : CARDINAL ;
  3029. BEGIN
  3030. k := 0 ;
  3031. WHILE k < size DO
  3032. bytes [k] := CHR (0) ;
  3033. INC (k)
  3034. END ;
  3035. ExpectDelim ('[', ENoSemi) ;
  3036. IF MatchDelim (']') THEN
  3037. RETURN OK () (* [] - the empty set *)
  3038. END ;
  3039. LOOP
  3040. RdConst (v, subcls, strf) ;
  3041. IF strf THEN
  3042. Err (ETypeErr) ;
  3043. RETURN FALSE
  3044. END ;
  3045. IF MatchRange () THEN
  3046. RdConst (hi, subcls, strf) ;
  3047. IF strf THEN
  3048. Err (ETypeErr) ;
  3049. RETURN FALSE
  3050. END
  3051. ELSE
  3052. hi := v
  3053. END ;
  3054. SetRangeBits (bytes, size, base, v, hi) ;
  3055. IF MatchDelim (',') THEN
  3056. (* another element *)
  3057. ELSE
  3058. EXIT
  3059. END
  3060. END ;
  3061. ExpectDelim (']', ENoSemi) ;
  3062. RETURN OK ()
  3063. END RdSetLiteral ;
  3064. PROCEDURE EmSetBlob (VAR bytes : ARRAY OF CHAR ; size : CARDINAL) : CARDINAL ;
  3065. (* Emit a set's bytes into the code stream behind a JMP over them - the same
  3066. trick EmPushStrAddr uses for a string literal - and return the image offset
  3067. of the first byte. Nothing goes in the data segment: a .COM's DS already
  3068. covers the code segment, which is why this port needs no `seg cs' where the
  3069. original did (see CALC-COMPILE.md's typed-constant storage decision). *)
  3070. VAR here, k : CARDINAL ;
  3071. BEGIN
  3072. IF size + 2 <= 127 THEN
  3073. Ebyte (0EBH) ; Ebyte (VAL (BYTE, size)) (* JMP rel8 over it *)
  3074. ELSE
  3075. Ebyte (0E9H) ; Eword (size) (* JMP rel16 over it *)
  3076. END ;
  3077. here := pc ;
  3078. k := 0 ;
  3079. WHILE k < size DO
  3080. Ebyte (VAL (BYTE, ORD (bytes [k]))) ;
  3081. INC (k)
  3082. END ;
  3083. RETURN here
  3084. END EmSetBlob ;
  3085. PROCEDURE FoldSetIn (left, right : ERes ; VAR res : ERes) ;
  3086. (* `e IN s' where e is a compile-time ordinal and s a set whose bytes are
  3087. already in the image: answer at compile time, exactly as TPSRC2's xsetin
  3088. answers at run time (AH > 255 -> FALSE, else test the bit). A run-time
  3089. element or a run-time set is still ENoLib - that is the next milestone, and
  3090. it must stay LOUD rather than silently answer wrong. *)
  3091. VAR off, base, size, rel, bidx, bit, m, o : CARDINAL ;
  3092. isIn, foldable : BOOLEAN ;
  3093. jl, jg, jmp : CARDINAL ;
  3094. BEGIN
  3095. res.kind := 0 ;
  3096. res.imm := 0 ;
  3097. res.cls := TBool ;
  3098. res.chr := FALSE ;
  3099. IF right.cls # TSet THEN
  3100. Err (ENoLib) ;
  3101. RETURN
  3102. END ;
  3103. IF left.kind # 0 THEN
  3104. (* A RUN-TIME element (`Upcase(Ch) in ['Y','N']', `Ch in ['a'..'z']').
  3105. Materialise it in AX, subtract the set's base, reject the out-of-range
  3106. values with the two signed tests the compiler declares (L and G), then
  3107. index the bit array: byte = (e-base) DIV 8, bit = (e-base) MOD 8.
  3108. The set's address is formed according to where its bytes live: an
  3109. inline literal or typed constant is code-resident (image offset +
  3110. LoadBias), a data set is an absolute or BP-relative address. A set
  3111. already addressed through DI and an element that itself needs DI are
  3112. refused: the address would be lost. *)
  3113. IF right.kind = 5 THEN
  3114. Err (ENoLib) ;
  3115. RETURN
  3116. END ;
  3117. LoadAtom (left) ;
  3118. IF NOT OK () THEN
  3119. RETURN
  3120. END ;
  3121. IF right.setbase # 0 THEN
  3122. EmMovCxi (W16 (right.setbase)) ;
  3123. EmSubAxCx ()
  3124. END ;
  3125. EmCmpAxi (0) ;
  3126. jl := EmJcc (8CH, 0) ; (* JL -> false *)
  3127. EmCmpAxi (W16 (right.setsize * 8)) ;
  3128. jg := EmJcc (8FH, 0) ; (* JG -> false *)
  3129. EmMovClAl () ;
  3130. EmAndClImm (7) ;
  3131. EmShrAx1 () ; EmShrAx1 () ; EmShrAx1 () ; (* AX := byte index *)
  3132. IF right.kind = 7 THEN
  3133. EmMovBxi (W16 (right.setoff + LoadBias))
  3134. ELSIF right.kind = 1 THEN
  3135. IF symtab [right.idx].local THEN
  3136. EmLeaBx ((right.setoff + right.boff) MOD 10000H)
  3137. ELSE
  3138. EmMovBxi ((right.setoff + right.boff + LoadBias) MOD 10000H)
  3139. END
  3140. ELSE
  3141. Err (ENoLib) ;
  3142. RETURN
  3143. END ;
  3144. EmAddBxAx () ; (* BX := the set's byte *)
  3145. EmLdAlBx () ;
  3146. EmMovDlImm (1) ;
  3147. EmShlDlCl () ; (* DL := 1 << bit *)
  3148. EmAndAlDl () ;
  3149. EmMovAh0 () ;
  3150. EmOrAxAx () ;
  3151. EmRelSetcc (2) ; (* SETNE: the bit was set *)
  3152. jmp := EmJmpNear (0) ;
  3153. SetPatTgt (jl, pc) ;
  3154. SetPatTgt (jg, pc) ;
  3155. EmMovAxi (0) ;
  3156. SetPatTgt (jmp, pc) ;
  3157. res.kind := 2 ;
  3158. res.cls := TBool ;
  3159. RETURN
  3160. END ;
  3161. (* A code-resident set (an inline literal or a typed constant) folds at
  3162. compile time. A DATA set - a record field, local or global, or one
  3163. addressed through DI - must be tested at run time: its byte is in the
  3164. image's data area, not in cbuf. A typed constant's offset is below
  3165. dataBase; a global's is at or above it. *)
  3166. foldable := (right.kind = 7) ;
  3167. IF (right.kind = 1) AND NOT symtab [right.idx].local
  3168. AND (right.setoff + right.boff < dataBase) THEN
  3169. foldable := TRUE
  3170. END ;
  3171. IF NOT foldable THEN
  3172. IF (left.imm >= 0) AND (left.imm <= 255) THEN
  3173. base := right.setbase ; size := right.setsize ;
  3174. rel := VAL (CARDINAL, left.imm) ;
  3175. IF rel >= base THEN
  3176. bidx := (rel - base) DIV 8 ;
  3177. IF bidx < size THEN
  3178. bit := (rel - base) MOD 8 ;
  3179. m := 1 ;
  3180. WHILE bit > 0 DO
  3181. m := m * 2 ; DEC (bit)
  3182. END ;
  3183. IF right.kind = 5 THEN
  3184. EmLdAlDiOff (bidx)
  3185. ELSIF right.kind = 1 THEN
  3186. EmLoadVar (symtab [right.idx].local,
  3187. (right.setoff + right.boff + bidx) MOD 10000H, 1)
  3188. ELSE
  3189. Err (ENoLib) ;
  3190. RETURN
  3191. END ;
  3192. EmAndAlImm (m) ;
  3193. EmMovAh0 () ;
  3194. EmOrAxAx () ;
  3195. EmRelSetcc (2) ; (* SETNE: the bit was set *)
  3196. res.kind := 2 ;
  3197. res.cls := TBool ;
  3198. RETURN
  3199. END
  3200. END
  3201. END ;
  3202. res.imm := 0 ;
  3203. RETURN
  3204. END ;
  3205. isIn := FALSE ;
  3206. IF (left.imm >= 0) AND (left.imm <= 255) THEN
  3207. off := right.setoff ; base := right.setbase ; size := right.setsize ;
  3208. rel := VAL (CARDINAL, left.imm) ;
  3209. IF rel >= base THEN
  3210. bidx := (rel - base) DIV 8 ;
  3211. IF bidx < size THEN
  3212. bit := (rel - base) MOD 8 ;
  3213. m := 1 ;
  3214. WHILE bit > 0 DO
  3215. m := m * 2 ; DEC (bit)
  3216. END ;
  3217. o := VAL (CARDINAL, cbuf [off + bidx]) ;
  3218. isIn := (o DIV m) MOD 2 # 0
  3219. END
  3220. END
  3221. END ;
  3222. res.imm := ORD (isIn)
  3223. END FoldSetIn ;
  3224. PROCEDURE RdSetBase (VAR baseCls : CARDINAL ; VAR lo, hi : LONGINT) ;
  3225. (* The base type of `set of ...': an ordinal range. `Char' and `byte' are
  3226. 0..255; a literal subrange `a..b' is read directly. A named or enumerated
  3227. base is ENoLib for now - `set of Attributes' waits for enums. *)
  3228. VAR idx, subcls : CARDINAL ; strf : BOOLEAN ;
  3229. BEGIN
  3230. baseCls := TScalar ;
  3231. Skip () ;
  3232. IF Alpha (CurCh ()) THEN
  3233. GetWord () ;
  3234. IF Search (wrd, idx) AND (symtab [idx].tag = KType) THEN
  3235. IF symtab [idx].cls = TEnum THEN
  3236. lo := symtab [idx].lo ;
  3237. hi := symtab [idx].hi ;
  3238. baseCls := TEnum
  3239. ELSIF (symtab [idx].cls = TChar)
  3240. OR ((symtab [idx].cls = TScalar) AND (symtab [idx].size = 1)) THEN
  3241. lo := 0 ; hi := 255 ; baseCls := symtab [idx].cls
  3242. ELSE
  3243. Err (ENoLib)
  3244. END
  3245. ELSE
  3246. Err (EUnknown)
  3247. END
  3248. ELSE
  3249. RdConst (lo, subcls, strf) ;
  3250. IF NOT MatchRange () THEN
  3251. Err (ESimpType) ;
  3252. RETURN
  3253. END ;
  3254. RdConst (hi, subcls, strf) ;
  3255. IF (lo < 0) OR (hi > 255) OR (lo > hi) THEN
  3256. Err (EConstRange)
  3257. END
  3258. END
  3259. END RdSetBase ;
  3260. PROCEDURE NewScratchStr () : CARDINAL ;
  3261. (* Allocate a fresh 256-byte global STRING and return its symbol index.
  3262. A concatenation result needs STORAGE the emitted code can name. Making the
  3263. result a string VARIABLE (kind 1) is what keeps every downstream consumer
  3264. untouched: Assignment, IoCall and ParseCmp already handle kind 1, so `s :=
  3265. a + b', `writeln(a + b)' and `(a + b) = c' work with no code of their own.
  3266. One scratch PER `+' in the source, so a repeated or nested concatenation
  3267. never shares a destination - `a + b + c' is two occurrences and gets two
  3268. scratch strings, and the first result survives until the second consumes it.
  3269. (A loop reuses its site's scratch, which is correct because each execution
  3270. writes before it reads.) Recursion would break that, but this compiler
  3271. compiles no procedures yet - see the item list.
  3272. 256 bytes is the STRING type's own size, length byte plus 255 characters:
  3273. see ParseType's TkString arm and the predefined STRING in Inittur. The name
  3274. is a placeholder (nothing looks a scratch up by name) spelled with '@', as
  3275. tmpA/tmpB are, so it cannot collide with a Modula-2 identifier. *)
  3276. BEGIN
  3277. DropC (NewSym ("@@SC", KVar, TString, 256, 1, dc, 0, FALSE)) ;
  3278. dc := dc + 256 ;
  3279. RETURN symTop - 1
  3280. END NewScratchStr ;
  3281. PROCEDURE NewScratchReal () : CARDINAL ;
  3282. (* Six bytes of storage for a REAL function result, one per call site. *)
  3283. BEGIN
  3284. DropC (NewSym ("@@RL", KVar, TReal, 6, 6, dc, 0, FALSE)) ;
  3285. dc := dc + 6 ;
  3286. RETURN symTop - 1
  3287. END NewScratchReal ;
  3288. PROCEDURE StrOperand (r : ERes) : BOOLEAN ;
  3289. (* Is r usable as one operand of `+'? A STRING, or a CHAR - which can appear
  3290. three ways: a quoted literal (cls TScalar with chr set), a CHAR variable
  3291. (cls TChar), and a string element s[i] (cls TChar, kind 5). A number is
  3292. not, and neither is a BOOLEAN. *)
  3293. BEGIN
  3294. RETURN (r.cls = TString) OR (r.cls = TChar) OR
  3295. ((r.cls = TScalar) AND r.chr)
  3296. END StrOperand ;
  3297. PROCEDURE EmPushStrOperand (r : ERes) ;
  3298. (* Push the ADDRESS of one concatenation operand, converting a CHAR to a
  3299. length-1 string first.
  3300. A STRING operand is pushed exactly as cmpstr pushes one (EmPushStrAddr):
  3301. a variable's value already IS its address, and a literal travels inline.
  3302. A CHAR operand has no such address, so it is materialised as a
  3303. `<length 1><character>' string in a fresh 2-byte scratch and the address of
  3304. THAT is pushed. This is TPSRC9's addch/addch2, which splice a character
  3305. into a concatenation by building a one-character string. The character is
  3306. loaded into AL (LoadAtom), the character is stored at [coff+1] and the
  3307. length 1 at [coff]. At most one operand of a concatenation is a CHAR -
  3308. StrConcat rejects char+char - so at most one such scratch is ever made. *)
  3309. VAR coff : CARDINAL ;
  3310. BEGIN
  3311. IF r.cls = TString THEN
  3312. EmPushStrAddr (r)
  3313. ELSE
  3314. coff := dc ;
  3315. dc := dc + 2 ;
  3316. LoadAtom (r) ; (* AX := the character *)
  3317. EmStoreVar (FALSE, (coff + 1) MOD 10000H, 1) ; (* [coff+1] := AL *)
  3318. EmMovAxi (1) ; (* AX := 1 *)
  3319. EmStoreVar (FALSE, coff MOD 10000H, 1) ; (* [coff] := AL = length *)
  3320. EmPushVarAddr (FALSE, coff MOD 10000H)
  3321. END
  3322. END EmPushStrOperand ;
  3323. PROCEDURE StrConcat (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  3324. (* `left + right' for strings: TPSRC2's xconcat, emitted as two runtime calls.
  3325. The layout is `<length byte><characters>' and `+' needs a place to build its
  3326. result, so this allocates one 256-byte global scratch (NewScratchStr) and
  3327. copies into it: first `dest := left' through the runtime's strstore, then
  3328. `dest := dest + right' through strcat, which appends. Using strstore for
  3329. the left operand and strcat for the right keeps the append logic in one
  3330. place and gives the compiler exactly one new entry to reach; it also means
  3331. the two calls have the same argument shape (source pushed first, destination
  3332. last, limit in CX), so the emission differs only in the entry named.
  3333. The limit is 255 - the scratch's 255 usable characters. An overflowing
  3334. result is a RUNTIME ERROR with exit code 16, TP3's `String too long', not a
  3335. truncation; see Runtime.EmitStrCat.
  3336. The operator rule: `+' concatenates, anything else over a STRING is a type
  3337. error, and char+char is ALSO a type error. ISO Modula-2 gives no meaning to
  3338. two characters summed, and inventing one (a two-character string?) would be
  3339. a guess this compiler does not make; TP3's own behaviour there is not
  3340. something the reference pins down. That char+char choice is the one place
  3341. this differs from a literal reading of the reference, and it is recorded
  3342. here and in v-TP3-STRCAT.md rather than left to be discovered.
  3343. The guard below is what ENFORCES that rule, and it only runs because
  3344. BinOpEmit's hook routes a CHAR operand here too. A hook that tested for a
  3345. STRING operand only would send `'a' + 'b'' to the integer path instead and
  3346. this test would never be reached - which is exactly what it did until
  3347. t53_strcat_type caught it. Three ways to fail it: a non-`+' operator over
  3348. a string operand, an operand that is neither a string nor a CHAR (a number),
  3349. and two CHARs with no STRING on either side. *)
  3350. VAR dest : CARDINAL ;
  3351. BEGIN
  3352. (* A placeholder for the paths below that bail out through Err. Err only
  3353. records the first error and does not unwind, so the caller goes on to use
  3354. res regardless: after `Err (ETypeErr)' in the guard, ParseAdd hands res
  3355. straight back to IoCall, to Assignment or to an enclosing concatenation.
  3356. The placeholder's SHAPE therefore matters. It must not claim to be a
  3357. STRING, because the string consumers reach symtab[res.idx] for any TString
  3358. (IoCall's `writeln' arm does) and idx is not a symbol here; and it must be
  3359. something LoadAtom can fold, because the numeric consumers will try. The
  3360. integer 0 is both. Leaving kind-2/TString here, as this once did, turned
  3361. `writeln('a' + 'b')' - two CHARs, a type error - into a compiler segfault
  3362. instead of an error message. *)
  3363. res.kind := 0 ;
  3364. res.imm := 0 ;
  3365. res.cls := TScalar ;
  3366. res.boff := 0 ;
  3367. res.chr := FALSE ;
  3368. IF (op # OpAdd) OR NOT StrOperand (left) OR NOT StrOperand (right) OR
  3369. ((left.cls # TString) AND (right.cls # TString)) THEN
  3370. Err (ETypeErr) ; (* a STRING on the wrong operator, a number,
  3371. or two CHARs *)
  3372. RETURN
  3373. END ;
  3374. dest := NewScratchStr () ;
  3375. (* dest := left *)
  3376. EmPushStrOperand (left) ;
  3377. EmPushVarAddr (FALSE, (symtab [dest].off) MOD 10000H) ;
  3378. EmMovCxi (255) ;
  3379. DropC (EmCall (TU_StrStore)) ;
  3380. EmAddSp (4) ; (* pop the two addresses strstore took *)
  3381. (* dest := dest + right *)
  3382. EmPushStrOperand (right) ;
  3383. EmPushVarAddr (FALSE, (symtab [dest].off) MOD 10000H) ;
  3384. EmMovCxi (255) ;
  3385. DropC (EmCall (TU_StrCat)) ;
  3386. EmAddSp (4) ;
  3387. res.kind := 1 ;
  3388. res.idx := dest ;
  3389. res.cls := TString ;
  3390. res.boff := 0 ;
  3391. res.chr := FALSE
  3392. END StrConcat ;
  3393. PROCEDURE ParseCmp (VAR r : ERes) ;
  3394. (* "=" | "<>" | "<" | "<=" | ">" | ">=" *)
  3395. VAR op : CARDINAL ;
  3396. left, right : ERes ;
  3397. f : LONGINT ;
  3398. size, base : CARDINAL ;
  3399. BEGIN
  3400. ParseAdd (r) ;
  3401. LOOP
  3402. op := 0 ;
  3403. Skip () ;
  3404. IF CurCh () = '=' THEN
  3405. op := 1 ; DropCh (GetCh ())
  3406. ELSIF (CurCh () = '<') AND (PeekAhead (1) = '>') THEN
  3407. op := 2 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  3408. ELSIF CurCh () = '<' THEN
  3409. IF PeekAhead (1) = '=' THEN
  3410. op := 6 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  3411. ELSE
  3412. op := 3 ; DropCh (GetCh ())
  3413. END
  3414. ELSIF CurCh () = '>' THEN
  3415. IF PeekAhead (1) = '=' THEN
  3416. op := 5 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  3417. ELSE
  3418. op := 4 ; DropCh (GetCh ())
  3419. END
  3420. END ;
  3421. IF op = 0 THEN
  3422. IF KwAhead ("IN") THEN
  3423. (* `e IN s' is at the comparison level in TP3 (TPSRC9 cmpin, 116).
  3424. Only the compile-time case is implemented; see FoldSetIn. *)
  3425. GetWord () ;
  3426. left := r ;
  3427. ParseAdd (right) ;
  3428. FoldSetIn (left, right, r) ;
  3429. RETURN
  3430. END ;
  3431. RETURN
  3432. END ;
  3433. left := r ;
  3434. IF left.cls = TSet THEN
  3435. (* Set comparison. set_cmp returns a mask (bit 0 equal, bit 1 strict
  3436. subset, bit 2 strict superset); the operator selects which bits
  3437. answer it, and the shared EmRelSetcc arm turns the flags into a
  3438. boolean. The left address is pushed before the right operand is
  3439. parsed, for the same DI reason as strings and set arithmetic. *)
  3440. EmPushSetAddr (left) ;
  3441. ParseAdd (right) ;
  3442. IF NOT OK () THEN
  3443. RETURN
  3444. END ;
  3445. IF right.cls # TSet THEN
  3446. Err (ETypeErr) ;
  3447. RETURN
  3448. END ;
  3449. IF NOT SetPairSize (left, right, size, base) THEN
  3450. Err (ENoLib) ;
  3451. RETURN
  3452. END ;
  3453. EmPushSetAddr (right) ;
  3454. EmMovAxi (size) ;
  3455. EmPushAx () ;
  3456. DropC (EmCall (TU_SetCmp)) ;
  3457. EmAddSp (4) ;
  3458. CASE op OF
  3459. 1 : EmMovCxi (1) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3460. | 2 : EmMovCxi (1) ; EmAndAxCx () ; EmXorAl01 () ;
  3461. EmOrAxAx () ; EmRelSetcc (2) ;
  3462. | 3 : EmMovCxi (2) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3463. | 4 : EmMovCxi (4) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3464. | 5 : EmMovCxi (5) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3465. | 6 : EmMovCxi (3) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2)
  3466. END ;
  3467. r.kind := 2 ;
  3468. r.cls := TBool
  3469. ELSIF left.cls = TString THEN
  3470. (* A string comparison: cmpstr, the entry TPSRC9's vcompare (133)
  3471. reaches through TPSRC2's csteq/cstne/cstge/cstle/cstg/cstl
  3472. wrappers (890-925).
  3473. The operands are pushed as ADDRESSES, before the right-hand parse
  3474. can clobber DI. This is why the left is pushed here rather than
  3475. through SaveLeft: a kind-5 left is a string FIELD whose address is
  3476. in DI, and SaveLeft's job is to materialise a scalar in AX, which
  3477. would read the length byte as a number (or, for a whole string,
  3478. refuse it). `if Sheet[UFX,UFY].Contents <> '' then' was exactly
  3479. that: the CALC rung after Write(Value:FW:DEC).
  3480. cmpstr returns AX = -1/0/+1 (see Runtime.mod for why a value and
  3481. not flags), so the second half is exactly the numeric arm's: put
  3482. the sign in the flags and let the shared six-arm table answer.
  3483. EmOrAxAx is OR AX,AX rather than EmCmpAxi(0) on purpose: tests/
  3484. check_8086.py reads every `CMP AX,0000' in the program region as
  3485. an IF/WHILE/REPEAT test and requires a branch right after it, and
  3486. a comparison used as a VALUE (writeln(a = b)) is not one. *)
  3487. EmPushStrAddr (left) ;
  3488. ParseAdd (right) ;
  3489. IF right.cls # TString THEN
  3490. Err (ETypeErr) ; (* a string compared with a number/char *)
  3491. RETURN
  3492. END ;
  3493. EmPushStrAddr (right) ;
  3494. DropC (EmCall (TU_CmpStr)) ;
  3495. EmAddSp (4) ;
  3496. EmOrAxAx () ;
  3497. EmRelSetcc (op) ;
  3498. r.kind := 2 ;
  3499. r.cls := TBool
  3500. ELSE
  3501. SaveLeft (left) ;
  3502. ParseAdd (right) ;
  3503. IF (right.cls = TString) OR (right.cls = TSet) THEN
  3504. Err (ETypeErr) ; (* a number compared with a string/set *)
  3505. RETURN
  3506. END ;
  3507. IF (left.kind = 0) AND (right.kind = 0) THEN
  3508. IF ConstCmp (op, left.imm, right.imm, f) THEN
  3509. r.kind := 0 ;
  3510. r.imm := f ;
  3511. r.cls := TBool
  3512. ELSE
  3513. r.kind := 0 ;
  3514. r.imm := 0 ;
  3515. r.cls := TBool
  3516. END
  3517. ELSE
  3518. LoadPair (left, right) ;
  3519. EmCmpAxCx () ;
  3520. EmRelSetcc (op) ;
  3521. r.kind := 2 ;
  3522. r.cls := TBool
  3523. END
  3524. END
  3525. END
  3526. END ParseCmp ;
  3527. PROCEDURE ParseAdd (VAR r : ERes) ;
  3528. VAR op : CARDINAL ;
  3529. left, right : ERes ;
  3530. BEGIN
  3531. ParseMul (r) ;
  3532. LOOP
  3533. op := 0 ;
  3534. Skip () ;
  3535. IF CurCh () = '+' THEN
  3536. op := OpAdd ; DropCh (GetCh ())
  3537. ELSIF CurCh () = '-' THEN
  3538. op := OpSub ; DropCh (GetCh ())
  3539. ELSIF KwAhead ("OR") THEN
  3540. GetWord () ;
  3541. op := TkOr
  3542. ELSE
  3543. RETURN
  3544. END ;
  3545. left := r ;
  3546. IF left.cls = TSet THEN
  3547. (* Set union/difference. The left address goes on the stack BEFORE
  3548. the right is parsed: a DI-addressed set field must survive the
  3549. right-hand expression. `OR' is boolean, not set union. *)
  3550. IF (op = OpAdd) OR (op = OpSub) THEN
  3551. EmPushSetAddr (left) ;
  3552. ParseMul (right) ;
  3553. IF NOT OK () THEN
  3554. RETURN
  3555. END ;
  3556. IF right.cls # TSet THEN
  3557. Err (ETypeErr) ;
  3558. RETURN
  3559. END ;
  3560. IF op = OpAdd THEN
  3561. SetBinEmit (1, left, right, r)
  3562. ELSE
  3563. SetBinEmit (2, left, right, r)
  3564. END
  3565. ELSE
  3566. Err (ETypeErr) ;
  3567. RETURN
  3568. END
  3569. ELSE
  3570. SaveLeft (left) ;
  3571. ParseMul (right) ;
  3572. IF NOT OK () THEN
  3573. RETURN
  3574. END ;
  3575. IF right.cls = TSet THEN
  3576. Err (ETypeErr) ;
  3577. RETURN
  3578. END ;
  3579. BinOpEmit (op, left, right, r)
  3580. END
  3581. END
  3582. END ParseAdd ;
  3583. PROCEDURE ParseMul (VAR r : ERes) ;
  3584. VAR op : CARDINAL ;
  3585. left, right : ERes ;
  3586. BEGIN
  3587. ParseNeg (r) ;
  3588. LOOP
  3589. op := 0 ;
  3590. Skip () ;
  3591. IF CurCh () = '*' THEN
  3592. op := OpMul ; DropCh (GetCh ()) (* OpAdd here meant a*b -> a+b *)
  3593. ELSIF CurCh () = '/' THEN
  3594. op := 2 ; DropCh (GetCh ()) ; Err (ENoLib)
  3595. ELSIF KwAhead ("DIV") THEN
  3596. GetWord () ; op := TkDiv
  3597. ELSIF KwAhead ("MOD") THEN
  3598. GetWord () ; op := TkMod
  3599. ELSIF KwAhead ("AND") THEN
  3600. GetWord () ; op := TkAnd
  3601. ELSE
  3602. RETURN
  3603. END ;
  3604. IF op = 2 THEN
  3605. RETURN
  3606. END ;
  3607. left := r ;
  3608. IF left.cls = TSet THEN
  3609. (* Set intersection. Same push-before-the-right rule as ParseAdd. *)
  3610. IF op = OpMul THEN
  3611. EmPushSetAddr (left) ;
  3612. ParseNeg (right) ;
  3613. IF NOT OK () THEN
  3614. RETURN
  3615. END ;
  3616. IF right.cls # TSet THEN
  3617. Err (ETypeErr) ;
  3618. RETURN
  3619. END ;
  3620. SetBinEmit (0, left, right, r)
  3621. ELSE
  3622. Err (ETypeErr) ;
  3623. RETURN
  3624. END
  3625. ELSE
  3626. SaveLeft (left) ;
  3627. ParseNeg (right) ;
  3628. IF NOT OK () THEN
  3629. RETURN
  3630. END ;
  3631. IF right.cls = TSet THEN
  3632. Err (ETypeErr) ;
  3633. RETURN
  3634. END ;
  3635. BinOpEmit (op, left, right, r)
  3636. END
  3637. END
  3638. END ParseMul ;
  3639. PROCEDURE ParseNeg (VAR r : ERes) ;
  3640. BEGIN
  3641. Skip () ;
  3642. IF CurCh () = '+' THEN
  3643. DropCh (GetCh ()) ;
  3644. ParseNeg (r) ;
  3645. RETURN
  3646. ELSIF CurCh () = '-' THEN
  3647. DropCh (GetCh ()) ;
  3648. ParseNeg (r) ;
  3649. IF r.kind = 0 THEN
  3650. r.imm := VAL (LONGINT, W16 (0 - r.imm))
  3651. ELSE
  3652. LoadAtom (r) ;
  3653. EmNegAx () ;
  3654. r.kind := 2
  3655. END ;
  3656. RETURN
  3657. ELSIF KwAhead ("NOT") THEN
  3658. GetWord () ;
  3659. ParseNeg (r) ;
  3660. (* TPSRC9 neglevel branches on the operand's type before emitting
  3661. anything: CMP CL,#$0A (integer) -> NOT AX, CMP CL,#$0B (boolean)
  3662. -> XOR AL,#01, anything else -> error. This used to emit NOT AX
  3663. for both, so `not (a = a)' produced 0FFFEh and the runtime's
  3664. wrbool test ([BP+4] <> 0) read it as TRUE.
  3665. The integer constant fold stays: the 16 bits are the same either
  3666. way. A boolean constant must not go through BitNot - 0FFFFH-1 is
  3667. 0FFFEh, which wrbool also reads as TRUE - so it is loaded and
  3668. xor-ed, exactly as neglevel does it. *)
  3669. IF r.cls = TBool THEN
  3670. LoadAtom (r) ;
  3671. EmXorAl01 () ;
  3672. r.kind := 2
  3673. ELSIF r.cls = TScalar THEN
  3674. IF r.kind = 0 THEN
  3675. r.imm := BitNot (r.imm)
  3676. ELSE
  3677. LoadAtom (r) ;
  3678. EmNotAx () ;
  3679. r.kind := 2
  3680. END
  3681. ELSE
  3682. Err (ETypeErr)
  3683. END ;
  3684. RETURN
  3685. END ;
  3686. ParseAtom (r)
  3687. END ParseNeg ;
  3688. PROCEDURE WithLookup (nm : ARRAY OF CHAR ; VAR r : ERes) : BOOLEAN ;
  3689. (* Resolve a bare identifier against the active WITH scopes, innermost first.
  3690. ISO Pascal lets a record field shadow an outer declaration, so this is
  3691. consulted BEFORE Search. The result is filled exactly as a variable
  3692. reference: kind 1 (base symbol + boff) for a record variable or a
  3693. constant-index element, kind 5 (address in DI) for a run-time-indexed one,
  3694. where the base is reloaded from the WITH slot because the body may have
  3695. used DI. *)
  3696. VAR w, fi : CARDINAL ;
  3697. found : BOOLEAN ;
  3698. BEGIN
  3699. w := withN ;
  3700. WHILE w > 0 DO
  3701. DEC (w) ;
  3702. fi := withFF [w] ;
  3703. found := FALSE ;
  3704. WHILE (fi < withFL [w]) AND NOT found DO
  3705. IF NameEq (fields [fi].name, nm) THEN
  3706. found := TRUE
  3707. ELSE
  3708. INC (fi)
  3709. END
  3710. END ;
  3711. IF found THEN
  3712. IF withIsDI [w] THEN
  3713. EmLdDiGlob (withSlot [w]) ;
  3714. IF fields [fi].off # 0 THEN
  3715. EmAddDiImm (fields [fi].off)
  3716. END ;
  3717. r.kind := 5
  3718. ELSE
  3719. r.kind := 1 ;
  3720. r.setoff := symtab [withIdx [w]].off ;
  3721. r.boff := W16 (VAL (LONGINT, withBoff [w])
  3722. + VAL (LONGINT, fields [fi].off))
  3723. END ;
  3724. r.idx := withIdx [w] ;
  3725. r.cls := fields [fi].cls ;
  3726. r.sz := fields [fi].size ;
  3727. IF r.cls = TSet THEN
  3728. r.setbase := fields [fi].elem ;
  3729. r.setsize := fields [fi].size
  3730. END ;
  3731. r.chr := FALSE ;
  3732. RETURN TRUE
  3733. END
  3734. END ;
  3735. RETURN FALSE
  3736. END WithLookup ;
  3737. PROCEDURE ParseAtom (VAR r : ERes) ;
  3738. (* const | variable | func(params) | '(' expr ')' | '[' set ']' *)
  3739. VAR idx : CARDINAL ;
  3740. strf : BOOLEAN ;
  3741. quoted : BOOLEAN ;
  3742. ssize, sbase : CARDINAL ;
  3743. sbuf : ARRAY [0..31] OF CHAR ;
  3744. uj1, uj2 : CARDINAL ;
  3745. BEGIN
  3746. r.chr := FALSE ; (* default: not a quoted char literal *)
  3747. Skip () ;
  3748. IF CurCh () = '(' THEN
  3749. DropCh (GetCh ()) ;
  3750. ParseExpr (r) ;
  3751. ExpectDelim (')', ENoSemi) ;
  3752. RETURN
  3753. END ;
  3754. IF CurCh () = '[' THEN
  3755. (* An inline set literal. Full TP3 takes its type from context; here it
  3756. defaults to `set of Char' (0..255), which is what CALC writes
  3757. (`Ch in ['a'..'z']'), and its bytes are materialised in the code
  3758. stream. A typed/larger-base literal is a later milestone. *)
  3759. ssize := 32 ;
  3760. sbase := 0 ;
  3761. IF RdSetLiteral (sbase, ssize, sbuf) THEN
  3762. r.kind := 7 ;
  3763. r.cls := TSet ;
  3764. r.setoff := EmSetBlob (sbuf, ssize) ;
  3765. r.setbase := sbase ;
  3766. r.setsize := ssize
  3767. END ;
  3768. RETURN
  3769. END ;
  3770. IF (CurCh () = '$') OR (Digit (CurCh ())) OR (ORD (CurCh ()) = AposC)
  3771. OR (CurCh () = '^') OR (CurCh () = '#') THEN
  3772. (* remember that this was a quoted literal BEFORE RdConst consumes it:
  3773. RdConst reports a 1-character literal as TScalar (its char code),
  3774. which is right for "c := 'a'" but would make writeln('a') print 97.
  3775. Mark it so the writer picks the char entry, not the integer one. A
  3776. caret (^M) or decimal (#27) character constant is a character too. *)
  3777. quoted := (ORD (CurCh ()) = AposC) OR (CurCh () = '^')
  3778. OR (CurCh () = '#') ;
  3779. RdConst (r.imm, r.cls, strf) ;
  3780. r.chr := quoted AND (r.cls = TScalar) AND NOT strf ;
  3781. IF r.cls = TReal THEN
  3782. Err (ENoLib) ;
  3783. r.kind := 2 ;
  3784. RETURN
  3785. END ;
  3786. IF strf THEN
  3787. (* A string literal is legal here as a *value* - it is not rejected
  3788. at this point, because writeln('hi') needs it and IoCall is the
  3789. only place that knows how to emit one. Everywhere else the
  3790. literal has to end up as a machine word, and that is caught by
  3791. LoadAtom, which refuses a TString. *)
  3792. r.strx := rdStrX ;
  3793. r.kind := 3 ;
  3794. RETURN
  3795. END ;
  3796. r.kind := 0 ;
  3797. RETURN
  3798. END ;
  3799. IF NOT Alpha (CurCh ()) THEN
  3800. Err (EUnknown) ;
  3801. RETURN
  3802. END ;
  3803. GetWord () ;
  3804. IF WithLookup (wrd, r) THEN
  3805. ParseSub (r) ;
  3806. RETURN
  3807. END ;
  3808. IF NOT Search (wrd, idx) THEN
  3809. Err (EUnknown) ;
  3810. RETURN
  3811. END ;
  3812. IF symtab [idx].tag = KConst THEN
  3813. r.kind := 0 ;
  3814. r.imm := symtab [idx].lval ;
  3815. r.cls := symtab [idx].cls ;
  3816. RETURN
  3817. ELSIF symtab [idx].tag = KFunc THEN
  3818. ParseCall (idx) ;
  3819. IF symtab [idx].cls = TReal THEN
  3820. (* A REAL function returns into the caller's six-byte scratch; DI is
  3821. reloaded with its address so real consumers can use the value. *)
  3822. EmLeaDi (FALSE, symtab [callScratch].off) ;
  3823. r.kind := 5 ;
  3824. r.cls := TReal ;
  3825. r.sz := 6
  3826. ELSE
  3827. r.kind := 2 ;
  3828. r.cls := symtab [idx].cls
  3829. END ;
  3830. RETURN
  3831. ELSIF (symtab [idx].tag = KVar) OR (symtab [idx].tag = KType) THEN
  3832. (* ParseVar initialises boff to 0 and then ParseSub folds any constant
  3833. subscript into it. Do NOT reset boff here: doing so after ParseVar
  3834. returns threw away the subscript, so s[i] read/wrote off+0 (the
  3835. length byte) while still reporting itself as a char. This was
  3836. latent for arrays because an array cannot be declared yet, so no
  3837. fixture had ever exercised the fold. *)
  3838. ParseVar (r) ;
  3839. RETURN
  3840. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Chr) THEN
  3841. (* Chr(n): the character with code n. A constant folds; otherwise the
  3842. integer is loaded and its class becomes CHAR. The value is one byte,
  3843. so an assignment stores AL and a CHAR writer uses AL. *)
  3844. IF NOT MatchDelim ('(') THEN
  3845. Err (ENoSemi) ;
  3846. RETURN
  3847. END ;
  3848. ParseExpr (r) ;
  3849. ExpectDelim (')', ENoSemi) ;
  3850. IF NOT OK () THEN
  3851. RETURN
  3852. END ;
  3853. IF (r.cls # TScalar) AND (r.cls # TChar) THEN
  3854. Err (ETypeErr) ;
  3855. RETURN
  3856. END ;
  3857. IF r.kind = 0 THEN
  3858. r.imm := VAL (LONGINT, W16 (r.imm) MOD 256) ;
  3859. r.chr := TRUE
  3860. ELSE
  3861. LoadAtom (r) ;
  3862. r.kind := 2 ;
  3863. r.chr := FALSE
  3864. END ;
  3865. r.cls := TChar ;
  3866. r.sz := 1 ;
  3867. RETURN
  3868. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Eof) THEN
  3869. (* Eof(F): text/typed-file end of file. The runtime compares the DOS
  3870. current position with the file size and also remembers a ^Z seen by
  3871. the text reader. *)
  3872. IF NOT MatchDelim ('(') THEN
  3873. Err (ENoSemi) ;
  3874. RETURN
  3875. END ;
  3876. ParseExpr (r) ;
  3877. IF OK () THEN
  3878. IF (r.kind = 1) AND
  3879. ((r.cls = TText) OR (r.cls = TFile)) THEN
  3880. EmPushVarAddr (symtab [r.idx].local,
  3881. (symtab [r.idx].off + r.boff) MOD 10000H) ;
  3882. DropC (EmCall (TU_FileEof)) ;
  3883. EmAddSp (2) ;
  3884. r.kind := 2 ;
  3885. r.cls := TBool ;
  3886. r.chr := FALSE
  3887. ELSE
  3888. Err (ETypeErr)
  3889. END
  3890. END ;
  3891. ExpectDelim (')', ENoSemi) ;
  3892. RETURN
  3893. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_IoResult) THEN
  3894. (* IOResult: the last file operation's DOS status; reading it clears the
  3895. slot in the runtime. It is a no-argument function. *)
  3896. IF MatchDelim ('(') THEN
  3897. ExpectDelim (')', ENoSemi)
  3898. END ;
  3899. DropC (EmCall (TU_IoResult)) ;
  3900. r.kind := 2 ;
  3901. r.cls := TScalar ;
  3902. r.chr := FALSE ;
  3903. RETURN
  3904. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_UpCase) THEN
  3905. (* UpCase(c): a CHAR function. A constant folds; otherwise AL is
  3906. mapped 'a'..'z' -> 'A'..'Z' and left alone outside that range. The
  3907. test is JL/JG (signed), which is the same answer for 0..127 chars and
  3908. leaves 128..255 unchanged; it also stays inside the four branch
  3909. conditions this compiler declares (check_8086's set). *)
  3910. IF NOT MatchDelim ('(') THEN
  3911. Err (ENoSemi) ;
  3912. RETURN
  3913. END ;
  3914. ParseExpr (r) ;
  3915. ExpectDelim (')', ENoSemi) ;
  3916. IF NOT OK () THEN
  3917. RETURN
  3918. END ;
  3919. IF (r.cls # TChar) AND (r.cls # TScalar) THEN
  3920. Err (ETypeErr) ; (* the STRING form is not implemented *)
  3921. RETURN
  3922. END ;
  3923. IF r.kind = 0 THEN
  3924. IF (r.imm >= VAL (LONGINT, ORD ('a')))
  3925. AND (r.imm <= VAL (LONGINT, ORD ('z'))) THEN
  3926. r.imm := r.imm - 32
  3927. END ;
  3928. r.kind := 0 ;
  3929. r.cls := TChar ;
  3930. r.chr := TRUE ;
  3931. RETURN
  3932. END ;
  3933. LoadAtom (r) ;
  3934. EmCmpAlImm (ORD ('a')) ;
  3935. uj1 := EmJcc (8CH, 0) ; (* JL -> leave it *)
  3936. EmCmpAlImm (ORD ('z')) ;
  3937. uj2 := EmJcc (8FH, 0) ; (* JG -> leave it *)
  3938. EmSubAlImm (32) ;
  3939. SetPatTgt (uj1, pc) ;
  3940. SetPatTgt (uj2, pc) ;
  3941. EmMovAh0 () ;
  3942. r.kind := 2 ;
  3943. r.cls := TChar ;
  3944. r.chr := FALSE ;
  3945. RETURN
  3946. ELSIF (symtab [idx].tag = KBuiltin) AND
  3947. ((symtab [idx].cls = BI_Succ) OR (symtab [idx].cls = BI_Pred)) THEN
  3948. (* Succ(x) / Pred(x) on an ordinal. CALC's `FX := Succ(FX)' is the
  3949. first use. A constant folds; otherwise load the value and INC/DEC
  3950. it, preserving the operand's class so an assignment back to a CHAR
  3951. subrange stores one byte as before. *)
  3952. IF NOT MatchDelim ('(') THEN
  3953. Err (ENoSemi) ;
  3954. RETURN
  3955. END ;
  3956. ParseExpr (r) ;
  3957. ExpectDelim (')', ENoSemi) ;
  3958. IF NOT OK () THEN
  3959. RETURN
  3960. END ;
  3961. IF (r.cls # TChar) AND (r.cls # TScalar) THEN
  3962. Err (ETypeErr) ;
  3963. RETURN
  3964. END ;
  3965. IF r.kind = 0 THEN
  3966. IF symtab [idx].cls = BI_Succ THEN
  3967. r.imm := VAL (LONGINT, W16 (r.imm + 1))
  3968. ELSE
  3969. r.imm := VAL (LONGINT, W16 (r.imm - 1))
  3970. END
  3971. ELSE
  3972. LoadAtom (r) ;
  3973. IF symtab [idx].cls = BI_Succ THEN
  3974. EmIncAx ()
  3975. ELSE
  3976. EmDecAx ()
  3977. END ;
  3978. r.kind := 2
  3979. END ;
  3980. r.chr := FALSE ;
  3981. RETURN
  3982. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_KeyPressed) THEN
  3983. (* KeyPressed: a boolean FUNCTION with no argument, and TP3 lets it be
  3984. written without a parameter list (`until KeyPressed'). *)
  3985. EmCrtSeq (CrtKey) ;
  3986. r.kind := 2 ;
  3987. r.cls := TBool ;
  3988. r.chr := FALSE ;
  3989. RETURN
  3990. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Length) THEN
  3991. (* LENGTH (s): the string's length byte, loaded inline. No runtime
  3992. entry, and no address is pushed - a string's value already carries its
  3993. length at [addr]. A literal is emitted inline (EmStrBlob) and the
  3994. byte is read from that image offset. *)
  3995. IF NOT MatchDelim ('(') THEN
  3996. Err (ENoSemi) ;
  3997. RETURN
  3998. END ;
  3999. ParseExpr (r) ;
  4000. ExpectDelim (')', ENoSemi) ;
  4001. IF NOT OK () THEN
  4002. RETURN
  4003. END ;
  4004. IF r.cls # TString THEN
  4005. Err (ETypeErr) ;
  4006. RETURN
  4007. END ;
  4008. IF (r.kind = 1) AND symtab [r.idx].isVar THEN
  4009. (* VAR string: the slot holds the address; read the length byte
  4010. through that pointer. *)
  4011. IF r.boff # 0 THEN
  4012. Err (ENoLib)
  4013. ELSE
  4014. EmLoadVarInd (symtab [r.idx].off, 1)
  4015. END
  4016. ELSIF r.kind = 1 THEN
  4017. EmLoadVar (symtab [r.idx].local,
  4018. (symtab [r.idx].off + r.boff) MOD 10000H, 1)
  4019. ELSIF r.kind = 3 THEN
  4020. EmLoadVar (FALSE, EmStrBlob (r), 1)
  4021. ELSE
  4022. Err (ENoLib) ;
  4023. RETURN
  4024. END ;
  4025. EmMovAh0 () ;
  4026. r.kind := 2 ;
  4027. r.cls := TScalar ;
  4028. r.chr := FALSE ;
  4029. RETURN
  4030. ELSE
  4031. Err (EUnknown)
  4032. END
  4033. END ParseAtom ;
  4034. PROCEDURE AddPend (kind, who, place : CARDINAL) ;
  4035. BEGIN
  4036. IF nPend < MaxPend THEN
  4037. pend [nPend].kind := kind ;
  4038. pend [nPend].who := who ;
  4039. pend [nPend].place := place ;
  4040. INC (nPend)
  4041. ELSE
  4042. Err (ECompOvf)
  4043. END
  4044. END AddPend ;
  4045. PROCEDURE ParmSlot (idx, k : CARDINAL) : CARDINAL ;
  4046. (* The stack slot of argument k (0-based) of procedure/function idx, from the
  4047. parameter list recorded on the symbol. Out of range means the source passed
  4048. more arguments than there are parameters: keep the old two-byte assumption
  4049. rather than inventing an arity error here. *)
  4050. VAR pi : CARDINAL ;
  4051. BEGIN
  4052. IF (symtab [idx].parmCount = 0) OR (k >= symtab [idx].parmCount) THEN
  4053. RETURN 2
  4054. END ;
  4055. pi := symtab [idx].parmFirst + k ;
  4056. RETURN ParamSlotBytes (symtab [pi])
  4057. END ParmSlot ;
  4058. PROCEDURE ParmIsVar (idx, k : CARDINAL) : BOOLEAN ;
  4059. (* Is argument k (0-based) of procedure/function idx declared VAR? Out of
  4060. range means the source passed more arguments than there are formals; treat
  4061. it as a value parameter, as ParmSlot already does. *)
  4062. VAR pi : CARDINAL ;
  4063. BEGIN
  4064. IF (symtab [idx].parmCount = 0) OR (k >= symtab [idx].parmCount) THEN
  4065. RETURN FALSE
  4066. END ;
  4067. pi := symtab [idx].parmFirst + k ;
  4068. RETURN symtab [pi].isVar
  4069. END ParmIsVar ;
  4070. PROCEDURE EmCallMost (idx, cleanup : CARDINAL) ;
  4071. (* emit the call to sym 'idx' and clean up `cleanup' bytes of arguments, which
  4072. is the SUM of their slot sizes - not 2 per argument once a slot can be
  4073. larger than a word. *)
  4074. VAR p : CARDINAL ;
  4075. BEGIN
  4076. IF symtab [idx].defnd THEN
  4077. DropC (EmCall (symtab [idx].goPos))
  4078. ELSE
  4079. p := EmCall (0) ;
  4080. AddPend (1, idx, p)
  4081. END ;
  4082. IF cleanup > 0 THEN
  4083. EmAddSp (cleanup)
  4084. END
  4085. END EmCallMost ;
  4086. PROCEDURE ParseCallArgs (idx : CARDINAL) ;
  4087. (* '(' already consumed: read args ')' then call.
  4088. Each argument is loaded and pushed the moment it has been parsed, which is
  4089. what TPSRC8 cproc/cprlp1/cprlp2 do - CALL exprsave, CALL epushax, one per
  4090. argument, then the CALL. A computed argument (kind 2) exists only in AX,
  4091. so parsing the argument after it would destroy it before the push loop
  4092. ever reached it: `p (a > b, x)' passed x twice.
  4093. Pushing in parse order is also the order the callee is laid out for: the
  4094. first argument goes on first and so ends up farthest from BP, and the LAST
  4095. declared parameter is the one at BP+4 (RESUME-TP3.md 3.11). *)
  4096. VAR arg : ERes ;
  4097. nArgs, cleanup, slot, nCopy : CARDINAL ;
  4098. BEGIN
  4099. nArgs := 0 ;
  4100. cleanup := 0 ;
  4101. IF CurCh () = ')' THEN
  4102. DropCh (GetCh ())
  4103. ELSE
  4104. LOOP
  4105. IF nArgs >= 16 THEN
  4106. Err (ECompOvf) ;
  4107. EXIT
  4108. END ;
  4109. ParseExpr (arg) ;
  4110. slot := ParmSlot (idx, nArgs) ;
  4111. IF ParmIsVar (idx, nArgs) THEN
  4112. (* VAR formal: push the actual's ADDRESS. Only a variable has
  4113. one; a constant or a computed value is refused rather than
  4114. quietly passed by value. An element with a constant index
  4115. (`s[i]') is a variable too: its address is off+boff, which
  4116. EmPushVarAddr forms directly. *)
  4117. IF arg.kind = 5 THEN
  4118. (* An element/field address already in DI, including a record
  4119. field passed as a VAR actual: Sheet[I,J].Contents. *)
  4120. EmPushDi ()
  4121. ELSIF arg.kind # 1 THEN
  4122. Err (ENoLib) ;
  4123. EXIT
  4124. ELSIF symtab [arg.idx].isVar THEN
  4125. (* Passing a VAR parameter on: its slot already HOLDS the
  4126. address, so push that, not the address of the slot. *)
  4127. IF arg.boff # 0 THEN
  4128. Err (ENoLib) ;
  4129. EXIT
  4130. END ;
  4131. EmLoadVar (TRUE, symtab [arg.idx].off, 2) ;
  4132. EmPushAx ()
  4133. ELSE
  4134. EmPushVarAddr (symtab [arg.idx].local,
  4135. (symtab [arg.idx].off + arg.boff) MOD 10000H)
  4136. END
  4137. ELSIF slot > 2 THEN
  4138. (* A STRING VALUE parameter: push its whole slot, copied.
  4139. `copyN' is at most the slot: an actual shorter than the formal
  4140. leaves the tail untouched, which is harmless because the length
  4141. byte bounds every read. *)
  4142. IF arg.cls = TString THEN
  4143. nCopy := slot ;
  4144. IF arg.kind = 1 THEN
  4145. IF VarSize (arg) < nCopy THEN
  4146. nCopy := VarSize (arg)
  4147. END
  4148. ELSIF arg.kind = 3 THEN
  4149. IF strLen [arg.strx] + 1 < nCopy THEN
  4150. nCopy := strLen [arg.strx] + 1
  4151. END
  4152. ELSE
  4153. Err (ENoLib) ; (* a computed string is not a value *)
  4154. EXIT
  4155. END ;
  4156. EmPushStrSlot (arg, slot, nCopy)
  4157. ELSIF arg.cls = TReal THEN
  4158. EmPushRealSlot (arg, slot)
  4159. ELSE
  4160. Err (ETypeErr) ;
  4161. EXIT
  4162. END
  4163. ELSE
  4164. LoadAtom (arg) ;
  4165. EmPushAx ()
  4166. END ;
  4167. cleanup := cleanup + slot ;
  4168. INC (nArgs) ;
  4169. IF NOT MatchDelim (',') THEN
  4170. EXIT
  4171. END
  4172. END ;
  4173. ExpectDelim (')', ENoSemi)
  4174. END ;
  4175. EmCallMost (idx, cleanup)
  4176. END ParseCallArgs ;
  4177. PROCEDURE ParseCall (idx : CARDINAL) ;
  4178. (* procedure/function call; '(' optional - same parse-then-push as above,
  4179. reached from ParseAtom when a FUNCTION is called in an expression *)
  4180. VAR arg : ERes ;
  4181. nArgs, cleanup, slot, nCopy : CARDINAL ;
  4182. BEGIN
  4183. nArgs := 0 ;
  4184. cleanup := 0 ;
  4185. IF MatchDelim ('(') THEN
  4186. IF CurCh () # ')' THEN
  4187. LOOP
  4188. IF nArgs >= 16 THEN
  4189. Err (ECompOvf) ;
  4190. EXIT
  4191. END ;
  4192. ParseExpr (arg) ;
  4193. slot := ParmSlot (idx, nArgs) ;
  4194. IF ParmIsVar (idx, nArgs) THEN
  4195. (* A VAR formal in a FUNCTION call: the callee wants the
  4196. address, exactly as ParseCallArgs does for a procedure. This
  4197. path was missing, so `Length(s)' inside a function whose
  4198. formal is `var s' pushed the string's VALUE and the callee
  4199. would have read it as an address. *)
  4200. IF arg.kind = 5 THEN
  4201. EmPushDi ()
  4202. ELSIF arg.kind # 1 THEN
  4203. Err (ENoLib) ;
  4204. EXIT
  4205. ELSIF symtab [arg.idx].isVar THEN
  4206. IF arg.boff # 0 THEN
  4207. Err (ENoLib) ;
  4208. EXIT
  4209. END ;
  4210. EmLoadVar (TRUE, symtab [arg.idx].off, 2) ;
  4211. EmPushAx ()
  4212. ELSE
  4213. EmPushVarAddr (symtab [arg.idx].local,
  4214. (symtab [arg.idx].off + arg.boff) MOD 10000H)
  4215. END
  4216. ELSIF slot > 2 THEN
  4217. (* see ParseCallArgs: push the whole string slot *)
  4218. IF arg.cls = TString THEN
  4219. nCopy := slot ;
  4220. IF arg.kind = 1 THEN
  4221. IF VarSize (arg) < nCopy THEN
  4222. nCopy := VarSize (arg)
  4223. END
  4224. ELSIF arg.kind = 3 THEN
  4225. IF strLen [arg.strx] + 1 < nCopy THEN
  4226. nCopy := strLen [arg.strx] + 1
  4227. END
  4228. ELSE
  4229. Err (ENoLib) ;
  4230. EXIT
  4231. END ;
  4232. EmPushStrSlot (arg, slot, nCopy)
  4233. ELSIF arg.cls = TReal THEN
  4234. EmPushRealSlot (arg, slot)
  4235. ELSE
  4236. Err (ETypeErr) ;
  4237. EXIT
  4238. END
  4239. ELSE
  4240. LoadAtom (arg) ;
  4241. EmPushAx ()
  4242. END ;
  4243. cleanup := cleanup + slot ;
  4244. INC (nArgs) ;
  4245. IF NOT MatchDelim (',') THEN
  4246. EXIT
  4247. END
  4248. END ;
  4249. ExpectDelim (')', ENoSemi)
  4250. ELSE
  4251. DropCh (GetCh ())
  4252. END
  4253. END ;
  4254. callScratch := 0 ;
  4255. IF symtab [idx].cls = TReal THEN
  4256. callScratch := NewScratchReal () ;
  4257. EmLeaDi (FALSE, symtab [callScratch].off)
  4258. END ;
  4259. EmCallMost (idx, cleanup)
  4260. END ParseCall ;
  4261. PROCEDURE ParseExpr (VAR r : ERes) ;
  4262. BEGIN
  4263. ParseCmp (r)
  4264. END ParseExpr ;
  4265. (* ---------------------------------------------------------------- *)
  4266. (* statements (TPSRC8) *)
  4267. (* ---------------------------------------------------------------- *)
  4268. PROCEDURE ParseLabelStmt () ;
  4269. (* numeric label definition 'n :' *)
  4270. VAR n : CARDINAL ;
  4271. nm : ARRAY [0..9] OF CHAR ;
  4272. idx : CARDINAL ;
  4273. i : CARDINAL ;
  4274. BEGIN
  4275. n := 0 ;
  4276. WHILE Digit (CurCh ()) DO
  4277. n := n * 10 + (ORD (CurCh ()) - ORD ('0')) ;
  4278. DropCh (GetCh ())
  4279. END ;
  4280. ExpectDelim (':', ENoSemi) ;
  4281. NumToName (n, nm) ;
  4282. IF Search (nm, idx) THEN
  4283. IF symtab [idx].tag = KLabel THEN
  4284. symtab [idx].defnd := TRUE ;
  4285. symtab [idx].goPos := pc ;
  4286. i := 0 ;
  4287. WHILE i < nPend DO
  4288. IF (pend [i].kind = 0) AND (pend [i].who = idx) THEN
  4289. SetPatTgt (pend [i].place, pc) ;
  4290. pend [i].kind := 99
  4291. END ;
  4292. INC (i)
  4293. END
  4294. ELSE
  4295. Err (EUnknown)
  4296. END
  4297. ELSE
  4298. idx := NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE) ;
  4299. symtab [idx].defnd := TRUE ;
  4300. symtab [idx].goPos := pc
  4301. END
  4302. END ParseLabelStmt ;
  4303. PROCEDURE IntToMbf (v : LONGINT ; VAR b : ARRAY OF CHAR) ;
  4304. (* TP3's 6-byte Real, as measured in CALC-PORT.md against the cells holding
  4305. 1, 2, 3, 4 and 5:
  4306. value = (1 + N / 2^39) * 2^(E - 129)
  4307. byte 0 is E (biased by 129); bytes 1..5 are N, little-endian; the SIGN is
  4308. bit 7 of byte 5 (the book's `sign + 39-bit mantissa'). An all-zero value
  4309. is 0.0. 1 -> 81 00 00 00 00 00, 3 -> 82 00 00 00 00 40,
  4310. 5 -> 83 00 00 00 00 20 - all cross-checked against CALCDEMO.MCS.
  4311. Only INTEGER values are converted; real arithmetic and formatting are a
  4312. later stage, and a real RHS that is not a constant or a copy is ENoLib. *)
  4313. VAR a, p, n : LONGINT ;
  4314. e, k, i : CARDINAL ;
  4315. neg : BOOLEAN ;
  4316. BEGIN
  4317. i := 0 ;
  4318. WHILE i < 6 DO
  4319. b [i] := CHR (0) ;
  4320. INC (i)
  4321. END ;
  4322. IF v = 0 THEN
  4323. RETURN
  4324. END ;
  4325. neg := v < 0 ;
  4326. IF neg THEN
  4327. a := -v
  4328. ELSE
  4329. a := v
  4330. END ;
  4331. (* k = floor(log2(a)); p = 2^k *)
  4332. p := 1 ;
  4333. k := 0 ;
  4334. WHILE (k < 39) AND (p * 2 <= a) DO
  4335. p := p * 2 ;
  4336. INC (k)
  4337. END ;
  4338. IF k >= 39 THEN
  4339. Err (EConstRange) ;
  4340. RETURN
  4341. END ;
  4342. e := 129 + k ;
  4343. b [0] := CHR (e) ;
  4344. n := a - p ;
  4345. i := 39 - k ;
  4346. WHILE i > 0 DO
  4347. n := n * 2 ;
  4348. DEC (i)
  4349. END ;
  4350. i := 1 ;
  4351. WHILE i <= 5 DO
  4352. b [i] := CHR (VAL (CARDINAL, n MOD 256)) ;
  4353. n := n DIV 256 ;
  4354. INC (i)
  4355. END ;
  4356. IF neg THEN
  4357. b [5] := CHR (ORD (b [5]) + 128)
  4358. END
  4359. END IntToMbf ;
  4360. PROCEDURE Assignment (r : ERes) ;
  4361. (* ':=' already consumed by the caller; store expression into r *)
  4362. VAR src : ERes ;
  4363. k, n : CARDINAL ;
  4364. sbuf : ARRAY [0..31] OF CHAR ;
  4365. rbuf : ARRAY [0..5] OF CHAR ;
  4366. BEGIN
  4367. IF (r.kind # 1) AND (r.kind # 5) THEN
  4368. Err (EUnknown) ;
  4369. RETURN
  4370. END ;
  4371. IF r.cls = TReal THEN
  4372. (* A 6-byte REAL. A compile-time ordinal constant is converted to the
  4373. measured MBF layout; another REAL field is copied byte for byte.
  4374. Real arithmetic and real-valued expressions are a later stage and
  4375. are refused, not guessed. *)
  4376. IF (r.kind = 1) AND symtab [r.idx].isVar THEN
  4377. (* VAR REAL parameter: the slot holds the destination address. *)
  4378. ParseExpr (src) ;
  4379. IF NOT OK () THEN
  4380. RETURN
  4381. END ;
  4382. IF (src.kind = 0) AND (src.cls # TString) AND (src.cls # TSet) THEN
  4383. IntToMbf (src.imm, rbuf) ;
  4384. EmLoadVar (TRUE, symtab [r.idx].off, 2) ;
  4385. EmMovDiAx () ;
  4386. k := 0 ;
  4387. WHILE k < 6 DO
  4388. EmStDiImm (k, ORD (rbuf [k])) ;
  4389. INC (k)
  4390. END ;
  4391. RETURN
  4392. END ;
  4393. IF (src.cls = TReal) AND ((src.kind = 1) OR (src.kind = 5)) THEN
  4394. (* Destination pointer first, then the source address; pop SI
  4395. (source), DI (destination), and copy six bytes. *)
  4396. EmLoadVar (TRUE, symtab [r.idx].off, 2) ;
  4397. EmPushAx () ;
  4398. IF src.kind = 5 THEN
  4399. EmPushDi ()
  4400. ELSIF symtab [src.idx].isVar THEN
  4401. EmLoadVar (TRUE, symtab [src.idx].off, 2) ;
  4402. EmPushAx ()
  4403. ELSE
  4404. EmPushVarAddr (symtab [src.idx].local,
  4405. (symtab [src.idx].off + src.boff) MOD 10000H)
  4406. END ;
  4407. EmPopSi () ;
  4408. EmPopDi () ;
  4409. k := 0 ;
  4410. WHILE k < 6 DO
  4411. EmLdAlSi () ;
  4412. EmStDiAl () ;
  4413. EmIncSi () ;
  4414. EmIncDi () ;
  4415. INC (k)
  4416. END ;
  4417. RETURN
  4418. END ;
  4419. Err (ENoLib) ;
  4420. RETURN
  4421. END ;
  4422. ParseExpr (src) ;
  4423. IF NOT OK () THEN
  4424. RETURN
  4425. END ;
  4426. IF (src.kind = 0) AND (src.cls # TString) AND (src.cls # TSet) THEN
  4427. IntToMbf (src.imm, rbuf) ;
  4428. IF r.kind = 5 THEN
  4429. k := 0 ;
  4430. WHILE k < 6 DO
  4431. EmStDiImm (k, ORD (rbuf [k])) ;
  4432. INC (k)
  4433. END
  4434. ELSIF r.kind = 1 THEN
  4435. k := 0 ;
  4436. WHILE k < 6 DO
  4437. EmMovAxi (ORD (rbuf [k])) ;
  4438. EmStoreVar (symtab [r.idx].local,
  4439. (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  4440. INC (k)
  4441. END
  4442. ELSE
  4443. Err (ENoLib)
  4444. END ;
  4445. RETURN
  4446. END ;
  4447. IF (src.kind = 1) AND (src.cls = TReal) THEN
  4448. IF r.kind = 5 THEN
  4449. IF src.kind = 5 THEN
  4450. Err (ENoLib) ;
  4451. RETURN
  4452. END ;
  4453. k := 0 ;
  4454. WHILE k < 6 DO
  4455. EmLoadVar (symtab [src.idx].local,
  4456. (symtab [src.idx].off + src.boff + k) MOD 10000H, 1) ;
  4457. EmStDiAlOff (k) ;
  4458. INC (k)
  4459. END
  4460. ELSIF r.kind = 1 THEN
  4461. k := 0 ;
  4462. WHILE k < 6 DO
  4463. EmLoadVar (symtab [src.idx].local,
  4464. (symtab [src.idx].off + src.boff + k) MOD 10000H, 1) ;
  4465. EmStoreVar (symtab [r.idx].local,
  4466. (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  4467. INC (k)
  4468. END
  4469. ELSE
  4470. Err (ENoLib)
  4471. END ;
  4472. RETURN
  4473. END ;
  4474. Err (ENoLib) ;
  4475. RETURN
  4476. END ;
  4477. IF r.cls = TSet THEN
  4478. (* `field := [elements]'. The constructor's TYPE comes from the target:
  4479. CALC's CellStatus is `set of Attributes' (one byte), not the default
  4480. 32-byte `set of Char' an untyped literal would become. The bytes are
  4481. known at compile time; store them at symbol+boff or through DI. *)
  4482. IF (r.setsize = 0) OR (r.setsize > 32) THEN
  4483. Err (ENoLib) ;
  4484. RETURN
  4485. END ;
  4486. Skip () ;
  4487. IF CurCh () # '[' THEN
  4488. (* `set := set' or a set expression. The destination is a variable
  4489. or a DI-addressed field; save the DI address BEFORE the right-hand
  4490. expression can reuse DI, then copy the runtime's bytes. *)
  4491. IF r.kind = 5 THEN
  4492. EmPushDi ()
  4493. END ;
  4494. ParseExpr (src) ;
  4495. IF NOT OK () THEN
  4496. RETURN
  4497. END ;
  4498. IF src.cls # TSet THEN
  4499. Err (ETypeErr) ;
  4500. RETURN
  4501. END ;
  4502. IF r.kind = 1 THEN
  4503. EmPushVarAddr (symtab [r.idx].local,
  4504. (symtab [r.idx].off + r.boff) MOD 10000H)
  4505. ELSIF r.kind # 5 THEN
  4506. Err (ENoLib) ;
  4507. RETURN
  4508. END ;
  4509. EmPushSetAddr (src) ;
  4510. n := r.setsize ;
  4511. IF (src.kind = 8) AND (src.setsize < n) THEN
  4512. n := src.setsize
  4513. END ;
  4514. EmMovAxi (n) ;
  4515. EmPushAx () ;
  4516. DropC (EmCall (TU_SetCopy)) ;
  4517. EmAddSp (4) ;
  4518. RETURN
  4519. END ;
  4520. IF NOT RdSetLiteral (r.setbase, r.setsize, sbuf) THEN
  4521. RETURN
  4522. END ;
  4523. IF r.kind = 5 THEN
  4524. k := 0 ;
  4525. WHILE k < r.setsize DO
  4526. EmStDiImm (k, ORD (sbuf [k])) ;
  4527. INC (k)
  4528. END
  4529. ELSIF r.kind = 1 THEN
  4530. k := 0 ;
  4531. WHILE k < r.setsize DO
  4532. EmMovAxi (ORD (sbuf [k])) ;
  4533. EmStoreVar (symtab [r.idx].local,
  4534. (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  4535. INC (k)
  4536. END
  4537. ELSE
  4538. Err (ENoLib)
  4539. END ;
  4540. RETURN
  4541. END ;
  4542. IF (r.kind = 5) AND (r.cls = TString) THEN
  4543. (* A string field of a DI-addressed record element (CALC's
  4544. `with Sheet[I,J] do Contents := '''). DI is the field's address.
  4545. A literal's bytes are known: store <len><chars>, truncated to the
  4546. field's capacity (size-1). A string VARIABLE source uses strstore,
  4547. with DI pushed as the destination. *)
  4548. ParseExpr (src) ;
  4549. IF NOT OK () THEN
  4550. RETURN
  4551. END ;
  4552. n := VarSize (r) - 1 ; (* usable characters *)
  4553. IF src.kind = 3 THEN
  4554. IF strLen [src.strx] < n THEN
  4555. n := strLen [src.strx]
  4556. END ;
  4557. EmStDiImm (0, n) ; (* the length byte *)
  4558. k := 1 ;
  4559. WHILE k <= n DO
  4560. EmStDiImm (k, ORD (strPool [strOff [src.strx] + k - 1])) ;
  4561. INC (k)
  4562. END
  4563. ELSIF src.kind = 1 THEN
  4564. EmPushVarAddr (symtab [src.idx].local,
  4565. (symtab [src.idx].off + src.boff) MOD 10000H) ;
  4566. EmPushDi () ; (* destination = the field *)
  4567. EmMovCxi (n) ; (* strstore's limit *)
  4568. DropC (EmCall (TU_StrStore)) ;
  4569. EmAddSp (4)
  4570. ELSE
  4571. Err (ENoLib)
  4572. END ;
  4573. RETURN
  4574. END ;
  4575. IF (r.kind = 1) AND (r.cls = TString) AND symtab [r.idx].isVar THEN
  4576. (* A VAR string parameter: the slot holds the caller's string address.
  4577. Copy through the runtime, exactly as a normal string variable does:
  4578. source address first, destination address second, limit in CX. This
  4579. covers literals and whole-string expressions alike; the destination
  4580. address is loaded from the parameter slot rather than formed with
  4581. LEA, which would point at the slot itself. *)
  4582. ParseExpr (src) ;
  4583. IF NOT OK () THEN
  4584. RETURN
  4585. END ;
  4586. IF src.cls # TString THEN
  4587. Err (ETypeErr) ;
  4588. RETURN
  4589. END ;
  4590. EmPushStrAddr (src) ;
  4591. EmLoadVar (TRUE, symtab [r.idx].off, 2) ;
  4592. EmPushAx () ;
  4593. EmMovCxi ((VarSize (r) - 1) MOD 10000H) ;
  4594. DropC (EmCall (TU_StrStore)) ;
  4595. EmAddSp (4) ;
  4596. RETURN
  4597. END ;
  4598. IF (r.kind = 1) AND symtab [r.idx].isVar THEN
  4599. (* `varparam := expr': store through the address the slot holds. DI is
  4600. parked across the right-hand side because that expression may use DI
  4601. for a string element of its own. *)
  4602. IF r.boff # 0 THEN
  4603. Err (ENoLib) ;
  4604. RETURN
  4605. END ;
  4606. EmStoreVarIndPrep (symtab [r.idx].off) ;
  4607. ParseExpr (src) ;
  4608. LoadAtom (src) ;
  4609. EmPopDi () ;
  4610. EmStoreInd (VarSize (r)) ;
  4611. RETURN
  4612. END ;
  4613. IF (r.cls = TString) AND (VarSize (r) > 1) THEN
  4614. (* s := <string literal>. TPSRC8 asn2 sends a string through the scalar
  4615. store path, and estcpl's string arm emits `MOV CL,max_len' (max_len =
  4616. varsize-1) then `CALL strstore', which copies the source - length byte
  4617. and characters - into the destination and TRUNCATES a source longer
  4618. than max_len by rewriting the length byte (TPSRC2 strstore's
  4619. `CMP.B CL,AL / JBE stslenok / MOV.B [SI],AL'). Here the literal is
  4620. known at compile time, so its bytes become immediate stores instead of
  4621. a stack push and a block move. The variable that results is the same,
  4622. truncation included, and the tail past the new length is left as it
  4623. was, exactly as strstore leaves it. *)
  4624. ParseExpr (src) ;
  4625. IF (src.cls # TString) THEN
  4626. Err (ENoLib) ;
  4627. RETURN
  4628. END ;
  4629. IF src.kind # 3 THEN
  4630. (* s := <string variable>. TPSRC8 asnvar pushes the destination
  4631. address, then the expression (strload) pushes the source, and
  4632. estcpl emits MOV CL,max_len then CALL strstore, which copies
  4633. length byte + chars, TRUNCATING to max_len and leaving the
  4634. destination tail past the new length untouched. Here both
  4635. addresses are pushed - source first, so it sits at [BP+6] and the
  4636. destination at [BP+4] - the caller pops both, and the limit is a
  4637. full MOV CX,imm16 because this runtime reads it in CX outright. *)
  4638. IF src.kind = 1 THEN
  4639. EmPushVarAddr (symtab [src.idx].local,
  4640. (symtab [src.idx].off + src.boff) MOD 10000H)
  4641. ELSIF src.kind = 5 THEN
  4642. (* A string element reached through DI, including a pointer
  4643. dereference: Buffer^[i]. Its address is already in DI. *)
  4644. EmPushDi ()
  4645. ELSE
  4646. Err (ENoLib) ; (* a computed string is not a value *)
  4647. RETURN
  4648. END ;
  4649. EmPushVarAddr (symtab [r.idx].local,
  4650. (symtab [r.idx].off + r.boff) MOD 10000H) ;
  4651. EmMovCxi ((VarSize (r) - 1) MOD 10000H) ;
  4652. DropC (EmCall (TU_StrStore)) ;
  4653. EmAddSp (4) ;
  4654. RETURN
  4655. END ;
  4656. n := strLen [src.strx] ;
  4657. IF n > VarSize (r) - 1 THEN
  4658. n := VarSize (r) - 1
  4659. END ;
  4660. EmMovAxi (n) ;
  4661. EmStoreVar (symtab [r.idx].local,
  4662. (symtab [r.idx].off + r.boff) MOD 10000H, 1) ;
  4663. k := 0 ;
  4664. WHILE k < n DO
  4665. EmMovAxi (ORD (strPool [strOff [src.strx] + k])) ;
  4666. EmStoreVar (symtab [r.idx].local,
  4667. (symtab [r.idx].off + r.boff + 1 + k) MOD 10000H, 1) ;
  4668. INC (k)
  4669. END ;
  4670. RETURN
  4671. END ;
  4672. IF (r.kind = 5) OR (VarSize (r) > 2) THEN
  4673. IF r.kind = 5 THEN
  4674. (* An element addressed by DI: a string character (one byte) or an
  4675. array element (one or two). A wider element - a record - is
  4676. refused rather than stored as its first two bytes.
  4677. The destination address is in DI, and the right-hand side is
  4678. parsed next, so DI has to be saved first - the RHS may hold a
  4679. subscript of its own, which reuses DI, or call a routine that
  4680. clobbers it. This is TPSRC8 asnscal2/asnscal3 (758-760, 786-789),
  4681. which set flgpshdi around the expression for the same reason. *)
  4682. IF VarSize (r) > 2 THEN
  4683. Err (ENoLib) ;
  4684. RETURN
  4685. END ;
  4686. EmPushDi () ;
  4687. ParseExpr (src) ;
  4688. LoadAtom (src) ;
  4689. EmPopDi () ; (* DI := the destination again *)
  4690. EmStoreInd (VarSize (r)) ; (* [DI] := AL or AX *)
  4691. RETURN
  4692. END ;
  4693. (* A value wider than a word with no address to store through: an
  4694. assignment to a whole string/record/array is not a scalar store. *)
  4695. Err (ENoLib) ;
  4696. RETURN
  4697. END ;
  4698. ParseExpr (src) ;
  4699. LoadAtom (src) ;
  4700. EmStoreVar (symtab [r.idx].local,
  4701. (symtab [r.idx].off + r.boff) MOD 10000H,
  4702. VarSize (r))
  4703. END Assignment ;
  4704. PROCEDURE Compound () ;
  4705. (* BEGIN statement ';' ... END; END is consumed here *)
  4706. VAR tok : CARDINAL ;
  4707. BEGIN
  4708. LOOP
  4709. PeekKw (tok) ;
  4710. IF tok = TkEnd THEN
  4711. DropB (MatchKey (tok)) ;
  4712. RETURN
  4713. END ;
  4714. Statmnt () ;
  4715. IF NOT OK () THEN
  4716. RETURN
  4717. END ;
  4718. IF NOT MatchDelim (';') THEN
  4719. PeekKw (tok) ;
  4720. IF tok = TkEnd THEN
  4721. DropB (MatchKey (tok)) ;
  4722. RETURN
  4723. END ;
  4724. Err (ENoSemi) ;
  4725. RETURN
  4726. END
  4727. END
  4728. END Compound ;
  4729. PROCEDURE EmCrtSeq (which : CARDINAL) ;
  4730. (* The CRT byte sequences, in one parameterised helper. Every one is a fixed
  4731. BIOS call:
  4732. CrtClr - INT 10h AH=06 blank the whole window, then AH=02 home the cursor
  4733. CrtDl - DEC AX ; MOV DL,AL (a 1-based column -> 0-based)
  4734. CrtDh - DEC AX ; MOV DH,AL (a 1-based row)
  4735. CrtCaret - INT 10h AH=02, page 0, cursor at DH,DL
  4736. It takes a parameter on purpose: audit_helpers audits helpers with an EMPTY
  4737. parameter list byte-for-byte, and these multi-instruction sequences have no
  4738. single name grammar. Listed as COMP in NON_ONE_LINE, like SetBlob. *)
  4739. BEGIN
  4740. IF which = CrtClr THEN
  4741. Ebyte (0B8H) ; Ebyte (00H) ; Ebyte (06H) ; (* MOV AX,0600h *)
  4742. Ebyte (8AH) ; Ebyte (3EH) ; Eword (crtAttrOff) ; (* MOV BH,[crtAttr] *)
  4743. Ebyte (0B9H) ; Ebyte (00H) ; Ebyte (00H) ; (* MOV CX,0000h *)
  4744. Ebyte (0BAH) ; Ebyte (4FH) ; Ebyte (18H) ; (* MOV DX,184Fh *)
  4745. Ebyte (0CDH) ; Ebyte (10H) ; (* INT 10h *)
  4746. Ebyte (0B8H) ; Ebyte (00H) ; Ebyte (02H) ; (* MOV AX,0200h *)
  4747. Ebyte (0B7H) ; Ebyte (00H) ; (* MOV BH,00h *)
  4748. Ebyte (0BAH) ; Ebyte (00H) ; Ebyte (00H) ; (* MOV DX,0000h *)
  4749. Ebyte (0CDH) ; Ebyte (10H) (* INT 10h *)
  4750. ELSIF which = CrtDl THEN
  4751. Ebyte (48H) ; (* DEC AX *)
  4752. Ebyte (88H) ; Ebyte (0C2H) (* MOV DL,AL *)
  4753. ELSIF which = CrtDh THEN
  4754. Ebyte (48H) ; (* DEC AX *)
  4755. Ebyte (88H) ; Ebyte (0C6H) (* MOV DH,AL *)
  4756. ELSIF which = CrtEol THEN
  4757. Ebyte (0B4H) ; Ebyte (03H) ; (* MOV AH,03h get cursor *)
  4758. Ebyte (0B7H) ; Ebyte (00H) ; (* MOV BH,00h *)
  4759. Ebyte (0CDH) ; Ebyte (10H) ; (* INT 10h -> DH,DL *)
  4760. Ebyte (89H) ; Ebyte (0D1H) ; (* MOV CX,DX upper-left *)
  4761. Ebyte (0B2H) ; Ebyte (4FH) ; (* MOV DL,4Fh col 79 *)
  4762. Ebyte (0B8H) ; Ebyte (00H) ; Ebyte (06H) ; (* MOV AX,0600h blank *)
  4763. Ebyte (8AH) ; Ebyte (3EH) ; Eword (crtAttrOff) ; (* MOV BH,[crtAttr] *)
  4764. Ebyte (0CDH) ; Ebyte (10H) (* INT 10h *)
  4765. ELSIF which = CrtLow THEN
  4766. (* [crtAttr] := 7: the dim attribute ClrScr/ClrEol use from now on. The
  4767. runtime's console writer is INT 21h teletype, which has no attribute
  4768. channel, but the clear operations do - so this is where the setting is
  4769. observable, not a no-op. *)
  4770. Ebyte (0C6H) ; Ebyte (06H) ; Eword (crtAttrOff) ; Ebyte (07H)
  4771. ELSIF which = CrtNorm THEN
  4772. Ebyte (0C6H) ; Ebyte (06H) ; Eword (crtAttrOff) ; Ebyte (0FH)
  4773. ELSIF which = CrtKey THEN
  4774. (* KeyPressed: DOS AH=0Bh asks whether a character is waiting. AL is
  4775. 0FFh when one is, 0 when not; the boolean representation here is 0/1.
  4776. AND AX,1, not AND AL,1: the interrupt leaves AH = 0Bh, so the
  4777. comparison that follows (`CMP AX,0') would read 0B00h and call the
  4778. false case TRUE. bootcom happened to return AH=0 (it IRETs the
  4779. handler's whole AX) and hid this; hlt. No argument, no runtime
  4780. entry. *)
  4781. Ebyte (0B4H) ; Ebyte (0BH) ; (* MOV AH,0Bh *)
  4782. Ebyte (0CDH) ; Ebyte (21H) ; (* INT 21h *)
  4783. Ebyte (25H) ; Ebyte (01H) ; Ebyte (00H) (* AND AX,1 *)
  4784. ELSIF which = CrtDelay THEN
  4785. (* Delay(AX) milliseconds: TP3's xdelay, a calibrated busy loop. The
  4786. count 110 is TP3's default `delaycnt' for 1 ms (TPSRC1). AX = 0 does
  4787. nothing. *)
  4788. (* OR AX,AX, not CMP AX,0: check_8086 treats every `CMP AX,0000' in the
  4789. program region as one of the IF/WHILE/REPEAT tests and demands
  4790. Jcc+3;EJMP after it. This is neither. *)
  4791. Ebyte (0BH) ; Ebyte (0C0H) ; (* OR AX,AX *)
  4792. Ebyte (74H) ; Ebyte (08H) ; (* JZ past the loop *)
  4793. Ebyte (0B9H) ; Ebyte (6EH) ; Ebyte (00H) ; (* MOV CX,110 (1 ms) *)
  4794. Ebyte (0E2H) ; Ebyte (0FEH) ; (* LOOP -> itself *)
  4795. Ebyte (48H) ; (* DEC AX *)
  4796. Ebyte (75H) ; Ebyte (0F8H) (* JNZ -> MOV CX *)
  4797. ELSE
  4798. Ebyte (0B4H) ; Ebyte (02H) ; (* MOV AH,02h *)
  4799. Ebyte (0B7H) ; Ebyte (00H) ; (* MOV BH,00h *)
  4800. Ebyte (0CDH) ; Ebyte (10H) (* INT 10h *)
  4801. END
  4802. END EmCrtSeq ;
  4803. PROCEDURE IoCall (idx : CARDINAL) ;
  4804. (* WRITE / WRITELN / READ / READLN / HALT.
  4805. TP3 (TPSRC8 pwriteln, pwrloop, prdtyped) does not pass a descriptor to
  4806. the runtime: it looks at each argument's class and emits a *different*
  4807. call per type, so the formatting is fixed at compile time. Mirrored
  4808. here - one call per argument, then a final call for the line break.
  4809. WRITE/WRITELN push the value; READ/READLN push the address, so the
  4810. runtime can store. As everywhere else in this compiler the caller
  4811. cleans the argument off the stack. *)
  4812. VAR arg : ERes ;
  4813. nArgs, ent, which, acls : CARDINAL ;
  4814. argBytes : CARDINAL ; (* bytes pushed for this argument: 2, or 4 with
  4815. a field width *)
  4816. reading : BOOLEAN ;
  4817. pushed : BOOLEAN ;
  4818. skipArg : BOOLEAN ;
  4819. n, p : CARDINAL ;
  4820. dummy, errV : ERes ;
  4821. BEGIN
  4822. which := symtab [idx].cls ; (* BI_* *)
  4823. IF which = BI_Halt THEN
  4824. IF MatchDelim ('(') THEN (* halt(0) - code ignored *)
  4825. ParseExpr (dummy) ;
  4826. ExpectDelim (')', ENoSemi)
  4827. END ;
  4828. DropC (EmCall (TU_Halt)) ;
  4829. RETURN
  4830. END ;
  4831. IF which = BI_New THEN
  4832. (* New(P): allocate sizeof(P^) bytes and store the address in P. The
  4833. pointer symbol's `elem' is the pointee TYPE symbol; its size is the
  4834. allocation. *)
  4835. IF MatchDelim ('(') THEN
  4836. ParseExpr (arg) ;
  4837. IF OK () AND (arg.kind = 1) AND (arg.cls = TPtr) THEN
  4838. p := symtab [arg.idx].elem ;
  4839. EmMovAxi (symtab [p].size) ;
  4840. EmPushAx () ; (* size -> [BP+6] *)
  4841. EmPushVarAddr (symtab [arg.idx].local,
  4842. (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  4843. DropC (EmCall (TU_HeapNew)) ;
  4844. EmAddSp (4)
  4845. ELSIF OK () THEN
  4846. Err (ETypeErr)
  4847. END ;
  4848. ExpectDelim (')', ENoSemi)
  4849. ELSE
  4850. Err (ENoSemi)
  4851. END ;
  4852. RETURN
  4853. END ;
  4854. IF which = BI_Dispose THEN
  4855. (* Dispose(P): consume the argument. The allocator is a bump pointer
  4856. with no free list; CALC allocates once at the start of Help and
  4857. disposes only after the help display is finished, so not reclaiming
  4858. the block has no observable effect. A future allocation after a
  4859. Dispose would need a real free list. *)
  4860. IF MatchDelim ('(') THEN
  4861. ParseExpr (arg) ;
  4862. IF OK () AND NOT ((arg.kind = 1) AND (arg.cls = TPtr)) THEN
  4863. Err (ETypeErr)
  4864. END ;
  4865. ExpectDelim (')', ENoSemi)
  4866. ELSE
  4867. Err (ENoSemi)
  4868. END ;
  4869. RETURN
  4870. END ;
  4871. IF which = BI_ClrScr THEN
  4872. EmCrtSeq (CrtClr) ;
  4873. RETURN
  4874. END ;
  4875. IF which = BI_ClrEol THEN
  4876. (* clear from the cursor to the end of the line - a real INT 10h call,
  4877. not a no-op: AH=03 reads the cursor, CX := it, DL := 79, AH=06 blanks
  4878. that window. *)
  4879. EmCrtSeq (CrtEol) ;
  4880. RETURN
  4881. END ;
  4882. IF which = BI_GotoXY THEN
  4883. (* GotoXY (x, y): 1-based; evaluate x first and park it, then y, so the
  4884. code for y cannot clobber DL. *)
  4885. IF MatchDelim ('(') THEN
  4886. ParseExpr (dummy) ;
  4887. IF OK () THEN
  4888. LoadAtom (dummy) ;
  4889. EmPushAx ()
  4890. END ;
  4891. ExpectDelim (',', ENoSemi) ;
  4892. ParseExpr (dummy) ;
  4893. IF OK () THEN
  4894. LoadAtom (dummy) ;
  4895. EmCrtSeq (CrtDh)
  4896. END ;
  4897. ExpectDelim (')', ENoSemi) ;
  4898. EmPopAx () ;
  4899. EmCrtSeq (CrtDl) ;
  4900. EmCrtSeq (CrtCaret)
  4901. ELSE
  4902. Err (ENoSemi)
  4903. END ;
  4904. RETURN
  4905. END ;
  4906. IF which = BI_LowVideo THEN
  4907. (* set the attribute the CLEAR operations use to 7; see EmCrtSeq *)
  4908. EmCrtSeq (CrtLow) ;
  4909. RETURN
  4910. END ;
  4911. IF which = BI_NormVideo THEN
  4912. EmCrtSeq (CrtNorm) ;
  4913. RETURN
  4914. END ;
  4915. IF which = BI_Delay THEN
  4916. (* Delay(ms): a real busy loop (EmCrtSeq's CrtDelay), no INT, so it is
  4917. identical under qemu and Exec86. *)
  4918. IF MatchDelim ('(') THEN
  4919. ParseExpr (dummy) ;
  4920. IF OK () THEN
  4921. LoadAtom (dummy) ;
  4922. EmCrtSeq (CrtDelay)
  4923. END ;
  4924. ExpectDelim (')', ENoSemi)
  4925. ELSE
  4926. Err (ENoSemi)
  4927. END ;
  4928. RETURN
  4929. END ;
  4930. IF which = BI_Val THEN
  4931. (* Val(S, V, E): integer conversion. E receives 0 on success or the
  4932. 1-based position of the first invalid character; V is left unchanged
  4933. on error. *)
  4934. IF NOT MatchDelim ('(') THEN
  4935. Err (ENoSemi) ;
  4936. RETURN
  4937. END ;
  4938. ParseExpr (arg) ;
  4939. IF OK () AND (arg.cls = TString) AND
  4940. ((arg.kind = 1) OR (arg.kind = 3) OR (arg.kind = 5)) THEN
  4941. ExpectDelim (',', ENoSemi) ;
  4942. ParseExpr (dummy) ;
  4943. ExpectDelim (',', ENoSemi) ;
  4944. ParseExpr (errV) ;
  4945. IF OK () AND (dummy.kind = 1) AND (dummy.cls = TScalar)
  4946. AND (VarSize (dummy) = 2) AND (errV.kind = 1)
  4947. AND (errV.cls = TScalar) AND (VarSize (errV) = 2) THEN
  4948. EmPushVarAddr (symtab [errV.idx].local,
  4949. (symtab [errV.idx].off + errV.boff) MOD 10000H) ;
  4950. EmPushVarAddr (symtab [dummy.idx].local,
  4951. (symtab [dummy.idx].off + dummy.boff) MOD 10000H) ;
  4952. EmPushStrAddr (arg) ;
  4953. DropC (EmCall (TU_ValInt)) ;
  4954. EmAddSp (6)
  4955. ELSIF OK () THEN
  4956. Err (ETypeErr)
  4957. END
  4958. ELSIF OK () THEN
  4959. Err (ETypeErr)
  4960. END ;
  4961. ExpectDelim (')', ENoSemi) ;
  4962. RETURN
  4963. END ;
  4964. IF which = BI_Str THEN
  4965. (* Str(Value[:FW[:DEC]], S): format a REAL into a string variable.
  4966. The source address is pushed first and parked on the stack while the
  4967. width/decimals expressions and the destination are parsed; the two
  4968. stack words are then swapped to real_fmt's order (source at [BP+4],
  4969. destination at [BP+6]). CX carries (decimals << 8) | width; it is
  4970. parked in tmpA across the destination parse, which may clobber CX.
  4971. Defaults match TPSRC8 pstdef1/pstdef3: width 18, decimals FFFFh. *)
  4972. IF NOT MatchDelim ('(') THEN
  4973. Err (ENoSemi) ;
  4974. RETURN
  4975. END ;
  4976. ParseExpr (arg) ;
  4977. IF OK () AND (arg.cls = TScalar) THEN
  4978. (* Str(integer, S): no width formatting here; TP3's default width is
  4979. zero, and CALC uses `Str(count,st)`. A colon would ask for a field
  4980. width, which is a separate increment. *)
  4981. IF MatchDelim (':') THEN
  4982. Err (ENoLib) ;
  4983. RETURN
  4984. END ;
  4985. LoadAtom (arg) ;
  4986. EmPushAx () ; (* value at [BP+6] *)
  4987. IF NOT MatchDelim (',') THEN
  4988. Err (ENoSemi) ;
  4989. RETURN
  4990. END ;
  4991. ParseExpr (dummy) ;
  4992. IF OK () AND (dummy.cls = TString) AND
  4993. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  4994. EmPushStrAddr (dummy) ; (* destination at [BP+4] *)
  4995. DropC (EmCall (TU_IntFmt)) ;
  4996. EmAddSp (4)
  4997. ELSIF OK () THEN
  4998. Err (ETypeErr)
  4999. END ;
  5000. ExpectDelim (')', ENoSemi) ;
  5001. RETURN
  5002. END ;
  5003. IF OK () AND (arg.cls = TReal) AND
  5004. ((arg.kind = 1) OR (arg.kind = 5)) THEN
  5005. IF arg.kind = 5 THEN
  5006. EmPushDi ()
  5007. ELSE
  5008. EmPushVarAddr (symtab [arg.idx].local,
  5009. (symtab [arg.idx].off + arg.boff) MOD 10000H)
  5010. END ;
  5011. IF MatchDelim (':') THEN
  5012. ParseExpr (dummy) ;
  5013. IF OK () THEN
  5014. LoadAtom (dummy)
  5015. END ;
  5016. IF MatchDelim (':') THEN
  5017. EmPushAx () ;
  5018. ParseExpr (dummy) ;
  5019. IF OK () THEN
  5020. LoadAtom (dummy) ;
  5021. EmMovChAl () ;
  5022. EmPopAx () ;
  5023. EmMovClAl ()
  5024. END
  5025. ELSIF OK () THEN
  5026. EmMovClAl () ;
  5027. EmMovChImm (0FFH)
  5028. END
  5029. ELSE
  5030. EmMovCxi (0FF12H) (* width 18, decimals FFFFh *)
  5031. END ;
  5032. IF NOT OK () THEN
  5033. RETURN
  5034. END ;
  5035. EmMovAxCx () ;
  5036. EmStoreVar (FALSE, symtab [tmpA].off, 2) ;
  5037. IF NOT MatchDelim (',') THEN
  5038. Err (ENoSemi) ;
  5039. RETURN
  5040. END ;
  5041. ParseExpr (dummy) ;
  5042. IF OK () AND (dummy.cls = TString) AND
  5043. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  5044. EmPushStrAddr (dummy) ;
  5045. EmPopAx () ; (* destination *)
  5046. EmPopBx () ; (* source *)
  5047. EmPushAx () ; (* destination at [BP+6] *)
  5048. EmPushBx () ; (* source at [BP+4] *)
  5049. EmLoadVar (FALSE, symtab [tmpA].off, 2) ;
  5050. EmXchgAxCx () ; (* CX := packed width/decimals *)
  5051. DropC (EmCall (TU_RealFmt)) ;
  5052. EmAddSp (4)
  5053. ELSIF OK () THEN
  5054. Err (ETypeErr)
  5055. END
  5056. ELSIF OK () THEN
  5057. Err (ETypeErr)
  5058. END ;
  5059. ExpectDelim (')', ENoSemi) ;
  5060. RETURN
  5061. END ;
  5062. IF which = BI_Assign THEN
  5063. (* Assign(F, Name): the file variable's address and the counted
  5064. string's address. The runtime copies the characters into the file
  5065. control block and marks it closed; Reset/Rewrite do the DOS open.
  5066. Name is pushed first, so the runtime sees F at [BP+4] and Name at
  5067. [BP+6]. *)
  5068. IF MatchDelim ('(') THEN
  5069. ParseExpr (arg) ;
  5070. IF OK () AND (arg.kind = 1) AND
  5071. ((arg.cls = TFile) OR (arg.cls = TText) OR (arg.cls = TUntyp)) THEN
  5072. ExpectDelim (',', ENoSemi) ;
  5073. ParseExpr (dummy) ;
  5074. IF OK () THEN
  5075. IF dummy.cls = TString THEN
  5076. EmPushStrAddr (dummy) ;
  5077. EmPushVarAddr (symtab [arg.idx].local,
  5078. (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  5079. DropC (EmCall (TU_FileAssign)) ;
  5080. EmAddSp (4)
  5081. ELSE
  5082. Err (ETypeErr)
  5083. END
  5084. END
  5085. ELSIF OK () THEN
  5086. Err (ETypeErr)
  5087. END ;
  5088. ExpectDelim (')', ENoSemi)
  5089. ELSE
  5090. Err (ENoSemi)
  5091. END ;
  5092. RETURN
  5093. END ;
  5094. IF (which = BI_Reset) OR (which = BI_Rewrite) OR (which = BI_Close) THEN
  5095. IF MatchDelim ('(') THEN
  5096. ParseExpr (arg) ;
  5097. IF OK () THEN
  5098. IF (arg.kind = 1) AND
  5099. ((arg.cls = TFile) OR (arg.cls = TText) OR (arg.cls = TUntyp)) THEN
  5100. EmPushVarAddr (symtab [arg.idx].local,
  5101. (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  5102. IF which = BI_Reset THEN
  5103. DropC (EmCall (TU_FileReset))
  5104. ELSIF which = BI_Rewrite THEN
  5105. DropC (EmCall (TU_FileRewrite))
  5106. ELSE
  5107. DropC (EmCall (TU_FileClose))
  5108. END ;
  5109. EmAddSp (2)
  5110. ELSE
  5111. Err (ETypeErr)
  5112. END
  5113. END ;
  5114. ExpectDelim (')', ENoSemi)
  5115. ELSE
  5116. Err (ENoSemi)
  5117. END ;
  5118. RETURN
  5119. END ;
  5120. reading := (which = BI_Read) OR (which = BI_ReadLn) ;
  5121. nArgs := 0 ;
  5122. IF MatchDelim ('(') THEN
  5123. IF CurCh () # ')' THEN
  5124. LOOP
  5125. IF nArgs >= 16 THEN
  5126. Err (ECompOvf) ;
  5127. EXIT
  5128. END ;
  5129. ParseExpr (arg) ;
  5130. IF NOT OK () THEN
  5131. (* An argument that did not type-check has no value and arg.idx
  5132. is not a symbol. The string arm below dereferences
  5133. symtab[arg.idx] for ANY value whose class is TString - unlike
  5134. the string assign in Assignment, which can tell a failed
  5135. concat from a real one by its kind - so a `writeln' of a
  5136. rejected expression read symtab with whatever idx held
  5137. before and dereferenced it. That is exactly how
  5138. `writeln('a' + 'b')' - two CHARs, a type error since
  5139. v-TP3-STRCAT - segfaulted the compiler instead of reporting
  5140. the error. ParseSub guards its subscript the same way (see
  5141. `IF OK () THEN' before symtab[r.idx] there), and the
  5142. statement loop stops at the next iteration, so leaving the
  5143. rest of this WRITELN unparsed is harmless. *)
  5144. RETURN
  5145. END ;
  5146. (* One argument at a time: parse it, give it its own push or its
  5147. own runtime call, and only then read the next one. TPSRC8
  5148. pwrloop does exactly this, and it has to - a computed argument
  5149. exists only in AX, so parsing the argument after it, and the
  5150. call made for THIS one, are both free to destroy it. Reading
  5151. the whole list first and emitting afterwards printed the first
  5152. argument as the last argument's value, and pushed an argument
  5153. that an earlier inline string's call had already overwritten. *)
  5154. skipArg := FALSE ;
  5155. IF (nArgs = 0) AND (arg.cls = TText) THEN
  5156. (* A leading TEXT file argument. KBD is the console keyboard
  5157. and this runtime's read entries already read the console, so
  5158. the file is dropped and the rest of the argument list is read
  5159. exactly as without it. *)
  5160. IF reading AND (arg.kind = 1) AND symtab [arg.idx].isKbd THEN
  5161. IF NOT MatchDelim (',') THEN
  5162. Err (ENoSemi) ;
  5163. RETURN
  5164. END ;
  5165. skipArg := TRUE
  5166. ELSIF reading THEN
  5167. (* Readln(F, S): read one line from a text file into the
  5168. counted string S. The runtime takes the file block,
  5169. destination and capacity; it consumes CR and terminates
  5170. on LF/^Z/physical EOF. *)
  5171. IF NOT MatchDelim (',') THEN
  5172. Err (ENoSemi) ;
  5173. RETURN
  5174. END ;
  5175. ParseExpr (dummy) ;
  5176. IF OK () AND (dummy.cls = TString) AND
  5177. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  5178. EmMovAxi ((VarSize (dummy) - 1) MOD 10000H) ;
  5179. EmPushAx () ;
  5180. EmPushStrAddr (dummy) ;
  5181. EmPushVarAddr (symtab [arg.idx].local,
  5182. (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  5183. DropC (EmCall (TU_FileReadln)) ;
  5184. EmAddSp (6)
  5185. ELSIF OK () THEN
  5186. Err (ETypeErr)
  5187. END ;
  5188. ExpectDelim (')', ENoSemi) ;
  5189. RETURN
  5190. ELSE
  5191. (* Text-file output: Writeln(File) is CRLF, Write(File, C)
  5192. is one CHAR, and writeln(File, S) is S followed by CRLF.
  5193. Write each byte range with DOS AH=40h; a string argument
  5194. carries its own length byte, a one-CHAR literal becomes a
  5195. tiny code-resident <1><char>. *)
  5196. IF (arg.kind # 1) OR (symtab [arg.idx].cls # TText) THEN
  5197. Err (ETypeErr) ;
  5198. RETURN
  5199. END ;
  5200. IF MatchDelim (',') THEN
  5201. ParseExpr (dummy) ;
  5202. IF NOT OK () THEN
  5203. RETURN
  5204. END ;
  5205. IF dummy.cls = TString THEN
  5206. IF dummy.kind = 1 THEN
  5207. EmFileIo (FALSE, symtab [arg.idx].local,
  5208. symtab [arg.idx].off, 1,
  5209. symtab [dummy.idx].local,
  5210. (symtab [dummy.idx].off + dummy.boff)
  5211. MOD 10000H,
  5212. 0, TRUE)
  5213. ELSIF dummy.kind = 3 THEN
  5214. EmFileIo (FALSE, symtab [arg.idx].local,
  5215. symtab [arg.idx].off, 4, FALSE,
  5216. EmStrBlob (dummy), 0, TRUE)
  5217. ELSE
  5218. Err (ENoLib) ;
  5219. RETURN
  5220. END
  5221. ELSIF (dummy.cls = TScalar) AND dummy.chr THEN
  5222. EmFileIo (FALSE, symtab [arg.idx].local,
  5223. symtab [arg.idx].off, 4, FALSE,
  5224. EmInlineCh (VAL (CARDINAL, dummy.imm)),
  5225. 0, TRUE)
  5226. ELSE
  5227. Err (ENoLib) ;
  5228. RETURN
  5229. END
  5230. END ;
  5231. IF which = BI_WriteLn THEN
  5232. EmFileIo (FALSE, symtab [arg.idx].local,
  5233. symtab [arg.idx].off, 4, FALSE,
  5234. EmInlineCrLf (), 0, TRUE)
  5235. END ;
  5236. ExpectDelim (')', ENoSemi) ;
  5237. RETURN
  5238. END
  5239. ELSIF (nArgs = 0) AND (arg.cls = TFile) THEN
  5240. (* Typed-file block I/O: `write(MCfile, Sheet[I,J])' and the
  5241. symmetric `read(MCfile, Sheet[FX,FY])'. TP3 moves exactly
  5242. one element per call, so consume the single data argument
  5243. here and emit the DOS block call. The element size comes
  5244. from the data expression and must agree with the file's
  5245. declared element size; a mismatch is a type error, not a
  5246. reason to write the wrong number of bytes. *)
  5247. IF (arg.kind # 1) OR NOT MatchDelim (',') THEN
  5248. Err (ETypeErr) ;
  5249. RETURN
  5250. END ;
  5251. ParseExpr (dummy) ;
  5252. IF NOT OK () THEN
  5253. RETURN
  5254. END ;
  5255. IF ((dummy.kind # 1) AND (dummy.kind # 5))
  5256. OR (VarSize (dummy) # symtab [arg.idx].elem) THEN
  5257. Err (ETypeErr) ;
  5258. RETURN
  5259. END ;
  5260. IF dummy.kind = 5 THEN
  5261. EmFileIo (reading, symtab [arg.idx].local,
  5262. symtab [arg.idx].off, 5, FALSE, 0, VarSize (dummy),
  5263. FALSE)
  5264. ELSE
  5265. EmFileIo (reading, symtab [arg.idx].local,
  5266. symtab [arg.idx].off, 1, symtab [dummy.idx].local,
  5267. (symtab [dummy.idx].off + dummy.boff) MOD 10000H,
  5268. VarSize (dummy), FALSE)
  5269. END ;
  5270. ExpectDelim (')', ENoSemi) ;
  5271. RETURN
  5272. END ;
  5273. IF NOT skipArg THEN
  5274. pushed := TRUE ; (* default: value is on the stack -> call + pop *)
  5275. argBytes := 2 ;
  5276. IF reading THEN
  5277. IF (arg.kind # 1) AND (arg.kind # 5) THEN
  5278. Err (ETypeErr) ; (* READ needs a variable *)
  5279. RETURN
  5280. END ;
  5281. IF arg.kind = 5 THEN
  5282. (* A DI-addressed element: its TYPE is on the expression
  5283. (WithLookup/ParseSub set it), because the symbol is the
  5284. containing array/record. s[i] is TChar; a WITH string
  5285. field is TString. *)
  5286. acls := arg.cls
  5287. ELSE
  5288. acls := symtab [arg.idx].cls ;
  5289. (* s[i] is a CHAR variable, so read(s[i]) must read one byte
  5290. at off+boff, not the whole string at off. arg.cls is
  5291. TChar (set by ParseSub) while the symbol's own class is
  5292. still TString, which is exactly how the element is told
  5293. from the whole string; the address push has to carry boff.
  5294. For a plain variable arg.boff is 0, so nothing changes. *)
  5295. IF (acls = TString) AND (arg.cls = TChar) THEN
  5296. acls := TChar
  5297. END
  5298. END ;
  5299. IF arg.kind = 5 THEN
  5300. (* A variable index: ParseSub computed the element's address
  5301. into DI, and nothing has been emitted since, so that is
  5302. the address READ wants. A constant index folds into boff
  5303. and takes the LEA path below. *)
  5304. EmPushDi ()
  5305. ELSIF symtab [arg.idx].isVar THEN
  5306. (* A VAR parameter: its slot holds the address READ must
  5307. store through, not the address of the slot. *)
  5308. IF arg.boff # 0 THEN
  5309. Err (ENoLib) ;
  5310. RETURN
  5311. END ;
  5312. EmLoadVar (TRUE, symtab [arg.idx].off, 2) ;
  5313. EmPushAx ()
  5314. ELSE
  5315. EmPushVarAddr (symtab [arg.idx].local,
  5316. (symtab [arg.idx].off + arg.boff) MOD 10000H)
  5317. END ;
  5318. IF acls = TString THEN
  5319. (* readln(s)/read(s) of a string variable: TPSRC8 prdstr
  5320. pushes the address, emits MOV CL,max_len and CALL
  5321. xrdstr. Here the limit is a full MOV CX,imm16 (this
  5322. runtime has no byte-register CH/CL setup), and rdstr
  5323. leaves the terminator pending so the readln wrapper can
  5324. consume the newline. *)
  5325. EmMovCxi ((VarSize (arg) - 1) MOD 10000H) ;
  5326. ent := TU_RdStr
  5327. ELSIF acls = TReal THEN
  5328. ent := TU_RdInt (* real reads: not yet *)
  5329. ELSIF acls = TBool THEN
  5330. (* TP3 refuses boolean I/O: TPSRC8 prderrtp is error 66,
  5331. "I/O not allowed here", and the runtime has no xrdbool
  5332. at all. rdbool stores a WORD, so one byte of an
  5333. accepted read would land on the variable AFTER b - the
  5334. exact overflow t37_readbyte pins for a byte. TP3's
  5335. answer to the whole class is the error; so is ours. *)
  5336. Err (EIoHere) ;
  5337. RETURN
  5338. ELSIF acls = TChar THEN
  5339. ent := TU_RdChar (* one byte, not a word *)
  5340. ELSIF acls = TScalar THEN
  5341. (* Dispatch on the SIZE, as TPSRC8 prdnum does:
  5342. `CMP varsize,#$01 / JA prdemit' keeps xrdint for
  5343. anything wider and falls through to `MOV AX,#xrdbyte'
  5344. for a one-byte variable. A BYTE is still TScalar (it
  5345. is *written* as an integer, as in TP3), but reading one
  5346. through rdint stored a word - and the second byte of
  5347. that word is the byte that FOLLOWS the variable. *)
  5348. IF VarSize (arg) = 1 THEN
  5349. ent := TU_RdByte
  5350. ELSE
  5351. ent := TU_RdInt
  5352. END
  5353. ELSE
  5354. Err (ETypeErr) ;
  5355. RETURN
  5356. END
  5357. ELSE
  5358. acls := arg.cls ;
  5359. IF (acls = TString) AND (arg.kind = 3) THEN
  5360. (* An inline string literal. TP3 TPSRC8 pwrinlin special-cases a
  5361. literal that is followed directly by ',' or ')' - i.e. an
  5362. argument, not an expression - and emits
  5363. CALL wrtinl <length byte> <characters...>
  5364. with no stack argument at all; wrtinl reads the length from the
  5365. return address and returns to just past the last character.
  5366. Mirrored exactly, so the literal costs only its own characters
  5367. in the code stream and nothing in the data segment. *)
  5368. IF strLen [arg.strx] > 255 THEN
  5369. (* The length is one byte, so a literal of 256 characters or
  5370. more would wrap: 300 characters emitted behind a length of
  5371. 44, and the runtime would print 44 of them and silently drop
  5372. the rest. TP3 strings are at most 255 characters, so refuse
  5373. rather than truncate. *)
  5374. Err (EConstRange) ;
  5375. RETURN
  5376. END ;
  5377. DropC (EmCall (TU_WrInl)) ;
  5378. Ebyte (VAL (BYTE, strLen [arg.strx])) ;
  5379. n := 0 ;
  5380. WHILE n < strLen [arg.strx] DO
  5381. Ebyte (VAL (BYTE, ORD (strPool [strOff [arg.strx] + n]))) ;
  5382. INC (n)
  5383. END ;
  5384. pushed := FALSE (* nothing was pushed for this one *)
  5385. ELSE
  5386. IF acls = TString THEN
  5387. (* A string *variable*. Its value IS its address, so it is
  5388. passed exactly the way READ passes an address: push it
  5389. and call the owner of the layout - wrstr - which reads
  5390. the length byte and prints that many characters. TPSRC8
  5391. pwrstr/pwrchk route a string argument to xwrtstr, and
  5392. xwrtstr reaches its argument through BP just like WrChar
  5393. does. The only difference from the inline-literal arm
  5394. above is where the bytes live: a literal's are in the
  5395. code stream (wrtinl) and a variable's are in the data
  5396. segment or the frame (wrstr). *)
  5397. (* off + boff: a string FIELD of a record/array has a
  5398. non-zero boff, and the address is the field's, not the
  5399. containing variable's. `writeln (g[2,3].S)' printed
  5400. the empty first element before this.
  5401. A DI-addressed field (`with Sheet[I,J] do
  5402. Write(Contents)') already HAS its address in DI. *)
  5403. IF arg.kind = 5 THEN
  5404. EmPushDi ()
  5405. ELSIF symtab [arg.idx].isVar THEN
  5406. (* VAR string: the slot holds the address of the
  5407. caller's characters; push that, not the slot. *)
  5408. EmLoadVar (TRUE, symtab [arg.idx].off, 2) ;
  5409. EmPushAx ()
  5410. ELSE
  5411. EmPushVarAddr (symtab [arg.idx].local,
  5412. (symtab [arg.idx].off + arg.boff) MOD 10000H)
  5413. END ;
  5414. ent := TU_WrStr
  5415. ELSE
  5416. IF acls = TReal THEN
  5417. (* A REAL is six bytes, so the writer takes its
  5418. ADDRESS, not its value: push the address first,
  5419. then the width, then the decimals. The entry is
  5420. real_write, with [BP+4]=decimals, [BP+6]=width and
  5421. [BP+8]=real. TP3's defaults are width 18 and
  5422. decimals FFFFh (its scientific mode); a width with
  5423. no second colon keeps that default, which is
  5424. exactly what TPSRC8 pwrdef/pwrdef3 push. *)
  5425. IF arg.kind = 5 THEN
  5426. EmPushDi ()
  5427. ELSE
  5428. EmPushVarAddr (symtab [arg.idx].local,
  5429. (symtab [arg.idx].off + arg.boff) MOD 10000H)
  5430. END ;
  5431. IF MatchDelim (':') THEN
  5432. ParseExpr (dummy) ;
  5433. IF NOT OK () THEN
  5434. RETURN
  5435. END ;
  5436. LoadAtom (dummy) ;
  5437. EmPushAx () ;
  5438. IF MatchDelim (':') THEN
  5439. ParseExpr (dummy) ;
  5440. IF NOT OK () THEN
  5441. RETURN
  5442. END ;
  5443. LoadAtom (dummy) ;
  5444. EmPushAx ()
  5445. ELSE
  5446. EmMovAxi (0FFFFH) ;
  5447. EmPushAx ()
  5448. END
  5449. ELSE
  5450. EmMovAxi (18) ;
  5451. EmPushAx () ;
  5452. EmMovAxi (0FFFFH) ;
  5453. EmPushAx ()
  5454. END ;
  5455. ent := TU_RealWrite ;
  5456. argBytes := 6
  5457. ELSE
  5458. LoadAtom (arg) ;
  5459. EmPushAx () ;
  5460. IF arg.chr THEN
  5461. ent := TU_WrChar (* 'a' - one char, not 97 *)
  5462. ELSIF acls = TBool THEN
  5463. ent := TU_WrBool
  5464. ELSIF acls = TChar THEN
  5465. ent := TU_WrChar (* c : char - one char *)
  5466. ELSIF acls = TScalar THEN
  5467. ent := TU_WrInt
  5468. ELSE
  5469. Err (ETypeErr) ;
  5470. RETURN
  5471. END ;
  5472. IF MatchDelim (':') THEN
  5473. (* Field width: `writeln(I:2)'. TP3 evaluates the
  5474. width (CALC passes `FW', a variable), so it is
  5475. parsed and pushed AFTER the value: width at BP+4,
  5476. value at BP+6. Only an integer has a width entry
  5477. yet - a CHAR/BOOLEAN/STRING width needs its own
  5478. writer, and `:decimals' is the REAL form. *)
  5479. IF (acls # TScalar) OR arg.chr THEN
  5480. Err (ENoLib) ;
  5481. RETURN
  5482. END ;
  5483. ParseExpr (dummy) ;
  5484. IF NOT OK () THEN
  5485. RETURN
  5486. END ;
  5487. LoadAtom (dummy) ;
  5488. EmPushAx () ;
  5489. IF MatchDelim (':') THEN
  5490. Err (ENoLib) ;
  5491. RETURN
  5492. END ;
  5493. ent := TU_WrIntW ;
  5494. argBytes := 4
  5495. END
  5496. END
  5497. END
  5498. END
  5499. END ;
  5500. IF pushed THEN
  5501. DropC (EmCall (ent)) ;
  5502. EmAddSp (argBytes) (* 2, or 4 with a width *)
  5503. END ;
  5504. INC (nArgs)
  5505. END ;
  5506. IF NOT skipArg THEN
  5507. IF NOT MatchDelim (',') THEN
  5508. EXIT
  5509. END
  5510. END
  5511. END ;
  5512. IF NOT MatchDelim (')') THEN
  5513. Err (ENoSemi) ;
  5514. RETURN
  5515. END
  5516. ELSE
  5517. DropCh (GetCh ())
  5518. END
  5519. END ;
  5520. IF reading AND (nArgs = 0) THEN
  5521. (* readln with no variable: just skip to the next line *)
  5522. DropC (EmCall (TU_RdLn)) ;
  5523. RETURN
  5524. END ;
  5525. IF which = BI_WriteLn THEN
  5526. DropC (EmCall (TU_WrLn))
  5527. ELSIF which = BI_ReadLn THEN
  5528. DropC (EmCall (TU_RdLn))
  5529. END
  5530. END IoCall ;
  5531. PROCEDURE Statmnt () ;
  5532. VAR tok : CARDINAL ;
  5533. idx, i2 : CARDINAL ;
  5534. t, src : ERes ;
  5535. L1, zj, zj2, exj : CARDINAL ;
  5536. lo, hi, v : LONGINT ;
  5537. clso : CARDINAL ;
  5538. i : CARDINAL ;
  5539. k, nlbl, nlblJmp, bodyPc, skipJmp : CARDINAL ;
  5540. nm : ARRAY [0..MaxName] OF CHAR ;
  5541. strf : BOOLEAN ;
  5542. dow : BOOLEAN ;
  5543. savedWith : CARDINAL ;
  5544. BEGIN
  5545. Skip () ;
  5546. IF Digit (CurCh ()) THEN
  5547. ParseLabelStmt () ; (* consumed 'n' ':' *)
  5548. Statmnt () ; (* 'n : statement' - the statement follows
  5549. the label directly, with no ';' between *)
  5550. RETURN
  5551. END ;
  5552. IF NOT Alpha (CurCh ()) THEN
  5553. ExpectDelim (';', ENoSemi) ;
  5554. RETURN
  5555. END ;
  5556. DropB (MatchKey (tok)) ;
  5557. IF tok = TkBegin THEN
  5558. Compound ()
  5559. ELSIF tok = TkIf THEN
  5560. ParseExpr (t) ;
  5561. LoadAtom (t) ;
  5562. EmCmpAxi (0) ;
  5563. zj := EmJcc (84H, 0) ; (* JZ -> else/end *)
  5564. IF NOT (MatchKey (tok) AND (tok = TkThen)) THEN
  5565. Err (ENoSemi)
  5566. END ;
  5567. Statmnt () ;
  5568. (* Peek the ELSE, do not match it. `MatchKey (tok) AND (tok = TkElse)`
  5569. consumes whatever the next keyword is even when the AND fails, so an
  5570. `if` that was the LAST statement of a BEGIN..END block ate the block's
  5571. own END: Compound then found neither ';' nor END and raised ENoSemi.
  5572. Any `if` as the last statement of a compound was unparseable - not
  5573. in a loop, not anywhere - and no fixture had one, so nothing noticed.
  5574. The visible symptom was a parse error at the statement AFTER the
  5575. block, which points at entirely the wrong piece of source. *)
  5576. PeekKw (tok) ;
  5577. IF tok = TkElse THEN
  5578. DropB (MatchKey (tok)) ;
  5579. exj := EmJmpNear (0) ;
  5580. SetPatTgt (zj, pc) ;
  5581. Statmnt () ;
  5582. SetPatTgt (exj, pc)
  5583. ELSE
  5584. SetPatTgt (zj, pc)
  5585. END
  5586. ELSIF tok = TkWhile THEN
  5587. L1 := pc ;
  5588. ParseExpr (t) ;
  5589. LoadAtom (t) ;
  5590. EmCmpAxi (0) ;
  5591. zj := EmJcc (84H, 0) ; (* JZ -> end *)
  5592. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  5593. Err (ENoSemi)
  5594. END ;
  5595. brkSave [brkN] := exitCnt ;
  5596. loopTy [brkN] := 1 ;
  5597. INC (brkN) ;
  5598. Statmnt () ;
  5599. DEC (brkN) ;
  5600. i := brkSave [brkN] ;
  5601. WHILE i < exitCnt DO
  5602. SetPatTgt (exitPatch [i], pc) ;
  5603. INC (i)
  5604. END ;
  5605. exitCnt := brkSave [brkN] ;
  5606. DropC (EmJmpNear (L1)) ;
  5607. SetPatTgt (zj, pc)
  5608. ELSIF tok = TkRepeat THEN
  5609. L1 := pc ;
  5610. brkSave [brkN] := exitCnt ;
  5611. loopTy [brkN] := 1 ;
  5612. INC (brkN) ;
  5613. (* The body is a STATEMENT SEQUENCE, not one statement: `repeat a; b
  5614. until c'. This used to call Statmnt once and then demand UNTIL, so
  5615. any body with a second statement was ENoSemi at the first ';'. t12's
  5616. two bodies are single statements and never saw it; CALC's `Flash' does
  5617. (GotoXY; Blink := not Blink; if ...; Write; Delay). Same shape as
  5618. Compound, stopping at UNTIL instead of END. *)
  5619. LOOP
  5620. PeekKw (tok) ;
  5621. IF tok = TkUntil THEN
  5622. DropB (MatchKey (tok)) ;
  5623. EXIT
  5624. END ;
  5625. Statmnt () ;
  5626. IF NOT OK () THEN
  5627. RETURN
  5628. END ;
  5629. IF NOT MatchDelim (';') THEN
  5630. PeekKw (tok) ;
  5631. IF tok = TkUntil THEN
  5632. DropB (MatchKey (tok)) ;
  5633. EXIT
  5634. END ;
  5635. Err (ENoSemi) ;
  5636. RETURN
  5637. END
  5638. END ;
  5639. ParseExpr (t) ;
  5640. LoadAtom (t) ;
  5641. EmCmpAxi (0) ;
  5642. (* UNTIL exits when the condition is TRUE, so the body runs again when
  5643. it is FALSE. The condition is a 0/1 in AX and EmCmpAxi (0) has just
  5644. compared it with 0, so ZF=1 means "condition false" -- which is
  5645. exactly the case that loops, hence JZ.
  5646. This was JNZ, which compiled repeat-until as while-until: the body
  5647. ran once, the condition was tested, and it stopped. t12_repeat is
  5648. `i:=0; repeat i:=i+1 until i>5' and it printed 1.
  5649. No patch slot: L1 is backwards and already known, so EmJcc returns
  5650. 0 and there is nothing to SetPatTgt. *)
  5651. DropC (EmJcc (84H, L1)) ; (* JZ -> body again *)
  5652. DEC (brkN) ;
  5653. i := brkSave [brkN] ;
  5654. WHILE i < exitCnt DO
  5655. SetPatTgt (exitPatch [i], pc) ;
  5656. INC (i)
  5657. END ;
  5658. exitCnt := brkSave [brkN]
  5659. ELSIF tok = TkFor THEN
  5660. (* control variable *)
  5661. Skip () ; (* after the FOR keyword: skip blanks *)
  5662. IF NOT Alpha (CurCh ()) THEN
  5663. Err (EUnknown) ;
  5664. RETURN
  5665. END ;
  5666. GetWord () ;
  5667. IF NOT Search (wrd, idx) THEN
  5668. Err (EUnknown) ;
  5669. RETURN
  5670. END ;
  5671. IF symtab [idx].size > 2 THEN
  5672. Err (ENoLib) ;
  5673. RETURN
  5674. END ;
  5675. IF NOT MatchAssign () THEN
  5676. Err (ENoSemi)
  5677. END ;
  5678. ParseExpr (src) ;
  5679. LoadAtom (src) ;
  5680. EmStoreVar (symtab [idx].local, symtab [idx].off,
  5681. symtab [idx].size) ;
  5682. IF NOT MatchKey (tok) THEN
  5683. Err (ESimpType) ;
  5684. RETURN
  5685. END ;
  5686. IF (tok = TkTo) OR (tok = TkDownto) THEN
  5687. dow := (tok = TkDownto)
  5688. ELSE
  5689. Err (ESimpType) ;
  5690. RETURN
  5691. END ;
  5692. ParseExpr (t) ;
  5693. LoadAtom (t) ;
  5694. EmPushAx () ; (* loop bound on the stack *)
  5695. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  5696. Err (ENoSemi)
  5697. END ;
  5698. brkSave [brkN] := exitCnt ;
  5699. loopTy [brkN] := 2 ;
  5700. INC (brkN) ;
  5701. L1 := pc ; (* Ltest: the test comes FIRST *)
  5702. (* The test is emitted before the body, not after it. It used to be
  5703. emitted after, which is a post-test loop and runs the body one time
  5704. too many: with `for i := 1 to 5`, the sequence of i at the test is
  5705. 1,2,3,4,5,6 - the test at i=5 is `5 > 5`, which is false, so the
  5706. body ran a sixth time with i=6. `for i := 1 to 5 do s := s + i`
  5707. printed 21. The size was right and the shape was right; only the
  5708. order was wrong, and no byte check can see an order.
  5709. The bound stays on the stack for the whole loop, so EmMovCxSp has to
  5710. re-read it every iteration - which is also what makes the bound a
  5711. *variable* rather than a constant. [SP] cannot be encoded on the
  5712. 8086, so EmMovCxSp is POP CX ; PUSH CX, an observational no-op that
  5713. leaves the bound in place. *)
  5714. EmMovCxSp () ;
  5715. EmLoadVar (symtab [idx].local, symtab [idx].off,
  5716. symtab [idx].size) ;
  5717. EmCmpAxCx () ;
  5718. IF dow THEN
  5719. zj := EmJcc (8CH, 0) (* JL -> done *)
  5720. ELSE
  5721. zj := EmJcc (8FH, 0) (* JG -> done *)
  5722. END ;
  5723. Statmnt () ;
  5724. DEC (brkN) ;
  5725. (* A FOR's exits are NOT patched here, even though `done` is not known
  5726. yet. For a WHILE or REPEAT, "just after the body" is a correct
  5727. target: the jump back to the test re-evaluates the condition and
  5728. leaves. For a FOR there is a STEP between the body and `done`, so
  5729. an EXIT that jumped here would increment the control variable and
  5730. jump back to the test - and if the incremented value still satisfied
  5731. the bound, it would run the body AGAIN. `exit` did not exit.
  5732. The fix needs no new bookkeeping: `brkSave [brkN] .. exitCnt` still
  5733. names exactly this loop's exits, because Statmnt may have added more
  5734. and nothing has reset exitCnt. So they are patched at `done`, below.
  5735. A WHILE nested inside the FOR saves and restores its own range and
  5736. leaves this one intact. *)
  5737. (* step *)
  5738. EmLoadVar (symtab [idx].local, symtab [idx].off,
  5739. symtab [idx].size) ;
  5740. IF dow THEN
  5741. EmDecAx ()
  5742. ELSE
  5743. EmIncAx ()
  5744. END ;
  5745. EmStoreVar (symtab [idx].local, symtab [idx].off,
  5746. symtab [idx].size) ;
  5747. DropC (EmJmpNear (L1)) ;
  5748. SetPatTgt (zj, pc) ; (* done: *)
  5749. (* A WHILE loop here, not a FOR over the exit range. The range is
  5750. usually EMPTY - most loops have no `exit` - and `exitCnt` is a
  5751. CARDINAL, so `TO exitCnt - 1` with exitCnt = 0 is `TO 65535`: the
  5752. loop does not terminate, it wraps, and it walks exitPatch [0..65535]
  5753. off the end of a 64-element array. `for i := 1 to 10 do i := i` has
  5754. no exit, so this is the ORDINARY case, and it faulted with
  5755. "invalid address referenced" on every FOR loop without an exit. *)
  5756. i := brkSave [brkN] ;
  5757. WHILE i < exitCnt DO
  5758. SetPatTgt (exitPatch [i], pc) ;
  5759. INC (i)
  5760. END ;
  5761. exitCnt := brkSave [brkN] ;
  5762. EmAddSp (2) (* drop the loop bound *)
  5763. ELSIF tok = TkCase THEN
  5764. ParseExpr (t) ;
  5765. LoadAtom (t) ;
  5766. EmPushAx () ; (* selector on the stack *)
  5767. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  5768. Err (ENoSemi)
  5769. END ;
  5770. caseN := 0 ;
  5771. LOOP
  5772. Skip () ;
  5773. IF MatchDelim (';') THEN
  5774. Skip ()
  5775. END ;
  5776. PeekKw (tok) ;
  5777. IF (tok = TkEnd) OR (tok = TkElse) THEN
  5778. EXIT
  5779. END ;
  5780. (* Case labels: `v' | `lo..hi', comma-separated, all selecting the
  5781. same statement (CALC's `'G', 'I', 'O', 'Q':'). Read the whole list
  5782. first; the emission then knows whether this is the one-label path
  5783. or a list. *)
  5784. nlbl := 0 ;
  5785. LOOP
  5786. IF Alpha (CurCh ()) THEN
  5787. GetWord () ;
  5788. IF Search (wrd, i2) AND (symtab [i2].tag = KConst) THEN
  5789. lo := symtab [i2].lval
  5790. ELSE
  5791. Err (EUnknown) ;
  5792. RETURN
  5793. END
  5794. ELSE
  5795. RdConst (lo, clso, strf)
  5796. END ;
  5797. IF MatchRange () THEN
  5798. RdConst (hi, clso, strf)
  5799. ELSE
  5800. hi := lo
  5801. END ;
  5802. IF nlbl >= 32 THEN
  5803. Err (ECompOvf) ;
  5804. RETURN
  5805. END ;
  5806. lblLo [nlbl] := lo ;
  5807. lblHi [nlbl] := hi ;
  5808. INC (nlbl) ;
  5809. IF MatchDelim (',') THEN
  5810. Skip ()
  5811. ELSE
  5812. EXIT
  5813. END
  5814. END ;
  5815. Skip () ;
  5816. ExpectDelim (':', ENoSemi) ;
  5817. IF NOT OK () THEN
  5818. RETURN
  5819. END ;
  5820. EmMovAxSp () ;
  5821. IF nlbl = 1 THEN
  5822. (* One label: the original bytes, except that a RANGE is now a
  5823. real range. It used to compare AX with lo and then with hi,
  5824. both demanding equality, so `1..5' matched NOTHING unless
  5825. lo = hi - a silently wrong answer, not an error. *)
  5826. lo := lblLo [0] ;
  5827. hi := lblHi [0] ;
  5828. IF hi = lo THEN
  5829. EmCmpAxi (W16 (lo)) ;
  5830. zj := EmJcc (85H, 0) ; (* JNZ -> next arm *)
  5831. zj2 := 0
  5832. ELSE
  5833. EmCmpAxi (W16 (lo)) ;
  5834. zj := EmJcc (8CH, 0) ; (* JL -> next arm *)
  5835. EmCmpAxi (W16 (hi)) ;
  5836. zj2 := EmJcc (8FH, 0) (* JG -> next arm *)
  5837. END ;
  5838. Statmnt () ;
  5839. IF caseN >= 64 THEN
  5840. Err (ECompOvf) ;
  5841. RETURN
  5842. END ;
  5843. caseJmp [caseN] := EmJmpNear (0) ;
  5844. INC (caseN) ;
  5845. SetPatTgt (zj, pc) ;
  5846. IF zj2 # 0 THEN
  5847. SetPatTgt (zj2, pc)
  5848. END
  5849. ELSE
  5850. (* A list. A single label branches INTO the statement; a range
  5851. first sends AX below lo to the next label, then `<= hi' to the
  5852. body. The body jumps are patched once the body's address is
  5853. known, and the no-match EJMP skips the body to the next arm. *)
  5854. nlblJmp := 0 ;
  5855. FOR k := 0 TO nlbl - 1 DO
  5856. IF lblHi [k] = lblLo [k] THEN
  5857. EmCmpAxi (W16 (lblLo [k])) ;
  5858. lblJmp [nlblJmp] := EmJcc (84H, 0) ; (* JZ -> body *)
  5859. INC (nlblJmp)
  5860. ELSE
  5861. (* `lo <= AX <= hi', built from the two conditions this
  5862. compiler declares for a branch (L and G): below lo skips
  5863. the label, above hi skips it, otherwise an unconditional
  5864. jump enters the body. JLE would be shorter and is legal
  5865. 8086, but check_8086's declared condition set is exactly
  5866. Z/NZ/L/G (the four the parser's comparisons use), and a
  5867. new condition in CASE would rightly be reported there. *)
  5868. EmCmpAxi (W16 (lblLo [k])) ;
  5869. zj := EmJcc (8CH, 0) ; (* JL -> next label *)
  5870. EmCmpAxi (W16 (lblHi [k])) ;
  5871. zj2 := EmJcc (8FH, 0) ; (* JG -> next label *)
  5872. lblJmp [nlblJmp] := EmJmpNear (0) ; (* in range -> body *)
  5873. INC (nlblJmp) ;
  5874. SetPatTgt (zj, pc) ;
  5875. SetPatTgt (zj2, pc)
  5876. END
  5877. END ;
  5878. skipJmp := EmJmpNear (0) ;
  5879. bodyPc := pc ;
  5880. FOR k := 0 TO nlblJmp - 1 DO
  5881. SetPatTgt (lblJmp [k], bodyPc)
  5882. END ;
  5883. Statmnt () ;
  5884. IF caseN >= 64 THEN
  5885. Err (ECompOvf) ;
  5886. RETURN
  5887. END ;
  5888. caseJmp [caseN] := EmJmpNear (0) ;
  5889. INC (caseN) ;
  5890. SetPatTgt (skipJmp, pc)
  5891. END
  5892. END ;
  5893. IF tok = TkElse THEN
  5894. DropB (MatchKey (tok)) ;
  5895. Statmnt () ;
  5896. IF NOT (MatchKey (tok) AND (tok = TkEnd)) THEN
  5897. Err (ENoSemi)
  5898. END
  5899. ELSE
  5900. DropB (MatchKey (tok))
  5901. END ;
  5902. EmAddSp (2) ;
  5903. FOR i := 0 TO caseN - 1 DO
  5904. SetPatTgt (caseJmp [i], pc)
  5905. END
  5906. ELSIF tok = TkGoto THEN
  5907. v := 0 ;
  5908. Skip () ; (* after the GOTO keyword: skip blanks *)
  5909. IF Digit (CurCh ()) THEN
  5910. RdIntConst (v) ;
  5911. NumToName (W16 (v), nm) ;
  5912. IF Search (nm, idx) THEN
  5913. IF symtab [idx].tag = KLabel THEN
  5914. IF symtab [idx].defnd THEN
  5915. DropC (EmJmpNear (symtab [idx].goPos))
  5916. ELSE
  5917. zj := EmJmpNear (0) ;
  5918. AddPend (0, idx, zj)
  5919. END
  5920. ELSE
  5921. Err (EUnknown)
  5922. END
  5923. ELSE
  5924. idx := NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE) ;
  5925. zj := EmJmpNear (0) ;
  5926. AddPend (0, idx, zj)
  5927. END
  5928. ELSE
  5929. Err (EUnknown)
  5930. END
  5931. ELSIF tok = TkExit THEN
  5932. IF brkN = 0 THEN
  5933. Err (EUnknown)
  5934. ELSE
  5935. (* No EmAddSp (2) here, even inside a FOR. The FOR's `done` label
  5936. drops the bound, so an EXIT that jumped to `done` would drop it a
  5937. second time - 4 bytes off a stack that only had 2 to give, which
  5938. silently corrupts the caller's frame. It used to do exactly
  5939. that, and it was doubly wrong: the exits were patched to the STEP
  5940. rather than to `done`, so the EXIT also incremented the control
  5941. variable and jumped back into the test. *)
  5942. zj := EmJmpNear (0) ;
  5943. IF exitCnt < 64 THEN
  5944. exitPatch [exitCnt] := zj ;
  5945. INC (exitCnt)
  5946. END
  5947. END
  5948. ELSIF tok = TkWith THEN
  5949. (* WITH base (, base)* DO statement. The record's field range comes from
  5950. the base symbol: DefVar copies the ELEMENT record's recFldFirst/
  5951. recFldLast onto an array variable, which is what makes
  5952. `Sheet[I,J].Field' and `with Sheet[I,J] do' work. Each base is
  5953. pushed on the scope stack and popped after the statement. *)
  5954. savedWith := withN ;
  5955. LOOP
  5956. ParseExpr (t) ;
  5957. IF NOT OK () THEN
  5958. RETURN
  5959. END ;
  5960. IF t.cls # TRecord THEN
  5961. Err (ETypeErr) ;
  5962. RETURN
  5963. END ;
  5964. IF withN >= 4 THEN
  5965. Err (ECompOvf) ;
  5966. RETURN
  5967. END ;
  5968. IF symtab [t.idx].fldFirst >= symtab [t.idx].fldLast THEN
  5969. Err (ETypeErr) ;
  5970. RETURN
  5971. END ;
  5972. IF t.kind = 5 THEN
  5973. (* the base address is in DI; park it. Allocating two data bytes
  5974. here is safe: the statement is parsed once, and the store
  5975. executes wherever the WITH does. *)
  5976. withSlot [withN] := dc ;
  5977. dc := dc + 2 ;
  5978. EmStDiGlob (withSlot [withN]) ;
  5979. withIsDI [withN] := TRUE ;
  5980. withBoff [withN] := 0
  5981. ELSIF t.kind = 1 THEN
  5982. withIsDI [withN] := FALSE ;
  5983. withBoff [withN] := t.boff
  5984. ELSE
  5985. Err (ENoLib) ;
  5986. RETURN
  5987. END ;
  5988. withIdx [withN] := t.idx ;
  5989. withFF [withN] := symtab [t.idx].fldFirst ;
  5990. withFL [withN] := symtab [t.idx].fldLast ;
  5991. INC (withN) ;
  5992. IF MatchDelim (',') THEN
  5993. (* another base; the last one is innermost *)
  5994. ELSE
  5995. EXIT
  5996. END
  5997. END ;
  5998. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  5999. Err (ENoSemi) ;
  6000. RETURN
  6001. END ;
  6002. Statmnt () ;
  6003. withN := savedWith
  6004. ELSE
  6005. (* identifier statement: assignment or call *)
  6006. IF WithLookup (wrd, t) THEN
  6007. IF MatchAssign () THEN
  6008. Assignment (t) ;
  6009. RETURN
  6010. END ;
  6011. Err (ENoSemi) ;
  6012. RETURN
  6013. END ;
  6014. IF NOT Search (wrd, idx) THEN
  6015. Err (EUnknown) ;
  6016. RETURN
  6017. END ;
  6018. IF symtab [idx].tag = KBuiltin THEN
  6019. IoCall (idx) ;
  6020. RETURN
  6021. END ;
  6022. IF symtab [idx].tag = KProc THEN
  6023. IF MatchDelim ('(') THEN
  6024. ParseCallArgs (idx)
  6025. ELSE
  6026. ParseCall (idx)
  6027. END ;
  6028. RETURN
  6029. END ;
  6030. IF (symtab [idx].tag = KVar) OR (symtab [idx].tag = KFunc) THEN
  6031. t.idx := idx ;
  6032. t.kind := 1 ;
  6033. t.boff := 0 ;
  6034. t.cls := symtab [idx].cls ;
  6035. IF symtab [idx].tag = KFunc THEN
  6036. t.idx := symtab [idx].resvar ;
  6037. t.cls := symtab [t.idx].cls
  6038. END ;
  6039. t.sz := symtab [t.idx].size ; (* ParseSub may narrow it further *)
  6040. ParseSub (t) ;
  6041. IF MatchAssign () THEN
  6042. Assignment (t) ;
  6043. RETURN
  6044. END ;
  6045. Err (ENoSemi) ;
  6046. RETURN
  6047. END ;
  6048. Err (ENoSemi)
  6049. END
  6050. END Statmnt ;
  6051. (* ---------------------------------------------------------------- *)
  6052. (* types and declarations (TPSRC7) *)
  6053. (* ---------------------------------------------------------------- *)
  6054. PROCEDURE ParseType (VAR cls, size, elem : CARDINAL) ;
  6055. VAR tok : CARDINAL ;
  6056. idx : CARDINAL ;
  6057. lo, hi : LONGINT ;
  6058. s2, e2 : CARDINAL ;
  6059. subcls : CARDINAL ;
  6060. strf : BOOLEAN ;
  6061. consumed : BOOLEAN ;
  6062. running, groupFirst, i, an, recStart : CARDINAL ;
  6063. alo, ahi : ARRAY [0..3] OF LONGINT ;
  6064. ecls2 : CARDINAL ;
  6065. BEGIN
  6066. cls := TNone ; size := 0 ; elem := 0 ;
  6067. consumed := FALSE ;
  6068. newArrIdx := 0 ;
  6069. Skip () ; (* after ':' / '=' : skip blanks *)
  6070. IF CurCh () = '^' THEN
  6071. (* A pointer type: ^Type. The pointer itself is a word; its `elem'
  6072. field holds the pointee TYPE symbol, not a byte size. The pointee's
  6073. array/record metadata is copied onto the pointer variable so a
  6074. dereference can hand it to ParseSub unchanged. *)
  6075. DropCh (GetCh ()) ;
  6076. Skip () ;
  6077. IF NOT Alpha (CurCh ()) THEN
  6078. Err (ESimpType) ;
  6079. RETURN
  6080. END ;
  6081. GetWord () ;
  6082. IF NOT Search (wrd, idx) OR (symtab [idx].tag # KType) THEN
  6083. Err (ESimpType) ;
  6084. RETURN
  6085. END ;
  6086. cls := TPtr ;
  6087. size := 2 ;
  6088. elem := idx ; (* pointee type symbol *)
  6089. newArrIdx := symtab [idx].arrIdx ;
  6090. recFldFirst := symtab [idx].fldFirst ;
  6091. recFldLast := symtab [idx].fldLast ;
  6092. enumLo := symtab [idx].lo ;
  6093. enumHi := symtab [idx].hi ;
  6094. RETURN
  6095. END ;
  6096. IF CurCh () = '(' THEN
  6097. (* An enumeration: `(Constant, Formula, Txt, ...)'. Each enumerator
  6098. becomes a constant with its ordinal position as its value, and the
  6099. TYPE is 1 byte (TP3 uses one below 256 values) with bounds 0..n-1,
  6100. which DefType records so `set of Attributes' can size its base.
  6101. Enumerators are TScalar, not TEnum: they behave as ordinals; only the
  6102. TYPE carries the enum identity and its bounds. *)
  6103. DropCh (GetCh ()) ;
  6104. lo := 0 ;
  6105. hi := -1 ;
  6106. LOOP
  6107. Skip () ;
  6108. IF NOT Alpha (CurCh ()) THEN
  6109. Err (EUnknown) ;
  6110. RETURN
  6111. END ;
  6112. GetWord () ;
  6113. DupTest (wrd) ;
  6114. DropC (NewSym (wrd, KConst, TScalar, 1, 1, 0, hi + 1, FALSE)) ;
  6115. hi := hi + 1 ;
  6116. IF MatchDelim (',') THEN
  6117. (* another enumerator *)
  6118. ELSE
  6119. EXIT
  6120. END
  6121. END ;
  6122. ExpectDelim (')', ENoSemi) ;
  6123. enumLo := lo ;
  6124. enumHi := hi ;
  6125. cls := TEnum ;
  6126. size := 1 ;
  6127. elem := 1 ;
  6128. RETURN
  6129. END ;
  6130. IF Alpha (CurCh ()) THEN
  6131. DropB (MatchKey (tok)) ;
  6132. consumed := TRUE
  6133. ELSE
  6134. tok := TkNone
  6135. END ;
  6136. IF tok = TkArray THEN
  6137. (* array [range, range, ...] of T. Several ranges are allowed; the LAST
  6138. varies fastest (row-major), so `Sheet[fx,fy]' strides the first range
  6139. by the second's count (RESUME-TP3.md §2). The shape is kept in the
  6140. array table; the symbol carries a pointer to it (SymEntry.arrIdx) so
  6141. indexing can scale by the element size and subtract each lower bound. *)
  6142. ExpectDelim ('[', ENoSemi) ;
  6143. an := 0 ;
  6144. LOOP
  6145. RdConst (lo, subcls, strf) ;
  6146. IF NOT MatchRange () THEN
  6147. Err (ESimpType) ;
  6148. RETURN
  6149. END ;
  6150. RdConst (hi, subcls, strf) ;
  6151. IF an < 4 THEN
  6152. alo [an] := lo ;
  6153. ahi [an] := hi ;
  6154. INC (an)
  6155. END ;
  6156. IF MatchDelim (',') THEN
  6157. (* another range *)
  6158. ELSE
  6159. EXIT
  6160. END
  6161. END ;
  6162. ExpectDelim (']', ENoSemi) ;
  6163. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  6164. Err (ENoSemi) ;
  6165. RETURN
  6166. END ;
  6167. ParseType (cls, s2, e2) ;
  6168. ecls2 := cls ;
  6169. cls := TArray ;
  6170. elem := s2 ;
  6171. size := s2 ;
  6172. i := 0 ;
  6173. WHILE i < an DO
  6174. size := size * W16 (VAL (LONGINT, W16 (ahi [i]))
  6175. - VAL (LONGINT, W16 (alo [i])) + 1) ;
  6176. INC (i)
  6177. END ;
  6178. (* register the shape so indexing can scale by element size and subtract
  6179. each lower bound; the symbol points at this slot *)
  6180. IF arrTop < MaxArrs THEN
  6181. arrs [arrTop].n := an ;
  6182. arrs [arrTop].elem := s2 ;
  6183. arrs [arrTop].ecls := ecls2 ;
  6184. i := 0 ;
  6185. WHILE i < an DO
  6186. arrs [arrTop].lo [i] := alo [i] ;
  6187. arrs [arrTop].hi [i] := ahi [i] ;
  6188. INC (i)
  6189. END ;
  6190. newArrIdx := arrTop + 1 ;
  6191. INC (arrTop)
  6192. ELSE
  6193. Err (ECompOvf)
  6194. END
  6195. ELSIF tok = TkString THEN
  6196. cls := TString ;
  6197. size := 256 ;
  6198. elem := 1 ;
  6199. IF MatchDelim ('[') THEN
  6200. RdConst (hi, subcls, strf) ;
  6201. ExpectDelim (']', ENoSemi) ;
  6202. size := W16 (hi) + 1
  6203. END
  6204. ELSIF tok = TkSet THEN
  6205. (* set of <ordinal>. TP3's hybrid layout (coeur-tp-ocr 4.7.2): only the
  6206. whole BYTES covering the base range are stored, so `set of Char' is 32
  6207. bytes and `set of 12..17' is 2. `elem' carries the origin - the first
  6208. element of the first byte, a multiple of 8 - which is all the
  6209. membership and constructor arithmetic needs. *)
  6210. PeekKw (tok) ;
  6211. IF tok = TkOf THEN
  6212. DropB (MatchKey (tok)) ;
  6213. RdSetBase (subcls, lo, hi) ;
  6214. IF OK () THEN
  6215. cls := TSet ;
  6216. elem := (VAL (CARDINAL, lo) DIV 8) * 8 ;
  6217. size := (VAL (CARDINAL, hi) DIV 8)
  6218. - (VAL (CARDINAL, lo) DIV 8) + 1
  6219. END
  6220. ELSE
  6221. Err (ENoSemi)
  6222. END
  6223. ELSIF tok = TkRecord THEN
  6224. (* record fieldList end, fields NON-COMPACTED and juxtaposed
  6225. (RESUME-TP3.md §2). Each name in a `a, b : T' group is a field of its
  6226. own at the running offset; the field table is where `x.f' looks, keyed
  6227. per record so field names need not be globally unique.
  6228. recFldFirst/recFldLast hand the range to DefType, or to the enclosing
  6229. record's field entry when records nest. *)
  6230. recStart := fldTop ;
  6231. recFldFirst := fldTop ;
  6232. running := 0 ;
  6233. LOOP
  6234. PeekKw (tok) ;
  6235. IF tok = TkEnd THEN
  6236. DropB (MatchKey (tok)) ;
  6237. EXIT
  6238. END ;
  6239. IF CurCh () = 0C THEN
  6240. Err (EUnknown) ;
  6241. EXIT
  6242. END ;
  6243. (* the group's names, then ONE `: type' *)
  6244. groupFirst := fldTop ;
  6245. LOOP
  6246. Skip () ;
  6247. IF NOT Alpha (CurCh ()) THEN
  6248. Err (EUnknown) ;
  6249. RETURN
  6250. END ;
  6251. GetWord () ;
  6252. IF fldTop < MaxFields THEN
  6253. CopyStr (fields [fldTop].name, wrd) ;
  6254. fields [fldTop].off := 0 ;
  6255. INC (fldTop)
  6256. ELSE
  6257. Err (ECompOvf)
  6258. END ;
  6259. IF NOT MatchDelim (',') THEN
  6260. EXIT
  6261. END
  6262. END ;
  6263. ExpectDelim (':', ENoSemi) ;
  6264. ParseType (subcls, s2, e2) ;
  6265. IF NOT OK () THEN
  6266. RETURN
  6267. END ;
  6268. (* every name in the group gets the type and its own slot *)
  6269. i := groupFirst ;
  6270. WHILE i < fldTop DO
  6271. fields [i].cls := subcls ;
  6272. fields [i].size := s2 ;
  6273. fields [i].elem := e2 ;
  6274. fields [i].off := running ;
  6275. fields [i].lo := enumLo ;
  6276. fields [i].hi := enumHi ;
  6277. running := running + s2 ;
  6278. INC (i)
  6279. END ;
  6280. IF NOT MatchDelim (';') THEN
  6281. Err (ENoSemi) ;
  6282. RETURN
  6283. END
  6284. END ;
  6285. (* The field TYPES are parsed by ParseType, and its named-type arm
  6286. copies the TYPE's field range into recFldFirst/recFldLast - for
  6287. `Integer' that is 0/0. So recFldFirst set at the top of this branch
  6288. is overwritten by every field type. It happens to be right for the
  6289. FIRST record in a compile (fldTop is 0 then), and wrong for every
  6290. later one: t77's second record started at 2 but recorded 0, which
  6291. made a WITH field lookup scan the previous record's fields. Restore
  6292. the saved start here, after the last field type has been parsed. *)
  6293. recFldFirst := recStart ;
  6294. recFldLast := fldTop ;
  6295. cls := TRecord ;
  6296. size := running ;
  6297. elem := running
  6298. ELSIF tok = TkText THEN
  6299. (* A TEXT file variable. We do our own unbuffered INT 21h I/O, so the
  6300. file variable is just our control block (see FileVarSize) and the
  6301. element is one byte; TP3 keeps a 128-byte buffer in its own, which is
  6302. a divergence we do not need. *)
  6303. cls := TText ;
  6304. size := FileVarSize ;
  6305. elem := 1
  6306. ELSIF tok = TkFile THEN
  6307. (* `file' (untyped) or `file of T'. The element type is parsed for its
  6308. SIZE, which is what a Read/Write moves per record; the variable
  6309. itself is always the control block. CALC.INC's Exist declares
  6310. `F : file', so the OF must be optional: requiring it produced ENoSemi
  6311. at the declaration's semicolon, which was misread as the function
  6312. declaration being unsupported. TUntyp keeps the untyped file
  6313. distinguishable, because a Read/Write through it has no element size
  6314. and must stay refused. *)
  6315. PeekKw (tok) ;
  6316. IF tok = TkOf THEN
  6317. DropB (MatchKey (tok)) ;
  6318. ParseType (subcls, s2, e2) ;
  6319. IF OK () THEN
  6320. cls := TFile ;
  6321. size := FileVarSize ;
  6322. elem := s2
  6323. END
  6324. ELSE
  6325. cls := TUntyp ;
  6326. size := FileVarSize ;
  6327. elem := 1
  6328. END
  6329. ELSE
  6330. IF consumed THEN
  6331. IF Search (wrd, idx) AND (symtab [idx].tag = KType) THEN
  6332. cls := symtab [idx].cls ;
  6333. size := symtab [idx].size ;
  6334. (* The type's ELEMENT size, not its total size. They are equal for
  6335. a record, but not for an array (`Letters' is 6 bytes of 3
  6336. Integers) or a file. A named array typed constant used to get
  6337. typElem = 6, wrote six bytes per element and wrapped the
  6338. little-endian divider to zero: SIGFPE in DefConst. *)
  6339. elem := symtab [idx].elem ;
  6340. enumLo := symtab [idx].lo ;
  6341. enumHi := symtab [idx].hi ;
  6342. recFldFirst := symtab [idx].fldFirst ;
  6343. recFldLast := symtab [idx].fldLast ;
  6344. newArrIdx := symtab [idx].arrIdx
  6345. ELSE
  6346. Err (EUnknown)
  6347. END
  6348. ELSE
  6349. (* subrange lo .. hi. TP3 sizes it by its bounds: ONE byte when both
  6350. are inside 0..255, else two (RESUME-TP3.md §2). `0..20' and
  6351. `'A'..'G'' are one byte - which is what makes CellRec 80 and not
  6352. 82, and `Sheet' 147*80. The bounds are kept for `set of T'. *)
  6353. RdConst (lo, subcls, strf) ;
  6354. IF NOT MatchRange () THEN
  6355. Err (ESimpType) ;
  6356. RETURN
  6357. END ;
  6358. RdConst (hi, subcls, strf) ;
  6359. cls := TScalar ;
  6360. IF (lo >= 0) AND (hi <= 255) AND (lo <= hi) THEN
  6361. size := 1
  6362. ELSE
  6363. size := 2
  6364. END ;
  6365. elem := size ;
  6366. enumLo := lo ;
  6367. enumHi := hi
  6368. END
  6369. END
  6370. END ParseType ;
  6371. PROCEDURE DefVar () ;
  6372. (* 'name' (',' name)* ':' type [ABSOLUTE addr] ; group repeated until a
  6373. declaration keyword appears *)
  6374. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  6375. cls, size, elem : CARDINAL ;
  6376. tok : CARDINAL ;
  6377. v : LONGINT ;
  6378. off : CARDINAL ;
  6379. first, i : CARDINAL ;
  6380. BEGIN
  6381. LOOP
  6382. Skip () ; (* after the VAR keyword: skip blanks *)
  6383. IF NOT Alpha (CurCh ()) THEN
  6384. Err (EUnknown) ;
  6385. RETURN
  6386. END ;
  6387. (* A group is `name (, name)* : type' - TPSRC7 rdvarlst/rdvloop collects
  6388. the whole comma-separated list of names FIRST (`CALL ccomma / JZ
  6389. rdvloop'), then reads one `: type' with ecolon/rdvartp, then vardef2
  6390. gives every name in the list that one type. The names are created as
  6391. placeholders here and filled in once the type is known, which is the
  6392. same shape as TP3 reserving a symtab slot per name and storing the
  6393. type pointer into each afterwards. *)
  6394. first := symTop ;
  6395. LOOP
  6396. Skip () ; (* GetWord does not skip blanks, and the cursor
  6397. sits on the space after the ',' *)
  6398. IF NOT Alpha (CurCh ()) THEN
  6399. Err (EUnknown) ;
  6400. RETURN
  6401. END ;
  6402. GetWord () ;
  6403. SaveWord (nm) ;
  6404. DupTest (nm) ;
  6405. DropC (NewSym (nm, KVar, TNone, 0, 0, 0, 0, lexnest # 0)) ;
  6406. IF NOT MatchDelim (',') THEN
  6407. EXIT
  6408. END
  6409. END ;
  6410. ExpectDelim (':', ENoSemi) ;
  6411. ParseType (cls, size, elem) ;
  6412. IF OK () THEN
  6413. IF lexnest = 0 THEN
  6414. IF (size > 2) AND (cls # TString) AND (cls # TRecord)
  6415. AND (cls # TArray) AND (cls # TFile) AND (cls # TText)
  6416. AND (cls # TUntyp) THEN
  6417. (* Still the catch-all for a type whose storage this compiler does
  6418. not lay out yet (an ARRAY at its point of use, a RECORD, ...).
  6419. A STRING is the exception now: its layout is length byte +
  6420. characters, and the rest of the compiler knows it - see
  6421. Assignment and IoCall. *)
  6422. Err (ENoLib) ;
  6423. RETURN
  6424. END ;
  6425. PeekKw (tok) ;
  6426. IF tok = TkAbsolute THEN
  6427. DropB (MatchKey (tok)) ;
  6428. IF (CurCh () = '$') OR (Digit (CurCh ())) THEN
  6429. RdIntConst (v) ;
  6430. off := W16 (v)
  6431. ELSE
  6432. Err (EUnknown) ;
  6433. off := 0
  6434. END ;
  6435. (* TP3's absolute path (vdstore) leaves the offset fixed, so
  6436. every name in the group aliases the same address. *)
  6437. i := first ;
  6438. WHILE i < symTop DO
  6439. symtab [i].cls := cls ; symtab [i].size := size ;
  6440. symtab [i].elem := elem ; symtab [i].off := off ;
  6441. symtab [i].fldFirst := recFldFirst ;
  6442. symtab [i].fldLast := recFldLast ;
  6443. symtab [i].arrIdx := newArrIdx ;
  6444. INC (i)
  6445. END
  6446. ELSE
  6447. i := first ;
  6448. WHILE i < symTop DO
  6449. symtab [i].cls := cls ; symtab [i].size := size ;
  6450. symtab [i].elem := elem ; symtab [i].off := dc ;
  6451. symtab [i].fldFirst := recFldFirst ;
  6452. symtab [i].fldLast := recFldLast ;
  6453. symtab [i].arrIdx := newArrIdx ;
  6454. dc := dc + size ;
  6455. INC (i)
  6456. END
  6457. END ;
  6458. varspc := varspc + size * (symTop - first)
  6459. ELSE
  6460. IF (size > 2) AND (cls # TString) AND (cls # TRecord)
  6461. AND (cls # TArray) AND (cls # TFile) AND (cls # TText)
  6462. AND (cls # TUntyp) THEN
  6463. Err (ENoLib) ;
  6464. RETURN
  6465. END ;
  6466. (* A local occupies [off, off+size-1] and lives BELOW the saved BP,
  6467. because BP-relative locals grow down from the top of the frame
  6468. while a STRING grows UP from its base (length byte, then chars).
  6469. Top-anchored packing keeps both true at once: locFree is the
  6470. highest still-free byte (-1 at the top), the new symbol's base
  6471. is placed so that its LAST byte lands on that byte, and locFree
  6472. then drops by the whole size. So the first INTEGER (size 2) is
  6473. at -2 and the second at -4, byte for byte what the old
  6474. pre-decrement rule produced, while a string[5] local (size 6) is
  6475. at -6..-1 instead of the old -2..+3 - which straddled the saved
  6476. BP at [BP] and the return address at [BP+2] and made `readln(s)'
  6477. into a local store characters over its own return address.
  6478. Each name in the group gets its own slot. *)
  6479. i := first ;
  6480. WHILE i < symTop DO
  6481. symtab [i].cls := cls ; symtab [i].size := size ;
  6482. symtab [i].elem := elem ;
  6483. symtab [i].fldFirst := recFldFirst ;
  6484. symtab [i].fldLast := recFldLast ;
  6485. symtab [i].arrIdx := newArrIdx ;
  6486. symtab [i].off := (locFree - size + 1) MOD 10000H ;
  6487. locFree := (locFree - size) MOD 10000H ;
  6488. INC (i)
  6489. END ;
  6490. locBytes := locBytes + size * (symTop - first)
  6491. END
  6492. END ;
  6493. IF MatchDelim (',') THEN
  6494. (* The older `name : type, name : type' spelling: another group
  6495. follows without a semicolon. TP3's grammar is names-then-one-type
  6496. with ',' inside a group and ';' between groups (TPSRC7 rdvarlst /
  6497. rdvloop then ecolon/rdvartp), and both spellings reach the same
  6498. symbols, so both separators are accepted here. *)
  6499. ELSIF MatchDelim (';') THEN
  6500. PeekKw (tok) ;
  6501. IF tok # TkNone THEN
  6502. RETURN
  6503. END
  6504. ELSE
  6505. Err (ENoSemi) ;
  6506. RETURN
  6507. END
  6508. END
  6509. END DefVar ;
  6510. PROCEDURE DefConst () ;
  6511. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  6512. v : LONGINT ;
  6513. cls, typCls, typSize, typElem, soff, ci : CARDINAL ;
  6514. isStr : BOOLEAN ;
  6515. tok : CARDINAL ;
  6516. typed, setTyped, arrTyped : BOOLEAN ;
  6517. nb, k, divi, w : CARDINAL ;
  6518. BEGIN
  6519. LOOP
  6520. PeekKw (tok) ;
  6521. IF tok # TkNone THEN
  6522. RETURN
  6523. END ;
  6524. Skip () ; (* after the CONST keyword: skip blanks *)
  6525. IF NOT Alpha (CurCh ()) THEN
  6526. Err (EUnknown) ;
  6527. RETURN
  6528. END ;
  6529. GetWord () ;
  6530. SaveWord (nm) ;
  6531. DupTest (nm) ;
  6532. (* Two spellings: a plain constant `name = value', and a TYPED constant
  6533. `name : Type = value', which TP3 3.0 added (TPSRC7's constant reader
  6534. takes the type first and the value second). For a SCALAR type the
  6535. value still folds to an immediate, so the declared type is simply
  6536. recorded as the symbol's class - `FXMax : Char = 'G'' is a TChar
  6537. constant holding 71, and `writeln (FXMax)' prints G rather than 71.
  6538. A SET typed constant (`Numbers : set of Char = ['0'..'9']') is a value
  6539. with a layout, so it cannot fold: its bytes are emitted code-resident
  6540. and the name becomes an addressable value. See the storage decision
  6541. in CALC-COMPILE.md. An ARRAY or RECORD typed constant is still
  6542. refused loudly - those layouts come later. *)
  6543. typed := FALSE ;
  6544. setTyped := FALSE ;
  6545. arrTyped := FALSE ;
  6546. Skip () ;
  6547. IF CurCh () = ':' THEN
  6548. DropCh (GetCh ()) ;
  6549. ParseType (typCls, typSize, typElem) ;
  6550. IF OK () THEN
  6551. IF typCls = TSet THEN
  6552. setTyped := TRUE
  6553. ELSIF typCls = TArray THEN
  6554. arrTyped := TRUE
  6555. ELSIF typSize > 2 THEN
  6556. Err (ENoLib)
  6557. ELSE
  6558. typed := TRUE
  6559. END
  6560. END
  6561. END ;
  6562. IF OK () AND setTyped THEN
  6563. ExpectDelim ('=', ENoSemi) ;
  6564. IF RdSetLiteral (typElem, typSize, constBlob) THEN
  6565. soff := EmSetBlob (constBlob, typSize) ;
  6566. DropC (NewSym (nm, KVar, TSet, typSize, typElem, soff, 0, FALSE))
  6567. END
  6568. ELSIF OK () AND arrTyped THEN
  6569. (* array typed constant: `(v1, v2, ...)' in element order, each value
  6570. written little-endian in typElem bytes. CALC's is XPOS (seven
  6571. Integers, 14 bytes). An element that is a STRING (CALC.INC's
  6572. StandardfunctionNames) is a later increment, and is refused here. *)
  6573. ExpectDelim ('=', ENoSemi) ;
  6574. ExpectDelim ('(', ENoSemi) ;
  6575. nb := 0 ;
  6576. LOOP
  6577. RdConst (v, cls, isStr) ;
  6578. IF isStr OR (nb + typElem > 512) THEN
  6579. Err (ENoLib) ;
  6580. EXIT
  6581. END ;
  6582. w := VAL (CARDINAL, W16 (v)) ;
  6583. divi := 1 ;
  6584. k := 0 ;
  6585. WHILE k < typElem DO
  6586. constBlob [nb] := CHR ((w DIV divi) MOD 256) ;
  6587. divi := divi * 256 ;
  6588. INC (nb) ;
  6589. INC (k)
  6590. END ;
  6591. IF MatchDelim (',') THEN
  6592. (* another element *)
  6593. ELSE
  6594. EXIT
  6595. END
  6596. END ;
  6597. ExpectDelim (')', ENoSemi) ;
  6598. IF OK () THEN
  6599. soff := EmSetBlob (constBlob, typSize) ;
  6600. ci := NewSym (nm, KVar, TArray, typSize, typElem, soff, 0, FALSE) ;
  6601. symtab [ci].arrIdx := newArrIdx
  6602. END
  6603. ELSIF OK () THEN
  6604. ExpectDelim ('=', ENoSemi) ;
  6605. RdConst (v, cls, isStr) ;
  6606. IF isStr THEN
  6607. Err (ENoLib)
  6608. END ;
  6609. IF OK () THEN
  6610. IF typed THEN
  6611. cls := typCls
  6612. END ;
  6613. DropC (NewSym (nm, KConst, cls, 0, 0, 0, v, FALSE))
  6614. END
  6615. END ;
  6616. IF NOT MatchDelim (';') THEN
  6617. Err (ENoSemi) ;
  6618. RETURN
  6619. END
  6620. END
  6621. END DefConst ;
  6622. PROCEDURE DefLabelPart () ;
  6623. VAR nm : ARRAY [0..9] OF CHAR ;
  6624. n : CARDINAL ;
  6625. BEGIN
  6626. LOOP
  6627. Skip () ;
  6628. IF NOT Digit (CurCh ()) THEN
  6629. Err (EUnknown) ;
  6630. RETURN
  6631. END ;
  6632. n := 0 ;
  6633. WHILE Digit (CurCh ()) DO
  6634. n := n * 10 + (ORD (CurCh ()) - ORD ('0')) ;
  6635. DropCh (GetCh ())
  6636. END ;
  6637. NumToName (n, nm) ;
  6638. IF NOT Search (nm, n) THEN
  6639. DropC (NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE))
  6640. END ;
  6641. IF NOT MatchDelim (',') THEN
  6642. IF MatchDelim (';') THEN
  6643. RETURN
  6644. END ;
  6645. Err (ENoSemi) ;
  6646. RETURN
  6647. END
  6648. END
  6649. END DefLabelPart ;
  6650. PROCEDURE IfMatchSemi () ;
  6651. BEGIN
  6652. IF NOT MatchDelim (';') THEN
  6653. Err (ENoSemi)
  6654. END
  6655. END IfMatchSemi ;
  6656. PROCEDURE SymEpi () ;
  6657. (* function result: AX := result var for a scalar, or copy six bytes to the
  6658. caller's result scratch for a REAL function. *)
  6659. VAR k : CARDINAL ;
  6660. BEGIN
  6661. IF curIsFunc THEN
  6662. IF OK () THEN
  6663. IF resultIsReal THEN
  6664. EmLoadVar (TRUE, resultPtrOff, 2) ;
  6665. EmMovDiAx () ;
  6666. k := 0 ;
  6667. WHILE k < 6 DO
  6668. EmLoadVar (TRUE, symtab [resultVar].off + k, 1) ;
  6669. EmStDiAlOff (k) ;
  6670. INC (k)
  6671. END
  6672. ELSE
  6673. EmLoadVar (TRUE, symtab [resultVar].off, symtab [resultVar].size)
  6674. END
  6675. END
  6676. END
  6677. END SymEpi ;
  6678. PROCEDURE SlotSize (cls, size : CARDINAL) : CARDINAL ;
  6679. (* How many bytes a value parameter occupies in the callee's stack frame.
  6680. TP3's `tstscal' (TPSRC7 1243): a scalar-like parameter is AT LEAST two
  6681. bytes on the stack even when its type is one byte; anything larger spends
  6682. its own size. Generalised here so step 2 can put a string's 71 bytes on
  6683. the stack without changing what a scalar does today. *)
  6684. BEGIN
  6685. IF (cls = TScalar) OR (cls = TChar) OR (cls = TBool) OR (cls = TEnum) THEN
  6686. RETURN 2
  6687. END ;
  6688. RETURN size
  6689. END SlotSize ;
  6690. PROCEDURE ParamSlotBytes (e : SymEntry) : CARDINAL ;
  6691. (* The bytes a parameter occupies in the frame. A VAR parameter's slot is the
  6692. ADDRESS, always two bytes, whatever its type: `size' stays the type's width
  6693. so loads and stores know whether they read one byte or two. *)
  6694. BEGIN
  6695. IF e.isVar THEN
  6696. RETURN 2
  6697. END ;
  6698. RETURN SlotSize (e.cls, e.size)
  6699. END ParamSlotBytes ;
  6700. PROCEDURE ProcFunc () ;
  6701. (* PROCEDURE name (params) ; body | FUNCTION name (params) : type ; body *)
  6702. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  6703. idx, old : CARDINAL ;
  6704. tok, tok2 : CARDINAL ;
  6705. parmNm : ARRAY [0..MaxName] OF CHAR ;
  6706. cls, size, elem : CARDINAL ;
  6707. isFunc, groupVar : BOOLEAN ;
  6708. saveNest, saveLoc, saveRes, saveF : CARDINAL ;
  6709. saveLB, savePO, saveRP : CARDINAL ;
  6710. saveR : BOOLEAN ;
  6711. realResult : BOOLEAN ;
  6712. nestMark : CARDINAL ; (* symTop just inside this procedure *)
  6713. i, first : CARDINAL ;
  6714. BEGIN
  6715. isFunc := curIsFunc ;
  6716. Skip () ; (* after the PROCEDURE/FUNCTION keyword *)
  6717. IF NOT Alpha (CurCh ()) THEN
  6718. Err (EUnknown) ;
  6719. RETURN
  6720. END ;
  6721. GetWord () ;
  6722. SaveWord (nm) ;
  6723. IF Search (nm, idx) AND (symtab [idx].tag = KProc)
  6724. AND (symtab [idx].fwd) THEN
  6725. old := idx
  6726. ELSIF Search (nm, idx) THEN
  6727. Err (EUnknown) ;
  6728. RETURN
  6729. ELSE
  6730. old := NewSym (nm, KProc, TNone, 0, 0, 0, 0, FALSE)
  6731. END ;
  6732. IF isFunc THEN
  6733. symtab [old].tag := KFunc
  6734. END ;
  6735. saveNest := lexnest ;
  6736. saveLoc := locFree ;
  6737. saveRes := resultVar ;
  6738. saveF := VAL (CARDINAL, ORD (curIsFunc)) ;
  6739. saveLB := locBytes ;
  6740. savePO := parmOff ;
  6741. saveRP := resultPtrOff ;
  6742. saveR := resultIsReal ;
  6743. INC (lexnest) ;
  6744. nestMark := symTop ; (* after the proc's own name, before its params *)
  6745. locFree := 0FFFFH ;
  6746. locBytes := 0 ;
  6747. parmOff := 4 ;
  6748. IF MatchDelim ('(') THEN
  6749. IF CurCh () # ')' THEN
  6750. LOOP
  6751. (* PeekKw, not MatchKey: MatchKey CONSUMES the word it reads, so
  6752. using it to test for VAR would eat the parameter's name. *)
  6753. PeekKw (tok) ;
  6754. groupVar := FALSE ;
  6755. IF tok = TkVar THEN
  6756. (* A by-reference parameter: the slot will hold an ADDRESS.
  6757. `size' still carries the type's own width, because the
  6758. callee's loads and stores must know whether they move one
  6759. byte or two. *)
  6760. DropB (MatchKey (tok)) ;
  6761. groupVar := TRUE
  6762. END ;
  6763. first := symTop ;
  6764. LOOP
  6765. Skip () ; (* blanks before the parameter name *)
  6766. IF NOT Alpha (CurCh ()) THEN
  6767. Err (EUnknown) ;
  6768. RETURN
  6769. END ;
  6770. GetWord () ;
  6771. SaveWord (parmNm) ;
  6772. DupTest (parmNm) ;
  6773. DropC (NewSym (parmNm, KVar, TNone, 0, 0, 0, 0, TRUE)) ;
  6774. IF NOT MatchDelim (',') THEN
  6775. EXIT
  6776. END
  6777. END ;
  6778. ExpectDelim (':', ENoSemi) ; (* formal is 'name : type' *)
  6779. ParseType (cls, size, elem) ;
  6780. IF groupVar AND (size > 2) AND (cls # TString) AND (cls # TReal) THEN
  6781. (* A VAR parameter of a record or array would need the callee
  6782. to address through a larger object than the scalar [BX]
  6783. paths; refusing is the honest answer. VAR STRING and VAR
  6784. REAL are the exceptions: both slots are two-byte ADDRESSES,
  6785. and both value types already have address-based runtime
  6786. paths. *)
  6787. Err (ENoLib) ;
  6788. RETURN
  6789. END ;
  6790. IF (size > 2) AND (cls # TString) AND (cls # TReal) THEN
  6791. (* A string VALUE parameter is a whole-slot copy (n+1 bytes on
  6792. the stack); any other type wider than a word - a record, an
  6793. array, a set - is still refused. A VAR REAL is allowed:
  6794. its slot is an address, not the six bytes. *)
  6795. Err (ENoLib) ;
  6796. RETURN
  6797. END ;
  6798. i := first ;
  6799. WHILE i < symTop DO
  6800. symtab [i].cls := cls ; symtab [i].size := size ;
  6801. symtab [i].elem := elem ;
  6802. symtab [i].off := parmOff ;
  6803. symtab [i].isVar := groupVar ;
  6804. IF groupVar THEN
  6805. parmOff := parmOff + 2 (* the address, whatever the type *)
  6806. ELSE
  6807. parmOff := parmOff + SlotSize (cls, size)
  6808. END ;
  6809. INC (i)
  6810. END ;
  6811. IF MatchDelim (')') THEN
  6812. EXIT
  6813. END ;
  6814. IF MatchDelim (';') THEN
  6815. (* next group *)
  6816. ELSIF MatchDelim (',') THEN
  6817. (* the older `name : type, name : type' spelling *)
  6818. ELSE
  6819. Err (ENoSemi) ;
  6820. EXIT
  6821. END
  6822. END
  6823. ELSE
  6824. DropCh (GetCh ())
  6825. END
  6826. END ;
  6827. (* TP3 lays a parameter list out BACKWARDS from BP+4: the LAST declared
  6828. parameter sits at BP+4 and each earlier one is 2 bytes farther from BP
  6829. (RESUME-TP3.md 3.11 - "le dernier paramètre déclaré est le plus proche
  6830. (BP+4)" - and the Code Generation Internals stack frame, "BP+4: last
  6831. parameter"). This is the other half of pushing the arguments as they
  6832. are parsed: the first argument is pushed first and so ends up farthest
  6833. from BP, which has to be the FIRST declared parameter's slot.
  6834. During the loop the offsets were provisional, in declaration order
  6835. (parmOff starts at 4 and grows by each parameter's SLOT size), so
  6836. parameter k sat at 4 + sum of the slots before it. For slot sizes s and
  6837. frame total T, the parameter's final base is
  6838. 4 + (T - prefix - s) = 8 + T - off - s, which sends the FIRST declared
  6839. parameter to the far end and the LAST to BP+4. With every slot 2 -
  6840. every scalar, which is all this compiler accepts today - that reduces
  6841. exactly to the old `parmOff + 2 - off`, so no existing frame moves.
  6842. A slot larger than 2 (a string's 71 bytes) falls out of the same
  6843. formula, and a VAR parameter is exactly 2 even when its TYPE is one
  6844. byte: ParamSlotBytes carries that rule.
  6845. The range is exactly the parameters: the only symbol created while the
  6846. list is read is the parameter itself (NewSym inside the loop),
  6847. ParseType creates no symbols, the procedure's own name predates
  6848. nestMark, and a function's result variable is created after this point. *)
  6849. i := nestMark ;
  6850. WHILE i < symTop DO
  6851. symtab [i].off := 8 + (parmOff - 4) - symtab [i].off
  6852. - ParamSlotBytes (symtab [i]) ;
  6853. INC (i)
  6854. END ;
  6855. (* Record the parameter list on the procedure symbol: a call site has to
  6856. know each argument's SLOT size to push and clean up the right number of
  6857. bytes. Only when this definition actually read a list - a FORWARD's
  6858. list is kept, and the later `function Expression;' that repeats only the
  6859. name must not wipe it. *)
  6860. IF symTop > nestMark THEN
  6861. symtab [old].parmFirst := nestMark ;
  6862. symtab [old].parmCount := symTop - nestMark
  6863. END ;
  6864. realResult := FALSE ;
  6865. IF isFunc THEN
  6866. IF MatchDelim (':') THEN
  6867. ParseType (cls, size, elem)
  6868. ELSIF symtab [old].fwd AND (symtab [old].cls # TNone) THEN
  6869. (* The definition of a forward repeats only the name; inherit the
  6870. result type declared by the forward. *)
  6871. cls := symtab [old].cls ;
  6872. size := symtab [old].size ;
  6873. elem := symtab [old].elem
  6874. ELSE
  6875. cls := TScalar ;
  6876. size := 2 ;
  6877. elem := 2
  6878. END ;
  6879. IF (size > 2) AND (cls # TReal) THEN
  6880. Err (ENoLib) ;
  6881. RETURN
  6882. END ;
  6883. symtab [old].cls := cls ;
  6884. symtab [old].size := size ;
  6885. resultVar := NewSym (nm, KVar, cls, size, elem,
  6886. (locFree - size + 1) MOD 10000H, 0, TRUE) ;
  6887. symtab [old].resvar := resultVar ;
  6888. locFree := (locFree - size) MOD 10000H ;
  6889. locBytes := locBytes + size ;
  6890. resultIsReal := (cls = TReal) ;
  6891. IF resultIsReal THEN
  6892. (* Hidden two-byte local holding the caller's result destination.
  6893. The caller passes it in DI before the CALL. *)
  6894. resultPtrOff := (locFree - 1) MOD 10000H ;
  6895. locFree := (locFree - 2) MOD 10000H ;
  6896. locBytes := locBytes + 2
  6897. ELSE
  6898. resultPtrOff := 0
  6899. END
  6900. END ;
  6901. IfMatchSemi () ;
  6902. PeekKw (tok2) ;
  6903. IF (tok2 = TkForward) OR (tok2 = TkExternal) THEN
  6904. DropB (MatchKey (tok2)) ;
  6905. symtab [old].fwd := TRUE ;
  6906. symtab [old].defnd := (tok2 = TkExternal) ;
  6907. IfMatchSemi () ;
  6908. HideLocals (nestMark) ; (* a FORWARD's parameters are not the
  6909. caller's to see either *)
  6910. lexnest := saveNest ;
  6911. locFree := saveLoc ;
  6912. resultVar := saveRes ;
  6913. resultIsReal := saveR ;
  6914. resultPtrOff := saveRP ;
  6915. curIsFunc := (saveF # 0) ;
  6916. locBytes := saveLB ;
  6917. parmOff := savePO ;
  6918. RETURN
  6919. END ;
  6920. (* body *)
  6921. symtab [old].goPos := pc ;
  6922. symtab [old].defnd := TRUE ;
  6923. EmPushBp () ;
  6924. EmMovBpSp () ;
  6925. IF resultIsReal THEN
  6926. (* The caller put the six-byte result destination in DI. Save it
  6927. before declarations/body can reuse DI. *)
  6928. EmMovAxDi () ;
  6929. EmStoreVar (TRUE, resultPtrOff, 2)
  6930. END ;
  6931. DefPart () ; (* nested declarations; stops at BEGIN *)
  6932. IF locBytes > 0 THEN
  6933. EmSubSp (locBytes)
  6934. END ;
  6935. DropC (EmCall (TU_StackChk)) ;
  6936. Statmnt () ; (* body *)
  6937. IF OK () THEN
  6938. SymEpi () ;
  6939. EmLeave () ;
  6940. EmRet ()
  6941. END ;
  6942. (* patch pending forward calls to this proc *)
  6943. i := 0 ;
  6944. WHILE i < nPend DO
  6945. IF (pend [i].kind = 1) AND (pend [i].who = old) THEN
  6946. SetPatTgt (pend [i].place, symtab [old].goPos) ;
  6947. pend [i].kind := 99
  6948. END ;
  6949. INC (i)
  6950. END ;
  6951. HideLocals (nestMark) ; (* parameters and locals stop here *)
  6952. lexnest := saveNest ;
  6953. locFree := saveLoc ;
  6954. resultVar := saveRes ;
  6955. resultIsReal := saveR ;
  6956. resultPtrOff := saveRP ;
  6957. curIsFunc := (saveF # 0) ;
  6958. locBytes := saveLB ;
  6959. parmOff := savePO
  6960. END ProcFunc ;
  6961. PROCEDURE DefType () ;
  6962. (* 'name' '=' typeDef ; ... until a declaration keyword appears *)
  6963. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  6964. cls, size, elem, tidx : CARDINAL ;
  6965. tok : CARDINAL ;
  6966. BEGIN
  6967. LOOP
  6968. PeekKw (tok) ;
  6969. IF tok # TkNone THEN
  6970. RETURN
  6971. END ;
  6972. Skip () ; (* after the TYPE keyword: skip blanks *)
  6973. IF NOT Alpha (CurCh ()) THEN
  6974. Err (EUnknown) ;
  6975. RETURN
  6976. END ;
  6977. GetWord () ;
  6978. SaveWord (nm) ;
  6979. DupTest (nm) ;
  6980. ExpectDelim ('=', ENoSemi) ;
  6981. ParseType (cls, size, elem) ;
  6982. IF NOT OK () THEN
  6983. RETURN
  6984. END ;
  6985. tidx := NewSym (nm, KType, cls, size, elem, 0, 0, FALSE) ;
  6986. IF cls = TEnum THEN
  6987. (* the enumeration's bounds, set by ParseType, belong to the type - so
  6988. `set of Attributes' can size its base (0..n-1, one byte) *)
  6989. symtab [tidx].lo := enumLo ;
  6990. symtab [tidx].hi := enumHi
  6991. END ;
  6992. IF cls = TRecord THEN
  6993. (* and a record's fields live in the field table; the type records
  6994. which range is its own *)
  6995. symtab [tidx].fldFirst := recFldFirst ;
  6996. symtab [tidx].fldLast := recFldLast
  6997. END ;
  6998. IF cls = TArray THEN
  6999. symtab [tidx].arrIdx := newArrIdx ;
  7000. (* ...and the ELEMENT's field table, if it is a record. A variable
  7001. declared `Sheet : Cells' gets its field metadata through the named
  7002. type; without this, `Sheet[I,J].Field' and `with Sheet[I,J]' found
  7003. fldFirst = fldLast = 0 and reported "not a record". An INLINE
  7004. `array ... of Rec' worked because ParseType's element call left
  7005. the globals set for DefVar; the named form had lost them. *)
  7006. symtab [tidx].fldFirst := recFldFirst ;
  7007. symtab [tidx].fldLast := recFldLast
  7008. END ;
  7009. DropC (tidx) ;
  7010. IF NOT MatchDelim (';') THEN
  7011. Err (ENoSemi) ;
  7012. RETURN
  7013. END
  7014. END
  7015. END DefType ;
  7016. PROCEDURE DefPart () ;
  7017. (* LABEL / CONST / TYPE / VAR / OVERLAY / PROC / FUNCTION / BEGIN *)
  7018. VAR tok : CARDINAL ;
  7019. BEGIN
  7020. LOOP
  7021. IF MatchDelim (';') THEN
  7022. (* separator between declarations *)
  7023. ELSE
  7024. PeekKw (tok) ;
  7025. IF tok = TkBegin THEN
  7026. RETURN
  7027. END ;
  7028. IF NOT MatchKey (tok) THEN
  7029. Err (EUnknown) ;
  7030. RETURN
  7031. END ;
  7032. CASE tok OF
  7033. TkLabel : DefLabelPart () ;
  7034. | TkConst : DefConst () ;
  7035. | TkType : DefType () ;
  7036. | TkVar : DefVar () ;
  7037. | TkOverlay :
  7038. LOOP
  7039. Skip () ;
  7040. IF CurCh () = ';' THEN
  7041. DropCh (GetCh ()) ;
  7042. EXIT
  7043. END ;
  7044. IF CurCh () = 0C THEN
  7045. Err (ENoSemi) ;
  7046. EXIT
  7047. END ;
  7048. DropCh (GetCh ())
  7049. END ;
  7050. | TkProcedure : curIsFunc := FALSE ; ProcFunc () ;
  7051. | TkFunction : curIsFunc := TRUE ; ProcFunc () ;
  7052. ELSE
  7053. Err (EUnknown) ;
  7054. RETURN
  7055. END ;
  7056. IF NOT OK () THEN
  7057. RETURN
  7058. END
  7059. END
  7060. END
  7061. END DefPart ;
  7062. (* ---------------------------------------------------------------- *)
  7063. (* driver (TPSRC7 compile) *)
  7064. (* ---------------------------------------------------------------- *)
  7065. PROCEDURE DefBuiltins () ;
  7066. (* The standard procedures. Without these, WRITELN is absent from the
  7067. symbol table, Statmnt's identifier branch fails its Search and every
  7068. program that prints anything dies with EUnknown (41) on the '(' after the
  7069. call name - the single remaining cause of failure in the fixture matrix.
  7070. Tagged KBuiltin (not KProc) because these are not called generically:
  7071. WRITE/WRITELN/READ/READLN need TP3's per-argument type dispatch
  7072. (IoCall), and HALT takes no argument at all. defnd is TRUE because the
  7073. entry point is known - there is no forward reference to patch. *)
  7074. VAR i : CARDINAL ;
  7075. BEGIN
  7076. i := NewSym ("WRITE" , KBuiltin, BI_Write , 0, 0, 0, 0, FALSE) ;
  7077. symtab [i].defnd := TRUE ;
  7078. symtab [i].goPos := TU_WrInt ;
  7079. i := NewSym ("WRITELN", KBuiltin, BI_WriteLn, 0, 0, 0, 0, FALSE) ;
  7080. symtab [i].defnd := TRUE ;
  7081. symtab [i].goPos := TU_WrInt ;
  7082. i := NewSym ("READ" , KBuiltin, BI_Read , 0, 0, 0, 0, FALSE) ;
  7083. symtab [i].defnd := TRUE ;
  7084. symtab [i].goPos := TU_RdInt ;
  7085. i := NewSym ("READLN" , KBuiltin, BI_ReadLn , 0, 0, 0, 0, FALSE) ;
  7086. symtab [i].defnd := TRUE ;
  7087. symtab [i].goPos := TU_RdInt ;
  7088. i := NewSym ("HALT" , KBuiltin, BI_Halt , 0, 0, 0, 0, FALSE) ;
  7089. symtab [i].defnd := TRUE ;
  7090. symtab [i].goPos := TU_Halt ;
  7091. i := NewSym ("LENGTH" , KBuiltin, BI_Length , 0, 0, 0, 0, FALSE) ;
  7092. symtab [i].defnd := TRUE ;
  7093. symtab [i].goPos := 0 ; (* inline: the length byte, no entry *)
  7094. (* The CRT identifiers. ClrScr and GotoXY emit real INT 10h code; the other
  7095. four are registered so they are recognised, and IoCall refuses them with
  7096. ENoLib - loud, not inert. *)
  7097. i := NewSym ("CLRSCR" , KBuiltin, BI_ClrScr , 0, 0, 0, 0, FALSE) ;
  7098. symtab [i].defnd := TRUE ;
  7099. i := NewSym ("GOTOXY" , KBuiltin, BI_GotoXY , 0, 0, 0, 0, FALSE) ;
  7100. symtab [i].defnd := TRUE ;
  7101. i := NewSym ("LOWVIDEO", KBuiltin, BI_LowVideo, 0, 0, 0, 0, FALSE) ;
  7102. symtab [i].defnd := TRUE ;
  7103. i := NewSym ("NORMVIDEO", KBuiltin, BI_NormVideo, 0, 0, 0, 0, FALSE) ;
  7104. symtab [i].defnd := TRUE ;
  7105. i := NewSym ("CLREOL" , KBuiltin, BI_ClrEol , 0, 0, 0, 0, FALSE) ;
  7106. symtab [i].defnd := TRUE ;
  7107. i := NewSym ("DELAY" , KBuiltin, BI_Delay , 0, 0, 0, 0, FALSE) ;
  7108. symtab [i].defnd := TRUE ;
  7109. i := NewSym ("KEYPRESSED", KBuiltin, BI_KeyPressed, 0, 0, 0, 0, FALSE) ;
  7110. symtab [i].defnd := TRUE ;
  7111. i := NewSym ("UPCASE", KBuiltin, BI_UpCase, 0, 0, 0, 0, FALSE) ;
  7112. i := NewSym ("SUCC", KBuiltin, BI_Succ, 0, 0, 0, 0, FALSE) ;
  7113. i := NewSym ("PRED", KBuiltin, BI_Pred, 0, 0, 0, 0, FALSE) ;
  7114. symtab [i].defnd := TRUE ;
  7115. (* File operations. These are the names CALC actually uses; Eof, not
  7116. EndOfFile, because CALC declares a variable called EndOfFile. *)
  7117. i := NewSym ("ASSIGN", KBuiltin, BI_Assign, 0, 0, 0, 0, FALSE) ;
  7118. symtab [i].defnd := TRUE ;
  7119. i := NewSym ("RESET", KBuiltin, BI_Reset, 0, 0, 0, 0, FALSE) ;
  7120. symtab [i].defnd := TRUE ;
  7121. i := NewSym ("REWRITE", KBuiltin, BI_Rewrite, 0, 0, 0, 0, FALSE) ;
  7122. symtab [i].defnd := TRUE ;
  7123. i := NewSym ("CLOSE", KBuiltin, BI_Close, 0, 0, 0, 0, FALSE) ;
  7124. symtab [i].defnd := TRUE ;
  7125. i := NewSym ("IORESULT", KBuiltin, BI_IoResult, 0, 0, 0, 0, FALSE) ;
  7126. symtab [i].defnd := TRUE ;
  7127. i := NewSym ("CHR", KBuiltin, BI_Chr, 0, 0, 0, 0, FALSE) ;
  7128. symtab [i].defnd := TRUE ;
  7129. i := NewSym ("STR", KBuiltin, BI_Str, 0, 0, 0, 0, FALSE) ;
  7130. symtab [i].defnd := TRUE ;
  7131. i := NewSym ("NEW", KBuiltin, BI_New, 0, 0, 0, 0, FALSE) ;
  7132. symtab [i].defnd := TRUE ;
  7133. i := NewSym ("DISPOSE", KBuiltin, BI_Dispose, 0, 0, 0, 0, FALSE) ;
  7134. symtab [i].defnd := TRUE ;
  7135. i := NewSym ("EOF", KBuiltin, BI_Eof, 0, 0, 0, 0, FALSE) ;
  7136. symtab [i].defnd := TRUE ;
  7137. i := NewSym ("VAL", KBuiltin, BI_Val, 0, 0, 0, 0, FALSE) ;
  7138. symtab [i].defnd := TRUE ;
  7139. (* KBD: the console keyboard as a TEXT file. This runtime's read entries
  7140. already read the console, so `Read(Kbd, Ch)' drops the file argument and
  7141. reads a character exactly as `Read(Ch)' does. *)
  7142. i := NewSym ("KBD", KVar, TText, FileVarSize, 1, 0, 0, FALSE) ;
  7143. symtab [i].isKbd := TRUE
  7144. END DefBuiltins ;
  7145. PROCEDURE Inittur () ;
  7146. (* reset compiler state and define the standard types *)
  7147. VAR i, rt : CARDINAL ;
  7148. BEGIN
  7149. abortFac := FALSE ;
  7150. errNum := 0 ;
  7151. txerrPos := 0 ;
  7152. srcPos := 0 ;
  7153. srcLen := Length () ;
  7154. inclActive := FALSE ;
  7155. inclEnd := 0 ;
  7156. (* The image layout is
  7157. [JMP rel16][runtime][program header][program code]
  7158. The runtime is copied to the front, which is what the original does:
  7159. TPSRC7 "copyrt" runs REPZ MOVSB with SI=DI=0 and then "MOV pc,#$2D7C".
  7160. Because the runtime sits at (near) offset 0, every address the compiler
  7161. emits is already image-absolute - the data symbols' offsets, the TU_*
  7162. call targets and the rel16 displacements all need no relocation pass.
  7163. (The base shift would in fact cancel in EmCall's arithmetic, since both
  7164. sides of a CALL move together; making the offsets absolute just means
  7165. the linker has nothing to do but copy bytes.)
  7166. The JMP is new, and it is not cosmetic. A DOS .COM is entered at
  7167. CS:0100, i.e. FILE offset 0, and for a long time offset 0 held the
  7168. runtime's first bytes - so a .COM built by this compiler started by
  7169. executing initmem with AX holding whatever the loader left in it. Every
  7170. test up to that point checked bytes and never ran the thing, so it could
  7171. not see this. The jump is the program's entry and the runtime is
  7172. ordinary data to it; keeping the runtime at the front is what preserves
  7173. the no-relocation property, so the jump goes in front of the runtime
  7174. rather than the runtime being moved behind the program.
  7175. dc is put a fixed 4 KiB above the end of the program so that data cannot
  7176. collide with code in a single 64 KiB .COM segment. LIMITATION: a
  7177. program whose code exceeds 4 KiB overruns its own data area. TP3 had
  7178. overlay segments for this; we do not, and the check belongs where the
  7179. limit is documented rather than as a silent truncation. *)
  7180. RT_Build (EntSize) ;
  7181. rt := RT_Size () ;
  7182. IF rt >= MaxCode THEN
  7183. Err (EMemOvf) ; (* cannot happen: rt is 436 *)
  7184. RETURN
  7185. END ;
  7186. (* The entry jump, at image offset 0. See the layout note above: a DOS
  7187. .COM is entered at CS:0100, which is file offset 0, so whatever sits
  7188. at offset 0 is the program's first executed instruction. *)
  7189. (* The jump's three bytes are written out longhand rather than through
  7190. Eword, because Eword writes at pc and advances it, and pc is stale at
  7191. this point -- the operand landed wherever the last compile left pc. *)
  7192. cbuf [0] := 0E9H ; (* JMP rel16 *)
  7193. entRel := 1 ;
  7194. cbuf [entRel] := 0 ;
  7195. cbuf [entRel + 1] := 0 ;
  7196. i := EntSize ;
  7197. WHILE i - EntSize < rt DO
  7198. cbuf [i] := RT_Byte (i - EntSize) ;
  7199. INC (i)
  7200. END ;
  7201. pc := rt + EntSize ;
  7202. rtSz := rt + EntSize ;
  7203. dataBase := DataStart ;
  7204. dc := dataBase ;
  7205. imgReady := TRUE ;
  7206. strTop := 0 ;
  7207. strCnt := 0 ;
  7208. rdStrX := 0 ;
  7209. varspc := 0 ;
  7210. symTop := 0 ;
  7211. (* Per-compile tables. They used to survive into the next file when the
  7212. driver compiles several in one process (the compile matrix does exactly
  7213. that): array indices kept growing, and a record's field range could be
  7214. read against a stale table. One compile must not depend on the last. *)
  7215. fldTop := 0 ;
  7216. arrTop := 0 ;
  7217. withN := 0 ;
  7218. nPatch := 0 ;
  7219. nPend := 0 ;
  7220. exitCnt := 0 ;
  7221. brkN := 0 ;
  7222. caseN := 0 ;
  7223. lexnest := 0 ;
  7224. curIsFunc := FALSE ;
  7225. resultVar := 0 ;
  7226. resultIsReal := FALSE ;
  7227. resultPtrOff := 0 ;
  7228. callScratch := 0 ;
  7229. locFree := 0FFFFH ;
  7230. locBytes := 0 ;
  7231. parmOff := 4 ;
  7232. dirs.rng := TRUE ;
  7233. dirs.chk := TRUE ;
  7234. InitKeys () ;
  7235. DropC (NewSym ("INTEGER", KType, TScalar, 2, 2, 0, 0, FALSE)) ;
  7236. DropC (NewSym ("BYTE" , KType, TScalar, 1, 1, 0, 0, FALSE)) ;
  7237. DropC (NewSym ("CHAR" , KType, TChar, 1, 1, 0, 0, FALSE)) ;
  7238. DropC (NewSym ("BOOLEAN", KType, TBool , 1, 1, 0, 0, FALSE)) ;
  7239. DropC (NewSym ("REAL" , KType, TReal , 6, 6, 0, 0, FALSE)) ;
  7240. DropC (NewSym ("STRING" , KType, TString, 256, 1, 0, 0, FALSE)) ;
  7241. DropC (NewSym ("TRUE" , KConst, TBool, 1, 1, 0, 1, FALSE)) ;
  7242. DropC (NewSym ("FALSE" , KConst, TBool, 1, 1, 0, 0, FALSE)) ;
  7243. DefBuiltins () ;
  7244. tmpA := NewSym ("@@T1", KVar, TScalar, 2, 2, dc, 0, FALSE) ;
  7245. dc := dc + 2 ;
  7246. tmpB := NewSym ("@@T2", KVar, TScalar, 2, 2, dc, 0, FALSE) ;
  7247. dc := dc + 2 ;
  7248. (* The text attribute LowVideo/NormVideo set (default 7, written by the
  7249. prologue). One byte, and its address is baked into the clear sequences. *)
  7250. crtAttrOff := dc ;
  7251. DropC (NewSym ("@@ATTR", KVar, TScalar, 1, 1, dc, 0, FALSE)) ;
  7252. dc := dc + 1 ;
  7253. (* Runtime entry offsets, derived from the blob rather than assumed. This
  7254. has to happen after RT_Build, since RT_Entry only knows where the code
  7255. landed once the blob is assembled.
  7256. RT_Entry returns an IMAGE-ABSOLUTE address, already biased by the base
  7257. RT_Build was given, so nothing here has to know where the runtime
  7258. landed. It used to return a blob-relative offset, and the bias was
  7259. applied here instead - three bytes' worth, for the entry jump. With
  7260. that line missing, every CALL landed three bytes short, in the middle of
  7261. a neighbouring runtime entry, and a CALL into the middle of wrtin's
  7262. `INT 21h' behaves perfectly plausibly: the program runs, prints nothing
  7263. and hangs. Only running it finds that. *)
  7264. IF (RT_Entry (13) = 0) OR (RT_Entry (11) = 0) OR (RT_Entry (3) = 0) THEN
  7265. (* RT_Entry returns 0 for an unknown selector. initmem sits at 0
  7266. legitimately, so it cannot appear in this test - but wrtinl, rdln
  7267. and wrint never can, so catching them is enough to catch a runtime
  7268. that failed to build or a selector that went stale. This test is on
  7269. 0 is not a usable address here, since RT_Entry returns an
  7270. image-absolute address and the base is EntSize. *)
  7271. Err (EMemOvf)
  7272. END ;
  7273. TU_InitMem := RT_Entry (0) ;
  7274. TU_ProgEnd := RT_Entry (1) ;
  7275. TU_StackChk := RT_Entry (2) ;
  7276. TU_WrInt := RT_Entry (3) ;
  7277. TU_WrChar := RT_Entry (4) ;
  7278. TU_WrBool := RT_Entry (5) ;
  7279. TU_WrReal := RT_Entry (6) ;
  7280. TU_RealWrite := RT_Entry (30) ; (* fmtreal + padding + output *)
  7281. TU_RealFmt := RT_Entry (28) ; (* fmtreal into a string variable *)
  7282. TU_WrLn := RT_Entry (7) ;
  7283. TU_RdInt := RT_Entry (8) ;
  7284. TU_RdChar := RT_Entry (9) ;
  7285. TU_RdBool := RT_Entry (10) ;
  7286. TU_RdLn := RT_Entry (11) ;
  7287. TU_Halt := RT_Entry (12) ;
  7288. TU_WrInl := RT_Entry (13) ; (* inline string literal *)
  7289. TU_RdByte := RT_Entry (14) ; (* a BYTE read stores one byte *)
  7290. TU_SetOp := RT_Entry (31) ;
  7291. TU_SetCmp := RT_Entry (32) ;
  7292. TU_SetCopy := RT_Entry (33) ;
  7293. TU_FileAssign := RT_Entry (34) ;
  7294. TU_FileReset := RT_Entry (35) ;
  7295. TU_FileRewrite := RT_Entry (36) ;
  7296. TU_FileClose := RT_Entry (37) ;
  7297. TU_IoResult := RT_Entry (38) ;
  7298. TU_IoStore := RT_Entry (39) ;
  7299. TU_HeapNew := RT_Entry (40) ;
  7300. TU_FileEof := RT_Entry (41) ;
  7301. TU_FileReadln := RT_Entry (42) ;
  7302. TU_IntFmt := RT_Entry (43) ;
  7303. TU_ValInt := RT_Entry (44) ;
  7304. TU_WrStr := RT_Entry (15) ; (* a string variable is written through
  7305. the address on the stack *)
  7306. TU_RdStr := RT_Entry (16) ; (* readln(s)/read(s): TPSRC8 prdstr,
  7307. TPSRC4 xrdstr *)
  7308. TU_StrStore := RT_Entry (17) ; (* s := t: TPSRC8 asnvar, TPSRC2 strstore *)
  7309. TU_CmpStr := RT_Entry (18) ; (* string compare: TPSRC9 vcompare,
  7310. TPSRC2 cmpstr *)
  7311. TU_StrCat := RT_Entry (19) ; (* string concatenation: TPSRC2 xconcat *)
  7312. TU_WrIntW := RT_Entry (20) ; (* width-aware integer: `I:2' *)
  7313. END Inittur ;
  7314. PROCEDURE HeadWord (VAR slot : CARDINAL) ;
  7315. BEGIN
  7316. slot := pc ;
  7317. Eword (0)
  7318. END HeadWord ;
  7319. PROCEDURE Compile (VAR errNo, errPos : CARDINAL) : BOOLEAN ;
  7320. VAR tok : CARDINAL ;
  7321. overProc : CARDINAL ;
  7322. hasProc : BOOLEAN ;
  7323. BEGIN
  7324. Inittur () ;
  7325. IF OK () THEN
  7326. (* Resolve every {$I} first: the parser (and DeclaresProc's lookahead)
  7327. must see one finished buffer. See ExpandIncludes. *)
  7328. ExpandIncludes ()
  7329. END ;
  7330. IF OK () THEN
  7331. (* program prologue: header words, CALL initmem, MOV BP,SP *)
  7332. HeadWord (hdrFlag) ;
  7333. HeadWord (hdrCS) ;
  7334. HeadWord (hdrDS) ;
  7335. HeadWord (hdrHeap) ;
  7336. HeadWord (hdrMax) ;
  7337. Eword (16) ; (* max open files *)
  7338. Eword (0) ; (* input buffer word *)
  7339. Eword (0) ; (* output buffer word *)
  7340. (* Here, and not one line earlier, is the first instruction of the
  7341. program: everything above is the header, which is DATA. The entry
  7342. jump has to land exactly here. Recorded rather than assumed, so that
  7343. a header that grows a word moves the target with it. *)
  7344. prologAt := pc ;
  7345. (* TU_InitMem takes the header offset in AX, not on the stack, so the
  7346. AX load has to precede the call. Previously the prologue called
  7347. offset 8 - which in this layout is the hdrMax word - and that was
  7348. coherent only because the runtime was not there. Now it is the
  7349. real header. *)
  7350. EmMovAxi ((rtSz + LoadBias) MOD 10000H) ;
  7351. DropC (EmCall (TU_InitMem)) ;
  7352. EmMovBpSp () ;
  7353. (* [@@ATTR] := 7 - the default text attribute, before any LowVideo or
  7354. NormVideo runs. The data area is zero-filled, and attribute 0 would
  7355. make every clear black-on-black. Through EmCrtSeq, not raw Ebyte:
  7356. Compile is not a one-line emitter, and the helper audit rightly
  7357. refuses to let a byte-emitting statement hide inside it. *)
  7358. EmCrtSeq (CrtLow) ;
  7359. IF MatchKey (tok) AND (tok = TkProgram) THEN
  7360. (* MatchKey stops right after "PROGRAM", so the optional program
  7361. name normally follows blanks. Skip them before testing for the
  7362. name: otherwise Alpha sees the blank, the name is never consumed
  7363. and IfMatchSemi reports ENoSemi at the name. *)
  7364. Skip () ;
  7365. IF Alpha (CurCh ()) THEN
  7366. GetWord ()
  7367. END ;
  7368. IF MatchDelim ('(') THEN
  7369. WHILE NOT MatchDelim (')') DO
  7370. IF Alpha (CurCh ()) THEN
  7371. GetWord ()
  7372. END ;
  7373. IF CurCh () = ',' THEN
  7374. DropCh (GetCh ())
  7375. END
  7376. END
  7377. END ;
  7378. IfMatchSemi ()
  7379. END ;
  7380. IF OK () THEN
  7381. (* Jump over the procedure bodies, if there are any. DefPart
  7382. compiles them HERE, between the prologue and the main statement
  7383. part, and there was no jump - so a program with a procedure fell
  7384. off the end of the prologue into the first procedure. See
  7385. DeclaresProc for why the condition is asked before DefPart runs
  7386. and not after. *)
  7387. hasProc := DeclaresProc () ;
  7388. IF hasProc THEN
  7389. overProc := EmJmpNear (0)
  7390. END ;
  7391. DefPart () ;
  7392. IF OK () THEN
  7393. (* A separate flag, NOT `overProc # 0`. EmJmpNear returns a
  7394. patch SLOT, and slot 0 is a perfectly ordinary slot - the
  7395. first forward jump in a program is slot 0. So a zero test
  7396. cannot tell "no forward jump" from "forward jump in slot 0",
  7397. it just skips the first patch, and the jump keeps its
  7398. placeholder target of 0. The program then jumped to image
  7399. offset 0, i.e. back to the entry jump, and ran the runtime
  7400. and the whole program again, forever. The slot is only
  7401. valid together with a boolean saying a slot was taken. *)
  7402. IF hasProc THEN
  7403. SetPatTgt (overProc, pc)
  7404. END ;
  7405. IF MatchKey (tok) AND (tok = TkBegin) THEN
  7406. Compound () ;
  7407. IF OK () THEN
  7408. EmXorAxAx () ;
  7409. DropC (EmCall (TU_ProgEnd)) ;
  7410. ResolvePatches () ;
  7411. (* Program-only sizes. The runtime is not part of the
  7412. program's code, and the fixture table has always meant
  7413. "the program's own code", so subtract it here rather
  7414. than making every expectation in expected.tsv wrong. *)
  7415. codeSz := pc - rtSz ;
  7416. dataSz := dc - dataBase ;
  7417. (* Header words. The layout is ours (the original's is
  7418. bigger and serves a real overlay loader), but
  7419. Runtime.EmitInitMem reads +4 and +8, so hdrDS and
  7420. hdrHeap must be the data base and the data end. *)
  7421. (* The entry jump's displacement. A .COM is entered at
  7422. CS:0100 = file offset 0, so the jump is the only thing
  7423. that decides where execution starts, and it has to land
  7424. on the START of the program code - the prologue, which is
  7425. at rtSz - not on pc, which is the END of it. (Patching
  7426. pc - EntSize, i.e. the end, lands one byte past the last
  7427. instruction, in the zero-filled code/data gap, where the
  7428. CPU slides through `ADD [BX+SI],AL' until it faults.)
  7429. The displacement is measured from the END of the jump,
  7430. and both addresses are image-absolute, so the load
  7431. segment cancels. *)
  7432. (* The entry jump's displacement. A .COM is entered at
  7433. CS:0100 = file offset 0, so this jump is the only thing
  7434. that decides where execution starts.
  7435. The target is prologAt - where the prologue ACTUALLY
  7436. began, recorded before the header words were emitted,
  7437. and the header is 16 bytes long, so this is rtSz + 16 and
  7438. NOT rtSz. Landing on rtSz lands on the HEADER, which is
  7439. data, and the CPU then decodes sixteen bytes of it as
  7440. instructions. That failure is spectacularly
  7441. non-deterministic across programs: 01 00 is
  7442. `ADD [BX+SI],AX' and is harmless, so writeln('hi') ran
  7443. fine by sliding through the header into the prologue,
  7444. while t07's hdrHeap word 90 12 decodes as a LOCK-prefixed
  7445. ADD whose displacement crosses a page and faults, and the
  7446. program hung with no output at all. Both looked like
  7447. "the jump is in the right area". Recording the position
  7448. rather than assuming it means a future header that grows
  7449. a word cannot silently reintroduce this. *)
  7450. PatchWord (entRel, (prologAt - EntSize) MOD 10000H) ;
  7451. PatchWord (hdrFlag, 1) ;
  7452. (* Every OFFSET field in the header is a segment offset,
  7453. i.e. an image offset plus LoadBias - one convention for
  7454. the whole structure, so that nobody has to remember
  7455. which of these five words is numbered which way.
  7456. hdrFlag 1 set, so a loader can recognise the header
  7457. hdrCS end of the generated code
  7458. hdrDS first byte of the data area <- read by initmem
  7459. hdrHeap one past the last <- read by initmem
  7460. hdrMax 0 (no overlay loader yet)
  7461. hdrDS and hdrHeap are the two that are CONSUMED, and
  7462. omitting the bias there is a silent no-op: initmem would
  7463. clear a range starting 0100h below the data, off the
  7464. front of the image, and never reach the globals at the
  7465. end. Nothing crashes, and the globals keep whatever the
  7466. loader left in them. *)
  7467. PatchWord (hdrCS, pc + LoadBias) ;
  7468. PatchWord (hdrDS, dataBase + LoadBias) ;
  7469. PatchWord (hdrHeap, dc + LoadBias) ;
  7470. PatchWord (hdrMax, 0)
  7471. END
  7472. ELSE
  7473. Err (EUnknown)
  7474. END
  7475. END
  7476. END
  7477. END ;
  7478. IF NOT MatchDelim ('.') THEN
  7479. Err (EPointExp)
  7480. END ;
  7481. IF abortFac THEN
  7482. errNo := errNum ; (* was "errNo := errNo": a self-assignment,
  7483. because the formal shadowed the module
  7484. variable, so the error code always
  7485. reached the caller as 0 *)
  7486. errPos := txerrPos ;
  7487. RETURN FALSE
  7488. END ;
  7489. errNo := 0 ;
  7490. errPos := 0 ;
  7491. RETURN TRUE
  7492. END Compile ;
  7493. PROCEDURE CodeBytes () : CARDINAL ;
  7494. BEGIN
  7495. RETURN codeSz
  7496. END CodeBytes ;
  7497. PROCEDURE DataBytes () : CARDINAL ;
  7498. BEGIN
  7499. RETURN dataSz
  7500. END DataBytes ;
  7501. PROCEDURE ImageBytes () : CARDINAL ;
  7502. (* Total linked image size: rtSz (the runtime) + CodeBytes (the program).
  7503. The data is NOT part of this image: globals live at DataBase (8000h image)
  7504. and initmem zeroes them. The .COM is exactly these bytes. *)
  7505. BEGIN
  7506. RETURN rtSz + codeSz
  7507. END ImageBytes ;
  7508. PROCEDURE DataBase () : CARDINAL ;
  7509. (* image-absolute offset at which the data area begins (DataStart = 8000H).
  7510. The data is outside the .COM; initmem zeroes [dataBase, dataBase+dataSz)
  7511. through the header's hdrDS/hdrHeap words. *)
  7512. BEGIN
  7513. RETURN dataBase
  7514. END DataBase ;
  7515. PROCEDURE ImageByteAt (i : CARDINAL) : BYTE ;
  7516. (* i-th byte of the WHOLE image, runtime included, so a test can check the
  7517. real thing a .COM would contain. Returns 0 past the end. *)
  7518. BEGIN
  7519. IF i >= rtSz + codeSz THEN
  7520. RETURN 0
  7521. END ;
  7522. RETURN cbuf [i]
  7523. END ImageByteAt ;
  7524. PROCEDURE CodeByteAt (i : CARDINAL) : BYTE ;
  7525. (* i-th byte of the emitted image, for test harnesses that need to check
  7526. the generated 8086 code rather than just its size. Returns 0 past the
  7527. end of the image. *)
  7528. BEGIN
  7529. IF i >= codeSz THEN
  7530. RETURN 0
  7531. END ;
  7532. RETURN cbuf [i]
  7533. END CodeByteAt ;
  7534. END Compiler.