FoxIntermediateBackend.Mod 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323
  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;
  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. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  302. meta.CheckTypeDeclaration(x.type);
  303. ELSIF currentScope IS SyntaxTree.RecordScope THEN
  304. ELSIF currentScope IS SyntaxTree.ProcedureScope THEN
  305. END;
  306. (* do not call Type(x.type) here as this must already performed in the type declaration section ! *)
  307. END VisitVariable;
  308. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  309. VAR name: Basic.SegmentedName; irv: IntermediateCode.Section; op: Operand;
  310. BEGIN
  311. ASSERT(currentScope.outerScope IS SyntaxTree.CellScope);
  312. (* code section for variable *)
  313. Global.GetSymbolSegmentedName(x,name);
  314. irv := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,x,dump);
  315. irv.SetPositionOrAlignment(x.fixed, x.alignment);
  316. (*
  317. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  318. *)
  319. IF x.defaultValue = NIL THEN
  320. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))))
  321. ELSE
  322. implementationVisitor.inData := TRUE;
  323. implementationVisitor.Evaluate(x.defaultValue, op);
  324. irv.Emit(Data(x.position,op.op));
  325. implementationVisitor.inData := FALSE;
  326. END;
  327. meta.CheckTypeDeclaration(x.type);
  328. END VisitParameter;
  329. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  330. BEGIN
  331. Type(x.declaredType); (* => code in objects *)
  332. IF ~(x.declaredType IS SyntaxTree.QualifiedType) & (x.declaredType.resolved IS SyntaxTree.PointerType) THEN
  333. Type(x.declaredType.resolved(SyntaxTree.PointerType).pointerBase);
  334. END;
  335. END VisitTypeDeclaration;
  336. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  337. BEGIN
  338. IF (SyntaxTree.Public * x.access # {}) THEN
  339. implementationVisitor.VisitConstant(x);
  340. END;
  341. END VisitConstant;
  342. PROCEDURE Scope(x: SyntaxTree.Scope);
  343. VAR procedure: SyntaxTree.Procedure;
  344. constant: SyntaxTree.Constant;
  345. variable: SyntaxTree.Variable;
  346. prevScope: SyntaxTree.Scope; typeDeclaration: SyntaxTree.TypeDeclaration;
  347. BEGIN
  348. prevScope := currentScope;
  349. currentScope := x;
  350. (* constants treated in implementation visitor *)
  351. typeDeclaration := x.firstTypeDeclaration;
  352. WHILE typeDeclaration # NIL DO
  353. VisitTypeDeclaration(typeDeclaration);
  354. typeDeclaration := typeDeclaration.nextTypeDeclaration;
  355. END;
  356. variable := x.firstVariable;
  357. WHILE variable # NIL DO
  358. VisitVariable(variable);
  359. variable := variable.nextVariable;
  360. END;
  361. procedure := x.firstProcedure;
  362. WHILE procedure # NIL DO
  363. VisitProcedure(procedure);
  364. procedure := procedure.nextProcedure;
  365. END;
  366. constant := x.firstConstant;
  367. WHILE constant # NIL DO
  368. VisitConstant(constant);
  369. constant := constant.nextConstant;
  370. END;
  371. currentScope := prevScope;
  372. END Scope;
  373. PROCEDURE Parameters(first: SyntaxTree.Parameter);
  374. VAR parameter: SyntaxTree.Parameter;
  375. BEGIN
  376. parameter := first;
  377. WHILE parameter # NIL DO
  378. VisitParameter(parameter);
  379. parameter := parameter.nextParameter;
  380. END;
  381. END Parameters;
  382. PROCEDURE Procedure(x: SyntaxTree.Procedure);
  383. VAR scope: SyntaxTree.ProcedureScope;
  384. prevScope: SyntaxTree.Scope;
  385. inline, finalizer: BOOLEAN;
  386. procedureType: SyntaxTree.ProcedureType;
  387. pc: LONGINT;
  388. stackSize: LONGINT;
  389. name,baseObject: Basic.SegmentedName; ir: IntermediateCode.Section;
  390. null,size,src,dest,fp,res: IntermediateCode.Operand;
  391. cc: LONGINT;
  392. cellType: SyntaxTree.CellType;
  393. registerNumber: LONGINT;
  394. registerClass: IntermediateCode.RegisterClass;
  395. type: IntermediateCode.Type;
  396. formalParameter: SyntaxTree.Parameter;
  397. recordType: SyntaxTree.RecordType;
  398. isModuleBody: BOOLEAN;
  399. PROCEDURE Signature;
  400. VAR parameter: SyntaxTree.Parameter; procedureType: SyntaxTree.ProcedureType; returnType : SyntaxTree.Type;
  401. BEGIN
  402. procedureType := x.type(SyntaxTree.ProcedureType);
  403. returnType := procedureType.returnType;
  404. IF returnType # NIL THEN
  405. meta.CheckTypeDeclaration(returnType)
  406. END;
  407. parameter := procedureType.firstParameter;
  408. WHILE parameter # NIL DO
  409. meta.CheckTypeDeclaration(parameter.type); (* we have to re-export a type, i.e. it has to be present in the list of symbols *)
  410. parameter := parameter.nextParameter;
  411. END;
  412. END Signature;
  413. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  414. VAR result: BOOLEAN;
  415. BEGIN
  416. result := FALSE;
  417. IF x = SyntaxTree.invalidExpression THEN
  418. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  419. result := TRUE;
  420. value := x.resolved(SyntaxTree.IntegerValue).value;
  421. ELSE
  422. Error(x.position,"expression is not an integer constant");
  423. END;
  424. RETURN result;
  425. END CheckIntegerValue;
  426. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; CONST name: ARRAY OF CHAR; VAR value: LONGINT): BOOLEAN;
  427. VAR this: SyntaxTree.Modifier; id: SyntaxTree.Identifier;
  428. BEGIN
  429. this := modifiers; id := SyntaxTree.NewIdentifier(name);
  430. WHILE (this # NIL) & (this.identifier # id) DO
  431. this := this.nextModifier;
  432. END;
  433. IF this # NIL THEN
  434. IF this.expression = NIL THEN
  435. Error(this.position,"expected expression value");
  436. ELSIF CheckIntegerValue(this.expression,value) THEN
  437. END;
  438. RETURN TRUE
  439. ELSE RETURN FALSE
  440. END;
  441. END HasValue;
  442. BEGIN
  443. IF x.externalName # NIL THEN RETURN END;
  444. (*
  445. IF Trace & (dump # NIL) THEN dump.String("DeclarationVisitor:Procedure"); dump.Ln END;
  446. *)
  447. (* code section for this procedure *)
  448. scope := x.procedureScope;
  449. prevScope := currentScope;
  450. currentScope := scope;
  451. procedureType := x.type(SyntaxTree.ProcedureType);
  452. isModuleBody := x = module.module.moduleScope.bodyProcedure;
  453. implementationVisitor.temporaries.Init;
  454. implementationVisitor.usedRegisters := NIL;
  455. implementationVisitor.registerUsageCount.Init;
  456. implementationVisitor.GetCodeSectionNameForSymbol(x, name);
  457. IF (scope.body # NIL) & (x.isInline) THEN
  458. inline := TRUE;
  459. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  460. ir.SetExported(IsExported(x));
  461. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  462. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  463. IF backend.cellsAreObjects THEN
  464. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  465. ir.SetExported(IsExported(x));
  466. ELSE
  467. RETURN; (* cellnet cannot be compiled for final static hardware *)
  468. END;
  469. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  470. inline := FALSE;
  471. AddBodyCallStub(x);
  472. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  473. ir.SetExported(IsExported(x));
  474. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  475. inline := FALSE;
  476. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  477. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := ActiveCells.defaultDataMemorySize END;
  478. AddBodyCallStub(x);
  479. AddStackAllocation(x,stackSize);
  480. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  481. ir.SetExported(IsExported(x));
  482. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  483. inline := FALSE;
  484. Parameters(procedureType.firstParameter);
  485. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  486. ir.SetExported(IsExported(x));
  487. ELSE
  488. inline := FALSE;
  489. IF x.isEntry OR x.isExit THEN
  490. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  491. ir.SetExported(TRUE);
  492. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  493. ELSE
  494. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  495. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  496. END;
  497. END;
  498. cc := procedureType.callingConvention;
  499. IF scope.body # NIL THEN
  500. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  501. registerNumber := 0;
  502. IF ~inline THEN
  503. IF scope.lastVariable = NIL THEN
  504. stackSize := 0
  505. ELSE
  506. stackSize := scope.lastVariable.offsetInBits;
  507. IF stackSize <0 THEN stackSize := -stackSize END;
  508. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  509. END;
  510. (*
  511. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  512. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  513. *)
  514. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  515. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  516. END;
  517. pc := ir.pc-1;
  518. (*
  519. ir.Emit(Nop(position)); (* placeholder for fill *)
  520. *)
  521. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  522. formalParameter := procedureType.lastParameter;
  523. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  524. IF ~PassInRegister(formalParameter) THEN
  525. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  526. ELSE
  527. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  528. type := GetType(system, formalParameter.type);
  529. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  530. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  531. ir.Emit(Mov(-1,dest, src));
  532. implementationVisitor.ReleaseIntermediateOperand(src);
  533. INC(registerNumber);
  534. formalParameter := formalParameter.prevParameter;
  535. END;
  536. END;
  537. END;
  538. ir.EnterValidPAF;
  539. END;
  540. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  541. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  542. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  543. IF scope.lastVariable # NIL THEN
  544. stackSize := scope.lastVariable.offsetInBits;
  545. IF stackSize <0 THEN stackSize := -stackSize END;
  546. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  547. END;
  548. END;
  549. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  550. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  551. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  552. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  553. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  554. ir.EmitAt(pc,Push(size));
  555. implementationVisitor.StaticCallOperand(result,procedure);
  556. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  557. END;
  558. *)
  559. END;
  560. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  561. IF stackSize > 0 THEN
  562. IF (stackSize MOD system.addressSize = 0) THEN
  563. null := IntermediateCode.Immediate(addressType,0);
  564. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  565. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  566. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  567. ELSE
  568. null := IntermediateCode.Immediate(int8,0);
  569. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  570. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  571. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  572. END;
  573. (*! should potentially be called by backend -- enter might initialize
  574. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  575. *)
  576. END;
  577. ir.ExitValidPAF;
  578. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  579. finalizer := FALSE;
  580. IF backend.cooperative & x.isFinalizer THEN
  581. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  582. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  583. GetRecordTypeName(recordType,name);
  584. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  585. END;
  586. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  587. IF backend.cooperative THEN
  588. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  589. IF implementationVisitor.profile & ~isModuleBody THEN
  590. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  591. END;
  592. END;
  593. implementationVisitor.EmitLeave(ir, x.position,cc);
  594. IF finalizer THEN
  595. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  596. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  597. ir.Emit(Br(x.position,dest));
  598. ELSE
  599. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  600. END;
  601. ELSE
  602. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  603. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  604. END;
  605. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  606. IF backend.cooperative THEN
  607. IF HasPointers (scope) THEN
  608. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  609. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  610. ir.Emit(Result(x.position, res));
  611. ir.Emit(Push(x.position, res));
  612. implementationVisitor.ResetVariables(scope);
  613. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  614. ir.Emit(Pop(x.position, res));
  615. ir.Emit(Return(x.position, res));
  616. ELSE
  617. implementationVisitor.ResetVariables(scope);
  618. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  619. END;
  620. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  621. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  622. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  623. ir.Emit(Result(x.position, res));
  624. ir.Emit(Push(x.position, res));
  625. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  626. ir.Emit(Pop(x.position, res));
  627. ir.Emit(Return(x.position, res));
  628. ELSE
  629. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  630. END;
  631. END;
  632. implementationVisitor.EmitLeave(ir,x.position,cc);
  633. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  634. ELSE
  635. ir.Emit(Nop(x.position));
  636. IF scope.body.isUnchecked OR backend.noRuntimeChecks THEN (* return from procedure in any case *)
  637. implementationVisitor.EmitLeave(ir,x.position,cc);
  638. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  639. END;
  640. END;
  641. END
  642. END;
  643. ELSE (* force body for procedures *)
  644. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  645. ir.EnterValidPAF;
  646. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  647. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  648. ir.ExitValidPAF;
  649. implementationVisitor.EmitLeave(ir,x.position,cc);
  650. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  651. END;
  652. Scope(scope);
  653. Signature;
  654. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  655. currentScope := prevScope;
  656. END Procedure;
  657. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  658. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  659. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  660. BEGIN
  661. ASSERT (bodyProcedure # NIL);
  662. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  663. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  664. procedure.SetScope(bodyProcedure.scope);
  665. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  666. procedure.SetAccess(SyntaxTree.Hidden);
  667. Global.GetSymbolSegmentedName (procedure,name);
  668. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  669. ir.SetExported(TRUE);
  670. ir.SetPriority(InitPriority);
  671. Global.GetSymbolSegmentedName (bodyProcedure,name);
  672. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  673. implementationVisitor.currentScope := module.module.moduleScope;
  674. implementationVisitor.section := ir;
  675. implementationVisitor.PushSelfPointer();
  676. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  677. ELSIF backend.preregisterStatic THEN
  678. implementationVisitor.currentScope := module.module.moduleScope;
  679. implementationVisitor.section := ir;
  680. implementationVisitor.PushSelfPointer();
  681. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  682. ELSE
  683. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  684. ir.Emit(Call(bodyProcedure.position,op, 0));
  685. END;
  686. END AddBodyCallStub;
  687. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  688. VAR name: Basic.SegmentedName;
  689. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  690. BEGIN
  691. Global.GetSymbolSegmentedName (symbol,name);
  692. Basic.RemoveSuffix(name);
  693. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  694. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  695. ir.SetExported(TRUE);
  696. ir.SetPriority(FirstPriority);
  697. IntermediateCode.InitImmediate(op,addressType,initStack);
  698. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  699. END AddStackAllocation;
  700. (** entry function to visit a complete module *)
  701. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  702. VAR
  703. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  704. hasDynamicOperatorDeclarations: BOOLEAN;
  705. operator: SyntaxTree.Operator;
  706. import: SyntaxTree.Import;
  707. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  708. BEGIN
  709. type := type.resolved;
  710. IF type IS SyntaxTree.RecordType THEN
  711. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  712. ELSIF (type IS SyntaxTree.ArrayType) THEN
  713. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  714. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  715. END;
  716. ELSIF type IS SyntaxTree.MathArrayType THEN
  717. WITH type: SyntaxTree.MathArrayType DO
  718. IF type.form = SyntaxTree.Open THEN
  719. RETURN TRUE
  720. ELSIF type.form = SyntaxTree.Static THEN
  721. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  722. END;
  723. END;
  724. END;
  725. RETURN FALSE
  726. END TypeNeedsInitialization;
  727. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  728. VAR variable: SyntaxTree.Variable;
  729. BEGIN
  730. variable := scope.firstVariable;
  731. WHILE variable # NIL DO
  732. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  733. IF variable.initializer # NIL THEN RETURN TRUE END;
  734. variable := variable.nextVariable;
  735. END;
  736. RETURN FALSE
  737. END ScopeNeedsInitialization;
  738. BEGIN
  739. ASSERT(x # NIL); ASSERT(module # NIL);
  740. SELF.module := module;
  741. (* add import names to the generated Sections.Module *)
  742. import := x.moduleScope.firstImport;
  743. WHILE import # NIL DO
  744. import.module.GetName(idstr);
  745. module.imports.AddName(idstr);
  746. import := import.nextImport
  747. END;
  748. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  749. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  750. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  751. moduleSelf.SetType(system.anyType);
  752. moduleSelf.SetScope(x.moduleScope);
  753. moduleSelf.SetUntraced(TRUE);
  754. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  755. ir.SetExported(TRUE);
  756. IntermediateCode.InitImmediate(op,addressType,0);
  757. ir.Emit(Data(-1,op));
  758. END;
  759. implementationVisitor.module := module;
  760. implementationVisitor.moduleScope := x.moduleScope;
  761. implementationVisitor.moduleSelf := moduleSelf;
  762. implementationVisitor.canBeLoaded := TRUE;
  763. meta.SetModule(module);
  764. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  765. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  766. END;
  767. IF backend.profile THEN
  768. EnsureBodyProcedure(x.moduleScope);
  769. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  770. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  771. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  772. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  773. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  774. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  775. Global.GetModuleName(module.module,idstr);
  776. implementationVisitor.ProfilerAddModule(idstr);
  777. implementationVisitor.numberProcedures := 0;
  778. END;
  779. implementationVisitor.profile := backend.profile;
  780. (* check if there is at least one dynamic operator locally defined *)
  781. hasDynamicOperatorDeclarations := FALSE;
  782. operator := x.moduleScope.firstOperator;
  783. WHILE operator # NIL DO
  784. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  785. operator := operator.nextOperator
  786. END;
  787. (* add operator initialization code section *)
  788. IF hasDynamicOperatorDeclarations THEN
  789. EnsureBodyProcedure(x.moduleScope);
  790. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  791. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  792. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  793. END;
  794. Scope(x.moduleScope);
  795. IF hasDynamicOperatorDeclarations THEN
  796. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  797. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  798. END;
  799. IF backend.profile THEN
  800. implementationVisitor.ProfilerPatchInit;
  801. END;
  802. END Module;
  803. END DeclarationVisitor;
  804. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  805. RegisterUsageCount*=OBJECT
  806. VAR used: UsedArray; count: LONGINT;
  807. PROCEDURE &Init;
  808. VAR i: LONGINT;
  809. BEGIN
  810. count := 0;
  811. IF used = NIL THEN NEW(used,64); END;
  812. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  813. END Init;
  814. PROCEDURE Grow;
  815. VAR new: UsedArray; size,i: LONGINT;
  816. BEGIN
  817. size := LEN(used)*2;
  818. NEW(new,size);
  819. FOR i := 0 TO LEN(used)-1 DO
  820. new[i].count := used[i].count;
  821. new[i].type := used[i].type;
  822. new[i].map := used[i].map
  823. END;
  824. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  825. used := new
  826. END Grow;
  827. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  828. BEGIN
  829. INC(count);
  830. IF count = LEN(used) THEN Grow END;
  831. used[count].type := type;
  832. used[count].class := class;
  833. used[count].map := count;
  834. RETURN count;
  835. END Next;
  836. PROCEDURE IncUse(register: LONGINT);
  837. BEGIN
  838. INC(used[register].count);
  839. (*
  840. IF (register = 1) & (count > 30) THEN
  841. D.TraceBack;
  842. END;
  843. *)
  844. END IncUse;
  845. PROCEDURE DecUse(register: LONGINT);
  846. BEGIN
  847. DEC(used[register].count);
  848. END DecUse;
  849. PROCEDURE Map(register: LONGINT): LONGINT;
  850. VAR map : LONGINT;
  851. BEGIN
  852. IF register > 0 THEN
  853. map := used[register].map;
  854. WHILE register # map DO register := map; map := used[register].map END;
  855. END;
  856. RETURN register
  857. END Map;
  858. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  859. BEGIN
  860. used[register].map:= to;
  861. used[to].count := used[register].count;
  862. used[register].count := 0;
  863. END Remap;
  864. PROCEDURE Use(register: LONGINT): LONGINT;
  865. BEGIN
  866. IF register< LEN(used) THEN
  867. RETURN used[register].count
  868. ELSE
  869. RETURN 0
  870. END
  871. END Use;
  872. END RegisterUsageCount;
  873. RegisterEntry = POINTER TO RECORD
  874. prev,next: RegisterEntry;
  875. register: LONGINT;
  876. registerClass: IntermediateCode.RegisterClass;
  877. type: IntermediateCode.Type;
  878. END;
  879. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  880. Variables = OBJECT (Basic.List)
  881. VAR
  882. inUse: VariableUse;
  883. registerIndex: LONGINT;
  884. PROCEDURE & Init;
  885. VAR i: LONGINT;
  886. BEGIN
  887. InitList(16);
  888. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  889. registerIndex := 1024;
  890. END Init;
  891. PROCEDURE GetUsage(VAR use: VariableUse);
  892. BEGIN
  893. use := inUse;
  894. END GetUsage;
  895. PROCEDURE SetUsage(CONST use: VariableUse);
  896. BEGIN
  897. inUse := use;
  898. END SetUsage;
  899. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  900. VAR any: ANY;
  901. BEGIN
  902. any := Get(i);;
  903. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  904. END GetVariable;
  905. PROCEDURE Occupy(pos: LONGINT);
  906. BEGIN
  907. INCL(inUse[pos DIV 32], pos MOD 32);
  908. END Occupy;
  909. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  910. BEGIN
  911. Occupy(Length());
  912. Add(v);
  913. END AddVariable;
  914. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  915. VAR var : SyntaxTree.Variable;
  916. BEGIN
  917. FOR pos := 0 TO Length()-1 DO
  918. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  919. var := GetVariable(pos);
  920. IF type.SameType(var.type) THEN
  921. Occupy(pos); RETURN var
  922. END;
  923. END;
  924. END;
  925. pos := Length();
  926. RETURN NIL
  927. END GetFreeVariable;
  928. END Variables;
  929. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  930. SymbolMapper = OBJECT
  931. VAR
  932. first: SymbolMap;
  933. PROCEDURE & Init;
  934. BEGIN
  935. first := NIL;
  936. END Init;
  937. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  938. VAR new: SymbolMap;
  939. BEGIN
  940. NEW(new); new.this := this; new.to := to; new.tag := tag;
  941. new.next := first; first := new;
  942. END Add;
  943. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  944. VAR s: SymbolMap;
  945. BEGIN
  946. s := first;
  947. WHILE (s # NIL) & (s.this # this) DO
  948. s := s.next
  949. END;
  950. RETURN s
  951. END Get;
  952. END SymbolMapper;
  953. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  954. VAR
  955. system: Global.System;
  956. section: IntermediateCode.Section;
  957. module: Sections.Module;
  958. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  959. awaitProcCounter, labelId, constId, caseId: LONGINT;
  960. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  961. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  962. checker: SemanticChecker.Checker;
  963. backend: IntermediateBackend;
  964. meta: MetaDataGenerator;
  965. position: LONGINT;
  966. moduleSelf: SyntaxTree.Variable;
  967. (* variables for hand over of variables / temporary state *)
  968. currentScope: SyntaxTree.Scope;
  969. constantDeclaration : SyntaxTree.Symbol;
  970. result: Operand; (* result of the most recent expression / statement *)
  971. destination: IntermediateCode.Operand;
  972. arrayDestinationTag: IntermediateCode.Operand;
  973. arrayDestinationDimension:LONGINT;
  974. currentLoop: Label; (* variable to hand over loop exit jump list *)
  975. conditional: BOOLEAN;
  976. trueLabel, falseLabel, exitLabel: Label;
  977. locked: BOOLEAN;
  978. (*
  979. usedRegisters: Registers;
  980. *)
  981. registerUsageCount: RegisterUsageCount;
  982. usedRegisters: RegisterEntry;
  983. (* useful operands and types *)
  984. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  985. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  986. commentPrintout: Printout.Printer;
  987. dump: Streams.Writer;
  988. tagsAvailable : BOOLEAN;
  989. supportedInstruction: SupportedInstructionProcedure;
  990. supportedImmediate: SupportedImmediateProcedure;
  991. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  992. emitLabels: BOOLEAN;
  993. runtimeModuleName : SyntaxTree.IdentifierString;
  994. newObjectFile: BOOLEAN;
  995. indexCounter: LONGINT;
  996. profile: BOOLEAN;
  997. profileId, profileInit: IntermediateCode.Section;
  998. profileInitPatchPosition: LONGINT;
  999. numberProcedures: LONGINT;
  1000. procedureResultDesignator : SyntaxTree.Designator;
  1001. operatorInitializationCodeSection: IntermediateCode.Section;
  1002. fingerPrinter: FingerPrinter.FingerPrinter;
  1003. temporaries: Variables;
  1004. canBeLoaded : BOOLEAN;
  1005. currentIsInline: BOOLEAN;
  1006. currentMapper: SymbolMapper;
  1007. currentInlineExit: Label;
  1008. moduleBodySection: IntermediateCode.Section;
  1009. NeedDescriptor : BOOLEAN;
  1010. cooperativeSwitches: BOOLEAN;
  1011. lastSwitchPC: LONGINT;
  1012. isUnchecked: BOOLEAN;
  1013. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1014. newObjectFile: BOOLEAN);
  1015. BEGIN
  1016. SELF.system := system;
  1017. SELF.runtimeModuleName := runtime;
  1018. currentScope := NIL;
  1019. hiddenPointerType := NIL;
  1020. delegatePointerType := NIL;
  1021. awaitProcCounter := 0;
  1022. labelId := 0; constId := 0; labelId := 0;
  1023. SELF.checker := checker;
  1024. SELF.backend := backend;
  1025. position := Diagnostics.Invalid;
  1026. conditional := FALSE;
  1027. locked := FALSE;
  1028. InitOperand(result,ModeUndefined);
  1029. addressType := IntermediateCode.GetType(system,system.addressType);
  1030. setType := IntermediateCode.GetType(system,system.setType);
  1031. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1032. byteType := IntermediateCode.GetType(system, system.byteType);
  1033. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1034. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1035. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1036. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1037. nil := IntermediateCode.Immediate(addressType,0);
  1038. IntermediateCode.InitOperand(destination);
  1039. tagsAvailable := TRUE;
  1040. supportedInstruction := supportedInstructionProcedure;
  1041. supportedImmediate := supportedImmediateProcedure;
  1042. inData := FALSE;
  1043. SELF.emitLabels := emitLabels;
  1044. IntermediateCode.InitOperand(arrayDestinationTag);
  1045. bool := IntermediateCode.GetType(system,system.booleanType);
  1046. IntermediateCode.InitImmediate(false,bool,0);
  1047. IntermediateCode.InitImmediate(true,bool,1);
  1048. SELF.newObjectFile := newObjectFile;
  1049. indexCounter := 0;
  1050. NEW(registerUsageCount);
  1051. usedRegisters := NIL;
  1052. procedureResultDesignator := NIL;
  1053. NEW(fingerPrinter, system);
  1054. NEW(temporaries);
  1055. currentIsInline := FALSE;
  1056. NeedDescriptor := FALSE;
  1057. isUnchecked := backend.noRuntimeChecks;
  1058. END Init;
  1059. TYPE Context = RECORD
  1060. section: IntermediateCode.Section;
  1061. registerUsageCount: RegisterUsageCount;
  1062. usedRegisters: RegisterEntry;
  1063. END;
  1064. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1065. VAR context: Context;
  1066. BEGIN
  1067. context.section := section;
  1068. context.registerUsageCount := registerUsageCount;
  1069. context.usedRegisters := usedRegisters;
  1070. section := new;
  1071. NEW(registerUsageCount);
  1072. usedRegisters := NIL;
  1073. RETURN context;
  1074. END SwitchContext;
  1075. PROCEDURE ReturnToContext(context: Context);
  1076. BEGIN
  1077. section := context.section;
  1078. registerUsageCount := context.registerUsageCount;
  1079. usedRegisters := context.usedRegisters;
  1080. END ReturnToContext;
  1081. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1082. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1083. BEGIN
  1084. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1085. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1086. END;
  1087. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1088. section.SetExported(IsExported(syntaxTreeSymbol));
  1089. RETURN section
  1090. END NewSection;
  1091. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1092. VAR new: LONGINT;
  1093. BEGIN
  1094. new := registerUsageCount.Next(type,class);
  1095. UseRegister(new);
  1096. RETURN new
  1097. END AcquireRegister;
  1098. (** get the name for the code section that represens a certain symbol
  1099. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1100. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1101. VAR
  1102. operatorFingerPrint: SyntaxTree.FingerPrint;
  1103. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1104. BEGIN
  1105. Global.GetSymbolSegmentedName(symbol, name);
  1106. (* if the symbol is an operator, then append the fingerprint to the name *)
  1107. IF symbol IS SyntaxTree.Operator THEN
  1108. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1109. string := "[";
  1110. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1111. Strings.Append(string, operatorFingerPrintString);
  1112. Strings.Append(string, "]");
  1113. Basic.AppendToSegmentedName(name,string);
  1114. END
  1115. END GetCodeSectionNameForSymbol;
  1116. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1117. BEGIN
  1118. IF dump # NIL THEN
  1119. dump.String("enter "); dump.String(s); dump.Ln;
  1120. END;
  1121. END TraceEnter;
  1122. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1123. BEGIN
  1124. IF dump # NIL THEN
  1125. dump.String("exit "); dump.String(s); dump.Ln;
  1126. END;
  1127. END TraceExit;
  1128. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1129. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1130. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1131. VAR i: LONGINT;
  1132. BEGIN
  1133. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1134. IF op.rule # NIL THEN
  1135. FOR i := 0 TO LEN(op.rule)-1 DO
  1136. CheckRegister(op.rule[i])
  1137. END;
  1138. END;
  1139. END CheckRegister;
  1140. BEGIN
  1141. CheckRegister(instruction.op1);
  1142. CheckRegister(instruction.op2);
  1143. CheckRegister(instruction.op3);
  1144. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1145. ELSE section.Emit(instruction);
  1146. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1147. END;
  1148. END Emit;
  1149. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1150. BEGIN
  1151. IF backend.cooperative THEN
  1152. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1153. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1154. ELSE
  1155. Emit(Trap(position,trapNo));
  1156. END;
  1157. END EmitTrap;
  1158. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1159. VAR name: Basic.SegmentedName;
  1160. VAR op1, op2, reg: IntermediateCode.Operand;
  1161. VAR call, nocall: Label;
  1162. VAR parametersSize: LONGINT;
  1163. VAR prevSection: IntermediateCode.Section;
  1164. VAR prevDump: Streams.Writer;
  1165. BEGIN
  1166. ASSERT(~procedure.type(SyntaxTree.ProcedureType).noPAF);
  1167. prevSection := SELF.section;
  1168. SELF.section := section;
  1169. prevDump := dump;
  1170. dump := section.comments;
  1171. IF backend.hasLinkRegister THEN
  1172. Emit(Push(-1, lr));
  1173. END;
  1174. Emit(Push(-1,fp));
  1175. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1176. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1177. GetCodeSectionNameForSymbol(procedure, name);
  1178. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1179. ELSE
  1180. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1181. END;
  1182. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1183. Emit(Push(-1,op1));
  1184. Emit(Mov(-1,fp, sp));
  1185. IF (procedure.procedureScope.body = NIL) OR ~procedure.procedureScope.body.isUnchecked THEN
  1186. NEW(call, section);
  1187. NEW(nocall, section);
  1188. reg := NewRegisterOperand(addressType);
  1189. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1190. Emit(Sub(-1,reg, sp, op1));
  1191. BrltL(call, sp, reg);
  1192. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1193. BrgeL(nocall, sp, op2);
  1194. call.Resolve(section.pc);
  1195. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1196. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1197. Emit(Push(-1, op2));
  1198. Emit(Push(-1, reg));
  1199. ReleaseIntermediateOperand(reg);
  1200. CallThis(position, "Activities","ExpandStack",0);
  1201. Emit(Result(-1, sp));
  1202. nocall.Resolve(section.pc);
  1203. END;
  1204. ELSE
  1205. Emit(Mov(-1, fp, sp));
  1206. END;
  1207. Emit(Enter(-1, callconv, varSize));
  1208. SELF.section := prevSection;
  1209. dump := prevDump;
  1210. END EmitEnter;
  1211. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1212. VAR op1,op2: IntermediateCode.Operand;
  1213. VAR instruction: IntermediateCode.Instruction;
  1214. BEGIN
  1215. IntermediateCode.InitNumber(op1,callconv);
  1216. IntermediateCode.InitNumber(op2,varSize);
  1217. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1218. RETURN instruction
  1219. END Enter;
  1220. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1221. VAR op1: IntermediateCode.Operand;
  1222. VAR instruction: IntermediateCode.Instruction;
  1223. BEGIN
  1224. IntermediateCode.InitNumber(op1,callconv);
  1225. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1226. RETURN instruction
  1227. END Leave;
  1228. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1229. VAR prevSection: IntermediateCode.Section;
  1230. VAR op2: IntermediateCode.Operand;
  1231. BEGIN
  1232. prevSection := SELF.section;
  1233. SELF.section := section;
  1234. Emit(Leave(position, callconv));
  1235. IF backend.cooperative THEN
  1236. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1237. Emit(Add(position, sp, fp, op2));
  1238. ELSE
  1239. Emit(Mov(position, sp, fp));
  1240. END;
  1241. Emit(Pop(position, fp));
  1242. SELF.section := prevSection;
  1243. END EmitLeave;
  1244. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1245. VAR m: SymbolMap;
  1246. BEGIN
  1247. position := x.position;
  1248. IF currentIsInline THEN
  1249. m := currentMapper.Get(x);
  1250. IF m # NIL THEN
  1251. (*
  1252. Printout.Info("mapping from", x);
  1253. Printout.Info("mapping to ", m.to);
  1254. *)
  1255. m.to.Accept(SELF);
  1256. op := result;
  1257. IF m.tag # NIL THEN
  1258. ReleaseIntermediateOperand(result.tag);
  1259. m.tag.Accept(SELF);
  1260. op.tag := result.op;
  1261. ReleaseIntermediateOperand(result.tag);
  1262. END;
  1263. RETURN
  1264. END;
  1265. END;
  1266. x.Accept(SELF);
  1267. op := result;
  1268. END Symbol;
  1269. PROCEDURE Expression(x: SyntaxTree.Expression);
  1270. BEGIN
  1271. position := x.position;
  1272. constantDeclaration := NIL;
  1273. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1274. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1275. END;
  1276. IF x.resolved # NIL THEN
  1277. x.resolved.Accept(SELF)
  1278. ELSE
  1279. x.Accept(SELF)
  1280. END;
  1281. (* check this, was commented out in ActiveCells3 *)
  1282. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1283. Error(x.position, "unsupported modifier");
  1284. END;
  1285. END Expression;
  1286. (*
  1287. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1288. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1289. BEGIN
  1290. temporaries.GetUsage(current);
  1291. FOR i := 0 TO LEN(current)-1 DO
  1292. set := current[i] - previous[i];
  1293. IF set # {} THEN
  1294. FOR j := 0 TO MAX(SET)-1 DO
  1295. IF j IN set THEN
  1296. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1297. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1298. Symbol(variable, op);
  1299. MakeMemory(tmp,op.op,addressType,0);
  1300. ReleaseOperand(op);
  1301. Emit(Mov(position,tmp, nil ) );
  1302. ReleaseIntermediateOperand(tmp);
  1303. END;
  1304. END;
  1305. END;
  1306. END;
  1307. END;
  1308. END ResetUsedTemporaries;
  1309. *)
  1310. PROCEDURE Statement(x: SyntaxTree.Statement);
  1311. VAR use: VariableUse;
  1312. BEGIN
  1313. temporaries.GetUsage(use);
  1314. position := x.position;
  1315. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1316. IF commentPrintout # NIL THEN
  1317. commentPrintout.Statement(x);
  1318. dump.Ln;
  1319. (*dump.Update;*)
  1320. END;
  1321. x.Accept(SELF);
  1322. (*
  1323. CheckRegistersFree();
  1324. *)
  1325. (*ResetUsedTemporaries(use);*)
  1326. temporaries.SetUsage(use);
  1327. END Statement;
  1328. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1329. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1330. *)
  1331. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1332. BEGIN
  1333. IF op.mode = IntermediateCode.ModeMemory THEN
  1334. ReuseCopy(res,op);
  1335. ELSE
  1336. res := op;
  1337. UseIntermediateOperand(res);
  1338. END;
  1339. IntermediateCode.AddOffset(res,offset);
  1340. IntermediateCode.MakeMemory(res,type);
  1341. END MakeMemory;
  1342. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1343. VAR mem: IntermediateCode.Operand;
  1344. BEGIN
  1345. MakeMemory(mem,res,type,offset);
  1346. ReleaseIntermediateOperand(res);
  1347. res := mem;
  1348. END ToMemory;
  1349. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1350. VAR mem: IntermediateCode.Operand;
  1351. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1352. componentType: SyntaxTree.Type;
  1353. BEGIN
  1354. type := type.resolved;
  1355. IF operand.mode = ModeReference THEN
  1356. IF type IS SyntaxTree.RangeType THEN
  1357. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1358. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1359. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1360. ReleaseOperand(operand);
  1361. operand.op := firstOp;
  1362. operand.tag := lastOp;
  1363. operand.extra := stepOp;
  1364. ELSIF type IS SyntaxTree.ComplexType THEN
  1365. componentType := type(SyntaxTree.ComplexType).componentType;
  1366. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1367. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1368. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1369. ReleaseOperand(operand);
  1370. operand.op := firstOp;
  1371. operand.tag := lastOp
  1372. ELSE
  1373. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1374. ReleaseIntermediateOperand(operand.op);
  1375. operand.op := mem;
  1376. END;
  1377. operand.mode := ModeValue;
  1378. END;
  1379. ASSERT(operand.mode = ModeValue);
  1380. END LoadValue;
  1381. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1382. VAR prevConditional: BOOLEAN;
  1383. BEGIN
  1384. prevConditional := conditional;
  1385. conditional := FALSE;
  1386. InitOperand(result, ModeUndefined);
  1387. Expression(x);
  1388. op := result;
  1389. LoadValue(op,x.type.resolved);
  1390. conditional := prevConditional;
  1391. END Evaluate;
  1392. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1393. VAR prevConditional: BOOLEAN;
  1394. BEGIN
  1395. prevConditional := conditional;
  1396. conditional := FALSE;
  1397. InitOperand(result,ModeUndefined);
  1398. Expression(x);
  1399. op := result;
  1400. (*
  1401. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1402. *)
  1403. conditional := prevConditional;
  1404. END Designate;
  1405. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1406. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1407. BEGIN
  1408. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1409. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1410. conditional := TRUE;
  1411. trueLabel := trueL; falseLabel := falseL;
  1412. Expression(x);
  1413. trueL := trueLabel; falseL := falseLabel;
  1414. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1415. END Condition;
  1416. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1417. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1418. BEGIN
  1419. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1420. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1421. RETURN op
  1422. END NewRegisterOperand;
  1423. PROCEDURE UnuseRegister(register: LONGINT);
  1424. BEGIN
  1425. IF (register > 0) THEN
  1426. register := registerUsageCount.Map(register);
  1427. registerUsageCount.DecUse(register);
  1428. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1429. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1430. END;
  1431. IF registerUsageCount.Use(register)=0 THEN
  1432. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1433. Warning(position, "register cannot be removed");
  1434. END;
  1435. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1436. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1437. END;
  1438. ELSIF registerUsageCount.Use(register)<0 THEN
  1439. Warning(position, "register removed too often");
  1440. IF dump # NIL THEN
  1441. dump.String("register removed too often"); dump.Ln; dump.Update;
  1442. END;
  1443. D.TraceBack;
  1444. END;
  1445. END;
  1446. END UnuseRegister;
  1447. PROCEDURE UseRegister(register: LONGINT);
  1448. BEGIN
  1449. IF (register > 0) THEN
  1450. register := registerUsageCount.Map(register);
  1451. registerUsageCount.IncUse(register);
  1452. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1453. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1454. END;
  1455. IF registerUsageCount.Use(register)=1 THEN
  1456. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1457. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1458. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1459. END;
  1460. END;
  1461. END;
  1462. END UseRegister;
  1463. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1464. BEGIN
  1465. UnuseRegister(op.register)
  1466. END ReleaseIntermediateOperand;
  1467. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1468. BEGIN
  1469. UseRegister(op.register)
  1470. END UseIntermediateOperand;
  1471. PROCEDURE ReleaseOperand(CONST op: Operand);
  1472. BEGIN
  1473. UnuseRegister(op.op.register);
  1474. UnuseRegister(op.tag.register);
  1475. UnuseRegister(op.extra.register);
  1476. END ReleaseOperand;
  1477. (* save registers marked in array "markedRegisters" to the stack
  1478. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1479. *)
  1480. PROCEDURE SaveRegisters();
  1481. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1482. BEGIN
  1483. entry := usedRegisters;
  1484. WHILE entry # NIL DO
  1485. type := registerUsageCount.used[entry.register].type;
  1486. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1487. Emit(Push(position,op));
  1488. entry := entry.next;
  1489. END;
  1490. END SaveRegisters;
  1491. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1492. BEGIN
  1493. saved := usedRegisters;
  1494. usedRegisters := NIL;
  1495. END ReleaseUsedRegisters;
  1496. (** remove parameter registers from used queue *)
  1497. PROCEDURE ReleaseParameterRegisters;
  1498. VAR entry,prev,next: RegisterEntry;
  1499. BEGIN
  1500. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1501. WHILE entry # NIL DO
  1502. next := entry.next;
  1503. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1504. registerUsageCount.DecUse(entry.register);
  1505. ASSERT(registerUsageCount.Use(entry.register)=0);
  1506. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1507. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1508. END;
  1509. ELSIF prev = NIL THEN
  1510. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1511. ELSE
  1512. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1513. END;
  1514. entry := next;
  1515. END;
  1516. END ReleaseParameterRegisters;
  1517. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1518. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1519. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1520. BEGIN
  1521. entry := saved;
  1522. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1523. entry := prev;
  1524. WHILE entry # NIL DO
  1525. prev := entry.prev;
  1526. type := entry.type;
  1527. class := entry.registerClass;
  1528. IntermediateCode.InitRegister(op,type,class,entry.register);
  1529. (*
  1530. new := registerUsageCount.Next(type,class);
  1531. registerUsageCount.Remap(entry.register,new);
  1532. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1533. dump.String("remap register "); dump.Int(entry.register,1);
  1534. dump.String("to "); dump.Int(new,1);
  1535. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1536. END;
  1537. entry.register := new;
  1538. *)
  1539. Emit(Pop(position,op));
  1540. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1541. entry := prev;
  1542. END;
  1543. (*
  1544. usedRegisters := saved;
  1545. *)
  1546. END RestoreRegisters;
  1547. PROCEDURE CheckRegistersFree;
  1548. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1549. BEGIN
  1550. IF usedRegisters # NIL THEN
  1551. r := usedRegisters;
  1552. WHILE r # NIL DO
  1553. warning := "register ";
  1554. Strings.AppendInt(warning, r.register);
  1555. Strings.Append(warning, " not released.");
  1556. Warning(position,warning);
  1557. r := r .next;
  1558. END;
  1559. END;
  1560. FOR i := 0 TO registerUsageCount.count-1 DO
  1561. IF registerUsageCount.used[i].count < 0 THEN
  1562. warning := "register ";
  1563. Strings.AppendInt(warning, i);
  1564. Strings.Append(warning, " unused too often.");
  1565. Warning(position,warning);
  1566. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1567. warning := "register ";
  1568. Strings.AppendInt(warning, i);
  1569. Strings.Append(warning, " not unused often enough.");
  1570. Warning(position,warning);
  1571. END;
  1572. END;
  1573. END CheckRegistersFree;
  1574. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1575. Otherwise allocate a new register.
  1576. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1577. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1578. BEGIN
  1579. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1580. UseIntermediateOperand(result);
  1581. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1582. UseIntermediateOperand(result);
  1583. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1584. END;
  1585. END Reuse2;
  1586. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1587. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1588. If not then allocate a new register.
  1589. Does NOT necessarily keep the content of src1 or src2 in result!
  1590. *)
  1591. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1592. BEGIN
  1593. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1594. UseIntermediateOperand(result);
  1595. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1596. UseIntermediateOperand(result);
  1597. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1598. result := alternative; alternative := emptyOperand;
  1599. UseIntermediateOperand(result);
  1600. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1601. END;
  1602. END Reuse2a;
  1603. (* like reuse2 but only one source *)
  1604. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1605. BEGIN
  1606. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1607. UseIntermediateOperand(result);
  1608. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1609. END;
  1610. END Reuse1;
  1611. (* like reuse2a but only one source *)
  1612. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1613. BEGIN
  1614. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1615. UseIntermediateOperand(result);
  1616. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1617. UseIntermediateOperand(result);
  1618. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1619. END;
  1620. END Reuse1a;
  1621. (* like reuse1 but guarantees that content of src1 is in result *)
  1622. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1623. BEGIN
  1624. IF ReusableRegister(src1) THEN
  1625. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1626. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1627. UseIntermediateOperand(result);
  1628. ELSE
  1629. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1630. Emit(Mov(position,result,src1));
  1631. END
  1632. END ReuseCopy;
  1633. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1634. BEGIN
  1635. IF ReusableRegister(src) THEN
  1636. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1637. ELSE
  1638. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1639. Emit(Mov(position,result,src));
  1640. ReleaseIntermediateOperand(src);
  1641. END
  1642. END TransferToRegister;
  1643. (** labels and branches **)
  1644. PROCEDURE NewLabel(): Label;
  1645. VAR label: Label;
  1646. BEGIN
  1647. NEW(label,section); RETURN label;
  1648. END NewLabel;
  1649. PROCEDURE SetLabel(label: Label);
  1650. BEGIN label.Resolve(section.pc);
  1651. END SetLabel;
  1652. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1653. BEGIN
  1654. ASSERT(label # NIL);
  1655. IF label.pc < 0 THEN (* label not yet set *)
  1656. label.AddFixup(section.pc);
  1657. END;
  1658. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1659. END LabelOperand;
  1660. PROCEDURE BrL(label: Label);
  1661. BEGIN
  1662. Emit(Br(position,LabelOperand(label)));
  1663. END BrL;
  1664. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1665. BEGIN
  1666. Emit(Brge(position,LabelOperand(label),left,right));
  1667. END BrgeL;
  1668. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1669. BEGIN
  1670. Emit(Brlt(position,LabelOperand(label),left,right));
  1671. END BrltL;
  1672. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1673. BEGIN
  1674. Emit(Breq(position,LabelOperand(label),left,right));
  1675. END BreqL;
  1676. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1677. BEGIN
  1678. Emit(Brne(position,LabelOperand(label),left,right));
  1679. END BrneL;
  1680. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1681. VAR new: IntermediateCode.Operand;
  1682. BEGIN
  1683. IF Trace THEN TraceEnter("Convert") END;
  1684. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1685. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1686. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1687. & (operand.offset = 0)
  1688. THEN
  1689. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1690. Emit(Conv(position,new,operand));
  1691. ELSE
  1692. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1693. Emit(Conv(position,new,operand));
  1694. ReleaseIntermediateOperand(operand);
  1695. END;
  1696. operand := new;
  1697. 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
  1698. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1699. ELSE
  1700. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1701. Emit(Conv(position,new,operand));
  1702. ReleaseIntermediateOperand(operand);
  1703. operand := new;
  1704. END;
  1705. IF Trace THEN TraceExit("Convert") END;
  1706. END Convert;
  1707. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1708. VAR exit: Label;
  1709. BEGIN
  1710. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1711. exit := NewLabel();
  1712. br(exit,left,right);
  1713. EmitTrap(position,trapNo);
  1714. SetLabel(exit);
  1715. END TrapC;
  1716. (** expressions *)
  1717. (** emit necessary runtime check for set elements **)
  1718. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1719. VAR max: IntermediateCode.Operand;
  1720. BEGIN
  1721. IF isUnchecked THEN RETURN END;
  1722. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1723. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1724. TrapC(BrgeL, max, o, SetElementTrap);
  1725. END;
  1726. END CheckSetElement;
  1727. (** the set that a range represents **)
  1728. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1729. VAR
  1730. operand: Operand;
  1731. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1732. BEGIN
  1733. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1734. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1735. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1736. one := IntermediateCode.Immediate(setType, 1);
  1737. Evaluate(x, operand);
  1738. Convert(operand.op, setType);
  1739. Convert(operand.tag, setType);
  1740. CheckSetElement(operand.op);
  1741. CheckSetElement(operand.tag);
  1742. (* create mask for lower bound
  1743. i.e. shift 11111111 to the left by the value of the lower bound
  1744. *)
  1745. Reuse1(temp, operand.op);
  1746. Emit(Shl(position,temp, allBits, operand.op));
  1747. ReleaseIntermediateOperand(operand.op);
  1748. operand.op := temp;
  1749. (* create mask for upper bound
  1750. i.e. shift 11111111 to the right by the difference between the
  1751. upper bound and the maximum number of set elements
  1752. *)
  1753. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1754. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1755. Reuse1(temp, operand.tag);
  1756. ELSE
  1757. Reuse1(temp, operand.tag);
  1758. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1759. Emit(Sub(position,temp, size, operand.tag));
  1760. END;
  1761. Emit(Shr(position,temp, allBits, operand.tag));
  1762. ReleaseIntermediateOperand(operand.tag);
  1763. operand.tag := temp;
  1764. Reuse2(resultingSet, operand.op, operand.tag);
  1765. (* intersect the two masks *)
  1766. Emit(And(position,resultingSet, operand.op, operand.tag));
  1767. ReleaseOperand(operand);
  1768. RETURN resultingSet
  1769. END SetFromRange;
  1770. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1771. VAR
  1772. res, operand: Operand;
  1773. temp, one, noBits, dest: IntermediateCode.Operand;
  1774. expression: SyntaxTree.Expression;
  1775. i: LONGINT;
  1776. BEGIN
  1777. IF Trace THEN TraceEnter("VisitSet") END;
  1778. dest := destination;
  1779. destination := emptyOperand;
  1780. noBits := IntermediateCode.Immediate(setType, 0);
  1781. one := IntermediateCode.Immediate(setType, 1);
  1782. (* start off with the empty set *)
  1783. InitOperand(res, ModeValue);
  1784. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1785. Emit(Mov(position,res.op, noBits));
  1786. FOR i := 0 TO x.elements.Length() - 1 DO
  1787. expression := x.elements.GetExpression(i);
  1788. IF expression IS SyntaxTree.RangeExpression THEN
  1789. (* range of set elements *)
  1790. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1791. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1792. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1793. ReleaseIntermediateOperand(temp)
  1794. ELSE
  1795. (* singelton element *)
  1796. Evaluate(expression, operand);
  1797. Convert(operand.op, setType);
  1798. CheckSetElement(operand.op);
  1799. (* create subset containing single element *)
  1800. Reuse1(temp, operand.op);
  1801. Emit(Shl(position,temp, one, operand.op));
  1802. ReleaseOperand(operand);
  1803. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1804. ReleaseIntermediateOperand(temp);
  1805. END
  1806. END;
  1807. result := res;
  1808. destination := dest;
  1809. IF Trace THEN TraceExit("VisitSet") END;
  1810. END VisitSet;
  1811. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1812. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1813. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1814. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1815. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1816. element: SyntaxTree.IntegerValue;
  1817. BEGIN
  1818. numberElements := x.elements.Length();
  1819. expression := index.parameters.GetExpression(dim);
  1820. element := expression(SyntaxTree.IntegerValue);
  1821. FOR i := 0 TO numberElements-1 DO
  1822. expression := x.elements.GetExpression(i);
  1823. element.SetValue(i);
  1824. IF expression IS SyntaxTree.MathArrayExpression THEN
  1825. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1826. ELSE
  1827. Assign(index,expression);
  1828. END;
  1829. END;
  1830. END RecursiveAssignment;
  1831. BEGIN
  1832. variable := GetTemporaryVariable(x.type, FALSE);
  1833. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1834. designator.SetType(variable.type);
  1835. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1836. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1837. FOR i := 0 TO dim-1 DO
  1838. element := SyntaxTree.NewIntegerValue(x.position,0);
  1839. element.SetType(system.longintType);
  1840. index.parameters.AddExpression(element);
  1841. END;
  1842. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1843. RecursiveAssignment(x,0);
  1844. Expression(designator);
  1845. END VisitMathArrayExpression;
  1846. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1847. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1848. BEGIN
  1849. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1850. dest := destination; destination := emptyOperand;
  1851. IF x.operator = Scanner.Not THEN
  1852. IF conditional THEN
  1853. Condition(x.left,falseLabel,trueLabel)
  1854. ELSE
  1855. Evaluate(x.left,operand);
  1856. InitOperand(result,ModeValue);
  1857. Reuse1a(result.op,operand.op,dest);
  1858. Emit(Xor(position,result.op,operand.op,true));
  1859. ReleaseOperand(operand);
  1860. END;
  1861. ELSIF x.operator = Scanner.Minus THEN
  1862. Evaluate(x.left,operand);
  1863. InitOperand(result,ModeValue);
  1864. Reuse1a(result.op,operand.op,dest);
  1865. type := x.left.type.resolved;
  1866. IF type IS SyntaxTree.SetType THEN
  1867. Emit(Not(position,result.op,operand.op));
  1868. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1869. Reuse1(result.tag,operand.tag);
  1870. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1871. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1872. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1873. Emit(Neg(position,result.op,operand.op));
  1874. ELSE HALT(200)
  1875. END;
  1876. ReleaseOperand(operand);
  1877. ELSIF x.operator = Scanner.Address THEN
  1878. Designate(x.left,operand);
  1879. operand.mode := ModeValue;
  1880. t0 := x.left.type.resolved;
  1881. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1882. ReleaseIntermediateOperand(operand.op);
  1883. operand.op := operand.tag;
  1884. IntermediateCode.InitOperand(operand.tag);
  1885. END;
  1886. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1887. result := operand;
  1888. ELSE HALT(100)
  1889. END;
  1890. destination := dest;
  1891. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1892. END VisitUnaryExpression;
  1893. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1894. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1895. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1896. BEGIN
  1897. type := type.resolved;
  1898. originalType := type;
  1899. IF type IS SyntaxTree.PointerType THEN
  1900. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1901. END;
  1902. IF type IS SyntaxTree.ObjectType THEN
  1903. BrL(trueL);
  1904. ELSE
  1905. ASSERT(type IS SyntaxTree.RecordType);
  1906. (*
  1907. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1908. *)
  1909. ReuseCopy(left,tag);
  1910. right := TypeDescriptorAdr(type);
  1911. IF ~newObjectFile THEN
  1912. IntermediateCode.MakeMemory(right,addressType);
  1913. END;
  1914. IF backend.cooperative THEN
  1915. repeatL := NewLabel();
  1916. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1917. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1918. END;
  1919. SetLabel(repeatL);
  1920. BreqL(trueL,left,right);
  1921. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1922. BrneL(repeatL,left,nil);
  1923. ELSE
  1924. level := type(SyntaxTree.RecordType).Level();
  1925. (* get type desc tag of level relative to base tag *)
  1926. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1927. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1928. IntermediateCode.MakeMemory(left,addressType);
  1929. BreqL(trueL,left,right);
  1930. END;
  1931. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1932. BrL(falseL);
  1933. END;
  1934. END TypeTest;
  1935. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1936. BEGIN
  1937. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1938. IF dump # NIL THEN
  1939. dump.String(s); dump.Ln;
  1940. END;
  1941. END Error;
  1942. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1943. BEGIN
  1944. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1945. IF dump # NIL THEN
  1946. dump.String(s); dump.Ln; dump.Update;
  1947. END;
  1948. END Warning;
  1949. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1950. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1951. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1952. operand:Operand;
  1953. BEGIN
  1954. previousSection := section;
  1955. Global.GetModuleName(mod,name);
  1956. Strings.Append(name,".@Trace");
  1957. Basic.ToSegmentedName(name, pooledName);
  1958. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1959. IF dump # NIL THEN dump := section.comments END;
  1960. variable := mod.moduleScope.firstVariable;
  1961. WHILE variable # NIL DO
  1962. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1963. Symbol(variable, operand);
  1964. register := operand.op;
  1965. CallTraceMethod(register, variable.type);
  1966. ReleaseIntermediateOperand(register);
  1967. END;
  1968. variable := variable.nextVariable;
  1969. END;
  1970. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1971. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1972. Emit(Br(position,op));
  1973. INC(statCoopTraceModule, section.pc);
  1974. section := previousSection;
  1975. IF dump # NIL THEN dump := section.comments END;
  1976. END CreateTraceModuleMethod;
  1977. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1978. BEGIN
  1979. Emit (Push(position, dst));
  1980. Emit (Push(position, src));
  1981. CallThis(position,"GarbageCollector","Assign", 2);
  1982. END CallAssignPointer;
  1983. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1984. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1985. BEGIN
  1986. IF SemanticChecker.IsPointerType (type) THEN
  1987. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  1988. ELSIF type.IsRecordType() THEN
  1989. Emit (Push(position,dst));
  1990. Emit (Push(position,src));
  1991. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  1992. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1993. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1994. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  1995. ELSIF IsStaticArray(type) THEN
  1996. size := StaticArrayNumElements(type);
  1997. base := StaticArrayBaseType(type);
  1998. IF base.IsRecordType() THEN
  1999. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2000. Emit (Push(position, dst));
  2001. Emit (Push(position, src));
  2002. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2003. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2004. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2005. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2006. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  2007. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2008. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2009. Emit (Push(position, dst));
  2010. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2011. Emit (Push(position, src));
  2012. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2013. ELSE
  2014. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2015. Emit (Push(position, dst));
  2016. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2017. Emit (Push(position, src));
  2018. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2019. ASSERT(StaticArrayBaseType(type).IsPointer());
  2020. END;
  2021. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2022. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2023. Emit (Push(position, dst));
  2024. Emit (Push(position, src));
  2025. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2026. ELSE HALT(100); (* missing ? *)
  2027. END;
  2028. END CallAssignMethod;
  2029. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2030. VAR name: Basic.SegmentedName;
  2031. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2032. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2033. context: Context;
  2034. BEGIN
  2035. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2036. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2037. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2038. GetRecordTypeName (recordType,name);
  2039. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2040. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2041. IF dump # NIL THEN dump := section.comments END;
  2042. variable := recordType.recordScope.firstVariable;
  2043. WHILE variable # NIL DO
  2044. IF variable.NeedsTrace() THEN
  2045. dst := NewRegisterOperand (addressType);
  2046. src := NewRegisterOperand (addressType);
  2047. Emit (Mov(position, dst, parameter1));
  2048. Emit (Mov(position, src, parameter2));
  2049. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2050. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2051. CallAssignMethod(dst, src, variable.type);
  2052. ReleaseIntermediateOperand(src);
  2053. ReleaseIntermediateOperand(dst);
  2054. END;
  2055. variable := variable.nextVariable;
  2056. END;
  2057. recordBase := recordType.GetBaseRecord();
  2058. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2059. GetRecordTypeName (recordBase,name);
  2060. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2061. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2062. Emit(Br(position,op));
  2063. ELSE
  2064. Emit(Exit(position,0,0));
  2065. END;
  2066. IF ~recordType.isObject THEN
  2067. GetRecordTypeName (recordType,name);
  2068. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2069. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2070. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2071. NEW(registerUsageCount);
  2072. usedRegisters := NIL;
  2073. dst := NewRegisterOperand (addressType);
  2074. src := NewRegisterOperand (addressType);
  2075. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2076. Emit(Mov(position, dst, parameter1));
  2077. Emit(Mov(position, src, parameter2));
  2078. label := NewLabel();
  2079. SetLabel(label);
  2080. Emit(Push(position, dst));
  2081. Emit(Push(position, src));
  2082. GetRecordTypeName (recordType,name);
  2083. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2084. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2085. Emit(Call(position, op, 0));
  2086. Emit(Pop(position, src));
  2087. Emit(Pop(position, dst));
  2088. Emit(Add(position, dst, dst, ofs));
  2089. Emit(Add(position, src, src, ofs));
  2090. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2091. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2092. Emit(Exit(position,0,0));
  2093. END;
  2094. INC(statCoopAssignProcedure, section.pc);
  2095. ReturnToContext(context);
  2096. IF dump # NIL THEN dump := section.comments END;
  2097. END CreateAssignProcedure;
  2098. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2099. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2100. BEGIN
  2101. IF IsUnsafePointer (type) THEN
  2102. skip := NewLabel();
  2103. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2104. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2105. BreqL (skip, op, nil);
  2106. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2107. SetLabel (skip);
  2108. ELSIF SemanticChecker.IsPointerType (type) THEN
  2109. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2110. CallThis(position,"GarbageCollector","Mark", 1);
  2111. ELSIF type.IsRecordType() THEN
  2112. Emit (Push(position,register));
  2113. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2114. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2115. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2116. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2117. ELSIF IsStaticArray(type) THEN
  2118. size := StaticArrayNumElements(type);
  2119. base := StaticArrayBaseType(type);
  2120. IF base.IsRecordType() THEN
  2121. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2122. Emit (Push(position, register));
  2123. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2124. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2125. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2126. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2127. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2128. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2129. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2130. Emit (Push(position, register));
  2131. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2132. ELSE
  2133. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2134. Emit (Push(position, register));
  2135. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2136. ASSERT(base.IsPointer());
  2137. END;
  2138. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2139. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2140. CallThis(position,"GarbageCollector","Mark", 1);
  2141. ELSE HALT(100); (* missing ? *)
  2142. END;
  2143. END CallTraceMethod;
  2144. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2145. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2146. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2147. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2148. BEGIN
  2149. previousSection := section;
  2150. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2151. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2152. GetRecordTypeName (recordType,name);
  2153. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2154. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2155. IF dump # NIL THEN dump := section.comments END;
  2156. variable := recordType.recordScope.firstVariable;
  2157. WHILE variable # NIL DO
  2158. IF variable.NeedsTrace() THEN
  2159. register := NewRegisterOperand (addressType);
  2160. Emit (Mov(position,register,parameter1));
  2161. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2162. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2163. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2164. END;
  2165. CallTraceMethod(register, variable.type);
  2166. ReleaseIntermediateOperand(register);
  2167. END;
  2168. variable := variable.nextVariable;
  2169. END;
  2170. recordBase := recordType.GetBaseRecord();
  2171. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2172. recordBase := recordBase.GetBaseRecord();
  2173. END;
  2174. IF recordBase # NIL THEN
  2175. GetRecordTypeName (recordBase,name);
  2176. IF HasExplicitTraceMethod (recordBase) THEN
  2177. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2178. ELSE
  2179. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2180. END;
  2181. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2182. Emit(Br(position,op));
  2183. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2184. Emit(Exit(position,0,0));
  2185. ELSE
  2186. IF recordType.isObject THEN
  2187. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2188. ELSE
  2189. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2190. END;
  2191. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2192. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2193. Emit(Br(position,op));
  2194. END;
  2195. IF ~recordType.isObject THEN
  2196. GetRecordTypeName (recordType,name);
  2197. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2198. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2199. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2200. NEW(registerUsageCount);
  2201. usedRegisters := NIL;
  2202. IF dump # NIL THEN dump := section.comments END;
  2203. register := NewRegisterOperand (addressType);
  2204. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2205. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2206. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2207. END;
  2208. Emit (Push(position,register));
  2209. GetRecordTypeName (recordType,name);
  2210. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2211. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2212. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2213. ReleaseIntermediateOperand(register);
  2214. Emit(Exit(position,0,0));
  2215. GetRecordTypeName (recordType,name);
  2216. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2217. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2218. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2219. NEW(registerUsageCount);
  2220. usedRegisters := NIL;
  2221. register := NewRegisterOperand (addressType);
  2222. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2223. Emit(Mov(position, register, parameter1));
  2224. label := NewLabel();
  2225. SetLabel(label);
  2226. Emit(Push(position, register));
  2227. GetRecordTypeName (recordType,name);
  2228. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2229. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2230. Emit(Call(position, op, 0));
  2231. Emit(Pop(position, register));
  2232. Emit(Add(position, register, register, ofs));
  2233. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2234. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2235. Emit(Exit(position,0,0));
  2236. END;
  2237. INC(statCoopTraceMethod, section.pc);
  2238. ReturnToContext(context);
  2239. IF dump # NIL THEN dump := section.comments END;
  2240. END CreateTraceMethod;
  2241. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2242. VAR name: Basic.SegmentedName;
  2243. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2244. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2245. context: Context;
  2246. BEGIN
  2247. IF recordType.isObject THEN RETURN END;
  2248. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2249. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2250. GetRecordTypeName (recordType,name);
  2251. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2252. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2253. IF dump # NIL THEN dump := section.comments END;
  2254. variable := recordType.recordScope.firstVariable;
  2255. WHILE variable # NIL DO
  2256. IF variable.NeedsTrace() THEN
  2257. dst := NewRegisterOperand (addressType);
  2258. Emit (Mov(position, dst, parameter1));
  2259. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2260. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2261. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2262. END;
  2263. CallResetProcedure(dst, nil, variable.type);
  2264. ReleaseIntermediateOperand(dst);
  2265. END;
  2266. variable := variable.nextVariable;
  2267. END;
  2268. recordBase := recordType.GetBaseRecord();
  2269. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2270. GetRecordTypeName (recordBase,name);
  2271. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2272. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2273. Emit(Br(position,op));
  2274. ELSE
  2275. Emit(Exit(position,0,0));
  2276. END;
  2277. GetRecordTypeName (recordType,name);
  2278. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2279. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2280. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2281. NEW(registerUsageCount);
  2282. usedRegisters := NIL;
  2283. dst := NewRegisterOperand (addressType);
  2284. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2285. Emit(Mov(position, dst, parameter1));
  2286. label := NewLabel();
  2287. SetLabel(label);
  2288. Emit(Push(position, dst));
  2289. GetRecordTypeName (recordType,name);
  2290. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2291. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2292. Emit(Call(position, op, 0));
  2293. Emit(Pop(position, dst));
  2294. Emit(Add(position, dst, dst, ofs));
  2295. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2296. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2297. Emit(Exit(position,0,0));
  2298. INC(statCoopResetProcedure, section.pc);
  2299. ReturnToContext(context);
  2300. IF dump # NIL THEN dump := section.comments END;
  2301. END CreateResetProcedure;
  2302. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2303. VAR name: Basic.SegmentedName; context: Context;
  2304. BEGIN
  2305. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2306. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2307. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2308. IF dump # NIL THEN dump := section.comments END;
  2309. Emit(Push(position,fp));
  2310. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2311. ResetVariables(scope);
  2312. Emit(Pop(position,fp));
  2313. Emit(Exit(position,0,0));
  2314. ReturnToContext(context);
  2315. IF dump # NIL THEN dump := section.comments END;
  2316. END CreateResetMethod;
  2317. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2318. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2319. BEGIN
  2320. IF SemanticChecker.IsPointerType (type) THEN
  2321. Emit (Push(position, dest));
  2322. CallThis(position,"GarbageCollector","Reset", 1);
  2323. ELSIF type.IsRecordType() THEN
  2324. Emit (Push(position, dest));
  2325. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2326. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2327. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2328. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2329. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2330. size := GetArrayLength(type, tag);
  2331. base := ArrayBaseType(type);
  2332. IF base.IsRecordType() THEN
  2333. Emit (Push(position, size));
  2334. Emit (Push(position, dest));
  2335. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2336. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2337. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2338. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2339. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2340. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2341. Emit (Push(position, size));
  2342. Emit (Push(position, dest));
  2343. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2344. ELSE
  2345. Emit (Push(position, size));
  2346. Emit (Push(position, dest));
  2347. CallThis(position,"GarbageCollector","ResetArray", 2);
  2348. ASSERT(ArrayBaseType(type).IsPointer());
  2349. END;
  2350. ReleaseIntermediateOperand(size);
  2351. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2352. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2353. Emit (Push(position, dest));
  2354. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2355. ELSE HALT(100); (* missing ? *)
  2356. END;
  2357. END CallResetProcedure;
  2358. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2359. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2360. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2361. VAR operand: Operand;
  2362. BEGIN
  2363. Symbol (symbol, operand);
  2364. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2365. ReleaseOperand(operand);
  2366. END Reset;
  2367. BEGIN
  2368. previousScope := currentScope;
  2369. currentScope := scope;
  2370. pc := section.pc;
  2371. variable := scope.firstVariable;
  2372. WHILE variable # NIL DO
  2373. IF variable.NeedsTrace() THEN
  2374. Reset (variable);
  2375. END;
  2376. variable := variable.nextVariable;
  2377. END;
  2378. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2379. WHILE parameter # NIL DO
  2380. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2381. Reset (parameter);
  2382. END;
  2383. parameter := parameter.nextParameter;
  2384. END;
  2385. INC(statCoopResetVariables, section.pc - pc);
  2386. currentScope := previousScope;
  2387. END ResetVariables;
  2388. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2389. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2390. VAR op: IntermediateCode.Operand; context: Context;
  2391. BEGIN
  2392. previousSection := section;
  2393. GetCodeSectionNameForSymbol(procedure, name);
  2394. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2395. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2396. IF dump # NIL THEN dump := section.comments END;
  2397. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2398. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2399. Emit(Data(position,op));
  2400. Emit(Data(position,nil));
  2401. IF HasPointers (procedure.procedureScope) THEN
  2402. GetCodeSectionNameForSymbol(procedure, name);
  2403. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2404. ELSE
  2405. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2406. END;
  2407. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2408. Emit(Data(position,op));
  2409. ReturnToContext(context);
  2410. IF dump # NIL THEN dump := section.comments END;
  2411. END CreateProcedureDescriptor;
  2412. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2413. VAR import: SyntaxTree.Import;
  2414. s: Basic.MessageString;
  2415. selfName: SyntaxTree.IdentifierString;
  2416. BEGIN
  2417. moduleScope.ownerModule.GetName(selfName);
  2418. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2419. module := moduleScope.ownerModule
  2420. ELSE
  2421. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2422. IF import = NIL THEN
  2423. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2424. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2425. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2426. s := "Module ";
  2427. Strings.Append(s,moduleName);
  2428. Strings.Append(s," cannot be imported.");
  2429. IF force THEN
  2430. Error(position,s);
  2431. ELSIF canBeLoaded THEN
  2432. Strings.Append(s, "=> no dynamic linking.");
  2433. Warning(position, s);
  2434. canBeLoaded := FALSE;
  2435. END;
  2436. RETURN FALSE
  2437. ELSE
  2438. SELF.module.imports.AddName(moduleName)
  2439. END;
  2440. ELSIF import.module = NIL THEN (* already tried *)
  2441. RETURN FALSE
  2442. END;
  2443. module := import.module;
  2444. END;
  2445. RETURN TRUE
  2446. END AddImport;
  2447. (* needed for old binary object file format*)
  2448. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2449. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2450. BEGIN
  2451. IF AddImport(moduleName,module,TRUE) THEN
  2452. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2453. IF procedure = NIL THEN
  2454. s := "Instruction not supported on target, emulation procedure ";
  2455. Strings.Append(s,moduleName);
  2456. Strings.Append(s,".");
  2457. Strings.Append(s,procedureName);
  2458. Strings.Append(s," not present");
  2459. Error(position,s);
  2460. ELSE
  2461. StaticCallOperand(r,procedure);
  2462. ReleaseOperand(r);
  2463. fp := GetFingerprint(procedure);
  2464. END;
  2465. END;
  2466. END EnsureSymbol;
  2467. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2468. VAR exit: Label; trueL,falseL: Label;
  2469. BEGIN
  2470. trueL := NewLabel();
  2471. falseL := NewLabel();
  2472. exit := NewLabel();
  2473. Condition(x,trueL,falseL);
  2474. InitOperand(result,ModeValue);
  2475. SetLabel(trueL);
  2476. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2477. Emit(Mov(position,result.op,true));
  2478. BrL(exit);
  2479. SetLabel(falseL);
  2480. Emit(MovReplace(position,result.op,false));
  2481. SetLabel(exit);
  2482. END ConditionToValue;
  2483. PROCEDURE ValueToCondition(VAR op: Operand);
  2484. BEGIN
  2485. LoadValue(op,system.booleanType);
  2486. BrneL(trueLabel,op.op, false);
  2487. ReleaseOperand(op);
  2488. BrL(falseLabel);
  2489. END ValueToCondition;
  2490. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2491. VAR size: LONGINT;
  2492. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2493. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2494. BEGIN
  2495. ASSERT(type.form = SyntaxTree.Open);
  2496. baseType := type.arrayBase.resolved;
  2497. IF IsOpenArray(baseType) THEN
  2498. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2499. ELSE
  2500. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2501. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2502. END;
  2503. len := tag;
  2504. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2505. IntermediateCode.MakeMemory(len,addressType);
  2506. UseIntermediateOperand(len);
  2507. Reuse2(res,sizeOperand,len);
  2508. Emit(Mul(position,res,sizeOperand,len));
  2509. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2510. RETURN res
  2511. END GetArraySize;
  2512. BEGIN
  2513. type := type.resolved;
  2514. IF IsOpenArray(type) THEN
  2515. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2516. RETURN tag
  2517. ELSE
  2518. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2519. END;
  2520. ELSE
  2521. size := ToMemoryUnits(system,system.SizeOf(type));
  2522. RETURN IntermediateCode.Immediate(addressType,size)
  2523. END;
  2524. END GetDynamicSize;
  2525. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2526. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2527. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2528. BEGIN
  2529. ASSERT(type.form = SyntaxTree.Open);
  2530. baseType := type.arrayBase.resolved;
  2531. IF IsOpenArray(baseType) THEN
  2532. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2533. ELSE
  2534. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2535. END;
  2536. len := tag;
  2537. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2538. IntermediateCode.MakeMemory(len,addressType);
  2539. UseIntermediateOperand(len);
  2540. Reuse2(res,sizeOperand,len);
  2541. Emit(Mul(position,res,sizeOperand,len));
  2542. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2543. RETURN res
  2544. END GetLength;
  2545. BEGIN
  2546. type := type.resolved;
  2547. IF IsOpenArray(type) THEN
  2548. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2549. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2550. ELSE
  2551. RETURN IntermediateCode.Immediate(addressType,1)
  2552. END;
  2553. END GetArrayLength;
  2554. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2555. VAR result: IntermediateCode.Operand;
  2556. BEGIN
  2557. IF backend.cooperative THEN
  2558. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2559. ELSE
  2560. MakeMemory(result, tag, addressType, 0);
  2561. END;
  2562. RETURN result
  2563. END GetSizeFromTag;
  2564. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2565. VAR parameter: SyntaxTree.Parameter;
  2566. BEGIN
  2567. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2568. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2569. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2570. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2571. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2572. END;
  2573. RETURN GetDynamicSize(e.type, tag);
  2574. END GetArrayOfBytesSize;
  2575. (*
  2576. to find imported symbol. not needed ?
  2577. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2578. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2579. BEGIN
  2580. IF AddImport(moduleName,importedModule,FALSE) THEN
  2581. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2582. RETURN symbol
  2583. ELSE
  2584. RETURN NIL
  2585. END
  2586. END SymbolByName;
  2587. *)
  2588. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2589. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2590. BEGIN
  2591. IF AddImport(moduleName,runtimeModule,force) THEN
  2592. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2593. IF procedure = NIL THEN
  2594. s := "Procedure ";
  2595. Strings.Append(s,moduleName);
  2596. Strings.Append(s,".");
  2597. Strings.Append(s,procedureName);
  2598. Strings.Append(s," not present");
  2599. Error(position,s);
  2600. RETURN FALSE
  2601. ELSE
  2602. RETURN TRUE
  2603. END;
  2604. ELSE RETURN FALSE
  2605. END;
  2606. END GetRuntimeProcedure;
  2607. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2608. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2609. s: Basic.MessageString;
  2610. BEGIN
  2611. Basic.InitSegmentedName(name);
  2612. name[0] := Basic.MakeString(moduleName);
  2613. name[1] := Basic.MakeString(typeName);
  2614. name[2] := -1;
  2615. IF AddImport(moduleName,importedModule, FALSE) THEN
  2616. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2617. IF symbol = NIL THEN
  2618. s := "type ";
  2619. Strings.Append(s,moduleName);
  2620. Strings.Append(s,".");
  2621. Strings.Append(s,typeName);
  2622. Strings.Append(s," not present");
  2623. Error(position,s);
  2624. END;
  2625. ELSE symbol := NIL;
  2626. END;
  2627. IF importedModule = moduleScope.ownerModule THEN
  2628. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2629. ELSE
  2630. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2631. END;
  2632. RETURN symbol
  2633. END GetTypeDescriptor;
  2634. (* 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. *)
  2635. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2636. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2637. pooledName: Basic.SegmentedName; size: LONGINT;
  2638. BEGIN
  2639. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2640. StaticCallOperand(result,procedure);
  2641. IF numberParameters < 0 THEN
  2642. size := ProcedureParametersSize(system,procedure);
  2643. ELSE
  2644. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2645. IF size # ProcedureParametersSize(system,procedure) THEN
  2646. Error(position,"runtime call parameter count mismatch");
  2647. END;
  2648. END;
  2649. Emit(Call(position, result.op, size));
  2650. ReleaseOperand(result);
  2651. ELSE (* only static linking possible *)
  2652. Basic.InitSegmentedName(pooledName);
  2653. pooledName[0] := Basic.MakeString(moduleName);
  2654. pooledName[1] := Basic.MakeString(procedureName);
  2655. pooledName[2] := -1;
  2656. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2657. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2658. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2659. END;
  2660. END CallThis;
  2661. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2662. VAR
  2663. left,right: Operand;
  2664. leftSize, rightSize: IntermediateCode.Operand;
  2665. saved: RegisterEntry;
  2666. reg: IntermediateCode.Operand;
  2667. procedureName: SyntaxTree.IdentifierString;
  2668. BEGIN
  2669. procedureName := "CompareString";
  2670. SaveRegisters();ReleaseUsedRegisters(saved);
  2671. Designate(leftExpression,left);
  2672. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2673. Emit(Push(position,leftSize));
  2674. ReleaseIntermediateOperand(leftSize);
  2675. Emit(Push(position,left.op));
  2676. ReleaseOperand(left);
  2677. Designate(rightExpression,right);
  2678. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2679. Emit(Push(position,rightSize));
  2680. ReleaseIntermediateOperand(rightSize);
  2681. Emit(Push(position,right.op));
  2682. ReleaseOperand(right);
  2683. IF backend.cooperative THEN
  2684. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2685. ELSE
  2686. CallThis(position,runtimeModuleName,procedureName, 4);
  2687. END;
  2688. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2689. Emit(Result(position,reg));
  2690. (*
  2691. AcquireThisRegister(int8,IntermediateCode.Result);
  2692. *)
  2693. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2694. (*
  2695. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2696. *)
  2697. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2698. ReleaseIntermediateOperand(reg);
  2699. BrL(falseLabel);
  2700. END CompareString;
  2701. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2702. VAR
  2703. left,right: Operand;
  2704. leftSize, rightSize: IntermediateCode.Operand;
  2705. saved: RegisterEntry;
  2706. procedureName: SyntaxTree.IdentifierString;
  2707. BEGIN
  2708. procedureName := "CopyString";
  2709. SaveRegisters();ReleaseUsedRegisters(saved);
  2710. Designate(leftExpression,left);
  2711. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2712. Emit(Push(position,leftSize));
  2713. ReleaseIntermediateOperand(leftSize);
  2714. Emit(Push(position,left.op));
  2715. ReleaseOperand(left);
  2716. Designate(rightExpression,right);
  2717. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2718. Emit(Push(position,rightSize));
  2719. ReleaseIntermediateOperand(rightSize);
  2720. Emit(Push(position,right.op));
  2721. ReleaseOperand(right);
  2722. IF backend.cooperative THEN
  2723. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2724. ELSE
  2725. CallThis(position,runtimeModuleName,procedureName,4);
  2726. END;
  2727. RestoreRegisters(saved);
  2728. END CopyString;
  2729. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2730. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2731. leftType,rightType: SyntaxTree.Type;
  2732. leftExpression,rightExpression : SyntaxTree.Expression;
  2733. componentType: IntermediateCode.Type;
  2734. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2735. size: LONGINT;
  2736. BEGIN
  2737. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2738. dest := destination; destination := emptyOperand;
  2739. leftType := x.left.type.resolved;
  2740. rightType := x.right.type.resolved;
  2741. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2742. CASE x.operator OF
  2743. Scanner.Or:
  2744. (* shortcut evaluation of left OR right *)
  2745. IF ~conditional THEN ConditionToValue(x);
  2746. ELSE
  2747. next := NewLabel();
  2748. Condition(x.left,trueLabel,next);
  2749. SetLabel(next);
  2750. Condition(x.right,trueLabel,falseLabel);
  2751. END;
  2752. |Scanner.And:
  2753. (* shortcut evaluation of left & right *)
  2754. IF ~conditional THEN ConditionToValue(x);
  2755. ELSE
  2756. next := NewLabel();
  2757. Condition(x.left,next,falseLabel);
  2758. SetLabel(next);
  2759. Condition(x.right,trueLabel,falseLabel);
  2760. END;
  2761. |Scanner.Is:
  2762. IF ~conditional THEN ConditionToValue(x);
  2763. ELSE
  2764. (* get type desc tag *)
  2765. IF IsPointerToRecord(leftType,recordType) THEN
  2766. Evaluate(x.left,left);
  2767. Dereference(left,recordType,IsUnsafePointer(leftType))
  2768. ELSE
  2769. Designate(x.left,left);
  2770. END;
  2771. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2772. ReleaseOperand(left);
  2773. END;
  2774. |Scanner.Plus:
  2775. Evaluate(x.left,left);
  2776. Evaluate(x.right,right);
  2777. IF leftType IS SyntaxTree.SetType THEN
  2778. InitOperand(result,ModeValue);
  2779. Reuse2a(result.op,left.op,right.op,dest);
  2780. Emit(Or(position,result.op,left.op,right.op));
  2781. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2782. InitOperand(result,ModeValue);
  2783. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2784. Reuse2(result.tag,left.tag,right.tag);
  2785. Emit(Add(position,result.op,left.op,right.op));
  2786. Emit(Add(position,result.tag,left.tag,right.tag))
  2787. ELSE
  2788. InitOperand(result,ModeValue);
  2789. Reuse2a(result.op,left.op,right.op,dest);
  2790. Emit(Add(position,result.op,left.op,right.op));
  2791. END;
  2792. ReleaseOperand(left); ReleaseOperand(right);
  2793. |Scanner.Minus:
  2794. Evaluate(x.left,left);
  2795. Evaluate(x.right,right);
  2796. IF leftType IS SyntaxTree.SetType THEN
  2797. InitOperand(result,ModeValue);
  2798. Reuse1(result.op,right.op);
  2799. Emit(Not(position,result.op,right.op));
  2800. ReleaseOperand(right);
  2801. Emit(And(position,result.op,result.op,left.op));
  2802. ReleaseOperand(left);
  2803. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2804. InitOperand(result,ModeValue);
  2805. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2806. Reuse2(result.tag,left.tag,right.tag);
  2807. Emit(Sub(position,result.op,left.op,right.op));
  2808. Emit(Sub(position,result.tag,left.tag,right.tag));
  2809. ReleaseOperand(left); ReleaseOperand(right)
  2810. ELSE
  2811. InitOperand(result,ModeValue);
  2812. Reuse2a(result.op,left.op,right.op,dest);
  2813. Emit(Sub(position,result.op,left.op,right.op));
  2814. ReleaseOperand(left); ReleaseOperand(right);
  2815. END;
  2816. |Scanner.Times:
  2817. Evaluate(x.left,left);
  2818. Evaluate(x.right,right);
  2819. IF leftType IS SyntaxTree.SetType THEN
  2820. InitOperand(result,ModeValue);
  2821. Reuse2a(result.op,left.op,right.op,dest);
  2822. Emit(And(position,result.op,left.op,right.op));
  2823. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2824. InitOperand(result,ModeValue);
  2825. Reuse1a(result.op,left.op,dest);
  2826. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2827. Emit(Shl(position,result.op,left.op,right.op));
  2828. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2829. InitOperand(result, ModeValue);
  2830. componentType := left.op.type;
  2831. (* TODO: review this *)
  2832. (*
  2833. result.op = left.op * right.op - left.tag * right.tag
  2834. result.tag = left.tag * right.op + left.op * right.tag
  2835. *)
  2836. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2837. Emit(Mul(position,result.op, left.op, right.op));
  2838. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2839. Emit(Mul(position,tempReg, left.tag, right.tag));
  2840. Emit(Sub(position,result.op, result.op, tempReg));
  2841. Reuse2(result.tag, left.tag, right.op);
  2842. Emit(Mul(position,result.tag, left.tag, right.op));
  2843. Emit(Mul(position,tempReg, left.op, right.tag));
  2844. Emit(Add(position,result.tag, result.tag, tempReg));
  2845. ReleaseIntermediateOperand(tempReg)
  2846. ELSE
  2847. InitOperand(result,ModeValue);
  2848. Reuse2a(result.op,left.op,right.op,dest);
  2849. Emit(Mul(position,result.op,left.op,right.op));
  2850. END;
  2851. ReleaseOperand(left); ReleaseOperand(right);
  2852. |Scanner.Div:
  2853. Evaluate(x.left,left);
  2854. Evaluate(x.right,right);
  2855. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2856. InitOperand(result,ModeValue);
  2857. Reuse1a(result.op,left.op,dest);
  2858. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2859. Emit(Shr(position,result.op,left.op,right.op));
  2860. ELSE
  2861. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2862. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2863. IF ~isUnchecked THEN
  2864. exit := NewLabel();
  2865. BrltL(exit,zero,right.op);
  2866. EmitTrap(position,NegativeDivisorTrap);
  2867. SetLabel(exit);
  2868. END;
  2869. END;
  2870. InitOperand(result,ModeValue);
  2871. Reuse2a(result.op,left.op,right.op,dest);
  2872. Emit(Div(position,result.op,left.op,right.op));
  2873. END;
  2874. ReleaseOperand(left); ReleaseOperand(right);
  2875. |Scanner.Mod:
  2876. Evaluate(x.left,left);
  2877. Evaluate(x.right,right);
  2878. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2879. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2880. InitOperand(result,ModeValue);
  2881. Reuse1a(result.op,left.op,dest);
  2882. Emit(And(position,result.op,left.op,right.op));
  2883. ELSE
  2884. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2885. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2886. IF ~isUnchecked THEN
  2887. exit := NewLabel();
  2888. BrltL(exit,zero,right.op);
  2889. EmitTrap(position,NegativeDivisorTrap);
  2890. SetLabel(exit);
  2891. END;
  2892. END;
  2893. InitOperand(result,ModeValue);
  2894. Reuse2a(result.op,left.op,right.op,dest);
  2895. Emit(Mod(position,result.op,left.op,right.op));
  2896. END;
  2897. ReleaseOperand(left); ReleaseOperand(right);
  2898. |Scanner.Slash:
  2899. Evaluate(x.left,left);
  2900. Evaluate(x.right,right);
  2901. IF leftType IS SyntaxTree.SetType THEN
  2902. InitOperand(result,ModeValue);
  2903. Reuse2a(result.op,left.op,right.op,dest);
  2904. Emit(Xor(position,result.op,left.op,right.op));
  2905. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2906. InitOperand(result,ModeValue);
  2907. componentType := left.op.type;
  2908. (* review this *)
  2909. (*
  2910. divisor = right.op * right.op + right.tag * right.tag
  2911. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2912. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2913. *)
  2914. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2915. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2916. Emit(Mul(position,tempReg, right.op, right.op));
  2917. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2918. Emit(Add(position,tempReg, tempReg, tempReg2));
  2919. result.op := tempReg2;
  2920. Emit(Mul(position,result.op, left.op, right.op));
  2921. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2922. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2923. Emit(Add(position,result.op, result.op, tempReg2));
  2924. Emit(Div(position,result.op, result.op, tempReg));
  2925. Reuse2(result.tag, left.tag, right.op);
  2926. Emit(Mul(position,result.tag, left.tag, right.op));
  2927. Emit(Mul(position,tempReg2, left.op, right.tag));
  2928. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2929. Emit(Div(position,result.tag, result.tag, tempReg));
  2930. ReleaseIntermediateOperand(tempReg);
  2931. ReleaseIntermediateOperand(tempReg2)
  2932. ELSE
  2933. InitOperand(result,ModeValue);
  2934. Reuse2a(result.op,left.op,right.op,dest);
  2935. Emit(Div(position,result.op,left.op,right.op));
  2936. END;
  2937. ReleaseOperand(left); ReleaseOperand(right);
  2938. |Scanner.Equal:
  2939. IF ~conditional THEN ConditionToValue(x);
  2940. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2941. CompareString(BreqL,x.left,x.right);
  2942. ELSE
  2943. Evaluate(x.left,left);
  2944. Evaluate(x.right,right);
  2945. IF leftType IS SyntaxTree.RangeType THEN
  2946. ASSERT(rightType IS SyntaxTree.RangeType);
  2947. BrneL(falseLabel, left.op, right.op); (* first *)
  2948. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2949. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2950. ReleaseOperand(left); ReleaseOperand(right);
  2951. BrL(trueLabel)
  2952. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2953. BrneL(falseLabel, left.op, right.op); (* first *)
  2954. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2955. ReleaseOperand(left); ReleaseOperand(right);
  2956. BrL(trueLabel)
  2957. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2958. (* TODO: review this *)
  2959. BrneL(falseLabel, left.op, right.op); (* real part *)
  2960. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2961. ReleaseOperand(left); ReleaseOperand(right);
  2962. BrL(trueLabel)
  2963. ELSE
  2964. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2965. ReleaseOperand(left); ReleaseOperand(right);
  2966. BrL(trueLabel);
  2967. END;
  2968. END;
  2969. |Scanner.LessEqual:
  2970. IF ~conditional THEN ConditionToValue(x);
  2971. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2972. CompareString(BrgeL,x.right,x.left);
  2973. ELSE
  2974. Evaluate(x.left,left);
  2975. Evaluate(x.right,right);
  2976. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2977. Reuse1(temp.op,right.op);
  2978. Emit(And(position,temp.op,left.op,right.op));
  2979. ReleaseOperand(right);
  2980. BreqL(trueLabel,temp.op,left.op);
  2981. BrL(falseLabel);
  2982. ReleaseOperand(temp);ReleaseOperand(left);
  2983. ELSE
  2984. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  2985. ReleaseOperand(left); ReleaseOperand(right);
  2986. BrL(trueLabel);
  2987. END;
  2988. END;
  2989. |Scanner.Less:
  2990. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  2991. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  2992. leftExpression.SetType(system.booleanType);
  2993. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  2994. rightExpression.SetType(system.booleanType);
  2995. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  2996. leftExpression.SetType(system.booleanType);
  2997. Expression(leftExpression);
  2998. ELSIF ~conditional THEN ConditionToValue(x);
  2999. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3000. CompareString(BrltL,x.left,x.right);
  3001. ELSE
  3002. Evaluate(x.left,left);
  3003. Evaluate(x.right,right);
  3004. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3005. ReleaseOperand(left); ReleaseOperand(right);
  3006. BrL(trueLabel);
  3007. END;
  3008. |Scanner.Greater:
  3009. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  3010. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  3011. leftExpression.SetType(system.booleanType);
  3012. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3013. rightExpression.SetType(system.booleanType);
  3014. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3015. leftExpression.SetType(system.booleanType);
  3016. Expression(leftExpression);
  3017. ELSIF ~conditional THEN ConditionToValue(x);
  3018. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3019. CompareString(BrltL,x.right,x.left);
  3020. ELSE
  3021. Evaluate(x.left,left);
  3022. Evaluate(x.right,right);
  3023. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3024. ReleaseOperand(left); ReleaseOperand(right);
  3025. BrL(trueLabel);
  3026. END;
  3027. |Scanner.GreaterEqual:
  3028. IF ~conditional THEN ConditionToValue(x);
  3029. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3030. CompareString(BrgeL,x.left,x.right);
  3031. ELSE
  3032. Evaluate(x.left,left);
  3033. Evaluate(x.right,right);
  3034. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3035. Reuse1(temp.op,left.op);
  3036. Emit(And(position,temp.op,left.op,right.op));
  3037. ReleaseOperand(left);
  3038. BreqL(trueLabel, temp.op,right.op);
  3039. ReleaseOperand(temp); ReleaseOperand(right);
  3040. BrL(falseLabel);
  3041. ELSE
  3042. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3043. ReleaseOperand(left); ReleaseOperand(right);
  3044. BrL(trueLabel);
  3045. END;
  3046. END;
  3047. |Scanner.Unequal:
  3048. IF ~conditional THEN ConditionToValue(x);
  3049. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3050. CompareString(BrneL,x.left,x.right);
  3051. ELSE
  3052. Evaluate(x.left,left);
  3053. Evaluate(x.right,right);
  3054. IF leftType IS SyntaxTree.RangeType THEN
  3055. ASSERT(rightType IS SyntaxTree.RangeType);
  3056. BrneL(trueLabel, left.op, right.op); (* first *)
  3057. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3058. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3059. ReleaseOperand(left); ReleaseOperand(right);
  3060. BrL(falseLabel)
  3061. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3062. BrneL(trueLabel, left.op, right.op); (* first *)
  3063. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3064. ReleaseOperand(left); ReleaseOperand(right);
  3065. BrL(falseLabel)
  3066. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3067. (* TODO: review this *)
  3068. BrneL(trueLabel, left.op, right.op); (* real part *)
  3069. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3070. ReleaseOperand(left); ReleaseOperand(right);
  3071. BrL(falseLabel)
  3072. ELSE
  3073. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3074. ReleaseOperand(left); ReleaseOperand(right);
  3075. BrL(trueLabel);
  3076. END;
  3077. END;
  3078. |Scanner.In:
  3079. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3080. Evaluate(x.left,left);
  3081. Evaluate(x.right,right);
  3082. Convert(left.op,setType);
  3083. ReuseCopy(temp.op,right.op);
  3084. Emit(Shr(position,temp.op,temp.op,left.op));
  3085. ReleaseOperand(right); ReleaseOperand(left);
  3086. IntermediateCode.InitImmediate(one,setType,1);
  3087. Emit(And(position,temp.op,temp.op,one));
  3088. IF conditional THEN
  3089. IntermediateCode.InitImmediate(zero,setType,0);
  3090. BrneL(trueLabel,temp.op,zero);
  3091. ReleaseOperand(temp);
  3092. BrL(falseLabel);
  3093. ELSE
  3094. Convert(temp.op,bool);
  3095. result.mode := ModeValue;
  3096. result.op := temp.op;
  3097. result.tag := nil; (* may be left over from calls to evaluate *)
  3098. END;
  3099. ELSE
  3100. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3101. IF x.operator = Scanner.Questionmarks THEN
  3102. leftExpression := x.left;
  3103. rightExpression := x.right;
  3104. ELSE
  3105. leftExpression := x.right;
  3106. rightExpression := x.left;
  3107. END;
  3108. Evaluate(leftExpression, left);
  3109. Emit(Push(position,left.op));
  3110. ReleaseOperand(left);
  3111. Designate(rightExpression, right);
  3112. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3113. IF ~backend.cellsAreObjects THEN
  3114. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3115. END;
  3116. Emit(Push(position,right.op));
  3117. ReleaseOperand(right);
  3118. IF backend.cellsAreObjects THEN
  3119. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3120. ELSE
  3121. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3122. END;
  3123. InitOperand(result, ModeValue);
  3124. result.op := NewRegisterOperand(bool);
  3125. Emit(Result(position,result.op));
  3126. IF conditional THEN
  3127. IntermediateCode.InitImmediate(zero,setType,0);
  3128. BrneL(trueLabel,result.op,zero);
  3129. ReleaseOperand(result);
  3130. BrL(falseLabel);
  3131. END;
  3132. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3133. leftExpression := x.left;
  3134. rightExpression := x.right;
  3135. Evaluate(leftExpression, left);
  3136. Emit(Push(position,left.op));
  3137. ReleaseOperand(left);
  3138. Evaluate(rightExpression, right);
  3139. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3140. IF ~backend.cellsAreObjects THEN
  3141. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3142. END;
  3143. Emit(Push(position,right.op));
  3144. ReleaseOperand(right);
  3145. IF backend.cellsAreObjects THEN
  3146. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3147. ELSE
  3148. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3149. END;
  3150. InitOperand(result, ModeValue);
  3151. result.op := NewRegisterOperand(bool);
  3152. Emit(Result(position,result.op));
  3153. IF conditional THEN
  3154. IntermediateCode.InitImmediate(zero,setType,0);
  3155. BrneL(trueLabel,result.op,zero);
  3156. ReleaseOperand(result);
  3157. BrL(falseLabel);
  3158. END;
  3159. ELSE
  3160. HALT(100);
  3161. END;
  3162. END;
  3163. destination := dest;
  3164. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3165. END VisitBinaryExpression;
  3166. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3167. VAR localResult, operand: Operand;
  3168. BEGIN
  3169. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3170. InitOperand(localResult, ModeValue);
  3171. ASSERT(x.first # NIL);
  3172. Evaluate(x.first, operand);
  3173. localResult.op := operand.op;
  3174. ReleaseOperand(operand);
  3175. UseIntermediateOperand(localResult.op);
  3176. ASSERT(x.last # NIL);
  3177. Evaluate(x.last, operand);
  3178. localResult.tag := operand.op;
  3179. ReleaseOperand(operand);
  3180. UseIntermediateOperand(localResult.tag);
  3181. IF x.step # NIL THEN
  3182. Evaluate(x.step, operand);
  3183. localResult.extra := operand.op;
  3184. ReleaseOperand(operand);
  3185. UseIntermediateOperand(localResult.extra);
  3186. END;
  3187. result := localResult;
  3188. IF Trace THEN TraceExit("VisitRangeExpression") END
  3189. END VisitRangeExpression;
  3190. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3191. BEGIN
  3192. HALT(100); (* should never be evaluated *)
  3193. END VisitTensorRangeExpression;
  3194. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3195. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3196. BEGIN
  3197. IF Trace THEN TraceEnter("VisitConversion") END;
  3198. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3199. dest := destination; destination := emptyOperand;
  3200. Evaluate(x.expression,old);
  3201. InitOperand(result,ModeValue);
  3202. result.op := old.op;
  3203. ASSERT(result.op.mode # 0);
  3204. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3205. (* convert TO a complex number *)
  3206. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3207. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3208. ASSERT(result.op.mode # 0);
  3209. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3210. (* convert FROM a complex number TO a complex number*)
  3211. result.tag := old.tag;
  3212. ASSERT(result.tag.mode # 0);
  3213. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3214. ASSERT(result.tag.mode # 0)
  3215. ELSE
  3216. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3217. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3218. END
  3219. ELSE
  3220. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3221. ASSERT(result.op.mode # 0);
  3222. result.tag := old.tag; (*! probably never used *)
  3223. END;
  3224. destination := dest;
  3225. IF Trace THEN TraceExit("VisitConversion") END;
  3226. END VisitConversion;
  3227. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3228. BEGIN
  3229. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3230. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3231. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3232. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3233. END VisitTypeDeclaration;
  3234. (** designators (expressions) *)
  3235. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3236. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3237. BEGIN
  3238. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3239. IF x.left # NIL THEN Expression(x.left) END;
  3240. Symbol(x.symbol,result);
  3241. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3242. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3243. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3244. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3245. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3246. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3247. END;
  3248. END;
  3249. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3250. END VisitSymbolDesignator;
  3251. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3252. BEGIN
  3253. IF isUnchecked THEN RETURN END;
  3254. IF tagsAvailable THEN
  3255. TrapC(BrltL,index,length,IndexCheckTrap);
  3256. END;
  3257. END BoundCheck;
  3258. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3259. VAR d: IntermediateCode.Operand;
  3260. BEGIN
  3261. IF isUnchecked THEN RETURN END;
  3262. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3263. TrapC(op,dim,d,ArraySizeTrap);
  3264. ReleaseIntermediateOperand(d);
  3265. END DimensionCheck;
  3266. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3267. VAR
  3268. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3269. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3270. expression: SyntaxTree.Expression;
  3271. resultingType, leftType, baseType: SyntaxTree.Type;
  3272. skipLabel1: Label;
  3273. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3274. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3275. variableOp: Operand;
  3276. variable: SyntaxTree.Variable;
  3277. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3278. VAR expression: SyntaxTree.Expression;
  3279. BEGIN
  3280. (* count the number of indices, ranges and tensorRanges in the index list *)
  3281. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3282. FOR i := 0 TO parameters.Length() - 1 DO
  3283. expression := parameters.GetExpression(i);
  3284. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3285. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3286. ELSE INC(indexCount)
  3287. END
  3288. END;
  3289. END CountIndices;
  3290. BEGIN
  3291. ASSERT(tagsAvailable);
  3292. resultingType := x.type.resolved; (* resulting type *)
  3293. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3294. InitOperand(localResult, ModeReference);
  3295. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3296. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3297. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3298. (* a globally defined array destination tag is available -> use and invalidate it*)
  3299. localResult.tag := arrayDestinationTag;
  3300. IntermediateCode.InitOperand(arrayDestinationTag)
  3301. ELSE
  3302. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3303. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3304. Symbol(variable, variableOp);
  3305. ReuseCopy(localResult.tag, variableOp.op);
  3306. ReleaseOperand(variableOp);
  3307. END
  3308. END;
  3309. indexListSize := x.parameters.Length();
  3310. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3311. ASSERT(tensorRangeCount <= 1);
  3312. (* designate the array to be indexed over, perform tensor range check if known *)
  3313. Designate(x.left, array);
  3314. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3315. Dereference(array, leftType,FALSE);
  3316. IF tensorRangeCount=0 THEN
  3317. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3318. END
  3319. END;
  3320. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3321. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3322. srcDimOffset := -indexListSize;
  3323. destDimOffset := -rangeCount
  3324. ELSE
  3325. srcDimOffset := 0;
  3326. destDimOffset := 0
  3327. END;
  3328. indexDim := 0;
  3329. (* use address of source array as basis *)
  3330. (*
  3331. ReuseCopy(localResult.op, array.op);
  3332. *)
  3333. localResult.op := array.op;
  3334. UseIntermediateOperand(localResult.op);
  3335. (* go through the index list *)
  3336. FOR i := 0 TO indexListSize - 1 DO
  3337. expression := x.parameters.GetExpression(i);
  3338. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3339. (* nothing to do *)
  3340. ELSE
  3341. (* determine which dimension of source array is currently looked at *)
  3342. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3343. (* get the memory operand pointing to array descriptor dimension *)
  3344. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3345. (* make a reusable register from it *)
  3346. ReuseCopy(srcDim, tmp);
  3347. ReleaseIntermediateOperand(tmp);
  3348. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3349. ELSE
  3350. srcDim := IntermediateCode.Immediate(int32, i)
  3351. END;
  3352. (* get length and increment of source array for current dimension *)
  3353. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3354. Convert(sourceLength.op, sizeType);
  3355. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3356. Convert(sourceIncrement.op, sizeType);
  3357. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3358. ReleaseIntermediateOperand(srcDim);
  3359. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3360. (* SINGLE INDEX *)
  3361. Evaluate(expression, index);
  3362. ReleaseIntermediateOperand(index.tag);
  3363. index.tag := emptyOperand;
  3364. Convert(index.op, sizeType);
  3365. (* lower bound check *)
  3366. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3367. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3368. ELSIF isUnchecked THEN (* do nothing *)
  3369. ELSE
  3370. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3371. END;
  3372. (* upper bound check *)
  3373. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3374. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3375. ELSIF isUnchecked THEN (* do nothing *)
  3376. ELSE
  3377. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3378. END;
  3379. ReleaseOperand(sourceLength);
  3380. Convert(index.op, addressType);
  3381. summand := index.op;
  3382. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3383. (* RANGE OF INDICES *)
  3384. Evaluate(expression, range);
  3385. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3386. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3387. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3388. ReleaseOperand(range);
  3389. Convert(firstIndex, sizeType);
  3390. Convert(lastIndex, sizeType);
  3391. Convert(stepSize, sizeType);
  3392. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3393. if it is 'MAX(LONGINT)' *)
  3394. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3395. TransferToRegister(lastIndex, lastIndex);
  3396. skipLabel1 := NewLabel();
  3397. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3398. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3399. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3400. SetLabel(skipLabel1)
  3401. END;
  3402. (* check if step size is valid *)
  3403. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3404. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3405. ELSIF isUnchecked THEN (* do nothing *)
  3406. ELSE
  3407. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3408. END;
  3409. (* check lower bound check *)
  3410. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3411. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3412. ELSIF isUnchecked THEN (* do nothing *)
  3413. ELSE
  3414. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3415. END;
  3416. (* check upper bound check *)
  3417. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3418. (* statically open range: nothing to do *)
  3419. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3420. ASSERT(staticLastIndex < staticSourceLength)
  3421. ELSIF isUnchecked THEN (* do nothing *)
  3422. ELSE
  3423. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3424. END;
  3425. (* determine length of target array for current dimension *)
  3426. (* 1. incorporate last index: *)
  3427. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3428. (* last index is static *)
  3429. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3430. targetLength := sourceLength.op
  3431. ELSE
  3432. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3433. END;
  3434. UseIntermediateOperand(targetLength);
  3435. ELSE
  3436. (* targetLength := lastIndex + 1
  3437. Reuse1(targetLength, lastIndex);
  3438. *)
  3439. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3440. END;
  3441. ReleaseOperand(sourceLength);
  3442. ReleaseIntermediateOperand(lastIndex);
  3443. (* 2. incorporate first index: *)
  3444. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3445. (* first index and current target length are static *)
  3446. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3447. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3448. (* first index = 0: nothing to do *)
  3449. ELSE
  3450. (* targetLength := targetLength - firstIndex *)
  3451. TransferToRegister(targetLength, targetLength);
  3452. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3453. END;
  3454. (* clip negative lengths to 0 *)
  3455. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3456. IF staticTargetLength < 0 THEN
  3457. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3458. END
  3459. ELSE
  3460. skipLabel1 := NewLabel();
  3461. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3462. TransferToRegister(targetLength, targetLength);
  3463. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3464. SetLabel(skipLabel1)
  3465. END;
  3466. (* 3. incorporate index step size: *)
  3467. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3468. (*step size and current target length are static *)
  3469. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3470. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3471. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3472. (* step size = 1: nothing to do *)
  3473. ELSE
  3474. (* emit code for this:
  3475. targetLength := (targetLength-1) DIV stepSize +1;
  3476. *)
  3477. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3478. DivInt(targetLength, targetLength, stepSize);
  3479. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3480. END;
  3481. (* determine increment of target array for current dimension *)
  3482. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3483. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3484. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3485. (* step size = 1 *)
  3486. targetIncrement := sourceIncrement.op;
  3487. UseIntermediateOperand(targetIncrement)
  3488. ELSE
  3489. (* targetIncrement := sourceIncrement * stepSize *)
  3490. Reuse1(targetIncrement, stepSize);
  3491. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3492. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3493. END;
  3494. ReleaseIntermediateOperand(stepSize);
  3495. (* write length and increment of target array to descriptor *)
  3496. IF destDimOffset < 0 THEN
  3497. (* determine which dimension of target array is currently looked at *)
  3498. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3499. TransferToRegister(destDim, tmp);
  3500. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3501. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3502. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3503. ReleaseIntermediateOperand(destDim)
  3504. ELSE
  3505. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3506. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3507. END;
  3508. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3509. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3510. INC(indexDim);
  3511. Convert(firstIndex, addressType);
  3512. TransferToRegister(summand, firstIndex);
  3513. ELSE HALT(100);
  3514. END;
  3515. (*
  3516. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3517. *)
  3518. Convert(sourceIncrement.op, addressType);
  3519. Convert(summand, addressType);
  3520. MulInt(summand, summand, sourceIncrement.op);
  3521. ReleaseIntermediateOperand(sourceIncrement.op);
  3522. AddInt(localResult.op, localResult.op, summand);
  3523. ReleaseIntermediateOperand(summand);
  3524. END
  3525. END;
  3526. result := localResult;
  3527. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3528. ReleaseIntermediateOperand(result.tag);
  3529. result.tag := TypeDescriptorAdr(resultingType);
  3530. IF ~newObjectFile THEN
  3531. IntermediateCode.MakeMemory(result.tag,addressType);
  3532. END;
  3533. ELSIF IsDelegate(resultingType) THEN
  3534. ReleaseIntermediateOperand(result.tag);
  3535. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3536. UseIntermediateOperand(result.tag);
  3537. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3538. ReleaseIntermediateOperand(result.tag);
  3539. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3540. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3541. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3542. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3543. (* finalize target array descriptor *)
  3544. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3545. (* write lengths and increments of target array for remaining dimensions *)
  3546. i := indexListSize;
  3547. WHILE indexDim < targetArrayDimensionality DO
  3548. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3549. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3550. ReleaseOperand(sourceLength);
  3551. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3552. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3553. ReleaseOperand(sourceIncrement);
  3554. INC(i); INC(indexDim);
  3555. END;
  3556. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3557. tmp := nil;
  3558. ELSE
  3559. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3560. END;
  3561. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3562. ReleaseIntermediateOperand(tmp);
  3563. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3564. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3565. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3566. ELSE
  3567. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3568. END;
  3569. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3570. ReleaseIntermediateOperand(tmp);
  3571. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3572. (* write dimensionality *)
  3573. IF targetArrayDimensionality # 0 THEN
  3574. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3575. END;
  3576. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3577. END;
  3578. ReleaseOperand(array);
  3579. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3580. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3581. END;
  3582. END MathIndexDesignator;
  3583. (* TENTATIVE *)
  3584. PROCEDURE DumpOperand(operand: Operand);
  3585. BEGIN
  3586. D.Log.String(" op = ");
  3587. IntermediateCode.DumpOperand(D.Log, operand.op );
  3588. D.Log.Ln;
  3589. D.Log.String(" tag = ");
  3590. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3591. D.Log.Ln;
  3592. D.Log.String(" extra = ");
  3593. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3594. D.Log.Ln;
  3595. D.Log.Update
  3596. END DumpOperand;
  3597. (* get the length of an array , trying to make use of static information *)
  3598. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3599. VAR res: IntermediateCode.Operand; size: LONGINT;
  3600. BEGIN
  3601. type := type.resolved;
  3602. IF type IS SyntaxTree.ArrayType THEN
  3603. WITH type: SyntaxTree.ArrayType DO
  3604. IF type.form = SyntaxTree.Static THEN
  3605. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3606. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3607. Evaluate(type.length, op);
  3608. ReleaseIntermediateOperand(op.tag);
  3609. RETURN op.op;*)
  3610. ELSE
  3611. res := tag;
  3612. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3613. IntermediateCode.MakeMemory(res,addressType);
  3614. UseIntermediateOperand(res);
  3615. RETURN res
  3616. END
  3617. END;
  3618. ELSE
  3619. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3620. RETURN IntermediateCode.Immediate(addressType,size);
  3621. END;
  3622. END ArrayLength;
  3623. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3624. BEGIN
  3625. ReleaseIntermediateOperand(res);
  3626. IF IsImmediate(x) & IsImmediate(y) THEN
  3627. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3628. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3629. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3630. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3631. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3632. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3633. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3634. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3635. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3636. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3637. UseIntermediateOperand(res);
  3638. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3639. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3640. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3641. UseIntermediateOperand(res);
  3642. ELSE
  3643. IF ~ReusableRegister(res) THEN
  3644. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3645. ELSE
  3646. UseIntermediateOperand(res);
  3647. END;
  3648. IF IsImmediate(x) & (x.intValue = 0) THEN
  3649. Emit(Mov(position,res,y))
  3650. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3651. Emit(Mov(position,res,x))
  3652. ELSE
  3653. Emit(Add(position,res, x, y));
  3654. END;
  3655. END;
  3656. END AddInt;
  3657. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3658. BEGIN
  3659. ReleaseIntermediateOperand(res);
  3660. IF IsImmediate(x) & IsImmediate(y) THEN
  3661. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3662. ELSE
  3663. IF ~ReusableRegister(res) THEN
  3664. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3665. ELSE UseIntermediateOperand(res);
  3666. END;
  3667. IF IsImmediate(x) & (x.intValue = 1) THEN
  3668. Emit(Mov(position,res,y))
  3669. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3670. Emit(Mov(position,res,x))
  3671. ELSE
  3672. Emit(Mul(position,res, x, y));
  3673. END;
  3674. END;
  3675. END MulInt;
  3676. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3677. BEGIN
  3678. ReleaseIntermediateOperand(res);
  3679. IF IsImmediate(x) & IsImmediate(y) THEN
  3680. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3681. ELSE
  3682. IF ~ReusableRegister(res) THEN
  3683. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3684. ELSE UseIntermediateOperand(res);
  3685. END;
  3686. IF IsImmediate(x) & (x.intValue = 1) THEN
  3687. Emit(Mov(position,res,y))
  3688. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3689. Emit(Mov(position,res,x))
  3690. ELSE
  3691. Emit(Div(position,res, x, y));
  3692. END;
  3693. END;
  3694. END DivInt;
  3695. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3696. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3697. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3698. BEGIN
  3699. type := x.left.type.resolved;
  3700. IF type IS SyntaxTree.StringType THEN
  3701. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3702. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3703. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3704. type := atype;
  3705. x.left.SetType(type);
  3706. END;
  3707. IntermediateCode.InitImmediate(res,addressType,0);
  3708. maxDim := x.parameters.Length()-1;
  3709. FOR i := 0 TO maxDim DO
  3710. e := x.parameters.GetExpression(i);
  3711. Evaluate(e,index);
  3712. Convert(index.op,addressType);
  3713. AddInt(res, res, index.op);
  3714. IF i = 0 THEN
  3715. (*
  3716. ReuseCopy(res, index.op);
  3717. *)
  3718. Designate(x.left,array);
  3719. type := x.left.type.resolved;
  3720. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3721. Dereference(array, type, FALSE);
  3722. END;
  3723. (*
  3724. ELSE AddInt(res, res, index.op);
  3725. *)
  3726. END;
  3727. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3728. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3729. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3730. BoundCheck(index.op, length);
  3731. END;
  3732. ReleaseIntermediateOperand(length);
  3733. END;
  3734. ReleaseOperand(index);
  3735. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3736. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3737. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3738. MulInt(res,res,length);
  3739. END;
  3740. ReleaseIntermediateOperand(length);
  3741. END;
  3742. IF (type IS SyntaxTree.ArrayType) THEN
  3743. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3744. size := StaticSize(system, type);
  3745. IF size # 1 THEN
  3746. length := IntermediateCode.Immediate(addressType,size);
  3747. MulInt(res,res,length);
  3748. END;
  3749. ELSE
  3750. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3751. IF size # 1 THEN
  3752. length := IntermediateCode.Immediate(addressType,size);
  3753. MulInt(res,res,length);
  3754. END;
  3755. END;
  3756. END;
  3757. AddInt(res,res,array.op);
  3758. InitOperand(result,ModeReference);
  3759. result.op := res;
  3760. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3761. ReleaseIntermediateOperand(result.tag);
  3762. result.tag := TypeDescriptorAdr(type);
  3763. IF ~newObjectFile THEN
  3764. IntermediateCode.MakeMemory(result.tag,addressType);
  3765. END
  3766. ELSIF IsDelegate(type) THEN
  3767. ReleaseIntermediateOperand(result.tag);
  3768. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3769. UseIntermediateOperand(result.tag);
  3770. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3771. ReleaseIntermediateOperand(result.tag);
  3772. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3773. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3774. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3775. END;
  3776. ReleaseOperand(array);
  3777. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3778. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3779. END;
  3780. END IndexDesignator;
  3781. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3782. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3783. BEGIN
  3784. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3785. dest := destination; destination := emptyOperand;
  3786. type := x.left.type.resolved;
  3787. IF type IS SyntaxTree.MathArrayType THEN
  3788. MathIndexDesignator(x);
  3789. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3790. IndexDesignator(x);
  3791. END;
  3792. destination := dest;
  3793. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3794. END VisitIndexDesignator;
  3795. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3796. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3797. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3798. procedure: SyntaxTree.Procedure;
  3799. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3800. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3801. BEGIN
  3802. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3803. parameters := expression.parameters;
  3804. moduleName := "FoxArrayBase";
  3805. procedureName := "CopyDescriptor";
  3806. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3807. SaveRegisters();ReleaseUsedRegisters(saved);
  3808. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3809. IF procedure = NIL THEN
  3810. s := "procedure ";
  3811. Strings.Append(s,moduleName);
  3812. Strings.Append(s,".");
  3813. Strings.Append(s,procedureName);
  3814. Strings.Append(s," not present");
  3815. Error(position,s);
  3816. ELSE
  3817. (* push address of temporary variable *)
  3818. Symbol(variable,destOperand);
  3819. Emit(Push(position,destOperand.op));
  3820. ReleaseOperand(destOperand);
  3821. (* push src *)
  3822. Evaluate(expression.left,srcOperand);
  3823. (*
  3824. Dereference(srcOperand,expression.type.resolved);
  3825. Emit(Push(position,srcOperand.tag));
  3826. *)
  3827. Emit(Push(position,srcOperand.op));
  3828. ReleaseOperand(srcOperand);
  3829. tensorFound := FALSE;
  3830. FOR i := 0 TO parameters.Length()-1 DO
  3831. e := parameters.GetExpression(i);
  3832. IF e IS SyntaxTree.TensorRangeExpression THEN
  3833. tensorFound := TRUE;
  3834. ELSIF e IS SyntaxTree.RangeExpression THEN
  3835. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3836. ELSE
  3837. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3838. END;
  3839. END;
  3840. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3841. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3842. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3843. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3844. StaticCallOperand(procOp,procedure);
  3845. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3846. ReleaseOperand(procOp);
  3847. END;
  3848. RestoreRegisters(saved);
  3849. END;
  3850. RETURN variable
  3851. END PrepareTensorDescriptor;
  3852. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3853. VAR
  3854. type, descriptorType, baseType: SyntaxTree.Type;
  3855. operand, tmpOperand, variableOp, variable2Op: Operand;
  3856. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3857. variable, variable2: SyntaxTree.Variable;
  3858. dim, i, size: LONGINT;
  3859. (* TODO: needed? *)
  3860. oldArrayDestinationTag: IntermediateCode.Operand;
  3861. oldArrayDestinationDimension: LONGINT;
  3862. position: LONGINT;
  3863. saved: RegisterEntry;
  3864. PROCEDURE Pass(op: IntermediateCode.Operand);
  3865. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3866. BEGIN
  3867. IF numberRegister >= 0 THEN
  3868. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3869. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3870. Emit(Mov(position,parameterRegister, op));
  3871. ELSE
  3872. Emit(Push(position,op))
  3873. END
  3874. END Pass;
  3875. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3876. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3877. BEGIN
  3878. formalType := formalType.resolved; actualType := actualType.resolved;
  3879. IF IsOpenArray(formalType)THEN
  3880. IF actualType IS SyntaxTree.StringType THEN
  3881. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3882. RETURN;
  3883. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3884. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3885. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3886. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3887. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3888. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3889. ELSE
  3890. tmp := baseReg;
  3891. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3892. IntermediateCode.MakeMemory(tmp,addressType);
  3893. Pass((tmp));
  3894. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3895. END;
  3896. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3897. END;
  3898. END PushArrayLens;
  3899. BEGIN
  3900. IF Trace THEN TraceEnter("PushParameter") END;
  3901. position := expression.position;
  3902. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3903. type := expression.type.resolved;
  3904. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3905. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3906. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3907. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3908. );
  3909. (* TODO: needed? *)
  3910. oldArrayDestinationTag := arrayDestinationTag;
  3911. oldArrayDestinationDimension := arrayDestinationDimension;
  3912. IF IsArrayOfSystemByte(parameter.type) THEN
  3913. Designate(expression,operand);
  3914. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3915. ReleaseIntermediateOperand(operand.tag);
  3916. operand.tag := tmp;
  3917. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3918. Pass((operand.tag));
  3919. END;
  3920. Pass((operand.op));
  3921. ELSIF IsOpenArray(parameter.type) THEN
  3922. Designate(expression,operand);
  3923. baseReg := operand.tag;
  3924. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3925. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3926. END;
  3927. Pass((operand.op)); (* address of the array *)
  3928. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3929. (* case 1 *)
  3930. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3931. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3932. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3933. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3934. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3935. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3936. arrayDestinationTag := sp;
  3937. (* case 1b *)
  3938. IF expression IS SyntaxTree.IndexDesignator THEN
  3939. (*
  3940. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3941. descriptorType := GetMathArrayDescriptorType(dim);
  3942. variable := GetTemporaryVariable(descriptorType,expression.position);
  3943. Symbol(variable,variableOp);
  3944. arrayDestinationTag := variableOp.op;
  3945. *)
  3946. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3947. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3948. arrayDestinationDimension := dim;
  3949. Designate(expression,operand);
  3950. (* case 1a *)
  3951. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3952. Designate(expression,operand);
  3953. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3954. i := 0;
  3955. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3956. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3957. INC(i);
  3958. END;
  3959. type := expression.type.resolved;
  3960. WHILE (i<dim) DO (* remaining static dimensions *)
  3961. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3962. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3963. ReleaseOperand(tmpOperand);
  3964. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3965. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3966. ReleaseOperand(tmpOperand);
  3967. INC(i);
  3968. END;
  3969. dimOp := IntermediateCode.Immediate(addressType,dim);
  3970. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3971. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3972. (* case 1d *)
  3973. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3974. Designate(expression,operand);
  3975. Dereference(operand,type.resolved,FALSE);
  3976. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3977. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3978. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3979. (* case 1f *)
  3980. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3981. Designate(expression,operand);
  3982. FOR i := 0 TO dim-1 DO
  3983. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3984. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3985. ReleaseOperand(tmpOperand);
  3986. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3987. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3988. ReleaseOperand(tmpOperand);
  3989. END;
  3990. dimOp := IntermediateCode.Immediate(addressType,dim);
  3991. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3992. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  3993. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  3994. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  3995. baseType := SemanticChecker.ArrayBase(type,dim);
  3996. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3997. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  3998. ELSE HALT(100);
  3999. END;
  4000. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4001. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  4002. (* case 2b *)
  4003. IF expression IS SyntaxTree.IndexDesignator THEN
  4004. descriptorType := GetMathArrayDescriptorType(dim);
  4005. variable := GetTemporaryVariable(descriptorType, FALSE);
  4006. Symbol(variable,variableOp);
  4007. arrayDestinationTag := variableOp.op;
  4008. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4009. arrayDestinationDimension := dim;
  4010. NeedDescriptor := TRUE;
  4011. Designate(expression,operand);
  4012. Pass((operand.tag));
  4013. NeedDescriptor := FALSE;
  4014. (* case 2a *)
  4015. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4016. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4017. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4018. INC(i);
  4019. END;
  4020. IF i = dim THEN
  4021. Designate(expression,operand);
  4022. Pass((operand.tag));
  4023. ELSE (* open-static *)
  4024. type := expression.type.resolved;
  4025. descriptorType := GetMathArrayDescriptorType(dim);
  4026. variable := GetTemporaryVariable(descriptorType, FALSE);
  4027. Symbol(variable,variableOp);
  4028. arrayDestinationTag := variableOp.op;
  4029. Designate(expression,operand);
  4030. FOR i := 0 TO dim-1 DO
  4031. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4032. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4033. ReleaseOperand(tmpOperand);
  4034. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4035. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4036. ReleaseOperand(tmpOperand);
  4037. END;
  4038. dimOp := IntermediateCode.Immediate(addressType,dim);
  4039. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4040. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4041. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4042. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4043. baseType := SemanticChecker.ArrayBase(type,dim);
  4044. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4045. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4046. Pass((arrayDestinationTag));
  4047. END;
  4048. (* case 2d *)
  4049. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4050. Designate(expression,operand);
  4051. Dereference(operand,type.resolved,FALSE);
  4052. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4053. Pass((operand.tag));
  4054. (* case 2f *)
  4055. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4056. descriptorType := GetMathArrayDescriptorType(dim);
  4057. variable := GetTemporaryVariable(descriptorType, FALSE);
  4058. Symbol(variable,variableOp);
  4059. arrayDestinationTag := variableOp.op;
  4060. Designate(expression,operand);
  4061. FOR i := 0 TO dim-1 DO
  4062. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4063. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4064. ReleaseOperand(tmpOperand);
  4065. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4066. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4067. ReleaseOperand(tmpOperand);
  4068. END;
  4069. dimOp := IntermediateCode.Immediate(addressType,dim);
  4070. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4071. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4072. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4073. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4074. baseType := SemanticChecker.ArrayBase(type,dim);
  4075. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4076. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4077. Pass((arrayDestinationTag));
  4078. ELSE HALT(100);
  4079. END;
  4080. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4081. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4082. (* case 3b *)
  4083. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4084. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4085. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4086. Symbol(variable,variableOp);
  4087. LoadValue(variableOp,system.addressType);
  4088. ELSE
  4089. descriptorType := GetMathArrayDescriptorType(dim);
  4090. variable := GetTemporaryVariable(descriptorType, FALSE);
  4091. Symbol(variable,variableOp);
  4092. END;
  4093. arrayDestinationTag := variableOp.op;
  4094. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4095. arrayDestinationDimension := 0;
  4096. Designate(expression,operand);
  4097. Pass((operand.tag));
  4098. (* case 3a *)
  4099. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4100. i := 0;
  4101. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4102. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4103. INC(i);
  4104. END;
  4105. IF i = dim THEN
  4106. Designate(expression,operand);
  4107. Pass((operand.tag));
  4108. ELSE (* open-static *)
  4109. type := expression.type.resolved;
  4110. descriptorType := GetMathArrayDescriptorType(dim);
  4111. variable := GetTemporaryVariable(descriptorType, FALSE);
  4112. Symbol(variable,variableOp);
  4113. arrayDestinationTag := variableOp.op;
  4114. Designate(expression,operand);
  4115. FOR i := 0 TO dim-1 DO
  4116. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4117. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4118. ReleaseOperand(tmpOperand);
  4119. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4120. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4121. ReleaseOperand(tmpOperand);
  4122. END;
  4123. dimOp := IntermediateCode.Immediate(addressType,dim);
  4124. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4125. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4126. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4127. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4128. baseType := SemanticChecker.ArrayBase(type,dim);
  4129. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4130. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4131. Pass((arrayDestinationTag));
  4132. END;
  4133. (* case 3d *)
  4134. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4135. Designate(expression,operand);
  4136. Dereference(operand,type.resolved,FALSE);
  4137. (*
  4138. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4139. *)
  4140. Pass((operand.tag));
  4141. (* case 3f *)
  4142. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4143. descriptorType := GetMathArrayDescriptorType(dim);
  4144. variable := GetTemporaryVariable(descriptorType, FALSE);
  4145. Symbol(variable,variableOp);
  4146. arrayDestinationTag := variableOp.op;
  4147. Designate(expression,operand);
  4148. IF operand.op.type.length >1 THEN (* vector register *)
  4149. variable2 := GetTemporaryVariable(type, FALSE);
  4150. Symbol(variable2, variable2Op);
  4151. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4152. Emit(Mov(position,tmp, operand.op));
  4153. ReleaseOperand(operand);
  4154. Symbol(variable2, operand);
  4155. END;
  4156. FOR i := 0 TO dim-1 DO
  4157. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4158. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4159. ReleaseOperand(tmpOperand);
  4160. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4161. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4162. ReleaseOperand(tmpOperand);
  4163. END;
  4164. dimOp := IntermediateCode.Immediate(addressType,dim);
  4165. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4166. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4167. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4168. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4169. baseType := SemanticChecker.ArrayBase(type,dim);
  4170. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4171. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4172. Pass((arrayDestinationTag));
  4173. ELSE HALT(100);
  4174. END;
  4175. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4176. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4177. (* case 4b *)
  4178. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4179. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4180. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4181. Symbol(variable,variableOp);
  4182. LoadValue(variableOp,system.addressType);
  4183. ELSE
  4184. descriptorType := GetMathArrayDescriptorType(dim);
  4185. variable := GetTemporaryVariable(descriptorType, FALSE);
  4186. Symbol(variable,variableOp);
  4187. END;
  4188. arrayDestinationTag := variableOp.op;
  4189. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4190. arrayDestinationDimension := 0;
  4191. Designate(expression,operand);
  4192. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4193. Symbol(variable,variableOp);
  4194. ELSE
  4195. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4196. Symbol(variable,variableOp);
  4197. MakeMemory(tmp,variableOp.op,addressType,0);
  4198. Emit(Mov(position,tmp,operand.tag));
  4199. ReleaseIntermediateOperand(tmp);
  4200. END;
  4201. Pass((variableOp.op));
  4202. ReleaseOperand(variableOp);
  4203. (* case 4a *)
  4204. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4205. i := 0;
  4206. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4207. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4208. INC(i);
  4209. END;
  4210. IF i = dim THEN
  4211. Designate(expression,operand);
  4212. arrayDestinationTag := operand.tag;
  4213. ELSE (* open-static *)
  4214. type := expression.type.resolved;
  4215. descriptorType := GetMathArrayDescriptorType(dim);
  4216. variable := GetTemporaryVariable(descriptorType, FALSE);
  4217. Symbol(variable,variableOp);
  4218. arrayDestinationTag := variableOp.op;
  4219. Designate(expression,operand);
  4220. FOR i := 0 TO dim-1 DO
  4221. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4222. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4223. ReleaseOperand(tmpOperand);
  4224. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4225. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4226. ReleaseOperand(tmpOperand);
  4227. END;
  4228. dimOp := IntermediateCode.Immediate(addressType,dim);
  4229. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4230. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4231. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4232. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4233. baseType := SemanticChecker.ArrayBase(type,dim);
  4234. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4235. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4236. END;
  4237. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4238. Symbol(variable,variableOp);
  4239. MakeMemory(tmp,variableOp.op,addressType,0);
  4240. Emit(Mov(position,tmp,arrayDestinationTag));
  4241. ReleaseIntermediateOperand(tmp);
  4242. Pass((variableOp.op));
  4243. ReleaseOperand(variableOp);
  4244. (* case 4d *)
  4245. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4246. Designate(expression,operand);
  4247. (*
  4248. Dereference(operand,type.resolved,FALSE);
  4249. *)
  4250. (*
  4251. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4252. *)
  4253. Pass((operand.op));
  4254. (* case 4f *)
  4255. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4256. descriptorType := GetMathArrayDescriptorType(dim);
  4257. variable := GetTemporaryVariable(descriptorType, FALSE);
  4258. Symbol(variable,variableOp);
  4259. arrayDestinationTag := variableOp.op;
  4260. Designate(expression,operand);
  4261. FOR i := 0 TO dim-1 DO
  4262. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4263. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4264. ReleaseOperand(tmpOperand);
  4265. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4266. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4267. ReleaseOperand(tmpOperand);
  4268. END;
  4269. dimOp := IntermediateCode.Immediate(addressType,dim);
  4270. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4271. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4272. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4273. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4274. baseType := SemanticChecker.ArrayBase(type,dim);
  4275. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4276. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4277. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4278. Symbol(variable,variableOp);
  4279. MakeMemory(tmp,variableOp.op,addressType,0);
  4280. Emit(Mov(position,tmp,arrayDestinationTag));
  4281. ReleaseIntermediateOperand(tmp);
  4282. Pass((variableOp.op));
  4283. ReleaseOperand(variableOp);
  4284. ELSE HALT(100);
  4285. END;
  4286. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4287. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4288. Designate(expression,operand);
  4289. IF operand.op.type.length > 1 THEN
  4290. Emit(Push(position, operand.op));
  4291. ReleaseOperand(operand);
  4292. ELSE
  4293. size := system.SizeOf(type);
  4294. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4295. size := ToMemoryUnits(system,size);
  4296. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4297. arrayDestinationTag := sp;
  4298. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4299. END;
  4300. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4301. Designate(expression,operand);
  4302. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4303. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4304. Symbol(variable,variableOp);
  4305. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4306. Emit(Mov(position,tmp,operand.op));
  4307. Emit(Push(position,variableOp.op));
  4308. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4309. Pass((operand.op));
  4310. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4311. END;
  4312. ELSE HALT(200)
  4313. END;
  4314. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4315. IF parameter.kind = SyntaxTree.VarParameter THEN
  4316. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4317. Designate(expression, operand);
  4318. Pass((operand.op));
  4319. ELSE
  4320. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4321. Evaluate(expression, operand);
  4322. Pass((operand.extra)); (* step *)
  4323. Pass((operand.tag)); (* last *)
  4324. Pass((operand.op)) (* first *)
  4325. END
  4326. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4327. IF parameter.kind = SyntaxTree.VarParameter THEN
  4328. Designate(expression, operand);
  4329. Pass((operand.op));
  4330. ELSE
  4331. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4332. Evaluate(expression, operand);
  4333. Pass((operand.tag)); (* real part *)
  4334. Pass((operand.op)) (* imaginary part *)
  4335. END
  4336. ELSE
  4337. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4338. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4339. Designate(expression,operand);
  4340. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4341. (* stack allocation *)
  4342. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4343. (*! parameter alignment to be discussed ... *)
  4344. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4345. size := type(SyntaxTree.StringType).length;
  4346. END;
  4347. IF backend.cooperative & parameter.NeedsTrace() THEN
  4348. dst := NewRegisterOperand (addressType);
  4349. Emit(Mov(position,dst, sp));
  4350. IntermediateCode.InitImmediate(null, byteType, 0);
  4351. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4352. ReleaseIntermediateOperand(dst);
  4353. SaveRegisters();ReleaseUsedRegisters(saved);
  4354. (* register dst has been freed before SaveRegisters already *)
  4355. CallAssignMethod(dst, operand.op, parameter.type);
  4356. RestoreRegisters(saved);
  4357. END;
  4358. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4359. ELSIF IsOpenArray(parameter.type) THEN
  4360. Designate(expression,operand);
  4361. baseReg := operand.tag;
  4362. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4363. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4364. END;
  4365. Pass((operand.op)); (* address of the array *)
  4366. ELSIF IsDelegate(parameter.type) THEN
  4367. Evaluate(expression,operand);
  4368. IF backend.cooperative & parameter.NeedsTrace() THEN
  4369. Emit(Push(position, nil));
  4370. dst := NewRegisterOperand (addressType);
  4371. Emit(Mov(position,dst, sp));
  4372. ReleaseIntermediateOperand(dst);
  4373. SaveRegisters();ReleaseUsedRegisters(saved);
  4374. Emit(Push(position, dst));
  4375. (* register dst has been freed before SaveRegisters already *)
  4376. Emit(Push(position, operand.tag));
  4377. CallThis(position,"GarbageCollector","Assign",2);
  4378. RestoreRegisters(saved);
  4379. ELSE
  4380. Pass((operand.tag));
  4381. END;
  4382. Pass((operand.op));
  4383. ELSE
  4384. Evaluate(expression,operand);
  4385. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4386. Emit(Push(position, nil));
  4387. dst := NewRegisterOperand (addressType);
  4388. Emit(Mov(position,dst, sp));
  4389. ReleaseIntermediateOperand(dst);
  4390. SaveRegisters();ReleaseUsedRegisters(saved);
  4391. Emit(Push(position, dst));
  4392. (* register dst has been freed before SaveRegisters already *)
  4393. Emit(Push(position, operand.op));
  4394. CallThis(position,"GarbageCollector","Assign",2);
  4395. RestoreRegisters(saved);
  4396. ELSE
  4397. Pass((operand.op));
  4398. END;
  4399. END;
  4400. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4401. Evaluate(expression,operand);
  4402. Pass((operand.op));
  4403. ELSE (* var parameter *)
  4404. Designate(expression,operand);
  4405. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4406. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4407. Pass((operand.tag));
  4408. END;
  4409. END;
  4410. Pass((operand.op));
  4411. END;
  4412. END;
  4413. (* TODO: needed? *)
  4414. arrayDestinationTag := oldArrayDestinationTag;
  4415. arrayDestinationDimension := oldArrayDestinationDimension;
  4416. IF needsParameterBackup THEN
  4417. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4418. ReuseCopy(parameterBackup, operand.op)
  4419. END;
  4420. ReleaseOperand(operand);
  4421. IF Trace THEN TraceExit("PushParameter") END;
  4422. END PushParameter;
  4423. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4424. VAR prevConditional: BOOLEAN;
  4425. BEGIN
  4426. prevConditional := conditional;
  4427. conditional := FALSE;
  4428. 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
  4429. Expression(x.result); (* use register *)
  4430. END;
  4431. Statement(x.statement);
  4432. conditional := prevConditional;
  4433. IF x.result # NIL THEN Expression(x.result) END;
  4434. 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
  4435. ReleaseIntermediateOperand(result.op);
  4436. END;
  4437. END VisitStatementDesignator;
  4438. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4439. VAR
  4440. procedure: SyntaxTree.Procedure;
  4441. procedureType: SyntaxTree.ProcedureType;
  4442. wasInline: BOOLEAN;
  4443. actualParameters: SyntaxTree.ExpressionList;
  4444. formalParameter: SyntaxTree.Parameter;
  4445. actualParameter: SyntaxTree.Expression;
  4446. i: LONGINT;
  4447. localVariable: SyntaxTree.Variable;
  4448. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4449. src, dest: Operand;
  4450. prevInlineExit : Label;
  4451. prevMapper: SymbolMapper;
  4452. tooComplex: BOOLEAN;
  4453. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4454. BEGIN
  4455. IF e = NIL THEN RETURN TRUE
  4456. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4457. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4458. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4459. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4460. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4461. ELSE RETURN FALSE
  4462. END;
  4463. END SimpleExpression;
  4464. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4465. BEGIN
  4466. RETURN checker.CanPassInRegister(type)
  4467. END FitsInRegister;
  4468. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4469. VAR
  4470. variable: SyntaxTree.Variable;
  4471. variableDesignator: SyntaxTree.Designator;
  4472. BEGIN
  4473. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4474. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4475. variableDesignator.SetType(type);
  4476. RETURN variableDesignator
  4477. END GetTemp;
  4478. BEGIN
  4479. wasInline := currentIsInline;
  4480. prevInlineExit := currentInlineExit;
  4481. prevMapper := currentMapper;
  4482. currentInlineExit := NewLabel();
  4483. tooComplex := FALSE;
  4484. NEW(currentMapper);
  4485. currentIsInline := TRUE;
  4486. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4487. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4488. formalParameter := procedureType.firstParameter;
  4489. actualParameters := x.parameters;
  4490. i := 0;
  4491. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4492. actualParameter := actualParameters.GetExpression(i);
  4493. IF actualParameter.resolved # NIL THEN
  4494. actualParameter := actualParameter.resolved
  4495. END;
  4496. (*
  4497. if expression is simple and can be passed immediately
  4498. or if type fits in register then we can proceed
  4499. otherwise we escape to ordinary procedure call.
  4500. *)
  4501. (* cases where the expression can be mapped identically *)
  4502. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4503. currentMapper.Add(formalParameter, actualParameter, NIL);
  4504. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4505. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4506. (*
  4507. Assign(variableDesignator, actualParameter);
  4508. *)
  4509. Evaluate(actualParameter, src);
  4510. Designate(variableDesignator, dest);
  4511. Emit(Mov(x.position, dest.op, src.op));
  4512. ReleaseOperand(dest);
  4513. ReleaseOperand(src);
  4514. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4515. ELSE tooComplex := TRUE
  4516. END;
  4517. (*
  4518. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4519. currentMapper.Add(formalParameter, actualParameter, NIL);
  4520. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4521. variableDesignator := GetTemp(system.addressType, FALSE);
  4522. Designate(actualParameter, src);
  4523. Designate(variableDesignator, dest);
  4524. IntermediateCode.MakeMemory(dest.op,addressType);
  4525. Emit(Mov(x.position, dest.op, src.op));
  4526. ReleaseOperand(dest);
  4527. IF src.tag.mode # IntermediateCode.Undefined THEN
  4528. tagDesignator := GetTemp(system.addressType, FALSE);
  4529. Designate(tagDesignator, dest);
  4530. IntermediateCode.MakeMemory(dest.op,addressType);
  4531. Emit(Mov(x.position, dest.op, src.op));
  4532. END;
  4533. ReleaseOperand(dest); ReleaseOperand(src);
  4534. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4535. END;
  4536. *)
  4537. formalParameter := formalParameter.nextParameter;
  4538. INC(i);
  4539. END;
  4540. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4541. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4542. currentMapper.Add(NIL, returnDesignator, NIL);
  4543. END;
  4544. localVariable := procedure.procedureScope.firstVariable;
  4545. WHILE ~tooComplex & (localVariable # NIL) DO
  4546. variableDesignator := GetTemp(localVariable.type, FALSE);
  4547. currentMapper.Add(localVariable, variableDesignator, NIL);
  4548. localVariable := localVariable.nextVariable;
  4549. END;
  4550. IF ~tooComplex THEN
  4551. VisitStatementBlock(procedure.procedureScope.body);
  4552. SetLabel(currentInlineExit);
  4553. IF procedureType.returnType # NIL THEN
  4554. Designate(returnDesignator, result);
  4555. IF conditional THEN
  4556. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4557. ValueToCondition(result)
  4558. END;
  4559. END;
  4560. END;
  4561. currentMapper := prevMapper;
  4562. currentInlineExit := prevInlineExit;
  4563. currentIsInline := wasInline;
  4564. RETURN ~tooComplex
  4565. END InlineProcedureCall;
  4566. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4567. VAR
  4568. parameters: SyntaxTree.ExpressionList;
  4569. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4570. designator: SyntaxTree.Designator;
  4571. procedureType: SyntaxTree.ProcedureType;
  4572. formalParameter: SyntaxTree.Parameter;
  4573. operand, returnValue: Operand;
  4574. reg, size, mask, dest: IntermediateCode.Operand;
  4575. saved: RegisterEntry;
  4576. symbol: SyntaxTree.Symbol;
  4577. variable: SyntaxTree.Variable;
  4578. i, parametersSize, returnTypeSize : LONGINT;
  4579. structuredReturnType: BOOLEAN;
  4580. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4581. tempVariableDesignator: SyntaxTree.Designator;
  4582. gap, alignment: LONGINT; (*fld*)
  4583. (* TODO: remove unnecessary backup variables *)
  4584. oldResult: Operand;
  4585. oldCurrentScope: SyntaxTree.Scope;
  4586. oldArrayDestinationTag: IntermediateCode.Operand;
  4587. oldArrayDestinationDimension: LONGINT;
  4588. oldConstantDeclaration: SyntaxTree.Symbol;
  4589. oldDestination: IntermediateCode.Operand;
  4590. oldCurrentLoop: Label;
  4591. oldConditional: BOOLEAN;
  4592. oldTrueLabel, oldFalseLabel: Label;
  4593. oldLocked: BOOLEAN;
  4594. usedRegisters,oldUsedRegisters: RegisterEntry;
  4595. return: IntermediateCode.Operand;
  4596. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4597. arg: IntermediateCode.Operand;
  4598. dummy: IntermediateCode.Operand;
  4599. recordType: SyntaxTree.RecordType;
  4600. operatorSelectionProcedureOperand: Operand;
  4601. operatorSelectionProcedure: SyntaxTree.Procedure;
  4602. fingerPrint: SyntaxTree.FingerPrint;
  4603. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4604. identifierNumber: LONGINT;
  4605. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4606. procedure: SyntaxTree.Procedure;
  4607. PROCEDURE BackupGlobalState;
  4608. BEGIN
  4609. oldResult := result;
  4610. oldCurrentScope := currentScope;
  4611. oldArrayDestinationTag := arrayDestinationTag;
  4612. oldArrayDestinationDimension := arrayDestinationDimension;
  4613. oldConstantDeclaration := constantDeclaration;
  4614. oldDestination := destination;
  4615. oldCurrentLoop := currentLoop;
  4616. oldConditional := conditional;
  4617. oldTrueLabel := trueLabel;
  4618. oldFalseLabel := falseLabel;
  4619. oldLocked := locked;
  4620. oldUsedRegisters := usedRegisters
  4621. END BackupGlobalState;
  4622. PROCEDURE RestoreGlobalState;
  4623. BEGIN
  4624. result := oldResult;
  4625. currentScope := oldCurrentScope;
  4626. arrayDestinationTag := oldArrayDestinationTag;
  4627. arrayDestinationDimension := oldArrayDestinationDimension;
  4628. constantDeclaration := oldConstantDeclaration;
  4629. destination := oldDestination;
  4630. currentLoop := oldCurrentLoop;
  4631. conditional := oldConditional;
  4632. trueLabel := oldTrueLabel;
  4633. falseLabel := oldFalseLabel;
  4634. locked := oldLocked;
  4635. usedRegisters := oldUsedRegisters
  4636. END RestoreGlobalState;
  4637. (** do preparations before parameter push for array-structured object types (ASOTs):
  4638. if ASOT is passed as VAR parameter:
  4639. - allocate temporary variable of math array type
  4640. - copy contents of ASOT to be passed to temporary variable
  4641. - use temporary variable as the actual parameter instead
  4642. - 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)
  4643. **)
  4644. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4645. VAR
  4646. expression: SyntaxTree.Expression;
  4647. BEGIN
  4648. IF actualParameter IS SyntaxTree.Designator THEN
  4649. designator := actualParameter(SyntaxTree.Designator);
  4650. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4651. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4652. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4653. ASSERT(checker # NIL);
  4654. checker.SetCurrentScope(currentScope);
  4655. (* allocate temporary variable *)
  4656. ASSERT(actualParameter.type # NIL);
  4657. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4658. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4659. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4660. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4661. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4662. ASSERT(tempVariableDesignator.type # NIL);
  4663. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4664. (* copy math array stored in actual parameter to temporary variable *)
  4665. BackupGlobalState;
  4666. AssignMathArray(tempVariableDesignator, actualParameter);
  4667. RestoreGlobalState;
  4668. (* use temporary variable as actual parameter instead of the original one *)
  4669. actualParameter := tempVariableDesignator;
  4670. (* create write-back call and store it in linked list *)
  4671. (* create new list entry *)
  4672. IF firstWriteBackCall = NIL THEN
  4673. NEW(firstWriteBackCall);
  4674. currentWriteBackCall := firstWriteBackCall
  4675. ELSE
  4676. ASSERT(currentWriteBackCall # NIL);
  4677. NEW(currentWriteBackCall.next);
  4678. currentWriteBackCall := currentWriteBackCall.next
  4679. END;
  4680. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4681. ASSERT(expression.type = NIL);
  4682. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4683. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4684. (* prepare writeback for any other "normal" indexer *)
  4685. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4686. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4687. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4688. Assign(tempVariableDesignator, actualParameter);
  4689. actualParameter := tempVariableDesignator;
  4690. IF firstWriteBackCall = NIL THEN
  4691. NEW(firstWriteBackCall);
  4692. currentWriteBackCall := firstWriteBackCall
  4693. ELSE
  4694. ASSERT(currentWriteBackCall # NIL);
  4695. NEW(currentWriteBackCall.next);
  4696. currentWriteBackCall := currentWriteBackCall.next
  4697. END;
  4698. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4699. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4700. END
  4701. END
  4702. END PrepareParameter;
  4703. BEGIN
  4704. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4705. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4706. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4707. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4708. IF InlineProcedureCall(x) THEN
  4709. RETURN
  4710. ELSE
  4711. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4712. END
  4713. END;
  4714. END;
  4715. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4716. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4717. dest := destination; destination := emptyOperand;
  4718. SaveRegisters();ReleaseUsedRegisters(saved);
  4719. parameters := x.parameters;
  4720. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4721. (* an operator is called *)
  4722. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4723. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4724. (* check if a dynamic operator call should be performed *)
  4725. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4726. ELSE
  4727. isCallOfDynamicOperator := FALSE
  4728. END;
  4729. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4730. Emit(Push(position, ap));
  4731. END;
  4732. alignment := procedureType.stackAlignment;
  4733. IF alignment > 1 THEN
  4734. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4735. Emit(Mov(position,reg, sp));
  4736. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4737. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4738. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4739. Emit(And(position,sp, sp, mask));
  4740. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4741. Emit(Push(position,reg));
  4742. (*
  4743. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4744. Emit(Mov(position,mem,reg));
  4745. *)
  4746. ReleaseIntermediateOperand(reg);
  4747. END;
  4748. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4749. IntermediateCode.InitImmediate(mask,addressType,-16);
  4750. Emit(And(position,sp, sp, mask));
  4751. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4752. IF gap # 0 THEN
  4753. IntermediateCode.InitImmediate(size,addressType,gap);
  4754. Emit(Sub(position,sp,sp,size))
  4755. END;
  4756. END;
  4757. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4758. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4759. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4760. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4761. ELSE
  4762. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4763. END;
  4764. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4765. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4766. (* push ID *)
  4767. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4768. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4769. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4770. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4771. formalParameter := procedureType.firstParameter;
  4772. FOR i := 0 TO parameters.Length() - 1 DO
  4773. IF formalParameter.access # SyntaxTree.Hidden THEN
  4774. ASSERT(i < 2);
  4775. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4776. (* push pointer *)
  4777. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4778. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4779. (* add dereference *)
  4780. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4781. ReleaseIntermediateOperand(parameterBackups[i]);
  4782. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4783. END;
  4784. Emit(Push(position,parameterBackups[i]));
  4785. ReleaseIntermediateOperand(parameterBackups[i]);
  4786. (* push typetag *)
  4787. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4788. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4789. arg := TypeDescriptorAdr(recordType);
  4790. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4791. Emit(Push(position,arg));
  4792. ELSE
  4793. (* push 'NonPointer' *)
  4794. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4795. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4796. (* push fingerprint *)
  4797. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4798. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4799. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4800. END
  4801. END;
  4802. formalParameter := formalParameter.nextParameter
  4803. END;
  4804. (* for unary operators: complete the information for the second parameter *)
  4805. IF procedureType.numberParameters < 2 THEN
  4806. (* push 'NonPointer' *)
  4807. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4808. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4809. (* push 'NoType' *)
  4810. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4811. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4812. END;
  4813. (* call operator selection procedure *)
  4814. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4815. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4816. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4817. ReleaseOperand(operatorSelectionProcedureOperand);
  4818. (* use the address that the operator selection procedure returned as the target address of the call *)
  4819. InitOperand(operand, ModeValue);
  4820. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4821. Emit(Result(position,operand.op))
  4822. END
  4823. ELSE
  4824. Evaluate(x.left, operand)
  4825. END;
  4826. IF symbol IS SyntaxTree.Procedure THEN
  4827. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4828. Emit(Push(position,operand.tag));
  4829. ELSIF (procedureType.isDelegate) THEN
  4830. Emit(Push(position,operand.tag));
  4831. END;
  4832. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4833. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4834. Emit(Push(position,operand.tag));
  4835. END;
  4836. ELSE HALT(200);
  4837. END;
  4838. ReleaseIntermediateOperand(operand.tag);
  4839. operand.tag := emptyOperand;
  4840. (* determine if a structured return type is needed *)
  4841. structuredReturnType := StructuredReturnType(procedureType);
  4842. IF structuredReturnType THEN
  4843. IF resultDesignator # NIL THEN
  4844. d := resultDesignator;
  4845. ELSE
  4846. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4847. variable.SetUntraced(procedureType.hasUntracedReturn);
  4848. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4849. d.SetType(variable.type);
  4850. END;
  4851. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4852. Designate(d,returnValue);
  4853. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4854. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4855. Emit(Push(position,size));
  4856. Emit(Push(position,returnValue.op));
  4857. ReleaseOperand(returnValue);
  4858. ELSE
  4859. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4860. END;
  4861. END;
  4862. firstWriteBackCall := NIL; (* reset write-back call list *)
  4863. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4864. passByRegister := system.registerParameters > 0;
  4865. registerNumber := 0;
  4866. formalParameter := procedureType.lastParameter;
  4867. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4868. actualParameter := parameters.GetExpression(i);
  4869. PrepareParameter(actualParameter, formalParameter);
  4870. IF passByRegister & (i < system.registerParameters) THEN
  4871. IF ~PassInRegister(formalParameter) THEN
  4872. Error(actualParameter.position,"cannot be passed by register")
  4873. ELSE
  4874. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4875. END;
  4876. INC(registerNumber);
  4877. ELSE
  4878. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4879. END;
  4880. formalParameter := formalParameter.prevParameter;
  4881. END;
  4882. IF passByRegister & (registerNumber > 0) THEN
  4883. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4884. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4885. END;
  4886. ELSE
  4887. hasDynamicOperands := FALSE;
  4888. formalParameter := procedureType.firstParameter;
  4889. FOR i := 0 TO parameters.Length() - 1 DO
  4890. actualParameter := parameters.GetExpression(i);
  4891. IF formalParameter # NIL THEN (* TENTATIVE *)
  4892. PrepareParameter(actualParameter, formalParameter);
  4893. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4894. ASSERT(i < 2);
  4895. hasDynamicOperands := TRUE;
  4896. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4897. ELSE
  4898. IF passByRegister & (registerNumber > 0) THEN
  4899. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4900. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4901. END;
  4902. passByRegister := FALSE;
  4903. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4904. END;
  4905. formalParameter := formalParameter.nextParameter;
  4906. END;
  4907. END;
  4908. END;
  4909. IF symbol IS SyntaxTree.Procedure THEN
  4910. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4911. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4912. Emit(Push(position,reg));
  4913. ReleaseIntermediateOperand(reg);
  4914. END;
  4915. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4916. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4917. parametersSize := ParametersSize(system,procedureType,FALSE);
  4918. END;
  4919. ReleaseParameterRegisters();
  4920. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4921. Emit(Call(position,operand.op,0));
  4922. ELSE
  4923. Emit(Call(position,operand.op,parametersSize));
  4924. END;
  4925. ReleaseOperand(operand);
  4926. IF procedureType.noReturn THEN
  4927. EmitTrap(position,NoReturnTrap);
  4928. END;
  4929. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4930. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4931. Emit(Result(position,return));
  4932. END;
  4933. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4934. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4935. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4936. END;
  4937. size := IntermediateCode.Immediate(addressType,parametersSize);
  4938. Emit(Add(position,sp,sp,size));
  4939. END;
  4940. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4941. IF structuredReturnType THEN
  4942. (* stack pointer rewinding done by callee
  4943. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4944. Emit(Add(position,sp,sp,size));
  4945. *)
  4946. RestoreRegisters(saved);
  4947. InitOperand(result,ModeReference);
  4948. Symbol(variable,result);
  4949. ELSE
  4950. RestoreRegisters(saved);
  4951. InitOperand(result,ModeValue);
  4952. result.op := return;
  4953. END;
  4954. END;
  4955. IF alignment > 1 THEN
  4956. Emit(Pop(position,sp));
  4957. END;
  4958. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4959. Emit(Pop(position, ap));
  4960. END;
  4961. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4962. ValueToCondition(result);
  4963. END;
  4964. destination := dest;
  4965. (* perform all write-back calls in the list *)
  4966. BackupGlobalState;
  4967. currentWriteBackCall := firstWriteBackCall;
  4968. WHILE currentWriteBackCall # NIL DO
  4969. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4970. currentWriteBackCall := currentWriteBackCall.next
  4971. END;
  4972. RestoreGlobalState;
  4973. (* TENATIVE *)
  4974. (*
  4975. IF isOperatorCall THEN
  4976. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4977. END;
  4978. *)
  4979. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  4980. END VisitProcedureCallDesignator;
  4981. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  4982. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  4983. td: SyntaxTree.Symbol;
  4984. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  4985. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4986. BEGIN
  4987. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4988. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4989. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4990. variable.SetType(system.anyType);
  4991. scope.AddVariable(variable);
  4992. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  4993. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  4994. typeDeclaration.SetDeclaredType(hiddenPointerType);
  4995. typeDeclaration.SetScope(module.module.moduleScope);
  4996. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  4997. hiddenPointerType.SetState(SyntaxTree.Resolved);
  4998. END;
  4999. RETURN hiddenPointerType;
  5000. END GetHiddenPointerType;
  5001. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  5002. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  5003. BEGIN
  5004. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  5005. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  5006. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  5007. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  5008. scope.AddVariable(variable);
  5009. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  5010. variable.SetType(system.anyType);
  5011. scope.AddVariable(variable);
  5012. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5013. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  5014. typeDeclaration.SetDeclaredType(delegatePointerType);
  5015. typeDeclaration.SetScope(module.module.moduleScope);
  5016. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5017. delegatePointerType.SetState(SyntaxTree.Resolved);
  5018. END;
  5019. RETURN delegatePointerType
  5020. END GetDelegateType;
  5021. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5022. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5023. such as in VAR a: RECORD ... END;
  5024. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5025. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5026. *)
  5027. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5028. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5029. BEGIN (* no code emission *)
  5030. source := NIL;
  5031. x := x.resolved;
  5032. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5033. x := GetHiddenPointerType();
  5034. ELSIF IsDelegate(x) THEN
  5035. x := GetDelegateType();
  5036. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5037. ELSE HALT(200);
  5038. END;
  5039. td := x.typeDeclaration;
  5040. IF td = NIL THEN
  5041. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5042. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5043. ASSERT(td # NIL);
  5044. END;
  5045. IF newObjectFile THEN
  5046. GetCodeSectionNameForSymbol(td,name);
  5047. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5048. meta.CheckTypeDeclaration(x);
  5049. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5050. ELSE
  5051. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5052. END;
  5053. IF backend.cooperative THEN
  5054. offset := 0;
  5055. ELSE
  5056. IF x IS SyntaxTree.CellType THEN
  5057. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5058. IF baseRecord = NIL THEN
  5059. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5060. ELSE
  5061. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5062. END;
  5063. ELSE
  5064. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5065. END;
  5066. END;
  5067. ELSE
  5068. offset := 0;
  5069. source := module.allSections.FindBySymbol(td); (*TODO*)
  5070. IF source = NIL THEN
  5071. null := 0;
  5072. GetCodeSectionNameForSymbol(td,name);
  5073. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5074. IntermediateCode.InitImmediate(op,addressType,0);
  5075. source(IntermediateCode.Section).Emit(Data(position,op));
  5076. source.SetReferenced(FALSE)
  5077. ELSE
  5078. name := source.name;
  5079. END;
  5080. END;
  5081. RETURN td
  5082. END GetBackendType;
  5083. BEGIN
  5084. (*td := t.typeDeclaration;*)
  5085. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5086. (*
  5087. IF t IS SyntaxTree.PointerType THEN
  5088. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5089. ELSE
  5090. source := GetBackendType(t);
  5091. END;
  5092. *)
  5093. IF newObjectFile THEN
  5094. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5095. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5096. IntermediateCode.SetOffset(res,offset);
  5097. ELSE
  5098. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5099. END;
  5100. (*
  5101. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5102. make memory should be used when tag is used, not earlier
  5103. *)
  5104. RETURN res
  5105. END TypeDescriptorAdr;
  5106. (*
  5107. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5108. VAR result: IntermediateCode.Operand;
  5109. BEGIN
  5110. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5111. operand.tag := TypeDescriptorAdr(operand.type);
  5112. IntermediateCode.MakeMemory(operand.tag,addressType);
  5113. UseIntermediateOperand(operand.tag);
  5114. END;
  5115. END MakeTypeTag;
  5116. *)
  5117. PROCEDURE ProfilerInit;
  5118. VAR reg: IntermediateCode.Operand;
  5119. BEGIN
  5120. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5121. Emit(Call(position,reg,0));
  5122. END ProfilerInit;
  5123. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5124. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5125. BEGIN
  5126. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5127. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5128. THEN
  5129. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5130. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5131. Emit(Push(position,reg));
  5132. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5133. Emit(Push(position,reg));
  5134. StaticCallOperand(result,procedure);
  5135. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5136. ReleaseOperand(result);
  5137. END;
  5138. END ProfilerEnterExit;
  5139. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5140. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5141. BEGIN
  5142. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5143. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5144. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5145. profileInit.Emit(Push(position,reg));
  5146. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5147. profileInit.Emit(Push(position,reg));
  5148. NEW(string, LEN(name)); COPY(name, string^);
  5149. sv := SyntaxTree.NewStringValue(-1,string);
  5150. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5151. sv.SetType(type);
  5152. Designate(sv,result);
  5153. profileInit.Emit(Push(position,result.tag));
  5154. profileInit.Emit(Push(position,result.op));
  5155. StaticCallOperand(result,procedure);
  5156. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5157. ReleaseOperand(result);
  5158. END;
  5159. END ProfilerAddProcedure;
  5160. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5161. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5162. BEGIN
  5163. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5164. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5165. profileInit.Emit(Push(position,reg));
  5166. profileInitPatchPosition := profileInit.pc;
  5167. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  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 ProfilerAddModule;
  5180. PROCEDURE ProfilerPatchInit;
  5181. VAR reg: IntermediateCode.Operand;
  5182. BEGIN
  5183. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5184. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5185. EmitLeave(profileInit,position,0);
  5186. profileInit.Emit(Exit(position,0,0));
  5187. END ProfilerPatchInit;
  5188. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5189. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5190. VAR
  5191. id: LONGINT;
  5192. leftType, rightType: SyntaxTree.Type;
  5193. procedureType: SyntaxTree.ProcedureType;
  5194. runtimeProcedure: SyntaxTree.Procedure;
  5195. runtimeProcedureOperand, operatorOperand: Operand;
  5196. kind: SET;
  5197. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5198. VAR
  5199. arg: IntermediateCode.Operand;
  5200. recordType: SyntaxTree.RecordType;
  5201. fingerPrint: SyntaxTree.FingerPrint;
  5202. BEGIN
  5203. IF type = NIL THEN
  5204. (* no type: push 'NoType' *)
  5205. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5206. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5207. ELSIF IsStrictlyPointerToRecord(type) THEN
  5208. (* pointer to record type: push typetag *)
  5209. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5210. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5211. arg := TypeDescriptorAdr(recordType);
  5212. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5213. ELSE
  5214. (* non-pointer to record type: push fingerprint *)
  5215. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5216. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5217. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5218. END;
  5219. operatorInitializationCodeSection.Emit(Push(position,arg))
  5220. END PushTypeInfo;
  5221. BEGIN
  5222. ASSERT(operatorInitializationCodeSection # NIL);
  5223. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5224. procedureType := operator.type(SyntaxTree.ProcedureType);
  5225. (* determine types *)
  5226. leftType := procedureType.firstParameter.type;
  5227. IF procedureType.numberParameters = 2 THEN
  5228. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5229. rightType := procedureType.firstParameter.nextParameter.type;
  5230. ELSE
  5231. rightType := NIL
  5232. END;
  5233. (* determine operator kind *)
  5234. IF IsStrictlyPointerToRecord(leftType) THEN
  5235. kind := {LhsIsPointer}
  5236. ELSE
  5237. kind := {}
  5238. END;
  5239. IF IsStrictlyPointerToRecord(rightType) THEN
  5240. kind := kind + {RhsIsPointer}
  5241. END;
  5242. IF kind # {} THEN (* TODO: to be removed later on *)
  5243. (* at least one of the types is a pointer to record *)
  5244. (* emit a code that registers this specific operator in the runtime *)
  5245. dump := operatorInitializationCodeSection.comments;
  5246. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5247. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5248. (* push ID *)
  5249. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5250. id := Global.GetSymbol(module.module.case, operator.name);
  5251. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5252. (* push kind *)
  5253. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5254. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5255. (* push type infos *)
  5256. PushTypeInfo(leftType);
  5257. PushTypeInfo(rightType);
  5258. (* push operator address *)
  5259. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5260. StaticCallOperand(operatorOperand, operator);
  5261. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5262. ReleaseOperand(operatorOperand);
  5263. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5264. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5265. ReleaseOperand(runtimeProcedureOperand)
  5266. END
  5267. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5268. END
  5269. END RegisterDynamicOperator;
  5270. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5271. VAR
  5272. traceModule: SyntaxTree.Module;
  5273. procedure: SyntaxTree.Procedure;
  5274. procedureVariable: SyntaxTree.Variable;
  5275. s,msg: Basic.MessageString;
  5276. res: Operand;
  5277. i: LONGINT;
  5278. sv: SyntaxTree.StringValue;
  5279. type: SyntaxTree.Type;
  5280. recordType: SyntaxTree.RecordType;
  5281. printout: Printout.Printer;
  5282. stringWriter: Streams.StringWriter;
  5283. expression: SyntaxTree.Expression;
  5284. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5285. BEGIN
  5286. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5287. IF procedure = NIL THEN
  5288. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5289. END;
  5290. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5291. s := "procedure ";
  5292. Strings.Append(s,backend.traceModuleName);
  5293. Strings.Append(s,".");
  5294. Strings.Append(s,procedureName);
  5295. Strings.Append(s," not present");
  5296. Error(position,s);
  5297. RETURN FALSE
  5298. ELSE
  5299. RETURN TRUE
  5300. END;
  5301. END GetProcedure;
  5302. PROCEDURE CallProcedure;
  5303. VAR size: LONGINT;
  5304. BEGIN
  5305. IF procedure # NIL THEN
  5306. StaticCallOperand(result,procedure);
  5307. size := ProcedureParametersSize(system,procedure);
  5308. ELSE
  5309. Symbol(procedureVariable, result);
  5310. LoadValue(result, procedureVariable.type.resolved);
  5311. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5312. END;
  5313. Emit(Call(position,result.op,size));
  5314. END CallProcedure;
  5315. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5316. VAR res: Operand; string: SyntaxTree.String;
  5317. BEGIN
  5318. IF GetProcedure("String") THEN
  5319. NEW(string, LEN(s)); COPY(s, string^);
  5320. sv := SyntaxTree.NewStringValue(-1,string);
  5321. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5322. sv.SetType(type);
  5323. Designate(sv,res);
  5324. Emit(Push(position,res.tag));
  5325. Emit(Push(position,res.op));
  5326. ReleaseOperand(res);
  5327. CallProcedure;
  5328. END;
  5329. END String;
  5330. PROCEDURE Integer(op: IntermediateCode.Operand);
  5331. BEGIN
  5332. IF GetProcedure("Int") THEN
  5333. Emit(Push(position,op));
  5334. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5335. CallProcedure;
  5336. END;
  5337. END Integer;
  5338. PROCEDURE Float(op: IntermediateCode.Operand);
  5339. BEGIN
  5340. IF GetProcedure("HIntHex") THEN
  5341. Emit(Push(position,op));
  5342. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5343. CallProcedure;
  5344. END;
  5345. END Float;
  5346. PROCEDURE Set(op: IntermediateCode.Operand);
  5347. BEGIN
  5348. IF GetProcedure("Set") THEN
  5349. Emit(Push(position,op));
  5350. (*
  5351. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5352. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5353. *)
  5354. CallProcedure;
  5355. END;
  5356. END Set;
  5357. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5358. BEGIN
  5359. IF GetProcedure("Boolean") THEN
  5360. Emit(Push(position,op));
  5361. CallProcedure;
  5362. END;
  5363. END Boolean;
  5364. PROCEDURE Char(op: IntermediateCode.Operand);
  5365. BEGIN
  5366. IF GetProcedure("Char") THEN
  5367. Emit(Push(position,op));
  5368. CallProcedure;
  5369. END;
  5370. END Char;
  5371. PROCEDURE Address(op: IntermediateCode.Operand);
  5372. BEGIN
  5373. IF GetProcedure("Address") THEN
  5374. Emit(Push(position,op));
  5375. CallProcedure;
  5376. END;
  5377. END Address;
  5378. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5379. BEGIN
  5380. IF GetProcedure("String") THEN
  5381. Emit(Push(position,tag));
  5382. Emit(Push(position,op));
  5383. CallProcedure;
  5384. END;
  5385. END StringOperand;
  5386. PROCEDURE Ln;
  5387. BEGIN
  5388. IF GetProcedure("Ln") THEN
  5389. CallProcedure;
  5390. END;
  5391. END Ln;
  5392. BEGIN
  5393. IF backend.traceModuleName = "" THEN RETURN END;
  5394. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5395. IF GetProcedure("Enter") THEN
  5396. CallProcedure
  5397. END;
  5398. NEW(stringWriter,LEN(s));
  5399. FOR i := 0 TO x.Length()-1 DO
  5400. msg := "";
  5401. expression := x.GetExpression(i);
  5402. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5403. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5404. ELSE
  5405. Global.GetModuleName(module.module, s);
  5406. END;
  5407. IF i = 0 THEN
  5408. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5409. stringWriter.String(":");
  5410. END;
  5411. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5412. IF ~(expression IS SyntaxTree.StringValue) THEN
  5413. printout.Expression(expression);
  5414. stringWriter.Get(s);
  5415. Strings.Append(msg,s);
  5416. Strings.Append(msg,"= ");
  5417. ELSE stringWriter.Get(s); (* remove from string writer *)
  5418. Strings.Append(msg, s);
  5419. END;
  5420. String(msg);
  5421. IF SemanticChecker.IsStringType(expression.type) THEN
  5422. Designate(expression,res);
  5423. StringOperand(res.op,res.tag);
  5424. ELSE
  5425. Evaluate(expression,res);
  5426. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5427. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5428. Convert(res.op,int64);
  5429. END;
  5430. Integer(res.op);
  5431. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5432. Boolean(res.op);
  5433. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5434. Set(res.op);
  5435. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5436. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5437. Convert(res.op,float64);
  5438. END;
  5439. Float(res.op);
  5440. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5441. Char(res.op);
  5442. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5443. Address(res.op);String("H");
  5444. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5445. Address(res.op);String("H");
  5446. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5447. Address(res.op);String("H");
  5448. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5449. Address(res.op);String("H");
  5450. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5451. String("NIL");
  5452. ELSE HALT(200);
  5453. END;
  5454. END;
  5455. ReleaseOperand(res);
  5456. String("; ");
  5457. END;
  5458. IF GetProcedure("Exit") THEN
  5459. CallProcedure
  5460. ELSE
  5461. Ln;
  5462. END;
  5463. END;
  5464. END SystemTrace;
  5465. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5466. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5467. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5468. BEGIN
  5469. type := type.resolved;
  5470. IF type IS SyntaxTree.RecordType THEN
  5471. WITH type: SyntaxTree.RecordType DO
  5472. baseType := type.baseType;
  5473. IF baseType # NIL THEN
  5474. baseType := baseType.resolved;
  5475. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5476. InitFields(baseType,adr,offset);
  5477. END;
  5478. variable := type.recordScope.firstVariable;
  5479. WHILE variable # NIL DO
  5480. IF variable.initializer # NIL THEN
  5481. Evaluate(variable.initializer,initializerOp);
  5482. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5483. Emit(Mov(position,mem,initializerOp.op));
  5484. ReleaseOperand(initializerOp);
  5485. ReleaseIntermediateOperand(mem);
  5486. END;
  5487. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5488. variable := variable.nextVariable
  5489. END;
  5490. END;
  5491. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5492. WITH type: SyntaxTree.ArrayType DO
  5493. IF type.form = SyntaxTree.Static THEN
  5494. baseType := type.arrayBase;
  5495. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5496. FOR i := 0 TO type.staticLength-1 DO
  5497. InitFields(baseType,adr,offset+i*size);
  5498. END;
  5499. END;
  5500. END;
  5501. ELSIF type IS SyntaxTree.MathArrayType THEN
  5502. WITH type: SyntaxTree.MathArrayType DO
  5503. IF type.form = SyntaxTree.Open THEN
  5504. dim := DynamicDim(type);
  5505. imm := IntermediateCode.Immediate(addressType,dim);
  5506. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5507. baseType := SemanticChecker.ArrayBase(type,dim);
  5508. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5509. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5510. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5511. ReleaseIntermediateOperand(imm);
  5512. (* flags remain empty (=0) for open array *)
  5513. ELSIF 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. END;
  5522. END InitFields;
  5523. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5524. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5525. BEGIN
  5526. type := variable.type.resolved;
  5527. IF (type IS SyntaxTree.MathArrayType) THEN
  5528. WITH type: SyntaxTree.MathArrayType DO
  5529. IF type.form = SyntaxTree.Open THEN
  5530. Symbol(variable,operand);
  5531. InitFields(type, operand.tag,0);
  5532. ELSIF type.form = SyntaxTree.Tensor THEN
  5533. Symbol(variable, operand);
  5534. MakeMemory(tmp,operand.op,addressType,0);
  5535. ReleaseOperand(operand);
  5536. Emit(Mov(position,tmp, nil ) );
  5537. ReleaseIntermediateOperand(tmp);
  5538. END;
  5539. END;
  5540. ELSE
  5541. Symbol(variable,operand);
  5542. IF variable.initializer # NIL THEN
  5543. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5544. reference.SetType(variable.type.resolved);
  5545. reference.SetAssignable(TRUE);
  5546. Assign(reference,variable.initializer);
  5547. END;
  5548. InitFields(type, operand.op,0);
  5549. ReleaseOperand(operand);
  5550. END;
  5551. END InitVariable;
  5552. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5553. VAR end: Label;
  5554. BEGIN
  5555. IF type.form = SyntaxTree.Tensor THEN
  5556. InitOperand(result,ModeValue);
  5557. ReuseCopy(result.op,base);
  5558. end := NewLabel();
  5559. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5560. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5561. SetLabel(end);
  5562. Convert(result.op,int32);
  5563. ELSE
  5564. InitOperand(result,ModeValue);
  5565. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5566. END
  5567. END MathArrayDim;
  5568. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5569. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5570. BEGIN
  5571. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5572. MakeMemory(mem,base,addressType,offset);
  5573. Emit(Mov(position,mem,value));
  5574. ReleaseIntermediateOperand(mem);
  5575. END PutMathArrayField;
  5576. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5577. VAR mem: IntermediateCode.Operand;
  5578. BEGIN
  5579. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5580. MakeMemory(mem,base,addressType,offset);
  5581. Emit(Mov(position,mem,value));
  5582. ReleaseIntermediateOperand(mem);
  5583. END PutMathArrayFieldOffset;
  5584. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5585. BEGIN
  5586. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5587. MakeMemory(value,base,addressType,offset);
  5588. END GetMathArrayField;
  5589. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5590. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5591. BEGIN
  5592. IF incr THEN
  5593. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5594. ELSE
  5595. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5596. END;
  5597. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5598. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5599. ELSE
  5600. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5601. Emit(Mov(position,reg,dim));
  5602. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5603. Emit(Add(position,reg,reg,base));
  5604. MakeMemory(mem, reg, addressType, offset);
  5605. ReleaseIntermediateOperand(reg);
  5606. Emit(Mov(position,mem,value));
  5607. ReleaseIntermediateOperand(mem);
  5608. END;
  5609. END PutMathArrayLenOrIncr;
  5610. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5611. BEGIN
  5612. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5613. END PutMathArrayLength;
  5614. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5615. BEGIN
  5616. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5617. END PutMathArrayIncrement;
  5618. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5619. BEGIN
  5620. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5621. END GetMathArrayIncrement;
  5622. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5623. BEGIN
  5624. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5625. END GetMathArrayLength;
  5626. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5627. VAR dimOp: IntermediateCode.Operand;
  5628. BEGIN
  5629. dimOp := IntermediateCode.Immediate(int32, dim);
  5630. GetMathArrayLength(type, operand, dimOp, check, result);
  5631. END GetMathArrayLengthAt;
  5632. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5633. VAR dimOp: IntermediateCode.Operand;
  5634. BEGIN
  5635. dimOp := IntermediateCode.Immediate(int32, dim);
  5636. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5637. END GetMathArrayIncrementAt;
  5638. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5639. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5640. offset: LONGINT;
  5641. BEGIN
  5642. IF increment THEN
  5643. offset := MathIncrOffset;
  5644. ELSE
  5645. offset := MathLenOffset;
  5646. END;
  5647. INC(offset,operand.dimOffset*2);
  5648. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5649. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5650. END;
  5651. (* static dimension *)
  5652. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5653. IF check & (type.form = SyntaxTree.Tensor) THEN
  5654. DimensionCheck(operand.tag,dim,BrltL);
  5655. END;
  5656. val := SHORT(dim.intValue);
  5657. IF type.form # SyntaxTree.Tensor THEN
  5658. t := SemanticChecker.ArrayBase(type,val);
  5659. type := t.resolved(SyntaxTree.MathArrayType);
  5660. IF type.form = SyntaxTree.Static THEN
  5661. IF increment THEN
  5662. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5663. ELSE
  5664. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5665. END;
  5666. InitOperand(result,ModeValue);
  5667. result.op := res;
  5668. RETURN;
  5669. END;
  5670. END;
  5671. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5672. MakeMemory(res,operand.tag,addressType,offset);
  5673. (*
  5674. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5675. *)
  5676. InitOperand(result,ModeValue);
  5677. result.op := res;
  5678. ELSE
  5679. Convert(dim,addressType);
  5680. IF check THEN
  5681. IF type.form = SyntaxTree.Tensor THEN
  5682. DimensionCheck(operand.tag,dim,BrltL);
  5683. ELSIF isUnchecked THEN (* do nothing *)
  5684. ELSE
  5685. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5686. END;
  5687. END;
  5688. end := NewLabel(); next := NIL;
  5689. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5690. Emit(Mov(position,res,dim));
  5691. Convert(res,int32);
  5692. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5693. WHILE t IS SyntaxTree.MathArrayType DO
  5694. type := t(SyntaxTree.MathArrayType);
  5695. IF type.form = SyntaxTree.Static THEN
  5696. imm := IntermediateCode.Immediate(int32,val);
  5697. next := NewLabel();
  5698. BrneL(next,imm,res);
  5699. IF increment THEN
  5700. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5701. ELSE
  5702. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5703. END;
  5704. Emit(MovReplace(position,res,imm));
  5705. BrL(end);
  5706. ELSE hasDynamicPart := TRUE;
  5707. END;
  5708. t := type.arrayBase.resolved;
  5709. val := val + 1;
  5710. IF next # NIL THEN SetLabel(next) END;
  5711. END;
  5712. IF hasDynamicPart THEN
  5713. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5714. Emit(Mov(position,res2,dim));
  5715. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5716. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5717. Emit(Add(position,res2,res2,imm));
  5718. Emit(Add(position,res2,res2,operand.tag));
  5719. IntermediateCode.MakeMemory(res2,int32);
  5720. Emit(MovReplace(position,res,res2));
  5721. ReleaseIntermediateOperand(res2);
  5722. END;
  5723. SetLabel(end);
  5724. Convert(res,int32);
  5725. InitOperand(result,ModeValue);
  5726. result.op := res;
  5727. END;
  5728. END MathArrayLenOrIncr;
  5729. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5730. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5731. offset: LONGINT;
  5732. BEGIN
  5733. offset := operand.dimOffset+DynamicDim(type)-1;
  5734. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5735. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5736. val := SHORT(dim.intValue);
  5737. t := SemanticChecker.ArrayBase(type,val);
  5738. type := t.resolved(SyntaxTree.ArrayType);
  5739. IF type.form = SyntaxTree.Static THEN
  5740. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5741. ELSE
  5742. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5743. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5744. END;
  5745. UseIntermediateOperand(res);
  5746. InitOperand(result,ModeValue);
  5747. result.op := res;
  5748. ELSE
  5749. Convert(dim,addressType);
  5750. IF ~isUnchecked THEN
  5751. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5752. END;
  5753. end := NewLabel(); next := NIL;
  5754. (* ReuseCopy(dim,res); *)
  5755. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5756. Emit(Mov(position,res,dim));
  5757. Convert(res,int32);
  5758. Convert(res,int32);
  5759. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5760. WHILE t IS SyntaxTree.ArrayType DO
  5761. type := t(SyntaxTree.ArrayType);
  5762. IF type.form = SyntaxTree.Static THEN
  5763. imm := IntermediateCode.Immediate(int32,val);
  5764. next := NewLabel();
  5765. BrneL(next,imm,res);
  5766. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5767. Emit(MovReplace(position,res,imm));
  5768. BrL(end);
  5769. ELSE hasDynamicPart := TRUE;
  5770. END;
  5771. t := type.arrayBase.resolved;
  5772. val := val + 1;
  5773. IF next # NIL THEN SetLabel(next) END;
  5774. END;
  5775. IF hasDynamicPart THEN
  5776. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5777. Convert(res2,addressType);
  5778. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5779. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5780. Emit(Sub(position,res2,imm,res2));
  5781. Emit(Add(position,res2,res2,operand.tag));
  5782. IntermediateCode.MakeMemory(res2,int32);
  5783. Emit(MovReplace(position,res,res2));
  5784. ReleaseIntermediateOperand(res2);
  5785. END;
  5786. SetLabel(end);
  5787. Convert(res,int32);
  5788. InitOperand(result,ModeValue);
  5789. result.op := res;
  5790. END;
  5791. END ArrayLen;
  5792. (**
  5793. create a temporary variable in current scope
  5794. **)
  5795. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5796. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5797. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5798. BEGIN
  5799. IF ~register THEN
  5800. variable := temporaries.GetFreeVariable(type, index);
  5801. ELSE
  5802. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5803. END;
  5804. scope := currentScope;
  5805. IF variable = NIL THEN
  5806. COPY("@hiddenIRVar",string);
  5807. Basic.AppendNumber(string,index);
  5808. name := SyntaxTree.NewIdentifier(string);
  5809. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5810. variable.SetType(type);
  5811. variable.SetAccess(SyntaxTree.Hidden);
  5812. IF ~register THEN
  5813. temporaries.AddVariable(variable);
  5814. IF scope.lastVariable # NIL THEN
  5815. offset := scope.lastVariable.offsetInBits;
  5816. ELSE
  5817. offset := 0;
  5818. END;
  5819. DEC(offset,system.SizeOf(variable.type));
  5820. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5821. variable(SyntaxTree.Variable).SetOffset(offset);
  5822. scope.AddVariable(variable(SyntaxTree.Variable));
  5823. scope.EnterSymbol(variable, duplicate);
  5824. ASSERT(~duplicate);
  5825. InitVariable(variable(SyntaxTree.Variable));
  5826. ELSE
  5827. variable.SetUseRegister(TRUE);
  5828. variable(SyntaxTree.Variable).SetOffset(0);
  5829. END;
  5830. ELSE
  5831. InitVariable(variable(SyntaxTree.Variable));
  5832. (*
  5833. ASSERT(variable.type.resolved = type.resolved)
  5834. *)
  5835. END;
  5836. RETURN variable(SyntaxTree.Variable)
  5837. END GetTemporaryVariable;
  5838. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5839. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5840. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5841. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5842. i: LONGINT; duplicate: BOOLEAN;
  5843. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5844. VAR variable: SyntaxTree.Variable;
  5845. BEGIN
  5846. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5847. variable.SetType(type);
  5848. recordScope.AddVariable(variable);
  5849. END AddVariable;
  5850. BEGIN
  5851. name := "@ArrayDescriptor";
  5852. Basic.AppendNumber(name,dimensions);
  5853. identifier := SyntaxTree.NewIdentifier(name);
  5854. parentScope := module.module.moduleScope;
  5855. symbol := parentScope.FindSymbol(identifier);
  5856. IF symbol # NIL THEN
  5857. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5858. type := typeDeclaration.declaredType;
  5859. ELSE
  5860. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5861. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5862. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5863. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5864. recordType.SetTypeDeclaration(typeDeclaration);
  5865. recordType.SetState(SyntaxTree.Resolved);
  5866. typeDeclaration.SetDeclaredType(recordType);
  5867. AddVariable("@ptr",system.anyType);
  5868. AddVariable("@adr",system.addressType);
  5869. AddVariable("@flags",system.addressType);
  5870. AddVariable("@dim",system.addressType);
  5871. AddVariable("@elementSize",system.addressType);
  5872. FOR i := 0 TO dimensions-1 DO
  5873. name := "@len";
  5874. Basic.AppendNumber(name,i);
  5875. AddVariable(name,system.addressType);
  5876. name := "@incr";
  5877. Basic.AppendNumber(name,i);
  5878. AddVariable(name,system.addressType);
  5879. END;
  5880. parentScope.AddTypeDeclaration(typeDeclaration);
  5881. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5882. ASSERT(~duplicate);
  5883. type := recordType;
  5884. END;
  5885. RETURN type
  5886. END GetMathArrayDescriptorType;
  5887. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5888. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5889. BEGIN
  5890. type := GetMathArrayDescriptorType(dimensions);
  5891. Emit(Push(position,op.op));
  5892. (* push type descriptor *)
  5893. reg := TypeDescriptorAdr(type);
  5894. IF ~newObjectFile THEN
  5895. IntermediateCode.MakeMemory(reg,addressType);
  5896. END;
  5897. Emit(Push(position,reg));
  5898. ReleaseIntermediateOperand(reg);
  5899. (* push realtime flag: false by default *)
  5900. Emit(Push(position,false));
  5901. CallThis(position,"Heaps","NewRec",3);
  5902. END NewMathArrayDescriptor;
  5903. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5904. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5905. BEGIN
  5906. NEW(string, LEN(s)); COPY(s, string^);
  5907. sv := SyntaxTree.NewStringValue(-1,string);
  5908. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5909. sv.SetType(type);
  5910. Designate(sv,res);
  5911. Emit(Push(position,res.tag));
  5912. Emit(Push(position,res.op));
  5913. ReleaseOperand(res);
  5914. END PushConstString;
  5915. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5916. BEGIN
  5917. IF b THEN
  5918. Emit(Push(-1, true));
  5919. ELSE
  5920. Emit(Push(-1, false));
  5921. END;
  5922. END PushConstBoolean;
  5923. PROCEDURE PushConstSet(v: SET);
  5924. VAR value: SyntaxTree.Value; op: Operand;
  5925. BEGIN
  5926. value := SyntaxTree.NewSetValue(-1, v);
  5927. value.SetType(system.setType);
  5928. Evaluate(value, op);
  5929. Emit(Push(-1, op.op));
  5930. ReleaseOperand(op);
  5931. END PushConstSet;
  5932. PROCEDURE PushConstInteger(v: LONGINT);
  5933. VAR value: SyntaxTree.Value; op: Operand;
  5934. BEGIN
  5935. value := SyntaxTree.NewIntegerValue(-1, v);
  5936. value.SetType(system.longintType);
  5937. Evaluate(value, op);
  5938. Emit(Push(-1, op.op));
  5939. ReleaseOperand(op);
  5940. END PushConstInteger;
  5941. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5942. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5943. section: IntermediateCode.Section;
  5944. BEGIN
  5945. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5946. Global.GetSymbolSegmentedName(symbol, name);
  5947. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5948. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5949. procedure.SetScope(moduleScope);
  5950. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5951. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5952. procedure.SetAccess(SyntaxTree.Hidden);
  5953. currentScope := procedureScope;
  5954. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5955. EmitEnter(section, -1,NIL,0,0,0);
  5956. RETURN section;
  5957. END OpenInitializer;
  5958. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5959. BEGIN
  5960. EmitLeave(section, 0, 0 );
  5961. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5962. section := prev;
  5963. END CloseInitializer;
  5964. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5965. VAR name: SyntaxTree.IdentifierString;
  5966. variable: SyntaxTree.Variable;
  5967. type: SyntaxTree.Type;
  5968. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5969. BEGIN
  5970. InitOperand(op,ModeReference);
  5971. op.op := fp;
  5972. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5973. Dereference(op, x, FALSE);
  5974. result := op;
  5975. Symbol(symbol, op);
  5976. END Field;
  5977. PROCEDURE Direction(direction: LONGINT): SET;
  5978. BEGIN
  5979. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  5980. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  5981. ELSE HALT(100);
  5982. END;
  5983. END Direction;
  5984. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  5985. VAR name: ARRAY 256 OF CHAR; op: Operand;
  5986. BEGIN
  5987. Field(port, op);
  5988. ToMemory(op.op,addressType,0);
  5989. Emit(Push(-1, op.op));
  5990. ReleaseOperand(op);
  5991. Basic.GetString(modifier.identifier, name);
  5992. PushConstString(name);
  5993. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  5994. ASSERT(SemanticChecker.IsStringType(value.type));
  5995. Designate(value, op);
  5996. Emit(Push(modifier.position, op.tag));
  5997. Emit(Push(modifier.position, op.op));
  5998. ReleaseOperand(op);
  5999. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  6000. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  6001. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6002. Evaluate(value, op);
  6003. Emit(Push(modifier.position, op.op));
  6004. ReleaseOperand(op);
  6005. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  6006. ELSE
  6007. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  6008. END;
  6009. END AddPortProperty;
  6010. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  6011. VAR modifier: SyntaxTree.Modifier;
  6012. BEGIN
  6013. modifier := variable.modifiers;
  6014. WHILE modifier # NIL DO
  6015. AddPortProperty(variable,modifier, modifier.expression);
  6016. modifier := modifier.nextModifier;
  6017. END;
  6018. END AddPortProperties;
  6019. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6020. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6021. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6022. PROCEDURE PushLens(type: SyntaxTree.Type);
  6023. BEGIN
  6024. IF type IS SyntaxTree.ArrayType THEN
  6025. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6026. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6027. Emit(Push(-1, op.op));
  6028. ReleaseOperand(op);
  6029. INC(dim);
  6030. ELSE
  6031. baseType := type;
  6032. END;
  6033. END PushLens;
  6034. BEGIN
  6035. (* cell *)
  6036. IF variable.type IS SyntaxTree.ArrayType THEN
  6037. type := variable.type;
  6038. dim := 0;
  6039. PushLens(type);
  6040. portType := baseType.resolved(SyntaxTree.PortType);
  6041. ELSE
  6042. portType := variable.type(SyntaxTree.PortType);
  6043. END;
  6044. PushSelfPointer();
  6045. (* port *)
  6046. Field(variable, op);
  6047. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6048. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6049. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6050. Designate(d, op);*)
  6051. Emit(Push(-1, op.op));
  6052. ReleaseOperand(op);
  6053. (* name *)
  6054. PushConstString(name);
  6055. (* inout *)
  6056. PushConstSet(Direction(portType.direction));
  6057. (* width *)
  6058. PushConstInteger(portType.sizeInBits);
  6059. IF variable.type IS SyntaxTree.PortType THEN
  6060. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6061. AddPortProperties(variable);
  6062. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6063. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6064. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6065. Emit(Add(position,reg, sp, size));
  6066. (* dim *)
  6067. PushConstInteger(dim);
  6068. (* len array *)
  6069. Emit(Push(position, reg));
  6070. ReleaseIntermediateOperand(reg);
  6071. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6072. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6073. Emit(Add(position, sp,sp, size));
  6074. END;
  6075. END Variable;
  6076. BEGIN
  6077. IF backend.cellsAreObjects THEN
  6078. variable := x.cellScope.firstVariable;
  6079. WHILE (variable # NIL) DO
  6080. type := variable.type.resolved;
  6081. WHILE (type IS SyntaxTree.ArrayType) DO
  6082. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6083. END;
  6084. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6085. (*!!! check port arrays !! *)
  6086. Global.GetSymbolName(variable,name);
  6087. Variable(name,variable);
  6088. END;
  6089. variable := variable.nextVariable;
  6090. END;
  6091. ELSE HALT(200)
  6092. END;
  6093. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6094. (*
  6095. x.typeDeclaration.GetName(componentName);
  6096. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6097. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6098. instanceType.SetDataMemorySize(dataMemorySize);
  6099. END;
  6100. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6101. instanceType.SetInstructionMemorySize(codeMemorySize)
  6102. END;
  6103. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6104. instanceType.AddCapability(ActiveCells.VectorCapability)
  6105. END;
  6106. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6107. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6108. END;
  6109. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6110. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6111. END;
  6112. AddDevices(instanceType, x);
  6113. *)
  6114. (*
  6115. IF x.isCellNet THEN
  6116. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6117. END;
  6118. *)
  6119. (*currentActiveCellsScope := instanceType;*)
  6120. (*
  6121. parameter := x.firstParameter;
  6122. portIndex := 0;
  6123. WHILE parameter # NIL DO
  6124. parameter.GetName(parameterName);
  6125. parameterType := parameter.type.resolved;
  6126. Parameter(parameterName, parameterType);
  6127. (*
  6128. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6129. ParameterArray(parameterType);
  6130. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6131. FOR i := 0 TO len-1 DO
  6132. COPY(parameterName,name);
  6133. AppendIndex(name,i);
  6134. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6135. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6136. INC(portIndex);
  6137. END;
  6138. ELSE
  6139. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6140. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6141. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6142. INC(portIndex);
  6143. END;
  6144. *)
  6145. parameter := parameter.nextParameter;
  6146. END;
  6147. *)
  6148. (*
  6149. Scope(x.cellScope);
  6150. currentActiveCellsScope := prevActiveCellsScope;
  6151. AddModules(instanceType,x.cellScope);
  6152. *)
  6153. END AddPorts;
  6154. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6155. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6156. BEGIN
  6157. Symbol(cell,op);
  6158. ToMemory(op.op,addressType,0);
  6159. Emit(Push(position,op.op));
  6160. ReleaseOperand(op);
  6161. Basic.GetString(property.name, name);
  6162. PushConstString(name);
  6163. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6164. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6165. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6166. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6167. Designate(d, op);
  6168. IF SemanticChecker.IsStringType(property.type) THEN
  6169. Emit(Push(-1, op.tag))
  6170. END;
  6171. Emit(Push(-1, op.op));
  6172. ReleaseOperand(op);
  6173. END;
  6174. IF SemanticChecker.IsStringType(property.type) THEN
  6175. ASSERT(SemanticChecker.IsStringType(value.type));
  6176. Designate(value, op);
  6177. Emit(Push(property.position, op.tag));
  6178. Emit(Push(property.position, op.op));
  6179. ReleaseOperand(op);
  6180. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6181. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6182. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6183. Evaluate(value, op);
  6184. Emit(Push(property.position, op.op));
  6185. ReleaseOperand(op);
  6186. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6187. ELSE
  6188. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6189. END;
  6190. END AddProperty;
  6191. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6192. VAR symbol: SyntaxTree.Symbol;
  6193. BEGIN
  6194. WHILE modifier # NIL DO
  6195. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6196. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6197. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6198. ELSE
  6199. (*! move this check to checker *)
  6200. Error(modifier.position, "undefined property");
  6201. END;
  6202. modifier := modifier.nextModifier;
  6203. END;
  6204. END AddModifiers;
  6205. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6206. VAR last: SyntaxTree.Modifier;
  6207. BEGIN
  6208. IF to = NIL THEN
  6209. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6210. ELSE
  6211. last := to;
  6212. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6213. last := last.nextModifier;
  6214. END;
  6215. IF last.identifier # this.identifier THEN
  6216. ASSERT(last.nextModifier = NIL);
  6217. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6218. END;
  6219. END;
  6220. END AppendModifier;
  6221. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6222. BEGIN
  6223. WHILE this # NIL DO
  6224. AppendModifier(to, this);
  6225. this := this.nextModifier;
  6226. END;
  6227. END AppendModifiers;
  6228. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6229. VAR op: Operand;
  6230. BEGIN
  6231. Evaluate(p, op);
  6232. Emit(Push(p.position, op.op));
  6233. ReleaseOperand(op);
  6234. (*
  6235. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6236. p := p(SyntaxTree.Designator).left;
  6237. END;
  6238. IF p # NIL THEN
  6239. Evaluate(p, op);
  6240. Emit(Push(p.position, op.op));
  6241. ReleaseOperand(op);
  6242. ELSE
  6243. Emit(Push(-1, nil));
  6244. END;
  6245. *)
  6246. END PushPort;
  6247. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6248. VAR
  6249. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6250. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6251. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6252. tmp:IntermediateCode.Operand;
  6253. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6254. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6255. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6256. dest: IntermediateCode.Operand;
  6257. staticLength: LONGINT; itype: IntermediateCode.Type;
  6258. convert,isTensor: BOOLEAN;
  6259. recordType: SyntaxTree.RecordType;
  6260. baseType: SyntaxTree.Type;
  6261. flags: SET;
  6262. left: SyntaxTree.Expression;
  6263. call: SyntaxTree.Designator;
  6264. procedure: SyntaxTree.Procedure;
  6265. temporaryVariable: SyntaxTree.Variable;
  6266. dummy: IntermediateCode.Operand;
  6267. customBuiltin: SyntaxTree.CustomBuiltin;
  6268. isVarPar: ARRAY 3 OF BOOLEAN;
  6269. callsection: Sections.Section;
  6270. segmentedName: Basic.SegmentedName;
  6271. needsTrace: BOOLEAN;
  6272. n: ARRAY 256 OF CHAR;
  6273. modifier: SyntaxTree.Modifier;
  6274. previous, init: IntermediateCode.Section;
  6275. prevScope: SyntaxTree.Scope;
  6276. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6277. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6278. priority: IntermediateCode.Operand;
  6279. op,callop: Operand;
  6280. BEGIN
  6281. IF type = NIL THEN RETURN END;
  6282. type := type.resolved;
  6283. IF type IS SyntaxTree.PointerType THEN
  6284. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6285. END;
  6286. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6287. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6288. recordScope := type(SyntaxTree.RecordType).recordScope;
  6289. IF recordScope.bodyProcedure # NIL THEN
  6290. procedure := recordScope.bodyProcedure;
  6291. body := procedure.procedureScope.body;
  6292. Emit(Push(position,self));
  6293. IF body.isActive THEN
  6294. StaticCallOperand(callop,procedure);
  6295. Emit(Push(position,callop.op));
  6296. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6297. Convert(priority,sizeType);
  6298. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6299. END;
  6300. Emit(Push(position,priority));
  6301. ReleaseIntermediateOperand(priority);
  6302. IF backend.cooperative THEN
  6303. Emit(Push(position,self));
  6304. CallThis(position,"Activities","Create",3)
  6305. ELSE
  6306. flags := 0;
  6307. IF body.isSafe THEN
  6308. flags := 1;
  6309. END;
  6310. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6311. Emit(Push(position,self));
  6312. CallThis(position,"Objects","CreateProcess",4)
  6313. END;
  6314. ELSE
  6315. Emit(Push(position,self));
  6316. StaticCallOperand(callop,procedure);
  6317. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6318. END;
  6319. Emit(Pop(position,self));
  6320. END;
  6321. END CallBodies;
  6322. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6323. BEGIN
  6324. actualType := actualType.resolved;
  6325. IF actualType IS SyntaxTree.StringType THEN
  6326. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6327. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6328. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6329. ELSE
  6330. tmp := op.tag;
  6331. IntermediateCode.MakeMemory(tmp,addressType);
  6332. Emit(Push(position,tmp));
  6333. END;
  6334. Emit(Push(position,op.op))
  6335. END PushString;
  6336. PROCEDURE PushTD(type: SyntaxTree.Type);
  6337. VAR op: IntermediateCode.Operand;
  6338. BEGIN
  6339. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6340. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6341. ELSE
  6342. IF type.resolved IS SyntaxTree.PointerType THEN
  6343. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6344. END;
  6345. op := TypeDescriptorAdr(type.resolved);
  6346. IF ~newObjectFile THEN
  6347. IntermediateCode.MakeMemory(op,addressType);
  6348. END;
  6349. Emit(Push(position,op));
  6350. END
  6351. END PushTD;
  6352. BEGIN
  6353. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6354. dest := destination; destination := emptyOperand;
  6355. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6356. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6357. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6358. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6359. CASE x.id OF
  6360. (* ---- COPY ----- *)
  6361. |Global.Copy:
  6362. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6363. (* ---- EXCL, INCL----- *)
  6364. |Global.Excl,Global.Incl:
  6365. Evaluate(p0,s0);
  6366. Evaluate(p1,s1);
  6367. Convert(s1.op,setType);
  6368. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6369. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6370. END;
  6371. ReuseCopy(res,s0.op);
  6372. ReleaseOperand(s0);
  6373. Reuse1(tmp,s1.op);
  6374. ReleaseOperand(s1);
  6375. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6376. IF x.id = Global.Excl THEN
  6377. Emit(Not(position,tmp,tmp));
  6378. Emit(And(position,res,res,tmp));
  6379. ELSE
  6380. Emit(Or(position,res,res,tmp));
  6381. END;
  6382. ReleaseIntermediateOperand(tmp);
  6383. Designate(p0,s0);
  6384. ToMemory(s0.op,setType,0);
  6385. Emit(Mov(position,s0.op,res));
  6386. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6387. (* ---- DISPOSE ----- *)
  6388. |Global.Dispose:
  6389. Designate(p0,s0);
  6390. Emit(Push(position,s0.op));
  6391. ReleaseOperand(s0);
  6392. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6393. (* ---- GETPROCEDURE ----- *)
  6394. |Global.GetProcedure:
  6395. Designate(p0,s0);
  6396. PushString(s0,p0.type);
  6397. Designate(p1,s1);
  6398. PushString(s1,p1.type);
  6399. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6400. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6401. ELSE PushTD(procedureType.firstParameter.type)
  6402. END;
  6403. PushTD(procedureType.returnType);
  6404. Designate(p2,s2);
  6405. Emit(Push(position,s2.op));
  6406. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6407. CallThis(position,"Modules","GetProcedure", 7);
  6408. (* ---- ASH, LSH, ROT ----- *)
  6409. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6410. Evaluate(p0,s0);
  6411. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6412. (* make unsigned arguments in order to produced a logical shift *)
  6413. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6414. convert:= TRUE;
  6415. itype := s0.op.type;
  6416. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6417. Convert(s0.op,itype);
  6418. ELSE
  6419. convert := FALSE;
  6420. END;
  6421. END;
  6422. Evaluate(p1,s1);
  6423. IF IsIntegerConstant(p1,hint) THEN
  6424. ReuseCopy(reg,s0.op);
  6425. IF hint > 0 THEN
  6426. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6427. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6428. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6429. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6430. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6431. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6432. END;
  6433. ELSIF hint < 0 THEN
  6434. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6435. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6436. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6437. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6438. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6439. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6440. END;
  6441. END;
  6442. ReleaseOperand(s0); ReleaseOperand(s1);
  6443. ELSE
  6444. exit := NewLabel();
  6445. end := NewLabel();
  6446. ReuseCopy(reg,s0.op);
  6447. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6448. Reuse1(tmp,s1.op);
  6449. Emit(Neg(position,tmp,s1.op));
  6450. Convert(tmp,s1.op.type);
  6451. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6452. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6453. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6454. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6455. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6456. END;
  6457. ReleaseIntermediateOperand(tmp);
  6458. BrL(end);
  6459. SetLabel(exit);
  6460. ReuseCopy(tmp,s1.op);
  6461. Convert(tmp,s1.op.type);
  6462. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6463. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6464. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6465. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6466. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6467. END;
  6468. ReleaseIntermediateOperand(tmp);
  6469. SetLabel(end);
  6470. ReleaseOperand(s0); ReleaseOperand(s1);
  6471. END;
  6472. InitOperand(result,ModeValue);
  6473. IF convert THEN
  6474. itype := reg.type;
  6475. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6476. Convert(reg,itype);
  6477. END;
  6478. result.op := reg;
  6479. (* ---- CAP ----- *)
  6480. |Global.Cap:
  6481. Evaluate(p0,result);
  6482. ReuseCopy(reg,result.op);
  6483. ReleaseIntermediateOperand(result.op);
  6484. ignore := NewLabel();
  6485. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6486. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6487. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6488. SetLabel(ignore);
  6489. result.op := reg;
  6490. (* ---- CHR ----- *)
  6491. |Global.Chr, Global.Chr32:
  6492. Evaluate(p0,result);
  6493. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6494. |Global.Entier, Global.EntierH:
  6495. Evaluate(p0,result);
  6496. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6497. (* ---- MIN and MAX ----- *)
  6498. |Global.Max,Global.Min:
  6499. Evaluate(p0,s0);
  6500. Evaluate(p1,s1);
  6501. Reuse2(res,s0.op,s1.op);
  6502. else := NewLabel();
  6503. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6504. ELSE BrltL(else,s1.op,s0.op) END;
  6505. Emit(Mov(position,res,s0.op));
  6506. ReleaseOperand(s0);
  6507. end := NewLabel();
  6508. BrL(end);
  6509. SetLabel(else);
  6510. Emit(MovReplace(position,res,s1.op));
  6511. SetLabel(end);
  6512. ReleaseOperand(s1);
  6513. InitOperand(result,ModeValue);
  6514. result.op := res;
  6515. (* ---- ODD ----- *)
  6516. |Global.Odd:
  6517. IF ~conditional THEN
  6518. ConditionToValue(x)
  6519. ELSE
  6520. Evaluate(p0,result);
  6521. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6522. Reuse1(res,result.op);
  6523. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6524. ReleaseIntermediateOperand(result.op);
  6525. result.op := res;
  6526. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6527. ReleaseOperand(result);
  6528. BrL(falseLabel);
  6529. END;
  6530. (* ---- ORD ----- *)
  6531. |Global.Ord, Global.Ord32:
  6532. Evaluate(p0,result);
  6533. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6534. (* ---- SHORT, LONG ----- *)
  6535. |Global.Short, Global.Long:
  6536. Evaluate(p0,result);
  6537. IF x.type IS SyntaxTree.ComplexType THEN
  6538. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6539. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6540. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6541. ELSE
  6542. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6543. END
  6544. (* ---- HALT, SYSTEM.HALT----- *)
  6545. |Global.Halt, Global.systemHalt:
  6546. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6547. EmitTrap (position, val);
  6548. (* ---- ASSERT ----- *)
  6549. |Global.Assert:
  6550. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6551. trueL := NewLabel();
  6552. falseL := NewLabel();
  6553. Condition(p0,trueL,falseL);
  6554. IF p1 = NIL THEN val := AssertTrap
  6555. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6556. END;
  6557. SetLabel(falseL);
  6558. EmitTrap(position,val);
  6559. SetLabel(trueL);
  6560. END;
  6561. (*
  6562. Emit(TrapC(result.op,val);
  6563. *)
  6564. (* ---- INC, DEC----- *)
  6565. |Global.Inc,Global.Dec:
  6566. Expression(p0); adr := result.op;
  6567. LoadValue(result,p0.type); l := result;
  6568. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6569. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6570. END;
  6571. IF x.id = Global.Inc THEN
  6572. Emit(Add(position,l.op,l.op,r.op));
  6573. ELSE
  6574. Emit(Sub(position,l.op,l.op,r.op));
  6575. END;
  6576. ReleaseOperand(l); ReleaseOperand(r);
  6577. (* ---- LEN ----- *)
  6578. |Global.Len: (* dynamic length, static length done by checker *)
  6579. Designate(p0,operand);
  6580. IF p1 = NIL THEN
  6581. InitOperand(l,ModeValue);
  6582. l.op := IntermediateCode.Immediate(int32,0);
  6583. ELSE
  6584. Evaluate(p1,l);
  6585. END;
  6586. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6587. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6588. Dereference(operand, p0.type.resolved, FALSE);
  6589. END;
  6590. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6591. ReleaseOperand(operand); ReleaseOperand(l);
  6592. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6593. ASSERT(p1 # NIL);
  6594. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6595. Dereference(operand,p0.type.resolved,FALSE);
  6596. END;
  6597. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6598. ReleaseOperand(operand); ReleaseOperand(l);
  6599. ELSE HALT(100);
  6600. END;
  6601. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6602. (* ---- FIRST ---- *)
  6603. |Global.First:
  6604. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6605. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6606. ELSE
  6607. Designate(p0, result)
  6608. END
  6609. (* ---- LAST ---- *)
  6610. |Global.Last:
  6611. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6612. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6613. ELSE
  6614. Designate(p0, result);
  6615. (* make sure result.op is a register *)
  6616. tmp := result.op;
  6617. ReuseCopy(result.op, result.op);
  6618. ReleaseIntermediateOperand(tmp);
  6619. (* add offset to result.op *)
  6620. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6621. END
  6622. (* ---- STEP ---- *)
  6623. |Global.Step:
  6624. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6625. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6626. ELSE
  6627. Designate(p0, result);
  6628. (* make sure result.op is a register *)
  6629. tmp := result.op;
  6630. ReuseCopy(result.op, result.op);
  6631. ReleaseIntermediateOperand(tmp);
  6632. (* add offset to result.op *)
  6633. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6634. END
  6635. (* ---- RE ---- *)
  6636. |Global.Re:
  6637. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6638. Designate(p0, result)
  6639. ELSE
  6640. Evaluate(p0, result)
  6641. END
  6642. (* ---- IM ---- *)
  6643. |Global.Im:
  6644. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6645. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6646. Designate(p0, result);
  6647. (* make sure result.op is a register *)
  6648. tmp := result.op;
  6649. ReuseCopy(result.op, result.op);
  6650. ReleaseIntermediateOperand(tmp);
  6651. (* add offset to result.op *)
  6652. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6653. (* ---- ABS ----- *)
  6654. |Global.Abs:
  6655. Evaluate(p0,operand);
  6656. type := p0.type.resolved;
  6657. InitOperand(result,ModeValue);
  6658. Reuse1a(result.op,operand.op,dest);
  6659. Emit(Abs(position,result.op,operand.op));
  6660. ReleaseOperand(operand);
  6661. (* ---- WAIT ----- *)
  6662. |Global.Wait:
  6663. Evaluate(p0,operand);
  6664. Emit(Push(position,operand.op));
  6665. ReleaseOperand(operand);
  6666. CallThis(position,"Activities","Wait", 1);
  6667. (* ---- NEW ----- *)
  6668. |Global.New:
  6669. (*! the following code is only correct for "standard" Oberon calling convention *)
  6670. type := p0.type.resolved;
  6671. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6672. THEN
  6673. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6674. IF backend.cooperative THEN
  6675. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6676. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6677. IF recordType.isObject THEN
  6678. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6679. IF recordType.IsActive() THEN
  6680. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6681. END;
  6682. IF recordType.IsProtected() THEN
  6683. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6684. END;
  6685. ELSE
  6686. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6687. END;
  6688. END;
  6689. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6690. CallThis(position,"Runtime","New", 1);
  6691. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6692. Emit(Result(position, pointer));
  6693. exit := NewLabel();
  6694. BreqL(exit,pointer,nil);
  6695. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6696. GetRecordTypeName (recordType,name);
  6697. IF ~recordType.isObject THEN
  6698. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6699. END;
  6700. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6701. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6702. IF recordType.isObject THEN
  6703. IF recordType.IsProtected() THEN
  6704. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6705. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6706. END;
  6707. IF recordType.IsActive() THEN
  6708. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6709. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6710. END;
  6711. END;
  6712. END;
  6713. (* initialize fields *)
  6714. IF type(SyntaxTree.PointerType).isPlain THEN
  6715. size := 0;
  6716. ELSIF recordType.isObject THEN
  6717. size := BaseObjectTypeSize;
  6718. ELSE
  6719. size := BaseRecordTypeSize;
  6720. END;
  6721. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6722. (* call initializer *)
  6723. constructor := GetConstructor(recordType);
  6724. IF constructor # NIL THEN
  6725. (*! should be unified with ProcedureCallDesignator *)
  6726. Emit(Push(position,pointer));
  6727. ReleaseIntermediateOperand(pointer);
  6728. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6729. FOR i := 1 TO x.parameters.Length()-1 DO
  6730. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6731. formalParameter := formalParameter.nextParameter;
  6732. END;
  6733. (* static call of the constructor *)
  6734. GetCodeSectionNameForSymbol(constructor,name);
  6735. ASSERT(~constructor.isInline);
  6736. IF constructor.scope.ownerModule # module.module THEN
  6737. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6738. ELSE
  6739. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6740. END;
  6741. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6742. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6743. Emit(Pop(position,pointer));
  6744. END;
  6745. (* call bodies *)
  6746. CallBodies(pointer,p0.type);
  6747. SetLabel(exit);
  6748. needsTrace := p0.NeedsTrace();
  6749. IF needsTrace THEN ModifyAssignments(true) END;
  6750. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6751. Emit(Push(position, pointer));
  6752. CallThis(position,"GarbageCollector","Watch",0);
  6753. Emit(Pop(position, pointer));
  6754. END;
  6755. Designate(p0,l);
  6756. IF needsTrace THEN
  6757. CallAssignPointer(l.op, pointer);
  6758. ELSE
  6759. ToMemory(l.op,addressType,0);
  6760. Emit(Mov(position,l.op,pointer));
  6761. END;
  6762. ReleaseIntermediateOperand(pointer);
  6763. ReleaseOperand(l);
  6764. IF needsTrace THEN ModifyAssignments(false) END;
  6765. ELSE
  6766. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6767. IF temporaryVariable # NIL THEN
  6768. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6769. ELSE
  6770. Designate(p0,l);
  6771. END;
  6772. (* l.op contains address of pointer to record *)
  6773. Emit(Push(position,l.op)); (* address for use after syscall *)
  6774. Emit(Push(position,l.op));
  6775. ReleaseOperand(l);
  6776. (* push type descriptor *)
  6777. reg := TypeDescriptorAdr(recordType);
  6778. IF ~newObjectFile THEN
  6779. IntermediateCode.MakeMemory(reg,addressType);
  6780. END;
  6781. Emit(Push(position,reg));
  6782. ReleaseIntermediateOperand(reg);
  6783. (* push realtime flag *)
  6784. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6785. ELSE Emit(Push(position,false));
  6786. END;
  6787. CallThis(position,"Heaps","NewRec", 3);
  6788. (* check allocation success, if not successful then do not call initializers and bodies *)
  6789. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6790. Emit(Pop(position,pointer));
  6791. MakeMemory(reg,pointer,addressType,0);
  6792. ReleaseIntermediateOperand(pointer);
  6793. pointer := reg;
  6794. exit := NewLabel();
  6795. BreqL(exit,pointer,nil);
  6796. Emit(Push(position,pointer));
  6797. (* initialize fields *)
  6798. InitFields(recordType, pointer,0);
  6799. (* call initializer *)
  6800. constructor := GetConstructor(recordType);
  6801. IF constructor # NIL THEN
  6802. (*! should be unified with ProcedureCallDesignator *)
  6803. Emit(Push(position,pointer));
  6804. ReleaseIntermediateOperand(pointer);
  6805. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6806. FOR i := 1 TO x.parameters.Length()-1 DO
  6807. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6808. formalParameter := formalParameter.nextParameter;
  6809. END;
  6810. (* static call of the constructor *)
  6811. GetCodeSectionNameForSymbol(constructor,name);
  6812. ASSERT(~constructor.isInline);
  6813. IF constructor.scope.ownerModule # module.module THEN
  6814. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6815. ELSE
  6816. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6817. END;
  6818. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6819. ELSE
  6820. ReleaseIntermediateOperand(pointer);
  6821. END;
  6822. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6823. Emit(Pop(position,pointer));
  6824. IF temporaryVariable # NIL THEN
  6825. Designate(p0,l);
  6826. ToMemory(l.op,addressType,0);
  6827. Emit(Mov(position,l.op,pointer));
  6828. ReleaseOperand(l);
  6829. result.tag := emptyOperand;
  6830. END;
  6831. (* call bodies *)
  6832. CallBodies(pointer,p0.type);
  6833. ReleaseIntermediateOperand(pointer);
  6834. IF temporaryVariable # NIL THEN
  6835. end := NewLabel();
  6836. BrL(end);
  6837. SetLabel(exit);
  6838. Designate(p0,l);
  6839. ToMemory(l.op,addressType,0);
  6840. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6841. ReleaseOperand(l);
  6842. SetLabel(end);
  6843. ELSE
  6844. SetLabel(exit);
  6845. END;
  6846. END;
  6847. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6848. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6849. dim := 0;
  6850. IF p1 # NIL THEN
  6851. FOR i := 1 TO x.parameters.Length()-1 DO
  6852. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6853. parameter := x.parameters.GetExpression(i);
  6854. Evaluate(parameter,r);
  6855. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6856. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6857. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6858. END;
  6859. Emit(Push(position,r.op));
  6860. IF i=1 THEN
  6861. ReuseCopy(reg,r.op);
  6862. ELSE
  6863. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6864. END;
  6865. ReleaseOperand(r);
  6866. INC(dim);
  6867. END;
  6868. Convert(reg,addressType);
  6869. ELSE
  6870. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6871. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6872. END;
  6873. openDim := dim;
  6874. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6875. IF backend.cooperative THEN
  6876. size := ToMemoryUnits(system,system.SizeOf(type));
  6877. WHILE type IS SyntaxTree.ArrayType DO
  6878. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6879. END;
  6880. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6881. IF (size # 1) THEN
  6882. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6883. END;
  6884. Emit(Push(position,reg));
  6885. size := ToMemoryUnits(system,system.SizeOf(type));
  6886. IF (size # 1) THEN
  6887. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6888. END;
  6889. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6890. Emit(Push(position,reg));
  6891. ReleaseIntermediateOperand(reg);
  6892. CallThis(position,"Runtime","New", 1);
  6893. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6894. Emit(Result(position, pointer));
  6895. exit := NewLabel();
  6896. else := NewLabel();
  6897. BreqL(else,pointer,nil);
  6898. IF ~type.hasPointers THEN
  6899. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6900. ELSIF type IS SyntaxTree.RecordType THEN
  6901. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6902. ELSIF type IS SyntaxTree.ProcedureType THEN
  6903. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6904. ELSE
  6905. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6906. END;
  6907. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6908. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6909. 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))));
  6910. IF type IS SyntaxTree.RecordType THEN
  6911. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6912. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6913. ELSE
  6914. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6915. END;
  6916. i := openDim;
  6917. WHILE i > 0 DO
  6918. DEC (i);
  6919. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6920. END;
  6921. needsTrace := p0.NeedsTrace();
  6922. IF needsTrace THEN ModifyAssignments(true) END;
  6923. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6924. Emit(Push(position, pointer));
  6925. CallThis(position,"GarbageCollector","Watch",0);
  6926. Emit(Pop(position, pointer));
  6927. END;
  6928. Designate(p0,l);
  6929. IF needsTrace THEN
  6930. CallAssignPointer(l.op, pointer);
  6931. ModifyAssignments(false);
  6932. ELSE
  6933. ToMemory(l.op,addressType,0);
  6934. Emit(Mov(position,l.op,pointer));
  6935. END;
  6936. ReleaseIntermediateOperand(pointer);
  6937. ReleaseOperand(l);
  6938. BrL(exit);
  6939. SetLabel(else);
  6940. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6941. Designate(p0,l);
  6942. IF needsTrace THEN
  6943. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6944. ELSE
  6945. ToMemory(l.op,addressType,0);
  6946. Emit(Mov(position,l.op,pointer));
  6947. END;
  6948. ReleaseOperand(l);
  6949. SetLabel(exit);
  6950. ELSE
  6951. (*! the following code is only correct for "standard" Oberon calling convention *)
  6952. IF SemanticChecker.ContainsPointer(type) THEN
  6953. IF type IS SyntaxTree.ArrayType THEN
  6954. staticLength := 1;
  6955. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6956. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6957. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6958. END;
  6959. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6960. Emit(Mul(position,reg,reg,tmp));
  6961. END;
  6962. Designate(p0,l);
  6963. IF openDim > 0 THEN
  6964. Emit(Push(position,l.op)); (* address for use after syscall *)
  6965. END;
  6966. Emit(Push(position,l.op)); (* address *)
  6967. ReleaseOperand(l);
  6968. tmp := TypeDescriptorAdr(type);
  6969. IF ~newObjectFile THEN
  6970. IntermediateCode.MakeMemory(tmp,addressType);
  6971. END;
  6972. Emit(Push(position,tmp)); (* type descriptor *)
  6973. ReleaseIntermediateOperand(tmp);
  6974. Emit(Push(position,reg)); (* number Elements *)
  6975. ReleaseIntermediateOperand(reg);
  6976. tmp := IntermediateCode.Immediate(addressType,dim);
  6977. Emit(Push(position,tmp)); (* dimensions *)
  6978. (* push realtime flag *)
  6979. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6980. ELSE Emit(Push(position,false));
  6981. END;
  6982. CallThis(position,"Heaps","NewArr",5)
  6983. ELSE
  6984. size := ToMemoryUnits(system,system.SizeOf(type));
  6985. IF (size # 1) THEN
  6986. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6987. END;
  6988. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  6989. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  6990. Emit(Add(position,reg,reg,tmp));
  6991. Designate(p0,l);
  6992. IF openDim >0 THEN
  6993. Emit(Push(position,l.op)); (* address for use after syscall *)
  6994. END;
  6995. Emit(Push(position,l.op)); (* address for syscall *)
  6996. ReleaseOperand(l); (* pointer address *)
  6997. Emit(Push(position,reg)); (* size *)
  6998. ReleaseIntermediateOperand(reg);
  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","NewSys", 3)
  7004. END;
  7005. IF openDim > 0 THEN
  7006. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7007. Emit(Pop(position,adr));
  7008. ToMemory(adr,addressType,0);
  7009. ReuseCopy(tmp,adr);
  7010. ReleaseIntermediateOperand(adr);
  7011. adr := tmp;
  7012. else := NewLabel();
  7013. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7014. i := openDim-1;
  7015. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7016. WHILE (i >= 0) DO
  7017. Emit(Pop(position,reg));
  7018. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7019. Emit(Mov(position,res,reg));
  7020. DEC(i);
  7021. END;
  7022. ReleaseIntermediateOperand(adr);
  7023. ReleaseIntermediateOperand(reg);
  7024. exit := NewLabel();
  7025. BrL(exit);
  7026. SetLabel(else);
  7027. (* else part: array could not be allocated *)
  7028. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7029. Emit(Add(position,sp,sp,tmp));
  7030. SetLabel(exit);
  7031. END;
  7032. END;
  7033. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7034. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7035. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7036. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7037. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7038. left.SetType(procedure.type);
  7039. formalParameter := procedureType.firstParameter;
  7040. (* push array to allocate *)
  7041. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7042. formalParameter :=formalParameter.nextParameter;
  7043. (* push length array *)
  7044. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7045. (* push size *)
  7046. type := t0;
  7047. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7048. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7049. END;
  7050. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7051. Emit(Push(position,tmp));
  7052. (* *)
  7053. IF SemanticChecker.ContainsPointer(type) THEN
  7054. tmp := TypeDescriptorAdr(type);
  7055. IF ~newObjectFile THEN
  7056. IntermediateCode.MakeMemory(tmp,addressType);
  7057. END;
  7058. ELSE
  7059. tmp := IntermediateCode.Immediate(addressType, 0);
  7060. END;
  7061. Emit(Push(position,tmp)); (* type descriptor *)
  7062. StaticCallOperand(result,procedure);
  7063. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7064. ReleaseOperand(result);
  7065. END;
  7066. (*
  7067. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7068. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7069. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7070. *)
  7071. ELSE
  7072. dim := 0;
  7073. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7074. (* generate geometry descriptor *)
  7075. Designate(p0,l);
  7076. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7077. ReleaseOperand(l);
  7078. isTensor := TRUE;
  7079. ELSE
  7080. isTensor := FALSE;
  7081. END;
  7082. FOR i := 1 TO x.parameters.Length()-1 DO
  7083. IF ~isTensor THEN
  7084. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7085. END;
  7086. parameter := x.parameters.GetExpression(i);
  7087. Evaluate(parameter,r);
  7088. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7089. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7090. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7091. END;
  7092. Emit(Push(position,r.op));
  7093. IF i=1 THEN
  7094. ReuseCopy(reg,r.op);
  7095. ELSE
  7096. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7097. END;
  7098. ReleaseOperand(r);
  7099. INC(dim);
  7100. END;
  7101. Convert(reg,addressType);
  7102. openDim := dim;
  7103. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7104. (*! the following code is only correct for "standard" Oberon calling convention *)
  7105. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7106. t := type;
  7107. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7108. staticLength := 1;
  7109. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7110. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7111. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7112. END;
  7113. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7114. Emit(Mul(position,reg,reg,tmp));
  7115. END;
  7116. Designate(p0,l);
  7117. IF isTensor THEN
  7118. Dereference(l,type,FALSE);
  7119. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7120. END;
  7121. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7122. Emit(Push(position,l.tag)); (* address *)
  7123. ReleaseOperand(l);
  7124. tmp := TypeDescriptorAdr(t);
  7125. IF ~newObjectFile THEN
  7126. IntermediateCode.MakeMemory(tmp,addressType);
  7127. END;
  7128. Emit(Push(position,tmp)); (* type descriptor *)
  7129. ReleaseIntermediateOperand(tmp);
  7130. Emit(Push(position,reg)); (* number Elements *)
  7131. ReleaseIntermediateOperand(reg);
  7132. tmp := IntermediateCode.Immediate(addressType,0);
  7133. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7134. (* push realtime flag: false by default *)
  7135. Emit(Push(position,false));
  7136. CallThis(position,"Heaps","NewArr",5);
  7137. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7138. Emit(Pop(position,adr));
  7139. GetMathArrayField(tmp,adr,MathPtrOffset);
  7140. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7141. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7142. PutMathArrayField(adr,reg,MathAdrOffset);
  7143. ReleaseIntermediateOperand(tmp);
  7144. ReleaseIntermediateOperand(reg);
  7145. ELSE
  7146. IF isTensor THEN
  7147. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7148. ELSE
  7149. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7150. END;
  7151. IF (size # 1) THEN
  7152. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7153. END;
  7154. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7155. Emit(Add(position,reg,reg,tmp));
  7156. Designate(p0,l);
  7157. IF isTensor THEN
  7158. Dereference(l,type,FALSE);
  7159. END;
  7160. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7161. Emit(Push(position,l.tag)); (* address for syscall *)
  7162. ReleaseOperand(l); (* pointer address *)
  7163. Emit(Push(position,reg)); (* size *)
  7164. ReleaseIntermediateOperand(reg);
  7165. (* push realtime flag: false by default *)
  7166. Emit(Push(position,false));
  7167. CallThis(position,"Heaps","NewSys",3);
  7168. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7169. Emit(Pop(position,adr));
  7170. GetMathArrayField(tmp,adr,MathPtrOffset);
  7171. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7172. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7173. PutMathArrayField(adr,reg,MathAdrOffset);
  7174. ReleaseIntermediateOperand(tmp);
  7175. ReleaseIntermediateOperand(reg);
  7176. END;
  7177. flags := {};
  7178. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7179. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7180. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7181. PutMathArrayField(adr,tmp,MathDimOffset);
  7182. else := NewLabel();
  7183. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7184. i := openDim-1;
  7185. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7186. IF isTensor THEN
  7187. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7188. ELSE
  7189. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7190. END;
  7191. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7192. WHILE (i >= 0) DO
  7193. Emit(Pop(position,reg));
  7194. PutMathArrayLength(adr,reg,i);
  7195. PutMathArrayIncrement(adr,tmp,i);
  7196. IF i > 0 THEN
  7197. IF i=openDim-1 THEN
  7198. ReuseCopy(tmp,tmp);
  7199. END;
  7200. Emit(Mul(position,tmp,tmp,reg));
  7201. END;
  7202. DEC(i);
  7203. END;
  7204. ReleaseIntermediateOperand(adr);
  7205. ReleaseIntermediateOperand(reg);
  7206. ReleaseIntermediateOperand(tmp);
  7207. exit := NewLabel();
  7208. BrL(exit);
  7209. SetLabel(else);
  7210. (* else part: array could not be allocated *)
  7211. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7212. Emit(Add(position,sp,sp,tmp));
  7213. SetLabel(exit);
  7214. END;
  7215. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7216. THEN
  7217. IF ~backend.cellsAreObjects THEN RETURN END;
  7218. IF InCellScope(currentScope) THEN
  7219. PushSelfPointer()
  7220. ELSE
  7221. Emit(Push(position, nil));
  7222. END;
  7223. (* push temp address *)
  7224. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7225. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7226. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7227. (* l.op contains address of pointer to record *)
  7228. Emit(Push(position,l.op)); (* address for use after syscall *)
  7229. ReleaseOperand(l);
  7230. (* push type descriptor *)
  7231. reg := TypeDescriptorAdr(baseType);
  7232. IF ~newObjectFile THEN
  7233. IntermediateCode.MakeMemory(reg,addressType);
  7234. END;
  7235. Emit(Push(position,reg));
  7236. ReleaseIntermediateOperand(reg);
  7237. (* push name *)
  7238. (*Global.GetSymbolName(p0, n);*)
  7239. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7240. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7241. ELSE
  7242. Global.GetModuleName(module.module, n);
  7243. END;
  7244. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7245. (*type.typeDeclaration.GetName(n);*)
  7246. PushConstString(n);
  7247. (* push cellnet boolean *)
  7248. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7249. (* push engine boolean *)
  7250. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7251. (* allocate *)
  7252. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7253. (* add capabilities *)
  7254. modifier := p0(SyntaxTree.Designator).modifiers;
  7255. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7256. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7257. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7258. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7259. END;
  7260. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7261. (*
  7262. modifier := baseType(SyntaxTree.CellType).modifiers;
  7263. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7264. modifier := p0(SyntaxTree.Designator).modifiers;
  7265. *)
  7266. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7267. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7268. (* l.op contains address of pointer to record *)
  7269. ToMemory(l.op,addressType,0);
  7270. (* l.op contains value of pointer to record *)
  7271. Emit(Push(position,l.op)); (* address for use after syscall *)
  7272. ReleaseOperand(l);
  7273. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7274. prevScope := currentScope;
  7275. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7276. previous := section;
  7277. section := init;
  7278. (* add ports *)
  7279. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7280. CloseInitializer(previous);
  7281. currentScope := prevScope;
  7282. Symbol(temporaryVariable,l);
  7283. ToMemory(l.op,addressType,0);
  7284. Emit(Push(position,l.op));
  7285. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7286. (*
  7287. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7288. IF constructor # NIL THEN
  7289. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7290. FOR i := 1 TO x.parameters.Length()-1 DO
  7291. p := x.parameters.GetExpression(i);
  7292. Global.GetSymbolName(parameter,name);
  7293. Evaluate(p, value);
  7294. ASSERT(value.type # NIL);
  7295. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7296. par := instance.AddParameter(name);
  7297. par.SetInteger(value.integer);
  7298. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7299. par := instance.AddParameter(name);
  7300. par.SetBoolean(value.boolean);
  7301. ELSE Error(x.position,NotYetImplemented)
  7302. END;
  7303. parameter := parameter.nextParameter
  7304. END;
  7305. END;
  7306. *)
  7307. (* call initializer *)
  7308. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7309. IF constructor # NIL THEN
  7310. (*! should be unified with ProcedureCallDesignator *)
  7311. IF backend.cellsAreObjects THEN
  7312. Symbol(temporaryVariable,l);
  7313. ToMemory(l.op,addressType,0);
  7314. Emit(Push(position,l.op));
  7315. ReleaseOperand(l);
  7316. END;
  7317. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7318. FOR i := 1 TO x.parameters.Length()-1 DO
  7319. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7320. formalParameter := formalParameter.nextParameter;
  7321. END;
  7322. (* static call of the constructor *)
  7323. Global.GetSymbolSegmentedName(constructor,name);
  7324. ASSERT(~constructor.isInline);
  7325. IF constructor.scope.ownerModule # module.module THEN
  7326. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7327. ELSE
  7328. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7329. END;
  7330. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7331. (*ELSE
  7332. ReleaseIntermediateOperand(pointer);*)
  7333. END;
  7334. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7335. ToMemory(l.op, addressType, 0);
  7336. Designate(p0,s0);
  7337. ToMemory(s0.op,addressType,0);
  7338. Emit(Mov(position,s0.op,l.op));
  7339. ReleaseOperand(l);
  7340. ReleaseOperand(s0);
  7341. result.tag := emptyOperand;
  7342. (* start *)
  7343. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7344. (* push cell *)
  7345. Symbol(temporaryVariable, l);
  7346. ToMemory(l.op,addressType,0);
  7347. Emit(Push(-1,l.op));
  7348. (* push delegate *)
  7349. Emit(Push(-1,l.op));
  7350. ReleaseOperand(l);
  7351. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7352. Emit(Push(position, s1.op));
  7353. ReleaseOperand(s1);
  7354. CallThis(position,"ActiveCellsRuntime","Start",3);
  7355. END;
  7356. (*IF temporaryVariable # NIL THEN
  7357. end := NewLabel();
  7358. BrL(end);
  7359. SetLabel(exit);
  7360. Designate(p0,l);
  7361. ToMemory(l.op,addressType,0);
  7362. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7363. ReleaseOperand(l);
  7364. SetLabel(end);
  7365. ELSE
  7366. SetLabel(exit);
  7367. END;
  7368. *)
  7369. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7370. ELSE (* no pointer to record, no pointer to array *)
  7371. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7372. (* ignore new statement *)
  7373. Warning(p0.position, "cannot run on final hardware");
  7374. ELSE
  7375. HALT(200);
  7376. END;
  7377. END;
  7378. (* ---- ADDRESSOF----- *)
  7379. |Global.systemAdr:
  7380. Designate(p0,s0);
  7381. s0.mode := ModeValue;
  7382. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7383. ReleaseIntermediateOperand(s0.op);
  7384. s0.op := s0.tag;
  7385. IntermediateCode.InitOperand(s0.tag);
  7386. END;
  7387. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7388. result := s0;
  7389. (* ---- BIT ----- *)
  7390. |Global.systemBit:
  7391. Evaluate(p0,s0);
  7392. ToMemory(s0.op,addressType,0);
  7393. ReuseCopy(res,s0.op);
  7394. ReleaseOperand(s0);
  7395. Evaluate(p1,s1);
  7396. Emit(Ror(position,res,res,s1.op));
  7397. ReleaseOperand(s1);
  7398. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7399. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7400. IF ~conditional THEN
  7401. InitOperand(result,ModeValue); result.op := res;
  7402. ELSE
  7403. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7404. BrL(falseLabel);
  7405. ReleaseIntermediateOperand(res);
  7406. END;
  7407. (* --- MSK ----*)
  7408. |Global.systemMsk:
  7409. Evaluate(p0,s0);
  7410. Evaluate(p1,s1);
  7411. ReuseCopy(res,s0.op);
  7412. ReleaseOperand(s0);
  7413. Emit(And(position,res,res,s1.op));
  7414. ReleaseOperand(s1);
  7415. InitOperand(result,ModeValue);
  7416. result.op := res;
  7417. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7418. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7419. Evaluate(p0,s0);
  7420. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7421. ReleaseOperand(s0);
  7422. InitOperand(result,ModeValue);
  7423. result.op := res;
  7424. (* ---- SYSTEM.GetStackPointer ----- *)
  7425. |Global.systemGetStackPointer:
  7426. InitOperand(result,ModeValue);
  7427. result.op := sp;
  7428. (* ---- SYSTEM.GetFramePointer ----- *)
  7429. |Global.systemGetFramePointer:
  7430. InitOperand(result,ModeValue);
  7431. result.op := fp;
  7432. (* ---- SYSTEM.GetActivity ----- *)
  7433. |Global.systemGetActivity:
  7434. ASSERT(backend.cooperative);
  7435. InitOperand(result,ModeValue);
  7436. result.op := ap;
  7437. (* ---- SYSTEM.SetStackPointer ----- *)
  7438. |Global.systemSetStackPointer:
  7439. Evaluate(p0,s0); (* *)
  7440. Emit(Mov(position,sp,s0.op));
  7441. ReleaseOperand(s0);
  7442. (* ---- SYSTEM.SetFramePointer ----- *)
  7443. |Global.systemSetFramePointer:
  7444. Evaluate(p0,s0); (* *)
  7445. Emit(Mov(position,fp,s0.op));
  7446. ReleaseOperand(s0);
  7447. (* ---- SYSTEM.Activity ----- *)
  7448. |Global.systemSetActivity:
  7449. ASSERT(backend.cooperative);
  7450. Evaluate(p0,s0); (* *)
  7451. Emit(Mov(position,ap,s0.op));
  7452. ReleaseOperand(s0);
  7453. (* ---- SYSTEM.VAL ----- *)
  7454. |Global.systemVal:
  7455. Expression(p1);
  7456. s1 := result;
  7457. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7458. IF s1.mode = ModeReference THEN
  7459. (* nothing to be done if not record type, just take over new type *)
  7460. IF (type IS SyntaxTree.RecordType) THEN
  7461. ReleaseIntermediateOperand(s1.tag);
  7462. s1.tag := TypeDescriptorAdr(type);
  7463. IF ~newObjectFile THEN
  7464. IntermediateCode.MakeMemory(s1.tag,addressType);
  7465. END;
  7466. UseIntermediateOperand(s1.tag);
  7467. END;
  7468. result := s1;
  7469. ELSE (* copy over result to different type, may not use convert *)
  7470. itype := IntermediateCode.GetType(system,type);
  7471. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7472. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7473. Emit(Mov(position,s0.op,s1.op));
  7474. ReleaseOperand(s1);
  7475. InitOperand(result,ModeValue);
  7476. result.op := s0.op;
  7477. ELSE (* different size, must convert *)
  7478. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7479. Convert(s1.op, IntermediateCode.GetType(system,type));
  7480. result := s1;
  7481. END;
  7482. END;
  7483. (* ---- SYSTEM.GET ----- *)
  7484. |Global.systemGet:
  7485. Evaluate(p0,s0); (* adr *)
  7486. Designate(p1,s1); (* variable *)
  7487. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7488. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7489. Emit(Mov(position,s1.op,s0.op));
  7490. ReleaseOperand(s1);
  7491. ReleaseOperand(s0);
  7492. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7493. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7494. Evaluate(p0,s0); (* *)
  7495. Evaluate(p1,s1); (* variable *)
  7496. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7497. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7498. (* real part *)
  7499. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7500. Emit(Mov(position,res, s1.op));
  7501. ReleaseIntermediateOperand(res);
  7502. (* imaginary part *)
  7503. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7504. Emit(Mov(position,res, s1.tag));
  7505. ReleaseIntermediateOperand(res);
  7506. ReleaseOperand(s1);
  7507. ReleaseOperand(s0);
  7508. ELSE
  7509. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7510. ReleaseOperand(s0);
  7511. Emit(Mov(position,res,s1.op));
  7512. ReleaseIntermediateOperand(res);
  7513. ReleaseOperand(s1);
  7514. END;
  7515. (* ---- SYSTEM.MOVE ----- *)
  7516. |Global.systemMove:
  7517. Evaluate(p0,s0);
  7518. Evaluate(p1,s1);
  7519. Evaluate(p2,s2);
  7520. Emit(Copy(position,s1.op,s0.op,s2.op));
  7521. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7522. (* ---- SYSTEM.NEW ----- *)
  7523. |Global.systemNew:
  7524. Designate(p0,s0);
  7525. Emit(Push(position,s0.op));
  7526. ReleaseOperand(s0);
  7527. Evaluate(p1,s1);
  7528. Emit(Push(position,s1.op));
  7529. ReleaseOperand(s1);
  7530. (* push realtime flag: false by default *)
  7531. Emit(Push(position,false));
  7532. CallThis(position,"Heaps","NewSys",3);
  7533. (* ---- SYSTEM.CALL ----- *)
  7534. |Global.systemRef:
  7535. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7536. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7537. s0.mode := ModeValue;
  7538. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7539. result := s0
  7540. (* ---- INCR ----- *)
  7541. |Global.Incr:
  7542. Designate(p0,operand);
  7543. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7544. Dereference(operand,p0.type.resolved,FALSE);
  7545. END;
  7546. ASSERT(p1 # NIL);
  7547. Evaluate(p1,l);
  7548. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7549. ReleaseOperand(operand); ReleaseOperand(l);
  7550. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7551. (* ---- SUM ----- *)
  7552. |Global.Sum: HALT(200);
  7553. (* ---- CAS ----- *)
  7554. |Global.Cas:
  7555. needsTrace := p0.NeedsTrace();
  7556. IF needsTrace THEN ModifyAssignments(true) END;
  7557. Designate(p0,s0);
  7558. Evaluate(p1,s1);
  7559. Evaluate(p2,s2);
  7560. IF needsTrace THEN
  7561. Emit(Push(position, s0.op));
  7562. Emit(Push(position, s1.op));
  7563. Emit(Push(position, s2.op));
  7564. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7565. ELSE
  7566. Emit(Cas(position,s0.op,s1.op,s2.op));
  7567. END;
  7568. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7569. IF needsTrace THEN ModifyAssignments(false) END;
  7570. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7571. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7572. IF conditional THEN
  7573. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7574. BrL(falseLabel);
  7575. ReleaseIntermediateOperand(res);
  7576. END;
  7577. (* ---- DIM ----- *)
  7578. |Global.Dim:
  7579. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7580. Designate(p0,s0);
  7581. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7582. Dereference(s0,p0.type.resolved,FALSE);
  7583. END;
  7584. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7585. ReleaseOperand(s0);
  7586. (* ---- RESHAPE ----- *)
  7587. |Global.Reshape:
  7588. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7589. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7590. left.SetType(procedure.type);
  7591. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7592. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7593. END;
  7594. (* ---- SYSTEM.TYPECODE ----- *)
  7595. |Global.systemTypeCode:
  7596. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7597. IF type.resolved IS SyntaxTree.PointerType THEN
  7598. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7599. END;
  7600. result.op := TypeDescriptorAdr(type);
  7601. IF ~newObjectFile THEN
  7602. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7603. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7604. END;
  7605. result.mode := ModeValue;
  7606. (* ---- SYSTEM.TRACE ----- *)
  7607. |Global.systemTrace:
  7608. SystemTrace(x.parameters, x.position);
  7609. (* ----- CONNECT ------*)
  7610. |Global.Connect:
  7611. IF backend.cellsAreObjects THEN
  7612. PushPort(p0);
  7613. PushPort(p1);
  7614. IF p2 # NIL THEN
  7615. Evaluate(p2, s2);
  7616. Emit(Push(p2.position, s2.op));
  7617. ReleaseOperand(s2);
  7618. ELSE
  7619. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7620. END;
  7621. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7622. ELSE
  7623. Warning(x.position, "cannot run on final hardware");
  7624. END;
  7625. (* ----- DELEGATE ------*)
  7626. |Global.Delegate:
  7627. IF backend.cellsAreObjects THEN
  7628. PushPort(p0);
  7629. PushPort(p1);
  7630. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7631. ELSE
  7632. Warning(x.position, "cannot run on final hardware");
  7633. END;
  7634. (* ----- SEND ------*)
  7635. |Global.Send:
  7636. Evaluate(p0,s0);
  7637. Evaluate(p1,s1);
  7638. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7639. Emit(Push(position,s0.op));
  7640. Emit(Push(position,s1.op));
  7641. (*
  7642. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7643. *)
  7644. IF ~backend.cellsAreObjects THEN
  7645. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7646. END;
  7647. ReleaseOperand(s0);
  7648. ReleaseOperand(s1);
  7649. IF backend.cellsAreObjects THEN
  7650. CallThis(position,"ActiveCellsRuntime","Send",2);
  7651. ELSE
  7652. CallThis(position,ChannelModuleName,"Send",2);
  7653. END;
  7654. (* ----- RECEIVE ------*)
  7655. |Global.Receive:
  7656. Evaluate(p0,s0);
  7657. Emit(Push(position,s0.op));
  7658. Designate(p1,s1);
  7659. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7660. Emit(Push(position,s1.op));
  7661. IF p2 # NIL THEN
  7662. Designate(p2,s2);
  7663. Emit(Push(position,s2.op));
  7664. END;
  7665. (*
  7666. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7667. *)
  7668. IF ~backend.cellsAreObjects THEN
  7669. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7670. END;
  7671. ReleaseOperand(s0);
  7672. ReleaseOperand(s1);
  7673. ReleaseOperand(s2);
  7674. IF backend.cellsAreObjects THEN
  7675. IF p2 = NIL THEN
  7676. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7677. ELSE
  7678. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7679. END;
  7680. ELSE
  7681. IF p2 = NIL THEN
  7682. CallThis(position,ChannelModuleName,"Receive",2)
  7683. ELSE
  7684. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7685. END;
  7686. END;
  7687. | Global.systemSpecial:
  7688. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7689. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7690. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7691. (* determine if parameters are of the VAR kind *)
  7692. ASSERT(x.parameters.Length() <= 3);
  7693. formalParameter := procedureType.firstParameter;
  7694. FOR i := 0 TO x.parameters.Length() - 1 DO
  7695. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7696. formalParameter := formalParameter.nextParameter
  7697. END;
  7698. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7699. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7700. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7701. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7702. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7703. IF procedureType.returnType # NIL THEN
  7704. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7705. Emit(Result(position, res));
  7706. (*InitOperand(result,ModeValue);
  7707. result.op := res;
  7708. *)
  7709. IF ~conditional THEN
  7710. InitOperand(result,ModeValue); result.op := res;
  7711. ELSE
  7712. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7713. BrL(falseLabel);
  7714. ReleaseIntermediateOperand(res);
  7715. END;
  7716. END
  7717. ELSE (* function not yet implemented *)
  7718. Error(position,"not yet implemented");
  7719. END;
  7720. destination := dest;
  7721. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7722. END VisitBuiltinCallDesignator;
  7723. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7724. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7725. BEGIN
  7726. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7727. dest := destination; destination := emptyOperand;
  7728. Expression(x.left);
  7729. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7730. ELSIF isUnchecked THEN (* no check *)
  7731. ELSE
  7732. trueL := NewLabel();
  7733. falseL := NewLabel();
  7734. IF IsPointerToRecord(x.left.type,recordType) THEN
  7735. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7736. Emit(Mov(position,tag, result.op));
  7737. IF result.mode # ModeValue THEN
  7738. ptr := tag;
  7739. IntermediateCode.MakeMemory(ptr,addressType);
  7740. Emit(Mov(position,tag, ptr));
  7741. END;
  7742. IF ~backend.cooperative THEN
  7743. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7744. END;
  7745. IntermediateCode.MakeMemory(tag,addressType);
  7746. ELSE
  7747. tag := result.tag;
  7748. END;
  7749. TypeTest(tag,x.type,trueL,falseL);
  7750. ReleaseIntermediateOperand(tag);
  7751. SetLabel(falseL);
  7752. EmitTrap(position,TypeCheckTrap);
  7753. SetLabel(trueL);
  7754. END;
  7755. destination := dest;
  7756. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7757. END VisitTypeGuardDesignator;
  7758. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7759. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7760. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7761. VAR label: Label; pc: LONGINT;
  7762. BEGIN
  7763. IF backend.cooperative & ~isUnchecked THEN
  7764. pc := section.pc;
  7765. label := NewLabel();
  7766. BrneL(label, operand.op, nil);
  7767. EmitTrap(position, NilPointerTrap);
  7768. SetLabel(label);
  7769. INC(statCoopNilCheck, section.pc - pc);
  7770. END;
  7771. END NilCheck;
  7772. BEGIN
  7773. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7774. ReuseCopy(dereferenced,operand.op);
  7775. ReleaseOperand(operand);
  7776. operand.mode := ModeReference;
  7777. operand.op := dereferenced;
  7778. operand.tag := dereferenced;
  7779. UseIntermediateOperand(operand.tag);
  7780. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7781. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7782. ReleaseIntermediateOperand(operand.tag);
  7783. operand.tag := TypeDescriptorAdr(type);
  7784. IF ~newObjectFile THEN
  7785. IntermediateCode.MakeMemory(operand.tag,addressType);
  7786. END;
  7787. ELSE
  7788. IF ~backend.cooperative THEN
  7789. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7790. END;
  7791. IntermediateCode.MakeMemory(operand.tag,addressType);
  7792. END;
  7793. NilCheck(operand.op);
  7794. ELSIF type IS SyntaxTree.ArrayType THEN
  7795. IF isUnsafe THEN
  7796. NilCheck(operand.op);
  7797. ReleaseIntermediateOperand(operand.tag);
  7798. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7799. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7800. ELSE
  7801. operand.tag := emptyOperand;
  7802. END;
  7803. ELSE
  7804. NilCheck(operand.op);
  7805. IF backend.cooperative THEN
  7806. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7807. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7808. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7809. ELSE
  7810. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7811. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7812. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7813. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7814. END;
  7815. END;
  7816. ELSIF type IS SyntaxTree.MathArrayType THEN
  7817. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7818. IntermediateCode.MakeMemory(operand.op,addressType);
  7819. ELSE HALT(100);
  7820. END;
  7821. END Dereference;
  7822. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7823. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7824. BEGIN
  7825. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7826. dest := destination; destination := emptyOperand;
  7827. Evaluate(x.left,d);
  7828. type := x.type.resolved;
  7829. prevIsUnchecked := isUnchecked;
  7830. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7831. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7832. END;
  7833. Dereference(d,type,IsUnsafePointer(x.left.type));
  7834. isUnchecked := prevIsUnchecked;
  7835. result := d;
  7836. 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
  7837. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7838. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7839. END;
  7840. END;
  7841. destination := dest;
  7842. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7843. END VisitDereferenceDesignator;
  7844. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7845. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7846. BEGIN
  7847. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7848. dest := destination; destination := emptyOperand;
  7849. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7850. tag := result.op;
  7851. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7852. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7853. StaticCallOperand(result,procedure.super);
  7854. ReleaseIntermediateOperand(result.tag);
  7855. UseIntermediateOperand(tag); (* necessary ? *)
  7856. result.tag := tag;
  7857. destination := dest;
  7858. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7859. END VisitSupercallDesignator;
  7860. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7861. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7862. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7863. name: Basic.SegmentedName;
  7864. BEGIN
  7865. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7866. dest := destination; destination := emptyOperand;
  7867. scope := currentScope;
  7868. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7869. scope := scope.outerScope;
  7870. END;
  7871. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7872. IF newObjectFile THEN
  7873. moduleSection := meta.ModuleSection();
  7874. IF backend.cooperative THEN
  7875. moduleOffset := 0;
  7876. ELSE
  7877. moduleOffset := moduleSection.pc;
  7878. END;
  7879. result.mode := ModeValue;
  7880. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7881. ELSE
  7882. Symbol(moduleSelf,result);
  7883. IntermediateCode.MakeMemory(result.op,addressType);
  7884. END
  7885. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7886. result.mode := ModeValue;
  7887. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7888. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7889. ELSE
  7890. GetBaseRegister(basereg,currentScope,scope);
  7891. InitOperand(result,ModeReference);
  7892. result.op := basereg;
  7893. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7894. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7895. IF backend.cooperative THEN
  7896. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7897. END;
  7898. (* tag must be loaded when dereferencing SELF pointer *)
  7899. END;
  7900. destination := dest;
  7901. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7902. END VisitSelfDesignator;
  7903. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7904. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7905. BEGIN
  7906. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7907. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7908. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7909. parameter := procedureType.returnParameter;
  7910. VisitParameter(parameter);
  7911. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7912. END VisitResultDesignator;
  7913. (** values *)
  7914. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7915. BEGIN
  7916. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7917. IF conditional THEN
  7918. IF x.value THEN BrL(trueLabel)
  7919. ELSE BrL(falseLabel)
  7920. END;
  7921. ELSE
  7922. InitOperand(result,ModeValue);
  7923. IF x.value THEN result.op := true ELSE result.op := false END;
  7924. END;
  7925. END VisitBooleanValue;
  7926. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7927. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7928. BEGIN
  7929. Global.GetModuleSegmentedName(module.module, name);
  7930. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7931. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7932. RETURN section
  7933. END GetDataSection;
  7934. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7935. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7936. BEGIN
  7937. type := vop.type;
  7938. data := GetDataSection();
  7939. pc := EnterImmediate(data,vop);
  7940. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7941. IntermediateCode.MakeMemory(vop, type);
  7942. END GetImmediateMem;
  7943. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7944. BEGIN
  7945. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7946. InitOperand(result,ModeValue);
  7947. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7948. IF ~supportedImmediate(result.op) &~inData THEN
  7949. GetImmediateMem(result.op)
  7950. END;
  7951. END VisitIntegerValue;
  7952. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7953. BEGIN
  7954. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7955. InitOperand(result,ModeValue);
  7956. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7957. END VisitCharacterValue;
  7958. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7959. BEGIN
  7960. IF Trace THEN TraceEnter("VisitSetValue") END;
  7961. InitOperand(result,ModeValue);
  7962. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7963. END VisitSetValue;
  7964. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7965. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7966. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7967. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7968. BEGIN
  7969. numberElements := x.elements.Length();
  7970. FOR i := 0 TO numberElements-1 DO
  7971. expression := x.elements.GetExpression(i);
  7972. IF expression IS SyntaxTree.MathArrayExpression THEN
  7973. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7974. ELSE
  7975. inData := TRUE;
  7976. Evaluate(expression,op);
  7977. irv.Emit(Data(position,op.op));
  7978. inData := FALSE;
  7979. ReleaseOperand(op);
  7980. END;
  7981. END;
  7982. END RecursiveData;
  7983. BEGIN
  7984. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  7985. IF ~TryConstantDeclaration() THEN
  7986. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  7987. GetCodeSectionNameForSymbol(constantDeclaration,name);
  7988. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  7989. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  7990. ELSE
  7991. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  7992. END;
  7993. RecursiveData(x.array);
  7994. InitOperand(result,ModeReference);
  7995. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  7996. END
  7997. END VisitMathArrayValue;
  7998. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  7999. VAR constant: Sections.Section;
  8000. BEGIN
  8001. IF constantDeclaration = NIL THEN
  8002. RETURN FALSE
  8003. ELSE
  8004. (* Is a constant in this module: did we generate it already? *)
  8005. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  8006. IF constant # NIL THEN
  8007. InitOperand(result,ModeReference);
  8008. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  8009. RETURN TRUE;
  8010. END;
  8011. END;
  8012. RETURN FALSE
  8013. END TryConstantDeclaration;
  8014. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8015. BEGIN
  8016. constantDeclaration := x;
  8017. x.value.resolved.Accept(SELF);
  8018. END VisitConstant;
  8019. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8020. BEGIN
  8021. IF Trace THEN TraceEnter("VisitRealValue") END;
  8022. InitOperand(result,ModeValue);
  8023. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8024. END VisitRealValue;
  8025. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8026. VAR
  8027. componentType: SyntaxTree.Type;
  8028. BEGIN
  8029. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8030. ASSERT(x.type IS SyntaxTree.ComplexType);
  8031. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8032. InitOperand(result,ModeValue);
  8033. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8034. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8035. END VisitComplexValue;
  8036. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8037. VAR i: LONGINT; name: Basic.SegmentedName;
  8038. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8039. BEGIN
  8040. IF Trace THEN TraceEnter("VisitStringValue") END;
  8041. IF ~TryConstantDeclaration() THEN
  8042. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8043. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8044. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8045. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8046. ELSE
  8047. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8048. END;
  8049. FOR i := 0 TO x.length-1 DO
  8050. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8051. irv.Emit(Data(position,op));
  8052. END;
  8053. InitOperand(result,ModeReference);
  8054. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8055. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8056. END
  8057. END VisitStringValue;
  8058. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8059. BEGIN
  8060. IF Trace THEN TraceEnter("VisitNilValue") END;
  8061. InitOperand(result,ModeValue);
  8062. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8063. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8064. END VisitNilValue;
  8065. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8066. BEGIN
  8067. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8068. InitOperand(result,ModeValue);
  8069. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8070. END VisitEnumerationValue;
  8071. (** symbols *)
  8072. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8073. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8074. END VisitImport;
  8075. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8076. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8077. BEGIN
  8078. IF scope # baseScope THEN
  8079. (* left := [fp+8] *)
  8080. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8081. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8082. ReuseCopy(left,right);
  8083. ReleaseIntermediateOperand(right);
  8084. scope := scope.outerScope; DEC(level);
  8085. (* { left := [left+8] } *)
  8086. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8087. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8088. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8089. Emit(Mov(position,left,right));
  8090. scope := scope.outerScope; DEC(level);
  8091. END;
  8092. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8093. result := left;
  8094. ELSE
  8095. result := fp;
  8096. END;
  8097. END GetBaseRegister;
  8098. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8099. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8100. BEGIN
  8101. IF Trace THEN TraceEnter("VisitVariable"); END;
  8102. type := x.type.resolved;
  8103. IF (x.useRegister) THEN
  8104. InitOperand(result, ModeValue);
  8105. IF x.registerNumber < 0 THEN
  8106. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8107. reg := x.registerNumber;
  8108. ELSE
  8109. reg := registerUsageCount.Map(x.registerNumber);
  8110. UseRegister(reg);
  8111. END;
  8112. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8113. ELSIF x.externalName # NIL THEN
  8114. InitOperand(result,ModeReference);
  8115. Basic.ToSegmentedName(x.externalName^, name);
  8116. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8117. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8118. InitOperand(result,ModeReference);
  8119. GetBaseRegister(result.op,currentScope,x.scope);
  8120. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8121. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8122. InitOperand(result,ModeReference);
  8123. GetCodeSectionNameForSymbol(x,name);
  8124. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8125. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8126. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8127. InitOperand(result,ModeReference);
  8128. GetCodeSectionNameForSymbol(x,name);
  8129. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8130. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8131. ELSE (* field, left designator must have been emitted *)
  8132. ASSERT(result.mode = ModeReference);
  8133. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8134. ReuseCopy(temp,result.op);
  8135. ReleaseIntermediateOperand(result.op);
  8136. result.op := temp;
  8137. END;
  8138. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8139. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8140. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8141. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8142. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8143. END;
  8144. END;
  8145. END;
  8146. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8147. ValueToCondition(result);
  8148. ELSIF type IS SyntaxTree.ProcedureType THEN
  8149. ReleaseIntermediateOperand(result.tag);
  8150. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8151. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8152. UseIntermediateOperand(result.tag);
  8153. ELSE
  8154. result.tag := nil; (* nil *)
  8155. END;
  8156. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8157. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8158. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8159. ReleaseIntermediateOperand(result.tag);
  8160. Global.GetSymbolSegmentedName(x,name);
  8161. Basic.AppendToSegmentedName(name,"@len");
  8162. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8163. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8164. ELSE
  8165. END;
  8166. ELSE
  8167. ReleaseIntermediateOperand(result.tag);
  8168. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8169. END;
  8170. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8171. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8172. ReleaseIntermediateOperand(result.tag);
  8173. result.tag := result.op;
  8174. UseIntermediateOperand(result.tag);
  8175. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8176. IntermediateCode.MakeMemory(result.op,addressType);
  8177. END;
  8178. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8179. ReleaseIntermediateOperand(result.tag);
  8180. result.tag := TypeDescriptorAdr(type);
  8181. IF ~newObjectFile THEN
  8182. IntermediateCode.MakeMemory(result.tag,addressType);
  8183. END;
  8184. UseIntermediateOperand(result.tag);
  8185. (* tag for pointer type computed not here but during dereferencing *)
  8186. END;
  8187. IF Trace THEN TraceExit("VisitVariable") END;
  8188. END VisitVariable;
  8189. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8190. BEGIN
  8191. VisitVariable(property);
  8192. END VisitProperty;
  8193. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8194. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8195. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8196. BEGIN
  8197. type := x.type.resolved;
  8198. IF Trace THEN TraceEnter("VisitParameter") END;
  8199. IF x.ownerType IS SyntaxTree.CellType THEN
  8200. ptype := x.type.resolved;
  8201. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8202. IF ~(ptype IS SyntaxTree.PortType) THEN
  8203. InitOperand(result,ModeReference);
  8204. GetCodeSectionNameForSymbol(x,name);
  8205. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8206. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8207. RETURN
  8208. ELSE
  8209. InitOperand(result, ModeValue);
  8210. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8211. adr := 0;
  8212. WHILE parameter # x DO
  8213. parameterType := parameter.type;
  8214. inc := 1;
  8215. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8216. INC(adr, inc);
  8217. parameter := parameter.nextParameter
  8218. END;
  8219. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8220. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8221. RETURN
  8222. END;
  8223. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8224. InitOperand(result,ModeReference);
  8225. GetCodeSectionNameForSymbol(x,name);
  8226. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8227. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8228. RETURN
  8229. ELSE
  8230. GetBaseRegister(basereg,currentScope,x.scope);
  8231. InitOperand(result,ModeReference);
  8232. result.op := basereg;
  8233. END;
  8234. IF IsOpenArray(type) THEN
  8235. result.tag := basereg;
  8236. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8237. IntermediateCode.MakeMemory(result.op,addressType);
  8238. IF Global.IsOberonProcedure(x.ownerType) THEN
  8239. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8240. UseIntermediateOperand(result.tag);
  8241. ELSE
  8242. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8243. END;
  8244. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8245. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8246. ELSIF IsStaticArray(type) THEN
  8247. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8248. IntermediateCode.MakeMemory(result.op,addressType);
  8249. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8250. ELSIF type IS SyntaxTree.MathArrayType THEN
  8251. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8252. WITH type: SyntaxTree.MathArrayType DO
  8253. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8254. IF type.form = SyntaxTree.Tensor THEN
  8255. ELSIF type.form = SyntaxTree.Open THEN
  8256. result.tag := result.op;
  8257. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8258. IntermediateCode.MakeMemory(result.op,addressType);
  8259. UseIntermediateOperand(result.tag);
  8260. ELSIF type.form = SyntaxTree.Static THEN
  8261. IF x.kind = SyntaxTree.ConstParameter THEN
  8262. IntermediateCode.MakeMemory(result.op,addressType);
  8263. END;
  8264. ELSE HALT(100)
  8265. END;
  8266. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8267. IF type.form = SyntaxTree.Tensor THEN
  8268. ToMemory(result.op,addressType,0);
  8269. ELSIF type.form = SyntaxTree.Open THEN
  8270. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8271. ReuseCopy(result.tag, mem);
  8272. ReleaseIntermediateOperand(mem);
  8273. ReleaseIntermediateOperand(result.op);
  8274. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8275. ELSIF type.form = SyntaxTree.Static THEN
  8276. IntermediateCode.MakeMemory(result.op,addressType);
  8277. ELSE HALT(100)
  8278. END;
  8279. ELSE HALT(100)
  8280. END;
  8281. END;
  8282. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8283. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8284. IntermediateCode.MakeMemory(result.op,addressType);
  8285. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8286. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8287. END;
  8288. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8289. ValueToCondition(result);
  8290. ELSIF type IS SyntaxTree.ProcedureType THEN
  8291. ReleaseIntermediateOperand(result.tag);
  8292. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8293. IF x.kind = SyntaxTree.VarParameter THEN
  8294. ReuseCopy(result.tag,result.op);
  8295. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8296. IntermediateCode.MakeMemory(result.tag,addressType);
  8297. ELSE
  8298. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8299. UseIntermediateOperand(result.tag);
  8300. END;
  8301. ELSE
  8302. result.tag := nil;
  8303. END;
  8304. (* tag for pointer type computed not here but during dereferencing *)
  8305. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8306. ReleaseIntermediateOperand(result.tag);
  8307. result.tag := basereg;
  8308. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8309. IntermediateCode.MakeMemory(result.tag,addressType);
  8310. UseIntermediateOperand(result.tag);
  8311. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8312. ReleaseIntermediateOperand(result.tag);
  8313. result.tag := TypeDescriptorAdr(type);
  8314. IF ~newObjectFile THEN
  8315. IntermediateCode.MakeMemory(result.tag,addressType);
  8316. END;
  8317. UseIntermediateOperand(result.tag);
  8318. END;
  8319. IF Trace THEN TraceExit("VisitParameter") END;
  8320. END VisitParameter;
  8321. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8322. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8323. BEGIN
  8324. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8325. (* left.p: left already emitted *)
  8326. tag := result.op; (* value of pointer to left *)
  8327. (* get type desc *)
  8328. tmp := result.tag;
  8329. IntermediateCode.MakeMemory(tmp,addressType);
  8330. (* get method adr *)
  8331. Reuse1(reg,tmp);
  8332. ReleaseIntermediateOperand(tmp);
  8333. IF backend.cooperative THEN
  8334. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8335. WHILE recordType.baseType # NIL DO
  8336. recordType := recordType.GetBaseRecord ();
  8337. END;
  8338. GetRecordTypeName (recordType,name);
  8339. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8340. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8341. offset := 0;
  8342. ELSE
  8343. offset := 2;
  8344. END;
  8345. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8346. ELSE
  8347. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8348. END;
  8349. InitOperand(operand,ModeReference);
  8350. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8351. operand.op := reg;
  8352. operand.tag := tag;
  8353. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8354. END DynamicCallOperand;
  8355. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8356. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8357. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8358. BEGIN
  8359. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8360. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8361. tag := operand.op;
  8362. ReleaseIntermediateOperand(operand.tag);
  8363. ELSE tag := nil
  8364. END;
  8365. IF x.isInline THEN
  8366. sectionType := Sections.InlineCodeSection;
  8367. ELSE
  8368. sectionType := Sections.CodeSection;
  8369. END;
  8370. IF x.externalName # NIL THEN
  8371. Basic.ToSegmentedName(x.externalName^, name);
  8372. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8373. ELSE
  8374. GetCodeSectionNameForSymbol(x, name);
  8375. IF (x.scope.ownerModule = module.module) THEN
  8376. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8377. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8378. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8379. IF source.pc = 0 THEN (* no code yet *)
  8380. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8381. END;
  8382. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8383. bits := x.procedureScope.body.code.inlineCode;
  8384. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8385. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8386. binary.CopyBits(bits, 0, bits.GetSize());
  8387. source.SetResolved(binary);
  8388. ELSE
  8389. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8390. END;
  8391. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8392. END;
  8393. InitOperand(operand,ModeValue);
  8394. operand.op := reg;
  8395. operand.tag := tag;
  8396. IF Trace THEN TraceExit("StaticCallOperand") END;
  8397. END StaticCallOperand;
  8398. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8399. (* handle expressions of the form designator.procedure or procedure *)
  8400. BEGIN
  8401. IF Trace THEN TraceEnter("VisitProcedure") END;
  8402. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8403. DynamicCallOperand(result,x);
  8404. ELSIF x.isInline THEN
  8405. StaticCallOperand(result,x);
  8406. ELSE
  8407. StaticCallOperand(result,x);
  8408. END;
  8409. IF Trace THEN TraceExit("VisitProcedure") END;
  8410. END VisitProcedure;
  8411. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8412. BEGIN
  8413. VisitProcedure(x);
  8414. END VisitOperator;
  8415. (** statements *)
  8416. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8417. BEGIN
  8418. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8419. Expression(x.call);
  8420. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8421. ReleaseOperand(result)
  8422. END;
  8423. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8424. END VisitProcedureCallStatement;
  8425. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8426. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8427. leftBase, rightBase: SyntaxTree.Type;
  8428. procedureName,s: SyntaxTree.IdentifierString;
  8429. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8430. size: LONGINT; rightKind: LONGINT;
  8431. dummy: IntermediateCode.Operand;
  8432. CONST moduleName = "FoxArrayBase";
  8433. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8434. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8435. BEGIN
  8436. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8437. IF base IS SyntaxTree.MathArrayType THEN
  8438. base := OpenArray(base(SyntaxTree.MathArrayType));
  8439. END;
  8440. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8441. result.SetArrayBase(base);
  8442. RETURN result
  8443. END OpenArray;
  8444. BEGIN
  8445. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8446. SaveRegisters();ReleaseUsedRegisters(saved);
  8447. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8448. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8449. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8450. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8451. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8452. IF leftType.form = SyntaxTree.Tensor THEN
  8453. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8454. ELSIF leftType.form = SyntaxTree.Open THEN
  8455. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8456. ELSIF leftType.form = SyntaxTree.Static THEN
  8457. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8458. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8459. END;
  8460. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8461. IF procedure = NIL THEN
  8462. s := "Instruction not supported on target, emulation procedure ";
  8463. Strings.Append(s,moduleName);
  8464. Strings.Append(s,".");
  8465. Strings.Append(s,procedureName);
  8466. Strings.Append(s," not present");
  8467. Error(position,s);
  8468. ELSE
  8469. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8470. parameter.SetType(leftType);
  8471. parameter.SetAccess(SyntaxTree.Internal);
  8472. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8473. parameter.SetKind(rightKind);
  8474. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8475. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8476. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8477. StaticCallOperand(result,procedure);
  8478. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8479. ReleaseOperand(result);
  8480. END;
  8481. RestoreRegisters(saved);
  8482. END;
  8483. END AssignMathArray;
  8484. VAR modifyAssignmentCounter := 0: LONGINT;
  8485. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8486. VAR processor,mem,dst: IntermediateCode.Operand;
  8487. BEGIN
  8488. IF value.intValue = true.intValue THEN
  8489. INC(modifyAssignmentCounter);
  8490. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8491. modifyAssignmentsPC := section.pc;
  8492. ELSE
  8493. DEC(modifyAssignmentCounter);
  8494. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8495. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8496. END;
  8497. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8498. dst := NewRegisterOperand (addressType);
  8499. Emit(Mov(position,dst, processor));
  8500. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8501. Emit(Mov(position,mem, value));
  8502. ReleaseIntermediateOperand(dst);
  8503. END ModifyAssignments;
  8504. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8505. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8506. BEGIN
  8507. type := left.type.resolved;
  8508. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8509. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8510. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8511. IF procedureType.returnParameter = parameter THEN
  8512. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8513. END;
  8514. END;
  8515. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8516. END CopySize;
  8517. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8518. VAR
  8519. leftO, rightO: Operand;
  8520. mem, sizeOp: IntermediateCode.Operand;
  8521. leftType, rightType, componentType: SyntaxTree.Type;
  8522. size: LONGINT;
  8523. parameters: SyntaxTree.ExpressionList;
  8524. procedure: SyntaxTree.Procedure;
  8525. call: SyntaxTree.ProcedureCallDesignator;
  8526. designator: SyntaxTree.Designator;
  8527. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8528. VAR procedureType: SyntaxTree.ProcedureType;
  8529. BEGIN
  8530. IF ReturnedAsParameter(right.type) THEN
  8531. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8532. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8533. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8534. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8535. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8536. IF right.id = Global.Reshape THEN RETURN TRUE
  8537. END;
  8538. END;
  8539. END;
  8540. END;
  8541. RETURN FALSE
  8542. END CanPassAsResultParameter;
  8543. BEGIN
  8544. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8545. leftType := left.type.resolved; rightType:= right.type.resolved;
  8546. IF backend.cooperative & left.NeedsTrace() THEN
  8547. ModifyAssignments(true);
  8548. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8549. Designate(right, rightO);
  8550. Designate(left, leftO);
  8551. ASSERT(leftO.mode = ModeReference);
  8552. TransferToRegister(leftO.op, leftO.op);
  8553. TransferToRegister(rightO.op, rightO.op);
  8554. Emit(Push(position, leftO.op));
  8555. Emit(Push(position, rightO.op));
  8556. CallAssignMethod(leftO.op, rightO.op, left.type);
  8557. Emit(Pop(position, rightO.op));
  8558. Emit(Pop(position, leftO.op));
  8559. sizeOp := CopySize(left);
  8560. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8561. ReleaseOperand(leftO);
  8562. ReleaseOperand(rightO);
  8563. ELSE
  8564. Evaluate(right,rightO);
  8565. Designate(left,leftO);
  8566. ASSERT(leftO.mode = ModeReference);
  8567. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8568. (* copy procedure address first *)
  8569. MakeMemory(mem,leftO.op,addressType,0);
  8570. Emit(Mov(position,mem,rightO.op));
  8571. ReleaseIntermediateOperand(mem);
  8572. (* copy pointer address *)
  8573. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8574. CallAssignPointer(leftO.tag, rightO.tag);
  8575. ELSE
  8576. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8577. CallAssignPointer(leftO.op, rightO.op);
  8578. END;
  8579. ReleaseOperand(leftO);
  8580. ReleaseOperand(rightO);
  8581. END;
  8582. ModifyAssignments(false);
  8583. RETURN;
  8584. END;
  8585. IF CanPassAsResultParameter(right) THEN
  8586. procedureResultDesignator := left(SyntaxTree.Designator);
  8587. Expression(right);
  8588. procedureResultDesignator := NIL;
  8589. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8590. (* left <-- ALIAS OF right: zerocopy *)
  8591. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8592. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8593. designator.SetType(procedure.type);
  8594. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8595. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8596. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8597. END;
  8598. ELSIF leftType IS SyntaxTree.RangeType THEN
  8599. (* LHS is of array range type *)
  8600. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8601. Evaluate(right, rightO);
  8602. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8603. (* first *)
  8604. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8605. Emit(Mov(position,mem, rightO.op));
  8606. ReleaseIntermediateOperand(mem);
  8607. (* last *)
  8608. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8609. Emit(Mov(position,mem, rightO.tag));
  8610. ReleaseIntermediateOperand(mem);
  8611. (* step *)
  8612. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8613. Emit(Mov(position,mem, rightO.extra));
  8614. ReleaseIntermediateOperand(mem);
  8615. ReleaseOperand(rightO);
  8616. ReleaseOperand(leftO)
  8617. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8618. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8619. Evaluate(right, rightO);
  8620. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8621. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8622. (* real part *)
  8623. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8624. Emit(Mov(position,mem, rightO.op));
  8625. ReleaseIntermediateOperand(mem);
  8626. (* imaginary part *)
  8627. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8628. Emit(Mov(position,mem, rightO.tag));
  8629. ReleaseIntermediateOperand(mem);
  8630. ReleaseOperand(rightO);
  8631. ReleaseOperand(leftO)
  8632. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8633. OR (leftType IS SyntaxTree.PortType) THEN
  8634. (* rightO := leftO;*)
  8635. Evaluate(right,rightO);
  8636. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8637. Designate(left,leftO);
  8638. IF leftO.mode = ModeReference THEN
  8639. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8640. destination := mem;
  8641. ELSE
  8642. destination := leftO.op;
  8643. END;
  8644. ReleaseOperand(leftO);
  8645. IF destination.mode # IntermediateCode.Undefined THEN
  8646. Emit(Mov(position,destination,rightO.op));
  8647. END;
  8648. ReleaseOperand(rightO);
  8649. ReleaseIntermediateOperand(mem);
  8650. IntermediateCode.InitOperand(destination);
  8651. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8652. Evaluate(right,rightO);
  8653. Designate(left,leftO);
  8654. MakeMemory(mem,leftO.op,addressType,0);
  8655. Emit(Mov(position,mem,rightO.op));
  8656. ReleaseIntermediateOperand(mem);
  8657. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8658. (* delegate *)
  8659. (*
  8660. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8661. *)
  8662. Emit(Mov(position,leftO.tag,rightO.tag));
  8663. END;
  8664. ReleaseOperand(leftO);
  8665. ReleaseOperand(rightO);
  8666. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8667. Designate(right,rightO);
  8668. Designate(left,leftO);
  8669. sizeOp := CopySize(left);
  8670. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8671. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8672. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8673. IF (rightType IS SyntaxTree.StringType) THEN
  8674. CopyString(left,right);
  8675. 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
  8676. Designate(right,rightO);
  8677. Designate(left,leftO);
  8678. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8679. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8680. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8681. ELSE
  8682. HALT(201)
  8683. END;
  8684. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8685. AssignMathArray(left,right);
  8686. ELSE
  8687. HALT(200);
  8688. END;
  8689. END Assign;
  8690. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8691. BEGIN
  8692. IF Trace THEN TraceEnter("VisitAssignment") END;
  8693. Assign(x.left,x.right);
  8694. IF Trace THEN TraceExit("VisitAssignment") END;
  8695. END VisitAssignment;
  8696. PROCEDURE EmitCooperativeSwitch;
  8697. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8698. BEGIN
  8699. ASSERT (cooperativeSwitches);
  8700. pc := section.pc;
  8701. IF lastSwitchPC = section.pc THEN RETURN END;
  8702. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8703. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8704. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8705. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8706. INC(statCoopSwitch, section.pc - pc);
  8707. END EmitCooperativeSwitch;
  8708. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8709. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8710. BEGIN
  8711. p0 := communication.left; p1 := communication.right;
  8712. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8713. Evaluate(p0,s0);
  8714. Evaluate(p1,s1);
  8715. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8716. Emit(Push(position,s0.op));
  8717. Emit(Push(position,s1.op));
  8718. (*
  8719. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8720. *)
  8721. IF ~backend.cellsAreObjects THEN
  8722. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8723. END;
  8724. ReleaseOperand(s0);
  8725. ReleaseOperand(s1);
  8726. IF backend.cellsAreObjects THEN
  8727. CallThis(position,"ActiveCellsRuntime","Send",2);
  8728. ELSE
  8729. CallThis(position,ChannelModuleName,"Send",2);
  8730. END;
  8731. (* ----- RECEIVE ------*)
  8732. ELSE
  8733. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8734. tmp := p0; p0 := p1; p1 := tmp;
  8735. END;
  8736. Evaluate(p0,s0);
  8737. Emit(Push(position,s0.op));
  8738. Designate(p1,s1);
  8739. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8740. Emit(Push(position,s1.op));
  8741. (*
  8742. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8743. *)
  8744. IF ~backend.cellsAreObjects THEN
  8745. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8746. END;
  8747. ReleaseOperand(s0);
  8748. ReleaseOperand(s1);
  8749. IF backend.cellsAreObjects THEN
  8750. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8751. ELSE
  8752. CallThis(position,ChannelModuleName,"Receive",2)
  8753. END;
  8754. END;
  8755. END VisitCommunicationStatement;
  8756. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8757. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8758. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8759. VAR true, false: Label; condition, value: BOOLEAN;
  8760. BEGIN
  8761. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8762. IF condition THEN
  8763. true := NewLabel();
  8764. false := NewLabel();
  8765. Condition(if.condition,true,false);
  8766. SetLabel(true);
  8767. StatementSequence(if.statements);
  8768. BrL(end);
  8769. SetLabel(false);
  8770. ELSE
  8771. IF value THEN (* always true *)
  8772. escape := TRUE;
  8773. StatementSequence(if.statements);
  8774. (* no branch necessary -- rest skipped *)
  8775. END;
  8776. END;
  8777. END IfPart;
  8778. BEGIN
  8779. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8780. end := NewLabel();
  8781. elsifs := x.ElsifParts();
  8782. IfPart(x.ifPart);
  8783. FOR i := 0 TO elsifs-1 DO
  8784. IF ~escape THEN
  8785. elsif := x.GetElsifPart(i);
  8786. IfPart(elsif);
  8787. END;
  8788. END;
  8789. IF (x.elsePart # NIL) & ~escape THEN
  8790. StatementSequence(x.elsePart);
  8791. END;
  8792. SetLabel(end);
  8793. IF Trace THEN TraceExit("VisitIfStatement") END;
  8794. END VisitIfStatement;
  8795. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8796. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8797. BEGIN
  8798. (*IF x.variable.type.resolved = x.type.resolved THEN
  8799. (* always true, do nothing *)
  8800. ELSE*)
  8801. Designate(x.variable,res);
  8802. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8803. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8804. END;
  8805. trueL := NewLabel();
  8806. TypeTest(res.tag,x.type,trueL,falseL);
  8807. ReleaseOperand(res);
  8808. SetLabel(trueL);
  8809. StatementSequence(x.statements);
  8810. BrL(endL);
  8811. END WithPart;
  8812. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8813. VAR endL,falseL: Label;i: LONGINT;
  8814. BEGIN
  8815. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8816. endL := NewLabel();
  8817. FOR i := 0 TO x.WithParts()-1 DO
  8818. falseL := NewLabel();
  8819. WithPart(x.GetWithPart(i),falseL,endL);
  8820. SetLabel(falseL);
  8821. END;
  8822. IF x.elsePart = NIL THEN
  8823. IF ~isUnchecked THEN
  8824. EmitTrap(position,WithTrap);
  8825. END;
  8826. ELSE
  8827. StatementSequence(x.elsePart)
  8828. END;
  8829. SetLabel(endL);
  8830. IF Trace THEN TraceExit("VisitWithStatement") END;
  8831. END VisitWithStatement;
  8832. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8833. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8834. out,else: Label; label: Label;
  8835. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8836. symbol: SyntaxTree.Symbol;
  8837. BEGIN
  8838. (*! split case statement into if-elsif statements for large case label lists *)
  8839. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8840. size := x.max-x.min+1;
  8841. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8842. END;
  8843. Evaluate(x.variable,var);
  8844. ReuseCopy(tmp,var.op);
  8845. ReleaseIntermediateOperand(var.op);
  8846. var.op := tmp;
  8847. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8848. Convert(var.op,addressType);
  8849. size := x.max-x.min+1;
  8850. else := NewLabel();
  8851. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8852. (*
  8853. UniqueId(name,module.module,"case",caseId);
  8854. *)
  8855. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8856. Global.GetModuleSegmentedName(module.module, name);
  8857. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8858. symbol := SyntaxTree.NewSymbol(name[1]);
  8859. symbol.SetScope(moduleScope);
  8860. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8861. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8862. section.isCaseTable := TRUE;
  8863. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8864. ReuseCopy(res,var.op);
  8865. ReleaseOperand(var);
  8866. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8867. Emit(Add(position,res,res,jmp));
  8868. IntermediateCode.MakeMemory(res,addressType);
  8869. Emit(Br(position,res));
  8870. ReleaseIntermediateOperand(res);
  8871. out := NewLabel();
  8872. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8873. part := x.GetCasePart(i);
  8874. constant := part.firstConstant;
  8875. label := NewLabel();
  8876. SetLabel(label);
  8877. WHILE(constant # NIL) DO (* case labels for this case part *)
  8878. FOR j := constant.min TO constant.max DO
  8879. fixups[j-x.min] := label;
  8880. END;
  8881. constant := constant.next;
  8882. END;
  8883. StatementSequence(part.statements);
  8884. BrL(out);
  8885. END;
  8886. SetLabel(else);
  8887. FOR i := 0 TO size-1 DO
  8888. IF fixups[i] = NIL THEN
  8889. fixups[i] := else;
  8890. END;
  8891. END;
  8892. IF x.elsePart # NIL THEN
  8893. StatementSequence(x.elsePart);
  8894. ELSIF ~isUnchecked THEN
  8895. EmitTrap(position,CaseTrap);
  8896. END;
  8897. SetLabel(out);
  8898. FOR i := 0 TO size-1 DO
  8899. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8900. section.Emit(Data(position,op));
  8901. END;
  8902. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8903. END VisitCaseStatement;
  8904. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8905. VAR start: Label; true,false: Label;
  8906. BEGIN
  8907. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8908. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8909. start := NewLabel();
  8910. true := NewLabel();
  8911. false := NewLabel();
  8912. SetLabel(start);
  8913. Condition(x.condition,true,false);
  8914. SetLabel(true);
  8915. StatementSequence(x.statements);
  8916. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8917. BrL(start);
  8918. SetLabel(false);
  8919. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8920. END VisitWhileStatement;
  8921. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8922. VAR false,true: Label;
  8923. BEGIN
  8924. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8925. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8926. true := NewLabel();
  8927. false := NewLabel();
  8928. SetLabel(false);
  8929. StatementSequence(x.statements);
  8930. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8931. Condition(x.condition,true,false);
  8932. SetLabel(true);
  8933. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8934. END VisitRepeatStatement;
  8935. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8936. VAR
  8937. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8938. temporaryVariable: SyntaxTree.Variable;
  8939. temporaryVariableDesignator : SyntaxTree.Designator;
  8940. BEGIN
  8941. IF Trace THEN TraceEnter("VisitForStatement") END;
  8942. true := NewLabel();
  8943. false := NewLabel();
  8944. start := NewLabel();
  8945. Assign(x.variable,x.from);
  8946. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8947. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8948. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8949. Assign(temporaryVariableDesignator,x.to);
  8950. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8951. IF by > 0 THEN
  8952. cmp := Scanner.LessEqual
  8953. ELSE
  8954. cmp := Scanner.GreaterEqual
  8955. END;
  8956. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8957. binary.SetType(system.booleanType);
  8958. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8959. SetLabel(start);
  8960. Condition(binary,true,false);
  8961. SetLabel(true);
  8962. StatementSequence(x.statements);
  8963. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8964. binary.SetType(x.variable.type);
  8965. Assign(x.variable,binary);
  8966. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8967. BrL(start);
  8968. SetLabel(false);
  8969. IF Trace THEN TraceExit("VisitForStatement") END;
  8970. END VisitForStatement;
  8971. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8972. VAR prevLoop: Label;
  8973. BEGIN
  8974. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8975. prevLoop := currentLoop;
  8976. currentLoop := NewLabel();
  8977. StatementSequence(x.statements);
  8978. SetLabel(currentLoop);
  8979. currentLoop := prevLoop;
  8980. IF Trace THEN TraceExit("VisitExitableBlock") END;
  8981. END VisitExitableBlock;
  8982. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  8983. VAR prevLoop,start: Label;
  8984. BEGIN
  8985. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  8986. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8987. start := NewLabel();
  8988. prevLoop := currentLoop;
  8989. SetLabel(start);
  8990. currentLoop := NewLabel();
  8991. StatementSequence(x.statements);
  8992. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8993. BrL(start);
  8994. SetLabel(currentLoop);
  8995. currentLoop := prevLoop;
  8996. IF Trace THEN TraceExit("VisitLoopStatement") END;
  8997. END VisitLoopStatement;
  8998. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  8999. VAR outer: SyntaxTree.Statement;
  9000. BEGIN
  9001. IF Trace THEN TraceEnter("VisitExitStatement") END;
  9002. IF locked THEN (* r if we jump out of an exclusive block *)
  9003. outer := x.outer;
  9004. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  9005. outer := outer.outer;
  9006. END;
  9007. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  9008. Lock(FALSE);
  9009. END;
  9010. END;
  9011. BrL(currentLoop);
  9012. IF Trace THEN TraceExit("VisitExitStatement") END;
  9013. END VisitExitStatement;
  9014. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9015. VAR
  9016. expression, parameterDesignator: SyntaxTree.Expression;
  9017. type, componentType: SyntaxTree.Type;
  9018. res, right: Operand;
  9019. left, mem, reg: IntermediateCode.Operand;
  9020. parameter: SyntaxTree.Parameter;
  9021. procedure: SyntaxTree.Procedure;
  9022. procedureType: SyntaxTree.ProcedureType;
  9023. returnTypeOffset: LONGINT;
  9024. delegate: BOOLEAN;
  9025. map: SymbolMap;
  9026. BEGIN
  9027. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9028. expression := x.returnValue;
  9029. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9030. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9031. IF currentIsInline THEN
  9032. IF expression # NIL THEN
  9033. map := currentMapper.Get(NIL);
  9034. IF map # NIL THEN
  9035. Assign(map.to, expression);
  9036. END;
  9037. END;
  9038. BrL(currentInlineExit);
  9039. RETURN;
  9040. END;
  9041. IF expression # NIL THEN
  9042. type := expression.type.resolved;
  9043. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9044. IF locked THEN Lock(FALSE) END;
  9045. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9046. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9047. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9048. (* return without structured return parameter *)
  9049. Evaluate(expression,res);
  9050. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9051. IF locked OR profile THEN
  9052. Emit(Push(position,res.op));
  9053. IF delegate THEN HALT(200) END;
  9054. ReleaseOperand(res);
  9055. IF locked THEN Lock(FALSE) END;
  9056. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9057. reg := NewRegisterOperand(res.op.type);
  9058. Emit(Pop(position,reg));
  9059. Emit(Return(position,reg));
  9060. ReleaseIntermediateOperand(reg);
  9061. ELSE
  9062. Emit(Return(position,res.op));
  9063. ReleaseOperand(res);
  9064. END;
  9065. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9066. THEN
  9067. (* return using structured return parameter *)
  9068. 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)
  9069. OR SemanticChecker.IsPointerType(type));
  9070. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9071. parameter :=procedureType.returnParameter;
  9072. ASSERT(parameter # NIL);
  9073. returnTypeOffset := parameter.offsetInBits;
  9074. (*
  9075. IF parameter# NIL THEN
  9076. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9077. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9078. ELSE
  9079. returnTypeOffset := system.offsetFirstParameter
  9080. END;
  9081. *)
  9082. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9083. IF type IS SyntaxTree.RangeType THEN
  9084. (* array range type *)
  9085. Evaluate(expression, right);
  9086. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9087. Emit(Mov(position,mem, right.op)); (* first *)
  9088. ReleaseIntermediateOperand(mem);
  9089. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9090. Emit(Mov(position,mem, right.tag)); (* last *)
  9091. ReleaseIntermediateOperand(mem);
  9092. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9093. Emit(Mov(position,mem, right.extra)); (* step *)
  9094. ReleaseIntermediateOperand(mem);
  9095. ReleaseOperand(right);
  9096. ELSIF type IS SyntaxTree.ComplexType THEN
  9097. Evaluate(expression, right);
  9098. componentType := type(SyntaxTree.ComplexType).componentType;
  9099. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9100. Emit(Mov(position,mem, right.op)); (* real part *)
  9101. ReleaseIntermediateOperand(mem);
  9102. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9103. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9104. ReleaseIntermediateOperand(mem);
  9105. ReleaseOperand(right);
  9106. ELSE (* covers cases: pointer / record / array *)
  9107. parameter := procedureType.returnParameter;
  9108. checker.SetCurrentScope(currentScope);
  9109. ASSERT(parameter # NIL);
  9110. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9111. Assign(parameterDesignator,expression);
  9112. END;
  9113. ReleaseIntermediateOperand(left);
  9114. IF locked THEN Lock(FALSE) END;
  9115. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9116. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9117. parameter := procedureType.returnParameter;
  9118. checker.SetCurrentScope(currentScope);
  9119. IF parameter = NIL THEN
  9120. Error(procedure.position, "structured return of parameter of procedure not found");
  9121. ELSE
  9122. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9123. Assign(parameterDesignator,expression);
  9124. END;
  9125. IF locked THEN Lock(FALSE) END;
  9126. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9127. ELSE
  9128. HALT(200);
  9129. END;
  9130. ELSE
  9131. IF locked THEN Lock(FALSE) END;
  9132. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9133. END;
  9134. IF backend.cooperative THEN
  9135. BrL(exitLabel);
  9136. ELSE
  9137. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9138. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9139. END;
  9140. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9141. END VisitReturnStatement;
  9142. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9143. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9144. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9145. statements: SyntaxTree.StatementSequence;
  9146. name, suffix: SyntaxTree.IdentifierString;
  9147. BEGIN
  9148. Strings.IntToStr(awaitProcCounter,suffix);
  9149. Strings.Concat("@AwaitProcedure",suffix,name);
  9150. identifier := SyntaxTree.NewIdentifier(name);
  9151. INC(awaitProcCounter);
  9152. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9153. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9154. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9155. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9156. procedure.SetAccess(SyntaxTree.Hidden);
  9157. procedure.SetScope(currentScope);
  9158. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9159. procedureType.SetReturnType(system.booleanType);
  9160. procedure.SetType(procedureType);
  9161. body := SyntaxTree.NewBody(x.position,procedureScope);
  9162. procedureScope.SetBody(body);
  9163. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9164. returnStatement.SetReturnValue(x.condition);
  9165. statements := SyntaxTree.NewStatementSequence();
  9166. statements.AddStatement(returnStatement);
  9167. body.SetStatementSequence(statements);
  9168. currentScope.AddProcedure(procedure);
  9169. RETURN procedure
  9170. END MakeAwaitProcedure;
  9171. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9172. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9173. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9174. BEGIN
  9175. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9176. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9177. IF backend.cooperative THEN
  9178. start := NewLabel();
  9179. true := NewLabel();
  9180. false := NewLabel();
  9181. SetLabel(start);
  9182. Condition(x.condition,true,false);
  9183. SetLabel(false);
  9184. PushSelfPointer();
  9185. CallThis(position,"ExclusiveBlocks","Await",1);
  9186. BrL(start);
  9187. SetLabel(true);
  9188. PushSelfPointer();
  9189. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9190. ELSE
  9191. proc := MakeAwaitProcedure(x);
  9192. Emit(Push(position,fp));
  9193. GetCodeSectionNameForSymbol(proc,name);
  9194. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9195. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9196. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9197. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9198. Emit(Result(position,res));
  9199. (*
  9200. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9201. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9202. *)
  9203. InitOperand(result,ModeValue);
  9204. result.op := res;
  9205. label := NewLabel();
  9206. BreqL(label, result.op, SELF.true);
  9207. ReleaseOperand(result);
  9208. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9209. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9210. Emit(Push(position,res));
  9211. Emit(Push(position,fp));
  9212. PushSelfPointer();
  9213. Emit(Push(position,nil));
  9214. CallThis(position,"Objects","Await",4);
  9215. SetLabel(label);
  9216. END;
  9217. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9218. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9219. END VisitAwaitStatement;
  9220. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9221. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9222. BEGIN
  9223. FOR i := 0 TO x.Length() - 1 DO
  9224. statement := x.GetStatement( i );
  9225. Statement(statement);
  9226. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9227. END;
  9228. END StatementSequence;
  9229. PROCEDURE PushSelfPointer;
  9230. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9231. BEGIN
  9232. scope := currentScope;
  9233. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9234. scope := scope.outerScope;
  9235. END;
  9236. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9237. IF ~newObjectFile THEN
  9238. Symbol(moduleSelf,op);
  9239. IntermediateCode.MakeMemory(op.op,addressType);
  9240. ELSE
  9241. moduleSection := meta.ModuleSection();
  9242. IF backend.cooperative THEN
  9243. moduleOffset := 0;
  9244. ELSE
  9245. moduleOffset := moduleSection.pc;
  9246. END;
  9247. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9248. END;
  9249. ELSE
  9250. GetBaseRegister(op.op,currentScope,scope);
  9251. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9252. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9253. IF backend.cooperative THEN
  9254. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9255. END;
  9256. IntermediateCode.MakeMemory(op.op,addressType);
  9257. END;
  9258. Emit(Push(position,op.op));
  9259. ReleaseOperand(op);
  9260. END PushSelfPointer;
  9261. PROCEDURE Lock(lock: BOOLEAN);
  9262. BEGIN
  9263. IF Trace THEN TraceEnter("Lock") END;
  9264. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9265. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9266. ASSERT(modifyAssignmentCounter = 0);
  9267. IF dump # NIL THEN
  9268. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9269. dump.Ln;dump.Update;
  9270. END;
  9271. PushSelfPointer;
  9272. IF backend.cooperative THEN
  9273. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9274. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9275. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9276. END;
  9277. ELSE
  9278. Emit(Push(position,true));
  9279. IF lock THEN CallThis(position,"Objects","Lock",2)
  9280. ELSE CallThis(position,"Objects","Unlock",2);
  9281. END;
  9282. END;
  9283. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9284. IF Trace THEN TraceExit("Lock") END;
  9285. END Lock;
  9286. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9287. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9288. BEGIN
  9289. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9290. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9291. previouslyUnchecked := isUnchecked;
  9292. isUnchecked := isUnchecked OR x.isUnchecked;
  9293. previouslyCooperativeSwitches := cooperativeSwitches;
  9294. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9295. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9296. IF x.statements # NIL THEN
  9297. StatementSequence(x.statements);
  9298. END;
  9299. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9300. isUnchecked := previouslyUnchecked;
  9301. cooperativeSwitches := previouslyCooperativeSwitches;
  9302. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9303. END VisitStatementBlock;
  9304. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9305. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9306. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9307. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9308. procedure: SyntaxTree.Procedure;
  9309. map: SymbolMap;
  9310. BEGIN
  9311. scope := currentScope;
  9312. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9313. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9314. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9315. return := emptyOperand;
  9316. IF Trace THEN TraceEnter("VisitCode") END;
  9317. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9318. NEW(in, x.inRules.Length());
  9319. FOR i := 0 TO LEN(in)-1 DO
  9320. statement := x.inRules.GetStatement(i);
  9321. WITH statement: SyntaxTree.Assignment DO
  9322. Evaluate(statement.right, operand);
  9323. result := operand.op;
  9324. NEW(str, 64);
  9325. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9326. in[i] := result; IntermediateCode.SetString(in[i], str);
  9327. ReleaseIntermediateOperand(operand.tag);
  9328. END;
  9329. END;
  9330. ELSE in := NIL
  9331. END;
  9332. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9333. NEW(out, x.outRules.Length());
  9334. FOR i := 0 TO LEN(out)-1 DO
  9335. statement := x.outRules.GetStatement(i);
  9336. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9337. WITH statement: SyntaxTree.Assignment DO
  9338. Evaluate(statement.left, operand); (*?? or designate *)
  9339. result := operand.op;
  9340. NEW(str, 64);
  9341. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9342. out[i] := result; IntermediateCode.SetString(out[i], str);
  9343. ReleaseIntermediateOperand(operand.tag);
  9344. |statement: SyntaxTree.ReturnStatement DO
  9345. NEW(str, 64);
  9346. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9347. IF currentIsInline THEN
  9348. map := currentMapper.Get(NIL);
  9349. Evaluate(map.to, operand);
  9350. out[i] := operand.op;
  9351. ELSE
  9352. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9353. END;
  9354. IntermediateCode.SetString(out[i], str);
  9355. ReleaseIntermediateOperand(operand.tag);
  9356. return := out[i];
  9357. ELSE
  9358. END;
  9359. END;
  9360. ELSE out := NIL
  9361. END;
  9362. Emit(Asm(x.position,x.sourceCode, in, out));
  9363. IF in # NIL THEN
  9364. FOR i := 0 TO LEN(in)-1 DO
  9365. ReleaseIntermediateOperand(in[i]);
  9366. END;
  9367. END;
  9368. IF out # NIL THEN
  9369. FOR i := 0 TO LEN(out)-1 DO
  9370. WITH statement: SyntaxTree.Assignment DO
  9371. ReleaseIntermediateOperand(out[i]);
  9372. ELSE
  9373. IF currentIsInline THEN
  9374. ReleaseIntermediateOperand(out[i]);
  9375. END;
  9376. END;
  9377. statement := x.outRules.GetStatement(i);
  9378. END;
  9379. END;
  9380. IF return.mode # IntermediateCode.Undefined THEN
  9381. IF currentIsInline THEN
  9382. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9383. Symbol(procedureType.returnParameter, par);
  9384. MakeMemory(mem, par.op, return.type, 0);
  9385. ReleaseOperand(par);
  9386. Emit(Mov(position, mem, return));
  9387. ReleaseIntermediateOperand(mem);
  9388. ELSE
  9389. Emit(Return(position,return));
  9390. END;
  9391. IF currentIsInline THEN RETURN END;
  9392. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9393. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9394. ReleaseIntermediateOperand(return);
  9395. END;
  9396. IF Trace THEN TraceExit("VisitCode") END;
  9397. END VisitCode;
  9398. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9399. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9400. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9401. const, call: IntermediateCode.Operand;
  9402. parameterDesignator: SyntaxTree.Expression;
  9403. saved: RegisterEntry;
  9404. name: Basic.SegmentedName;
  9405. BEGIN
  9406. IF Trace THEN TraceEnter("ParameterCopies") END;
  9407. parameter := x.firstParameter;
  9408. WHILE parameter # NIL DO
  9409. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9410. type := parameter.type.resolved;
  9411. IF IsOpenArray(type) THEN
  9412. VisitParameter(parameter);
  9413. op := result;
  9414. IF backend.cooperative & parameter.NeedsTrace() THEN
  9415. length := GetArrayLength(type, op.tag);
  9416. size := NewRegisterOperand(addressType);
  9417. base := ArrayBaseType(type);
  9418. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9419. Emit(Mul(position, size, length, const));
  9420. dst := NewRegisterOperand (addressType);
  9421. Emit(Mov(position, dst, size));
  9422. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9423. Emit(Sub(position,dst,sp,dst));
  9424. Emit(And(position,dst,dst,const));
  9425. Emit(Mov(position,sp,dst));
  9426. par := fp;
  9427. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9428. IntermediateCode.InitImmediate(null, byteType, 0);
  9429. Emit(Fill(position, dst, size, null));
  9430. ReleaseIntermediateOperand(dst);
  9431. ReleaseIntermediateOperand(length);
  9432. SaveRegisters();ReleaseUsedRegisters(saved);
  9433. (* register dst has been freed before SaveRegisters already *)
  9434. base := ArrayBaseType(type);
  9435. (* assign method of open array *)
  9436. IF base.IsRecordType() THEN
  9437. Emit (Push(position, length));
  9438. Emit (Push(position, dst));
  9439. Emit (Push(position, op.op));
  9440. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9441. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9442. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9443. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9444. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9445. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9446. Emit (Push(position,length));
  9447. Emit (Push(position, dst));
  9448. Emit (Push(position, length));
  9449. Emit (Push(position, op.op));
  9450. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9451. ELSE
  9452. Emit (Push(position, length));
  9453. Emit (Push(position, dst));
  9454. Emit (Push(position, length));
  9455. Emit (Push(position, op.op));
  9456. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9457. ASSERT(ArrayBaseType(type).IsPointer());
  9458. END;
  9459. RestoreRegisters(saved);
  9460. ELSE
  9461. temp := GetDynamicSize(type,op.tag);
  9462. ReuseCopy(size,temp);
  9463. ReleaseIntermediateOperand(temp);
  9464. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9465. Emit(Sub(position,size,sp,size));
  9466. Emit(And(position,size,size,const));
  9467. Emit(Mov(position,sp,size));
  9468. par := fp;
  9469. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9470. ReleaseIntermediateOperand(size);
  9471. size := GetDynamicSize(type,op.tag);
  9472. END;
  9473. Emit(Copy(position,sp,op.op,size));
  9474. ReleaseIntermediateOperand(size);
  9475. ReleaseOperand(op);
  9476. IntermediateCode.MakeMemory(par,addressType);
  9477. Emit(Mov(position,par,sp));
  9478. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9479. checker.SetCurrentScope(currentScope);
  9480. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9481. Assign(parameterDesignator,parameterDesignator);
  9482. END;
  9483. END;
  9484. parameter := parameter.nextParameter;
  9485. END;
  9486. IF Trace THEN TraceExit("ParameterCopies") END;
  9487. END ParameterCopies;
  9488. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9489. VAR x: SyntaxTree.Variable;
  9490. BEGIN
  9491. x := scope.firstVariable;
  9492. WHILE x # NIL DO
  9493. InitVariable(x);
  9494. x := x.nextVariable;
  9495. END;
  9496. END InitVariables;
  9497. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9498. BEGIN
  9499. IF (symbol # NIL) THEN
  9500. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9501. ELSE
  9502. RETURN 0
  9503. END;
  9504. END GetFingerprint;
  9505. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9506. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9507. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9508. saved: RegisterEntry;
  9509. BEGIN
  9510. IF Trace THEN TraceEnter("Body") END;
  9511. ReleaseUsedRegisters(saved); (* just in case ... *)
  9512. section := ir;
  9513. exitLabel := NewLabel ();
  9514. IF moduleBody THEN moduleBodySection := section END;
  9515. IF ir.comments # NIL THEN
  9516. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9517. commentPrintout.SingleStatement(TRUE);
  9518. dump := ir.comments;
  9519. ELSE
  9520. commentPrintout := NIL;
  9521. dump := NIL;
  9522. END;
  9523. prevScope := currentScope;
  9524. currentScope := scope;
  9525. lastSwitchPC := 0;
  9526. cooperativeSwitches := backend.cooperative;
  9527. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9528. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9529. IF x # NIL THEN
  9530. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9531. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9532. IF moduleBody THEN
  9533. ProfilerInit();
  9534. ELSE
  9535. Basic.SegmentedNameToString(ir.name, string);
  9536. ProfilerAddProcedure(numberProcedures,string);
  9537. ProfilerEnterExit(numberProcedures,TRUE);
  9538. END;
  9539. END;
  9540. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9541. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9542. END;
  9543. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9544. IF moduleBody THEN
  9545. InitVariables(moduleScope)
  9546. END;
  9547. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9548. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9549. IF procedure = cellScope.bodyProcedure THEN
  9550. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9551. StaticCallOperand(op, cellScope.constructor);
  9552. Emit(Call(position,op.op,0));
  9553. END;
  9554. END;
  9555. END;
  9556. ParameterCopies(procedureType);
  9557. InitVariables(scope);
  9558. IF x.code = NIL THEN
  9559. VisitStatementBlock(x);
  9560. ELSE
  9561. VisitCode(x.code)
  9562. END;
  9563. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9564. ir.SetFinally(ir.pc);
  9565. StatementSequence(x.finally)
  9566. END;
  9567. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9568. IF ~backend.cooperative THEN
  9569. ProfilerEnterExit(numberProcedures,FALSE);
  9570. END;
  9571. INC(numberProcedures);
  9572. END;
  9573. END;
  9574. IF backend.cooperative THEN
  9575. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9576. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9577. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9578. (*! not required any more? check and delete!
  9579. PushSelfPointer();
  9580. CallThis(position,"Modules","SetInitialized",1);
  9581. *)
  9582. (*
  9583. SetLabel(end);
  9584. *)
  9585. END;
  9586. IF x # NIL THEN
  9587. SELF.position := x.position;
  9588. END;
  9589. CheckRegistersFree();
  9590. ASSERT(modifyAssignmentCounter = 0);
  9591. currentScope := prevScope;
  9592. IF Trace THEN TraceExit("Body") END;
  9593. END Body;
  9594. END ImplementationVisitor;
  9595. MetaDataGenerator=OBJECT
  9596. VAR
  9597. implementationVisitor: ImplementationVisitor;
  9598. declarationVisitor: DeclarationVisitor;
  9599. module: Sections.Module;
  9600. moduleName: ARRAY 128 OF CHAR;
  9601. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9602. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9603. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9604. TypeTags: LONGINT; (* type extension level support *)
  9605. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9606. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9607. BEGIN
  9608. IF implementationVisitor.backend.cooperative THEN
  9609. TypeTags := MAX(LONGINT); (* only 3 extensions allowed *)
  9610. BaseTypesTableOffset := 0;
  9611. MethodTableOffset := 2;
  9612. TypeRecordBaseOffset := 0;
  9613. ELSIF simple THEN
  9614. TypeTags := 3; (* only 3 extensions allowed *)
  9615. BaseTypesTableOffset := -1;
  9616. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9617. TypeRecordBaseOffset := TypeTags;
  9618. ELSE
  9619. TypeTags := 16;
  9620. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9621. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9622. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9623. END;
  9624. SELF.simple := simple;
  9625. SELF.implementationVisitor := implementationVisitor;
  9626. SELF.declarationVisitor := declarationVisitor;
  9627. implementationVisitor.meta := SELF;
  9628. declarationVisitor.meta := SELF;
  9629. END InitMetaDataGenerator;
  9630. PROCEDURE SetModule(module: Sections.Module);
  9631. BEGIN
  9632. SELF.module := module
  9633. END SetModule;
  9634. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9635. BEGIN
  9636. IF implementationVisitor.backend.cooperative THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9637. END GetTypeRecordBaseOffset;
  9638. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9639. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9640. BEGIN
  9641. INC(dataAdrOffset,6);
  9642. Info(section,"headerAdr");
  9643. Address(section,0);
  9644. Info(section,"typeDesc");
  9645. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9646. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9647. NamedSymbol(section, name, symbol, 0, offset);
  9648. Info(section,"mark: LONGINT;");
  9649. Longint(section,-1);
  9650. Info(section,"dataAdr-: ADDRESS");
  9651. Symbol(section,section, dataAdrOffset,0);
  9652. Info(section,"size-: SIZE");
  9653. Address(section,0);
  9654. Info(section,"nextRealtime: HeapBlock;");
  9655. Address(section,0);
  9656. END HeapBlock;
  9657. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9658. VAR i: LONGINT;
  9659. BEGIN
  9660. INC(dataAdrOffset,14);
  9661. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9662. Info(section,"count*: LONGINT");
  9663. Longint(section,0);
  9664. Info(section,"locked*: BOOLEAN");
  9665. Longint(section,0);
  9666. Info(section,"awaitingLock*: ProcessQueue");
  9667. Address(section,0);
  9668. Address(section,0);
  9669. Info(section,"awaitingCond*: ProcessQueue");
  9670. Address(section,0);
  9671. Address(section,0);
  9672. Info(section,"lockedBy*: ANY");
  9673. Address(section,0);
  9674. Info(section,"lock*: ANY");
  9675. Address(section,0);
  9676. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9677. Longint(section,1);
  9678. FOR i := 2 TO 6 DO
  9679. Longint(section,0);
  9680. END;
  9681. END ProtectedHeapBlock;
  9682. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9683. BEGIN
  9684. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9685. END Info;
  9686. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9687. VAR op: IntermediateCode.Operand;
  9688. BEGIN
  9689. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9690. section.Emit(Data(-11,op));
  9691. END Address;
  9692. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9693. VAR op: IntermediateCode.Operand;
  9694. BEGIN
  9695. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9696. section.Emit(Data(-12,op));
  9697. END Size;
  9698. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9699. VAR op: IntermediateCode.Operand;
  9700. BEGIN
  9701. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9702. section.Emit(Data(-1,op));
  9703. END Set;
  9704. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9705. VAR op: IntermediateCode.Operand;
  9706. BEGIN
  9707. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9708. section.Emit(Data(-1,op));
  9709. END Longint;
  9710. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9711. VAR op,noOperand: IntermediateCode.Operand;
  9712. BEGIN
  9713. IntermediateCode.InitOperand(noOperand);
  9714. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9715. section.PatchOperands(pc,op,noOperand,noOperand);
  9716. END PatchLongint;
  9717. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9718. VAR op, noOperand: IntermediateCode.Operand;
  9719. BEGIN
  9720. IntermediateCode.InitOperand(noOperand);
  9721. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9722. section.PatchOperands(pc,op,noOperand,noOperand);
  9723. END PatchSymbol;
  9724. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9725. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9726. BEGIN
  9727. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9728. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9729. section.Emit(Data(-1,op));
  9730. END Boolean;
  9731. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9732. VAR op: IntermediateCode.Operand;
  9733. BEGIN
  9734. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9735. section.Emit(Data(-1,op));
  9736. END Char;
  9737. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9738. VAR i: LONGINT;
  9739. BEGIN
  9740. Info(section,str);
  9741. i := 0;
  9742. WHILE(str[i] # 0X) DO
  9743. Char(section,str[i]);
  9744. INC(i);
  9745. END;
  9746. Char(section,0X);
  9747. END String;
  9748. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9749. VAR op: IntermediateCode.Operand;
  9750. BEGIN
  9751. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9752. IntermediateCode.SetOffset(op,realOffset);
  9753. section.Emit(Data(-1,op));
  9754. END NamedSymbol;
  9755. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9756. BEGIN
  9757. IF symbol= NIL THEN
  9758. Address( section, realOffset);
  9759. ASSERT(virtualOffset = 0);
  9760. ELSE
  9761. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9762. END;
  9763. END Symbol;
  9764. (* OutPointers delivers
  9765. {pointerOffset}
  9766. *)
  9767. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9768. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9769. BEGIN
  9770. type := type.resolved;
  9771. IF type IS SyntaxTree.AnyType THEN
  9772. Symbol(section, symbol, 0, (offset ));
  9773. INC(numberPointers);
  9774. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9775. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9776. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9777. ELSIF type IS SyntaxTree.PointerType THEN
  9778. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9779. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9780. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9781. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9782. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9783. ELSIF (type IS SyntaxTree.RecordType) THEN
  9784. (* never treat a record like a pointer, even if the pointer field is set! *)
  9785. WITH type: SyntaxTree.RecordType DO
  9786. base := type.GetBaseRecord();
  9787. IF base # NIL THEN
  9788. Pointers(offset,symbol,section, base,numberPointers);
  9789. END;
  9790. variable := type.recordScope.firstVariable;
  9791. WHILE(variable # NIL) DO
  9792. IF ~(variable.untraced) THEN
  9793. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9794. END;
  9795. variable := variable.nextVariable;
  9796. END;
  9797. END;
  9798. ELSIF (type IS SyntaxTree.CellType) THEN
  9799. WITH type: SyntaxTree.CellType DO
  9800. base := type.GetBaseRecord();
  9801. IF base # NIL THEN
  9802. Pointers(offset,symbol,section, base,numberPointers);
  9803. END;
  9804. variable := type.cellScope.firstVariable;
  9805. WHILE(variable # NIL) DO
  9806. IF ~(variable.untraced) THEN
  9807. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9808. END;
  9809. variable := variable.nextVariable;
  9810. END;
  9811. END;
  9812. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9813. WITH type: SyntaxTree.ArrayType DO
  9814. IF type.form= SyntaxTree.Static THEN
  9815. n := type.staticLength;
  9816. base := type.arrayBase.resolved;
  9817. WHILE(base IS SyntaxTree.ArrayType) DO
  9818. type := base(SyntaxTree.ArrayType);
  9819. n := n* type.staticLength;
  9820. base := type.arrayBase.resolved;
  9821. END;
  9822. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9823. IF SemanticChecker.ContainsPointer(base) THEN
  9824. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9825. FOR i := 0 TO n-1 DO
  9826. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9827. END;
  9828. END;
  9829. ELSE
  9830. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9831. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9832. END;
  9833. END;
  9834. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9835. WITH type: SyntaxTree.MathArrayType DO
  9836. IF type.form = SyntaxTree.Static THEN
  9837. n := type.staticLength;
  9838. base := type.arrayBase.resolved;
  9839. WHILE(base IS SyntaxTree.MathArrayType) DO
  9840. type := base(SyntaxTree.MathArrayType);
  9841. n := n* type.staticLength;
  9842. base := type.arrayBase.resolved;
  9843. END;
  9844. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9845. IF SemanticChecker.ContainsPointer(base) THEN
  9846. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9847. FOR i := 0 TO n-1 DO
  9848. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9849. END;
  9850. END;
  9851. ELSE
  9852. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9853. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9854. END
  9855. END;
  9856. (* ELSE no pointers in type *)
  9857. END;
  9858. END Pointers;
  9859. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9860. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9861. BEGIN
  9862. IF pool.Has(name) THEN
  9863. RETURN pool.GetInt(name)
  9864. ELSE
  9865. position := source.pc;
  9866. pool.PutInt(name, position);
  9867. StringPool.GetString(name, s);
  9868. Info(source, s);
  9869. i := 0;
  9870. REPEAT
  9871. ch := s[i]; INC(i);
  9872. Char( source, ch);
  9873. UNTIL ch = 0X;
  9874. END;
  9875. RETURN position;
  9876. END DynamicName;
  9877. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9878. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9879. BEGIN
  9880. COPY(moduleName,name);
  9881. Strings.Append(name,suffix);
  9882. Basic.ToSegmentedName(name, pooledName);
  9883. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9884. IF implementationVisitor.backend.cooperative THEN
  9885. Info(section, "TypeDescriptor");
  9886. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9887. NamedSymbol(section, pooledName,NIL, 0, 0);
  9888. BasePointer(section);
  9889. offset := 0;
  9890. ELSE
  9891. HeapBlock(mName,typeName,section,2);
  9892. Info(section, "HeapBlock");
  9893. (*
  9894. Symbol(section,section,2,0);
  9895. *)
  9896. Address(section,0); (* empty such that GC does not go on traversing *)
  9897. Info(section, "TypeDescriptor");
  9898. Address(section,0);
  9899. offset := section.pc;
  9900. END;
  9901. RETURN section
  9902. END Block;
  9903. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9904. VAR name: Basic.SegmentedName;
  9905. BEGIN
  9906. Info(source,"ArrayHeader");
  9907. IF implementationVisitor.backend.cooperative THEN
  9908. sizePC := source.pc;
  9909. Address(source,0);
  9910. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9911. IF baseType # "" THEN
  9912. Basic.ToSegmentedName(baseType, name);
  9913. NamedSymbol(source, name,NIL, 0, 0);
  9914. ELSE
  9915. Address(source,0);
  9916. END;
  9917. Address(source,0);
  9918. ELSE
  9919. Address(source,0);
  9920. Address(source,0);
  9921. Address(source,0);
  9922. sizePC := source.pc;
  9923. Address(source,0);
  9924. Info(source,"array data");
  9925. END;
  9926. END Array;
  9927. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9928. BEGIN
  9929. IF implementationVisitor.backend.cooperative THEN
  9930. PatchLongint(section, pc, size);
  9931. PatchLongint(section, pc + 3, size);
  9932. ELSE
  9933. PatchLongint(section, pc, size);
  9934. END;
  9935. END PatchArray;
  9936. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9937. VAR
  9938. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9939. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9940. poolMap: Basic.HashTableInt;
  9941. namePool: IntermediateCode.Section;
  9942. namePoolOffset: LONGINT;
  9943. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9944. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9945. BEGIN
  9946. Basic.SegmentedNameToString(s1.name,n1);
  9947. Basic.SegmentedNameToString(s2.name,n2);
  9948. index := 0;
  9949. ch1 := n1[index];
  9950. ch2 := n2[index];
  9951. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9952. INC(index);
  9953. ch1 := n1[index];
  9954. ch2 := n2[index];
  9955. END;
  9956. RETURN ch1 < ch2;
  9957. END Compare;
  9958. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9959. VAR
  9960. i, j: LONGINT;
  9961. x, t: Sections.Section;
  9962. BEGIN
  9963. IF lo < hi THEN
  9964. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9965. WHILE i <= j DO
  9966. WHILE Compare(list[i], x) DO INC(i) END;
  9967. WHILE Compare(x, list[j]) DO DEC(j) END;
  9968. IF i <= j THEN
  9969. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9970. INC(i); DEC(j)
  9971. END
  9972. END;
  9973. IF lo < j THEN QuickSort(list, lo, j) END;
  9974. IF i < hi THEN QuickSort(list, i, hi) END
  9975. END;
  9976. END QuickSort;
  9977. (*
  9978. ExportDesc* = RECORD
  9979. fp*: ADDRESS;
  9980. name* {UNTRACED}: DynamicName;
  9981. adr*: ADDRESS;
  9982. exports*: LONGINT;
  9983. dsc* {UNTRACED}: ExportArray
  9984. END;
  9985. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  9986. *)
  9987. PROCEDURE ExportDesc2(
  9988. source: IntermediateCode.Section;
  9989. namePool: IntermediateCode.Section;
  9990. fingerPrinter: FingerPrinter.FingerPrinter;
  9991. symbol: Sections.Section;
  9992. name: StringPool.Index;
  9993. VAR patchAdr: LONGINT
  9994. ): BOOLEAN;
  9995. VAR fingerPrint: SyntaxTree.FingerPrint;
  9996. BEGIN
  9997. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  9998. & (symbol.type # Sections.InlineCodeSection)
  9999. THEN
  10000. *)
  10001. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  10002. & (symbol.type # Sections.InlineCodeSection))
  10003. THEN
  10004. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  10005. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  10006. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  10007. Longint(source,fingerPrint.shallow);
  10008. ELSE
  10009. Longint(source, 0);
  10010. END;
  10011. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10012. Symbol(source, symbol,0,0);
  10013. patchAdr := source.pc;
  10014. Address(source,0);
  10015. Address(source,0);
  10016. END;
  10017. RETURN TRUE
  10018. ELSE
  10019. RETURN FALSE
  10020. END;
  10021. END ExportDesc2;
  10022. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10023. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10024. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10025. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10026. TYPE
  10027. Scope = RECORD
  10028. elements: LONGINT;
  10029. gelements: LONGINT;
  10030. section: IntermediateCode.Section;
  10031. patchAdr: LONGINT;
  10032. arraySizePC: LONGINT;
  10033. beginPC: LONGINT; (* current scope start pc *)
  10034. END;
  10035. BEGIN
  10036. Basic.InitSegmentedName(prev);
  10037. olevel := -1;
  10038. scopes[0].section := source;
  10039. FOR s := 0 TO LEN(sections)-1 DO
  10040. symbol := sections[s];
  10041. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10042. this := sections[s].name;
  10043. level := 0;
  10044. WHILE (this[level] > 0) DO
  10045. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10046. INC(level);
  10047. END;
  10048. WHILE level < olevel DO
  10049. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10050. IF olevel > 0 THEN
  10051. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10052. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10053. END;
  10054. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10055. DEC(olevel);
  10056. END;
  10057. IF (this[level] > 0) THEN
  10058. IF level > olevel THEN
  10059. (*TRACE("opening",level); *)
  10060. IF scopes[level].section = NIL THEN
  10061. arrayName := ".@ExportArray";
  10062. Strings.AppendInt(arrayName, level);
  10063. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10064. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10065. END;
  10066. scopes[level].beginPC := scopes[level].section.pc;
  10067. olevel := level;
  10068. scopes[olevel].elements := 0;
  10069. END;
  10070. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10071. sym := sections[s];
  10072. ELSE
  10073. sym := NIL;
  10074. END;
  10075. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10076. THEN
  10077. INC(scopes[olevel].elements);
  10078. END;
  10079. (*StringPool.GetString(this[level], name);*)
  10080. (*TRACE(level, name);*)
  10081. (* enter string in scope *)
  10082. INC(level);
  10083. END;
  10084. END;
  10085. Basic.SegmentedNameToString(this, name);
  10086. (*TRACE(level, "enter", name);*)
  10087. prev := this;
  10088. END;
  10089. END;
  10090. WHILE 0 <= olevel DO
  10091. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10092. IF olevel > 0 THEN
  10093. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10094. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10095. END;
  10096. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10097. DEC(olevel);
  10098. END;
  10099. level := 0;
  10100. WHILE (level < LEN(scopes)) DO
  10101. IF scopes[level].section # NIL THEN
  10102. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10103. END;
  10104. INC(level);
  10105. END;
  10106. END Export;
  10107. BEGIN
  10108. NEW(fingerPrinter, module.system);
  10109. NEW(poolMap, 64);
  10110. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10111. NEW(sectionArray, module.allSections.Length());
  10112. FOR i := 0 TO module.allSections.Length() - 1 DO
  10113. section := module.allSections.GetSection(i);
  10114. sectionArray[i] := section;
  10115. END;
  10116. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10117. Export(sectionArray^);
  10118. END ExportDesc;
  10119. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10120. VAR
  10121. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10122. BEGIN
  10123. Info(source, "exception table offsets array descriptor");
  10124. size := 0;
  10125. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10126. Info(source, "exception table content");
  10127. FOR i := 0 TO module.allSections.Length() - 1 DO
  10128. p := module.allSections.GetSection(i);
  10129. IF p.type = Sections.CodeSection THEN
  10130. finallyPC := p(IntermediateCode.Section).finally;
  10131. IF finallyPC>=0 THEN
  10132. Symbol( source, p, 0,0);
  10133. Symbol( source, p, finallyPC, 0);
  10134. Symbol( source, p, finallyPC,0);
  10135. INC(size);
  10136. END;
  10137. END
  10138. END;
  10139. PatchArray(source,sizePC,size);
  10140. END ExceptionArray;
  10141. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10142. VAR i: LONGINT; ch: CHAR;
  10143. BEGIN
  10144. i := 0;
  10145. REPEAT
  10146. ch := name[i]; INC(i);
  10147. Char( section, ch);
  10148. UNTIL ch = 0X;
  10149. WHILE i < 32 DO
  10150. Char( section, 0X); INC(i);
  10151. END;
  10152. END Name;
  10153. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10154. VAR i: LONGINT; ch: CHAR;
  10155. BEGIN
  10156. i := 0;
  10157. REPEAT
  10158. ch := name[i]; INC(i);
  10159. Char( section, ch);
  10160. UNTIL ch = 0X;
  10161. WHILE i < 128 DO
  10162. Char( section, 0X); INC(i);
  10163. END;
  10164. END LongName;
  10165. PROCEDURE References(section: IntermediateCode.Section);
  10166. CONST
  10167. rfDirect = 1X; rfIndirect = 3X;
  10168. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10169. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10170. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10171. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10172. rfArrayFlag = 80X;
  10173. VAR
  10174. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10175. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10176. VAR char: CHAR;
  10177. BEGIN
  10178. IF type = NIL THEN char := rfLongint
  10179. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10180. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10181. ELSIF type IS SyntaxTree.CharacterType THEN
  10182. IF type.sizeInBits = 8 THEN char := rfChar8
  10183. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10184. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10185. END;
  10186. ELSIF type IS SyntaxTree.IntegerType THEN
  10187. IF type.sizeInBits = 8 THEN char := rfShortint
  10188. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10189. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10190. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10191. END;
  10192. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10193. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10194. ELSIF type IS SyntaxTree.FloatType THEN
  10195. IF type.sizeInBits = 32 THEN char := rfReal
  10196. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10197. END;
  10198. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10199. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10200. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10201. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10202. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10203. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10204. END;
  10205. IF arrayOf THEN
  10206. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10207. ELSE
  10208. Char(section,char)
  10209. END;
  10210. INC(size);
  10211. END BaseType;
  10212. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10213. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10214. BEGIN
  10215. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10216. IF destination = NIL THEN
  10217. (* imported unused record type *)
  10218. Char(section,0X); (* nil type *)
  10219. INC(size);
  10220. type.typeDeclaration.GetName(name);
  10221. (*
  10222. 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
  10223. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10224. *)
  10225. ELSE
  10226. IF type.pointerType # NIL THEN
  10227. Char(section,rfRecordPointer)
  10228. ELSE
  10229. Char(section,rfRecord);
  10230. END;
  10231. INC(size);
  10232. Longint(section,(destination.offset ));
  10233. INC(size,4);
  10234. END;
  10235. END RecordType;
  10236. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10237. BEGIN
  10238. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10239. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10240. INC(size);
  10241. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10242. END;
  10243. IF type.form = SyntaxTree.Static THEN
  10244. Longint(section,type.staticLength)
  10245. ELSE
  10246. Longint(section,0)
  10247. END;
  10248. INC(size,4);
  10249. END ArrayType;
  10250. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10251. BEGIN
  10252. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10253. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType THEN
  10254. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10255. INC(size);
  10256. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10257. END;
  10258. IF type.form = SyntaxTree.Static THEN
  10259. Longint(section,type.staticLength)
  10260. ELSE
  10261. Longint(section,0)
  10262. END;
  10263. INC(size,4);
  10264. END MathArrayType;
  10265. PROCEDURE Type(type: SyntaxTree.Type);
  10266. BEGIN
  10267. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10268. IF type IS SyntaxTree.BasicType THEN
  10269. BaseType(FALSE,type)
  10270. ELSIF type IS SyntaxTree.RecordType THEN
  10271. RecordType(type(SyntaxTree.RecordType));
  10272. ELSIF type IS SyntaxTree.ArrayType THEN
  10273. ArrayType(type(SyntaxTree.ArrayType))
  10274. ELSIF type IS SyntaxTree.EnumerationType THEN
  10275. BaseType(FALSE,module.system.longintType)
  10276. ELSIF type IS SyntaxTree.PointerType THEN
  10277. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10278. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10279. ELSE
  10280. BaseType(FALSE,type)
  10281. END;
  10282. ELSIF type IS SyntaxTree.ProcedureType THEN
  10283. BaseType(FALSE,type);
  10284. ELSIF type IS SyntaxTree.MathArrayType THEN
  10285. MathArrayType(type(SyntaxTree.MathArrayType));
  10286. ELSIF type IS SyntaxTree.CellType THEN
  10287. BaseType(FALSE,module.system.anyType);
  10288. ELSE HALT(200)
  10289. END;
  10290. END Type;
  10291. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10292. VAR name: ARRAY 256 OF CHAR;
  10293. BEGIN
  10294. IF variable.externalName # NIL THEN RETURN END;
  10295. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10296. INC(size);
  10297. variable.GetName(name);
  10298. Type(variable.type);
  10299. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10300. INC(size,4);
  10301. String(section,name);
  10302. INC(size,Strings.Length(name)+1);
  10303. END WriteVariable;
  10304. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10305. VAR name: ARRAY 256 OF CHAR;
  10306. BEGIN
  10307. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10308. INC(size);
  10309. variable.GetName(name);
  10310. Type(variable.type);
  10311. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10312. INC(size,4);
  10313. variable.GetName(name);
  10314. String(section,name);
  10315. INC(size,Strings.Length(name)+1);
  10316. END WriteParameter;
  10317. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10318. BEGIN
  10319. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10320. IF type IS SyntaxTree.ArrayType THEN
  10321. WITH type: SyntaxTree.ArrayType DO
  10322. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10323. ELSE Char(section,rfOpenArray)
  10324. END;
  10325. INC(size);
  10326. END
  10327. ELSIF type IS SyntaxTree.MathArrayType THEN
  10328. WITH type: SyntaxTree.MathArrayType DO
  10329. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10330. ELSE Char(section,rfOpenArray)
  10331. END;
  10332. INC(size);
  10333. END
  10334. ELSIF type IS SyntaxTree.RecordType THEN
  10335. Char(section,rfRecord);
  10336. INC(size);
  10337. ELSE
  10338. BaseType(FALSE,type);
  10339. END;
  10340. END ReturnType;
  10341. PROCEDURE Procedure(s: Sections.Section);
  10342. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10343. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10344. name: ARRAY 256 OF CHAR;
  10345. BEGIN
  10346. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10347. procedure.GetName(name);
  10348. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10349. Char(section,0F9X);
  10350. INC(size);
  10351. Symbol(section,s,0,0);
  10352. INC(size,4);
  10353. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10354. INC(size,4);
  10355. Longint(section,procedureType.numberParameters);
  10356. INC(size,4);
  10357. ReturnType(procedureType.returnType);
  10358. Longint(section,0); (*! level *)
  10359. INC(size,4);
  10360. Longint(section,0);
  10361. INC(size,4);
  10362. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10363. (*
  10364. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10365. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10366. recordName := "";
  10367. IF record.pointerType # NIL THEN
  10368. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10369. ELSE
  10370. DeclarationName(record.typeDeclaration,recordName);
  10371. END;
  10372. i := 0;
  10373. Info(section,recordName);
  10374. WHILE recordName[i] # 0X DO
  10375. Char(section,recordName[i]); INC(i);
  10376. INC(size);
  10377. END;
  10378. Char(section,".");
  10379. INC(size);
  10380. END;
  10381. *)
  10382. String(section,name);
  10383. INC(size,Strings.Length(name)+1);
  10384. parameter := procedureType.firstParameter;
  10385. WHILE(parameter # NIL) DO
  10386. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10387. parameter := parameter.nextParameter;
  10388. END;
  10389. variable := procedure.procedureScope.firstVariable;
  10390. WHILE(variable # NIL) DO
  10391. WriteVariable(variable,FALSE);
  10392. variable := variable.nextVariable;
  10393. END;
  10394. END Procedure;
  10395. PROCEDURE Scope(s: Sections.Section);
  10396. BEGIN
  10397. Char(section,0F8X);
  10398. INC(size);
  10399. Symbol(section,s,0,0); (* start *)
  10400. INC(size,4);
  10401. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10402. INC(size,4);
  10403. String(section,"$$");
  10404. INC(size,3);
  10405. (* removed variables -- wrongly interpreted by Reflection
  10406. variable := module.module.moduleScope.firstVariable;
  10407. WHILE(variable # NIL) DO
  10408. WriteVariable(variable,FALSE);
  10409. variable := variable.nextVariable;
  10410. END;
  10411. *)
  10412. END Scope;
  10413. BEGIN
  10414. Array(section,sizePC,"");
  10415. size := 0;
  10416. Char(section,0FFX); (* sign for trap writer *)
  10417. INC(size);
  10418. FOR i := 0 TO module.allSections.Length() - 1 DO
  10419. s := module.allSections.GetSection(i);
  10420. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10421. Scope(s) (*! must be first procedure in ref section *)
  10422. END
  10423. END;
  10424. FOR i := 0 TO module.allSections.Length() - 1 DO
  10425. s := module.allSections.GetSection(i);
  10426. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10427. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10428. Procedure(s)
  10429. END
  10430. END;
  10431. PatchArray(section,sizePC,size);
  10432. END References;
  10433. (*
  10434. Command* = RECORD
  10435. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10436. name*: Name; (* name of the procedure *)
  10437. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10438. entryAdr* : ADDRESS; (* entry address of procedure *)
  10439. END;
  10440. *)
  10441. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10442. VAR
  10443. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10444. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10445. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10446. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10447. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10448. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10449. BEGIN
  10450. RETURN
  10451. (type = NIL) OR
  10452. (type.resolved IS SyntaxTree.RecordType) OR
  10453. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10454. (type.resolved IS SyntaxTree.AnyType);
  10455. END TypeAllowed;
  10456. BEGIN
  10457. numberParameters := procedureType.numberParameters;
  10458. RETURN
  10459. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10460. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10461. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10462. END GetProcedureAllowed;
  10463. PROCEDURE WriteType(type : SyntaxTree.Type);
  10464. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10465. name: Basic.SegmentedName; offset: LONGINT;
  10466. BEGIN
  10467. IF type = NIL THEN
  10468. Address(source,0);
  10469. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10470. Address(source,1);
  10471. ELSE
  10472. type := type.resolved;
  10473. IF type IS SyntaxTree.PointerType THEN
  10474. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10475. END;
  10476. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10477. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10478. name[0] := typeDeclaration.name; name[1] := -1;
  10479. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10480. ASSERT(section # NIL);
  10481. ELSE
  10482. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10483. (* TODO *)
  10484. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10485. END;
  10486. IF implementationVisitor.backend.cooperative THEN
  10487. offset := 0;
  10488. ELSE
  10489. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10490. END;
  10491. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10492. END;
  10493. END WriteType;
  10494. BEGIN
  10495. Info(source, "command array descriptor");
  10496. Array(source,sizePC,"Modules.Command");
  10497. numberCommands := 0;
  10498. Info(source, "command array content");
  10499. FOR i := 0 TO module.allSections.Length() - 1 DO
  10500. p := module.allSections.GetSection(i);
  10501. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10502. procedure := p.symbol(SyntaxTree.Procedure);
  10503. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10504. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10505. procedure.GetName(name);
  10506. Name(source,name);
  10507. numberParameters := procedureType.numberParameters;
  10508. (* offset of type of first parameter *)
  10509. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10510. ELSE WriteType(procedureType.firstParameter.type)
  10511. END;
  10512. (* offset of type of return parameter *)
  10513. WriteType(procedureType.returnType);
  10514. (* command name *)
  10515. (* command code offset *)
  10516. Symbol(source,p,0,0);
  10517. INC(numberCommands);
  10518. IF Trace THEN
  10519. D.Ln;
  10520. END;
  10521. END;
  10522. END
  10523. END;
  10524. PatchArray(source,sizePC,numberCommands);
  10525. END CommandArray;
  10526. (* to prevent from double import of different module aliases *)
  10527. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10528. VAR i: SyntaxTree.Import;
  10529. BEGIN
  10530. i := module.module.moduleScope.firstImport;
  10531. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10532. i := i.nextImport;
  10533. END;
  10534. RETURN i = import
  10535. END IsFirstDirectOccurence;
  10536. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10537. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10538. BEGIN
  10539. Array(source,pc,"");
  10540. Info(source, "import module array data");
  10541. IF implementationVisitor.backend.cooperative THEN
  10542. offset := 0;
  10543. ELSE
  10544. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10545. END;
  10546. import := module.module.moduleScope.firstImport;
  10547. numberImports := 0;
  10548. WHILE import # NIL DO
  10549. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10550. Global.GetModuleSegmentedName(import.module,name);
  10551. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10552. NamedSymbol(source, name, NIL, 0, offset);
  10553. INC(numberImports);
  10554. END;
  10555. import := import.nextImport
  10556. END;
  10557. PatchArray(source,pc,numberImports);
  10558. END ImportsArray;
  10559. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10560. VAR
  10561. p: Sections.Section; sizePC, size, i: LONGINT;
  10562. BEGIN
  10563. Info(source, "Type info section");
  10564. size := 0;
  10565. Array(source,sizePC,"Modules.TypeDesc");
  10566. FOR i := 0 TO module.allSections.Length() - 1 DO
  10567. p := module.allSections.GetSection(i);
  10568. WITH p: IntermediateCode.Section DO
  10569. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10570. Symbol(source,p,0,0);
  10571. INC(size);
  10572. END;
  10573. END
  10574. END;
  10575. PatchArray(source,sizePC,size);
  10576. END TypeInfoSection;
  10577. (*
  10578. ProcTableEntry* = RECORD
  10579. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10580. noPtr*: LONGINT;
  10581. END;
  10582. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10583. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10584. *)
  10585. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10586. VAR
  10587. destination: Sections.Section;
  10588. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10589. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10590. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10591. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10592. BEGIN
  10593. Info(procArray,"pcFrom");
  10594. Symbol(procArray,destination,0,0);
  10595. Info(procArray,"pcTo");
  10596. Symbol(procArray,destination,destination.pc,0);
  10597. Info(procArray,"pcStatementBegin");
  10598. Symbol(procArray,destination,destination.validPAFEnter,0);
  10599. Info(procArray,"pcStatementEnd");
  10600. Symbol(procArray,destination,destination.validPAFExit,0);
  10601. IF ~implementationVisitor.backend.cooperative THEN
  10602. Basic.SegmentedNameToString(destination.name, string);
  10603. Info(pointerArray,string);
  10604. procedure := destination.symbol(SyntaxTree.Procedure);
  10605. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10606. variable := procedure.procedureScope.firstVariable;
  10607. WHILE(variable # NIL) DO
  10608. IF ~(variable.untraced) THEN
  10609. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10610. END;
  10611. variable := variable.nextVariable
  10612. END;
  10613. parameter := procedureType.firstParameter;
  10614. WHILE(parameter # NIL) DO
  10615. IF ~(parameter.untraced) THEN
  10616. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10617. END;
  10618. parameter := parameter.nextParameter;
  10619. END;
  10620. END;
  10621. Info(procArray,"numberPointers");
  10622. Longint(procArray,numberPointers);
  10623. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10624. INC(pointerArraySize, numberPointers);
  10625. END PointerOffsets;
  10626. BEGIN
  10627. maxPointers := 0;
  10628. Info(procArray, "proc array descriptor");
  10629. Address(procArray,0);
  10630. Address(procArray,0);
  10631. Address(procArray,0);
  10632. procArraySizePC := procArray.pc;
  10633. Address(procArray,0);
  10634. procArraySize := 0;
  10635. IF ~implementationVisitor.backend.cooperative THEN
  10636. Info(pointerArray, "pointer array descriptor");
  10637. Address(pointerArray,0);
  10638. Address(pointerArray,0);
  10639. Address(pointerArray,0);
  10640. pointerArraySizePC := pointerArray.pc;
  10641. Address(pointerArray,0);
  10642. pointerArraySize := 0;
  10643. END;
  10644. procArraySize := 0;
  10645. FOR i := 0 TO module.allSections.Length() - 1 DO
  10646. destination := module.allSections.GetSection(i);
  10647. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10648. PointerOffsets(destination(IntermediateCode.Section));
  10649. INC(procArraySize);
  10650. END
  10651. END;
  10652. PatchLongint(procArray,procArraySizePC,procArraySize);
  10653. IF ~implementationVisitor.backend.cooperative THEN
  10654. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10655. END;
  10656. END PointersInProcTables;
  10657. (*
  10658. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10659. VAR
  10660. next*: Module; (** once a module is published, all fields are read-only *)
  10661. name*: Name;
  10662. init, published: BOOLEAN;
  10663. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10664. sb*: ADDRESS; <- set to beginning of data section by loader
  10665. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10666. command*: POINTER TO ARRAY OF Command;
  10667. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10668. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10669. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10670. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10671. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10672. data*, code*: Bytes;
  10673. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10674. export*: ExportDesc;
  10675. term*: TerminationHandler;
  10676. exTable*: ExceptionTable;
  10677. noProcs*: LONGINT;
  10678. firstProc*: ADDRESS; <- done by loader
  10679. maxPtrs*: LONGINT;
  10680. crc*: LONGINT;
  10681. *)
  10682. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10683. BEGIN
  10684. Info(section, "cycle");
  10685. Size(section,0);
  10686. Info(section, "references");
  10687. Size(section,0);
  10688. Info(section, "nextMarked");
  10689. Address(section,0);
  10690. Info(section, "nextWatched");
  10691. Address(section,0);
  10692. END BasePointer;
  10693. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10694. BEGIN
  10695. BasePointer(section);
  10696. Info(section, "action");
  10697. Address(section,0);
  10698. Info(section, "monitor");
  10699. Address(section,0);
  10700. END BaseObject;
  10701. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10702. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10703. pooledName: Basic.SegmentedName;
  10704. symbol: SyntaxTree.Symbol;
  10705. BEGIN
  10706. Global.GetModuleName(module.module,name);
  10707. Strings.Append(name,".@Module.@Descriptor");
  10708. Basic.ToSegmentedName(name, pooledName);
  10709. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10710. Symbol(section,descriptorSection,0,0);
  10711. Info(descriptorSection, "descriptor");
  10712. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10713. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10714. Address(descriptorSection,0);
  10715. Global.GetModuleName(module.module,name);
  10716. Strings.Append(name,".@Trace");
  10717. Basic.ToSegmentedName(name, pooledName);
  10718. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10719. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10720. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10721. END ModuleDescriptor;
  10722. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10723. VAR name: ARRAY 128 OF CHAR;
  10724. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10725. symbol: SyntaxTree.Symbol;
  10726. BEGIN
  10727. Global.GetModuleName(module.module,name);
  10728. Strings.Append(name,".@Module");
  10729. Basic.ToSegmentedName(name, pooledName);
  10730. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10731. moduleSection.SetExported(TRUE);
  10732. IF moduleSection.pc = 0 THEN
  10733. IF implementationVisitor.backend.cooperative THEN
  10734. Info(moduleSection, "descriptor");
  10735. ModuleDescriptor(moduleSection);
  10736. BaseObject(moduleSection);
  10737. implementationVisitor.CreateTraceModuleMethod(module.module);
  10738. ELSE
  10739. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10740. Info(moduleSection, "HeapBlock");
  10741. Symbol(moduleSection,moduleSection,2,0);
  10742. Info(moduleSection, "TypeDescriptor");
  10743. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10744. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10745. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10746. END;
  10747. END;
  10748. RETURN moduleSection;
  10749. END ModuleSection;
  10750. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10751. VAR
  10752. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10753. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10754. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10755. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10756. referenceSectionOffset : LONGINT;
  10757. BEGIN
  10758. Global.GetModuleName(module.module,moduleName);
  10759. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10760. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10761. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10762. ImportsArray(importSection);
  10763. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10764. CommandArray(commandsSection);
  10765. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10766. ExceptionArray(exceptionSection);
  10767. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10768. TypeInfoSection(typeInfoSection);
  10769. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10770. References(referenceSection);
  10771. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10772. IF ~implementationVisitor.backend.cooperative THEN
  10773. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10774. ELSE
  10775. ptrTableSection := NIL;
  10776. END;
  10777. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10778. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10779. Array(emptyArraySection,temp,"");
  10780. moduleSection := ModuleSection();
  10781. Info(moduleSection, "nextRoot*: RootObject");
  10782. Address(moduleSection,0);
  10783. Info(moduleSection, "next*: Module");
  10784. Address(moduleSection,0);
  10785. Info(moduleSection, "name*: Name");
  10786. Name(moduleSection,moduleName);
  10787. Info(moduleSection, "init, published: BOOLEAN");
  10788. Boolean(moduleSection,FALSE);
  10789. Boolean(moduleSection,FALSE);
  10790. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10791. Boolean(moduleSection,FALSE);
  10792. Boolean(moduleSection,FALSE);
  10793. Info(moduleSection, "refcnt*: LONGINT");
  10794. Longint(moduleSection,0);
  10795. Info(moduleSection, "sb*: ADDRESS");
  10796. Address(moduleSection,0);
  10797. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10798. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10799. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10800. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10801. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10802. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10803. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10804. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10805. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10806. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10807. Info(moduleSection, "procTable*: ProcTable");
  10808. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10809. Info(moduleSection, "ptrTable*: PtrTable");
  10810. IF ~implementationVisitor.backend.cooperative THEN
  10811. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10812. ELSE
  10813. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10814. END;
  10815. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10816. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10817. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10818. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10819. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10820. Info(moduleSection, "export*: ExportDesc");
  10821. ExportDesc(moduleSection);
  10822. Info(moduleSection, "term*: TerminationHandler");
  10823. Address(moduleSection,0);
  10824. Info(moduleSection, "exTable*: ExceptionTable");
  10825. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10826. Info(moduleSection, "noProcs*: LONGINT");
  10827. Longint(moduleSection,numberProcs);
  10828. Info(moduleSection, "firstProc*: ADDRESS");
  10829. Address(moduleSection,0);
  10830. Info(moduleSection, "maxPtrs*: LONGINT");
  10831. Longint(moduleSection,maxPointers);
  10832. Info(moduleSection, "crc*: LONGINT");
  10833. Longint(moduleSection, 0); (*! must be implemented *)
  10834. Info(moduleSection, "body*: ADDRESS");
  10835. Symbol(moduleSection, bodyProc, 0,0);
  10836. IF implementationVisitor.backend.cooperative THEN
  10837. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10838. Info(moduleSection, "monitor.owner");
  10839. Address(moduleSection,0);
  10840. Info(moduleSection, "monitor.nestingLevel");
  10841. Address(moduleSection,0);
  10842. Info(moduleSection, "monitor.blockedQueue");
  10843. Address(moduleSection,0); Address(moduleSection,0);
  10844. Info(moduleSection, "monitor.waitingQueue");
  10845. Address(moduleSection,0); Address(moduleSection,0);
  10846. Info(moduleSection, "monitor.waitingSentinel");
  10847. Address(moduleSection,0);
  10848. END;
  10849. END Module;
  10850. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10851. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10852. BEGIN
  10853. Array(source,pc,"");
  10854. Info(source, "pointer offsets array data");
  10855. IF scope IS SyntaxTree.RecordScope THEN
  10856. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10857. ELSIF scope IS SyntaxTree.CellScope THEN
  10858. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10859. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10860. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10861. WHILE variable # NIL DO
  10862. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10863. symbol := module.allSections.FindBySymbol(variable);
  10864. ASSERT(symbol # NIL);
  10865. Pointers(0,symbol, source,variable.type,numberPointers);
  10866. END;
  10867. variable := variable.nextVariable;
  10868. END;
  10869. END;
  10870. PatchArray(source,pc,numberPointers);
  10871. END PointerArray;
  10872. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10873. VAR recordType: SyntaxTree.RecordType;
  10874. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10875. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10876. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10877. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10878. segmentedName: Basic.SegmentedName;
  10879. td: SyntaxTree.TypeDeclaration;
  10880. BEGIN
  10881. Array(source,pc,"Modules.FieldEntry");
  10882. Info(source, "FieldArray");
  10883. IF x IS SyntaxTree.RecordType THEN
  10884. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10885. ELSE
  10886. variable := NIL;
  10887. END;
  10888. size :=0;
  10889. WHILE variable # NIL DO
  10890. type := variable.type.resolved;
  10891. Info(source,"offset");
  10892. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10893. Info(source,"type class");
  10894. IF type IS SyntaxTree.PointerType THEN
  10895. Size(source, 1);
  10896. ELSIF type IS SyntaxTree.RecordType THEN
  10897. Size(source, 2);
  10898. ELSIF type IS SyntaxTree.NumberType THEN
  10899. Size(source, 3);
  10900. ELSE
  10901. Size(source, 0);
  10902. END;
  10903. Info(source, "type desc");
  10904. IF type IS SyntaxTree.RecordType THEN
  10905. td := type(SyntaxTree.RecordType).typeDeclaration;
  10906. Global.GetSymbolSegmentedName(td,segmentedName);
  10907. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10908. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10909. ELSE
  10910. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10911. END;
  10912. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10913. Symbol(source, tir, 0, offset);
  10914. ELSE
  10915. Address(source, 0);
  10916. END;
  10917. Info(source,"flags");
  10918. Set(source, {});
  10919. Info(source,"name");
  10920. variable.GetName(name);
  10921. LongName(source, name);
  10922. variable := variable.nextVariable;
  10923. INC(size);
  10924. END;
  10925. PatchArray(source,pc,size);
  10926. END FieldArray;
  10927. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10928. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10929. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10930. sectionName: Basic.SectionName;
  10931. CONST MPO=-40000000H;
  10932. BEGIN
  10933. (*
  10934. TypeDesc* = POINTER TO RECORD
  10935. descSize: LONGINT;
  10936. sentinel: LONGINT; (* = MPO-4 *)
  10937. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10938. flags*: SET;
  10939. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10940. name*: Name;
  10941. END;
  10942. *)
  10943. (* source := module.sections.FindByName(...) *)
  10944. Global.GetSymbolSegmentedName(td,name);
  10945. Basic.AppendToSegmentedName(name,"@Info");
  10946. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10947. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10948. Address(source,MPO-4);
  10949. Info(source, "type tag pointer");
  10950. Symbol( source, tag, offset, 0);
  10951. Info(source, "type flags");
  10952. flags := {};
  10953. IF isProtected THEN INCL(flags,31) END;
  10954. Set( source, flags);
  10955. Info(source, "pointer to module");
  10956. moduleSection := ModuleSection();
  10957. Symbol( source, moduleSection, moduleSection.pc,0);
  10958. Info(source, "type name");
  10959. i := 0;
  10960. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10961. (*
  10962. Global.GetSymbolSegmentedName(td,name);
  10963. Basic.SegmentedNameToString(name, sectionName);
  10964. *)
  10965. Name(source,sectionName);
  10966. source.SetReferenced(FALSE);
  10967. Global.GetSymbolSegmentedName(td,name);
  10968. Basic.AppendToSegmentedName(name,"@Fields");
  10969. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10970. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10971. Info(fieldSection, "HeapBlock");
  10972. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10973. Info(fieldSection, "TypeDescriptor");
  10974. Address(fieldSection,0);
  10975. Info(source, "FieldArray ref");
  10976. Symbol(source, fieldSection, fieldSection.pc, 0);
  10977. FieldArray(fieldSection);
  10978. RETURN source;
  10979. END NewTypeDescriptorInfo;
  10980. PROCEDURE NewTypeDescriptor;
  10981. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  10982. procedure: SyntaxTree.Procedure;
  10983. baseRecord: SyntaxTree.RecordType; baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  10984. numberPointers: LONGINT; padding,offset, i: LONGINT;
  10985. CONST MPO=-40000000H;
  10986. PROCEDURE TdTable(size: LONGINT);
  10987. VAR i: LONGINT;
  10988. BEGIN
  10989. Info(source, "tag table");
  10990. baseRecord := recordType;
  10991. i := 0;
  10992. WHILE baseRecord # NIL DO
  10993. INC(i);
  10994. baseRecord := baseRecord.GetBaseRecord();
  10995. END;
  10996. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  10997. WHILE i < size DO
  10998. Address(source,0);
  10999. INC(i);
  11000. END;
  11001. baseRecord := recordType;
  11002. WHILE baseRecord # NIL DO
  11003. baseTD := baseRecord.typeDeclaration;
  11004. Global.GetSymbolSegmentedName(baseTD,name);
  11005. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11006. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11007. ELSE
  11008. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11009. END;
  11010. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*module.system.addressSize);
  11011. Symbol(source, tir, 0, offset);
  11012. baseRecord := baseRecord.GetBaseRecord();
  11013. END;
  11014. END TdTable;
  11015. PROCEDURE MethodTable;
  11016. VAR i,methods: LONGINT;
  11017. BEGIN
  11018. Info(source, "method table");
  11019. IF recordType # NIL THEN
  11020. methods := recordType.recordScope.numberMethods;
  11021. FOR i := methods-1 TO 0 BY -1 DO
  11022. procedure := recordType.recordScope.FindMethod(i);
  11023. Global.GetSymbolSegmentedName(procedure, name);
  11024. NamedSymbol(source, name,procedure, 0,0);
  11025. END;
  11026. END;
  11027. END MethodTable;
  11028. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11029. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11030. BEGIN
  11031. Info(source, "method table");
  11032. baseRecord := recordType;
  11033. WHILE baseRecord.baseType # NIL DO
  11034. baseRecord := baseRecord.GetBaseRecord ();
  11035. END;
  11036. GetRecordTypeName (baseRecord, name);
  11037. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11038. IF name = stackFrame THEN
  11039. start := 0;
  11040. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11041. baseRecord := recordType;
  11042. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11043. baseRecord := baseRecord.GetBaseRecord ();
  11044. END;
  11045. IF baseRecord # NIL THEN
  11046. GetRecordTypeName (baseRecord, name);
  11047. IF pointer & ~baseRecord.isObject THEN
  11048. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11049. END;
  11050. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11051. ELSIF recordType.isObject THEN
  11052. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11053. ELSIF pointer THEN
  11054. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11055. ELSE
  11056. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11057. END;
  11058. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11059. Symbol(source, tir, 0, 0);
  11060. start := 0;
  11061. baseRecord := recordType;
  11062. WHILE (baseRecord # NIL) DO
  11063. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11064. baseRecord := baseRecord.GetBaseRecord ();
  11065. END;
  11066. ELSE
  11067. (* explicit trace method: *)
  11068. procedure := recordType.recordScope.FindMethod(0);
  11069. IF ~procedure.isFinalizer THEN
  11070. Global.GetSymbolSegmentedName(procedure, name);
  11071. NamedSymbol(source, name,procedure, 0,0);
  11072. END;
  11073. start := 1;
  11074. END;
  11075. IF (name # stackFrame) & recordType.isObject THEN
  11076. baseRecord := recordType;
  11077. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11078. baseRecord := baseRecord.GetBaseRecord ();
  11079. END;
  11080. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11081. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11082. ELSE
  11083. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11084. END;
  11085. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11086. Symbol(source, tir, 0, 0);
  11087. END;
  11088. methods := recordType.recordScope.numberMethods;
  11089. FOR i := start TO methods-1 DO
  11090. procedure := recordType.recordScope.FindMethod(i);
  11091. IF ~procedure.isFinalizer THEN
  11092. Global.GetSymbolSegmentedName(procedure, name);
  11093. NamedSymbol(source, name,procedure, 0,0);
  11094. END;
  11095. END;
  11096. END CooperativeMethodTable;
  11097. BEGIN
  11098. Global.GetSymbolSegmentedName(td,name);
  11099. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11100. source.SetExported(IsExported(td));
  11101. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11102. IF implementationVisitor.backend.cooperative THEN
  11103. base := NIL;
  11104. baseRecord := recordType.GetBaseRecord();
  11105. IF baseRecord # NIL THEN
  11106. baseTD := baseRecord.typeDeclaration;
  11107. END;
  11108. IF ~recordType.isObject THEN
  11109. Info(source, "parent");
  11110. IF baseRecord # NIL THEN
  11111. Global.GetSymbolSegmentedName(baseTD,name);
  11112. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11113. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11114. ELSE
  11115. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11116. END;
  11117. Symbol(source, tir, 0, 0);
  11118. ELSE
  11119. Address(source,0);
  11120. END;
  11121. Info(source, "record size");
  11122. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11123. source.SetReferenced(FALSE);
  11124. CooperativeMethodTable(FALSE);
  11125. base := source;
  11126. Global.GetSymbolSegmentedName(td,name);
  11127. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11128. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11129. source.SetExported(IsExported(td));
  11130. source.SetReferenced(FALSE);
  11131. END;
  11132. Info(source, "parent");
  11133. IF baseRecord # NIL THEN
  11134. Global.GetSymbolSegmentedName(baseTD,name);
  11135. sym := baseTD;
  11136. IF ~recordType.isObject THEN
  11137. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11138. sym := NIL;
  11139. END;
  11140. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11141. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11142. ELSE
  11143. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11144. END;
  11145. Symbol(source, tir, 0, 0);
  11146. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11147. Address(source,0);
  11148. ELSE
  11149. IF recordType.isObject THEN
  11150. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11151. ELSE
  11152. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11153. END;
  11154. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11155. Symbol(source, tir, 0, 0);
  11156. END;
  11157. Info(source, "base record descriptor");
  11158. Symbol(source, base, 0, 0);
  11159. CooperativeMethodTable(TRUE);
  11160. source.SetReferenced(FALSE);
  11161. IF recordType.hasPointers THEN
  11162. IF ~HasExplicitTraceMethod (recordType) THEN
  11163. implementationVisitor.CreateTraceMethod(recordType);
  11164. END;
  11165. implementationVisitor.CreateResetProcedure(recordType);
  11166. implementationVisitor.CreateAssignProcedure(recordType);
  11167. END;
  11168. ELSIF ~simple THEN
  11169. (*
  11170. MethodEnd = MPO
  11171. ---
  11172. methods (# methods)
  11173. ---
  11174. tags (16)
  11175. ---
  11176. TypeDesc = TypeInfoAdr
  11177. ---
  11178. td adr ---> rec size
  11179. ----
  11180. pointer offsets
  11181. ----
  11182. (padding)
  11183. -----
  11184. empty [2 addresses aligned]
  11185. empty
  11186. empty
  11187. numPtrs
  11188. ---
  11189. pointer offsets
  11190. ---
  11191. *)
  11192. Info(source, "MethodEnd = MPO");
  11193. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11194. source(IntermediateCode.Section).Emit(Data(-1,op));
  11195. MethodTable;
  11196. TdTable(TypeTags);
  11197. Info(source, "type descriptor info pointer");
  11198. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11199. IF (cellType # NIL) THEN
  11200. IF cellType.sizeInBits < 0 THEN
  11201. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11202. END;
  11203. Info(source, "cell size");
  11204. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11205. ELSE
  11206. Info(source, "record size");
  11207. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11208. END;
  11209. Info(source, "pointer offsets pointer");
  11210. padding := 1- source.pc MOD 2;
  11211. Symbol(source, source, source.pc+1+padding,0);
  11212. IF padding >0 THEN
  11213. Info(source, "padding");
  11214. FOR i := 1 TO padding DO Address(source,0) END;
  11215. END;
  11216. IF cellType # NIL THEN
  11217. PointerArray(source, cellType.cellScope, numberPointers);
  11218. ELSE
  11219. PointerArray(source, recordType.recordScope, numberPointers);
  11220. END;
  11221. ELSE
  11222. (*
  11223. simple:
  11224. methods
  11225. tag(2)
  11226. tag(1)
  11227. td adr ---> tag(0)
  11228. td adr ---> size
  11229. *)
  11230. MethodTable;
  11231. TdTable(TypeTags);
  11232. Info(source, "record size");
  11233. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11234. source.SetReferenced(FALSE);
  11235. END;
  11236. END NewTypeDescriptor;
  11237. BEGIN
  11238. x := x.resolved;
  11239. IF (x IS SyntaxTree.PointerType) THEN
  11240. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11241. END;
  11242. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11243. recordType := x(SyntaxTree.RecordType);
  11244. td := x.typeDeclaration;
  11245. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11246. ASSERT(td # NIL);
  11247. section := module.allSections.FindBySymbol(td); (* TODO *)
  11248. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11249. IF implementationVisitor.newObjectFile THEN
  11250. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11251. NewTypeDescriptor
  11252. END;
  11253. ELSE
  11254. (* data section in intermediate code *)
  11255. Global.GetSymbolSegmentedName(td,name);
  11256. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11257. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11258. tir.Emit(Data(-1,op));
  11259. END;
  11260. END;
  11261. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11262. cellType := x(SyntaxTree.CellType);
  11263. td := x.typeDeclaration;
  11264. section := module.allSections.FindBySymbol(td); (* TODO *)
  11265. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11266. IF implementationVisitor.newObjectFile THEN
  11267. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11268. NewTypeDescriptor
  11269. END;
  11270. END;
  11271. END;
  11272. END
  11273. END CheckTypeDeclaration
  11274. END MetaDataGenerator;
  11275. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11276. VAR
  11277. trace-: BOOLEAN;
  11278. traceString-: SyntaxTree.IdentifierString;
  11279. traceModuleName-: SyntaxTree.IdentifierString;
  11280. newObjectFile-: BOOLEAN;
  11281. profile-: BOOLEAN;
  11282. noRuntimeChecks: BOOLEAN;
  11283. simpleMetaData-: BOOLEAN;
  11284. noAsserts: BOOLEAN;
  11285. optimize-: BOOLEAN;
  11286. cooperative-: BOOLEAN;
  11287. preregisterStatic-: BOOLEAN;
  11288. dump-: Basic.Writer;
  11289. cellsAreObjects: BOOLEAN;
  11290. PROCEDURE &InitIntermediateBackend*;
  11291. BEGIN
  11292. simpleMetaData := FALSE;
  11293. newObjectFile := FALSE;
  11294. InitBackend;
  11295. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11296. SetTraceModuleName(DefaultTraceModuleName);
  11297. END InitIntermediateBackend;
  11298. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11299. VAR
  11300. declarationVisitor: DeclarationVisitor;
  11301. implementationVisitor: ImplementationVisitor;
  11302. module: Sections.Module;
  11303. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11304. meta: MetaDataGenerator;
  11305. BEGIN
  11306. ResetError;
  11307. Global.GetSymbolName(x,name);
  11308. IF activeCellsSpecification # NIL THEN
  11309. GetDescription(instructionSet);
  11310. activeCellsSpecification.SetInstructionSet(instructionSet)
  11311. END;
  11312. NEW(module,x,system); (* backend structures *)
  11313. Global.GetModuleName(x, name);
  11314. module.SetModuleName(name);
  11315. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11316. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11317. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11318. declarationVisitor.Module(x,module);
  11319. IF newObjectFile & ~meta.simple THEN
  11320. meta.Module(implementationVisitor.moduleBodySection);
  11321. END;
  11322. GetDescription(platformName);
  11323. module.SetPlatformName(platformName);
  11324. RETURN module
  11325. END GenerateIntermediate;
  11326. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11327. BEGIN RETURN TRUE
  11328. END SupportedImmediate;
  11329. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11330. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11331. END ProcessSyntaxTreeModule;
  11332. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11333. VAR
  11334. result: Sections.Module;
  11335. traceName: Basic.MessageString;
  11336. BEGIN
  11337. ASSERT(intermediateCodeModule IS Sections.Module);
  11338. result := intermediateCodeModule(Sections.Module);
  11339. IF trace THEN
  11340. traceName := "intermediate code trace: ";
  11341. Strings.Append(traceName,traceString);
  11342. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11343. IF (traceString="") OR (traceString="*") THEN
  11344. result.Dump(dump);
  11345. dump.Update
  11346. ELSE
  11347. Sections.DumpFiltered(dump, result, traceString);
  11348. END
  11349. END;
  11350. RETURN result
  11351. END ProcessIntermediateCodeModule;
  11352. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11353. BEGIN instructionSet := "Intermediate";
  11354. END GetDescription;
  11355. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11356. BEGIN
  11357. SELF.newObjectFile := newObjectFile;
  11358. SELF.simpleMetaData := simpleMetaData;
  11359. END SetNewObjectFile;
  11360. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11361. BEGIN COPY(name, traceModuleName)
  11362. END SetTraceModuleName;
  11363. PROCEDURE DefineOptions(options: Options.Options);
  11364. BEGIN
  11365. DefineOptions^(options);
  11366. options.Add(0X,"trace",Options.String);
  11367. options.Add(0X,"runtime",Options.String);
  11368. options.Add(0X,"newObjectFile",Options.Flag);
  11369. options.Add(0X,"traceModule",Options.String);
  11370. options.Add(0X,"profile",Options.Flag);
  11371. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11372. options.Add(0X,"noAsserts",Options.Flag);
  11373. options.Add(0X,"metaData",Options.String);
  11374. options.Add('o',"optimize", Options.Flag);
  11375. options.Add(0X,"preregisterStatic", Options.Flag);
  11376. options.Add(0X,"cellsAreObjects", Options.Flag);
  11377. END DefineOptions;
  11378. PROCEDURE GetOptions(options: Options.Options);
  11379. VAR name,string: SyntaxTree.IdentifierString;
  11380. BEGIN
  11381. GetOptions^(options);
  11382. trace := options.GetString("trace",traceString);
  11383. profile := options.GetFlag("profile");
  11384. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11385. noAsserts := options.GetFlag("noAsserts");
  11386. cooperative := options.GetFlag("cooperative");
  11387. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11388. IF cooperative THEN
  11389. SetRuntimeModuleName("CPU") END
  11390. END;
  11391. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11392. simpleMetaData := TRUE
  11393. END;
  11394. IF options.GetString("metaData",string) THEN
  11395. IF string = "simple" THEN simpleMetaData := TRUE
  11396. ELSIF string ="full" THEN simpleMetaData := FALSE
  11397. END;
  11398. END;
  11399. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11400. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11401. optimize := options.GetFlag("optimize");
  11402. preregisterStatic := options.GetFlag("preregisterStatic");
  11403. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11404. END GetOptions;
  11405. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11406. BEGIN RETURN SymbolFileFormat.Get()
  11407. END DefaultSymbolFileFormat;
  11408. END IntermediateBackend;
  11409. (* ----------------------------------- register allocation ------------------------------------- *)
  11410. (* register mapping scheme
  11411. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11412. spill offset
  11413. part(n) --> ticket <--> physical register
  11414. spill offset
  11415. *)
  11416. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11417. emptyOperand: IntermediateCode.Operand;
  11418. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11419. statCoopResetVariables: LONGINT;
  11420. statCoopModifyAssignments: LONGINT;
  11421. modifyAssignmentsPC : LONGINT;
  11422. statCoopNilCheck: LONGINT;
  11423. statCoopSwitch: LONGINT;
  11424. statCoopAssignProcedure: LONGINT;
  11425. statCoopTraceMethod: LONGINT;
  11426. statCoopResetProcedure: LONGINT;
  11427. statCoopTraceModule: LONGINT;
  11428. PROCEDURE ResetStatistics*;
  11429. BEGIN
  11430. statCoopResetVariables := 0;
  11431. statCoopModifyAssignments := 0;
  11432. statCoopNilCheck:= 0;
  11433. statCoopSwitch:= 0;
  11434. statCoopAssignProcedure:= 0;
  11435. statCoopTraceMethod:= 0;
  11436. statCoopResetProcedure:= 0;
  11437. statCoopTraceModule:= 0;
  11438. END ResetStatistics;
  11439. PROCEDURE Statistics*;
  11440. BEGIN
  11441. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11442. TRACE(statCoopNilCheck, statCoopSwitch);
  11443. TRACE(statCoopAssignProcedure,
  11444. statCoopTraceMethod,
  11445. statCoopResetProcedure,
  11446. statCoopTraceModule)
  11447. END Statistics;
  11448. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11449. BEGIN
  11450. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11451. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11452. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11453. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11454. 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)
  11455. END
  11456. END PassBySingleReference;
  11457. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11458. BEGIN
  11459. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11460. END PassInRegister;
  11461. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11462. VAR new: RegisterEntry;
  11463. BEGIN
  11464. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11465. IF queue = NIL THEN
  11466. queue := new
  11467. ELSE
  11468. new.next := queue;
  11469. IF queue#NIL THEN queue.prev := new END;
  11470. queue := new
  11471. END;
  11472. END AddRegisterEntry;
  11473. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11474. VAR this: RegisterEntry;
  11475. BEGIN
  11476. this := queue;
  11477. WHILE (this # NIL) & (this.register # register) DO
  11478. this := this.next;
  11479. END;
  11480. IF this = NIL THEN
  11481. RETURN FALSE
  11482. END;
  11483. ASSERT(this # NIL);
  11484. IF this = queue THEN queue := queue.next END;
  11485. IF this.prev # NIL THEN this.prev.next := this.next END;
  11486. IF this.next # NIL THEN this.next.prev := this.prev END;
  11487. RETURN TRUE
  11488. END RemoveRegisterEntry;
  11489. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11490. BEGIN ASSERT(cond);
  11491. END Assert;
  11492. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11493. BEGIN
  11494. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11495. END ReusableRegister;
  11496. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11497. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11498. BEGIN
  11499. procedure := moduleScope.bodyProcedure;
  11500. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11501. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11502. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11503. procedure.SetScope(moduleScope);
  11504. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11505. procedure.SetAccess(SyntaxTree.Hidden);
  11506. moduleScope.SetBodyProcedure(procedure);
  11507. moduleScope.AddProcedure(procedure);
  11508. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11509. END;
  11510. END EnsureBodyProcedure;
  11511. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11512. VAR import: SyntaxTree.Import;
  11513. s: Basic.MessageString;
  11514. selfName: SyntaxTree.IdentifierString;
  11515. module: SyntaxTree.Module;
  11516. BEGIN
  11517. scope.ownerModule.GetName(selfName);
  11518. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11519. module := scope.ownerModule
  11520. ELSE
  11521. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11522. IF import = NIL THEN
  11523. RETURN NIL
  11524. ELSIF import.module = NIL THEN
  11525. RETURN NIL
  11526. ELSE module := import.module
  11527. END;
  11528. END;
  11529. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11530. END GetSymbol;
  11531. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11532. BEGIN
  11533. op.mode := mode;
  11534. IntermediateCode.InitOperand(op.op);
  11535. IntermediateCode.InitOperand(op.tag);
  11536. IntermediateCode.InitOperand(op.extra);
  11537. op.dimOffset := 0;
  11538. END InitOperand;
  11539. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11540. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11541. BEGIN RETURN IntermediateCode.GetType(system, type)
  11542. END GetType;
  11543. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11544. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11545. BEGIN
  11546. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11547. (*
  11548. UniqueId(name,module,name,adr);
  11549. *)
  11550. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11551. constant.SetValue(value);
  11552. module.moduleScope.AddConstant(constant);
  11553. constant.SetScope(module.moduleScope);
  11554. RETURN constant
  11555. END BuildConstant;
  11556. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11557. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11558. BEGIN
  11559. variable := scope.firstVariable;
  11560. WHILE variable # NIL DO
  11561. IF variable.NeedsTrace() THEN
  11562. RETURN TRUE;
  11563. END;
  11564. variable := variable.nextVariable;
  11565. END;
  11566. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11567. WHILE parameter # NIL DO
  11568. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11569. RETURN TRUE;
  11570. END;
  11571. parameter := parameter.nextParameter;
  11572. END;
  11573. RETURN FALSE;
  11574. END HasPointers;
  11575. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11576. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11577. END IsVariableParameter;
  11578. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11579. VAR parameter: SyntaxTree.Parameter;
  11580. BEGIN
  11581. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11582. WHILE parameter # NIL DO
  11583. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11584. IF parameter.movable THEN RETURN TRUE END;
  11585. parameter := parameter.nextParameter;
  11586. END;
  11587. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11588. END HasVariableParameters;
  11589. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11590. BEGIN
  11591. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11592. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11593. END HasExplicitTraceMethod;
  11594. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11595. BEGIN
  11596. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11597. IF (expression IS SyntaxTree.IntegerValue) THEN
  11598. val := expression(SyntaxTree.IntegerValue).value;
  11599. RETURN TRUE
  11600. ELSE
  11601. RETURN FALSE
  11602. END;
  11603. END IsIntegerConstant;
  11604. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11605. BEGIN
  11606. IF val <= 0 THEN RETURN FALSE END;
  11607. exp := 0;
  11608. WHILE ~ODD(val) DO
  11609. val := val DIV 2;
  11610. INC(exp)
  11611. END;
  11612. RETURN val = 1
  11613. END PowerOf2;
  11614. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11615. VAR procedure: SyntaxTree.Procedure;
  11616. BEGIN
  11617. procedure := record.recordScope.constructor;
  11618. IF procedure = NIL THEN
  11619. record := record.GetBaseRecord();
  11620. IF record # NIL THEN
  11621. procedure := GetConstructor(record)
  11622. END;
  11623. END;
  11624. RETURN procedure;
  11625. END GetConstructor;
  11626. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11627. BEGIN
  11628. value := SHORT(op.intValue);
  11629. RETURN op.mode = IntermediateCode.ModeImmediate;
  11630. END IsIntegerImmediate;
  11631. (** whether a type strictily is a pointer to record or object type
  11632. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11633. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11634. BEGIN
  11635. IF type = NIL THEN
  11636. RETURN FALSE
  11637. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11638. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11639. ELSE
  11640. RETURN FALSE
  11641. END
  11642. END IsStrictlyPointerToRecord;
  11643. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11644. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11645. END IsUnsafePointer;
  11646. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11647. BEGIN type := type.resolved;
  11648. IF type IS SyntaxTree.PointerType THEN
  11649. type := type(SyntaxTree.PointerType).pointerBase;
  11650. type := type.resolved;
  11651. IF type IS SyntaxTree.RecordType THEN
  11652. recordType := type(SyntaxTree.RecordType);
  11653. RETURN TRUE
  11654. ELSE
  11655. RETURN FALSE
  11656. END
  11657. ELSIF type IS SyntaxTree.RecordType THEN
  11658. recordType := type(SyntaxTree.RecordType);
  11659. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11660. ELSIF type IS SyntaxTree.ObjectType THEN
  11661. RETURN TRUE
  11662. ELSIF type IS SyntaxTree.AnyType THEN
  11663. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11664. ELSE
  11665. RETURN FALSE
  11666. END;
  11667. END IsPointerToRecord;
  11668. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11669. BEGIN
  11670. type := type.resolved;
  11671. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11672. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11673. END IsArrayOfSystemByte;
  11674. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11675. BEGIN
  11676. IF type = NIL THEN RETURN FALSE END;
  11677. type := type.resolved;
  11678. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11679. END IsOpenArray;
  11680. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11681. BEGIN
  11682. IF type = NIL THEN RETURN FALSE END;
  11683. type := type.resolved;
  11684. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11685. END IsStaticArray;
  11686. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11687. VAR size: LONGINT;
  11688. BEGIN
  11689. size := 1;
  11690. WHILE (IsStaticArray(type)) DO
  11691. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11692. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11693. END;
  11694. RETURN size;
  11695. END StaticArrayNumElements;
  11696. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11697. BEGIN
  11698. WHILE (IsStaticArray(type)) DO
  11699. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11700. END;
  11701. RETURN type;
  11702. END StaticArrayBaseType;
  11703. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11704. BEGIN
  11705. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11706. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11707. END;
  11708. RETURN type;
  11709. END ArrayBaseType;
  11710. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11711. BEGIN
  11712. IF type = NIL THEN RETURN FALSE END;
  11713. type := type.resolved;
  11714. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11715. END IsDelegate;
  11716. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11717. VAR i: LONGINT;
  11718. BEGIN
  11719. i := 0; type := type.resolved;
  11720. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11721. INC(i);
  11722. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11723. END;
  11724. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11725. INC(i);
  11726. type := type(SyntaxTree.MathArrayType).arrayBase;
  11727. IF type # NIL THEN type := type.resolved END;
  11728. END;
  11729. RETURN i
  11730. END DynamicDim;
  11731. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11732. BEGIN
  11733. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11734. type := type(SyntaxTree.ArrayType).arrayBase;
  11735. END;
  11736. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11737. type := type(SyntaxTree.MathArrayType).arrayBase;
  11738. END;
  11739. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11740. END StaticSize;
  11741. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11742. BEGIN
  11743. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11744. END IsImmediate;
  11745. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11746. BEGIN
  11747. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11748. END IsAddress;
  11749. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11750. BEGIN
  11751. RETURN (x.mode = IntermediateCode.ModeRegister);
  11752. END IsRegister;
  11753. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11754. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11755. BEGIN
  11756. typeDeclaration := recordType.typeDeclaration;
  11757. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11758. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11759. END GetRecordTypeName;
  11760. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11761. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11762. BEGIN
  11763. parSize := 0;
  11764. IF StructuredReturnType(procedureType) THEN
  11765. parameter := procedureType.returnParameter;
  11766. INC(parSize,system.SizeOfParameter(parameter));
  11767. parSize := parSize + (-parSize) MOD system.addressSize;
  11768. END;
  11769. parameter :=procedureType.lastParameter;
  11770. WHILE (parameter # NIL) DO
  11771. INC(parSize,system.SizeOfParameter(parameter));
  11772. parSize := parSize + (-parSize) MOD system.addressSize;
  11773. parameter := parameter.prevParameter;
  11774. END;
  11775. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11776. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11777. RETURN ToMemoryUnits(system,parSize)
  11778. END ParametersSize;
  11779. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11780. BEGIN
  11781. IF type = NIL THEN RETURN FALSE
  11782. ELSE
  11783. type := type.resolved;
  11784. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11785. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11786. END
  11787. END ReturnedAsParameter;
  11788. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11789. BEGIN
  11790. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11791. END StructuredReturnType;
  11792. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11793. BEGIN
  11794. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11795. END IsNested;
  11796. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11797. BEGIN
  11798. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11799. scope := scope.outerScope;
  11800. END;
  11801. RETURN scope # NIL;
  11802. END InCellScope;
  11803. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11804. BEGIN
  11805. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11806. RETURN 0
  11807. ELSE
  11808. *)
  11809. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11810. (*END;*)
  11811. END ProcedureParametersSize;
  11812. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11813. VAR dataUnit: LONGINT;
  11814. BEGIN dataUnit := system.dataUnit;
  11815. ASSERT(size MOD system.dataUnit = 0);
  11816. RETURN size DIV system.dataUnit
  11817. END ToMemoryUnits;
  11818. PROCEDURE Get*(): Backend.Backend;
  11819. VAR backend: IntermediateBackend;
  11820. BEGIN NEW(backend); RETURN backend
  11821. END Get;
  11822. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11823. VAR instruction: IntermediateCode.Instruction;
  11824. BEGIN
  11825. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11826. RETURN instruction
  11827. END Nop;
  11828. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11829. VAR instruction: IntermediateCode.Instruction;
  11830. BEGIN
  11831. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11832. RETURN instruction
  11833. END Mov;
  11834. (* like Mov but ensures that no new register will be allocated for dest *)
  11835. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11836. VAR instruction: IntermediateCode.Instruction;
  11837. BEGIN
  11838. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11839. RETURN instruction
  11840. END MovReplace;
  11841. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11842. VAR instruction: IntermediateCode.Instruction;
  11843. BEGIN
  11844. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11845. RETURN instruction
  11846. END Conv;
  11847. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11848. VAR instruction: IntermediateCode.Instruction;
  11849. BEGIN
  11850. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11851. RETURN instruction
  11852. END Call;
  11853. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11854. VAR op1, op2: IntermediateCode.Operand;
  11855. VAR instruction: IntermediateCode.Instruction;
  11856. BEGIN
  11857. IntermediateCode.InitNumber(op1,pcOffset);
  11858. IntermediateCode.InitNumber(op2,callingConvention);
  11859. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11860. RETURN instruction
  11861. END Exit;
  11862. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11863. VAR instruction: IntermediateCode.Instruction;
  11864. BEGIN
  11865. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11866. RETURN instruction
  11867. END Return;
  11868. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11869. VAR instruction: IntermediateCode.Instruction;
  11870. BEGIN
  11871. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11872. RETURN instruction
  11873. END Result;
  11874. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11875. VAR op1: IntermediateCode.Operand;
  11876. VAR instruction: IntermediateCode.Instruction;
  11877. BEGIN
  11878. IntermediateCode.InitNumber(op1,nr);
  11879. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11880. RETURN instruction
  11881. END Trap;
  11882. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11883. VAR instruction: IntermediateCode.Instruction;
  11884. BEGIN
  11885. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11886. RETURN instruction
  11887. END Br;
  11888. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11889. VAR instruction: IntermediateCode.Instruction;
  11890. BEGIN
  11891. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11892. RETURN instruction
  11893. END Breq;
  11894. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11895. VAR instruction: IntermediateCode.Instruction;
  11896. BEGIN
  11897. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11898. RETURN instruction
  11899. END Brne;
  11900. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11901. VAR instruction: IntermediateCode.Instruction;
  11902. BEGIN
  11903. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11904. RETURN instruction
  11905. END Brge;
  11906. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11907. VAR instruction: IntermediateCode.Instruction;
  11908. BEGIN
  11909. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11910. RETURN instruction
  11911. END Brlt;
  11912. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11913. VAR instruction: IntermediateCode.Instruction;
  11914. BEGIN
  11915. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11916. RETURN instruction
  11917. END Pop;
  11918. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11919. VAR instruction: IntermediateCode.Instruction;
  11920. BEGIN
  11921. ASSERT(op.mode # IntermediateCode.Undefined);
  11922. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11923. RETURN instruction
  11924. END Push;
  11925. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11926. VAR instruction: IntermediateCode.Instruction;
  11927. BEGIN
  11928. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11929. RETURN instruction
  11930. END Neg;
  11931. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11932. VAR instruction: IntermediateCode.Instruction;
  11933. BEGIN
  11934. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11935. RETURN instruction
  11936. END Not;
  11937. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11938. VAR instruction: IntermediateCode.Instruction;
  11939. BEGIN
  11940. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11941. RETURN instruction
  11942. END Abs;
  11943. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11944. VAR instruction: IntermediateCode.Instruction;
  11945. BEGIN
  11946. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  11947. RETURN instruction
  11948. END Mul;
  11949. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11950. VAR instruction: IntermediateCode.Instruction;
  11951. BEGIN
  11952. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  11953. RETURN instruction
  11954. END Div;
  11955. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11956. VAR instruction: IntermediateCode.Instruction;
  11957. BEGIN
  11958. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  11959. RETURN instruction
  11960. END Mod;
  11961. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11962. VAR instruction: IntermediateCode.Instruction;
  11963. BEGIN
  11964. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  11965. RETURN instruction
  11966. END Sub;
  11967. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11968. VAR instruction: IntermediateCode.Instruction;
  11969. BEGIN
  11970. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  11971. RETURN instruction
  11972. END Add;
  11973. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11974. VAR instruction: IntermediateCode.Instruction;
  11975. BEGIN
  11976. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  11977. RETURN instruction
  11978. END And;
  11979. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11980. VAR instruction: IntermediateCode.Instruction;
  11981. BEGIN
  11982. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  11983. RETURN instruction
  11984. END Or;
  11985. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11986. VAR instruction: IntermediateCode.Instruction;
  11987. BEGIN
  11988. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  11989. RETURN instruction
  11990. END Xor;
  11991. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11992. VAR instruction: IntermediateCode.Instruction;
  11993. BEGIN
  11994. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  11995. RETURN instruction
  11996. END Shl;
  11997. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11998. VAR instruction: IntermediateCode.Instruction;
  11999. BEGIN
  12000. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  12001. RETURN instruction
  12002. END Shr;
  12003. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12004. VAR instruction: IntermediateCode.Instruction;
  12005. BEGIN
  12006. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  12007. RETURN instruction
  12008. END Rol;
  12009. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  12010. VAR instruction: IntermediateCode.Instruction;
  12011. BEGIN
  12012. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12013. RETURN instruction
  12014. END Ror;
  12015. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12016. VAR instruction: IntermediateCode.Instruction;
  12017. BEGIN
  12018. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12019. RETURN instruction
  12020. END Cas;
  12021. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12022. VAR instruction: IntermediateCode.Instruction;
  12023. BEGIN
  12024. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12025. RETURN instruction
  12026. END Copy;
  12027. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12028. VAR instruction: IntermediateCode.Instruction;
  12029. BEGIN
  12030. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12031. RETURN instruction
  12032. END Fill;
  12033. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12034. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12035. BEGIN
  12036. string := IntermediateCode.String(s);
  12037. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12038. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12039. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12040. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12041. RETURN instruction
  12042. END Asm;
  12043. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12044. VAR instruction: IntermediateCode.Instruction;
  12045. BEGIN
  12046. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12047. RETURN instruction
  12048. END Data;
  12049. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12050. VAR instruction: IntermediateCode.Instruction;
  12051. BEGIN
  12052. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12053. IntermediateCode.SetSubType(instruction, subtype);
  12054. RETURN instruction
  12055. END SpecialInstruction;
  12056. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12057. VAR op1: IntermediateCode.Operand;
  12058. VAR instruction: IntermediateCode.Instruction;
  12059. BEGIN
  12060. (*! generate a warning if size exceeds a certain limit *)
  12061. (*
  12062. ASSERT(bytes < 1000000); (* sanity check *)
  12063. *)
  12064. ASSERT(0 <= units); (* sanity check *)
  12065. IntermediateCode.InitNumber(op1,units);
  12066. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12067. RETURN instruction
  12068. END Reserve;
  12069. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12070. VAR op1: IntermediateCode.Operand;
  12071. VAR instruction: IntermediateCode.Instruction;
  12072. BEGIN
  12073. IntermediateCode.InitNumber(op1,position);
  12074. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12075. RETURN instruction
  12076. END LabelInstruction;
  12077. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12078. VAR pc: LONGINT;
  12079. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12080. BEGIN
  12081. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12082. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12083. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12084. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12085. IF instr.op1.type.form = IntermediateCode.Float THEN
  12086. RETURN instr.op1.floatValue = op.floatValue
  12087. ELSE
  12088. RETURN instr.op1.intValue = op.intValue
  12089. END;
  12090. END ProvidesValue;
  12091. BEGIN
  12092. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12093. pc := 0;
  12094. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12095. INC(pc);
  12096. END;
  12097. IF pc = data.pc THEN
  12098. data.Emit(Data(-1,vop));
  12099. END;
  12100. RETURN pc
  12101. END EnterImmediate;
  12102. PROCEDURE Init;
  12103. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12104. BEGIN
  12105. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12106. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12107. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12108. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12109. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12110. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12111. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12112. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12113. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12114. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12115. IntermediateCode.InitOperand(emptyOperand);
  12116. FOR i := 0 TO NumberSystemCalls-1 DO
  12117. name := "@SystemCall";
  12118. Basic.AppendNumber(name,i);
  12119. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12120. END;
  12121. END Init;
  12122. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12123. BEGIN
  12124. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12125. END IsExported;
  12126. BEGIN
  12127. Init;
  12128. END FoxIntermediateBackend.
  12129. Compiler.Compile FoxIntermediateBackend.Mod ~