Compiler.mod 364 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881
  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 = 63 ;
  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 = 0A000H ;
  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. DataStart is now 0xA000h so the large CALC
  72. image fits; the current boot harness still puts its input descriptor at
  73. memory 7000h, so a .COM above that is compile-only until the loader is
  74. moved. *)
  75. DataStart = 0A000H ; (* image offset of the first global (memory 0A100h) *)
  76. CodeMax = 0A000H ; (* code must end below the 0A000h data base *)
  77. MaxSym = 3000 ;
  78. MaxPatch = 2000 ;
  79. MaxPend = 400 ;
  80. MaxFields = 2000 ; (* record fields, in a table of their own: a
  81. field is scoped to its record, not global *)
  82. MaxArrs = 1000 ; (* array SHAPES, likewise: a symbol points at
  83. one, and 2-D indexing needs both ranges *)
  84. FileVarSize = 132 ; (* a file variable is our control block: the DOS
  85. handle (word), a mode word (0 closed, 1 read,
  86. 2 write), then a 128-byte NUL-terminated
  87. filename. TP3 keeps its own name buffer and
  88. a 128-byte data buffer here; we keep the name
  89. and do unbuffered INT 21h I/O, with the data
  90. buffer supplied by the caller. *)
  91. (* type codes (TP3 vartp) *)
  92. TNone = 0 ; TArray = 1 ; TRecord = 2 ; TSet = 3 ;
  93. TPtr = 4 ; TFile = 5 ; TText = 6 ; TUntyp = 7 ;
  94. TString = 8 ; TReal = 9 ; TScalar = 10 ; TBool = 11 ;
  95. (* CHAR needs a class of its own. It used to be registered as TScalar,
  96. which made a CHAR variable indistinguishable from an INTEGER one: the
  97. class is all IoCall has to dispatch on, so `write(c)` called wrint (the
  98. value 65 became the *address* 65 and it printed whatever lived at 0x41)
  99. and `readln(c)` called rdint (which stores a 16-bit result, so it wrote
  100. two bytes into a one-byte variable). TP3 TPSRC8 prdtyped/pwriteln
  101. dispatches on the type identifier for exactly this reason. BYTE stays
  102. TScalar: a BYTE is written as an integer, as in TP3 - but a READ of one
  103. is dispatched on the variable's size instead (TPSRC8 prdnum
  104. `CMP varsize,#$01 / JA -> rdint, else rdbyte'), so the one thing class
  105. alone cannot tell apart is told apart where the size is in hand. *)
  106. TChar = 12 ;
  107. TEnum = 13 ; (* an enumeration type: ordinal, size 1 (TP3 uses 1
  108. byte below 256 values), bounds in SymEntry.lo/hi *)
  109. (* symbol kinds *)
  110. KLabel = 100H ; KConst = 200H ; KType = 300H ;
  111. KVar = 400H ; KProc = 500H ; KFunc = 600H ;
  112. KBuiltin = 700H ; (* standard procedure, see BI_* below *)
  113. (* which standard procedure a KBuiltin symbol denotes *)
  114. BI_Write = 0 ; BI_WriteLn = 1 ; BI_Read = 2 ;
  115. BI_ReadLn = 3 ; BI_Halt = 4 ; BI_Length = 5 ;
  116. (* CRT identifiers. There is no unit system, so these are builtins. They
  117. are statements (except Length), dispatched in IoCall; each one that is
  118. not implemented yet is a loud ENoLib, never a silent no-op. *)
  119. BI_ClrScr = 6 ; BI_GotoXY = 7 ; BI_LowVideo = 8 ;
  120. BI_NormVideo = 9 ; BI_ClrEol = 10 ; BI_Delay = 11 ;
  121. BI_KeyPressed = 12 ; BI_UpCase = 13 ; BI_Succ = 14 ; BI_Pred = 15 ;
  122. (* File operations. TP3 has these as standard procedures/functions; our
  123. control block is Compiler.FileVarSize, so Assign stores the name for
  124. the later Reset/Rewrite. IOResult is an expression; EndOfFile is the
  125. next file increment. *)
  126. BI_Assign = 16 ; BI_Reset = 17 ; BI_Rewrite = 18 ;
  127. BI_Close = 19 ; BI_IoResult = 20 ; BI_Chr = 21 ; BI_Str = 22 ;
  128. BI_New = 23 ; BI_Dispose = 24 ; BI_Eof = 25 ; BI_Val = 26 ;
  129. BI_Copy = 27 ; BI_Abs = 28 ; BI_Sqrt = 29 ; BI_Sqr = 30 ;
  130. BI_Ln = 31 ; BI_Exp = 32 ; BI_Sin = 33 ; BI_Cos = 34 ; BI_Arctan = 35 ;
  131. BI_Int = 36 ; BI_Delete = 37 ; BI_Insert = 38 ; BI_Ord = 39 ;
  132. BI_Round = 40 ; BI_Trunc = 41 ; BI_Msdos = 42 ; BI_Intr = 43 ;
  133. BI_GetDir = 44 ; BI_Seg = 45 ; BI_Ofs = 46 ;
  134. (* the sub-codes EmCrtSeq takes *)
  135. CrtClr = 0 ; CrtDl = 1 ; CrtDh = 2 ; CrtCaret = 3 ; CrtEol = 4 ;
  136. CrtLow = 5 ; CrtNorm = 6 ; CrtDelay = 7 ; CrtKey = 8 ;
  137. (* keyword tokens *)
  138. TkNone = 0 ; TkProgram = 1 ; TkBegin = 2 ; TkEnd = 3 ;
  139. TkIf = 4 ; TkThen = 5 ; TkElse = 6 ; TkWhile = 7 ;
  140. TkDo = 8 ; TkRepeat = 9 ; TkUntil = 10 ; TkFor = 11 ;
  141. TkTo = 12 ; TkDownto = 13 ; TkCase = 14 ; TkOf = 15 ;
  142. TkGoto = 16 ; TkExit = 17 ; TkWith = 18 ; TkVar = 19 ;
  143. TkConst= 20 ; TkType = 21 ; TkLabel = 22 ; TkProcedure = 23 ;
  144. TkFunction = 24 ; TkNil = 25 ; TkAnd = 26 ; TkOr = 27 ;
  145. TkNot = 28 ; TkDiv = 29 ; TkMod = 30 ; TkIn = 31 ;
  146. TkFile = 32 ; TkText = 33 ; TkRecord = 34 ; TkArray = 35 ;
  147. TkSet = 36 ; TkPacked = 37 ; TkForward = 38 ; TkExternal = 39 ;
  148. TkAbsolute = 40 ; TkOverlay = 41 ; TkString = 42 ;
  149. (* Codes for the SYMBOL operators, as BinOpEmit numbers them. The word
  150. operators carry their own Tk* token and need no code here.
  151. These are named, not bare numbers, because every precedence level's
  152. parser picks its own code out of the same set and BinOpEmit cannot see
  153. which level called it. ParseAdd chose 1 for '+' and ParseMul chose 1
  154. for '*', so every multiplication dispatched to EmAddAxCx and a * b
  155. compiled to a + b. Only the constant-folding path was right, which is
  156. why n * n with n a CONST was correct and a * b with a a variable was
  157. not. *)
  158. OpAdd = 1 ; OpSub = 2 ; OpMul = 3 ; OpDiv = 4 ;
  159. OpShl = 5 ; OpShr = 6 ;
  160. (* TP3 error numbers *)
  161. ENoSemi = 1 ; EPointExp = 10 ; ESimpType = 30 ;
  162. EUnknown = 41 ; EConstRange = 45 ; EMemOvf = 98 ;
  163. ECompOvf = 99 ; ENoLib = 102 ; ETypeErr = 56 ;
  164. EIoHere = 66 ; (* I/O not allowed here - TPSRC8 prderrtp *)
  165. EFileNF = 90 ; (* File not found - TPSRC10 cdincl, errb *)
  166. EInclNest = 96 ; (* Illegal nesting of include files - TPSRC10 cdincl *)
  167. AposC = 39 ; (* ORD ("'") - avoids quote-in-quote *)
  168. (* ---------------------------------------------------------------- *)
  169. (* types *)
  170. (* ---------------------------------------------------------------- *)
  171. TYPE
  172. SymEntry =
  173. RECORD
  174. name : ARRAY [0..MaxName] OF CHAR ;
  175. tag : CARDINAL ;
  176. cls : CARDINAL ;
  177. size : CARDINAL ;
  178. elem : CARDINAL ;
  179. off : CARDINAL ;
  180. lval : LONGINT ;
  181. lo, hi : LONGINT ; (* an ORDINAL type's bounds: an enumeration
  182. (0..n-1) or a subrange. Needed so
  183. `set of T' can size its base. *)
  184. fldFirst, fldLast : CARDINAL ; (* a RECORD type's field range in the
  185. field table (0,0 when not a record) *)
  186. arrIdx : CARDINAL ; (* an ARRAY type's shape in ArrRec (0 = no) *)
  187. parmFirst, parmCount : CARDINAL ; (* a PROCEDURE/FUNCTION's parameters:
  188. symtab indices, in declaration order *)
  189. isKbd : BOOLEAN ; (* the predefined KBD file: reads are console *)
  190. isVar : BOOLEAN ; (* a VAR parameter: the slot holds an address *)
  191. level : CARDINAL ;
  192. local : BOOLEAN ;
  193. parent : CARDINAL ; (* lexical parent procedure, 0 at top level *)
  194. staticOff : CARDINAL ; (* nested proc's hidden static-link local *)
  195. resvar : CARDINAL ;
  196. goPos : CARDINAL ;
  197. defnd : BOOLEAN ;
  198. fwd : BOOLEAN ;
  199. END ;
  200. PatchRec =
  201. RECORD
  202. place : CARDINAL ;
  203. target : CARDINAL ;
  204. filled : BOOLEAN ;
  205. END ;
  206. PendRec =
  207. RECORD
  208. kind : CARDINAL ; (* 0 goto, 1 call *)
  209. who : CARDINAL ;
  210. place : CARDINAL ; (* patch slot index *)
  211. END ;
  212. ERes =
  213. RECORD
  214. cls : CARDINAL ;
  215. kind : CARDINAL ; (* 0 const, 1 var, 2 value in AX,
  216. 3 string literal (cls = TString),
  217. 4 value on the stack (SaveLeft),
  218. 5 string element, address in DI,
  219. 7 set value (cls = TSet) *)
  220. imm : LONGINT ;
  221. idx : CARDINAL ;
  222. boff : CARDINAL ; (* constant fold-in for subscripts *)
  223. chr : BOOLEAN ; (* single-quoted literal, e.g. 'a' *)
  224. strx : CARDINAL ; (* string literal: index into strPool *)
  225. setoff : CARDINAL ; (* SET value: image offset of the bytes *)
  226. setbase : CARDINAL ; (* ... the first byte's first element,
  227. a multiple of 8 (TP3's hybrid origin) *)
  228. setsize : CARDINAL ; (* ... and how many bytes there are *)
  229. sz : CARDINAL ; (* width of a kind-1 value: an element or
  230. field's size, not its variable's *)
  231. fldFirst, fldLast : CARDINAL ; (* current record metadata *)
  232. arrIdx : CARDINAL ; (* current array shape, if any *)
  233. ptrElem : CARDINAL ; (* pointee type, for pointer arrays *)
  234. dynElem : CARDINAL ; (* element symbol for a dynamic set literal *)
  235. END ;
  236. DirRec = RECORD rng, chk : BOOLEAN END ;
  237. FldRec =
  238. RECORD
  239. name : ARRAY [0..MaxName] OF CHAR ;
  240. cls : CARDINAL ;
  241. size : CARDINAL ;
  242. elem : CARDINAL ;
  243. off : CARDINAL ; (* byte offset within the record *)
  244. lo, hi : LONGINT ; (* bounds, for a subrange/enum field *)
  245. fldFirst, fldLast : CARDINAL ; (* nested record metadata *)
  246. arrIdx : CARDINAL ;
  247. ptrElem : CARDINAL ;
  248. END ;
  249. ArrRec =
  250. RECORD
  251. n : CARDINAL ; (* number of ranges (1 for array[1..5]) *)
  252. lo, hi : ARRAY [0..3] OF LONGINT ;
  253. elem : CARDINAL ; (* element size in bytes *)
  254. ecls : CARDINAL ; (* element CLASS, for a subscripted value *)
  255. fldFirst, fldLast : CARDINAL ; (* leaf record metadata *)
  256. elemType : CARDINAL ;
  257. fieldPtrElem : CARDINAL ;
  258. variantBase, armRunning, armMax, savePosVar : CARDINAL ;
  259. moreFields : BOOLEAN ;
  260. tagName : ARRAY [0..MaxName] OF CHAR ; (* pointer target / named element type *)
  261. END ;
  262. (* ---------------------------------------------------------------- *)
  263. (* state *)
  264. (* ---------------------------------------------------------------- *)
  265. VAR
  266. srcPos, srcLen : CARDINAL ;
  267. (* Include state. srcName is the path the buffer was loaded from, set by
  268. SetSource; only its directory is used, because a {$I file} directive
  269. names the file relative to the INCLUDING file. Includes are expanded by
  270. ExpandIncludes BEFORE parsing, so inclEnd is the end of the include
  271. region currently being scanned and inclActive says one is open - a
  272. nested {$I} inside that region is error 96, since TP3 permits only one
  273. level. incBuf holds one whole include file while it is spliced. *)
  274. srcName : ARRAY [0..255] OF CHAR ; inclName : ARRAY [0..127] OF CHAR ;
  275. inclPath : ARRAY [0..383] OF CHAR ;
  276. inclEnd : CARDINAL ;
  277. inclActive : BOOLEAN ;
  278. incBuf : ARRAY [0..TextLimit] OF CHAR ;
  279. enumLo, enumHi : LONGINT ; (* bounds of the enumeration ParseType just read,
  280. handed to DefType which owns the type NAME *)
  281. fields : ARRAY [0..MaxFields - 1] OF FldRec ;
  282. fldTop : CARDINAL ; (* next free field-table slot *)
  283. recFldFirst, recFldLast : CARDINAL ; (* field range ParseType just built *)
  284. arrs : ARRAY [0..MaxArrs - 1] OF ArrRec ;
  285. arrTop : CARDINAL ; (* next free array-shape slot *)
  286. newArrIdx : CARDINAL ; (* ArrRec index+1 for the type just parsed *)
  287. constBlob : ARRAY [0..511] OF CHAR ; (* bytes of a set/array typed constant
  288. while it is being built *)
  289. wrd : ARRAY [0..MaxName] OF CHAR ;
  290. (* String-literal pool.
  291. TP3 does not put a literal in the data segment at all: it emits the
  292. literal *inline in the code stream* as <length byte><characters>, and
  293. the runtime entry "wrtinl" reads the length from the return address and
  294. returns to just past the last character (TPSRC4 xwrtinl, TPSRC10
  295. estring). So nothing here ends up in the image as data - the pool only
  296. has to survive from the moment the literal is scanned until IoCall
  297. decides to emit it, because by then the parser has moved on. *)
  298. strPool : ARRAY [0..4095] OF CHAR ;
  299. strOff : ARRAY [0..255] OF CARDINAL ;
  300. strLen : ARRAY [0..255] OF CARDINAL ;
  301. strTop : CARDINAL ; (* next free byte in strPool *)
  302. strCnt : CARDINAL ; (* literals collected so far *)
  303. rdStrX : CARDINAL ; (* pool index of the literal RdConst just read *)
  304. symtab : ARRAY [0..MaxSym - 1] OF SymEntry ;
  305. symTop : CARDINAL ;
  306. patches : ARRAY [0..MaxPatch - 1] OF PatchRec ;
  307. nPatch : CARDINAL ;
  308. pend : ARRAY [0..MaxPend - 1] OF PendRec ;
  309. nPend : CARDINAL ;
  310. exitPatch : ARRAY [0..63] OF CARDINAL ;
  311. exitCnt : CARDINAL ;
  312. brkSave : ARRAY [0..15] OF CARDINAL ;
  313. loopTy : ARRAY [0..15] OF CARDINAL ; (* 1 while/repeat, 2 for *)
  314. brkN : CARDINAL ;
  315. caseJmp : ARRAY [0..63] OF CARDINAL ;
  316. caseN : CARDINAL ;
  317. (* the active WITH scopes, innermost last. A DI-based base (an array
  318. element addressed at run time) is parked in a two-byte data slot: the
  319. body may clobber DI, so each field access reloads it. *)
  320. withN : CARDINAL ;
  321. withIsDI : ARRAY [0..3] OF BOOLEAN ;
  322. withIdx : ARRAY [0..3] OF CARDINAL ;
  323. withBoff : ARRAY [0..3] OF CARDINAL ;
  324. withSlot : ARRAY [0..3] OF CARDINAL ;
  325. withFF : ARRAY [0..3] OF CARDINAL ;
  326. withFL : ARRAY [0..3] OF CARDINAL ;
  327. lblLo, lblHi : ARRAY [0..31] OF LONGINT ; (* one arm's label list *)
  328. lblJmp : ARRAY [0..63] OF CARDINAL ; (* its body jumps: 1 or 2 per label *)
  329. pc, dc : CARDINAL ;
  330. varspc : CARDINAL ;
  331. cbuf : ARRAY [0..MaxCode - 1] OF BYTE ;
  332. codeSz, dataSz : CARDINAL ;
  333. (* Image layout, all image-absolute. rtSz is where the runtime ends and
  334. the program header begins; dataBase is the fixed high global area
  335. (DataStart = 0A000H image, 0A100H memory), which is NOT part of the .COM
  336. image: initmem zeroes it at startup, as the original's separate DS
  337. segment is zeroed. codeSz and dataSz are PROGRAM sizes - the runtime is
  338. excluded. *)
  339. rtSz, dataBase : CARDINAL ;
  340. (* TRUE once Compile has computed rtSz/dataBase, i.e. once the code window
  341. has been measured. Ebyte refuses to write past CodeMax/MaxCode and
  342. needs to know the window is real before it can: imgReady is FALSE until
  343. then, and a zero CodeMax would abort on the very first byte. *)
  344. imgReady : BOOLEAN ;
  345. (* Image offset of the entry jump's rel16 operand, patched at the end of
  346. Compile. The jump is at image offset 0, so its displacement is simply
  347. the program code's end - 3. *)
  348. entRel, prologAt : CARDINAL ;
  349. (* Runtime entry offsets as IMAGE-ABSOLUTE addresses, which is what
  350. EmCall and EmJmp want. They are derived from Runtime.RT_Entry in
  351. Inittur (after RT_Build, since RT_Entry only knows where the code
  352. landed once the blob is assembled) rather than written down, so a moved
  353. entry cannot leave the compiler calling the old address. Not a CONST
  354. block because RT_Entry is a function.
  355. Standard-procedure entries: TP3 does NOT pass a descriptor -
  356. TPSRC8 pwriteln/pwrloop inspects each argument's class in CL and emits
  357. a *different* call per type, so the type is fixed at compile time and
  358. the runtime needs only the value. Mirrored here. *)
  359. TU_InitMem : CARDINAL ;
  360. TU_ProgEnd : CARDINAL ;
  361. TU_StackChk : CARDINAL ;
  362. TU_WrInt : CARDINAL ; TU_WrIntW : CARDINAL ;
  363. TU_WrChar : CARDINAL ; TU_WrBool : CARDINAL ;
  364. TU_WrCharW : CARDINAL ; TU_WrBoolW : CARDINAL ; TU_WrStrW : CARDINAL ;
  365. TU_MemCopy : CARDINAL ;
  366. TU_RealToInt : CARDINAL ;
  367. TU_Msdos : CARDINAL ;
  368. TU_GetDir : CARDINAL ;
  369. TU_WrReal : CARDINAL ; TU_RealWrite : CARDINAL ; TU_RealFmt : CARDINAL ;
  370. TU_RealVal : CARDINAL ; TU_RealCmp : CARDINAL ;
  371. TU_RealAdd : CARDINAL ; TU_RealSub : CARDINAL ;
  372. TU_RealMul : CARDINAL ; TU_RealDiv : CARDINAL ;
  373. TU_RealFromInt : CARDINAL ;
  374. TU_WrLn : CARDINAL ;
  375. TU_RdInt : CARDINAL ; TU_RdChar : CARDINAL ; TU_RdBool : CARDINAL ;
  376. TU_RdLn : CARDINAL ; TU_Halt : CARDINAL ;
  377. TU_RdByte : CARDINAL ; (* one-byte variable reads; see IoCall *)
  378. TU_WrInl : CARDINAL ; (* inline string literal; NO stack argument *)
  379. TU_WrStr : CARDINAL ; (* string variable; address is the stack argument *)
  380. TU_RdStr : CARDINAL ; (* readln(s)/read(s); address on the stack, limit
  381. in CX *)
  382. TU_StrStore : CARDINAL ; (* s := t; dest and source addresses on the stack,
  383. limit in CX *)
  384. TU_SetOp : CARDINAL ; (* runtime set operations, entries 31..33 *)
  385. TU_SetCmp : CARDINAL ;
  386. TU_SetCopy : CARDINAL ;
  387. TU_SetSingle : CARDINAL ;
  388. TU_FileAssign : CARDINAL ; (* file control block operations, entries 34..38 *)
  389. TU_FileReset : CARDINAL ;
  390. TU_FileRewrite : CARDINAL ;
  391. TU_FileClose : CARDINAL ;
  392. TU_IoResult : CARDINAL ;
  393. TU_IoStore : CARDINAL ; (* save AX as the last I/O status *)
  394. TU_HeapNew : CARDINAL ; (* bump allocation for New *)
  395. TU_FileEof : CARDINAL ; (* text-file end of file *)
  396. TU_FileReadln : CARDINAL ; (* text-file line read *)
  397. TU_FileRdtok : CARDINAL ; (* text-file token read *)
  398. TU_FileRdln : CARDINAL ; (* discard the rest of a text-file line *)
  399. TU_IntFmt : CARDINAL ; (* integer -> counted string *)
  400. TU_IntFmtW : CARDINAL ; (* integer -> counted string, width-padded *)
  401. TU_StrFmtW : CARDINAL ; (* string -> fixed-width counted string *)
  402. TU_ValInt : CARDINAL ; (* counted string -> integer *)
  403. TU_StrCopy : CARDINAL ; (* Copy(S,start,len) *)
  404. TU_ValReal : CARDINAL ; (* counted string -> REAL *)
  405. TU_RealAbs : CARDINAL ; TU_RealSqrt : CARDINAL ;
  406. TU_RealLn : CARDINAL ; TU_RealExp : CARDINAL ;
  407. TU_RealSin : CARDINAL ; TU_RealCos : CARDINAL ; TU_RealArctan : CARDINAL ;
  408. TU_RealIntFunc : CARDINAL ; TU_StrDelete : CARDINAL ;
  409. TU_StrInsert : CARDINAL ;
  410. TU_CmpStr : CARDINAL ; (* string comparison; two addresses on the stack,
  411. signed -1/0/+1 in AX *)
  412. TU_StrCat : CARDINAL ; (* string concatenation; dest and source addresses
  413. on the stack, limit in CX; exit code 16 if the
  414. result would be too long *)
  415. abortFac : BOOLEAN ;
  416. errNum : CARDINAL ; (* NOT "errNo": Compile's formal of that
  417. name would shadow it, and the caller's
  418. errNo would never be filled in *)
  419. txerrPos : CARDINAL ;
  420. lexnest : CARDINAL ;
  421. curProc : CARDINAL ; (* symbol of the procedure body being compiled *)
  422. curStaticOff : CARDINAL ; (* its hidden static-link slot, 0 if none *)
  423. curIsFunc : BOOLEAN ;
  424. resultVar : CARDINAL ;
  425. resultIsReal : BOOLEAN ; (* the CURRENT function returns TReal *)
  426. resultPtrOff : CARDINAL ; (* hidden local: caller's 6-byte result dest *)
  427. callScratch : CARDINAL ; (* per-site real-result scratch symbol *)
  428. locFree : CARDINAL ; (* next local slot (BP-relative, 8-bit) *)
  429. locBytes : CARDINAL ; (* frame size for SUB SP *)
  430. parmOff : CARDINAL ; (* provisional parameter slot, in declaration
  431. order; remapped to TP3's BP+4 rule when the
  432. list has been read (see ProcFunc) *)
  433. dirs : DirRec ;
  434. tmpA, tmpB : CARDINAL ; (* global scratch word addresses *)
  435. crtAttrOff : CARDINAL ; (* image offset of the text attribute byte that
  436. LowVideo/NormVideo set and ClrScr/ClrEol use *)
  437. hdrFlag, hdrCS, hdrDS, hdrHeap, hdrMax : CARDINAL ;
  438. kName : ARRAY [0..42] OF ARRAY [0..15] OF CHAR ;
  439. kTk : ARRAY [0..42] OF CARDINAL ;
  440. (* ---------------------------------------------------------------- *)
  441. (* small char helpers *)
  442. (* ---------------------------------------------------------------- *)
  443. PROCEDURE CurCh () : CHAR ;
  444. BEGIN
  445. IF srcPos >= srcLen THEN
  446. RETURN 0C
  447. END ;
  448. RETURN CharAt (srcPos)
  449. END CurCh ;
  450. PROCEDURE GetCh () : CHAR ;
  451. VAR ch : CHAR ;
  452. BEGIN
  453. ch := CurCh () ;
  454. IF srcPos < srcLen THEN
  455. INC (srcPos)
  456. END ;
  457. RETURN ch
  458. END GetCh ;
  459. PROCEDURE PeekAhead (k : CARDINAL) : CHAR ;
  460. BEGIN
  461. IF srcPos + k >= srcLen THEN
  462. RETURN 0C
  463. END ;
  464. RETURN CharAt (srcPos + k)
  465. END PeekAhead ;
  466. PROCEDURE Digit (ch : CHAR) : BOOLEAN ;
  467. BEGIN
  468. RETURN (ch >= '0') AND (ch <= '9')
  469. END Digit ;
  470. PROCEDURE Alpha (ch : CHAR) : BOOLEAN ;
  471. BEGIN
  472. RETURN ((ch >= 'A') AND (ch <= 'Z')) OR ((ch >= 'a') AND (ch <= 'z'))
  473. OR (ch = '_')
  474. END Alpha ;
  475. PROCEDURE AlphaNum (ch : CHAR) : BOOLEAN ;
  476. BEGIN
  477. RETURN (Alpha (ch)) OR (Digit (ch))
  478. END AlphaNum ;
  479. PROCEDURE IsHexCh (ch : CHAR) : BOOLEAN ;
  480. BEGIN
  481. RETURN (Digit (ch)) OR ((ch >= 'A') AND (ch <= 'F'))
  482. OR ((ch >= 'a') AND (ch <= 'f'))
  483. END IsHexCh ;
  484. PROCEDURE Upper (ch : CHAR) : CHAR ;
  485. BEGIN
  486. IF (ch >= 'a') AND (ch <= 'z') THEN
  487. RETURN CHR (ORD (ch) - ORD ('a') + ORD ('A'))
  488. END ;
  489. RETURN ch
  490. END Upper ;
  491. PROCEDURE W16 (x : LONGINT) : CARDINAL ;
  492. (* fold x modulo 10000H, handling negatives (no negative MOD) *)
  493. VAR m : CARDINAL ;
  494. BEGIN
  495. IF x >= 0 THEN
  496. RETURN VAL (CARDINAL, x MOD 10000H)
  497. END ;
  498. m := VAL (CARDINAL, (0 - x) MOD 10000H) ;
  499. RETURN (10000H - m) MOD 10000H
  500. END W16 ;
  501. PROCEDURE DropCh (v : CHAR) ;
  502. BEGIN
  503. END DropCh ;
  504. PROCEDURE DropB (v : BOOLEAN) ;
  505. BEGIN
  506. END DropB ;
  507. PROCEDURE DropC (v : CARDINAL) ;
  508. BEGIN
  509. END DropC ;
  510. PROCEDURE BitAnd (a, b : LONGINT) : LONGINT ;
  511. BEGIN
  512. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, W16 (a))
  513. * VAL (BITSET, W16 (b))))
  514. END BitAnd ;
  515. PROCEDURE BitOr (a, b : LONGINT) : LONGINT ;
  516. BEGIN
  517. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, W16 (a))
  518. + VAL (BITSET, W16 (b))))
  519. END BitOr ;
  520. PROCEDURE BitNot (a : LONGINT) : LONGINT ;
  521. BEGIN
  522. RETURN VAL (LONGINT, CARDINAL (VAL (BITSET, 0FFFFH)
  523. - VAL (BITSET, W16 (a))))
  524. END BitNot ;
  525. (* ---------------------------------------------------------------- *)
  526. (* errors *)
  527. (* ---------------------------------------------------------------- *)
  528. PROCEDURE Err (n : CARDINAL) ;
  529. BEGIN
  530. IF NOT abortFac THEN
  531. abortFac := TRUE ;
  532. errNum := n ;
  533. txerrPos := srcPos
  534. END
  535. END Err ;
  536. PROCEDURE OK () : BOOLEAN ;
  537. BEGIN
  538. RETURN NOT abortFac
  539. END OK ;
  540. (* ---------------------------------------------------------------- *)
  541. (* {$I file} include *)
  542. (* ---------------------------------------------------------------- *)
  543. PROCEDURE SetSource (path : ARRAY OF CHAR) ;
  544. (* remember the path the buffer was loaded from; MakeIncludePath keeps only its
  545. directory *)
  546. VAR i : CARDINAL ;
  547. BEGIN
  548. i := 0 ;
  549. WHILE (i < 255) AND (i <= HIGH (path)) AND (path [i] # 0C) DO
  550. srcName [i] := path [i] ;
  551. INC (i)
  552. END ;
  553. srcName [i] := 0C
  554. END SetSource ;
  555. PROCEDURE ReadWholeFile (path : ARRAY OF CHAR ; VAR n : CARDINAL) : BOOLEAN ;
  556. (* Read path into incBuf with the same CR/LF normalisation as LoadWorkFile,
  557. and report its length in n. FALSE if it could not be opened. ^Z ends the
  558. text, as it does for every other reader here. *)
  559. VAR fd : INTEGER ;
  560. k : LONGINT ;
  561. b : BYTE ;
  562. ch : CHAR ;
  563. prevCR, emit : BOOLEAN ;
  564. BEGIN
  565. n := 0 ;
  566. fd := open (ADR (path), 0, 0) ;
  567. IF fd < 0 THEN
  568. RETURN FALSE
  569. END ;
  570. prevCR := FALSE ;
  571. LOOP
  572. k := read (fd, ADR (b), 1) ;
  573. IF k # 1 THEN EXIT END ;
  574. IF b = 26 THEN EXIT END ; (* ^Z *)
  575. emit := TRUE ;
  576. IF b = 10 THEN
  577. IF prevCR THEN
  578. emit := FALSE
  579. ELSE
  580. ch := CHR (13) (* lone LF -> CR *)
  581. END ;
  582. prevCR := FALSE
  583. ELSIF b = 13 THEN
  584. ch := CHR (13) ;
  585. prevCR := TRUE
  586. ELSE
  587. ch := CHR (ORD (b)) ;
  588. prevCR := FALSE
  589. END ;
  590. IF emit AND (n <= TextLimit) THEN
  591. incBuf [n] := ch ;
  592. INC (n)
  593. END
  594. END ;
  595. k := close (fd) ;
  596. RETURN TRUE
  597. END ReadWholeFile ;
  598. PROCEDURE MakeIncludePath () ;
  599. (* inclPath := directory (srcName) + inclName, defaulting the extension to
  600. .PAS when the name has none (TPSRC10 kpasext). *)
  601. VAR i, j, last : CARDINAL ; dot : BOOLEAN ;
  602. BEGIN
  603. dot := FALSE ;
  604. i := 0 ;
  605. WHILE (i < 127) AND (inclName [i] # 0C) DO
  606. IF inclName [i] = '.' THEN dot := TRUE END ;
  607. INC (i)
  608. END ;
  609. IF (NOT dot) AND (i <= 123) THEN
  610. inclName [i] := '.' ;
  611. inclName [i + 1] := 'P' ;
  612. inclName [i + 2] := 'A' ;
  613. inclName [i + 3] := 'S' ;
  614. inclName [i + 4] := 0C
  615. END ;
  616. last := 0 ;
  617. i := 0 ;
  618. WHILE (i < 256) AND (srcName [i] # 0C) DO
  619. IF srcName [i] = '/' THEN last := i + 1 END ;
  620. INC (i)
  621. END ;
  622. j := 0 ;
  623. WHILE j < last DO
  624. inclPath [j] := srcName [j] ;
  625. INC (j)
  626. END ;
  627. i := 0 ;
  628. WHILE (i < 127) AND (inclName [i] # 0C) DO
  629. inclPath [j] := inclName [i] ;
  630. INC (j) ;
  631. INC (i)
  632. END ;
  633. inclPath [j] := 0C
  634. END MakeIncludePath ;
  635. PROCEDURE ExpandIncludes () ;
  636. (* Replace every {$I name} directive in the buffer with the named file's text,
  637. BEFORE parsing.
  638. This is a pre-pass, and it has to be. Skip is called by lookahead routines
  639. (DeclaresProc, PeekKw, KwAhead) that SAVE AND RESTORE srcPos; an include
  640. performed inside Skip would run once during the lookahead and once for real,
  641. and the second pass would see the first splice's text and report error 96.
  642. A splice also grows the buffer, which a rewind cannot undo. So the whole
  643. expansion is done here, in one forward scan with no lookahead, and the
  644. parser only ever sees the finished text.
  645. The scan has to recognise exactly what the lexer does, or it would expand a
  646. directive that the lexer would have read as a string or a comment: a quoted
  647. literal is skipped, then star-paren comments (which is why {$I} written
  648. inside one is inert - t56_include.inc relies on that), then brace comments,
  649. and only then a brace directive.
  650. Divergence from the original: TP3 STREAMS the include file line by line and
  651. switches back to the outer file at EOF (TPSRC10 fileline/getincl/fileeof);
  652. we hold the whole source in TextBuf and address it by index, so the include
  653. is spliced in place. A nested {$I} therefore cannot switch back on its own
  654. - which TP3 also refuses, and that is the error 96 reported here. *)
  655. VAR i, d0, d1, k, ni, n : CARDINAL ;
  656. c, letter : CHAR ;
  657. isInc : BOOLEAN ;
  658. BEGIN
  659. i := 0 ;
  660. inclActive := FALSE ;
  661. inclEnd := 0 ;
  662. WHILE (i < Length ()) AND OK () DO
  663. c := CharAt (i) ;
  664. IF c = "'" THEN
  665. INC (i) ;
  666. WHILE (i < Length ()) AND (CharAt (i) # "'") DO INC (i) END ;
  667. IF i < Length () THEN INC (i) END
  668. ELSIF (c = '(') AND (i + 1 < Length ()) AND (CharAt (i + 1) = '*') THEN
  669. INC (i, 2) ;
  670. WHILE (i < Length ()) AND NOT ((CharAt (i) = '*')
  671. AND (i + 1 < Length ()) AND (CharAt (i + 1) = ')')) DO
  672. INC (i)
  673. END ;
  674. IF i < Length () THEN INC (i, 2) END
  675. ELSIF (c = '{') AND (i + 1 < Length ()) AND (CharAt (i + 1) = '$') THEN
  676. d0 := i ;
  677. d1 := i + 2 ;
  678. WHILE (d1 < Length ()) AND (CharAt (d1) # '}') DO INC (d1) END ;
  679. IF i + 2 < Length () THEN
  680. letter := Upper (CharAt (i + 2))
  681. ELSE
  682. letter := 0C
  683. END ;
  684. isInc := FALSE ;
  685. IF letter = 'I' THEN
  686. k := i + 3 ;
  687. WHILE (k < d1) AND ((CharAt (k) = ' ') OR (CharAt (k) = CHR (9))) DO
  688. INC (k)
  689. END ;
  690. IF (k >= d1) OR ((CharAt (k) # '+') AND (CharAt (k) # '-')) THEN
  691. isInc := TRUE
  692. END
  693. END ;
  694. IF isInc THEN
  695. IF inclActive AND (d0 < inclEnd) THEN
  696. srcPos := d0 ;
  697. Err (EInclNest) ; (* 96 *)
  698. RETURN
  699. END ;
  700. ni := 0 ;
  701. WHILE (k < d1) AND (ni < 127) DO
  702. inclName [ni] := CharAt (k) ;
  703. INC (k) ;
  704. INC (ni)
  705. END ;
  706. WHILE (ni > 0) AND ((inclName [ni - 1] = ' ')
  707. OR (inclName [ni - 1] = CHR (9))) DO
  708. DEC (ni)
  709. END ;
  710. inclName [ni] := 0C ;
  711. MakeIncludePath () ;
  712. IF NOT ReadWholeFile (inclPath, n) THEN
  713. srcPos := d0 ;
  714. Err (EFileNF) ; (* 90 *)
  715. RETURN
  716. END ;
  717. DeleteFromTo (d0, d1 - d0 + 1) ;
  718. IF NOT InsertStr (d0, incBuf, n) THEN
  719. srcPos := d0 ;
  720. Err (EMemOvf) ;
  721. RETURN
  722. END ;
  723. inclEnd := d0 + n ;
  724. inclActive := TRUE ;
  725. i := d0 (* scan the new text too *)
  726. ELSE
  727. i := d1 + 1 (* ordinary directive: leave it *)
  728. END
  729. ELSIF c = '{' THEN
  730. INC (i) ;
  731. WHILE (i < Length ()) AND (CharAt (i) # '}') DO INC (i) END ;
  732. IF i < Length () THEN INC (i) END
  733. ELSE
  734. INC (i)
  735. END
  736. END ;
  737. srcLen := Length () ;
  738. srcPos := 0
  739. END ExpandIncludes ;
  740. (* ---------------------------------------------------------------- *)
  741. (* emission : ebyte / eword / ecall / ejump *)
  742. (* ---------------------------------------------------------------- *)
  743. PROCEDURE Ebyte (b : BYTE) ;
  744. BEGIN
  745. IF (pc >= MaxCode) OR (imgReady AND (pc >= CodeMax)) THEN
  746. (* The image buffer bound (MaxCode) and the boot harness's code ceiling
  747. (CodeMax). Globals no longer sit immediately above the code - they
  748. live at DataStart and initmem zeroes them - so this is purely a
  749. capacity check: cbuf cannot hold another byte, or the image would
  750. reach the configured data base. Reported as TP3's error
  751. 98 (memory overflow) rather than a silent truncation. *)
  752. Err (EMemOvf)
  753. ELSE
  754. cbuf [pc] := b ;
  755. INC (pc)
  756. END
  757. END Ebyte ;
  758. PROCEDURE Eword (w : CARDINAL) ;
  759. BEGIN
  760. Ebyte (VAL (BYTE, w MOD 100H)) ;
  761. Ebyte (VAL (BYTE, (w DIV 100H) MOD 100H))
  762. END Eword ;
  763. PROCEDURE PatchWord (at, w : CARDINAL) ;
  764. (* Store a 16-bit word into cbuf at an absolute offset.
  765. A helper, because writing this inline got it wrong in all four header
  766. words: the low byte was (w DIV 16) MOD 100H, which is a NIBBLE shift, not
  767. the byte shift (w MOD 100H). So 1181h - the data base - was stored as
  768. 0118h = 280. It was invisible for as long as nothing read those words,
  769. which is exactly what "write it inline once and trust it" buys you. *)
  770. BEGIN
  771. cbuf [at] := VAL (BYTE, w MOD 100H) ;
  772. cbuf [at + 1] := VAL (BYTE, (w DIV 100H) MOD 100H)
  773. END PatchWord ;
  774. PROCEDURE AddPatch (place, target : CARDINAL) ;
  775. BEGIN
  776. IF nPatch < MaxPatch THEN
  777. patches [nPatch].place := place ;
  778. patches [nPatch].target := target ;
  779. patches [nPatch].filled := FALSE ;
  780. INC (nPatch)
  781. ELSE
  782. Err (ECompOvf)
  783. END
  784. END AddPatch ;
  785. PROCEDURE SetPatTgt (idx, t : CARDINAL) ;
  786. BEGIN
  787. IF idx < nPatch THEN
  788. patches [idx].target := t
  789. END
  790. END SetPatTgt ;
  791. PROCEDURE EmCall (target : CARDINAL) : CARDINAL ;
  792. (* E8 rel16 near call; target = 0 => forward (patched later).
  793. Returns the patch slot, or 0 when resolved directly. *)
  794. VAR rel, p : CARDINAL ;
  795. BEGIN
  796. Ebyte (0E8H) ;
  797. IF target = 0 THEN
  798. Eword (0) ;
  799. p := nPatch ;
  800. AddPatch (pc - 2, 0) ;
  801. RETURN p
  802. END ;
  803. (* rel16 is measured from the END of the instruction. Here pc already
  804. points past the opcode(s) and at the displacement field, so the
  805. instruction ends at pc+2 - the same convention ResolvePatches uses
  806. with "place + 2". Omitting the +2 lands every direct call/jump 2 bytes
  807. past its target. *)
  808. rel := (target + 10000H - (pc + 2)) MOD 10000H ;
  809. Eword (rel) ;
  810. RETURN 0
  811. END EmCall ;
  812. PROCEDURE EmJmpNear (target : CARDINAL) : CARDINAL ;
  813. (* E9 rel16; target = 0 => forward. Returns patch slot or 0. *)
  814. VAR rel, p : CARDINAL ;
  815. BEGIN
  816. Ebyte (0E9H) ;
  817. IF target = 0 THEN
  818. Eword (0) ;
  819. p := nPatch ;
  820. AddPatch (pc - 2, 0) ;
  821. RETURN p
  822. END ;
  823. rel := (target + 10000H - (pc + 2)) MOD 10000H ; (* see EmCall *)
  824. Eword (rel) ;
  825. RETURN 0
  826. END EmJmpNear ;
  827. PROCEDURE JccShort (cc : BYTE) : BYTE ;
  828. (* The 8086 SHORT Jcc opcode for a condition nibble. 70h..7Fh is exactly
  829. 70h + nibble: 70 JO 71 JNO 72 JB 73 JAE 74 JE 75 JNE 76 JBE 77 JA
  830. 78 JS 79 JNS 7A JP 7B JNP 7C JL 7D JGE 7E JLE 7F JG.
  831. So 70H + cc is the same condition the 386-only `0F 8x rel16' (for a Jcc) or
  832. `0F 9x' (for a SETcc) encoded, which is what lets the seven EmJcc sites and
  833. the six EmSetcc arms go on passing the low byte they always passed. *)
  834. VAR n : CARDINAL ;
  835. BEGIN
  836. n := VAL (CARDINAL, cc) MOD 10H ;
  837. RETURN VAL (BYTE, 70H + n)
  838. END JccShort ;
  839. PROCEDURE JccShortInv (cc : BYTE) : BYTE ;
  840. (* The same, for a jump that is taken when the condition does NOT hold.
  841. EmJcc needs this one and EmSetcc needs the other, and the difference is the
  842. whole bug, so it is worth being explicit about where it comes from: the low
  843. bit of a Jcc code IS the negation bit. 4/5, C/D, E/F, 2/3, 6/7, A/9, B/8 and
  844. 0/1 are the eight (condition, its negation) pairs, so negating a condition is
  845. `n XOR 1' and nothing more - `JE' and `JNE' are 0x74 and 0x75.
  846. Gm2 under -fiso has no XOR on integers at all: BITAND and BAND are both
  847. syntax errors, and arithmetic on a BYTE operand is rejected too, which is
  848. why every operand here goes through VAL. n + 1 - 2*(n MOD 2) is XOR 1 for a
  849. four-bit n and it lives in one named place rather than open-coded, because
  850. an open-coded negation at two call sites is how they end up disagreeing. *)
  851. VAR n : CARDINAL ;
  852. BEGIN
  853. n := VAL (CARDINAL, cc) MOD 10H ;
  854. RETURN VAL (BYTE, 70H + n + 1 - 2 * (n MOD 2))
  855. END JccShortInv ;
  856. PROCEDURE EmJcc (cc : BYTE ; target : CARDINAL) : CARDINAL ;
  857. (* A conditional branch, 8086 style. `cc' is the condition nibble, and it is
  858. exactly the low byte of the `0F 8x rel16' this used to emit.
  859. That was a real fault and nothing in the build could see it: `0F' is a
  860. 386-and-later opcode prefix and the 8086 has none, so EVERY conditional
  861. branch in EVERY compiled program was an illegal instruction on the machine
  862. TP3 targets. FCML decoded it happily, because FCML's -m16 mode is 386 --
  863. and FCML is this project's independent disassembler, so the one tool that
  864. could have objected was the one guaranteed to agree. qemu-system-i386 has
  865. no 8086 model either; its lowest is 486. So the compile succeeded, the
  866. .COM linked, the layout checked, the golden held and all 30 fixtures ran to
  867. the right answers, all at once, with the bug in.
  868. TPSRC8 lays IF, WHILE and REPEAT out as
  869. MOV AL,brnchop ; MOV AH,#$03 ; CALL eword ; PUSH pc ; CALL ejump
  870. i.e. a SHORT Jcc of displacement 3, stepping over a 3-byte EJMP. That is
  871. the shape here too, and EmJmpNear already owns the displacement arithmetic
  872. and the patch slot, so it is three lines and there is no second copy of
  873. that rule.
  874. The one thing that is NOT the same as TPSRC8, and cost a round of "every
  875. conditional is inverted" (t09_if printed pos/nonpos/lt for a program that
  876. must print nonpos/pos/ge): TP3's brnchop is the branch taken when the
  877. condition is TRUE, and TP3 steps over the EJMP when it is taken. Here `cc'
  878. is the branch taken when the condition is FALSE -- IF's `EmJcc (84H)' is
  879. JZ, patched to the ELSE, so it must fire when the test failed. EmJcc jumps
  880. to the target, it does not step over it, so stepping over an EJMP and then
  881. falling into the destination is the wrong way round: the byte has to be
  882. JccShortInv, not JccShort. The control flow that comes out is identical to
  883. the `0F 8x' form this replaces; only which of the pair is spelled differs.
  884. The flags survive, and the FOR test needs them to: it emits CMP and then
  885. Jcc with nothing in between, so anything that wrote a flag here would
  886. break the loop. Jcc and EJMP both leave the flags alone. *)
  887. BEGIN
  888. Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)
  889. Ebyte (03H) ; (* rel8: step over the 3-byte EJMP *)
  890. RETURN EmJmpNear (target) (* target = 0 => forward, see above *)
  891. END EmJcc ;
  892. PROCEDURE ResolvePatches () ;
  893. VAR i : CARDINAL ;
  894. rel : CARDINAL ;
  895. BEGIN
  896. i := 0 ;
  897. WHILE i < nPatch DO
  898. IF NOT patches [i].filled THEN
  899. rel := (patches [i].target + 10000H - (patches [i].place + 2))
  900. MOD 10000H ;
  901. cbuf [patches [i].place] := VAL (BYTE, rel MOD 100H) ;
  902. cbuf [patches [i].place + 1] := VAL (BYTE, (rel DIV 100H) MOD 100H) ;
  903. patches [i].filled := TRUE
  904. END ;
  905. INC (i)
  906. END
  907. END ResolvePatches ;
  908. (* 1-byte const emitters *)
  909. PROCEDURE EmMovAxi (imm : CARDINAL) ;
  910. BEGIN
  911. Ebyte (0B8H) ; Eword (imm)
  912. END EmMovAxi ;
  913. PROCEDURE EmMovCxi (imm : CARDINAL) ;
  914. BEGIN
  915. Ebyte (0B9H) ; Eword (imm)
  916. END EmMovCxi ;
  917. PROCEDURE EmMovBpSp () ;
  918. BEGIN
  919. Ebyte (8BH) ; Ebyte (0ECH)
  920. END EmMovBpSp ;
  921. PROCEDURE EmMovAh0 () ;
  922. BEGIN
  923. Ebyte (0B4H) ; Ebyte (0H)
  924. END EmMovAh0 ;
  925. PROCEDURE EmMovAxSp () ;
  926. (* Read the top of the stack into AX, leaving the stack unchanged.
  927. The obvious encoding, MOV AX,[SP], DOES NOT EXIST on the 8086. There is
  928. no encoding of [SP] as a memory operand: SIB bytes, which is how [ESP]
  929. would be written, did not exist until the 386, and mod=00 / rm=100 is
  930. [SI], not [SP]. The first version of this emitted 8B 44 24 00 - mod=01,
  931. rm=100, SIB=24h, disp8=0 - which is correct only on a 386 and above. Two
  932. of this project's oracles agree that it is wrong: fcml in 16-bit mode
  933. decodes it as MOV AX,[SI+0x24h], and so does qemu executing it, because
  934. qemu follows the CPU's rules for the encoding it is given rather than
  935. guessing. It was not the encoder's fault that the bytes were well formed;
  936. they were, and they read SI+24h.
  937. The observable effect was that CASE compiled to no branches at all: each
  938. label test loaded a garbage address, every comparison failed, and the
  939. program fell straight past the whole statement and exited without printing.
  940. A CASE fixture caught it. Nothing else could have - the encoding is
  941. valid, the size is right, and the byte-level checks have no way to know
  942. what register was meant.
  943. So: POP then PUSH the same value. Two bytes, no SIB, correct on every
  944. 8086, and observationally identical to peeking - the stack pointer ends
  945. where it started, holding the same value. *)
  946. BEGIN
  947. Ebyte (58H) ; (* POP AX *)
  948. Ebyte (50H) (* PUSH AX *)
  949. END EmMovAxSp ;
  950. PROCEDURE EmMovCxSp () ;
  951. (* The same, for CX - the FOR loop's bound, pushed by the FOR statement and
  952. re-read on every iteration. This one was emitting 8B 0C and nothing else,
  953. which is MOV CX,[SI] with the SIB slot missing: the *next* instruction was
  954. consumed as the SIB byte and the displacement. Same root cause, same fix,
  955. and it had not been noticed only because no FOR fixture is executed yet. *)
  956. BEGIN
  957. Ebyte (59H) ; (* POP CX *)
  958. Ebyte (51H) (* PUSH CX *)
  959. END EmMovCxSp ;
  960. PROCEDURE EmPushAx () ;
  961. BEGIN
  962. Ebyte (50H)
  963. END EmPushAx ;
  964. PROCEDURE EmPopCx () ;
  965. BEGIN
  966. Ebyte (59H)
  967. END EmPopCx ;
  968. PROCEDURE EmPopDx () ;
  969. BEGIN
  970. Ebyte (5AH)
  971. END EmPopDx ;
  972. PROCEDURE EmPopSi () ;
  973. BEGIN
  974. Ebyte (5EH) (* POP SI *)
  975. END EmPopSi ;
  976. PROCEDURE EmPopAx () ;
  977. BEGIN
  978. Ebyte (58H) (* POP AX *)
  979. END EmPopAx ;
  980. PROCEDURE EmPushBx () ;
  981. BEGIN Ebyte (53H) END EmPushBx ;
  982. PROCEDURE EmPushCx () ;
  983. BEGIN Ebyte (51H) END EmPushCx ;
  984. PROCEDURE EmPushDx () ;
  985. BEGIN Ebyte (52H) END EmPushDx ;
  986. PROCEDURE EmPopBx () ;
  987. BEGIN Ebyte (5BH) END EmPopBx ;
  988. PROCEDURE EmMovAxCx () ;
  989. BEGIN Ebyte (89H) ; Ebyte (0C8H) END EmMovAxCx ; (* MOV AX,CX *)
  990. PROCEDURE EmMovChAl () ;
  991. BEGIN Ebyte (88H) ; Ebyte (0C5H) END EmMovChAl ; (* MOV CH,AL *)
  992. PROCEDURE EmMovChImm (v : CARDINAL) ;
  993. BEGIN Ebyte (0B5H) ; Ebyte (VAL (BYTE, v MOD 256)) END EmMovChImm ;
  994. (* 91 = XCHG AX,CX, and NOT 93. LoadPair hands the operator the left
  995. operand in CX and the right in AX (in the original, and still the
  996. commonest, shape: it pushes the left, loads the right, then pops the
  997. left into CX), so the exchange is what puts LEFT in AX for the
  998. operation to act on.
  999. Without it, `a - b` computes `b - a`; with the wrong register, `a + b`
  1000. computes `AX' + a` where AX' is whatever BX happened to hold.
  1001. This emitted 93H = XCHG BX,AX for its entire life, which is the same class
  1002. of mistake as MovSiBx = 89 DC in Runtime.mod: the right opcode, the wrong
  1003. ModRM, decoding cleanly. Byte counts were right, the compile matrix was
  1004. green, and no exec fixture did arithmetic on two variables - the first one
  1005. to do so, `c := a + b` with a=7 b=5, printed 263 = 0100h+7, where 0100h
  1006. was the caller's leftover BX. The name was the only thing wrong, and
  1007. nothing read the name: audit_helpers.py swept Runtime.mod and not
  1008. Compiler.mod, which is where most of these emitters live. It does both
  1009. modules now. *)
  1010. PROCEDURE EmXchgAxCx () ;
  1011. BEGIN
  1012. Ebyte (91H)
  1013. END EmXchgAxCx ;
  1014. PROCEDURE EmXorAxAx () ;
  1015. BEGIN
  1016. Ebyte (33H) ; Ebyte (0C0H)
  1017. END EmXorAxAx ;
  1018. PROCEDURE EmAddAxCx () ;
  1019. BEGIN
  1020. Ebyte (3H) ; Ebyte (0C1H)
  1021. END EmAddAxCx ;
  1022. PROCEDURE EmSubAxCx () ;
  1023. BEGIN
  1024. Ebyte (2BH) ; Ebyte (0C1H)
  1025. END EmSubAxCx ;
  1026. PROCEDURE EmMulAxCx () ;
  1027. BEGIN
  1028. Ebyte (0F7H) ; Ebyte (0E9H)
  1029. END EmMulAxCx ;
  1030. PROCEDURE EmIDivAxCx () ;
  1031. BEGIN
  1032. Ebyte (99H) ; Ebyte (0F7H) ; Ebyte (0F9H)
  1033. END EmIDivAxCx ;
  1034. PROCEDURE EmAndAxCx () ;
  1035. BEGIN
  1036. Ebyte (23H) ; Ebyte (0C1H)
  1037. END EmAndAxCx ;
  1038. PROCEDURE EmOrAxCx () ;
  1039. BEGIN
  1040. Ebyte (0BH) ; Ebyte (0C1H)
  1041. END EmOrAxCx ;
  1042. PROCEDURE EmOrAxAx () ;
  1043. (* 09 C0: OR AX,AX - the 8086's flag-only test of AX, and the one the string
  1044. comparison needs, because cmpstr hands back a signed -1/0/+1 rather than
  1045. flags. OR AX,AX sets SF and ZF from the value and clears OF and CF, which
  1046. is exactly the flag state a CMP AX,0 would leave - see EmSetcc and
  1047. ParseCmp's EmRelSetcc. TEST AX,AX (85 C0) does the same thing, but
  1048. tests/audit_helpers.py's name grammar has no `Test' mnemonic, and an
  1049. emitter that cannot be name-checked is worse than one that can.
  1050. It is NOT EmXorAxAx: XORing AX with itself zeroes it, which would throw
  1051. away the very value the flags are supposed to describe. *)
  1052. BEGIN
  1053. Ebyte (09H) ; Ebyte (0C0H)
  1054. END EmOrAxAx ;
  1055. PROCEDURE EmNegAx () ;
  1056. BEGIN
  1057. Ebyte (0F7H) ; Ebyte (0D8H)
  1058. END EmNegAx ;
  1059. PROCEDURE EmNotAx () ;
  1060. BEGIN
  1061. Ebyte (0F7H) ; Ebyte (0D0H)
  1062. END EmNotAx ;
  1063. PROCEDURE EmXorAl01 () ;
  1064. (* XOR AL,#01 - TPSRC9 neglevel's *boolean* NOT:
  1065. CALL loadatom ; CALL ecode ; B $02,$34,$01
  1066. neglevel tests the type first (CMP CL,#$0A -> integer -> NOT AX,
  1067. CMP CL,#$0B -> boolean -> XOR AL,#01) and reports error 47 for anything
  1068. else. Emitting NOT AX for a boolean leaves 0FFFEh or 0FFFFh, and the
  1069. runtime's wrbool tests [BP+4] <> 0, so `not (a = a)' came out TRUE. *)
  1070. BEGIN
  1071. Ebyte (34H) ; Ebyte (01H)
  1072. END EmXorAl01 ;
  1073. PROCEDURE EmCmpAxCx () ;
  1074. BEGIN
  1075. Ebyte (3BH) ; Ebyte (0C1H)
  1076. END EmCmpAxCx ;
  1077. PROCEDURE EmCmpAxi (imm : CARDINAL) ;
  1078. BEGIN
  1079. Ebyte (03DH) ; Eword (imm)
  1080. END EmCmpAxi ;
  1081. PROCEDURE EmSetcc (cc : BYTE) ;
  1082. (* Flags -> a Boolean in AX, on an 8086. `cc' is the SETcc opcode's low byte
  1083. (94H = E, 95H = NE, 9CH = L, 9DH = GE, 9EH = LE, 9FH = G), i.e. the same
  1084. condition nibble EmJcc takes.
  1085. This used to emit `0F cc C0' - SETcc - which is 386-and-later, and then a
  1086. MOV AH,0. The 8086 cannot read its flags as a value at all, so there was
  1087. nothing else to fall back on.
  1088. TPSRC9's flgbool is the fallback, and emits exactly this for exactly this
  1089. case (CH = 04h, a comparison whose result is wanted as a value rather than
  1090. as a branch):
  1091. CALL ecode ; B $03,$B8,$01,$00 -> MOV AX,#0001
  1092. MOV AL,brnchop ; CALL ebyte -> JNZ +1
  1093. CALL ecode ; B $02,$01,$48 -> DEC AX
  1094. AX stays 1 because the DEC was stepped over, and becomes 0 because it ran.
  1095. So the shape is one MOV, one short Jcc whose displacement is the length of
  1096. the DEC, and the DEC - which is EmJcc's shape with a different displacement,
  1097. and the reason both are two instructions and a byte.
  1098. The polarity is the opposite of EmJcc's, and deliberately so: here the jump
  1099. must be taken when the comparison is TRUE, because what is being asked is
  1100. "is this comparison true", and the nibble the six ParseCmp arms pass is the
  1101. comparison's own opcode. So this is JccShort and EmJcc is JccShortInv --
  1102. see EmJcc for why the difference is there at all. TP3's flgbool writes JNZ
  1103. for the same reason; in the one case IT reaches flgbool from, the boolean is
  1104. sitting in AX rather than in the flags, so JNZ is how it says "AX is
  1105. non-zero".
  1106. AH comes out 0 for free, which is why the EmMovAh0 this used to end with is
  1107. gone: 6 bytes here where the old sequence was 5. The FLAGS do not survive,
  1108. which the old SETcc did - and nothing reads them. Every conditional branch
  1109. in the compiler is preceded by its own CMP (see EmJcc's note on the FOR
  1110. test), and a comparison's value is consumed either as an AX operand or by
  1111. the test that follows it; the 30 executed fixtures are what holds that
  1112. down, not this comment. *)
  1113. BEGIN
  1114. Ebyte (0B8H) ; Eword (1) ; (* MOV AX,#0001 *)
  1115. Ebyte (JccShort (cc)) ; (* taken when the comparison HOLDS *)
  1116. Ebyte (01H) ; (* rel8: step over the DEC AX *)
  1117. Ebyte (48H) (* DEC AX *)
  1118. END EmSetcc ;
  1119. PROCEDURE EmRelSetcc (op : CARDINAL) ;
  1120. (* The relational operator -> SETcc table, in ONE place, because there are now
  1121. two callers: ParseCmp's numeric arm, whose flags come from EmCmpAxCx, and
  1122. its string arm, whose flags come from a cmpstr result put through
  1123. EmOrAxAx. It was factored out of ParseCmp when the string arm arrived, and
  1124. the note it carried is repeated here rather than referenced, because a
  1125. comment in the caller about a table in another routine is a comment nobody
  1126. reads.
  1127. The mnemonic is written next to every opcode on purpose. `op' is a number,
  1128. so the arm for ">" and the arm for ">=" differed only by two hex digits
  1129. that are each one letter from the other meaning -- SETG (9FH) and SETGE
  1130. (9DH). They were swapped once, which made a > b mean a >= b and a >= b
  1131. mean a > b. Only the equality boundary could see it: 6>5, 5<5, -1>-2 and
  1132. every other case I tried were already right. An opcode on its own does not
  1133. say which comparison it is the answer to.
  1134. `op' is ParseCmp's own numbering: 1 "=", 2 "<>", 3 "<", 4 ">", 5 ">=",
  1135. 6 "<=". The condition nibbles are the SIGNED ones (L/G/GE/LE), and both
  1136. callers hand it a value whose SIGN is the answer - ParseCmp's numeric arm
  1137. compares with CMP AX,CX on signed data, and cmpstr answers -1/0/+1 - so
  1138. the signed test is the right one in both places. One table also means that
  1139. swap cannot come back in one arm and not the other: tests/nonvacuity.sh
  1140. mutates exactly these two lines, and it now breaks the string comparison
  1141. at the same time as the numeric one. *)
  1142. BEGIN
  1143. CASE op OF
  1144. 1 : EmSetcc (94H) ; (* = SETE *)
  1145. | 2 : EmSetcc (95H) ; (* <> SETNE *)
  1146. | 3 : EmSetcc (9CH) ; (* < SETL *)
  1147. | 4 : EmSetcc (9FH) ; (* > SETG *)
  1148. | 5 : EmSetcc (9DH) ; (* >= SETGE *)
  1149. | 6 : EmSetcc (9EH) (* <= SETLE *)
  1150. END
  1151. END EmRelSetcc ;
  1152. PROCEDURE EmIncAx () ;
  1153. BEGIN
  1154. Ebyte (40H)
  1155. END EmIncAx ;
  1156. PROCEDURE EmDecAx () ;
  1157. BEGIN
  1158. Ebyte (48H)
  1159. END EmDecAx ;
  1160. PROCEDURE EmBpDisp (off, regf : CARDINAL) ;
  1161. (* Emit the ModR/M byte and displacement for a [BP+off] operand, picking the
  1162. encoding from the size of off. This is the ONE place that choice is made,
  1163. because getting it wrong is invisible: 8B 46 d8 and 8B 86 lo hi are both
  1164. well-formed MOVs, both decode cleanly, and only one of them reads the
  1165. variable the symbol table names. So the two are chosen here, once, rather
  1166. than re-derived at each of the four call sites. See the ModR/M table in
  1167. Runtime.mod.
  1168. off <= 127 -> mod=01 rm=110 -> 46 <disp8> 3 bytes with the opcode
  1169. otherwise -> mod=10 rm=110 -> 86 <disp16> 4 bytes with the opcode
  1170. Both displacements are SIGNED, and that is the whole subtlety:
  1171. - `off` is a 16-bit value and locals are packed top-anchored below BP:
  1172. locFree is the highest still-free byte (0FFFFh = -1 at entry), a local of
  1173. size n is based so its LAST byte sits on locFree, and locFree then drops
  1174. by n. So the first INTEGER local is at off = 0FFFEh = -2, the second at
  1175. 0FFFCh = -4, and so on - and a string local, which grows UP from its
  1176. base, lands wholly below BP instead of straddling it. The disp16 form
  1177. reads those same two bytes as a signed value and addresses the negative
  1178. slot correctly. There is no overflow case: all 65536 values of `off`
  1179. are representable, and a frame larger than 64K is a different problem.
  1180. - The old code took `off MOD 100H` and always emitted disp8. That is
  1181. the correct low byte for every displacement, so it was accidentally
  1182. right across -32768..+127, which is where locals actually live. It
  1183. went wrong at +128, where disp8 80h is -128 and not +128. So this
  1184. changes no existing program's bytes and fixes the one case that was
  1185. broken -- a bug nobody had hit yet, which is exactly why it wanted a
  1186. test rather than an argument. *)
  1187. BEGIN
  1188. (* mod=01 rm=110 -> 40H + regf*8 + 6H, mod=10 rm=110 -> 80H + regf*8 + 6H.
  1189. regf is the ModR/M reg field: 0 when the opcode's register is the
  1190. accumulator (every original caller), 7 for the DI forms EmLeaDi needs.
  1191. rm is 110 ([BP+disp]) in both. *)
  1192. IF off <= 127 THEN
  1193. Ebyte (VAL (BYTE, 40H + regf * 8 + 6H)) ; Ebyte (VAL (BYTE, off))
  1194. ELSE
  1195. Ebyte (VAL (BYTE, 80H + regf * 8 + 6H)) ; Eword (off)
  1196. END
  1197. END EmBpDisp ;
  1198. PROCEDURE EmBxDisp (off, regf : CARDINAL) ;
  1199. (* The [BX+disp] twin of EmBpDisp. A nested procedure reaches an enclosing
  1200. frame through the static link in BX; the displacement rules are identical
  1201. except for the rm field (111 for BX instead of 110 for BP). *)
  1202. BEGIN
  1203. IF off <= 127 THEN
  1204. Ebyte (VAL (BYTE, 40H + regf * 8 + 7H)) ; Ebyte (VAL (BYTE, off))
  1205. ELSE
  1206. Ebyte (VAL (BYTE, 80H + regf * 8 + 7H)) ; Eword (off)
  1207. END
  1208. END EmBxDisp ;
  1209. PROCEDURE EmMovBxBp () ;
  1210. BEGIN Ebyte (89H) ; Ebyte (0EBH) END EmMovBxBp ; (* MOV BX,BP *)
  1211. PROCEDURE EmMovDxBp () ;
  1212. BEGIN Ebyte (89H) ; Ebyte (0EAH) END EmMovDxBp ; (* MOV DX,BP *)
  1213. PROCEDURE EmMovDxBx () ;
  1214. BEGIN Ebyte (89H) ; Ebyte (0DAH) END EmMovDxBx ; (* MOV DX,BX *)
  1215. PROCEDURE EmMovAxDx () ;
  1216. BEGIN Ebyte (89H) ; Ebyte (0D0H) END EmMovAxDx ; (* MOV AX,DX *)
  1217. PROCEDURE EmLdBxBx (off : CARDINAL) ;
  1218. (* BX := [BX+off] - one step up the static-link chain. *)
  1219. BEGIN Ebyte (8BH) ; EmBxDisp (off, 3) END EmLdBxBx ;
  1220. PROCEDURE EmLoadVarBx (off, nbytes : CARDINAL) ;
  1221. BEGIN
  1222. IF nbytes = 1 THEN
  1223. Ebyte (8AH) ; EmBxDisp (off, 0)
  1224. ELSE
  1225. Ebyte (8BH) ; EmBxDisp (off, 0)
  1226. END
  1227. END EmLoadVarBx ;
  1228. PROCEDURE EmStoreVarBx (off, nbytes : CARDINAL) ;
  1229. BEGIN
  1230. IF nbytes = 1 THEN
  1231. Ebyte (88H) ; EmBxDisp (off, 0)
  1232. ELSE
  1233. Ebyte (89H) ; EmBxDisp (off, 0)
  1234. END
  1235. END EmStoreVarBx ;
  1236. PROCEDURE EmLeaDiBx (off : CARDINAL) ;
  1237. BEGIN Ebyte (8DH) ; EmBxDisp (off, 7) END EmLeaDiBx ;
  1238. PROCEDURE EmLoadVar (local : BOOLEAN ; off, nbytes : CARDINAL) ;
  1239. (* A local is [BP+off] and `off' is already a frame displacement, so it needs
  1240. no bias. A global is [off] with a DIRECT displacement, i.e. an absolute
  1241. address, and that is the image offset + LoadBias - see Runtime.LoadBias. *)
  1242. BEGIN
  1243. IF nbytes = 1 THEN
  1244. IF local THEN
  1245. Ebyte (8AH) ; EmBpDisp (off, 0)
  1246. ELSE
  1247. Ebyte (0A0H) ; Eword ((off + LoadBias) MOD 10000H)
  1248. END
  1249. ELSE
  1250. IF local THEN
  1251. Ebyte (8BH) ; EmBpDisp (off, 0)
  1252. ELSE
  1253. Ebyte (0A1H) ; Eword ((off + LoadBias) MOD 10000H)
  1254. END
  1255. END
  1256. END EmLoadVar ;
  1257. PROCEDURE EmXorByteGlob (off, v : CARDINAL) ;
  1258. BEGIN Ebyte (80H) ; Ebyte (36H) ; Eword ((off + LoadBias) MOD 10000H) ;
  1259. Ebyte (VAL (BYTE, v)) END EmXorByteGlob ;
  1260. PROCEDURE EmStoreVar (local : BOOLEAN ; off, nbytes : CARDINAL) ;
  1261. BEGIN
  1262. IF nbytes = 1 THEN
  1263. IF local THEN
  1264. Ebyte (88H) ; EmBpDisp (off, 0)
  1265. ELSE
  1266. Ebyte (0A2H) ; Eword ((off + LoadBias) MOD 10000H)
  1267. END
  1268. ELSE
  1269. IF local THEN
  1270. Ebyte (89H) ; EmBpDisp (off, 0)
  1271. ELSE
  1272. Ebyte (0A3H) ; Eword ((off + LoadBias) MOD 10000H)
  1273. END
  1274. END
  1275. END EmStoreVar ;
  1276. PROCEDURE EmPushVarAddr (local : BOOLEAN ; off : CARDINAL) ;
  1277. (* LEA AX,[BP+disp] / LEA AX,[off] then PUSH AX - READ passes the address of
  1278. a variable, not its value. 8D 46 disp is LEA AX,[BP+disp8] and
  1279. 8D 86 lo hi is LEA AX,[BP+disp16]; 8D 06 off is LEA AX,[off]
  1280. (mod=00 rm=110 = the direct disp16 form). All three are 8086-legal.
  1281. The [off] form is absolute and so carries LoadBias; the [BP+disp] forms
  1282. are displacements and so do not. *)
  1283. BEGIN
  1284. IF local THEN
  1285. Ebyte (8DH) ; EmBpDisp (off, 0)
  1286. ELSE
  1287. Ebyte (8DH) ; Ebyte (06H) ; Eword ((off + LoadBias) MOD 10000H)
  1288. END ;
  1289. EmPushAx ()
  1290. END EmPushVarAddr ;
  1291. PROCEDURE EmLeaDi (local : BOOLEAN ; off : CARDINAL) ;
  1292. (* LEA DI,[BP+disp] / LEA DI,[off] - the run-time address of an indexed
  1293. string's element, computed once in ParseSub and read by LoadAtom,
  1294. Assignment and IoCall.
  1295. It is the same three encodings EmPushVarAddr uses for AX - 8D 7E disp8,
  1296. 8D BE disp16, 8D 3E off - with the ModR/M reg field set to DI (111)
  1297. instead of the accumulator, which is why EmBpDisp now takes that field and
  1298. this is still the one place the disp8/disp16 choice is made. A local is
  1299. [BP+disp] with no bias; a global is [off] and carries LoadBias, absolute.
  1300. The later `MOV AL,[DI]` / `MOV [DI],AL` read through DS rather than SS.
  1301. That is correct here for the same reason TP3's own string ops use DI with
  1302. the default segment: a .COM loads with DS=SS=CS=ES, and nothing in this
  1303. compiler or its runtime ever reloads a segment register, so a BP-relative
  1304. frame address held in DI addresses the same byte. *)
  1305. BEGIN
  1306. IF local THEN
  1307. Ebyte (8DH) ; EmBpDisp (off, 7)
  1308. ELSE
  1309. Ebyte (8DH) ; Ebyte (3EH) ; Eword ((off + LoadBias) MOD 10000H)
  1310. END
  1311. END EmLeaDi ;
  1312. PROCEDURE EmMovBxAx () ;
  1313. BEGIN Ebyte (89H) ; Ebyte (0C3H) END EmMovBxAx ; (* MOV BX,AX *)
  1314. PROCEDURE EmitFrameBase (level : CARDINAL) ;
  1315. (* Leave BX pointing at the frame for lexical scope `level', pushing the old
  1316. BX. The current frame is at `lexnest'; each step follows the hidden
  1317. static link stored by the enclosing procedure. The caller must POP BX
  1318. after the access. *)
  1319. VAR p, k : CARDINAL ;
  1320. BEGIN
  1321. EmPushBx () ;
  1322. EmMovBxBp () ;
  1323. p := curProc ;
  1324. k := lexnest ;
  1325. WHILE k > level DO
  1326. EmLdBxBx (symtab [p].staticOff) ;
  1327. p := symtab [p].parent ;
  1328. DEC (k)
  1329. END
  1330. END EmitFrameBase ;
  1331. PROCEDURE IsOuterLocal (idx : CARDINAL) : BOOLEAN ;
  1332. BEGIN
  1333. RETURN symtab [idx].local AND (symtab [idx].level < lexnest)
  1334. END IsOuterLocal ;
  1335. PROCEDURE LoadSymVar (idx, off, nbytes : CARDINAL) ;
  1336. BEGIN
  1337. IF IsOuterLocal (idx) THEN
  1338. EmitFrameBase (symtab [idx].level) ;
  1339. EmLoadVarBx (off, nbytes) ;
  1340. EmPopBx ()
  1341. ELSE
  1342. EmLoadVar (symtab [idx].local, off, nbytes)
  1343. END
  1344. END LoadSymVar ;
  1345. PROCEDURE StoreSymVar (idx, off, nbytes : CARDINAL) ;
  1346. BEGIN
  1347. IF IsOuterLocal (idx) THEN
  1348. EmitFrameBase (symtab [idx].level) ;
  1349. EmStoreVarBx (off, nbytes) ;
  1350. EmPopBx ()
  1351. ELSE
  1352. EmStoreVar (symtab [idx].local, off, nbytes)
  1353. END
  1354. END StoreSymVar ;
  1355. PROCEDURE EmPushSymAddr (idx, off : CARDINAL) ;
  1356. BEGIN
  1357. IF IsOuterLocal (idx) THEN
  1358. EmitFrameBase (symtab [idx].level) ;
  1359. Ebyte (8DH) ; EmBxDisp (off, 0) ; (* LEA AX,[BX+off] *)
  1360. EmPopBx () ; (* restore BX before pushing AX *)
  1361. EmPushAx ()
  1362. ELSE
  1363. EmPushVarAddr (symtab [idx].local, off)
  1364. END
  1365. END EmPushSymAddr ;
  1366. PROCEDURE LeaDiSym (idx, off : CARDINAL) ;
  1367. BEGIN
  1368. IF IsOuterLocal (idx) THEN
  1369. EmitFrameBase (symtab [idx].level) ;
  1370. EmLeaDiBx (off) ;
  1371. EmPopBx ()
  1372. ELSE
  1373. EmLeaDi (symtab [idx].local, off)
  1374. END
  1375. END LeaDiSym ;
  1376. PROCEDURE EmLoadVarIndSym (idx, nbytes : CARDINAL) ;
  1377. (* AX := the value reached through a VAR parameter's address slot. *)
  1378. BEGIN
  1379. LoadSymVar (idx, symtab [idx].off, 2) ;
  1380. EmMovBxAx () ;
  1381. IF nbytes = 1 THEN
  1382. Ebyte (8AH) ; Ebyte (07H) ;
  1383. EmMovAh0 ()
  1384. ELSE
  1385. Ebyte (8BH) ; Ebyte (07H)
  1386. END
  1387. END EmLoadVarIndSym ;
  1388. PROCEDURE StoreVarIndPrepSym (idx : CARDINAL) ;
  1389. (* DI := the address held by a VAR parameter's slot, then PUSH DI. *)
  1390. BEGIN
  1391. LoadSymVar (idx, symtab [idx].off, 2) ;
  1392. EmMovDiAx () ;
  1393. EmPushDi ()
  1394. END StoreVarIndPrepSym ;
  1395. PROCEDURE EmitStaticLink (idx : CARDINAL) ;
  1396. (* Pass the lexical parent frame in DX before a call to a nested procedure.
  1397. The callee saves it in its hidden static-link local. A top-level procedure
  1398. has no parent frame and gets nothing. *)
  1399. VAR level : CARDINAL ;
  1400. BEGIN
  1401. level := symtab [idx].level ;
  1402. IF level = 0 THEN
  1403. RETURN
  1404. END ;
  1405. IF level = lexnest THEN
  1406. EmMovDxBp ()
  1407. ELSE
  1408. EmitFrameBase (level) ;
  1409. EmMovDxBx () ;
  1410. EmPopBx ()
  1411. END
  1412. END EmitStaticLink ;
  1413. PROCEDURE EmAddDiAx () ;
  1414. BEGIN Ebyte (01H) ; Ebyte (0C7H) END EmAddDiAx ; (* 01 C7: DI := DI + AX *)
  1415. PROCEDURE EmAddDiImm (n : CARDINAL) ;
  1416. (* ADD DI,imm16: a record field's offset when the element's address is already
  1417. in DI (`Sheet[I,J].Field', `with Sheet[I,J] do'). *)
  1418. BEGIN
  1419. Ebyte (81H) ; Ebyte (0C7H) ; Eword (n MOD 10000H)
  1420. END EmAddDiImm ;
  1421. PROCEDURE EmLdDiGlob (off : CARDINAL) ;
  1422. (* MOV DI,[off]: recover the base address WITH saved in a scratch word. The
  1423. body of a WITH may clobber DI, so the address is parked in the data area
  1424. and reloaded for every field access. *)
  1425. BEGIN
  1426. Ebyte (8BH) ; Ebyte (3EH) ; Eword ((off + LoadBias) MOD 10000H)
  1427. END EmLdDiGlob ;
  1428. PROCEDURE EmStDiGlob (off : CARDINAL) ;
  1429. (* MOV [off],DI: the WITH statement's base-address scratch. *)
  1430. BEGIN
  1431. Ebyte (89H) ; Ebyte (3EH) ; Eword ((off + LoadBias) MOD 10000H)
  1432. END EmStDiGlob ;
  1433. PROCEDURE EmLdAlDi () ;
  1434. BEGIN Ebyte (8AH) ; Ebyte (05H) END EmLdAlDi ; (* 8A 05: AL := [DI] *)
  1435. PROCEDURE EmLdAlSi () ;
  1436. BEGIN Ebyte (8AH) ; Ebyte (04H) END EmLdAlSi ; (* 8A 04: AL := [SI] *)
  1437. PROCEDURE EmIncSi () ;
  1438. BEGIN Ebyte (46H) END EmIncSi ;
  1439. PROCEDURE EmIncDi () ;
  1440. BEGIN Ebyte (47H) END EmIncDi ;
  1441. PROCEDURE EmMovDiAx () ;
  1442. BEGIN Ebyte (89H) ; Ebyte (0C7H) END EmMovDiAx ; (* 89 C7: DI := AX *)
  1443. PROCEDURE EmMovAxDi () ;
  1444. BEGIN Ebyte (89H) ; Ebyte (0F8H) END EmMovAxDi ; (* 89 F8: AX := DI *)
  1445. PROCEDURE EmLdAxDi () ;
  1446. BEGIN Ebyte (8BH) ; Ebyte (05H) END EmLdAxDi ; (* 8B 05: AX := [DI] *)
  1447. PROCEDURE EmStDiAl () ;
  1448. BEGIN Ebyte (88H) ; Ebyte (05H) END EmStDiAl ; (* 88 05: [DI] := AL *)
  1449. PROCEDURE EmPushDi () ;
  1450. BEGIN Ebyte (57H) END EmPushDi ; (* PUSH DI *)
  1451. PROCEDURE EmPushSi () ;
  1452. BEGIN Ebyte (56H) END EmPushSi ; (* PUSH SI *)
  1453. PROCEDURE EmMovAxDs () ;
  1454. BEGIN Ebyte (8CH) ; Ebyte (0D8H) END EmMovAxDs ; (* MOV AX,DS *)
  1455. PROCEDURE EmPopDi () ;
  1456. BEGIN Ebyte (5FH) END EmPopDi ; (* POP DI *)
  1457. PROCEDURE EmLoadVarInd (off, nbytes : CARDINAL) ;
  1458. (* AX := the value of a VAR parameter. [BP+off] holds the ADDRESS; BX carries
  1459. it so the byte/word load has a base register. The address is a DS offset:
  1460. a .COM has one segment, which is why no segment register appears here. *)
  1461. BEGIN
  1462. EmLoadVar (TRUE, off, 2) ; (* AX := [BP+off], the address *)
  1463. Ebyte (89H) ; Ebyte (0C3H) ; (* MOV BX,AX *)
  1464. IF nbytes = 1 THEN
  1465. Ebyte (8AH) ; Ebyte (07H) ; (* MOV AL,[BX] *)
  1466. EmMovAh0 ()
  1467. ELSE
  1468. Ebyte (8BH) ; Ebyte (07H) (* MOV AX,[BX] *)
  1469. END
  1470. END EmLoadVarInd ;
  1471. PROCEDURE EmStoreVarIndPrep (off : CARDINAL) ;
  1472. (* DI := the address of a VAR parameter, then PUSH DI. The push protects it
  1473. across the parse of the right-hand side, which may use DI for a string
  1474. element of its own. EmStoreInd finishes the store after POP DI. *)
  1475. BEGIN
  1476. Ebyte (8BH) ; EmBpDisp (off, 7) ; (* MOV DI,[BP+off] *)
  1477. EmPushDi ()
  1478. END EmStoreVarIndPrep ;
  1479. PROCEDURE EmStDiImm (off, v : CARDINAL) ;
  1480. (* MOV byte [DI+off],imm8: one byte of a set constructor stored through a
  1481. DI-addressed record field. Sets are at most 32 bytes, so disp8 is enough. *)
  1482. BEGIN
  1483. Ebyte (0C6H) ; Ebyte (45H) ; Ebyte (VAL (BYTE, off)) ; Ebyte (VAL (BYTE, v))
  1484. END EmStDiImm ;
  1485. PROCEDURE EmStDiAlOff (off : CARDINAL) ;
  1486. (* MOV [DI+off],AL: one byte of a real copied through a DI-addressed field. *)
  1487. BEGIN
  1488. Ebyte (88H) ; Ebyte (45H) ; Ebyte (VAL (BYTE, off))
  1489. END EmStDiAlOff ;
  1490. PROCEDURE EmLdAlDiOff (off : CARDINAL) ;
  1491. (* MOV AL,[DI+off]: the byte of a set variable that holds an element's bit. *)
  1492. BEGIN
  1493. Ebyte (8AH) ; Ebyte (45H) ; Ebyte (VAL (BYTE, off))
  1494. END EmLdAlDiOff ;
  1495. PROCEDURE EmCmpAlImm (v : CARDINAL) ;
  1496. BEGIN Ebyte (3CH) ; Ebyte (VAL (BYTE, v)) END EmCmpAlImm ; (* CMP AL,imm8 *)
  1497. PROCEDURE EmSubAlImm (v : CARDINAL) ;
  1498. BEGIN Ebyte (2CH) ; Ebyte (VAL (BYTE, v)) END EmSubAlImm ; (* SUB AL,imm8 *)
  1499. PROCEDURE EmMovClAl () ;
  1500. BEGIN Ebyte (88H) ; Ebyte (0C1H) END EmMovClAl ; (* MOV CL,AL *)
  1501. PROCEDURE EmAndClImm (v : CARDINAL) ;
  1502. BEGIN Ebyte (80H) ; Ebyte (0E1H) ; Ebyte (VAL (BYTE, v)) END EmAndClImm ;
  1503. PROCEDURE EmShrAx1 () ;
  1504. BEGIN Ebyte (0D1H) ; Ebyte (0E8H) END EmShrAx1 ; (* SHR AX,1 *)
  1505. PROCEDURE EmShlAxCl () ;
  1506. BEGIN Ebyte (0D3H) ; Ebyte (0E0H) END EmShlAxCl ; (* SHL AX,CL *)
  1507. PROCEDURE EmShrAxCl () ;
  1508. BEGIN Ebyte (0D3H) ; Ebyte (0E8H) END EmShrAxCl ; (* SHR AX,CL *)
  1509. PROCEDURE EmMovBxi (v : CARDINAL) ;
  1510. BEGIN Ebyte (0BBH) ; Eword (v MOD 10000H) END EmMovBxi ; (* MOV BX,imm16 *)
  1511. PROCEDURE EmLeaBx (off : CARDINAL) ;
  1512. (* LEA BX,[BP+off]: the address of a local set. *)
  1513. BEGIN
  1514. Ebyte (8DH) ; EmBpDisp (off, 3)
  1515. END EmLeaBx ;
  1516. PROCEDURE EmMovBxDi () ;
  1517. BEGIN Ebyte (89H) ; Ebyte (0FBH) END EmMovBxDi ; (* MOV BX,DI *)
  1518. PROCEDURE EmAddBxAx () ;
  1519. BEGIN Ebyte (01H) ; Ebyte (0C3H) END EmAddBxAx ; (* ADD BX,AX *)
  1520. PROCEDURE EmLdAlBx () ;
  1521. BEGIN Ebyte (8AH) ; Ebyte (07H) END EmLdAlBx ; (* MOV AL,[BX] *)
  1522. PROCEDURE EmMovDlImm (v : CARDINAL) ;
  1523. BEGIN Ebyte (0B2H) ; Ebyte (VAL (BYTE, v)) END EmMovDlImm ;
  1524. PROCEDURE EmShlDlCl () ;
  1525. BEGIN Ebyte (0D2H) ; Ebyte (0E2H) END EmShlDlCl ; (* SHL DL,CL *)
  1526. PROCEDURE EmAndAlDl () ;
  1527. BEGIN Ebyte (20H) ; Ebyte (0D0H) END EmAndAlDl ; (* AND AL,DL *)
  1528. PROCEDURE EmAndAlImm (v : CARDINAL) ;
  1529. BEGIN Ebyte (24H) ; Ebyte (VAL (BYTE, v)) END EmAndAlImm ; (* AND AL,imm8 *)
  1530. PROCEDURE EmStoreInd (nbytes : CARDINAL) ;
  1531. (* [DI] := AL/WORD, after POP DI - the companion of EmStoreVarIndPrep. *)
  1532. BEGIN
  1533. IF nbytes = 1 THEN
  1534. EmStDiAl () (* MOV [DI],AL *)
  1535. ELSE
  1536. Ebyte (89H) ; Ebyte (05H) (* MOV [DI],AX *)
  1537. END
  1538. END EmStoreInd ;
  1539. PROCEDURE EmFileIo (reading : BOOLEAN ; fileLocal : BOOLEAN ; fileOff : CARDINAL ;
  1540. addrKind : CARDINAL ; addrLocal : BOOLEAN ;
  1541. addrOff, count : CARDINAL ; stringArg : BOOLEAN) ;
  1542. (* Typed-file block I/O: read/write `count' bytes at the data argument to the
  1543. open file in the file variable.
  1544. ABI to DOS: BX = handle, CX = byte count, DS:DX = buffer, AH = 3Fh read /
  1545. 40h write. The handle is the first word of the file control block. For a
  1546. DI-addressed record (`Sheet[I,J]') the address is already in DI; a plain
  1547. variable's address is formed by LEA. The DOS result is handed to the
  1548. io_store runtime entry, so IOResult sees it.
  1549. This is emitted directly rather than through a runtime entry because the
  1550. runtime has only ~45 bytes of probe headroom left; the code is two words
  1551. per call site and the compiler's code budget is the loose one. *)
  1552. BEGIN
  1553. IF addrKind = 5 THEN
  1554. (* DI already holds a kind-5 element's address. *)
  1555. ELSIF addrKind = 4 THEN
  1556. EmLeaDi (FALSE, addrOff) (* a code-resident blob *)
  1557. ELSIF addrKind = 1 THEN
  1558. EmLeaDi (addrLocal, addrOff)
  1559. ELSE
  1560. Err (ETypeErr) ;
  1561. RETURN
  1562. END ;
  1563. Ebyte (89H) ; Ebyte (0FAH) ; (* MOV DX,DI *)
  1564. IF stringArg THEN
  1565. (* DX points at the characters, CX = the length byte at [DI]. *)
  1566. Ebyte (42H) ; (* INC DX *)
  1567. EmLdAlDi () ;
  1568. EmMovClAl () ;
  1569. Ebyte (30H) ; Ebyte (0EDH) (* XOR CH,CH *)
  1570. ELSE
  1571. EmMovCxi (count)
  1572. END ;
  1573. EmLoadVar (fileLocal, fileOff, 2) ; (* AX := the DOS handle *)
  1574. Ebyte (89H) ; Ebyte (0C3H) ; (* MOV BX,AX *)
  1575. Ebyte (0B4H) ; (* MOV AH,3Fh/40h *)
  1576. IF reading THEN Ebyte (3FH) ELSE Ebyte (40H) END ;
  1577. Ebyte (0CDH) ; Ebyte (21H) ; (* INT 21h *)
  1578. DropC (EmCall (TU_IoStore))
  1579. END EmFileIo ;
  1580. PROCEDURE EmInlineCh (ch : CARDINAL) : CARDINAL ;
  1581. (* Emit a one-character string literal behind a JMP and return the image
  1582. offset of its length byte. Used by text-file Write(File, ' '). *)
  1583. VAR here : CARDINAL ;
  1584. BEGIN
  1585. Ebyte (0EBH) ; Ebyte (2) ;
  1586. here := pc ;
  1587. Ebyte (1) ; Ebyte (VAL (BYTE, ch)) ;
  1588. RETURN here
  1589. END EmInlineCh ;
  1590. PROCEDURE EmInlineCrLf () : CARDINAL ;
  1591. (* The CR LF pair every text-file Writeln emits. *)
  1592. VAR here : CARDINAL ;
  1593. BEGIN
  1594. Ebyte (0EBH) ; Ebyte (3) ;
  1595. here := pc ;
  1596. Ebyte (2) ; Ebyte (13) ; Ebyte (10) ;
  1597. RETURN here
  1598. END EmInlineCrLf ;
  1599. PROCEDURE EmSubSp (n : CARDINAL) ;
  1600. BEGIN
  1601. Ebyte (81H) ; Ebyte (0ECH) ; Eword (n MOD 10000H)
  1602. END EmSubSp ;
  1603. PROCEDURE EmAddSp (n : CARDINAL) ;
  1604. BEGIN
  1605. IF n <= 126 THEN
  1606. Ebyte (83H) ; Ebyte (0C4H) ; Ebyte (VAL (BYTE, n))
  1607. ELSE
  1608. Ebyte (81H) ; Ebyte (0C4H) ; Eword (n MOD 10000H)
  1609. END
  1610. END EmAddSp ;
  1611. PROCEDURE EmPushBp () ;
  1612. BEGIN
  1613. Ebyte (55H)
  1614. END EmPushBp ;
  1615. PROCEDURE EmLeave () ;
  1616. BEGIN
  1617. Ebyte (0C9H)
  1618. END EmLeave ;
  1619. PROCEDURE EmRet () ;
  1620. BEGIN
  1621. Ebyte (0C3H)
  1622. END EmRet ;
  1623. (* ---------------------------------------------------------------- *)
  1624. (* symbol table *)
  1625. (* ---------------------------------------------------------------- *)
  1626. PROCEDURE NameEq (a : ARRAY OF CHAR ; b : ARRAY OF CHAR) : BOOLEAN ;
  1627. VAR i : CARDINAL ;
  1628. BEGIN
  1629. i := 0 ;
  1630. LOOP
  1631. IF i > HIGH (a) THEN
  1632. RETURN FALSE
  1633. END ;
  1634. IF i > HIGH (b) THEN
  1635. RETURN FALSE
  1636. END ;
  1637. IF a [i] # b [i] THEN
  1638. RETURN FALSE
  1639. END ;
  1640. IF a [i] = 0C THEN
  1641. RETURN TRUE
  1642. END ;
  1643. INC (i)
  1644. END
  1645. END NameEq ;
  1646. PROCEDURE CopyStr (VAR dst : ARRAY OF CHAR ; src : ARRAY OF CHAR) ;
  1647. VAR i : CARDINAL ;
  1648. BEGIN
  1649. i := 0 ;
  1650. LOOP
  1651. IF i > HIGH (dst) THEN
  1652. dst [HIGH (dst)] := 0C ;
  1653. RETURN
  1654. END ;
  1655. IF i > HIGH (src) THEN
  1656. dst [i] := 0C ;
  1657. RETURN
  1658. END ;
  1659. dst [i] := src [i] ;
  1660. IF src [i] = 0C THEN
  1661. RETURN
  1662. END ;
  1663. INC (i)
  1664. END
  1665. END CopyStr ;
  1666. PROCEDURE CopyWord (name : ARRAY OF CHAR) ;
  1667. (* stash current word into global wrd (uppercased) *)
  1668. VAR i : CARDINAL ;
  1669. BEGIN
  1670. i := 0 ;
  1671. LOOP
  1672. IF i > HIGH (name) THEN
  1673. wrd [i] := 0C ;
  1674. RETURN
  1675. END ;
  1676. IF i > MaxName THEN
  1677. wrd [MaxName] := 0C ;
  1678. RETURN
  1679. END ;
  1680. IF name [i] = 0C THEN
  1681. wrd [i] := 0C ;
  1682. RETURN
  1683. END ;
  1684. wrd [i] := Upper (name [i]) ;
  1685. INC (i)
  1686. END
  1687. END CopyWord ;
  1688. PROCEDURE SaveWord (VAR dst : ARRAY OF CHAR) ;
  1689. (* copy wrd into dst *)
  1690. VAR i : CARDINAL ;
  1691. BEGIN
  1692. i := 0 ;
  1693. LOOP
  1694. IF i > HIGH (dst) THEN
  1695. dst [HIGH (dst)] := 0C ;
  1696. RETURN
  1697. END ;
  1698. IF i > MaxName THEN
  1699. dst [MaxName] := 0C ;
  1700. RETURN
  1701. END ;
  1702. dst [i] := wrd [i] ;
  1703. IF wrd [i] = 0C THEN
  1704. RETURN
  1705. END ;
  1706. INC (i)
  1707. END
  1708. END SaveWord ;
  1709. PROCEDURE NumToName (n : CARDINAL ; VAR dst : ARRAY OF CHAR) ;
  1710. VAR buf : ARRAY [0..9] OF CHAR ;
  1711. i, j : CARDINAL ;
  1712. BEGIN
  1713. IF n = 0 THEN
  1714. dst [0] := '0' ;
  1715. dst [1] := 0C ;
  1716. RETURN
  1717. END ;
  1718. i := 0 ;
  1719. WHILE n > 0 DO
  1720. IF i <= 9 THEN
  1721. buf [i] := CHR (ORD ('0') + (n MOD 10)) ;
  1722. INC (i)
  1723. END ;
  1724. n := n DIV 10
  1725. END ;
  1726. j := 0 ;
  1727. WHILE i > 0 DO
  1728. DEC (i) ;
  1729. IF j <= HIGH (dst) THEN
  1730. dst [j] := buf [i] ;
  1731. INC (j)
  1732. END
  1733. END ;
  1734. IF j <= HIGH (dst) THEN
  1735. dst [j] := 0C
  1736. END
  1737. END NumToName ;
  1738. PROCEDURE NewSym (name : ARRAY OF CHAR ; tag : CARDINAL ;
  1739. cls, size, elem, off : CARDINAL ; v : LONGINT ;
  1740. local : BOOLEAN) : CARDINAL ;
  1741. VAR e : SymEntry ;
  1742. BEGIN
  1743. IF symTop >= MaxSym THEN
  1744. Err (ECompOvf) ;
  1745. RETURN 0
  1746. END ;
  1747. CopyWord (name) ;
  1748. CopyStr (e.name, wrd) ;
  1749. e.tag := tag ;
  1750. e.cls := cls ;
  1751. e.size := size ;
  1752. e.elem := elem ;
  1753. e.off := off ;
  1754. e.lval := v ;
  1755. e.lo := 0 ;
  1756. e.hi := 0 ;
  1757. e.fldFirst := 0 ;
  1758. e.fldLast := 0 ;
  1759. e.arrIdx := 0 ;
  1760. e.parmFirst := 0 ;
  1761. e.parmCount := 0 ;
  1762. e.isKbd := FALSE ;
  1763. e.isVar := FALSE ;
  1764. e.level := lexnest ;
  1765. e.local := local ;
  1766. e.parent := 0 ;
  1767. e.staticOff := 0 ;
  1768. e.resvar := 0 ;
  1769. e.goPos := 0 ;
  1770. e.defnd := FALSE ;
  1771. e.fwd := FALSE ;
  1772. symtab [symTop] := e ;
  1773. INC (symTop) ;
  1774. RETURN symTop - 1
  1775. END NewSym ;
  1776. PROCEDURE Search (nm : ARRAY OF CHAR ; VAR idx : CARDINAL) : BOOLEAN ;
  1777. (* find nm among symbols visible at the current lexical level *)
  1778. VAR p : CARDINAL ;
  1779. BEGIN
  1780. CopyWord (nm) ;
  1781. p := symTop ;
  1782. WHILE p > 0 DO
  1783. DEC (p) ;
  1784. IF symtab [p].level <= lexnest THEN
  1785. IF NameEq (symtab [p].name, wrd) THEN
  1786. idx := p ;
  1787. RETURN TRUE
  1788. END
  1789. END
  1790. END ;
  1791. RETURN FALSE
  1792. END Search ;
  1793. PROCEDURE DupTest (nm : ARRAY OF CHAR) ;
  1794. (* A name may be declared twice only in the SAME scope. This used to reject
  1795. any name Search could see, so a parameter could not shadow a global - legal
  1796. ISO Pascal, and CALC.INC's `procedure IBMCh(var Ch: Char)' does exactly
  1797. that (`Ch' is a global). Same-level duplicates stay errors: two parameters
  1798. in one list, two locals, or two globals. *)
  1799. VAR i : CARDINAL ;
  1800. BEGIN
  1801. IF Search (nm, i) THEN
  1802. IF symtab [i].level = lexnest THEN
  1803. Err (EUnknown)
  1804. END
  1805. END
  1806. END DupTest ;
  1807. PROCEDURE HideLocals (from : CARDINAL) ;
  1808. (* Make every symbol from index `from' up invisible to everything outside the
  1809. procedure that declared it.
  1810. Search accepts a symbol when its level is <= lexnest, and every procedure
  1811. body is compiled at the same depth (lexnest 1), so a finished procedure's
  1812. parameters stayed visible to the NEXT procedure: a second `a : integer'
  1813. was a duplicate (err 41) and an unqualified `a' inside procedure two read
  1814. procedure one's argument. Level 0FFFFH fails `level <= lexnest' at every
  1815. depth a later procedure can be at, and by the time this runs the body that
  1816. could still legitimately see them is finished.
  1817. The symbols are relabelled, not popped: symtab[old].resvar holds an INDEX,
  1818. and a function's result variable is one of the entries being hidden. *)
  1819. VAR p : CARDINAL ;
  1820. BEGIN
  1821. p := from ;
  1822. WHILE p < symTop DO
  1823. symtab [p].level := 0FFFFH ;
  1824. INC (p)
  1825. END
  1826. END HideLocals ;
  1827. (* ---------------------------------------------------------------- *)
  1828. (* lexer *)
  1829. (* ---------------------------------------------------------------- *)
  1830. PROCEDURE InitKeys () ;
  1831. VAR i : CARDINAL ;
  1832. BEGIN
  1833. FOR i := 0 TO 42 DO
  1834. kTk [i] := 0 ;
  1835. kName [i] [0] := 0C
  1836. END ;
  1837. kName [1] := "PROGRAM" ; kTk [1] := TkProgram ;
  1838. kName [2] := "BEGIN" ; kTk [2] := TkBegin ;
  1839. kName [3] := "END" ; kTk [3] := TkEnd ;
  1840. kName [4] := "IF" ; kTk [4] := TkIf ;
  1841. kName [5] := "THEN" ; kTk [5] := TkThen ;
  1842. kName [6] := "ELSE" ; kTk [6] := TkElse ;
  1843. kName [7] := "WHILE" ; kTk [7] := TkWhile ;
  1844. kName [8] := "DO" ; kTk [8] := TkDo ;
  1845. kName [9] := "REPEAT" ; kTk [9] := TkRepeat ;
  1846. kName [10] := "UNTIL" ; kTk [10] := TkUntil ;
  1847. kName [11] := "FOR" ; kTk [11] := TkFor ;
  1848. kName [12] := "TO" ; kTk [12] := TkTo ;
  1849. kName [13] := "DOWNTO" ; kTk [13] := TkDownto ;
  1850. kName [14] := "CASE" ; kTk [14] := TkCase ;
  1851. kName [15] := "OF" ; kTk [15] := TkOf ;
  1852. kName [16] := "GOTO" ; kTk [16] := TkGoto ;
  1853. kName [17] := "EXIT" ; kTk [17] := TkExit ;
  1854. kName [18] := "WITH" ; kTk [18] := TkWith ;
  1855. kName [19] := "VAR" ; kTk [19] := TkVar ;
  1856. kName [20] := "CONST" ; kTk [20] := TkConst ;
  1857. kName [21] := "TYPE" ; kTk [21] := TkType ;
  1858. kName [22] := "LABEL" ; kTk [22] := TkLabel ;
  1859. kName [23] := "PROCEDURE" ; kTk [23] := TkProcedure ;
  1860. kName [24] := "FUNCTION" ; kTk [24] := TkFunction ;
  1861. kName [25] := "NIL" ; kTk [25] := TkNil ;
  1862. kName [26] := "AND" ; kTk [26] := TkAnd ;
  1863. kName [27] := "OR" ; kTk [27] := TkOr ;
  1864. kName [28] := "NOT" ; kTk [28] := TkNot ;
  1865. kName [29] := "DIV" ; kTk [29] := TkDiv ;
  1866. kName [30] := "MOD" ; kTk [30] := TkMod ;
  1867. kName [31] := "IN" ; kTk [31] := TkIn ;
  1868. kName [32] := "FILE" ; kTk [32] := TkFile ;
  1869. kName [33] := "TEXT" ; kTk [33] := TkText ;
  1870. kName [34] := "RECORD" ; kTk [34] := TkRecord ;
  1871. kName [35] := "ARRAY" ; kTk [35] := TkArray ;
  1872. kName [36] := "SET" ; kTk [36] := TkSet ;
  1873. kName [37] := "PACKED" ; kTk [37] := TkPacked ;
  1874. kName [38] := "FORWARD" ; kTk [38] := TkForward ;
  1875. kName [39] := "EXTERNAL" ; kTk [39] := TkExternal ;
  1876. kName [40] := "ABSOLUTE" ; kTk [40] := TkAbsolute ;
  1877. kName [41] := "OVERLAY" ; kTk [41] := TkOverlay ;
  1878. kName [42] := "STRING" ; kTk [42] := TkString
  1879. END InitKeys ;
  1880. PROCEDURE IsBlank (ch : CHAR) : BOOLEAN ;
  1881. BEGIN
  1882. RETURN (ch = ' ') OR (ORD (ch) = 09H) OR (ORD (ch) = 0DH) OR (ORD (ch) = 0AH) OR (ORD (ch) = 0CH)
  1883. END IsBlank ;
  1884. PROCEDURE Skip () ;
  1885. (* blanks, comments { } and (* *), compiler directives {$ } / (*$ *)
  1886. letter + sign toggles rng/chk. Include directives are already gone by the
  1887. time Skip runs: ExpandIncludes splices them into the buffer first, so the
  1888. {$I file} spelling never reaches here. {$I+}/{$I-} still do, and stay the
  1889. I/O-check toggle, exactly as TPSRC10 cdirec splits them on the sign. *)
  1890. VAR ch : CHAR ;
  1891. letter : CHAR ;
  1892. BEGIN
  1893. WHILE NOT abortFac DO
  1894. WHILE IsBlank (CurCh ()) DO
  1895. ch := GetCh ()
  1896. END ;
  1897. IF CurCh () = '{' THEN
  1898. ch := GetCh () ;
  1899. IF CurCh () = '$' THEN
  1900. ch := GetCh () ;
  1901. letter := Upper (GetCh ()) ;
  1902. IF CurCh () = '+' THEN
  1903. ch := GetCh () ;
  1904. IF letter = 'I' THEN dirs.chk := TRUE END ;
  1905. IF letter = 'R' THEN dirs.rng := TRUE END
  1906. ELSIF CurCh () = '-' THEN
  1907. ch := GetCh () ;
  1908. IF letter = 'I' THEN dirs.chk := FALSE END ;
  1909. IF letter = 'R' THEN dirs.rng := FALSE END
  1910. END
  1911. END ;
  1912. WHILE (CurCh () # '}') AND (CurCh () # 0C) DO
  1913. ch := GetCh ()
  1914. END ;
  1915. IF CurCh () = '}' THEN
  1916. ch := GetCh ()
  1917. END
  1918. ELSIF (CurCh () = '(') AND (PeekAhead (1) = '*') THEN
  1919. ch := GetCh () ;
  1920. ch := GetCh () ;
  1921. IF CurCh () = '$' THEN
  1922. ch := GetCh () ;
  1923. letter := Upper (GetCh ()) ;
  1924. IF CurCh () = '+' THEN
  1925. ch := GetCh () ;
  1926. IF letter = 'I' THEN dirs.chk := TRUE END ;
  1927. IF letter = 'R' THEN dirs.rng := TRUE END
  1928. ELSIF CurCh () = '-' THEN
  1929. ch := GetCh () ;
  1930. IF letter = 'I' THEN dirs.chk := FALSE END ;
  1931. IF letter = 'R' THEN dirs.rng := FALSE END
  1932. END
  1933. END ;
  1934. LOOP
  1935. IF (CurCh () = '*') AND (PeekAhead (1) = ')') THEN
  1936. ch := GetCh () ;
  1937. ch := GetCh () ;
  1938. EXIT
  1939. END ;
  1940. IF CurCh () = 0C THEN
  1941. EXIT
  1942. END ;
  1943. ch := GetCh ()
  1944. END
  1945. ELSE
  1946. RETURN
  1947. END
  1948. END
  1949. END Skip ;
  1950. PROCEDURE GetWord () ;
  1951. (* read identifier into wrd (uppercased); next char must be alpha *)
  1952. VAR i : CARDINAL ;
  1953. ch : CHAR ;
  1954. BEGIN
  1955. i := 0 ;
  1956. ch := GetCh () ;
  1957. LOOP
  1958. IF i > MaxName THEN
  1959. wrd [MaxName] := 0C ;
  1960. RETURN
  1961. END ;
  1962. wrd [i] := Upper (ch) ;
  1963. INC (i) ;
  1964. ch := CurCh () ;
  1965. IF NOT AlphaNum (ch) THEN
  1966. wrd [i] := 0C ;
  1967. RETURN
  1968. END ;
  1969. ch := GetCh ()
  1970. END
  1971. END GetWord ;
  1972. PROCEDURE WddTok () : CARDINAL ;
  1973. (* map wrd -> keyword token *)
  1974. VAR i : CARDINAL ;
  1975. BEGIN
  1976. i := 1 ;
  1977. WHILE i <= 42 DO
  1978. IF kName [i] [0] # 0C THEN
  1979. IF NameEq (wrd, kName [i]) THEN
  1980. RETURN kTk [i]
  1981. END
  1982. END ;
  1983. INC (i)
  1984. END ;
  1985. RETURN TkNone
  1986. END WddTok ;
  1987. PROCEDURE DeclaresProc () : BOOLEAN ;
  1988. (* Does the REST of the source declare a PROCEDURE or a FUNCTION?
  1989. Needed because the declaration part is compiled BEFORE the main statement
  1990. part, so a procedure's code lands between the program prologue and the
  1991. main body - and nothing jumps over it. A program with a procedure
  1992. therefore ran off the end of the prologue, straight into the first
  1993. procedure, which read its argument out of an uninitialised frame and
  1994. returned to address 0. Every Pascal program containing a procedure was
  1995. broken; `t13_proc` compiled and was never executed, so nothing saw it.
  1996. The jump that fixes it has to be emitted BEFORE the declaration part, but
  1997. whether one is needed is only known AFTER - so the only honest options are
  1998. to emit it unconditionally (3 dead bytes in every program, and every code
  1999. size in expected.tsv moves) or to know the answer in advance. This is the
  2000. second: it scans ahead and puts srcPos back.
  2001. That is safe because the whole program is already in `src` and `srcPos` is
  2002. a plain index into it - the same trick PeekKw and KwAhead use. The scan
  2003. looks for the keywords anywhere in the remainder rather than tracking the
  2004. nesting of `begin`s, which is deliberately loose: a program with no
  2005. procedures that merely mentions the word in a string literal would get a
  2006. 3-byte jump to the next instruction, which is harmless, whereas tracking
  2007. the main `begin` against a procedure's `begin` would be a second parser
  2008. to get wrong. *)
  2009. VAR save : CARDINAL ;
  2010. found : BOOLEAN ;
  2011. tk : CARDINAL ;
  2012. ch : CHAR ;
  2013. BEGIN
  2014. save := srcPos ;
  2015. found := FALSE ;
  2016. tk := TkNone ; (* so the answer is defined if src is empty *)
  2017. (* Step over delimiters as well as blanks. Stopping at the first
  2018. non-letter looked reasonable and was wrong: `var x : integer ;` is full
  2019. of ':' and ';', so the scan gave up inside the variable section and
  2020. never reached the PROCEDURE. The loop ends at the end of the source,
  2021. not at the first punctuation. *)
  2022. WHILE (NOT found) AND (srcPos < srcLen) DO
  2023. Skip () ;
  2024. IF Alpha (CurCh ()) THEN
  2025. GetWord () ;
  2026. tk := WddTok () ;
  2027. IF (tk = TkProcedure) OR (tk = TkFunction) THEN
  2028. found := TRUE
  2029. END
  2030. ELSE
  2031. ch := GetCh () (* a ':' or ';' - step over it *)
  2032. END
  2033. END ;
  2034. srcPos := save ;
  2035. RETURN (tk = TkProcedure) OR (tk = TkFunction)
  2036. END DeclaresProc ;
  2037. PROCEDURE KwAhead (word : ARRAY OF CHAR) : BOOLEAN ;
  2038. (* does the next token (past blanks/comments) equal the keyword 'word',
  2039. without consuming it? srcPos is saved and restored. *)
  2040. VAR save : CARDINAL ;
  2041. k : BOOLEAN ;
  2042. BEGIN
  2043. save := srcPos ;
  2044. Skip () ;
  2045. k := FALSE ;
  2046. IF Alpha (CurCh ()) THEN
  2047. GetWord () ;
  2048. k := NameEq (wrd, word)
  2049. END ;
  2050. srcPos := save ;
  2051. RETURN k
  2052. END KwAhead ;
  2053. PROCEDURE PeekKw (VAR tok : CARDINAL) ;
  2054. (* peek at the next keyword token without consuming it *)
  2055. VAR i : CARDINAL ;
  2056. BEGIN
  2057. tok := TkNone ;
  2058. i := 1 ;
  2059. WHILE i <= 42 DO
  2060. IF kName [i] [0] # 0C THEN
  2061. IF KwAhead (kName [i]) THEN
  2062. tok := kTk [i] ;
  2063. RETURN
  2064. END
  2065. END ;
  2066. INC (i)
  2067. END
  2068. END PeekKw ;
  2069. PROCEDURE MatchKey (VAR tok : CARDINAL) : BOOLEAN ;
  2070. (* skip; if next symbol is a word, read it into wrd and set its token.
  2071. Returns TRUE when a word was read (tok = TkNone for plain ids). *)
  2072. BEGIN
  2073. tok := TkNone ;
  2074. Skip () ;
  2075. IF NOT Alpha (CurCh ()) THEN
  2076. RETURN FALSE
  2077. END ;
  2078. GetWord () ;
  2079. tok := WddTok () ;
  2080. RETURN TRUE
  2081. END MatchKey ;
  2082. PROCEDURE MatchDelim (ch : CHAR) : BOOLEAN ;
  2083. BEGIN
  2084. Skip () ;
  2085. IF CurCh () = ch THEN
  2086. DropCh (GetCh ()) ;
  2087. RETURN TRUE
  2088. END ;
  2089. RETURN FALSE
  2090. END MatchDelim ;
  2091. PROCEDURE MatchAssign () : BOOLEAN ;
  2092. BEGIN
  2093. Skip () ;
  2094. IF (CurCh () = ':') AND (PeekAhead (1) = '=') THEN
  2095. DropCh (GetCh ()) ;
  2096. DropCh (GetCh ()) ;
  2097. RETURN TRUE
  2098. END ;
  2099. RETURN FALSE
  2100. END MatchAssign ;
  2101. PROCEDURE MatchRange () : BOOLEAN ;
  2102. BEGIN
  2103. Skip () ;
  2104. IF (CurCh () = '.') AND (PeekAhead (1) = '.') THEN
  2105. DropCh (GetCh ()) ;
  2106. DropCh (GetCh ()) ;
  2107. RETURN TRUE
  2108. END ;
  2109. RETURN FALSE
  2110. END MatchRange ;
  2111. PROCEDURE ExpectDelim (ch : CHAR ; n : CARDINAL) ;
  2112. BEGIN
  2113. Skip () ;
  2114. IF CurCh () = ch THEN
  2115. DropCh (GetCh ())
  2116. ELSE
  2117. Err (n)
  2118. END
  2119. END ExpectDelim ;
  2120. PROCEDURE HexVal (ch : CHAR) : CARDINAL ;
  2121. BEGIN
  2122. IF (ch >= '0') AND (ch <= '9') THEN
  2123. RETURN ORD (ch) - ORD ('0')
  2124. ELSIF (ch >= 'A') AND (ch <= 'F') THEN
  2125. RETURN ORD (ch) - ORD ('A') + 10
  2126. END ;
  2127. RETURN ORD (ch) - ORD ('a') + 10
  2128. END HexVal ;
  2129. PROCEDURE RdIntConst (VAR v : LONGINT) ;
  2130. (* bare integer constant; current char is digit or '$' *)
  2131. VAR acc : LONGINT ;
  2132. BEGIN
  2133. acc := 0 ;
  2134. IF CurCh () = '$' THEN
  2135. DropCh (GetCh ()) ;
  2136. WHILE IsHexCh (CurCh ()) DO
  2137. acc := acc * 16 + VAL (LONGINT, HexVal (CurCh ())) ;
  2138. DropCh (GetCh ())
  2139. END
  2140. ELSE
  2141. WHILE Digit (CurCh ()) DO
  2142. acc := acc * 10 + VAL (LONGINT, ORD (CurCh ()) - ORD ('0')) ;
  2143. DropCh (GetCh ())
  2144. END
  2145. END ;
  2146. v := acc
  2147. END RdIntConst ;
  2148. PROCEDURE StrNew (first : CARDINAL ; hasFirst : BOOLEAN ) : CARDINAL ;
  2149. (* Open a pool slot for a string literal, optionally pre-seeded with its
  2150. first character.
  2151. RdConst has to read the first character before it can tell a one-character
  2152. literal from a longer one - the "is the next character another quote?"
  2153. test only makes sense once something has been read - so the seeding has to
  2154. happen here and has to advance strTop. Leaving strTop alone and letting the
  2155. caller write the character by hand is a trap: strTop is the next FREE byte,
  2156. so the first StrPut lands on top of the seeded character and overwrites it.
  2157. (That bug shipped the literal 'hi' as 69 00 - 'i' then NUL.) *)
  2158. VAR x : CARDINAL ;
  2159. BEGIN
  2160. IF strCnt > HIGH (strOff) THEN
  2161. Err (ECompOvf) ; (* too many literals in one unit *)
  2162. RETURN 0
  2163. END ;
  2164. x := strCnt ;
  2165. strOff [x] := strTop ;
  2166. strLen [x] := 0 ;
  2167. INC (strCnt) ;
  2168. IF hasFirst THEN
  2169. strPool [strTop] := CHR (first) ;
  2170. INC (strTop) ;
  2171. strLen [x] := 1
  2172. END ;
  2173. rdStrX := x ;
  2174. RETURN x
  2175. END StrNew ;
  2176. PROCEDURE StrPut (x : CARDINAL ) ;
  2177. (* append the current source character to pool slot x *)
  2178. BEGIN
  2179. IF x > HIGH (strOff) THEN
  2180. RETURN
  2181. END ;
  2182. IF strTop > HIGH (strPool) THEN
  2183. Err (ECompOvf) ; (* literal longer than the pool *)
  2184. RETURN
  2185. END ;
  2186. strPool [strTop] := CurCh () ;
  2187. INC (strTop) ;
  2188. INC (strLen [x])
  2189. END StrPut ;
  2190. PROCEDURE RdConst (VAR v : LONGINT ; VAR cls : CARDINAL ;
  2191. VAR isStr : BOOLEAN) ;
  2192. (* scalar or string constant. A string literal's text is collected into the
  2193. pool and its slot index left in rdStrX; a single-character literal stays a
  2194. TScalar holding its character code, which is what "c := 'a'" wants. *)
  2195. CONST q = AposC ;
  2196. VAR ch : CHAR ;
  2197. idx : CARDINAL ;
  2198. BEGIN
  2199. isStr := FALSE ;
  2200. cls := TScalar ;
  2201. v := 0 ;
  2202. Skip () ;
  2203. IF CurCh () = '-' THEN
  2204. (* Signed ordinal constant, needed for array bounds such as
  2205. `array[-17..42] of real'. ParseExpr already handles unary minus in
  2206. expressions; RdConst is the constant-only path and had no sign. *)
  2207. DropCh (GetCh ()) ;
  2208. RdConst (v, cls, isStr) ;
  2209. IF NOT isStr THEN
  2210. v := 0 - v
  2211. END ;
  2212. RETURN
  2213. END ;
  2214. IF CurCh () = '$' THEN
  2215. RdIntConst (v) ;
  2216. cls := TScalar
  2217. ELSIF Digit (CurCh ()) THEN
  2218. RdIntConst (v) ;
  2219. IF (CurCh () = '.') AND (Digit (PeekAhead (1))) THEN
  2220. cls := TReal ;
  2221. DropCh (GetCh ())
  2222. END ;
  2223. IF (CurCh () = 'E') OR (CurCh () = 'e') THEN
  2224. cls := TReal ;
  2225. DropCh (GetCh ())
  2226. END ;
  2227. IF cls = TReal THEN
  2228. WHILE AlphaNum (CurCh ()) OR (CurCh () = '.') OR (CurCh () = '-')
  2229. OR (CurCh () = '+') DO
  2230. DropCh (GetCh ())
  2231. END
  2232. END
  2233. ELSIF CurCh () = '^' THEN
  2234. (* TP3 caret character constant, unquoted: ^M is CR (13), ^[ is ESC
  2235. (27), ^@ is 0. The value is the upper-cased character minus 64, so
  2236. the +256-MOD-256 keeps ^@ and any control character from wrapping. *)
  2237. DropCh (GetCh ()) ;
  2238. IF CurCh () = 0C THEN
  2239. Err (EUnknown)
  2240. ELSE
  2241. ch := Upper (GetCh ()) ;
  2242. v := VAL (LONGINT, (ORD (ch) + 256 - 64) MOD 256) ;
  2243. cls := TScalar
  2244. END
  2245. ELSIF CurCh () = '#' THEN
  2246. (* TP3 decimal character constant: #27, #255. *)
  2247. DropCh (GetCh ()) ;
  2248. IF Digit (CurCh ()) THEN
  2249. RdIntConst (v) ;
  2250. cls := TScalar
  2251. ELSE
  2252. Err (EUnknown)
  2253. END
  2254. ELSIF Alpha (CurCh ()) THEN
  2255. (* TRUE / FALSE, or a NAMED constant - an enumerator (`[Constant,
  2256. Formula]'), or any earlier const. A constant expression may name a
  2257. constant, and set literals and `case' labels are full of enumerators. *)
  2258. GetWord () ;
  2259. IF NameEq (wrd, "TRUE") THEN
  2260. v := 1 ;
  2261. cls := TBool
  2262. ELSIF NameEq (wrd, "FALSE") THEN
  2263. v := 0 ;
  2264. cls := TBool
  2265. ELSIF Search (wrd, idx) AND (symtab [idx].tag = KConst) THEN
  2266. v := symtab [idx].lval ;
  2267. cls := symtab [idx].cls
  2268. ELSE
  2269. Err (EUnknown)
  2270. END
  2271. ELSIF ORD (CurCh ()) = q THEN
  2272. DropCh (GetCh ()) ;
  2273. IF ORD (CurCh ()) = q THEN
  2274. (* '' - the empty string. It used to be reported as the scalar 39,
  2275. so writeln('') printed a single quote mark. It is a string of
  2276. length zero, and an inline zero-length literal is exactly what the
  2277. runtime's JCXZ path is for. *)
  2278. DropCh (GetCh ()) ;
  2279. isStr := TRUE ;
  2280. cls := TString ;
  2281. v := 0 ;
  2282. rdStrX := StrNew (0, FALSE)
  2283. ELSIF (CurCh () = 0C) OR ((ORD (CurCh ()) = 0DH)) OR ((ORD (CurCh ()) = 0AH)) THEN
  2284. Err (EUnknown)
  2285. ELSE
  2286. v := VAL (LONGINT, ORD (GetCh ())) ;
  2287. cls := TScalar ;
  2288. IF ORD (CurCh ()) = q THEN
  2289. DropCh (GetCh ())
  2290. ELSE
  2291. isStr := TRUE ;
  2292. cls := TString ;
  2293. (* Scan to the closing quote. NB: the loop condition must test
  2294. the CURRENT character and only the current character. The
  2295. obvious-looking "while CurCh # quote" with a
  2296. "if PeekAhead(1) = quote then consume two" body is wrong:
  2297. consuming the quote moves the cursor past it, so the next
  2298. condition test sees the character AFTER the literal, is
  2299. satisfied, and the scan runs on to end-of-buffer - which
  2300. silently eats the rest of the program and makes every later
  2301. error point at end-of-file. Stop on the quote itself, and
  2302. treat a doubled quote as one embedded quote character.
  2303. The first character is already gone - it was read into v above -
  2304. so the pool slot is pre-seeded with it. *)
  2305. rdStrX := StrNew (VAL (CARDINAL, v), TRUE) ;
  2306. LOOP
  2307. IF ORD (CurCh ()) = q THEN
  2308. IF ORD (PeekAhead (1)) = q THEN
  2309. StrPut (rdStrX) ; (* '' inside *)
  2310. DropCh (GetCh ()) ; DropCh (GetCh ())
  2311. ELSE
  2312. EXIT (* closing quote *)
  2313. END
  2314. ELSIF (CurCh () = 0C) OR (ORD (CurCh ()) = 0DH) THEN
  2315. Err (EUnknown) ; (* unterminated *)
  2316. EXIT
  2317. ELSE
  2318. StrPut (rdStrX) ;
  2319. DropCh (GetCh ())
  2320. END
  2321. END ;
  2322. IF ORD (CurCh ()) = q THEN
  2323. DropCh (GetCh ())
  2324. END
  2325. END
  2326. END
  2327. ELSE
  2328. Err (EUnknown)
  2329. END
  2330. END RdConst ;
  2331. (* ---------------------------------------------------------------- *)
  2332. (* forward declarations *)
  2333. (* ---------------------------------------------------------------- *)
  2334. PROCEDURE ParseExpr (VAR r : ERes) ; FORWARD ;
  2335. PROCEDURE ParseAdd (VAR r : ERes) ; FORWARD ;
  2336. PROCEDURE ParseMul (VAR r : ERes) ; FORWARD ;
  2337. PROCEDURE ParseNeg (VAR r : ERes) ; FORWARD ;
  2338. PROCEDURE ParseAtom (VAR r : ERes) ; FORWARD ;
  2339. PROCEDURE ParseType (VAR cls, size, elem : CARDINAL) ; FORWARD ;
  2340. PROCEDURE Statmnt () ; FORWARD ;
  2341. PROCEDURE ParseCall (idx : CARDINAL) ; FORWARD ;
  2342. PROCEDURE ParseCallArgs (idx : CARDINAL) ; FORWARD ;
  2343. PROCEDURE EmCallMost (idx, cleanup : CARDINAL) ; FORWARD ;
  2344. PROCEDURE SlotSize (cls, size : CARDINAL) : CARDINAL ; FORWARD ;
  2345. PROCEDURE ParamSlotBytes (e : SymEntry) : CARDINAL ; FORWARD ;
  2346. PROCEDURE NewScratchReal () : CARDINAL ; FORWARD ;
  2347. PROCEDURE EmPushRealAddr (r : ERes) ; FORWARD ;
  2348. PROCEDURE EmCopyRealTo (dstOff : CARDINAL ; r : ERes) ; FORWARD ;
  2349. PROCEDURE RealBin (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ; FORWARD ;
  2350. PROCEDURE RealUnary (ent : CARDINAL ; VAR r : ERes) ; FORWARD ;
  2351. PROCEDURE IntToMbf (v : LONGINT ; VAR b : ARRAY OF CHAR) ; FORWARD ;
  2352. (* ---------------------------------------------------------------- *)
  2353. (* expressions (TPSRC9) *)
  2354. (* ---------------------------------------------------------------- *)
  2355. PROCEDURE VarSize (r : ERes) : CARDINAL ;
  2356. (* The width of a kind-1 value. An element (`b[i]') or a field (`r.f') has a
  2357. size of its own, which is NOT its variable's; when the parser has recorded
  2358. one, it wins. Otherwise the variable's declared size is the answer. *)
  2359. BEGIN
  2360. IF r.sz > 0 THEN
  2361. RETURN r.sz
  2362. END ;
  2363. RETURN symtab [r.idx].size
  2364. END VarSize ;
  2365. PROCEDURE IsCharArray (r : ERes) : BOOLEAN ;
  2366. (* A fixed `array[..] of char', as opposed to a counted STRING. *)
  2367. BEGIN
  2368. IF (r.cls # TArray) OR (r.arrIdx = 0) THEN
  2369. RETURN FALSE
  2370. END ;
  2371. RETURN arrs [r.arrIdx - 1].ecls = TChar
  2372. END IsCharArray ;
  2373. PROCEDURE LoadAtom (VAR r : ERes) ;
  2374. (* load value of r into AX (folding constants).
  2375. A string literal is refused here, and this is the single place that has to:
  2376. assignment, IF, WHILE, FOR, REPEAT, CASE, array subscripts and every
  2377. operator all reach their operand through LoadAtom, and none of them can use
  2378. a counted string where a 16-bit word is expected. Reporting it here rather
  2379. than in the parser means writeln('hi') still works - IoCall handles a
  2380. literal before it ever calls LoadAtom. *)
  2381. VAR emsz : CARDINAL ;
  2382. BEGIN
  2383. IF r.cls = TString THEN
  2384. Err (ENoLib) ; (* string value used as a number *)
  2385. r.kind := 2 ;
  2386. RETURN
  2387. END ;
  2388. IF r.cls = TSet THEN
  2389. (* A set is bytes, not a 16-bit value. It is legal as the right operand
  2390. of `in' (and later as a whole-value operand of `+'/`*'/'='/assignment),
  2391. all of which handle it BEFORE LoadAtom - but never as a number, which
  2392. would otherwise load its first two bytes as if it were an integer. *)
  2393. Err (ENoLib) ;
  2394. r.kind := 2 ;
  2395. RETURN
  2396. END ;
  2397. IF r.kind = 5 THEN
  2398. (* An element whose address ParseSub computed into DI (LEA DI,[base] /
  2399. ADD DI,AX): a string character, or an array element of one or two
  2400. bytes. Zeroing AH keeps a one-byte value a clean 16-bit char. The
  2401. read must happen HERE, before anything else is emitted: the caller
  2402. reaches this immediately after parsing the subscript, or - for a
  2403. binary operator - SaveLeft has already materialised it, so DI is
  2404. still the element's address. *)
  2405. IF VarSize (r) > 2 THEN
  2406. Err (ENoLib) ; (* a whole record/array as a value *)
  2407. r.kind := 2 ;
  2408. RETURN
  2409. END ;
  2410. IF VarSize (r) = 2 THEN
  2411. EmLdAxDi ()
  2412. ELSE
  2413. EmLdAlDi () ;
  2414. EmMovAh0 ()
  2415. END ;
  2416. r.kind := 2 ;
  2417. RETURN
  2418. END ;
  2419. IF r.kind = 0 THEN
  2420. EmMovAxi (W16 (r.imm)) ;
  2421. r.kind := 2
  2422. ELSIF r.kind = 1 THEN
  2423. IF symtab [r.idx].isVar THEN
  2424. (* A VAR parameter: [BP+off] holds the address, and the value is one
  2425. level down. r.boff is necessarily 0 - a by-reference parameter is
  2426. a scalar in this increment, so it has nothing to subscript. *)
  2427. IF r.boff # 0 THEN
  2428. Err (ENoLib) ;
  2429. r.kind := 2 ;
  2430. RETURN
  2431. END ;
  2432. EmLoadVarIndSym (r.idx, VarSize (r)) ;
  2433. r.kind := 2 ;
  2434. RETURN
  2435. END ;
  2436. IF (VarSize (r) > 2) AND NOT
  2437. ((r.cls = TChar) AND (symtab [r.idx].cls = TString)) THEN
  2438. Err (ENoLib)
  2439. ELSE
  2440. (* s[i] indexes one byte of a string, not the string, so its width is
  2441. one whatever the variable's declared width is. *)
  2442. emsz := VarSize (r) ;
  2443. IF (r.cls = TChar) AND (symtab [r.idx].cls = TString) THEN
  2444. emsz := 1
  2445. END ;
  2446. LoadSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, emsz) ;
  2447. IF emsz = 1 THEN
  2448. EmMovAh0 ()
  2449. END ;
  2450. r.kind := 2
  2451. END
  2452. END
  2453. END LoadAtom ;
  2454. PROCEDURE ParseSub (VAR r : ERes) ;
  2455. (* consume '[' expr ']' while present.
  2456. A CONSTANT index folds into the base offset boff, exactly as before. A
  2457. VARIABLE index on a STRING cannot fold - off+i is a run-time value - so it
  2458. is computed once here into DI and r.kind drops to 5, which is how LoadAtom,
  2459. Assignment and IoCall are told the element's address is in DI. Arrays
  2460. still accept only a constant index: a variable array subscript needs the
  2461. element-size scaling that a one-byte string element does not, and it is
  2462. item 2. *)
  2463. VAR t : ERes ;
  2464. ai, fi, nd, k, st, p : CARDINAL ;
  2465. coff : LONGINT ;
  2466. accOnStack, haveVar, more : BOOLEAN ;
  2467. baseDI, found : BOOLEAN ;
  2468. BEGIN
  2469. LOOP
  2470. Skip () ;
  2471. IF ((r.cls = TPtr) OR ((r.kind = 1) AND (symtab [r.idx].cls = TPtr)))
  2472. AND (CurCh () = '^') THEN
  2473. (* Dereference after a variable, a field or an array element. The
  2474. pointer word may be an ordinary symbol (kind 1) or the element
  2475. address already in DI (kind 5, e.g. Friend[Index]^). *)
  2476. DropCh (GetCh ()) ;
  2477. p := r.ptrElem ;
  2478. IF p = 0 THEN
  2479. p := symtab [r.idx].elem
  2480. END ;
  2481. IF r.kind = 5 THEN
  2482. EmLdAxDi () ;
  2483. ELSE
  2484. LoadSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, 2)
  2485. END ;
  2486. EmMovDiAx () ;
  2487. r.idx := p ;
  2488. r.kind := 5 ;
  2489. r.boff := 0 ;
  2490. r.cls := symtab [p].cls ;
  2491. r.sz := symtab [p].size ;
  2492. r.fldFirst := symtab [p].fldFirst ;
  2493. r.fldLast := symtab [p].fldLast ;
  2494. r.arrIdx := symtab [p].arrIdx ;
  2495. r.ptrElem := symtab [p].elem
  2496. ELSIF CurCh () = '.' THEN
  2497. (* FIELD selection: `r.f'. The field's offset adds to the record
  2498. base's, and the value's class and WIDTH become the field's - which
  2499. is the whole reason a width rides on the expression (VarSize). *)
  2500. DropCh (GetCh ()) ;
  2501. Skip () ;
  2502. IF NOT Alpha (CurCh ()) THEN
  2503. Err (EUnknown) ;
  2504. RETURN
  2505. END ;
  2506. GetWord () ;
  2507. IF r.fldFirst >= r.fldLast THEN
  2508. Err (ESimpType) ; (* not a record *)
  2509. RETURN
  2510. END ;
  2511. fi := r.fldFirst ;
  2512. found := FALSE ;
  2513. WHILE (fi < r.fldLast) AND NOT found DO
  2514. IF NameEq (fields [fi].name, wrd) THEN
  2515. found := TRUE
  2516. ELSE
  2517. INC (fi)
  2518. END
  2519. END ;
  2520. IF NOT found THEN
  2521. Err (EUnknown) ;
  2522. RETURN
  2523. END ;
  2524. IF r.kind = 5 THEN
  2525. (* The element's address is in DI: a field is a fixed offset from
  2526. it. The array symbol already carries the element record's
  2527. field range (DefVar copies recFldFirst/recFldLast), which is
  2528. why the lookup above works for `Sheet[I,J].Field' too. *)
  2529. IF fields [fi].off # 0 THEN
  2530. EmAddDiImm (fields [fi].off)
  2531. END
  2532. ELSE
  2533. r.boff := W16 (VAL (LONGINT, r.boff)
  2534. + VAL (LONGINT, fields [fi].off)) ;
  2535. END ;
  2536. r.cls := fields [fi].cls ;
  2537. r.sz := fields [fi].size ;
  2538. r.fldFirst := fields [fi].fldFirst ;
  2539. r.fldLast := fields [fi].fldLast ;
  2540. r.arrIdx := fields [fi].arrIdx ;
  2541. r.ptrElem := fields [fi].ptrElem ;
  2542. IF r.cls = TSet THEN
  2543. (* ParseVar set setoff only when the SYMBOL itself was a set; for
  2544. an array/record the class was still TArray/TRecord then, so the
  2545. base offset has to be recorded now. *)
  2546. r.setoff := symtab [r.idx].off ;
  2547. r.setbase := fields [fi].elem ;
  2548. r.setsize := fields [fi].size
  2549. END
  2550. ELSIF CurCh () = '[' THEN
  2551. DropCh (GetCh ()) ;
  2552. IF r.cls = TString THEN
  2553. (* s[i]: a STRING is a length byte at s[0] followed by the
  2554. characters, so index i selects the one byte at off+i. A second
  2555. subscript on a character is not a type. *)
  2556. ParseExpr (t) ;
  2557. IF OK () THEN
  2558. IF r.cls = TChar THEN
  2559. Err (ESimpType) ;
  2560. RETURN
  2561. END ;
  2562. IF symtab [r.idx].isVar THEN
  2563. (* VAR string parameter: the base is the address in the
  2564. parameter slot, not LEA of the slot. Compute DI once for
  2565. both constant and variable indices, so LoadAtom and
  2566. Assignment see the same kind-5 character address they do
  2567. for an ordinary string. *)
  2568. LoadSymVar (r.idx, symtab [r.idx].off, 2) ;
  2569. EmMovDiAx () ;
  2570. IF t.kind = 0 THEN
  2571. EmAddDiImm ((r.boff + W16 (t.imm)) MOD 10000H)
  2572. ELSE
  2573. IF r.boff # 0 THEN
  2574. EmAddDiImm (r.boff)
  2575. END ;
  2576. LoadAtom (t) ;
  2577. EmAddDiAx ()
  2578. END ;
  2579. r.kind := 5
  2580. ELSIF t.kind = 0 THEN
  2581. (* constant index: fold it into boff, as an array's does, and
  2582. r.cls drops to TChar so every consumer below - LoadAtom,
  2583. Assignment, IoCall - sees a one-byte value instead of the
  2584. whole string. *)
  2585. r.boff := W16 (VAL (LONGINT, r.boff) + t.imm)
  2586. ELSE
  2587. (* Variable index. Compute the element address once, now:
  2588. the index is evaluated into AX (LoadAtom is a no-op for a
  2589. kind-2 value and re-reads [DI] for a nested subscript), then
  2590. DI := base + AX. This is TP3's indstr/ialoop shape (TPSRC9
  2591. 1100-1111, 1200-1229), which likewise holds the address in
  2592. DI until the byte is loaded or stored; a register is what
  2593. lets a later LoadAtom read [DI] and a later assignment PUSH
  2594. DI across its right-hand side. *)
  2595. LoadAtom (t) ;
  2596. LeaDiSym (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H) ;
  2597. EmAddDiAx () ;
  2598. r.kind := 5
  2599. END ;
  2600. r.cls := TChar ;
  2601. r.sz := 1
  2602. END ;
  2603. ExpectDelim (']', ENoSemi)
  2604. ELSIF r.arrIdx > 0 THEN
  2605. (* An array subscript LIST: one expression per dimension, comma
  2606. separated. The byte offset is
  2607. sum over k of (i[k] - lo[k]) * stride[k]
  2608. where stride[k] is the element size times the count of every
  2609. LATER dimension (row-major: the LAST dimension varies fastest).
  2610. A constant index folds into coff; a run-time one is scaled as
  2611. soon as it is parsed and summed on the STACK, because parsing
  2612. the next index may clobber AX and DI. *)
  2613. baseDI := (r.kind = 5) ;
  2614. ai := r.arrIdx - 1 ;
  2615. nd := 0 ;
  2616. coff := 0 ;
  2617. accOnStack := FALSE ;
  2618. haveVar := FALSE ;
  2619. LOOP
  2620. IF nd >= arrs [ai].n THEN
  2621. Err (ETypeErr) ;
  2622. RETURN
  2623. END ;
  2624. ParseExpr (t) ;
  2625. IF NOT OK () THEN
  2626. RETURN
  2627. END ;
  2628. Skip () ;
  2629. IF CurCh () = ':' THEN
  2630. (* TP3 segment:offset form in Mem[...]; the segment
  2631. expression is consumed and the offset is the index. *)
  2632. DropCh (GetCh ()) ;
  2633. ParseExpr (t) ;
  2634. IF NOT OK () THEN
  2635. RETURN
  2636. END
  2637. END ;
  2638. st := arrs [ai].elem ;
  2639. k := nd + 1 ;
  2640. WHILE k < arrs [ai].n DO
  2641. st := st * W16 (arrs [ai].hi [k] - arrs [ai].lo [k] + 1) ;
  2642. INC (k)
  2643. END ;
  2644. Skip () ;
  2645. more := CurCh () = ',' ;
  2646. IF t.kind = 0 THEN
  2647. coff := coff + (t.imm - arrs [ai].lo [nd])
  2648. * VAL (LONGINT, st)
  2649. ELSE
  2650. LoadAtom (t) ;
  2651. IF arrs [ai].lo [nd] # 0 THEN
  2652. EmMovCxi (W16 (arrs [ai].lo [nd])) ;
  2653. EmSubAxCx ()
  2654. END ;
  2655. IF st # 1 THEN
  2656. EmMovCxi (st) ;
  2657. EmMulAxCx ()
  2658. END ;
  2659. IF more THEN
  2660. (* the next dimension's expression will clobber AX: park
  2661. the running sum. A SINGLE index skips this entirely,
  2662. so 1-D code is byte-identical to the old path. *)
  2663. IF accOnStack THEN
  2664. EmPopCx () ;
  2665. EmAddAxCx () ;
  2666. EmPushAx ()
  2667. ELSE
  2668. EmPushAx () ;
  2669. accOnStack := TRUE
  2670. END
  2671. ELSE
  2672. (* last dimension: AX is the total *)
  2673. IF accOnStack THEN
  2674. EmPopCx () ;
  2675. EmAddAxCx () ;
  2676. accOnStack := FALSE
  2677. END ;
  2678. haveVar := TRUE
  2679. END
  2680. END ;
  2681. INC (nd) ;
  2682. IF more THEN
  2683. DropCh (GetCh ())
  2684. ELSE
  2685. EXIT
  2686. END
  2687. END ;
  2688. IF nd # arrs [ai].n THEN
  2689. (* fewer subscripts than dimensions: the value would be a
  2690. sub-array, which has no scalar representation *)
  2691. Err (ETypeErr) ;
  2692. RETURN
  2693. END ;
  2694. ExpectDelim (']', ENoSemi) ;
  2695. IF accOnStack THEN
  2696. (* the last dimensions were constant: the parked sum is still
  2697. on the stack, and AX is the total *)
  2698. EmPopAx () ;
  2699. accOnStack := FALSE
  2700. END ;
  2701. IF baseDI THEN
  2702. (* The base address is already in DI: a pointer dereference
  2703. (`Buffer^[i]'). Add the computed byte offset to it; a
  2704. constant-only index can use ADD DI,imm16. *)
  2705. IF haveVar THEN
  2706. EmAddDiAx ()
  2707. ELSE
  2708. IF coff # 0 THEN
  2709. EmAddDiImm (W16 (coff))
  2710. END
  2711. END ;
  2712. r.kind := 5
  2713. ELSIF haveVar THEN
  2714. LeaDiSym (r.idx, (symtab [r.idx].off + r.boff
  2715. + W16 (coff)) MOD 10000H) ;
  2716. EmAddDiAx () ;
  2717. r.kind := 5
  2718. ELSE
  2719. r.boff := W16 (VAL (LONGINT, r.boff) + coff)
  2720. END ;
  2721. r.sz := arrs [ai].elem ;
  2722. r.cls := arrs [ai].ecls ;
  2723. r.fldFirst := arrs [ai].fldFirst ;
  2724. r.fldLast := arrs [ai].fldLast ;
  2725. r.arrIdx := 0 ;
  2726. r.ptrElem := arrs [ai].elemType
  2727. ELSE
  2728. ParseExpr (t) ;
  2729. IF OK () THEN
  2730. IF t.kind # 0 THEN
  2731. Err (ENoLib)
  2732. ELSE
  2733. Err (ESimpType)
  2734. END
  2735. END ;
  2736. ExpectDelim (']', ENoSemi)
  2737. END
  2738. ELSE
  2739. RETURN
  2740. END
  2741. END
  2742. END ParseSub ;
  2743. PROCEDURE ParseVar (VAR r : ERes) ;
  2744. (* name [ '[' constExpr ']' ]* ; a function name maps to its result var *)
  2745. VAR idx, p : CARDINAL ;
  2746. BEGIN
  2747. IF NOT Search (wrd, idx) THEN
  2748. Err (EUnknown) ;
  2749. RETURN
  2750. END ;
  2751. IF symtab [idx].cls = TPtr THEN
  2752. Skip () ;
  2753. IF CurCh () = '^' THEN
  2754. (* Dereference: the pointer word is the base address. r.idx becomes
  2755. the pointee TYPE symbol, because ParseSub reads an array/record's
  2756. metadata from the symbol, not from the expression. *)
  2757. DropCh (GetCh ()) ;
  2758. p := symtab [idx].elem ;
  2759. LoadSymVar (idx, symtab [idx].off, 2) ;
  2760. EmMovDiAx () ;
  2761. r.idx := p ;
  2762. r.kind := 5 ;
  2763. r.boff := 0 ;
  2764. r.cls := symtab [p].cls ;
  2765. r.sz := symtab [p].size ;
  2766. r.fldFirst := symtab [p].fldFirst ;
  2767. r.fldLast := symtab [p].fldLast ;
  2768. r.arrIdx := symtab [p].arrIdx ;
  2769. r.ptrElem := symtab [p].elem ;
  2770. ParseSub (r) ;
  2771. RETURN
  2772. END
  2773. END ;
  2774. r.idx := idx ;
  2775. r.kind := 1 ;
  2776. r.boff := 0 ;
  2777. r.cls := symtab [idx].cls ;
  2778. IF symtab [idx].tag = KFunc THEN
  2779. idx := symtab [idx].resvar ;
  2780. r.idx := idx ;
  2781. r.cls := symtab [idx].cls
  2782. ELSIF (symtab [idx].tag # KVar) AND (symtab [idx].tag # KType) THEN
  2783. Err (EUnknown) ;
  2784. RETURN
  2785. END ;
  2786. IF r.cls = TSet THEN
  2787. (* a set value: carry where its bytes are so ParseCmp can fold `in' and
  2788. (later) compare. A set VARIABLE is still refused at its declaration,
  2789. so today this is a typed constant or an inline literal. *)
  2790. r.setoff := symtab [r.idx].off ;
  2791. r.setbase := symtab [r.idx].elem ;
  2792. r.setsize := symtab [r.idx].size
  2793. END ;
  2794. r.sz := symtab [r.idx].size ;
  2795. r.fldFirst := symtab [r.idx].fldFirst ;
  2796. r.fldLast := symtab [r.idx].fldLast ;
  2797. r.arrIdx := symtab [r.idx].arrIdx ;
  2798. r.ptrElem := symtab [r.idx].elem ;
  2799. ParseSub (r)
  2800. END ParseVar ;
  2801. PROCEDURE ConstAdd (a, b : LONGINT) : LONGINT ;
  2802. BEGIN
  2803. RETURN VAL (LONGINT, W16 (a + b))
  2804. END ConstAdd ;
  2805. PROCEDURE ConstSub (a, b : LONGINT) : LONGINT ;
  2806. BEGIN
  2807. RETURN VAL (LONGINT, W16 (a - b))
  2808. END ConstSub ;
  2809. PROCEDURE ConstMul (a, b : LONGINT) : LONGINT ;
  2810. BEGIN
  2811. RETURN VAL (LONGINT, W16 (a * b))
  2812. END ConstMul ;
  2813. PROCEDURE EmXchgAxDx () ;
  2814. (* 92h = XCHG AX,DX. Named for what it EMITS, which is the point of the
  2815. whole naming convention: this used to be called EmMoveAxDx, which is what
  2816. somebody would expect the opcode to be, and it is not - 89 D8 is
  2817. MOV AX,DX, 92h is the exchange. Here the exchange is what is wanted, so
  2818. the name is the only thing that was wrong, and it was wrong in the exact
  2819. way this file's names are not allowed to be: reading as "a move" when it
  2820. is a swap. After EmIDivAxCx the remainder is in DX and `mod` wants it in
  2821. AX; an exchange gets it there in one byte where a move also would, so the
  2822. behaviour is identical either way and only the name lied. *)
  2823. BEGIN
  2824. Ebyte (92H)
  2825. END EmXchgAxDx ;
  2826. PROCEDURE StrConcat (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ; FORWARD ;
  2827. PROCEDURE StrOperand (r : ERes) : BOOLEAN ; FORWARD ;
  2828. PROCEDURE BinOpEmit (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  2829. (* binary operation at one precedence level; folds constant operands *)
  2830. VAR f : LONGINT ;
  2831. okc : BOOLEAN ;
  2832. BEGIN
  2833. IF (left.cls = TReal) OR (right.cls = TReal) OR (op = OpDiv) THEN
  2834. (* `/` is real division even when both operands are integers; Pascal's
  2835. `div` is the integer operator and has its own token. *)
  2836. RealBin (op, left, right, res) ;
  2837. RETURN
  2838. END ;
  2839. IF StrOperand (left) OR StrOperand (right) THEN
  2840. (* A string OPERAND - a STRING or a CHAR, which is a one-character string
  2841. for this purpose - makes this a string operation, and `+' is the only
  2842. one ISO Modula-2 defines: every other binary operator reached here
  2843. (OR, and the multiplicative and additive numeric ops) has no meaning
  2844. over a STRING, and StrConcat rejects it.
  2845. The test is StrOperand and NOT `cls = TString' because of the two
  2846. CHAR operands. `'a' + 'b'' has no STRING in it at all, so a
  2847. TString-only test let it fall through to the integer path, where the
  2848. character codes were added and CHR(195) was printed - a silently
  2849. wrong program rather than an error (see t53_strcat_type). A CHAR
  2850. operand is still a string operand, which is also what makes `a + ch'
  2851. concatenate.
  2852. Handling it here, at the head of the one routine every binary
  2853. operator funnels through, is what keeps `a + b', `writeln(a + b)',
  2854. `(a + b) = c' and `s := a + b + c' all going through the same
  2855. emission. *)
  2856. StrConcat (op, left, right, res) ;
  2857. RETURN
  2858. END ;
  2859. IF op = TkAnd THEN
  2860. IF (left.kind = 0) AND (right.kind = 0) THEN
  2861. res.kind := 0 ;
  2862. res.imm := BitAnd (left.imm, right.imm) ;
  2863. IF (left.cls = TBool) OR (right.cls = TBool) THEN
  2864. res.cls := TBool
  2865. ELSE
  2866. res.cls := left.cls
  2867. END ;
  2868. RETURN
  2869. END ;
  2870. LoadPair (left, right) ;
  2871. EmAndAxCx () ;
  2872. res.kind := 2 ;
  2873. IF (left.cls = TBool) OR (right.cls = TBool) THEN
  2874. res.cls := TBool
  2875. ELSE
  2876. res.cls := left.cls
  2877. END ;
  2878. RETURN
  2879. END ;
  2880. IF op = TkOr THEN
  2881. IF (left.kind = 0) AND (right.kind = 0) THEN
  2882. res.kind := 0 ;
  2883. res.imm := BitOr (left.imm, right.imm) ;
  2884. IF (left.cls = TBool) OR (right.cls = TBool) THEN
  2885. res.cls := TBool
  2886. ELSE
  2887. res.cls := left.cls
  2888. END ;
  2889. RETURN
  2890. END ;
  2891. LoadPair (left, right) ;
  2892. EmOrAxCx () ;
  2893. res.kind := 2 ;
  2894. IF (left.cls = TBool) OR (right.cls = TBool) THEN
  2895. res.cls := TBool
  2896. ELSE
  2897. res.cls := left.cls
  2898. END ;
  2899. RETURN
  2900. END ;
  2901. IF (left.kind = 0) AND (right.kind = 0) THEN
  2902. okc := FALSE ;
  2903. CASE op OF
  2904. OpAdd : f := ConstAdd (left.imm, right.imm) ; okc := TRUE ;
  2905. | OpSub : f := ConstSub (left.imm, right.imm) ; okc := TRUE ;
  2906. | OpMul : f := ConstMul (left.imm, right.imm) ; okc := TRUE ;
  2907. | TkDiv : okc := (right.imm # 0) AND (right.imm > 0)
  2908. AND (left.imm >= 0) ;
  2909. IF okc THEN f := left.imm DIV right.imm END ;
  2910. | TkMod : okc := (right.imm # 0) AND (right.imm > 0)
  2911. AND (left.imm >= 0) ;
  2912. IF okc THEN f := left.imm MOD right.imm END ;
  2913. ELSE
  2914. okc := FALSE
  2915. END ;
  2916. IF okc THEN
  2917. res.kind := 0 ;
  2918. res.imm := VAL (LONGINT, W16 (f)) ;
  2919. res.cls := left.cls ;
  2920. RETURN
  2921. ELSIF op = TkDiv THEN
  2922. Err (EConstRange) ;
  2923. RETURN
  2924. END
  2925. END ;
  2926. LoadPair (left, right) ;
  2927. CASE op OF
  2928. OpAdd : EmAddAxCx ;
  2929. | OpSub : EmSubAxCx ;
  2930. | OpMul : EmMulAxCx ;
  2931. | OpShl : EmShlAxCl ;
  2932. | OpShr : EmShrAxCl ;
  2933. | TkDiv : EmIDivAxCx ;
  2934. | TkMod : EmIDivAxCx ; EmXchgAxDx ;
  2935. ELSE
  2936. Err (ETypeErr)
  2937. END ;
  2938. res.kind := 2 ;
  2939. res.cls := left.cls
  2940. END BinOpEmit ;
  2941. PROCEDURE ConstCmp (op : CARDINAL ; a, b : LONGINT ; VAR f : LONGINT)
  2942. : BOOLEAN ;
  2943. VAR a16, b16 : CARDINAL ;
  2944. BEGIN
  2945. a16 := W16 (a) ;
  2946. b16 := W16 (b) ;
  2947. f := 0 ;
  2948. CASE op OF
  2949. 1 : f := VAL (LONGINT, ORD (a16 = b16)) ;
  2950. | 2 : f := VAL (LONGINT, ORD (a16 # b16)) ;
  2951. | 3 : f := VAL (LONGINT, ORD (a16 < b16)) ;
  2952. | 4 : f := VAL (LONGINT, ORD (a16 >= b16)) ;
  2953. | 5 : f := VAL (LONGINT, ORD (a16 > b16)) ;
  2954. | 6 : f := VAL (LONGINT, ORD (a16 <= b16)) ;
  2955. ELSE
  2956. RETURN FALSE
  2957. END ;
  2958. RETURN TRUE
  2959. END ConstCmp ;
  2960. (* ---------------------------------------------------------------- *)
  2961. (* keeping an operand alive across the parse of the other one *)
  2962. (* ---------------------------------------------------------------- *)
  2963. PROCEDURE SaveLeft (VAR left : ERes) ;
  2964. VAR sk : CARDINAL ;
  2965. (* Called the moment the operator has been recognised, i.e. *before* the
  2966. right-hand operand is parsed.
  2967. Every binary level in this parser has the shape
  2968. left := r ; ParseXxx (right) ; BinOpEmit (op, left, right, r) ;
  2969. so the right-hand operand's code is emitted between those two lines, and
  2970. the left-hand operand has to survive that. For kind 0 (a constant) and
  2971. kind 1 (a named variable) there is nothing to survive: LoadAtom re-emits
  2972. the MOV or the load at the operator site, and no code has been emitted
  2973. for the left yet. A kind-2 value is different - its code is already in
  2974. the image and its value exists only in AX, which the right-hand parse is
  2975. about to overwrite. The value had to be parked on the stack instead.
  2976. TPSRC9 does exactly this in the other order: addptr and mulptr CALL
  2977. pushres (which calls loadatom and sets flgpshax) *before* CALL mullevel,
  2978. so PUSH AX lands in the image ahead of the right-hand operand's code.
  2979. Without this, `(p > q) or (q > p)` emitted both comparisons and then
  2980. ORed the *second* comparison's result with itself - which is why it
  2981. printed FALSE for TRUE or FALSE. *)
  2982. BEGIN
  2983. IF left.cls = TReal THEN
  2984. (* Park six bytes in a fresh scratch, exactly as kind-5 does for a
  2985. scalar: the right-hand parse may reuse DI or call a routine. *)
  2986. sk := NewScratchReal () ;
  2987. EmCopyRealTo (symtab [sk].off, left) ;
  2988. left.idx := sk ;
  2989. left.kind := 1 ;
  2990. left.cls := TReal ;
  2991. left.sz := 6 ;
  2992. left.boff := 0
  2993. ELSIF left.kind = 2 THEN
  2994. EmPushAx () ;
  2995. left.kind := 4 (* 4 = on the top of the stack *)
  2996. ELSIF left.kind = 5 THEN
  2997. (* A string element whose address is in DI. This is the one place the
  2998. address must be spent BEFORE the right-hand operand is parsed: DI
  2999. cannot survive it, because the right operand may hold a subscript of
  3000. its own (which reuses DI) or call a routine (which may clobber it).
  3001. Reading [DI] now - which LoadAtom does, turning kind 5 into kind 2 -
  3002. and pushing the value is exactly what the kind-2 case above does
  3003. next, so the two branches converge on kind 4. A subscript on the
  3004. RIGHT is fine: it sets DI after this point and LoadPair reads it
  3005. immediately, with no parse in between. *)
  3006. LoadAtom (left) ; (* AL := [DI], AH := 0; kind := 2 *)
  3007. EmPushAx () ;
  3008. left.kind := 4
  3009. END
  3010. END SaveLeft ;
  3011. PROCEDURE LoadPair (VAR left, right : ERes) ;
  3012. (* Materialise both operands of a binary operator so that, once this
  3013. returns, AX holds the LEFT one and CX the RIGHT one - which is the state
  3014. EmAddAxCx/EmSubAxCx/EmMulAxCx/EmIDivAxCx/EmAndAxCx/EmOrAxCx/EmCmpAxCx
  3015. all expect (see EmXchgAxCx).
  3016. Three shapes, and only three:
  3017. left.kind = 4 SaveLeft pushed it before the right operand was
  3018. parsed, so the right value is in AX and the left one
  3019. is on the stack: load the right (a no-op for kind 2),
  3020. pop the left into CX, then swap.
  3021. right.kind = 2 The right value is in AX, and loading the left would
  3022. destroy it: push the right, load the left, then pop
  3023. the right straight into CX. No swap needed.
  3024. neither Both can still be materialised from scratch, and
  3025. neither is at risk, so this is the original sequence.
  3026. A kind-5 right-hand operand takes that last shape, and is safe there: the
  3027. only instruction emitted before its LoadAtom is the left's, and MOV AX,[var]
  3028. and MOV AX,imm do not touch DI, so [DI] still holds the element address.
  3029. Whichever shape is taken, exactly one push is matched by exactly one
  3030. pop. *)
  3031. BEGIN
  3032. IF left.kind = 4 THEN
  3033. LoadAtom (right) ;
  3034. EmPopCx () ;
  3035. EmXchgAxCx ()
  3036. ELSIF right.kind = 2 THEN
  3037. EmPushAx () ;
  3038. LoadAtom (left) ;
  3039. EmPopCx ()
  3040. ELSE
  3041. LoadAtom (left) ; EmPushAx () ;
  3042. LoadAtom (right) ; EmPopCx () ;
  3043. EmXchgAxCx ()
  3044. END
  3045. END LoadPair ;
  3046. PROCEDURE EmStrBlob (r : ERes) : CARDINAL ;
  3047. (* Emit a string literal's <len><chars> in the code stream behind a JMP and
  3048. return the image offset of the LENGTH byte. Shared by EmPushStrAddr (which
  3049. pushes that address) and EmPushStrSlot (which copies the bytes onto the
  3050. stack for a string value parameter). *)
  3051. VAR n, len, here : CARDINAL ;
  3052. BEGIN
  3053. len := strLen [r.strx] ;
  3054. IF len > 255 THEN
  3055. (* As in IoCall: the length is one byte, so a longer literal would wrap
  3056. and silently carry only its first (length MOD 256) characters.
  3057. TP3 strings are at most 255 characters. *)
  3058. Err (EConstRange) ;
  3059. RETURN 0
  3060. END ;
  3061. IF len + 1 <= 127 THEN
  3062. Ebyte (0EBH) ; Ebyte (VAL (BYTE, len + 1)) (* JMP rel8 over it *)
  3063. ELSE
  3064. Ebyte (0E9H) ; Eword (len + 1) (* JMP rel16 over it *)
  3065. END ;
  3066. here := pc ; (* the length byte: the literal's address *)
  3067. Ebyte (VAL (BYTE, len)) ;
  3068. n := 0 ;
  3069. WHILE n < len DO
  3070. Ebyte (VAL (BYTE, ORD (strPool [strOff [r.strx] + n]))) ;
  3071. INC (n)
  3072. END ;
  3073. RETURN here
  3074. END EmStrBlob ;
  3075. PROCEDURE EmAsciiZLiteral (start, stop : CARDINAL) : CARDINAL ;
  3076. (* Emit the source text [start,stop) as a NUL-terminated code-resident string
  3077. behind a JMP and return its image offset. Used for REAL literals, which are
  3078. parsed by the runtime's ascreal rather than by a host-side decimal parser. *)
  3079. VAR here, n, len : CARDINAL ;
  3080. BEGIN
  3081. len := stop - start ;
  3082. IF len <= 126 THEN
  3083. Ebyte (0EBH) ; Ebyte (VAL (BYTE, len + 1))
  3084. ELSE
  3085. Ebyte (0E9H) ; Eword (len + 1)
  3086. END ;
  3087. here := pc ;
  3088. n := start ;
  3089. WHILE n < stop DO
  3090. Ebyte (VAL (BYTE, ORD (CharAt (n)))) ;
  3091. INC (n)
  3092. END ;
  3093. Ebyte (0) ;
  3094. RETURN here
  3095. END EmAsciiZLiteral ;
  3096. PROCEDURE EmPushSetAddr (r : ERes) ;
  3097. (* Push the ADDRESS of a set value: the runtime's set entries take addresses.
  3098. A set literal/typed constant is code-resident (kind 7), a set-expression
  3099. result is a data buffer (kind 8), a named set is symbol+boff, and a field
  3100. reached through DI is already addressed. *)
  3101. BEGIN
  3102. IF r.kind = 5 THEN
  3103. EmPushDi ()
  3104. ELSIF r.kind = 1 THEN
  3105. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  3106. ELSIF (r.kind = 7) OR (r.kind = 8) THEN
  3107. EmPushVarAddr (FALSE, r.setoff)
  3108. ELSE
  3109. Err (ENoLib)
  3110. END
  3111. END EmPushSetAddr ;
  3112. PROCEDURE SetPairSize (left, right : ERes ; VAR size, base : CARDINAL) : BOOLEAN ;
  3113. (* The byte count for a set operation. An inline literal is a 32-byte
  3114. `set of Char' by default; when the other operand is a typed set (CALC's
  3115. one-byte `set of Attributes') that type wins. Two non-literals must have
  3116. the same origin and are taken at the smaller size; the runtime only ever
  3117. touches that many bytes. *)
  3118. BEGIN
  3119. IF left.setbase # right.setbase THEN
  3120. RETURN FALSE
  3121. END ;
  3122. base := left.setbase ;
  3123. IF left.kind = 7 THEN
  3124. IF right.kind = 7 THEN
  3125. size := left.setsize
  3126. ELSE
  3127. size := right.setsize
  3128. END
  3129. ELSIF right.kind = 7 THEN
  3130. size := left.setsize
  3131. ELSE
  3132. size := left.setsize ;
  3133. IF right.setsize < size THEN
  3134. size := right.setsize
  3135. END
  3136. END ;
  3137. RETURN (size > 0) AND (size <= 32)
  3138. END SetPairSize ;
  3139. PROCEDURE AllocSetScratch (size : CARDINAL) : CARDINAL ;
  3140. (* A fresh data buffer for one set expression. One per operation, so a nested
  3141. or repeated expression cannot overwrite the other side while it is still
  3142. being consumed. The bytes need no initialisation: every operation writes
  3143. all `size' of them before the value is read. *)
  3144. VAR off : CARDINAL ;
  3145. BEGIN
  3146. IF dc + size > 0F000H THEN
  3147. Err (EConstRange) ;
  3148. RETURN 0
  3149. END ;
  3150. off := dc ;
  3151. dc := dc + size ;
  3152. RETURN off
  3153. END AllocSetScratch ;
  3154. PROCEDURE SetBinEmit (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  3155. (* The caller has already pushed the LEFT address (before parsing the right
  3156. operand, so a DI-addressed field survives). Finish the argument list -
  3157. right, destination, (op<<8)|size - call the runtime, and return the buffer
  3158. as a kind-8 set value. *)
  3159. VAR size, base, dest : CARDINAL ;
  3160. BEGIN
  3161. IF NOT SetPairSize (left, right, size, base) THEN
  3162. Err (ENoLib) ;
  3163. RETURN
  3164. END ;
  3165. dest := AllocSetScratch (size) ;
  3166. IF NOT OK () THEN
  3167. RETURN
  3168. END ;
  3169. EmPushSetAddr (right) ;
  3170. EmPushVarAddr (FALSE, dest) ;
  3171. EmMovAxi (W16 (op * 256 + size)) ;
  3172. EmPushAx () ;
  3173. DropC (EmCall (TU_SetOp)) ;
  3174. EmAddSp (8) ;
  3175. res.kind := 8 ;
  3176. res.cls := TSet ;
  3177. res.chr := FALSE ;
  3178. res.setoff := dest ;
  3179. res.setbase := base ;
  3180. res.setsize := size
  3181. END SetBinEmit ;
  3182. PROCEDURE MaterializeDyn (VAR r : ERes ; base, size : CARDINAL) ;
  3183. (* Build a scratch set of `size' bytes with the single dynamic element at
  3184. `base' set. Used for TP3's `[Storage[Index]]' constructor. *)
  3185. VAR dest : CARDINAL ;
  3186. BEGIN
  3187. dest := AllocSetScratch (size) ;
  3188. IF NOT OK () THEN
  3189. RETURN
  3190. END ;
  3191. EmMovAxi (size) ; EmPushAx () ;
  3192. EmMovAxi (base) ; EmPushAx () ;
  3193. LoadSymVar (r.dynElem, symtab [r.dynElem].off, 2) ;
  3194. EmPushAx () ;
  3195. EmPushVarAddr (FALSE, dest) ;
  3196. DropC (EmCall (TU_SetSingle)) ;
  3197. EmAddSp (8) ;
  3198. r.kind := 8 ;
  3199. r.setoff := dest ;
  3200. r.setbase := base ;
  3201. r.setsize := size
  3202. END MaterializeDyn ;
  3203. PROCEDURE EmPushStrAddr (r : ERes) ;
  3204. (* Push the ADDRESS of a whole-string operand - the argument shape cmpstr
  3205. takes, one per operand.
  3206. A string VARIABLE's value already is its address, so this is the same push
  3207. READ makes (EmPushVarAddr), through symtab's local/off.
  3208. A string LITERAL has no storage, and the image carries no initialised data
  3209. to put one in (globals are left to the loader's zero fill), so the
  3210. characters have to travel in the code stream and the operand is the address
  3211. of those bytes. TP3 does the same in TPSRC9 fullvar (993): it builds the
  3212. `JMP' opcode in AL and the length in AH (`MOV AL,#$EB' / `MOV.B AH,CH'),
  3213. emits that word, and takes the position immediately after it as varofs,
  3214. with varseg := CS - the instruction stream steps over the constant instead
  3215. of running into it.
  3216. The difference is one byte, and it is deliberate. TP3's constant is an
  3217. ARRAY [1..n] OF CHAR, and `estr2' (TPSRC10 85) emits it WITHOUT a length
  3218. byte because the length travelled on the stack instead. Every string in
  3219. THIS runtime - variable or literal - is a `string': a length byte and then
  3220. the characters, which is what rdstr writes and what cmpstr reads. So a
  3221. literal operand has to look the same, and the jump carries one extra byte:
  3222. <len><chars>, with the ADDRESS pushed being the length byte itself. The
  3223. jump still exists only to keep the CPU from walking into the characters.
  3224. Nothing is added to the data segment and no runtime entry is involved in
  3225. reading the literal; LEA AX,[that] is absolute into the code segment, which
  3226. is where a .COM's DS already is.
  3227. A 1-character literal is NOT a TString here: RdConst reports it as TScalar
  3228. (its character code), so `s = 'a'` compares a string with a character and
  3229. is a type error rather than a length-1 string compare. That is deliberate
  3230. - it is the same rule `c := 'a'` relies on - and the caller rejects it. *)
  3231. BEGIN
  3232. IF r.kind = 5 THEN
  3233. (* a string element/field computed through DI - `Sheet[X,Y].Contents'
  3234. and the `with' form. DI holds the field's address, which is exactly
  3235. what cmpstr/strcat/strstore take on the stack, so push it. This used
  3236. to be ENoLib, and it was the CALC rung after Write(Value:FW:DEC):
  3237. `if Sheet[UFX,UFY].Contents <> '' then'. *)
  3238. EmPushDi ()
  3239. ELSIF r.kind = 1 THEN
  3240. IF symtab [r.idx].isVar THEN
  3241. (* A VAR string parameter: the slot at [BP+off] holds the address of
  3242. the caller's characters, so push its CONTENTS, not the address of
  3243. the slot. boff is zero for the whole string; an element/subscript
  3244. is kind 5 and handled above. *)
  3245. IF r.boff # 0 THEN
  3246. Err (ENoLib)
  3247. ELSE
  3248. LoadSymVar (r.idx, symtab [r.idx].off, 2) ;
  3249. EmPushAx ()
  3250. END
  3251. ELSE
  3252. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  3253. END
  3254. ELSIF r.kind = 3 THEN
  3255. EmPushVarAddr (FALSE, EmStrBlob (r)) (* the literal travels inline *)
  3256. ELSE
  3257. Err (ENoLib) (* a computed string is not a value *)
  3258. END
  3259. END EmPushStrAddr ;
  3260. PROCEDURE EmPushRealAddr (r : ERes) ;
  3261. (* Push the address of a six-byte REAL actual: ordinary variable, VAR REAL
  3262. parameter (slot holds the address), or a kind-5 field/function result. *)
  3263. BEGIN
  3264. IF r.kind = 5 THEN
  3265. EmPushDi ()
  3266. ELSIF r.kind = 1 THEN
  3267. IF symtab [r.idx].isVar THEN
  3268. LoadSymVar (r.idx, symtab [r.idx].off, 2) ;
  3269. EmPushAx ()
  3270. ELSE
  3271. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  3272. END
  3273. ELSE
  3274. Err (ENoLib)
  3275. END
  3276. END EmPushRealAddr ;
  3277. PROCEDURE EmPushRealSlot (r : ERes ; reserve : CARDINAL) ;
  3278. (* Push a six-byte value REAL argument's whole slot onto the stack. *)
  3279. BEGIN
  3280. EmPushRealAddr (r) ;
  3281. EmPopSi () ;
  3282. EmSubSp (reserve) ;
  3283. Ebyte (89H) ; Ebyte (0E7H) ; (* MOV DI,SP *)
  3284. EmMovCxi (reserve) ;
  3285. Ebyte (8AH) ; Ebyte (04H) ; (* MOV AL,[SI] *)
  3286. Ebyte (88H) ; Ebyte (05H) ; (* MOV [DI],AL *)
  3287. Ebyte (46H) ; Ebyte (47H) ; (* INC SI / INC DI *)
  3288. Ebyte (0E2H) ; Ebyte (0F8H) (* LOOP back *)
  3289. END EmPushRealSlot ;
  3290. PROCEDURE EmCopyRealTo (dstOff : CARDINAL ; r : ERes) ;
  3291. (* Copy six REAL bytes from r's address into the image global at dstOff, or
  3292. materialise an integer constant there as MBF. *)
  3293. VAR k : CARDINAL ;
  3294. rbuf : ARRAY [0..5] OF CHAR ;
  3295. BEGIN
  3296. IF r.cls = TReal THEN
  3297. EmPushRealAddr (r) ;
  3298. EmPopSi () ;
  3299. EmLeaDi (FALSE, dstOff) ;
  3300. EmMovCxi (6) ;
  3301. Ebyte (8AH) ; Ebyte (04H) ; (* MOV AL,[SI] *)
  3302. Ebyte (88H) ; Ebyte (05H) ; (* MOV [DI],AL *)
  3303. Ebyte (46H) ; Ebyte (47H) ;
  3304. Ebyte (0E2H) ; Ebyte (0F8H) (* LOOP -8 *)
  3305. ELSIF (r.cls = TScalar) AND (r.kind = 0) THEN
  3306. IntToMbf (r.imm, rbuf) ;
  3307. k := 0 ;
  3308. WHILE k < 6 DO
  3309. EmMovAxi (ORD (rbuf [k])) ;
  3310. EmStoreVar (FALSE, (dstOff + k) MOD 10000H, 1) ;
  3311. INC (k)
  3312. END
  3313. ELSE
  3314. Err (ENoLib)
  3315. END
  3316. END EmCopyRealTo ;
  3317. PROCEDURE EmPushStrSlot (r : ERes ; reserve, copyN : CARDINAL) ;
  3318. (* Push a STRING VALUE argument's whole slot: reserve `reserve' bytes on the
  3319. stack and copy `copyN' of the source string's <len><chars> into them, so
  3320. that after the CALL the callee's BP+4 is the length byte and the characters
  3321. follow it.
  3322. <push the source address, then POP SI> ; local/global/literal alike
  3323. SUB SP,reserve ; MOV DI,SP ; MOV CX,copyN
  3324. loop: MOV AL,[SI] ; MOV [DI],AL ; INC SI ; INC DI ; LOOP loop
  3325. The address comes from EmPushStrAddr, which already knows how to form it for
  3326. a local ([BP+off]), a global (absolute) or a literal (inline blob) - so a
  3327. caller's LOCAL string can be passed too, not just a global.
  3328. A byte loop, not REP MOVSB, on purpose: MOVS/STOS address through ES, and a
  3329. .COM's ES happens to equal DS but nothing in this compiler establishes that
  3330. contract. Every access here is DS-relative, which is the one segment the
  3331. image already guarantees. `copyN' is at most `reserve' (a shorter actual
  3332. string than the formal leaves the tail of the slot untouched, harmless
  3333. because the length byte bounds every read). *)
  3334. BEGIN
  3335. EmPushStrAddr (r) ; (* push the source address *)
  3336. EmPopSi () ; (* SI := source *)
  3337. EmSubSp (reserve) ;
  3338. Ebyte (89H) ; Ebyte (0E7H) ; (* MOV DI,SP *)
  3339. EmMovCxi (copyN) ;
  3340. IF copyN > 0 THEN
  3341. Ebyte (8AH) ; Ebyte (04H) ; (* MOV AL,[SI] *)
  3342. Ebyte (88H) ; Ebyte (05H) ; (* MOV [DI],AL *)
  3343. Ebyte (46H) ; (* INC SI *)
  3344. Ebyte (47H) ; (* INC DI *)
  3345. Ebyte (0E2H) ; Ebyte (0F8H) (* LOOP -8: back to MOV AL,[SI] *)
  3346. END
  3347. END EmPushStrSlot ;
  3348. (* ---------------------------------------------------------------- *)
  3349. (* sets - type sizing, literals, compile-time membership *)
  3350. (* ---------------------------------------------------------------- *)
  3351. PROCEDURE SetRangeBits (VAR bytes : ARRAY OF CHAR ; size, base : CARDINAL ;
  3352. lo, hi : LONGINT) ;
  3353. (* Set the bits for every element in [lo, hi], using TP3's hybrid rule
  3354. (coeur-tp-ocr 4.7.2): element e is bit (e MOD 8) of byte
  3355. ((e DIV 8) - (base DIV 8)), and is IGNORED unless that byte is in
  3356. [0, size). A value that lands outside the used bytes is truncated away, as
  3357. TP3 does; one inside them but outside the DECLARED range still lands, which
  3358. is also TP3's behaviour. *)
  3359. VAR rel, first, last, bidx, bit, m, o : CARDINAL ;
  3360. BEGIN
  3361. IF (hi < 0) OR (lo > 255) THEN
  3362. RETURN
  3363. END ;
  3364. IF lo < 0 THEN first := 0 ELSE first := VAL (CARDINAL, lo) END ;
  3365. IF hi > 255 THEN last := 255 ELSE last := VAL (CARDINAL, hi) END ;
  3366. rel := first ;
  3367. WHILE rel <= last DO
  3368. IF rel >= base THEN
  3369. bidx := (rel - base) DIV 8 ;
  3370. IF bidx < size THEN
  3371. bit := (rel - base) MOD 8 ;
  3372. m := 1 ;
  3373. WHILE bit > 0 DO
  3374. m := m * 2 ; DEC (bit)
  3375. END ;
  3376. o := ORD (bytes [bidx]) ;
  3377. IF (o DIV m) MOD 2 = 0 THEN
  3378. bytes [bidx] := CHR (o + m)
  3379. END
  3380. END
  3381. END ;
  3382. INC (rel)
  3383. END
  3384. END SetRangeBits ;
  3385. PROCEDURE RdSetLiteral (base, size : CARDINAL ; VAR bytes : ARRAY OF CHAR) : BOOLEAN ;
  3386. (* Read a set constructor `[e, a..b, ...]' and fill `bytes' (already size
  3387. long). `[]' is the empty set. *)
  3388. VAR v, hi : LONGINT ; subcls : CARDINAL ; strf : BOOLEAN ; k : CARDINAL ;
  3389. BEGIN
  3390. k := 0 ;
  3391. WHILE k < size DO
  3392. bytes [k] := CHR (0) ;
  3393. INC (k)
  3394. END ;
  3395. ExpectDelim ('[', ENoSemi) ;
  3396. IF MatchDelim (']') THEN
  3397. RETURN OK () (* [] - the empty set *)
  3398. END ;
  3399. LOOP
  3400. RdConst (v, subcls, strf) ;
  3401. IF strf THEN
  3402. Err (ETypeErr) ;
  3403. RETURN FALSE
  3404. END ;
  3405. IF MatchRange () THEN
  3406. RdConst (hi, subcls, strf) ;
  3407. IF strf THEN
  3408. Err (ETypeErr) ;
  3409. RETURN FALSE
  3410. END
  3411. ELSE
  3412. hi := v
  3413. END ;
  3414. SetRangeBits (bytes, size, base, v, hi) ;
  3415. IF MatchDelim (',') THEN
  3416. (* another element *)
  3417. ELSE
  3418. EXIT
  3419. END
  3420. END ;
  3421. ExpectDelim (']', ENoSemi) ;
  3422. RETURN OK ()
  3423. END RdSetLiteral ;
  3424. PROCEDURE EmSetBlob (VAR bytes : ARRAY OF CHAR ; size : CARDINAL) : CARDINAL ;
  3425. (* Emit a set's bytes into the code stream behind a JMP over them - the same
  3426. trick EmPushStrAddr uses for a string literal - and return the image offset
  3427. of the first byte. Nothing goes in the data segment: a .COM's DS already
  3428. covers the code segment, which is why this port needs no `seg cs' where the
  3429. original did (see CALC-COMPILE.md's typed-constant storage decision). *)
  3430. VAR here, k : CARDINAL ;
  3431. BEGIN
  3432. IF size + 2 <= 127 THEN
  3433. Ebyte (0EBH) ; Ebyte (VAL (BYTE, size)) (* JMP rel8 over it *)
  3434. ELSE
  3435. Ebyte (0E9H) ; Eword (size) (* JMP rel16 over it *)
  3436. END ;
  3437. here := pc ;
  3438. k := 0 ;
  3439. WHILE k < size DO
  3440. Ebyte (VAL (BYTE, ORD (bytes [k]))) ;
  3441. INC (k)
  3442. END ;
  3443. RETURN here
  3444. END EmSetBlob ;
  3445. PROCEDURE FoldSetIn (left, right : ERes ; VAR res : ERes) ;
  3446. (* `e IN s' where e is a compile-time ordinal and s a set whose bytes are
  3447. already in the image: answer at compile time, exactly as TPSRC2's xsetin
  3448. answers at run time (AH > 255 -> FALSE, else test the bit). A run-time
  3449. element or a run-time set is still ENoLib - that is the next milestone, and
  3450. it must stay LOUD rather than silently answer wrong. *)
  3451. VAR off, base, size, rel, bidx, bit, m, o : CARDINAL ;
  3452. isIn, foldable : BOOLEAN ;
  3453. jl, jg, jmp : CARDINAL ;
  3454. BEGIN
  3455. res.kind := 0 ;
  3456. res.imm := 0 ;
  3457. res.cls := TBool ;
  3458. res.chr := FALSE ;
  3459. IF right.cls # TSet THEN
  3460. Err (ENoLib) ;
  3461. RETURN
  3462. END ;
  3463. IF left.kind # 0 THEN
  3464. (* A RUN-TIME element (`Upcase(Ch) in ['Y','N']', `Ch in ['a'..'z']').
  3465. Materialise it in AX, subtract the set's base, reject the out-of-range
  3466. values with the two signed tests the compiler declares (L and G), then
  3467. index the bit array: byte = (e-base) DIV 8, bit = (e-base) MOD 8.
  3468. The set's address is formed according to where its bytes live: an
  3469. inline literal or typed constant is code-resident (image offset +
  3470. LoadBias), a data set is an absolute or BP-relative address. A set
  3471. already addressed through DI and an element that itself needs DI are
  3472. refused: the address would be lost. *)
  3473. IF right.kind = 5 THEN
  3474. Err (ENoLib) ;
  3475. RETURN
  3476. END ;
  3477. LoadAtom (left) ;
  3478. IF NOT OK () THEN
  3479. RETURN
  3480. END ;
  3481. IF right.setbase # 0 THEN
  3482. EmMovCxi (W16 (right.setbase)) ;
  3483. EmSubAxCx ()
  3484. END ;
  3485. EmCmpAxi (0) ;
  3486. jl := EmJcc (8CH, 0) ; (* JL -> false *)
  3487. EmCmpAxi (W16 (right.setsize * 8)) ;
  3488. jg := EmJcc (8FH, 0) ; (* JG -> false *)
  3489. EmMovClAl () ;
  3490. EmAndClImm (7) ;
  3491. EmShrAx1 () ; EmShrAx1 () ; EmShrAx1 () ; (* AX := byte index *)
  3492. IF right.kind = 7 THEN
  3493. EmMovBxi (W16 (right.setoff + LoadBias))
  3494. ELSIF right.kind = 1 THEN
  3495. IF symtab [right.idx].local THEN
  3496. EmLeaBx ((right.setoff + right.boff) MOD 10000H)
  3497. ELSE
  3498. EmMovBxi ((right.setoff + right.boff + LoadBias) MOD 10000H)
  3499. END
  3500. ELSE
  3501. Err (ENoLib) ;
  3502. RETURN
  3503. END ;
  3504. EmAddBxAx () ; (* BX := the set's byte *)
  3505. EmLdAlBx () ;
  3506. EmMovDlImm (1) ;
  3507. EmShlDlCl () ; (* DL := 1 << bit *)
  3508. EmAndAlDl () ;
  3509. EmMovAh0 () ;
  3510. EmOrAxAx () ;
  3511. EmRelSetcc (2) ; (* SETNE: the bit was set *)
  3512. jmp := EmJmpNear (0) ;
  3513. SetPatTgt (jl, pc) ;
  3514. SetPatTgt (jg, pc) ;
  3515. EmMovAxi (0) ;
  3516. SetPatTgt (jmp, pc) ;
  3517. res.kind := 2 ;
  3518. res.cls := TBool ;
  3519. RETURN
  3520. END ;
  3521. (* A code-resident set (an inline literal or a typed constant) folds at
  3522. compile time. A DATA set - a record field, local or global, or one
  3523. addressed through DI - must be tested at run time: its byte is in the
  3524. image's data area, not in cbuf. A typed constant's offset is below
  3525. dataBase; a global's is at or above it. *)
  3526. foldable := (right.kind = 7) ;
  3527. IF (right.kind = 1) AND NOT symtab [right.idx].local
  3528. AND (right.setoff + right.boff < dataBase) THEN
  3529. foldable := TRUE
  3530. END ;
  3531. IF NOT foldable THEN
  3532. IF (left.imm >= 0) AND (left.imm <= 255) THEN
  3533. base := right.setbase ; size := right.setsize ;
  3534. rel := VAL (CARDINAL, left.imm) ;
  3535. IF rel >= base THEN
  3536. bidx := (rel - base) DIV 8 ;
  3537. IF bidx < size THEN
  3538. bit := (rel - base) MOD 8 ;
  3539. m := 1 ;
  3540. WHILE bit > 0 DO
  3541. m := m * 2 ; DEC (bit)
  3542. END ;
  3543. IF right.kind = 5 THEN
  3544. EmLdAlDiOff (bidx)
  3545. ELSIF right.kind = 1 THEN
  3546. LoadSymVar (right.idx, (right.setoff + right.boff + bidx) MOD 10000H, 1)
  3547. ELSE
  3548. Err (ENoLib) ;
  3549. RETURN
  3550. END ;
  3551. EmAndAlImm (m) ;
  3552. EmMovAh0 () ;
  3553. EmOrAxAx () ;
  3554. EmRelSetcc (2) ; (* SETNE: the bit was set *)
  3555. res.kind := 2 ;
  3556. res.cls := TBool ;
  3557. RETURN
  3558. END
  3559. END
  3560. END ;
  3561. res.imm := 0 ;
  3562. RETURN
  3563. END ;
  3564. isIn := FALSE ;
  3565. IF (left.imm >= 0) AND (left.imm <= 255) THEN
  3566. off := right.setoff ; base := right.setbase ; size := right.setsize ;
  3567. rel := VAL (CARDINAL, left.imm) ;
  3568. IF rel >= base THEN
  3569. bidx := (rel - base) DIV 8 ;
  3570. IF bidx < size THEN
  3571. bit := (rel - base) MOD 8 ;
  3572. m := 1 ;
  3573. WHILE bit > 0 DO
  3574. m := m * 2 ; DEC (bit)
  3575. END ;
  3576. o := VAL (CARDINAL, cbuf [off + bidx]) ;
  3577. isIn := (o DIV m) MOD 2 # 0
  3578. END
  3579. END
  3580. END ;
  3581. res.imm := ORD (isIn)
  3582. END FoldSetIn ;
  3583. PROCEDURE RdSetBase (VAR baseCls : CARDINAL ; VAR lo, hi : LONGINT) ;
  3584. (* The base type of `set of ...': an ordinal range. `Char' and `byte' are
  3585. 0..255; a literal subrange `a..b' is read directly. A named or enumerated
  3586. base is ENoLib for now - `set of Attributes' waits for enums. *)
  3587. VAR idx, subcls : CARDINAL ; strf : BOOLEAN ;
  3588. BEGIN
  3589. baseCls := TScalar ;
  3590. Skip () ;
  3591. IF Alpha (CurCh ()) THEN
  3592. GetWord () ;
  3593. IF Search (wrd, idx) AND (symtab [idx].tag = KType) THEN
  3594. IF symtab [idx].cls = TEnum THEN
  3595. lo := symtab [idx].lo ;
  3596. hi := symtab [idx].hi ;
  3597. baseCls := TEnum
  3598. ELSIF (symtab [idx].cls = TChar)
  3599. OR ((symtab [idx].cls = TScalar) AND (symtab [idx].size = 1)) THEN
  3600. lo := 0 ; hi := 255 ; baseCls := symtab [idx].cls
  3601. ELSE
  3602. Err (ENoLib)
  3603. END
  3604. ELSE
  3605. Err (EUnknown)
  3606. END
  3607. ELSE
  3608. RdConst (lo, subcls, strf) ;
  3609. IF NOT MatchRange () THEN
  3610. Err (ESimpType) ;
  3611. RETURN
  3612. END ;
  3613. RdConst (hi, subcls, strf) ;
  3614. IF (lo < 0) OR (hi > 255) OR (lo > hi) THEN
  3615. Err (EConstRange)
  3616. END
  3617. END
  3618. END RdSetBase ;
  3619. PROCEDURE NewScratchStr () : CARDINAL ;
  3620. (* Allocate a fresh 256-byte global STRING and return its symbol index.
  3621. A concatenation result needs STORAGE the emitted code can name. Making the
  3622. result a string VARIABLE (kind 1) is what keeps every downstream consumer
  3623. untouched: Assignment, IoCall and ParseCmp already handle kind 1, so `s :=
  3624. a + b', `writeln(a + b)' and `(a + b) = c' work with no code of their own.
  3625. One scratch PER `+' in the source, so a repeated or nested concatenation
  3626. never shares a destination - `a + b + c' is two occurrences and gets two
  3627. scratch strings, and the first result survives until the second consumes it.
  3628. (A loop reuses its site's scratch, which is correct because each execution
  3629. writes before it reads.) Recursion would break that, but this compiler
  3630. compiles no procedures yet - see the item list.
  3631. 256 bytes is the STRING type's own size, length byte plus 255 characters:
  3632. see ParseType's TkString arm and the predefined STRING in Inittur. The name
  3633. is a placeholder (nothing looks a scratch up by name) spelled with '@', as
  3634. tmpA/tmpB are, so it cannot collide with a Modula-2 identifier. *)
  3635. BEGIN
  3636. DropC (NewSym ("@@SC", KVar, TString, 256, 1, dc, 0, FALSE)) ;
  3637. dc := dc + 256 ;
  3638. RETURN symTop - 1
  3639. END NewScratchStr ;
  3640. PROCEDURE NewScratchWord () : CARDINAL ;
  3641. (* Two bytes for a dynamic set literal's element value. *)
  3642. BEGIN
  3643. DropC (NewSym ("@@DW", KVar, TScalar, 2, 2, dc, 0, FALSE)) ;
  3644. dc := dc + 2 ;
  3645. RETURN symTop - 1
  3646. END NewScratchWord ;
  3647. PROCEDURE NewScratchReal () : CARDINAL ;
  3648. (* Six bytes of storage for a REAL function result, one per call site. *)
  3649. BEGIN
  3650. DropC (NewSym ("@@RL", KVar, TReal, 6, 6, dc, 0, FALSE)) ;
  3651. dc := dc + 6 ;
  3652. RETURN symTop - 1
  3653. END NewScratchReal ;
  3654. PROCEDURE NewScratchRealBlock () : CARDINAL ;
  3655. (* Twelve bytes for a binary REAL operation: a followed by b. *)
  3656. BEGIN
  3657. DropC (NewSym ("@@RB", KVar, TReal, 12, 6, dc, 0, FALSE)) ;
  3658. dc := dc + 12 ;
  3659. RETURN symTop - 1
  3660. END NewScratchRealBlock ;
  3661. PROCEDURE StrOperand (r : ERes) : BOOLEAN ;
  3662. (* Is r usable as one operand of `+'? A STRING, or a CHAR - which can appear
  3663. three ways: a quoted literal (cls TScalar with chr set), a CHAR variable
  3664. (cls TChar), and a string element s[i] (cls TChar, kind 5). A number is
  3665. not, and neither is a BOOLEAN. *)
  3666. BEGIN
  3667. RETURN (r.cls = TString) OR (r.cls = TChar) OR
  3668. ((r.cls = TScalar) AND r.chr)
  3669. END StrOperand ;
  3670. PROCEDURE EmPushStrOperand (r : ERes) ;
  3671. (* Push the ADDRESS of one concatenation operand, converting a CHAR to a
  3672. length-1 string first.
  3673. A STRING operand is pushed exactly as cmpstr pushes one (EmPushStrAddr):
  3674. a variable's value already IS its address, and a literal travels inline.
  3675. A CHAR operand has no such address, so it is materialised as a
  3676. `<length 1><character>' string in a fresh 2-byte scratch and the address of
  3677. THAT is pushed. This is TPSRC9's addch/addch2, which splice a character
  3678. into a concatenation by building a one-character string. The character is
  3679. loaded into AL (LoadAtom), the character is stored at [coff+1] and the
  3680. length 1 at [coff]. At most one operand of a concatenation is a CHAR -
  3681. StrConcat rejects char+char - so at most one such scratch is ever made. *)
  3682. VAR coff : CARDINAL ;
  3683. BEGIN
  3684. IF r.cls = TString THEN
  3685. EmPushStrAddr (r)
  3686. ELSE
  3687. coff := dc ;
  3688. dc := dc + 2 ;
  3689. LoadAtom (r) ; (* AX := the character *)
  3690. EmStoreVar (FALSE, (coff + 1) MOD 10000H, 1) ; (* [coff+1] := AL *)
  3691. EmMovAxi (1) ; (* AX := 1 *)
  3692. EmStoreVar (FALSE, coff MOD 10000H, 1) ; (* [coff] := AL = length *)
  3693. EmPushVarAddr (FALSE, coff MOD 10000H)
  3694. END
  3695. END EmPushStrOperand ;
  3696. PROCEDURE StrConcat (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  3697. (* `left + right' for strings: TPSRC2's xconcat, emitted as two runtime calls.
  3698. The layout is `<length byte><characters>' and `+' needs a place to build its
  3699. result, so this allocates one 256-byte global scratch (NewScratchStr) and
  3700. copies into it: first `dest := left' through the runtime's strstore, then
  3701. `dest := dest + right' through strcat, which appends. Using strstore for
  3702. the left operand and strcat for the right keeps the append logic in one
  3703. place and gives the compiler exactly one new entry to reach; it also means
  3704. the two calls have the same argument shape (source pushed first, destination
  3705. last, limit in CX), so the emission differs only in the entry named.
  3706. The limit is 255 - the scratch's 255 usable characters. An overflowing
  3707. result is a RUNTIME ERROR with exit code 16, TP3's `String too long', not a
  3708. truncation; see Runtime.EmitStrCat.
  3709. The operator rule: `+' concatenates, anything else over a STRING is a type
  3710. error, and char+char is ALSO a type error. ISO Modula-2 gives no meaning to
  3711. two characters summed, and inventing one (a two-character string?) would be
  3712. a guess this compiler does not make; TP3's own behaviour there is not
  3713. something the reference pins down. That char+char choice is the one place
  3714. this differs from a literal reading of the reference, and it is recorded
  3715. here and in v-TP3-STRCAT.md rather than left to be discovered.
  3716. The guard below is what ENFORCES that rule, and it only runs because
  3717. BinOpEmit's hook routes a CHAR operand here too. A hook that tested for a
  3718. STRING operand only would send `'a' + 'b'' to the integer path instead and
  3719. this test would never be reached - which is exactly what it did until
  3720. t53_strcat_type caught it. Three ways to fail it: a non-`+' operator over
  3721. a string operand, an operand that is neither a string nor a CHAR (a number),
  3722. and two CHARs with no STRING on either side. *)
  3723. VAR dest : CARDINAL ;
  3724. BEGIN
  3725. (* A placeholder for the paths below that bail out through Err. Err only
  3726. records the first error and does not unwind, so the caller goes on to use
  3727. res regardless: after `Err (ETypeErr)' in the guard, ParseAdd hands res
  3728. straight back to IoCall, to Assignment or to an enclosing concatenation.
  3729. The placeholder's SHAPE therefore matters. It must not claim to be a
  3730. STRING, because the string consumers reach symtab[res.idx] for any TString
  3731. (IoCall's `writeln' arm does) and idx is not a symbol here; and it must be
  3732. something LoadAtom can fold, because the numeric consumers will try. The
  3733. integer 0 is both. Leaving kind-2/TString here, as this once did, turned
  3734. `writeln('a' + 'b')' - two CHARs, a type error - into a compiler segfault
  3735. instead of an error message. *)
  3736. res.kind := 0 ;
  3737. res.imm := 0 ;
  3738. res.cls := TScalar ;
  3739. res.boff := 0 ;
  3740. res.chr := FALSE ;
  3741. IF (op # OpAdd) OR NOT StrOperand (left) OR NOT StrOperand (right) OR
  3742. ((left.cls # TString) AND (right.cls # TString)) THEN
  3743. Err (ETypeErr) ; (* a STRING on the wrong operator, a number,
  3744. or two CHARs *)
  3745. RETURN
  3746. END ;
  3747. dest := NewScratchStr () ;
  3748. (* dest := left *)
  3749. EmPushStrOperand (left) ;
  3750. EmPushVarAddr (FALSE, (symtab [dest].off) MOD 10000H) ;
  3751. EmMovCxi (255) ;
  3752. DropC (EmCall (TU_StrStore)) ;
  3753. EmAddSp (4) ; (* pop the two addresses strstore took *)
  3754. (* dest := dest + right *)
  3755. EmPushStrOperand (right) ;
  3756. EmPushVarAddr (FALSE, (symtab [dest].off) MOD 10000H) ;
  3757. EmMovCxi (255) ;
  3758. DropC (EmCall (TU_StrCat)) ;
  3759. EmAddSp (4) ;
  3760. res.kind := 1 ;
  3761. res.idx := dest ;
  3762. res.cls := TString ;
  3763. res.boff := 0 ;
  3764. res.chr := FALSE
  3765. END StrConcat ;
  3766. PROCEDURE ParseCmp (VAR r : ERes) ;
  3767. (* "=" | "<>" | "<" | "<=" | ">" | ">=" *)
  3768. VAR op : CARDINAL ;
  3769. left, right : ERes ;
  3770. f : LONGINT ;
  3771. size, base, block, scratch : CARDINAL ;
  3772. BEGIN
  3773. ParseAdd (r) ;
  3774. LOOP
  3775. op := 0 ;
  3776. Skip () ;
  3777. IF CurCh () = '=' THEN
  3778. op := 1 ; DropCh (GetCh ())
  3779. ELSIF (CurCh () = '<') AND (PeekAhead (1) = '>') THEN
  3780. op := 2 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  3781. ELSIF CurCh () = '<' THEN
  3782. IF PeekAhead (1) = '=' THEN
  3783. op := 6 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  3784. ELSE
  3785. op := 3 ; DropCh (GetCh ())
  3786. END
  3787. ELSIF CurCh () = '>' THEN
  3788. IF PeekAhead (1) = '=' THEN
  3789. op := 5 ; DropCh (GetCh ()) ; DropCh (GetCh ())
  3790. ELSE
  3791. op := 4 ; DropCh (GetCh ())
  3792. END
  3793. END ;
  3794. IF op = 0 THEN
  3795. IF KwAhead ("IN") THEN
  3796. (* `e IN s' is at the comparison level in TP3 (TPSRC9 cmpin, 116).
  3797. Only the compile-time case is implemented; see FoldSetIn. *)
  3798. GetWord () ;
  3799. left := r ;
  3800. ParseAdd (right) ;
  3801. FoldSetIn (left, right, r) ;
  3802. RETURN
  3803. END ;
  3804. RETURN
  3805. END ;
  3806. left := r ;
  3807. IF left.cls = TSet THEN
  3808. (* Set comparison. set_cmp returns a mask (bit 0 equal, bit 1 strict
  3809. subset, bit 2 strict superset); the operator selects which bits
  3810. answer it, and the shared EmRelSetcc arm turns the flags into a
  3811. boolean. The left address is pushed before the right operand is
  3812. parsed, for the same DI reason as strings and set arithmetic. *)
  3813. EmPushSetAddr (left) ;
  3814. ParseAdd (right) ;
  3815. IF NOT OK () THEN
  3816. RETURN
  3817. END ;
  3818. IF right.cls # TSet THEN
  3819. Err (ETypeErr) ;
  3820. RETURN
  3821. END ;
  3822. IF NOT SetPairSize (left, right, size, base) THEN
  3823. Err (ENoLib) ;
  3824. RETURN
  3825. END ;
  3826. EmPushSetAddr (right) ;
  3827. EmMovAxi (size) ;
  3828. EmPushAx () ;
  3829. DropC (EmCall (TU_SetCmp)) ;
  3830. EmAddSp (4) ;
  3831. CASE op OF
  3832. 1 : EmMovCxi (1) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3833. | 2 : EmMovCxi (1) ; EmAndAxCx () ; EmXorAl01 () ;
  3834. EmOrAxAx () ; EmRelSetcc (2) ;
  3835. | 3 : EmMovCxi (2) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3836. | 4 : EmMovCxi (4) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3837. | 5 : EmMovCxi (5) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2) ;
  3838. | 6 : EmMovCxi (3) ; EmAndAxCx () ; EmOrAxAx () ; EmRelSetcc (2)
  3839. END ;
  3840. r.kind := 2 ;
  3841. r.cls := TBool
  3842. ELSIF left.cls = TString THEN
  3843. (* A string comparison: cmpstr, the entry TPSRC9's vcompare (133)
  3844. reaches through TPSRC2's csteq/cstne/cstge/cstle/cstg/cstl
  3845. wrappers (890-925).
  3846. The operands are pushed as ADDRESSES, before the right-hand parse
  3847. can clobber DI. This is why the left is pushed here rather than
  3848. through SaveLeft: a kind-5 left is a string FIELD whose address is
  3849. in DI, and SaveLeft's job is to materialise a scalar in AX, which
  3850. would read the length byte as a number (or, for a whole string,
  3851. refuse it). `if Sheet[UFX,UFY].Contents <> '' then' was exactly
  3852. that: the CALC rung after Write(Value:FW:DEC).
  3853. cmpstr returns AX = -1/0/+1 (see Runtime.mod for why a value and
  3854. not flags), so the second half is exactly the numeric arm's: put
  3855. the sign in the flags and let the shared six-arm table answer.
  3856. EmOrAxAx is OR AX,AX rather than EmCmpAxi(0) on purpose: tests/
  3857. check_8086.py reads every `CMP AX,0000' in the program region as
  3858. an IF/WHILE/REPEAT test and requires a branch right after it, and
  3859. a comparison used as a VALUE (writeln(a = b)) is not one. *)
  3860. EmPushStrAddr (left) ;
  3861. ParseAdd (right) ;
  3862. IF (right.cls = TChar) OR ((right.cls = TScalar) AND right.chr) THEN
  3863. (* `S <> Chr($FF)' / `S <> 'x'': a CHAR is a one-character string.
  3864. Materialise it as a counted scratch so cmpstr sees the same
  3865. layout as every other string operand. *)
  3866. scratch := NewScratchStr () ;
  3867. IF right.kind = 0 THEN
  3868. EmMovAxi (W16 (right.imm)) ;
  3869. EmStoreVar (FALSE, (symtab [scratch].off + 1) MOD 10000H, 1) ;
  3870. EmMovAxi (1) ;
  3871. EmStoreVar (FALSE, symtab [scratch].off, 1)
  3872. ELSE
  3873. LoadAtom (right) ;
  3874. EmStoreVar (FALSE, (symtab [scratch].off + 1) MOD 10000H, 1) ;
  3875. EmMovAxi (1) ;
  3876. EmStoreVar (FALSE, symtab [scratch].off, 1)
  3877. END ;
  3878. right.kind := 1 ; right.idx := scratch ; right.cls := TString ;
  3879. right.sz := 256 ; right.boff := 0
  3880. END ;
  3881. IF right.cls # TString THEN
  3882. Err (ETypeErr) ; (* a string compared with a number *)
  3883. RETURN
  3884. END ;
  3885. EmPushStrAddr (right) ;
  3886. DropC (EmCall (TU_CmpStr)) ;
  3887. EmAddSp (4) ;
  3888. EmOrAxAx () ;
  3889. EmRelSetcc (op) ;
  3890. r.kind := 2 ;
  3891. r.cls := TBool
  3892. ELSIF left.cls = TReal THEN
  3893. (* REAL comparison: recmp takes one 12-byte block holding a then b.
  3894. SaveLeft parks the left address; the right is copied first when it
  3895. is DI-addressed, before the block copy clobbers DI. *)
  3896. SaveLeft (left) ;
  3897. ParseAdd (right) ;
  3898. IF NOT OK () THEN
  3899. RETURN
  3900. END ;
  3901. IF (right.cls = TScalar) AND (right.kind = 0) THEN
  3902. scratch := NewScratchReal () ;
  3903. EmCopyRealTo (symtab [scratch].off, right) ;
  3904. right.idx := scratch ; right.kind := 1 ; right.cls := TReal ;
  3905. right.sz := 6 ; right.boff := 0
  3906. END ;
  3907. IF right.cls # TReal THEN
  3908. Err (ETypeErr) ;
  3909. RETURN
  3910. END ;
  3911. block := NewScratchRealBlock () ;
  3912. IF right.kind = 5 THEN
  3913. EmCopyRealTo (symtab [block].off + 6, right) ;
  3914. EmCopyRealTo (symtab [block].off, left)
  3915. ELSE
  3916. EmCopyRealTo (symtab [block].off, left) ;
  3917. EmCopyRealTo (symtab [block].off + 6, right)
  3918. END ;
  3919. EmPushVarAddr (FALSE, symtab [block].off) ;
  3920. DropC (EmCall (TU_RealCmp)) ;
  3921. EmAddSp (2) ;
  3922. EmOrAxAx () ;
  3923. EmRelSetcc (op) ;
  3924. r.kind := 2 ;
  3925. r.cls := TBool
  3926. ELSE
  3927. SaveLeft (left) ;
  3928. ParseAdd (right) ;
  3929. IF (right.cls = TString) OR (right.cls = TSet) THEN
  3930. Err (ETypeErr) ; (* a number compared with a string/set *)
  3931. RETURN
  3932. END ;
  3933. IF (left.kind = 0) AND (right.kind = 0) THEN
  3934. IF ConstCmp (op, left.imm, right.imm, f) THEN
  3935. r.kind := 0 ;
  3936. r.imm := f ;
  3937. r.cls := TBool
  3938. ELSE
  3939. r.kind := 0 ;
  3940. r.imm := 0 ;
  3941. r.cls := TBool
  3942. END
  3943. ELSE
  3944. LoadPair (left, right) ;
  3945. EmCmpAxCx () ;
  3946. EmRelSetcc (op) ;
  3947. r.kind := 2 ;
  3948. r.cls := TBool
  3949. END
  3950. END
  3951. END
  3952. END ParseCmp ;
  3953. PROCEDURE ParseAdd (VAR r : ERes) ;
  3954. VAR op : CARDINAL ;
  3955. left, right : ERes ;
  3956. BEGIN
  3957. ParseMul (r) ;
  3958. LOOP
  3959. op := 0 ;
  3960. Skip () ;
  3961. IF CurCh () = '+' THEN
  3962. op := OpAdd ; DropCh (GetCh ())
  3963. ELSIF CurCh () = '-' THEN
  3964. op := OpSub ; DropCh (GetCh ())
  3965. ELSIF KwAhead ("OR") THEN
  3966. GetWord () ;
  3967. op := TkOr
  3968. ELSE
  3969. RETURN
  3970. END ;
  3971. left := r ;
  3972. IF left.cls = TSet THEN
  3973. (* Set union/difference. The left address goes on the stack BEFORE
  3974. the right is parsed: a DI-addressed set field must survive the
  3975. right-hand expression. `OR' is boolean, not set union. *)
  3976. IF (op = OpAdd) OR (op = OpSub) THEN
  3977. EmPushSetAddr (left) ;
  3978. ParseMul (right) ;
  3979. IF NOT OK () THEN
  3980. RETURN
  3981. END ;
  3982. IF right.cls # TSet THEN
  3983. Err (ETypeErr) ;
  3984. RETURN
  3985. END ;
  3986. IF right.kind = 9 THEN
  3987. MaterializeDyn (right, left.setbase, left.setsize)
  3988. END ;
  3989. IF op = OpAdd THEN
  3990. SetBinEmit (1, left, right, r)
  3991. ELSE
  3992. SetBinEmit (2, left, right, r)
  3993. END
  3994. ELSE
  3995. Err (ETypeErr) ;
  3996. RETURN
  3997. END
  3998. ELSE
  3999. SaveLeft (left) ;
  4000. ParseMul (right) ;
  4001. IF NOT OK () THEN
  4002. RETURN
  4003. END ;
  4004. IF right.cls = TSet THEN
  4005. Err (ETypeErr) ;
  4006. RETURN
  4007. END ;
  4008. BinOpEmit (op, left, right, r)
  4009. END
  4010. END
  4011. END ParseAdd ;
  4012. PROCEDURE ParseMul (VAR r : ERes) ;
  4013. VAR op : CARDINAL ;
  4014. left, right : ERes ;
  4015. BEGIN
  4016. ParseNeg (r) ;
  4017. LOOP
  4018. op := 0 ;
  4019. Skip () ;
  4020. IF CurCh () = '*' THEN
  4021. op := OpMul ; DropCh (GetCh ()) (* OpAdd here meant a*b -> a+b *)
  4022. ELSIF CurCh () = '/' THEN
  4023. op := OpDiv ; DropCh (GetCh ())
  4024. ELSIF KwAhead ("DIV") THEN
  4025. GetWord () ; op := TkDiv
  4026. ELSIF KwAhead ("MOD") THEN
  4027. GetWord () ; op := TkMod
  4028. ELSIF KwAhead ("AND") THEN
  4029. GetWord () ; op := TkAnd
  4030. ELSIF KwAhead ("SHL") THEN
  4031. GetWord () ; op := OpShl
  4032. ELSIF KwAhead ("SHR") THEN
  4033. GetWord () ; op := OpShr
  4034. ELSE
  4035. RETURN
  4036. END ;
  4037. left := r ;
  4038. IF left.cls = TSet THEN
  4039. (* Set intersection. Same push-before-the-right rule as ParseAdd. *)
  4040. IF op = OpMul THEN
  4041. EmPushSetAddr (left) ;
  4042. ParseNeg (right) ;
  4043. IF NOT OK () THEN
  4044. RETURN
  4045. END ;
  4046. IF right.cls # TSet THEN
  4047. Err (ETypeErr) ;
  4048. RETURN
  4049. END ;
  4050. SetBinEmit (0, left, right, r)
  4051. ELSE
  4052. Err (ETypeErr) ;
  4053. RETURN
  4054. END
  4055. ELSE
  4056. SaveLeft (left) ;
  4057. ParseNeg (right) ;
  4058. IF NOT OK () THEN
  4059. RETURN
  4060. END ;
  4061. IF right.cls = TSet THEN
  4062. Err (ETypeErr) ;
  4063. RETURN
  4064. END ;
  4065. BinOpEmit (op, left, right, r)
  4066. END
  4067. END
  4068. END ParseMul ;
  4069. PROCEDURE ParseNeg (VAR r : ERes) ;
  4070. BEGIN
  4071. Skip () ;
  4072. IF CurCh () = '+' THEN
  4073. DropCh (GetCh ()) ;
  4074. ParseNeg (r) ;
  4075. RETURN
  4076. ELSIF CurCh () = '-' THEN
  4077. DropCh (GetCh ()) ;
  4078. ParseNeg (r) ;
  4079. IF r.cls = TReal THEN
  4080. r.idx := NewScratchReal () ;
  4081. EmCopyRealTo (symtab [r.idx].off, r) ;
  4082. EmXorByteGlob (symtab [r.idx].off + 5, 80H) ;
  4083. r.kind := 1 ;
  4084. r.cls := TReal ;
  4085. r.sz := 6 ;
  4086. r.boff := 0
  4087. ELSIF r.kind = 0 THEN
  4088. r.imm := VAL (LONGINT, W16 (0 - r.imm))
  4089. ELSE
  4090. LoadAtom (r) ;
  4091. EmNegAx () ;
  4092. r.kind := 2
  4093. END ;
  4094. RETURN
  4095. ELSIF KwAhead ("NOT") THEN
  4096. GetWord () ;
  4097. ParseNeg (r) ;
  4098. (* TPSRC9 neglevel branches on the operand's type before emitting
  4099. anything: CMP CL,#$0A (integer) -> NOT AX, CMP CL,#$0B (boolean)
  4100. -> XOR AL,#01, anything else -> error. This used to emit NOT AX
  4101. for both, so `not (a = a)' produced 0FFFEh and the runtime's
  4102. wrbool test ([BP+4] <> 0) read it as TRUE.
  4103. The integer constant fold stays: the 16 bits are the same either
  4104. way. A boolean constant must not go through BitNot - 0FFFFH-1 is
  4105. 0FFFEh, which wrbool also reads as TRUE - so it is loaded and
  4106. xor-ed, exactly as neglevel does it. *)
  4107. IF r.cls = TBool THEN
  4108. LoadAtom (r) ;
  4109. EmXorAl01 () ;
  4110. r.kind := 2
  4111. ELSIF r.cls = TScalar THEN
  4112. IF r.kind = 0 THEN
  4113. r.imm := BitNot (r.imm)
  4114. ELSE
  4115. LoadAtom (r) ;
  4116. EmNotAx () ;
  4117. r.kind := 2
  4118. END
  4119. ELSE
  4120. Err (ETypeErr)
  4121. END ;
  4122. RETURN
  4123. END ;
  4124. ParseAtom (r)
  4125. END ParseNeg ;
  4126. PROCEDURE WithLookup (nm : ARRAY OF CHAR ; VAR r : ERes) : BOOLEAN ;
  4127. (* Resolve a bare identifier against the active WITH scopes, innermost first.
  4128. ISO Pascal lets a record field shadow an outer declaration, so this is
  4129. consulted BEFORE Search. The result is filled exactly as a variable
  4130. reference: kind 1 (base symbol + boff) for a record variable or a
  4131. constant-index element, kind 5 (address in DI) for a run-time-indexed one,
  4132. where the base is reloaded from the WITH slot because the body may have
  4133. used DI. *)
  4134. VAR w, fi : CARDINAL ;
  4135. found : BOOLEAN ;
  4136. BEGIN
  4137. w := withN ;
  4138. WHILE w > 0 DO
  4139. DEC (w) ;
  4140. fi := withFF [w] ;
  4141. found := FALSE ;
  4142. WHILE (fi < withFL [w]) AND NOT found DO
  4143. IF NameEq (fields [fi].name, nm) THEN
  4144. found := TRUE
  4145. ELSE
  4146. INC (fi)
  4147. END
  4148. END ;
  4149. IF found THEN
  4150. IF withIsDI [w] THEN
  4151. EmLdDiGlob (withSlot [w]) ;
  4152. IF fields [fi].off # 0 THEN
  4153. EmAddDiImm (fields [fi].off)
  4154. END ;
  4155. r.kind := 5
  4156. ELSE
  4157. r.kind := 1 ;
  4158. r.setoff := symtab [withIdx [w]].off ;
  4159. r.boff := W16 (VAL (LONGINT, withBoff [w])
  4160. + VAL (LONGINT, fields [fi].off))
  4161. END ;
  4162. r.idx := withIdx [w] ;
  4163. r.cls := fields [fi].cls ;
  4164. r.sz := fields [fi].size ;
  4165. r.fldFirst := fields [fi].fldFirst ;
  4166. r.fldLast := fields [fi].fldLast ;
  4167. r.arrIdx := fields [fi].arrIdx ;
  4168. r.ptrElem := fields [fi].ptrElem ;
  4169. IF r.cls = TSet THEN
  4170. r.setbase := fields [fi].elem ;
  4171. r.setsize := fields [fi].size
  4172. END ;
  4173. r.chr := FALSE ;
  4174. RETURN TRUE
  4175. END
  4176. END ;
  4177. RETURN FALSE
  4178. END WithLookup ;
  4179. PROCEDURE ParseAtom (VAR r : ERes) ;
  4180. (* const | variable | func(params) | '(' expr ')' | '[' set ']' *)
  4181. VAR idx : CARDINAL ;
  4182. strf : BOOLEAN ;
  4183. quoted : BOOLEAN ;
  4184. ssize, sbase : CARDINAL ;
  4185. sbuf : ARRAY [0..31] OF CHAR ;
  4186. uj1, uj2 : CARDINAL ;
  4187. litStart, scratch, asciiAddr, savePos : CARDINAL ;
  4188. dyn : ERes ;
  4189. BEGIN
  4190. r.chr := FALSE ; (* default: not a quoted char literal *)
  4191. Skip () ;
  4192. IF CurCh () = '(' THEN
  4193. DropCh (GetCh ()) ;
  4194. ParseExpr (r) ;
  4195. ExpectDelim (')', ENoSemi) ;
  4196. RETURN
  4197. END ;
  4198. IF CurCh () = '[' THEN
  4199. (* An inline set literal. A literal whose first element is a variable
  4200. (e.g. `[Storage[Index]]') is dynamic: evaluate the element once and
  4201. mark it kind 9, to be materialised when the target's base/size is
  4202. known. Constant literals stay code-resident. *)
  4203. savePos := srcPos ;
  4204. DropCh (GetCh ()) ;
  4205. Skip () ;
  4206. IF Alpha (CurCh ()) THEN
  4207. GetWord () ;
  4208. IF Search (wrd, idx) AND ((symtab [idx].tag = KVar) OR
  4209. (symtab [idx].tag = KFunc)) THEN
  4210. srcPos := savePos + 1 ;
  4211. ParseExpr (dyn) ;
  4212. ExpectDelim (']', ENoSemi) ;
  4213. IF NOT OK () THEN
  4214. RETURN
  4215. END ;
  4216. IF (dyn.cls # TScalar) AND (dyn.cls # TChar) AND
  4217. (dyn.cls # TEnum) THEN
  4218. Err (ETypeErr) ;
  4219. RETURN
  4220. END ;
  4221. scratch := NewScratchWord () ;
  4222. LoadAtom (dyn) ;
  4223. EmStoreVar (FALSE, symtab [scratch].off, 2) ;
  4224. r.kind := 9 ;
  4225. r.cls := TSet ;
  4226. r.setbase := 0 ;
  4227. r.setsize := 0 ;
  4228. r.dynElem := scratch ;
  4229. RETURN
  4230. END
  4231. END ;
  4232. srcPos := savePos ;
  4233. ssize := 32 ;
  4234. sbase := 0 ;
  4235. IF RdSetLiteral (sbase, ssize, sbuf) THEN
  4236. r.kind := 7 ;
  4237. r.cls := TSet ;
  4238. r.setoff := EmSetBlob (sbuf, ssize) ;
  4239. r.setbase := sbase ;
  4240. r.setsize := ssize
  4241. END ;
  4242. RETURN
  4243. END ;
  4244. IF (CurCh () = '$') OR (Digit (CurCh ())) OR (ORD (CurCh ()) = AposC)
  4245. OR (CurCh () = '^') OR (CurCh () = '#') THEN
  4246. (* remember that this was a quoted literal BEFORE RdConst consumes it:
  4247. RdConst reports a 1-character literal as TScalar (its char code),
  4248. which is right for "c := 'a'" but would make writeln('a') print 97.
  4249. Mark it so the writer picks the char entry, not the integer one. A
  4250. caret (^M) or decimal (#27) character constant is a character too. *)
  4251. quoted := (ORD (CurCh ()) = AposC) OR (CurCh () = '^')
  4252. OR (CurCh () = '#') ;
  4253. litStart := srcPos ;
  4254. RdConst (r.imm, r.cls, strf) ;
  4255. r.chr := quoted AND (r.cls = TScalar) AND NOT strf ;
  4256. IF r.cls = TReal THEN
  4257. (* A REAL literal is converted at run time by ascreal. Keep the
  4258. literal text as an ASCIIZ code blob and call real_val into a
  4259. six-byte scratch; the expression then behaves like a REAL
  4260. variable whose address is the scratch. *)
  4261. scratch := NewScratchReal () ;
  4262. asciiAddr := EmAsciiZLiteral (litStart, srcPos) ;
  4263. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  4264. EmPushVarAddr (FALSE, asciiAddr) ;
  4265. DropC (EmCall (TU_RealVal)) ;
  4266. EmAddSp (4) ;
  4267. EmLeaDi (FALSE, symtab [scratch].off) ;
  4268. r.idx := scratch ;
  4269. r.kind := 5 ;
  4270. r.cls := TReal ;
  4271. r.sz := 6 ;
  4272. RETURN
  4273. END ;
  4274. IF strf THEN
  4275. (* A string literal is legal here as a *value* - it is not rejected
  4276. at this point, because writeln('hi') needs it and IoCall is the
  4277. only place that knows how to emit one. Everywhere else the
  4278. literal has to end up as a machine word, and that is caught by
  4279. LoadAtom, which refuses a TString. *)
  4280. r.strx := rdStrX ;
  4281. r.kind := 3 ;
  4282. RETURN
  4283. END ;
  4284. r.kind := 0 ;
  4285. RETURN
  4286. END ;
  4287. IF NOT Alpha (CurCh ()) THEN
  4288. Err (EUnknown) ;
  4289. RETURN
  4290. END ;
  4291. GetWord () ;
  4292. IF WithLookup (wrd, r) THEN
  4293. ParseSub (r) ;
  4294. RETURN
  4295. END ;
  4296. IF NameEq (wrd, "NIL") THEN
  4297. r.kind := 0 ;
  4298. r.cls := TPtr ;
  4299. r.imm := 0 ;
  4300. r.sz := 2 ;
  4301. RETURN
  4302. END ;
  4303. IF NameEq (wrd, "ADDR") THEN
  4304. (* Addr(x): the run-time address of x, as a pointer value in AX. TP3
  4305. spells this Addr; the @ operator is a later Turbo spelling and is not
  4306. part of the TP3 subset yet. *)
  4307. IF NOT MatchDelim ('(') THEN
  4308. Err (ENoSemi) ;
  4309. RETURN
  4310. END ;
  4311. Skip () ;
  4312. IF NOT Alpha (CurCh ()) THEN
  4313. Err (EUnknown) ;
  4314. RETURN
  4315. END ;
  4316. GetWord () ;
  4317. ParseVar (r) ;
  4318. ExpectDelim (')', ENoSemi) ;
  4319. IF NOT OK () THEN
  4320. RETURN
  4321. END ;
  4322. IF r.kind = 5 THEN
  4323. EmMovAxDi ()
  4324. ELSIF r.kind = 1 THEN
  4325. IF symtab [r.idx].isVar THEN
  4326. LoadSymVar (r.idx, symtab [r.idx].off, 2)
  4327. ELSE
  4328. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H) ;
  4329. EmPopAx ()
  4330. END
  4331. ELSE
  4332. Err (ETypeErr) ;
  4333. RETURN
  4334. END ;
  4335. r.kind := 2 ;
  4336. r.cls := TPtr ;
  4337. r.sz := 2 ;
  4338. r.idx := 0 ;
  4339. RETURN
  4340. END ;
  4341. IF NameEq (wrd, "SEG") THEN
  4342. (* TP3 Seg(x): the current DS. The argument is parsed for its type
  4343. and side effects; the .COM has one segment. *)
  4344. IF NOT MatchDelim ('(') THEN
  4345. Err (ENoSemi) ;
  4346. RETURN
  4347. END ;
  4348. ParseExpr (r) ;
  4349. ExpectDelim (')', ENoSemi) ;
  4350. IF NOT OK () THEN
  4351. RETURN
  4352. END ;
  4353. EmMovAxDs () ; (* MOV AX,DS *)
  4354. r.kind := 2 ; r.cls := TScalar ; r.sz := 2 ;
  4355. RETURN
  4356. END ;
  4357. IF NameEq (wrd, "OFS") THEN
  4358. (* TP3 Ofs(x): the run-time address of x. *)
  4359. IF NOT MatchDelim ('(') THEN
  4360. Err (ENoSemi) ;
  4361. RETURN
  4362. END ;
  4363. Skip () ;
  4364. IF NOT Alpha (CurCh ()) THEN
  4365. Err (EUnknown) ;
  4366. RETURN
  4367. END ;
  4368. GetWord () ;
  4369. ParseVar (r) ;
  4370. ExpectDelim (')', ENoSemi) ;
  4371. IF NOT OK () THEN
  4372. RETURN
  4373. END ;
  4374. IF r.kind = 5 THEN
  4375. EmMovAxDi ()
  4376. ELSIF r.kind = 1 THEN
  4377. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H) ;
  4378. EmPopAx ()
  4379. ELSE
  4380. Err (ETypeErr) ;
  4381. RETURN
  4382. END ;
  4383. r.kind := 2 ; r.cls := TScalar ; r.sz := 2 ;
  4384. RETURN
  4385. END ;
  4386. IF NOT Search (wrd, idx) THEN
  4387. Err (EUnknown) ;
  4388. RETURN
  4389. END ;
  4390. IF (symtab [idx].tag = KVar) AND (symtab [idx].resvar # 0) THEN
  4391. (* A function's result variable shadows its own name inside the body;
  4392. a following '(' means this is a recursive call, not a read of the
  4393. result slot. *)
  4394. Skip () ;
  4395. IF CurCh () = '(' THEN
  4396. idx := symtab [idx].resvar
  4397. END
  4398. END ;
  4399. IF symtab [idx].tag = KConst THEN
  4400. r.kind := 0 ;
  4401. r.imm := symtab [idx].lval ;
  4402. r.cls := symtab [idx].cls ;
  4403. RETURN
  4404. ELSIF symtab [idx].tag = KFunc THEN
  4405. ParseCall (idx) ;
  4406. IF symtab [idx].cls = TReal THEN
  4407. (* A REAL function returns into the caller's six-byte scratch; DI is
  4408. reloaded with its address so real consumers can use the value. *)
  4409. EmLeaDi (FALSE, symtab [callScratch].off) ;
  4410. r.kind := 5 ;
  4411. r.cls := TReal ;
  4412. r.sz := 6
  4413. ELSE
  4414. r.kind := 2 ;
  4415. r.cls := symtab [idx].cls
  4416. END ;
  4417. RETURN
  4418. ELSIF (symtab [idx].tag = KType) AND (symtab [idx].cls = TChar)
  4419. AND (CurCh () = '(') THEN
  4420. (* TP3 also spells the character cast `Char(n)', not only `Chr(n)'. *)
  4421. DropCh (GetCh ()) ;
  4422. ParseExpr (r) ;
  4423. ExpectDelim (')', ENoSemi) ;
  4424. IF NOT OK () THEN
  4425. RETURN
  4426. END ;
  4427. IF (r.cls # TScalar) AND (r.cls # TChar) THEN
  4428. Err (ETypeErr) ;
  4429. RETURN
  4430. END ;
  4431. IF r.kind = 0 THEN
  4432. r.imm := VAL (LONGINT, W16 (r.imm) MOD 256) ;
  4433. r.chr := TRUE
  4434. ELSE
  4435. LoadAtom (r) ;
  4436. r.kind := 2 ;
  4437. r.chr := FALSE
  4438. END ;
  4439. r.cls := TChar ;
  4440. r.sz := 1 ;
  4441. RETURN
  4442. ELSIF (symtab [idx].tag = KVar) OR (symtab [idx].tag = KType) THEN
  4443. (* ParseVar initialises boff to 0 and then ParseSub folds any constant
  4444. subscript into it. Do NOT reset boff here: doing so after ParseVar
  4445. returns threw away the subscript, so s[i] read/wrote off+0 (the
  4446. length byte) while still reporting itself as a char. This was
  4447. latent for arrays because an array cannot be declared yet, so no
  4448. fixture had ever exercised the fold. *)
  4449. ParseVar (r) ;
  4450. RETURN
  4451. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Chr) THEN
  4452. (* Chr(n): the character with code n. A constant folds; otherwise the
  4453. integer is loaded and its class becomes CHAR. The value is one byte,
  4454. so an assignment stores AL and a CHAR writer uses AL. *)
  4455. IF NOT MatchDelim ('(') THEN
  4456. Err (ENoSemi) ;
  4457. RETURN
  4458. END ;
  4459. ParseExpr (r) ;
  4460. ExpectDelim (')', ENoSemi) ;
  4461. IF NOT OK () THEN
  4462. RETURN
  4463. END ;
  4464. IF (r.cls # TScalar) AND (r.cls # TChar) THEN
  4465. Err (ETypeErr) ;
  4466. RETURN
  4467. END ;
  4468. IF r.kind = 0 THEN
  4469. r.imm := VAL (LONGINT, W16 (r.imm) MOD 256) ;
  4470. r.chr := TRUE
  4471. ELSE
  4472. LoadAtom (r) ;
  4473. r.kind := 2 ;
  4474. r.chr := FALSE
  4475. END ;
  4476. r.cls := TChar ;
  4477. r.sz := 1 ;
  4478. RETURN
  4479. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Eof) THEN
  4480. (* Eof(F): text/typed-file end of file. The runtime compares the DOS
  4481. current position with the file size and also remembers a ^Z seen by
  4482. the text reader. *)
  4483. IF NOT MatchDelim ('(') THEN
  4484. Err (ENoSemi) ;
  4485. RETURN
  4486. END ;
  4487. ParseExpr (r) ;
  4488. IF OK () THEN
  4489. IF (r.kind = 1) AND
  4490. ((r.cls = TText) OR (r.cls = TFile)) THEN
  4491. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H) ;
  4492. DropC (EmCall (TU_FileEof)) ;
  4493. EmAddSp (2) ;
  4494. r.kind := 2 ;
  4495. r.cls := TBool ;
  4496. r.chr := FALSE
  4497. ELSE
  4498. Err (ETypeErr)
  4499. END
  4500. END ;
  4501. ExpectDelim (')', ENoSemi) ;
  4502. RETURN
  4503. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_IoResult) THEN
  4504. (* IOResult: the last file operation's DOS status; reading it clears the
  4505. slot in the runtime. It is a no-argument function. *)
  4506. IF MatchDelim ('(') THEN
  4507. ExpectDelim (')', ENoSemi)
  4508. END ;
  4509. DropC (EmCall (TU_IoResult)) ;
  4510. r.kind := 2 ;
  4511. r.cls := TScalar ;
  4512. r.chr := FALSE ;
  4513. RETURN
  4514. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_UpCase) THEN
  4515. (* UpCase(c): a CHAR function. A constant folds; otherwise AL is
  4516. mapped 'a'..'z' -> 'A'..'Z' and left alone outside that range. The
  4517. test is JL/JG (signed), which is the same answer for 0..127 chars and
  4518. leaves 128..255 unchanged; it also stays inside the four branch
  4519. conditions this compiler declares (check_8086's set). *)
  4520. IF NOT MatchDelim ('(') THEN
  4521. Err (ENoSemi) ;
  4522. RETURN
  4523. END ;
  4524. ParseExpr (r) ;
  4525. ExpectDelim (')', ENoSemi) ;
  4526. IF NOT OK () THEN
  4527. RETURN
  4528. END ;
  4529. IF (r.cls # TChar) AND (r.cls # TScalar) THEN
  4530. Err (ETypeErr) ; (* the STRING form is not implemented *)
  4531. RETURN
  4532. END ;
  4533. IF r.kind = 0 THEN
  4534. IF (r.imm >= VAL (LONGINT, ORD ('a')))
  4535. AND (r.imm <= VAL (LONGINT, ORD ('z'))) THEN
  4536. r.imm := r.imm - 32
  4537. END ;
  4538. r.kind := 0 ;
  4539. r.cls := TChar ;
  4540. r.chr := TRUE ;
  4541. RETURN
  4542. END ;
  4543. LoadAtom (r) ;
  4544. EmCmpAlImm (ORD ('a')) ;
  4545. uj1 := EmJcc (8CH, 0) ; (* JL -> leave it *)
  4546. EmCmpAlImm (ORD ('z')) ;
  4547. uj2 := EmJcc (8FH, 0) ; (* JG -> leave it *)
  4548. EmSubAlImm (32) ;
  4549. SetPatTgt (uj1, pc) ;
  4550. SetPatTgt (uj2, pc) ;
  4551. EmMovAh0 () ;
  4552. r.kind := 2 ;
  4553. r.cls := TChar ;
  4554. r.chr := FALSE ;
  4555. RETURN
  4556. ELSIF (symtab [idx].tag = KBuiltin) AND
  4557. ((symtab [idx].cls = BI_Succ) OR (symtab [idx].cls = BI_Pred)) THEN
  4558. (* Succ(x) / Pred(x) on an ordinal. CALC's `FX := Succ(FX)' is the
  4559. first use. A constant folds; otherwise load the value and INC/DEC
  4560. it, preserving the operand's class so an assignment back to a CHAR
  4561. subrange stores one byte as before. *)
  4562. IF NOT MatchDelim ('(') THEN
  4563. Err (ENoSemi) ;
  4564. RETURN
  4565. END ;
  4566. ParseExpr (r) ;
  4567. ExpectDelim (')', ENoSemi) ;
  4568. IF NOT OK () THEN
  4569. RETURN
  4570. END ;
  4571. IF (r.cls # TChar) AND (r.cls # TScalar) AND (r.cls # TEnum) THEN
  4572. Err (ETypeErr) ;
  4573. RETURN
  4574. END ;
  4575. IF r.kind = 0 THEN
  4576. IF symtab [idx].cls = BI_Succ THEN
  4577. r.imm := VAL (LONGINT, W16 (r.imm + 1))
  4578. ELSE
  4579. r.imm := VAL (LONGINT, W16 (r.imm - 1))
  4580. END
  4581. ELSE
  4582. LoadAtom (r) ;
  4583. IF symtab [idx].cls = BI_Succ THEN
  4584. EmIncAx ()
  4585. ELSE
  4586. EmDecAx ()
  4587. END ;
  4588. r.kind := 2
  4589. END ;
  4590. r.chr := FALSE ;
  4591. RETURN
  4592. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_KeyPressed) THEN
  4593. (* KeyPressed: a boolean FUNCTION with no argument, and TP3 lets it be
  4594. written without a parameter list (`until KeyPressed'). *)
  4595. EmCrtSeq (CrtKey) ;
  4596. r.kind := 2 ;
  4597. r.cls := TBool ;
  4598. r.chr := FALSE ;
  4599. RETURN
  4600. ELSIF (symtab [idx].tag = KBuiltin) AND
  4601. ((symtab [idx].cls = BI_Abs) OR (symtab [idx].cls = BI_Sqrt)
  4602. OR (symtab [idx].cls = BI_Ln) OR (symtab [idx].cls = BI_Exp)
  4603. OR (symtab [idx].cls = BI_Sin) OR (symtab [idx].cls = BI_Cos)
  4604. OR (symtab [idx].cls = BI_Arctan) OR (symtab [idx].cls = BI_Int)) THEN
  4605. IF NOT MatchDelim ('(') THEN
  4606. Err (ENoSemi) ;
  4607. RETURN
  4608. END ;
  4609. ParseExpr (r) ;
  4610. ExpectDelim (')', ENoSemi) ;
  4611. IF NOT OK () THEN
  4612. RETURN
  4613. END ;
  4614. IF (r.cls = TScalar) AND (r.kind = 0) THEN
  4615. scratch := NewScratchReal () ;
  4616. EmCopyRealTo (symtab [scratch].off, r) ;
  4617. r.idx := scratch ; r.kind := 1 ; r.cls := TReal ;
  4618. r.sz := 6 ; r.boff := 0
  4619. END ;
  4620. IF r.cls # TReal THEN
  4621. Err (ETypeErr) ;
  4622. RETURN
  4623. END ;
  4624. IF symtab [idx].cls = BI_Abs THEN
  4625. RealUnary (TU_RealAbs, r)
  4626. ELSIF symtab [idx].cls = BI_Sqrt THEN
  4627. RealUnary (TU_RealSqrt, r)
  4628. ELSIF symtab [idx].cls = BI_Ln THEN
  4629. RealUnary (TU_RealLn, r)
  4630. ELSIF symtab [idx].cls = BI_Exp THEN
  4631. RealUnary (TU_RealExp, r)
  4632. ELSIF symtab [idx].cls = BI_Sin THEN
  4633. RealUnary (TU_RealSin, r)
  4634. ELSIF symtab [idx].cls = BI_Cos THEN
  4635. RealUnary (TU_RealCos, r)
  4636. ELSIF symtab [idx].cls = BI_Arctan THEN
  4637. RealUnary (TU_RealArctan, r)
  4638. ELSE
  4639. RealUnary (TU_RealIntFunc, r)
  4640. END ;
  4641. RETURN
  4642. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Sqr) THEN
  4643. IF NOT MatchDelim ('(') THEN
  4644. Err (ENoSemi) ;
  4645. RETURN
  4646. END ;
  4647. ParseExpr (r) ;
  4648. ExpectDelim (')', ENoSemi) ;
  4649. IF NOT OK () THEN
  4650. RETURN
  4651. END ;
  4652. IF r.cls # TReal THEN
  4653. Err (ETypeErr) ;
  4654. RETURN
  4655. END ;
  4656. RealBin (OpMul, r, r, r) ;
  4657. RETURN
  4658. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Copy) THEN
  4659. (* Copy(S, start, len): a counted-string result in a fresh 256-byte
  4660. scratch. S's address is parked while start/len are parsed, then the
  4661. four words are re-pushed in the runtime's [source,dest,start,len]
  4662. order. *)
  4663. IF NOT MatchDelim ('(') THEN
  4664. Err (ENoSemi) ;
  4665. RETURN
  4666. END ;
  4667. ParseExpr (r) ;
  4668. IF OK () AND (r.cls = TString) AND
  4669. ((r.kind = 1) OR (r.kind = 3) OR (r.kind = 5)) THEN
  4670. EmPushStrAddr (r) ; (* park S *)
  4671. ExpectDelim (',', ENoSemi) ;
  4672. ParseExpr (r) ;
  4673. IF OK () AND ((r.cls = TScalar) OR (r.cls = TChar)) THEN
  4674. LoadAtom (r) ; EmPushAx () ;
  4675. ExpectDelim (',', ENoSemi) ;
  4676. ParseExpr (r) ;
  4677. IF OK () AND ((r.cls = TScalar) OR (r.cls = TChar)) THEN
  4678. LoadAtom (r) ; EmPushAx () ;
  4679. scratch := NewScratchStr () ;
  4680. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  4681. EmPopAx () ; (* dest *)
  4682. EmPopBx () ; (* len *)
  4683. EmPopCx () ; (* start *)
  4684. EmPopDx () ; (* source *)
  4685. EmPushBx () ; EmPushCx () ; EmPushAx () ; EmPushDx () ;
  4686. DropC (EmCall (TU_StrCopy)) ;
  4687. EmAddSp (8) ;
  4688. r.kind := 1 ; r.idx := scratch ; r.cls := TString ;
  4689. r.sz := 256 ; r.boff := 0 ; r.chr := FALSE
  4690. ELSIF OK () THEN
  4691. Err (ETypeErr)
  4692. END
  4693. ELSIF OK () THEN
  4694. Err (ETypeErr)
  4695. END ;
  4696. ExpectDelim (')', ENoSemi)
  4697. ELSIF OK () THEN
  4698. Err (ETypeErr)
  4699. END ;
  4700. RETURN
  4701. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Ord) THEN
  4702. IF NOT MatchDelim ('(') THEN
  4703. Err (ENoSemi) ;
  4704. RETURN
  4705. END ;
  4706. ParseExpr (r) ;
  4707. ExpectDelim (')', ENoSemi) ;
  4708. IF NOT OK () THEN
  4709. RETURN
  4710. END ;
  4711. IF (r.cls # TChar) AND (r.cls # TEnum) AND (r.cls # TScalar)
  4712. AND (r.cls # TBool) THEN
  4713. Err (ETypeErr) ;
  4714. RETURN
  4715. END ;
  4716. LoadAtom (r) ;
  4717. r.kind := 2 ;
  4718. r.cls := TScalar ;
  4719. r.chr := FALSE ;
  4720. RETURN
  4721. ELSIF (symtab [idx].tag = KBuiltin) AND
  4722. ((symtab [idx].cls = BI_Round) OR (symtab [idx].cls = BI_Trunc)) THEN
  4723. IF NOT MatchDelim ('(') THEN
  4724. Err (ENoSemi) ;
  4725. RETURN
  4726. END ;
  4727. ParseExpr (r) ;
  4728. ExpectDelim (')', ENoSemi) ;
  4729. IF NOT OK () THEN
  4730. RETURN
  4731. END ;
  4732. IF r.cls = TReal THEN
  4733. IF r.kind = 5 THEN
  4734. EmPushDi ()
  4735. ELSIF r.kind = 1 THEN
  4736. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  4737. ELSE
  4738. Err (ETypeErr) ;
  4739. RETURN
  4740. END ;
  4741. IF symtab [idx].cls = BI_Round THEN
  4742. EmMovAxi (1)
  4743. ELSE
  4744. EmMovAxi (0)
  4745. END ;
  4746. EmPushAx () ;
  4747. DropC (EmCall (TU_RealToInt)) ;
  4748. EmAddSp (4) ;
  4749. r.kind := 2 ;
  4750. r.cls := TScalar ;
  4751. r.sz := 2 ;
  4752. RETURN
  4753. ELSIF (r.cls = TScalar) OR (r.cls = TChar) OR (r.cls = TEnum) THEN
  4754. LoadAtom (r) ;
  4755. r.kind := 2 ;
  4756. r.cls := TScalar ;
  4757. r.sz := 2 ;
  4758. RETURN
  4759. ELSE
  4760. Err (ETypeErr) ;
  4761. RETURN
  4762. END ;
  4763. ELSIF (symtab [idx].tag = KBuiltin) AND (symtab [idx].cls = BI_Length) THEN
  4764. (* LENGTH (s): the string's length byte, loaded inline. No runtime
  4765. entry, and no address is pushed - a string's value already carries its
  4766. length at [addr]. A literal is emitted inline (EmStrBlob) and the
  4767. byte is read from that image offset. *)
  4768. IF NOT MatchDelim ('(') THEN
  4769. Err (ENoSemi) ;
  4770. RETURN
  4771. END ;
  4772. ParseExpr (r) ;
  4773. ExpectDelim (')', ENoSemi) ;
  4774. IF NOT OK () THEN
  4775. RETURN
  4776. END ;
  4777. IF r.cls # TString THEN
  4778. Err (ETypeErr) ;
  4779. RETURN
  4780. END ;
  4781. IF r.kind = 5 THEN
  4782. (* A string field/element addressed by DI (e.g. a typed-constant
  4783. array element): its length byte is at [DI]. *)
  4784. EmLdAlDi () ; EmMovAh0 ()
  4785. ELSIF (r.kind = 1) AND symtab [r.idx].isVar THEN
  4786. (* VAR string: the slot holds the address; read the length byte
  4787. through that pointer. *)
  4788. IF r.boff # 0 THEN
  4789. Err (ENoLib)
  4790. ELSE
  4791. EmLoadVarIndSym (r.idx, 1)
  4792. END
  4793. ELSIF r.kind = 1 THEN
  4794. LoadSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, 1)
  4795. ELSIF r.kind = 3 THEN
  4796. EmLoadVar (FALSE, EmStrBlob (r), 1)
  4797. ELSE
  4798. Err (ENoLib) ;
  4799. RETURN
  4800. END ;
  4801. EmMovAh0 () ;
  4802. r.kind := 2 ;
  4803. r.cls := TScalar ;
  4804. r.chr := FALSE ;
  4805. RETURN
  4806. ELSE
  4807. Err (EUnknown)
  4808. END
  4809. END ParseAtom ;
  4810. PROCEDURE AddPend (kind, who, place : CARDINAL) ;
  4811. BEGIN
  4812. IF nPend < MaxPend THEN
  4813. pend [nPend].kind := kind ;
  4814. pend [nPend].who := who ;
  4815. pend [nPend].place := place ;
  4816. INC (nPend)
  4817. ELSE
  4818. Err (ECompOvf)
  4819. END
  4820. END AddPend ;
  4821. PROCEDURE ParmSlot (idx, k : CARDINAL) : CARDINAL ;
  4822. (* The stack slot of argument k (0-based) of procedure/function idx, from the
  4823. parameter list recorded on the symbol. Out of range means the source passed
  4824. more arguments than there are parameters: keep the old two-byte assumption
  4825. rather than inventing an arity error here. *)
  4826. VAR pi : CARDINAL ;
  4827. BEGIN
  4828. IF (symtab [idx].parmCount = 0) OR (k >= symtab [idx].parmCount) THEN
  4829. RETURN 2
  4830. END ;
  4831. pi := symtab [idx].parmFirst + k ;
  4832. RETURN ParamSlotBytes (symtab [pi])
  4833. END ParmSlot ;
  4834. PROCEDURE ParmIsVar (idx, k : CARDINAL) : BOOLEAN ;
  4835. (* Is argument k (0-based) of procedure/function idx declared VAR? Out of
  4836. range means the source passed more arguments than there are formals; treat
  4837. it as a value parameter, as ParmSlot already does. *)
  4838. VAR pi : CARDINAL ;
  4839. BEGIN
  4840. IF (symtab [idx].parmCount = 0) OR (k >= symtab [idx].parmCount) THEN
  4841. RETURN FALSE
  4842. END ;
  4843. pi := symtab [idx].parmFirst + k ;
  4844. RETURN symtab [pi].isVar
  4845. END ParmIsVar ;
  4846. PROCEDURE EmCallMost (idx, cleanup : CARDINAL) ;
  4847. (* emit the call to sym 'idx' and clean up `cleanup' bytes of arguments, which
  4848. is the SUM of their slot sizes - not 2 per argument once a slot can be
  4849. larger than a word. *)
  4850. VAR p : CARDINAL ;
  4851. BEGIN
  4852. EmitStaticLink (idx) ;
  4853. IF symtab [idx].defnd THEN
  4854. DropC (EmCall (symtab [idx].goPos))
  4855. ELSE
  4856. p := EmCall (0) ;
  4857. AddPend (1, idx, p)
  4858. END ;
  4859. IF cleanup > 0 THEN
  4860. EmAddSp (cleanup)
  4861. END
  4862. END EmCallMost ;
  4863. PROCEDURE ParseCallArgs (idx : CARDINAL) ;
  4864. (* '(' already consumed: read args ')' then call.
  4865. Each argument is loaded and pushed the moment it has been parsed, which is
  4866. what TPSRC8 cproc/cprlp1/cprlp2 do - CALL exprsave, CALL epushax, one per
  4867. argument, then the CALL. A computed argument (kind 2) exists only in AX,
  4868. so parsing the argument after it would destroy it before the push loop
  4869. ever reached it: `p (a > b, x)' passed x twice.
  4870. Pushing in parse order is also the order the callee is laid out for: the
  4871. first argument goes on first and so ends up farthest from BP, and the LAST
  4872. declared parameter is the one at BP+4 (RESUME-TP3.md 3.11). *)
  4873. VAR arg : ERes ;
  4874. nArgs, cleanup, slot, nCopy : CARDINAL ;
  4875. BEGIN
  4876. nArgs := 0 ;
  4877. cleanup := 0 ;
  4878. IF CurCh () = ')' THEN
  4879. DropCh (GetCh ())
  4880. ELSE
  4881. LOOP
  4882. IF nArgs >= 16 THEN
  4883. Err (ECompOvf) ;
  4884. EXIT
  4885. END ;
  4886. ParseExpr (arg) ;
  4887. slot := ParmSlot (idx, nArgs) ;
  4888. IF ParmIsVar (idx, nArgs) THEN
  4889. (* VAR formal: push the actual's ADDRESS. Only a variable has
  4890. one; a constant or a computed value is refused rather than
  4891. quietly passed by value. An element with a constant index
  4892. (`s[i]') is a variable too: its address is off+boff, which
  4893. EmPushVarAddr forms directly. *)
  4894. IF arg.kind = 5 THEN
  4895. (* An element/field address already in DI, including a record
  4896. field passed as a VAR actual: Sheet[I,J].Contents. *)
  4897. EmPushDi ()
  4898. ELSIF arg.kind # 1 THEN
  4899. Err (ENoLib) ;
  4900. EXIT
  4901. ELSIF symtab [arg.idx].isVar THEN
  4902. (* Passing a VAR parameter on: its slot already HOLDS the
  4903. address, so push that, not the address of the slot. *)
  4904. IF arg.boff # 0 THEN
  4905. Err (ENoLib) ;
  4906. EXIT
  4907. END ;
  4908. LoadSymVar (arg.idx, symtab [arg.idx].off, 2) ;
  4909. EmPushAx ()
  4910. ELSE
  4911. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  4912. END
  4913. ELSIF slot > 2 THEN
  4914. (* A STRING VALUE parameter: push its whole slot, copied.
  4915. `copyN' is at most the slot: an actual shorter than the formal
  4916. leaves the tail untouched, which is harmless because the length
  4917. byte bounds every read. *)
  4918. IF arg.cls = TString THEN
  4919. nCopy := slot ;
  4920. IF arg.kind = 1 THEN
  4921. IF VarSize (arg) < nCopy THEN
  4922. nCopy := VarSize (arg)
  4923. END
  4924. ELSIF arg.kind = 3 THEN
  4925. IF strLen [arg.strx] + 1 < nCopy THEN
  4926. nCopy := strLen [arg.strx] + 1
  4927. END
  4928. ELSE
  4929. Err (ENoLib) ; (* a computed string is not a value *)
  4930. EXIT
  4931. END ;
  4932. EmPushStrSlot (arg, slot, nCopy)
  4933. ELSIF arg.cls = TReal THEN
  4934. EmPushRealSlot (arg, slot)
  4935. ELSE
  4936. Err (ETypeErr) ;
  4937. EXIT
  4938. END
  4939. ELSE
  4940. LoadAtom (arg) ;
  4941. EmPushAx ()
  4942. END ;
  4943. cleanup := cleanup + slot ;
  4944. INC (nArgs) ;
  4945. IF NOT MatchDelim (',') THEN
  4946. EXIT
  4947. END
  4948. END ;
  4949. ExpectDelim (')', ENoSemi)
  4950. END ;
  4951. EmCallMost (idx, cleanup)
  4952. END ParseCallArgs ;
  4953. PROCEDURE ParseCall (idx : CARDINAL) ;
  4954. (* procedure/function call; '(' optional - same parse-then-push as above,
  4955. reached from ParseAtom when a FUNCTION is called in an expression *)
  4956. VAR arg : ERes ;
  4957. nArgs, cleanup, slot, nCopy : CARDINAL ;
  4958. BEGIN
  4959. nArgs := 0 ;
  4960. cleanup := 0 ;
  4961. IF MatchDelim ('(') THEN
  4962. IF CurCh () # ')' THEN
  4963. LOOP
  4964. IF nArgs >= 16 THEN
  4965. Err (ECompOvf) ;
  4966. EXIT
  4967. END ;
  4968. ParseExpr (arg) ;
  4969. slot := ParmSlot (idx, nArgs) ;
  4970. IF ParmIsVar (idx, nArgs) THEN
  4971. (* A VAR formal in a FUNCTION call: the callee wants the
  4972. address, exactly as ParseCallArgs does for a procedure. This
  4973. path was missing, so `Length(s)' inside a function whose
  4974. formal is `var s' pushed the string's VALUE and the callee
  4975. would have read it as an address. *)
  4976. IF arg.kind = 5 THEN
  4977. EmPushDi ()
  4978. ELSIF arg.kind # 1 THEN
  4979. Err (ENoLib) ;
  4980. EXIT
  4981. ELSIF symtab [arg.idx].isVar THEN
  4982. IF arg.boff # 0 THEN
  4983. Err (ENoLib) ;
  4984. EXIT
  4985. END ;
  4986. LoadSymVar (arg.idx, symtab [arg.idx].off, 2) ;
  4987. EmPushAx ()
  4988. ELSE
  4989. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  4990. END
  4991. ELSIF slot > 2 THEN
  4992. (* see ParseCallArgs: push the whole string slot *)
  4993. IF arg.cls = TString THEN
  4994. nCopy := slot ;
  4995. IF arg.kind = 1 THEN
  4996. IF VarSize (arg) < nCopy THEN
  4997. nCopy := VarSize (arg)
  4998. END
  4999. ELSIF arg.kind = 3 THEN
  5000. IF strLen [arg.strx] + 1 < nCopy THEN
  5001. nCopy := strLen [arg.strx] + 1
  5002. END
  5003. ELSE
  5004. Err (ENoLib) ;
  5005. EXIT
  5006. END ;
  5007. EmPushStrSlot (arg, slot, nCopy)
  5008. ELSIF arg.cls = TReal THEN
  5009. EmPushRealSlot (arg, slot)
  5010. ELSE
  5011. Err (ETypeErr) ;
  5012. EXIT
  5013. END
  5014. ELSE
  5015. LoadAtom (arg) ;
  5016. EmPushAx ()
  5017. END ;
  5018. cleanup := cleanup + slot ;
  5019. INC (nArgs) ;
  5020. IF NOT MatchDelim (',') THEN
  5021. EXIT
  5022. END
  5023. END ;
  5024. ExpectDelim (')', ENoSemi)
  5025. ELSE
  5026. DropCh (GetCh ())
  5027. END
  5028. END ;
  5029. callScratch := 0 ;
  5030. IF symtab [idx].cls = TReal THEN
  5031. callScratch := NewScratchReal () ;
  5032. EmLeaDi (FALSE, symtab [callScratch].off)
  5033. END ;
  5034. EmCallMost (idx, cleanup)
  5035. END ParseCall ;
  5036. PROCEDURE ParseExpr (VAR r : ERes) ;
  5037. BEGIN
  5038. ParseCmp (r)
  5039. END ParseExpr ;
  5040. (* ---------------------------------------------------------------- *)
  5041. (* statements (TPSRC8) *)
  5042. (* ---------------------------------------------------------------- *)
  5043. PROCEDURE ParseLabelStmt () ;
  5044. (* numeric or named label definition 'n :' / 'Name:' *)
  5045. VAR n : CARDINAL ;
  5046. nm : ARRAY [0..MaxName] OF CHAR ;
  5047. idx : CARDINAL ;
  5048. i : CARDINAL ;
  5049. BEGIN
  5050. IF Alpha (CurCh ()) THEN
  5051. GetWord () ;
  5052. SaveWord (nm)
  5053. ELSE
  5054. n := 0 ;
  5055. WHILE Digit (CurCh ()) DO
  5056. n := n * 10 + (ORD (CurCh ()) - ORD ('0')) ;
  5057. DropCh (GetCh ())
  5058. END ;
  5059. NumToName (n, nm)
  5060. END ;
  5061. ExpectDelim (':', ENoSemi) ;
  5062. IF Search (nm, idx) THEN
  5063. IF symtab [idx].tag = KLabel THEN
  5064. symtab [idx].defnd := TRUE ;
  5065. symtab [idx].goPos := pc ;
  5066. i := 0 ;
  5067. WHILE i < nPend DO
  5068. IF (pend [i].kind = 0) AND (pend [i].who = idx) THEN
  5069. SetPatTgt (pend [i].place, pc) ;
  5070. pend [i].kind := 99
  5071. END ;
  5072. INC (i)
  5073. END
  5074. ELSE
  5075. Err (EUnknown)
  5076. END
  5077. ELSE
  5078. idx := NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE) ;
  5079. symtab [idx].defnd := TRUE ;
  5080. symtab [idx].goPos := pc
  5081. END
  5082. END ParseLabelStmt ;
  5083. PROCEDURE IntToMbf (v : LONGINT ; VAR b : ARRAY OF CHAR) ;
  5084. (* TP3's 6-byte Real, as measured in CALC-PORT.md against the cells holding
  5085. 1, 2, 3, 4 and 5:
  5086. value = (1 + N / 2^39) * 2^(E - 129)
  5087. byte 0 is E (biased by 129); bytes 1..5 are N, little-endian; the SIGN is
  5088. bit 7 of byte 5 (the book's `sign + 39-bit mantissa'). An all-zero value
  5089. is 0.0. 1 -> 81 00 00 00 00 00, 3 -> 82 00 00 00 00 40,
  5090. 5 -> 83 00 00 00 00 20 - all cross-checked against CALCDEMO.MCS.
  5091. Only INTEGER values are converted; real arithmetic and formatting are a
  5092. later stage, and a real RHS that is not a constant or a copy is ENoLib. *)
  5093. VAR a, p, n : LONGINT ;
  5094. e, k, i : CARDINAL ;
  5095. neg : BOOLEAN ;
  5096. BEGIN
  5097. i := 0 ;
  5098. WHILE i < 6 DO
  5099. b [i] := CHR (0) ;
  5100. INC (i)
  5101. END ;
  5102. (* The parser represents a negated 16-bit INTEGER constant as its
  5103. unsigned 16-bit bit pattern (W16), so -1 arrives as 65535. Convert that
  5104. back to a signed value before building a REAL; otherwise `R := -1' would
  5105. become +65535. *)
  5106. IF (v > 32767) AND (v <= 65535) THEN
  5107. v := v - 65536
  5108. END ;
  5109. IF v = 0 THEN
  5110. RETURN
  5111. END ;
  5112. neg := v < 0 ;
  5113. IF neg THEN
  5114. a := -v
  5115. ELSE
  5116. a := v
  5117. END ;
  5118. (* k = floor(log2(a)); p = 2^k *)
  5119. p := 1 ;
  5120. k := 0 ;
  5121. WHILE (k < 39) AND (p * 2 <= a) DO
  5122. p := p * 2 ;
  5123. INC (k)
  5124. END ;
  5125. IF k >= 39 THEN
  5126. Err (EConstRange) ;
  5127. RETURN
  5128. END ;
  5129. e := 129 + k ;
  5130. b [0] := CHR (e) ;
  5131. n := a - p ;
  5132. i := 39 - k ;
  5133. WHILE i > 0 DO
  5134. n := n * 2 ;
  5135. DEC (i)
  5136. END ;
  5137. i := 1 ;
  5138. WHILE i <= 5 DO
  5139. b [i] := CHR (VAL (CARDINAL, n MOD 256)) ;
  5140. n := n DIV 256 ;
  5141. INC (i)
  5142. END ;
  5143. IF neg THEN
  5144. b [5] := CHR (ORD (b [5]) + 128)
  5145. END
  5146. END IntToMbf ;
  5147. PROCEDURE RealOperand (r : ERes ; dstOff : CARDINAL) ;
  5148. (* Copy a REAL operand into a 6-byte scratch, converting an ordinal value to
  5149. REAL first. RealBin and RealUnary used to call EmCopyRealTo directly; an
  5150. INTEGER variable there was refused, so `E := A/B' could not compile. *)
  5151. VAR tmp : CARDINAL ;
  5152. BEGIN
  5153. IF r.cls = TReal THEN
  5154. EmCopyRealTo (dstOff, r)
  5155. ELSIF (r.cls = TSet) OR (r.cls = TString) THEN
  5156. Err (ETypeErr)
  5157. ELSE
  5158. IF r.kind = 5 THEN
  5159. EmPushDi ()
  5160. ELSIF r.kind = 1 THEN
  5161. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  5162. ELSE
  5163. LoadAtom (r) ;
  5164. EmStoreVar (FALSE, symtab [tmpA].off, 2) ;
  5165. EmPushVarAddr (FALSE, symtab [tmpA].off)
  5166. END ;
  5167. EmPushVarAddr (FALSE, dstOff) ;
  5168. DropC (EmCall (TU_RealFromInt)) ;
  5169. EmAddSp (4)
  5170. END
  5171. END RealOperand ;
  5172. PROCEDURE RealBin (op : CARDINAL ; left, right : ERes ; VAR res : ERes) ;
  5173. (* REAL + - * / using the runtime's 12-byte block entries. SaveLeft has
  5174. already copied a REAL left operand to a stable scratch; a kind-5 right is
  5175. copied first, while its DI address is live. *)
  5176. VAR block, ent : CARDINAL ;
  5177. BEGIN
  5178. block := NewScratchRealBlock () ;
  5179. IF right.kind = 5 THEN
  5180. RealOperand (right, symtab [block].off + 6) ;
  5181. RealOperand (left, symtab [block].off)
  5182. ELSE
  5183. RealOperand (left, symtab [block].off) ;
  5184. RealOperand (right, symtab [block].off + 6)
  5185. END ;
  5186. CASE op OF
  5187. OpAdd : ent := TU_RealAdd
  5188. | OpSub : ent := TU_RealSub
  5189. | OpMul : ent := TU_RealMul
  5190. | OpDiv : ent := TU_RealDiv
  5191. ELSE
  5192. Err (ETypeErr) ;
  5193. RETURN
  5194. END ;
  5195. EmPushVarAddr (FALSE, symtab [block].off) ;
  5196. DropC (EmCall (ent)) ;
  5197. EmAddSp (2) ;
  5198. res.kind := 1 ;
  5199. res.idx := block ;
  5200. res.cls := TReal ;
  5201. res.sz := 6 ;
  5202. res.boff := 0
  5203. END RealBin ;
  5204. PROCEDURE RealUnary (ent : CARDINAL ; VAR r : ERes) ;
  5205. (* A one-argument REAL builtin: copy the operand into a 12-byte block and let
  5206. the runtime entry leave its result in the first six bytes. *)
  5207. VAR block : CARDINAL ;
  5208. BEGIN
  5209. block := NewScratchRealBlock () ;
  5210. EmCopyRealTo (symtab [block].off, r) ;
  5211. EmPushVarAddr (FALSE, symtab [block].off) ;
  5212. DropC (EmCall (ent)) ;
  5213. EmAddSp (2) ;
  5214. r.kind := 1 ;
  5215. r.idx := block ;
  5216. r.cls := TReal ;
  5217. r.sz := 6 ;
  5218. r.boff := 0
  5219. END RealUnary ;
  5220. PROCEDURE Assignment (r : ERes) ;
  5221. (* ':=' already consumed by the caller; store expression into r *)
  5222. VAR src : ERes ;
  5223. k, n, savePos, scratch : CARDINAL ;
  5224. sbuf : ARRAY [0..31] OF CHAR ;
  5225. rbuf : ARRAY [0..5] OF CHAR ;
  5226. BEGIN
  5227. IF (r.kind # 1) AND (r.kind # 5) THEN
  5228. Err (EUnknown) ;
  5229. RETURN
  5230. END ;
  5231. IF IsCharArray (r) AND ((r.kind = 1) OR (r.kind = 5)) THEN
  5232. (* Fixed CHAR arrays are Pascal strings too, but they have no length
  5233. byte: a literal fills the array and pads the tail with spaces. *)
  5234. ParseExpr (src) ;
  5235. IF NOT OK () THEN
  5236. RETURN
  5237. END ;
  5238. IF src.kind = 3 THEN
  5239. n := VarSize (r) ;
  5240. k := 0 ;
  5241. WHILE k < n DO
  5242. IF k < strLen [src.strx] THEN
  5243. IF r.kind = 5 THEN
  5244. EmStDiImm (k, ORD (strPool [strOff [src.strx] + k]))
  5245. ELSE
  5246. EmMovAxi (ORD (strPool [strOff [src.strx] + k])) ;
  5247. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1)
  5248. END
  5249. ELSE
  5250. IF r.kind = 5 THEN
  5251. EmStDiImm (k, ORD (' '))
  5252. ELSE
  5253. EmMovAxi (ORD (' ')) ;
  5254. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1)
  5255. END
  5256. END ;
  5257. INC (k)
  5258. END ;
  5259. RETURN
  5260. END ;
  5261. Err (ETypeErr) ;
  5262. RETURN
  5263. END ;
  5264. IF ((r.kind = 1) OR (r.kind = 5)) AND (VarSize (r) > 2)
  5265. AND ((r.cls = TRecord) OR (r.cls = TArray)) THEN
  5266. (* Whole record/array assignment: copy VarSize(r) bytes. This is what
  5267. BIGREC/DYNREC's `Mother := Self', `Father^ := Mother^' and
  5268. `Friend[Index] := Mother' need. A DI-addressed destination has to
  5269. be parked before the source expression can reuse DI. *)
  5270. IF r.kind = 5 THEN
  5271. EmPushDi ()
  5272. END ;
  5273. ParseExpr (src) ;
  5274. IF NOT OK () THEN
  5275. RETURN
  5276. END ;
  5277. IF (src.kind # 1) AND (src.kind # 5) THEN
  5278. Err (ETypeErr) ;
  5279. RETURN
  5280. END ;
  5281. IF VarSize (src) # VarSize (r) THEN
  5282. Err (ETypeErr) ;
  5283. RETURN
  5284. END ;
  5285. IF src.kind = 5 THEN
  5286. EmPushDi ()
  5287. ELSE
  5288. EmPushSymAddr (src.idx, (symtab [src.idx].off + src.boff) MOD 10000H)
  5289. END ;
  5290. IF r.kind = 5 THEN
  5291. (* stack now holds dest, source; make it source, dest for the ABI *)
  5292. EmPopAx () ;
  5293. EmPopBx () ;
  5294. EmPushAx () ;
  5295. EmPushBx ()
  5296. ELSE
  5297. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  5298. END ;
  5299. EmMovCxi (VarSize (r)) ;
  5300. DropC (EmCall (TU_MemCopy)) ;
  5301. EmAddSp (4) ;
  5302. RETURN
  5303. END ;
  5304. IF r.cls = TReal THEN
  5305. (* A 6-byte REAL. A compile-time ordinal constant is converted to the
  5306. measured MBF layout; another REAL field is copied byte for byte.
  5307. Real arithmetic and real-valued expressions are a later stage and
  5308. are refused, not guessed. *)
  5309. IF (r.kind = 1) AND symtab [r.idx].isVar THEN
  5310. (* VAR REAL parameter: the slot holds the destination address. *)
  5311. ParseExpr (src) ;
  5312. IF NOT OK () THEN
  5313. RETURN
  5314. END ;
  5315. IF (src.kind = 0) AND (src.cls # TString) AND (src.cls # TSet) THEN
  5316. IntToMbf (src.imm, rbuf) ;
  5317. LoadSymVar (r.idx, symtab [r.idx].off, 2) ;
  5318. EmMovDiAx () ;
  5319. k := 0 ;
  5320. WHILE k < 6 DO
  5321. EmStDiImm (k, ORD (rbuf [k])) ;
  5322. INC (k)
  5323. END ;
  5324. RETURN
  5325. END ;
  5326. IF (src.cls = TReal) AND ((src.kind = 1) OR (src.kind = 5)) THEN
  5327. (* Destination pointer first, then the source address; pop SI
  5328. (source), DI (destination), and copy six bytes. *)
  5329. LoadSymVar (r.idx, symtab [r.idx].off, 2) ;
  5330. EmPushAx () ;
  5331. IF src.kind = 5 THEN
  5332. EmPushDi ()
  5333. ELSIF symtab [src.idx].isVar THEN
  5334. LoadSymVar (src.idx, symtab [src.idx].off, 2) ;
  5335. EmPushAx ()
  5336. ELSE
  5337. EmPushSymAddr (src.idx, (symtab [src.idx].off + src.boff) MOD 10000H)
  5338. END ;
  5339. EmPopSi () ;
  5340. EmPopDi () ;
  5341. k := 0 ;
  5342. WHILE k < 6 DO
  5343. EmLdAlSi () ;
  5344. EmStDiAl () ;
  5345. EmIncSi () ;
  5346. EmIncDi () ;
  5347. INC (k)
  5348. END ;
  5349. RETURN
  5350. END ;
  5351. Err (ENoLib) ;
  5352. RETURN
  5353. END ;
  5354. IF r.kind = 5 THEN
  5355. EmPushDi ()
  5356. END ;
  5357. ParseExpr (src) ;
  5358. IF NOT OK () THEN
  5359. RETURN
  5360. END ;
  5361. IF (src.cls # TReal) AND (src.cls # TString) AND (src.cls # TSet)
  5362. AND (src.kind # 0) AND ((r.kind = 1) OR (r.kind = 5)) THEN
  5363. (* INTEGER/CHAR/BOOLEAN -> REAL, e.g. Size := Count in CONVERT. *)
  5364. scratch := NewScratchReal () ;
  5365. RealOperand (src, symtab [scratch].off) ;
  5366. IF r.kind = 5 THEN
  5367. EmPopDi ()
  5368. END ;
  5369. k := 0 ;
  5370. WHILE k < 6 DO
  5371. LoadSymVar (scratch, (symtab [scratch].off + k) MOD 10000H, 1) ;
  5372. IF r.kind = 5 THEN
  5373. EmStDiAlOff (k)
  5374. ELSE
  5375. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1)
  5376. END ;
  5377. INC (k)
  5378. END ;
  5379. RETURN
  5380. END ;
  5381. IF (src.kind = 0) AND (src.cls # TString) AND (src.cls # TSet) THEN
  5382. IntToMbf (src.imm, rbuf) ;
  5383. IF r.kind = 5 THEN
  5384. EmPopDi () ;
  5385. k := 0 ;
  5386. WHILE k < 6 DO
  5387. EmStDiImm (k, ORD (rbuf [k])) ;
  5388. INC (k)
  5389. END
  5390. ELSIF r.kind = 1 THEN
  5391. k := 0 ;
  5392. WHILE k < 6 DO
  5393. EmMovAxi (ORD (rbuf [k])) ;
  5394. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  5395. INC (k)
  5396. END
  5397. ELSE
  5398. Err (ENoLib)
  5399. END ;
  5400. RETURN
  5401. END ;
  5402. IF (src.kind = 5) AND (src.cls = TReal) AND (r.kind = 5) THEN
  5403. EmPushDi () ; (* source address *)
  5404. EmPopSi () ;
  5405. EmPopDi () ; (* destination address *)
  5406. k := 0 ;
  5407. WHILE k < 6 DO
  5408. EmLdAlSi () ; EmStDiAl () ; EmIncSi () ; EmIncDi () ;
  5409. INC (k)
  5410. END ;
  5411. RETURN
  5412. END ;
  5413. IF (src.kind = 5) AND (src.cls = TReal) AND (r.kind = 1) THEN
  5414. EmPushDi () ;
  5415. EmPopSi () ;
  5416. k := 0 ;
  5417. WHILE k < 6 DO
  5418. EmLdAlSi () ;
  5419. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  5420. EmIncSi () ;
  5421. INC (k)
  5422. END ;
  5423. RETURN
  5424. END ;
  5425. IF (src.kind = 1) AND (src.cls = TReal) THEN
  5426. IF r.kind = 5 THEN
  5427. IF src.kind = 5 THEN
  5428. Err (ENoLib) ;
  5429. RETURN
  5430. END ;
  5431. EmPopDi () ;
  5432. k := 0 ;
  5433. WHILE k < 6 DO
  5434. LoadSymVar (src.idx, (symtab [src.idx].off + src.boff + k) MOD 10000H, 1) ;
  5435. EmStDiAlOff (k) ;
  5436. INC (k)
  5437. END
  5438. ELSIF r.kind = 1 THEN
  5439. k := 0 ;
  5440. WHILE k < 6 DO
  5441. LoadSymVar (src.idx, (symtab [src.idx].off + src.boff + k) MOD 10000H, 1) ;
  5442. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  5443. INC (k)
  5444. END
  5445. ELSE
  5446. Err (ENoLib)
  5447. END ;
  5448. RETURN
  5449. END ;
  5450. Err (ENoLib) ;
  5451. RETURN
  5452. END ;
  5453. IF r.cls = TSet THEN
  5454. (* `field := [elements]'. The constructor's TYPE comes from the target:
  5455. CALC's CellStatus is `set of Attributes' (one byte), not the default
  5456. 32-byte `set of Char' an untyped literal would become. The bytes are
  5457. known at compile time; store them at symbol+boff or through DI. *)
  5458. IF (r.setsize = 0) OR (r.setsize > 32) THEN
  5459. Err (ENoLib) ;
  5460. RETURN
  5461. END ;
  5462. Skip () ;
  5463. IF CurCh () = '[' THEN
  5464. (* A leading constructor normally belongs to the TARGET, because its
  5465. type (e.g. CALC's one-byte `set of Attributes') is narrower than
  5466. the default 32-byte literal. But `[A..D] - X' is an expression:
  5467. parse it through ParseExpr instead of storing it directly. The
  5468. source cursor is reversible, so try the constructor first. *)
  5469. savePos := srcPos ;
  5470. IF NOT RdSetLiteral (r.setbase, r.setsize, sbuf) THEN
  5471. RETURN
  5472. END ;
  5473. Skip () ;
  5474. IF (CurCh () = '+') OR (CurCh () = '-') OR (CurCh () = '*') THEN
  5475. srcPos := savePos
  5476. ELSE
  5477. IF r.kind = 5 THEN
  5478. k := 0 ;
  5479. WHILE k < r.setsize DO
  5480. EmStDiImm (k, ORD (sbuf [k])) ;
  5481. INC (k)
  5482. END
  5483. ELSIF r.kind = 1 THEN
  5484. k := 0 ;
  5485. WHILE k < r.setsize DO
  5486. EmMovAxi (ORD (sbuf [k])) ;
  5487. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + k) MOD 10000H, 1) ;
  5488. INC (k)
  5489. END
  5490. ELSE
  5491. Err (ENoLib)
  5492. END ;
  5493. RETURN
  5494. END
  5495. END ;
  5496. (* `set := set' or a set expression. The destination is a variable
  5497. or a DI-addressed field; save the DI address BEFORE the right-hand
  5498. expression can reuse DI, then copy the runtime's bytes. *)
  5499. IF r.kind = 5 THEN
  5500. EmPushDi ()
  5501. END ;
  5502. ParseExpr (src) ;
  5503. IF NOT OK () THEN
  5504. RETURN
  5505. END ;
  5506. IF src.cls # TSet THEN
  5507. Err (ETypeErr) ;
  5508. RETURN
  5509. END ;
  5510. IF src.kind = 9 THEN
  5511. MaterializeDyn (src, r.setbase, r.setsize)
  5512. END ;
  5513. IF r.kind = 1 THEN
  5514. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H)
  5515. ELSIF r.kind # 5 THEN
  5516. Err (ENoLib) ;
  5517. RETURN
  5518. END ;
  5519. EmPushSetAddr (src) ;
  5520. n := r.setsize ;
  5521. IF (src.kind = 8) AND (src.setsize < n) THEN
  5522. n := src.setsize
  5523. END ;
  5524. EmMovAxi (n) ;
  5525. EmPushAx () ;
  5526. DropC (EmCall (TU_SetCopy)) ;
  5527. EmAddSp (4) ;
  5528. RETURN
  5529. END ;
  5530. IF (r.kind = 5) AND (r.cls = TString) THEN
  5531. (* A string field of a DI-addressed record element. DI is the field's
  5532. address; park it because the source expression may reuse DI. *)
  5533. EmPushDi () ;
  5534. ParseExpr (src) ;
  5535. IF NOT OK () THEN
  5536. RETURN
  5537. END ;
  5538. n := VarSize (r) - 1 ; (* usable characters *)
  5539. IF src.kind = 3 THEN
  5540. EmPopDi () ;
  5541. IF strLen [src.strx] < n THEN
  5542. n := strLen [src.strx]
  5543. END ;
  5544. EmStDiImm (0, n) ; (* the length byte *)
  5545. k := 1 ;
  5546. WHILE k <= n DO
  5547. EmStDiImm (k, ORD (strPool [strOff [src.strx] + k - 1])) ;
  5548. INC (k)
  5549. END
  5550. ELSIF src.kind = 1 THEN
  5551. EmPushSymAddr (src.idx, (symtab [src.idx].off + src.boff) MOD 10000H) ;
  5552. EmPopDi () ; (* destination = the field *)
  5553. EmPushDi () ;
  5554. EmMovCxi (n) ; (* strstore's limit *)
  5555. DropC (EmCall (TU_StrStore)) ;
  5556. EmAddSp (4)
  5557. ELSIF src.kind = 5 THEN
  5558. EmPushDi () ; (* source field address *)
  5559. EmPopSi () ;
  5560. EmPopDi () ; (* destination field address *)
  5561. EmPushSi () ; (* source address *)
  5562. EmPushDi () ;
  5563. EmMovCxi (n) ;
  5564. DropC (EmCall (TU_StrStore)) ;
  5565. EmAddSp (4)
  5566. ELSE
  5567. Err (ENoLib)
  5568. END ;
  5569. RETURN
  5570. END ;
  5571. IF (r.kind = 1) AND (r.cls = TString) AND symtab [r.idx].isVar THEN
  5572. (* A VAR string parameter: the slot holds the caller's string address.
  5573. Copy through the runtime, exactly as a normal string variable does:
  5574. source address first, destination address second, limit in CX. This
  5575. covers literals and whole-string expressions alike; the destination
  5576. address is loaded from the parameter slot rather than formed with
  5577. LEA, which would point at the slot itself. *)
  5578. ParseExpr (src) ;
  5579. IF NOT OK () THEN
  5580. RETURN
  5581. END ;
  5582. IF src.cls # TString THEN
  5583. Err (ETypeErr) ;
  5584. RETURN
  5585. END ;
  5586. EmPushStrAddr (src) ;
  5587. LoadSymVar (r.idx, symtab [r.idx].off, 2) ;
  5588. EmPushAx () ;
  5589. EmMovCxi ((VarSize (r) - 1) MOD 10000H) ;
  5590. DropC (EmCall (TU_StrStore)) ;
  5591. EmAddSp (4) ;
  5592. RETURN
  5593. END ;
  5594. IF (r.kind = 1) AND symtab [r.idx].isVar THEN
  5595. (* `varparam := expr': store through the address the slot holds. DI is
  5596. parked across the right-hand side because that expression may use DI
  5597. for a string element of its own. *)
  5598. IF r.boff # 0 THEN
  5599. Err (ENoLib) ;
  5600. RETURN
  5601. END ;
  5602. StoreVarIndPrepSym (r.idx) ;
  5603. ParseExpr (src) ;
  5604. LoadAtom (src) ;
  5605. EmPopDi () ;
  5606. EmStoreInd (VarSize (r)) ;
  5607. RETURN
  5608. END ;
  5609. IF (r.cls = TString) AND (VarSize (r) > 1) THEN
  5610. (* s := <string literal>. TPSRC8 asn2 sends a string through the scalar
  5611. store path, and estcpl's string arm emits `MOV CL,max_len' (max_len =
  5612. varsize-1) then `CALL strstore', which copies the source - length byte
  5613. and characters - into the destination and TRUNCATES a source longer
  5614. than max_len by rewriting the length byte (TPSRC2 strstore's
  5615. `CMP.B CL,AL / JBE stslenok / MOV.B [SI],AL'). Here the literal is
  5616. known at compile time, so its bytes become immediate stores instead of
  5617. a stack push and a block move. The variable that results is the same,
  5618. truncation included, and the tail past the new length is left as it
  5619. was, exactly as strstore leaves it. *)
  5620. ParseExpr (src) ;
  5621. IF (src.cls = TChar) OR ((src.cls = TScalar) AND src.chr) THEN
  5622. (* `s := ch': a one-character string. This is CALC's
  5623. `EXY := FactorCh' before Val. *)
  5624. IF src.kind = 0 THEN
  5625. EmMovAxi (1) ;
  5626. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, 1) ;
  5627. EmMovAxi (W16 (src.imm)) ;
  5628. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + 1) MOD 10000H, 1)
  5629. ELSE
  5630. LoadAtom (src) ;
  5631. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + 1) MOD 10000H, 1) ;
  5632. EmMovAxi (1) ;
  5633. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, 1)
  5634. END ;
  5635. RETURN
  5636. END ;
  5637. IF (src.cls # TString) THEN
  5638. Err (ENoLib) ;
  5639. RETURN
  5640. END ;
  5641. IF src.kind # 3 THEN
  5642. (* s := <string variable>. TPSRC8 asnvar pushes the destination
  5643. address, then the expression (strload) pushes the source, and
  5644. estcpl emits MOV CL,max_len then CALL strstore, which copies
  5645. length byte + chars, TRUNCATING to max_len and leaving the
  5646. destination tail past the new length untouched. Here both
  5647. addresses are pushed - source first, so it sits at [BP+6] and the
  5648. destination at [BP+4] - the caller pops both, and the limit is a
  5649. full MOV CX,imm16 because this runtime reads it in CX outright. *)
  5650. IF src.kind = 1 THEN
  5651. EmPushSymAddr (src.idx, (symtab [src.idx].off + src.boff) MOD 10000H)
  5652. ELSIF src.kind = 5 THEN
  5653. (* A string element reached through DI, including a pointer
  5654. dereference: Buffer^[i]. Its address is already in DI. *)
  5655. EmPushDi ()
  5656. ELSE
  5657. Err (ENoLib) ; (* a computed string is not a value *)
  5658. RETURN
  5659. END ;
  5660. EmPushSymAddr (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H) ;
  5661. EmMovCxi ((VarSize (r) - 1) MOD 10000H) ;
  5662. DropC (EmCall (TU_StrStore)) ;
  5663. EmAddSp (4) ;
  5664. RETURN
  5665. END ;
  5666. n := strLen [src.strx] ;
  5667. IF n > VarSize (r) - 1 THEN
  5668. n := VarSize (r) - 1
  5669. END ;
  5670. EmMovAxi (n) ;
  5671. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, 1) ;
  5672. k := 0 ;
  5673. WHILE k < n DO
  5674. EmMovAxi (ORD (strPool [strOff [src.strx] + k])) ;
  5675. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff + 1 + k) MOD 10000H, 1) ;
  5676. INC (k)
  5677. END ;
  5678. RETURN
  5679. END ;
  5680. IF (r.kind = 5) OR (VarSize (r) > 2) THEN
  5681. IF r.kind = 5 THEN
  5682. (* An element addressed by DI: a string character (one byte) or an
  5683. array element (one or two). A wider element - a record - is
  5684. refused rather than stored as its first two bytes.
  5685. The destination address is in DI, and the right-hand side is
  5686. parsed next, so DI has to be saved first - the RHS may hold a
  5687. subscript of its own, which reuses DI, or call a routine that
  5688. clobbers it. This is TPSRC8 asnscal2/asnscal3 (758-760, 786-789),
  5689. which set flgpshdi around the expression for the same reason. *)
  5690. IF VarSize (r) > 2 THEN
  5691. Err (ENoLib) ;
  5692. RETURN
  5693. END ;
  5694. EmPushDi () ;
  5695. ParseExpr (src) ;
  5696. LoadAtom (src) ;
  5697. EmPopDi () ; (* DI := the destination again *)
  5698. EmStoreInd (VarSize (r)) ; (* [DI] := AL or AX *)
  5699. RETURN
  5700. END ;
  5701. (* A value wider than a word with no address to store through: an
  5702. assignment to a whole string/record/array is not a scalar store. *)
  5703. Err (ENoLib) ;
  5704. RETURN
  5705. END ;
  5706. ParseExpr (src) ;
  5707. LoadAtom (src) ;
  5708. StoreSymVar (r.idx, (symtab [r.idx].off + r.boff) MOD 10000H, VarSize (r))
  5709. END Assignment ;
  5710. PROCEDURE Compound () ;
  5711. (* BEGIN statement ';' ... END; END is consumed here *)
  5712. VAR tok : CARDINAL ;
  5713. BEGIN
  5714. LOOP
  5715. PeekKw (tok) ;
  5716. IF tok = TkEnd THEN
  5717. DropB (MatchKey (tok)) ;
  5718. RETURN
  5719. END ;
  5720. Statmnt () ;
  5721. IF NOT OK () THEN
  5722. RETURN
  5723. END ;
  5724. IF NOT MatchDelim (';') THEN
  5725. PeekKw (tok) ;
  5726. IF tok = TkEnd THEN
  5727. DropB (MatchKey (tok)) ;
  5728. RETURN
  5729. END ;
  5730. Err (ENoSemi) ;
  5731. RETURN
  5732. END
  5733. END
  5734. END Compound ;
  5735. PROCEDURE EmCrtSeq (which : CARDINAL) ;
  5736. (* The CRT byte sequences, in one parameterised helper. Every one is a fixed
  5737. BIOS call:
  5738. CrtClr - INT 10h AH=06 blank the whole window, then AH=02 home the cursor
  5739. CrtDl - DEC AX ; MOV DL,AL (a 1-based column -> 0-based)
  5740. CrtDh - DEC AX ; MOV DH,AL (a 1-based row)
  5741. CrtCaret - INT 10h AH=02, page 0, cursor at DH,DL
  5742. It takes a parameter on purpose: audit_helpers audits helpers with an EMPTY
  5743. parameter list byte-for-byte, and these multi-instruction sequences have no
  5744. single name grammar. Listed as COMP in NON_ONE_LINE, like SetBlob. *)
  5745. BEGIN
  5746. IF which = CrtClr THEN
  5747. Ebyte (0B8H) ; Ebyte (00H) ; Ebyte (06H) ; (* MOV AX,0600h *)
  5748. Ebyte (8AH) ; Ebyte (3EH) ; Eword ((crtAttrOff + LoadBias) MOD 10000H) ; (* MOV BH,[crtAttr] *)
  5749. Ebyte (0B9H) ; Ebyte (00H) ; Ebyte (00H) ; (* MOV CX,0000h *)
  5750. Ebyte (0BAH) ; Ebyte (4FH) ; Ebyte (18H) ; (* MOV DX,184Fh *)
  5751. Ebyte (0CDH) ; Ebyte (10H) ; (* INT 10h *)
  5752. Ebyte (0B8H) ; Ebyte (00H) ; Ebyte (02H) ; (* MOV AX,0200h *)
  5753. Ebyte (0B7H) ; Ebyte (00H) ; (* MOV BH,00h *)
  5754. Ebyte (0BAH) ; Ebyte (00H) ; Ebyte (00H) ; (* MOV DX,0000h *)
  5755. Ebyte (0CDH) ; Ebyte (10H) (* INT 10h *)
  5756. ELSIF which = CrtDl THEN
  5757. Ebyte (48H) ; (* DEC AX *)
  5758. Ebyte (88H) ; Ebyte (0C2H) (* MOV DL,AL *)
  5759. ELSIF which = CrtDh THEN
  5760. Ebyte (48H) ; (* DEC AX *)
  5761. Ebyte (88H) ; Ebyte (0C6H) (* MOV DH,AL *)
  5762. ELSIF which = CrtEol THEN
  5763. Ebyte (0B4H) ; Ebyte (03H) ; (* MOV AH,03h get cursor *)
  5764. Ebyte (0B7H) ; Ebyte (00H) ; (* MOV BH,00h *)
  5765. Ebyte (0CDH) ; Ebyte (10H) ; (* INT 10h -> DH,DL *)
  5766. Ebyte (89H) ; Ebyte (0D1H) ; (* MOV CX,DX upper-left *)
  5767. Ebyte (0B2H) ; Ebyte (4FH) ; (* MOV DL,4Fh col 79 *)
  5768. Ebyte (0B8H) ; Ebyte (00H) ; Ebyte (06H) ; (* MOV AX,0600h blank *)
  5769. Ebyte (8AH) ; Ebyte (3EH) ; Eword ((crtAttrOff + LoadBias) MOD 10000H) ; (* MOV BH,[crtAttr] *)
  5770. Ebyte (0CDH) ; Ebyte (10H) (* INT 10h *)
  5771. ELSIF which = CrtLow THEN
  5772. (* [crtAttr] := 7: the dim attribute ClrScr/ClrEol use from now on. The
  5773. runtime's console writer is INT 21h teletype, which has no attribute
  5774. channel, but the clear operations do - so this is where the setting is
  5775. observable, not a no-op. *)
  5776. Ebyte (0C6H) ; Ebyte (06H) ; Eword ((crtAttrOff + LoadBias) MOD 10000H) ; Ebyte (07H)
  5777. ELSIF which = CrtNorm THEN
  5778. Ebyte (0C6H) ; Ebyte (06H) ; Eword ((crtAttrOff + LoadBias) MOD 10000H) ; Ebyte (0FH)
  5779. ELSIF which = CrtKey THEN
  5780. (* KeyPressed: DOS AH=0Bh asks whether a character is waiting. AL is
  5781. 0FFh when one is, 0 when not; the boolean representation here is 0/1.
  5782. AND AX,1, not AND AL,1: the interrupt leaves AH = 0Bh, so the
  5783. comparison that follows (`CMP AX,0') would read 0B00h and call the
  5784. false case TRUE. bootcom happened to return AH=0 (it IRETs the
  5785. handler's whole AX) and hid this; hlt. No argument, no runtime
  5786. entry. *)
  5787. Ebyte (0B4H) ; Ebyte (0BH) ; (* MOV AH,0Bh *)
  5788. Ebyte (0CDH) ; Ebyte (21H) ; (* INT 21h *)
  5789. Ebyte (25H) ; Ebyte (01H) ; Ebyte (00H) (* AND AX,1 *)
  5790. ELSIF which = CrtDelay THEN
  5791. (* Delay(AX) milliseconds: TP3's xdelay, a calibrated busy loop. The
  5792. count 110 is TP3's default `delaycnt' for 1 ms (TPSRC1). AX = 0 does
  5793. nothing. *)
  5794. (* OR AX,AX, not CMP AX,0: check_8086 treats every `CMP AX,0000' in the
  5795. program region as one of the IF/WHILE/REPEAT tests and demands
  5796. Jcc+3;EJMP after it. This is neither. *)
  5797. Ebyte (0BH) ; Ebyte (0C0H) ; (* OR AX,AX *)
  5798. Ebyte (74H) ; Ebyte (08H) ; (* JZ past the loop *)
  5799. Ebyte (0B9H) ; Ebyte (6EH) ; Ebyte (00H) ; (* MOV CX,110 (1 ms) *)
  5800. Ebyte (0E2H) ; Ebyte (0FEH) ; (* LOOP -> itself *)
  5801. Ebyte (48H) ; (* DEC AX *)
  5802. Ebyte (75H) ; Ebyte (0F8H) (* JNZ -> MOV CX *)
  5803. ELSE
  5804. Ebyte (0B4H) ; Ebyte (02H) ; (* MOV AH,02h *)
  5805. Ebyte (0B7H) ; Ebyte (00H) ; (* MOV BH,00h *)
  5806. Ebyte (0CDH) ; Ebyte (10H) (* INT 10h *)
  5807. END
  5808. END EmCrtSeq ;
  5809. PROCEDURE IoCall (idx : CARDINAL) ;
  5810. (* WRITE / WRITELN / READ / READLN / HALT.
  5811. TP3 (TPSRC8 pwriteln, pwrloop, prdtyped) does not pass a descriptor to
  5812. the runtime: it looks at each argument's class and emits a *different*
  5813. call per type, so the formatting is fixed at compile time. Mirrored
  5814. here - one call per argument, then a final call for the line break.
  5815. WRITE/WRITELN push the value; READ/READLN push the address, so the
  5816. runtime can store. As everywhere else in this compiler the caller
  5817. cleans the argument off the stack. *)
  5818. VAR arg : ERes ;
  5819. nArgs, ent, which, acls : CARDINAL ;
  5820. argBytes : CARDINAL ; (* bytes pushed for this argument: 2, or 4 with
  5821. a field width *)
  5822. reading : BOOLEAN ;
  5823. pushed : BOOLEAN ;
  5824. skipArg, handled : BOOLEAN ;
  5825. k, n, p, scratch, cap : CARDINAL ;
  5826. dummy, errV : ERes ;
  5827. BEGIN
  5828. which := symtab [idx].cls ; (* BI_* *)
  5829. IF which = BI_Halt THEN
  5830. IF MatchDelim ('(') THEN (* halt(0) - code ignored *)
  5831. ParseExpr (dummy) ;
  5832. ExpectDelim (')', ENoSemi)
  5833. END ;
  5834. DropC (EmCall (TU_Halt)) ;
  5835. RETURN
  5836. END ;
  5837. IF which = BI_New THEN
  5838. (* New(P): allocate sizeof(P^) bytes and store the address in P. The
  5839. pointer symbol's `elem' is the pointee TYPE symbol; its size is the
  5840. allocation. *)
  5841. IF MatchDelim ('(') THEN
  5842. ParseExpr (arg) ;
  5843. IF OK () AND ((arg.kind = 1) OR (arg.kind = 5)) AND
  5844. (arg.cls = TPtr) THEN
  5845. p := arg.ptrElem ;
  5846. IF p = 0 THEN
  5847. p := symtab [arg.idx].elem
  5848. END ;
  5849. EmMovAxi (symtab [p].size) ;
  5850. EmPushAx () ; (* size -> [BP+6] *)
  5851. IF arg.kind = 5 THEN
  5852. EmPushDi () (* element pointer slot *)
  5853. ELSE
  5854. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  5855. END ;
  5856. DropC (EmCall (TU_HeapNew)) ;
  5857. EmAddSp (4)
  5858. ELSIF OK () THEN
  5859. Err (ETypeErr)
  5860. END ;
  5861. ExpectDelim (')', ENoSemi)
  5862. ELSE
  5863. Err (ENoSemi)
  5864. END ;
  5865. RETURN
  5866. END ;
  5867. IF which = BI_Dispose THEN
  5868. (* Dispose(P): consume the argument. The allocator is a bump pointer
  5869. with no free list; CALC allocates once at the start of Help and
  5870. disposes only after the help display is finished, so not reclaiming
  5871. the block has no observable effect. A future allocation after a
  5872. Dispose would need a real free list. *)
  5873. IF MatchDelim ('(') THEN
  5874. ParseExpr (arg) ;
  5875. IF OK () AND NOT (((arg.kind = 1) OR (arg.kind = 5))
  5876. AND (arg.cls = TPtr)) THEN
  5877. Err (ETypeErr)
  5878. END ;
  5879. ExpectDelim (')', ENoSemi)
  5880. ELSE
  5881. Err (ENoSemi)
  5882. END ;
  5883. RETURN
  5884. END ;
  5885. IF which = BI_ClrScr THEN
  5886. EmCrtSeq (CrtClr) ;
  5887. RETURN
  5888. END ;
  5889. IF which = BI_ClrEol THEN
  5890. (* clear from the cursor to the end of the line - a real INT 10h call,
  5891. not a no-op: AH=03 reads the cursor, CX := it, DL := 79, AH=06 blanks
  5892. that window. *)
  5893. EmCrtSeq (CrtEol) ;
  5894. RETURN
  5895. END ;
  5896. IF which = BI_GotoXY THEN
  5897. (* GotoXY (x, y): 1-based; evaluate x first and park it, then y, so the
  5898. code for y cannot clobber DL. *)
  5899. IF MatchDelim ('(') THEN
  5900. ParseExpr (dummy) ;
  5901. IF OK () THEN
  5902. LoadAtom (dummy) ;
  5903. EmPushAx ()
  5904. END ;
  5905. ExpectDelim (',', ENoSemi) ;
  5906. ParseExpr (dummy) ;
  5907. IF OK () THEN
  5908. LoadAtom (dummy) ;
  5909. EmCrtSeq (CrtDh)
  5910. END ;
  5911. ExpectDelim (')', ENoSemi) ;
  5912. EmPopAx () ;
  5913. EmCrtSeq (CrtDl) ;
  5914. EmCrtSeq (CrtCaret)
  5915. ELSE
  5916. Err (ENoSemi)
  5917. END ;
  5918. RETURN
  5919. END ;
  5920. IF which = BI_LowVideo THEN
  5921. (* set the attribute the CLEAR operations use to 7; see EmCrtSeq *)
  5922. EmCrtSeq (CrtLow) ;
  5923. RETURN
  5924. END ;
  5925. IF which = BI_NormVideo THEN
  5926. EmCrtSeq (CrtNorm) ;
  5927. RETURN
  5928. END ;
  5929. IF which = BI_Delay THEN
  5930. (* Delay(ms): a real busy loop (EmCrtSeq's CrtDelay), no INT, so it is
  5931. identical under qemu and Exec86. *)
  5932. IF MatchDelim ('(') THEN
  5933. ParseExpr (dummy) ;
  5934. IF OK () THEN
  5935. LoadAtom (dummy) ;
  5936. EmCrtSeq (CrtDelay)
  5937. END ;
  5938. ExpectDelim (')', ENoSemi)
  5939. ELSE
  5940. Err (ENoSemi)
  5941. END ;
  5942. RETURN
  5943. END ;
  5944. IF which = BI_Insert THEN
  5945. (* Insert(Source, Dest, Index). A CHAR source is materialised as a
  5946. one-character counted string in a scratch; Dest must be a modifiable
  5947. string variable/field. Stack order is source, dest, index, max, so
  5948. the runtime sees max at [BP+4] and source at [BP+10]. *)
  5949. IF NOT MatchDelim ('(') THEN
  5950. Err (ENoSemi) ;
  5951. RETURN
  5952. END ;
  5953. ParseExpr (arg) ;
  5954. IF OK () AND (arg.cls = TString) AND
  5955. ((arg.kind = 1) OR (arg.kind = 3) OR (arg.kind = 5)) THEN
  5956. EmPushStrAddr (arg)
  5957. ELSIF OK () AND ((arg.cls = TChar) OR
  5958. ((arg.cls = TScalar) AND arg.chr)) THEN
  5959. scratch := NewScratchStr () ;
  5960. LoadAtom (arg) ;
  5961. EmStoreVar (FALSE, (symtab [scratch].off + 1) MOD 10000H, 1) ;
  5962. EmMovAxi (1) ;
  5963. EmStoreVar (FALSE, symtab [scratch].off, 1) ;
  5964. EmPushVarAddr (FALSE, symtab [scratch].off)
  5965. ELSIF OK () THEN
  5966. Err (ETypeErr) ;
  5967. RETURN
  5968. END ;
  5969. ExpectDelim (',', ENoSemi) ;
  5970. ParseExpr (dummy) ;
  5971. IF OK () AND (dummy.cls = TString) AND
  5972. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  5973. EmPushStrAddr (dummy) ;
  5974. cap := VarSize (dummy) - 1 ;
  5975. ExpectDelim (',', ENoSemi) ;
  5976. ParseExpr (errV) ;
  5977. IF OK () AND ((errV.cls = TScalar) OR (errV.cls = TChar)) THEN
  5978. LoadAtom (errV) ; EmPushAx () ;
  5979. EmMovAxi (cap) ; EmPushAx () ;
  5980. DropC (EmCall (TU_StrInsert)) ;
  5981. EmAddSp (8)
  5982. ELSIF OK () THEN
  5983. Err (ETypeErr)
  5984. END
  5985. ELSIF OK () THEN
  5986. Err (ETypeErr)
  5987. END ;
  5988. ExpectDelim (')', ENoSemi) ;
  5989. RETURN
  5990. END ;
  5991. IF which = BI_Delete THEN
  5992. (* Delete(S, pos, len): S must be a variable (or a DI-addressed field);
  5993. the runtime mutates it in place. S's address is parked while the
  5994. two integer expressions are parsed. *)
  5995. IF NOT MatchDelim ('(') THEN
  5996. Err (ENoSemi) ;
  5997. RETURN
  5998. END ;
  5999. ParseExpr (arg) ;
  6000. IF OK () AND (arg.cls = TString) AND
  6001. ((arg.kind = 1) OR (arg.kind = 5)) THEN
  6002. EmPushStrAddr (arg) ;
  6003. ExpectDelim (',', ENoSemi) ;
  6004. ParseExpr (dummy) ;
  6005. IF OK () AND ((dummy.cls = TScalar) OR (dummy.cls = TChar)) THEN
  6006. LoadAtom (dummy) ; EmPushAx () ;
  6007. ExpectDelim (',', ENoSemi) ;
  6008. ParseExpr (errV) ;
  6009. IF OK () AND ((errV.cls = TScalar) OR (errV.cls = TChar)) THEN
  6010. LoadAtom (errV) ; EmPushAx () ;
  6011. EmPopAx () ; (* len *)
  6012. EmPopBx () ; (* pos *)
  6013. EmPopCx () ; (* source *)
  6014. EmPushAx () ; EmPushBx () ; EmPushCx () ;
  6015. DropC (EmCall (TU_StrDelete)) ;
  6016. EmAddSp (6)
  6017. ELSIF OK () THEN
  6018. Err (ETypeErr)
  6019. END
  6020. ELSIF OK () THEN
  6021. Err (ETypeErr)
  6022. END
  6023. ELSIF OK () THEN
  6024. Err (ETypeErr)
  6025. END ;
  6026. ExpectDelim (')', ENoSemi) ;
  6027. RETURN
  6028. END ;
  6029. IF which = BI_Val THEN
  6030. (* Val(S, V, E): integer conversion. E receives 0 on success or the
  6031. 1-based position of the first invalid character; V is left unchanged
  6032. on error. *)
  6033. IF NOT MatchDelim ('(') THEN
  6034. Err (ENoSemi) ;
  6035. RETURN
  6036. END ;
  6037. ParseExpr (arg) ;
  6038. IF OK () AND (arg.cls = TString) AND
  6039. ((arg.kind = 1) OR (arg.kind = 3) OR (arg.kind = 5)) THEN
  6040. ExpectDelim (',', ENoSemi) ;
  6041. ParseExpr (dummy) ;
  6042. ExpectDelim (',', ENoSemi) ;
  6043. ParseExpr (errV) ;
  6044. IF OK () AND (dummy.kind = 1) AND (dummy.cls = TScalar)
  6045. AND (VarSize (dummy) = 2) AND (errV.kind = 1)
  6046. AND (errV.cls = TScalar) AND (VarSize (errV) = 2) THEN
  6047. EmPushSymAddr (errV.idx, (symtab [errV.idx].off + errV.boff) MOD 10000H) ;
  6048. EmPushSymAddr (dummy.idx, (symtab [dummy.idx].off + dummy.boff) MOD 10000H) ;
  6049. EmPushStrAddr (arg) ;
  6050. DropC (EmCall (TU_ValInt)) ;
  6051. EmAddSp (6)
  6052. ELSIF OK () AND (dummy.cls = TReal) AND
  6053. ((dummy.kind = 1) OR (dummy.kind = 5)) AND
  6054. (errV.kind = 1) AND (errV.cls = TScalar) AND (VarSize (errV) = 2)
  6055. THEN
  6056. EmPushSymAddr (errV.idx, (symtab [errV.idx].off + errV.boff) MOD 10000H) ;
  6057. EmPushRealAddr (dummy) ;
  6058. EmPushStrAddr (arg) ;
  6059. DropC (EmCall (TU_ValReal)) ;
  6060. EmAddSp (6)
  6061. ELSIF OK () THEN
  6062. Err (ETypeErr)
  6063. END
  6064. ELSIF OK () THEN
  6065. Err (ETypeErr)
  6066. END ;
  6067. ExpectDelim (')', ENoSemi) ;
  6068. RETURN
  6069. END ;
  6070. IF which = BI_Str THEN
  6071. (* Str(Value[:FW[:DEC]], S): format a REAL into a string variable.
  6072. The source address is pushed first and parked on the stack while the
  6073. width/decimals expressions and the destination are parsed; the two
  6074. stack words are then swapped to real_fmt's order (source at [BP+4],
  6075. destination at [BP+6]). CX carries (decimals << 8) | width; it is
  6076. parked in tmpA across the destination parse, which may clobber CX.
  6077. Defaults match TPSRC8 pstdef1/pstdef3: width 18, decimals FFFFh. *)
  6078. IF NOT MatchDelim ('(') THEN
  6079. Err (ENoSemi) ;
  6080. RETURN
  6081. END ;
  6082. ParseExpr (arg) ;
  6083. IF OK () AND (arg.cls = TScalar) THEN
  6084. IF MatchDelim (':') THEN
  6085. ParseExpr (errV) ;
  6086. IF NOT OK () THEN
  6087. RETURN
  6088. END ;
  6089. LoadAtom (arg) ; EmPushAx () ;
  6090. LoadAtom (errV) ; EmPushAx () ;
  6091. IF NOT MatchDelim (',') THEN
  6092. Err (ENoSemi) ;
  6093. RETURN
  6094. END ;
  6095. ParseExpr (dummy) ;
  6096. IF OK () AND (dummy.cls = TString) AND
  6097. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  6098. EmPushStrAddr (dummy) ;
  6099. DropC (EmCall (TU_IntFmtW)) ;
  6100. EmAddSp (6)
  6101. ELSIF OK () THEN
  6102. Err (ETypeErr)
  6103. END ;
  6104. ExpectDelim (')', ENoSemi) ;
  6105. RETURN
  6106. END ;
  6107. LoadAtom (arg) ;
  6108. EmPushAx () ; (* value at [BP+6] *)
  6109. IF NOT MatchDelim (',') THEN
  6110. Err (ENoSemi) ;
  6111. RETURN
  6112. END ;
  6113. ParseExpr (dummy) ;
  6114. IF OK () AND (dummy.cls = TString) AND
  6115. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  6116. EmPushStrAddr (dummy) ; (* destination at [BP+4] *)
  6117. DropC (EmCall (TU_IntFmt)) ;
  6118. EmAddSp (4)
  6119. ELSIF OK () THEN
  6120. Err (ETypeErr)
  6121. END ;
  6122. ExpectDelim (')', ENoSemi) ;
  6123. RETURN
  6124. END ;
  6125. IF OK () AND (arg.cls = TReal) AND
  6126. ((arg.kind = 1) OR (arg.kind = 5)) THEN
  6127. IF arg.kind = 5 THEN
  6128. EmPushDi ()
  6129. ELSE
  6130. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  6131. END ;
  6132. IF MatchDelim (':') THEN
  6133. ParseExpr (dummy) ;
  6134. IF OK () THEN
  6135. LoadAtom (dummy)
  6136. END ;
  6137. IF MatchDelim (':') THEN
  6138. EmPushAx () ;
  6139. ParseExpr (dummy) ;
  6140. IF OK () THEN
  6141. LoadAtom (dummy) ;
  6142. EmMovChAl () ;
  6143. EmPopAx () ;
  6144. EmMovClAl ()
  6145. END
  6146. ELSIF OK () THEN
  6147. EmMovClAl () ;
  6148. EmMovChImm (0FFH)
  6149. END
  6150. ELSE
  6151. EmMovCxi (0FF12H) (* width 18, decimals FFFFh *)
  6152. END ;
  6153. IF NOT OK () THEN
  6154. RETURN
  6155. END ;
  6156. EmMovAxCx () ;
  6157. EmStoreVar (FALSE, symtab [tmpA].off, 2) ;
  6158. IF NOT MatchDelim (',') THEN
  6159. Err (ENoSemi) ;
  6160. RETURN
  6161. END ;
  6162. ParseExpr (dummy) ;
  6163. IF OK () AND (dummy.cls = TString) AND
  6164. ((dummy.kind = 1) OR (dummy.kind = 5)) THEN
  6165. EmPushStrAddr (dummy) ;
  6166. EmPopAx () ; (* destination *)
  6167. EmPopBx () ; (* source *)
  6168. EmPushAx () ; (* destination at [BP+6] *)
  6169. EmPushBx () ; (* source at [BP+4] *)
  6170. EmLoadVar (FALSE, symtab [tmpA].off, 2) ;
  6171. EmXchgAxCx () ; (* CX := packed width/decimals *)
  6172. DropC (EmCall (TU_RealFmt)) ;
  6173. EmAddSp (4)
  6174. ELSIF OK () THEN
  6175. Err (ETypeErr)
  6176. END
  6177. ELSIF OK () THEN
  6178. Err (ETypeErr)
  6179. END ;
  6180. ExpectDelim (')', ENoSemi) ;
  6181. RETURN
  6182. END ;
  6183. IF which = BI_GetDir THEN
  6184. IF MatchDelim ('(') THEN
  6185. ParseExpr (arg) ;
  6186. IF OK () AND ((arg.cls = TScalar) OR (arg.cls = TChar)) THEN
  6187. LoadAtom (arg) ;
  6188. EmPushAx () ; (* drive *)
  6189. ExpectDelim (',', ENoSemi) ;
  6190. ParseExpr (dummy) ;
  6191. IF OK () AND (dummy.cls = TString) AND (dummy.kind = 1) THEN
  6192. EmPushSymAddr (dummy.idx, (symtab [dummy.idx].off + dummy.boff) MOD 10000H) ;
  6193. DropC (EmCall (TU_GetDir)) ;
  6194. EmAddSp (4)
  6195. ELSIF OK () THEN
  6196. Err (ETypeErr)
  6197. END
  6198. ELSIF OK () THEN
  6199. Err (ETypeErr)
  6200. END ;
  6201. ExpectDelim (')', ENoSemi)
  6202. ELSE
  6203. Err (ENoSemi)
  6204. END ;
  6205. RETURN
  6206. END ;
  6207. IF (which = BI_Msdos) OR (which = BI_Intr) THEN
  6208. IF MatchDelim ('(') THEN
  6209. IF which = BI_Intr THEN
  6210. ParseExpr (dummy) ;
  6211. IF OK () AND NOT ((dummy.kind = 0) AND (W16 (dummy.imm) = 21H)) THEN
  6212. Err (ENoLib) ;
  6213. RETURN
  6214. END ;
  6215. ExpectDelim (',', ENoSemi)
  6216. END ;
  6217. ParseExpr (arg) ;
  6218. IF OK () AND (arg.kind = 1) AND (arg.cls = TRecord) THEN
  6219. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6220. DropC (EmCall (TU_Msdos)) ;
  6221. EmAddSp (2)
  6222. ELSIF OK () THEN
  6223. Err (ETypeErr)
  6224. END ;
  6225. ExpectDelim (')', ENoSemi)
  6226. ELSE
  6227. Err (ENoSemi)
  6228. END ;
  6229. RETURN
  6230. END ;
  6231. IF which = BI_Assign THEN
  6232. (* Assign(F, Name): the file variable's address and the counted
  6233. string's address. The runtime copies the characters into the file
  6234. control block and marks it closed; Reset/Rewrite do the DOS open.
  6235. Name is pushed first, so the runtime sees F at [BP+4] and Name at
  6236. [BP+6]. *)
  6237. IF MatchDelim ('(') THEN
  6238. ParseExpr (arg) ;
  6239. IF OK () AND (arg.kind = 1) AND
  6240. ((arg.cls = TFile) OR (arg.cls = TText) OR (arg.cls = TUntyp)) THEN
  6241. ExpectDelim (',', ENoSemi) ;
  6242. ParseExpr (dummy) ;
  6243. IF OK () THEN
  6244. IF dummy.cls = TString THEN
  6245. EmPushStrAddr (dummy) ;
  6246. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6247. DropC (EmCall (TU_FileAssign)) ;
  6248. EmAddSp (4)
  6249. ELSE
  6250. Err (ETypeErr)
  6251. END
  6252. END
  6253. ELSIF OK () THEN
  6254. Err (ETypeErr)
  6255. END ;
  6256. ExpectDelim (')', ENoSemi)
  6257. ELSE
  6258. Err (ENoSemi)
  6259. END ;
  6260. RETURN
  6261. END ;
  6262. IF (which = BI_Reset) OR (which = BI_Rewrite) OR (which = BI_Close) THEN
  6263. IF MatchDelim ('(') THEN
  6264. ParseExpr (arg) ;
  6265. IF OK () THEN
  6266. IF (arg.kind = 1) AND
  6267. ((arg.cls = TFile) OR (arg.cls = TText) OR (arg.cls = TUntyp)) THEN
  6268. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6269. IF which = BI_Reset THEN
  6270. DropC (EmCall (TU_FileReset))
  6271. ELSIF which = BI_Rewrite THEN
  6272. DropC (EmCall (TU_FileRewrite))
  6273. ELSE
  6274. DropC (EmCall (TU_FileClose))
  6275. END ;
  6276. EmAddSp (2)
  6277. ELSE
  6278. Err (ETypeErr)
  6279. END
  6280. END ;
  6281. ExpectDelim (')', ENoSemi)
  6282. ELSE
  6283. Err (ENoSemi)
  6284. END ;
  6285. RETURN
  6286. END ;
  6287. reading := (which = BI_Read) OR (which = BI_ReadLn) ;
  6288. nArgs := 0 ;
  6289. IF MatchDelim ('(') THEN
  6290. IF CurCh () # ')' THEN
  6291. LOOP
  6292. IF nArgs >= 16 THEN
  6293. Err (ECompOvf) ;
  6294. EXIT
  6295. END ;
  6296. ParseExpr (arg) ;
  6297. IF NOT OK () THEN
  6298. (* An argument that did not type-check has no value and arg.idx
  6299. is not a symbol. The string arm below dereferences
  6300. symtab[arg.idx] for ANY value whose class is TString - unlike
  6301. the string assign in Assignment, which can tell a failed
  6302. concat from a real one by its kind - so a `writeln' of a
  6303. rejected expression read symtab with whatever idx held
  6304. before and dereferenced it. That is exactly how
  6305. `writeln('a' + 'b')' - two CHARs, a type error since
  6306. v-TP3-STRCAT - segfaulted the compiler instead of reporting
  6307. the error. ParseSub guards its subscript the same way (see
  6308. `IF OK () THEN' before symtab[r.idx] there), and the
  6309. statement loop stops at the next iteration, so leaving the
  6310. rest of this WRITELN unparsed is harmless. *)
  6311. RETURN
  6312. END ;
  6313. (* One argument at a time: parse it, give it its own push or its
  6314. own runtime call, and only then read the next one. TPSRC8
  6315. pwrloop does exactly this, and it has to - a computed argument
  6316. exists only in AX, so parsing the argument after it, and the
  6317. call made for THIS one, are both free to destroy it. Reading
  6318. the whole list first and emitting afterwards printed the first
  6319. argument as the last argument's value, and pushed an argument
  6320. that an earlier inline string's call had already overwritten. *)
  6321. skipArg := FALSE ;
  6322. handled := FALSE ;
  6323. IF (nArgs = 0) AND (arg.cls = TText) THEN
  6324. (* A leading TEXT file argument. KBD is the console keyboard
  6325. and this runtime's read entries already read the console, so
  6326. the file is dropped and the rest of the argument list is read
  6327. exactly as without it. *)
  6328. IF reading AND (arg.kind = 1) AND symtab [arg.idx].isKbd THEN
  6329. IF NOT MatchDelim (',') THEN
  6330. Err (ENoSemi) ;
  6331. RETURN
  6332. END ;
  6333. skipArg := TRUE
  6334. ELSIF reading THEN
  6335. (* Text-file reads. A number is read as a whitespace-
  6336. delimited token and handed to ValInt/ValReal; a string
  6337. still uses the line reader. Readln consumes the rest
  6338. of the line after all numeric arguments. *)
  6339. IF NOT MatchDelim (',') THEN
  6340. Err (ENoSemi) ;
  6341. RETURN
  6342. END ;
  6343. LOOP
  6344. ParseExpr (dummy) ;
  6345. IF NOT OK () THEN
  6346. RETURN
  6347. END ;
  6348. IF dummy.cls = TString THEN
  6349. IF (dummy.kind = 1) OR (dummy.kind = 5) THEN
  6350. EmMovAxi ((VarSize (dummy) - 1) MOD 10000H) ;
  6351. EmPushAx () ;
  6352. EmPushStrAddr (dummy) ;
  6353. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6354. DropC (EmCall (TU_FileReadln)) ;
  6355. EmAddSp (6)
  6356. ELSE
  6357. Err (ETypeErr) ;
  6358. RETURN
  6359. END
  6360. ELSIF IsCharArray (dummy) AND (dummy.kind = 1) THEN
  6361. scratch := NewScratchStr () ;
  6362. k := 1 ;
  6363. WHILE k <= VarSize (dummy) DO
  6364. EmMovAxi (ORD (' ')) ;
  6365. EmStoreVar (FALSE, (symtab [scratch].off + k) MOD 10000H, 1) ;
  6366. INC (k)
  6367. END ;
  6368. EmMovAxi (VarSize (dummy)) ;
  6369. EmPushAx () ;
  6370. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6371. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6372. DropC (EmCall (TU_FileReadln)) ;
  6373. EmAddSp (6) ;
  6374. EmPushVarAddr (FALSE, (symtab [scratch].off + 1) MOD 10000H) ;
  6375. EmPushSymAddr (dummy.idx, (symtab [dummy.idx].off + dummy.boff) MOD 10000H) ;
  6376. EmMovCxi (VarSize (dummy)) ;
  6377. DropC (EmCall (TU_MemCopy)) ;
  6378. EmAddSp (4)
  6379. ELSIF (dummy.kind = 1) AND
  6380. ((dummy.cls = TScalar) OR (dummy.cls = TReal)) THEN
  6381. scratch := NewScratchStr () ;
  6382. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6383. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6384. DropC (EmCall (TU_FileRdtok)) ;
  6385. EmAddSp (4) ;
  6386. EmPushVarAddr (FALSE, symtab [tmpA].off) ;
  6387. EmPushSymAddr (dummy.idx, (symtab [dummy.idx].off + dummy.boff) MOD 10000H) ;
  6388. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6389. IF dummy.cls = TReal THEN
  6390. DropC (EmCall (TU_ValReal))
  6391. ELSE
  6392. DropC (EmCall (TU_ValInt))
  6393. END ;
  6394. EmAddSp (6)
  6395. ELSE
  6396. Err (ETypeErr) ;
  6397. RETURN
  6398. END ;
  6399. IF NOT MatchDelim (',') THEN
  6400. EXIT
  6401. END
  6402. END ;
  6403. IF which = BI_ReadLn THEN
  6404. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6405. DropC (EmCall (TU_FileRdln)) ;
  6406. EmAddSp (2)
  6407. END ;
  6408. ExpectDelim (')', ENoSemi) ;
  6409. RETURN
  6410. ELSE
  6411. (* Text-file output: Writeln(File) is CRLF, Write(File, C)
  6412. is one CHAR, and writeln(File, S) is S followed by CRLF.
  6413. Write each byte range with DOS AH=40h; a string argument
  6414. carries its own length byte, a one-CHAR literal becomes a
  6415. tiny code-resident <1><char>. *)
  6416. IF (arg.kind # 1) OR (symtab [arg.idx].cls # TText) THEN
  6417. Err (ETypeErr) ;
  6418. RETURN
  6419. END ;
  6420. IF MatchDelim (',') THEN
  6421. LOOP
  6422. ParseExpr (dummy) ;
  6423. IF NOT OK () THEN
  6424. RETURN
  6425. END ;
  6426. IF dummy.cls = TString THEN
  6427. IF MatchDelim (':') THEN
  6428. ParseExpr (errV) ;
  6429. IF NOT OK () THEN
  6430. RETURN
  6431. END ;
  6432. LoadAtom (errV) ;
  6433. EmPushAx () ; (* width *)
  6434. EmPushStrAddr (dummy) ; (* source *)
  6435. scratch := NewScratchStr () ;
  6436. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6437. DropC (EmCall (TU_StrFmtW)) ;
  6438. EmAddSp (6) ;
  6439. EmFileIo (FALSE, symtab [arg.idx].local,
  6440. symtab [arg.idx].off, 1, FALSE,
  6441. symtab [scratch].off, 0, TRUE)
  6442. ELSIF dummy.kind = 1 THEN
  6443. EmFileIo (FALSE, symtab [arg.idx].local,
  6444. symtab [arg.idx].off, 1,
  6445. symtab [dummy.idx].local,
  6446. (symtab [dummy.idx].off + dummy.boff)
  6447. MOD 10000H,
  6448. 0, TRUE)
  6449. ELSIF dummy.kind = 5 THEN
  6450. EmFileIo (FALSE, symtab [arg.idx].local,
  6451. symtab [arg.idx].off, 5, FALSE,
  6452. 0, 0, TRUE)
  6453. ELSIF dummy.kind = 3 THEN
  6454. EmFileIo (FALSE, symtab [arg.idx].local,
  6455. symtab [arg.idx].off, 4, FALSE,
  6456. EmStrBlob (dummy), 0, TRUE)
  6457. ELSE
  6458. Err (ENoLib) ;
  6459. RETURN
  6460. END
  6461. ELSIF (dummy.cls = TChar) OR
  6462. ((dummy.cls = TScalar) AND dummy.chr) THEN
  6463. IF dummy.kind = 1 THEN
  6464. EmFileIo (FALSE, symtab [arg.idx].local,
  6465. symtab [arg.idx].off, 1,
  6466. symtab [dummy.idx].local,
  6467. (symtab [dummy.idx].off + dummy.boff)
  6468. MOD 10000H,
  6469. 1, FALSE)
  6470. ELSIF dummy.kind = 5 THEN
  6471. EmFileIo (FALSE, symtab [arg.idx].local,
  6472. symtab [arg.idx].off, 5, FALSE,
  6473. 0, 1, FALSE)
  6474. ELSIF dummy.kind = 2 THEN
  6475. EmStoreVar (FALSE, symtab [tmpA].off, 1) ;
  6476. EmFileIo (FALSE, symtab [arg.idx].local,
  6477. symtab [arg.idx].off, 1, FALSE,
  6478. symtab [tmpA].off, 1, FALSE)
  6479. ELSIF dummy.kind = 0 THEN
  6480. EmFileIo (FALSE, symtab [arg.idx].local,
  6481. symtab [arg.idx].off, 4, FALSE,
  6482. EmInlineCh (VAL (CARDINAL, dummy.imm)),
  6483. 0, TRUE)
  6484. ELSE
  6485. Err (ENoLib) ;
  6486. RETURN
  6487. END
  6488. ELSIF dummy.cls = TReal THEN
  6489. EmPushRealAddr (dummy) ;
  6490. IF MatchDelim (':') THEN
  6491. ParseExpr (errV) ;
  6492. IF OK () THEN LoadAtom (errV) END ;
  6493. IF MatchDelim (':') THEN
  6494. EmPushAx () ;
  6495. ParseExpr (errV) ;
  6496. IF OK () THEN
  6497. LoadAtom (errV) ;
  6498. EmMovChAl () ;
  6499. EmPopAx () ;
  6500. EmMovClAl ()
  6501. END
  6502. ELSIF OK () THEN
  6503. EmMovClAl () ;
  6504. EmMovChImm (0FFH)
  6505. END
  6506. ELSE
  6507. EmMovCxi (0FF12H)
  6508. END ;
  6509. EmMovAxCx () ;
  6510. EmStoreVar (FALSE, symtab [tmpA].off, 2) ;
  6511. scratch := NewScratchStr () ;
  6512. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6513. EmPopAx () ;
  6514. EmPopBx () ;
  6515. EmPushAx () ;
  6516. EmPushBx () ;
  6517. EmLoadVar (FALSE, symtab [tmpA].off, 2) ;
  6518. EmXchgAxCx () ;
  6519. DropC (EmCall (TU_RealFmt)) ;
  6520. EmAddSp (4) ;
  6521. EmFileIo (FALSE, symtab [arg.idx].local,
  6522. symtab [arg.idx].off, 1, FALSE,
  6523. symtab [scratch].off, 0, TRUE)
  6524. ELSIF dummy.cls = TScalar THEN
  6525. (* integer with the optional TP3 `:width' *)
  6526. LoadAtom (dummy) ;
  6527. EmPushAx () ;
  6528. IF MatchDelim (':') THEN
  6529. ParseExpr (errV) ;
  6530. IF NOT OK () THEN
  6531. RETURN
  6532. END ;
  6533. LoadAtom (errV) ;
  6534. EmPushAx ()
  6535. ELSE
  6536. EmMovAxi (0) ;
  6537. EmPushAx ()
  6538. END ;
  6539. scratch := NewScratchStr () ;
  6540. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6541. DropC (EmCall (TU_IntFmtW)) ;
  6542. EmAddSp (6) ;
  6543. EmFileIo (FALSE, symtab [arg.idx].local,
  6544. symtab [arg.idx].off, 1, FALSE,
  6545. symtab [scratch].off, 0, TRUE)
  6546. ELSE
  6547. Err (ENoLib) ;
  6548. RETURN
  6549. END ;
  6550. IF NOT MatchDelim (',') THEN
  6551. EXIT
  6552. END
  6553. END
  6554. END ;
  6555. IF which = BI_WriteLn THEN
  6556. EmFileIo (FALSE, symtab [arg.idx].local,
  6557. symtab [arg.idx].off, 4, FALSE,
  6558. EmInlineCrLf (), 0, TRUE)
  6559. END ;
  6560. ExpectDelim (')', ENoSemi) ;
  6561. RETURN
  6562. END
  6563. ELSIF (nArgs = 0) AND (arg.cls = TFile) THEN
  6564. (* Typed-file block I/O: `write(MCfile, Sheet[I,J])' and the
  6565. symmetric `read(MCfile, Sheet[FX,FY])'. TP3 moves exactly
  6566. one element per call, so consume the single data argument
  6567. here and emit the DOS block call. The element size comes
  6568. from the data expression and must agree with the file's
  6569. declared element size; a mismatch is a type error, not a
  6570. reason to write the wrong number of bytes. *)
  6571. IF (arg.kind # 1) OR NOT MatchDelim (',') THEN
  6572. Err (ETypeErr) ;
  6573. RETURN
  6574. END ;
  6575. ParseExpr (dummy) ;
  6576. IF NOT OK () THEN
  6577. RETURN
  6578. END ;
  6579. IF ((dummy.kind # 1) AND (dummy.kind # 5))
  6580. OR (VarSize (dummy) # symtab [arg.idx].elem) THEN
  6581. Err (ETypeErr) ;
  6582. RETURN
  6583. END ;
  6584. IF dummy.kind = 5 THEN
  6585. EmFileIo (reading, symtab [arg.idx].local,
  6586. symtab [arg.idx].off, 5, FALSE, 0, VarSize (dummy),
  6587. FALSE)
  6588. ELSE
  6589. EmFileIo (reading, symtab [arg.idx].local,
  6590. symtab [arg.idx].off, 1, symtab [dummy.idx].local,
  6591. (symtab [dummy.idx].off + dummy.boff) MOD 10000H,
  6592. VarSize (dummy), FALSE)
  6593. END ;
  6594. ExpectDelim (')', ENoSemi) ;
  6595. RETURN
  6596. END ;
  6597. IF NOT skipArg AND IsCharArray (arg) AND (arg.kind = 1) THEN
  6598. IF reading THEN
  6599. (* Readln(Place): read through a temporary counted string,
  6600. then copy its fixed-width bytes over the char array.
  6601. The temporary is pre-filled with spaces so a short line
  6602. leaves the array padded, and rdstr leaves the newline
  6603. pending for the Readln wrapper. *)
  6604. scratch := NewScratchStr () ;
  6605. k := 1 ;
  6606. WHILE k <= VarSize (arg) DO
  6607. EmMovAxi (ORD (' ')) ;
  6608. EmStoreVar (FALSE, (symtab [scratch].off + k) MOD 10000H, 1) ;
  6609. INC (k)
  6610. END ;
  6611. EmPushVarAddr (FALSE, symtab [scratch].off) ;
  6612. EmMovCxi (VarSize (arg)) ;
  6613. DropC (EmCall (TU_RdStr)) ;
  6614. EmAddSp (2) ;
  6615. EmPushVarAddr (FALSE, (symtab [scratch].off + 1) MOD 10000H) ;
  6616. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H) ;
  6617. EmMovCxi (VarSize (arg)) ;
  6618. DropC (EmCall (TU_MemCopy)) ;
  6619. EmAddSp (4)
  6620. ELSE
  6621. (* Write a fixed char array one byte at a time. *)
  6622. k := 0 ;
  6623. WHILE k < VarSize (arg) DO
  6624. LoadSymVar (arg.idx, (symtab [arg.idx].off + arg.boff + k) MOD 10000H, 1) ;
  6625. EmMovAh0 () ;
  6626. EmPushAx () ;
  6627. DropC (EmCall (TU_WrChar)) ;
  6628. EmAddSp (2) ;
  6629. INC (k)
  6630. END
  6631. END ;
  6632. handled := TRUE
  6633. END ;
  6634. IF NOT skipArg AND NOT handled THEN
  6635. pushed := TRUE ; (* default: value is on the stack -> call + pop *)
  6636. argBytes := 2 ;
  6637. IF reading THEN
  6638. IF (arg.kind # 1) AND (arg.kind # 5) THEN
  6639. Err (ETypeErr) ; (* READ needs a variable *)
  6640. RETURN
  6641. END ;
  6642. IF arg.kind = 5 THEN
  6643. (* A DI-addressed element: its TYPE is on the expression
  6644. (WithLookup/ParseSub set it), because the symbol is the
  6645. containing array/record. s[i] is TChar; a WITH string
  6646. field is TString. *)
  6647. acls := arg.cls
  6648. ELSE
  6649. acls := symtab [arg.idx].cls ;
  6650. (* s[i] is a CHAR variable, so read(s[i]) must read one byte
  6651. at off+boff, not the whole string at off. arg.cls is
  6652. TChar (set by ParseSub) while the symbol's own class is
  6653. still TString, which is exactly how the element is told
  6654. from the whole string; the address push has to carry boff.
  6655. For a plain variable arg.boff is 0, so nothing changes. *)
  6656. IF (acls = TString) AND (arg.cls = TChar) THEN
  6657. acls := TChar
  6658. END
  6659. END ;
  6660. IF arg.kind = 5 THEN
  6661. (* A variable index: ParseSub computed the element's address
  6662. into DI, and nothing has been emitted since, so that is
  6663. the address READ wants. A constant index folds into boff
  6664. and takes the LEA path below. *)
  6665. EmPushDi ()
  6666. ELSIF symtab [arg.idx].isVar THEN
  6667. (* A VAR parameter: its slot holds the address READ must
  6668. store through, not the address of the slot. *)
  6669. IF arg.boff # 0 THEN
  6670. Err (ENoLib) ;
  6671. RETURN
  6672. END ;
  6673. LoadSymVar (arg.idx, symtab [arg.idx].off, 2) ;
  6674. EmPushAx ()
  6675. ELSE
  6676. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  6677. END ;
  6678. IF acls = TString THEN
  6679. (* readln(s)/read(s) of a string variable: TPSRC8 prdstr
  6680. pushes the address, emits MOV CL,max_len and CALL
  6681. xrdstr. Here the limit is a full MOV CX,imm16 (this
  6682. runtime has no byte-register CH/CL setup), and rdstr
  6683. leaves the terminator pending so the readln wrapper can
  6684. consume the newline. *)
  6685. EmMovCxi ((VarSize (arg) - 1) MOD 10000H) ;
  6686. ent := TU_RdStr
  6687. ELSIF acls = TReal THEN
  6688. ent := TU_RdInt (* real reads: not yet *)
  6689. ELSIF acls = TBool THEN
  6690. (* TP3 refuses boolean I/O: TPSRC8 prderrtp is error 66,
  6691. "I/O not allowed here", and the runtime has no xrdbool
  6692. at all. rdbool stores a WORD, so one byte of an
  6693. accepted read would land on the variable AFTER b - the
  6694. exact overflow t37_readbyte pins for a byte. TP3's
  6695. answer to the whole class is the error; so is ours. *)
  6696. Err (EIoHere) ;
  6697. RETURN
  6698. ELSIF acls = TChar THEN
  6699. ent := TU_RdChar (* one byte, not a word *)
  6700. ELSIF acls = TScalar THEN
  6701. (* Dispatch on the SIZE, as TPSRC8 prdnum does:
  6702. `CMP varsize,#$01 / JA prdemit' keeps xrdint for
  6703. anything wider and falls through to `MOV AX,#xrdbyte'
  6704. for a one-byte variable. A BYTE is still TScalar (it
  6705. is *written* as an integer, as in TP3), but reading one
  6706. through rdint stored a word - and the second byte of
  6707. that word is the byte that FOLLOWS the variable. *)
  6708. IF VarSize (arg) = 1 THEN
  6709. ent := TU_RdByte
  6710. ELSE
  6711. ent := TU_RdInt
  6712. END
  6713. ELSE
  6714. Err (ETypeErr) ;
  6715. RETURN
  6716. END
  6717. ELSE
  6718. acls := arg.cls ;
  6719. IF (acls = TString) AND (arg.kind = 3) THEN
  6720. (* An inline string literal. TP3 TPSRC8 pwrinlin special-cases a
  6721. literal that is followed directly by ',' or ')' - i.e. an
  6722. argument, not an expression - and emits
  6723. CALL wrtinl <length byte> <characters...>
  6724. with no stack argument at all; wrtinl reads the length from the
  6725. return address and returns to just past the last character.
  6726. A following `:width' needs the ordinary address form, so
  6727. wrstrw can pad it; the no-width path keeps the compact
  6728. inline byte stream. *)
  6729. IF strLen [arg.strx] > 255 THEN
  6730. (* The length is one byte, so a literal of 256 characters or
  6731. more would wrap: 300 characters emitted behind a length of
  6732. 44, and the runtime would print 44 of them and silently drop
  6733. the rest. TP3 strings are at most 255 characters, so refuse
  6734. rather than truncate. *)
  6735. Err (EConstRange) ;
  6736. RETURN
  6737. END ;
  6738. IF MatchDelim (':') THEN
  6739. (* A string literal with a field width: make its bytes an
  6740. ordinary code-resident address, then pad before it. *)
  6741. EmPushStrAddr (arg) ;
  6742. ParseExpr (dummy) ;
  6743. IF NOT OK () THEN
  6744. RETURN
  6745. END ;
  6746. LoadAtom (dummy) ;
  6747. EmPushAx () ;
  6748. ent := TU_WrStrW ;
  6749. argBytes := 4
  6750. ELSE
  6751. DropC (EmCall (TU_WrInl)) ;
  6752. Ebyte (VAL (BYTE, strLen [arg.strx])) ;
  6753. n := 0 ;
  6754. WHILE n < strLen [arg.strx] DO
  6755. Ebyte (VAL (BYTE, ORD (strPool [strOff [arg.strx] + n]))) ;
  6756. INC (n)
  6757. END ;
  6758. pushed := FALSE (* nothing was pushed for this one *)
  6759. END
  6760. ELSE
  6761. IF acls = TString THEN
  6762. (* A string *variable*. Its value IS its address, so it is
  6763. passed exactly the way READ passes an address: push it
  6764. and call the owner of the layout - wrstr - which reads
  6765. the length byte and prints that many characters. TPSRC8
  6766. pwrstr/pwrchk route a string argument to xwrtstr, and
  6767. xwrtstr reaches its argument through BP just like WrChar
  6768. does. The only difference from the inline-literal arm
  6769. above is where the bytes live: a literal's are in the
  6770. code stream (wrtinl) and a variable's are in the data
  6771. segment or the frame (wrstr). *)
  6772. (* off + boff: a string FIELD of a record/array has a
  6773. non-zero boff, and the address is the field's, not the
  6774. containing variable's. `writeln (g[2,3].S)' printed
  6775. the empty first element before this.
  6776. A DI-addressed field (`with Sheet[I,J] do
  6777. Write(Contents)') already HAS its address in DI. *)
  6778. IF arg.kind = 5 THEN
  6779. EmPushDi ()
  6780. ELSIF symtab [arg.idx].isVar THEN
  6781. (* VAR string: the slot holds the address of the
  6782. caller's characters; push that, not the slot. *)
  6783. LoadSymVar (arg.idx, symtab [arg.idx].off, 2) ;
  6784. EmPushAx ()
  6785. ELSE
  6786. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  6787. END ;
  6788. ent := TU_WrStr ;
  6789. IF MatchDelim (':') THEN
  6790. ParseExpr (dummy) ;
  6791. IF NOT OK () THEN
  6792. RETURN
  6793. END ;
  6794. LoadAtom (dummy) ;
  6795. EmPushAx () ;
  6796. ent := TU_WrStrW ;
  6797. argBytes := 4
  6798. END
  6799. ELSE
  6800. IF acls = TReal THEN
  6801. (* A REAL is six bytes, so the writer takes its
  6802. ADDRESS, not its value: push the address first,
  6803. then the width, then the decimals. The entry is
  6804. real_write, with [BP+4]=decimals, [BP+6]=width and
  6805. [BP+8]=real. TP3's defaults are width 18 and
  6806. decimals FFFFh (its scientific mode); a width with
  6807. no second colon keeps that default, which is
  6808. exactly what TPSRC8 pwrdef/pwrdef3 push. *)
  6809. IF arg.kind = 5 THEN
  6810. EmPushDi ()
  6811. ELSE
  6812. EmPushSymAddr (arg.idx, (symtab [arg.idx].off + arg.boff) MOD 10000H)
  6813. END ;
  6814. IF MatchDelim (':') THEN
  6815. ParseExpr (dummy) ;
  6816. IF NOT OK () THEN
  6817. RETURN
  6818. END ;
  6819. LoadAtom (dummy) ;
  6820. EmPushAx () ;
  6821. IF MatchDelim (':') THEN
  6822. ParseExpr (dummy) ;
  6823. IF NOT OK () THEN
  6824. RETURN
  6825. END ;
  6826. LoadAtom (dummy) ;
  6827. EmPushAx ()
  6828. ELSE
  6829. EmMovAxi (0FFFFH) ;
  6830. EmPushAx ()
  6831. END
  6832. ELSE
  6833. EmMovAxi (18) ;
  6834. EmPushAx () ;
  6835. EmMovAxi (0FFFFH) ;
  6836. EmPushAx ()
  6837. END ;
  6838. ent := TU_RealWrite ;
  6839. argBytes := 6
  6840. ELSE
  6841. LoadAtom (arg) ;
  6842. EmPushAx () ;
  6843. IF arg.chr THEN
  6844. ent := TU_WrChar (* 'a' - one char, not 97 *)
  6845. ELSIF acls = TBool THEN
  6846. ent := TU_WrBool
  6847. ELSIF acls = TChar THEN
  6848. ent := TU_WrChar (* c : char - one char *)
  6849. ELSIF acls = TScalar THEN
  6850. ent := TU_WrInt
  6851. ELSE
  6852. Err (ETypeErr) ;
  6853. RETURN
  6854. END ;
  6855. IF MatchDelim (':') THEN
  6856. (* Field width: `writeln(I:2)'. TP3 evaluates the
  6857. width (CALC passes `FW', a variable), so it is
  6858. parsed and pushed AFTER the value: width at BP+4,
  6859. value at BP+6. Char, BOOLEAN and integer each
  6860. have their own width entry; a second colon is
  6861. only legal for REAL, handled above. *)
  6862. ParseExpr (dummy) ;
  6863. IF NOT OK () THEN
  6864. RETURN
  6865. END ;
  6866. LoadAtom (dummy) ;
  6867. EmPushAx () ;
  6868. IF MatchDelim (':') THEN
  6869. Err (ENoLib) ;
  6870. RETURN
  6871. END ;
  6872. IF arg.chr OR (acls = TChar) THEN
  6873. ent := TU_WrCharW
  6874. ELSIF acls = TBool THEN
  6875. ent := TU_WrBoolW
  6876. ELSE
  6877. ent := TU_WrIntW
  6878. END ;
  6879. argBytes := 4
  6880. END
  6881. END
  6882. END
  6883. END
  6884. END ;
  6885. IF pushed THEN
  6886. DropC (EmCall (ent)) ;
  6887. EmAddSp (argBytes) (* 2, or 4 with a width *)
  6888. END ;
  6889. INC (nArgs)
  6890. END ;
  6891. IF NOT skipArg THEN
  6892. IF NOT MatchDelim (',') THEN
  6893. EXIT
  6894. END
  6895. END
  6896. END ;
  6897. IF NOT MatchDelim (')') THEN
  6898. Err (ENoSemi) ;
  6899. RETURN
  6900. END
  6901. ELSE
  6902. DropCh (GetCh ())
  6903. END
  6904. END ;
  6905. IF reading AND (nArgs = 0) THEN
  6906. (* readln with no variable: just skip to the next line *)
  6907. DropC (EmCall (TU_RdLn)) ;
  6908. RETURN
  6909. END ;
  6910. IF which = BI_WriteLn THEN
  6911. DropC (EmCall (TU_WrLn))
  6912. ELSIF which = BI_ReadLn THEN
  6913. DropC (EmCall (TU_RdLn))
  6914. END
  6915. END IoCall ;
  6916. PROCEDURE LabelColonAhead () : BOOLEAN ;
  6917. (* `Label:' is a statement label, but `Name;' is a procedure call. Look
  6918. past an identifier and blanks without consuming input. *)
  6919. VAR k : CARDINAL ;
  6920. ch : CHAR ;
  6921. BEGIN
  6922. k := 0 ;
  6923. ch := PeekAhead (k) ;
  6924. IF NOT Alpha (ch) THEN
  6925. RETURN FALSE
  6926. END ;
  6927. WHILE AlphaNum (ch) DO
  6928. INC (k) ;
  6929. ch := PeekAhead (k)
  6930. END ;
  6931. WHILE ch = ' ' DO
  6932. INC (k) ;
  6933. ch := PeekAhead (k)
  6934. END ;
  6935. IF ch # ':' THEN
  6936. RETURN FALSE
  6937. END ;
  6938. (* `Name := ...' is an assignment, not a label definition. *)
  6939. INC (k) ;
  6940. ch := PeekAhead (k) ;
  6941. WHILE ch = ' ' DO
  6942. INC (k) ;
  6943. ch := PeekAhead (k)
  6944. END ;
  6945. RETURN ch # '='
  6946. END LabelColonAhead ;
  6947. PROCEDURE Statmnt () ;
  6948. VAR tok : CARDINAL ;
  6949. idx, i2 : CARDINAL ;
  6950. t, src : ERes ;
  6951. L1, zj, zj2, exj : CARDINAL ;
  6952. lo, hi, v : LONGINT ;
  6953. clso : CARDINAL ;
  6954. i : CARDINAL ;
  6955. k, nlbl, nlblJmp, bodyPc, skipJmp : CARDINAL ;
  6956. nm : ARRAY [0..MaxName] OF CHAR ;
  6957. strf : BOOLEAN ;
  6958. dow : BOOLEAN ;
  6959. savedWith : CARDINAL ;
  6960. BEGIN
  6961. Skip () ;
  6962. IF Digit (CurCh ()) OR LabelColonAhead () THEN
  6963. ParseLabelStmt () ; (* consumed the label and ':' *)
  6964. Statmnt () ; (* the statement follows the label directly *)
  6965. RETURN
  6966. END ;
  6967. IF NOT Alpha (CurCh ()) THEN
  6968. ExpectDelim (';', ENoSemi) ;
  6969. RETURN
  6970. END ;
  6971. DropB (MatchKey (tok)) ;
  6972. IF tok = TkBegin THEN
  6973. Compound ()
  6974. ELSIF tok = TkIf THEN
  6975. ParseExpr (t) ;
  6976. LoadAtom (t) ;
  6977. EmCmpAxi (0) ;
  6978. zj := EmJcc (84H, 0) ; (* JZ -> else/end *)
  6979. IF NOT (MatchKey (tok) AND (tok = TkThen)) THEN
  6980. Err (ENoSemi)
  6981. END ;
  6982. Statmnt () ;
  6983. (* Peek the ELSE, do not match it. `MatchKey (tok) AND (tok = TkElse)`
  6984. consumes whatever the next keyword is even when the AND fails, so an
  6985. `if` that was the LAST statement of a BEGIN..END block ate the block's
  6986. own END: Compound then found neither ';' nor END and raised ENoSemi.
  6987. Any `if` as the last statement of a compound was unparseable - not
  6988. in a loop, not anywhere - and no fixture had one, so nothing noticed.
  6989. The visible symptom was a parse error at the statement AFTER the
  6990. block, which points at entirely the wrong piece of source. *)
  6991. PeekKw (tok) ;
  6992. IF tok = TkElse THEN
  6993. DropB (MatchKey (tok)) ;
  6994. exj := EmJmpNear (0) ;
  6995. SetPatTgt (zj, pc) ;
  6996. Statmnt () ;
  6997. SetPatTgt (exj, pc)
  6998. ELSE
  6999. SetPatTgt (zj, pc)
  7000. END
  7001. ELSIF tok = TkWhile THEN
  7002. L1 := pc ;
  7003. ParseExpr (t) ;
  7004. LoadAtom (t) ;
  7005. EmCmpAxi (0) ;
  7006. zj := EmJcc (84H, 0) ; (* JZ -> end *)
  7007. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  7008. Err (ENoSemi)
  7009. END ;
  7010. brkSave [brkN] := exitCnt ;
  7011. loopTy [brkN] := 1 ;
  7012. INC (brkN) ;
  7013. Statmnt () ;
  7014. DEC (brkN) ;
  7015. i := brkSave [brkN] ;
  7016. WHILE i < exitCnt DO
  7017. SetPatTgt (exitPatch [i], pc) ;
  7018. INC (i)
  7019. END ;
  7020. exitCnt := brkSave [brkN] ;
  7021. DropC (EmJmpNear (L1)) ;
  7022. SetPatTgt (zj, pc)
  7023. ELSIF tok = TkRepeat THEN
  7024. L1 := pc ;
  7025. brkSave [brkN] := exitCnt ;
  7026. loopTy [brkN] := 1 ;
  7027. INC (brkN) ;
  7028. (* The body is a STATEMENT SEQUENCE, not one statement: `repeat a; b
  7029. until c'. This used to call Statmnt once and then demand UNTIL, so
  7030. any body with a second statement was ENoSemi at the first ';'. t12's
  7031. two bodies are single statements and never saw it; CALC's `Flash' does
  7032. (GotoXY; Blink := not Blink; if ...; Write; Delay). Same shape as
  7033. Compound, stopping at UNTIL instead of END. *)
  7034. LOOP
  7035. PeekKw (tok) ;
  7036. IF tok = TkUntil THEN
  7037. DropB (MatchKey (tok)) ;
  7038. EXIT
  7039. END ;
  7040. Statmnt () ;
  7041. IF NOT OK () THEN
  7042. RETURN
  7043. END ;
  7044. IF NOT MatchDelim (';') THEN
  7045. PeekKw (tok) ;
  7046. IF tok = TkUntil THEN
  7047. DropB (MatchKey (tok)) ;
  7048. EXIT
  7049. END ;
  7050. Err (ENoSemi) ;
  7051. RETURN
  7052. END
  7053. END ;
  7054. ParseExpr (t) ;
  7055. LoadAtom (t) ;
  7056. EmCmpAxi (0) ;
  7057. (* UNTIL exits when the condition is TRUE, so the body runs again when
  7058. it is FALSE. The condition is a 0/1 in AX and EmCmpAxi (0) has just
  7059. compared it with 0, so ZF=1 means "condition false" -- which is
  7060. exactly the case that loops, hence JZ.
  7061. This was JNZ, which compiled repeat-until as while-until: the body
  7062. ran once, the condition was tested, and it stopped. t12_repeat is
  7063. `i:=0; repeat i:=i+1 until i>5' and it printed 1.
  7064. No patch slot: L1 is backwards and already known, so EmJcc returns
  7065. 0 and there is nothing to SetPatTgt. *)
  7066. DropC (EmJcc (84H, L1)) ; (* JZ -> body again *)
  7067. DEC (brkN) ;
  7068. i := brkSave [brkN] ;
  7069. WHILE i < exitCnt DO
  7070. SetPatTgt (exitPatch [i], pc) ;
  7071. INC (i)
  7072. END ;
  7073. exitCnt := brkSave [brkN]
  7074. ELSIF tok = TkFor THEN
  7075. (* control variable *)
  7076. Skip () ; (* after the FOR keyword: skip blanks *)
  7077. IF NOT Alpha (CurCh ()) THEN
  7078. Err (EUnknown) ;
  7079. RETURN
  7080. END ;
  7081. GetWord () ;
  7082. IF NOT Search (wrd, idx) THEN
  7083. Err (EUnknown) ;
  7084. RETURN
  7085. END ;
  7086. IF symtab [idx].size > 2 THEN
  7087. Err (ENoLib) ;
  7088. RETURN
  7089. END ;
  7090. IF NOT MatchAssign () THEN
  7091. Err (ENoSemi)
  7092. END ;
  7093. ParseExpr (src) ;
  7094. LoadAtom (src) ;
  7095. StoreSymVar (idx, symtab [idx].off, symtab [idx].size) ;
  7096. IF NOT MatchKey (tok) THEN
  7097. Err (ESimpType) ;
  7098. RETURN
  7099. END ;
  7100. IF (tok = TkTo) OR (tok = TkDownto) THEN
  7101. dow := (tok = TkDownto)
  7102. ELSE
  7103. Err (ESimpType) ;
  7104. RETURN
  7105. END ;
  7106. ParseExpr (t) ;
  7107. LoadAtom (t) ;
  7108. EmPushAx () ; (* loop bound on the stack *)
  7109. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  7110. Err (ENoSemi)
  7111. END ;
  7112. brkSave [brkN] := exitCnt ;
  7113. loopTy [brkN] := 2 ;
  7114. INC (brkN) ;
  7115. L1 := pc ; (* Ltest: the test comes FIRST *)
  7116. (* The test is emitted before the body, not after it. It used to be
  7117. emitted after, which is a post-test loop and runs the body one time
  7118. too many: with `for i := 1 to 5`, the sequence of i at the test is
  7119. 1,2,3,4,5,6 - the test at i=5 is `5 > 5`, which is false, so the
  7120. body ran a sixth time with i=6. `for i := 1 to 5 do s := s + i`
  7121. printed 21. The size was right and the shape was right; only the
  7122. order was wrong, and no byte check can see an order.
  7123. The bound stays on the stack for the whole loop, so EmMovCxSp has to
  7124. re-read it every iteration - which is also what makes the bound a
  7125. *variable* rather than a constant. [SP] cannot be encoded on the
  7126. 8086, so EmMovCxSp is POP CX ; PUSH CX, an observational no-op that
  7127. leaves the bound in place. *)
  7128. EmMovCxSp () ;
  7129. LoadSymVar (idx, symtab [idx].off, symtab [idx].size) ;
  7130. IF symtab [idx].size = 1 THEN
  7131. (* A CHAR/BYTE control variable loads into AL only. Without zeroing
  7132. AH, the previous body's leftover AH (wrchar leaves 02h there) makes
  7133. the 16-bit CMP read 0242h and the loop stops after 'A'. *)
  7134. EmMovAh0 ()
  7135. END ;
  7136. EmCmpAxCx () ;
  7137. IF dow THEN
  7138. zj := EmJcc (8CH, 0) (* JL -> done *)
  7139. ELSE
  7140. zj := EmJcc (8FH, 0) (* JG -> done *)
  7141. END ;
  7142. Statmnt () ;
  7143. DEC (brkN) ;
  7144. (* A FOR's exits are NOT patched here, even though `done` is not known
  7145. yet. For a WHILE or REPEAT, "just after the body" is a correct
  7146. target: the jump back to the test re-evaluates the condition and
  7147. leaves. For a FOR there is a STEP between the body and `done`, so
  7148. an EXIT that jumped here would increment the control variable and
  7149. jump back to the test - and if the incremented value still satisfied
  7150. the bound, it would run the body AGAIN. `exit` did not exit.
  7151. The fix needs no new bookkeeping: `brkSave [brkN] .. exitCnt` still
  7152. names exactly this loop's exits, because Statmnt may have added more
  7153. and nothing has reset exitCnt. So they are patched at `done`, below.
  7154. A WHILE nested inside the FOR saves and restores its own range and
  7155. leaves this one intact. *)
  7156. (* step *)
  7157. LoadSymVar (idx, symtab [idx].off, symtab [idx].size) ;
  7158. IF symtab [idx].size = 1 THEN
  7159. EmMovAh0 ()
  7160. END ;
  7161. IF dow THEN
  7162. EmDecAx ()
  7163. ELSE
  7164. EmIncAx ()
  7165. END ;
  7166. StoreSymVar (idx, symtab [idx].off, symtab [idx].size) ;
  7167. DropC (EmJmpNear (L1)) ;
  7168. SetPatTgt (zj, pc) ; (* done: *)
  7169. (* A WHILE loop here, not a FOR over the exit range. The range is
  7170. usually EMPTY - most loops have no `exit` - and `exitCnt` is a
  7171. CARDINAL, so `TO exitCnt - 1` with exitCnt = 0 is `TO 65535`: the
  7172. loop does not terminate, it wraps, and it walks exitPatch [0..65535]
  7173. off the end of a 64-element array. `for i := 1 to 10 do i := i` has
  7174. no exit, so this is the ORDINARY case, and it faulted with
  7175. "invalid address referenced" on every FOR loop without an exit. *)
  7176. i := brkSave [brkN] ;
  7177. WHILE i < exitCnt DO
  7178. SetPatTgt (exitPatch [i], pc) ;
  7179. INC (i)
  7180. END ;
  7181. exitCnt := brkSave [brkN] ;
  7182. EmAddSp (2) (* drop the loop bound *)
  7183. ELSIF tok = TkCase THEN
  7184. ParseExpr (t) ;
  7185. LoadAtom (t) ;
  7186. EmPushAx () ; (* selector on the stack *)
  7187. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  7188. Err (ENoSemi)
  7189. END ;
  7190. caseN := 0 ;
  7191. LOOP
  7192. Skip () ;
  7193. IF MatchDelim (';') THEN
  7194. Skip ()
  7195. END ;
  7196. PeekKw (tok) ;
  7197. IF (tok = TkEnd) OR (tok = TkElse) THEN
  7198. EXIT
  7199. END ;
  7200. (* Case labels: `v' | `lo..hi', comma-separated, all selecting the
  7201. same statement (CALC's `'G', 'I', 'O', 'Q':'). Read the whole list
  7202. first; the emission then knows whether this is the one-label path
  7203. or a list. *)
  7204. nlbl := 0 ;
  7205. LOOP
  7206. IF Alpha (CurCh ()) THEN
  7207. GetWord () ;
  7208. IF Search (wrd, i2) AND (symtab [i2].tag = KConst) THEN
  7209. lo := symtab [i2].lval
  7210. ELSE
  7211. Err (EUnknown) ;
  7212. RETURN
  7213. END
  7214. ELSE
  7215. RdConst (lo, clso, strf)
  7216. END ;
  7217. IF MatchRange () THEN
  7218. RdConst (hi, clso, strf)
  7219. ELSE
  7220. hi := lo
  7221. END ;
  7222. IF nlbl >= 32 THEN
  7223. Err (ECompOvf) ;
  7224. RETURN
  7225. END ;
  7226. lblLo [nlbl] := lo ;
  7227. lblHi [nlbl] := hi ;
  7228. INC (nlbl) ;
  7229. IF MatchDelim (',') THEN
  7230. Skip ()
  7231. ELSE
  7232. EXIT
  7233. END
  7234. END ;
  7235. Skip () ;
  7236. ExpectDelim (':', ENoSemi) ;
  7237. IF NOT OK () THEN
  7238. RETURN
  7239. END ;
  7240. EmMovAxSp () ;
  7241. IF nlbl = 1 THEN
  7242. (* One label: the original bytes, except that a RANGE is now a
  7243. real range. It used to compare AX with lo and then with hi,
  7244. both demanding equality, so `1..5' matched NOTHING unless
  7245. lo = hi - a silently wrong answer, not an error. *)
  7246. lo := lblLo [0] ;
  7247. hi := lblHi [0] ;
  7248. IF hi = lo THEN
  7249. EmCmpAxi (W16 (lo)) ;
  7250. zj := EmJcc (85H, 0) ; (* JNZ -> next arm *)
  7251. zj2 := 0
  7252. ELSE
  7253. EmCmpAxi (W16 (lo)) ;
  7254. zj := EmJcc (8CH, 0) ; (* JL -> next arm *)
  7255. EmCmpAxi (W16 (hi)) ;
  7256. zj2 := EmJcc (8FH, 0) (* JG -> next arm *)
  7257. END ;
  7258. Statmnt () ;
  7259. IF caseN >= 64 THEN
  7260. Err (ECompOvf) ;
  7261. RETURN
  7262. END ;
  7263. caseJmp [caseN] := EmJmpNear (0) ;
  7264. INC (caseN) ;
  7265. SetPatTgt (zj, pc) ;
  7266. IF zj2 # 0 THEN
  7267. SetPatTgt (zj2, pc)
  7268. END
  7269. ELSE
  7270. (* A list. A single label branches INTO the statement; a range
  7271. first sends AX below lo to the next label, then `<= hi' to the
  7272. body. The body jumps are patched once the body's address is
  7273. known, and the no-match EJMP skips the body to the next arm. *)
  7274. nlblJmp := 0 ;
  7275. FOR k := 0 TO nlbl - 1 DO
  7276. IF lblHi [k] = lblLo [k] THEN
  7277. EmCmpAxi (W16 (lblLo [k])) ;
  7278. lblJmp [nlblJmp] := EmJcc (84H, 0) ; (* JZ -> body *)
  7279. INC (nlblJmp)
  7280. ELSE
  7281. (* `lo <= AX <= hi', built from the two conditions this
  7282. compiler declares for a branch (L and G): below lo skips
  7283. the label, above hi skips it, otherwise an unconditional
  7284. jump enters the body. JLE would be shorter and is legal
  7285. 8086, but check_8086's declared condition set is exactly
  7286. Z/NZ/L/G (the four the parser's comparisons use), and a
  7287. new condition in CASE would rightly be reported there. *)
  7288. EmCmpAxi (W16 (lblLo [k])) ;
  7289. zj := EmJcc (8CH, 0) ; (* JL -> next label *)
  7290. EmCmpAxi (W16 (lblHi [k])) ;
  7291. zj2 := EmJcc (8FH, 0) ; (* JG -> next label *)
  7292. lblJmp [nlblJmp] := EmJmpNear (0) ; (* in range -> body *)
  7293. INC (nlblJmp) ;
  7294. SetPatTgt (zj, pc) ;
  7295. SetPatTgt (zj2, pc)
  7296. END
  7297. END ;
  7298. skipJmp := EmJmpNear (0) ;
  7299. bodyPc := pc ;
  7300. FOR k := 0 TO nlblJmp - 1 DO
  7301. SetPatTgt (lblJmp [k], bodyPc)
  7302. END ;
  7303. Statmnt () ;
  7304. IF caseN >= 64 THEN
  7305. Err (ECompOvf) ;
  7306. RETURN
  7307. END ;
  7308. caseJmp [caseN] := EmJmpNear (0) ;
  7309. INC (caseN) ;
  7310. SetPatTgt (skipJmp, pc)
  7311. END
  7312. END ;
  7313. IF tok = TkElse THEN
  7314. DropB (MatchKey (tok)) ;
  7315. Statmnt () ;
  7316. (* A semicolon between the ELSE statement and the CASE's END is an
  7317. empty statement, and TP3/CALC writes it after a compound body:
  7318. `else begin ... end; end`. The non-ELSE path already tolerates
  7319. the same separator at the top of the arm loop. *)
  7320. IF MatchDelim (';') THEN
  7321. Skip ()
  7322. END ;
  7323. IF NOT (MatchKey (tok) AND (tok = TkEnd)) THEN
  7324. Err (ENoSemi)
  7325. END
  7326. ELSE
  7327. DropB (MatchKey (tok))
  7328. END ;
  7329. EmAddSp (2) ;
  7330. FOR i := 0 TO caseN - 1 DO
  7331. SetPatTgt (caseJmp [i], pc)
  7332. END
  7333. ELSIF tok = TkGoto THEN
  7334. v := 0 ;
  7335. Skip () ; (* after the GOTO keyword: skip blanks *)
  7336. IF Alpha (CurCh ()) THEN
  7337. GetWord () ;
  7338. SaveWord (nm)
  7339. ELSIF Digit (CurCh ()) THEN
  7340. RdIntConst (v) ;
  7341. NumToName (W16 (v), nm)
  7342. ELSE
  7343. Err (EUnknown) ;
  7344. RETURN
  7345. END ;
  7346. IF Search (nm, idx) THEN
  7347. IF symtab [idx].tag = KLabel THEN
  7348. IF symtab [idx].defnd THEN
  7349. DropC (EmJmpNear (symtab [idx].goPos))
  7350. ELSE
  7351. zj := EmJmpNear (0) ;
  7352. AddPend (0, idx, zj)
  7353. END
  7354. ELSE
  7355. Err (EUnknown)
  7356. END
  7357. ELSE
  7358. idx := NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE) ;
  7359. zj := EmJmpNear (0) ;
  7360. AddPend (0, idx, zj)
  7361. END
  7362. ELSIF tok = TkExit THEN
  7363. IF brkN = 0 THEN
  7364. Err (EUnknown)
  7365. ELSE
  7366. (* No EmAddSp (2) here, even inside a FOR. The FOR's `done` label
  7367. drops the bound, so an EXIT that jumped to `done` would drop it a
  7368. second time - 4 bytes off a stack that only had 2 to give, which
  7369. silently corrupts the caller's frame. It used to do exactly
  7370. that, and it was doubly wrong: the exits were patched to the STEP
  7371. rather than to `done`, so the EXIT also incremented the control
  7372. variable and jumped back into the test. *)
  7373. zj := EmJmpNear (0) ;
  7374. IF exitCnt < 64 THEN
  7375. exitPatch [exitCnt] := zj ;
  7376. INC (exitCnt)
  7377. END
  7378. END
  7379. ELSIF tok = TkWith THEN
  7380. (* WITH base (, base)* DO statement. The record's field range comes from
  7381. the base symbol: DefVar copies the ELEMENT record's recFldFirst/
  7382. recFldLast onto an array variable, which is what makes
  7383. `Sheet[I,J].Field' and `with Sheet[I,J] do' work. Each base is
  7384. pushed on the scope stack and popped after the statement. *)
  7385. savedWith := withN ;
  7386. LOOP
  7387. ParseExpr (t) ;
  7388. IF NOT OK () THEN
  7389. RETURN
  7390. END ;
  7391. IF t.cls # TRecord THEN
  7392. Err (ETypeErr) ;
  7393. RETURN
  7394. END ;
  7395. IF withN >= 4 THEN
  7396. Err (ECompOvf) ;
  7397. RETURN
  7398. END ;
  7399. IF t.fldFirst >= t.fldLast THEN
  7400. Err (ETypeErr) ;
  7401. RETURN
  7402. END ;
  7403. IF t.kind = 5 THEN
  7404. (* the base address is in DI; park it. Allocating two data bytes
  7405. here is safe: the statement is parsed once, and the store
  7406. executes wherever the WITH does. *)
  7407. withSlot [withN] := dc ;
  7408. dc := dc + 2 ;
  7409. EmStDiGlob (withSlot [withN]) ;
  7410. withIsDI [withN] := TRUE ;
  7411. withBoff [withN] := 0
  7412. ELSIF t.kind = 1 THEN
  7413. withIsDI [withN] := FALSE ;
  7414. withBoff [withN] := t.boff
  7415. ELSE
  7416. Err (ENoLib) ;
  7417. RETURN
  7418. END ;
  7419. withIdx [withN] := t.idx ;
  7420. withFF [withN] := t.fldFirst ;
  7421. withFL [withN] := t.fldLast ;
  7422. INC (withN) ;
  7423. IF MatchDelim (',') THEN
  7424. (* another base; the last one is innermost *)
  7425. ELSE
  7426. EXIT
  7427. END
  7428. END ;
  7429. IF NOT (MatchKey (tok) AND (tok = TkDo)) THEN
  7430. Err (ENoSemi) ;
  7431. RETURN
  7432. END ;
  7433. Statmnt () ;
  7434. withN := savedWith
  7435. ELSE
  7436. (* identifier statement: assignment or call *)
  7437. IF WithLookup (wrd, t) THEN
  7438. (* A field inside WITH may be followed by `.subfield' or `[index]'. *)
  7439. ParseSub (t) ;
  7440. IF MatchAssign () THEN
  7441. Assignment (t) ;
  7442. RETURN
  7443. END ;
  7444. Err (ENoSemi) ;
  7445. RETURN
  7446. END ;
  7447. IF NOT Search (wrd, idx) THEN
  7448. Err (EUnknown) ;
  7449. RETURN
  7450. END ;
  7451. IF symtab [idx].tag = KBuiltin THEN
  7452. IoCall (idx) ;
  7453. RETURN
  7454. END ;
  7455. IF symtab [idx].tag = KProc THEN
  7456. IF MatchDelim ('(') THEN
  7457. ParseCallArgs (idx)
  7458. ELSE
  7459. ParseCall (idx)
  7460. END ;
  7461. RETURN
  7462. END ;
  7463. IF (symtab [idx].tag = KVar) OR (symtab [idx].tag = KFunc) THEN
  7464. t.idx := idx ;
  7465. t.kind := 1 ;
  7466. t.boff := 0 ;
  7467. t.cls := symtab [idx].cls ;
  7468. IF symtab [idx].tag = KFunc THEN
  7469. t.idx := symtab [idx].resvar ;
  7470. t.cls := symtab [t.idx].cls
  7471. END ;
  7472. t.sz := symtab [t.idx].size ; (* ParseSub may narrow it further *)
  7473. t.fldFirst := symtab [t.idx].fldFirst ;
  7474. t.fldLast := symtab [t.idx].fldLast ;
  7475. t.arrIdx := symtab [t.idx].arrIdx ;
  7476. t.ptrElem := symtab [t.idx].elem ;
  7477. IF t.cls = TSet THEN
  7478. (* The identifier-statement path does not go through ParseVar, so
  7479. it must carry the same set metadata: Assignment's set branch
  7480. uses setsize/setbase to build the constructor. *)
  7481. t.setoff := symtab [t.idx].off ;
  7482. t.setbase := symtab [t.idx].elem ;
  7483. t.setsize := symtab [t.idx].size
  7484. END ;
  7485. ParseSub (t) ;
  7486. IF MatchAssign () THEN
  7487. Assignment (t) ;
  7488. RETURN
  7489. END ;
  7490. Err (ENoSemi) ;
  7491. RETURN
  7492. END ;
  7493. Err (ENoSemi)
  7494. END
  7495. END Statmnt ;
  7496. (* ---------------------------------------------------------------- *)
  7497. (* types and declarations (TPSRC7) *)
  7498. (* ---------------------------------------------------------------- *)
  7499. PROCEDURE ParseType (VAR cls, size, elem : CARDINAL) ;
  7500. VAR tok : CARDINAL ;
  7501. idx : CARDINAL ;
  7502. lo, hi : LONGINT ;
  7503. s2, e2 : CARDINAL ;
  7504. subcls : CARDINAL ;
  7505. strf : BOOLEAN ;
  7506. isCharRange : BOOLEAN ;
  7507. consumed : BOOLEAN ;
  7508. running, groupFirst, i, an, recStart : CARDINAL ;
  7509. innerIdx, innerN, leafElem, leafCls, j : CARDINAL ;
  7510. elemFldFirst, elemFldLast, fieldFldFirst, fieldFldLast, fieldArrIdx : CARDINAL ;
  7511. elemType : CARDINAL ;
  7512. fieldPtrElem : CARDINAL ;
  7513. variantBase, armRunning, armMax, savePosVar : CARDINAL ;
  7514. moreFields : BOOLEAN ;
  7515. tagName : ARRAY [0..MaxName] OF CHAR ;
  7516. alo, ahi : ARRAY [0..3] OF LONGINT ;
  7517. ecls2 : CARDINAL ;
  7518. BEGIN
  7519. cls := TNone ; size := 0 ; elem := 0 ;
  7520. consumed := FALSE ;
  7521. newArrIdx := 0 ;
  7522. Skip () ; (* after ':' / '=' : skip blanks *)
  7523. IF CurCh () = '^' THEN
  7524. (* A pointer type: ^Type. The pointer itself is a word; its `elem'
  7525. field holds the pointee TYPE symbol, not a byte size. The pointee's
  7526. array/record metadata is copied onto the pointer variable so a
  7527. dereference can hand it to ParseSub unchanged. *)
  7528. DropCh (GetCh ()) ;
  7529. Skip () ;
  7530. IF NOT Alpha (CurCh ()) THEN
  7531. Err (ESimpType) ;
  7532. RETURN
  7533. END ;
  7534. GetWord () ;
  7535. IF Search (wrd, idx) THEN
  7536. IF symtab [idx].tag # KType THEN
  7537. Err (ESimpType) ;
  7538. RETURN
  7539. END
  7540. ELSE
  7541. (* Forward type reference: `Next : ^Person' before the record is
  7542. declared. Create a placeholder that DefType will fill. *)
  7543. idx := NewSym (wrd, KType, TNone, 0, 0, 0, 0, FALSE) ;
  7544. symtab [idx].fwd := TRUE
  7545. END ;
  7546. cls := TPtr ;
  7547. size := 2 ;
  7548. elem := idx ; (* pointee type symbol *)
  7549. newArrIdx := symtab [idx].arrIdx ;
  7550. recFldFirst := symtab [idx].fldFirst ;
  7551. recFldLast := symtab [idx].fldLast ;
  7552. enumLo := symtab [idx].lo ;
  7553. enumHi := symtab [idx].hi ;
  7554. RETURN
  7555. END ;
  7556. IF CurCh () = '(' THEN
  7557. (* An enumeration: `(Constant, Formula, Txt, ...)'. Each enumerator
  7558. becomes a constant with its ordinal position as its value, and the
  7559. TYPE is 1 byte (TP3 uses one below 256 values) with bounds 0..n-1,
  7560. which DefType records so `set of Attributes' can size its base.
  7561. Enumerators are TScalar, not TEnum: they behave as ordinals; only the
  7562. TYPE carries the enum identity and its bounds. *)
  7563. DropCh (GetCh ()) ;
  7564. lo := 0 ;
  7565. hi := -1 ;
  7566. LOOP
  7567. Skip () ;
  7568. IF NOT Alpha (CurCh ()) THEN
  7569. Err (EUnknown) ;
  7570. RETURN
  7571. END ;
  7572. GetWord () ;
  7573. DupTest (wrd) ;
  7574. DropC (NewSym (wrd, KConst, TScalar, 1, 1, 0, hi + 1, FALSE)) ;
  7575. hi := hi + 1 ;
  7576. IF MatchDelim (',') THEN
  7577. (* another enumerator *)
  7578. ELSE
  7579. EXIT
  7580. END
  7581. END ;
  7582. ExpectDelim (')', ENoSemi) ;
  7583. enumLo := lo ;
  7584. enumHi := hi ;
  7585. cls := TEnum ;
  7586. size := 1 ;
  7587. elem := 1 ;
  7588. RETURN
  7589. END ;
  7590. IF Alpha (CurCh ()) THEN
  7591. DropB (MatchKey (tok)) ;
  7592. consumed := TRUE
  7593. ELSE
  7594. tok := TkNone
  7595. END ;
  7596. IF tok = TkArray THEN
  7597. (* array [range, range, ...] of T. Several ranges are allowed; the LAST
  7598. varies fastest (row-major), so `Sheet[fx,fy]' strides the first range
  7599. by the second's count (RESUME-TP3.md §2). The shape is kept in the
  7600. array table; the symbol carries a pointer to it (SymEntry.arrIdx) so
  7601. indexing can scale by the element size and subtract each lower bound. *)
  7602. ExpectDelim ('[', ENoSemi) ;
  7603. an := 0 ;
  7604. LOOP
  7605. Skip () ;
  7606. IF Alpha (CurCh ()) THEN
  7607. (* An enum/subrange TYPE as the index: `array[Standardfunction]'.
  7608. Its bounds are on the type symbol. *)
  7609. GetWord () ;
  7610. IF NOT Search (wrd, idx) OR (symtab [idx].tag # KType)
  7611. OR (symtab [idx].cls # TEnum) THEN
  7612. Err (ESimpType) ;
  7613. RETURN
  7614. END ;
  7615. lo := symtab [idx].lo ;
  7616. hi := symtab [idx].hi
  7617. ELSE
  7618. RdConst (lo, subcls, strf) ;
  7619. IF NOT MatchRange () THEN
  7620. Err (ESimpType) ;
  7621. RETURN
  7622. END ;
  7623. RdConst (hi, subcls, strf)
  7624. END ;
  7625. IF an < 4 THEN
  7626. alo [an] := lo ;
  7627. ahi [an] := hi ;
  7628. INC (an)
  7629. END ;
  7630. IF MatchDelim (',') THEN
  7631. (* another range *)
  7632. ELSE
  7633. EXIT
  7634. END
  7635. END ;
  7636. ExpectDelim (']', ENoSemi) ;
  7637. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  7638. Err (ENoSemi) ;
  7639. RETURN
  7640. END ;
  7641. ParseType (cls, s2, e2) ;
  7642. ecls2 := cls ;
  7643. innerIdx := newArrIdx ;
  7644. elemFldFirst := recFldFirst ;
  7645. elemFldLast := recFldLast ;
  7646. elemType := 0 ;
  7647. IF cls = TPtr THEN
  7648. elemType := e2
  7649. END ;
  7650. IF (cls = TArray) AND (innerIdx > 0) THEN
  7651. (* Flatten array-of-array: the outer symbol must index the same
  7652. number of dimensions as `array[a,b]'. A named element array
  7653. (`Airplane = array[...] of Dog_Food') carries its shape in the
  7654. inner ArrRec; keep only the leaf element size and class here. *)
  7655. leafElem := arrs [innerIdx - 1].elem ;
  7656. leafCls := arrs [innerIdx - 1].ecls ;
  7657. elemFldFirst := arrs [innerIdx - 1].fldFirst ;
  7658. elemFldLast := arrs [innerIdx - 1].fldLast ;
  7659. elemType := arrs [innerIdx - 1].elemType ;
  7660. innerN := arrs [innerIdx - 1].n
  7661. ELSE
  7662. leafElem := s2 ;
  7663. leafCls := ecls2 ;
  7664. innerN := 0
  7665. END ;
  7666. cls := TArray ;
  7667. elem := leafElem ;
  7668. size := s2 ;
  7669. i := 0 ;
  7670. WHILE i < an DO
  7671. size := size * W16 (VAL (LONGINT, W16 (ahi [i]))
  7672. - VAL (LONGINT, W16 (alo [i])) + 1) ;
  7673. INC (i)
  7674. END ;
  7675. (* register the flattened shape so indexing can scale by leaf element
  7676. size and subtract each lower bound; the symbol points at this slot *)
  7677. IF (an + innerN <= 4) AND (arrTop < MaxArrs) THEN
  7678. arrs [arrTop].n := an + innerN ;
  7679. arrs [arrTop].elem := leafElem ;
  7680. arrs [arrTop].ecls := leafCls ;
  7681. arrs [arrTop].fldFirst := elemFldFirst ;
  7682. arrs [arrTop].fldLast := elemFldLast ;
  7683. arrs [arrTop].elemType := elemType ;
  7684. i := 0 ;
  7685. WHILE i < an DO
  7686. arrs [arrTop].lo [i] := alo [i] ;
  7687. arrs [arrTop].hi [i] := ahi [i] ;
  7688. INC (i)
  7689. END ;
  7690. j := 0 ;
  7691. WHILE j < innerN DO
  7692. arrs [arrTop].lo [an + j] := arrs [innerIdx - 1].lo [j] ;
  7693. arrs [arrTop].hi [an + j] := arrs [innerIdx - 1].hi [j] ;
  7694. INC (j)
  7695. END ;
  7696. newArrIdx := arrTop + 1 ;
  7697. INC (arrTop)
  7698. ELSE
  7699. Err (ECompOvf)
  7700. END
  7701. ELSIF tok = TkString THEN
  7702. cls := TString ;
  7703. size := 256 ;
  7704. elem := 1 ;
  7705. IF MatchDelim ('[') THEN
  7706. RdConst (hi, subcls, strf) ;
  7707. ExpectDelim (']', ENoSemi) ;
  7708. size := W16 (hi) + 1
  7709. END
  7710. ELSIF tok = TkSet THEN
  7711. (* set of <ordinal>. TP3's hybrid layout (coeur-tp-ocr 4.7.2): only the
  7712. whole BYTES covering the base range are stored, so `set of Char' is 32
  7713. bytes and `set of 12..17' is 2. `elem' carries the origin - the first
  7714. element of the first byte, a multiple of 8 - which is all the
  7715. membership and constructor arithmetic needs. *)
  7716. PeekKw (tok) ;
  7717. IF tok = TkOf THEN
  7718. DropB (MatchKey (tok)) ;
  7719. RdSetBase (subcls, lo, hi) ;
  7720. IF OK () THEN
  7721. cls := TSet ;
  7722. elem := (VAL (CARDINAL, lo) DIV 8) * 8 ;
  7723. size := (VAL (CARDINAL, hi) DIV 8)
  7724. - (VAL (CARDINAL, lo) DIV 8) + 1
  7725. END
  7726. ELSE
  7727. Err (ENoSemi)
  7728. END
  7729. ELSIF tok = TkRecord THEN
  7730. (* record fieldList end, fields NON-COMPACTED and juxtaposed
  7731. (RESUME-TP3.md §2). Each name in a `a, b : T' group is a field of its
  7732. own at the running offset; the field table is where `x.f' looks, keyed
  7733. per record so field names need not be globally unique.
  7734. recFldFirst/recFldLast hand the range to DefType, or to the enclosing
  7735. record's field entry when records nest. *)
  7736. recStart := fldTop ;
  7737. recFldFirst := fldTop ;
  7738. running := 0 ;
  7739. LOOP
  7740. PeekKw (tok) ;
  7741. IF tok = TkEnd THEN
  7742. DropB (MatchKey (tok)) ;
  7743. EXIT
  7744. END ;
  7745. IF CurCh () = 0C THEN
  7746. Err (EUnknown) ;
  7747. EXIT
  7748. END ;
  7749. IF tok = TkCase THEN
  7750. (* Variant part: fields in all arms overlay at the same base.
  7751. The tag, when named, is an ordinary field before the arms. *)
  7752. DropB (MatchKey (tok)) ;
  7753. Skip () ;
  7754. tagName [0] := CHR (0) ;
  7755. savePosVar := srcPos ;
  7756. IF Alpha (CurCh ()) THEN
  7757. GetWord () ;
  7758. Skip () ;
  7759. IF CurCh () = ':' THEN
  7760. CopyStr (tagName, wrd) ;
  7761. DropCh (GetCh ()) ;
  7762. ParseType (subcls, s2, e2) ;
  7763. IF OK () THEN
  7764. IF fldTop < MaxFields THEN
  7765. CopyStr (fields [fldTop].name, tagName) ;
  7766. fields [fldTop].cls := subcls ;
  7767. fields [fldTop].size := s2 ;
  7768. fields [fldTop].elem := e2 ;
  7769. fields [fldTop].off := running ;
  7770. fields [fldTop].fldFirst := recFldFirst ;
  7771. fields [fldTop].fldLast := recFldLast ;
  7772. fields [fldTop].arrIdx := newArrIdx ;
  7773. fields [fldTop].ptrElem := 0 ;
  7774. INC (fldTop) ;
  7775. running := running + s2
  7776. ELSE
  7777. Err (ECompOvf)
  7778. END
  7779. ELSE
  7780. RETURN
  7781. END
  7782. ELSE
  7783. srcPos := savePosVar ;
  7784. ParseType (subcls, s2, e2) ;
  7785. IF NOT OK () THEN
  7786. RETURN
  7787. END
  7788. END
  7789. ELSE
  7790. ParseType (subcls, s2, e2) ;
  7791. IF NOT OK () THEN
  7792. RETURN
  7793. END
  7794. END ;
  7795. IF NOT (MatchKey (tok) AND (tok = TkOf)) THEN
  7796. Err (ENoSemi) ;
  7797. RETURN
  7798. END ;
  7799. variantBase := running ;
  7800. armMax := 0 ;
  7801. LOOP
  7802. Skip () ;
  7803. PeekKw (tok) ;
  7804. IF tok = TkEnd THEN
  7805. EXIT
  7806. END ;
  7807. RdConst (lo, subcls, strf) ;
  7808. IF NOT MatchDelim (':') THEN
  7809. Err (ENoSemi) ;
  7810. RETURN
  7811. END ;
  7812. IF NOT MatchDelim ('(') THEN
  7813. Err (ENoSemi) ;
  7814. RETURN
  7815. END ;
  7816. armRunning := variantBase ;
  7817. moreFields := TRUE ;
  7818. WHILE moreFields AND (CurCh () # ')') DO
  7819. groupFirst := fldTop ;
  7820. LOOP
  7821. Skip () ;
  7822. IF NOT Alpha (CurCh ()) THEN
  7823. Err (EUnknown) ;
  7824. RETURN
  7825. END ;
  7826. GetWord () ;
  7827. IF fldTop < MaxFields THEN
  7828. CopyStr (fields [fldTop].name, wrd) ;
  7829. fields [fldTop].off := 0 ;
  7830. INC (fldTop)
  7831. ELSE
  7832. Err (ECompOvf)
  7833. END ;
  7834. IF NOT MatchDelim (',') THEN
  7835. EXIT
  7836. END
  7837. END ;
  7838. ExpectDelim (':', ENoSemi) ;
  7839. ParseType (subcls, s2, e2) ;
  7840. IF NOT OK () THEN
  7841. RETURN
  7842. END ;
  7843. fieldFldFirst := recFldFirst ;
  7844. fieldFldLast := recFldLast ;
  7845. fieldArrIdx := newArrIdx ;
  7846. fieldPtrElem := 0 ;
  7847. IF subcls = TPtr THEN
  7848. fieldPtrElem := e2
  7849. END ;
  7850. i := groupFirst ;
  7851. WHILE i < fldTop DO
  7852. fields [i].cls := subcls ;
  7853. fields [i].size := s2 ;
  7854. fields [i].elem := e2 ;
  7855. fields [i].off := armRunning ;
  7856. fields [i].lo := enumLo ;
  7857. fields [i].hi := enumHi ;
  7858. fields [i].fldFirst := fieldFldFirst ;
  7859. fields [i].fldLast := fieldFldLast ;
  7860. fields [i].arrIdx := fieldArrIdx ;
  7861. fields [i].ptrElem := fieldPtrElem ;
  7862. armRunning := armRunning + s2 ;
  7863. INC (i)
  7864. END ;
  7865. IF MatchDelim (';') THEN
  7866. (* another field group in this arm *)
  7867. ELSE
  7868. moreFields := FALSE
  7869. END
  7870. END ;
  7871. ExpectDelim (')', ENoSemi) ;
  7872. IF armRunning - variantBase > armMax THEN
  7873. armMax := armRunning - variantBase
  7874. END ;
  7875. IF MatchDelim (';') THEN
  7876. (* another variant arm *)
  7877. ELSE
  7878. EXIT
  7879. END
  7880. END ;
  7881. running := variantBase + armMax ;
  7882. (* the record's own END is read by the outer loop *)
  7883. ELSE
  7884. (* the group's names, then ONE `: type' *)
  7885. groupFirst := fldTop ;
  7886. LOOP
  7887. Skip () ;
  7888. IF NOT Alpha (CurCh ()) THEN
  7889. Err (EUnknown) ;
  7890. RETURN
  7891. END ;
  7892. GetWord () ;
  7893. IF fldTop < MaxFields THEN
  7894. CopyStr (fields [fldTop].name, wrd) ;
  7895. fields [fldTop].off := 0 ;
  7896. INC (fldTop)
  7897. ELSE
  7898. Err (ECompOvf)
  7899. END ;
  7900. IF NOT MatchDelim (',') THEN
  7901. EXIT
  7902. END
  7903. END ;
  7904. ExpectDelim (':', ENoSemi) ;
  7905. ParseType (subcls, s2, e2) ;
  7906. IF NOT OK () THEN
  7907. RETURN
  7908. END ;
  7909. fieldFldFirst := recFldFirst ;
  7910. fieldFldLast := recFldLast ;
  7911. fieldArrIdx := newArrIdx ;
  7912. fieldPtrElem := 0 ;
  7913. IF subcls = TPtr THEN
  7914. fieldPtrElem := e2
  7915. END ;
  7916. (* every name in the group gets the type and its own slot *)
  7917. i := groupFirst ;
  7918. WHILE i < fldTop DO
  7919. fields [i].cls := subcls ;
  7920. fields [i].size := s2 ;
  7921. fields [i].elem := e2 ;
  7922. fields [i].off := running ;
  7923. fields [i].lo := enumLo ;
  7924. fields [i].hi := enumHi ;
  7925. fields [i].fldFirst := fieldFldFirst ;
  7926. fields [i].fldLast := fieldFldLast ;
  7927. fields [i].arrIdx := fieldArrIdx ;
  7928. fields [i].ptrElem := fieldPtrElem ;
  7929. running := running + s2 ;
  7930. INC (i)
  7931. END ;
  7932. IF NOT MatchDelim (';') THEN
  7933. Err (ENoSemi) ;
  7934. RETURN
  7935. END
  7936. END ; (* the variant IF *)
  7937. END ; (* the record field LOOP *)
  7938. (* The field TYPES are parsed by ParseType, and its named-type arm
  7939. copies the TYPE's field range into recFldFirst/recFldLast - for
  7940. `Integer' that is 0/0. So recFldFirst set at the top of this branch
  7941. is overwritten by every field type. It happens to be right for the
  7942. FIRST record in a compile (fldTop is 0 then), and wrong for every
  7943. later one: t77's second record started at 2 but recorded 0, which
  7944. made a WITH field lookup scan the previous record's fields. Restore
  7945. the saved start here, after the last field type has been parsed. *)
  7946. recFldFirst := recStart ;
  7947. recFldLast := fldTop ;
  7948. cls := TRecord ;
  7949. size := running ;
  7950. elem := running
  7951. ELSIF tok = TkText THEN
  7952. (* A TEXT file variable. We do our own unbuffered INT 21h I/O, so the
  7953. file variable is just our control block (see FileVarSize) and the
  7954. element is one byte; TP3 keeps a 128-byte buffer in its own, which is
  7955. a divergence we do not need. *)
  7956. cls := TText ;
  7957. size := FileVarSize ;
  7958. elem := 1
  7959. ELSIF tok = TkFile THEN
  7960. (* `file' (untyped) or `file of T'. The element type is parsed for its
  7961. SIZE, which is what a Read/Write moves per record; the variable
  7962. itself is always the control block. CALC.INC's Exist declares
  7963. `F : file', so the OF must be optional: requiring it produced ENoSemi
  7964. at the declaration's semicolon, which was misread as the function
  7965. declaration being unsupported. TUntyp keeps the untyped file
  7966. distinguishable, because a Read/Write through it has no element size
  7967. and must stay refused. *)
  7968. PeekKw (tok) ;
  7969. IF tok = TkOf THEN
  7970. DropB (MatchKey (tok)) ;
  7971. ParseType (subcls, s2, e2) ;
  7972. IF OK () THEN
  7973. cls := TFile ;
  7974. size := FileVarSize ;
  7975. elem := s2
  7976. END
  7977. ELSE
  7978. cls := TUntyp ;
  7979. size := FileVarSize ;
  7980. elem := 1
  7981. END
  7982. ELSE
  7983. IF consumed THEN
  7984. IF Search (wrd, idx) AND (symtab [idx].tag = KConst) THEN
  7985. (* An enum/ordinal subrange written with named constants:
  7986. `Work = Mon..Fri`. Without this, `Mon' was mistaken for a
  7987. named TYPE and failed at the first dot. *)
  7988. lo := symtab [idx].lval ;
  7989. subcls := symtab [idx].cls ;
  7990. IF NOT MatchRange () THEN
  7991. Err (ESimpType) ;
  7992. RETURN
  7993. END ;
  7994. RdConst (hi, subcls, strf) ;
  7995. IF NOT OK () THEN
  7996. RETURN
  7997. END ;
  7998. cls := TEnum ;
  7999. IF (lo >= 0) AND (hi <= 255) AND (lo <= hi) THEN
  8000. size := 1
  8001. ELSE
  8002. size := 2
  8003. END ;
  8004. elem := size ;
  8005. enumLo := lo ;
  8006. enumHi := hi
  8007. ELSIF Search (wrd, idx) AND (symtab [idx].tag = KType) THEN
  8008. cls := symtab [idx].cls ;
  8009. size := symtab [idx].size ;
  8010. (* The type's ELEMENT size, not its total size. They are equal for
  8011. a record, but not for an array (`Letters' is 6 bytes of 3
  8012. Integers) or a file. A named array typed constant used to get
  8013. typElem = 6, wrote six bytes per element and wrapped the
  8014. little-endian divider to zero: SIGFPE in DefConst. *)
  8015. elem := symtab [idx].elem ;
  8016. enumLo := symtab [idx].lo ;
  8017. enumHi := symtab [idx].hi ;
  8018. recFldFirst := symtab [idx].fldFirst ;
  8019. recFldLast := symtab [idx].fldLast ;
  8020. newArrIdx := symtab [idx].arrIdx
  8021. ELSE
  8022. Err (EUnknown)
  8023. END
  8024. ELSE
  8025. (* subrange lo .. hi. TP3 sizes it by its bounds: ONE byte when both
  8026. are inside 0..255, else two (RESUME-TP3.md §2). `0..20' and
  8027. `'A'..'G'' are one byte - which is what makes CellRec 80 and not
  8028. 82, and `Sheet' 147*80. The bounds are kept for `set of T'. *)
  8029. Skip () ;
  8030. isCharRange := (ORD (CurCh ()) = AposC) OR (CurCh () = '^')
  8031. OR (CurCh () = '#') ;
  8032. RdConst (lo, subcls, strf) ;
  8033. IF NOT MatchRange () THEN
  8034. Err (ESimpType) ;
  8035. RETURN
  8036. END ;
  8037. RdConst (hi, subcls, strf) ;
  8038. IF isCharRange THEN
  8039. cls := TChar
  8040. ELSE
  8041. cls := TScalar
  8042. END ;
  8043. IF (lo >= 0) AND (hi <= 255) AND (lo <= hi) THEN
  8044. size := 1
  8045. ELSE
  8046. size := 2
  8047. END ;
  8048. elem := size ;
  8049. enumLo := lo ;
  8050. enumHi := hi
  8051. END
  8052. END
  8053. END ParseType ;
  8054. PROCEDURE DefVar () ;
  8055. (* 'name' (',' name)* ':' type [ABSOLUTE addr] ; group repeated until a
  8056. declaration keyword appears *)
  8057. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  8058. cls, size, elem : CARDINAL ;
  8059. tok : CARDINAL ;
  8060. v : LONGINT ;
  8061. off : CARDINAL ;
  8062. first, i : CARDINAL ;
  8063. segAbs : BOOLEAN ;
  8064. BEGIN
  8065. LOOP
  8066. Skip () ; (* after the VAR keyword: skip blanks *)
  8067. IF NOT Alpha (CurCh ()) THEN
  8068. Err (EUnknown) ;
  8069. RETURN
  8070. END ;
  8071. (* A group is `name (, name)* : type' - TPSRC7 rdvarlst/rdvloop collects
  8072. the whole comma-separated list of names FIRST (`CALL ccomma / JZ
  8073. rdvloop'), then reads one `: type' with ecolon/rdvartp, then vardef2
  8074. gives every name in the list that one type. The names are created as
  8075. placeholders here and filled in once the type is known, which is the
  8076. same shape as TP3 reserving a symtab slot per name and storing the
  8077. type pointer into each afterwards. *)
  8078. first := symTop ;
  8079. LOOP
  8080. Skip () ; (* GetWord does not skip blanks, and the cursor
  8081. sits on the space after the ',' *)
  8082. IF NOT Alpha (CurCh ()) THEN
  8083. Err (EUnknown) ;
  8084. RETURN
  8085. END ;
  8086. GetWord () ;
  8087. SaveWord (nm) ;
  8088. DupTest (nm) ;
  8089. DropC (NewSym (nm, KVar, TNone, 0, 0, 0, 0, lexnest # 0)) ;
  8090. IF NOT MatchDelim (',') THEN
  8091. EXIT
  8092. END
  8093. END ;
  8094. ExpectDelim (':', ENoSemi) ;
  8095. ParseType (cls, size, elem) ;
  8096. IF OK () THEN
  8097. IF lexnest = 0 THEN
  8098. IF (size > 2) AND (cls # TString) AND (cls # TRecord)
  8099. AND (cls # TArray) AND (cls # TFile) AND (cls # TText)
  8100. AND (cls # TUntyp) AND (cls # TReal) AND (cls # TSet) THEN
  8101. (* Still the catch-all for a type whose storage this compiler does
  8102. not lay out yet (an ARRAY at its point of use, a RECORD, ...).
  8103. A STRING is the exception now: its layout is length byte +
  8104. characters, and the rest of the compiler knows it - see
  8105. Assignment and IoCall. *)
  8106. Err (ENoLib) ;
  8107. RETURN
  8108. END ;
  8109. PeekKw (tok) ;
  8110. IF tok = TkAbsolute THEN
  8111. DropB (MatchKey (tok)) ;
  8112. Skip () ;
  8113. segAbs := FALSE ;
  8114. IF Alpha (CurCh ()) THEN
  8115. (* Turbo's `Cseg:$80' / `Dseg:$80'. Our globals carry
  8116. LoadBias in their direct addresses, so subtract it from
  8117. the requested absolute address. *)
  8118. GetWord () ;
  8119. ExpectDelim (':', ENoSemi) ;
  8120. segAbs := TRUE
  8121. END ;
  8122. IF (CurCh () = '$') OR (Digit (CurCh ())) THEN
  8123. RdIntConst (v) ;
  8124. IF segAbs THEN
  8125. off := W16 (v - VAL (LONGINT, LoadBias))
  8126. ELSE
  8127. off := W16 (v)
  8128. END
  8129. ELSE
  8130. Err (EUnknown) ;
  8131. off := 0
  8132. END ;
  8133. (* TP3's absolute path (vdstore) leaves the offset fixed, so
  8134. every name in the group aliases the same address. *)
  8135. i := first ;
  8136. WHILE i < symTop DO
  8137. symtab [i].cls := cls ; symtab [i].size := size ;
  8138. symtab [i].elem := elem ; symtab [i].off := off ;
  8139. symtab [i].fldFirst := recFldFirst ;
  8140. symtab [i].fldLast := recFldLast ;
  8141. symtab [i].arrIdx := newArrIdx ;
  8142. INC (i)
  8143. END
  8144. ELSE
  8145. i := first ;
  8146. WHILE i < symTop DO
  8147. symtab [i].cls := cls ; symtab [i].size := size ;
  8148. symtab [i].elem := elem ; symtab [i].off := dc ;
  8149. symtab [i].fldFirst := recFldFirst ;
  8150. symtab [i].fldLast := recFldLast ;
  8151. symtab [i].arrIdx := newArrIdx ;
  8152. dc := dc + size ;
  8153. INC (i)
  8154. END
  8155. END ;
  8156. varspc := varspc + size * (symTop - first)
  8157. ELSE
  8158. PeekKw (tok) ;
  8159. IF tok = TkAbsolute THEN
  8160. DropB (MatchKey (tok)) ;
  8161. Skip () ;
  8162. segAbs := FALSE ;
  8163. IF Alpha (CurCh ()) THEN
  8164. GetWord () ;
  8165. ExpectDelim (':', ENoSemi) ;
  8166. segAbs := TRUE
  8167. END ;
  8168. IF (CurCh () = '$') OR (Digit (CurCh ())) THEN
  8169. RdIntConst (v) ;
  8170. IF segAbs THEN
  8171. off := W16 (v - VAL (LONGINT, LoadBias))
  8172. ELSE
  8173. off := W16 (v)
  8174. END
  8175. ELSE
  8176. Err (EUnknown) ;
  8177. off := 0
  8178. END ;
  8179. i := first ;
  8180. WHILE i < symTop DO
  8181. symtab [i].cls := cls ; symtab [i].size := size ;
  8182. symtab [i].elem := elem ; symtab [i].off := off ;
  8183. symtab [i].fldFirst := recFldFirst ;
  8184. symtab [i].fldLast := recFldLast ;
  8185. symtab [i].arrIdx := newArrIdx ;
  8186. symtab [i].local := FALSE ;
  8187. INC (i)
  8188. END
  8189. ELSE
  8190. IF (size > 2) AND (cls # TString) AND (cls # TRecord)
  8191. AND (cls # TArray) AND (cls # TFile) AND (cls # TText)
  8192. AND (cls # TUntyp) AND (cls # TReal) AND (cls # TSet) THEN
  8193. Err (ENoLib) ;
  8194. RETURN
  8195. END ;
  8196. (* A local occupies [off, off+size-1] and lives BELOW the saved BP,
  8197. because BP-relative locals grow down from the top of the frame
  8198. while a STRING grows UP from its base (length byte, then chars).
  8199. Top-anchored packing keeps both true at once: locFree is the
  8200. highest still-free byte (-1 at the top), the new symbol's base
  8201. is placed so that its LAST byte lands on that byte, and locFree
  8202. then drops by the whole size. So the first INTEGER (size 2) is
  8203. at -2 and the second at -4, byte for byte what the old
  8204. pre-decrement rule produced, while a string[5] local (size 6) is
  8205. at -6..-1 instead of the old -2..+3 - which straddled the saved
  8206. BP at [BP] and the return address at [BP+2] and made `readln(s)'
  8207. into a local store characters over its own return address.
  8208. Each name in the group gets its own slot. *)
  8209. i := first ;
  8210. WHILE i < symTop DO
  8211. symtab [i].cls := cls ; symtab [i].size := size ;
  8212. symtab [i].elem := elem ;
  8213. symtab [i].fldFirst := recFldFirst ;
  8214. symtab [i].fldLast := recFldLast ;
  8215. symtab [i].arrIdx := newArrIdx ;
  8216. symtab [i].off := (locFree - size + 1) MOD 10000H ;
  8217. locFree := (locFree - size) MOD 10000H ;
  8218. INC (i)
  8219. END ;
  8220. locBytes := locBytes + size * (symTop - first)
  8221. END
  8222. END
  8223. END ;
  8224. IF MatchDelim (',') THEN
  8225. (* The older `name : type, name : type' spelling: another group
  8226. follows without a semicolon. TP3's grammar is names-then-one-type
  8227. with ',' inside a group and ';' between groups (TPSRC7 rdvarlst /
  8228. rdvloop then ecolon/rdvartp), and both spellings reach the same
  8229. symbols, so both separators are accepted here. *)
  8230. ELSIF MatchDelim (';') THEN
  8231. PeekKw (tok) ;
  8232. IF tok # TkNone THEN
  8233. RETURN
  8234. END
  8235. ELSE
  8236. Err (ENoSemi) ;
  8237. RETURN
  8238. END
  8239. END
  8240. END DefVar ;
  8241. PROCEDURE DefConst () ;
  8242. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  8243. v : LONGINT ;
  8244. cls, typCls, typSize, typElem, soff, ci : CARDINAL ;
  8245. isStr : BOOLEAN ;
  8246. tok : CARDINAL ;
  8247. typed, setTyped, arrTyped : BOOLEAN ;
  8248. nb, k, divi, w, slen, eltCls : CARDINAL ;
  8249. BEGIN
  8250. LOOP
  8251. PeekKw (tok) ;
  8252. IF tok # TkNone THEN
  8253. RETURN
  8254. END ;
  8255. Skip () ; (* after the CONST keyword: skip blanks *)
  8256. IF NOT Alpha (CurCh ()) THEN
  8257. Err (EUnknown) ;
  8258. RETURN
  8259. END ;
  8260. GetWord () ;
  8261. SaveWord (nm) ;
  8262. DupTest (nm) ;
  8263. (* Two spellings: a plain constant `name = value', and a TYPED constant
  8264. `name : Type = value', which TP3 3.0 added (TPSRC7's constant reader
  8265. takes the type first and the value second). For a SCALAR type the
  8266. value still folds to an immediate, so the declared type is simply
  8267. recorded as the symbol's class - `FXMax : Char = 'G'' is a TChar
  8268. constant holding 71, and `writeln (FXMax)' prints G rather than 71.
  8269. A SET typed constant (`Numbers : set of Char = ['0'..'9']') is a value
  8270. with a layout, so it cannot fold: its bytes are emitted code-resident
  8271. and the name becomes an addressable value. See the storage decision
  8272. in CALC-COMPILE.md. An ARRAY or RECORD typed constant is still
  8273. refused loudly - those layouts come later. *)
  8274. typed := FALSE ;
  8275. setTyped := FALSE ;
  8276. arrTyped := FALSE ;
  8277. Skip () ;
  8278. IF CurCh () = ':' THEN
  8279. DropCh (GetCh ()) ;
  8280. ParseType (typCls, typSize, typElem) ;
  8281. IF OK () THEN
  8282. IF typCls = TSet THEN
  8283. setTyped := TRUE
  8284. ELSIF typCls = TArray THEN
  8285. arrTyped := TRUE
  8286. ELSIF typSize > 2 THEN
  8287. Err (ENoLib)
  8288. ELSE
  8289. typed := TRUE
  8290. END
  8291. END
  8292. END ;
  8293. IF OK () AND setTyped THEN
  8294. ExpectDelim ('=', ENoSemi) ;
  8295. IF RdSetLiteral (typElem, typSize, constBlob) THEN
  8296. soff := EmSetBlob (constBlob, typSize) ;
  8297. DropC (NewSym (nm, KVar, TSet, typSize, typElem, soff, 0, FALSE))
  8298. END
  8299. ELSIF OK () AND arrTyped THEN
  8300. (* array typed constant: `(v1, v2, ...)' in element order. Integers
  8301. are written little-endian in typElem bytes; a STRING element is
  8302. <length><chars>. CALC uses both XPOS (Integers) and
  8303. StandardfunctionNames (string[6]). *)
  8304. ExpectDelim ('=', ENoSemi) ;
  8305. ExpectDelim ('(', ENoSemi) ;
  8306. eltCls := 0 ;
  8307. IF newArrIdx > 0 THEN
  8308. eltCls := arrs [newArrIdx - 1].ecls
  8309. END ;
  8310. nb := 0 ;
  8311. LOOP
  8312. RdConst (v, cls, isStr) ;
  8313. IF nb + typElem > 512 THEN
  8314. Err (ECompOvf) ;
  8315. EXIT
  8316. END ;
  8317. IF isStr OR ((eltCls = TString) AND (v >= 0) AND (v <= 255)) THEN
  8318. (* A string element is <length><chars>, padded with NULs to
  8319. typElem bytes. StandardfunctionNames is array of string[6].
  8320. A single-character literal ('I') is a TScalar, not a
  8321. string, so an array of STRING must still store it as a
  8322. one-character element. Treating it as an integer divided
  8323. by 256 once per byte and hit DIV by zero at the fourth. *)
  8324. IF isStr THEN
  8325. slen := strLen [rdStrX] ;
  8326. IF slen > typElem - 1 THEN
  8327. slen := typElem - 1
  8328. END
  8329. ELSE
  8330. slen := 1
  8331. END ;
  8332. constBlob [nb] := CHR (slen) ;
  8333. k := 0 ;
  8334. WHILE k < typElem - 1 DO
  8335. IF k < slen THEN
  8336. IF isStr THEN
  8337. constBlob [nb + 1 + k] :=
  8338. strPool [strOff [rdStrX] + k]
  8339. ELSE
  8340. constBlob [nb + 1 + k] := CHR (v)
  8341. END
  8342. ELSE
  8343. constBlob [nb + 1 + k] := CHR (0)
  8344. END ;
  8345. INC (k)
  8346. END ;
  8347. nb := nb + typElem
  8348. ELSIF typElem > 2 THEN
  8349. (* An array of records/reals has no scalar encoding here.
  8350. Refuse loudly instead of shifting a 16-bit value through
  8351. divi until it wraps to zero and aborts the compiler. *)
  8352. Err (ENoLib) ;
  8353. EXIT
  8354. ELSE
  8355. w := VAL (CARDINAL, W16 (v)) ;
  8356. divi := 1 ;
  8357. k := 0 ;
  8358. WHILE k < typElem DO
  8359. constBlob [nb] := CHR ((w DIV divi) MOD 256) ;
  8360. divi := divi * 256 ;
  8361. INC (nb) ;
  8362. INC (k)
  8363. END
  8364. END ;
  8365. IF MatchDelim (',') THEN
  8366. (* another element *)
  8367. ELSE
  8368. EXIT
  8369. END
  8370. END ;
  8371. ExpectDelim (')', ENoSemi) ;
  8372. IF OK () THEN
  8373. soff := EmSetBlob (constBlob, typSize) ;
  8374. ci := NewSym (nm, KVar, TArray, typSize, typElem, soff, 0, FALSE) ;
  8375. symtab [ci].arrIdx := newArrIdx
  8376. END
  8377. ELSIF OK () THEN
  8378. ExpectDelim ('=', ENoSemi) ;
  8379. RdConst (v, cls, isStr) ;
  8380. IF isStr THEN
  8381. Err (ENoLib)
  8382. END ;
  8383. IF OK () THEN
  8384. IF typed THEN
  8385. cls := typCls
  8386. END ;
  8387. DropC (NewSym (nm, KConst, cls, 0, 0, 0, v, FALSE))
  8388. END
  8389. END ;
  8390. IF NOT MatchDelim (';') THEN
  8391. Err (ENoSemi) ;
  8392. RETURN
  8393. END
  8394. END
  8395. END DefConst ;
  8396. PROCEDURE DefLabelPart () ;
  8397. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  8398. n : CARDINAL ;
  8399. BEGIN
  8400. LOOP
  8401. Skip () ;
  8402. IF Alpha (CurCh ()) THEN
  8403. GetWord () ;
  8404. SaveWord (nm)
  8405. ELSIF Digit (CurCh ()) THEN
  8406. n := 0 ;
  8407. WHILE Digit (CurCh ()) DO
  8408. n := n * 10 + (ORD (CurCh ()) - ORD ('0')) ;
  8409. DropCh (GetCh ())
  8410. END ;
  8411. NumToName (n, nm)
  8412. ELSE
  8413. Err (EUnknown) ;
  8414. RETURN
  8415. END ;
  8416. IF NOT Search (nm, n) THEN
  8417. DropC (NewSym (nm, KLabel, TScalar, 0, 0, 0, 0, FALSE))
  8418. END ;
  8419. IF NOT MatchDelim (',') THEN
  8420. IF MatchDelim (';') THEN
  8421. RETURN
  8422. END ;
  8423. Err (ENoSemi) ;
  8424. RETURN
  8425. END
  8426. END
  8427. END DefLabelPart ;
  8428. PROCEDURE IfMatchSemi () ;
  8429. BEGIN
  8430. IF NOT MatchDelim (';') THEN
  8431. Err (ENoSemi)
  8432. END
  8433. END IfMatchSemi ;
  8434. PROCEDURE SymEpi () ;
  8435. (* function result: AX := result var for a scalar, or copy six bytes to the
  8436. caller's result scratch for a REAL function. *)
  8437. VAR k : CARDINAL ;
  8438. BEGIN
  8439. IF curIsFunc THEN
  8440. IF OK () THEN
  8441. IF resultIsReal THEN
  8442. EmLoadVar (TRUE, resultPtrOff, 2) ;
  8443. EmMovDiAx () ;
  8444. k := 0 ;
  8445. WHILE k < 6 DO
  8446. EmLoadVar (TRUE, symtab [resultVar].off + k, 1) ;
  8447. EmStDiAlOff (k) ;
  8448. INC (k)
  8449. END
  8450. ELSE
  8451. LoadSymVar (resultVar, symtab [resultVar].off, symtab [resultVar].size)
  8452. END
  8453. END
  8454. END
  8455. END SymEpi ;
  8456. PROCEDURE SlotSize (cls, size : CARDINAL) : CARDINAL ;
  8457. (* How many bytes a value parameter occupies in the callee's stack frame.
  8458. TP3's `tstscal' (TPSRC7 1243): a scalar-like parameter is AT LEAST two
  8459. bytes on the stack even when its type is one byte; anything larger spends
  8460. its own size. Generalised here so step 2 can put a string's 71 bytes on
  8461. the stack without changing what a scalar does today. *)
  8462. BEGIN
  8463. IF (cls = TScalar) OR (cls = TChar) OR (cls = TBool) OR (cls = TEnum) THEN
  8464. RETURN 2
  8465. END ;
  8466. RETURN size
  8467. END SlotSize ;
  8468. PROCEDURE ParamSlotBytes (e : SymEntry) : CARDINAL ;
  8469. (* The bytes a parameter occupies in the frame. A VAR parameter's slot is the
  8470. ADDRESS, always two bytes, whatever its type: `size' stays the type's width
  8471. so loads and stores know whether they read one byte or two. *)
  8472. BEGIN
  8473. IF e.isVar THEN
  8474. RETURN 2
  8475. END ;
  8476. RETURN SlotSize (e.cls, e.size)
  8477. END ParamSlotBytes ;
  8478. PROCEDURE ProcFunc () ;
  8479. (* PROCEDURE name (params) ; body | FUNCTION name (params) : type ; body *)
  8480. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  8481. idx, old : CARDINAL ;
  8482. tok, tok2 : CARDINAL ;
  8483. parmNm : ARRAY [0..MaxName] OF CHAR ;
  8484. cls, size, elem : CARDINAL ;
  8485. isFunc, groupVar : BOOLEAN ;
  8486. saveNest, saveLoc, saveRes, saveF : CARDINAL ;
  8487. saveLB, savePO, saveRP : CARDINAL ;
  8488. saveProc, saveStatic, staticOff : CARDINAL ;
  8489. saveR : BOOLEAN ;
  8490. realResult : BOOLEAN ;
  8491. reusedFwd : BOOLEAN ;
  8492. nestMark : CARDINAL ; (* symTop just inside this procedure *)
  8493. i, first : CARDINAL ;
  8494. BEGIN
  8495. isFunc := curIsFunc ;
  8496. Skip () ; (* after the PROCEDURE/FUNCTION keyword *)
  8497. IF NOT Alpha (CurCh ()) THEN
  8498. Err (EUnknown) ;
  8499. RETURN
  8500. END ;
  8501. GetWord () ;
  8502. SaveWord (nm) ;
  8503. IF Search (nm, idx) AND ((symtab [idx].tag = KProc)
  8504. OR (symtab [idx].tag = KFunc)) AND (symtab [idx].fwd) THEN
  8505. old := idx
  8506. ELSIF Search (nm, idx) THEN
  8507. Err (EUnknown) ;
  8508. RETURN
  8509. ELSE
  8510. old := NewSym (nm, KProc, TNone, 0, 0, 0, 0, FALSE)
  8511. END ;
  8512. IF isFunc THEN
  8513. symtab [old].tag := KFunc
  8514. END ;
  8515. saveProc := curProc ;
  8516. saveStatic := curStaticOff ;
  8517. symtab [old].parent := curProc ;
  8518. curProc := old ;
  8519. saveNest := lexnest ;
  8520. saveLoc := locFree ;
  8521. saveRes := resultVar ;
  8522. saveF := VAL (CARDINAL, ORD (curIsFunc)) ;
  8523. saveLB := locBytes ;
  8524. savePO := parmOff ;
  8525. saveRP := resultPtrOff ;
  8526. saveR := resultIsReal ;
  8527. INC (lexnest) ;
  8528. nestMark := symTop ; (* after the proc's own name, before its params *)
  8529. locFree := 0FFFFH ;
  8530. locBytes := 0 ;
  8531. parmOff := 4 ;
  8532. IF MatchDelim ('(') THEN
  8533. IF CurCh () # ')' THEN
  8534. LOOP
  8535. (* PeekKw, not MatchKey: MatchKey CONSUMES the word it reads, so
  8536. using it to test for VAR would eat the parameter's name. *)
  8537. PeekKw (tok) ;
  8538. groupVar := FALSE ;
  8539. IF tok = TkVar THEN
  8540. (* A by-reference parameter: the slot will hold an ADDRESS.
  8541. `size' still carries the type's own width, because the
  8542. callee's loads and stores must know whether they move one
  8543. byte or two. *)
  8544. DropB (MatchKey (tok)) ;
  8545. groupVar := TRUE
  8546. END ;
  8547. first := symTop ;
  8548. LOOP
  8549. Skip () ; (* blanks before the parameter name *)
  8550. IF NOT Alpha (CurCh ()) THEN
  8551. Err (EUnknown) ;
  8552. RETURN
  8553. END ;
  8554. GetWord () ;
  8555. SaveWord (parmNm) ;
  8556. DupTest (parmNm) ;
  8557. DropC (NewSym (parmNm, KVar, TNone, 0, 0, 0, 0, TRUE)) ;
  8558. IF NOT MatchDelim (',') THEN
  8559. EXIT
  8560. END
  8561. END ;
  8562. ExpectDelim (':', ENoSemi) ; (* formal is 'name : type' *)
  8563. ParseType (cls, size, elem) ;
  8564. IF groupVar AND (size > 2) AND (cls # TString) AND (cls # TReal) THEN
  8565. (* A VAR parameter of a record or array would need the callee
  8566. to address through a larger object than the scalar [BX]
  8567. paths; refusing is the honest answer. VAR STRING and VAR
  8568. REAL are the exceptions: both slots are two-byte ADDRESSES,
  8569. and both value types already have address-based runtime
  8570. paths. *)
  8571. Err (ENoLib) ;
  8572. RETURN
  8573. END ;
  8574. IF (size > 2) AND (cls # TString) AND (cls # TReal) THEN
  8575. (* A string VALUE parameter is a whole-slot copy (n+1 bytes on
  8576. the stack); any other type wider than a word - a record, an
  8577. array, a set - is still refused. A VAR REAL is allowed:
  8578. its slot is an address, not the six bytes. *)
  8579. Err (ENoLib) ;
  8580. RETURN
  8581. END ;
  8582. i := first ;
  8583. WHILE i < symTop DO
  8584. symtab [i].cls := cls ; symtab [i].size := size ;
  8585. symtab [i].elem := elem ;
  8586. symtab [i].off := parmOff ;
  8587. symtab [i].isVar := groupVar ;
  8588. IF groupVar THEN
  8589. parmOff := parmOff + 2 (* the address, whatever the type *)
  8590. ELSE
  8591. parmOff := parmOff + SlotSize (cls, size)
  8592. END ;
  8593. INC (i)
  8594. END ;
  8595. IF MatchDelim (')') THEN
  8596. EXIT
  8597. END ;
  8598. IF MatchDelim (';') THEN
  8599. (* next group *)
  8600. ELSIF MatchDelim (',') THEN
  8601. (* the older `name : type, name : type' spelling *)
  8602. ELSE
  8603. Err (ENoSemi) ;
  8604. EXIT
  8605. END
  8606. END
  8607. ELSE
  8608. DropCh (GetCh ())
  8609. END
  8610. END ;
  8611. (* TP3 lays a parameter list out BACKWARDS from BP+4: the LAST declared
  8612. parameter sits at BP+4 and each earlier one is 2 bytes farther from BP
  8613. (RESUME-TP3.md 3.11 - "le dernier paramètre déclaré est le plus proche
  8614. (BP+4)" - and the Code Generation Internals stack frame, "BP+4: last
  8615. parameter"). This is the other half of pushing the arguments as they
  8616. are parsed: the first argument is pushed first and so ends up farthest
  8617. from BP, which has to be the FIRST declared parameter's slot.
  8618. During the loop the offsets were provisional, in declaration order
  8619. (parmOff starts at 4 and grows by each parameter's SLOT size), so
  8620. parameter k sat at 4 + sum of the slots before it. For slot sizes s and
  8621. frame total T, the parameter's final base is
  8622. 4 + (T - prefix - s) = 8 + T - off - s, which sends the FIRST declared
  8623. parameter to the far end and the LAST to BP+4. With every slot 2 -
  8624. every scalar, which is all this compiler accepts today - that reduces
  8625. exactly to the old `parmOff + 2 - off`, so no existing frame moves.
  8626. A slot larger than 2 (a string's 71 bytes) falls out of the same
  8627. formula, and a VAR parameter is exactly 2 even when its TYPE is one
  8628. byte: ParamSlotBytes carries that rule.
  8629. The range is exactly the parameters: the only symbol created while the
  8630. list is read is the parameter itself (NewSym inside the loop),
  8631. ParseType creates no symbols, the procedure's own name predates
  8632. nestMark, and a function's result variable is created after this point. *)
  8633. i := nestMark ;
  8634. WHILE i < symTop DO
  8635. symtab [i].off := 8 + (parmOff - 4) - symtab [i].off
  8636. - ParamSlotBytes (symtab [i]) ;
  8637. INC (i)
  8638. END ;
  8639. (* Record the parameter list on the procedure symbol: a call site has to
  8640. know each argument's SLOT size to push and clean up the right number of
  8641. bytes. Only when this definition actually read a list - a FORWARD's
  8642. list is kept, and the later `function Expression;' that repeats only the
  8643. name must not wipe it. *)
  8644. IF symTop > nestMark THEN
  8645. symtab [old].parmFirst := nestMark ;
  8646. symtab [old].parmCount := symTop - nestMark
  8647. END ;
  8648. (* TP3 lets the definition of a forward repeat only the name. CALC writes
  8649. `function Expression{(...):Real;forward};` -- the parameter list is in a
  8650. comment, but the body still uses ExpressionCh. The forward's formal
  8651. symbols are still in the table (HideLocals only changed their level), so
  8652. make exactly that range visible for this body again. *)
  8653. reusedFwd := FALSE ;
  8654. IF (symTop = nestMark) AND symtab [old].fwd
  8655. AND (symtab [old].parmCount > 0) THEN
  8656. i := symtab [old].parmFirst ;
  8657. WHILE i < symtab [old].parmFirst + symtab [old].parmCount DO
  8658. symtab [i].level := lexnest ;
  8659. INC (i)
  8660. END ;
  8661. reusedFwd := TRUE
  8662. END ;
  8663. realResult := FALSE ;
  8664. IF isFunc THEN
  8665. IF MatchDelim (':') THEN
  8666. ParseType (cls, size, elem)
  8667. ELSIF symtab [old].fwd AND (symtab [old].cls # TNone) THEN
  8668. (* The definition of a forward repeats only the name; inherit the
  8669. result type declared by the forward. *)
  8670. cls := symtab [old].cls ;
  8671. size := symtab [old].size ;
  8672. elem := symtab [old].elem
  8673. ELSE
  8674. cls := TScalar ;
  8675. size := 2 ;
  8676. elem := 2
  8677. END ;
  8678. IF (size > 2) AND (cls # TReal) THEN
  8679. Err (ENoLib) ;
  8680. RETURN
  8681. END ;
  8682. symtab [old].cls := cls ;
  8683. symtab [old].size := size ;
  8684. resultVar := NewSym (nm, KVar, cls, size, elem,
  8685. (locFree - size + 1) MOD 10000H, 0, TRUE) ;
  8686. symtab [old].resvar := resultVar ;
  8687. symtab [resultVar].resvar := old ; (* reverse link for self-calls *)
  8688. locFree := (locFree - size) MOD 10000H ;
  8689. locBytes := locBytes + size ;
  8690. resultIsReal := (cls = TReal) ;
  8691. IF resultIsReal THEN
  8692. (* Hidden two-byte local holding the caller's result destination.
  8693. The caller passes it in DI before the CALL. *)
  8694. resultPtrOff := (locFree - 1) MOD 10000H ;
  8695. locFree := (locFree - 2) MOD 10000H ;
  8696. locBytes := locBytes + 2
  8697. ELSE
  8698. resultPtrOff := 0
  8699. END
  8700. END ;
  8701. IfMatchSemi () ;
  8702. PeekKw (tok2) ;
  8703. IF (tok2 = TkForward) OR (tok2 = TkExternal) THEN
  8704. DropB (MatchKey (tok2)) ;
  8705. symtab [old].fwd := TRUE ;
  8706. symtab [old].defnd := (tok2 = TkExternal) ;
  8707. IfMatchSemi () ;
  8708. HideLocals (nestMark) ; (* a FORWARD's parameters are not the
  8709. caller's to see either *)
  8710. lexnest := saveNest ;
  8711. locFree := saveLoc ;
  8712. resultVar := saveRes ;
  8713. resultIsReal := saveR ;
  8714. resultPtrOff := saveRP ;
  8715. curIsFunc := (saveF # 0) ;
  8716. locBytes := saveLB ;
  8717. parmOff := savePO ;
  8718. curProc := saveProc ;
  8719. curStaticOff := saveStatic ;
  8720. RETURN
  8721. END ;
  8722. (* A procedure declared inside another one receives its lexical parent's
  8723. frame in DX. Reserve a hidden local for it before ordinary locals, so
  8724. nested code can walk the static chain. Top-level procedures have no
  8725. parent frame and no slot. *)
  8726. IF symtab [old].level > 0 THEN
  8727. staticOff := (locFree - 1) MOD 10000H ;
  8728. locFree := (locFree - 2) MOD 10000H ;
  8729. locBytes := locBytes + 2 ;
  8730. symtab [old].staticOff := staticOff ;
  8731. curStaticOff := staticOff
  8732. ELSE
  8733. staticOff := 0 ;
  8734. curStaticOff := 0
  8735. END ;
  8736. (* Body. Nested procedures are emitted BEFORE the enclosing procedure's
  8737. prologue: otherwise execution entering the outer procedure falls straight
  8738. through the nested bodies. The outer entry point is therefore set only
  8739. after DefPart has emitted them. *)
  8740. DefPart () ; (* nested declarations; stops at BEGIN *)
  8741. symtab [old].goPos := pc ;
  8742. symtab [old].defnd := TRUE ;
  8743. EmPushBp () ;
  8744. EmMovBpSp () ;
  8745. IF symtab [old].level > 0 THEN
  8746. (* The caller passed the parent frame in DX. *)
  8747. EmMovAxDx () ;
  8748. EmStoreVar (TRUE, staticOff, 2)
  8749. END ;
  8750. IF resultIsReal THEN
  8751. (* The caller put the six-byte result destination in DI. Save it
  8752. before declarations/body can reuse DI. *)
  8753. EmMovAxDi () ;
  8754. EmStoreVar (TRUE, resultPtrOff, 2)
  8755. END ;
  8756. IF locBytes > 0 THEN
  8757. EmSubSp (locBytes)
  8758. END ;
  8759. DropC (EmCall (TU_StackChk)) ;
  8760. Statmnt () ; (* body *)
  8761. IF OK () THEN
  8762. SymEpi () ;
  8763. EmLeave () ;
  8764. EmRet ()
  8765. END ;
  8766. (* patch pending forward calls to this proc *)
  8767. i := 0 ;
  8768. WHILE i < nPend DO
  8769. IF (pend [i].kind = 1) AND (pend [i].who = old) THEN
  8770. SetPatTgt (pend [i].place, symtab [old].goPos) ;
  8771. pend [i].kind := 99
  8772. END ;
  8773. INC (i)
  8774. END ;
  8775. HideLocals (nestMark) ; (* parameters and locals stop here *)
  8776. IF reusedFwd THEN
  8777. (* Hide only the forward's parameter range. HideLocals starts at
  8778. parmFirst and would also hide every procedure declared between the
  8779. forward and its definition; t120's `B' disappeared when A was
  8780. defined that way. *)
  8781. i := symtab [old].parmFirst ;
  8782. WHILE i < symtab [old].parmFirst + symtab [old].parmCount DO
  8783. symtab [i].level := 0FFFFH ;
  8784. INC (i)
  8785. END
  8786. END ;
  8787. lexnest := saveNest ;
  8788. locFree := saveLoc ;
  8789. resultVar := saveRes ;
  8790. resultIsReal := saveR ;
  8791. resultPtrOff := saveRP ;
  8792. curIsFunc := (saveF # 0) ;
  8793. locBytes := saveLB ;
  8794. parmOff := savePO ;
  8795. curProc := saveProc ;
  8796. curStaticOff := saveStatic
  8797. END ProcFunc ;
  8798. PROCEDURE DefType () ;
  8799. (* 'name' '=' typeDef ; ... until a declaration keyword appears *)
  8800. VAR nm : ARRAY [0..MaxName] OF CHAR ;
  8801. cls, size, elem, tidx : CARDINAL ;
  8802. tok : CARDINAL ;
  8803. BEGIN
  8804. LOOP
  8805. PeekKw (tok) ;
  8806. IF tok # TkNone THEN
  8807. RETURN
  8808. END ;
  8809. Skip () ; (* after the TYPE keyword: skip blanks *)
  8810. IF NOT Alpha (CurCh ()) THEN
  8811. Err (EUnknown) ;
  8812. RETURN
  8813. END ;
  8814. GetWord () ;
  8815. SaveWord (nm) ;
  8816. (* A type name may already exist as a forward placeholder created by
  8817. `Next : ^Person' before `Person = record'. Reuse that entry; only a
  8818. real duplicate is an error. *)
  8819. IF Search (nm, tidx) AND (symtab [tidx].tag = KType)
  8820. AND (symtab [tidx].cls = TNone) THEN
  8821. (* fill the placeholder below *)
  8822. ELSE
  8823. DupTest (nm) ;
  8824. tidx := NewSym (nm, KType, TNone, 0, 0, 0, 0, FALSE)
  8825. END ;
  8826. ExpectDelim ('=', ENoSemi) ;
  8827. ParseType (cls, size, elem) ;
  8828. IF NOT OK () THEN
  8829. RETURN
  8830. END ;
  8831. symtab [tidx].cls := cls ;
  8832. symtab [tidx].size := size ;
  8833. symtab [tidx].elem := elem ;
  8834. symtab [tidx].fwd := FALSE ;
  8835. IF cls = TEnum THEN
  8836. (* the enumeration's bounds, set by ParseType, belong to the type - so
  8837. `set of Attributes' can size its base (0..n-1, one byte) *)
  8838. symtab [tidx].lo := enumLo ;
  8839. symtab [tidx].hi := enumHi
  8840. END ;
  8841. IF cls = TRecord THEN
  8842. (* and a record's fields live in the field table; the type records
  8843. which range is its own *)
  8844. symtab [tidx].fldFirst := recFldFirst ;
  8845. symtab [tidx].fldLast := recFldLast
  8846. END ;
  8847. IF cls = TArray THEN
  8848. symtab [tidx].arrIdx := newArrIdx ;
  8849. (* ...and the ELEMENT's field table, if it is a record. A variable
  8850. declared `Sheet : Cells' gets its field metadata through the named
  8851. type; without this, `Sheet[I,J].Field' and `with Sheet[I,J]' found
  8852. fldFirst = fldLast = 0 and reported "not a record". An INLINE
  8853. `array ... of Rec' worked because ParseType's element call left
  8854. the globals set for DefVar; the named form had lost them. *)
  8855. symtab [tidx].fldFirst := recFldFirst ;
  8856. symtab [tidx].fldLast := recFldLast
  8857. END ;
  8858. DropC (tidx) ;
  8859. IF NOT MatchDelim (';') THEN
  8860. Err (ENoSemi) ;
  8861. RETURN
  8862. END
  8863. END
  8864. END DefType ;
  8865. PROCEDURE DefPart () ;
  8866. (* LABEL / CONST / TYPE / VAR / OVERLAY / PROC / FUNCTION / BEGIN *)
  8867. VAR tok : CARDINAL ;
  8868. BEGIN
  8869. LOOP
  8870. IF MatchDelim (';') THEN
  8871. (* separator between declarations *)
  8872. ELSE
  8873. PeekKw (tok) ;
  8874. IF tok = TkBegin THEN
  8875. RETURN
  8876. END ;
  8877. IF NOT MatchKey (tok) THEN
  8878. Err (EUnknown) ;
  8879. RETURN
  8880. END ;
  8881. CASE tok OF
  8882. TkLabel : DefLabelPart () ;
  8883. | TkConst : DefConst () ;
  8884. | TkType : DefType () ;
  8885. | TkVar : DefVar () ;
  8886. | TkOverlay :
  8887. LOOP
  8888. Skip () ;
  8889. IF CurCh () = ';' THEN
  8890. DropCh (GetCh ()) ;
  8891. EXIT
  8892. END ;
  8893. IF CurCh () = 0C THEN
  8894. Err (ENoSemi) ;
  8895. EXIT
  8896. END ;
  8897. DropCh (GetCh ())
  8898. END ;
  8899. | TkProcedure : curIsFunc := FALSE ; ProcFunc () ;
  8900. | TkFunction : curIsFunc := TRUE ; ProcFunc () ;
  8901. ELSE
  8902. Err (EUnknown) ;
  8903. RETURN
  8904. END ;
  8905. IF NOT OK () THEN
  8906. RETURN
  8907. END
  8908. END
  8909. END
  8910. END DefPart ;
  8911. (* ---------------------------------------------------------------- *)
  8912. (* driver (TPSRC7 compile) *)
  8913. (* ---------------------------------------------------------------- *)
  8914. PROCEDURE DefBuiltins () ;
  8915. (* The standard procedures. Without these, WRITELN is absent from the
  8916. symbol table, Statmnt's identifier branch fails its Search and every
  8917. program that prints anything dies with EUnknown (41) on the '(' after the
  8918. call name - the single remaining cause of failure in the fixture matrix.
  8919. Tagged KBuiltin (not KProc) because these are not called generically:
  8920. WRITE/WRITELN/READ/READLN need TP3's per-argument type dispatch
  8921. (IoCall), and HALT takes no argument at all. defnd is TRUE because the
  8922. entry point is known - there is no forward reference to patch. *)
  8923. VAR i : CARDINAL ;
  8924. BEGIN
  8925. i := NewSym ("WRITE" , KBuiltin, BI_Write , 0, 0, 0, 0, FALSE) ;
  8926. symtab [i].defnd := TRUE ;
  8927. symtab [i].goPos := TU_WrInt ;
  8928. i := NewSym ("WRITELN", KBuiltin, BI_WriteLn, 0, 0, 0, 0, FALSE) ;
  8929. symtab [i].defnd := TRUE ;
  8930. symtab [i].goPos := TU_WrInt ;
  8931. i := NewSym ("READ" , KBuiltin, BI_Read , 0, 0, 0, 0, FALSE) ;
  8932. symtab [i].defnd := TRUE ;
  8933. symtab [i].goPos := TU_RdInt ;
  8934. i := NewSym ("READLN" , KBuiltin, BI_ReadLn , 0, 0, 0, 0, FALSE) ;
  8935. symtab [i].defnd := TRUE ;
  8936. symtab [i].goPos := TU_RdInt ;
  8937. i := NewSym ("HALT" , KBuiltin, BI_Halt , 0, 0, 0, 0, FALSE) ;
  8938. symtab [i].defnd := TRUE ;
  8939. symtab [i].goPos := TU_Halt ;
  8940. i := NewSym ("LENGTH" , KBuiltin, BI_Length , 0, 0, 0, 0, FALSE) ;
  8941. symtab [i].defnd := TRUE ;
  8942. symtab [i].goPos := 0 ; (* inline: the length byte, no entry *)
  8943. (* The CRT identifiers. ClrScr and GotoXY emit real INT 10h code; the other
  8944. four are registered so they are recognised, and IoCall refuses them with
  8945. ENoLib - loud, not inert. *)
  8946. i := NewSym ("CLRSCR" , KBuiltin, BI_ClrScr , 0, 0, 0, 0, FALSE) ;
  8947. symtab [i].defnd := TRUE ;
  8948. i := NewSym ("GOTOXY" , KBuiltin, BI_GotoXY , 0, 0, 0, 0, FALSE) ;
  8949. symtab [i].defnd := TRUE ;
  8950. i := NewSym ("LOWVIDEO", KBuiltin, BI_LowVideo, 0, 0, 0, 0, FALSE) ;
  8951. symtab [i].defnd := TRUE ;
  8952. i := NewSym ("NORMVIDEO", KBuiltin, BI_NormVideo, 0, 0, 0, 0, FALSE) ;
  8953. symtab [i].defnd := TRUE ;
  8954. i := NewSym ("CLREOL" , KBuiltin, BI_ClrEol , 0, 0, 0, 0, FALSE) ;
  8955. symtab [i].defnd := TRUE ;
  8956. i := NewSym ("DELAY" , KBuiltin, BI_Delay , 0, 0, 0, 0, FALSE) ;
  8957. symtab [i].defnd := TRUE ;
  8958. i := NewSym ("KEYPRESSED", KBuiltin, BI_KeyPressed, 0, 0, 0, 0, FALSE) ;
  8959. symtab [i].defnd := TRUE ;
  8960. i := NewSym ("UPCASE", KBuiltin, BI_UpCase, 0, 0, 0, 0, FALSE) ;
  8961. i := NewSym ("ORD", KBuiltin, BI_Ord, 0, 0, 0, 0, FALSE) ;
  8962. symtab [i].defnd := TRUE ;
  8963. i := NewSym ("ROUND", KBuiltin, BI_Round, 0, 0, 0, 0, FALSE) ;
  8964. symtab [i].defnd := TRUE ;
  8965. i := NewSym ("TRUNC", KBuiltin, BI_Trunc, 0, 0, 0, 0, FALSE) ;
  8966. symtab [i].defnd := TRUE ;
  8967. i := NewSym ("MSDOS", KBuiltin, BI_Msdos, 0, 0, 0, 0, FALSE) ;
  8968. symtab [i].defnd := TRUE ;
  8969. i := NewSym ("INTR", KBuiltin, BI_Intr, 0, 0, 0, 0, FALSE) ;
  8970. symtab [i].defnd := TRUE ;
  8971. i := NewSym ("GETDIR", KBuiltin, BI_GetDir, 0, 0, 0, 0, FALSE) ;
  8972. symtab [i].defnd := TRUE ;
  8973. i := NewSym ("SEG", KBuiltin, BI_Seg, 0, 0, 0, 0, FALSE) ;
  8974. symtab [i].defnd := TRUE ;
  8975. i := NewSym ("OFS", KBuiltin, BI_Ofs, 0, 0, 0, 0, FALSE) ;
  8976. symtab [i].defnd := TRUE ;
  8977. i := NewSym ("SUCC", KBuiltin, BI_Succ, 0, 0, 0, 0, FALSE) ;
  8978. i := NewSym ("PRED", KBuiltin, BI_Pred, 0, 0, 0, 0, FALSE) ;
  8979. symtab [i].defnd := TRUE ;
  8980. (* File operations. These are the names CALC actually uses; Eof, not
  8981. EndOfFile, because CALC declares a variable called EndOfFile. *)
  8982. i := NewSym ("ASSIGN", KBuiltin, BI_Assign, 0, 0, 0, 0, FALSE) ;
  8983. symtab [i].defnd := TRUE ;
  8984. i := NewSym ("RESET", KBuiltin, BI_Reset, 0, 0, 0, 0, FALSE) ;
  8985. symtab [i].defnd := TRUE ;
  8986. i := NewSym ("REWRITE", KBuiltin, BI_Rewrite, 0, 0, 0, 0, FALSE) ;
  8987. symtab [i].defnd := TRUE ;
  8988. i := NewSym ("CLOSE", KBuiltin, BI_Close, 0, 0, 0, 0, FALSE) ;
  8989. symtab [i].defnd := TRUE ;
  8990. i := NewSym ("IORESULT", KBuiltin, BI_IoResult, 0, 0, 0, 0, FALSE) ;
  8991. symtab [i].defnd := TRUE ;
  8992. i := NewSym ("CHR", KBuiltin, BI_Chr, 0, 0, 0, 0, FALSE) ;
  8993. symtab [i].defnd := TRUE ;
  8994. i := NewSym ("STR", KBuiltin, BI_Str, 0, 0, 0, 0, FALSE) ;
  8995. symtab [i].defnd := TRUE ;
  8996. i := NewSym ("NEW", KBuiltin, BI_New, 0, 0, 0, 0, FALSE) ;
  8997. symtab [i].defnd := TRUE ;
  8998. i := NewSym ("DISPOSE", KBuiltin, BI_Dispose, 0, 0, 0, 0, FALSE) ;
  8999. symtab [i].defnd := TRUE ;
  9000. i := NewSym ("EOF", KBuiltin, BI_Eof, 0, 0, 0, 0, FALSE) ;
  9001. symtab [i].defnd := TRUE ;
  9002. i := NewSym ("VAL", KBuiltin, BI_Val, 0, 0, 0, 0, FALSE) ;
  9003. DropC (i) ;
  9004. i := NewSym ("COPY", KBuiltin, BI_Copy, 0, 0, 0, 0, FALSE) ;
  9005. symtab [i].defnd := TRUE ;
  9006. i := NewSym ("ABS", KBuiltin, BI_Abs, 0, 0, 0, 0, FALSE) ;
  9007. symtab [i].defnd := TRUE ;
  9008. i := NewSym ("SQRT", KBuiltin, BI_Sqrt, 0, 0, 0, 0, FALSE) ;
  9009. symtab [i].defnd := TRUE ;
  9010. i := NewSym ("SQR", KBuiltin, BI_Sqr, 0, 0, 0, 0, FALSE) ;
  9011. symtab [i].defnd := TRUE ;
  9012. i := NewSym ("LN", KBuiltin, BI_Ln, 0, 0, 0, 0, FALSE) ;
  9013. symtab [i].defnd := TRUE ;
  9014. i := NewSym ("EXP", KBuiltin, BI_Exp, 0, 0, 0, 0, FALSE) ;
  9015. symtab [i].defnd := TRUE ;
  9016. i := NewSym ("SIN", KBuiltin, BI_Sin, 0, 0, 0, 0, FALSE) ;
  9017. symtab [i].defnd := TRUE ;
  9018. i := NewSym ("COS", KBuiltin, BI_Cos, 0, 0, 0, 0, FALSE) ;
  9019. symtab [i].defnd := TRUE ;
  9020. i := NewSym ("ARCTAN", KBuiltin, BI_Arctan, 0, 0, 0, 0, FALSE) ;
  9021. symtab [i].defnd := TRUE ;
  9022. i := NewSym ("INT", KBuiltin, BI_Int, 0, 0, 0, 0, FALSE) ;
  9023. symtab [i].defnd := TRUE ;
  9024. i := NewSym ("DELETE", KBuiltin, BI_Delete, 0, 0, 0, 0, FALSE) ;
  9025. symtab [i].defnd := TRUE ;
  9026. i := NewSym ("INSERT", KBuiltin, BI_Insert, 0, 0, 0, 0, FALSE) ;
  9027. symtab [i].defnd := TRUE ;
  9028. (* KBD: the console keyboard as a TEXT file. This runtime's read entries
  9029. already read the console, so `Read(Kbd, Ch)' drops the file argument and
  9030. reads a character exactly as `Read(Ch)' does. *)
  9031. i := NewSym ("KBD", KVar, TText, FileVarSize, 1, 0, 0, FALSE) ;
  9032. symtab [i].isKbd := TRUE ;
  9033. (* LST is TP3's printer device. The corpus uses it for listings; define
  9034. the identifier as a TEXT variable so Write(Lst, ...) type-checks. *)
  9035. i := NewSym ("LST", KVar, TText, FileVarSize, 1, 0, 0, FALSE)
  9036. END DefBuiltins ;
  9037. PROCEDURE Inittur () ;
  9038. (* reset compiler state and define the standard types *)
  9039. VAR i, rt : CARDINAL ;
  9040. BEGIN
  9041. abortFac := FALSE ;
  9042. errNum := 0 ;
  9043. txerrPos := 0 ;
  9044. srcPos := 0 ;
  9045. srcLen := Length () ;
  9046. inclActive := FALSE ;
  9047. inclEnd := 0 ;
  9048. (* The image layout is
  9049. [JMP rel16][runtime][program header][program code]
  9050. The runtime is copied to the front, which is what the original does:
  9051. TPSRC7 "copyrt" runs REPZ MOVSB with SI=DI=0 and then "MOV pc,#$2D7C".
  9052. Because the runtime sits at (near) offset 0, every address the compiler
  9053. emits is already image-absolute - the data symbols' offsets, the TU_*
  9054. call targets and the rel16 displacements all need no relocation pass.
  9055. (The base shift would in fact cancel in EmCall's arithmetic, since both
  9056. sides of a CALL move together; making the offsets absolute just means
  9057. the linker has nothing to do but copy bytes.)
  9058. The JMP is new, and it is not cosmetic. A DOS .COM is entered at
  9059. CS:0100, i.e. FILE offset 0, and for a long time offset 0 held the
  9060. runtime's first bytes - so a .COM built by this compiler started by
  9061. executing initmem with AX holding whatever the loader left in it. Every
  9062. test up to that point checked bytes and never ran the thing, so it could
  9063. not see this. The jump is the program's entry and the runtime is
  9064. ordinary data to it; keeping the runtime at the front is what preserves
  9065. the no-relocation property, so the jump goes in front of the runtime
  9066. rather than the runtime being moved behind the program.
  9067. dc is put a fixed 4 KiB above the end of the program so that data cannot
  9068. collide with code in a single 64 KiB .COM segment. LIMITATION: a
  9069. program whose code exceeds 4 KiB overruns its own data area. TP3 had
  9070. overlay segments for this; we do not, and the check belongs where the
  9071. limit is documented rather than as a silent truncation. *)
  9072. RT_Build (EntSize) ;
  9073. rt := RT_Size () ;
  9074. IF rt >= MaxCode THEN
  9075. Err (EMemOvf) ; (* cannot happen: rt is 436 *)
  9076. RETURN
  9077. END ;
  9078. (* The entry jump, at image offset 0. See the layout note above: a DOS
  9079. .COM is entered at CS:0100, which is file offset 0, so whatever sits
  9080. at offset 0 is the program's first executed instruction. *)
  9081. (* The jump's three bytes are written out longhand rather than through
  9082. Eword, because Eword writes at pc and advances it, and pc is stale at
  9083. this point -- the operand landed wherever the last compile left pc. *)
  9084. cbuf [0] := 0E9H ; (* JMP rel16 *)
  9085. entRel := 1 ;
  9086. cbuf [entRel] := 0 ;
  9087. cbuf [entRel + 1] := 0 ;
  9088. i := EntSize ;
  9089. WHILE i - EntSize < rt DO
  9090. cbuf [i] := RT_Byte (i - EntSize) ;
  9091. INC (i)
  9092. END ;
  9093. pc := rt + EntSize ;
  9094. rtSz := rt + EntSize ;
  9095. dataBase := DataStart ;
  9096. dc := dataBase ;
  9097. imgReady := TRUE ;
  9098. strTop := 0 ;
  9099. strCnt := 0 ;
  9100. rdStrX := 0 ;
  9101. varspc := 0 ;
  9102. symTop := 0 ;
  9103. (* Per-compile tables. They used to survive into the next file when the
  9104. driver compiles several in one process (the compile matrix does exactly
  9105. that): array indices kept growing, and a record's field range could be
  9106. read against a stale table. One compile must not depend on the last. *)
  9107. fldTop := 0 ;
  9108. arrTop := 0 ;
  9109. withN := 0 ;
  9110. nPatch := 0 ;
  9111. nPend := 0 ;
  9112. exitCnt := 0 ;
  9113. brkN := 0 ;
  9114. caseN := 0 ;
  9115. lexnest := 0 ;
  9116. curProc := 0 ;
  9117. curStaticOff := 0 ;
  9118. curIsFunc := FALSE ;
  9119. resultVar := 0 ;
  9120. resultIsReal := FALSE ;
  9121. resultPtrOff := 0 ;
  9122. callScratch := 0 ;
  9123. locFree := 0FFFFH ;
  9124. locBytes := 0 ;
  9125. parmOff := 4 ;
  9126. dirs.rng := TRUE ;
  9127. dirs.chk := TRUE ;
  9128. InitKeys () ;
  9129. DropC (NewSym ("INTEGER", KType, TScalar, 2, 2, 0, 0, FALSE)) ;
  9130. DropC (NewSym ("BYTE" , KType, TScalar, 1, 1, 0, 0, FALSE)) ;
  9131. DropC (NewSym ("CHAR" , KType, TChar, 1, 1, 0, 0, FALSE)) ;
  9132. DropC (NewSym ("BOOLEAN", KType, TBool , 1, 1, 0, 0, FALSE)) ;
  9133. DropC (NewSym ("REAL" , KType, TReal , 6, 6, 0, 0, FALSE)) ;
  9134. DropC (NewSym ("STRING" , KType, TString, 256, 1, 0, 0, FALSE)) ;
  9135. DropC (NewSym ("TRUE" , KConst, TBool, 1, 1, 0, 1, FALSE)) ;
  9136. DropC (NewSym ("FALSE" , KConst, TBool, 1, 1, 0, 0, FALSE)) ;
  9137. (* TP3's absolute byte array `Mem[address]'. The base is chosen so the
  9138. normal LoadBias addition lands on absolute offset zero. *)
  9139. arrs [0].n := 1 ; arrs [0].elem := 1 ; arrs [0].ecls := TScalar ;
  9140. arrs [0].lo [0] := 0 ; arrs [0].hi [0] := 65535 ;
  9141. arrs [0].fldFirst := 0 ; arrs [0].fldLast := 0 ; arrs [0].elemType := 0 ;
  9142. i := NewSym ("MEM", KVar, TArray, 65535, 1,
  9143. W16 (-VAL (LONGINT, LoadBias)), 0, FALSE) ;
  9144. symtab [i].arrIdx := 1 ;
  9145. arrTop := 1 ;
  9146. DefBuiltins () ;
  9147. tmpA := NewSym ("@@T1", KVar, TScalar, 2, 2, dc, 0, FALSE) ;
  9148. dc := dc + 2 ;
  9149. tmpB := NewSym ("@@T2", KVar, TScalar, 2, 2, dc, 0, FALSE) ;
  9150. dc := dc + 2 ;
  9151. (* The text attribute LowVideo/NormVideo set (default 7, written by the
  9152. prologue). One byte, and its address is baked into the clear sequences. *)
  9153. crtAttrOff := dc ;
  9154. DropC (NewSym ("@@ATTR", KVar, TScalar, 1, 1, dc, 0, FALSE)) ;
  9155. dc := dc + 1 ;
  9156. (* Runtime entry offsets, derived from the blob rather than assumed. This
  9157. has to happen after RT_Build, since RT_Entry only knows where the code
  9158. landed once the blob is assembled.
  9159. RT_Entry returns an IMAGE-ABSOLUTE address, already biased by the base
  9160. RT_Build was given, so nothing here has to know where the runtime
  9161. landed. It used to return a blob-relative offset, and the bias was
  9162. applied here instead - three bytes' worth, for the entry jump. With
  9163. that line missing, every CALL landed three bytes short, in the middle of
  9164. a neighbouring runtime entry, and a CALL into the middle of wrtin's
  9165. `INT 21h' behaves perfectly plausibly: the program runs, prints nothing
  9166. and hangs. Only running it finds that. *)
  9167. IF (RT_Entry (13) = 0) OR (RT_Entry (11) = 0) OR (RT_Entry (3) = 0) THEN
  9168. (* RT_Entry returns 0 for an unknown selector. initmem sits at 0
  9169. legitimately, so it cannot appear in this test - but wrtinl, rdln
  9170. and wrint never can, so catching them is enough to catch a runtime
  9171. that failed to build or a selector that went stale. This test is on
  9172. 0 is not a usable address here, since RT_Entry returns an
  9173. image-absolute address and the base is EntSize. *)
  9174. Err (EMemOvf)
  9175. END ;
  9176. TU_InitMem := RT_Entry (0) ;
  9177. TU_ProgEnd := RT_Entry (1) ;
  9178. TU_StackChk := RT_Entry (2) ;
  9179. TU_WrInt := RT_Entry (3) ;
  9180. TU_WrChar := RT_Entry (4) ;
  9181. TU_WrBool := RT_Entry (5) ;
  9182. TU_WrCharW := RT_Entry (56) ;
  9183. TU_WrBoolW := RT_Entry (57) ;
  9184. TU_WrStrW := RT_Entry (58) ;
  9185. TU_MemCopy := RT_Entry (59) ;
  9186. TU_RealToInt := RT_Entry (60) ;
  9187. TU_Msdos := RT_Entry (61) ;
  9188. TU_GetDir := RT_Entry (64) ;
  9189. TU_WrReal := RT_Entry (6) ;
  9190. TU_RealWrite := RT_Entry (30) ; (* fmtreal + padding + output *)
  9191. TU_RealFmt := RT_Entry (28) ; (* fmtreal into a string variable *)
  9192. TU_RealVal := RT_Entry (29) ; (* ascreal: ASCIIZ -> REAL *)
  9193. TU_RealCmp := RT_Entry (26) ; (* recmp: AX = -1/0/+1 *)
  9194. TU_RealAdd := RT_Entry (22) ;
  9195. TU_RealFromInt := RT_Entry (21) ;
  9196. TU_RealSub := RT_Entry (23) ;
  9197. TU_RealMul := RT_Entry (24) ;
  9198. TU_RealDiv := RT_Entry (25) ;
  9199. TU_WrLn := RT_Entry (7) ;
  9200. TU_RdInt := RT_Entry (8) ;
  9201. TU_RdChar := RT_Entry (9) ;
  9202. TU_RdBool := RT_Entry (10) ;
  9203. TU_RdLn := RT_Entry (11) ;
  9204. TU_Halt := RT_Entry (12) ;
  9205. TU_WrInl := RT_Entry (13) ; (* inline string literal *)
  9206. TU_RdByte := RT_Entry (14) ; (* a BYTE read stores one byte *)
  9207. TU_SetOp := RT_Entry (31) ;
  9208. TU_SetCmp := RT_Entry (32) ;
  9209. TU_SetCopy := RT_Entry (33) ;
  9210. TU_SetSingle := RT_Entry (67) ;
  9211. TU_FileAssign := RT_Entry (34) ;
  9212. TU_FileReset := RT_Entry (35) ;
  9213. TU_FileRewrite := RT_Entry (36) ;
  9214. TU_FileClose := RT_Entry (37) ;
  9215. TU_IoResult := RT_Entry (38) ;
  9216. TU_IoStore := RT_Entry (39) ;
  9217. TU_HeapNew := RT_Entry (40) ;
  9218. TU_FileEof := RT_Entry (41) ;
  9219. TU_FileReadln := RT_Entry (42) ;
  9220. TU_FileRdtok := RT_Entry (65) ;
  9221. TU_FileRdln := RT_Entry (66) ;
  9222. TU_IntFmt := RT_Entry (43) ;
  9223. TU_IntFmtW := RT_Entry (62) ;
  9224. TU_StrFmtW := RT_Entry (63) ;
  9225. TU_ValInt := RT_Entry (44) ;
  9226. TU_StrCopy := RT_Entry (45) ;
  9227. TU_ValReal := RT_Entry (46) ;
  9228. TU_RealAbs := RT_Entry (47) ;
  9229. TU_RealSqrt := RT_Entry (48) ;
  9230. TU_RealLn := RT_Entry (49) ;
  9231. TU_RealExp := RT_Entry (50) ;
  9232. TU_RealSin := RT_Entry (51) ;
  9233. TU_RealCos := RT_Entry (52) ;
  9234. TU_RealArctan := RT_Entry (53) ;
  9235. TU_RealIntFunc := RT_Entry (27) ;
  9236. TU_StrDelete := RT_Entry (54) ;
  9237. TU_StrInsert := RT_Entry (55) ;
  9238. TU_WrStr := RT_Entry (15) ; (* a string variable is written through
  9239. the address on the stack *)
  9240. TU_RdStr := RT_Entry (16) ; (* readln(s)/read(s): TPSRC8 prdstr,
  9241. TPSRC4 xrdstr *)
  9242. TU_StrStore := RT_Entry (17) ; (* s := t: TPSRC8 asnvar, TPSRC2 strstore *)
  9243. TU_CmpStr := RT_Entry (18) ; (* string compare: TPSRC9 vcompare,
  9244. TPSRC2 cmpstr *)
  9245. TU_StrCat := RT_Entry (19) ; (* string concatenation: TPSRC2 xconcat *)
  9246. TU_WrIntW := RT_Entry (20) ; (* width-aware integer: `I:2' *)
  9247. END Inittur ;
  9248. PROCEDURE HeadWord (VAR slot : CARDINAL) ;
  9249. BEGIN
  9250. slot := pc ;
  9251. Eword (0)
  9252. END HeadWord ;
  9253. PROCEDURE Compile (VAR errNo, errPos : CARDINAL) : BOOLEAN ;
  9254. VAR tok : CARDINAL ;
  9255. overProc : CARDINAL ;
  9256. hasProc : BOOLEAN ;
  9257. BEGIN
  9258. Inittur () ;
  9259. IF OK () THEN
  9260. (* Resolve every {$I} first: the parser (and DeclaresProc's lookahead)
  9261. must see one finished buffer. See ExpandIncludes. *)
  9262. ExpandIncludes ()
  9263. END ;
  9264. IF OK () THEN
  9265. (* program prologue: header words, CALL initmem, MOV BP,SP *)
  9266. HeadWord (hdrFlag) ;
  9267. HeadWord (hdrCS) ;
  9268. HeadWord (hdrDS) ;
  9269. HeadWord (hdrHeap) ;
  9270. HeadWord (hdrMax) ;
  9271. Eword (16) ; (* max open files *)
  9272. Eword (0) ; (* input buffer word *)
  9273. Eword (0) ; (* output buffer word *)
  9274. (* Here, and not one line earlier, is the first instruction of the
  9275. program: everything above is the header, which is DATA. The entry
  9276. jump has to land exactly here. Recorded rather than assumed, so that
  9277. a header that grows a word moves the target with it. *)
  9278. prologAt := pc ;
  9279. (* TU_InitMem takes the header offset in AX, not on the stack, so the
  9280. AX load has to precede the call. Previously the prologue called
  9281. offset 8 - which in this layout is the hdrMax word - and that was
  9282. coherent only because the runtime was not there. Now it is the
  9283. real header. *)
  9284. EmMovAxi ((rtSz + LoadBias) MOD 10000H) ;
  9285. DropC (EmCall (TU_InitMem)) ;
  9286. EmMovBpSp () ;
  9287. (* [@@ATTR] := 7 - the default text attribute, before any LowVideo or
  9288. NormVideo runs. The data area is zero-filled, and attribute 0 would
  9289. make every clear black-on-black. Through EmCrtSeq, not raw Ebyte:
  9290. Compile is not a one-line emitter, and the helper audit rightly
  9291. refuses to let a byte-emitting statement hide inside it. *)
  9292. EmCrtSeq (CrtLow) ;
  9293. IF MatchKey (tok) AND (tok = TkProgram) THEN
  9294. (* MatchKey stops right after "PROGRAM", so the optional program
  9295. name normally follows blanks. Skip them before testing for the
  9296. name: otherwise Alpha sees the blank, the name is never consumed
  9297. and IfMatchSemi reports ENoSemi at the name. *)
  9298. Skip () ;
  9299. IF Alpha (CurCh ()) THEN
  9300. GetWord ()
  9301. END ;
  9302. IF MatchDelim ('(') THEN
  9303. WHILE NOT MatchDelim (')') DO
  9304. IF Alpha (CurCh ()) THEN
  9305. GetWord ()
  9306. END ;
  9307. IF CurCh () = ',' THEN
  9308. DropCh (GetCh ())
  9309. END
  9310. END
  9311. END ;
  9312. IfMatchSemi ()
  9313. END ;
  9314. IF OK () THEN
  9315. (* Jump over the procedure bodies, if there are any. DefPart
  9316. compiles them HERE, between the prologue and the main statement
  9317. part, and there was no jump - so a program with a procedure fell
  9318. off the end of the prologue into the first procedure. See
  9319. DeclaresProc for why the condition is asked before DefPart runs
  9320. and not after. *)
  9321. hasProc := DeclaresProc () ;
  9322. IF hasProc THEN
  9323. overProc := EmJmpNear (0)
  9324. END ;
  9325. DefPart () ;
  9326. IF OK () THEN
  9327. (* A separate flag, NOT `overProc # 0`. EmJmpNear returns a
  9328. patch SLOT, and slot 0 is a perfectly ordinary slot - the
  9329. first forward jump in a program is slot 0. So a zero test
  9330. cannot tell "no forward jump" from "forward jump in slot 0",
  9331. it just skips the first patch, and the jump keeps its
  9332. placeholder target of 0. The program then jumped to image
  9333. offset 0, i.e. back to the entry jump, and ran the runtime
  9334. and the whole program again, forever. The slot is only
  9335. valid together with a boolean saying a slot was taken. *)
  9336. IF hasProc THEN
  9337. SetPatTgt (overProc, pc)
  9338. END ;
  9339. IF MatchKey (tok) AND (tok = TkBegin) THEN
  9340. Compound () ;
  9341. IF OK () THEN
  9342. EmXorAxAx () ;
  9343. DropC (EmCall (TU_ProgEnd)) ;
  9344. ResolvePatches () ;
  9345. (* Program-only sizes. The runtime is not part of the
  9346. program's code, and the fixture table has always meant
  9347. "the program's own code", so subtract it here rather
  9348. than making every expectation in expected.tsv wrong. *)
  9349. codeSz := pc - rtSz ;
  9350. dataSz := dc - dataBase ;
  9351. (* Header words. The layout is ours (the original's is
  9352. bigger and serves a real overlay loader), but
  9353. Runtime.EmitInitMem reads +4 and +8, so hdrDS and
  9354. hdrHeap must be the data base and the data end. *)
  9355. (* The entry jump's displacement. A .COM is entered at
  9356. CS:0100 = file offset 0, so the jump is the only thing
  9357. that decides where execution starts, and it has to land
  9358. on the START of the program code - the prologue, which is
  9359. at rtSz - not on pc, which is the END of it. (Patching
  9360. pc - EntSize, i.e. the end, lands one byte past the last
  9361. instruction, in the zero-filled code/data gap, where the
  9362. CPU slides through `ADD [BX+SI],AL' until it faults.)
  9363. The displacement is measured from the END of the jump,
  9364. and both addresses are image-absolute, so the load
  9365. segment cancels. *)
  9366. (* The entry jump's displacement. A .COM is entered at
  9367. CS:0100 = file offset 0, so this jump is the only thing
  9368. that decides where execution starts.
  9369. The target is prologAt - where the prologue ACTUALLY
  9370. began, recorded before the header words were emitted,
  9371. and the header is 16 bytes long, so this is rtSz + 16 and
  9372. NOT rtSz. Landing on rtSz lands on the HEADER, which is
  9373. data, and the CPU then decodes sixteen bytes of it as
  9374. instructions. That failure is spectacularly
  9375. non-deterministic across programs: 01 00 is
  9376. `ADD [BX+SI],AX' and is harmless, so writeln('hi') ran
  9377. fine by sliding through the header into the prologue,
  9378. while t07's hdrHeap word 90 12 decodes as a LOCK-prefixed
  9379. ADD whose displacement crosses a page and faults, and the
  9380. program hung with no output at all. Both looked like
  9381. "the jump is in the right area". Recording the position
  9382. rather than assuming it means a future header that grows
  9383. a word cannot silently reintroduce this. *)
  9384. PatchWord (entRel, (prologAt - EntSize) MOD 10000H) ;
  9385. PatchWord (hdrFlag, 1) ;
  9386. (* Every OFFSET field in the header is a segment offset,
  9387. i.e. an image offset plus LoadBias - one convention for
  9388. the whole structure, so that nobody has to remember
  9389. which of these five words is numbered which way.
  9390. hdrFlag 1 set, so a loader can recognise the header
  9391. hdrCS end of the generated code
  9392. hdrDS first byte of the data area <- read by initmem
  9393. hdrHeap one past the last <- read by initmem
  9394. hdrMax 0 (no overlay loader yet)
  9395. hdrDS and hdrHeap are the two that are CONSUMED, and
  9396. omitting the bias there is a silent no-op: initmem would
  9397. clear a range starting 0100h below the data, off the
  9398. front of the image, and never reach the globals at the
  9399. end. Nothing crashes, and the globals keep whatever the
  9400. loader left in them. *)
  9401. PatchWord (hdrCS, pc + LoadBias) ;
  9402. PatchWord (hdrDS, dataBase + LoadBias) ;
  9403. PatchWord (hdrHeap, dc + LoadBias) ;
  9404. PatchWord (hdrMax, 0)
  9405. END
  9406. ELSE
  9407. Err (EUnknown)
  9408. END
  9409. END
  9410. END
  9411. END ;
  9412. IF NOT MatchDelim ('.') THEN
  9413. Err (EPointExp)
  9414. END ;
  9415. IF abortFac THEN
  9416. errNo := errNum ; (* was "errNo := errNo": a self-assignment,
  9417. because the formal shadowed the module
  9418. variable, so the error code always
  9419. reached the caller as 0 *)
  9420. errPos := txerrPos ;
  9421. RETURN FALSE
  9422. END ;
  9423. errNo := 0 ;
  9424. errPos := 0 ;
  9425. RETURN TRUE
  9426. END Compile ;
  9427. PROCEDURE CodeBytes () : CARDINAL ;
  9428. BEGIN
  9429. RETURN codeSz
  9430. END CodeBytes ;
  9431. PROCEDURE DataBytes () : CARDINAL ;
  9432. BEGIN
  9433. RETURN dataSz
  9434. END DataBytes ;
  9435. PROCEDURE ImageBytes () : CARDINAL ;
  9436. (* Total linked image size: rtSz (the runtime) + CodeBytes (the program).
  9437. The data is NOT part of this image: globals live at DataBase (8000h image)
  9438. and initmem zeroes them. The .COM is exactly these bytes. *)
  9439. BEGIN
  9440. RETURN rtSz + codeSz
  9441. END ImageBytes ;
  9442. PROCEDURE DataBase () : CARDINAL ;
  9443. (* image-absolute offset at which the data area begins (DataStart = 0A000H).
  9444. The data is outside the .COM; initmem zeroes [dataBase, dataBase+dataSz)
  9445. through the header's hdrDS/hdrHeap words. *)
  9446. BEGIN
  9447. RETURN dataBase
  9448. END DataBase ;
  9449. PROCEDURE ImageByteAt (i : CARDINAL) : BYTE ;
  9450. (* i-th byte of the WHOLE image, runtime included, so a test can check the
  9451. real thing a .COM would contain. Returns 0 past the end. *)
  9452. BEGIN
  9453. IF i >= rtSz + codeSz THEN
  9454. RETURN 0
  9455. END ;
  9456. RETURN cbuf [i]
  9457. END ImageByteAt ;
  9458. PROCEDURE CodeByteAt (i : CARDINAL) : BYTE ;
  9459. (* i-th byte of the emitted image, for test harnesses that need to check
  9460. the generated 8086 code rather than just its size. Returns 0 past the
  9461. end of the image. *)
  9462. BEGIN
  9463. IF i >= codeSz THEN
  9464. RETURN 0
  9465. END ;
  9466. RETURN cbuf [i]
  9467. END CodeByteAt ;
  9468. END Compiler.