FoxIntermediateBackend.Mod 521 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389
  1. MODULE FoxIntermediateBackend; (** AUTHOR ""; PURPOSE ""; *)
  2. IMPORT Basic := FoxBasic, SyntaxTree := FoxSyntaxTree, SemanticChecker := FoxSemanticChecker, Backend := FoxBackend, Global := FoxGlobal,
  3. Scanner := FoxScanner, IntermediateCode := FoxIntermediateCode, Sections := FoxSections, BinaryCode := FoxBinaryCode, Printout := FoxPrintout,
  4. SYSTEM, Diagnostics, Strings, Options, Streams, Compiler, Formats := FoxFormats, SymbolFileFormat := FoxTextualSymbolFile, D := Debugging,
  5. FingerPrinter := FoxFingerPrinter, StringPool, ActiveCells := FoxActiveCells;
  6. CONST
  7. (* operand modes *)
  8. ModeUndefined = 0;
  9. ModeReference = 1;
  10. ModeValue = 2;
  11. (* heap data offsets *)
  12. ArrayDimTable = 3; (* dimension table in dyn arrays *)
  13. (* math array offsets *)
  14. MathPtrOffset=0;
  15. MathAdrOffset=1;
  16. MathFlagsOffset=2;
  17. MathDimOffset=3;
  18. MathElementSizeOffset=4;
  19. MathLenOffset=5;
  20. MathIncrOffset=6;
  21. SysDataArrayOffset* = 0; (* array offset in system bl ock, for 32 byte alignment *)
  22. ArrDataArrayOffset*= 16*8; (* 16 bytes array offset in array block, to be compatible with the GC scheme of POINTER TO ARRAY OF ... *)
  23. TensorFlag* = 0; (* flag indicating a tensor array *)
  24. RangeFlag* = 1; (* flag indicating a range, e.g. an array derived from A[..,..] *)
  25. StackFlag* = 2; (* flag indicates temporary result *)
  26. StaticFlag* = 1; (* flag indicating a static array, may not be reallocated *)
  27. (** compiler generated traps *)
  28. WithTrap* = 1; (* generated when a WITH statement fails *)
  29. CaseTrap* = 2; (* generated when a case statement without else block fails *)
  30. ReturnTrap* = 3;
  31. TypeEqualTrap* = 5;
  32. TypeCheckTrap* = 6;
  33. IndexCheckTrap* = 7; (* generated when index is out of bounds or range is invalid *)
  34. AssertTrap* = 8; (* generated when an assert fails *)
  35. ArraySizeTrap* = 9;
  36. ArrayFormTrap*=10; (* indicates that array cannot be (re-)allocated since shape, type or size does not match *)
  37. SetElementTrap*=11; (* indicates that a set element is out of MIN(SET)...MAX(SET) *)
  38. NegativeDivisorTrap*=12;
  39. NoReturnTrap*=16; (* indicates that a procedure marked no return did return *)
  40. NilPointerTrap*=17; (* indicates that a nil pointer was being dereferenced *)
  41. Trace = FALSE;
  42. TraceRegisterUsageCount=TRUE;
  43. ArrayAlignment = 8*8; (* first array element of ArrayBlock and first data element of SystemBlock must be aligned to 0 MOD ArrayAlignment *)
  44. (** system call numbers *)
  45. NumberSystemCalls* = 12;
  46. SysNewRec* = 0; SysNewArr* = 1; SysNewSys* = 2; SysCaseTable* = 3; SysProcAddr* = 4;
  47. SysLock* = 5; SysUnlock* = 6; SysStart* = 7; SysAwait* = 8; SysInterfaceLookup* = 9;
  48. SysRegisterInterface* = 10; SysGetProcedure* = 11;
  49. DefaultRuntimeModuleName ="Runtime";
  50. DefaultTraceModuleName ="KernelLog";
  51. ChannelModuleName = "Channels";
  52. suppressModuleRegistration=FALSE;
  53. NonPointer = -1; (* special pointer values *)
  54. NoType = 0; (* special type info values *)
  55. LhsIsPointer = 0; (* for the operator kind *)
  56. RhsIsPointer = 1;
  57. (* priority values, lower means higher priority *)
  58. EntryPriority=-4;
  59. FirstPriority=-3;
  60. InitPriority=-2;
  61. ExitPriority=-1;
  62. StrictChecks = FALSE;
  63. BasePointerTypeSize = 5;
  64. BaseArrayTypeSize = BasePointerTypeSize + 3;
  65. LengthOffset = BasePointerTypeSize + 0;
  66. DataOffset = BasePointerTypeSize + 1;
  67. DescriptorOffset = BasePointerTypeSize + 2;
  68. BaseRecordTypeSize = BasePointerTypeSize + 2;
  69. ActionOffset = BasePointerTypeSize + 0;
  70. MonitorOffset = BasePointerTypeSize + 1;
  71. BaseObjectTypeSize = BaseRecordTypeSize;
  72. ActionTypeSize = 3;
  73. MonitorTypeSize = 7;
  74. ProcessorOffset = BaseObjectTypeSize + 1;
  75. StackLimitOffset* = BaseObjectTypeSize + 3;
  76. QuantumOffset = BaseObjectTypeSize + 4;
  77. TYPE
  78. SupportedInstructionProcedure* = PROCEDURE {DELEGATE} (CONST instr: IntermediateCode.Instruction; VAR moduleName,procedureName: ARRAY OF CHAR): BOOLEAN;
  79. SupportedImmediateProcedure* = PROCEDURE {DELEGATE} (CONST op: IntermediateCode.Operand): BOOLEAN;
  80. Operand = RECORD
  81. mode: SHORTINT;
  82. op: IntermediateCode.Operand;
  83. tag: IntermediateCode.Operand;
  84. extra: IntermediateCode.Operand; (* stores the step size of an array range *)
  85. dimOffset: LONGINT;
  86. END;
  87. Fixup= POINTER TO RECORD
  88. pc: LONGINT;
  89. nextFixup: Fixup;
  90. END;
  91. WriteBackCall = POINTER TO RECORD
  92. call: SyntaxTree.ProcedureCallDesignator;
  93. next: WriteBackCall;
  94. END;
  95. Label= OBJECT
  96. VAR
  97. fixups: Fixup;
  98. section: IntermediateCode.Section;
  99. pc: LONGINT;
  100. PROCEDURE &InitLabel(section: IntermediateCode.Section);
  101. BEGIN
  102. SELF.section := section; pc := -1;
  103. END InitLabel;
  104. PROCEDURE Resolve(pc: LONGINT);
  105. VAR at: LONGINT;
  106. BEGIN
  107. SELF.pc := pc;
  108. WHILE(fixups # NIL) DO
  109. at := fixups.pc;
  110. section.PatchAddress(at,pc);
  111. fixups := fixups.nextFixup;
  112. END;
  113. END Resolve;
  114. PROCEDURE AddFixup(at: LONGINT);
  115. VAR fixup: Fixup;
  116. BEGIN
  117. ASSERT(pc=-1);
  118. NEW(fixup); fixup.pc := at; fixup.nextFixup := fixups; fixups := fixup;
  119. END AddFixup;
  120. END Label;
  121. ConditionalBranch = PROCEDURE {DELEGATE}(label: Label; op1,op2: IntermediateCode.Operand);
  122. DeclarationVisitor =OBJECT(SyntaxTree.Visitor)
  123. VAR
  124. backend: IntermediateBackend;
  125. implementationVisitor: ImplementationVisitor;
  126. meta: MetaDataGenerator;
  127. system: Global.System;
  128. currentScope: SyntaxTree.Scope;
  129. module: Sections.Module;
  130. moduleSelf: SyntaxTree.Variable;
  131. dump: BOOLEAN;
  132. forceModuleBody: BOOLEAN;
  133. addressType: IntermediateCode.Type;
  134. PROCEDURE & Init(system: Global.System; implementationVisitor: ImplementationVisitor; backend: IntermediateBackend; forceModuleBody, dump: BOOLEAN);
  135. BEGIN
  136. currentScope := NIL; module := NIL; moduleSelf := NIL;
  137. SELF.system := system; SELF.implementationVisitor := implementationVisitor;
  138. SELF.dump := dump;
  139. SELF.backend := backend;
  140. SELF.forceModuleBody := forceModuleBody;
  141. addressType := IntermediateCode.GetType(system,system.addressType)
  142. END Init;
  143. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  144. BEGIN
  145. backend.Error(module.module.sourceName, position, Diagnostics.Invalid, s);
  146. END Error;
  147. PROCEDURE Type(x: SyntaxTree.Type);
  148. BEGIN
  149. x.Accept(SELF);
  150. END Type;
  151. (** types **)
  152. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  153. BEGIN (* no code emission *) END VisitBasicType;
  154. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  155. BEGIN (* no code emission *) END VisitCharacterType;
  156. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  157. BEGIN (* no code emission *) END VisitIntegerType;
  158. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  159. BEGIN (* no code emission *) END VisitFloatType;
  160. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  161. BEGIN (* no code emission *) END VisitComplexType;
  162. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  163. VAR type: SyntaxTree.Type;
  164. BEGIN (* no further traversal to x.resolved necessary since type descriptor and code will be inserted at "original" position ? *)
  165. type := x.resolved;
  166. IF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule # module.module) THEN
  167. meta.CheckTypeDeclaration(type);
  168. END;
  169. END VisitQualifiedType;
  170. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  171. BEGIN (* no code emission *) END VisitStringType;
  172. PROCEDURE VisitArrayRangeType(x: SyntaxTree.RangeType);
  173. BEGIN (* no code emission *)
  174. END VisitArrayRangeType;
  175. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  176. BEGIN (* no code emission *) END VisitArrayType;
  177. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  178. BEGIN
  179. meta.CheckTypeDeclaration(x);
  180. END VisitMathArrayType;
  181. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  182. BEGIN
  183. meta.CheckTypeDeclaration(x);
  184. (* base type must not be visited => will be done via record type declaration, otherwise is done twice ! *)
  185. END VisitPointerType;
  186. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  187. VAR name: ARRAY 256 OF CHAR; td: SyntaxTree.TypeDeclaration;
  188. BEGIN (* no code emission *)
  189. meta.CheckTypeDeclaration(x);
  190. IF (x.recordScope.ownerModule = module.module) & (x.isObject) THEN
  191. IF x.pointerType.typeDeclaration # NIL THEN
  192. td := x.pointerType.typeDeclaration
  193. ELSE
  194. td := x.typeDeclaration
  195. END;
  196. Global.GetSymbolName(td,name);
  197. (* code section for object *)
  198. END;
  199. Scope(x.recordScope);
  200. END VisitRecordType;
  201. PROCEDURE HasFlag(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR): BOOLEAN;
  202. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  203. BEGIN
  204. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  205. WHILE (this # NIL) & (this.identifier# id) DO
  206. this := this.nextModifier;
  207. END;
  208. RETURN this # NIL
  209. END HasFlag;
  210. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  211. VAR name:Basic.SegmentedName; td: SyntaxTree.TypeDeclaration; type: SyntaxTree.Type; len,port,adr: LONGINT;
  212. parameter: SyntaxTree.Parameter; symbol: IntermediateCode.Section; op: IntermediateCode.Operand; capabilities: SET;
  213. PROCEDURE CreatePortArray(type: SyntaxTree.Type; len: LONGINT);
  214. VAR i,len2: LONGINT; baseType: SyntaxTree.Type;
  215. BEGIN
  216. FOR i := 0 TO len-1 DO
  217. IF SemanticChecker.IsStaticArray(type, baseType, len2) THEN
  218. CreatePortArray(baseType, len2);
  219. ELSE
  220. IF backend.cellsAreObjects THEN
  221. adr := port
  222. ELSE
  223. (*! add check from ActiveCells2 *)
  224. adr := backend.activeCellsSpecification.GetPortAddress(port);
  225. END;
  226. IntermediateCode.InitImmediate(op,addressType,adr);
  227. symbol.Emit(Data(-1,op));
  228. INC(port);
  229. END;
  230. END;
  231. END CreatePortArray;
  232. BEGIN
  233. IF backend.cellsAreObjects THEN meta.CheckTypeDeclaration(x) END;
  234. IF (x.cellScope.ownerModule = module.module) THEN
  235. td := x.typeDeclaration;
  236. Global.GetSymbolSegmentedName(td,name);
  237. (* code section for object *)
  238. END;
  239. port := 0;
  240. parameter := x.firstParameter;
  241. WHILE parameter # NIL DO
  242. type := parameter.type.resolved;
  243. IF type IS SyntaxTree.PortType THEN
  244. len := 1;
  245. INC(port);
  246. ELSIF SemanticChecker.IsStaticArray(type,type,len) OR SemanticChecker.IsDynamicArray(type, type) THEN
  247. IF backend.cellsAreObjects THEN
  248. IF IsStaticArray(parameter.type.resolved) THEN
  249. Error(parameter.position, "static arrays of ports are currently not implemented, please use a property (array property of port)");
  250. END;
  251. (* do nothing *)
  252. ELSE
  253. Global.GetSymbolSegmentedName(parameter,name);
  254. symbol := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,parameter,dump);
  255. CreatePortArray(type, len);
  256. (*
  257. WHILE len > 0 DO
  258. adr := backend.activeCellsSpecification.GetPortAddress(port);
  259. IntermediateCode.InitImmediate(op,addressType,adr);
  260. symbol.Emit(Data(-1,op));
  261. DEC(len); INC(port);
  262. END;
  263. *)
  264. END;
  265. ELSE
  266. Error(parameter.position,"should never happen, check semantic checker!");
  267. END;
  268. parameter := parameter.nextParameter;
  269. END;
  270. capabilities := {};
  271. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN INCL(capabilities, Global.FloatingPointCapability) END;
  272. IF HasFlag(x.modifiers, Global.StringVector) THEN INCL(capabilities, Global.VectorCapability) END;
  273. backend.SetCapabilities(capabilities);
  274. Scope(x.cellScope);
  275. END VisitCellType;
  276. PROCEDURE VisitProcedureType(x: SyntaxTree.ProcedureType);
  277. BEGIN (* no code emission *) END VisitProcedureType;
  278. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  279. BEGIN (* no code emission, exported enumeration type values should be included in symbol file *)
  280. END VisitEnumerationType;
  281. (* symbols *)
  282. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  283. BEGIN
  284. Procedure(x);
  285. END VisitProcedure;
  286. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  287. BEGIN
  288. Procedure(x);
  289. END VisitOperator;
  290. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  291. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; align: LONGINT;
  292. BEGIN
  293. IF x.externalName # NIL THEN RETURN END;
  294. IF (currentScope IS SyntaxTree.ModuleScope) OR (currentScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  295. (* code section for variable *)
  296. Global.GetSymbolSegmentedName(x,name);
  297. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  298. irv.SetExported(IsExported(x));
  299. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  300. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))));
  301. IF ~x.fixed THEN
  302. align := CommonAlignment(x.alignment, ToMemoryUnits(system, system.AlignmentOf(system.variableAlignment, x.type)));
  303. ELSE
  304. align := x.alignment;
  305. END;
  306. irv.SetPositionOrAlignment(x.fixed, align);
  307. meta.CheckTypeDeclaration(x.type);
  308. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  309. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  310. END;
  311. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  312. END VisitVariable;
  313. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  314. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; op: Operand;
  315. BEGIN
  316. ASSERT(currentScope.outerScope IS SyntaxTree.CellScope);
  317. (* code section for variable *)
  318. Global.GetSymbolSegmentedName(x,name);
  319. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  320. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  321. (*
  322. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  323. *)
  324. IF x.defaultValue = NIL THEN
  325. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))))
  326. ELSE
  327. implementationVisitor.inData := TRUE;
  328. implementationVisitor.Evaluate(x.defaultValue, op);
  329. irv.Emit(Data(x.position,op.op));
  330. implementationVisitor.inData := FALSE;
  331. END;
  332. meta.CheckTypeDeclaration(x.type);
  333. END VisitParameter;
  334. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  335. BEGIN
  336. Type(x.declaredType); (* => code in objects *)
  337. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  338. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  339. END;
  340. END VisitTypeDeclaration;
  341. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  342. BEGIN
  343. IF (SyntaxTree.Public * x.access # {}) THEN
  344. implementationVisitor.VisitConstant(x);
  345. END;
  346. END VisitConstant;
  347. PROCEDURE Scope(x: SyntaxTree.Scope);
  348. VAR procedure: SyntaxTree.Procedure;
  349. constant: SyntaxTree.Constant;
  350. variable: SyntaxTree.Variable;
  351. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  352. BEGIN
  353. prevScope := currentScope;
  354. currentScope := x;
  355. (* constants treated in implementation visitor *)
  356. typeDeclaration := x.firstTypeDeclaration;
  357. WHILE typeDeclaration # NIL DO
  358. VisitTypeDeclaration(typeDeclaration);
  359. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  360. END;
  361. variable := x.firstVariable;
  362. WHILE variable # NIL DO
  363. VisitVariable(variable);
  364. variable := variable.nextVariable;
  365. END;
  366. procedure := x.firstProcedure;
  367. WHILE procedure # NIL DO
  368. VisitProcedure(procedure);
  369. procedure := procedure.nextProcedure;
  370. END;
  371. constant := x.firstConstant;
  372. WHILE constant # NIL DO
  373. VisitConstant(constant);
  374. constant := constant.nextConstant;
  375. END;
  376. currentScope := prevScope;
  377. END Scope;
  378. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  379. VAR parameter: SyntaxTree.Parameter;
  380. BEGIN
  381. parameter := first;
  382. WHILE parameter # NIL DO
  383. VisitParameter(parameter);
  384. parameter := parameter.nextParameter;
  385. END;
  386. END Parameters;
  387. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  388. VAR scope: SyntaxTree.ProcedureScope;
  389. prevScope: SyntaxTree.Scope;
  390. inline, finalizer: BOOLEAN;
  391. procedureType: SyntaxTree.ProcedureType;
  392. pc: LONGINT;
  393. stackSize: LONGINT;
  394. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  395. null,size,src,dest,fp,res: IntermediateCode.Operand;
  396. cc: LONGINT;
  397. cellType: SyntaxTree.CellType;
  398. registerNumber: LONGINT;
  399. registerClass: IntermediateCode.RegisterClass;
  400. type: IntermediateCode.Type;
  401. formalParameter: SyntaxTree.Parameter;
  402. recordType: SyntaxTree.RecordType;
  403. isModuleBody: BOOLEAN;
  404. PROCEDURE Signature;
  405. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  406. BEGIN
  407. procedureType := x.type(SyntaxTree.ProcedureType);
  408. returnType := procedureType.returnType;
  409. IF returnType # NIL THEN
  410. meta.CheckTypeDeclaration(returnType)
  411. END;
  412. parameter := procedureType.firstParameter;
  413. WHILE parameter # NIL DO
  414. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  415. parameter := parameter.nextParameter;
  416. END;
  417. END Signature;
  418. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  419. VAR result: BOOLEAN;
  420. BEGIN
  421. result := FALSE;
  422. IF x = SyntaxTree.invalidExpression THEN
  423. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  424. result := TRUE;
  425. value := x.resolved(SyntaxTree.IntegerValue).value;
  426. ELSE
  427. Error(x.position,"expression is not an integer constant");
  428. END;
  429. RETURN result;
  430. END CheckIntegerValue;
  431. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  432. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  433. BEGIN
  434. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  435. WHILE (this # NIL) & (this.identifier # id) DO
  436. this := this.nextModifier;
  437. END;
  438. IF this # NIL THEN
  439. IF this.expression = NIL THEN
  440. Error(this.position,"expected expression value");
  441. ELSIF CheckIntegerValue(this.expression,value) THEN
  442. END;
  443. RETURN TRUE
  444. ELSE RETURN FALSE
  445. END;
  446. END HasValue;
  447. BEGIN
  448. IF x.externalName # NIL THEN RETURN END;
  449. (*
  450. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  451. *)
  452. (* code section for this procedure *)
  453. scope := x.procedureScope;
  454. prevScope := currentScope;
  455. currentScope := scope;
  456. procedureType := x.type(SyntaxTree.ProcedureType);
  457. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  458. implementationVisitor.temporaries.Init;
  459. implementationVisitor.usedRegisters := NIL;
  460. implementationVisitor.registerUsageCount.Init;
  461. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  462. IF (scope.body # NIL) & (x.isInline) THEN
  463. inline := TRUE;
  464. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  465. ir.SetExported(IsExported(x));
  466. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  467. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  468. IF backend.cellsAreObjects THEN
  469. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  470. ir.SetExported(IsExported(x));
  471. ELSE
  472. RETURN; (* cellnet cannot be compiled for final static hardware *)
  473. END;
  474. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  475. inline := FALSE;
  476. AddBodyCallStub(x);
  477. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  478. ir.SetExported(IsExported(x));
  479. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  480. inline := FALSE;
  481. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  482. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := ActiveCells.defaultDataMemorySize END;
  483. AddBodyCallStub(x);
  484. AddStackAllocation(x,stackSize);
  485. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  486. ir.SetExported(IsExported(x));
  487. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  488. inline := FALSE;
  489. Parameters(procedureType.firstParameter);
  490. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  491. ir.SetExported(IsExported(x));
  492. ELSE
  493. inline := FALSE;
  494. IF x.isEntry OR x.isExit THEN
  495. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  496. ir.SetExported(TRUE);
  497. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  498. ELSE
  499. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  500. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  501. END;
  502. END;
  503. cc := procedureType.callingConvention;
  504. IF scope.body # NIL THEN
  505. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  506. registerNumber := 0;
  507. IF ~inline THEN
  508. IF scope.lastVariable = NIL THEN
  509. stackSize := 0
  510. ELSE
  511. stackSize := scope.lastVariable.offsetInBits;
  512. IF stackSize <0 THEN stackSize := -stackSize END;
  513. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  514. END;
  515. (*
  516. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  517. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  518. *)
  519. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  520. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  521. END;
  522. pc := ir.pc-1;
  523. (*
  524. ir.Emit(Nop(position)); (* placeholder for fill *)
  525. *)
  526. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  527. formalParameter := procedureType.lastParameter;
  528. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  529. IF ~PassInRegister(formalParameter) THEN
  530. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  531. ELSE
  532. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  533. type := GetType(system, formalParameter.type);
  534. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  535. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  536. ir.Emit(Mov(-1,dest, src));
  537. implementationVisitor.ReleaseIntermediateOperand(src);
  538. INC(registerNumber);
  539. formalParameter := formalParameter.prevParameter;
  540. END;
  541. END;
  542. END;
  543. ir.EnterValidPAF;
  544. END;
  545. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  546. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  547. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  548. IF scope.lastVariable # NIL THEN
  549. stackSize := scope.lastVariable.offsetInBits;
  550. IF stackSize <0 THEN stackSize := -stackSize END;
  551. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  552. END;
  553. END;
  554. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  555. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  556. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  557. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  558. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  559. ir.EmitAt(pc,Push(size));
  560. implementationVisitor.StaticCallOperand(result,procedure);
  561. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  562. END;
  563. *)
  564. END;
  565. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  566. IF stackSize > 0 THEN
  567. IF (stackSize MOD system.addressSize = 0) THEN
  568. null := IntermediateCode.Immediate(addressType,0);
  569. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  570. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  571. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  572. ELSE
  573. null := IntermediateCode.Immediate(int8,0);
  574. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  575. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  576. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  577. END;
  578. (*! should potentially be called by backend -- enter might initialize
  579. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  580. *)
  581. END;
  582. ir.ExitValidPAF;
  583. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  584. finalizer := FALSE;
  585. IF backend.cooperative & x.isFinalizer THEN
  586. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  587. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  588. GetRecordTypeName(recordType,name);
  589. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  590. END;
  591. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  592. IF backend.cooperative THEN
  593. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  594. IF implementationVisitor.profile & ~isModuleBody THEN
  595. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  596. END;
  597. END;
  598. implementationVisitor.EmitLeave(ir, x.position,cc);
  599. IF finalizer THEN
  600. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  601. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  602. ir.Emit(Br(x.position,dest));
  603. ELSE
  604. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  605. END;
  606. ELSE
  607. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  608. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  609. END;
  610. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  611. IF backend.cooperative THEN
  612. IF HasPointers (scope) THEN
  613. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  614. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  615. ir.Emit(Result(x.position, res));
  616. ir.Emit(Push(x.position, res));
  617. implementationVisitor.ResetVariables(scope);
  618. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  619. ir.Emit(Pop(x.position, res));
  620. ir.Emit(Return(x.position, res));
  621. ELSE
  622. implementationVisitor.ResetVariables(scope);
  623. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  624. END;
  625. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  626. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  627. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  628. ir.Emit(Result(x.position, res));
  629. ir.Emit(Push(x.position, res));
  630. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  631. ir.Emit(Pop(x.position, res));
  632. ir.Emit(Return(x.position, res));
  633. ELSE
  634. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  635. END;
  636. END;
  637. implementationVisitor.EmitLeave(ir,x.position,cc);
  638. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  639. ELSE
  640. ir.Emit(Nop(x.position));
  641. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  642. implementationVisitor.EmitLeave(ir,x.position,cc);
  643. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  644. END;
  645. END;
  646. END
  647. END;
  648. ELSE (* force body for procedures *)
  649. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  650. ir.EnterValidPAF;
  651. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  652. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  653. ir.ExitValidPAF;
  654. implementationVisitor.EmitLeave(ir,x.position,cc);
  655. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  656. END;
  657. Scope(scope);
  658. Signature;
  659. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  660. currentScope := prevScope;
  661. END Procedure;
  662. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  663. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  664. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  665. BEGIN
  666. ASSERT (bodyProcedure # NIL);
  667. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  668. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  669. procedure.SetScope(bodyProcedure.scope);
  670. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  671. procedure.SetAccess(SyntaxTree.Hidden);
  672. Global.GetSymbolSegmentedName (procedure,name);
  673. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  674. ir.SetExported(TRUE);
  675. ir.SetPriority(InitPriority);
  676. Global.GetSymbolSegmentedName (bodyProcedure,name);
  677. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  678. implementationVisitor.currentScope := module.module.moduleScope;
  679. implementationVisitor.section := ir;
  680. implementationVisitor.PushSelfPointer();
  681. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  682. ELSIF backend.preregisterStatic THEN
  683. implementationVisitor.currentScope := module.module.moduleScope;
  684. implementationVisitor.section := ir;
  685. implementationVisitor.PushSelfPointer();
  686. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  687. ELSE
  688. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  689. ir.Emit(Call(bodyProcedure.position,op, 0));
  690. END;
  691. END AddBodyCallStub;
  692. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  693. VAR name: Basic.SegmentedName;
  694. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  695. BEGIN
  696. Global.GetSymbolSegmentedName (symbol,name);
  697. Basic.RemoveSuffix(name);
  698. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  699. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  700. ir.SetExported(TRUE);
  701. ir.SetPriority(FirstPriority);
  702. IntermediateCode.InitImmediate(op,addressType,initStack);
  703. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  704. END AddStackAllocation;
  705. (** entry function to visit a complete module *)
  706. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  707. VAR
  708. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  709. hasDynamicOperatorDeclarations: BOOLEAN;
  710. operator: SyntaxTree.Operator;
  711. import: SyntaxTree.Import;
  712. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  713. BEGIN
  714. type := type.resolved;
  715. IF type IS SyntaxTree.RecordType THEN
  716. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  717. ELSIF (type IS SyntaxTree.ArrayType) THEN
  718. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  719. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  720. END;
  721. ELSIF type IS SyntaxTree.MathArrayType THEN
  722. WITH type: SyntaxTree.MathArrayType DO
  723. IF type.form = SyntaxTree.Open THEN
  724. RETURN TRUE
  725. ELSIF type.form = SyntaxTree.Static THEN
  726. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  727. END;
  728. END;
  729. END;
  730. RETURN FALSE
  731. END TypeNeedsInitialization;
  732. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  733. VAR variable: SyntaxTree.Variable;
  734. BEGIN
  735. variable := scope.firstVariable;
  736. WHILE variable # NIL DO
  737. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  738. IF variable.initializer # NIL THEN RETURN TRUE END;
  739. variable := variable.nextVariable;
  740. END;
  741. RETURN FALSE
  742. END ScopeNeedsInitialization;
  743. BEGIN
  744. ASSERT(x # NIL); ASSERT(module # NIL);
  745. SELF.module := module;
  746. (* add import names to the generated Sections.Module *)
  747. import := x.moduleScope.firstImport;
  748. WHILE import # NIL DO
  749. import.module.GetName(idstr);
  750. module.imports.AddName(idstr);
  751. import := import.nextImport
  752. END;
  753. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  754. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  755. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  756. moduleSelf.SetType(system.anyType);
  757. moduleSelf.SetScope(x.moduleScope);
  758. moduleSelf.SetUntraced(TRUE);
  759. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  760. ir.SetExported(TRUE);
  761. IntermediateCode.InitImmediate(op,addressType,0);
  762. ir.Emit(Data(-1,op));
  763. END;
  764. implementationVisitor.module := module;
  765. implementationVisitor.moduleScope := x.moduleScope;
  766. implementationVisitor.moduleSelf := moduleSelf;
  767. implementationVisitor.canBeLoaded := TRUE;
  768. meta.SetModule(module);
  769. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  770. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  771. END;
  772. IF backend.profile THEN
  773. EnsureBodyProcedure(x.moduleScope);
  774. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  775. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  776. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  777. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  778. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  779. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  780. Global.GetModuleName(module.module,idstr);
  781. implementationVisitor.ProfilerAddModule(idstr);
  782. implementationVisitor.numberProcedures := 0;
  783. END;
  784. implementationVisitor.profile := backend.profile;
  785. (* check if there is at least one dynamic operator locally defined *)
  786. hasDynamicOperatorDeclarations := FALSE;
  787. operator := x.moduleScope.firstOperator;
  788. WHILE operator # NIL DO
  789. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  790. operator := operator.nextOperator
  791. END;
  792. (* add operator initialization code section *)
  793. IF hasDynamicOperatorDeclarations THEN
  794. EnsureBodyProcedure(x.moduleScope);
  795. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  796. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  797. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  798. END;
  799. Scope(x.moduleScope);
  800. IF hasDynamicOperatorDeclarations THEN
  801. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  802. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  803. END;
  804. IF backend.profile THEN
  805. implementationVisitor.ProfilerPatchInit;
  806. END;
  807. END Module;
  808. END DeclarationVisitor;
  809. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  810. RegisterUsageCount*=OBJECT
  811. VAR used: UsedArray; count: LONGINT;
  812. PROCEDURE &Init;
  813. VAR i: LONGINT;
  814. BEGIN
  815. count := 0;
  816. IF used = NIL THEN NEW(used,64); END;
  817. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  818. END Init;
  819. PROCEDURE Grow;
  820. VAR new: UsedArray; size,i: LONGINT;
  821. BEGIN
  822. size := LEN(used)*2;
  823. NEW(new,size);
  824. FOR i := 0 TO LEN(used)-1 DO
  825. new[i].count := used[i].count;
  826. new[i].type := used[i].type;
  827. new[i].map := used[i].map
  828. END;
  829. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  830. used := new
  831. END Grow;
  832. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  833. BEGIN
  834. INC(count);
  835. IF count = LEN(used) THEN Grow END;
  836. used[count].type := type;
  837. used[count].class := class;
  838. used[count].map := count;
  839. RETURN count;
  840. END Next;
  841. PROCEDURE IncUse(register: LONGINT);
  842. BEGIN
  843. INC(used[register].count);
  844. (*
  845. IF (register = 1) & (count > 30) THEN
  846. D.TraceBack;
  847. END;
  848. *)
  849. END IncUse;
  850. PROCEDURE DecUse(register: LONGINT);
  851. BEGIN
  852. DEC(used[register].count);
  853. END DecUse;
  854. PROCEDURE Map(register: LONGINT): LONGINT;
  855. VAR map : LONGINT;
  856. BEGIN
  857. IF register > 0 THEN
  858. map := used[register].map;
  859. WHILE register # map DO register := map; map := used[register].map END;
  860. END;
  861. RETURN register
  862. END Map;
  863. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  864. BEGIN
  865. used[register].map:= to;
  866. used[to].count := used[register].count;
  867. used[register].count := 0;
  868. END Remap;
  869. PROCEDURE Use(register: LONGINT): LONGINT;
  870. BEGIN
  871. IF register< LEN(used) THEN
  872. RETURN used[register].count
  873. ELSE
  874. RETURN 0
  875. END
  876. END Use;
  877. END RegisterUsageCount;
  878. RegisterEntry = POINTER TO RECORD
  879. prev,next: RegisterEntry;
  880. register: LONGINT;
  881. registerClass: IntermediateCode.RegisterClass;
  882. type: IntermediateCode.Type;
  883. END;
  884. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  885. Variables = OBJECT (Basic.List)
  886. VAR
  887. inUse: VariableUse;
  888. registerIndex: LONGINT;
  889. PROCEDURE & Init;
  890. VAR i: LONGINT;
  891. BEGIN
  892. InitList(16);
  893. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  894. registerIndex := 1024;
  895. END Init;
  896. PROCEDURE GetUsage(VAR use: VariableUse);
  897. BEGIN
  898. use := inUse;
  899. END GetUsage;
  900. PROCEDURE SetUsage(CONST use: VariableUse);
  901. BEGIN
  902. inUse := use;
  903. END SetUsage;
  904. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  905. VAR any: ANY;
  906. BEGIN
  907. any := Get(i);;
  908. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  909. END GetVariable;
  910. PROCEDURE Occupy(pos: LONGINT);
  911. BEGIN
  912. INCL(inUse[pos DIV 32], pos MOD 32);
  913. END Occupy;
  914. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  915. BEGIN
  916. Occupy(Length());
  917. Add(v);
  918. END AddVariable;
  919. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  920. VAR var : SyntaxTree.Variable;
  921. BEGIN
  922. FOR pos := 0 TO Length()-1 DO
  923. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  924. var := GetVariable(pos);
  925. IF type.SameType(var.type) THEN
  926. Occupy(pos); RETURN var
  927. END;
  928. END;
  929. END;
  930. pos := Length();
  931. RETURN NIL
  932. END GetFreeVariable;
  933. END Variables;
  934. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  935. SymbolMapper = OBJECT
  936. VAR
  937. first: SymbolMap;
  938. PROCEDURE & Init;
  939. BEGIN
  940. first := NIL;
  941. END Init;
  942. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  943. VAR new: SymbolMap;
  944. BEGIN
  945. NEW(new); new.this := this; new.to := to; new.tag := tag;
  946. new.next := first; first := new;
  947. END Add;
  948. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  949. VAR s: SymbolMap;
  950. BEGIN
  951. s := first;
  952. WHILE (s # NIL) & (s.this # this) DO
  953. s := s.next
  954. END;
  955. RETURN s
  956. END Get;
  957. END SymbolMapper;
  958. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  959. VAR
  960. system: Global.System;
  961. section: IntermediateCode.Section;
  962. module: Sections.Module;
  963. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  964. awaitProcCounter, labelId, constId, caseId: LONGINT;
  965. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  966. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  967. checker: SemanticChecker.Checker;
  968. backend: IntermediateBackend;
  969. meta: MetaDataGenerator;
  970. position: LONGINT;
  971. moduleSelf: SyntaxTree.Variable;
  972. (* variables for hand over of variables / temporary state *)
  973. currentScope: SyntaxTree.Scope;
  974. constantDeclaration : SyntaxTree.Symbol;
  975. result: Operand; (* result of the most recent expression / statement *)
  976. destination: IntermediateCode.Operand;
  977. arrayDestinationTag: IntermediateCode.Operand;
  978. arrayDestinationDimension:LONGINT;
  979. currentLoop: Label; (* variable to hand over loop exit jump list *)
  980. conditional: BOOLEAN;
  981. trueLabel, falseLabel, exitLabel: Label;
  982. locked: BOOLEAN;
  983. (*
  984. usedRegisters: Registers;
  985. *)
  986. registerUsageCount: RegisterUsageCount;
  987. usedRegisters: RegisterEntry;
  988. (* useful operands and types *)
  989. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  990. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  991. commentPrintout: Printout.Printer;
  992. dump: Streams.Writer;
  993. tagsAvailable : BOOLEAN;
  994. supportedInstruction: SupportedInstructionProcedure;
  995. supportedImmediate: SupportedImmediateProcedure;
  996. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  997. emitLabels: BOOLEAN;
  998. runtimeModuleName : SyntaxTree.IdentifierString;
  999. newObjectFile: BOOLEAN;
  1000. indexCounter: LONGINT;
  1001. profile: BOOLEAN;
  1002. profileId, profileInit: IntermediateCode.Section;
  1003. profileInitPatchPosition: LONGINT;
  1004. numberProcedures: LONGINT;
  1005. procedureResultDesignator : SyntaxTree.Designator;
  1006. operatorInitializationCodeSection: IntermediateCode.Section;
  1007. fingerPrinter: FingerPrinter.FingerPrinter;
  1008. temporaries: Variables;
  1009. canBeLoaded : BOOLEAN;
  1010. currentIsInline: BOOLEAN;
  1011. currentMapper: SymbolMapper;
  1012. currentInlineExit: Label;
  1013. moduleBodySection: IntermediateCode.Section;
  1014. NeedDescriptor : BOOLEAN;
  1015. cooperativeSwitches: BOOLEAN;
  1016. lastSwitchPC: LONGINT;
  1017. isUnchecked: BOOLEAN;
  1018. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1019. newObjectFile: BOOLEAN);
  1020. BEGIN
  1021. SELF.system := system;
  1022. SELF.runtimeModuleName := runtime;
  1023. currentScope := NIL;
  1024. hiddenPointerType := NIL;
  1025. delegatePointerType := NIL;
  1026. awaitProcCounter := 0;
  1027. labelId := 0; constId := 0; labelId := 0;
  1028. SELF.checker := checker;
  1029. SELF.backend := backend;
  1030. position := Diagnostics.Invalid;
  1031. conditional := FALSE;
  1032. locked := FALSE;
  1033. InitOperand(result,ModeUndefined);
  1034. addressType := IntermediateCode.GetType(system,system.addressType);
  1035. setType := IntermediateCode.GetType(system,system.setType);
  1036. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1037. byteType := IntermediateCode.GetType(system, system.byteType);
  1038. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1039. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1040. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1041. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1042. nil := IntermediateCode.Immediate(addressType,0);
  1043. IntermediateCode.InitOperand(destination);
  1044. tagsAvailable := TRUE;
  1045. supportedInstruction := supportedInstructionProcedure;
  1046. supportedImmediate := supportedImmediateProcedure;
  1047. inData := FALSE;
  1048. SELF.emitLabels := emitLabels;
  1049. IntermediateCode.InitOperand(arrayDestinationTag);
  1050. bool := IntermediateCode.GetType(system,system.booleanType);
  1051. IntermediateCode.InitImmediate(false,bool,0);
  1052. IntermediateCode.InitImmediate(true,bool,1);
  1053. SELF.newObjectFile := newObjectFile;
  1054. indexCounter := 0;
  1055. NEW(registerUsageCount);
  1056. usedRegisters := NIL;
  1057. procedureResultDesignator := NIL;
  1058. NEW(fingerPrinter, system);
  1059. NEW(temporaries);
  1060. currentIsInline := FALSE;
  1061. NeedDescriptor := FALSE;
  1062. isUnchecked := backend.noRuntimeChecks;
  1063. END Init;
  1064. TYPE Context = RECORD
  1065. section: IntermediateCode.Section;
  1066. registerUsageCount: RegisterUsageCount;
  1067. usedRegisters: RegisterEntry;
  1068. END;
  1069. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1070. VAR context: Context;
  1071. BEGIN
  1072. context.section := section;
  1073. context.registerUsageCount := registerUsageCount;
  1074. context.usedRegisters := usedRegisters;
  1075. section := new;
  1076. NEW(registerUsageCount);
  1077. usedRegisters := NIL;
  1078. RETURN context;
  1079. END SwitchContext;
  1080. PROCEDURE ReturnToContext(context: Context);
  1081. BEGIN
  1082. section := context.section;
  1083. registerUsageCount := context.registerUsageCount;
  1084. usedRegisters := context.usedRegisters;
  1085. END ReturnToContext;
  1086. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1087. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1088. BEGIN
  1089. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1090. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1091. END;
  1092. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1093. section.SetExported(IsExported(syntaxTreeSymbol));
  1094. RETURN section
  1095. END NewSection;
  1096. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1097. VAR new: LONGINT;
  1098. BEGIN
  1099. new := registerUsageCount.Next(type,class);
  1100. UseRegister(new);
  1101. RETURN new
  1102. END AcquireRegister;
  1103. (** get the name for the code section that represens a certain symbol
  1104. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1105. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1106. VAR
  1107. operatorFingerPrint: SyntaxTree.FingerPrint;
  1108. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1109. BEGIN
  1110. Global.GetSymbolSegmentedName(symbol, name);
  1111. (* if the symbol is an operator, then append the fingerprint to the name *)
  1112. IF symbol IS SyntaxTree.Operator THEN
  1113. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1114. string := "[";
  1115. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1116. Strings.Append(string, operatorFingerPrintString);
  1117. Strings.Append(string, "]");
  1118. Basic.AppendToSegmentedName(name,string);
  1119. END
  1120. END GetCodeSectionNameForSymbol;
  1121. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1122. BEGIN
  1123. IF dump # NIL THEN
  1124. dump.String("enter "); dump.String(s); dump.Ln;
  1125. END;
  1126. END TraceEnter;
  1127. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1128. BEGIN
  1129. IF dump # NIL THEN
  1130. dump.String("exit "); dump.String(s); dump.Ln;
  1131. END;
  1132. END TraceExit;
  1133. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1134. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1135. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1136. VAR i: LONGINT;
  1137. BEGIN
  1138. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1139. IF op.rule # NIL THEN
  1140. FOR i := 0 TO LEN(op.rule)-1 DO
  1141. CheckRegister(op.rule[i])
  1142. END;
  1143. END;
  1144. END CheckRegister;
  1145. BEGIN
  1146. CheckRegister(instruction.op1);
  1147. CheckRegister(instruction.op2);
  1148. CheckRegister(instruction.op3);
  1149. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1150. ELSE section.Emit(instruction);
  1151. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1152. END;
  1153. END Emit;
  1154. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1155. BEGIN
  1156. IF backend.cooperative THEN
  1157. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1158. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1159. ELSE
  1160. Emit(Trap(position,trapNo));
  1161. END;
  1162. END EmitTrap;
  1163. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1164. VAR name: Basic.SegmentedName;
  1165. VAR op1, op2, reg: IntermediateCode.Operand;
  1166. VAR call, nocall: Label;
  1167. VAR parametersSize: LONGINT;
  1168. VAR prevSection: IntermediateCode.Section;
  1169. VAR prevDump: Streams.Writer;
  1170. VAR body: SyntaxTree.Body;
  1171. BEGIN
  1172. ASSERT(~procedure.type(SyntaxTree.ProcedureType).noPAF);
  1173. prevSection := SELF.section;
  1174. SELF.section := section;
  1175. prevDump := dump;
  1176. dump := section.comments;
  1177. IF backend.hasLinkRegister THEN
  1178. Emit(Push(-1, lr));
  1179. END;
  1180. Emit(Push(-1,fp));
  1181. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1182. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1183. GetCodeSectionNameForSymbol(procedure, name);
  1184. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1185. ELSE
  1186. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1187. END;
  1188. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1189. Emit(Push(-1,op1));
  1190. Emit(Mov(-1,fp, sp));
  1191. body := procedure.procedureScope.body;
  1192. IF (body # NIL) & (body.code = NIL) & ~procedure.procedureScope.body.isUnchecked THEN
  1193. NEW(call, section);
  1194. NEW(nocall, section);
  1195. reg := NewRegisterOperand(addressType);
  1196. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1197. Emit(Sub(-1,reg, sp, op1));
  1198. BrltL(call, sp, reg);
  1199. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1200. BrgeL(nocall, sp, op2);
  1201. call.Resolve(section.pc);
  1202. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1203. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1204. Emit(Push(-1, op2));
  1205. Emit(Push(-1, reg));
  1206. ReleaseIntermediateOperand(reg);
  1207. CallThis(position, "Activities","ExpandStack",2);
  1208. Emit(Result(-1, sp));
  1209. nocall.Resolve(section.pc);
  1210. END;
  1211. ELSE
  1212. Emit(Mov(-1, fp, sp));
  1213. END;
  1214. Emit(Enter(-1, callconv, varSize));
  1215. SELF.section := prevSection;
  1216. dump := prevDump;
  1217. END EmitEnter;
  1218. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1219. VAR op1,op2: IntermediateCode.Operand;
  1220. VAR instruction: IntermediateCode.Instruction;
  1221. BEGIN
  1222. IntermediateCode.InitNumber(op1,callconv);
  1223. IntermediateCode.InitNumber(op2,varSize);
  1224. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1225. RETURN instruction
  1226. END Enter;
  1227. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1228. VAR op1: IntermediateCode.Operand;
  1229. VAR instruction: IntermediateCode.Instruction;
  1230. BEGIN
  1231. IntermediateCode.InitNumber(op1,callconv);
  1232. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1233. RETURN instruction
  1234. END Leave;
  1235. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1236. VAR prevSection: IntermediateCode.Section;
  1237. VAR op2: IntermediateCode.Operand;
  1238. BEGIN
  1239. prevSection := SELF.section;
  1240. SELF.section := section;
  1241. Emit(Leave(position, callconv));
  1242. IF backend.cooperative THEN
  1243. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1244. Emit(Add(position, sp, fp, op2));
  1245. ELSE
  1246. Emit(Mov(position, sp, fp));
  1247. END;
  1248. Emit(Pop(position, fp));
  1249. SELF.section := prevSection;
  1250. END EmitLeave;
  1251. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1252. VAR m: SymbolMap;
  1253. BEGIN
  1254. position := x.position;
  1255. IF currentIsInline THEN
  1256. m := currentMapper.Get(x);
  1257. IF m # NIL THEN
  1258. (*
  1259. Printout.Info("mapping from", x);
  1260. Printout.Info("mapping to ", m.to);
  1261. *)
  1262. m.to.Accept(SELF);
  1263. op := result;
  1264. IF m.tag # NIL THEN
  1265. ReleaseIntermediateOperand(result.tag);
  1266. m.tag.Accept(SELF);
  1267. op.tag := result.op;
  1268. ReleaseIntermediateOperand(result.tag);
  1269. END;
  1270. RETURN
  1271. END;
  1272. END;
  1273. x.Accept(SELF);
  1274. op := result;
  1275. END Symbol;
  1276. PROCEDURE Expression(x: SyntaxTree.Expression);
  1277. BEGIN
  1278. position := x.position;
  1279. constantDeclaration := NIL;
  1280. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1281. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1282. END;
  1283. IF x.resolved # NIL THEN
  1284. x.resolved.Accept(SELF)
  1285. ELSE
  1286. x.Accept(SELF)
  1287. END;
  1288. (* check this, was commented out in ActiveCells3 *)
  1289. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1290. Error(x.position, "unsupported modifier");
  1291. END;
  1292. END Expression;
  1293. (*
  1294. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1295. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1296. BEGIN
  1297. temporaries.GetUsage(current);
  1298. FOR i := 0 TO LEN(current)-1 DO
  1299. set := current[i] - previous[i];
  1300. IF set # {} THEN
  1301. FOR j := 0 TO MAX(SET)-1 DO
  1302. IF j IN set THEN
  1303. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1304. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1305. Symbol(variable, op);
  1306. MakeMemory(tmp,op.op,addressType,0);
  1307. ReleaseOperand(op);
  1308. Emit(Mov(position,tmp, nil ) );
  1309. ReleaseIntermediateOperand(tmp);
  1310. END;
  1311. END;
  1312. END;
  1313. END;
  1314. END;
  1315. END ResetUsedTemporaries;
  1316. *)
  1317. PROCEDURE Statement(x: SyntaxTree.Statement);
  1318. VAR use: VariableUse;
  1319. BEGIN
  1320. temporaries.GetUsage(use);
  1321. position := x.position;
  1322. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1323. IF commentPrintout # NIL THEN
  1324. commentPrintout.Statement(x);
  1325. dump.Ln;
  1326. (*dump.Update;*)
  1327. END;
  1328. x.Accept(SELF);
  1329. (*
  1330. CheckRegistersFree();
  1331. *)
  1332. (*ResetUsedTemporaries(use);*)
  1333. temporaries.SetUsage(use);
  1334. END Statement;
  1335. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1336. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1337. *)
  1338. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1339. BEGIN
  1340. IF op.mode = IntermediateCode.ModeMemory THEN
  1341. ReuseCopy(res,op);
  1342. ELSE
  1343. res := op;
  1344. UseIntermediateOperand(res);
  1345. END;
  1346. IntermediateCode.AddOffset(res,offset);
  1347. IntermediateCode.MakeMemory(res,type);
  1348. END MakeMemory;
  1349. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1350. VAR mem: IntermediateCode.Operand;
  1351. BEGIN
  1352. MakeMemory(mem,res,type,offset);
  1353. ReleaseIntermediateOperand(res);
  1354. res := mem;
  1355. END ToMemory;
  1356. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1357. VAR mem: IntermediateCode.Operand;
  1358. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1359. componentType: SyntaxTree.Type;
  1360. BEGIN
  1361. type := type.resolved;
  1362. IF operand.mode = ModeReference THEN
  1363. IF type IS SyntaxTree.RangeType THEN
  1364. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1365. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1366. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1367. ReleaseOperand(operand);
  1368. operand.op := firstOp;
  1369. operand.tag := lastOp;
  1370. operand.extra := stepOp;
  1371. ELSIF type IS SyntaxTree.ComplexType THEN
  1372. componentType := type(SyntaxTree.ComplexType).componentType;
  1373. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1374. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1375. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1376. ReleaseOperand(operand);
  1377. operand.op := firstOp;
  1378. operand.tag := lastOp
  1379. ELSE
  1380. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1381. ReleaseIntermediateOperand(operand.op);
  1382. operand.op := mem;
  1383. END;
  1384. operand.mode := ModeValue;
  1385. END;
  1386. ASSERT(operand.mode = ModeValue);
  1387. END LoadValue;
  1388. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1389. VAR prevConditional: BOOLEAN;
  1390. BEGIN
  1391. prevConditional := conditional;
  1392. conditional := FALSE;
  1393. InitOperand(result, ModeUndefined);
  1394. Expression(x);
  1395. op := result;
  1396. LoadValue(op,x.type.resolved);
  1397. conditional := prevConditional;
  1398. END Evaluate;
  1399. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1400. VAR prevConditional: BOOLEAN;
  1401. BEGIN
  1402. prevConditional := conditional;
  1403. conditional := FALSE;
  1404. InitOperand(result,ModeUndefined);
  1405. Expression(x);
  1406. op := result;
  1407. (*
  1408. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1409. *)
  1410. conditional := prevConditional;
  1411. END Designate;
  1412. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1413. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1414. BEGIN
  1415. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1416. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1417. conditional := TRUE;
  1418. trueLabel := trueL; falseLabel := falseL;
  1419. Expression(x);
  1420. trueL := trueLabel; falseL := falseLabel;
  1421. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1422. END Condition;
  1423. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1424. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1425. BEGIN
  1426. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1427. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1428. RETURN op
  1429. END NewRegisterOperand;
  1430. PROCEDURE UnuseRegister(register: LONGINT);
  1431. BEGIN
  1432. IF (register > 0) THEN
  1433. register := registerUsageCount.Map(register);
  1434. registerUsageCount.DecUse(register);
  1435. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1436. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1437. END;
  1438. IF registerUsageCount.Use(register)=0 THEN
  1439. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1440. Warning(position, "register cannot be removed");
  1441. END;
  1442. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1443. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1444. END;
  1445. ELSIF registerUsageCount.Use(register)<0 THEN
  1446. Warning(position, "register removed too often");
  1447. IF dump # NIL THEN
  1448. dump.String("register removed too often"); dump.Ln; dump.Update;
  1449. END;
  1450. D.TraceBack;
  1451. END;
  1452. END;
  1453. END UnuseRegister;
  1454. PROCEDURE UseRegister(register: LONGINT);
  1455. BEGIN
  1456. IF (register > 0) THEN
  1457. register := registerUsageCount.Map(register);
  1458. registerUsageCount.IncUse(register);
  1459. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1460. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1461. END;
  1462. IF registerUsageCount.Use(register)=1 THEN
  1463. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1464. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1465. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1466. END;
  1467. END;
  1468. END;
  1469. END UseRegister;
  1470. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1471. BEGIN
  1472. UnuseRegister(op.register)
  1473. END ReleaseIntermediateOperand;
  1474. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1475. BEGIN
  1476. UseRegister(op.register)
  1477. END UseIntermediateOperand;
  1478. PROCEDURE ReleaseOperand(CONST op: Operand);
  1479. BEGIN
  1480. UnuseRegister(op.op.register);
  1481. UnuseRegister(op.tag.register);
  1482. UnuseRegister(op.extra.register);
  1483. END ReleaseOperand;
  1484. (* save registers marked in array "markedRegisters" to the stack
  1485. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1486. *)
  1487. PROCEDURE SaveRegisters();
  1488. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1489. BEGIN
  1490. entry := usedRegisters;
  1491. WHILE entry # NIL DO
  1492. type := registerUsageCount.used[entry.register].type;
  1493. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1494. Emit(Push(position,op));
  1495. entry := entry.next;
  1496. END;
  1497. END SaveRegisters;
  1498. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1499. BEGIN
  1500. saved := usedRegisters;
  1501. usedRegisters := NIL;
  1502. END ReleaseUsedRegisters;
  1503. (** remove parameter registers from used queue *)
  1504. PROCEDURE ReleaseParameterRegisters;
  1505. VAR entry,prev,next: RegisterEntry;
  1506. BEGIN
  1507. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1508. WHILE entry # NIL DO
  1509. next := entry.next;
  1510. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1511. registerUsageCount.DecUse(entry.register);
  1512. ASSERT(registerUsageCount.Use(entry.register)=0);
  1513. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1514. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1515. END;
  1516. ELSIF prev = NIL THEN
  1517. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1518. ELSE
  1519. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1520. END;
  1521. entry := next;
  1522. END;
  1523. END ReleaseParameterRegisters;
  1524. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1525. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1526. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1527. BEGIN
  1528. entry := saved;
  1529. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1530. entry := prev;
  1531. WHILE entry # NIL DO
  1532. prev := entry.prev;
  1533. type := entry.type;
  1534. class := entry.registerClass;
  1535. IntermediateCode.InitRegister(op,type,class,entry.register);
  1536. (*
  1537. new := registerUsageCount.Next(type,class);
  1538. registerUsageCount.Remap(entry.register,new);
  1539. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1540. dump.String("remap register "); dump.Int(entry.register,1);
  1541. dump.String("to "); dump.Int(new,1);
  1542. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1543. END;
  1544. entry.register := new;
  1545. *)
  1546. Emit(Pop(position,op));
  1547. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1548. entry := prev;
  1549. END;
  1550. (*
  1551. usedRegisters := saved;
  1552. *)
  1553. END RestoreRegisters;
  1554. PROCEDURE CheckRegistersFree;
  1555. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1556. BEGIN
  1557. IF usedRegisters # NIL THEN
  1558. r := usedRegisters;
  1559. WHILE r # NIL DO
  1560. warning := "register ";
  1561. Strings.AppendInt(warning, r.register);
  1562. Strings.Append(warning, " not released.");
  1563. Warning(position,warning);
  1564. r := r .next;
  1565. END;
  1566. END;
  1567. FOR i := 0 TO registerUsageCount.count-1 DO
  1568. IF registerUsageCount.used[i].count < 0 THEN
  1569. warning := "register ";
  1570. Strings.AppendInt(warning, i);
  1571. Strings.Append(warning, " unused too often.");
  1572. Warning(position,warning);
  1573. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1574. warning := "register ";
  1575. Strings.AppendInt(warning, i);
  1576. Strings.Append(warning, " not unused often enough.");
  1577. Warning(position,warning);
  1578. END;
  1579. END;
  1580. END CheckRegistersFree;
  1581. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1582. Otherwise allocate a new register.
  1583. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1584. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1585. BEGIN
  1586. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1587. UseIntermediateOperand(result);
  1588. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1589. UseIntermediateOperand(result);
  1590. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1591. END;
  1592. END Reuse2;
  1593. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1594. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1595. If not then allocate a new register.
  1596. Does NOT necessarily keep the content of src1 or src2 in result!
  1597. *)
  1598. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1599. BEGIN
  1600. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1601. UseIntermediateOperand(result);
  1602. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1603. UseIntermediateOperand(result);
  1604. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1605. result := alternative; alternative := emptyOperand;
  1606. UseIntermediateOperand(result);
  1607. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1608. END;
  1609. END Reuse2a;
  1610. (* like reuse2 but only one source *)
  1611. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1612. BEGIN
  1613. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1614. UseIntermediateOperand(result);
  1615. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1616. END;
  1617. END Reuse1;
  1618. (* like reuse2a but only one source *)
  1619. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1620. BEGIN
  1621. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1622. UseIntermediateOperand(result);
  1623. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1624. UseIntermediateOperand(result);
  1625. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1626. END;
  1627. END Reuse1a;
  1628. (* like reuse1 but guarantees that content of src1 is in result *)
  1629. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1630. BEGIN
  1631. IF ReusableRegister(src1) THEN
  1632. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1633. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1634. UseIntermediateOperand(result);
  1635. ELSE
  1636. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1637. Emit(Mov(position,result,src1));
  1638. END
  1639. END ReuseCopy;
  1640. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1641. BEGIN
  1642. IF ReusableRegister(src) THEN
  1643. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1644. ELSE
  1645. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1646. Emit(Mov(position,result,src));
  1647. ReleaseIntermediateOperand(src);
  1648. END
  1649. END TransferToRegister;
  1650. (** labels and branches **)
  1651. PROCEDURE NewLabel(): Label;
  1652. VAR label: Label;
  1653. BEGIN
  1654. NEW(label,section); RETURN label;
  1655. END NewLabel;
  1656. PROCEDURE SetLabel(label: Label);
  1657. BEGIN label.Resolve(section.pc);
  1658. END SetLabel;
  1659. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1660. BEGIN
  1661. ASSERT(label # NIL);
  1662. IF label.pc < 0 THEN (* label not yet set *)
  1663. label.AddFixup(section.pc);
  1664. END;
  1665. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1666. END LabelOperand;
  1667. PROCEDURE BrL(label: Label);
  1668. BEGIN
  1669. Emit(Br(position,LabelOperand(label)));
  1670. END BrL;
  1671. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1672. BEGIN
  1673. Emit(Brge(position,LabelOperand(label),left,right));
  1674. END BrgeL;
  1675. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1676. BEGIN
  1677. Emit(Brlt(position,LabelOperand(label),left,right));
  1678. END BrltL;
  1679. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1680. BEGIN
  1681. Emit(Breq(position,LabelOperand(label),left,right));
  1682. END BreqL;
  1683. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1684. BEGIN
  1685. Emit(Brne(position,LabelOperand(label),left,right));
  1686. END BrneL;
  1687. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1688. VAR new: IntermediateCode.Operand;
  1689. BEGIN
  1690. IF Trace THEN TraceEnter("Convert") END;
  1691. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1692. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1693. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1694. & (operand.offset = 0)
  1695. THEN
  1696. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1697. Emit(Conv(position,new,operand));
  1698. ELSE
  1699. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1700. Emit(Conv(position,new,operand));
  1701. ReleaseIntermediateOperand(operand);
  1702. END;
  1703. operand := new;
  1704. ELSIF (operand.mode = IntermediateCode.ModeImmediate) & (operand.symbol.name = "") & (operand.type.sizeInBits <= type.sizeInBits) & (operand.type.form IN IntermediateCode.Integer) & (type.form IN IntermediateCode.Integer) THEN
  1705. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1706. ELSE
  1707. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1708. Emit(Conv(position,new,operand));
  1709. ReleaseIntermediateOperand(operand);
  1710. operand := new;
  1711. END;
  1712. IF Trace THEN TraceExit("Convert") END;
  1713. END Convert;
  1714. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1715. VAR exit: Label;
  1716. BEGIN
  1717. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1718. exit := NewLabel();
  1719. br(exit,left,right);
  1720. EmitTrap(position,trapNo);
  1721. SetLabel(exit);
  1722. END TrapC;
  1723. (** expressions *)
  1724. (** emit necessary runtime check for set elements **)
  1725. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1726. VAR max: IntermediateCode.Operand;
  1727. BEGIN
  1728. IF isUnchecked THEN RETURN END;
  1729. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1730. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1731. TrapC(BrgeL, max, o, SetElementTrap);
  1732. END;
  1733. END CheckSetElement;
  1734. (** the set that a range represents **)
  1735. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1736. VAR
  1737. operand: Operand;
  1738. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1739. BEGIN
  1740. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1741. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1742. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1743. one := IntermediateCode.Immediate(setType, 1);
  1744. Evaluate(x, operand);
  1745. Convert(operand.op, setType);
  1746. Convert(operand.tag, setType);
  1747. CheckSetElement(operand.op);
  1748. CheckSetElement(operand.tag);
  1749. (* create mask for lower bound
  1750. i.e. shift 11111111 to the left by the value of the lower bound
  1751. *)
  1752. Reuse1(temp, operand.op);
  1753. Emit(Shl(position,temp, allBits, operand.op));
  1754. ReleaseIntermediateOperand(operand.op);
  1755. operand.op := temp;
  1756. (* create mask for upper bound
  1757. i.e. shift 11111111 to the right by the difference between the
  1758. upper bound and the maximum number of set elements
  1759. *)
  1760. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1761. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1762. Reuse1(temp, operand.tag);
  1763. ELSE
  1764. Reuse1(temp, operand.tag);
  1765. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1766. Emit(Sub(position,temp, size, operand.tag));
  1767. END;
  1768. Emit(Shr(position,temp, allBits, operand.tag));
  1769. ReleaseIntermediateOperand(operand.tag);
  1770. operand.tag := temp;
  1771. Reuse2(resultingSet, operand.op, operand.tag);
  1772. (* intersect the two masks *)
  1773. Emit(And(position,resultingSet, operand.op, operand.tag));
  1774. ReleaseOperand(operand);
  1775. RETURN resultingSet
  1776. END SetFromRange;
  1777. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1778. VAR
  1779. res, operand: Operand;
  1780. temp, one, noBits, dest: IntermediateCode.Operand;
  1781. expression: SyntaxTree.Expression;
  1782. i: LONGINT;
  1783. BEGIN
  1784. IF Trace THEN TraceEnter("VisitSet") END;
  1785. dest := destination;
  1786. destination := emptyOperand;
  1787. noBits := IntermediateCode.Immediate(setType, 0);
  1788. one := IntermediateCode.Immediate(setType, 1);
  1789. (* start off with the empty set *)
  1790. InitOperand(res, ModeValue);
  1791. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1792. Emit(Mov(position,res.op, noBits));
  1793. FOR i := 0 TO x.elements.Length() - 1 DO
  1794. expression := x.elements.GetExpression(i);
  1795. IF expression IS SyntaxTree.RangeExpression THEN
  1796. (* range of set elements *)
  1797. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1798. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1799. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1800. ReleaseIntermediateOperand(temp)
  1801. ELSE
  1802. (* singelton element *)
  1803. Evaluate(expression, operand);
  1804. Convert(operand.op, setType);
  1805. CheckSetElement(operand.op);
  1806. (* create subset containing single element *)
  1807. Reuse1(temp, operand.op);
  1808. Emit(Shl(position,temp, one, operand.op));
  1809. ReleaseOperand(operand);
  1810. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1811. ReleaseIntermediateOperand(temp);
  1812. END
  1813. END;
  1814. result := res;
  1815. destination := dest;
  1816. IF Trace THEN TraceExit("VisitSet") END;
  1817. END VisitSet;
  1818. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1819. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1820. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1821. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1822. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1823. element: SyntaxTree.IntegerValue;
  1824. BEGIN
  1825. numberElements := x.elements.Length();
  1826. expression := index.parameters.GetExpression(dim);
  1827. element := expression(SyntaxTree.IntegerValue);
  1828. FOR i := 0 TO numberElements-1 DO
  1829. expression := x.elements.GetExpression(i);
  1830. element.SetValue(i);
  1831. IF expression IS SyntaxTree.MathArrayExpression THEN
  1832. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1833. ELSE
  1834. Assign(index,expression);
  1835. END;
  1836. END;
  1837. END RecursiveAssignment;
  1838. BEGIN
  1839. variable := GetTemporaryVariable(x.type, FALSE);
  1840. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1841. designator.SetType(variable.type);
  1842. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1843. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1844. FOR i := 0 TO dim-1 DO
  1845. element := SyntaxTree.NewIntegerValue(x.position,0);
  1846. element.SetType(system.longintType);
  1847. index.parameters.AddExpression(element);
  1848. END;
  1849. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1850. RecursiveAssignment(x,0);
  1851. Expression(designator);
  1852. END VisitMathArrayExpression;
  1853. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1854. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1855. BEGIN
  1856. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1857. dest := destination; destination := emptyOperand;
  1858. IF x.operator = Scanner.Not THEN
  1859. IF conditional THEN
  1860. Condition(x.left,falseLabel,trueLabel)
  1861. ELSE
  1862. Evaluate(x.left,operand);
  1863. InitOperand(result,ModeValue);
  1864. Reuse1a(result.op,operand.op,dest);
  1865. Emit(Xor(position,result.op,operand.op,true));
  1866. ReleaseOperand(operand);
  1867. END;
  1868. ELSIF x.operator = Scanner.Minus THEN
  1869. Evaluate(x.left,operand);
  1870. InitOperand(result,ModeValue);
  1871. Reuse1a(result.op,operand.op,dest);
  1872. type := x.left.type.resolved;
  1873. IF type IS SyntaxTree.SetType THEN
  1874. Emit(Not(position,result.op,operand.op));
  1875. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1876. Reuse1(result.tag,operand.tag);
  1877. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1878. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1879. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1880. Emit(Neg(position,result.op,operand.op));
  1881. ELSE HALT(200)
  1882. END;
  1883. ReleaseOperand(operand);
  1884. ELSIF x.operator = Scanner.Address THEN
  1885. Designate(x.left,operand);
  1886. operand.mode := ModeValue;
  1887. t0 := x.left.type.resolved;
  1888. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1889. ReleaseIntermediateOperand(operand.op);
  1890. operand.op := operand.tag;
  1891. IntermediateCode.InitOperand(operand.tag);
  1892. END;
  1893. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1894. result := operand;
  1895. ELSE HALT(100)
  1896. END;
  1897. destination := dest;
  1898. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1899. END VisitUnaryExpression;
  1900. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1901. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1902. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1903. BEGIN
  1904. type := type.resolved;
  1905. originalType := type;
  1906. IF type IS SyntaxTree.PointerType THEN
  1907. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1908. END;
  1909. IF type IS SyntaxTree.ObjectType THEN
  1910. BrL(trueL);
  1911. ELSE
  1912. ASSERT(type IS SyntaxTree.RecordType);
  1913. (*
  1914. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1915. *)
  1916. ReuseCopy(left,tag);
  1917. right := TypeDescriptorAdr(type);
  1918. IF ~newObjectFile THEN
  1919. IntermediateCode.MakeMemory(right,addressType);
  1920. END;
  1921. IF backend.cooperative THEN
  1922. repeatL := NewLabel();
  1923. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1924. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1925. END;
  1926. SetLabel(repeatL);
  1927. BreqL(trueL,left,right);
  1928. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1929. BrneL(repeatL,left,nil);
  1930. ELSIF meta.simple THEN
  1931. level := type(SyntaxTree.RecordType).Level();
  1932. (* get type desc tag of level relative to base tag *)
  1933. offset := (meta.BaseTypesTableOffset + level) * addressType.sizeInBits;
  1934. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1935. IntermediateCode.MakeMemory(left,addressType);
  1936. BreqL(trueL,left,right);
  1937. ELSE
  1938. level := type(SyntaxTree.RecordType).Level();
  1939. (* get type desc tag of level relative to base tag *)
  1940. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1941. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1942. IntermediateCode.MakeMemory(left,addressType);
  1943. BreqL(trueL,left,right);
  1944. END;
  1945. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1946. BrL(falseL);
  1947. END;
  1948. END TypeTest;
  1949. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1950. BEGIN
  1951. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1952. IF dump # NIL THEN
  1953. dump.String(s); dump.Ln;
  1954. END;
  1955. END Error;
  1956. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1957. BEGIN
  1958. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1959. IF dump # NIL THEN
  1960. dump.String(s); dump.Ln; dump.Update;
  1961. END;
  1962. END Warning;
  1963. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1964. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1965. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1966. operand:Operand;
  1967. BEGIN
  1968. previousSection := section;
  1969. Global.GetModuleName(mod,name);
  1970. Strings.Append(name,".@Trace");
  1971. Basic.ToSegmentedName(name, pooledName);
  1972. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1973. IF dump # NIL THEN dump := section.comments END;
  1974. variable := mod.moduleScope.firstVariable;
  1975. WHILE variable # NIL DO
  1976. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1977. Symbol(variable, operand);
  1978. register := operand.op;
  1979. CallTraceMethod(register, variable.type);
  1980. ReleaseIntermediateOperand(register);
  1981. END;
  1982. variable := variable.nextVariable;
  1983. END;
  1984. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1985. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1986. Emit(Br(position,op));
  1987. INC(statCoopTraceModule, section.pc);
  1988. section := previousSection;
  1989. IF dump # NIL THEN dump := section.comments END;
  1990. END CreateTraceModuleMethod;
  1991. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1992. BEGIN
  1993. Emit (Push(position, dst));
  1994. Emit (Push(position, src));
  1995. CallThis(position,"GarbageCollector","Assign", 2);
  1996. END CallAssignPointer;
  1997. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1998. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1999. BEGIN
  2000. IF SemanticChecker.IsPointerType (type) THEN
  2001. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  2002. ELSIF type.IsRecordType() THEN
  2003. Emit (Push(position,dst));
  2004. Emit (Push(position,src));
  2005. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2006. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2007. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2008. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  2009. ELSIF IsStaticArray(type) THEN
  2010. size := StaticArrayNumElements(type);
  2011. base := StaticArrayBaseType(type);
  2012. IF base.IsRecordType() THEN
  2013. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2014. Emit (Push(position, dst));
  2015. Emit (Push(position, src));
  2016. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2017. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2018. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2019. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2020. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2021. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2022. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2023. Emit (Push(position, dst));
  2024. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2025. Emit (Push(position, src));
  2026. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2027. ELSE
  2028. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2029. Emit (Push(position, dst));
  2030. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2031. Emit (Push(position, src));
  2032. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2033. ASSERT(StaticArrayBaseType(type).IsPointer());
  2034. END;
  2035. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2036. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2037. Emit (Push(position, dst));
  2038. Emit (Push(position, src));
  2039. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2040. ELSE HALT(100); (* missing ? *)
  2041. END;
  2042. END CallAssignMethod;
  2043. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2044. VAR name: Basic.SegmentedName;
  2045. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2046. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2047. context: Context;
  2048. BEGIN
  2049. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2050. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2051. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2052. GetRecordTypeName (recordType,name);
  2053. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2054. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2055. IF dump # NIL THEN dump := section.comments END;
  2056. variable := recordType.recordScope.firstVariable;
  2057. WHILE variable # NIL DO
  2058. IF variable.NeedsTrace() THEN
  2059. dst := NewRegisterOperand (addressType);
  2060. src := NewRegisterOperand (addressType);
  2061. Emit (Mov(position, dst, parameter1));
  2062. Emit (Mov(position, src, parameter2));
  2063. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2064. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2065. CallAssignMethod(dst, src, variable.type);
  2066. ReleaseIntermediateOperand(src);
  2067. ReleaseIntermediateOperand(dst);
  2068. END;
  2069. variable := variable.nextVariable;
  2070. END;
  2071. recordBase := recordType.GetBaseRecord();
  2072. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2073. GetRecordTypeName (recordBase,name);
  2074. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2075. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2076. Emit(Br(position,op));
  2077. ELSE
  2078. Emit(Exit(position,0,0));
  2079. END;
  2080. IF ~recordType.isObject THEN
  2081. GetRecordTypeName (recordType,name);
  2082. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2083. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2084. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2085. NEW(registerUsageCount);
  2086. usedRegisters := NIL;
  2087. dst := NewRegisterOperand (addressType);
  2088. src := NewRegisterOperand (addressType);
  2089. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2090. Emit(Mov(position, dst, parameter1));
  2091. Emit(Mov(position, src, parameter2));
  2092. label := NewLabel();
  2093. SetLabel(label);
  2094. Emit(Push(position, dst));
  2095. Emit(Push(position, src));
  2096. GetRecordTypeName (recordType,name);
  2097. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2098. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2099. Emit(Call(position, op, 0));
  2100. Emit(Pop(position, src));
  2101. Emit(Pop(position, dst));
  2102. Emit(Add(position, dst, dst, ofs));
  2103. Emit(Add(position, src, src, ofs));
  2104. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2105. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2106. Emit(Exit(position,0,0));
  2107. END;
  2108. INC(statCoopAssignProcedure, section.pc);
  2109. ReturnToContext(context);
  2110. IF dump # NIL THEN dump := section.comments END;
  2111. END CreateAssignProcedure;
  2112. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2113. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2114. BEGIN
  2115. IF IsUnsafePointer (type) THEN
  2116. skip := NewLabel();
  2117. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2118. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2119. BreqL (skip, op, nil);
  2120. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2121. SetLabel (skip);
  2122. ELSIF SemanticChecker.IsPointerType (type) THEN
  2123. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2124. CallThis(position,"GarbageCollector","Mark", 1);
  2125. ELSIF type.IsRecordType() THEN
  2126. Emit (Push(position,register));
  2127. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2128. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2129. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2130. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2131. ELSIF IsStaticArray(type) THEN
  2132. size := StaticArrayNumElements(type);
  2133. base := StaticArrayBaseType(type);
  2134. IF base.IsRecordType() THEN
  2135. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2136. Emit (Push(position, register));
  2137. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2138. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2139. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2140. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2141. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2142. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2143. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2144. Emit (Push(position, register));
  2145. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2146. ELSE
  2147. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2148. Emit (Push(position, register));
  2149. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2150. ASSERT(base.IsPointer());
  2151. END;
  2152. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2153. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2154. CallThis(position,"GarbageCollector","Mark", 1);
  2155. ELSE HALT(100); (* missing ? *)
  2156. END;
  2157. END CallTraceMethod;
  2158. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2159. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2160. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2161. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2162. BEGIN
  2163. previousSection := section;
  2164. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2165. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2166. GetRecordTypeName (recordType,name);
  2167. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2168. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2169. IF dump # NIL THEN dump := section.comments END;
  2170. variable := recordType.recordScope.firstVariable;
  2171. WHILE variable # NIL DO
  2172. IF variable.NeedsTrace() THEN
  2173. register := NewRegisterOperand (addressType);
  2174. Emit (Mov(position,register,parameter1));
  2175. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2176. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2177. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2178. END;
  2179. CallTraceMethod(register, variable.type);
  2180. ReleaseIntermediateOperand(register);
  2181. END;
  2182. variable := variable.nextVariable;
  2183. END;
  2184. recordBase := recordType.GetBaseRecord();
  2185. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2186. recordBase := recordBase.GetBaseRecord();
  2187. END;
  2188. IF recordBase # NIL THEN
  2189. GetRecordTypeName (recordBase,name);
  2190. IF HasExplicitTraceMethod (recordBase) THEN
  2191. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2192. ELSE
  2193. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2194. END;
  2195. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2196. Emit(Br(position,op));
  2197. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2198. Emit(Exit(position,0,0));
  2199. ELSE
  2200. IF recordType.isObject THEN
  2201. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2202. ELSE
  2203. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2204. END;
  2205. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2206. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2207. Emit(Br(position,op));
  2208. END;
  2209. IF ~recordType.isObject THEN
  2210. GetRecordTypeName (recordType,name);
  2211. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2212. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2213. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2214. NEW(registerUsageCount);
  2215. usedRegisters := NIL;
  2216. IF dump # NIL THEN dump := section.comments END;
  2217. register := NewRegisterOperand (addressType);
  2218. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2219. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2220. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2221. END;
  2222. Emit (Push(position,register));
  2223. GetRecordTypeName (recordType,name);
  2224. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2225. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2226. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2227. ReleaseIntermediateOperand(register);
  2228. Emit(Exit(position,0,0));
  2229. GetRecordTypeName (recordType,name);
  2230. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2231. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2232. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2233. NEW(registerUsageCount);
  2234. usedRegisters := NIL;
  2235. register := NewRegisterOperand (addressType);
  2236. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2237. Emit(Mov(position, register, parameter1));
  2238. label := NewLabel();
  2239. SetLabel(label);
  2240. Emit(Push(position, register));
  2241. GetRecordTypeName (recordType,name);
  2242. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2243. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2244. Emit(Call(position, op, 0));
  2245. Emit(Pop(position, register));
  2246. Emit(Add(position, register, register, ofs));
  2247. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2248. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2249. Emit(Exit(position,0,0));
  2250. END;
  2251. INC(statCoopTraceMethod, section.pc);
  2252. ReturnToContext(context);
  2253. IF dump # NIL THEN dump := section.comments END;
  2254. END CreateTraceMethod;
  2255. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2256. VAR name: Basic.SegmentedName;
  2257. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2258. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2259. context: Context;
  2260. BEGIN
  2261. IF recordType.isObject THEN RETURN END;
  2262. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2263. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2264. GetRecordTypeName (recordType,name);
  2265. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2266. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2267. IF dump # NIL THEN dump := section.comments END;
  2268. variable := recordType.recordScope.firstVariable;
  2269. WHILE variable # NIL DO
  2270. IF variable.NeedsTrace() THEN
  2271. dst := NewRegisterOperand (addressType);
  2272. Emit (Mov(position, dst, parameter1));
  2273. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2274. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2275. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2276. END;
  2277. CallResetProcedure(dst, nil, variable.type);
  2278. ReleaseIntermediateOperand(dst);
  2279. END;
  2280. variable := variable.nextVariable;
  2281. END;
  2282. recordBase := recordType.GetBaseRecord();
  2283. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2284. GetRecordTypeName (recordBase,name);
  2285. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2286. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2287. Emit(Br(position,op));
  2288. ELSE
  2289. Emit(Exit(position,0,0));
  2290. END;
  2291. GetRecordTypeName (recordType,name);
  2292. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2293. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2294. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2295. NEW(registerUsageCount);
  2296. usedRegisters := NIL;
  2297. dst := NewRegisterOperand (addressType);
  2298. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2299. Emit(Mov(position, dst, parameter1));
  2300. label := NewLabel();
  2301. SetLabel(label);
  2302. Emit(Push(position, dst));
  2303. GetRecordTypeName (recordType,name);
  2304. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2305. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2306. Emit(Call(position, op, 0));
  2307. Emit(Pop(position, dst));
  2308. Emit(Add(position, dst, dst, ofs));
  2309. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2310. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2311. Emit(Exit(position,0,0));
  2312. INC(statCoopResetProcedure, section.pc);
  2313. ReturnToContext(context);
  2314. IF dump # NIL THEN dump := section.comments END;
  2315. END CreateResetProcedure;
  2316. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2317. VAR name: Basic.SegmentedName; context: Context;
  2318. BEGIN
  2319. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2320. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2321. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2322. IF dump # NIL THEN dump := section.comments END;
  2323. Emit(Push(position,fp));
  2324. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2325. ResetVariables(scope);
  2326. Emit(Pop(position,fp));
  2327. Emit(Exit(position,0,0));
  2328. ReturnToContext(context);
  2329. IF dump # NIL THEN dump := section.comments END;
  2330. END CreateResetMethod;
  2331. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2332. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2333. BEGIN
  2334. IF SemanticChecker.IsPointerType (type) THEN
  2335. Emit (Push(position, dest));
  2336. CallThis(position,"GarbageCollector","Reset", 1);
  2337. ELSIF type.IsRecordType() THEN
  2338. Emit (Push(position, dest));
  2339. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2340. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2341. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2342. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2343. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2344. size := GetArrayLength(type, tag);
  2345. base := ArrayBaseType(type);
  2346. IF base.IsRecordType() THEN
  2347. Emit (Push(position, size));
  2348. Emit (Push(position, dest));
  2349. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2350. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2351. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2352. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2353. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2354. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2355. Emit (Push(position, size));
  2356. Emit (Push(position, dest));
  2357. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2358. ELSE
  2359. Emit (Push(position, size));
  2360. Emit (Push(position, dest));
  2361. CallThis(position,"GarbageCollector","ResetArray", 2);
  2362. ASSERT(ArrayBaseType(type).IsPointer());
  2363. END;
  2364. ReleaseIntermediateOperand(size);
  2365. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2366. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2367. Emit (Push(position, dest));
  2368. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2369. ELSE HALT(100); (* missing ? *)
  2370. END;
  2371. END CallResetProcedure;
  2372. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2373. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2374. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2375. VAR operand: Operand;
  2376. BEGIN
  2377. Symbol (symbol, operand);
  2378. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2379. ReleaseOperand(operand);
  2380. END Reset;
  2381. BEGIN
  2382. previousScope := currentScope;
  2383. currentScope := scope;
  2384. pc := section.pc;
  2385. variable := scope.firstVariable;
  2386. WHILE variable # NIL DO
  2387. IF variable.NeedsTrace() THEN
  2388. Reset (variable);
  2389. END;
  2390. variable := variable.nextVariable;
  2391. END;
  2392. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2393. WHILE parameter # NIL DO
  2394. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2395. Reset (parameter);
  2396. END;
  2397. parameter := parameter.nextParameter;
  2398. END;
  2399. INC(statCoopResetVariables, section.pc - pc);
  2400. currentScope := previousScope;
  2401. END ResetVariables;
  2402. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2403. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2404. VAR op: IntermediateCode.Operand; context: Context;
  2405. BEGIN
  2406. previousSection := section;
  2407. GetCodeSectionNameForSymbol(procedure, name);
  2408. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2409. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2410. IF dump # NIL THEN dump := section.comments END;
  2411. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2412. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2413. Emit(Data(position,op));
  2414. Emit(Data(position,nil));
  2415. IF HasPointers (procedure.procedureScope) THEN
  2416. GetCodeSectionNameForSymbol(procedure, name);
  2417. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2418. ELSE
  2419. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2420. END;
  2421. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2422. Emit(Data(position,op));
  2423. ReturnToContext(context);
  2424. IF dump # NIL THEN dump := section.comments END;
  2425. END CreateProcedureDescriptor;
  2426. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2427. VAR import: SyntaxTree.Import;
  2428. s: Basic.MessageString;
  2429. selfName: SyntaxTree.IdentifierString;
  2430. BEGIN
  2431. moduleScope.ownerModule.GetName(selfName);
  2432. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2433. module := moduleScope.ownerModule
  2434. ELSE
  2435. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2436. IF import = NIL THEN
  2437. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2438. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2439. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2440. s := "Module ";
  2441. Strings.Append(s,moduleName);
  2442. Strings.Append(s," cannot be imported.");
  2443. IF force THEN
  2444. Error(position,s);
  2445. ELSIF canBeLoaded THEN
  2446. Strings.Append(s, "=> no dynamic linking.");
  2447. Warning(position, s);
  2448. canBeLoaded := FALSE;
  2449. END;
  2450. RETURN FALSE
  2451. ELSE
  2452. SELF.module.imports.AddName(moduleName)
  2453. END;
  2454. ELSIF import.module = NIL THEN (* already tried *)
  2455. RETURN FALSE
  2456. END;
  2457. module := import.module;
  2458. END;
  2459. RETURN TRUE
  2460. END AddImport;
  2461. (* needed for old binary object file format*)
  2462. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2463. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2464. BEGIN
  2465. IF AddImport(moduleName,module,TRUE) THEN
  2466. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2467. IF procedure = NIL THEN
  2468. s := "Instruction not supported on target, emulation procedure ";
  2469. Strings.Append(s,moduleName);
  2470. Strings.Append(s,".");
  2471. Strings.Append(s,procedureName);
  2472. Strings.Append(s," not present");
  2473. Error(position,s);
  2474. ELSE
  2475. StaticCallOperand(r,procedure);
  2476. ReleaseOperand(r);
  2477. fp := GetFingerprint(procedure);
  2478. END;
  2479. END;
  2480. END EnsureSymbol;
  2481. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2482. VAR exit: Label; trueL,falseL: Label;
  2483. BEGIN
  2484. trueL := NewLabel();
  2485. falseL := NewLabel();
  2486. exit := NewLabel();
  2487. Condition(x,trueL,falseL);
  2488. InitOperand(result,ModeValue);
  2489. SetLabel(trueL);
  2490. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2491. Emit(Mov(position,result.op,true));
  2492. BrL(exit);
  2493. SetLabel(falseL);
  2494. Emit(MovReplace(position,result.op,false));
  2495. SetLabel(exit);
  2496. END ConditionToValue;
  2497. PROCEDURE ValueToCondition(VAR op: Operand);
  2498. BEGIN
  2499. LoadValue(op,system.booleanType);
  2500. BrneL(trueLabel,op.op, false);
  2501. ReleaseOperand(op);
  2502. BrL(falseLabel);
  2503. END ValueToCondition;
  2504. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2505. VAR size: LONGINT;
  2506. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2507. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2508. BEGIN
  2509. ASSERT(type.form = SyntaxTree.Open);
  2510. baseType := type.arrayBase.resolved;
  2511. IF IsOpenArray(baseType) THEN
  2512. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2513. ELSE
  2514. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2515. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2516. END;
  2517. len := tag;
  2518. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2519. IntermediateCode.MakeMemory(len,addressType);
  2520. UseIntermediateOperand(len);
  2521. Reuse2(res,sizeOperand,len);
  2522. Emit(Mul(position,res,sizeOperand,len));
  2523. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2524. RETURN res
  2525. END GetArraySize;
  2526. BEGIN
  2527. type := type.resolved;
  2528. IF IsOpenArray(type) THEN
  2529. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2530. RETURN tag
  2531. ELSE
  2532. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2533. END;
  2534. ELSE
  2535. size := ToMemoryUnits(system,system.SizeOf(type));
  2536. RETURN IntermediateCode.Immediate(addressType,size)
  2537. END;
  2538. END GetDynamicSize;
  2539. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2540. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2541. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2542. BEGIN
  2543. ASSERT(type.form = SyntaxTree.Open);
  2544. baseType := type.arrayBase.resolved;
  2545. IF IsOpenArray(baseType) THEN
  2546. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2547. ELSE
  2548. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2549. END;
  2550. len := tag;
  2551. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2552. IntermediateCode.MakeMemory(len,addressType);
  2553. UseIntermediateOperand(len);
  2554. Reuse2(res,sizeOperand,len);
  2555. Emit(Mul(position,res,sizeOperand,len));
  2556. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2557. RETURN res
  2558. END GetLength;
  2559. BEGIN
  2560. type := type.resolved;
  2561. IF IsOpenArray(type) THEN
  2562. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2563. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2564. ELSE
  2565. RETURN IntermediateCode.Immediate(addressType,1)
  2566. END;
  2567. END GetArrayLength;
  2568. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2569. VAR result: IntermediateCode.Operand;
  2570. BEGIN
  2571. IF backend.cooperative THEN
  2572. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2573. ELSE
  2574. MakeMemory(result, tag, addressType, 0);
  2575. END;
  2576. RETURN result
  2577. END GetSizeFromTag;
  2578. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2579. VAR parameter: SyntaxTree.Parameter;
  2580. BEGIN
  2581. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2582. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2583. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2584. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2585. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2586. END;
  2587. RETURN GetDynamicSize(e.type, tag);
  2588. END GetArrayOfBytesSize;
  2589. (*
  2590. to find imported symbol. not needed ?
  2591. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2592. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2593. BEGIN
  2594. IF AddImport(moduleName,importedModule,FALSE) THEN
  2595. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2596. RETURN symbol
  2597. ELSE
  2598. RETURN NIL
  2599. END
  2600. END SymbolByName;
  2601. *)
  2602. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2603. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2604. BEGIN
  2605. IF AddImport(moduleName,runtimeModule,force) THEN
  2606. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2607. IF procedure = NIL THEN
  2608. s := "Procedure ";
  2609. Strings.Append(s,moduleName);
  2610. Strings.Append(s,".");
  2611. Strings.Append(s,procedureName);
  2612. Strings.Append(s," not present");
  2613. Error(position,s);
  2614. RETURN FALSE
  2615. ELSE
  2616. RETURN TRUE
  2617. END;
  2618. ELSE RETURN FALSE
  2619. END;
  2620. END GetRuntimeProcedure;
  2621. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2622. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2623. s: Basic.MessageString;
  2624. BEGIN
  2625. Basic.InitSegmentedName(name);
  2626. name[0] := Basic.MakeString(moduleName);
  2627. name[1] := Basic.MakeString(typeName);
  2628. name[2] := -1;
  2629. IF AddImport(moduleName,importedModule, FALSE) THEN
  2630. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2631. IF symbol = NIL THEN
  2632. s := "type ";
  2633. Strings.Append(s,moduleName);
  2634. Strings.Append(s,".");
  2635. Strings.Append(s,typeName);
  2636. Strings.Append(s," not present");
  2637. Error(position,s);
  2638. END;
  2639. ELSE symbol := NIL;
  2640. END;
  2641. IF importedModule = moduleScope.ownerModule THEN
  2642. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2643. ELSE
  2644. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2645. END;
  2646. RETURN symbol
  2647. END GetTypeDescriptor;
  2648. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2649. PROCEDURE CallThisChecked(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT; checkNumParameters: BOOLEAN);
  2650. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2651. pooledName: Basic.SegmentedName; size: LONGINT;
  2652. BEGIN
  2653. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2654. StaticCallOperand(result,procedure);
  2655. IF numberParameters < 0 THEN
  2656. size := ProcedureParametersSize(system,procedure);
  2657. ELSE
  2658. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2659. IF checkNumParameters & (size # ProcedureParametersSize(system,procedure)) THEN
  2660. Error(position,"runtime call parameter count mismatch");
  2661. END;
  2662. END;
  2663. Emit(Call(position, result.op, size));
  2664. ReleaseOperand(result);
  2665. ELSE (* only static linking possible *)
  2666. ASSERT(numberParameters >= 0);
  2667. Basic.InitSegmentedName(pooledName);
  2668. pooledName[0] := Basic.MakeString(moduleName);
  2669. pooledName[1] := Basic.MakeString(procedureName);
  2670. pooledName[2] := -1;
  2671. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2672. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2673. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2674. END;
  2675. END CallThisChecked;
  2676. (* Call a runtime procedure. If numberParameters >= 0 then the procedure may be called without module import. Otherwise the signature has to be inferred from the import. *)
  2677. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2678. BEGIN
  2679. CallThisChecked(position, moduleName, procedureName, numberParameters,TRUE);
  2680. END CallThis;
  2681. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2682. VAR
  2683. left,right: Operand;
  2684. leftSize, rightSize: IntermediateCode.Operand;
  2685. saved: RegisterEntry;
  2686. reg: IntermediateCode.Operand;
  2687. procedureName: SyntaxTree.IdentifierString;
  2688. BEGIN
  2689. procedureName := "CompareString";
  2690. SaveRegisters();ReleaseUsedRegisters(saved);
  2691. Designate(leftExpression,left);
  2692. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2693. Emit(Push(position,leftSize));
  2694. ReleaseIntermediateOperand(leftSize);
  2695. Emit(Push(position,left.op));
  2696. ReleaseOperand(left);
  2697. Designate(rightExpression,right);
  2698. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2699. Emit(Push(position,rightSize));
  2700. ReleaseIntermediateOperand(rightSize);
  2701. Emit(Push(position,right.op));
  2702. ReleaseOperand(right);
  2703. IF backend.cooperative THEN
  2704. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2705. ELSE
  2706. CallThis(position,runtimeModuleName,procedureName, 4);
  2707. END;
  2708. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2709. Emit(Result(position,reg));
  2710. (*
  2711. AcquireThisRegister(int8,IntermediateCode.Result);
  2712. *)
  2713. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2714. (*
  2715. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2716. *)
  2717. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2718. ReleaseIntermediateOperand(reg);
  2719. BrL(falseLabel);
  2720. END CompareString;
  2721. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2722. VAR
  2723. left,right: Operand;
  2724. leftSize, rightSize: IntermediateCode.Operand;
  2725. saved: RegisterEntry;
  2726. procedureName: SyntaxTree.IdentifierString;
  2727. BEGIN
  2728. procedureName := "CopyString";
  2729. SaveRegisters();ReleaseUsedRegisters(saved);
  2730. Designate(leftExpression,left);
  2731. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2732. Emit(Push(position,leftSize));
  2733. ReleaseIntermediateOperand(leftSize);
  2734. Emit(Push(position,left.op));
  2735. ReleaseOperand(left);
  2736. Designate(rightExpression,right);
  2737. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2738. Emit(Push(position,rightSize));
  2739. ReleaseIntermediateOperand(rightSize);
  2740. Emit(Push(position,right.op));
  2741. ReleaseOperand(right);
  2742. IF backend.cooperative THEN
  2743. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2744. ELSE
  2745. CallThis(position,runtimeModuleName,procedureName,4);
  2746. END;
  2747. RestoreRegisters(saved);
  2748. END CopyString;
  2749. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2750. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2751. leftType,rightType: SyntaxTree.Type;
  2752. leftExpression,rightExpression : SyntaxTree.Expression;
  2753. componentType: IntermediateCode.Type;
  2754. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2755. size: LONGINT;
  2756. BEGIN
  2757. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2758. dest := destination; destination := emptyOperand;
  2759. leftType := x.left.type.resolved;
  2760. rightType := x.right.type.resolved;
  2761. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2762. CASE x.operator OF
  2763. Scanner.Or:
  2764. (* shortcut evaluation of left OR right *)
  2765. IF ~conditional THEN ConditionToValue(x);
  2766. ELSE
  2767. next := NewLabel();
  2768. Condition(x.left,trueLabel,next);
  2769. SetLabel(next);
  2770. Condition(x.right,trueLabel,falseLabel);
  2771. END;
  2772. |Scanner.And:
  2773. (* shortcut evaluation of left & right *)
  2774. IF ~conditional THEN ConditionToValue(x);
  2775. ELSE
  2776. next := NewLabel();
  2777. Condition(x.left,next,falseLabel);
  2778. SetLabel(next);
  2779. Condition(x.right,trueLabel,falseLabel);
  2780. END;
  2781. |Scanner.Is:
  2782. IF ~conditional THEN ConditionToValue(x);
  2783. ELSE
  2784. (* get type desc tag *)
  2785. IF IsPointerToRecord(leftType,recordType) THEN
  2786. Evaluate(x.left,left);
  2787. Dereference(left,recordType,IsUnsafePointer(leftType))
  2788. ELSE
  2789. Designate(x.left,left);
  2790. END;
  2791. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2792. ReleaseOperand(left);
  2793. END;
  2794. |Scanner.Plus:
  2795. Evaluate(x.left,left);
  2796. Evaluate(x.right,right);
  2797. IF leftType IS SyntaxTree.SetType THEN
  2798. InitOperand(result,ModeValue);
  2799. Reuse2a(result.op,left.op,right.op,dest);
  2800. Emit(Or(position,result.op,left.op,right.op));
  2801. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2802. InitOperand(result,ModeValue);
  2803. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2804. Reuse2(result.tag,left.tag,right.tag);
  2805. Emit(Add(position,result.op,left.op,right.op));
  2806. Emit(Add(position,result.tag,left.tag,right.tag))
  2807. ELSE
  2808. InitOperand(result,ModeValue);
  2809. Reuse2a(result.op,left.op,right.op,dest);
  2810. Emit(Add(position,result.op,left.op,right.op));
  2811. END;
  2812. ReleaseOperand(left); ReleaseOperand(right);
  2813. |Scanner.Minus:
  2814. Evaluate(x.left,left);
  2815. Evaluate(x.right,right);
  2816. IF leftType IS SyntaxTree.SetType THEN
  2817. InitOperand(result,ModeValue);
  2818. Reuse1(result.op,right.op);
  2819. Emit(Not(position,result.op,right.op));
  2820. ReleaseOperand(right);
  2821. Emit(And(position,result.op,result.op,left.op));
  2822. ReleaseOperand(left);
  2823. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2824. InitOperand(result,ModeValue);
  2825. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2826. Reuse2(result.tag,left.tag,right.tag);
  2827. Emit(Sub(position,result.op,left.op,right.op));
  2828. Emit(Sub(position,result.tag,left.tag,right.tag));
  2829. ReleaseOperand(left); ReleaseOperand(right)
  2830. ELSE
  2831. InitOperand(result,ModeValue);
  2832. Reuse2a(result.op,left.op,right.op,dest);
  2833. Emit(Sub(position,result.op,left.op,right.op));
  2834. ReleaseOperand(left); ReleaseOperand(right);
  2835. END;
  2836. |Scanner.Times:
  2837. Evaluate(x.left,left);
  2838. Evaluate(x.right,right);
  2839. IF leftType IS SyntaxTree.SetType THEN
  2840. InitOperand(result,ModeValue);
  2841. Reuse2a(result.op,left.op,right.op,dest);
  2842. Emit(And(position,result.op,left.op,right.op));
  2843. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2844. InitOperand(result,ModeValue);
  2845. Reuse1a(result.op,left.op,dest);
  2846. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2847. Emit(Shl(position,result.op,left.op,right.op));
  2848. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2849. InitOperand(result, ModeValue);
  2850. componentType := left.op.type;
  2851. (* TODO: review this *)
  2852. (*
  2853. result.op = left.op * right.op - left.tag * right.tag
  2854. result.tag = left.tag * right.op + left.op * right.tag
  2855. *)
  2856. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2857. Emit(Mul(position,result.op, left.op, right.op));
  2858. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2859. Emit(Mul(position,tempReg, left.tag, right.tag));
  2860. Emit(Sub(position,result.op, result.op, tempReg));
  2861. Reuse2(result.tag, left.tag, right.op);
  2862. Emit(Mul(position,result.tag, left.tag, right.op));
  2863. Emit(Mul(position,tempReg, left.op, right.tag));
  2864. Emit(Add(position,result.tag, result.tag, tempReg));
  2865. ReleaseIntermediateOperand(tempReg)
  2866. ELSE
  2867. InitOperand(result,ModeValue);
  2868. Reuse2a(result.op,left.op,right.op,dest);
  2869. Emit(Mul(position,result.op,left.op,right.op));
  2870. END;
  2871. ReleaseOperand(left); ReleaseOperand(right);
  2872. |Scanner.Div:
  2873. Evaluate(x.left,left);
  2874. Evaluate(x.right,right);
  2875. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2876. InitOperand(result,ModeValue);
  2877. Reuse1a(result.op,left.op,dest);
  2878. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2879. Emit(Shr(position,result.op,left.op,right.op));
  2880. ELSE
  2881. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2882. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2883. IF ~isUnchecked THEN
  2884. exit := NewLabel();
  2885. BrltL(exit,zero,right.op);
  2886. EmitTrap(position,NegativeDivisorTrap);
  2887. SetLabel(exit);
  2888. END;
  2889. END;
  2890. InitOperand(result,ModeValue);
  2891. Reuse2a(result.op,left.op,right.op,dest);
  2892. Emit(Div(position,result.op,left.op,right.op));
  2893. END;
  2894. ReleaseOperand(left); ReleaseOperand(right);
  2895. |Scanner.Mod:
  2896. Evaluate(x.left,left);
  2897. Evaluate(x.right,right);
  2898. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2899. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2900. InitOperand(result,ModeValue);
  2901. Reuse1a(result.op,left.op,dest);
  2902. Emit(And(position,result.op,left.op,right.op));
  2903. ELSE
  2904. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2905. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2906. IF ~isUnchecked THEN
  2907. exit := NewLabel();
  2908. BrltL(exit,zero,right.op);
  2909. EmitTrap(position,NegativeDivisorTrap);
  2910. SetLabel(exit);
  2911. END;
  2912. END;
  2913. InitOperand(result,ModeValue);
  2914. Reuse2a(result.op,left.op,right.op,dest);
  2915. Emit(Mod(position,result.op,left.op,right.op));
  2916. END;
  2917. ReleaseOperand(left); ReleaseOperand(right);
  2918. |Scanner.Slash:
  2919. Evaluate(x.left,left);
  2920. Evaluate(x.right,right);
  2921. IF leftType IS SyntaxTree.SetType THEN
  2922. InitOperand(result,ModeValue);
  2923. Reuse2a(result.op,left.op,right.op,dest);
  2924. Emit(Xor(position,result.op,left.op,right.op));
  2925. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2926. InitOperand(result,ModeValue);
  2927. componentType := left.op.type;
  2928. (* review this *)
  2929. (*
  2930. divisor = right.op * right.op + right.tag * right.tag
  2931. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2932. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2933. *)
  2934. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2935. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2936. Emit(Mul(position,tempReg, right.op, right.op));
  2937. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2938. Emit(Add(position,tempReg, tempReg, tempReg2));
  2939. result.op := tempReg2;
  2940. Emit(Mul(position,result.op, left.op, right.op));
  2941. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2942. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2943. Emit(Add(position,result.op, result.op, tempReg2));
  2944. Emit(Div(position,result.op, result.op, tempReg));
  2945. Reuse2(result.tag, left.tag, right.op);
  2946. Emit(Mul(position,result.tag, left.tag, right.op));
  2947. Emit(Mul(position,tempReg2, left.op, right.tag));
  2948. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2949. Emit(Div(position,result.tag, result.tag, tempReg));
  2950. ReleaseIntermediateOperand(tempReg);
  2951. ReleaseIntermediateOperand(tempReg2)
  2952. ELSE
  2953. InitOperand(result,ModeValue);
  2954. Reuse2a(result.op,left.op,right.op,dest);
  2955. Emit(Div(position,result.op,left.op,right.op));
  2956. END;
  2957. ReleaseOperand(left); ReleaseOperand(right);
  2958. |Scanner.Equal:
  2959. IF ~conditional THEN ConditionToValue(x);
  2960. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2961. CompareString(BreqL,x.left,x.right);
  2962. ELSE
  2963. Evaluate(x.left,left);
  2964. Evaluate(x.right,right);
  2965. IF leftType IS SyntaxTree.RangeType THEN
  2966. ASSERT(rightType IS SyntaxTree.RangeType);
  2967. BrneL(falseLabel, left.op, right.op); (* first *)
  2968. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2969. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2970. ReleaseOperand(left); ReleaseOperand(right);
  2971. BrL(trueLabel)
  2972. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2973. BrneL(falseLabel, left.op, right.op); (* first *)
  2974. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2975. ReleaseOperand(left); ReleaseOperand(right);
  2976. BrL(trueLabel)
  2977. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2978. (* TODO: review this *)
  2979. BrneL(falseLabel, left.op, right.op); (* real part *)
  2980. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2981. ReleaseOperand(left); ReleaseOperand(right);
  2982. BrL(trueLabel)
  2983. ELSE
  2984. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2985. ReleaseOperand(left); ReleaseOperand(right);
  2986. BrL(trueLabel);
  2987. END;
  2988. END;
  2989. |Scanner.LessEqual:
  2990. IF ~conditional THEN ConditionToValue(x);
  2991. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2992. CompareString(BrgeL,x.right,x.left);
  2993. ELSE
  2994. Evaluate(x.left,left);
  2995. Evaluate(x.right,right);
  2996. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2997. Reuse1(temp.op,right.op);
  2998. Emit(And(position,temp.op,left.op,right.op));
  2999. ReleaseOperand(right);
  3000. BreqL(trueLabel,temp.op,left.op);
  3001. BrL(falseLabel);
  3002. ReleaseOperand(temp);ReleaseOperand(left);
  3003. ELSE
  3004. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3005. ReleaseOperand(left); ReleaseOperand(right);
  3006. BrL(trueLabel);
  3007. END;
  3008. END;
  3009. |Scanner.Less:
  3010. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  3011. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  3012. leftExpression.SetType(system.booleanType);
  3013. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3014. rightExpression.SetType(system.booleanType);
  3015. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3016. leftExpression.SetType(system.booleanType);
  3017. Expression(leftExpression);
  3018. ELSIF ~conditional THEN ConditionToValue(x);
  3019. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3020. CompareString(BrltL,x.left,x.right);
  3021. ELSE
  3022. Evaluate(x.left,left);
  3023. Evaluate(x.right,right);
  3024. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3025. ReleaseOperand(left); ReleaseOperand(right);
  3026. BrL(trueLabel);
  3027. END;
  3028. |Scanner.Greater:
  3029. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3030. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  3031. leftExpression.SetType(system.booleanType);
  3032. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3033. rightExpression.SetType(system.booleanType);
  3034. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3035. leftExpression.SetType(system.booleanType);
  3036. Expression(leftExpression);
  3037. ELSIF ~conditional THEN ConditionToValue(x);
  3038. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3039. CompareString(BrltL,x.right,x.left);
  3040. ELSE
  3041. Evaluate(x.left,left);
  3042. Evaluate(x.right,right);
  3043. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3044. ReleaseOperand(left); ReleaseOperand(right);
  3045. BrL(trueLabel);
  3046. END;
  3047. |Scanner.GreaterEqual:
  3048. IF ~conditional THEN ConditionToValue(x);
  3049. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3050. CompareString(BrgeL,x.left,x.right);
  3051. ELSE
  3052. Evaluate(x.left,left);
  3053. Evaluate(x.right,right);
  3054. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3055. Reuse1(temp.op,left.op);
  3056. Emit(And(position,temp.op,left.op,right.op));
  3057. ReleaseOperand(left);
  3058. BreqL(trueLabel, temp.op,right.op);
  3059. ReleaseOperand(temp); ReleaseOperand(right);
  3060. BrL(falseLabel);
  3061. ELSE
  3062. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3063. ReleaseOperand(left); ReleaseOperand(right);
  3064. BrL(trueLabel);
  3065. END;
  3066. END;
  3067. |Scanner.Unequal:
  3068. IF ~conditional THEN ConditionToValue(x);
  3069. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3070. CompareString(BrneL,x.left,x.right);
  3071. ELSE
  3072. Evaluate(x.left,left);
  3073. Evaluate(x.right,right);
  3074. IF leftType IS SyntaxTree.RangeType THEN
  3075. ASSERT(rightType IS SyntaxTree.RangeType);
  3076. BrneL(trueLabel, left.op, right.op); (* first *)
  3077. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3078. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3079. ReleaseOperand(left); ReleaseOperand(right);
  3080. BrL(falseLabel)
  3081. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3082. BrneL(trueLabel, left.op, right.op); (* first *)
  3083. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3084. ReleaseOperand(left); ReleaseOperand(right);
  3085. BrL(falseLabel)
  3086. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3087. (* TODO: review this *)
  3088. BrneL(trueLabel, left.op, right.op); (* real part *)
  3089. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3090. ReleaseOperand(left); ReleaseOperand(right);
  3091. BrL(falseLabel)
  3092. ELSE
  3093. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3094. ReleaseOperand(left); ReleaseOperand(right);
  3095. BrL(trueLabel);
  3096. END;
  3097. END;
  3098. |Scanner.In:
  3099. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3100. Evaluate(x.left,left);
  3101. Evaluate(x.right,right);
  3102. Convert(left.op,setType);
  3103. ReuseCopy(temp.op,right.op);
  3104. Emit(Shr(position,temp.op,temp.op,left.op));
  3105. ReleaseOperand(right); ReleaseOperand(left);
  3106. IntermediateCode.InitImmediate(one,setType,1);
  3107. Emit(And(position,temp.op,temp.op,one));
  3108. IF conditional THEN
  3109. IntermediateCode.InitImmediate(zero,setType,0);
  3110. BrneL(trueLabel,temp.op,zero);
  3111. ReleaseOperand(temp);
  3112. BrL(falseLabel);
  3113. ELSE
  3114. Convert(temp.op,bool);
  3115. result.mode := ModeValue;
  3116. result.op := temp.op;
  3117. result.tag := nil; (* may be left over from calls to evaluate *)
  3118. END;
  3119. ELSE
  3120. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3121. IF x.operator = Scanner.Questionmarks THEN
  3122. leftExpression := x.left;
  3123. rightExpression := x.right;
  3124. ELSE
  3125. leftExpression := x.right;
  3126. rightExpression := x.left;
  3127. END;
  3128. Evaluate(leftExpression, left);
  3129. Emit(Push(position,left.op));
  3130. ReleaseOperand(left);
  3131. Designate(rightExpression, right);
  3132. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3133. IF ~backend.cellsAreObjects THEN
  3134. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3135. END;
  3136. Emit(Push(position,right.op));
  3137. ReleaseOperand(right);
  3138. IF backend.cellsAreObjects THEN
  3139. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3140. ELSE
  3141. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3142. END;
  3143. InitOperand(result, ModeValue);
  3144. result.op := NewRegisterOperand(bool);
  3145. Emit(Result(position,result.op));
  3146. IF conditional THEN
  3147. IntermediateCode.InitImmediate(zero,setType,0);
  3148. BrneL(trueLabel,result.op,zero);
  3149. ReleaseOperand(result);
  3150. BrL(falseLabel);
  3151. END;
  3152. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3153. leftExpression := x.left;
  3154. rightExpression := x.right;
  3155. Evaluate(leftExpression, left);
  3156. Emit(Push(position,left.op));
  3157. ReleaseOperand(left);
  3158. Evaluate(rightExpression, right);
  3159. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3160. IF ~backend.cellsAreObjects THEN
  3161. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3162. END;
  3163. Emit(Push(position,right.op));
  3164. ReleaseOperand(right);
  3165. IF backend.cellsAreObjects THEN
  3166. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3167. ELSE
  3168. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3169. END;
  3170. InitOperand(result, ModeValue);
  3171. result.op := NewRegisterOperand(bool);
  3172. Emit(Result(position,result.op));
  3173. IF conditional THEN
  3174. IntermediateCode.InitImmediate(zero,setType,0);
  3175. BrneL(trueLabel,result.op,zero);
  3176. ReleaseOperand(result);
  3177. BrL(falseLabel);
  3178. END;
  3179. ELSE
  3180. HALT(100);
  3181. END;
  3182. END;
  3183. destination := dest;
  3184. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3185. END VisitBinaryExpression;
  3186. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3187. VAR localResult, operand: Operand;
  3188. BEGIN
  3189. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3190. InitOperand(localResult, ModeValue);
  3191. ASSERT(x.first # NIL);
  3192. Evaluate(x.first, operand);
  3193. localResult.op := operand.op;
  3194. ReleaseOperand(operand);
  3195. UseIntermediateOperand(localResult.op);
  3196. ASSERT(x.last # NIL);
  3197. Evaluate(x.last, operand);
  3198. localResult.tag := operand.op;
  3199. ReleaseOperand(operand);
  3200. UseIntermediateOperand(localResult.tag);
  3201. IF x.step # NIL THEN
  3202. Evaluate(x.step, operand);
  3203. localResult.extra := operand.op;
  3204. ReleaseOperand(operand);
  3205. UseIntermediateOperand(localResult.extra);
  3206. END;
  3207. result := localResult;
  3208. IF Trace THEN TraceExit("VisitRangeExpression") END
  3209. END VisitRangeExpression;
  3210. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3211. BEGIN
  3212. HALT(100); (* should never be evaluated *)
  3213. END VisitTensorRangeExpression;
  3214. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3215. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3216. BEGIN
  3217. IF Trace THEN TraceEnter("VisitConversion") END;
  3218. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3219. dest := destination; destination := emptyOperand;
  3220. Evaluate(x.expression,old);
  3221. InitOperand(result,ModeValue);
  3222. result.op := old.op;
  3223. ASSERT(result.op.mode # 0);
  3224. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3225. (* convert TO a complex number *)
  3226. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3227. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3228. ASSERT(result.op.mode # 0);
  3229. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3230. (* convert FROM a complex number TO a complex number*)
  3231. result.tag := old.tag;
  3232. ASSERT(result.tag.mode # 0);
  3233. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3234. ASSERT(result.tag.mode # 0)
  3235. ELSE
  3236. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3237. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3238. END
  3239. ELSE
  3240. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3241. ASSERT(result.op.mode # 0);
  3242. result.tag := old.tag; (*! probably never used *)
  3243. END;
  3244. destination := dest;
  3245. IF Trace THEN TraceExit("VisitConversion") END;
  3246. END VisitConversion;
  3247. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3248. BEGIN
  3249. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3250. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3251. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3252. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3253. END VisitTypeDeclaration;
  3254. (** designators (expressions) *)
  3255. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3256. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3257. BEGIN
  3258. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3259. IF x.left # NIL THEN Expression(x.left) END;
  3260. Symbol(x.symbol,result);
  3261. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3262. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3263. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3264. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3265. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3266. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3267. END;
  3268. END;
  3269. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3270. END VisitSymbolDesignator;
  3271. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3272. BEGIN
  3273. IF isUnchecked THEN RETURN END;
  3274. IF tagsAvailable THEN
  3275. TrapC(BrltL,index,length,IndexCheckTrap);
  3276. END;
  3277. END BoundCheck;
  3278. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3279. VAR d: IntermediateCode.Operand;
  3280. BEGIN
  3281. IF isUnchecked THEN RETURN END;
  3282. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3283. TrapC(op,dim,d,ArraySizeTrap);
  3284. ReleaseIntermediateOperand(d);
  3285. END DimensionCheck;
  3286. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3287. VAR
  3288. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3289. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3290. expression: SyntaxTree.Expression;
  3291. resultingType, leftType, baseType: SyntaxTree.Type;
  3292. skipLabel1: Label;
  3293. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3294. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3295. variableOp: Operand;
  3296. variable: SyntaxTree.Variable;
  3297. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3298. VAR expression: SyntaxTree.Expression;
  3299. BEGIN
  3300. (* count the number of indices, ranges and tensorRanges in the index list *)
  3301. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3302. FOR i := 0 TO parameters.Length() - 1 DO
  3303. expression := parameters.GetExpression(i);
  3304. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3305. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3306. ELSE INC(indexCount)
  3307. END
  3308. END;
  3309. END CountIndices;
  3310. BEGIN
  3311. ASSERT(tagsAvailable);
  3312. resultingType := x.type.resolved; (* resulting type *)
  3313. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3314. InitOperand(localResult, ModeReference);
  3315. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3316. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3317. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3318. (* a globally defined array destination tag is available -> use and invalidate it*)
  3319. localResult.tag := arrayDestinationTag;
  3320. IntermediateCode.InitOperand(arrayDestinationTag)
  3321. ELSE
  3322. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3323. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3324. Symbol(variable, variableOp);
  3325. ReuseCopy(localResult.tag, variableOp.op);
  3326. ReleaseOperand(variableOp);
  3327. END
  3328. END;
  3329. indexListSize := x.parameters.Length();
  3330. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3331. ASSERT(tensorRangeCount <= 1);
  3332. (* designate the array to be indexed over, perform tensor range check if known *)
  3333. Designate(x.left, array);
  3334. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3335. Dereference(array, leftType,FALSE);
  3336. IF tensorRangeCount=0 THEN
  3337. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3338. END
  3339. END;
  3340. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3341. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3342. srcDimOffset := -indexListSize;
  3343. destDimOffset := -rangeCount
  3344. ELSE
  3345. srcDimOffset := 0;
  3346. destDimOffset := 0
  3347. END;
  3348. indexDim := 0;
  3349. (* use address of source array as basis *)
  3350. (*
  3351. ReuseCopy(localResult.op, array.op);
  3352. *)
  3353. localResult.op := array.op;
  3354. UseIntermediateOperand(localResult.op);
  3355. (* go through the index list *)
  3356. FOR i := 0 TO indexListSize - 1 DO
  3357. expression := x.parameters.GetExpression(i);
  3358. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3359. (* nothing to do *)
  3360. ELSE
  3361. (* determine which dimension of source array is currently looked at *)
  3362. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3363. (* get the memory operand pointing to array descriptor dimension *)
  3364. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3365. (* make a reusable register from it *)
  3366. ReuseCopy(srcDim, tmp);
  3367. ReleaseIntermediateOperand(tmp);
  3368. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3369. ELSE
  3370. srcDim := IntermediateCode.Immediate(int32, i)
  3371. END;
  3372. (* get length and increment of source array for current dimension *)
  3373. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3374. Convert(sourceLength.op, sizeType);
  3375. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3376. Convert(sourceIncrement.op, sizeType);
  3377. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3378. ReleaseIntermediateOperand(srcDim);
  3379. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3380. (* SINGLE INDEX *)
  3381. Evaluate(expression, index);
  3382. ReleaseIntermediateOperand(index.tag);
  3383. index.tag := emptyOperand;
  3384. Convert(index.op, sizeType);
  3385. (* lower bound check *)
  3386. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3387. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3388. ELSIF isUnchecked THEN (* do nothing *)
  3389. ELSE
  3390. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3391. END;
  3392. (* upper bound check *)
  3393. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3394. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3395. ELSIF isUnchecked THEN (* do nothing *)
  3396. ELSE
  3397. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3398. END;
  3399. ReleaseOperand(sourceLength);
  3400. Convert(index.op, addressType);
  3401. summand := index.op;
  3402. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3403. (* RANGE OF INDICES *)
  3404. Evaluate(expression, range);
  3405. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3406. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3407. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3408. ReleaseOperand(range);
  3409. Convert(firstIndex, sizeType);
  3410. Convert(lastIndex, sizeType);
  3411. Convert(stepSize, sizeType);
  3412. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3413. if it is 'MAX(LONGINT)' *)
  3414. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3415. TransferToRegister(lastIndex, lastIndex);
  3416. skipLabel1 := NewLabel();
  3417. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3418. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3419. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3420. SetLabel(skipLabel1)
  3421. END;
  3422. (* check if step size is valid *)
  3423. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3424. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3425. ELSIF isUnchecked THEN (* do nothing *)
  3426. ELSE
  3427. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3428. END;
  3429. (* check lower bound check *)
  3430. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3431. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3432. ELSIF isUnchecked THEN (* do nothing *)
  3433. ELSE
  3434. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3435. END;
  3436. (* check upper bound check *)
  3437. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3438. (* statically open range: nothing to do *)
  3439. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3440. ASSERT(staticLastIndex < staticSourceLength)
  3441. ELSIF isUnchecked THEN (* do nothing *)
  3442. ELSE
  3443. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3444. END;
  3445. (* determine length of target array for current dimension *)
  3446. (* 1. incorporate last index: *)
  3447. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3448. (* last index is static *)
  3449. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3450. targetLength := sourceLength.op
  3451. ELSE
  3452. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3453. END;
  3454. UseIntermediateOperand(targetLength);
  3455. ELSE
  3456. (* targetLength := lastIndex + 1
  3457. Reuse1(targetLength, lastIndex);
  3458. *)
  3459. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3460. END;
  3461. ReleaseOperand(sourceLength);
  3462. ReleaseIntermediateOperand(lastIndex);
  3463. (* 2. incorporate first index: *)
  3464. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3465. (* first index and current target length are static *)
  3466. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3467. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3468. (* first index = 0: nothing to do *)
  3469. ELSE
  3470. (* targetLength := targetLength - firstIndex *)
  3471. TransferToRegister(targetLength, targetLength);
  3472. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3473. END;
  3474. (* clip negative lengths to 0 *)
  3475. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3476. IF staticTargetLength < 0 THEN
  3477. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3478. END
  3479. ELSE
  3480. skipLabel1 := NewLabel();
  3481. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3482. TransferToRegister(targetLength, targetLength);
  3483. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3484. SetLabel(skipLabel1)
  3485. END;
  3486. (* 3. incorporate index step size: *)
  3487. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3488. (*step size and current target length are static *)
  3489. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3490. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3491. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3492. (* step size = 1: nothing to do *)
  3493. ELSE
  3494. (* emit code for this:
  3495. targetLength := (targetLength-1) DIV stepSize +1;
  3496. *)
  3497. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3498. DivInt(targetLength, targetLength, stepSize);
  3499. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3500. END;
  3501. (* determine increment of target array for current dimension *)
  3502. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3503. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3504. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3505. (* step size = 1 *)
  3506. targetIncrement := sourceIncrement.op;
  3507. UseIntermediateOperand(targetIncrement)
  3508. ELSE
  3509. (* targetIncrement := sourceIncrement * stepSize *)
  3510. Reuse1(targetIncrement, stepSize);
  3511. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3512. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3513. END;
  3514. ReleaseIntermediateOperand(stepSize);
  3515. (* write length and increment of target array to descriptor *)
  3516. IF destDimOffset < 0 THEN
  3517. (* determine which dimension of target array is currently looked at *)
  3518. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3519. TransferToRegister(destDim, tmp);
  3520. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3521. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3522. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3523. ReleaseIntermediateOperand(destDim)
  3524. ELSE
  3525. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3526. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3527. END;
  3528. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3529. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3530. INC(indexDim);
  3531. Convert(firstIndex, addressType);
  3532. TransferToRegister(summand, firstIndex);
  3533. ELSE HALT(100);
  3534. END;
  3535. (*
  3536. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3537. *)
  3538. Convert(sourceIncrement.op, addressType);
  3539. Convert(summand, addressType);
  3540. MulInt(summand, summand, sourceIncrement.op);
  3541. ReleaseIntermediateOperand(sourceIncrement.op);
  3542. AddInt(localResult.op, localResult.op, summand);
  3543. ReleaseIntermediateOperand(summand);
  3544. END
  3545. END;
  3546. result := localResult;
  3547. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3548. ReleaseIntermediateOperand(result.tag);
  3549. result.tag := TypeDescriptorAdr(resultingType);
  3550. IF ~newObjectFile THEN
  3551. IntermediateCode.MakeMemory(result.tag,addressType);
  3552. END;
  3553. ELSIF IsDelegate(resultingType) THEN
  3554. ReleaseIntermediateOperand(result.tag);
  3555. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3556. UseIntermediateOperand(result.tag);
  3557. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3558. ReleaseIntermediateOperand(result.tag);
  3559. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3560. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3561. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3562. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3563. (* finalize target array descriptor *)
  3564. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3565. (* write lengths and increments of target array for remaining dimensions *)
  3566. i := indexListSize;
  3567. WHILE indexDim < targetArrayDimensionality DO
  3568. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3569. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3570. ReleaseOperand(sourceLength);
  3571. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3572. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3573. ReleaseOperand(sourceIncrement);
  3574. INC(i); INC(indexDim);
  3575. END;
  3576. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3577. tmp := nil;
  3578. ELSE
  3579. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3580. END;
  3581. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3582. ReleaseIntermediateOperand(tmp);
  3583. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3584. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3585. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3586. ELSE
  3587. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3588. END;
  3589. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3590. ReleaseIntermediateOperand(tmp);
  3591. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3592. (* write dimensionality *)
  3593. IF targetArrayDimensionality # 0 THEN
  3594. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3595. END;
  3596. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3597. END;
  3598. ReleaseOperand(array);
  3599. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3600. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3601. END;
  3602. END MathIndexDesignator;
  3603. (* TENTATIVE *)
  3604. PROCEDURE DumpOperand(operand: Operand);
  3605. BEGIN
  3606. D.Log.String(" op = ");
  3607. IntermediateCode.DumpOperand(D.Log, operand.op );
  3608. D.Log.Ln;
  3609. D.Log.String(" tag = ");
  3610. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3611. D.Log.Ln;
  3612. D.Log.String(" extra = ");
  3613. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3614. D.Log.Ln;
  3615. D.Log.Update
  3616. END DumpOperand;
  3617. (* get the length of an array , trying to make use of static information *)
  3618. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3619. VAR res: IntermediateCode.Operand; size: LONGINT;
  3620. BEGIN
  3621. type := type.resolved;
  3622. IF type IS SyntaxTree.ArrayType THEN
  3623. WITH type: SyntaxTree.ArrayType DO
  3624. IF type.form = SyntaxTree.Static THEN
  3625. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3626. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3627. Evaluate(type.length, op);
  3628. ReleaseIntermediateOperand(op.tag);
  3629. RETURN op.op;*)
  3630. ELSE
  3631. res := tag;
  3632. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3633. IntermediateCode.MakeMemory(res,addressType);
  3634. UseIntermediateOperand(res);
  3635. RETURN res
  3636. END
  3637. END;
  3638. ELSE
  3639. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3640. RETURN IntermediateCode.Immediate(addressType,size);
  3641. END;
  3642. END ArrayLength;
  3643. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3644. BEGIN
  3645. ReleaseIntermediateOperand(res);
  3646. IF IsImmediate(x) & IsImmediate(y) THEN
  3647. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3648. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3649. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3650. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3651. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3652. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3653. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3654. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3655. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3656. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3657. UseIntermediateOperand(res);
  3658. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3659. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3660. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3661. UseIntermediateOperand(res);
  3662. ELSE
  3663. IF ~ReusableRegister(res) THEN
  3664. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3665. ELSE
  3666. UseIntermediateOperand(res);
  3667. END;
  3668. IF IsImmediate(x) & (x.intValue = 0) THEN
  3669. Emit(Mov(position,res,y))
  3670. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3671. Emit(Mov(position,res,x))
  3672. ELSE
  3673. Emit(Add(position,res, x, y));
  3674. END;
  3675. END;
  3676. END AddInt;
  3677. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3678. BEGIN
  3679. ReleaseIntermediateOperand(res);
  3680. IF IsImmediate(x) & IsImmediate(y) THEN
  3681. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3682. ELSE
  3683. IF ~ReusableRegister(res) THEN
  3684. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3685. ELSE UseIntermediateOperand(res);
  3686. END;
  3687. IF IsImmediate(x) & (x.intValue = 1) THEN
  3688. Emit(Mov(position,res,y))
  3689. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3690. Emit(Mov(position,res,x))
  3691. ELSE
  3692. Emit(Mul(position,res, x, y));
  3693. END;
  3694. END;
  3695. END MulInt;
  3696. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3697. BEGIN
  3698. ReleaseIntermediateOperand(res);
  3699. IF IsImmediate(x) & IsImmediate(y) THEN
  3700. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3701. ELSE
  3702. IF ~ReusableRegister(res) THEN
  3703. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3704. ELSE UseIntermediateOperand(res);
  3705. END;
  3706. IF IsImmediate(x) & (x.intValue = 1) THEN
  3707. Emit(Mov(position,res,y))
  3708. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3709. Emit(Mov(position,res,x))
  3710. ELSE
  3711. Emit(Div(position,res, x, y));
  3712. END;
  3713. END;
  3714. END DivInt;
  3715. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3716. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3717. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3718. BEGIN
  3719. type := x.left.type.resolved;
  3720. IF type IS SyntaxTree.StringType THEN
  3721. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3722. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3723. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3724. type := atype;
  3725. x.left.SetType(type);
  3726. END;
  3727. IntermediateCode.InitImmediate(res,addressType,0);
  3728. maxDim := x.parameters.Length()-1;
  3729. FOR i := 0 TO maxDim DO
  3730. e := x.parameters.GetExpression(i);
  3731. Evaluate(e,index);
  3732. Convert(index.op,addressType);
  3733. AddInt(res, res, index.op);
  3734. IF i = 0 THEN
  3735. (*
  3736. ReuseCopy(res, index.op);
  3737. *)
  3738. Designate(x.left,array);
  3739. type := x.left.type.resolved;
  3740. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3741. Dereference(array, type, FALSE);
  3742. END;
  3743. (*
  3744. ELSE AddInt(res, res, index.op);
  3745. *)
  3746. END;
  3747. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3748. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3749. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3750. BoundCheck(index.op, length);
  3751. END;
  3752. ReleaseIntermediateOperand(length);
  3753. END;
  3754. ReleaseOperand(index);
  3755. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3756. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3757. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3758. MulInt(res,res,length);
  3759. END;
  3760. ReleaseIntermediateOperand(length);
  3761. END;
  3762. IF (type IS SyntaxTree.ArrayType) THEN
  3763. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3764. size := StaticSize(system, type);
  3765. IF size # 1 THEN
  3766. length := IntermediateCode.Immediate(addressType,size);
  3767. MulInt(res,res,length);
  3768. END;
  3769. ELSE
  3770. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3771. IF size # 1 THEN
  3772. length := IntermediateCode.Immediate(addressType,size);
  3773. MulInt(res,res,length);
  3774. END;
  3775. END;
  3776. END;
  3777. AddInt(res,res,array.op);
  3778. InitOperand(result,ModeReference);
  3779. result.op := res;
  3780. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3781. ReleaseIntermediateOperand(result.tag);
  3782. result.tag := TypeDescriptorAdr(type);
  3783. IF ~newObjectFile THEN
  3784. IntermediateCode.MakeMemory(result.tag,addressType);
  3785. END
  3786. ELSIF IsDelegate(type) THEN
  3787. ReleaseIntermediateOperand(result.tag);
  3788. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3789. UseIntermediateOperand(result.tag);
  3790. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3791. ReleaseIntermediateOperand(result.tag);
  3792. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3793. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3794. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3795. END;
  3796. ReleaseOperand(array);
  3797. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3798. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3799. END;
  3800. END IndexDesignator;
  3801. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3802. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3803. BEGIN
  3804. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3805. dest := destination; destination := emptyOperand;
  3806. type := x.left.type.resolved;
  3807. IF type IS SyntaxTree.MathArrayType THEN
  3808. MathIndexDesignator(x);
  3809. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3810. IndexDesignator(x);
  3811. END;
  3812. destination := dest;
  3813. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3814. END VisitIndexDesignator;
  3815. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3816. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3817. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3818. procedure: SyntaxTree.Procedure;
  3819. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3820. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3821. BEGIN
  3822. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3823. parameters := expression.parameters;
  3824. moduleName := "FoxArrayBase";
  3825. procedureName := "CopyDescriptor";
  3826. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3827. SaveRegisters();ReleaseUsedRegisters(saved);
  3828. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3829. IF procedure = NIL THEN
  3830. s := "procedure ";
  3831. Strings.Append(s,moduleName);
  3832. Strings.Append(s,".");
  3833. Strings.Append(s,procedureName);
  3834. Strings.Append(s," not present");
  3835. Error(position,s);
  3836. ELSE
  3837. (* push address of temporary variable *)
  3838. Symbol(variable,destOperand);
  3839. Emit(Push(position,destOperand.op));
  3840. ReleaseOperand(destOperand);
  3841. (* push src *)
  3842. Evaluate(expression.left,srcOperand);
  3843. (*
  3844. Dereference(srcOperand,expression.type.resolved);
  3845. Emit(Push(position,srcOperand.tag));
  3846. *)
  3847. Emit(Push(position,srcOperand.op));
  3848. ReleaseOperand(srcOperand);
  3849. tensorFound := FALSE;
  3850. FOR i := 0 TO parameters.Length()-1 DO
  3851. e := parameters.GetExpression(i);
  3852. IF e IS SyntaxTree.TensorRangeExpression THEN
  3853. tensorFound := TRUE;
  3854. ELSIF e IS SyntaxTree.RangeExpression THEN
  3855. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3856. ELSE
  3857. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3858. END;
  3859. END;
  3860. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3861. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3862. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3863. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3864. StaticCallOperand(procOp,procedure);
  3865. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3866. ReleaseOperand(procOp);
  3867. END;
  3868. RestoreRegisters(saved);
  3869. END;
  3870. RETURN variable
  3871. END PrepareTensorDescriptor;
  3872. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3873. VAR
  3874. type, descriptorType, baseType: SyntaxTree.Type;
  3875. operand, tmpOperand, variableOp, variable2Op: Operand;
  3876. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3877. variable, variable2: SyntaxTree.Variable;
  3878. dim, i, size: LONGINT;
  3879. (* TODO: needed? *)
  3880. oldArrayDestinationTag: IntermediateCode.Operand;
  3881. oldArrayDestinationDimension: LONGINT;
  3882. position: LONGINT;
  3883. saved: RegisterEntry;
  3884. PROCEDURE Pass(op: IntermediateCode.Operand);
  3885. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3886. BEGIN
  3887. IF numberRegister >= 0 THEN
  3888. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3889. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3890. Emit(Mov(position,parameterRegister, op));
  3891. ELSE
  3892. Emit(Push(position,op))
  3893. END
  3894. END Pass;
  3895. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3896. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3897. BEGIN
  3898. formalType := formalType.resolved; actualType := actualType.resolved;
  3899. IF IsOpenArray(formalType)THEN
  3900. IF actualType IS SyntaxTree.StringType THEN
  3901. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3902. RETURN;
  3903. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3904. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3905. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3906. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3907. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3908. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3909. ELSE
  3910. tmp := baseReg;
  3911. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3912. IntermediateCode.MakeMemory(tmp,addressType);
  3913. Pass((tmp));
  3914. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3915. END;
  3916. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3917. END;
  3918. END PushArrayLens;
  3919. BEGIN
  3920. IF Trace THEN TraceEnter("PushParameter") END;
  3921. position := expression.position;
  3922. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3923. type := expression.type.resolved;
  3924. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3925. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3926. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3927. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3928. );
  3929. (* TODO: needed? *)
  3930. oldArrayDestinationTag := arrayDestinationTag;
  3931. oldArrayDestinationDimension := arrayDestinationDimension;
  3932. IF IsArrayOfSystemByte(parameter.type) THEN
  3933. Designate(expression,operand);
  3934. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3935. ReleaseIntermediateOperand(operand.tag);
  3936. operand.tag := tmp;
  3937. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3938. Pass((operand.tag));
  3939. END;
  3940. Pass((operand.op));
  3941. ELSIF IsOpenArray(parameter.type) THEN
  3942. Designate(expression,operand);
  3943. baseReg := operand.tag;
  3944. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3945. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3946. END;
  3947. Pass((operand.op)); (* address of the array *)
  3948. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3949. (* case 1 *)
  3950. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3951. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3952. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3953. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3954. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3955. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3956. arrayDestinationTag := sp;
  3957. (* case 1b *)
  3958. IF expression IS SyntaxTree.IndexDesignator THEN
  3959. (*
  3960. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3961. descriptorType := GetMathArrayDescriptorType(dim);
  3962. variable := GetTemporaryVariable(descriptorType,expression.position);
  3963. Symbol(variable,variableOp);
  3964. arrayDestinationTag := variableOp.op;
  3965. *)
  3966. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3967. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3968. arrayDestinationDimension := dim;
  3969. Designate(expression,operand);
  3970. (* case 1a *)
  3971. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3972. Designate(expression,operand);
  3973. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3974. i := 0;
  3975. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3976. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3977. INC(i);
  3978. END;
  3979. type := expression.type.resolved;
  3980. WHILE (i<dim) DO (* remaining static dimensions *)
  3981. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3982. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3983. ReleaseOperand(tmpOperand);
  3984. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3985. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3986. ReleaseOperand(tmpOperand);
  3987. INC(i);
  3988. END;
  3989. dimOp := IntermediateCode.Immediate(addressType,dim);
  3990. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3991. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3992. (* case 1d *)
  3993. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3994. Designate(expression,operand);
  3995. Dereference(operand,type.resolved,FALSE);
  3996. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3997. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3998. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3999. (* case 1f *)
  4000. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4001. Designate(expression,operand);
  4002. FOR i := 0 TO dim-1 DO
  4003. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4004. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4005. ReleaseOperand(tmpOperand);
  4006. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4007. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4008. ReleaseOperand(tmpOperand);
  4009. END;
  4010. dimOp := IntermediateCode.Immediate(addressType,dim);
  4011. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4012. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4013. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4014. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4015. baseType := SemanticChecker.ArrayBase(type,dim);
  4016. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4017. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4018. ELSE HALT(100);
  4019. END;
  4020. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4021. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4022. (* case 2b *)
  4023. IF expression IS SyntaxTree.IndexDesignator THEN
  4024. descriptorType := GetMathArrayDescriptorType(dim);
  4025. variable := GetTemporaryVariable(descriptorType, FALSE);
  4026. Symbol(variable,variableOp);
  4027. arrayDestinationTag := variableOp.op;
  4028. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4029. arrayDestinationDimension := dim;
  4030. NeedDescriptor := TRUE;
  4031. Designate(expression,operand);
  4032. Pass((operand.tag));
  4033. NeedDescriptor := FALSE;
  4034. (* case 2a *)
  4035. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4036. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4037. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4038. INC(i);
  4039. END;
  4040. IF i = dim THEN
  4041. Designate(expression,operand);
  4042. Pass((operand.tag));
  4043. ELSE (* open-static *)
  4044. type := expression.type.resolved;
  4045. descriptorType := GetMathArrayDescriptorType(dim);
  4046. variable := GetTemporaryVariable(descriptorType, FALSE);
  4047. Symbol(variable,variableOp);
  4048. arrayDestinationTag := variableOp.op;
  4049. Designate(expression,operand);
  4050. FOR i := 0 TO dim-1 DO
  4051. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4052. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4053. ReleaseOperand(tmpOperand);
  4054. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4055. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4056. ReleaseOperand(tmpOperand);
  4057. END;
  4058. dimOp := IntermediateCode.Immediate(addressType,dim);
  4059. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4060. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4061. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4062. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4063. baseType := SemanticChecker.ArrayBase(type,dim);
  4064. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4065. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4066. Pass((arrayDestinationTag));
  4067. END;
  4068. (* case 2d *)
  4069. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4070. Designate(expression,operand);
  4071. Dereference(operand,type.resolved,FALSE);
  4072. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4073. Pass((operand.tag));
  4074. (* case 2f *)
  4075. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4076. descriptorType := GetMathArrayDescriptorType(dim);
  4077. variable := GetTemporaryVariable(descriptorType, FALSE);
  4078. Symbol(variable,variableOp);
  4079. arrayDestinationTag := variableOp.op;
  4080. Designate(expression,operand);
  4081. FOR i := 0 TO dim-1 DO
  4082. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4083. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4084. ReleaseOperand(tmpOperand);
  4085. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4086. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4087. ReleaseOperand(tmpOperand);
  4088. END;
  4089. dimOp := IntermediateCode.Immediate(addressType,dim);
  4090. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4091. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4092. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4093. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4094. baseType := SemanticChecker.ArrayBase(type,dim);
  4095. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4096. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4097. Pass((arrayDestinationTag));
  4098. ELSE HALT(100);
  4099. END;
  4100. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4101. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4102. (* case 3b *)
  4103. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4104. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4105. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4106. Symbol(variable,variableOp);
  4107. LoadValue(variableOp,system.addressType);
  4108. ELSE
  4109. descriptorType := GetMathArrayDescriptorType(dim);
  4110. variable := GetTemporaryVariable(descriptorType, FALSE);
  4111. Symbol(variable,variableOp);
  4112. END;
  4113. arrayDestinationTag := variableOp.op;
  4114. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4115. arrayDestinationDimension := 0;
  4116. Designate(expression,operand);
  4117. Pass((operand.tag));
  4118. (* case 3a *)
  4119. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4120. i := 0;
  4121. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4122. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4123. INC(i);
  4124. END;
  4125. IF i = dim THEN
  4126. Designate(expression,operand);
  4127. Pass((operand.tag));
  4128. ELSE (* open-static *)
  4129. type := expression.type.resolved;
  4130. descriptorType := GetMathArrayDescriptorType(dim);
  4131. variable := GetTemporaryVariable(descriptorType, FALSE);
  4132. Symbol(variable,variableOp);
  4133. arrayDestinationTag := variableOp.op;
  4134. Designate(expression,operand);
  4135. FOR i := 0 TO dim-1 DO
  4136. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4137. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4138. ReleaseOperand(tmpOperand);
  4139. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4140. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4141. ReleaseOperand(tmpOperand);
  4142. END;
  4143. dimOp := IntermediateCode.Immediate(addressType,dim);
  4144. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4145. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4146. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4147. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4148. baseType := SemanticChecker.ArrayBase(type,dim);
  4149. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4150. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4151. Pass((arrayDestinationTag));
  4152. END;
  4153. (* case 3d *)
  4154. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4155. Designate(expression,operand);
  4156. Dereference(operand,type.resolved,FALSE);
  4157. (*
  4158. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4159. *)
  4160. Pass((operand.tag));
  4161. (* case 3f *)
  4162. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4163. descriptorType := GetMathArrayDescriptorType(dim);
  4164. variable := GetTemporaryVariable(descriptorType, FALSE);
  4165. Symbol(variable,variableOp);
  4166. arrayDestinationTag := variableOp.op;
  4167. Designate(expression,operand);
  4168. IF operand.op.type.length >1 THEN (* vector register *)
  4169. variable2 := GetTemporaryVariable(type, FALSE);
  4170. Symbol(variable2, variable2Op);
  4171. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4172. Emit(Mov(position,tmp, operand.op));
  4173. ReleaseOperand(operand);
  4174. Symbol(variable2, operand);
  4175. END;
  4176. FOR i := 0 TO dim-1 DO
  4177. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4178. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4179. ReleaseOperand(tmpOperand);
  4180. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4181. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4182. ReleaseOperand(tmpOperand);
  4183. END;
  4184. dimOp := IntermediateCode.Immediate(addressType,dim);
  4185. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4186. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4187. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4188. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4189. baseType := SemanticChecker.ArrayBase(type,dim);
  4190. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4191. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4192. Pass((arrayDestinationTag));
  4193. ELSE HALT(100);
  4194. END;
  4195. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4196. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4197. (* case 4b *)
  4198. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4199. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4200. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4201. Symbol(variable,variableOp);
  4202. LoadValue(variableOp,system.addressType);
  4203. ELSE
  4204. descriptorType := GetMathArrayDescriptorType(dim);
  4205. variable := GetTemporaryVariable(descriptorType, FALSE);
  4206. Symbol(variable,variableOp);
  4207. END;
  4208. arrayDestinationTag := variableOp.op;
  4209. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4210. arrayDestinationDimension := 0;
  4211. Designate(expression,operand);
  4212. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4213. Symbol(variable,variableOp);
  4214. ELSE
  4215. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4216. Symbol(variable,variableOp);
  4217. MakeMemory(tmp,variableOp.op,addressType,0);
  4218. Emit(Mov(position,tmp,operand.tag));
  4219. ReleaseIntermediateOperand(tmp);
  4220. END;
  4221. Pass((variableOp.op));
  4222. ReleaseOperand(variableOp);
  4223. (* case 4a *)
  4224. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4225. i := 0;
  4226. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4227. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4228. INC(i);
  4229. END;
  4230. IF i = dim THEN
  4231. Designate(expression,operand);
  4232. arrayDestinationTag := operand.tag;
  4233. ELSE (* open-static *)
  4234. type := expression.type.resolved;
  4235. descriptorType := GetMathArrayDescriptorType(dim);
  4236. variable := GetTemporaryVariable(descriptorType, FALSE);
  4237. Symbol(variable,variableOp);
  4238. arrayDestinationTag := variableOp.op;
  4239. Designate(expression,operand);
  4240. FOR i := 0 TO dim-1 DO
  4241. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4242. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4243. ReleaseOperand(tmpOperand);
  4244. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4245. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4246. ReleaseOperand(tmpOperand);
  4247. END;
  4248. dimOp := IntermediateCode.Immediate(addressType,dim);
  4249. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4250. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4251. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4252. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4253. baseType := SemanticChecker.ArrayBase(type,dim);
  4254. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4255. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4256. END;
  4257. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4258. Symbol(variable,variableOp);
  4259. MakeMemory(tmp,variableOp.op,addressType,0);
  4260. Emit(Mov(position,tmp,arrayDestinationTag));
  4261. ReleaseIntermediateOperand(tmp);
  4262. Pass((variableOp.op));
  4263. ReleaseOperand(variableOp);
  4264. (* case 4d *)
  4265. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4266. Designate(expression,operand);
  4267. (*
  4268. Dereference(operand,type.resolved,FALSE);
  4269. *)
  4270. (*
  4271. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4272. *)
  4273. Pass((operand.op));
  4274. (* case 4f *)
  4275. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4276. descriptorType := GetMathArrayDescriptorType(dim);
  4277. variable := GetTemporaryVariable(descriptorType, FALSE);
  4278. Symbol(variable,variableOp);
  4279. arrayDestinationTag := variableOp.op;
  4280. Designate(expression,operand);
  4281. FOR i := 0 TO dim-1 DO
  4282. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4283. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4284. ReleaseOperand(tmpOperand);
  4285. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4286. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4287. ReleaseOperand(tmpOperand);
  4288. END;
  4289. dimOp := IntermediateCode.Immediate(addressType,dim);
  4290. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4291. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4292. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4293. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4294. baseType := SemanticChecker.ArrayBase(type,dim);
  4295. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4296. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4297. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4298. Symbol(variable,variableOp);
  4299. MakeMemory(tmp,variableOp.op,addressType,0);
  4300. Emit(Mov(position,tmp,arrayDestinationTag));
  4301. ReleaseIntermediateOperand(tmp);
  4302. Pass((variableOp.op));
  4303. ReleaseOperand(variableOp);
  4304. ELSE HALT(100);
  4305. END;
  4306. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4307. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4308. Designate(expression,operand);
  4309. IF operand.op.type.length > 1 THEN
  4310. Emit(Push(position, operand.op));
  4311. ReleaseOperand(operand);
  4312. ELSE
  4313. size := system.SizeOf(type);
  4314. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4315. size := ToMemoryUnits(system,size);
  4316. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4317. arrayDestinationTag := sp;
  4318. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4319. END;
  4320. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4321. Designate(expression,operand);
  4322. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4323. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4324. Symbol(variable,variableOp);
  4325. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4326. Emit(Mov(position,tmp,operand.op));
  4327. Emit(Push(position,variableOp.op));
  4328. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4329. Pass((operand.op));
  4330. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4331. END;
  4332. ELSE HALT(200)
  4333. END;
  4334. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4335. IF parameter.kind = SyntaxTree.VarParameter THEN
  4336. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4337. Designate(expression, operand);
  4338. Pass((operand.op));
  4339. ELSE
  4340. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4341. Evaluate(expression, operand);
  4342. Pass((operand.extra)); (* step *)
  4343. Pass((operand.tag)); (* last *)
  4344. Pass((operand.op)) (* first *)
  4345. END
  4346. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4347. IF parameter.kind = SyntaxTree.VarParameter THEN
  4348. Designate(expression, operand);
  4349. Pass((operand.op));
  4350. ELSE
  4351. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4352. Evaluate(expression, operand);
  4353. Pass((operand.tag)); (* real part *)
  4354. Pass((operand.op)) (* imaginary part *)
  4355. END
  4356. ELSE
  4357. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4358. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4359. Designate(expression,operand);
  4360. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4361. (* stack allocation *)
  4362. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4363. (*! parameter alignment to be discussed ... *)
  4364. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4365. size := type(SyntaxTree.StringType).length;
  4366. END;
  4367. IF backend.cooperative & parameter.NeedsTrace() THEN
  4368. dst := NewRegisterOperand (addressType);
  4369. Emit(Mov(position,dst, sp));
  4370. IntermediateCode.InitImmediate(null, byteType, 0);
  4371. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4372. ReleaseIntermediateOperand(dst);
  4373. SaveRegisters();ReleaseUsedRegisters(saved);
  4374. (* register dst has been freed before SaveRegisters already *)
  4375. CallAssignMethod(dst, operand.op, parameter.type);
  4376. RestoreRegisters(saved);
  4377. END;
  4378. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4379. ELSIF IsOpenArray(parameter.type) THEN
  4380. Designate(expression,operand);
  4381. baseReg := operand.tag;
  4382. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4383. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4384. END;
  4385. Pass((operand.op)); (* address of the array *)
  4386. ELSIF IsDelegate(parameter.type) THEN
  4387. Evaluate(expression,operand);
  4388. IF backend.cooperative & parameter.NeedsTrace() THEN
  4389. Emit(Push(position, nil));
  4390. dst := NewRegisterOperand (addressType);
  4391. Emit(Mov(position,dst, sp));
  4392. ReleaseIntermediateOperand(dst);
  4393. SaveRegisters();ReleaseUsedRegisters(saved);
  4394. Emit(Push(position, dst));
  4395. (* register dst has been freed before SaveRegisters already *)
  4396. Emit(Push(position, operand.tag));
  4397. CallThis(position,"GarbageCollector","Assign",2);
  4398. RestoreRegisters(saved);
  4399. ELSE
  4400. Pass((operand.tag));
  4401. END;
  4402. Pass((operand.op));
  4403. ELSE
  4404. Evaluate(expression,operand);
  4405. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4406. Emit(Push(position, nil));
  4407. dst := NewRegisterOperand (addressType);
  4408. Emit(Mov(position,dst, sp));
  4409. ReleaseIntermediateOperand(dst);
  4410. SaveRegisters();ReleaseUsedRegisters(saved);
  4411. Emit(Push(position, dst));
  4412. (* register dst has been freed before SaveRegisters already *)
  4413. Emit(Push(position, operand.op));
  4414. CallThis(position,"GarbageCollector","Assign",2);
  4415. RestoreRegisters(saved);
  4416. ELSE
  4417. Pass((operand.op));
  4418. END;
  4419. END;
  4420. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4421. Evaluate(expression,operand);
  4422. Pass((operand.op));
  4423. ELSE (* var parameter *)
  4424. Designate(expression,operand);
  4425. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4426. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4427. Pass((operand.tag));
  4428. END;
  4429. END;
  4430. Pass((operand.op));
  4431. END;
  4432. END;
  4433. (* TODO: needed? *)
  4434. arrayDestinationTag := oldArrayDestinationTag;
  4435. arrayDestinationDimension := oldArrayDestinationDimension;
  4436. IF needsParameterBackup THEN
  4437. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4438. ReuseCopy(parameterBackup, operand.op)
  4439. END;
  4440. ReleaseOperand(operand);
  4441. IF Trace THEN TraceExit("PushParameter") END;
  4442. END PushParameter;
  4443. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4444. VAR prevConditional: BOOLEAN;
  4445. BEGIN
  4446. prevConditional := conditional;
  4447. conditional := FALSE;
  4448. IF (x.result # NIL) & ( x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4449. Expression(x.result); (* use register *)
  4450. END;
  4451. Statement(x.statement);
  4452. conditional := prevConditional;
  4453. IF x.result # NIL THEN Expression(x.result) END;
  4454. IF (x.result # NIL) & (x.result IS SyntaxTree.SymbolDesignator) & (x.result(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) & (x.result(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).useRegister) THEN
  4455. ReleaseIntermediateOperand(result.op);
  4456. END;
  4457. END VisitStatementDesignator;
  4458. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4459. VAR
  4460. procedure: SyntaxTree.Procedure;
  4461. procedureType: SyntaxTree.ProcedureType;
  4462. wasInline: BOOLEAN;
  4463. actualParameters: SyntaxTree.ExpressionList;
  4464. formalParameter: SyntaxTree.Parameter;
  4465. actualParameter: SyntaxTree.Expression;
  4466. i: LONGINT;
  4467. localVariable: SyntaxTree.Variable;
  4468. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4469. src, dest: Operand;
  4470. prevInlineExit : Label;
  4471. prevMapper: SymbolMapper;
  4472. tooComplex: BOOLEAN;
  4473. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4474. BEGIN
  4475. IF e = NIL THEN RETURN TRUE
  4476. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4477. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4478. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4479. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4480. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4481. ELSE RETURN FALSE
  4482. END;
  4483. END SimpleExpression;
  4484. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4485. BEGIN
  4486. RETURN checker.CanPassInRegister(type)
  4487. END FitsInRegister;
  4488. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4489. VAR
  4490. variable: SyntaxTree.Variable;
  4491. variableDesignator: SyntaxTree.Designator;
  4492. BEGIN
  4493. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4494. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4495. variableDesignator.SetType(type);
  4496. RETURN variableDesignator
  4497. END GetTemp;
  4498. BEGIN
  4499. wasInline := currentIsInline;
  4500. prevInlineExit := currentInlineExit;
  4501. prevMapper := currentMapper;
  4502. currentInlineExit := NewLabel();
  4503. tooComplex := FALSE;
  4504. NEW(currentMapper);
  4505. currentIsInline := TRUE;
  4506. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4507. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4508. formalParameter := procedureType.firstParameter;
  4509. actualParameters := x.parameters;
  4510. i := 0;
  4511. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4512. actualParameter := actualParameters.GetExpression(i);
  4513. IF actualParameter.resolved # NIL THEN
  4514. actualParameter := actualParameter.resolved
  4515. END;
  4516. (*
  4517. if expression is simple and can be passed immediately
  4518. or if type fits in register then we can proceed
  4519. otherwise we escape to ordinary procedure call.
  4520. *)
  4521. (* cases where the expression can be mapped identically *)
  4522. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4523. currentMapper.Add(formalParameter, actualParameter, NIL);
  4524. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4525. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4526. (*
  4527. Assign(variableDesignator, actualParameter);
  4528. *)
  4529. Evaluate(actualParameter, src);
  4530. Designate(variableDesignator, dest);
  4531. Emit(Mov(x.position, dest.op, src.op));
  4532. ReleaseOperand(dest);
  4533. ReleaseOperand(src);
  4534. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4535. ELSE tooComplex := TRUE
  4536. END;
  4537. (*
  4538. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4539. currentMapper.Add(formalParameter, actualParameter, NIL);
  4540. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4541. variableDesignator := GetTemp(system.addressType, FALSE);
  4542. Designate(actualParameter, src);
  4543. Designate(variableDesignator, dest);
  4544. IntermediateCode.MakeMemory(dest.op,addressType);
  4545. Emit(Mov(x.position, dest.op, src.op));
  4546. ReleaseOperand(dest);
  4547. IF src.tag.mode # IntermediateCode.Undefined THEN
  4548. tagDesignator := GetTemp(system.addressType, FALSE);
  4549. Designate(tagDesignator, dest);
  4550. IntermediateCode.MakeMemory(dest.op,addressType);
  4551. Emit(Mov(x.position, dest.op, src.op));
  4552. END;
  4553. ReleaseOperand(dest); ReleaseOperand(src);
  4554. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4555. END;
  4556. *)
  4557. formalParameter := formalParameter.nextParameter;
  4558. INC(i);
  4559. END;
  4560. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4561. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4562. currentMapper.Add(NIL, returnDesignator, NIL);
  4563. END;
  4564. localVariable := procedure.procedureScope.firstVariable;
  4565. WHILE ~tooComplex & (localVariable # NIL) DO
  4566. variableDesignator := GetTemp(localVariable.type, FALSE);
  4567. currentMapper.Add(localVariable, variableDesignator, NIL);
  4568. localVariable := localVariable.nextVariable;
  4569. END;
  4570. IF ~tooComplex THEN
  4571. VisitStatementBlock(procedure.procedureScope.body);
  4572. SetLabel(currentInlineExit);
  4573. IF procedureType.returnType # NIL THEN
  4574. Designate(returnDesignator, result);
  4575. IF conditional THEN
  4576. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4577. ValueToCondition(result)
  4578. END;
  4579. END;
  4580. END;
  4581. currentMapper := prevMapper;
  4582. currentInlineExit := prevInlineExit;
  4583. currentIsInline := wasInline;
  4584. RETURN ~tooComplex
  4585. END InlineProcedureCall;
  4586. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4587. VAR
  4588. parameters: SyntaxTree.ExpressionList;
  4589. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4590. designator: SyntaxTree.Designator;
  4591. procedureType: SyntaxTree.ProcedureType;
  4592. formalParameter: SyntaxTree.Parameter;
  4593. operand, returnValue: Operand;
  4594. reg, size, mask, dest: IntermediateCode.Operand;
  4595. saved: RegisterEntry;
  4596. symbol: SyntaxTree.Symbol;
  4597. variable: SyntaxTree.Variable;
  4598. i, parametersSize, returnTypeSize : LONGINT;
  4599. structuredReturnType: BOOLEAN;
  4600. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4601. tempVariableDesignator: SyntaxTree.Designator;
  4602. gap, alignment: LONGINT; (*fld*)
  4603. (* TODO: remove unnecessary backup variables *)
  4604. oldResult: Operand;
  4605. oldCurrentScope: SyntaxTree.Scope;
  4606. oldArrayDestinationTag: IntermediateCode.Operand;
  4607. oldArrayDestinationDimension: LONGINT;
  4608. oldConstantDeclaration: SyntaxTree.Symbol;
  4609. oldDestination: IntermediateCode.Operand;
  4610. oldCurrentLoop: Label;
  4611. oldConditional: BOOLEAN;
  4612. oldTrueLabel, oldFalseLabel: Label;
  4613. oldLocked: BOOLEAN;
  4614. usedRegisters,oldUsedRegisters: RegisterEntry;
  4615. return: IntermediateCode.Operand;
  4616. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4617. arg: IntermediateCode.Operand;
  4618. dummy: IntermediateCode.Operand;
  4619. recordType: SyntaxTree.RecordType;
  4620. operatorSelectionProcedureOperand: Operand;
  4621. operatorSelectionProcedure: SyntaxTree.Procedure;
  4622. fingerPrint: SyntaxTree.FingerPrint;
  4623. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4624. identifierNumber: LONGINT;
  4625. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4626. procedure: SyntaxTree.Procedure;
  4627. PROCEDURE BackupGlobalState;
  4628. BEGIN
  4629. oldResult := result;
  4630. oldCurrentScope := currentScope;
  4631. oldArrayDestinationTag := arrayDestinationTag;
  4632. oldArrayDestinationDimension := arrayDestinationDimension;
  4633. oldConstantDeclaration := constantDeclaration;
  4634. oldDestination := destination;
  4635. oldCurrentLoop := currentLoop;
  4636. oldConditional := conditional;
  4637. oldTrueLabel := trueLabel;
  4638. oldFalseLabel := falseLabel;
  4639. oldLocked := locked;
  4640. oldUsedRegisters := usedRegisters
  4641. END BackupGlobalState;
  4642. PROCEDURE RestoreGlobalState;
  4643. BEGIN
  4644. result := oldResult;
  4645. currentScope := oldCurrentScope;
  4646. arrayDestinationTag := oldArrayDestinationTag;
  4647. arrayDestinationDimension := oldArrayDestinationDimension;
  4648. constantDeclaration := oldConstantDeclaration;
  4649. destination := oldDestination;
  4650. currentLoop := oldCurrentLoop;
  4651. conditional := oldConditional;
  4652. trueLabel := oldTrueLabel;
  4653. falseLabel := oldFalseLabel;
  4654. locked := oldLocked;
  4655. usedRegisters := oldUsedRegisters
  4656. END RestoreGlobalState;
  4657. (** do preparations before parameter push for array-structured object types (ASOTs):
  4658. if ASOT is passed as VAR parameter:
  4659. - allocate temporary variable of math array type
  4660. - copy contents of ASOT to be passed to temporary variable
  4661. - use temporary variable as the actual parameter instead
  4662. - create and store a write-back call in a list (an index operator call that writes the contents of the temp. variable back into the ASOT)
  4663. **)
  4664. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4665. VAR
  4666. expression: SyntaxTree.Expression;
  4667. BEGIN
  4668. IF actualParameter IS SyntaxTree.Designator THEN
  4669. designator := actualParameter(SyntaxTree.Designator);
  4670. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4671. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4672. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4673. ASSERT(checker # NIL);
  4674. checker.SetCurrentScope(currentScope);
  4675. (* allocate temporary variable *)
  4676. ASSERT(actualParameter.type # NIL);
  4677. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4678. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4679. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4680. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4681. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4682. ASSERT(tempVariableDesignator.type # NIL);
  4683. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4684. (* copy math array stored in actual parameter to temporary variable *)
  4685. BackupGlobalState;
  4686. AssignMathArray(tempVariableDesignator, actualParameter);
  4687. RestoreGlobalState;
  4688. (* use temporary variable as actual parameter instead of the original one *)
  4689. actualParameter := tempVariableDesignator;
  4690. (* create write-back call and store it in linked list *)
  4691. (* create new list entry *)
  4692. IF firstWriteBackCall = NIL THEN
  4693. NEW(firstWriteBackCall);
  4694. currentWriteBackCall := firstWriteBackCall
  4695. ELSE
  4696. ASSERT(currentWriteBackCall # NIL);
  4697. NEW(currentWriteBackCall.next);
  4698. currentWriteBackCall := currentWriteBackCall.next
  4699. END;
  4700. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4701. ASSERT(expression.type = NIL);
  4702. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4703. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4704. (* prepare writeback for any other "normal" indexer *)
  4705. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4706. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4707. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4708. Assign(tempVariableDesignator, actualParameter);
  4709. actualParameter := tempVariableDesignator;
  4710. IF firstWriteBackCall = NIL THEN
  4711. NEW(firstWriteBackCall);
  4712. currentWriteBackCall := firstWriteBackCall
  4713. ELSE
  4714. ASSERT(currentWriteBackCall # NIL);
  4715. NEW(currentWriteBackCall.next);
  4716. currentWriteBackCall := currentWriteBackCall.next
  4717. END;
  4718. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4719. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4720. END
  4721. END
  4722. END PrepareParameter;
  4723. BEGIN
  4724. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4725. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4726. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4727. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4728. IF InlineProcedureCall(x) THEN
  4729. RETURN
  4730. ELSE
  4731. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4732. END
  4733. END;
  4734. END;
  4735. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4736. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4737. dest := destination; destination := emptyOperand;
  4738. SaveRegisters();ReleaseUsedRegisters(saved);
  4739. parameters := x.parameters;
  4740. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4741. (* an operator is called *)
  4742. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4743. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4744. (* check if a dynamic operator call should be performed *)
  4745. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4746. ELSE
  4747. isCallOfDynamicOperator := FALSE
  4748. END;
  4749. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4750. Emit(Push(position, ap));
  4751. END;
  4752. alignment := procedureType.stackAlignment;
  4753. IF alignment > 1 THEN
  4754. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4755. Emit(Mov(position,reg, sp));
  4756. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4757. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4758. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4759. Emit(And(position,sp, sp, mask));
  4760. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4761. Emit(Push(position,reg));
  4762. (*
  4763. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4764. Emit(Mov(position,mem,reg));
  4765. *)
  4766. ReleaseIntermediateOperand(reg);
  4767. END;
  4768. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4769. IntermediateCode.InitImmediate(mask,addressType,-16);
  4770. Emit(And(position,sp, sp, mask));
  4771. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4772. IF gap # 0 THEN
  4773. IntermediateCode.InitImmediate(size,addressType,gap);
  4774. Emit(Sub(position,sp,sp,size))
  4775. END;
  4776. END;
  4777. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4778. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4779. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4780. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4781. ELSE
  4782. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4783. END;
  4784. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4785. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4786. (* push ID *)
  4787. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4788. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4789. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4790. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4791. formalParameter := procedureType.firstParameter;
  4792. FOR i := 0 TO parameters.Length() - 1 DO
  4793. IF formalParameter.access # SyntaxTree.Hidden THEN
  4794. ASSERT(i < 2);
  4795. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4796. (* push pointer *)
  4797. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4798. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4799. (* add dereference *)
  4800. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4801. ReleaseIntermediateOperand(parameterBackups[i]);
  4802. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4803. END;
  4804. Emit(Push(position,parameterBackups[i]));
  4805. ReleaseIntermediateOperand(parameterBackups[i]);
  4806. (* push typetag *)
  4807. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4808. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4809. arg := TypeDescriptorAdr(recordType);
  4810. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4811. Emit(Push(position,arg));
  4812. ELSE
  4813. (* push 'NonPointer' *)
  4814. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4815. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4816. (* push fingerprint *)
  4817. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4818. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4819. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4820. END
  4821. END;
  4822. formalParameter := formalParameter.nextParameter
  4823. END;
  4824. (* for unary operators: complete the information for the second parameter *)
  4825. IF procedureType.numberParameters < 2 THEN
  4826. (* push 'NonPointer' *)
  4827. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4828. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4829. (* push 'NoType' *)
  4830. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4831. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4832. END;
  4833. (* call operator selection procedure *)
  4834. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4835. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4836. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4837. ReleaseOperand(operatorSelectionProcedureOperand);
  4838. (* use the address that the operator selection procedure returned as the target address of the call *)
  4839. InitOperand(operand, ModeValue);
  4840. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4841. Emit(Result(position,operand.op))
  4842. END
  4843. ELSE
  4844. Evaluate(x.left, operand)
  4845. END;
  4846. IF symbol IS SyntaxTree.Procedure THEN
  4847. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4848. Emit(Push(position,operand.tag));
  4849. ELSIF (procedureType.isDelegate) THEN
  4850. Emit(Push(position,operand.tag));
  4851. END;
  4852. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4853. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4854. Emit(Push(position,operand.tag));
  4855. END;
  4856. ELSE HALT(200);
  4857. END;
  4858. ReleaseIntermediateOperand(operand.tag);
  4859. operand.tag := emptyOperand;
  4860. (* determine if a structured return type is needed *)
  4861. structuredReturnType := StructuredReturnType(procedureType);
  4862. IF structuredReturnType THEN
  4863. IF resultDesignator # NIL THEN
  4864. d := resultDesignator;
  4865. ELSE
  4866. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4867. variable.SetUntraced(procedureType.hasUntracedReturn);
  4868. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4869. d.SetType(variable.type);
  4870. END;
  4871. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4872. Designate(d,returnValue);
  4873. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4874. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4875. Emit(Push(position,size));
  4876. Emit(Push(position,returnValue.op));
  4877. ReleaseOperand(returnValue);
  4878. ELSE
  4879. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4880. END;
  4881. END;
  4882. firstWriteBackCall := NIL; (* reset write-back call list *)
  4883. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4884. passByRegister := system.registerParameters > 0;
  4885. registerNumber := 0;
  4886. formalParameter := procedureType.lastParameter;
  4887. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4888. actualParameter := parameters.GetExpression(i);
  4889. PrepareParameter(actualParameter, formalParameter);
  4890. IF passByRegister & (i < system.registerParameters) THEN
  4891. IF ~PassInRegister(formalParameter) THEN
  4892. Error(actualParameter.position,"cannot be passed by register")
  4893. ELSE
  4894. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4895. END;
  4896. INC(registerNumber);
  4897. ELSE
  4898. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4899. END;
  4900. formalParameter := formalParameter.prevParameter;
  4901. END;
  4902. IF passByRegister & (registerNumber > 0) THEN
  4903. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4904. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4905. END;
  4906. ELSE
  4907. hasDynamicOperands := FALSE;
  4908. formalParameter := procedureType.firstParameter;
  4909. FOR i := 0 TO parameters.Length() - 1 DO
  4910. actualParameter := parameters.GetExpression(i);
  4911. IF formalParameter # NIL THEN (* TENTATIVE *)
  4912. PrepareParameter(actualParameter, formalParameter);
  4913. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4914. ASSERT(i < 2);
  4915. hasDynamicOperands := TRUE;
  4916. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4917. ELSE
  4918. IF passByRegister & (registerNumber > 0) THEN
  4919. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4920. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4921. END;
  4922. passByRegister := FALSE;
  4923. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4924. END;
  4925. formalParameter := formalParameter.nextParameter;
  4926. END;
  4927. END;
  4928. END;
  4929. IF symbol IS SyntaxTree.Procedure THEN
  4930. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4931. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4932. Emit(Push(position,reg));
  4933. ReleaseIntermediateOperand(reg);
  4934. END;
  4935. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4936. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4937. parametersSize := ParametersSize(system,procedureType,FALSE);
  4938. END;
  4939. ReleaseParameterRegisters();
  4940. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4941. Emit(Call(position,operand.op,0));
  4942. ELSE
  4943. Emit(Call(position,operand.op,parametersSize));
  4944. END;
  4945. ReleaseOperand(operand);
  4946. IF procedureType.noReturn THEN
  4947. EmitTrap(position,NoReturnTrap);
  4948. END;
  4949. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4950. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4951. Emit(Result(position,return));
  4952. END;
  4953. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4954. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4955. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4956. END;
  4957. size := IntermediateCode.Immediate(addressType,parametersSize);
  4958. Emit(Add(position,sp,sp,size));
  4959. END;
  4960. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4961. IF structuredReturnType THEN
  4962. (* stack pointer rewinding done by callee
  4963. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4964. Emit(Add(position,sp,sp,size));
  4965. *)
  4966. RestoreRegisters(saved);
  4967. InitOperand(result,ModeReference);
  4968. Symbol(variable,result);
  4969. ELSE
  4970. RestoreRegisters(saved);
  4971. InitOperand(result,ModeValue);
  4972. result.op := return;
  4973. END;
  4974. END;
  4975. IF alignment > 1 THEN
  4976. Emit(Pop(position,sp));
  4977. END;
  4978. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4979. Emit(Pop(position, ap));
  4980. END;
  4981. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4982. ValueToCondition(result);
  4983. END;
  4984. destination := dest;
  4985. (* perform all write-back calls in the list *)
  4986. BackupGlobalState;
  4987. currentWriteBackCall := firstWriteBackCall;
  4988. WHILE currentWriteBackCall # NIL DO
  4989. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4990. currentWriteBackCall := currentWriteBackCall.next
  4991. END;
  4992. RestoreGlobalState;
  4993. (* TENATIVE *)
  4994. (*
  4995. IF isOperatorCall THEN
  4996. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4997. END;
  4998. *)
  4999. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  5000. END VisitProcedureCallDesignator;
  5001. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  5002. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  5003. td: SyntaxTree.Symbol;
  5004. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  5005. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5006. BEGIN
  5007. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5008. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5009. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5010. variable.SetType(system.anyType);
  5011. scope.AddVariable(variable);
  5012. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5013. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  5014. typeDeclaration.SetDeclaredType(hiddenPointerType);
  5015. typeDeclaration.SetScope(module.module.moduleScope);
  5016. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  5017. hiddenPointerType.SetState(SyntaxTree.Resolved);
  5018. END;
  5019. RETURN hiddenPointerType;
  5020. END GetHiddenPointerType;
  5021. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5022. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5023. BEGIN
  5024. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5025. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5026. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  5027. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  5028. scope.AddVariable(variable);
  5029. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5030. variable.SetType(system.anyType);
  5031. scope.AddVariable(variable);
  5032. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5033. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  5034. typeDeclaration.SetDeclaredType(delegatePointerType);
  5035. typeDeclaration.SetScope(module.module.moduleScope);
  5036. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5037. delegatePointerType.SetState(SyntaxTree.Resolved);
  5038. END;
  5039. RETURN delegatePointerType
  5040. END GetDelegateType;
  5041. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5042. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5043. such as in VAR a: RECORD ... END;
  5044. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5045. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5046. *)
  5047. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5048. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5049. BEGIN (* no code emission *)
  5050. source := NIL;
  5051. x := x.resolved;
  5052. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5053. x := GetHiddenPointerType();
  5054. ELSIF IsDelegate(x) THEN
  5055. x := GetDelegateType();
  5056. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5057. ELSE HALT(200);
  5058. END;
  5059. td := x.typeDeclaration;
  5060. IF td = NIL THEN
  5061. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5062. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5063. ASSERT(td # NIL);
  5064. END;
  5065. IF newObjectFile THEN
  5066. GetCodeSectionNameForSymbol(td,name);
  5067. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5068. meta.CheckTypeDeclaration(x);
  5069. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5070. ELSE
  5071. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5072. END;
  5073. IF backend.cooperative OR meta.simple THEN
  5074. offset := 0;
  5075. ELSE
  5076. IF x IS SyntaxTree.CellType THEN
  5077. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5078. IF baseRecord = NIL THEN
  5079. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5080. ELSE
  5081. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5082. END;
  5083. ELSE
  5084. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5085. END;
  5086. END;
  5087. ELSE
  5088. offset := 0;
  5089. source := module.allSections.FindBySymbol(td); (*TODO*)
  5090. IF source = NIL THEN
  5091. null := 0;
  5092. GetCodeSectionNameForSymbol(td,name);
  5093. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5094. IntermediateCode.InitImmediate(op,addressType,0);
  5095. source(IntermediateCode.Section).Emit(Data(position,op));
  5096. source.SetReferenced(FALSE)
  5097. ELSE
  5098. name := source.name;
  5099. END;
  5100. END;
  5101. RETURN td
  5102. END GetBackendType;
  5103. BEGIN
  5104. (*td := t.typeDeclaration;*)
  5105. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5106. (*
  5107. IF t IS SyntaxTree.PointerType THEN
  5108. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5109. ELSE
  5110. source := GetBackendType(t);
  5111. END;
  5112. *)
  5113. IF newObjectFile THEN
  5114. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5115. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5116. IntermediateCode.SetOffset(res,offset);
  5117. ELSE
  5118. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5119. END;
  5120. (*
  5121. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5122. make memory should be used when tag is used, not earlier
  5123. *)
  5124. RETURN res
  5125. END TypeDescriptorAdr;
  5126. (*
  5127. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5128. VAR result: IntermediateCode.Operand;
  5129. BEGIN
  5130. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5131. operand.tag := TypeDescriptorAdr(operand.type);
  5132. IntermediateCode.MakeMemory(operand.tag,addressType);
  5133. UseIntermediateOperand(operand.tag);
  5134. END;
  5135. END MakeTypeTag;
  5136. *)
  5137. PROCEDURE ProfilerInit;
  5138. VAR reg: IntermediateCode.Operand;
  5139. BEGIN
  5140. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5141. Emit(Call(position,reg,0));
  5142. END ProfilerInit;
  5143. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5144. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5145. BEGIN
  5146. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5147. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5148. THEN
  5149. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5150. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5151. Emit(Push(position,reg));
  5152. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5153. Emit(Push(position,reg));
  5154. StaticCallOperand(result,procedure);
  5155. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5156. ReleaseOperand(result);
  5157. END;
  5158. END ProfilerEnterExit;
  5159. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5160. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5161. BEGIN
  5162. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5163. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5164. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5165. profileInit.Emit(Push(position,reg));
  5166. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5167. profileInit.Emit(Push(position,reg));
  5168. NEW(string, LEN(name)); COPY(name, string^);
  5169. sv := SyntaxTree.NewStringValue(-1,string);
  5170. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5171. sv.SetType(type);
  5172. Designate(sv,result);
  5173. profileInit.Emit(Push(position,result.tag));
  5174. profileInit.Emit(Push(position,result.op));
  5175. StaticCallOperand(result,procedure);
  5176. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5177. ReleaseOperand(result);
  5178. END;
  5179. END ProfilerAddProcedure;
  5180. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5181. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5182. BEGIN
  5183. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5184. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5185. profileInit.Emit(Push(position,reg));
  5186. profileInitPatchPosition := profileInit.pc;
  5187. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5188. NEW(string, LEN(name)); COPY(name, string^);
  5189. sv := SyntaxTree.NewStringValue(-1,string);
  5190. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5191. sv.SetType(type);
  5192. Designate(sv,result);
  5193. profileInit.Emit(Push(position,result.tag));
  5194. profileInit.Emit(Push(position,result.op));
  5195. StaticCallOperand(result,procedure);
  5196. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5197. ReleaseOperand(result);
  5198. END;
  5199. END ProfilerAddModule;
  5200. PROCEDURE ProfilerPatchInit;
  5201. VAR reg: IntermediateCode.Operand;
  5202. BEGIN
  5203. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5204. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5205. EmitLeave(profileInit,position,0);
  5206. profileInit.Emit(Exit(position,0,0));
  5207. END ProfilerPatchInit;
  5208. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5209. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5210. VAR
  5211. id: LONGINT;
  5212. leftType, rightType: SyntaxTree.Type;
  5213. procedureType: SyntaxTree.ProcedureType;
  5214. runtimeProcedure: SyntaxTree.Procedure;
  5215. runtimeProcedureOperand, operatorOperand: Operand;
  5216. kind: SET;
  5217. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5218. VAR
  5219. arg: IntermediateCode.Operand;
  5220. recordType: SyntaxTree.RecordType;
  5221. fingerPrint: SyntaxTree.FingerPrint;
  5222. BEGIN
  5223. IF type = NIL THEN
  5224. (* no type: push 'NoType' *)
  5225. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5226. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5227. ELSIF IsStrictlyPointerToRecord(type) THEN
  5228. (* pointer to record type: push typetag *)
  5229. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5230. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5231. arg := TypeDescriptorAdr(recordType);
  5232. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5233. ELSE
  5234. (* non-pointer to record type: push fingerprint *)
  5235. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5236. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5237. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5238. END;
  5239. operatorInitializationCodeSection.Emit(Push(position,arg))
  5240. END PushTypeInfo;
  5241. BEGIN
  5242. ASSERT(operatorInitializationCodeSection # NIL);
  5243. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5244. procedureType := operator.type(SyntaxTree.ProcedureType);
  5245. (* determine types *)
  5246. leftType := procedureType.firstParameter.type;
  5247. IF procedureType.numberParameters = 2 THEN
  5248. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5249. rightType := procedureType.firstParameter.nextParameter.type;
  5250. ELSE
  5251. rightType := NIL
  5252. END;
  5253. (* determine operator kind *)
  5254. IF IsStrictlyPointerToRecord(leftType) THEN
  5255. kind := {LhsIsPointer}
  5256. ELSE
  5257. kind := {}
  5258. END;
  5259. IF IsStrictlyPointerToRecord(rightType) THEN
  5260. kind := kind + {RhsIsPointer}
  5261. END;
  5262. IF kind # {} THEN (* TODO: to be removed later on *)
  5263. (* at least one of the types is a pointer to record *)
  5264. (* emit a code that registers this specific operator in the runtime *)
  5265. dump := operatorInitializationCodeSection.comments;
  5266. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5267. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5268. (* push ID *)
  5269. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5270. id := Global.GetSymbol(module.module.case, operator.name);
  5271. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5272. (* push kind *)
  5273. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5274. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5275. (* push type infos *)
  5276. PushTypeInfo(leftType);
  5277. PushTypeInfo(rightType);
  5278. (* push operator address *)
  5279. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5280. StaticCallOperand(operatorOperand, operator);
  5281. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5282. ReleaseOperand(operatorOperand);
  5283. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5284. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5285. ReleaseOperand(runtimeProcedureOperand)
  5286. END
  5287. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5288. END
  5289. END RegisterDynamicOperator;
  5290. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5291. VAR
  5292. traceModule: SyntaxTree.Module;
  5293. procedure: SyntaxTree.Procedure;
  5294. procedureVariable: SyntaxTree.Variable;
  5295. s,msg: Basic.MessageString;
  5296. res: Operand;
  5297. i: LONGINT;
  5298. sv: SyntaxTree.StringValue;
  5299. type: SyntaxTree.Type;
  5300. recordType: SyntaxTree.RecordType;
  5301. printout: Printout.Printer;
  5302. stringWriter: Streams.StringWriter;
  5303. expression: SyntaxTree.Expression;
  5304. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5305. BEGIN
  5306. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5307. IF procedure = NIL THEN
  5308. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5309. END;
  5310. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5311. s := "procedure ";
  5312. Strings.Append(s,backend.traceModuleName);
  5313. Strings.Append(s,".");
  5314. Strings.Append(s,procedureName);
  5315. Strings.Append(s," not present");
  5316. Error(position,s);
  5317. RETURN FALSE
  5318. ELSE
  5319. RETURN TRUE
  5320. END;
  5321. END GetProcedure;
  5322. PROCEDURE CallProcedure;
  5323. VAR size: LONGINT;
  5324. BEGIN
  5325. IF procedure # NIL THEN
  5326. StaticCallOperand(result,procedure);
  5327. size := ProcedureParametersSize(system,procedure);
  5328. ELSE
  5329. Symbol(procedureVariable, result);
  5330. LoadValue(result, procedureVariable.type.resolved);
  5331. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5332. END;
  5333. Emit(Call(position,result.op,size));
  5334. END CallProcedure;
  5335. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5336. VAR res: Operand; string: SyntaxTree.String;
  5337. BEGIN
  5338. IF GetProcedure("String") THEN
  5339. NEW(string, LEN(s)); COPY(s, string^);
  5340. sv := SyntaxTree.NewStringValue(-1,string);
  5341. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5342. sv.SetType(type);
  5343. Designate(sv,res);
  5344. Emit(Push(position,res.tag));
  5345. Emit(Push(position,res.op));
  5346. ReleaseOperand(res);
  5347. CallProcedure;
  5348. END;
  5349. END String;
  5350. PROCEDURE Integer(op: IntermediateCode.Operand);
  5351. BEGIN
  5352. IF GetProcedure("Int") THEN
  5353. Emit(Push(position,op));
  5354. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5355. CallProcedure;
  5356. END;
  5357. END Integer;
  5358. PROCEDURE Float(op: IntermediateCode.Operand);
  5359. BEGIN
  5360. IF GetProcedure("HIntHex") THEN
  5361. Emit(Push(position,op));
  5362. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5363. CallProcedure;
  5364. END;
  5365. END Float;
  5366. PROCEDURE Set(op: IntermediateCode.Operand);
  5367. BEGIN
  5368. IF GetProcedure("Set") THEN
  5369. Emit(Push(position,op));
  5370. (*
  5371. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5372. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5373. *)
  5374. CallProcedure;
  5375. END;
  5376. END Set;
  5377. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5378. BEGIN
  5379. IF GetProcedure("Boolean") THEN
  5380. Emit(Push(position,op));
  5381. CallProcedure;
  5382. END;
  5383. END Boolean;
  5384. PROCEDURE Char(op: IntermediateCode.Operand);
  5385. BEGIN
  5386. IF GetProcedure("Char") THEN
  5387. Emit(Push(position,op));
  5388. CallProcedure;
  5389. END;
  5390. END Char;
  5391. PROCEDURE Address(op: IntermediateCode.Operand);
  5392. BEGIN
  5393. IF GetProcedure("Address") THEN
  5394. Emit(Push(position,op));
  5395. CallProcedure;
  5396. END;
  5397. END Address;
  5398. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5399. BEGIN
  5400. IF GetProcedure("String") THEN
  5401. Emit(Push(position,tag));
  5402. Emit(Push(position,op));
  5403. CallProcedure;
  5404. END;
  5405. END StringOperand;
  5406. PROCEDURE Ln;
  5407. BEGIN
  5408. IF GetProcedure("Ln") THEN
  5409. CallProcedure;
  5410. END;
  5411. END Ln;
  5412. BEGIN
  5413. IF backend.traceModuleName = "" THEN RETURN END;
  5414. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5415. IF GetProcedure("Enter") THEN
  5416. CallProcedure
  5417. END;
  5418. NEW(stringWriter,LEN(s));
  5419. FOR i := 0 TO x.Length()-1 DO
  5420. msg := "";
  5421. expression := x.GetExpression(i);
  5422. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5423. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5424. ELSE
  5425. Global.GetModuleName(module.module, s);
  5426. END;
  5427. IF i = 0 THEN
  5428. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5429. stringWriter.String(":");
  5430. END;
  5431. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5432. IF ~(expression IS SyntaxTree.StringValue) THEN
  5433. printout.Expression(expression);
  5434. stringWriter.Get(s);
  5435. Strings.Append(msg,s);
  5436. Strings.Append(msg,"= ");
  5437. ELSE stringWriter.Get(s); (* remove from string writer *)
  5438. Strings.Append(msg, s);
  5439. END;
  5440. String(msg);
  5441. IF SemanticChecker.IsStringType(expression.type) THEN
  5442. Designate(expression,res);
  5443. StringOperand(res.op,res.tag);
  5444. ELSE
  5445. Evaluate(expression,res);
  5446. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5447. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5448. Convert(res.op,int64);
  5449. END;
  5450. Integer(res.op);
  5451. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5452. Boolean(res.op);
  5453. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5454. Set(res.op);
  5455. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5456. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5457. Convert(res.op,float64);
  5458. END;
  5459. Float(res.op);
  5460. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5461. Char(res.op);
  5462. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5463. Address(res.op);String("H");
  5464. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5465. Address(res.op);String("H");
  5466. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5467. Address(res.op);String("H");
  5468. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5469. Address(res.op);String("H");
  5470. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5471. String("NIL");
  5472. ELSE HALT(200);
  5473. END;
  5474. END;
  5475. ReleaseOperand(res);
  5476. String("; ");
  5477. END;
  5478. IF GetProcedure("Exit") THEN
  5479. CallProcedure
  5480. ELSE
  5481. Ln;
  5482. END;
  5483. END;
  5484. END SystemTrace;
  5485. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5486. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5487. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5488. BEGIN
  5489. type := type.resolved;
  5490. IF type IS SyntaxTree.RecordType THEN
  5491. WITH type: SyntaxTree.RecordType DO
  5492. baseType := type.baseType;
  5493. IF baseType # NIL THEN
  5494. baseType := baseType.resolved;
  5495. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5496. InitFields(baseType,adr,offset);
  5497. END;
  5498. variable := type.recordScope.firstVariable;
  5499. WHILE variable # NIL DO
  5500. IF variable.initializer # NIL THEN
  5501. Evaluate(variable.initializer,initializerOp);
  5502. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5503. Emit(Mov(position,mem,initializerOp.op));
  5504. ReleaseOperand(initializerOp);
  5505. ReleaseIntermediateOperand(mem);
  5506. END;
  5507. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5508. variable := variable.nextVariable
  5509. END;
  5510. END;
  5511. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5512. WITH type: SyntaxTree.ArrayType DO
  5513. IF type.form = SyntaxTree.Static THEN
  5514. baseType := type.arrayBase;
  5515. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5516. FOR i := 0 TO type.staticLength-1 DO
  5517. InitFields(baseType,adr,offset+i*size);
  5518. END;
  5519. END;
  5520. END;
  5521. ELSIF type IS SyntaxTree.MathArrayType THEN
  5522. WITH type: SyntaxTree.MathArrayType DO
  5523. IF type.form = SyntaxTree.Open THEN
  5524. dim := DynamicDim(type);
  5525. imm := IntermediateCode.Immediate(addressType,dim);
  5526. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5527. baseType := SemanticChecker.ArrayBase(type,dim);
  5528. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5529. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5530. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5531. ReleaseIntermediateOperand(imm);
  5532. (* flags remain empty (=0) for open array *)
  5533. ELSIF type.form = SyntaxTree.Static THEN
  5534. baseType := type.arrayBase;
  5535. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5536. ASSERT(type.staticLength < 1024*1024*1024);
  5537. FOR i := 0 TO type.staticLength-1 DO
  5538. InitFields(baseType,adr,offset+i*size);
  5539. END;
  5540. END;
  5541. END;
  5542. END;
  5543. END InitFields;
  5544. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5545. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5546. BEGIN
  5547. type := variable.type.resolved;
  5548. IF (type IS SyntaxTree.MathArrayType) THEN
  5549. WITH type: SyntaxTree.MathArrayType DO
  5550. IF type.form = SyntaxTree.Open THEN
  5551. Symbol(variable,operand);
  5552. InitFields(type, operand.tag,0);
  5553. ELSIF type.form = SyntaxTree.Tensor THEN
  5554. Symbol(variable, operand);
  5555. MakeMemory(tmp,operand.op,addressType,0);
  5556. ReleaseOperand(operand);
  5557. Emit(Mov(position,tmp, nil ) );
  5558. ReleaseIntermediateOperand(tmp);
  5559. END;
  5560. END;
  5561. ELSE
  5562. Symbol(variable,operand);
  5563. IF variable.initializer # NIL THEN
  5564. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5565. reference.SetType(variable.type.resolved);
  5566. reference.SetAssignable(TRUE);
  5567. Assign(reference,variable.initializer);
  5568. END;
  5569. InitFields(type, operand.op,0);
  5570. ReleaseOperand(operand);
  5571. END;
  5572. END InitVariable;
  5573. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5574. VAR end: Label;
  5575. BEGIN
  5576. IF type.form = SyntaxTree.Tensor THEN
  5577. InitOperand(result,ModeValue);
  5578. ReuseCopy(result.op,base);
  5579. end := NewLabel();
  5580. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5581. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5582. SetLabel(end);
  5583. Convert(result.op,int32);
  5584. ELSE
  5585. InitOperand(result,ModeValue);
  5586. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5587. END
  5588. END MathArrayDim;
  5589. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5590. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5591. BEGIN
  5592. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5593. MakeMemory(mem,base,addressType,offset);
  5594. Emit(Mov(position,mem,value));
  5595. ReleaseIntermediateOperand(mem);
  5596. END PutMathArrayField;
  5597. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5598. VAR mem: IntermediateCode.Operand;
  5599. BEGIN
  5600. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5601. MakeMemory(mem,base,addressType,offset);
  5602. Emit(Mov(position,mem,value));
  5603. ReleaseIntermediateOperand(mem);
  5604. END PutMathArrayFieldOffset;
  5605. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5606. BEGIN
  5607. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5608. MakeMemory(value,base,addressType,offset);
  5609. END GetMathArrayField;
  5610. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5611. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5612. BEGIN
  5613. IF incr THEN
  5614. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5615. ELSE
  5616. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5617. END;
  5618. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5619. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5620. ELSE
  5621. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5622. Emit(Mov(position,reg,dim));
  5623. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5624. Emit(Add(position,reg,reg,base));
  5625. MakeMemory(mem, reg, addressType, offset);
  5626. ReleaseIntermediateOperand(reg);
  5627. Emit(Mov(position,mem,value));
  5628. ReleaseIntermediateOperand(mem);
  5629. END;
  5630. END PutMathArrayLenOrIncr;
  5631. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5632. BEGIN
  5633. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5634. END PutMathArrayLength;
  5635. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5636. BEGIN
  5637. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5638. END PutMathArrayIncrement;
  5639. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5640. BEGIN
  5641. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5642. END GetMathArrayIncrement;
  5643. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5644. BEGIN
  5645. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5646. END GetMathArrayLength;
  5647. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5648. VAR dimOp: IntermediateCode.Operand;
  5649. BEGIN
  5650. dimOp := IntermediateCode.Immediate(int32, dim);
  5651. GetMathArrayLength(type, operand, dimOp, check, result);
  5652. END GetMathArrayLengthAt;
  5653. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5654. VAR dimOp: IntermediateCode.Operand;
  5655. BEGIN
  5656. dimOp := IntermediateCode.Immediate(int32, dim);
  5657. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5658. END GetMathArrayIncrementAt;
  5659. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5660. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5661. offset: LONGINT;
  5662. BEGIN
  5663. IF increment THEN
  5664. offset := MathIncrOffset;
  5665. ELSE
  5666. offset := MathLenOffset;
  5667. END;
  5668. INC(offset,operand.dimOffset*2);
  5669. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5670. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5671. END;
  5672. (* static dimension *)
  5673. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5674. IF check & (type.form = SyntaxTree.Tensor) THEN
  5675. DimensionCheck(operand.tag,dim,BrltL);
  5676. END;
  5677. val := SHORT(dim.intValue);
  5678. IF type.form # SyntaxTree.Tensor THEN
  5679. t := SemanticChecker.ArrayBase(type,val);
  5680. type := t.resolved(SyntaxTree.MathArrayType);
  5681. IF type.form = SyntaxTree.Static THEN
  5682. IF increment THEN
  5683. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5684. ELSE
  5685. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5686. END;
  5687. InitOperand(result,ModeValue);
  5688. result.op := res;
  5689. RETURN;
  5690. END;
  5691. END;
  5692. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5693. MakeMemory(res,operand.tag,addressType,offset);
  5694. (*
  5695. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5696. *)
  5697. InitOperand(result,ModeValue);
  5698. result.op := res;
  5699. ELSE
  5700. Convert(dim,addressType);
  5701. IF check THEN
  5702. IF type.form = SyntaxTree.Tensor THEN
  5703. DimensionCheck(operand.tag,dim,BrltL);
  5704. ELSIF isUnchecked THEN (* do nothing *)
  5705. ELSE
  5706. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5707. END;
  5708. END;
  5709. end := NewLabel(); next := NIL;
  5710. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5711. Emit(Mov(position,res,dim));
  5712. Convert(res,int32);
  5713. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5714. WHILE t IS SyntaxTree.MathArrayType DO
  5715. type := t(SyntaxTree.MathArrayType);
  5716. IF type.form = SyntaxTree.Static THEN
  5717. imm := IntermediateCode.Immediate(int32,val);
  5718. next := NewLabel();
  5719. BrneL(next,imm,res);
  5720. IF increment THEN
  5721. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5722. ELSE
  5723. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5724. END;
  5725. Emit(MovReplace(position,res,imm));
  5726. BrL(end);
  5727. ELSE hasDynamicPart := TRUE;
  5728. END;
  5729. t := type.arrayBase.resolved;
  5730. val := val + 1;
  5731. IF next # NIL THEN SetLabel(next) END;
  5732. END;
  5733. IF hasDynamicPart THEN
  5734. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5735. Emit(Mov(position,res2,dim));
  5736. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5737. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5738. Emit(Add(position,res2,res2,imm));
  5739. Emit(Add(position,res2,res2,operand.tag));
  5740. IntermediateCode.MakeMemory(res2,int32);
  5741. Emit(MovReplace(position,res,res2));
  5742. ReleaseIntermediateOperand(res2);
  5743. END;
  5744. SetLabel(end);
  5745. Convert(res,int32);
  5746. InitOperand(result,ModeValue);
  5747. result.op := res;
  5748. END;
  5749. END MathArrayLenOrIncr;
  5750. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5751. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5752. offset: LONGINT;
  5753. BEGIN
  5754. offset := operand.dimOffset+DynamicDim(type)-1;
  5755. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5756. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5757. val := SHORT(dim.intValue);
  5758. t := SemanticChecker.ArrayBase(type,val);
  5759. type := t.resolved(SyntaxTree.ArrayType);
  5760. IF type.form = SyntaxTree.Static THEN
  5761. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5762. ELSE
  5763. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5764. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5765. END;
  5766. UseIntermediateOperand(res);
  5767. InitOperand(result,ModeValue);
  5768. result.op := res;
  5769. ELSE
  5770. Convert(dim,addressType);
  5771. IF ~isUnchecked THEN
  5772. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5773. END;
  5774. end := NewLabel(); next := NIL;
  5775. (* ReuseCopy(dim,res); *)
  5776. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5777. Emit(Mov(position,res,dim));
  5778. Convert(res,int32);
  5779. Convert(res,int32);
  5780. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5781. WHILE t IS SyntaxTree.ArrayType DO
  5782. type := t(SyntaxTree.ArrayType);
  5783. IF type.form = SyntaxTree.Static THEN
  5784. imm := IntermediateCode.Immediate(int32,val);
  5785. next := NewLabel();
  5786. BrneL(next,imm,res);
  5787. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5788. Emit(MovReplace(position,res,imm));
  5789. BrL(end);
  5790. ELSE hasDynamicPart := TRUE;
  5791. END;
  5792. t := type.arrayBase.resolved;
  5793. val := val + 1;
  5794. IF next # NIL THEN SetLabel(next) END;
  5795. END;
  5796. IF hasDynamicPart THEN
  5797. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5798. Convert(res2,addressType);
  5799. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5800. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5801. Emit(Sub(position,res2,imm,res2));
  5802. Emit(Add(position,res2,res2,operand.tag));
  5803. IntermediateCode.MakeMemory(res2,int32);
  5804. Emit(MovReplace(position,res,res2));
  5805. ReleaseIntermediateOperand(res2);
  5806. END;
  5807. SetLabel(end);
  5808. Convert(res,int32);
  5809. InitOperand(result,ModeValue);
  5810. result.op := res;
  5811. END;
  5812. END ArrayLen;
  5813. (**
  5814. create a temporary variable in current scope
  5815. **)
  5816. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5817. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5818. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5819. BEGIN
  5820. IF ~register THEN
  5821. variable := temporaries.GetFreeVariable(type, index);
  5822. ELSE
  5823. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5824. END;
  5825. scope := currentScope;
  5826. IF variable = NIL THEN
  5827. COPY("@hiddenIRVar",string);
  5828. Basic.AppendNumber(string,index);
  5829. name := SyntaxTree.NewIdentifier(string);
  5830. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5831. variable.SetType(type);
  5832. variable.SetAccess(SyntaxTree.Hidden);
  5833. IF ~register THEN
  5834. temporaries.AddVariable(variable);
  5835. IF scope.lastVariable # NIL THEN
  5836. offset := scope.lastVariable.offsetInBits;
  5837. ELSE
  5838. offset := 0;
  5839. END;
  5840. DEC(offset,system.SizeOf(variable.type));
  5841. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5842. variable(SyntaxTree.Variable).SetOffset(offset);
  5843. scope.AddVariable(variable(SyntaxTree.Variable));
  5844. scope.EnterSymbol(variable, duplicate);
  5845. ASSERT(~duplicate);
  5846. InitVariable(variable(SyntaxTree.Variable));
  5847. ELSE
  5848. variable.SetUseRegister(TRUE);
  5849. variable(SyntaxTree.Variable).SetOffset(0);
  5850. END;
  5851. ELSE
  5852. InitVariable(variable(SyntaxTree.Variable));
  5853. (*
  5854. ASSERT(variable.type.resolved = type.resolved)
  5855. *)
  5856. END;
  5857. RETURN variable(SyntaxTree.Variable)
  5858. END GetTemporaryVariable;
  5859. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5860. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5861. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5862. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5863. i: LONGINT; duplicate: BOOLEAN;
  5864. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5865. VAR variable: SyntaxTree.Variable;
  5866. BEGIN
  5867. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5868. variable.SetType(type);
  5869. recordScope.AddVariable(variable);
  5870. END AddVariable;
  5871. BEGIN
  5872. name := "@ArrayDescriptor";
  5873. Basic.AppendNumber(name,dimensions);
  5874. identifier := SyntaxTree.NewIdentifier(name);
  5875. parentScope := module.module.moduleScope;
  5876. symbol := parentScope.FindSymbol(identifier);
  5877. IF symbol # NIL THEN
  5878. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5879. type := typeDeclaration.declaredType;
  5880. ELSE
  5881. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5882. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5883. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5884. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5885. recordType.SetTypeDeclaration(typeDeclaration);
  5886. recordType.SetState(SyntaxTree.Resolved);
  5887. typeDeclaration.SetDeclaredType(recordType);
  5888. AddVariable("@ptr",system.anyType);
  5889. AddVariable("@adr",system.addressType);
  5890. AddVariable("@flags",system.addressType);
  5891. AddVariable("@dim",system.addressType);
  5892. AddVariable("@elementSize",system.addressType);
  5893. FOR i := 0 TO dimensions-1 DO
  5894. name := "@len";
  5895. Basic.AppendNumber(name,i);
  5896. AddVariable(name,system.addressType);
  5897. name := "@incr";
  5898. Basic.AppendNumber(name,i);
  5899. AddVariable(name,system.addressType);
  5900. END;
  5901. parentScope.AddTypeDeclaration(typeDeclaration);
  5902. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5903. ASSERT(~duplicate);
  5904. type := recordType;
  5905. END;
  5906. RETURN type
  5907. END GetMathArrayDescriptorType;
  5908. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5909. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5910. BEGIN
  5911. type := GetMathArrayDescriptorType(dimensions);
  5912. Emit(Push(position,op.op));
  5913. (* push type descriptor *)
  5914. reg := TypeDescriptorAdr(type);
  5915. IF ~newObjectFile THEN
  5916. IntermediateCode.MakeMemory(reg,addressType);
  5917. END;
  5918. Emit(Push(position,reg));
  5919. ReleaseIntermediateOperand(reg);
  5920. (* push realtime flag: false by default *)
  5921. Emit(Push(position,false));
  5922. CallThis(position,"Heaps","NewRec",3);
  5923. END NewMathArrayDescriptor;
  5924. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5925. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5926. BEGIN
  5927. NEW(string, LEN(s)); COPY(s, string^);
  5928. sv := SyntaxTree.NewStringValue(-1,string);
  5929. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5930. sv.SetType(type);
  5931. Designate(sv,res);
  5932. Emit(Push(position,res.tag));
  5933. Emit(Push(position,res.op));
  5934. ReleaseOperand(res);
  5935. END PushConstString;
  5936. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5937. BEGIN
  5938. IF b THEN
  5939. Emit(Push(-1, true));
  5940. ELSE
  5941. Emit(Push(-1, false));
  5942. END;
  5943. END PushConstBoolean;
  5944. PROCEDURE PushConstSet(v: SET);
  5945. VAR value: SyntaxTree.Value; op: Operand;
  5946. BEGIN
  5947. value := SyntaxTree.NewSetValue(-1, v);
  5948. value.SetType(system.setType);
  5949. Evaluate(value, op);
  5950. Emit(Push(-1, op.op));
  5951. ReleaseOperand(op);
  5952. END PushConstSet;
  5953. PROCEDURE PushConstInteger(v: LONGINT);
  5954. VAR value: SyntaxTree.Value; op: Operand;
  5955. BEGIN
  5956. value := SyntaxTree.NewIntegerValue(-1, v);
  5957. value.SetType(system.longintType);
  5958. Evaluate(value, op);
  5959. Emit(Push(-1, op.op));
  5960. ReleaseOperand(op);
  5961. END PushConstInteger;
  5962. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5963. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5964. section: IntermediateCode.Section;
  5965. BEGIN
  5966. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5967. Global.GetSymbolSegmentedName(symbol, name);
  5968. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5969. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5970. procedure.SetScope(moduleScope);
  5971. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5972. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5973. procedure.SetAccess(SyntaxTree.Hidden);
  5974. currentScope := procedureScope;
  5975. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5976. EmitEnter(section, -1,NIL,0,0,0);
  5977. RETURN section;
  5978. END OpenInitializer;
  5979. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5980. BEGIN
  5981. EmitLeave(section, 0, 0 );
  5982. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5983. section := prev;
  5984. END CloseInitializer;
  5985. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5986. VAR name: SyntaxTree.IdentifierString;
  5987. variable: SyntaxTree.Variable;
  5988. type: SyntaxTree.Type;
  5989. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5990. BEGIN
  5991. InitOperand(op,ModeReference);
  5992. op.op := fp;
  5993. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5994. Dereference(op, x, FALSE);
  5995. result := op;
  5996. Symbol(symbol, op);
  5997. END Field;
  5998. PROCEDURE Direction(direction: LONGINT): SET;
  5999. BEGIN
  6000. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  6001. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  6002. ELSE HALT(100);
  6003. END;
  6004. END Direction;
  6005. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  6006. VAR name: ARRAY 256 OF CHAR; op: Operand;
  6007. BEGIN
  6008. Field(port, op);
  6009. ToMemory(op.op,addressType,0);
  6010. Emit(Push(-1, op.op));
  6011. ReleaseOperand(op);
  6012. Basic.GetString(modifier.identifier, name);
  6013. PushConstString(name);
  6014. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  6015. ASSERT(SemanticChecker.IsStringType(value.type));
  6016. Designate(value, op);
  6017. Emit(Push(modifier.position, op.tag));
  6018. Emit(Push(modifier.position, op.op));
  6019. ReleaseOperand(op);
  6020. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6021. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6022. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6023. Evaluate(value, op);
  6024. Emit(Push(modifier.position, op.op));
  6025. ReleaseOperand(op);
  6026. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6027. ELSE
  6028. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6029. END;
  6030. END AddPortProperty;
  6031. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  6032. VAR modifier: SyntaxTree.Modifier;
  6033. BEGIN
  6034. modifier := variable.modifiers;
  6035. WHILE modifier # NIL DO
  6036. AddPortProperty(variable,modifier, modifier.expression);
  6037. modifier := modifier.nextModifier;
  6038. END;
  6039. END AddPortProperties;
  6040. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6041. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6042. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6043. PROCEDURE PushLens(type: SyntaxTree.Type);
  6044. BEGIN
  6045. IF type IS SyntaxTree.ArrayType THEN
  6046. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6047. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6048. Emit(Push(-1, op.op));
  6049. ReleaseOperand(op);
  6050. INC(dim);
  6051. ELSE
  6052. baseType := type;
  6053. END;
  6054. END PushLens;
  6055. BEGIN
  6056. (* cell *)
  6057. IF variable.type IS SyntaxTree.ArrayType THEN
  6058. type := variable.type;
  6059. dim := 0;
  6060. PushLens(type);
  6061. portType := baseType.resolved(SyntaxTree.PortType);
  6062. ELSE
  6063. portType := variable.type(SyntaxTree.PortType);
  6064. END;
  6065. PushSelfPointer();
  6066. (* port *)
  6067. Field(variable, op);
  6068. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6069. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6070. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6071. Designate(d, op);*)
  6072. Emit(Push(-1, op.op));
  6073. ReleaseOperand(op);
  6074. (* name *)
  6075. PushConstString(name);
  6076. (* inout *)
  6077. PushConstSet(Direction(portType.direction));
  6078. (* width *)
  6079. PushConstInteger(portType.sizeInBits);
  6080. IF variable.type IS SyntaxTree.PortType THEN
  6081. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6082. AddPortProperties(variable);
  6083. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6084. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6085. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6086. Emit(Add(position,reg, sp, size));
  6087. (* dim *)
  6088. PushConstInteger(dim);
  6089. (* len array *)
  6090. Emit(Push(position, reg));
  6091. ReleaseIntermediateOperand(reg);
  6092. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6093. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6094. Emit(Add(position, sp,sp, size));
  6095. END;
  6096. END Variable;
  6097. BEGIN
  6098. IF backend.cellsAreObjects THEN
  6099. variable := x.cellScope.firstVariable;
  6100. WHILE (variable # NIL) DO
  6101. type := variable.type.resolved;
  6102. WHILE (type IS SyntaxTree.ArrayType) DO
  6103. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6104. END;
  6105. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6106. (*!!! check port arrays !! *)
  6107. Global.GetSymbolName(variable,name);
  6108. Variable(name,variable);
  6109. END;
  6110. variable := variable.nextVariable;
  6111. END;
  6112. ELSE HALT(200)
  6113. END;
  6114. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6115. (*
  6116. x.typeDeclaration.GetName(componentName);
  6117. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6118. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6119. instanceType.SetDataMemorySize(dataMemorySize);
  6120. END;
  6121. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6122. instanceType.SetInstructionMemorySize(codeMemorySize)
  6123. END;
  6124. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6125. instanceType.AddCapability(ActiveCells.VectorCapability)
  6126. END;
  6127. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6128. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6129. END;
  6130. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6131. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6132. END;
  6133. AddDevices(instanceType, x);
  6134. *)
  6135. (*
  6136. IF x.isCellNet THEN
  6137. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6138. END;
  6139. *)
  6140. (*currentActiveCellsScope := instanceType;*)
  6141. (*
  6142. parameter := x.firstParameter;
  6143. portIndex := 0;
  6144. WHILE parameter # NIL DO
  6145. parameter.GetName(parameterName);
  6146. parameterType := parameter.type.resolved;
  6147. Parameter(parameterName, parameterType);
  6148. (*
  6149. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6150. ParameterArray(parameterType);
  6151. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6152. FOR i := 0 TO len-1 DO
  6153. COPY(parameterName,name);
  6154. AppendIndex(name,i);
  6155. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6156. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6157. INC(portIndex);
  6158. END;
  6159. ELSE
  6160. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6161. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6162. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6163. INC(portIndex);
  6164. END;
  6165. *)
  6166. parameter := parameter.nextParameter;
  6167. END;
  6168. *)
  6169. (*
  6170. Scope(x.cellScope);
  6171. currentActiveCellsScope := prevActiveCellsScope;
  6172. AddModules(instanceType,x.cellScope);
  6173. *)
  6174. END AddPorts;
  6175. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6176. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6177. BEGIN
  6178. Symbol(cell,op);
  6179. ToMemory(op.op,addressType,0);
  6180. Emit(Push(position,op.op));
  6181. ReleaseOperand(op);
  6182. Basic.GetString(property.name, name);
  6183. PushConstString(name);
  6184. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6185. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6186. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6187. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6188. Designate(d, op);
  6189. IF SemanticChecker.IsStringType(property.type) THEN
  6190. Emit(Push(-1, op.tag))
  6191. END;
  6192. Emit(Push(-1, op.op));
  6193. ReleaseOperand(op);
  6194. END;
  6195. IF SemanticChecker.IsStringType(property.type) THEN
  6196. ASSERT(SemanticChecker.IsStringType(value.type));
  6197. Designate(value, op);
  6198. Emit(Push(property.position, op.tag));
  6199. Emit(Push(property.position, op.op));
  6200. ReleaseOperand(op);
  6201. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6202. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6203. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6204. Evaluate(value, op);
  6205. Emit(Push(property.position, op.op));
  6206. ReleaseOperand(op);
  6207. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6208. ELSE
  6209. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6210. END;
  6211. END AddProperty;
  6212. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6213. VAR symbol: SyntaxTree.Symbol;
  6214. BEGIN
  6215. WHILE modifier # NIL DO
  6216. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6217. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6218. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6219. ELSE
  6220. (*! move this check to checker *)
  6221. Error(modifier.position, "undefined property");
  6222. END;
  6223. modifier := modifier.nextModifier;
  6224. END;
  6225. END AddModifiers;
  6226. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6227. VAR last: SyntaxTree.Modifier;
  6228. BEGIN
  6229. IF to = NIL THEN
  6230. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6231. ELSE
  6232. last := to;
  6233. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6234. last := last.nextModifier;
  6235. END;
  6236. IF last.identifier # this.identifier THEN
  6237. ASSERT(last.nextModifier = NIL);
  6238. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6239. END;
  6240. END;
  6241. END AppendModifier;
  6242. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6243. BEGIN
  6244. WHILE this # NIL DO
  6245. AppendModifier(to, this);
  6246. this := this.nextModifier;
  6247. END;
  6248. END AppendModifiers;
  6249. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6250. VAR op: Operand;
  6251. BEGIN
  6252. Evaluate(p, op);
  6253. Emit(Push(p.position, op.op));
  6254. ReleaseOperand(op);
  6255. (*
  6256. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6257. p := p(SyntaxTree.Designator).left;
  6258. END;
  6259. IF p # NIL THEN
  6260. Evaluate(p, op);
  6261. Emit(Push(p.position, op.op));
  6262. ReleaseOperand(op);
  6263. ELSE
  6264. Emit(Push(-1, nil));
  6265. END;
  6266. *)
  6267. END PushPort;
  6268. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6269. VAR
  6270. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6271. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6272. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6273. tmp:IntermediateCode.Operand;
  6274. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6275. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6276. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6277. dest: IntermediateCode.Operand;
  6278. staticLength: LONGINT; itype: IntermediateCode.Type;
  6279. convert,isTensor: BOOLEAN;
  6280. recordType: SyntaxTree.RecordType;
  6281. baseType: SyntaxTree.Type;
  6282. flags: SET;
  6283. left: SyntaxTree.Expression;
  6284. call: SyntaxTree.Designator;
  6285. procedure: SyntaxTree.Procedure;
  6286. temporaryVariable: SyntaxTree.Variable;
  6287. dummy: IntermediateCode.Operand;
  6288. customBuiltin: SyntaxTree.CustomBuiltin;
  6289. isVarPar: ARRAY 3 OF BOOLEAN;
  6290. callsection: Sections.Section;
  6291. segmentedName: Basic.SegmentedName;
  6292. needsTrace: BOOLEAN;
  6293. n: ARRAY 256 OF CHAR;
  6294. modifier: SyntaxTree.Modifier;
  6295. previous, init: IntermediateCode.Section;
  6296. prevScope: SyntaxTree.Scope;
  6297. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6298. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6299. priority: IntermediateCode.Operand;
  6300. op,callop: Operand;
  6301. BEGIN
  6302. IF type = NIL THEN RETURN END;
  6303. type := type.resolved;
  6304. IF type IS SyntaxTree.PointerType THEN
  6305. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6306. END;
  6307. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6308. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6309. recordScope := type(SyntaxTree.RecordType).recordScope;
  6310. IF recordScope.bodyProcedure # NIL THEN
  6311. procedure := recordScope.bodyProcedure;
  6312. body := procedure.procedureScope.body;
  6313. Emit(Push(position,self));
  6314. IF body.isActive THEN
  6315. StaticCallOperand(callop,procedure);
  6316. Emit(Push(position,callop.op));
  6317. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6318. Convert(priority,sizeType);
  6319. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6320. END;
  6321. Emit(Push(position,priority));
  6322. ReleaseIntermediateOperand(priority);
  6323. IF backend.cooperative THEN
  6324. Emit(Push(position,self));
  6325. CallThis(position,"Activities","Create",3)
  6326. ELSE
  6327. flags := 0;
  6328. IF body.isSafe THEN
  6329. flags := 1;
  6330. END;
  6331. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6332. Emit(Push(position,self));
  6333. CallThis(position,"Objects","CreateProcess",4)
  6334. END;
  6335. ELSE
  6336. Emit(Push(position,self));
  6337. StaticCallOperand(callop,procedure);
  6338. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6339. END;
  6340. Emit(Pop(position,self));
  6341. END;
  6342. END CallBodies;
  6343. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6344. BEGIN
  6345. actualType := actualType.resolved;
  6346. IF actualType IS SyntaxTree.StringType THEN
  6347. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6348. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6349. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6350. ELSE
  6351. tmp := op.tag;
  6352. IntermediateCode.MakeMemory(tmp,addressType);
  6353. Emit(Push(position,tmp));
  6354. END;
  6355. Emit(Push(position,op.op))
  6356. END PushString;
  6357. PROCEDURE PushTD(type: SyntaxTree.Type);
  6358. VAR op: IntermediateCode.Operand;
  6359. BEGIN
  6360. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6361. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6362. ELSE
  6363. IF type.resolved IS SyntaxTree.PointerType THEN
  6364. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6365. END;
  6366. op := TypeDescriptorAdr(type.resolved);
  6367. IF ~newObjectFile THEN
  6368. IntermediateCode.MakeMemory(op,addressType);
  6369. END;
  6370. Emit(Push(position,op));
  6371. END
  6372. END PushTD;
  6373. BEGIN
  6374. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6375. dest := destination; destination := emptyOperand;
  6376. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6377. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6378. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6379. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6380. CASE x.id OF
  6381. (* ---- COPY ----- *)
  6382. |Global.Copy:
  6383. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6384. (* ---- EXCL, INCL----- *)
  6385. |Global.Excl,Global.Incl:
  6386. Evaluate(p0,s0);
  6387. Evaluate(p1,s1);
  6388. Convert(s1.op,setType);
  6389. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6390. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6391. END;
  6392. ReuseCopy(res,s0.op);
  6393. ReleaseOperand(s0);
  6394. Reuse1(tmp,s1.op);
  6395. ReleaseOperand(s1);
  6396. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6397. IF x.id = Global.Excl THEN
  6398. Emit(Not(position,tmp,tmp));
  6399. Emit(And(position,res,res,tmp));
  6400. ELSE
  6401. Emit(Or(position,res,res,tmp));
  6402. END;
  6403. ReleaseIntermediateOperand(tmp);
  6404. Designate(p0,s0);
  6405. ToMemory(s0.op,setType,0);
  6406. Emit(Mov(position,s0.op,res));
  6407. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6408. (* ---- DISPOSE ----- *)
  6409. |Global.Dispose:
  6410. Designate(p0,s0);
  6411. Emit(Push(position,s0.op));
  6412. ReleaseOperand(s0);
  6413. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6414. (* ---- GETPROCEDURE ----- *)
  6415. |Global.GetProcedure:
  6416. Designate(p0,s0);
  6417. PushString(s0,p0.type);
  6418. Designate(p1,s1);
  6419. PushString(s1,p1.type);
  6420. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6421. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6422. ELSE PushTD(procedureType.firstParameter.type)
  6423. END;
  6424. PushTD(procedureType.returnType);
  6425. Designate(p2,s2);
  6426. Emit(Push(position,s2.op));
  6427. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6428. CallThis(position,"Modules","GetProcedure", 7);
  6429. (* ---- ASH, LSH, ROT ----- *)
  6430. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6431. Evaluate(p0,s0);
  6432. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6433. (* make unsigned arguments in order to produced a logical shift *)
  6434. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6435. convert:= TRUE;
  6436. itype := s0.op.type;
  6437. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6438. Convert(s0.op,itype);
  6439. ELSE
  6440. convert := FALSE;
  6441. END;
  6442. END;
  6443. Evaluate(p1,s1);
  6444. IF IsIntegerConstant(p1,hint) THEN
  6445. ReuseCopy(reg,s0.op);
  6446. IF hint > 0 THEN
  6447. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6448. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6449. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6450. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6451. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6452. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6453. END;
  6454. ELSIF hint < 0 THEN
  6455. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6456. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6457. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6458. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6459. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6460. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6461. END;
  6462. END;
  6463. ReleaseOperand(s0); ReleaseOperand(s1);
  6464. ELSE
  6465. exit := NewLabel();
  6466. end := NewLabel();
  6467. ReuseCopy(reg,s0.op);
  6468. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6469. Reuse1(tmp,s1.op);
  6470. Emit(Neg(position,tmp,s1.op));
  6471. Convert(tmp,s1.op.type);
  6472. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6473. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6474. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6475. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6476. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6477. END;
  6478. ReleaseIntermediateOperand(tmp);
  6479. BrL(end);
  6480. SetLabel(exit);
  6481. ReuseCopy(tmp,s1.op);
  6482. Convert(tmp,s1.op.type);
  6483. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6484. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6485. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6486. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6487. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6488. END;
  6489. ReleaseIntermediateOperand(tmp);
  6490. SetLabel(end);
  6491. ReleaseOperand(s0); ReleaseOperand(s1);
  6492. END;
  6493. InitOperand(result,ModeValue);
  6494. IF convert THEN
  6495. itype := reg.type;
  6496. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6497. Convert(reg,itype);
  6498. END;
  6499. result.op := reg;
  6500. (* ---- CAP ----- *)
  6501. |Global.Cap:
  6502. Evaluate(p0,result);
  6503. ReuseCopy(reg,result.op);
  6504. ReleaseIntermediateOperand(result.op);
  6505. ignore := NewLabel();
  6506. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6507. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6508. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6509. SetLabel(ignore);
  6510. result.op := reg;
  6511. (* ---- CHR ----- *)
  6512. |Global.Chr, Global.Chr32:
  6513. Evaluate(p0,result);
  6514. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6515. |Global.Entier, Global.EntierH:
  6516. Evaluate(p0,result);
  6517. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6518. (* ---- MIN and MAX ----- *)
  6519. |Global.Max,Global.Min:
  6520. Evaluate(p0,s0);
  6521. Evaluate(p1,s1);
  6522. Reuse2(res,s0.op,s1.op);
  6523. else := NewLabel();
  6524. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6525. ELSE BrltL(else,s1.op,s0.op) END;
  6526. Emit(Mov(position,res,s0.op));
  6527. ReleaseOperand(s0);
  6528. end := NewLabel();
  6529. BrL(end);
  6530. SetLabel(else);
  6531. Emit(MovReplace(position,res,s1.op));
  6532. SetLabel(end);
  6533. ReleaseOperand(s1);
  6534. InitOperand(result,ModeValue);
  6535. result.op := res;
  6536. (* ---- ODD ----- *)
  6537. |Global.Odd:
  6538. IF ~conditional THEN
  6539. ConditionToValue(x)
  6540. ELSE
  6541. Evaluate(p0,result);
  6542. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6543. Reuse1(res,result.op);
  6544. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6545. ReleaseIntermediateOperand(result.op);
  6546. result.op := res;
  6547. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6548. ReleaseOperand(result);
  6549. BrL(falseLabel);
  6550. END;
  6551. (* ---- ORD ----- *)
  6552. |Global.Ord, Global.Ord32:
  6553. Evaluate(p0,result);
  6554. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6555. (* ---- SHORT, LONG ----- *)
  6556. |Global.Short, Global.Long:
  6557. Evaluate(p0,result);
  6558. IF x.type IS SyntaxTree.ComplexType THEN
  6559. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6560. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6561. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6562. ELSE
  6563. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6564. END
  6565. (* ---- HALT, SYSTEM.HALT----- *)
  6566. |Global.Halt, Global.systemHalt:
  6567. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6568. EmitTrap (position, val);
  6569. (* ---- ASSERT ----- *)
  6570. |Global.Assert:
  6571. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6572. trueL := NewLabel();
  6573. falseL := NewLabel();
  6574. Condition(p0,trueL,falseL);
  6575. IF p1 = NIL THEN val := AssertTrap
  6576. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6577. END;
  6578. SetLabel(falseL);
  6579. EmitTrap(position,val);
  6580. SetLabel(trueL);
  6581. END;
  6582. (*
  6583. Emit(TrapC(result.op,val);
  6584. *)
  6585. (* ---- INC, DEC----- *)
  6586. |Global.Inc,Global.Dec:
  6587. Expression(p0); adr := result.op;
  6588. LoadValue(result,p0.type); l := result;
  6589. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6590. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6591. END;
  6592. IF x.id = Global.Inc THEN
  6593. Emit(Add(position,l.op,l.op,r.op));
  6594. ELSE
  6595. Emit(Sub(position,l.op,l.op,r.op));
  6596. END;
  6597. ReleaseOperand(l); ReleaseOperand(r);
  6598. (* ---- LEN ----- *)
  6599. |Global.Len: (* dynamic length, static length done by checker *)
  6600. Designate(p0,operand);
  6601. IF p1 = NIL THEN
  6602. InitOperand(l,ModeValue);
  6603. l.op := IntermediateCode.Immediate(int32,0);
  6604. ELSE
  6605. Evaluate(p1,l);
  6606. END;
  6607. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6608. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6609. Dereference(operand, p0.type.resolved, FALSE);
  6610. END;
  6611. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6612. ReleaseOperand(operand); ReleaseOperand(l);
  6613. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6614. ASSERT(p1 # NIL);
  6615. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6616. Dereference(operand,p0.type.resolved,FALSE);
  6617. END;
  6618. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6619. ReleaseOperand(operand); ReleaseOperand(l);
  6620. ELSE HALT(100);
  6621. END;
  6622. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6623. (* ---- FIRST ---- *)
  6624. |Global.First:
  6625. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6626. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6627. ELSE
  6628. Designate(p0, result)
  6629. END
  6630. (* ---- LAST ---- *)
  6631. |Global.Last:
  6632. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6633. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6634. ELSE
  6635. Designate(p0, result);
  6636. (* make sure result.op is a register *)
  6637. tmp := result.op;
  6638. ReuseCopy(result.op, result.op);
  6639. ReleaseIntermediateOperand(tmp);
  6640. (* add offset to result.op *)
  6641. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6642. END
  6643. (* ---- STEP ---- *)
  6644. |Global.Step:
  6645. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6646. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6647. ELSE
  6648. Designate(p0, result);
  6649. (* make sure result.op is a register *)
  6650. tmp := result.op;
  6651. ReuseCopy(result.op, result.op);
  6652. ReleaseIntermediateOperand(tmp);
  6653. (* add offset to result.op *)
  6654. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6655. END
  6656. (* ---- RE ---- *)
  6657. |Global.Re:
  6658. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6659. Designate(p0, result)
  6660. ELSE
  6661. Evaluate(p0, result)
  6662. END
  6663. (* ---- IM ---- *)
  6664. |Global.Im:
  6665. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6666. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6667. Designate(p0, result);
  6668. (* make sure result.op is a register *)
  6669. tmp := result.op;
  6670. ReuseCopy(result.op, result.op);
  6671. ReleaseIntermediateOperand(tmp);
  6672. (* add offset to result.op *)
  6673. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6674. (* ---- ABS ----- *)
  6675. |Global.Abs:
  6676. Evaluate(p0,operand);
  6677. type := p0.type.resolved;
  6678. InitOperand(result,ModeValue);
  6679. Reuse1a(result.op,operand.op,dest);
  6680. Emit(Abs(position,result.op,operand.op));
  6681. ReleaseOperand(operand);
  6682. (* ---- WAIT ----- *)
  6683. |Global.Wait:
  6684. Evaluate(p0,operand);
  6685. Emit(Push(position,operand.op));
  6686. ReleaseOperand(operand);
  6687. CallThis(position,"Activities","Wait", 1);
  6688. (* ---- NEW ----- *)
  6689. |Global.New:
  6690. (*! the following code is only correct for "standard" Oberon calling convention *)
  6691. type := p0.type.resolved;
  6692. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6693. THEN
  6694. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6695. IF backend.cooperative THEN
  6696. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6697. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6698. IF recordType.isObject THEN
  6699. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6700. IF recordType.IsActive() THEN
  6701. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6702. END;
  6703. IF recordType.IsProtected() THEN
  6704. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6705. END;
  6706. ELSE
  6707. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6708. END;
  6709. END;
  6710. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6711. CallThis(position,"Runtime","New", 1);
  6712. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6713. Emit(Result(position, pointer));
  6714. exit := NewLabel();
  6715. BreqL(exit,pointer,nil);
  6716. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6717. GetRecordTypeName (recordType,name);
  6718. IF ~recordType.isObject THEN
  6719. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6720. END;
  6721. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6722. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6723. IF recordType.isObject THEN
  6724. IF recordType.IsProtected() THEN
  6725. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6726. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6727. END;
  6728. IF recordType.IsActive() THEN
  6729. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6730. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6731. END;
  6732. END;
  6733. END;
  6734. (* initialize fields *)
  6735. IF type(SyntaxTree.PointerType).isPlain THEN
  6736. size := 0;
  6737. ELSIF recordType.isObject THEN
  6738. size := BaseObjectTypeSize;
  6739. ELSE
  6740. size := BaseRecordTypeSize;
  6741. END;
  6742. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6743. (* call initializer *)
  6744. constructor := GetConstructor(recordType);
  6745. IF constructor # NIL THEN
  6746. (*! should be unified with ProcedureCallDesignator *)
  6747. Emit(Push(position,pointer));
  6748. ReleaseIntermediateOperand(pointer);
  6749. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6750. FOR i := 1 TO x.parameters.Length()-1 DO
  6751. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6752. formalParameter := formalParameter.nextParameter;
  6753. END;
  6754. (* static call of the constructor *)
  6755. GetCodeSectionNameForSymbol(constructor,name);
  6756. ASSERT(~constructor.isInline);
  6757. IF constructor.scope.ownerModule # module.module THEN
  6758. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6759. ELSE
  6760. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6761. END;
  6762. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6763. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6764. Emit(Pop(position,pointer));
  6765. END;
  6766. (* call bodies *)
  6767. CallBodies(pointer,p0.type);
  6768. SetLabel(exit);
  6769. needsTrace := p0.NeedsTrace();
  6770. IF needsTrace THEN ModifyAssignments(true) END;
  6771. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6772. Emit(Push(position, pointer));
  6773. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  6774. Emit(Pop(position, pointer));
  6775. END;
  6776. Designate(p0,l);
  6777. IF needsTrace THEN
  6778. CallAssignPointer(l.op, pointer);
  6779. ELSE
  6780. ToMemory(l.op,addressType,0);
  6781. Emit(Mov(position,l.op,pointer));
  6782. END;
  6783. ReleaseIntermediateOperand(pointer);
  6784. ReleaseOperand(l);
  6785. IF needsTrace THEN ModifyAssignments(false) END;
  6786. ELSE
  6787. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6788. IF temporaryVariable # NIL THEN
  6789. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6790. ELSE
  6791. Designate(p0,l);
  6792. END;
  6793. (* l.op contains address of pointer to record *)
  6794. Emit(Push(position,l.op)); (* address for use after syscall *)
  6795. Emit(Push(position,l.op));
  6796. ReleaseOperand(l);
  6797. (* push type descriptor *)
  6798. reg := TypeDescriptorAdr(recordType);
  6799. IF ~newObjectFile THEN
  6800. IntermediateCode.MakeMemory(reg,addressType);
  6801. END;
  6802. Emit(Push(position,reg));
  6803. ReleaseIntermediateOperand(reg);
  6804. (* push realtime flag *)
  6805. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6806. ELSE Emit(Push(position,false));
  6807. END;
  6808. CallThis(position,"Heaps","NewRec", 3);
  6809. (* check allocation success, if not successful then do not call initializers and bodies *)
  6810. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6811. Emit(Pop(position,pointer));
  6812. MakeMemory(reg,pointer,addressType,0);
  6813. ReleaseIntermediateOperand(pointer);
  6814. pointer := reg;
  6815. exit := NewLabel();
  6816. BreqL(exit,pointer,nil);
  6817. Emit(Push(position,pointer));
  6818. (* initialize fields *)
  6819. InitFields(recordType, pointer,0);
  6820. (* call initializer *)
  6821. constructor := GetConstructor(recordType);
  6822. IF constructor # NIL THEN
  6823. (*! should be unified with ProcedureCallDesignator *)
  6824. Emit(Push(position,pointer));
  6825. ReleaseIntermediateOperand(pointer);
  6826. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6827. FOR i := 1 TO x.parameters.Length()-1 DO
  6828. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6829. formalParameter := formalParameter.nextParameter;
  6830. END;
  6831. (* static call of the constructor *)
  6832. GetCodeSectionNameForSymbol(constructor,name);
  6833. ASSERT(~constructor.isInline);
  6834. IF constructor.scope.ownerModule # module.module THEN
  6835. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6836. ELSE
  6837. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6838. END;
  6839. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6840. ELSE
  6841. ReleaseIntermediateOperand(pointer);
  6842. END;
  6843. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6844. Emit(Pop(position,pointer));
  6845. IF temporaryVariable # NIL THEN
  6846. Designate(p0,l);
  6847. ToMemory(l.op,addressType,0);
  6848. Emit(Mov(position,l.op,pointer));
  6849. ReleaseOperand(l);
  6850. result.tag := emptyOperand;
  6851. END;
  6852. (* call bodies *)
  6853. CallBodies(pointer,p0.type);
  6854. ReleaseIntermediateOperand(pointer);
  6855. IF temporaryVariable # NIL THEN
  6856. end := NewLabel();
  6857. BrL(end);
  6858. SetLabel(exit);
  6859. Designate(p0,l);
  6860. ToMemory(l.op,addressType,0);
  6861. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6862. ReleaseOperand(l);
  6863. SetLabel(end);
  6864. ELSE
  6865. SetLabel(exit);
  6866. END;
  6867. END;
  6868. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6869. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6870. dim := 0;
  6871. IF p1 # NIL THEN
  6872. FOR i := 1 TO x.parameters.Length()-1 DO
  6873. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6874. parameter := x.parameters.GetExpression(i);
  6875. Evaluate(parameter,r);
  6876. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6877. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6878. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6879. END;
  6880. Emit(Push(position,r.op));
  6881. IF i=1 THEN
  6882. ReuseCopy(reg,r.op);
  6883. ELSE
  6884. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6885. END;
  6886. ReleaseOperand(r);
  6887. INC(dim);
  6888. END;
  6889. Convert(reg,addressType);
  6890. ELSE
  6891. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6892. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6893. END;
  6894. openDim := dim;
  6895. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6896. IF backend.cooperative THEN
  6897. size := ToMemoryUnits(system,system.SizeOf(type));
  6898. WHILE type IS SyntaxTree.ArrayType DO
  6899. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6900. END;
  6901. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6902. IF (size # 1) THEN
  6903. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6904. END;
  6905. Emit(Push(position,reg));
  6906. size := ToMemoryUnits(system,system.SizeOf(type));
  6907. IF (size # 1) THEN
  6908. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6909. END;
  6910. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6911. Emit(Push(position,reg));
  6912. ReleaseIntermediateOperand(reg);
  6913. CallThis(position,"Runtime","New", 1);
  6914. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6915. Emit(Result(position, pointer));
  6916. exit := NewLabel();
  6917. else := NewLabel();
  6918. BreqL(else,pointer,nil);
  6919. IF ~type.hasPointers THEN
  6920. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6921. ELSIF type IS SyntaxTree.RecordType THEN
  6922. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6923. ELSIF type IS SyntaxTree.ProcedureType THEN
  6924. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6925. ELSE
  6926. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6927. END;
  6928. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6929. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6930. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DataOffset * system.addressSize)),IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6931. IF type IS SyntaxTree.RecordType THEN
  6932. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6933. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6934. ELSE
  6935. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6936. END;
  6937. i := openDim;
  6938. WHILE i > 0 DO
  6939. DEC (i);
  6940. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6941. END;
  6942. needsTrace := p0.NeedsTrace();
  6943. IF needsTrace THEN ModifyAssignments(true) END;
  6944. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6945. Emit(Push(position, pointer));
  6946. CallThisChecked(position,"GarbageCollector","Watch",0,FALSE);
  6947. Emit(Pop(position, pointer));
  6948. END;
  6949. Designate(p0,l);
  6950. IF needsTrace THEN
  6951. CallAssignPointer(l.op, pointer);
  6952. ModifyAssignments(false);
  6953. ELSE
  6954. ToMemory(l.op,addressType,0);
  6955. Emit(Mov(position,l.op,pointer));
  6956. END;
  6957. ReleaseIntermediateOperand(pointer);
  6958. ReleaseOperand(l);
  6959. BrL(exit);
  6960. SetLabel(else);
  6961. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6962. Designate(p0,l);
  6963. IF needsTrace THEN
  6964. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6965. ELSE
  6966. ToMemory(l.op,addressType,0);
  6967. Emit(Mov(position,l.op,pointer));
  6968. END;
  6969. ReleaseOperand(l);
  6970. SetLabel(exit);
  6971. ELSE
  6972. (*! the following code is only correct for "standard" Oberon calling convention *)
  6973. IF SemanticChecker.ContainsPointer(type) THEN
  6974. IF type IS SyntaxTree.ArrayType THEN
  6975. staticLength := 1;
  6976. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6977. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6978. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6979. END;
  6980. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6981. Emit(Mul(position,reg,reg,tmp));
  6982. END;
  6983. Designate(p0,l);
  6984. IF openDim > 0 THEN
  6985. Emit(Push(position,l.op)); (* address for use after syscall *)
  6986. END;
  6987. Emit(Push(position,l.op)); (* address *)
  6988. ReleaseOperand(l);
  6989. tmp := TypeDescriptorAdr(type);
  6990. IF ~newObjectFile THEN
  6991. IntermediateCode.MakeMemory(tmp,addressType);
  6992. END;
  6993. Emit(Push(position,tmp)); (* type descriptor *)
  6994. ReleaseIntermediateOperand(tmp);
  6995. Emit(Push(position,reg)); (* number Elements *)
  6996. ReleaseIntermediateOperand(reg);
  6997. tmp := IntermediateCode.Immediate(addressType,dim);
  6998. Emit(Push(position,tmp)); (* dimensions *)
  6999. (* push realtime flag *)
  7000. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7001. ELSE Emit(Push(position,false));
  7002. END;
  7003. CallThis(position,"Heaps","NewArr",5)
  7004. ELSE
  7005. size := ToMemoryUnits(system,system.SizeOf(type));
  7006. IF (size # 1) THEN
  7007. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7008. END;
  7009. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  7010. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  7011. Emit(Add(position,reg,reg,tmp));
  7012. Designate(p0,l);
  7013. IF openDim >0 THEN
  7014. Emit(Push(position,l.op)); (* address for use after syscall *)
  7015. END;
  7016. Emit(Push(position,l.op)); (* address for syscall *)
  7017. ReleaseOperand(l); (* pointer address *)
  7018. Emit(Push(position,reg)); (* size *)
  7019. ReleaseIntermediateOperand(reg);
  7020. (* push realtime flag *)
  7021. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  7022. ELSE Emit(Push(position,false));
  7023. END;
  7024. CallThis(position,"Heaps","NewSys", 3)
  7025. END;
  7026. IF openDim > 0 THEN
  7027. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7028. Emit(Pop(position,adr));
  7029. ToMemory(adr,addressType,0);
  7030. ReuseCopy(tmp,adr);
  7031. ReleaseIntermediateOperand(adr);
  7032. adr := tmp;
  7033. else := NewLabel();
  7034. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7035. i := openDim-1;
  7036. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7037. WHILE (i >= 0) DO
  7038. Emit(Pop(position,reg));
  7039. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7040. Emit(Mov(position,res,reg));
  7041. DEC(i);
  7042. END;
  7043. ReleaseIntermediateOperand(adr);
  7044. ReleaseIntermediateOperand(reg);
  7045. exit := NewLabel();
  7046. BrL(exit);
  7047. SetLabel(else);
  7048. (* else part: array could not be allocated *)
  7049. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7050. Emit(Add(position,sp,sp,tmp));
  7051. SetLabel(exit);
  7052. END;
  7053. END;
  7054. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7055. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7056. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7057. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7058. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7059. left.SetType(procedure.type);
  7060. formalParameter := procedureType.firstParameter;
  7061. (* push array to allocate *)
  7062. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7063. formalParameter :=formalParameter.nextParameter;
  7064. (* push length array *)
  7065. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7066. (* push size *)
  7067. type := t0;
  7068. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7069. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7070. END;
  7071. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7072. Emit(Push(position,tmp));
  7073. (* *)
  7074. IF SemanticChecker.ContainsPointer(type) THEN
  7075. tmp := TypeDescriptorAdr(type);
  7076. IF ~newObjectFile THEN
  7077. IntermediateCode.MakeMemory(tmp,addressType);
  7078. END;
  7079. ELSE
  7080. tmp := IntermediateCode.Immediate(addressType, 0);
  7081. END;
  7082. Emit(Push(position,tmp)); (* type descriptor *)
  7083. StaticCallOperand(result,procedure);
  7084. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7085. ReleaseOperand(result);
  7086. END;
  7087. (*
  7088. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7089. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7090. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7091. *)
  7092. ELSE
  7093. dim := 0;
  7094. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7095. (* generate geometry descriptor *)
  7096. Designate(p0,l);
  7097. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7098. ReleaseOperand(l);
  7099. isTensor := TRUE;
  7100. ELSE
  7101. isTensor := FALSE;
  7102. END;
  7103. FOR i := 1 TO x.parameters.Length()-1 DO
  7104. IF ~isTensor THEN
  7105. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7106. END;
  7107. parameter := x.parameters.GetExpression(i);
  7108. Evaluate(parameter,r);
  7109. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7110. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7111. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7112. END;
  7113. Emit(Push(position,r.op));
  7114. IF i=1 THEN
  7115. ReuseCopy(reg,r.op);
  7116. ELSE
  7117. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7118. END;
  7119. ReleaseOperand(r);
  7120. INC(dim);
  7121. END;
  7122. Convert(reg,addressType);
  7123. openDim := dim;
  7124. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7125. (*! the following code is only correct for "standard" Oberon calling convention *)
  7126. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7127. t := type;
  7128. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7129. staticLength := 1;
  7130. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7131. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7132. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7133. END;
  7134. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7135. Emit(Mul(position,reg,reg,tmp));
  7136. END;
  7137. Designate(p0,l);
  7138. IF isTensor THEN
  7139. Dereference(l,type,FALSE);
  7140. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7141. END;
  7142. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7143. Emit(Push(position,l.tag)); (* address *)
  7144. ReleaseOperand(l);
  7145. tmp := TypeDescriptorAdr(t);
  7146. IF ~newObjectFile THEN
  7147. IntermediateCode.MakeMemory(tmp,addressType);
  7148. END;
  7149. Emit(Push(position,tmp)); (* type descriptor *)
  7150. ReleaseIntermediateOperand(tmp);
  7151. Emit(Push(position,reg)); (* number Elements *)
  7152. ReleaseIntermediateOperand(reg);
  7153. tmp := IntermediateCode.Immediate(addressType,0);
  7154. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7155. (* push realtime flag: false by default *)
  7156. Emit(Push(position,false));
  7157. CallThis(position,"Heaps","NewArr",5);
  7158. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7159. Emit(Pop(position,adr));
  7160. GetMathArrayField(tmp,adr,MathPtrOffset);
  7161. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7162. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7163. PutMathArrayField(adr,reg,MathAdrOffset);
  7164. ReleaseIntermediateOperand(tmp);
  7165. ReleaseIntermediateOperand(reg);
  7166. ELSE
  7167. IF isTensor THEN
  7168. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7169. ELSE
  7170. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7171. END;
  7172. IF (size # 1) THEN
  7173. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7174. END;
  7175. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7176. Emit(Add(position,reg,reg,tmp));
  7177. Designate(p0,l);
  7178. IF isTensor THEN
  7179. Dereference(l,type,FALSE);
  7180. END;
  7181. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7182. Emit(Push(position,l.tag)); (* address for syscall *)
  7183. ReleaseOperand(l); (* pointer address *)
  7184. Emit(Push(position,reg)); (* size *)
  7185. ReleaseIntermediateOperand(reg);
  7186. (* push realtime flag: false by default *)
  7187. Emit(Push(position,false));
  7188. CallThis(position,"Heaps","NewSys",3);
  7189. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7190. Emit(Pop(position,adr));
  7191. GetMathArrayField(tmp,adr,MathPtrOffset);
  7192. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7193. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7194. PutMathArrayField(adr,reg,MathAdrOffset);
  7195. ReleaseIntermediateOperand(tmp);
  7196. ReleaseIntermediateOperand(reg);
  7197. END;
  7198. flags := {};
  7199. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7200. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7201. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7202. PutMathArrayField(adr,tmp,MathDimOffset);
  7203. else := NewLabel();
  7204. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7205. i := openDim-1;
  7206. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7207. IF isTensor THEN
  7208. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7209. ELSE
  7210. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7211. END;
  7212. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7213. WHILE (i >= 0) DO
  7214. Emit(Pop(position,reg));
  7215. PutMathArrayLength(adr,reg,i);
  7216. PutMathArrayIncrement(adr,tmp,i);
  7217. IF i > 0 THEN
  7218. IF i=openDim-1 THEN
  7219. ReuseCopy(tmp,tmp);
  7220. END;
  7221. Emit(Mul(position,tmp,tmp,reg));
  7222. END;
  7223. DEC(i);
  7224. END;
  7225. ReleaseIntermediateOperand(adr);
  7226. ReleaseIntermediateOperand(reg);
  7227. ReleaseIntermediateOperand(tmp);
  7228. exit := NewLabel();
  7229. BrL(exit);
  7230. SetLabel(else);
  7231. (* else part: array could not be allocated *)
  7232. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7233. Emit(Add(position,sp,sp,tmp));
  7234. SetLabel(exit);
  7235. END;
  7236. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7237. THEN
  7238. IF ~backend.cellsAreObjects THEN RETURN END;
  7239. IF InCellScope(currentScope) THEN
  7240. PushSelfPointer()
  7241. ELSE
  7242. Emit(Push(position, nil));
  7243. END;
  7244. (* push temp address *)
  7245. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7246. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7247. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7248. (* l.op contains address of pointer to record *)
  7249. Emit(Push(position,l.op)); (* address for use after syscall *)
  7250. ReleaseOperand(l);
  7251. (* push type descriptor *)
  7252. reg := TypeDescriptorAdr(baseType);
  7253. IF ~newObjectFile THEN
  7254. IntermediateCode.MakeMemory(reg,addressType);
  7255. END;
  7256. Emit(Push(position,reg));
  7257. ReleaseIntermediateOperand(reg);
  7258. (* push name *)
  7259. (*Global.GetSymbolName(p0, n);*)
  7260. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7261. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7262. ELSE
  7263. Global.GetModuleName(module.module, n);
  7264. END;
  7265. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7266. (*type.typeDeclaration.GetName(n);*)
  7267. PushConstString(n);
  7268. (* push cellnet boolean *)
  7269. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7270. (* push engine boolean *)
  7271. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7272. (* allocate *)
  7273. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7274. (* add capabilities *)
  7275. modifier := p0(SyntaxTree.Designator).modifiers;
  7276. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7277. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7278. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7279. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7280. END;
  7281. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7282. (*
  7283. modifier := baseType(SyntaxTree.CellType).modifiers;
  7284. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7285. modifier := p0(SyntaxTree.Designator).modifiers;
  7286. *)
  7287. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7288. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7289. (* l.op contains address of pointer to record *)
  7290. ToMemory(l.op,addressType,0);
  7291. (* l.op contains value of pointer to record *)
  7292. Emit(Push(position,l.op)); (* address for use after syscall *)
  7293. ReleaseOperand(l);
  7294. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7295. prevScope := currentScope;
  7296. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7297. previous := section;
  7298. section := init;
  7299. (* add ports *)
  7300. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7301. CloseInitializer(previous);
  7302. currentScope := prevScope;
  7303. Symbol(temporaryVariable,l);
  7304. ToMemory(l.op,addressType,0);
  7305. Emit(Push(position,l.op));
  7306. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7307. (*
  7308. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7309. IF constructor # NIL THEN
  7310. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7311. FOR i := 1 TO x.parameters.Length()-1 DO
  7312. p := x.parameters.GetExpression(i);
  7313. Global.GetSymbolName(parameter,name);
  7314. Evaluate(p, value);
  7315. ASSERT(value.type # NIL);
  7316. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7317. par := instance.AddParameter(name);
  7318. par.SetInteger(value.integer);
  7319. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7320. par := instance.AddParameter(name);
  7321. par.SetBoolean(value.boolean);
  7322. ELSE Error(x.position,NotYetImplemented)
  7323. END;
  7324. parameter := parameter.nextParameter
  7325. END;
  7326. END;
  7327. *)
  7328. (* call initializer *)
  7329. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7330. IF constructor # NIL THEN
  7331. (*! should be unified with ProcedureCallDesignator *)
  7332. IF backend.cellsAreObjects THEN
  7333. Symbol(temporaryVariable,l);
  7334. ToMemory(l.op,addressType,0);
  7335. Emit(Push(position,l.op));
  7336. ReleaseOperand(l);
  7337. END;
  7338. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7339. FOR i := 1 TO x.parameters.Length()-1 DO
  7340. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7341. formalParameter := formalParameter.nextParameter;
  7342. END;
  7343. (* static call of the constructor *)
  7344. Global.GetSymbolSegmentedName(constructor,name);
  7345. ASSERT(~constructor.isInline);
  7346. IF constructor.scope.ownerModule # module.module THEN
  7347. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7348. ELSE
  7349. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7350. END;
  7351. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7352. (*ELSE
  7353. ReleaseIntermediateOperand(pointer);*)
  7354. END;
  7355. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7356. ToMemory(l.op, addressType, 0);
  7357. Designate(p0,s0);
  7358. ToMemory(s0.op,addressType,0);
  7359. Emit(Mov(position,s0.op,l.op));
  7360. ReleaseOperand(l);
  7361. ReleaseOperand(s0);
  7362. result.tag := emptyOperand;
  7363. (* start *)
  7364. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7365. (* push cell *)
  7366. Symbol(temporaryVariable, l);
  7367. ToMemory(l.op,addressType,0);
  7368. Emit(Push(-1,l.op));
  7369. (* push delegate *)
  7370. Emit(Push(-1,l.op));
  7371. ReleaseOperand(l);
  7372. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7373. Emit(Push(position, s1.op));
  7374. ReleaseOperand(s1);
  7375. CallThis(position,"ActiveCellsRuntime","Start",3);
  7376. END;
  7377. (*IF temporaryVariable # NIL THEN
  7378. end := NewLabel();
  7379. BrL(end);
  7380. SetLabel(exit);
  7381. Designate(p0,l);
  7382. ToMemory(l.op,addressType,0);
  7383. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7384. ReleaseOperand(l);
  7385. SetLabel(end);
  7386. ELSE
  7387. SetLabel(exit);
  7388. END;
  7389. *)
  7390. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7391. ELSE (* no pointer to record, no pointer to array *)
  7392. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7393. (* ignore new statement *)
  7394. Warning(p0.position, "cannot run on final hardware");
  7395. ELSE
  7396. HALT(200);
  7397. END;
  7398. END;
  7399. (* ---- ADDRESSOF----- *)
  7400. |Global.systemAdr:
  7401. Designate(p0,s0);
  7402. s0.mode := ModeValue;
  7403. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7404. ReleaseIntermediateOperand(s0.op);
  7405. s0.op := s0.tag;
  7406. IntermediateCode.InitOperand(s0.tag);
  7407. END;
  7408. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7409. result := s0;
  7410. (* ---- BIT ----- *)
  7411. |Global.systemBit:
  7412. Evaluate(p0,s0);
  7413. ToMemory(s0.op,addressType,0);
  7414. ReuseCopy(res,s0.op);
  7415. ReleaseOperand(s0);
  7416. Evaluate(p1,s1);
  7417. Emit(Ror(position,res,res,s1.op));
  7418. ReleaseOperand(s1);
  7419. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7420. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7421. IF ~conditional THEN
  7422. InitOperand(result,ModeValue); result.op := res;
  7423. ELSE
  7424. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7425. BrL(falseLabel);
  7426. ReleaseIntermediateOperand(res);
  7427. END;
  7428. (* --- MSK ----*)
  7429. |Global.systemMsk:
  7430. Evaluate(p0,s0);
  7431. Evaluate(p1,s1);
  7432. ReuseCopy(res,s0.op);
  7433. ReleaseOperand(s0);
  7434. Emit(And(position,res,res,s1.op));
  7435. ReleaseOperand(s1);
  7436. InitOperand(result,ModeValue);
  7437. result.op := res;
  7438. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7439. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7440. Evaluate(p0,s0);
  7441. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7442. ReleaseOperand(s0);
  7443. InitOperand(result,ModeValue);
  7444. result.op := res;
  7445. (* ---- SYSTEM.GetStackPointer ----- *)
  7446. |Global.systemGetStackPointer:
  7447. InitOperand(result,ModeValue);
  7448. result.op := sp;
  7449. (* ---- SYSTEM.GetFramePointer ----- *)
  7450. |Global.systemGetFramePointer:
  7451. InitOperand(result,ModeValue);
  7452. result.op := fp;
  7453. (* ---- SYSTEM.GetActivity ----- *)
  7454. |Global.systemGetActivity:
  7455. ASSERT(backend.cooperative);
  7456. InitOperand(result,ModeValue);
  7457. result.op := ap;
  7458. (* ---- SYSTEM.SetStackPointer ----- *)
  7459. |Global.systemSetStackPointer:
  7460. Evaluate(p0,s0); (* *)
  7461. Emit(Mov(position,sp,s0.op));
  7462. ReleaseOperand(s0);
  7463. (* ---- SYSTEM.SetFramePointer ----- *)
  7464. |Global.systemSetFramePointer:
  7465. Evaluate(p0,s0); (* *)
  7466. Emit(Mov(position,fp,s0.op));
  7467. ReleaseOperand(s0);
  7468. (* ---- SYSTEM.Activity ----- *)
  7469. |Global.systemSetActivity:
  7470. ASSERT(backend.cooperative);
  7471. Evaluate(p0,s0); (* *)
  7472. Emit(Mov(position,ap,s0.op));
  7473. ReleaseOperand(s0);
  7474. (* ---- SYSTEM.VAL ----- *)
  7475. |Global.systemVal:
  7476. Expression(p1);
  7477. s1 := result;
  7478. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7479. IF s1.mode = ModeReference THEN
  7480. (* nothing to be done if not record type, just take over new type *)
  7481. IF (type IS SyntaxTree.RecordType) THEN
  7482. ReleaseIntermediateOperand(s1.tag);
  7483. s1.tag := TypeDescriptorAdr(type);
  7484. IF ~newObjectFile THEN
  7485. IntermediateCode.MakeMemory(s1.tag,addressType);
  7486. END;
  7487. UseIntermediateOperand(s1.tag);
  7488. END;
  7489. result := s1;
  7490. ELSE (* copy over result to different type, may not use convert *)
  7491. itype := IntermediateCode.GetType(system,type);
  7492. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7493. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7494. Emit(Mov(position,s0.op,s1.op));
  7495. ReleaseOperand(s1);
  7496. InitOperand(result,ModeValue);
  7497. result.op := s0.op;
  7498. ELSE (* different size, must convert *)
  7499. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7500. Convert(s1.op, IntermediateCode.GetType(system,type));
  7501. result := s1;
  7502. END;
  7503. END;
  7504. (* ---- SYSTEM.GET ----- *)
  7505. |Global.systemGet:
  7506. Evaluate(p0,s0); (* adr *)
  7507. Designate(p1,s1); (* variable *)
  7508. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7509. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7510. Emit(Mov(position,s1.op,s0.op));
  7511. ReleaseOperand(s1);
  7512. ReleaseOperand(s0);
  7513. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7514. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7515. Evaluate(p0,s0); (* *)
  7516. Evaluate(p1,s1); (* variable *)
  7517. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7518. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7519. (* real part *)
  7520. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7521. Emit(Mov(position,res, s1.op));
  7522. ReleaseIntermediateOperand(res);
  7523. (* imaginary part *)
  7524. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7525. Emit(Mov(position,res, s1.tag));
  7526. ReleaseIntermediateOperand(res);
  7527. ReleaseOperand(s1);
  7528. ReleaseOperand(s0);
  7529. ELSE
  7530. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7531. ReleaseOperand(s0);
  7532. Emit(Mov(position,res,s1.op));
  7533. ReleaseIntermediateOperand(res);
  7534. ReleaseOperand(s1);
  7535. END;
  7536. (* ---- SYSTEM.MOVE ----- *)
  7537. |Global.systemMove:
  7538. Evaluate(p0,s0);
  7539. Evaluate(p1,s1);
  7540. Evaluate(p2,s2);
  7541. Emit(Copy(position,s1.op,s0.op,s2.op));
  7542. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7543. (* ---- SYSTEM.NEW ----- *)
  7544. |Global.systemNew:
  7545. Designate(p0,s0);
  7546. Emit(Push(position,s0.op));
  7547. ReleaseOperand(s0);
  7548. Evaluate(p1,s1);
  7549. Emit(Push(position,s1.op));
  7550. ReleaseOperand(s1);
  7551. (* push realtime flag: false by default *)
  7552. Emit(Push(position,false));
  7553. CallThis(position,"Heaps","NewSys",3);
  7554. (* ---- SYSTEM.CALL ----- *)
  7555. |Global.systemRef:
  7556. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7557. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7558. s0.mode := ModeValue;
  7559. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7560. result := s0
  7561. (* ---- INCR ----- *)
  7562. |Global.Incr:
  7563. Designate(p0,operand);
  7564. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7565. Dereference(operand,p0.type.resolved,FALSE);
  7566. END;
  7567. ASSERT(p1 # NIL);
  7568. Evaluate(p1,l);
  7569. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7570. ReleaseOperand(operand); ReleaseOperand(l);
  7571. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7572. (* ---- SUM ----- *)
  7573. |Global.Sum: HALT(200);
  7574. (* ---- CAS ----- *)
  7575. |Global.Cas:
  7576. needsTrace := p0.NeedsTrace();
  7577. IF needsTrace THEN ModifyAssignments(true) END;
  7578. Designate(p0,s0);
  7579. Evaluate(p1,s1);
  7580. Evaluate(p2,s2);
  7581. IF needsTrace THEN
  7582. Emit(Push(position, s0.op));
  7583. Emit(Push(position, s1.op));
  7584. Emit(Push(position, s2.op));
  7585. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7586. ELSE
  7587. Emit(Cas(position,s0.op,s1.op,s2.op));
  7588. END;
  7589. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7590. IF needsTrace THEN ModifyAssignments(false) END;
  7591. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7592. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7593. IF conditional THEN
  7594. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7595. BrL(falseLabel);
  7596. ReleaseIntermediateOperand(res);
  7597. END;
  7598. (* ---- DIM ----- *)
  7599. |Global.Dim:
  7600. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7601. Designate(p0,s0);
  7602. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7603. Dereference(s0,p0.type.resolved,FALSE);
  7604. END;
  7605. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7606. ReleaseOperand(s0);
  7607. (* ---- RESHAPE ----- *)
  7608. |Global.Reshape:
  7609. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7610. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7611. left.SetType(procedure.type);
  7612. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7613. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7614. END;
  7615. (* ---- SYSTEM.TYPECODE ----- *)
  7616. |Global.systemTypeCode:
  7617. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7618. IF type.resolved IS SyntaxTree.PointerType THEN
  7619. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7620. END;
  7621. result.op := TypeDescriptorAdr(type);
  7622. IF ~newObjectFile THEN
  7623. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7624. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7625. END;
  7626. result.mode := ModeValue;
  7627. (* ---- SYSTEM.TRACE ----- *)
  7628. |Global.systemTrace:
  7629. SystemTrace(x.parameters, x.position);
  7630. (* ----- CONNECT ------*)
  7631. |Global.Connect:
  7632. IF backend.cellsAreObjects THEN
  7633. PushPort(p0);
  7634. PushPort(p1);
  7635. IF p2 # NIL THEN
  7636. Evaluate(p2, s2);
  7637. Emit(Push(p2.position, s2.op));
  7638. ReleaseOperand(s2);
  7639. ELSE
  7640. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7641. END;
  7642. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7643. ELSE
  7644. Warning(x.position, "cannot run on final hardware");
  7645. END;
  7646. (* ----- DELEGATE ------*)
  7647. |Global.Delegate:
  7648. IF backend.cellsAreObjects THEN
  7649. PushPort(p0);
  7650. PushPort(p1);
  7651. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7652. ELSE
  7653. Warning(x.position, "cannot run on final hardware");
  7654. END;
  7655. (* ----- SEND ------*)
  7656. |Global.Send:
  7657. Evaluate(p0,s0);
  7658. Evaluate(p1,s1);
  7659. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7660. Emit(Push(position,s0.op));
  7661. Emit(Push(position,s1.op));
  7662. (*
  7663. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7664. *)
  7665. IF ~backend.cellsAreObjects THEN
  7666. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7667. END;
  7668. ReleaseOperand(s0);
  7669. ReleaseOperand(s1);
  7670. IF backend.cellsAreObjects THEN
  7671. CallThis(position,"ActiveCellsRuntime","Send",2);
  7672. ELSE
  7673. CallThis(position,ChannelModuleName,"Send",2);
  7674. END;
  7675. (* ----- RECEIVE ------*)
  7676. |Global.Receive:
  7677. Evaluate(p0,s0);
  7678. Emit(Push(position,s0.op));
  7679. Designate(p1,s1);
  7680. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7681. Emit(Push(position,s1.op));
  7682. IF p2 # NIL THEN
  7683. Designate(p2,s2);
  7684. Emit(Push(position,s2.op));
  7685. END;
  7686. (*
  7687. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7688. *)
  7689. IF ~backend.cellsAreObjects THEN
  7690. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7691. END;
  7692. ReleaseOperand(s0);
  7693. ReleaseOperand(s1);
  7694. ReleaseOperand(s2);
  7695. IF backend.cellsAreObjects THEN
  7696. IF p2 = NIL THEN
  7697. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7698. ELSE
  7699. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7700. END;
  7701. ELSE
  7702. IF p2 = NIL THEN
  7703. CallThis(position,ChannelModuleName,"Receive",2)
  7704. ELSE
  7705. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7706. END;
  7707. END;
  7708. | Global.systemSpecial:
  7709. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7710. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7711. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7712. (* determine if parameters are of the VAR kind *)
  7713. ASSERT(x.parameters.Length() <= 3);
  7714. formalParameter := procedureType.firstParameter;
  7715. FOR i := 0 TO x.parameters.Length() - 1 DO
  7716. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7717. formalParameter := formalParameter.nextParameter
  7718. END;
  7719. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7720. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7721. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7722. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7723. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7724. IF procedureType.returnType # NIL THEN
  7725. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7726. Emit(Result(position, res));
  7727. (*InitOperand(result,ModeValue);
  7728. result.op := res;
  7729. *)
  7730. IF ~conditional THEN
  7731. InitOperand(result,ModeValue); result.op := res;
  7732. ELSE
  7733. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7734. BrL(falseLabel);
  7735. ReleaseIntermediateOperand(res);
  7736. END;
  7737. END
  7738. ELSE (* function not yet implemented *)
  7739. Error(position,"not yet implemented");
  7740. END;
  7741. destination := dest;
  7742. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7743. END VisitBuiltinCallDesignator;
  7744. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7745. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7746. BEGIN
  7747. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7748. dest := destination; destination := emptyOperand;
  7749. Expression(x.left);
  7750. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7751. ELSIF isUnchecked THEN (* no check *)
  7752. ELSE
  7753. trueL := NewLabel();
  7754. falseL := NewLabel();
  7755. IF IsPointerToRecord(x.left.type,recordType) THEN
  7756. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7757. Emit(Mov(position,tag, result.op));
  7758. IF result.mode # ModeValue THEN
  7759. ptr := tag;
  7760. IntermediateCode.MakeMemory(ptr,addressType);
  7761. Emit(Mov(position,tag, ptr));
  7762. END;
  7763. IF ~backend.cooperative THEN
  7764. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7765. END;
  7766. IntermediateCode.MakeMemory(tag,addressType);
  7767. ELSE
  7768. tag := result.tag;
  7769. END;
  7770. TypeTest(tag,x.type,trueL,falseL);
  7771. ReleaseIntermediateOperand(tag);
  7772. SetLabel(falseL);
  7773. EmitTrap(position,TypeCheckTrap);
  7774. SetLabel(trueL);
  7775. END;
  7776. destination := dest;
  7777. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7778. END VisitTypeGuardDesignator;
  7779. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7780. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7781. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7782. VAR label: Label; pc: LONGINT;
  7783. BEGIN
  7784. IF backend.cooperative & ~isUnchecked THEN
  7785. pc := section.pc;
  7786. label := NewLabel();
  7787. BrneL(label, operand.op, nil);
  7788. EmitTrap(position, NilPointerTrap);
  7789. SetLabel(label);
  7790. INC(statCoopNilCheck, section.pc - pc);
  7791. END;
  7792. END NilCheck;
  7793. BEGIN
  7794. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7795. ReuseCopy(dereferenced,operand.op);
  7796. ReleaseOperand(operand);
  7797. operand.mode := ModeReference;
  7798. operand.op := dereferenced;
  7799. operand.tag := dereferenced;
  7800. UseIntermediateOperand(operand.tag);
  7801. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7802. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7803. ReleaseIntermediateOperand(operand.tag);
  7804. operand.tag := TypeDescriptorAdr(type);
  7805. IF ~newObjectFile THEN
  7806. IntermediateCode.MakeMemory(operand.tag,addressType);
  7807. END;
  7808. ELSE
  7809. IF ~backend.cooperative THEN
  7810. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7811. END;
  7812. IntermediateCode.MakeMemory(operand.tag,addressType);
  7813. END;
  7814. NilCheck(operand.op);
  7815. ELSIF type IS SyntaxTree.ArrayType THEN
  7816. IF isUnsafe THEN
  7817. NilCheck(operand.op);
  7818. ReleaseIntermediateOperand(operand.tag);
  7819. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7820. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7821. ELSE
  7822. operand.tag := emptyOperand;
  7823. END;
  7824. ELSE
  7825. NilCheck(operand.op);
  7826. IF backend.cooperative THEN
  7827. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7828. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7829. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7830. ELSE
  7831. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7832. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7833. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7834. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7835. END;
  7836. END;
  7837. ELSIF type IS SyntaxTree.MathArrayType THEN
  7838. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7839. IntermediateCode.MakeMemory(operand.op,addressType);
  7840. ELSE HALT(100);
  7841. END;
  7842. END Dereference;
  7843. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7844. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7845. BEGIN
  7846. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7847. dest := destination; destination := emptyOperand;
  7848. Evaluate(x.left,d);
  7849. type := x.type.resolved;
  7850. prevIsUnchecked := isUnchecked;
  7851. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7852. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7853. END;
  7854. Dereference(d,type,IsUnsafePointer(x.left.type));
  7855. isUnchecked := prevIsUnchecked;
  7856. result := d;
  7857. IF backend.cooperative & (x.left.type.resolved IS SyntaxTree.PointerType) & ~x.left.type.resolved(SyntaxTree.PointerType).isPlain & ~x.left.type.resolved(SyntaxTree.PointerType).isUnsafe THEN
  7858. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7859. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7860. END;
  7861. END;
  7862. destination := dest;
  7863. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7864. END VisitDereferenceDesignator;
  7865. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7866. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7867. BEGIN
  7868. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7869. dest := destination; destination := emptyOperand;
  7870. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7871. tag := result.op;
  7872. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7873. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7874. StaticCallOperand(result,procedure.super);
  7875. ReleaseIntermediateOperand(result.tag);
  7876. UseIntermediateOperand(tag); (* necessary ? *)
  7877. result.tag := tag;
  7878. destination := dest;
  7879. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7880. END VisitSupercallDesignator;
  7881. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7882. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7883. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7884. name: Basic.SegmentedName;
  7885. BEGIN
  7886. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7887. dest := destination; destination := emptyOperand;
  7888. scope := currentScope;
  7889. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7890. scope := scope.outerScope;
  7891. END;
  7892. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7893. IF newObjectFile THEN
  7894. moduleSection := meta.ModuleSection();
  7895. IF backend.cooperative THEN
  7896. moduleOffset := 0;
  7897. ELSE
  7898. moduleOffset := moduleSection.pc;
  7899. END;
  7900. result.mode := ModeValue;
  7901. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7902. ELSE
  7903. Symbol(moduleSelf,result);
  7904. IntermediateCode.MakeMemory(result.op,addressType);
  7905. END
  7906. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7907. result.mode := ModeValue;
  7908. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7909. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7910. ELSE
  7911. GetBaseRegister(basereg,currentScope,scope);
  7912. InitOperand(result,ModeReference);
  7913. result.op := basereg;
  7914. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7915. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7916. IF backend.cooperative THEN
  7917. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7918. END;
  7919. (* tag must be loaded when dereferencing SELF pointer *)
  7920. END;
  7921. destination := dest;
  7922. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7923. END VisitSelfDesignator;
  7924. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7925. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7926. BEGIN
  7927. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7928. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7929. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7930. parameter := procedureType.returnParameter;
  7931. VisitParameter(parameter);
  7932. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7933. END VisitResultDesignator;
  7934. (** values *)
  7935. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7936. BEGIN
  7937. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7938. IF conditional THEN
  7939. IF x.value THEN BrL(trueLabel)
  7940. ELSE BrL(falseLabel)
  7941. END;
  7942. ELSE
  7943. InitOperand(result,ModeValue);
  7944. IF x.value THEN result.op := true ELSE result.op := false END;
  7945. END;
  7946. END VisitBooleanValue;
  7947. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7948. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7949. BEGIN
  7950. Global.GetModuleSegmentedName(module.module, name);
  7951. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7952. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7953. RETURN section
  7954. END GetDataSection;
  7955. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7956. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7957. BEGIN
  7958. type := vop.type;
  7959. data := GetDataSection();
  7960. pc := EnterImmediate(data,vop);
  7961. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7962. IntermediateCode.MakeMemory(vop, type);
  7963. END GetImmediateMem;
  7964. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7965. BEGIN
  7966. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7967. InitOperand(result,ModeValue);
  7968. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7969. IF ~supportedImmediate(result.op) &~inData THEN
  7970. GetImmediateMem(result.op)
  7971. END;
  7972. END VisitIntegerValue;
  7973. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7974. BEGIN
  7975. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7976. InitOperand(result,ModeValue);
  7977. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7978. END VisitCharacterValue;
  7979. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7980. BEGIN
  7981. IF Trace THEN TraceEnter("VisitSetValue") END;
  7982. InitOperand(result,ModeValue);
  7983. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7984. END VisitSetValue;
  7985. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7986. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7987. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7988. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7989. BEGIN
  7990. numberElements := x.elements.Length();
  7991. FOR i := 0 TO numberElements-1 DO
  7992. expression := x.elements.GetExpression(i);
  7993. IF expression IS SyntaxTree.MathArrayExpression THEN
  7994. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7995. ELSE
  7996. inData := TRUE;
  7997. Evaluate(expression,op);
  7998. irv.Emit(Data(position,op.op));
  7999. inData := FALSE;
  8000. ReleaseOperand(op);
  8001. END;
  8002. END;
  8003. END RecursiveData;
  8004. BEGIN
  8005. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  8006. IF ~TryConstantDeclaration() THEN
  8007. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8008. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8009. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8010. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  8011. ELSE
  8012. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8013. END;
  8014. RecursiveData(x.array);
  8015. InitOperand(result,ModeReference);
  8016. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8017. END
  8018. END VisitMathArrayValue;
  8019. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  8020. VAR constant: Sections.Section;
  8021. BEGIN
  8022. IF constantDeclaration = NIL THEN
  8023. RETURN FALSE
  8024. ELSE
  8025. (* Is a constant in this module: did we generate it already? *)
  8026. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8027. IF constant # NIL THEN
  8028. InitOperand(result,ModeReference);
  8029. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8030. RETURN TRUE;
  8031. END;
  8032. END;
  8033. RETURN FALSE
  8034. END TryConstantDeclaration;
  8035. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8036. BEGIN
  8037. constantDeclaration := x;
  8038. x.value.resolved.Accept(SELF);
  8039. END VisitConstant;
  8040. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8041. BEGIN
  8042. IF Trace THEN TraceEnter("VisitRealValue") END;
  8043. InitOperand(result,ModeValue);
  8044. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8045. END VisitRealValue;
  8046. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8047. VAR
  8048. componentType: SyntaxTree.Type;
  8049. BEGIN
  8050. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8051. ASSERT(x.type IS SyntaxTree.ComplexType);
  8052. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8053. InitOperand(result,ModeValue);
  8054. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8055. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8056. END VisitComplexValue;
  8057. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8058. VAR i: LONGINT; name: Basic.SegmentedName;
  8059. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8060. BEGIN
  8061. IF Trace THEN TraceEnter("VisitStringValue") END;
  8062. IF ~TryConstantDeclaration() THEN
  8063. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8064. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8065. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8066. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8067. ELSE
  8068. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8069. END;
  8070. FOR i := 0 TO x.length-1 DO
  8071. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8072. irv.Emit(Data(position,op));
  8073. END;
  8074. InitOperand(result,ModeReference);
  8075. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8076. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8077. END
  8078. END VisitStringValue;
  8079. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8080. BEGIN
  8081. IF Trace THEN TraceEnter("VisitNilValue") END;
  8082. InitOperand(result,ModeValue);
  8083. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8084. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8085. END VisitNilValue;
  8086. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8087. BEGIN
  8088. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8089. InitOperand(result,ModeValue);
  8090. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8091. END VisitEnumerationValue;
  8092. (** symbols *)
  8093. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8094. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8095. END VisitImport;
  8096. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8097. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8098. BEGIN
  8099. IF scope # baseScope THEN
  8100. (* left := [fp+8] *)
  8101. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8102. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8103. ReuseCopy(left,right);
  8104. ReleaseIntermediateOperand(right);
  8105. scope := scope.outerScope; DEC(level);
  8106. (* { left := [left+8] } *)
  8107. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8108. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8109. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8110. Emit(Mov(position,left,right));
  8111. scope := scope.outerScope; DEC(level);
  8112. END;
  8113. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8114. result := left;
  8115. ELSE
  8116. result := fp;
  8117. END;
  8118. END GetBaseRegister;
  8119. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8120. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8121. BEGIN
  8122. IF Trace THEN TraceEnter("VisitVariable"); END;
  8123. type := x.type.resolved;
  8124. IF (x.useRegister) THEN
  8125. InitOperand(result, ModeValue);
  8126. IF x.registerNumber < 0 THEN
  8127. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8128. reg := x.registerNumber;
  8129. ELSE
  8130. reg := registerUsageCount.Map(x.registerNumber);
  8131. UseRegister(reg);
  8132. END;
  8133. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8134. ELSIF x.externalName # NIL THEN
  8135. InitOperand(result,ModeReference);
  8136. Basic.ToSegmentedName(x.externalName^, name);
  8137. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8138. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8139. InitOperand(result,ModeReference);
  8140. GetBaseRegister(result.op,currentScope,x.scope);
  8141. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8142. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8143. InitOperand(result,ModeReference);
  8144. GetCodeSectionNameForSymbol(x,name);
  8145. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8146. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8147. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8148. InitOperand(result,ModeReference);
  8149. GetCodeSectionNameForSymbol(x,name);
  8150. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8151. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8152. ELSE (* field, left designator must have been emitted *)
  8153. ASSERT(result.mode = ModeReference);
  8154. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8155. ReuseCopy(temp,result.op);
  8156. ReleaseIntermediateOperand(result.op);
  8157. result.op := temp;
  8158. END;
  8159. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8160. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8161. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8162. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8163. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8164. END;
  8165. END;
  8166. END;
  8167. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8168. ValueToCondition(result);
  8169. ELSIF type IS SyntaxTree.ProcedureType THEN
  8170. ReleaseIntermediateOperand(result.tag);
  8171. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8172. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8173. UseIntermediateOperand(result.tag);
  8174. ELSE
  8175. result.tag := nil; (* nil *)
  8176. END;
  8177. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8178. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8179. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8180. ReleaseIntermediateOperand(result.tag);
  8181. Global.GetSymbolSegmentedName(x,name);
  8182. Basic.AppendToSegmentedName(name,"@len");
  8183. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8184. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8185. ELSE
  8186. END;
  8187. ELSE
  8188. ReleaseIntermediateOperand(result.tag);
  8189. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8190. END;
  8191. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8192. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8193. ReleaseIntermediateOperand(result.tag);
  8194. result.tag := result.op;
  8195. UseIntermediateOperand(result.tag);
  8196. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8197. IntermediateCode.MakeMemory(result.op,addressType);
  8198. END;
  8199. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8200. ReleaseIntermediateOperand(result.tag);
  8201. result.tag := TypeDescriptorAdr(type);
  8202. IF ~newObjectFile THEN
  8203. IntermediateCode.MakeMemory(result.tag,addressType);
  8204. END;
  8205. UseIntermediateOperand(result.tag);
  8206. (* tag for pointer type computed not here but during dereferencing *)
  8207. END;
  8208. IF Trace THEN TraceExit("VisitVariable") END;
  8209. END VisitVariable;
  8210. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8211. BEGIN
  8212. VisitVariable(property);
  8213. END VisitProperty;
  8214. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8215. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8216. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8217. BEGIN
  8218. type := x.type.resolved;
  8219. IF Trace THEN TraceEnter("VisitParameter") END;
  8220. IF x.ownerType IS SyntaxTree.CellType THEN
  8221. ptype := x.type.resolved;
  8222. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8223. IF ~(ptype IS SyntaxTree.PortType) THEN
  8224. InitOperand(result,ModeReference);
  8225. GetCodeSectionNameForSymbol(x,name);
  8226. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8227. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8228. RETURN
  8229. ELSE
  8230. InitOperand(result, ModeValue);
  8231. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8232. adr := 0;
  8233. WHILE parameter # x DO
  8234. parameterType := parameter.type;
  8235. inc := 1;
  8236. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8237. INC(adr, inc);
  8238. parameter := parameter.nextParameter
  8239. END;
  8240. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8241. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8242. RETURN
  8243. END;
  8244. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8245. InitOperand(result,ModeReference);
  8246. GetCodeSectionNameForSymbol(x,name);
  8247. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8248. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8249. RETURN
  8250. ELSE
  8251. GetBaseRegister(basereg,currentScope,x.scope);
  8252. InitOperand(result,ModeReference);
  8253. result.op := basereg;
  8254. END;
  8255. IF IsOpenArray(type) THEN
  8256. result.tag := basereg;
  8257. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8258. IntermediateCode.MakeMemory(result.op,addressType);
  8259. IF Global.IsOberonProcedure(x.ownerType) THEN
  8260. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8261. UseIntermediateOperand(result.tag);
  8262. ELSE
  8263. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8264. END;
  8265. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8266. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8267. ELSIF IsStaticArray(type) THEN
  8268. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8269. IntermediateCode.MakeMemory(result.op,addressType);
  8270. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8271. ELSIF type IS SyntaxTree.MathArrayType THEN
  8272. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8273. WITH type: SyntaxTree.MathArrayType DO
  8274. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8275. IF type.form = SyntaxTree.Tensor THEN
  8276. ELSIF type.form = SyntaxTree.Open THEN
  8277. result.tag := result.op;
  8278. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8279. IntermediateCode.MakeMemory(result.op,addressType);
  8280. UseIntermediateOperand(result.tag);
  8281. ELSIF type.form = SyntaxTree.Static THEN
  8282. IF x.kind = SyntaxTree.ConstParameter THEN
  8283. IntermediateCode.MakeMemory(result.op,addressType);
  8284. END;
  8285. ELSE HALT(100)
  8286. END;
  8287. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8288. IF type.form = SyntaxTree.Tensor THEN
  8289. ToMemory(result.op,addressType,0);
  8290. ELSIF type.form = SyntaxTree.Open THEN
  8291. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8292. ReuseCopy(result.tag, mem);
  8293. ReleaseIntermediateOperand(mem);
  8294. ReleaseIntermediateOperand(result.op);
  8295. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8296. ELSIF type.form = SyntaxTree.Static THEN
  8297. IntermediateCode.MakeMemory(result.op,addressType);
  8298. ELSE HALT(100)
  8299. END;
  8300. ELSE HALT(100)
  8301. END;
  8302. END;
  8303. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8304. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8305. IntermediateCode.MakeMemory(result.op,addressType);
  8306. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8307. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8308. END;
  8309. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8310. ValueToCondition(result);
  8311. ELSIF type IS SyntaxTree.ProcedureType THEN
  8312. ReleaseIntermediateOperand(result.tag);
  8313. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8314. IF x.kind = SyntaxTree.VarParameter THEN
  8315. ReuseCopy(result.tag,result.op);
  8316. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8317. IntermediateCode.MakeMemory(result.tag,addressType);
  8318. ELSE
  8319. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8320. UseIntermediateOperand(result.tag);
  8321. END;
  8322. ELSE
  8323. result.tag := nil;
  8324. END;
  8325. (* tag for pointer type computed not here but during dereferencing *)
  8326. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8327. ReleaseIntermediateOperand(result.tag);
  8328. result.tag := basereg;
  8329. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8330. IntermediateCode.MakeMemory(result.tag,addressType);
  8331. UseIntermediateOperand(result.tag);
  8332. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8333. ReleaseIntermediateOperand(result.tag);
  8334. result.tag := TypeDescriptorAdr(type);
  8335. IF ~newObjectFile THEN
  8336. IntermediateCode.MakeMemory(result.tag,addressType);
  8337. END;
  8338. UseIntermediateOperand(result.tag);
  8339. END;
  8340. IF Trace THEN TraceExit("VisitParameter") END;
  8341. END VisitParameter;
  8342. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8343. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8344. BEGIN
  8345. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8346. (* left.p: left already emitted *)
  8347. tag := result.op; (* value of pointer to left *)
  8348. (* get type desc *)
  8349. tmp := result.tag;
  8350. IntermediateCode.MakeMemory(tmp,addressType);
  8351. (* get method adr *)
  8352. Reuse1(reg,tmp);
  8353. ReleaseIntermediateOperand(tmp);
  8354. IF backend.cooperative THEN
  8355. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8356. WHILE recordType.baseType # NIL DO
  8357. recordType := recordType.GetBaseRecord ();
  8358. END;
  8359. GetRecordTypeName (recordType,name);
  8360. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8361. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8362. offset := 0;
  8363. ELSE
  8364. offset := 2;
  8365. END;
  8366. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8367. ELSE
  8368. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8369. END;
  8370. InitOperand(operand,ModeReference);
  8371. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8372. operand.op := reg;
  8373. operand.tag := tag;
  8374. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8375. END DynamicCallOperand;
  8376. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8377. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8378. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8379. BEGIN
  8380. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8381. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8382. tag := operand.op;
  8383. ReleaseIntermediateOperand(operand.tag);
  8384. ELSE tag := nil
  8385. END;
  8386. IF x.isInline THEN
  8387. sectionType := Sections.InlineCodeSection;
  8388. ELSE
  8389. sectionType := Sections.CodeSection;
  8390. END;
  8391. IF x.externalName # NIL THEN
  8392. Basic.ToSegmentedName(x.externalName^, name);
  8393. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8394. ELSE
  8395. GetCodeSectionNameForSymbol(x, name);
  8396. IF (x.scope.ownerModule = module.module) THEN
  8397. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8398. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8399. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8400. IF source.pc = 0 THEN (* no code yet *)
  8401. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8402. END;
  8403. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8404. bits := x.procedureScope.body.code.inlineCode;
  8405. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8406. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8407. binary.CopyBits(bits, 0, bits.GetSize());
  8408. source.SetResolved(binary);
  8409. ELSE
  8410. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8411. END;
  8412. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8413. END;
  8414. InitOperand(operand,ModeValue);
  8415. operand.op := reg;
  8416. operand.tag := tag;
  8417. IF Trace THEN TraceExit("StaticCallOperand") END;
  8418. END StaticCallOperand;
  8419. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8420. (* handle expressions of the form designator.procedure or procedure *)
  8421. BEGIN
  8422. IF Trace THEN TraceEnter("VisitProcedure") END;
  8423. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8424. DynamicCallOperand(result,x);
  8425. ELSIF x.isInline THEN
  8426. StaticCallOperand(result,x);
  8427. ELSE
  8428. StaticCallOperand(result,x);
  8429. END;
  8430. IF Trace THEN TraceExit("VisitProcedure") END;
  8431. END VisitProcedure;
  8432. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8433. BEGIN
  8434. VisitProcedure(x);
  8435. END VisitOperator;
  8436. (** statements *)
  8437. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8438. BEGIN
  8439. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8440. Expression(x.call);
  8441. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8442. ReleaseOperand(result)
  8443. END;
  8444. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8445. END VisitProcedureCallStatement;
  8446. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8447. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8448. leftBase, rightBase: SyntaxTree.Type;
  8449. procedureName,s: SyntaxTree.IdentifierString;
  8450. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8451. size: LONGINT; rightKind: LONGINT;
  8452. dummy: IntermediateCode.Operand;
  8453. CONST moduleName = "FoxArrayBase";
  8454. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8455. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8456. BEGIN
  8457. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8458. IF base IS SyntaxTree.MathArrayType THEN
  8459. base := OpenArray(base(SyntaxTree.MathArrayType));
  8460. END;
  8461. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8462. result.SetArrayBase(base);
  8463. RETURN result
  8464. END OpenArray;
  8465. BEGIN
  8466. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8467. SaveRegisters();ReleaseUsedRegisters(saved);
  8468. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8469. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8470. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8471. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8472. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8473. IF leftType.form = SyntaxTree.Tensor THEN
  8474. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8475. ELSIF leftType.form = SyntaxTree.Open THEN
  8476. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8477. ELSIF leftType.form = SyntaxTree.Static THEN
  8478. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8479. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8480. END;
  8481. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8482. IF procedure = NIL THEN
  8483. s := "Instruction not supported on target, emulation procedure ";
  8484. Strings.Append(s,moduleName);
  8485. Strings.Append(s,".");
  8486. Strings.Append(s,procedureName);
  8487. Strings.Append(s," not present");
  8488. Error(position,s);
  8489. ELSE
  8490. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8491. parameter.SetType(leftType);
  8492. parameter.SetAccess(SyntaxTree.Internal);
  8493. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8494. parameter.SetKind(rightKind);
  8495. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8496. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8497. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8498. StaticCallOperand(result,procedure);
  8499. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8500. ReleaseOperand(result);
  8501. END;
  8502. RestoreRegisters(saved);
  8503. END;
  8504. END AssignMathArray;
  8505. VAR modifyAssignmentCounter := 0: LONGINT;
  8506. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8507. VAR processor,mem,dst: IntermediateCode.Operand;
  8508. BEGIN
  8509. IF value.intValue = true.intValue THEN
  8510. INC(modifyAssignmentCounter);
  8511. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8512. modifyAssignmentsPC := section.pc;
  8513. ELSE
  8514. DEC(modifyAssignmentCounter);
  8515. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8516. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8517. END;
  8518. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8519. dst := NewRegisterOperand (addressType);
  8520. Emit(Mov(position,dst, processor));
  8521. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8522. Emit(Mov(position,mem, value));
  8523. ReleaseIntermediateOperand(dst);
  8524. END ModifyAssignments;
  8525. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8526. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8527. BEGIN
  8528. type := left.type.resolved;
  8529. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8530. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8531. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8532. IF procedureType.returnParameter = parameter THEN
  8533. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8534. END;
  8535. END;
  8536. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8537. END CopySize;
  8538. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8539. VAR
  8540. leftO, rightO: Operand;
  8541. mem, sizeOp: IntermediateCode.Operand;
  8542. leftType, rightType, componentType: SyntaxTree.Type;
  8543. size: LONGINT;
  8544. parameters: SyntaxTree.ExpressionList;
  8545. procedure: SyntaxTree.Procedure;
  8546. call: SyntaxTree.ProcedureCallDesignator;
  8547. designator: SyntaxTree.Designator;
  8548. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8549. VAR procedureType: SyntaxTree.ProcedureType;
  8550. BEGIN
  8551. IF ReturnedAsParameter(right.type) THEN
  8552. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8553. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8554. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8555. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8556. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8557. IF right.id = Global.Reshape THEN RETURN TRUE
  8558. END;
  8559. END;
  8560. END;
  8561. END;
  8562. RETURN FALSE
  8563. END CanPassAsResultParameter;
  8564. BEGIN
  8565. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8566. leftType := left.type.resolved; rightType:= right.type.resolved;
  8567. IF backend.cooperative & left.NeedsTrace() THEN
  8568. ModifyAssignments(true);
  8569. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8570. Designate(right, rightO);
  8571. Designate(left, leftO);
  8572. ASSERT(leftO.mode = ModeReference);
  8573. TransferToRegister(leftO.op, leftO.op);
  8574. TransferToRegister(rightO.op, rightO.op);
  8575. Emit(Push(position, leftO.op));
  8576. Emit(Push(position, rightO.op));
  8577. CallAssignMethod(leftO.op, rightO.op, left.type);
  8578. Emit(Pop(position, rightO.op));
  8579. Emit(Pop(position, leftO.op));
  8580. sizeOp := CopySize(left);
  8581. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8582. ReleaseOperand(leftO);
  8583. ReleaseOperand(rightO);
  8584. ELSE
  8585. Evaluate(right,rightO);
  8586. Designate(left,leftO);
  8587. ASSERT(leftO.mode = ModeReference);
  8588. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8589. (* copy procedure address first *)
  8590. MakeMemory(mem,leftO.op,addressType,0);
  8591. Emit(Mov(position,mem,rightO.op));
  8592. ReleaseIntermediateOperand(mem);
  8593. (* copy pointer address *)
  8594. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8595. CallAssignPointer(leftO.tag, rightO.tag);
  8596. ELSE
  8597. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8598. CallAssignPointer(leftO.op, rightO.op);
  8599. END;
  8600. ReleaseOperand(leftO);
  8601. ReleaseOperand(rightO);
  8602. END;
  8603. ModifyAssignments(false);
  8604. RETURN;
  8605. END;
  8606. IF CanPassAsResultParameter(right) THEN
  8607. procedureResultDesignator := left(SyntaxTree.Designator);
  8608. Expression(right);
  8609. procedureResultDesignator := NIL;
  8610. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8611. (* left <-- ALIAS OF right: zerocopy *)
  8612. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8613. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8614. designator.SetType(procedure.type);
  8615. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8616. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8617. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8618. END;
  8619. ELSIF leftType IS SyntaxTree.RangeType THEN
  8620. (* LHS is of array range type *)
  8621. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8622. Evaluate(right, rightO);
  8623. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8624. (* first *)
  8625. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8626. Emit(Mov(position,mem, rightO.op));
  8627. ReleaseIntermediateOperand(mem);
  8628. (* last *)
  8629. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8630. Emit(Mov(position,mem, rightO.tag));
  8631. ReleaseIntermediateOperand(mem);
  8632. (* step *)
  8633. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8634. Emit(Mov(position,mem, rightO.extra));
  8635. ReleaseIntermediateOperand(mem);
  8636. ReleaseOperand(rightO);
  8637. ReleaseOperand(leftO)
  8638. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8639. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8640. Evaluate(right, rightO);
  8641. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8642. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8643. (* real part *)
  8644. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8645. Emit(Mov(position,mem, rightO.op));
  8646. ReleaseIntermediateOperand(mem);
  8647. (* imaginary part *)
  8648. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8649. Emit(Mov(position,mem, rightO.tag));
  8650. ReleaseIntermediateOperand(mem);
  8651. ReleaseOperand(rightO);
  8652. ReleaseOperand(leftO)
  8653. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8654. OR (leftType IS SyntaxTree.PortType) THEN
  8655. (* rightO := leftO;*)
  8656. Evaluate(right,rightO);
  8657. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8658. Designate(left,leftO);
  8659. IF leftO.mode = ModeReference THEN
  8660. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8661. destination := mem;
  8662. ELSE
  8663. destination := leftO.op;
  8664. END;
  8665. ReleaseOperand(leftO);
  8666. IF destination.mode # IntermediateCode.Undefined THEN
  8667. Emit(Mov(position,destination,rightO.op));
  8668. END;
  8669. ReleaseOperand(rightO);
  8670. ReleaseIntermediateOperand(mem);
  8671. IntermediateCode.InitOperand(destination);
  8672. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8673. Evaluate(right,rightO);
  8674. Designate(left,leftO);
  8675. MakeMemory(mem,leftO.op,addressType,0);
  8676. Emit(Mov(position,mem,rightO.op));
  8677. ReleaseIntermediateOperand(mem);
  8678. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8679. (* delegate *)
  8680. (*
  8681. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8682. *)
  8683. Emit(Mov(position,leftO.tag,rightO.tag));
  8684. END;
  8685. ReleaseOperand(leftO);
  8686. ReleaseOperand(rightO);
  8687. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8688. Designate(right,rightO);
  8689. Designate(left,leftO);
  8690. sizeOp := CopySize(left);
  8691. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8692. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8693. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8694. IF (rightType IS SyntaxTree.StringType) THEN
  8695. CopyString(left,right);
  8696. ELSIF ((rightType IS SyntaxTree.ArrayType) & (rightType(SyntaxTree.ArrayType).staticLength # 0) OR (rightType IS SyntaxTree.MathArrayType) & (rightType(SyntaxTree.MathArrayType).staticLength # 0)) & (leftType(SyntaxTree.ArrayType).staticLength # 0) THEN
  8697. Designate(right,rightO);
  8698. Designate(left,leftO);
  8699. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8700. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8701. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8702. ELSE
  8703. HALT(201)
  8704. END;
  8705. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8706. AssignMathArray(left,right);
  8707. ELSE
  8708. HALT(200);
  8709. END;
  8710. END Assign;
  8711. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8712. BEGIN
  8713. IF Trace THEN TraceEnter("VisitAssignment") END;
  8714. Assign(x.left,x.right);
  8715. IF Trace THEN TraceExit("VisitAssignment") END;
  8716. END VisitAssignment;
  8717. PROCEDURE EmitCooperativeSwitch;
  8718. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8719. BEGIN
  8720. ASSERT (cooperativeSwitches);
  8721. pc := section.pc;
  8722. IF lastSwitchPC = section.pc THEN RETURN END;
  8723. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8724. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8725. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8726. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8727. INC(statCoopSwitch, section.pc - pc);
  8728. END EmitCooperativeSwitch;
  8729. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8730. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8731. BEGIN
  8732. p0 := communication.left; p1 := communication.right;
  8733. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8734. Evaluate(p0,s0);
  8735. Evaluate(p1,s1);
  8736. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8737. Emit(Push(position,s0.op));
  8738. Emit(Push(position,s1.op));
  8739. (*
  8740. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8741. *)
  8742. IF ~backend.cellsAreObjects THEN
  8743. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8744. END;
  8745. ReleaseOperand(s0);
  8746. ReleaseOperand(s1);
  8747. IF backend.cellsAreObjects THEN
  8748. CallThis(position,"ActiveCellsRuntime","Send",2);
  8749. ELSE
  8750. CallThis(position,ChannelModuleName,"Send",2);
  8751. END;
  8752. (* ----- RECEIVE ------*)
  8753. ELSE
  8754. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8755. tmp := p0; p0 := p1; p1 := tmp;
  8756. END;
  8757. Evaluate(p0,s0);
  8758. Emit(Push(position,s0.op));
  8759. Designate(p1,s1);
  8760. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8761. Emit(Push(position,s1.op));
  8762. (*
  8763. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8764. *)
  8765. IF ~backend.cellsAreObjects THEN
  8766. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8767. END;
  8768. ReleaseOperand(s0);
  8769. ReleaseOperand(s1);
  8770. IF backend.cellsAreObjects THEN
  8771. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8772. ELSE
  8773. CallThis(position,ChannelModuleName,"Receive",2)
  8774. END;
  8775. END;
  8776. END VisitCommunicationStatement;
  8777. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8778. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8779. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8780. VAR true, false: Label; condition, value: BOOLEAN;
  8781. BEGIN
  8782. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8783. IF condition THEN
  8784. true := NewLabel();
  8785. false := NewLabel();
  8786. Condition(if.condition,true,false);
  8787. SetLabel(true);
  8788. StatementSequence(if.statements);
  8789. BrL(end);
  8790. SetLabel(false);
  8791. ELSE
  8792. IF value THEN (* always true *)
  8793. escape := TRUE;
  8794. StatementSequence(if.statements);
  8795. (* no branch necessary -- rest skipped *)
  8796. END;
  8797. END;
  8798. END IfPart;
  8799. BEGIN
  8800. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8801. end := NewLabel();
  8802. elsifs := x.ElsifParts();
  8803. IfPart(x.ifPart);
  8804. FOR i := 0 TO elsifs-1 DO
  8805. IF ~escape THEN
  8806. elsif := x.GetElsifPart(i);
  8807. IfPart(elsif);
  8808. END;
  8809. END;
  8810. IF (x.elsePart # NIL) & ~escape THEN
  8811. StatementSequence(x.elsePart);
  8812. END;
  8813. SetLabel(end);
  8814. IF Trace THEN TraceExit("VisitIfStatement") END;
  8815. END VisitIfStatement;
  8816. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8817. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8818. BEGIN
  8819. (*IF x.variable.type.resolved = x.type.resolved THEN
  8820. (* always true, do nothing *)
  8821. ELSE*)
  8822. Designate(x.variable,res);
  8823. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8824. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8825. END;
  8826. trueL := NewLabel();
  8827. TypeTest(res.tag,x.type,trueL,falseL);
  8828. ReleaseOperand(res);
  8829. SetLabel(trueL);
  8830. StatementSequence(x.statements);
  8831. BrL(endL);
  8832. END WithPart;
  8833. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8834. VAR endL,falseL: Label;i: LONGINT;
  8835. BEGIN
  8836. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8837. endL := NewLabel();
  8838. FOR i := 0 TO x.WithParts()-1 DO
  8839. falseL := NewLabel();
  8840. WithPart(x.GetWithPart(i),falseL,endL);
  8841. SetLabel(falseL);
  8842. END;
  8843. IF x.elsePart = NIL THEN
  8844. IF ~isUnchecked THEN
  8845. EmitTrap(position,WithTrap);
  8846. END;
  8847. ELSE
  8848. StatementSequence(x.elsePart)
  8849. END;
  8850. SetLabel(endL);
  8851. IF Trace THEN TraceExit("VisitWithStatement") END;
  8852. END VisitWithStatement;
  8853. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8854. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8855. out,else: Label; label: Label;
  8856. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8857. symbol: SyntaxTree.Symbol;
  8858. BEGIN
  8859. (*! split case statement into if-elsif statements for large case label lists *)
  8860. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8861. size := x.max-x.min+1;
  8862. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8863. END;
  8864. Evaluate(x.variable,var);
  8865. ReuseCopy(tmp,var.op);
  8866. ReleaseIntermediateOperand(var.op);
  8867. var.op := tmp;
  8868. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8869. Convert(var.op,addressType);
  8870. size := x.max-x.min+1;
  8871. else := NewLabel();
  8872. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8873. (*
  8874. UniqueId(name,module.module,"case",caseId);
  8875. *)
  8876. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8877. Global.GetModuleSegmentedName(module.module, name);
  8878. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8879. symbol := SyntaxTree.NewSymbol(name[1]);
  8880. symbol.SetScope(moduleScope);
  8881. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8882. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8883. section.isCaseTable := TRUE;
  8884. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8885. ReuseCopy(res,var.op);
  8886. ReleaseOperand(var);
  8887. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8888. Emit(Add(position,res,res,jmp));
  8889. IntermediateCode.MakeMemory(res,addressType);
  8890. Emit(Br(position,res));
  8891. ReleaseIntermediateOperand(res);
  8892. out := NewLabel();
  8893. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8894. part := x.GetCasePart(i);
  8895. constant := part.firstConstant;
  8896. label := NewLabel();
  8897. SetLabel(label);
  8898. WHILE(constant # NIL) DO (* case labels for this case part *)
  8899. FOR j := constant.min TO constant.max DO
  8900. fixups[j-x.min] := label;
  8901. END;
  8902. constant := constant.next;
  8903. END;
  8904. StatementSequence(part.statements);
  8905. BrL(out);
  8906. END;
  8907. SetLabel(else);
  8908. FOR i := 0 TO size-1 DO
  8909. IF fixups[i] = NIL THEN
  8910. fixups[i] := else;
  8911. END;
  8912. END;
  8913. IF x.elsePart # NIL THEN
  8914. StatementSequence(x.elsePart);
  8915. ELSIF ~isUnchecked THEN
  8916. EmitTrap(position,CaseTrap);
  8917. END;
  8918. SetLabel(out);
  8919. FOR i := 0 TO size-1 DO
  8920. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8921. section.Emit(Data(position,op));
  8922. END;
  8923. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8924. END VisitCaseStatement;
  8925. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8926. VAR start: Label; true,false: Label;
  8927. BEGIN
  8928. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8929. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8930. start := NewLabel();
  8931. true := NewLabel();
  8932. false := NewLabel();
  8933. SetLabel(start);
  8934. Condition(x.condition,true,false);
  8935. SetLabel(true);
  8936. StatementSequence(x.statements);
  8937. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8938. BrL(start);
  8939. SetLabel(false);
  8940. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8941. END VisitWhileStatement;
  8942. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8943. VAR false,true: Label;
  8944. BEGIN
  8945. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8946. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8947. true := NewLabel();
  8948. false := NewLabel();
  8949. SetLabel(false);
  8950. StatementSequence(x.statements);
  8951. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8952. Condition(x.condition,true,false);
  8953. SetLabel(true);
  8954. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8955. END VisitRepeatStatement;
  8956. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8957. VAR
  8958. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8959. temporaryVariable: SyntaxTree.Variable;
  8960. temporaryVariableDesignator : SyntaxTree.Designator;
  8961. BEGIN
  8962. IF Trace THEN TraceEnter("VisitForStatement") END;
  8963. true := NewLabel();
  8964. false := NewLabel();
  8965. start := NewLabel();
  8966. Assign(x.variable,x.from);
  8967. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8968. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8969. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8970. Assign(temporaryVariableDesignator,x.to);
  8971. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8972. IF by > 0 THEN
  8973. cmp := Scanner.LessEqual
  8974. ELSE
  8975. cmp := Scanner.GreaterEqual
  8976. END;
  8977. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8978. binary.SetType(system.booleanType);
  8979. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8980. SetLabel(start);
  8981. Condition(binary,true,false);
  8982. SetLabel(true);
  8983. StatementSequence(x.statements);
  8984. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8985. binary.SetType(x.variable.type);
  8986. Assign(x.variable,binary);
  8987. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8988. BrL(start);
  8989. SetLabel(false);
  8990. IF Trace THEN TraceExit("VisitForStatement") END;
  8991. END VisitForStatement;
  8992. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8993. VAR prevLoop: Label;
  8994. BEGIN
  8995. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8996. prevLoop := currentLoop;
  8997. currentLoop := NewLabel();
  8998. StatementSequence(x.statements);
  8999. SetLabel(currentLoop);
  9000. currentLoop := prevLoop;
  9001. IF Trace THEN TraceExit("VisitExitableBlock") END;
  9002. END VisitExitableBlock;
  9003. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  9004. VAR prevLoop,start: Label;
  9005. BEGIN
  9006. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  9007. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9008. start := NewLabel();
  9009. prevLoop := currentLoop;
  9010. SetLabel(start);
  9011. currentLoop := NewLabel();
  9012. StatementSequence(x.statements);
  9013. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  9014. BrL(start);
  9015. SetLabel(currentLoop);
  9016. currentLoop := prevLoop;
  9017. IF Trace THEN TraceExit("VisitLoopStatement") END;
  9018. END VisitLoopStatement;
  9019. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  9020. VAR outer: SyntaxTree.Statement;
  9021. BEGIN
  9022. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9023. IF locked THEN (* r if we jump out of an exclusive block *)
  9024. outer := x.outer;
  9025. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9026. outer := outer.outer;
  9027. END;
  9028. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9029. Lock(FALSE);
  9030. END;
  9031. END;
  9032. BrL(currentLoop);
  9033. IF Trace THEN TraceExit("VisitExitStatement") END;
  9034. END VisitExitStatement;
  9035. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9036. VAR
  9037. expression, parameterDesignator: SyntaxTree.Expression;
  9038. type, componentType: SyntaxTree.Type;
  9039. res, right: Operand;
  9040. left, mem, reg: IntermediateCode.Operand;
  9041. parameter: SyntaxTree.Parameter;
  9042. procedure: SyntaxTree.Procedure;
  9043. procedureType: SyntaxTree.ProcedureType;
  9044. returnTypeOffset: LONGINT;
  9045. delegate: BOOLEAN;
  9046. map: SymbolMap;
  9047. BEGIN
  9048. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9049. expression := x.returnValue;
  9050. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9051. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9052. IF currentIsInline THEN
  9053. IF expression # NIL THEN
  9054. map := currentMapper.Get(NIL);
  9055. IF map # NIL THEN
  9056. Assign(map.to, expression);
  9057. END;
  9058. END;
  9059. BrL(currentInlineExit);
  9060. RETURN;
  9061. END;
  9062. IF expression # NIL THEN
  9063. type := expression.type.resolved;
  9064. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9065. IF locked THEN Lock(FALSE) END;
  9066. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9067. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9068. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9069. (* return without structured return parameter *)
  9070. Evaluate(expression,res);
  9071. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9072. IF locked OR profile THEN
  9073. Emit(Push(position,res.op));
  9074. IF delegate THEN HALT(200) END;
  9075. ReleaseOperand(res);
  9076. IF locked THEN Lock(FALSE) END;
  9077. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9078. reg := NewRegisterOperand(res.op.type);
  9079. Emit(Pop(position,reg));
  9080. Emit(Return(position,reg));
  9081. ReleaseIntermediateOperand(reg);
  9082. ELSE
  9083. Emit(Return(position,res.op));
  9084. ReleaseOperand(res);
  9085. END;
  9086. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9087. THEN
  9088. (* return using structured return parameter *)
  9089. ASSERT((type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  9090. OR SemanticChecker.IsPointerType(type));
  9091. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9092. parameter :=procedureType.returnParameter;
  9093. ASSERT(parameter # NIL);
  9094. returnTypeOffset := parameter.offsetInBits;
  9095. (*
  9096. IF parameter# NIL THEN
  9097. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9098. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9099. ELSE
  9100. returnTypeOffset := system.offsetFirstParameter
  9101. END;
  9102. *)
  9103. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9104. IF type IS SyntaxTree.RangeType THEN
  9105. (* array range type *)
  9106. Evaluate(expression, right);
  9107. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9108. Emit(Mov(position,mem, right.op)); (* first *)
  9109. ReleaseIntermediateOperand(mem);
  9110. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9111. Emit(Mov(position,mem, right.tag)); (* last *)
  9112. ReleaseIntermediateOperand(mem);
  9113. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9114. Emit(Mov(position,mem, right.extra)); (* step *)
  9115. ReleaseIntermediateOperand(mem);
  9116. ReleaseOperand(right);
  9117. ELSIF type IS SyntaxTree.ComplexType THEN
  9118. Evaluate(expression, right);
  9119. componentType := type(SyntaxTree.ComplexType).componentType;
  9120. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9121. Emit(Mov(position,mem, right.op)); (* real part *)
  9122. ReleaseIntermediateOperand(mem);
  9123. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9124. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9125. ReleaseIntermediateOperand(mem);
  9126. ReleaseOperand(right);
  9127. ELSE (* covers cases: pointer / record / array *)
  9128. parameter := procedureType.returnParameter;
  9129. checker.SetCurrentScope(currentScope);
  9130. ASSERT(parameter # NIL);
  9131. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9132. Assign(parameterDesignator,expression);
  9133. END;
  9134. ReleaseIntermediateOperand(left);
  9135. IF locked THEN Lock(FALSE) END;
  9136. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9137. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9138. parameter := procedureType.returnParameter;
  9139. checker.SetCurrentScope(currentScope);
  9140. IF parameter = NIL THEN
  9141. Error(procedure.position, "structured return of parameter of procedure not found");
  9142. ELSE
  9143. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9144. Assign(parameterDesignator,expression);
  9145. END;
  9146. IF locked THEN Lock(FALSE) END;
  9147. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9148. ELSE
  9149. HALT(200);
  9150. END;
  9151. ELSE
  9152. IF locked THEN Lock(FALSE) END;
  9153. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9154. END;
  9155. IF backend.cooperative THEN
  9156. BrL(exitLabel);
  9157. ELSE
  9158. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9159. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9160. END;
  9161. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9162. END VisitReturnStatement;
  9163. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9164. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9165. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9166. statements: SyntaxTree.StatementSequence;
  9167. name, suffix: SyntaxTree.IdentifierString;
  9168. BEGIN
  9169. Strings.IntToStr(awaitProcCounter,suffix);
  9170. Strings.Concat("@AwaitProcedure",suffix,name);
  9171. identifier := SyntaxTree.NewIdentifier(name);
  9172. INC(awaitProcCounter);
  9173. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9174. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9175. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9176. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9177. procedure.SetAccess(SyntaxTree.Hidden);
  9178. procedure.SetScope(currentScope);
  9179. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9180. procedureType.SetReturnType(system.booleanType);
  9181. procedure.SetType(procedureType);
  9182. body := SyntaxTree.NewBody(x.position,procedureScope);
  9183. procedureScope.SetBody(body);
  9184. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9185. returnStatement.SetReturnValue(x.condition);
  9186. statements := SyntaxTree.NewStatementSequence();
  9187. statements.AddStatement(returnStatement);
  9188. body.SetStatementSequence(statements);
  9189. currentScope.AddProcedure(procedure);
  9190. RETURN procedure
  9191. END MakeAwaitProcedure;
  9192. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9193. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9194. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9195. BEGIN
  9196. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9197. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9198. IF backend.cooperative THEN
  9199. start := NewLabel();
  9200. true := NewLabel();
  9201. false := NewLabel();
  9202. SetLabel(start);
  9203. Condition(x.condition,true,false);
  9204. SetLabel(false);
  9205. PushSelfPointer();
  9206. CallThis(position,"ExclusiveBlocks","Await",1);
  9207. BrL(start);
  9208. SetLabel(true);
  9209. PushSelfPointer();
  9210. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9211. ELSE
  9212. proc := MakeAwaitProcedure(x);
  9213. Emit(Push(position,fp));
  9214. GetCodeSectionNameForSymbol(proc,name);
  9215. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9216. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9217. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9218. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9219. Emit(Result(position,res));
  9220. (*
  9221. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9222. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9223. *)
  9224. InitOperand(result,ModeValue);
  9225. result.op := res;
  9226. label := NewLabel();
  9227. BreqL(label, result.op, SELF.true);
  9228. ReleaseOperand(result);
  9229. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9230. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9231. Emit(Push(position,res));
  9232. Emit(Push(position,fp));
  9233. PushSelfPointer();
  9234. Emit(Push(position,nil));
  9235. CallThis(position,"Objects","Await",4);
  9236. SetLabel(label);
  9237. END;
  9238. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9239. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9240. END VisitAwaitStatement;
  9241. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9242. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9243. BEGIN
  9244. FOR i := 0 TO x.Length() - 1 DO
  9245. statement := x.GetStatement( i );
  9246. Statement(statement);
  9247. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9248. END;
  9249. END StatementSequence;
  9250. PROCEDURE PushSelfPointer;
  9251. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9252. BEGIN
  9253. scope := currentScope;
  9254. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9255. scope := scope.outerScope;
  9256. END;
  9257. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9258. IF ~newObjectFile THEN
  9259. Symbol(moduleSelf,op);
  9260. IntermediateCode.MakeMemory(op.op,addressType);
  9261. ELSE
  9262. moduleSection := meta.ModuleSection();
  9263. IF backend.cooperative THEN
  9264. moduleOffset := 0;
  9265. ELSE
  9266. moduleOffset := moduleSection.pc;
  9267. END;
  9268. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9269. END;
  9270. ELSE
  9271. GetBaseRegister(op.op,currentScope,scope);
  9272. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9273. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9274. IF backend.cooperative THEN
  9275. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9276. END;
  9277. IntermediateCode.MakeMemory(op.op,addressType);
  9278. END;
  9279. Emit(Push(position,op.op));
  9280. ReleaseOperand(op);
  9281. END PushSelfPointer;
  9282. PROCEDURE Lock(lock: BOOLEAN);
  9283. BEGIN
  9284. IF Trace THEN TraceEnter("Lock") END;
  9285. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9286. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9287. ASSERT(modifyAssignmentCounter = 0);
  9288. IF dump # NIL THEN
  9289. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9290. dump.Ln;dump.Update;
  9291. END;
  9292. PushSelfPointer;
  9293. IF backend.cooperative THEN
  9294. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9295. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9296. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9297. END;
  9298. ELSE
  9299. Emit(Push(position,true));
  9300. IF lock THEN CallThis(position,"Objects","Lock",2)
  9301. ELSE CallThis(position,"Objects","Unlock",2);
  9302. END;
  9303. END;
  9304. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9305. IF Trace THEN TraceExit("Lock") END;
  9306. END Lock;
  9307. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9308. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9309. BEGIN
  9310. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9311. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9312. previouslyUnchecked := isUnchecked;
  9313. isUnchecked := isUnchecked OR x.isUnchecked;
  9314. previouslyCooperativeSwitches := cooperativeSwitches;
  9315. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9316. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9317. IF x.statements # NIL THEN
  9318. StatementSequence(x.statements);
  9319. END;
  9320. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9321. isUnchecked := previouslyUnchecked;
  9322. cooperativeSwitches := previouslyCooperativeSwitches;
  9323. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9324. END VisitStatementBlock;
  9325. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9326. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9327. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9328. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9329. procedure: SyntaxTree.Procedure;
  9330. map: SymbolMap;
  9331. BEGIN
  9332. scope := currentScope;
  9333. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9334. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9335. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9336. return := emptyOperand;
  9337. IF Trace THEN TraceEnter("VisitCode") END;
  9338. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9339. NEW(in, x.inRules.Length());
  9340. FOR i := 0 TO LEN(in)-1 DO
  9341. statement := x.inRules.GetStatement(i);
  9342. WITH statement: SyntaxTree.Assignment DO
  9343. Evaluate(statement.right, operand);
  9344. result := operand.op;
  9345. NEW(str, 64);
  9346. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9347. in[i] := result; IntermediateCode.SetString(in[i], str);
  9348. ReleaseIntermediateOperand(operand.tag);
  9349. END;
  9350. END;
  9351. ELSE in := NIL
  9352. END;
  9353. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9354. NEW(out, x.outRules.Length());
  9355. FOR i := 0 TO LEN(out)-1 DO
  9356. statement := x.outRules.GetStatement(i);
  9357. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9358. WITH statement: SyntaxTree.Assignment DO
  9359. Evaluate(statement.left, operand); (*?? or designate *)
  9360. result := operand.op;
  9361. NEW(str, 64);
  9362. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9363. out[i] := result; IntermediateCode.SetString(out[i], str);
  9364. ReleaseIntermediateOperand(operand.tag);
  9365. |statement: SyntaxTree.ReturnStatement DO
  9366. NEW(str, 64);
  9367. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9368. IF currentIsInline THEN
  9369. map := currentMapper.Get(NIL);
  9370. Evaluate(map.to, operand);
  9371. out[i] := operand.op;
  9372. ELSE
  9373. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9374. END;
  9375. IntermediateCode.SetString(out[i], str);
  9376. ReleaseIntermediateOperand(operand.tag);
  9377. return := out[i];
  9378. ELSE
  9379. END;
  9380. END;
  9381. ELSE out := NIL
  9382. END;
  9383. Emit(Asm(x.position,x.sourceCode, in, out));
  9384. IF in # NIL THEN
  9385. FOR i := 0 TO LEN(in)-1 DO
  9386. ReleaseIntermediateOperand(in[i]);
  9387. END;
  9388. END;
  9389. IF out # NIL THEN
  9390. FOR i := 0 TO LEN(out)-1 DO
  9391. WITH statement: SyntaxTree.Assignment DO
  9392. ReleaseIntermediateOperand(out[i]);
  9393. ELSE
  9394. IF currentIsInline THEN
  9395. ReleaseIntermediateOperand(out[i]);
  9396. END;
  9397. END;
  9398. statement := x.outRules.GetStatement(i);
  9399. END;
  9400. END;
  9401. IF return.mode # IntermediateCode.Undefined THEN
  9402. IF currentIsInline THEN
  9403. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9404. Symbol(procedureType.returnParameter, par);
  9405. MakeMemory(mem, par.op, return.type, 0);
  9406. ReleaseOperand(par);
  9407. Emit(Mov(position, mem, return));
  9408. ReleaseIntermediateOperand(mem);
  9409. ELSE
  9410. Emit(Return(position,return));
  9411. END;
  9412. IF currentIsInline THEN RETURN END;
  9413. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9414. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9415. ReleaseIntermediateOperand(return);
  9416. END;
  9417. IF Trace THEN TraceExit("VisitCode") END;
  9418. END VisitCode;
  9419. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9420. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9421. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9422. const, call: IntermediateCode.Operand;
  9423. parameterDesignator: SyntaxTree.Expression;
  9424. saved: RegisterEntry;
  9425. name: Basic.SegmentedName;
  9426. BEGIN
  9427. IF Trace THEN TraceEnter("ParameterCopies") END;
  9428. parameter := x.firstParameter;
  9429. WHILE parameter # NIL DO
  9430. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9431. type := parameter.type.resolved;
  9432. IF IsOpenArray(type) THEN
  9433. VisitParameter(parameter);
  9434. op := result;
  9435. IF backend.cooperative & parameter.NeedsTrace() THEN
  9436. length := GetArrayLength(type, op.tag);
  9437. size := NewRegisterOperand(addressType);
  9438. base := ArrayBaseType(type);
  9439. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9440. Emit(Mul(position, size, length, const));
  9441. dst := NewRegisterOperand (addressType);
  9442. Emit(Mov(position, dst, size));
  9443. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9444. Emit(Sub(position,dst,sp,dst));
  9445. Emit(And(position,dst,dst,const));
  9446. Emit(Mov(position,sp,dst));
  9447. par := fp;
  9448. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9449. IntermediateCode.InitImmediate(null, byteType, 0);
  9450. Emit(Fill(position, dst, size, null));
  9451. ReleaseIntermediateOperand(dst);
  9452. ReleaseIntermediateOperand(length);
  9453. SaveRegisters();ReleaseUsedRegisters(saved);
  9454. (* register dst has been freed before SaveRegisters already *)
  9455. base := ArrayBaseType(type);
  9456. (* assign method of open array *)
  9457. IF base.IsRecordType() THEN
  9458. Emit (Push(position, length));
  9459. Emit (Push(position, dst));
  9460. Emit (Push(position, op.op));
  9461. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9462. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9463. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9464. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9465. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9466. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9467. Emit (Push(position,length));
  9468. Emit (Push(position, dst));
  9469. Emit (Push(position, length));
  9470. Emit (Push(position, op.op));
  9471. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9472. ELSE
  9473. Emit (Push(position, length));
  9474. Emit (Push(position, dst));
  9475. Emit (Push(position, length));
  9476. Emit (Push(position, op.op));
  9477. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9478. ASSERT(ArrayBaseType(type).IsPointer());
  9479. END;
  9480. RestoreRegisters(saved);
  9481. ELSE
  9482. temp := GetDynamicSize(type,op.tag);
  9483. ReuseCopy(size,temp);
  9484. ReleaseIntermediateOperand(temp);
  9485. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9486. Emit(Sub(position,size,sp,size));
  9487. Emit(And(position,size,size,const));
  9488. Emit(Mov(position,sp,size));
  9489. par := fp;
  9490. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9491. ReleaseIntermediateOperand(size);
  9492. size := GetDynamicSize(type,op.tag);
  9493. END;
  9494. Emit(Copy(position,sp,op.op,size));
  9495. ReleaseIntermediateOperand(size);
  9496. ReleaseOperand(op);
  9497. IntermediateCode.MakeMemory(par,addressType);
  9498. Emit(Mov(position,par,sp));
  9499. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9500. checker.SetCurrentScope(currentScope);
  9501. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9502. Assign(parameterDesignator,parameterDesignator);
  9503. END;
  9504. END;
  9505. parameter := parameter.nextParameter;
  9506. END;
  9507. IF Trace THEN TraceExit("ParameterCopies") END;
  9508. END ParameterCopies;
  9509. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9510. VAR x: SyntaxTree.Variable;
  9511. BEGIN
  9512. x := scope.firstVariable;
  9513. WHILE x # NIL DO
  9514. InitVariable(x);
  9515. x := x.nextVariable;
  9516. END;
  9517. END InitVariables;
  9518. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9519. BEGIN
  9520. IF (symbol # NIL) THEN
  9521. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9522. ELSE
  9523. RETURN 0
  9524. END;
  9525. END GetFingerprint;
  9526. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9527. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9528. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9529. saved: RegisterEntry;
  9530. BEGIN
  9531. IF Trace THEN TraceEnter("Body") END;
  9532. ReleaseUsedRegisters(saved); (* just in case ... *)
  9533. section := ir;
  9534. exitLabel := NewLabel ();
  9535. IF moduleBody THEN moduleBodySection := section END;
  9536. IF ir.comments # NIL THEN
  9537. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9538. commentPrintout.SingleStatement(TRUE);
  9539. dump := ir.comments;
  9540. ELSE
  9541. commentPrintout := NIL;
  9542. dump := NIL;
  9543. END;
  9544. prevScope := currentScope;
  9545. currentScope := scope;
  9546. lastSwitchPC := 0;
  9547. cooperativeSwitches := backend.cooperative;
  9548. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9549. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9550. IF x # NIL THEN
  9551. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9552. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9553. IF moduleBody THEN
  9554. ProfilerInit();
  9555. ELSE
  9556. Basic.SegmentedNameToString(ir.name, string);
  9557. ProfilerAddProcedure(numberProcedures,string);
  9558. ProfilerEnterExit(numberProcedures,TRUE);
  9559. END;
  9560. END;
  9561. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9562. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9563. END;
  9564. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9565. IF moduleBody THEN
  9566. InitVariables(moduleScope)
  9567. END;
  9568. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9569. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9570. IF procedure = cellScope.bodyProcedure THEN
  9571. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9572. StaticCallOperand(op, cellScope.constructor);
  9573. Emit(Call(position,op.op,0));
  9574. END;
  9575. END;
  9576. END;
  9577. ParameterCopies(procedureType);
  9578. InitVariables(scope);
  9579. IF x.code = NIL THEN
  9580. VisitStatementBlock(x);
  9581. ELSE
  9582. VisitCode(x.code)
  9583. END;
  9584. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9585. ir.SetFinally(ir.pc);
  9586. StatementSequence(x.finally)
  9587. END;
  9588. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9589. IF ~backend.cooperative THEN
  9590. ProfilerEnterExit(numberProcedures,FALSE);
  9591. END;
  9592. INC(numberProcedures);
  9593. END;
  9594. END;
  9595. IF backend.cooperative THEN
  9596. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9597. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9598. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9599. (*! not required any more? check and delete!
  9600. PushSelfPointer();
  9601. CallThis(position,"Modules","SetInitialized",1);
  9602. *)
  9603. (*
  9604. SetLabel(end);
  9605. *)
  9606. END;
  9607. IF x # NIL THEN
  9608. SELF.position := x.position;
  9609. END;
  9610. CheckRegistersFree();
  9611. ASSERT(modifyAssignmentCounter = 0);
  9612. currentScope := prevScope;
  9613. IF Trace THEN TraceExit("Body") END;
  9614. END Body;
  9615. END ImplementationVisitor;
  9616. MetaDataGenerator=OBJECT
  9617. VAR
  9618. implementationVisitor: ImplementationVisitor;
  9619. declarationVisitor: DeclarationVisitor;
  9620. module: Sections.Module;
  9621. moduleName: ARRAY 128 OF CHAR;
  9622. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9623. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9624. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9625. TypeTags: LONGINT; (* type extension level support *)
  9626. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9627. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9628. BEGIN
  9629. IF implementationVisitor.backend.cooperative THEN
  9630. TypeTags := MAX(LONGINT);
  9631. BaseTypesTableOffset := 0;
  9632. MethodTableOffset := 2;
  9633. TypeRecordBaseOffset := 0;
  9634. ELSIF simple THEN
  9635. TypeTags := 3; (* only 3 extensions allowed *)
  9636. BaseTypesTableOffset := 1;
  9637. MethodTableOffset := BaseTypesTableOffset+TypeTags;
  9638. TypeRecordBaseOffset := 0;
  9639. ELSE
  9640. TypeTags := 16;
  9641. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9642. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9643. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9644. END;
  9645. SELF.simple := simple;
  9646. SELF.implementationVisitor := implementationVisitor;
  9647. SELF.declarationVisitor := declarationVisitor;
  9648. implementationVisitor.meta := SELF;
  9649. declarationVisitor.meta := SELF;
  9650. END InitMetaDataGenerator;
  9651. PROCEDURE SetModule(module: Sections.Module);
  9652. BEGIN
  9653. SELF.module := module
  9654. END SetModule;
  9655. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9656. BEGIN
  9657. IF implementationVisitor.backend.cooperative OR simple THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9658. END GetTypeRecordBaseOffset;
  9659. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9660. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9661. BEGIN
  9662. INC(dataAdrOffset,6);
  9663. Info(section,"headerAdr");
  9664. Address(section,0);
  9665. Info(section,"typeDesc");
  9666. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9667. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9668. NamedSymbol(section, name, symbol, 0, offset);
  9669. Info(section,"mark: LONGINT;");
  9670. Longint(section,-1);
  9671. Info(section,"dataAdr-: ADDRESS");
  9672. Symbol(section,section, dataAdrOffset,0);
  9673. Info(section,"size-: SIZE");
  9674. Address(section,0);
  9675. Info(section,"nextRealtime: HeapBlock;");
  9676. Address(section,0);
  9677. END HeapBlock;
  9678. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9679. VAR i: LONGINT;
  9680. BEGIN
  9681. INC(dataAdrOffset,14);
  9682. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9683. Info(section,"count*: LONGINT");
  9684. Longint(section,0);
  9685. Info(section,"locked*: BOOLEAN");
  9686. Longint(section,0);
  9687. Info(section,"awaitingLock*: ProcessQueue");
  9688. Address(section,0);
  9689. Address(section,0);
  9690. Info(section,"awaitingCond*: ProcessQueue");
  9691. Address(section,0);
  9692. Address(section,0);
  9693. Info(section,"lockedBy*: ANY");
  9694. Address(section,0);
  9695. Info(section,"lock*: ANY");
  9696. Address(section,0);
  9697. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9698. Longint(section,1);
  9699. FOR i := 2 TO 6 DO
  9700. Longint(section,0);
  9701. END;
  9702. END ProtectedHeapBlock;
  9703. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9704. BEGIN
  9705. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9706. END Info;
  9707. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9708. VAR op: IntermediateCode.Operand;
  9709. BEGIN
  9710. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9711. section.Emit(Data(-11,op));
  9712. END Address;
  9713. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9714. VAR op: IntermediateCode.Operand;
  9715. BEGIN
  9716. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9717. section.Emit(Data(-12,op));
  9718. END Size;
  9719. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9720. VAR op: IntermediateCode.Operand;
  9721. BEGIN
  9722. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9723. section.Emit(Data(-1,op));
  9724. END Set;
  9725. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9726. VAR op: IntermediateCode.Operand;
  9727. BEGIN
  9728. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9729. section.Emit(Data(-1,op));
  9730. END Longint;
  9731. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9732. VAR op,noOperand: IntermediateCode.Operand;
  9733. BEGIN
  9734. IntermediateCode.InitOperand(noOperand);
  9735. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9736. section.PatchOperands(pc,op,noOperand,noOperand);
  9737. END PatchLongint;
  9738. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9739. VAR op, noOperand: IntermediateCode.Operand;
  9740. BEGIN
  9741. IntermediateCode.InitOperand(noOperand);
  9742. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9743. section.PatchOperands(pc,op,noOperand,noOperand);
  9744. END PatchSymbol;
  9745. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9746. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9747. BEGIN
  9748. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9749. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9750. section.Emit(Data(-1,op));
  9751. END Boolean;
  9752. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9753. VAR op: IntermediateCode.Operand;
  9754. BEGIN
  9755. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9756. section.Emit(Data(-1,op));
  9757. END Char;
  9758. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9759. VAR i: LONGINT;
  9760. BEGIN
  9761. Info(section,str);
  9762. i := 0;
  9763. WHILE(str[i] # 0X) DO
  9764. Char(section,str[i]);
  9765. INC(i);
  9766. END;
  9767. Char(section,0X);
  9768. END String;
  9769. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9770. VAR op: IntermediateCode.Operand;
  9771. BEGIN
  9772. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9773. IntermediateCode.SetOffset(op,realOffset);
  9774. section.Emit(Data(-1,op));
  9775. END NamedSymbol;
  9776. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9777. BEGIN
  9778. IF symbol= NIL THEN
  9779. Address( section, realOffset);
  9780. ASSERT(virtualOffset = 0);
  9781. ELSE
  9782. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9783. END;
  9784. END Symbol;
  9785. (* OutPointers delivers
  9786. {pointerOffset}
  9787. *)
  9788. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9789. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9790. BEGIN
  9791. type := type.resolved;
  9792. IF type IS SyntaxTree.AnyType THEN
  9793. Symbol(section, symbol, 0, (offset ));
  9794. INC(numberPointers);
  9795. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9796. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9797. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9798. ELSIF type IS SyntaxTree.PointerType THEN
  9799. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9800. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9801. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9802. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9803. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9804. ELSIF (type IS SyntaxTree.RecordType) THEN
  9805. (* never treat a record like a pointer, even if the pointer field is set! *)
  9806. WITH type: SyntaxTree.RecordType DO
  9807. base := type.GetBaseRecord();
  9808. IF base # NIL THEN
  9809. Pointers(offset,symbol,section, base,numberPointers);
  9810. END;
  9811. variable := type.recordScope.firstVariable;
  9812. WHILE(variable # NIL) DO
  9813. IF ~(variable.untraced) THEN
  9814. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9815. END;
  9816. variable := variable.nextVariable;
  9817. END;
  9818. END;
  9819. ELSIF (type IS SyntaxTree.CellType) THEN
  9820. WITH type: SyntaxTree.CellType DO
  9821. base := type.GetBaseRecord();
  9822. IF base # NIL THEN
  9823. Pointers(offset,symbol,section, base,numberPointers);
  9824. END;
  9825. variable := type.cellScope.firstVariable;
  9826. WHILE(variable # NIL) DO
  9827. IF ~(variable.untraced) THEN
  9828. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9829. END;
  9830. variable := variable.nextVariable;
  9831. END;
  9832. END;
  9833. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9834. WITH type: SyntaxTree.ArrayType DO
  9835. IF type.form= SyntaxTree.Static THEN
  9836. n := type.staticLength;
  9837. base := type.arrayBase.resolved;
  9838. WHILE(base IS SyntaxTree.ArrayType) DO
  9839. type := base(SyntaxTree.ArrayType);
  9840. n := n* type.staticLength;
  9841. base := type.arrayBase.resolved;
  9842. END;
  9843. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9844. IF SemanticChecker.ContainsPointer(base) THEN
  9845. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9846. FOR i := 0 TO n-1 DO
  9847. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9848. END;
  9849. END;
  9850. ELSE
  9851. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9852. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9853. END;
  9854. END;
  9855. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9856. WITH type: SyntaxTree.MathArrayType DO
  9857. IF type.form = SyntaxTree.Static THEN
  9858. n := type.staticLength;
  9859. base := type.arrayBase.resolved;
  9860. WHILE(base IS SyntaxTree.MathArrayType) DO
  9861. type := base(SyntaxTree.MathArrayType);
  9862. n := n* type.staticLength;
  9863. base := type.arrayBase.resolved;
  9864. END;
  9865. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9866. IF SemanticChecker.ContainsPointer(base) THEN
  9867. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9868. FOR i := 0 TO n-1 DO
  9869. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9870. END;
  9871. END;
  9872. ELSE
  9873. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9874. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9875. END
  9876. END;
  9877. (* ELSE no pointers in type *)
  9878. END;
  9879. END Pointers;
  9880. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9881. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9882. BEGIN
  9883. IF pool.Has(name) THEN
  9884. RETURN pool.GetInt(name)
  9885. ELSE
  9886. position := source.pc;
  9887. pool.PutInt(name, position);
  9888. StringPool.GetString(name, s);
  9889. Info(source, s);
  9890. i := 0;
  9891. REPEAT
  9892. ch := s[i]; INC(i);
  9893. Char( source, ch);
  9894. UNTIL ch = 0X;
  9895. END;
  9896. RETURN position;
  9897. END DynamicName;
  9898. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9899. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9900. BEGIN
  9901. COPY(moduleName,name);
  9902. Strings.Append(name,suffix);
  9903. Basic.ToSegmentedName(name, pooledName);
  9904. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9905. IF implementationVisitor.backend.cooperative THEN
  9906. Info(section, "TypeDescriptor");
  9907. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9908. NamedSymbol(section, pooledName,NIL, 0, 0);
  9909. BasePointer(section);
  9910. offset := 0;
  9911. ELSE
  9912. HeapBlock(mName,typeName,section,2);
  9913. Info(section, "HeapBlock");
  9914. (*
  9915. Symbol(section,section,2,0);
  9916. *)
  9917. Address(section,0); (* empty such that GC does not go on traversing *)
  9918. Info(section, "TypeDescriptor");
  9919. Address(section,0);
  9920. offset := section.pc;
  9921. END;
  9922. RETURN section
  9923. END Block;
  9924. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9925. VAR name: Basic.SegmentedName;
  9926. BEGIN
  9927. Info(source,"ArrayHeader");
  9928. IF implementationVisitor.backend.cooperative THEN
  9929. sizePC := source.pc;
  9930. Address(source,0);
  9931. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9932. IF baseType # "" THEN
  9933. Basic.ToSegmentedName(baseType, name);
  9934. NamedSymbol(source, name,NIL, 0, 0);
  9935. ELSE
  9936. Address(source,0);
  9937. END;
  9938. Address(source,0);
  9939. ELSE
  9940. Address(source,0);
  9941. Address(source,0);
  9942. Address(source,0);
  9943. sizePC := source.pc;
  9944. Address(source,0);
  9945. Info(source,"array data");
  9946. END;
  9947. END Array;
  9948. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9949. BEGIN
  9950. IF implementationVisitor.backend.cooperative THEN
  9951. PatchLongint(section, pc, size);
  9952. PatchLongint(section, pc + 3, size);
  9953. ELSE
  9954. PatchLongint(section, pc, size);
  9955. END;
  9956. END PatchArray;
  9957. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9958. VAR
  9959. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9960. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9961. poolMap: Basic.HashTableInt;
  9962. namePool: IntermediateCode.Section;
  9963. namePoolOffset: LONGINT;
  9964. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9965. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9966. BEGIN
  9967. Basic.SegmentedNameToString(s1.name,n1);
  9968. Basic.SegmentedNameToString(s2.name,n2);
  9969. index := 0;
  9970. ch1 := n1[index];
  9971. ch2 := n2[index];
  9972. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9973. INC(index);
  9974. ch1 := n1[index];
  9975. ch2 := n2[index];
  9976. END;
  9977. RETURN ch1 < ch2;
  9978. END Compare;
  9979. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9980. VAR
  9981. i, j: LONGINT;
  9982. x, t: Sections.Section;
  9983. BEGIN
  9984. IF lo < hi THEN
  9985. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9986. WHILE i <= j DO
  9987. WHILE Compare(list[i], x) DO INC(i) END;
  9988. WHILE Compare(x, list[j]) DO DEC(j) END;
  9989. IF i <= j THEN
  9990. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9991. INC(i); DEC(j)
  9992. END
  9993. END;
  9994. IF lo < j THEN QuickSort(list, lo, j) END;
  9995. IF i < hi THEN QuickSort(list, i, hi) END
  9996. END;
  9997. END QuickSort;
  9998. (*
  9999. ExportDesc* = RECORD
  10000. fp*: ADDRESS;
  10001. name* {UNTRACED}: DynamicName;
  10002. adr*: ADDRESS;
  10003. exports*: LONGINT;
  10004. dsc* {UNTRACED}: ExportArray
  10005. END;
  10006. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  10007. *)
  10008. PROCEDURE ExportDesc2(
  10009. source: IntermediateCode.Section;
  10010. namePool: IntermediateCode.Section;
  10011. fingerPrinter: FingerPrinter.FingerPrinter;
  10012. symbol: Sections.Section;
  10013. name: StringPool.Index;
  10014. VAR patchAdr: LONGINT
  10015. ): BOOLEAN;
  10016. VAR fingerPrint: SyntaxTree.FingerPrint;
  10017. BEGIN
  10018. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10019. & (symbol.type # Sections.InlineCodeSection)
  10020. THEN
  10021. *)
  10022. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10023. & (symbol.type # Sections.InlineCodeSection))
  10024. THEN
  10025. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10026. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10027. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10028. Longint(source,fingerPrint.shallow);
  10029. ELSE
  10030. Longint(source, 0);
  10031. END;
  10032. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10033. Symbol(source, symbol,0,0);
  10034. patchAdr := source.pc;
  10035. Address(source,0);
  10036. Address(source,0);
  10037. END;
  10038. RETURN TRUE
  10039. ELSE
  10040. RETURN FALSE
  10041. END;
  10042. END ExportDesc2;
  10043. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10044. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10045. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10046. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10047. TYPE
  10048. Scope = RECORD
  10049. elements: LONGINT;
  10050. gelements: LONGINT;
  10051. section: IntermediateCode.Section;
  10052. patchAdr: LONGINT;
  10053. arraySizePC: LONGINT;
  10054. beginPC: LONGINT; (* current scope start pc *)
  10055. END;
  10056. BEGIN
  10057. Basic.InitSegmentedName(prev);
  10058. olevel := -1;
  10059. scopes[0].section := source;
  10060. FOR s := 0 TO LEN(sections)-1 DO
  10061. symbol := sections[s];
  10062. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10063. this := sections[s].name;
  10064. level := 0;
  10065. WHILE (this[level] > 0) DO
  10066. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10067. INC(level);
  10068. END;
  10069. WHILE level < olevel DO
  10070. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10071. IF olevel > 0 THEN
  10072. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10073. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10074. END;
  10075. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10076. DEC(olevel);
  10077. END;
  10078. IF (this[level] > 0) THEN
  10079. IF level > olevel THEN
  10080. (*TRACE("opening",level); *)
  10081. IF scopes[level].section = NIL THEN
  10082. arrayName := ".@ExportArray";
  10083. Strings.AppendInt(arrayName, level);
  10084. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10085. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10086. END;
  10087. scopes[level].beginPC := scopes[level].section.pc;
  10088. olevel := level;
  10089. scopes[olevel].elements := 0;
  10090. END;
  10091. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10092. sym := sections[s];
  10093. ELSE
  10094. sym := NIL;
  10095. END;
  10096. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10097. THEN
  10098. INC(scopes[olevel].elements);
  10099. END;
  10100. (*StringPool.GetString(this[level], name);*)
  10101. (*TRACE(level, name);*)
  10102. (* enter string in scope *)
  10103. INC(level);
  10104. END;
  10105. END;
  10106. Basic.SegmentedNameToString(this, name);
  10107. (*TRACE(level, "enter", name);*)
  10108. prev := this;
  10109. END;
  10110. END;
  10111. WHILE 0 <= olevel DO
  10112. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10113. IF olevel > 0 THEN
  10114. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10115. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10116. END;
  10117. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10118. DEC(olevel);
  10119. END;
  10120. level := 0;
  10121. WHILE (level < LEN(scopes)) DO
  10122. IF scopes[level].section # NIL THEN
  10123. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10124. END;
  10125. INC(level);
  10126. END;
  10127. END Export;
  10128. BEGIN
  10129. NEW(fingerPrinter, module.system);
  10130. NEW(poolMap, 64);
  10131. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10132. NEW(sectionArray, module.allSections.Length());
  10133. FOR i := 0 TO module.allSections.Length() - 1 DO
  10134. section := module.allSections.GetSection(i);
  10135. sectionArray[i] := section;
  10136. END;
  10137. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10138. Export(sectionArray^);
  10139. END ExportDesc;
  10140. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10141. VAR
  10142. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10143. BEGIN
  10144. Info(source, "exception table offsets array descriptor");
  10145. size := 0;
  10146. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10147. Info(source, "exception table content");
  10148. FOR i := 0 TO module.allSections.Length() - 1 DO
  10149. p := module.allSections.GetSection(i);
  10150. IF p.type = Sections.CodeSection THEN
  10151. finallyPC := p(IntermediateCode.Section).finally;
  10152. IF finallyPC>=0 THEN
  10153. Symbol( source, p, 0,0);
  10154. Symbol( source, p, finallyPC, 0);
  10155. Symbol( source, p, finallyPC,0);
  10156. INC(size);
  10157. END;
  10158. END
  10159. END;
  10160. PatchArray(source,sizePC,size);
  10161. END ExceptionArray;
  10162. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10163. VAR i: LONGINT; ch: CHAR;
  10164. BEGIN
  10165. i := 0;
  10166. REPEAT
  10167. ch := name[i]; INC(i);
  10168. Char( section, ch);
  10169. UNTIL ch = 0X;
  10170. WHILE i < 32 DO
  10171. Char( section, 0X); INC(i);
  10172. END;
  10173. END Name;
  10174. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10175. VAR i: LONGINT; ch: CHAR;
  10176. BEGIN
  10177. i := 0;
  10178. REPEAT
  10179. ch := name[i]; INC(i);
  10180. Char( section, ch);
  10181. UNTIL ch = 0X;
  10182. WHILE i < 128 DO
  10183. Char( section, 0X); INC(i);
  10184. END;
  10185. END LongName;
  10186. PROCEDURE References(section: IntermediateCode.Section);
  10187. CONST
  10188. rfDirect = 1X; rfIndirect = 3X;
  10189. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10190. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10191. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10192. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10193. rfArrayFlag = 80X;
  10194. VAR
  10195. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10196. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10197. VAR char: CHAR;
  10198. BEGIN
  10199. IF type = NIL THEN char := rfLongint
  10200. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10201. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10202. ELSIF type IS SyntaxTree.CharacterType THEN
  10203. IF type.sizeInBits = 8 THEN char := rfChar8
  10204. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10205. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10206. END;
  10207. ELSIF type IS SyntaxTree.IntegerType THEN
  10208. IF type.sizeInBits = 8 THEN char := rfShortint
  10209. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10210. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10211. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10212. END;
  10213. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10214. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10215. ELSIF type IS SyntaxTree.FloatType THEN
  10216. IF type.sizeInBits = 32 THEN char := rfReal
  10217. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10218. END;
  10219. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10220. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10221. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10222. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10223. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10224. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10225. END;
  10226. IF arrayOf THEN
  10227. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10228. ELSE
  10229. Char(section,char)
  10230. END;
  10231. INC(size);
  10232. END BaseType;
  10233. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10234. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10235. BEGIN
  10236. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10237. IF destination = NIL THEN destination := importedSymbols.BySymbol(type.typeDeclaration) END;
  10238. IF destination = NIL THEN
  10239. (* imported unused record type *)
  10240. Char(section,0X); (* nil type *)
  10241. INC(size);
  10242. type.typeDeclaration.GetName(name);
  10243. (*
  10244. this happens when a symbol from a different module is used but the type desciptor is not necessary to be present in the current module
  10245. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10246. *)
  10247. ELSE
  10248. IF type.pointerType # NIL THEN
  10249. Char(section,rfRecordPointer)
  10250. ELSE
  10251. Char(section,rfRecord);
  10252. END;
  10253. INC(size);
  10254. Longint(section,(destination.offset ));
  10255. INC(size,4);
  10256. END;
  10257. END RecordType;
  10258. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10259. BEGIN
  10260. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10261. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10262. INC(size);
  10263. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10264. END;
  10265. IF type.form = SyntaxTree.Static THEN
  10266. Longint(section,type.staticLength)
  10267. ELSE
  10268. Longint(section,0)
  10269. END;
  10270. INC(size,4);
  10271. END ArrayType;
  10272. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10273. BEGIN
  10274. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10275. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType THEN
  10276. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10277. INC(size);
  10278. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10279. END;
  10280. IF type.form = SyntaxTree.Static THEN
  10281. Longint(section,type.staticLength)
  10282. ELSE
  10283. Longint(section,0)
  10284. END;
  10285. INC(size,4);
  10286. END MathArrayType;
  10287. PROCEDURE Type(type: SyntaxTree.Type);
  10288. BEGIN
  10289. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10290. IF type IS SyntaxTree.BasicType THEN
  10291. BaseType(FALSE,type)
  10292. ELSIF type IS SyntaxTree.RecordType THEN
  10293. RecordType(type(SyntaxTree.RecordType));
  10294. ELSIF type IS SyntaxTree.ArrayType THEN
  10295. ArrayType(type(SyntaxTree.ArrayType))
  10296. ELSIF type IS SyntaxTree.EnumerationType THEN
  10297. BaseType(FALSE,module.system.longintType)
  10298. ELSIF type IS SyntaxTree.PointerType THEN
  10299. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10300. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10301. ELSE
  10302. BaseType(FALSE,type)
  10303. END;
  10304. ELSIF type IS SyntaxTree.ProcedureType THEN
  10305. BaseType(FALSE,type);
  10306. ELSIF type IS SyntaxTree.MathArrayType THEN
  10307. MathArrayType(type(SyntaxTree.MathArrayType));
  10308. ELSIF type IS SyntaxTree.CellType THEN
  10309. BaseType(FALSE,module.system.anyType);
  10310. ELSE HALT(200)
  10311. END;
  10312. END Type;
  10313. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10314. VAR name: ARRAY 256 OF CHAR;
  10315. BEGIN
  10316. IF variable.externalName # NIL THEN RETURN END;
  10317. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10318. INC(size);
  10319. variable.GetName(name);
  10320. Type(variable.type);
  10321. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10322. INC(size,4);
  10323. String(section,name);
  10324. INC(size,Strings.Length(name)+1);
  10325. END WriteVariable;
  10326. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10327. VAR name: ARRAY 256 OF CHAR;
  10328. BEGIN
  10329. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10330. INC(size);
  10331. variable.GetName(name);
  10332. Type(variable.type);
  10333. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10334. INC(size,4);
  10335. variable.GetName(name);
  10336. String(section,name);
  10337. INC(size,Strings.Length(name)+1);
  10338. END WriteParameter;
  10339. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10340. BEGIN
  10341. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10342. IF type IS SyntaxTree.ArrayType THEN
  10343. WITH type: SyntaxTree.ArrayType DO
  10344. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10345. ELSE Char(section,rfOpenArray)
  10346. END;
  10347. INC(size);
  10348. END
  10349. ELSIF type IS SyntaxTree.MathArrayType THEN
  10350. WITH type: SyntaxTree.MathArrayType DO
  10351. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10352. ELSE Char(section,rfOpenArray)
  10353. END;
  10354. INC(size);
  10355. END
  10356. ELSIF type IS SyntaxTree.RecordType THEN
  10357. Char(section,rfRecord);
  10358. INC(size);
  10359. ELSE
  10360. BaseType(FALSE,type);
  10361. END;
  10362. END ReturnType;
  10363. PROCEDURE Procedure(s: Sections.Section);
  10364. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10365. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10366. name: ARRAY 256 OF CHAR;
  10367. BEGIN
  10368. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10369. procedure.GetName(name);
  10370. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10371. Char(section,0F9X);
  10372. INC(size);
  10373. Symbol(section,s,0,0);
  10374. INC(size,4);
  10375. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10376. INC(size,4);
  10377. Longint(section,procedureType.numberParameters);
  10378. INC(size,4);
  10379. ReturnType(procedureType.returnType);
  10380. Longint(section,0); (*! level *)
  10381. INC(size,4);
  10382. Longint(section,0);
  10383. INC(size,4);
  10384. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10385. (*
  10386. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10387. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10388. recordName := "";
  10389. IF record.pointerType # NIL THEN
  10390. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10391. ELSE
  10392. DeclarationName(record.typeDeclaration,recordName);
  10393. END;
  10394. i := 0;
  10395. Info(section,recordName);
  10396. WHILE recordName[i] # 0X DO
  10397. Char(section,recordName[i]); INC(i);
  10398. INC(size);
  10399. END;
  10400. Char(section,".");
  10401. INC(size);
  10402. END;
  10403. *)
  10404. String(section,name);
  10405. INC(size,Strings.Length(name)+1);
  10406. parameter := procedureType.firstParameter;
  10407. WHILE(parameter # NIL) DO
  10408. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10409. parameter := parameter.nextParameter;
  10410. END;
  10411. variable := procedure.procedureScope.firstVariable;
  10412. WHILE(variable # NIL) DO
  10413. WriteVariable(variable,FALSE);
  10414. variable := variable.nextVariable;
  10415. END;
  10416. END Procedure;
  10417. PROCEDURE Scope(s: Sections.Section);
  10418. BEGIN
  10419. Char(section,0F8X);
  10420. INC(size);
  10421. Symbol(section,s,0,0); (* start *)
  10422. INC(size,4);
  10423. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10424. INC(size,4);
  10425. String(section,"$$");
  10426. INC(size,3);
  10427. (* removed variables -- wrongly interpreted by Reflection
  10428. variable := module.module.moduleScope.firstVariable;
  10429. WHILE(variable # NIL) DO
  10430. WriteVariable(variable,FALSE);
  10431. variable := variable.nextVariable;
  10432. END;
  10433. *)
  10434. END Scope;
  10435. BEGIN
  10436. Array(section,sizePC,"");
  10437. size := 0;
  10438. Char(section,0FFX); (* sign for trap writer *)
  10439. INC(size);
  10440. FOR i := 0 TO module.allSections.Length() - 1 DO
  10441. s := module.allSections.GetSection(i);
  10442. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10443. Scope(s) (*! must be first procedure in ref section *)
  10444. END
  10445. END;
  10446. FOR i := 0 TO module.allSections.Length() - 1 DO
  10447. s := module.allSections.GetSection(i);
  10448. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10449. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10450. Procedure(s)
  10451. END
  10452. END;
  10453. PatchArray(section,sizePC,size);
  10454. END References;
  10455. (*
  10456. Command* = RECORD
  10457. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10458. name*: Name; (* name of the procedure *)
  10459. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10460. entryAdr* : ADDRESS; (* entry address of procedure *)
  10461. END;
  10462. *)
  10463. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10464. VAR
  10465. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10466. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10467. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10468. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10469. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10470. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10471. BEGIN
  10472. RETURN
  10473. (type = NIL) OR
  10474. (type.resolved IS SyntaxTree.RecordType) OR
  10475. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10476. (type.resolved IS SyntaxTree.AnyType);
  10477. END TypeAllowed;
  10478. BEGIN
  10479. numberParameters := procedureType.numberParameters;
  10480. RETURN
  10481. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10482. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10483. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10484. END GetProcedureAllowed;
  10485. PROCEDURE WriteType(type : SyntaxTree.Type);
  10486. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10487. name: Basic.SegmentedName; offset: LONGINT;
  10488. BEGIN
  10489. IF type = NIL THEN
  10490. Address(source,0);
  10491. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10492. Address(source,1);
  10493. ELSE
  10494. type := type.resolved;
  10495. IF type IS SyntaxTree.PointerType THEN
  10496. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10497. END;
  10498. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10499. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10500. name[0] := typeDeclaration.name; name[1] := -1;
  10501. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10502. ASSERT(section # NIL);
  10503. ELSE
  10504. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10505. (* TODO *)
  10506. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10507. END;
  10508. IF implementationVisitor.backend.cooperative THEN
  10509. offset := 0;
  10510. ELSE
  10511. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10512. END;
  10513. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10514. END;
  10515. END WriteType;
  10516. BEGIN
  10517. Info(source, "command array descriptor");
  10518. Array(source,sizePC,"Modules.Command");
  10519. numberCommands := 0;
  10520. Info(source, "command array content");
  10521. FOR i := 0 TO module.allSections.Length() - 1 DO
  10522. p := module.allSections.GetSection(i);
  10523. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10524. procedure := p.symbol(SyntaxTree.Procedure);
  10525. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10526. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10527. procedure.GetName(name);
  10528. Name(source,name);
  10529. numberParameters := procedureType.numberParameters;
  10530. (* offset of type of first parameter *)
  10531. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10532. ELSE WriteType(procedureType.firstParameter.type)
  10533. END;
  10534. (* offset of type of return parameter *)
  10535. WriteType(procedureType.returnType);
  10536. (* command name *)
  10537. (* command code offset *)
  10538. Symbol(source,p,0,0);
  10539. INC(numberCommands);
  10540. IF Trace THEN
  10541. D.Ln;
  10542. END;
  10543. END;
  10544. END
  10545. END;
  10546. PatchArray(source,sizePC,numberCommands);
  10547. END CommandArray;
  10548. (* to prevent from double import of different module aliases *)
  10549. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10550. VAR i: SyntaxTree.Import;
  10551. BEGIN
  10552. i := module.module.moduleScope.firstImport;
  10553. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10554. i := i.nextImport;
  10555. END;
  10556. RETURN i = import
  10557. END IsFirstDirectOccurence;
  10558. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10559. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10560. BEGIN
  10561. Array(source,pc,"");
  10562. Info(source, "import module array data");
  10563. IF implementationVisitor.backend.cooperative THEN
  10564. offset := 0;
  10565. ELSE
  10566. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10567. END;
  10568. import := module.module.moduleScope.firstImport;
  10569. numberImports := 0;
  10570. WHILE import # NIL DO
  10571. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10572. Global.GetModuleSegmentedName(import.module,name);
  10573. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10574. NamedSymbol(source, name, NIL, 0, offset);
  10575. INC(numberImports);
  10576. END;
  10577. import := import.nextImport
  10578. END;
  10579. PatchArray(source,pc,numberImports);
  10580. END ImportsArray;
  10581. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10582. VAR
  10583. p: Sections.Section; sizePC, size, i: LONGINT;
  10584. BEGIN
  10585. Info(source, "Type info section");
  10586. size := 0;
  10587. Array(source,sizePC,"Modules.TypeDesc");
  10588. FOR i := 0 TO module.allSections.Length() - 1 DO
  10589. p := module.allSections.GetSection(i);
  10590. WITH p: IntermediateCode.Section DO
  10591. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10592. Symbol(source,p,0,0);
  10593. INC(size);
  10594. END;
  10595. END
  10596. END;
  10597. PatchArray(source,sizePC,size);
  10598. END TypeInfoSection;
  10599. (*
  10600. ProcTableEntry* = RECORD
  10601. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10602. noPtr*: LONGINT;
  10603. END;
  10604. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10605. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10606. *)
  10607. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10608. VAR
  10609. destination: Sections.Section;
  10610. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10611. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10612. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10613. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10614. BEGIN
  10615. Info(procArray,"pcFrom");
  10616. Symbol(procArray,destination,0,0);
  10617. Info(procArray,"pcTo");
  10618. Symbol(procArray,destination,destination.pc,0);
  10619. Info(procArray,"pcStatementBegin");
  10620. Symbol(procArray,destination,destination.validPAFEnter,0);
  10621. Info(procArray,"pcStatementEnd");
  10622. Symbol(procArray,destination,destination.validPAFExit,0);
  10623. IF ~implementationVisitor.backend.cooperative THEN
  10624. Basic.SegmentedNameToString(destination.name, string);
  10625. Info(pointerArray,string);
  10626. procedure := destination.symbol(SyntaxTree.Procedure);
  10627. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10628. variable := procedure.procedureScope.firstVariable;
  10629. WHILE(variable # NIL) DO
  10630. IF ~(variable.untraced) THEN
  10631. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10632. END;
  10633. variable := variable.nextVariable
  10634. END;
  10635. parameter := procedureType.firstParameter;
  10636. WHILE(parameter # NIL) DO
  10637. IF ~(parameter.untraced) THEN
  10638. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10639. END;
  10640. parameter := parameter.nextParameter;
  10641. END;
  10642. END;
  10643. Info(procArray,"numberPointers");
  10644. Longint(procArray,numberPointers);
  10645. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10646. INC(pointerArraySize, numberPointers);
  10647. END PointerOffsets;
  10648. BEGIN
  10649. maxPointers := 0;
  10650. Info(procArray, "proc array descriptor");
  10651. Address(procArray,0);
  10652. Address(procArray,0);
  10653. Address(procArray,0);
  10654. procArraySizePC := procArray.pc;
  10655. Address(procArray,0);
  10656. procArraySize := 0;
  10657. IF ~implementationVisitor.backend.cooperative THEN
  10658. Info(pointerArray, "pointer array descriptor");
  10659. Address(pointerArray,0);
  10660. Address(pointerArray,0);
  10661. Address(pointerArray,0);
  10662. pointerArraySizePC := pointerArray.pc;
  10663. Address(pointerArray,0);
  10664. pointerArraySize := 0;
  10665. END;
  10666. procArraySize := 0;
  10667. FOR i := 0 TO module.allSections.Length() - 1 DO
  10668. destination := module.allSections.GetSection(i);
  10669. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10670. PointerOffsets(destination(IntermediateCode.Section));
  10671. INC(procArraySize);
  10672. END
  10673. END;
  10674. PatchLongint(procArray,procArraySizePC,procArraySize);
  10675. IF ~implementationVisitor.backend.cooperative THEN
  10676. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10677. END;
  10678. END PointersInProcTables;
  10679. (*
  10680. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10681. VAR
  10682. next*: Module; (** once a module is published, all fields are read-only *)
  10683. name*: Name;
  10684. init, published: BOOLEAN;
  10685. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10686. sb*: ADDRESS; <- set to beginning of data section by loader
  10687. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10688. command*: POINTER TO ARRAY OF Command;
  10689. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10690. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10691. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10692. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10693. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10694. data*, code*: Bytes;
  10695. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10696. export*: ExportDesc;
  10697. term*: TerminationHandler;
  10698. exTable*: ExceptionTable;
  10699. noProcs*: LONGINT;
  10700. firstProc*: ADDRESS; <- done by loader
  10701. maxPtrs*: LONGINT;
  10702. crc*: LONGINT;
  10703. *)
  10704. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10705. BEGIN
  10706. Info(section, "cycle");
  10707. Size(section,0);
  10708. Info(section, "references");
  10709. Size(section,0);
  10710. Info(section, "nextMarked");
  10711. Address(section,0);
  10712. Info(section, "nextWatched");
  10713. Address(section,0);
  10714. END BasePointer;
  10715. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10716. BEGIN
  10717. BasePointer(section);
  10718. Info(section, "action");
  10719. Address(section,0);
  10720. Info(section, "monitor");
  10721. Address(section,0);
  10722. END BaseObject;
  10723. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10724. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10725. pooledName: Basic.SegmentedName;
  10726. symbol: SyntaxTree.Symbol;
  10727. BEGIN
  10728. Global.GetModuleName(module.module,name);
  10729. Strings.Append(name,".@Module.@Descriptor");
  10730. Basic.ToSegmentedName(name, pooledName);
  10731. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10732. Symbol(section,descriptorSection,0,0);
  10733. Info(descriptorSection, "descriptor");
  10734. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10735. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10736. Address(descriptorSection,0);
  10737. Global.GetModuleName(module.module,name);
  10738. Strings.Append(name,".@Trace");
  10739. Basic.ToSegmentedName(name, pooledName);
  10740. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10741. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10742. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10743. END ModuleDescriptor;
  10744. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10745. VAR name: ARRAY 128 OF CHAR;
  10746. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10747. symbol: SyntaxTree.Symbol;
  10748. BEGIN
  10749. Global.GetModuleName(module.module,name);
  10750. Strings.Append(name,".@Module");
  10751. Basic.ToSegmentedName(name, pooledName);
  10752. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10753. moduleSection.SetExported(TRUE);
  10754. IF moduleSection.pc = 0 THEN
  10755. IF implementationVisitor.backend.cooperative THEN
  10756. Info(moduleSection, "descriptor");
  10757. ModuleDescriptor(moduleSection);
  10758. BaseObject(moduleSection);
  10759. implementationVisitor.CreateTraceModuleMethod(module.module);
  10760. ELSE
  10761. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10762. Info(moduleSection, "HeapBlock");
  10763. Symbol(moduleSection,moduleSection,2,0);
  10764. Info(moduleSection, "TypeDescriptor");
  10765. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10766. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10767. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10768. END;
  10769. END;
  10770. RETURN moduleSection;
  10771. END ModuleSection;
  10772. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10773. VAR
  10774. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10775. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10776. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10777. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10778. referenceSectionOffset : LONGINT;
  10779. BEGIN
  10780. Global.GetModuleName(module.module,moduleName);
  10781. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10782. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10783. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10784. ImportsArray(importSection);
  10785. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10786. CommandArray(commandsSection);
  10787. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10788. ExceptionArray(exceptionSection);
  10789. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10790. TypeInfoSection(typeInfoSection);
  10791. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10792. References(referenceSection);
  10793. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10794. IF ~implementationVisitor.backend.cooperative THEN
  10795. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10796. ELSE
  10797. ptrTableSection := NIL;
  10798. END;
  10799. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10800. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10801. Array(emptyArraySection,temp,"");
  10802. moduleSection := ModuleSection();
  10803. Info(moduleSection, "nextRoot*: RootObject");
  10804. Address(moduleSection,0);
  10805. Info(moduleSection, "next*: Module");
  10806. Address(moduleSection,0);
  10807. Info(moduleSection, "name*: Name");
  10808. Name(moduleSection,moduleName);
  10809. Info(moduleSection, "init, published: BOOLEAN");
  10810. Boolean(moduleSection,FALSE);
  10811. Boolean(moduleSection,FALSE);
  10812. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10813. Boolean(moduleSection,FALSE);
  10814. Boolean(moduleSection,FALSE);
  10815. Info(moduleSection, "refcnt*: LONGINT");
  10816. Longint(moduleSection,0);
  10817. Info(moduleSection, "sb*: ADDRESS");
  10818. Address(moduleSection,0);
  10819. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10820. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10821. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10822. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10823. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10824. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10825. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10826. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10827. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10828. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10829. Info(moduleSection, "procTable*: ProcTable");
  10830. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10831. Info(moduleSection, "ptrTable*: PtrTable");
  10832. IF ~implementationVisitor.backend.cooperative THEN
  10833. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10834. ELSE
  10835. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10836. END;
  10837. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10838. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10839. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10840. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10841. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10842. Info(moduleSection, "export*: ExportDesc");
  10843. ExportDesc(moduleSection);
  10844. Info(moduleSection, "term*: TerminationHandler");
  10845. Address(moduleSection,0);
  10846. Info(moduleSection, "exTable*: ExceptionTable");
  10847. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10848. Info(moduleSection, "noProcs*: LONGINT");
  10849. Longint(moduleSection,numberProcs);
  10850. Info(moduleSection, "firstProc*: ADDRESS");
  10851. Address(moduleSection,0);
  10852. Info(moduleSection, "maxPtrs*: LONGINT");
  10853. Longint(moduleSection,maxPointers);
  10854. Info(moduleSection, "crc*: LONGINT");
  10855. Longint(moduleSection, 0); (*! must be implemented *)
  10856. Info(moduleSection, "body*: ADDRESS");
  10857. Symbol(moduleSection, bodyProc, 0,0);
  10858. IF implementationVisitor.backend.cooperative THEN
  10859. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10860. Info(moduleSection, "monitor.owner");
  10861. Address(moduleSection,0);
  10862. Info(moduleSection, "monitor.nestingLevel");
  10863. Address(moduleSection,0);
  10864. Info(moduleSection, "monitor.blockedQueue");
  10865. Address(moduleSection,0); Address(moduleSection,0);
  10866. Info(moduleSection, "monitor.waitingQueue");
  10867. Address(moduleSection,0); Address(moduleSection,0);
  10868. Info(moduleSection, "monitor.waitingSentinel");
  10869. Address(moduleSection,0);
  10870. END;
  10871. END Module;
  10872. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10873. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10874. BEGIN
  10875. Array(source,pc,"");
  10876. Info(source, "pointer offsets array data");
  10877. IF scope IS SyntaxTree.RecordScope THEN
  10878. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10879. ELSIF scope IS SyntaxTree.CellScope THEN
  10880. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10881. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10882. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10883. WHILE variable # NIL DO
  10884. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10885. symbol := module.allSections.FindBySymbol(variable);
  10886. ASSERT(symbol # NIL);
  10887. Pointers(0,symbol, source,variable.type,numberPointers);
  10888. END;
  10889. variable := variable.nextVariable;
  10890. END;
  10891. END;
  10892. PatchArray(source,pc,numberPointers);
  10893. END PointerArray;
  10894. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10895. VAR recordType: SyntaxTree.RecordType;
  10896. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10897. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10898. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10899. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10900. segmentedName: Basic.SegmentedName;
  10901. td: SyntaxTree.TypeDeclaration;
  10902. BEGIN
  10903. Array(source,pc,"Modules.FieldEntry");
  10904. Info(source, "FieldArray");
  10905. IF x IS SyntaxTree.RecordType THEN
  10906. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10907. ELSE
  10908. variable := NIL;
  10909. END;
  10910. size :=0;
  10911. WHILE variable # NIL DO
  10912. type := variable.type.resolved;
  10913. Info(source,"offset");
  10914. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10915. Info(source,"type class");
  10916. IF type IS SyntaxTree.PointerType THEN
  10917. Size(source, 1);
  10918. ELSIF type IS SyntaxTree.RecordType THEN
  10919. Size(source, 2);
  10920. ELSIF type IS SyntaxTree.NumberType THEN
  10921. Size(source, 3);
  10922. ELSE
  10923. Size(source, 0);
  10924. END;
  10925. Info(source, "type desc");
  10926. IF type IS SyntaxTree.RecordType THEN
  10927. td := type(SyntaxTree.RecordType).typeDeclaration;
  10928. Global.GetSymbolSegmentedName(td,segmentedName);
  10929. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10930. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10931. ELSE
  10932. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10933. END;
  10934. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10935. Symbol(source, tir, 0, offset);
  10936. ELSE
  10937. Address(source, 0);
  10938. END;
  10939. Info(source,"flags");
  10940. Set(source, {});
  10941. Info(source,"name");
  10942. variable.GetName(name);
  10943. LongName(source, name);
  10944. variable := variable.nextVariable;
  10945. INC(size);
  10946. END;
  10947. PatchArray(source,pc,size);
  10948. END FieldArray;
  10949. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10950. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10951. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10952. sectionName: Basic.SectionName;
  10953. CONST MPO=-40000000H;
  10954. BEGIN
  10955. (*
  10956. TypeDesc* = POINTER TO RECORD
  10957. descSize: LONGINT;
  10958. sentinel: LONGINT; (* = MPO-4 *)
  10959. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10960. flags*: SET;
  10961. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10962. name*: Name;
  10963. END;
  10964. *)
  10965. (* source := module.sections.FindByName(...) *)
  10966. Global.GetSymbolSegmentedName(td,name);
  10967. Basic.AppendToSegmentedName(name,"@Info");
  10968. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10969. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10970. Address(source,MPO-4);
  10971. Info(source, "type tag pointer");
  10972. Symbol( source, tag, offset, 0);
  10973. Info(source, "type flags");
  10974. flags := {};
  10975. IF isProtected THEN INCL(flags,31) END;
  10976. Set( source, flags);
  10977. Info(source, "pointer to module");
  10978. moduleSection := ModuleSection();
  10979. Symbol( source, moduleSection, moduleSection.pc,0);
  10980. Info(source, "type name");
  10981. i := 0;
  10982. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10983. (*
  10984. Global.GetSymbolSegmentedName(td,name);
  10985. Basic.SegmentedNameToString(name, sectionName);
  10986. *)
  10987. Name(source,sectionName);
  10988. source.SetReferenced(FALSE);
  10989. Global.GetSymbolSegmentedName(td,name);
  10990. Basic.AppendToSegmentedName(name,"@Fields");
  10991. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10992. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10993. Info(fieldSection, "HeapBlock");
  10994. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10995. Info(fieldSection, "TypeDescriptor");
  10996. Address(fieldSection,0);
  10997. Info(source, "FieldArray ref");
  10998. Symbol(source, fieldSection, fieldSection.pc, 0);
  10999. FieldArray(fieldSection);
  11000. RETURN source;
  11001. END NewTypeDescriptorInfo;
  11002. PROCEDURE NewTypeDescriptor;
  11003. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  11004. procedure: SyntaxTree.Procedure; baseRecord: SyntaxTree.RecordType;
  11005. baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  11006. numberPointers: LONGINT; padding,offset, i: LONGINT;
  11007. CONST MPO=-40000000H;
  11008. PROCEDURE TdTable(size: LONGINT; reverse: BOOLEAN);
  11009. VAR i: LONGINT;
  11010. PROCEDURE Td(record: SyntaxTree.RecordType);
  11011. VAR baseTD: SyntaxTree.TypeDeclaration; name: Basic.SegmentedName; offset: LONGINT;
  11012. BEGIN
  11013. IF record # NIL THEN
  11014. IF ~reverse THEN Td(record.GetBaseRecord()) END;
  11015. baseTD := record.typeDeclaration;
  11016. Global.GetSymbolSegmentedName(baseTD,name);
  11017. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11018. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11019. ELSE
  11020. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11021. END;
  11022. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(record.recordScope.numberMethods)*module.system.addressSize);
  11023. Symbol(source, tir, 0, offset);
  11024. IF reverse THEN Td(record.GetBaseRecord()) END;
  11025. END;
  11026. END Td;
  11027. BEGIN
  11028. Info(source, "tag table");
  11029. baseRecord := recordType;
  11030. i := 0;
  11031. WHILE baseRecord # NIL DO
  11032. INC(i);
  11033. baseRecord := baseRecord.GetBaseRecord();
  11034. END;
  11035. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  11036. IF ~reverse THEN Td(recordType) END;
  11037. WHILE i < size DO
  11038. Address(source,0);
  11039. INC(i);
  11040. END;
  11041. IF reverse THEN Td(recordType) END;
  11042. END TdTable;
  11043. PROCEDURE MethodTable(reverse: BOOLEAN);
  11044. VAR i,methods: LONGINT;
  11045. BEGIN
  11046. Info(source, "method table");
  11047. IF recordType # NIL THEN
  11048. methods := recordType.recordScope.numberMethods;
  11049. IF reverse THEN
  11050. FOR i := methods-1 TO 0 BY -1 DO
  11051. procedure := recordType.recordScope.FindMethod(i);
  11052. Global.GetSymbolSegmentedName(procedure, name);
  11053. NamedSymbol(source, name,procedure, 0,0);
  11054. END;
  11055. ELSE
  11056. FOR i := 0 TO methods-1 DO
  11057. procedure := recordType.recordScope.FindMethod(i);
  11058. Global.GetSymbolSegmentedName(procedure, name);
  11059. NamedSymbol(source, name,procedure, 0,0);
  11060. END;
  11061. END;
  11062. END;
  11063. END MethodTable;
  11064. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11065. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11066. BEGIN
  11067. Info(source, "method table");
  11068. baseRecord := recordType;
  11069. WHILE baseRecord.baseType # NIL DO
  11070. baseRecord := baseRecord.GetBaseRecord ();
  11071. END;
  11072. GetRecordTypeName (baseRecord, name);
  11073. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11074. IF name = stackFrame THEN
  11075. start := 0;
  11076. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11077. baseRecord := recordType;
  11078. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11079. baseRecord := baseRecord.GetBaseRecord ();
  11080. END;
  11081. IF baseRecord # NIL THEN
  11082. GetRecordTypeName (baseRecord, name);
  11083. IF pointer & ~baseRecord.isObject THEN
  11084. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11085. END;
  11086. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11087. ELSIF recordType.isObject THEN
  11088. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11089. ELSIF pointer THEN
  11090. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11091. ELSE
  11092. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11093. END;
  11094. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11095. Symbol(source, tir, 0, 0);
  11096. start := 0;
  11097. baseRecord := recordType;
  11098. WHILE (baseRecord # NIL) DO
  11099. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11100. baseRecord := baseRecord.GetBaseRecord ();
  11101. END;
  11102. ELSE
  11103. (* explicit trace method: *)
  11104. procedure := recordType.recordScope.FindMethod(0);
  11105. IF ~procedure.isFinalizer THEN
  11106. Global.GetSymbolSegmentedName(procedure, name);
  11107. NamedSymbol(source, name,procedure, 0,0);
  11108. END;
  11109. start := 1;
  11110. END;
  11111. IF (name # stackFrame) & recordType.isObject THEN
  11112. baseRecord := recordType;
  11113. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11114. baseRecord := baseRecord.GetBaseRecord ();
  11115. END;
  11116. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11117. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11118. ELSE
  11119. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11120. END;
  11121. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11122. Symbol(source, tir, 0, 0);
  11123. END;
  11124. methods := recordType.recordScope.numberMethods;
  11125. FOR i := start TO methods-1 DO
  11126. procedure := recordType.recordScope.FindMethod(i);
  11127. IF ~procedure.isFinalizer THEN
  11128. Global.GetSymbolSegmentedName(procedure, name);
  11129. NamedSymbol(source, name,procedure, 0,0);
  11130. END;
  11131. END;
  11132. END CooperativeMethodTable;
  11133. BEGIN
  11134. Global.GetSymbolSegmentedName(td,name);
  11135. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11136. source.SetExported(IsExported(td));
  11137. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11138. IF implementationVisitor.backend.cooperative THEN
  11139. base := NIL;
  11140. baseRecord := recordType.GetBaseRecord();
  11141. IF baseRecord # NIL THEN
  11142. baseTD := baseRecord.typeDeclaration;
  11143. END;
  11144. IF ~recordType.isObject THEN
  11145. Info(source, "parent");
  11146. IF baseRecord # NIL THEN
  11147. Global.GetSymbolSegmentedName(baseTD,name);
  11148. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11149. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11150. ELSE
  11151. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11152. END;
  11153. Symbol(source, tir, 0, 0);
  11154. ELSE
  11155. Address(source,0);
  11156. END;
  11157. Info(source, "record size");
  11158. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11159. source.SetReferenced(FALSE);
  11160. CooperativeMethodTable(FALSE);
  11161. base := source;
  11162. Global.GetSymbolSegmentedName(td,name);
  11163. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11164. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11165. source.SetExported(IsExported(td));
  11166. source.SetReferenced(FALSE);
  11167. END;
  11168. Info(source, "parent");
  11169. IF baseRecord # NIL THEN
  11170. Global.GetSymbolSegmentedName(baseTD,name);
  11171. sym := baseTD;
  11172. IF ~recordType.isObject THEN
  11173. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11174. sym := NIL;
  11175. END;
  11176. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11177. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11178. ELSE
  11179. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11180. END;
  11181. Symbol(source, tir, 0, 0);
  11182. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11183. Address(source,0);
  11184. ELSE
  11185. IF recordType.isObject THEN
  11186. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11187. ELSE
  11188. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11189. END;
  11190. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11191. Symbol(source, tir, 0, 0);
  11192. END;
  11193. Info(source, "base record descriptor");
  11194. Symbol(source, base, 0, 0);
  11195. CooperativeMethodTable(TRUE);
  11196. source.SetReferenced(FALSE);
  11197. IF recordType.hasPointers THEN
  11198. IF ~HasExplicitTraceMethod (recordType) THEN
  11199. implementationVisitor.CreateTraceMethod(recordType);
  11200. END;
  11201. implementationVisitor.CreateResetProcedure(recordType);
  11202. implementationVisitor.CreateAssignProcedure(recordType);
  11203. END;
  11204. ELSIF ~simple THEN
  11205. (*
  11206. MethodEnd = MPO
  11207. ---
  11208. methods (# methods)
  11209. ---
  11210. tags (16)
  11211. ---
  11212. TypeDesc = TypeInfoAdr
  11213. ---
  11214. td adr ---> rec size
  11215. ----
  11216. pointer offsets
  11217. ----
  11218. (padding)
  11219. -----
  11220. empty [2 addresses aligned]
  11221. empty
  11222. empty
  11223. numPtrs
  11224. ---
  11225. pointer offsets
  11226. ---
  11227. *)
  11228. Info(source, "MethodEnd = MPO");
  11229. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11230. source(IntermediateCode.Section).Emit(Data(-1,op));
  11231. MethodTable(TRUE);
  11232. TdTable(TypeTags, TRUE);
  11233. Info(source, "type descriptor info pointer");
  11234. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11235. IF (cellType # NIL) THEN
  11236. IF cellType.sizeInBits < 0 THEN
  11237. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11238. END;
  11239. Info(source, "cell size");
  11240. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11241. ELSE
  11242. Info(source, "record size");
  11243. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11244. END;
  11245. Info(source, "pointer offsets pointer");
  11246. padding := 1- source.pc MOD 2;
  11247. Symbol(source, source, source.pc+1+padding,0);
  11248. IF padding >0 THEN
  11249. Info(source, "padding");
  11250. FOR i := 1 TO padding DO Address(source,0) END;
  11251. END;
  11252. IF cellType # NIL THEN
  11253. PointerArray(source, cellType.cellScope, numberPointers);
  11254. ELSE
  11255. PointerArray(source, recordType.recordScope, numberPointers);
  11256. END;
  11257. ELSE
  11258. (*
  11259. simple:
  11260. td adr --> size
  11261. tag(1)
  11262. tag(2)
  11263. tag(3)
  11264. methods ->
  11265. *)
  11266. Info(source, "record size");
  11267. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11268. TdTable(TypeTags, FALSE);
  11269. MethodTable(FALSE);
  11270. source.SetReferenced(FALSE);
  11271. END;
  11272. END NewTypeDescriptor;
  11273. BEGIN
  11274. x := x.resolved;
  11275. IF (x IS SyntaxTree.PointerType) THEN
  11276. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11277. END;
  11278. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11279. recordType := x(SyntaxTree.RecordType);
  11280. td := x.typeDeclaration;
  11281. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11282. ASSERT(td # NIL);
  11283. section := module.allSections.FindBySymbol(td); (* TODO *)
  11284. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11285. IF implementationVisitor.newObjectFile THEN
  11286. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11287. NewTypeDescriptor
  11288. END;
  11289. ELSE
  11290. (* data section in intermediate code *)
  11291. Global.GetSymbolSegmentedName(td,name);
  11292. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11293. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11294. tir.Emit(Data(-1,op));
  11295. END;
  11296. END;
  11297. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11298. cellType := x(SyntaxTree.CellType);
  11299. td := x.typeDeclaration;
  11300. section := module.allSections.FindBySymbol(td); (* TODO *)
  11301. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11302. IF implementationVisitor.newObjectFile THEN
  11303. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11304. NewTypeDescriptor
  11305. END;
  11306. END;
  11307. END;
  11308. END
  11309. END CheckTypeDeclaration
  11310. END MetaDataGenerator;
  11311. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11312. VAR
  11313. trace-: BOOLEAN;
  11314. traceString-: SyntaxTree.IdentifierString;
  11315. traceModuleName-: SyntaxTree.IdentifierString;
  11316. newObjectFile-: BOOLEAN;
  11317. profile-: BOOLEAN;
  11318. noRuntimeChecks: BOOLEAN;
  11319. simpleMetaData-: BOOLEAN;
  11320. noAsserts: BOOLEAN;
  11321. optimize-: BOOLEAN;
  11322. cooperative-: BOOLEAN;
  11323. preregisterStatic-: BOOLEAN;
  11324. dump-: Basic.Writer;
  11325. cellsAreObjects: BOOLEAN;
  11326. PROCEDURE &InitIntermediateBackend*;
  11327. BEGIN
  11328. simpleMetaData := FALSE;
  11329. newObjectFile := FALSE;
  11330. InitBackend;
  11331. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11332. SetTraceModuleName(DefaultTraceModuleName);
  11333. END InitIntermediateBackend;
  11334. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11335. VAR
  11336. declarationVisitor: DeclarationVisitor;
  11337. implementationVisitor: ImplementationVisitor;
  11338. module: Sections.Module;
  11339. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11340. meta: MetaDataGenerator;
  11341. BEGIN
  11342. ResetError;
  11343. Global.GetSymbolName(x,name);
  11344. IF activeCellsSpecification # NIL THEN
  11345. GetDescription(instructionSet);
  11346. activeCellsSpecification.SetInstructionSet(instructionSet)
  11347. END;
  11348. NEW(module,x,system); (* backend structures *)
  11349. Global.GetModuleName(x, name);
  11350. module.SetModuleName(name);
  11351. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11352. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11353. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11354. declarationVisitor.Module(x,module);
  11355. IF newObjectFile & ~meta.simple THEN
  11356. meta.Module(implementationVisitor.moduleBodySection);
  11357. END;
  11358. GetDescription(platformName);
  11359. module.SetPlatformName(platformName);
  11360. RETURN module
  11361. END GenerateIntermediate;
  11362. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11363. BEGIN RETURN TRUE
  11364. END SupportedImmediate;
  11365. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11366. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11367. END ProcessSyntaxTreeModule;
  11368. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11369. VAR
  11370. result: Sections.Module;
  11371. traceName: Basic.MessageString;
  11372. BEGIN
  11373. ASSERT(intermediateCodeModule IS Sections.Module);
  11374. result := intermediateCodeModule(Sections.Module);
  11375. IF trace THEN
  11376. traceName := "intermediate code trace: ";
  11377. Strings.Append(traceName,traceString);
  11378. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11379. IF (traceString="") OR (traceString="*") THEN
  11380. result.Dump(dump);
  11381. dump.Update
  11382. ELSE
  11383. Sections.DumpFiltered(dump, result, traceString);
  11384. END
  11385. END;
  11386. RETURN result
  11387. END ProcessIntermediateCodeModule;
  11388. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11389. BEGIN instructionSet := "Intermediate";
  11390. END GetDescription;
  11391. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11392. BEGIN
  11393. SELF.newObjectFile := newObjectFile;
  11394. SELF.simpleMetaData := simpleMetaData;
  11395. END SetNewObjectFile;
  11396. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11397. BEGIN COPY(name, traceModuleName)
  11398. END SetTraceModuleName;
  11399. PROCEDURE DefineOptions(options: Options.Options);
  11400. BEGIN
  11401. DefineOptions^(options);
  11402. options.Add(0X,"trace",Options.String);
  11403. options.Add(0X,"runtime",Options.String);
  11404. options.Add(0X,"newObjectFile",Options.Flag);
  11405. options.Add(0X,"traceModule",Options.String);
  11406. options.Add(0X,"profile",Options.Flag);
  11407. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11408. options.Add(0X,"noAsserts",Options.Flag);
  11409. options.Add(0X,"metaData",Options.String);
  11410. options.Add('o',"optimize", Options.Flag);
  11411. options.Add(0X,"preregisterStatic", Options.Flag);
  11412. options.Add(0X,"cellsAreObjects", Options.Flag);
  11413. END DefineOptions;
  11414. PROCEDURE GetOptions(options: Options.Options);
  11415. VAR name,string: SyntaxTree.IdentifierString;
  11416. BEGIN
  11417. GetOptions^(options);
  11418. trace := options.GetString("trace",traceString);
  11419. profile := options.GetFlag("profile");
  11420. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11421. noAsserts := options.GetFlag("noAsserts");
  11422. cooperative := options.GetFlag("cooperative");
  11423. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11424. IF cooperative THEN
  11425. SetRuntimeModuleName("CPU") END
  11426. END;
  11427. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11428. simpleMetaData := TRUE
  11429. END;
  11430. IF options.GetString("metaData",string) THEN
  11431. IF string = "simple" THEN simpleMetaData := TRUE
  11432. ELSIF string ="full" THEN simpleMetaData := FALSE
  11433. END;
  11434. END;
  11435. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11436. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11437. optimize := options.GetFlag("optimize");
  11438. preregisterStatic := options.GetFlag("preregisterStatic");
  11439. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11440. END GetOptions;
  11441. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11442. BEGIN RETURN SymbolFileFormat.Get()
  11443. END DefaultSymbolFileFormat;
  11444. END IntermediateBackend;
  11445. (* ----------------------------------- register allocation ------------------------------------- *)
  11446. (* register mapping scheme
  11447. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11448. spill offset
  11449. part(n) --> ticket <--> physical register
  11450. spill offset
  11451. *)
  11452. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11453. emptyOperand: IntermediateCode.Operand;
  11454. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11455. statCoopResetVariables: LONGINT;
  11456. statCoopModifyAssignments: LONGINT;
  11457. modifyAssignmentsPC : LONGINT;
  11458. statCoopNilCheck: LONGINT;
  11459. statCoopSwitch: LONGINT;
  11460. statCoopAssignProcedure: LONGINT;
  11461. statCoopTraceMethod: LONGINT;
  11462. statCoopResetProcedure: LONGINT;
  11463. statCoopTraceModule: LONGINT;
  11464. PROCEDURE ResetStatistics*;
  11465. BEGIN
  11466. statCoopResetVariables := 0;
  11467. statCoopModifyAssignments := 0;
  11468. statCoopNilCheck:= 0;
  11469. statCoopSwitch:= 0;
  11470. statCoopAssignProcedure:= 0;
  11471. statCoopTraceMethod:= 0;
  11472. statCoopResetProcedure:= 0;
  11473. statCoopTraceModule:= 0;
  11474. END ResetStatistics;
  11475. PROCEDURE Statistics*;
  11476. BEGIN
  11477. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11478. TRACE(statCoopNilCheck, statCoopSwitch);
  11479. TRACE(statCoopAssignProcedure,
  11480. statCoopTraceMethod,
  11481. statCoopResetProcedure,
  11482. statCoopTraceModule)
  11483. END Statistics;
  11484. PROCEDURE GCD(a,b: LONGINT): LONGINT;
  11485. VAR h: LONGINT;
  11486. BEGIN
  11487. WHILE b # 0 DO
  11488. h := a MOD b;
  11489. a := b;
  11490. b := h;
  11491. END;
  11492. RETURN a
  11493. END GCD;
  11494. PROCEDURE SCM(a,b: LONGINT): LONGINT;
  11495. BEGIN
  11496. RETURN a*b DIV GCD(a,b)
  11497. END SCM;
  11498. PROCEDURE CommonAlignment(a,b: LONGINT): LONGINT;
  11499. BEGIN
  11500. (*TRACE(a,b);*)
  11501. IF a = 0 THEN RETURN b
  11502. ELSIF b = 0 THEN RETURN a
  11503. ELSE RETURN SCM(a,b)
  11504. END;
  11505. END CommonAlignment;
  11506. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11507. BEGIN
  11508. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11509. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11510. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11511. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11512. RETURN ~(parameter.type.resolved IS SyntaxTree.ArrayType) & ~(parameter.type.resolved IS SyntaxTree.MathArrayType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11513. END
  11514. END PassBySingleReference;
  11515. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11516. BEGIN
  11517. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11518. END PassInRegister;
  11519. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11520. VAR new: RegisterEntry;
  11521. BEGIN
  11522. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11523. IF queue = NIL THEN
  11524. queue := new
  11525. ELSE
  11526. new.next := queue;
  11527. IF queue#NIL THEN queue.prev := new END;
  11528. queue := new
  11529. END;
  11530. END AddRegisterEntry;
  11531. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11532. VAR this: RegisterEntry;
  11533. BEGIN
  11534. this := queue;
  11535. WHILE (this # NIL) & (this.register # register) DO
  11536. this := this.next;
  11537. END;
  11538. IF this = NIL THEN
  11539. RETURN FALSE
  11540. END;
  11541. ASSERT(this # NIL);
  11542. IF this = queue THEN queue := queue.next END;
  11543. IF this.prev # NIL THEN this.prev.next := this.next END;
  11544. IF this.next # NIL THEN this.next.prev := this.prev END;
  11545. RETURN TRUE
  11546. END RemoveRegisterEntry;
  11547. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11548. BEGIN ASSERT(cond);
  11549. END Assert;
  11550. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11551. BEGIN
  11552. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11553. END ReusableRegister;
  11554. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11555. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11556. BEGIN
  11557. procedure := moduleScope.bodyProcedure;
  11558. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11559. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11560. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11561. procedure.SetScope(moduleScope);
  11562. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11563. procedure.SetAccess(SyntaxTree.Hidden);
  11564. moduleScope.SetBodyProcedure(procedure);
  11565. moduleScope.AddProcedure(procedure);
  11566. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11567. END;
  11568. END EnsureBodyProcedure;
  11569. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11570. VAR import: SyntaxTree.Import;
  11571. s: Basic.MessageString;
  11572. selfName: SyntaxTree.IdentifierString;
  11573. module: SyntaxTree.Module;
  11574. BEGIN
  11575. scope.ownerModule.GetName(selfName);
  11576. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11577. module := scope.ownerModule
  11578. ELSE
  11579. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11580. IF import = NIL THEN
  11581. RETURN NIL
  11582. ELSIF import.module = NIL THEN
  11583. RETURN NIL
  11584. ELSE module := import.module
  11585. END;
  11586. END;
  11587. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11588. END GetSymbol;
  11589. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11590. BEGIN
  11591. op.mode := mode;
  11592. IntermediateCode.InitOperand(op.op);
  11593. IntermediateCode.InitOperand(op.tag);
  11594. IntermediateCode.InitOperand(op.extra);
  11595. op.dimOffset := 0;
  11596. END InitOperand;
  11597. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11598. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11599. BEGIN RETURN IntermediateCode.GetType(system, type)
  11600. END GetType;
  11601. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11602. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11603. BEGIN
  11604. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11605. (*
  11606. UniqueId(name,module,name,adr);
  11607. *)
  11608. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11609. constant.SetValue(value);
  11610. module.moduleScope.AddConstant(constant);
  11611. constant.SetScope(module.moduleScope);
  11612. RETURN constant
  11613. END BuildConstant;
  11614. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11615. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11616. BEGIN
  11617. variable := scope.firstVariable;
  11618. WHILE variable # NIL DO
  11619. IF variable.NeedsTrace() THEN
  11620. RETURN TRUE;
  11621. END;
  11622. variable := variable.nextVariable;
  11623. END;
  11624. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11625. WHILE parameter # NIL DO
  11626. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11627. RETURN TRUE;
  11628. END;
  11629. parameter := parameter.nextParameter;
  11630. END;
  11631. RETURN FALSE;
  11632. END HasPointers;
  11633. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11634. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11635. END IsVariableParameter;
  11636. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11637. VAR parameter: SyntaxTree.Parameter;
  11638. BEGIN
  11639. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11640. WHILE parameter # NIL DO
  11641. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11642. IF parameter.movable THEN RETURN TRUE END;
  11643. parameter := parameter.nextParameter;
  11644. END;
  11645. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11646. END HasVariableParameters;
  11647. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11648. BEGIN
  11649. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11650. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11651. END HasExplicitTraceMethod;
  11652. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11653. BEGIN
  11654. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11655. IF (expression IS SyntaxTree.IntegerValue) THEN
  11656. val := expression(SyntaxTree.IntegerValue).value;
  11657. RETURN TRUE
  11658. ELSE
  11659. RETURN FALSE
  11660. END;
  11661. END IsIntegerConstant;
  11662. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11663. BEGIN
  11664. IF val <= 0 THEN RETURN FALSE END;
  11665. exp := 0;
  11666. WHILE ~ODD(val) DO
  11667. val := val DIV 2;
  11668. INC(exp)
  11669. END;
  11670. RETURN val = 1
  11671. END PowerOf2;
  11672. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11673. VAR procedure: SyntaxTree.Procedure;
  11674. BEGIN
  11675. procedure := record.recordScope.constructor;
  11676. IF procedure = NIL THEN
  11677. record := record.GetBaseRecord();
  11678. IF record # NIL THEN
  11679. procedure := GetConstructor(record)
  11680. END;
  11681. END;
  11682. RETURN procedure;
  11683. END GetConstructor;
  11684. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11685. BEGIN
  11686. value := SHORT(op.intValue);
  11687. RETURN op.mode = IntermediateCode.ModeImmediate;
  11688. END IsIntegerImmediate;
  11689. (** whether a type strictily is a pointer to record or object type
  11690. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11691. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11692. BEGIN
  11693. IF type = NIL THEN
  11694. RETURN FALSE
  11695. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11696. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11697. ELSE
  11698. RETURN FALSE
  11699. END
  11700. END IsStrictlyPointerToRecord;
  11701. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11702. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11703. END IsUnsafePointer;
  11704. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11705. BEGIN type := type.resolved;
  11706. IF type IS SyntaxTree.PointerType THEN
  11707. type := type(SyntaxTree.PointerType).pointerBase;
  11708. type := type.resolved;
  11709. IF type IS SyntaxTree.RecordType THEN
  11710. recordType := type(SyntaxTree.RecordType);
  11711. RETURN TRUE
  11712. ELSE
  11713. RETURN FALSE
  11714. END
  11715. ELSIF type IS SyntaxTree.RecordType THEN
  11716. recordType := type(SyntaxTree.RecordType);
  11717. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11718. ELSIF type IS SyntaxTree.ObjectType THEN
  11719. RETURN TRUE
  11720. ELSIF type IS SyntaxTree.AnyType THEN
  11721. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11722. ELSE
  11723. RETURN FALSE
  11724. END;
  11725. END IsPointerToRecord;
  11726. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11727. BEGIN
  11728. type := type.resolved;
  11729. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11730. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11731. END IsArrayOfSystemByte;
  11732. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11733. BEGIN
  11734. IF type = NIL THEN RETURN FALSE END;
  11735. type := type.resolved;
  11736. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11737. END IsOpenArray;
  11738. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11739. BEGIN
  11740. IF type = NIL THEN RETURN FALSE END;
  11741. type := type.resolved;
  11742. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11743. END IsStaticArray;
  11744. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11745. VAR size: LONGINT;
  11746. BEGIN
  11747. size := 1;
  11748. WHILE (IsStaticArray(type)) DO
  11749. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11750. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11751. END;
  11752. RETURN size;
  11753. END StaticArrayNumElements;
  11754. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11755. BEGIN
  11756. WHILE (IsStaticArray(type)) DO
  11757. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11758. END;
  11759. RETURN type;
  11760. END StaticArrayBaseType;
  11761. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11762. BEGIN
  11763. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11764. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11765. END;
  11766. RETURN type;
  11767. END ArrayBaseType;
  11768. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11769. BEGIN
  11770. IF type = NIL THEN RETURN FALSE END;
  11771. type := type.resolved;
  11772. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11773. END IsDelegate;
  11774. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11775. VAR i: LONGINT;
  11776. BEGIN
  11777. i := 0; type := type.resolved;
  11778. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11779. INC(i);
  11780. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11781. END;
  11782. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11783. INC(i);
  11784. type := type(SyntaxTree.MathArrayType).arrayBase;
  11785. IF type # NIL THEN type := type.resolved END;
  11786. END;
  11787. RETURN i
  11788. END DynamicDim;
  11789. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11790. BEGIN
  11791. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11792. type := type(SyntaxTree.ArrayType).arrayBase;
  11793. END;
  11794. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11795. type := type(SyntaxTree.MathArrayType).arrayBase;
  11796. END;
  11797. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11798. END StaticSize;
  11799. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11800. BEGIN
  11801. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11802. END IsImmediate;
  11803. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11804. BEGIN
  11805. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11806. END IsAddress;
  11807. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11808. BEGIN
  11809. RETURN (x.mode = IntermediateCode.ModeRegister);
  11810. END IsRegister;
  11811. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11812. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11813. BEGIN
  11814. typeDeclaration := recordType.typeDeclaration;
  11815. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11816. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11817. END GetRecordTypeName;
  11818. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11819. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11820. BEGIN
  11821. parSize := 0;
  11822. IF StructuredReturnType(procedureType) THEN
  11823. parameter := procedureType.returnParameter;
  11824. INC(parSize,system.SizeOfParameter(parameter));
  11825. parSize := parSize + (-parSize) MOD system.addressSize;
  11826. END;
  11827. parameter :=procedureType.lastParameter;
  11828. WHILE (parameter # NIL) DO
  11829. INC(parSize,system.SizeOfParameter(parameter));
  11830. parSize := parSize + (-parSize) MOD system.addressSize;
  11831. parameter := parameter.prevParameter;
  11832. END;
  11833. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11834. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11835. RETURN ToMemoryUnits(system,parSize)
  11836. END ParametersSize;
  11837. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11838. BEGIN
  11839. IF type = NIL THEN RETURN FALSE
  11840. ELSE
  11841. type := type.resolved;
  11842. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11843. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11844. END
  11845. END ReturnedAsParameter;
  11846. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11847. BEGIN
  11848. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11849. END StructuredReturnType;
  11850. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11851. BEGIN
  11852. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11853. END IsNested;
  11854. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11855. BEGIN
  11856. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11857. scope := scope.outerScope;
  11858. END;
  11859. RETURN scope # NIL;
  11860. END InCellScope;
  11861. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11862. BEGIN
  11863. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11864. RETURN 0
  11865. ELSE
  11866. *)
  11867. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11868. (*END;*)
  11869. END ProcedureParametersSize;
  11870. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11871. VAR dataUnit: LONGINT;
  11872. BEGIN dataUnit := system.dataUnit;
  11873. ASSERT(size MOD system.dataUnit = 0);
  11874. RETURN size DIV system.dataUnit
  11875. END ToMemoryUnits;
  11876. PROCEDURE Get*(): Backend.Backend;
  11877. VAR backend: IntermediateBackend;
  11878. BEGIN NEW(backend); RETURN backend
  11879. END Get;
  11880. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11881. VAR instruction: IntermediateCode.Instruction;
  11882. BEGIN
  11883. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11884. RETURN instruction
  11885. END Nop;
  11886. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11887. VAR instruction: IntermediateCode.Instruction;
  11888. BEGIN
  11889. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11890. RETURN instruction
  11891. END Mov;
  11892. (* like Mov but ensures that no new register will be allocated for dest *)
  11893. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11894. VAR instruction: IntermediateCode.Instruction;
  11895. BEGIN
  11896. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11897. RETURN instruction
  11898. END MovReplace;
  11899. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11900. VAR instruction: IntermediateCode.Instruction;
  11901. BEGIN
  11902. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11903. RETURN instruction
  11904. END Conv;
  11905. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11906. VAR instruction: IntermediateCode.Instruction;
  11907. BEGIN
  11908. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11909. RETURN instruction
  11910. END Call;
  11911. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11912. VAR op1, op2: IntermediateCode.Operand;
  11913. VAR instruction: IntermediateCode.Instruction;
  11914. BEGIN
  11915. IntermediateCode.InitNumber(op1,pcOffset);
  11916. IntermediateCode.InitNumber(op2,callingConvention);
  11917. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11918. RETURN instruction
  11919. END Exit;
  11920. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11921. VAR instruction: IntermediateCode.Instruction;
  11922. BEGIN
  11923. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11924. RETURN instruction
  11925. END Return;
  11926. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11927. VAR instruction: IntermediateCode.Instruction;
  11928. BEGIN
  11929. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11930. RETURN instruction
  11931. END Result;
  11932. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11933. VAR op1: IntermediateCode.Operand;
  11934. VAR instruction: IntermediateCode.Instruction;
  11935. BEGIN
  11936. IntermediateCode.InitNumber(op1,nr);
  11937. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11938. RETURN instruction
  11939. END Trap;
  11940. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11941. VAR instruction: IntermediateCode.Instruction;
  11942. BEGIN
  11943. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11944. RETURN instruction
  11945. END Br;
  11946. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11947. VAR instruction: IntermediateCode.Instruction;
  11948. BEGIN
  11949. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11950. RETURN instruction
  11951. END Breq;
  11952. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11953. VAR instruction: IntermediateCode.Instruction;
  11954. BEGIN
  11955. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11956. RETURN instruction
  11957. END Brne;
  11958. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11959. VAR instruction: IntermediateCode.Instruction;
  11960. BEGIN
  11961. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11962. RETURN instruction
  11963. END Brge;
  11964. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11965. VAR instruction: IntermediateCode.Instruction;
  11966. BEGIN
  11967. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11968. RETURN instruction
  11969. END Brlt;
  11970. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11971. VAR instruction: IntermediateCode.Instruction;
  11972. BEGIN
  11973. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11974. RETURN instruction
  11975. END Pop;
  11976. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11977. VAR instruction: IntermediateCode.Instruction;
  11978. BEGIN
  11979. ASSERT(op.mode # IntermediateCode.Undefined);
  11980. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11981. RETURN instruction
  11982. END Push;
  11983. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11984. VAR instruction: IntermediateCode.Instruction;
  11985. BEGIN
  11986. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11987. RETURN instruction
  11988. END Neg;
  11989. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11990. VAR instruction: IntermediateCode.Instruction;
  11991. BEGIN
  11992. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11993. RETURN instruction
  11994. END Not;
  11995. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11996. VAR instruction: IntermediateCode.Instruction;
  11997. BEGIN
  11998. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11999. RETURN instruction
  12000. END Abs;
  12001. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12002. VAR instruction: IntermediateCode.Instruction;
  12003. BEGIN
  12004. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  12005. RETURN instruction
  12006. END Mul;
  12007. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12008. VAR instruction: IntermediateCode.Instruction;
  12009. BEGIN
  12010. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  12011. RETURN instruction
  12012. END Div;
  12013. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12014. VAR instruction: IntermediateCode.Instruction;
  12015. BEGIN
  12016. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  12017. RETURN instruction
  12018. END Mod;
  12019. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12020. VAR instruction: IntermediateCode.Instruction;
  12021. BEGIN
  12022. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  12023. RETURN instruction
  12024. END Sub;
  12025. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12026. VAR instruction: IntermediateCode.Instruction;
  12027. BEGIN
  12028. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  12029. RETURN instruction
  12030. END Add;
  12031. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12032. VAR instruction: IntermediateCode.Instruction;
  12033. BEGIN
  12034. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  12035. RETURN instruction
  12036. END And;
  12037. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12038. VAR instruction: IntermediateCode.Instruction;
  12039. BEGIN
  12040. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  12041. RETURN instruction
  12042. END Or;
  12043. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12044. VAR instruction: IntermediateCode.Instruction;
  12045. BEGIN
  12046. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  12047. RETURN instruction
  12048. END Xor;
  12049. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12050. VAR instruction: IntermediateCode.Instruction;
  12051. BEGIN
  12052. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  12053. RETURN instruction
  12054. END Shl;
  12055. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12056. VAR instruction: IntermediateCode.Instruction;
  12057. BEGIN
  12058. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12059. RETURN instruction
  12060. END Shr;
  12061. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12062. VAR instruction: IntermediateCode.Instruction;
  12063. BEGIN
  12064. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12065. RETURN instruction
  12066. END Rol;
  12067. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12068. VAR instruction: IntermediateCode.Instruction;
  12069. BEGIN
  12070. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12071. RETURN instruction
  12072. END Ror;
  12073. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12074. VAR instruction: IntermediateCode.Instruction;
  12075. BEGIN
  12076. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12077. RETURN instruction
  12078. END Cas;
  12079. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12080. VAR instruction: IntermediateCode.Instruction;
  12081. BEGIN
  12082. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12083. RETURN instruction
  12084. END Copy;
  12085. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12086. VAR instruction: IntermediateCode.Instruction;
  12087. BEGIN
  12088. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12089. RETURN instruction
  12090. END Fill;
  12091. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12092. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12093. BEGIN
  12094. string := IntermediateCode.String(s);
  12095. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12096. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12097. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12098. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12099. RETURN instruction
  12100. END Asm;
  12101. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12102. VAR instruction: IntermediateCode.Instruction;
  12103. BEGIN
  12104. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12105. RETURN instruction
  12106. END Data;
  12107. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12108. VAR instruction: IntermediateCode.Instruction;
  12109. BEGIN
  12110. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12111. IntermediateCode.SetSubType(instruction, subtype);
  12112. RETURN instruction
  12113. END SpecialInstruction;
  12114. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12115. VAR op1: IntermediateCode.Operand;
  12116. VAR instruction: IntermediateCode.Instruction;
  12117. BEGIN
  12118. (*! generate a warning if size exceeds a certain limit *)
  12119. (*
  12120. ASSERT(bytes < 1000000); (* sanity check *)
  12121. *)
  12122. ASSERT(0 <= units); (* sanity check *)
  12123. IntermediateCode.InitNumber(op1,units);
  12124. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12125. RETURN instruction
  12126. END Reserve;
  12127. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12128. VAR op1: IntermediateCode.Operand;
  12129. VAR instruction: IntermediateCode.Instruction;
  12130. BEGIN
  12131. IntermediateCode.InitNumber(op1,position);
  12132. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12133. RETURN instruction
  12134. END LabelInstruction;
  12135. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12136. VAR pc: LONGINT;
  12137. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12138. BEGIN
  12139. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12140. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12141. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12142. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12143. IF instr.op1.type.form = IntermediateCode.Float THEN
  12144. RETURN instr.op1.floatValue = op.floatValue
  12145. ELSE
  12146. RETURN instr.op1.intValue = op.intValue
  12147. END;
  12148. END ProvidesValue;
  12149. BEGIN
  12150. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12151. pc := 0;
  12152. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12153. INC(pc);
  12154. END;
  12155. IF pc = data.pc THEN
  12156. data.Emit(Data(-1,vop));
  12157. END;
  12158. RETURN pc
  12159. END EnterImmediate;
  12160. PROCEDURE Init;
  12161. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12162. BEGIN
  12163. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12164. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12165. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12166. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12167. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12168. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12169. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12170. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12171. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12172. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12173. IntermediateCode.InitOperand(emptyOperand);
  12174. FOR i := 0 TO NumberSystemCalls-1 DO
  12175. name := "@SystemCall";
  12176. Basic.AppendNumber(name,i);
  12177. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12178. END;
  12179. END Init;
  12180. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12181. BEGIN
  12182. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12183. END IsExported;
  12184. BEGIN
  12185. Init;
  12186. END FoxIntermediateBackend.
  12187. Compiler.Compile FoxIntermediateBackend.Mod ~