FoxIntermediateBackend.Mod 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308
  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.SetPositionOrAlignment(x.fixed, x.alignment);
  300. irv.SetOffset(ToMemoryUnits(system,x.offsetInBits));
  301. irv.Emit(Reserve(x.position,ToMemoryUnits(system,system.SizeOf(x.type))));
  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.GetCodeSectionNameForSymbol(x, name);
  455. IF (scope.body # NIL) & (x.isInline) THEN
  456. inline := TRUE;
  457. ir := implementationVisitor.NewSection(module.allSections, Sections.InlineCodeSection, name,x,dump);
  458. ir.SetExported(IsExported(x));
  459. ELSIF (x.scope # NIL) & (x.scope IS SyntaxTree.CellScope) & (x.scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  460. OR (x.scope # NIL) & (x.scope IS SyntaxTree.ModuleScope) & (x.scope(SyntaxTree.ModuleScope).ownerModule.isCellNet) THEN
  461. IF backend.cellsAreObjects THEN
  462. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name, x, dump);
  463. ir.SetExported(IsExported(x));
  464. ELSE
  465. RETURN; (* cellnet cannot be compiled for final static hardware *)
  466. END;
  467. ELSIF x = module.module.moduleScope.bodyProcedure THEN
  468. inline := FALSE;
  469. AddBodyCallStub(x);
  470. ir := implementationVisitor.NewSection(module.allSections, Sections.BodyCodeSection, name,x,dump);
  471. ir.SetExported(IsExported(x));
  472. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x = scope.outerScope(SyntaxTree.CellScope).bodyProcedure) THEN
  473. inline := FALSE;
  474. cellType := scope.outerScope(SyntaxTree.CellScope).ownerCell;
  475. IF ~HasValue(cellType.modifiers,Global.StringDataMemorySize,stackSize) THEN stackSize := ActiveCells.defaultDataMemorySize END;
  476. AddBodyCallStub(x);
  477. AddStackAllocation(x,stackSize);
  478. ir := implementationVisitor.NewSection(module.allSections,Sections.BodyCodeSection, name,x,dump);
  479. ir.SetExported(IsExported(x));
  480. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & (x.isConstructor) THEN
  481. inline := FALSE;
  482. Parameters(procedureType.firstParameter);
  483. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  484. ir.SetExported(IsExported(x));
  485. ELSE
  486. inline := FALSE;
  487. IF x.isEntry OR x.isExit THEN
  488. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,x,dump);
  489. ir.SetExported(TRUE);
  490. IF x.isEntry THEN ir.SetPriority(EntryPriority) ELSE ir.SetPriority(ExitPriority) END;
  491. ELSE
  492. ir := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,x,dump);
  493. ir.SetExported(IsExported(x) OR SemanticChecker.InMethodTable(x));
  494. END;
  495. END;
  496. cc := procedureType.callingConvention;
  497. IF scope.body # NIL THEN
  498. IF implementationVisitor.emitLabels THEN ir.Emit(LabelInstruction(scope.body.position)) END;
  499. registerNumber := 0;
  500. IF ~inline THEN
  501. IF scope.lastVariable = NIL THEN
  502. stackSize := 0
  503. ELSE
  504. stackSize := scope.lastVariable.offsetInBits;
  505. IF stackSize <0 THEN stackSize := -stackSize END;
  506. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  507. END;
  508. (*
  509. ir.Emit(Nop(position)); (* placeholder for stack frame check *)
  510. ir.Emit(Nop(position)); (* placeholder for stack frame check (2) *)
  511. *)
  512. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  513. pc := ir.pc-1;
  514. (*
  515. ir.Emit(Nop(position)); (* placeholder for fill *)
  516. *)
  517. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  518. formalParameter := procedureType.lastParameter;
  519. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  520. IF ~PassInRegister(formalParameter) THEN
  521. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  522. ELSE
  523. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  524. type := GetType(system, formalParameter.type);
  525. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  526. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  527. ir.Emit(Mov(-1,dest, src));
  528. implementationVisitor.ReleaseIntermediateOperand(src);
  529. INC(registerNumber);
  530. formalParameter := formalParameter.prevParameter;
  531. END;
  532. END;
  533. END;
  534. ir.EnterValidPAF;
  535. END;
  536. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  537. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  538. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  539. IF scope.lastVariable # NIL THEN
  540. stackSize := scope.lastVariable.offsetInBits;
  541. IF stackSize <0 THEN stackSize := -stackSize END;
  542. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  543. END;
  544. END;
  545. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  546. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  547. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  548. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  549. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  550. ir.EmitAt(pc,Push(size));
  551. implementationVisitor.StaticCallOperand(result,procedure);
  552. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  553. END;
  554. *)
  555. END;
  556. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  557. IF stackSize > 0 THEN
  558. IF (stackSize MOD system.addressSize = 0) THEN
  559. null := IntermediateCode.Immediate(addressType,0);
  560. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  561. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  562. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  563. ELSE
  564. null := IntermediateCode.Immediate(int8,0);
  565. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  566. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  567. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  568. END;
  569. (*! should potentially be called by backend -- enter might initialize
  570. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  571. *)
  572. END;
  573. ir.ExitValidPAF;
  574. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  575. finalizer := FALSE;
  576. IF backend.cooperative & x.isFinalizer THEN
  577. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  578. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  579. GetRecordTypeName(recordType,name);
  580. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  581. END;
  582. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  583. IF backend.cooperative THEN
  584. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  585. IF implementationVisitor.profile & ~isModuleBody THEN
  586. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  587. END;
  588. END;
  589. implementationVisitor.EmitLeave(ir, x.position,cc);
  590. IF finalizer THEN
  591. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  592. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  593. ir.Emit(Br(x.position,dest));
  594. ELSE
  595. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  596. END;
  597. ELSE
  598. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  599. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  600. END;
  601. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  602. IF backend.cooperative THEN
  603. IF HasPointers (scope) THEN
  604. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  605. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  606. ir.Emit(Result(x.position, res));
  607. ir.Emit(Push(x.position, res));
  608. implementationVisitor.ResetVariables(scope);
  609. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  610. ir.Emit(Pop(x.position, res));
  611. ir.Emit(Return(x.position, res));
  612. ELSE
  613. implementationVisitor.ResetVariables(scope);
  614. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  615. END;
  616. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  617. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  618. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  619. ir.Emit(Result(x.position, res));
  620. ir.Emit(Push(x.position, res));
  621. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  622. ir.Emit(Pop(x.position, res));
  623. ir.Emit(Return(x.position, res));
  624. ELSE
  625. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  626. END;
  627. END;
  628. implementationVisitor.EmitLeave(ir,x.position,cc);
  629. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  630. ELSE
  631. ir.Emit(Nop(x.position));
  632. END;
  633. END
  634. END;
  635. ELSE (* force body for procedures *)
  636. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  637. ir.EnterValidPAF;
  638. implementationVisitor.usedRegisters := NIL;
  639. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  640. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  641. ir.ExitValidPAF;
  642. implementationVisitor.EmitLeave(ir,x.position,cc);
  643. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  644. END;
  645. Scope(scope);
  646. Signature;
  647. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  648. currentScope := prevScope;
  649. END Procedure;
  650. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  651. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  652. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  653. BEGIN
  654. ASSERT (bodyProcedure # NIL);
  655. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  656. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  657. procedure.SetScope(bodyProcedure.scope);
  658. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  659. procedure.SetAccess(SyntaxTree.Hidden);
  660. Global.GetSymbolSegmentedName (procedure,name);
  661. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  662. ir.SetExported(TRUE);
  663. ir.SetPriority(InitPriority);
  664. Global.GetSymbolSegmentedName (bodyProcedure,name);
  665. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  666. implementationVisitor.currentScope := module.module.moduleScope;
  667. implementationVisitor.section := ir;
  668. implementationVisitor.PushSelfPointer();
  669. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  670. ELSIF backend.preregisterStatic THEN
  671. implementationVisitor.currentScope := module.module.moduleScope;
  672. implementationVisitor.section := ir;
  673. implementationVisitor.PushSelfPointer();
  674. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  675. ELSE
  676. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  677. ir.Emit(Call(bodyProcedure.position,op, 0));
  678. END;
  679. END AddBodyCallStub;
  680. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  681. VAR name: Basic.SegmentedName;
  682. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  683. BEGIN
  684. Global.GetSymbolSegmentedName (symbol,name);
  685. Basic.RemoveSuffix(name);
  686. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  687. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  688. ir.SetExported(TRUE);
  689. ir.SetPriority(FirstPriority);
  690. IntermediateCode.InitImmediate(op,addressType,initStack);
  691. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  692. END AddStackAllocation;
  693. (** entry function to visit a complete module *)
  694. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  695. VAR
  696. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  697. hasDynamicOperatorDeclarations: BOOLEAN;
  698. operator: SyntaxTree.Operator;
  699. import: SyntaxTree.Import;
  700. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  701. BEGIN
  702. type := type.resolved;
  703. IF type IS SyntaxTree.RecordType THEN
  704. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  705. ELSIF (type IS SyntaxTree.ArrayType) THEN
  706. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  707. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  708. END;
  709. ELSIF type IS SyntaxTree.MathArrayType THEN
  710. WITH type: SyntaxTree.MathArrayType DO
  711. IF type.form = SyntaxTree.Open THEN
  712. RETURN TRUE
  713. ELSIF type.form = SyntaxTree.Static THEN
  714. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  715. END;
  716. END;
  717. END;
  718. RETURN FALSE
  719. END TypeNeedsInitialization;
  720. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  721. VAR variable: SyntaxTree.Variable;
  722. BEGIN
  723. variable := scope.firstVariable;
  724. WHILE variable # NIL DO
  725. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  726. IF variable.initializer # NIL THEN RETURN TRUE END;
  727. variable := variable.nextVariable;
  728. END;
  729. RETURN FALSE
  730. END ScopeNeedsInitialization;
  731. BEGIN
  732. ASSERT(x # NIL); ASSERT(module # NIL);
  733. SELF.module := module;
  734. (* add import names to the generated Sections.Module *)
  735. import := x.moduleScope.firstImport;
  736. WHILE import # NIL DO
  737. import.module.GetName(idstr);
  738. module.imports.AddName(idstr);
  739. import := import.nextImport
  740. END;
  741. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  742. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  743. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  744. moduleSelf.SetType(system.anyType);
  745. moduleSelf.SetScope(x.moduleScope);
  746. moduleSelf.SetUntraced(TRUE);
  747. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  748. ir.SetExported(TRUE);
  749. IntermediateCode.InitImmediate(op,addressType,0);
  750. ir.Emit(Data(-1,op));
  751. END;
  752. implementationVisitor.module := module;
  753. implementationVisitor.moduleScope := x.moduleScope;
  754. implementationVisitor.moduleSelf := moduleSelf;
  755. implementationVisitor.canBeLoaded := TRUE;
  756. meta.SetModule(module);
  757. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  758. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  759. END;
  760. IF backend.profile THEN
  761. EnsureBodyProcedure(x.moduleScope);
  762. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  763. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  764. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  765. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  766. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  767. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  768. Global.GetModuleName(module.module,idstr);
  769. implementationVisitor.ProfilerAddModule(idstr);
  770. implementationVisitor.numberProcedures := 0;
  771. END;
  772. implementationVisitor.profile := backend.profile;
  773. (* check if there is at least one dynamic operator locally defined *)
  774. hasDynamicOperatorDeclarations := FALSE;
  775. operator := x.moduleScope.firstOperator;
  776. WHILE operator # NIL DO
  777. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  778. operator := operator.nextOperator
  779. END;
  780. (* add operator initialization code section *)
  781. IF hasDynamicOperatorDeclarations THEN
  782. EnsureBodyProcedure(x.moduleScope);
  783. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  784. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  785. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  786. END;
  787. Scope(x.moduleScope);
  788. IF hasDynamicOperatorDeclarations THEN
  789. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  790. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  791. END;
  792. IF backend.profile THEN
  793. implementationVisitor.ProfilerPatchInit;
  794. END;
  795. END Module;
  796. END DeclarationVisitor;
  797. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  798. RegisterUsageCount*=OBJECT
  799. VAR used: UsedArray; count: LONGINT;
  800. PROCEDURE &Init;
  801. VAR i: LONGINT;
  802. BEGIN
  803. count := 0;
  804. IF used = NIL THEN NEW(used,64); END;
  805. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  806. END Init;
  807. PROCEDURE Grow;
  808. VAR new: UsedArray; size,i: LONGINT;
  809. BEGIN
  810. size := LEN(used)*2;
  811. NEW(new,size);
  812. FOR i := 0 TO LEN(used)-1 DO
  813. new[i].count := used[i].count;
  814. new[i].type := used[i].type;
  815. new[i].map := used[i].map
  816. END;
  817. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  818. used := new
  819. END Grow;
  820. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  821. BEGIN
  822. INC(count);
  823. IF count = LEN(used) THEN Grow END;
  824. used[count].type := type;
  825. used[count].class := class;
  826. used[count].map := count;
  827. RETURN count;
  828. END Next;
  829. PROCEDURE IncUse(register: LONGINT);
  830. BEGIN
  831. INC(used[register].count);
  832. (*
  833. IF (register = 1) & (count > 30) THEN
  834. D.TraceBack;
  835. END;
  836. *)
  837. END IncUse;
  838. PROCEDURE DecUse(register: LONGINT);
  839. BEGIN
  840. DEC(used[register].count);
  841. END DecUse;
  842. PROCEDURE Map(register: LONGINT): LONGINT;
  843. VAR map : LONGINT;
  844. BEGIN
  845. IF register > 0 THEN
  846. map := used[register].map;
  847. WHILE register # map DO register := map; map := used[register].map END;
  848. END;
  849. RETURN register
  850. END Map;
  851. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  852. BEGIN
  853. used[register].map:= to;
  854. used[to].count := used[register].count;
  855. used[register].count := 0;
  856. END Remap;
  857. PROCEDURE Use(register: LONGINT): LONGINT;
  858. BEGIN
  859. IF register< LEN(used) THEN
  860. RETURN used[register].count
  861. ELSE
  862. RETURN 0
  863. END
  864. END Use;
  865. END RegisterUsageCount;
  866. RegisterEntry = POINTER TO RECORD
  867. prev,next: RegisterEntry;
  868. register: LONGINT;
  869. registerClass: IntermediateCode.RegisterClass;
  870. type: IntermediateCode.Type;
  871. END;
  872. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  873. Variables = OBJECT (Basic.List)
  874. VAR
  875. inUse: VariableUse;
  876. registerIndex: LONGINT;
  877. PROCEDURE & Init;
  878. VAR i: LONGINT;
  879. BEGIN
  880. InitList(16);
  881. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  882. registerIndex := 1024;
  883. END Init;
  884. PROCEDURE GetUsage(VAR use: VariableUse);
  885. BEGIN
  886. use := inUse;
  887. END GetUsage;
  888. PROCEDURE SetUsage(CONST use: VariableUse);
  889. BEGIN
  890. inUse := use;
  891. END SetUsage;
  892. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  893. VAR any: ANY;
  894. BEGIN
  895. any := Get(i);;
  896. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  897. END GetVariable;
  898. PROCEDURE Occupy(pos: LONGINT);
  899. BEGIN
  900. INCL(inUse[pos DIV 32], pos MOD 32);
  901. END Occupy;
  902. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  903. BEGIN
  904. Occupy(Length());
  905. Add(v);
  906. END AddVariable;
  907. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  908. VAR var : SyntaxTree.Variable;
  909. BEGIN
  910. FOR pos := 0 TO Length()-1 DO
  911. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  912. var := GetVariable(pos);
  913. IF type.SameType(var.type) THEN
  914. Occupy(pos); RETURN var
  915. END;
  916. END;
  917. END;
  918. pos := Length();
  919. RETURN NIL
  920. END GetFreeVariable;
  921. END Variables;
  922. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  923. SymbolMapper = OBJECT
  924. VAR
  925. first: SymbolMap;
  926. PROCEDURE & Init;
  927. BEGIN
  928. first := NIL;
  929. END Init;
  930. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  931. VAR new: SymbolMap;
  932. BEGIN
  933. NEW(new); new.this := this; new.to := to; new.tag := tag;
  934. new.next := first; first := new;
  935. END Add;
  936. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  937. VAR s: SymbolMap;
  938. BEGIN
  939. s := first;
  940. WHILE (s # NIL) & (s.this # this) DO
  941. s := s.next
  942. END;
  943. RETURN s
  944. END Get;
  945. END SymbolMapper;
  946. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  947. VAR
  948. system: Global.System;
  949. section: IntermediateCode.Section;
  950. module: Sections.Module;
  951. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  952. awaitProcCounter, labelId, constId, caseId: LONGINT;
  953. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  954. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  955. checker: SemanticChecker.Checker;
  956. backend: IntermediateBackend;
  957. meta: MetaDataGenerator;
  958. position: LONGINT;
  959. moduleSelf: SyntaxTree.Variable;
  960. (* variables for hand over of variables / temporary state *)
  961. currentScope: SyntaxTree.Scope;
  962. constantDeclaration : SyntaxTree.Symbol;
  963. result: Operand; (* result of the most recent expression / statement *)
  964. destination: IntermediateCode.Operand;
  965. arrayDestinationTag: IntermediateCode.Operand;
  966. arrayDestinationDimension:LONGINT;
  967. currentLoop: Label; (* variable to hand over loop exit jump list *)
  968. conditional: BOOLEAN;
  969. trueLabel, falseLabel, exitLabel: Label;
  970. locked: BOOLEAN;
  971. (*
  972. usedRegisters: Registers;
  973. *)
  974. registerUsageCount: RegisterUsageCount;
  975. usedRegisters: RegisterEntry;
  976. (* useful operands and types *)
  977. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  978. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  979. commentPrintout: Printout.Printer;
  980. dump: Streams.Writer;
  981. tagsAvailable : BOOLEAN;
  982. supportedInstruction: SupportedInstructionProcedure;
  983. supportedImmediate: SupportedImmediateProcedure;
  984. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  985. emitLabels: BOOLEAN;
  986. runtimeModuleName : SyntaxTree.IdentifierString;
  987. newObjectFile: BOOLEAN;
  988. indexCounter: LONGINT;
  989. profile: BOOLEAN;
  990. profileId, profileInit: IntermediateCode.Section;
  991. profileInitPatchPosition: LONGINT;
  992. numberProcedures: LONGINT;
  993. procedureResultDesignator : SyntaxTree.Designator;
  994. operatorInitializationCodeSection: IntermediateCode.Section;
  995. fingerPrinter: FingerPrinter.FingerPrinter;
  996. temporaries: Variables;
  997. canBeLoaded : BOOLEAN;
  998. currentIsInline: BOOLEAN;
  999. currentMapper: SymbolMapper;
  1000. currentInlineExit: Label;
  1001. moduleBodySection: IntermediateCode.Section;
  1002. NeedDescriptor : BOOLEAN;
  1003. cooperativeSwitches: BOOLEAN;
  1004. lastSwitchPC: LONGINT;
  1005. isUnchecked: BOOLEAN;
  1006. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1007. newObjectFile: BOOLEAN);
  1008. BEGIN
  1009. SELF.system := system;
  1010. SELF.runtimeModuleName := runtime;
  1011. currentScope := NIL;
  1012. hiddenPointerType := NIL;
  1013. delegatePointerType := NIL;
  1014. awaitProcCounter := 0;
  1015. labelId := 0; constId := 0; labelId := 0;
  1016. SELF.checker := checker;
  1017. SELF.backend := backend;
  1018. position := Diagnostics.Invalid;
  1019. conditional := FALSE;
  1020. locked := FALSE;
  1021. InitOperand(result,ModeUndefined);
  1022. addressType := IntermediateCode.GetType(system,system.addressType);
  1023. setType := IntermediateCode.GetType(system,system.setType);
  1024. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1025. byteType := IntermediateCode.GetType(system, system.byteType);
  1026. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1027. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1028. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1029. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1030. nil := IntermediateCode.Immediate(addressType,0);
  1031. IntermediateCode.InitOperand(destination);
  1032. tagsAvailable := TRUE;
  1033. supportedInstruction := supportedInstructionProcedure;
  1034. supportedImmediate := supportedImmediateProcedure;
  1035. inData := FALSE;
  1036. SELF.emitLabels := emitLabels;
  1037. IntermediateCode.InitOperand(arrayDestinationTag);
  1038. bool := IntermediateCode.GetType(system,system.booleanType);
  1039. IntermediateCode.InitImmediate(false,bool,0);
  1040. IntermediateCode.InitImmediate(true,bool,1);
  1041. SELF.newObjectFile := newObjectFile;
  1042. indexCounter := 0;
  1043. NEW(registerUsageCount);
  1044. usedRegisters := NIL;
  1045. procedureResultDesignator := NIL;
  1046. NEW(fingerPrinter, system);
  1047. NEW(temporaries);
  1048. currentIsInline := FALSE;
  1049. NeedDescriptor := FALSE;
  1050. isUnchecked := backend.noRuntimeChecks;
  1051. END Init;
  1052. TYPE Context = RECORD
  1053. section: IntermediateCode.Section;
  1054. registerUsageCount: RegisterUsageCount;
  1055. usedRegisters: RegisterEntry;
  1056. END;
  1057. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1058. VAR context: Context;
  1059. BEGIN
  1060. context.section := section;
  1061. context.registerUsageCount := registerUsageCount;
  1062. context.usedRegisters := usedRegisters;
  1063. section := new;
  1064. NEW(registerUsageCount);
  1065. usedRegisters := NIL;
  1066. RETURN context;
  1067. END SwitchContext;
  1068. PROCEDURE ReturnToContext(context: Context);
  1069. BEGIN
  1070. section := context.section;
  1071. registerUsageCount := context.registerUsageCount;
  1072. usedRegisters := context.usedRegisters;
  1073. END ReturnToContext;
  1074. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1075. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1076. BEGIN
  1077. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1078. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1079. END;
  1080. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1081. section.SetExported(IsExported(syntaxTreeSymbol));
  1082. RETURN section
  1083. END NewSection;
  1084. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1085. VAR new: LONGINT;
  1086. BEGIN
  1087. new := registerUsageCount.Next(type,class);
  1088. UseRegister(new);
  1089. RETURN new
  1090. END AcquireRegister;
  1091. (** get the name for the code section that represens a certain symbol
  1092. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1093. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1094. VAR
  1095. operatorFingerPrint: SyntaxTree.FingerPrint;
  1096. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1097. BEGIN
  1098. Global.GetSymbolSegmentedName(symbol, name);
  1099. (* if the symbol is an operator, then append the fingerprint to the name *)
  1100. IF symbol IS SyntaxTree.Operator THEN
  1101. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1102. string := "[";
  1103. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1104. Strings.Append(string, operatorFingerPrintString);
  1105. Strings.Append(string, "]");
  1106. Basic.AppendToSegmentedName(name,string);
  1107. END
  1108. END GetCodeSectionNameForSymbol;
  1109. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1110. BEGIN
  1111. IF dump # NIL THEN
  1112. dump.String("enter "); dump.String(s); dump.Ln;
  1113. END;
  1114. END TraceEnter;
  1115. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1116. BEGIN
  1117. IF dump # NIL THEN
  1118. dump.String("exit "); dump.String(s); dump.Ln;
  1119. END;
  1120. END TraceExit;
  1121. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1122. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1123. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1124. VAR i: LONGINT;
  1125. BEGIN
  1126. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1127. IF op.rule # NIL THEN
  1128. FOR i := 0 TO LEN(op.rule)-1 DO
  1129. CheckRegister(op.rule[i])
  1130. END;
  1131. END;
  1132. END CheckRegister;
  1133. BEGIN
  1134. CheckRegister(instruction.op1);
  1135. CheckRegister(instruction.op2);
  1136. CheckRegister(instruction.op3);
  1137. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1138. ELSE section.Emit(instruction);
  1139. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1140. END;
  1141. END Emit;
  1142. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1143. BEGIN
  1144. IF backend.cooperative THEN
  1145. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1146. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1147. ELSE
  1148. Emit(Trap(position,trapNo));
  1149. END;
  1150. END EmitTrap;
  1151. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1152. VAR name: Basic.SegmentedName;
  1153. VAR op1, op2, reg: IntermediateCode.Operand;
  1154. VAR call, nocall: Label;
  1155. VAR parametersSize: LONGINT;
  1156. VAR prevSection: IntermediateCode.Section;
  1157. BEGIN
  1158. prevSection := SELF.section;
  1159. SELF.section := section;
  1160. IF backend.hasLinkRegister THEN
  1161. Emit(Push(-1, lr));
  1162. END;
  1163. Emit(Push(-1,fp));
  1164. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1165. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1166. GetCodeSectionNameForSymbol(procedure, name);
  1167. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1168. ELSE
  1169. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1170. END;
  1171. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1172. Emit(Push(-1,op1));
  1173. Emit(Mov(-1,fp, sp));
  1174. NEW(call, section);
  1175. NEW(nocall, section);
  1176. reg := NewRegisterOperand(addressType);
  1177. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1178. Emit(Sub(-1,reg, sp, op1));
  1179. BrltL(call, sp, reg);
  1180. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1181. BrgeL(nocall, sp, op2);
  1182. call.Resolve(section.pc);
  1183. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1184. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1185. Emit(Push(-1, op2));
  1186. Emit(Push(-1, reg));
  1187. ReleaseIntermediateOperand(reg);
  1188. CallThis(position, "Activities","ExpandStack",0);
  1189. Emit(Result(-1, sp));
  1190. ReleaseIntermediateOperand(reg);
  1191. nocall.Resolve(section.pc);
  1192. ELSE
  1193. Emit(Mov(-1, fp, sp));
  1194. END;
  1195. Emit(Enter(-1, callconv, varSize));
  1196. SELF.section := prevSection;
  1197. END EmitEnter;
  1198. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1199. VAR op1,op2: IntermediateCode.Operand;
  1200. VAR instruction: IntermediateCode.Instruction;
  1201. BEGIN
  1202. IntermediateCode.InitNumber(op1,callconv);
  1203. IntermediateCode.InitNumber(op2,varSize);
  1204. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1205. RETURN instruction
  1206. END Enter;
  1207. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1208. VAR op1: IntermediateCode.Operand;
  1209. VAR instruction: IntermediateCode.Instruction;
  1210. BEGIN
  1211. IntermediateCode.InitNumber(op1,callconv);
  1212. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1213. RETURN instruction
  1214. END Leave;
  1215. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1216. VAR prevSection: IntermediateCode.Section;
  1217. VAR op2: IntermediateCode.Operand;
  1218. BEGIN
  1219. prevSection := SELF.section;
  1220. SELF.section := section;
  1221. Emit(Leave(position, callconv));
  1222. IF backend.cooperative THEN
  1223. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1224. Emit(Add(position, sp, fp, op2));
  1225. ELSE
  1226. Emit(Mov(position, sp, fp));
  1227. END;
  1228. Emit(Pop(position, fp));
  1229. SELF.section := prevSection;
  1230. END EmitLeave;
  1231. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1232. VAR m: SymbolMap;
  1233. BEGIN
  1234. position := x.position;
  1235. IF currentIsInline THEN
  1236. m := currentMapper.Get(x);
  1237. IF m # NIL THEN
  1238. (*
  1239. Printout.Info("mapping from", x);
  1240. Printout.Info("mapping to ", m.to);
  1241. *)
  1242. m.to.Accept(SELF);
  1243. op := result;
  1244. IF m.tag # NIL THEN
  1245. ReleaseIntermediateOperand(result.tag);
  1246. m.tag.Accept(SELF);
  1247. op.tag := result.op;
  1248. ReleaseIntermediateOperand(result.tag);
  1249. END;
  1250. RETURN
  1251. END;
  1252. END;
  1253. x.Accept(SELF);
  1254. op := result;
  1255. END Symbol;
  1256. PROCEDURE Expression(x: SyntaxTree.Expression);
  1257. BEGIN
  1258. position := x.position;
  1259. constantDeclaration := NIL;
  1260. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1261. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1262. END;
  1263. IF x.resolved # NIL THEN
  1264. x.resolved.Accept(SELF)
  1265. ELSE
  1266. x.Accept(SELF)
  1267. END;
  1268. (* check this, was commented out in ActiveCells3 *)
  1269. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1270. Error(x.position, "unsupported modifier");
  1271. END;
  1272. END Expression;
  1273. (*
  1274. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1275. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1276. BEGIN
  1277. temporaries.GetUsage(current);
  1278. FOR i := 0 TO LEN(current)-1 DO
  1279. set := current[i] - previous[i];
  1280. IF set # {} THEN
  1281. FOR j := 0 TO MAX(SET)-1 DO
  1282. IF j IN set THEN
  1283. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1284. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1285. Symbol(variable, op);
  1286. MakeMemory(tmp,op.op,addressType,0);
  1287. ReleaseOperand(op);
  1288. Emit(Mov(position,tmp, nil ) );
  1289. ReleaseIntermediateOperand(tmp);
  1290. END;
  1291. END;
  1292. END;
  1293. END;
  1294. END;
  1295. END ResetUsedTemporaries;
  1296. *)
  1297. PROCEDURE Statement(x: SyntaxTree.Statement);
  1298. VAR use: VariableUse;
  1299. BEGIN
  1300. temporaries.GetUsage(use);
  1301. position := x.position;
  1302. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1303. IF commentPrintout # NIL THEN
  1304. commentPrintout.Statement(x);
  1305. dump.Ln;
  1306. (*dump.Update;*)
  1307. END;
  1308. x.Accept(SELF);
  1309. (*
  1310. CheckRegistersFree();
  1311. *)
  1312. (*ResetUsedTemporaries(use);*)
  1313. temporaries.SetUsage(use);
  1314. END Statement;
  1315. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1316. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1317. *)
  1318. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1319. BEGIN
  1320. IF op.mode = IntermediateCode.ModeMemory THEN
  1321. ReuseCopy(res,op);
  1322. ELSE
  1323. res := op;
  1324. UseIntermediateOperand(res);
  1325. END;
  1326. IntermediateCode.AddOffset(res,offset);
  1327. IntermediateCode.MakeMemory(res,type);
  1328. END MakeMemory;
  1329. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1330. VAR mem: IntermediateCode.Operand;
  1331. BEGIN
  1332. MakeMemory(mem,res,type,offset);
  1333. ReleaseIntermediateOperand(res);
  1334. res := mem;
  1335. END ToMemory;
  1336. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1337. VAR mem: IntermediateCode.Operand;
  1338. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1339. componentType: SyntaxTree.Type;
  1340. BEGIN
  1341. type := type.resolved;
  1342. IF operand.mode = ModeReference THEN
  1343. IF type IS SyntaxTree.RangeType THEN
  1344. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1345. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1346. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1347. ReleaseOperand(operand);
  1348. operand.op := firstOp;
  1349. operand.tag := lastOp;
  1350. operand.extra := stepOp;
  1351. ELSIF type IS SyntaxTree.ComplexType THEN
  1352. componentType := type(SyntaxTree.ComplexType).componentType;
  1353. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1354. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1355. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1356. ReleaseOperand(operand);
  1357. operand.op := firstOp;
  1358. operand.tag := lastOp
  1359. ELSE
  1360. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1361. ReleaseIntermediateOperand(operand.op);
  1362. operand.op := mem;
  1363. END;
  1364. operand.mode := ModeValue;
  1365. END;
  1366. ASSERT(operand.mode = ModeValue);
  1367. END LoadValue;
  1368. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1369. VAR prevConditional: BOOLEAN;
  1370. BEGIN
  1371. prevConditional := conditional;
  1372. conditional := FALSE;
  1373. InitOperand(result, ModeUndefined);
  1374. Expression(x);
  1375. op := result;
  1376. LoadValue(op,x.type.resolved);
  1377. conditional := prevConditional;
  1378. END Evaluate;
  1379. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1380. VAR prevConditional: BOOLEAN;
  1381. BEGIN
  1382. prevConditional := conditional;
  1383. conditional := FALSE;
  1384. InitOperand(result,ModeUndefined);
  1385. Expression(x);
  1386. op := result;
  1387. (*
  1388. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1389. *)
  1390. conditional := prevConditional;
  1391. END Designate;
  1392. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1393. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1394. BEGIN
  1395. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1396. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1397. conditional := TRUE;
  1398. trueLabel := trueL; falseLabel := falseL;
  1399. Expression(x);
  1400. trueL := trueLabel; falseL := falseLabel;
  1401. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1402. END Condition;
  1403. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1404. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1405. BEGIN
  1406. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1407. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1408. RETURN op
  1409. END NewRegisterOperand;
  1410. PROCEDURE UnuseRegister(register: LONGINT);
  1411. BEGIN
  1412. IF (register > 0) THEN
  1413. register := registerUsageCount.Map(register);
  1414. registerUsageCount.DecUse(register);
  1415. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1416. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1417. END;
  1418. IF registerUsageCount.Use(register)=0 THEN
  1419. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1420. Warning(position, "register cannot be removed");
  1421. END;
  1422. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1423. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1424. END;
  1425. ELSIF registerUsageCount.Use(register)<0 THEN
  1426. Warning(position, "register removed too often");
  1427. D.TraceBack;
  1428. END;
  1429. END;
  1430. END UnuseRegister;
  1431. PROCEDURE UseRegister(register: LONGINT);
  1432. BEGIN
  1433. IF (register > 0) THEN
  1434. register := registerUsageCount.Map(register);
  1435. registerUsageCount.IncUse(register);
  1436. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1437. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1438. END;
  1439. IF registerUsageCount.Use(register)=1 THEN
  1440. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1441. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1442. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1443. END;
  1444. END;
  1445. END;
  1446. END UseRegister;
  1447. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1448. BEGIN
  1449. UnuseRegister(op.register)
  1450. END ReleaseIntermediateOperand;
  1451. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1452. BEGIN
  1453. UseRegister(op.register)
  1454. END UseIntermediateOperand;
  1455. PROCEDURE ReleaseOperand(CONST op: Operand);
  1456. BEGIN
  1457. UnuseRegister(op.op.register);
  1458. UnuseRegister(op.tag.register);
  1459. UnuseRegister(op.extra.register);
  1460. END ReleaseOperand;
  1461. (* save registers marked in array "markedRegisters" to the stack
  1462. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1463. *)
  1464. PROCEDURE SaveRegisters();
  1465. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1466. BEGIN
  1467. entry := usedRegisters;
  1468. WHILE entry # NIL DO
  1469. type := registerUsageCount.used[entry.register].type;
  1470. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1471. Emit(Push(position,op));
  1472. entry := entry.next;
  1473. END;
  1474. END SaveRegisters;
  1475. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1476. BEGIN
  1477. saved := usedRegisters;
  1478. usedRegisters := NIL;
  1479. END ReleaseUsedRegisters;
  1480. (** remove parameter registers from used queue *)
  1481. PROCEDURE ReleaseParameterRegisters;
  1482. VAR entry,prev,next: RegisterEntry;
  1483. BEGIN
  1484. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1485. WHILE entry # NIL DO
  1486. next := entry.next;
  1487. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1488. registerUsageCount.DecUse(entry.register);
  1489. ASSERT(registerUsageCount.Use(entry.register)=0);
  1490. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1491. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1492. END;
  1493. ELSIF prev = NIL THEN
  1494. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1495. ELSE
  1496. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1497. END;
  1498. entry := next;
  1499. END;
  1500. END ReleaseParameterRegisters;
  1501. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1502. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1503. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1504. BEGIN
  1505. entry := saved;
  1506. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1507. entry := prev;
  1508. WHILE entry # NIL DO
  1509. prev := entry.prev;
  1510. type := entry.type;
  1511. class := entry.registerClass;
  1512. IntermediateCode.InitRegister(op,type,class,entry.register);
  1513. (*
  1514. new := registerUsageCount.Next(type,class);
  1515. registerUsageCount.Remap(entry.register,new);
  1516. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1517. dump.String("remap register "); dump.Int(entry.register,1);
  1518. dump.String("to "); dump.Int(new,1);
  1519. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1520. END;
  1521. entry.register := new;
  1522. *)
  1523. Emit(Pop(position,op));
  1524. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1525. entry := prev;
  1526. END;
  1527. (*
  1528. usedRegisters := saved;
  1529. *)
  1530. END RestoreRegisters;
  1531. PROCEDURE CheckRegistersFree;
  1532. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1533. BEGIN
  1534. IF usedRegisters # NIL THEN
  1535. r := usedRegisters;
  1536. WHILE r # NIL DO
  1537. warning := "register ";
  1538. Strings.AppendInt(warning, r.register);
  1539. Strings.Append(warning, " not released.");
  1540. Warning(position,warning);
  1541. r := r .next;
  1542. END;
  1543. END;
  1544. FOR i := 0 TO registerUsageCount.count-1 DO
  1545. IF registerUsageCount.used[i].count < 0 THEN
  1546. warning := "register ";
  1547. Strings.AppendInt(warning, i);
  1548. Strings.Append(warning, " unused too often.");
  1549. Warning(position,warning);
  1550. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1551. warning := "register ";
  1552. Strings.AppendInt(warning, i);
  1553. Strings.Append(warning, " not unused often enough.");
  1554. Warning(position,warning);
  1555. END;
  1556. END;
  1557. END CheckRegistersFree;
  1558. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1559. Otherwise allocate a new register.
  1560. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1561. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1562. BEGIN
  1563. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1564. UseIntermediateOperand(result);
  1565. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1566. UseIntermediateOperand(result);
  1567. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1568. END;
  1569. END Reuse2;
  1570. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1571. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1572. If not then allocate a new register.
  1573. Does NOT necessarily keep the content of src1 or src2 in result!
  1574. *)
  1575. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1576. BEGIN
  1577. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1578. UseIntermediateOperand(result);
  1579. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1580. UseIntermediateOperand(result);
  1581. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1582. result := alternative; alternative := emptyOperand;
  1583. UseIntermediateOperand(result);
  1584. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1585. END;
  1586. END Reuse2a;
  1587. (* like reuse2 but only one source *)
  1588. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1589. BEGIN
  1590. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1591. UseIntermediateOperand(result);
  1592. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1593. END;
  1594. END Reuse1;
  1595. (* like reuse2a but only one source *)
  1596. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1597. BEGIN
  1598. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1599. UseIntermediateOperand(result);
  1600. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1601. UseIntermediateOperand(result);
  1602. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1603. END;
  1604. END Reuse1a;
  1605. (* like reuse1 but guarantees that content of src1 is in result *)
  1606. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1607. BEGIN
  1608. IF ReusableRegister(src1) THEN
  1609. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1610. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1611. UseIntermediateOperand(result);
  1612. ELSE
  1613. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1614. Emit(Mov(position,result,src1));
  1615. END
  1616. END ReuseCopy;
  1617. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1618. BEGIN
  1619. IF ReusableRegister(src) THEN
  1620. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1621. ELSE
  1622. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1623. Emit(Mov(position,result,src));
  1624. ReleaseIntermediateOperand(src);
  1625. END
  1626. END TransferToRegister;
  1627. (** labels and branches **)
  1628. PROCEDURE NewLabel(): Label;
  1629. VAR label: Label;
  1630. BEGIN
  1631. NEW(label,section); RETURN label;
  1632. END NewLabel;
  1633. PROCEDURE SetLabel(label: Label);
  1634. BEGIN label.Resolve(section.pc);
  1635. END SetLabel;
  1636. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1637. BEGIN
  1638. ASSERT(label # NIL);
  1639. IF label.pc < 0 THEN (* label not yet set *)
  1640. label.AddFixup(section.pc);
  1641. END;
  1642. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1643. END LabelOperand;
  1644. PROCEDURE BrL(label: Label);
  1645. BEGIN
  1646. Emit(Br(position,LabelOperand(label)));
  1647. END BrL;
  1648. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1649. BEGIN
  1650. Emit(Brge(position,LabelOperand(label),left,right));
  1651. END BrgeL;
  1652. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1653. BEGIN
  1654. Emit(Brlt(position,LabelOperand(label),left,right));
  1655. END BrltL;
  1656. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1657. BEGIN
  1658. Emit(Breq(position,LabelOperand(label),left,right));
  1659. END BreqL;
  1660. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1661. BEGIN
  1662. Emit(Brne(position,LabelOperand(label),left,right));
  1663. END BrneL;
  1664. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1665. VAR new: IntermediateCode.Operand;
  1666. BEGIN
  1667. IF Trace THEN TraceEnter("Convert") END;
  1668. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1669. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1670. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1671. & (operand.offset = 0)
  1672. THEN
  1673. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1674. Emit(Conv(position,new,operand));
  1675. ELSE
  1676. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1677. Emit(Conv(position,new,operand));
  1678. ReleaseIntermediateOperand(operand);
  1679. END;
  1680. operand := new;
  1681. 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
  1682. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1683. ELSE
  1684. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1685. Emit(Conv(position,new,operand));
  1686. ReleaseIntermediateOperand(operand);
  1687. operand := new;
  1688. END;
  1689. IF Trace THEN TraceExit("Convert") END;
  1690. END Convert;
  1691. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1692. VAR exit: Label;
  1693. BEGIN
  1694. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1695. exit := NewLabel();
  1696. br(exit,left,right);
  1697. EmitTrap(position,trapNo);
  1698. SetLabel(exit);
  1699. END TrapC;
  1700. (** expressions *)
  1701. (** emit necessary runtime check for set elements **)
  1702. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1703. VAR max: IntermediateCode.Operand;
  1704. BEGIN
  1705. IF isUnchecked THEN RETURN END;
  1706. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1707. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1708. TrapC(BrgeL, max, o, SetElementTrap);
  1709. END;
  1710. END CheckSetElement;
  1711. (** the set that a range represents **)
  1712. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1713. VAR
  1714. operand: Operand;
  1715. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1716. BEGIN
  1717. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1718. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1719. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1720. one := IntermediateCode.Immediate(setType, 1);
  1721. Evaluate(x, operand);
  1722. Convert(operand.op, setType);
  1723. Convert(operand.tag, setType);
  1724. CheckSetElement(operand.op);
  1725. CheckSetElement(operand.tag);
  1726. (* create mask for lower bound
  1727. i.e. shift 11111111 to the left by the value of the lower bound
  1728. *)
  1729. Reuse1(temp, operand.op);
  1730. Emit(Shl(position,temp, allBits, operand.op));
  1731. ReleaseIntermediateOperand(operand.op);
  1732. operand.op := temp;
  1733. (* create mask for upper bound
  1734. i.e. shift 11111111 to the right by the difference between the
  1735. upper bound and the maximum number of set elements
  1736. *)
  1737. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1738. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1739. Reuse1(temp, operand.tag);
  1740. ELSE
  1741. Reuse1(temp, operand.tag);
  1742. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1743. Emit(Sub(position,temp, size, operand.tag));
  1744. END;
  1745. Emit(Shr(position,temp, allBits, operand.tag));
  1746. ReleaseIntermediateOperand(operand.tag);
  1747. operand.tag := temp;
  1748. Reuse2(resultingSet, operand.op, operand.tag);
  1749. (* intersect the two masks *)
  1750. Emit(And(position,resultingSet, operand.op, operand.tag));
  1751. ReleaseOperand(operand);
  1752. RETURN resultingSet
  1753. END SetFromRange;
  1754. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1755. VAR
  1756. res, operand: Operand;
  1757. temp, one, noBits, dest: IntermediateCode.Operand;
  1758. expression: SyntaxTree.Expression;
  1759. i: LONGINT;
  1760. BEGIN
  1761. IF Trace THEN TraceEnter("VisitSet") END;
  1762. dest := destination;
  1763. destination := emptyOperand;
  1764. noBits := IntermediateCode.Immediate(setType, 0);
  1765. one := IntermediateCode.Immediate(setType, 1);
  1766. (* start off with the empty set *)
  1767. InitOperand(res, ModeValue);
  1768. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1769. Emit(Mov(position,res.op, noBits));
  1770. FOR i := 0 TO x.elements.Length() - 1 DO
  1771. expression := x.elements.GetExpression(i);
  1772. IF expression IS SyntaxTree.RangeExpression THEN
  1773. (* range of set elements *)
  1774. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1775. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1776. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1777. ReleaseIntermediateOperand(temp)
  1778. ELSE
  1779. (* singelton element *)
  1780. Evaluate(expression, operand);
  1781. Convert(operand.op, setType);
  1782. CheckSetElement(operand.op);
  1783. (* create subset containing single element *)
  1784. Reuse1(temp, operand.op);
  1785. Emit(Shl(position,temp, one, operand.op));
  1786. ReleaseOperand(operand);
  1787. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1788. ReleaseIntermediateOperand(temp);
  1789. END
  1790. END;
  1791. result := res;
  1792. destination := dest;
  1793. IF Trace THEN TraceExit("VisitSet") END;
  1794. END VisitSet;
  1795. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1796. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1797. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1798. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1799. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1800. element: SyntaxTree.IntegerValue;
  1801. BEGIN
  1802. numberElements := x.elements.Length();
  1803. expression := index.parameters.GetExpression(dim);
  1804. element := expression(SyntaxTree.IntegerValue);
  1805. FOR i := 0 TO numberElements-1 DO
  1806. expression := x.elements.GetExpression(i);
  1807. element.SetValue(i);
  1808. IF expression IS SyntaxTree.MathArrayExpression THEN
  1809. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1810. ELSE
  1811. Assign(index,expression);
  1812. END;
  1813. END;
  1814. END RecursiveAssignment;
  1815. BEGIN
  1816. variable := GetTemporaryVariable(x.type, FALSE);
  1817. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1818. designator.SetType(variable.type);
  1819. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1820. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1821. FOR i := 0 TO dim-1 DO
  1822. element := SyntaxTree.NewIntegerValue(x.position,0);
  1823. element.SetType(system.longintType);
  1824. index.parameters.AddExpression(element);
  1825. END;
  1826. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1827. RecursiveAssignment(x,0);
  1828. Expression(designator);
  1829. END VisitMathArrayExpression;
  1830. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1831. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1832. BEGIN
  1833. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1834. dest := destination; destination := emptyOperand;
  1835. IF x.operator = Scanner.Not THEN
  1836. IF conditional THEN
  1837. Condition(x.left,falseLabel,trueLabel)
  1838. ELSE
  1839. Evaluate(x.left,operand);
  1840. InitOperand(result,ModeValue);
  1841. Reuse1a(result.op,operand.op,dest);
  1842. Emit(Xor(position,result.op,operand.op,true));
  1843. ReleaseOperand(operand);
  1844. END;
  1845. ELSIF x.operator = Scanner.Minus THEN
  1846. Evaluate(x.left,operand);
  1847. InitOperand(result,ModeValue);
  1848. Reuse1a(result.op,operand.op,dest);
  1849. type := x.left.type.resolved;
  1850. IF type IS SyntaxTree.SetType THEN
  1851. Emit(Not(position,result.op,operand.op));
  1852. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1853. Reuse1(result.tag,operand.tag);
  1854. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1855. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1856. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1857. Emit(Neg(position,result.op,operand.op));
  1858. ELSE HALT(200)
  1859. END;
  1860. ReleaseOperand(operand);
  1861. ELSIF x.operator = Scanner.Address THEN
  1862. Designate(x.left,operand);
  1863. operand.mode := ModeValue;
  1864. t0 := x.left.type.resolved;
  1865. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1866. ReleaseIntermediateOperand(operand.op);
  1867. operand.op := operand.tag;
  1868. IntermediateCode.InitOperand(operand.tag);
  1869. END;
  1870. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1871. result := operand;
  1872. ELSE HALT(100)
  1873. END;
  1874. destination := dest;
  1875. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1876. END VisitUnaryExpression;
  1877. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1878. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1879. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1880. BEGIN
  1881. type := type.resolved;
  1882. originalType := type;
  1883. IF type IS SyntaxTree.PointerType THEN
  1884. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1885. END;
  1886. IF type IS SyntaxTree.ObjectType THEN
  1887. BrL(trueL);
  1888. ELSE
  1889. ASSERT(type IS SyntaxTree.RecordType);
  1890. (*
  1891. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1892. *)
  1893. ReuseCopy(left,tag);
  1894. right := TypeDescriptorAdr(type);
  1895. IF ~newObjectFile THEN
  1896. IntermediateCode.MakeMemory(right,addressType);
  1897. END;
  1898. IF backend.cooperative THEN
  1899. repeatL := NewLabel();
  1900. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1901. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1902. END;
  1903. SetLabel(repeatL);
  1904. BreqL(trueL,left,right);
  1905. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1906. BrneL(repeatL,left,nil);
  1907. ELSE
  1908. level := type(SyntaxTree.RecordType).Level();
  1909. (* get type desc tag of level relative to base tag *)
  1910. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1911. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1912. IntermediateCode.MakeMemory(left,addressType);
  1913. BreqL(trueL,left,right);
  1914. END;
  1915. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1916. BrL(falseL);
  1917. END;
  1918. END TypeTest;
  1919. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1920. BEGIN
  1921. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1922. IF dump # NIL THEN
  1923. dump.String(s); dump.Ln;
  1924. END;
  1925. END Error;
  1926. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1927. BEGIN
  1928. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1929. IF dump # NIL THEN
  1930. dump.String(s); dump.Ln; dump.Update;
  1931. END;
  1932. END Warning;
  1933. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1934. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1935. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1936. operand:Operand;
  1937. BEGIN
  1938. previousSection := section;
  1939. Global.GetModuleName(mod,name);
  1940. Strings.Append(name,".@Trace");
  1941. Basic.ToSegmentedName(name, pooledName);
  1942. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1943. IF dump # NIL THEN dump := section.comments END;
  1944. variable := mod.moduleScope.firstVariable;
  1945. WHILE variable # NIL DO
  1946. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1947. Symbol(variable, operand);
  1948. register := operand.op;
  1949. CallTraceMethod(register, variable.type);
  1950. ReleaseIntermediateOperand(register);
  1951. END;
  1952. variable := variable.nextVariable;
  1953. END;
  1954. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1955. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1956. Emit(Br(position,op));
  1957. INC(statCoopTraceModule, section.pc);
  1958. section := previousSection;
  1959. IF dump # NIL THEN dump := section.comments END;
  1960. END CreateTraceModuleMethod;
  1961. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1962. BEGIN
  1963. Emit (Push(position, dst));
  1964. Emit (Push(position, src));
  1965. CallThis(position,"GarbageCollector","Assign", 2);
  1966. END CallAssignPointer;
  1967. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1968. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1969. BEGIN
  1970. IF SemanticChecker.IsPointerType (type) THEN
  1971. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  1972. ELSIF type.IsRecordType() THEN
  1973. Emit (Push(position,dst));
  1974. Emit (Push(position,src));
  1975. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  1976. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1977. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1978. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  1979. ELSIF IsStaticArray(type) THEN
  1980. size := StaticArrayNumElements(type);
  1981. base := StaticArrayBaseType(type);
  1982. IF base.IsRecordType() THEN
  1983. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1984. Emit (Push(position, dst));
  1985. Emit (Push(position, src));
  1986. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  1987. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  1988. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1989. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1990. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  1991. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  1992. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1993. Emit (Push(position, dst));
  1994. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1995. Emit (Push(position, src));
  1996. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  1997. ELSE
  1998. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1999. Emit (Push(position, dst));
  2000. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2001. Emit (Push(position, src));
  2002. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2003. ASSERT(StaticArrayBaseType(type).IsPointer());
  2004. END;
  2005. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2006. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2007. Emit (Push(position, dst));
  2008. Emit (Push(position, src));
  2009. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2010. ELSE HALT(100); (* missing ? *)
  2011. END;
  2012. END CallAssignMethod;
  2013. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2014. VAR name: Basic.SegmentedName;
  2015. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2016. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2017. context: Context;
  2018. BEGIN
  2019. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2020. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2021. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2022. GetRecordTypeName (recordType,name);
  2023. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2024. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2025. IF dump # NIL THEN dump := section.comments END;
  2026. variable := recordType.recordScope.firstVariable;
  2027. WHILE variable # NIL DO
  2028. IF variable.NeedsTrace() THEN
  2029. dst := NewRegisterOperand (addressType);
  2030. src := NewRegisterOperand (addressType);
  2031. Emit (Mov(position, dst, parameter1));
  2032. Emit (Mov(position, src, parameter2));
  2033. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2034. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2035. CallAssignMethod(dst, src, variable.type);
  2036. ReleaseIntermediateOperand(src);
  2037. ReleaseIntermediateOperand(dst);
  2038. END;
  2039. variable := variable.nextVariable;
  2040. END;
  2041. recordBase := recordType.GetBaseRecord();
  2042. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2043. GetRecordTypeName (recordBase,name);
  2044. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2045. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2046. Emit(Br(position,op));
  2047. ELSE
  2048. Emit(Exit(position,0,0));
  2049. END;
  2050. IF ~recordType.isObject THEN
  2051. GetRecordTypeName (recordType,name);
  2052. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2053. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2054. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2055. NEW(registerUsageCount);
  2056. usedRegisters := NIL;
  2057. dst := NewRegisterOperand (addressType);
  2058. src := NewRegisterOperand (addressType);
  2059. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2060. Emit(Mov(position, dst, parameter1));
  2061. Emit(Mov(position, src, parameter2));
  2062. label := NewLabel();
  2063. SetLabel(label);
  2064. Emit(Push(position, dst));
  2065. Emit(Push(position, src));
  2066. GetRecordTypeName (recordType,name);
  2067. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2068. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2069. Emit(Call(position, op, 0));
  2070. Emit(Pop(position, src));
  2071. Emit(Pop(position, dst));
  2072. Emit(Add(position, dst, dst, ofs));
  2073. Emit(Add(position, src, src, ofs));
  2074. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2075. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2076. Emit(Exit(position,0,0));
  2077. END;
  2078. INC(statCoopAssignProcedure, section.pc);
  2079. ReturnToContext(context);
  2080. IF dump # NIL THEN dump := section.comments END;
  2081. END CreateAssignProcedure;
  2082. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2083. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2084. BEGIN
  2085. IF IsUnsafePointer (type) THEN
  2086. skip := NewLabel();
  2087. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2088. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2089. BreqL (skip, op, nil);
  2090. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2091. SetLabel (skip);
  2092. ELSIF SemanticChecker.IsPointerType (type) THEN
  2093. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2094. CallThis(position,"GarbageCollector","Mark", 1);
  2095. ELSIF type.IsRecordType() THEN
  2096. Emit (Push(position,register));
  2097. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2098. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2099. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2100. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2101. ELSIF IsStaticArray(type) THEN
  2102. size := StaticArrayNumElements(type);
  2103. base := StaticArrayBaseType(type);
  2104. IF base.IsRecordType() THEN
  2105. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2106. Emit (Push(position, register));
  2107. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2108. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2109. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2110. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2111. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2112. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2113. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2114. Emit (Push(position, register));
  2115. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2116. ELSE
  2117. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2118. Emit (Push(position, register));
  2119. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2120. ASSERT(base.IsPointer());
  2121. END;
  2122. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2123. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2124. CallThis(position,"GarbageCollector","Mark", 1);
  2125. ELSE HALT(100); (* missing ? *)
  2126. END;
  2127. END CallTraceMethod;
  2128. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2129. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2130. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2131. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2132. BEGIN
  2133. previousSection := section;
  2134. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2135. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2136. GetRecordTypeName (recordType,name);
  2137. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2138. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2139. IF dump # NIL THEN dump := section.comments END;
  2140. variable := recordType.recordScope.firstVariable;
  2141. WHILE variable # NIL DO
  2142. IF variable.NeedsTrace() THEN
  2143. register := NewRegisterOperand (addressType);
  2144. Emit (Mov(position,register,parameter1));
  2145. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2146. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2147. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2148. END;
  2149. CallTraceMethod(register, variable.type);
  2150. ReleaseIntermediateOperand(register);
  2151. END;
  2152. variable := variable.nextVariable;
  2153. END;
  2154. recordBase := recordType.GetBaseRecord();
  2155. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2156. recordBase := recordBase.GetBaseRecord();
  2157. END;
  2158. IF recordBase # NIL THEN
  2159. GetRecordTypeName (recordBase,name);
  2160. IF HasExplicitTraceMethod (recordBase) THEN
  2161. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2162. ELSE
  2163. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2164. END;
  2165. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2166. Emit(Br(position,op));
  2167. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2168. Emit(Exit(position,0,0));
  2169. ELSE
  2170. IF recordType.isObject THEN
  2171. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2172. ELSE
  2173. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2174. END;
  2175. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2176. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2177. Emit(Br(position,op));
  2178. END;
  2179. IF ~recordType.isObject THEN
  2180. GetRecordTypeName (recordType,name);
  2181. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2182. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2183. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2184. NEW(registerUsageCount);
  2185. usedRegisters := NIL;
  2186. IF dump # NIL THEN dump := section.comments END;
  2187. register := NewRegisterOperand (addressType);
  2188. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2189. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2190. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2191. END;
  2192. Emit (Push(position,register));
  2193. GetRecordTypeName (recordType,name);
  2194. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2195. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2196. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2197. ReleaseIntermediateOperand(register);
  2198. Emit(Exit(position,0,0));
  2199. GetRecordTypeName (recordType,name);
  2200. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2201. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2202. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2203. NEW(registerUsageCount);
  2204. usedRegisters := NIL;
  2205. register := NewRegisterOperand (addressType);
  2206. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2207. Emit(Mov(position, register, parameter1));
  2208. label := NewLabel();
  2209. SetLabel(label);
  2210. Emit(Push(position, register));
  2211. GetRecordTypeName (recordType,name);
  2212. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2213. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2214. Emit(Call(position, op, 0));
  2215. Emit(Pop(position, register));
  2216. Emit(Add(position, register, register, ofs));
  2217. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2218. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2219. Emit(Exit(position,0,0));
  2220. END;
  2221. INC(statCoopTraceMethod, section.pc);
  2222. ReturnToContext(context);
  2223. IF dump # NIL THEN dump := section.comments END;
  2224. END CreateTraceMethod;
  2225. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2226. VAR name: Basic.SegmentedName;
  2227. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2228. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2229. context: Context;
  2230. BEGIN
  2231. IF recordType.isObject THEN RETURN END;
  2232. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2233. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2234. GetRecordTypeName (recordType,name);
  2235. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2236. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2237. IF dump # NIL THEN dump := section.comments END;
  2238. variable := recordType.recordScope.firstVariable;
  2239. WHILE variable # NIL DO
  2240. IF variable.NeedsTrace() THEN
  2241. dst := NewRegisterOperand (addressType);
  2242. Emit (Mov(position, dst, parameter1));
  2243. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2244. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2245. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2246. END;
  2247. CallResetProcedure(dst, nil, variable.type);
  2248. ReleaseIntermediateOperand(dst);
  2249. END;
  2250. variable := variable.nextVariable;
  2251. END;
  2252. recordBase := recordType.GetBaseRecord();
  2253. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2254. GetRecordTypeName (recordBase,name);
  2255. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2256. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2257. Emit(Br(position,op));
  2258. ELSE
  2259. Emit(Exit(position,0,0));
  2260. END;
  2261. GetRecordTypeName (recordType,name);
  2262. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2263. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2264. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2265. NEW(registerUsageCount);
  2266. usedRegisters := NIL;
  2267. dst := NewRegisterOperand (addressType);
  2268. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2269. Emit(Mov(position, dst, parameter1));
  2270. label := NewLabel();
  2271. SetLabel(label);
  2272. Emit(Push(position, dst));
  2273. GetRecordTypeName (recordType,name);
  2274. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2275. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2276. Emit(Call(position, op, 0));
  2277. Emit(Pop(position, dst));
  2278. Emit(Add(position, dst, dst, ofs));
  2279. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2280. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2281. Emit(Exit(position,0,0));
  2282. INC(statCoopResetProcedure, section.pc);
  2283. ReturnToContext(context);
  2284. IF dump # NIL THEN dump := section.comments END;
  2285. END CreateResetProcedure;
  2286. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2287. VAR name: Basic.SegmentedName; context: Context;
  2288. BEGIN
  2289. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2290. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2291. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2292. IF dump # NIL THEN dump := section.comments END;
  2293. Emit(Push(position,fp));
  2294. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2295. ResetVariables(scope);
  2296. Emit(Pop(position,fp));
  2297. Emit(Exit(position,0,0));
  2298. ReturnToContext(context);
  2299. IF dump # NIL THEN dump := section.comments END;
  2300. END CreateResetMethod;
  2301. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2302. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2303. BEGIN
  2304. IF SemanticChecker.IsPointerType (type) THEN
  2305. Emit (Push(position, dest));
  2306. CallThis(position,"GarbageCollector","Reset", 1);
  2307. ELSIF type.IsRecordType() THEN
  2308. Emit (Push(position, dest));
  2309. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2310. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2311. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2312. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2313. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2314. size := GetArrayLength(type, tag);
  2315. base := ArrayBaseType(type);
  2316. IF base.IsRecordType() THEN
  2317. Emit (Push(position, size));
  2318. Emit (Push(position, dest));
  2319. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2320. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2321. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2322. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2323. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2324. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2325. Emit (Push(position, size));
  2326. Emit (Push(position, dest));
  2327. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2328. ELSE
  2329. Emit (Push(position, size));
  2330. Emit (Push(position, dest));
  2331. CallThis(position,"GarbageCollector","ResetArray", 2);
  2332. ASSERT(ArrayBaseType(type).IsPointer());
  2333. END;
  2334. ReleaseIntermediateOperand(size);
  2335. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2336. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2337. Emit (Push(position, dest));
  2338. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2339. ELSE HALT(100); (* missing ? *)
  2340. END;
  2341. END CallResetProcedure;
  2342. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2343. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2344. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2345. VAR operand: Operand;
  2346. BEGIN
  2347. Symbol (symbol, operand);
  2348. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2349. ReleaseOperand(operand);
  2350. END Reset;
  2351. BEGIN
  2352. previousScope := currentScope;
  2353. currentScope := scope;
  2354. pc := section.pc;
  2355. variable := scope.firstVariable;
  2356. WHILE variable # NIL DO
  2357. IF variable.NeedsTrace() THEN
  2358. Reset (variable);
  2359. END;
  2360. variable := variable.nextVariable;
  2361. END;
  2362. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2363. WHILE parameter # NIL DO
  2364. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2365. Reset (parameter);
  2366. END;
  2367. parameter := parameter.nextParameter;
  2368. END;
  2369. INC(statCoopResetVariables, section.pc - pc);
  2370. currentScope := previousScope;
  2371. END ResetVariables;
  2372. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2373. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2374. VAR op: IntermediateCode.Operand; context: Context;
  2375. BEGIN
  2376. previousSection := section;
  2377. GetCodeSectionNameForSymbol(procedure, name);
  2378. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2379. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2380. IF dump # NIL THEN dump := section.comments END;
  2381. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2382. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2383. Emit(Data(position,op));
  2384. Emit(Data(position,nil));
  2385. IF HasPointers (procedure.procedureScope) THEN
  2386. GetCodeSectionNameForSymbol(procedure, name);
  2387. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2388. ELSE
  2389. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2390. END;
  2391. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2392. Emit(Data(position,op));
  2393. ReturnToContext(context);
  2394. IF dump # NIL THEN dump := section.comments END;
  2395. END CreateProcedureDescriptor;
  2396. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2397. VAR import: SyntaxTree.Import;
  2398. s: Basic.MessageString;
  2399. selfName: SyntaxTree.IdentifierString;
  2400. BEGIN
  2401. moduleScope.ownerModule.GetName(selfName);
  2402. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2403. module := moduleScope.ownerModule
  2404. ELSE
  2405. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2406. IF import = NIL THEN
  2407. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2408. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2409. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2410. s := "Module ";
  2411. Strings.Append(s,moduleName);
  2412. Strings.Append(s," cannot be imported.");
  2413. IF force THEN
  2414. Error(position,s);
  2415. ELSIF canBeLoaded THEN
  2416. Strings.Append(s, "=> no dynamic linking.");
  2417. Warning(position, s);
  2418. canBeLoaded := FALSE;
  2419. END;
  2420. RETURN FALSE
  2421. ELSE
  2422. SELF.module.imports.AddName(moduleName)
  2423. END;
  2424. ELSIF import.module = NIL THEN (* already tried *)
  2425. RETURN FALSE
  2426. END;
  2427. module := import.module;
  2428. END;
  2429. RETURN TRUE
  2430. END AddImport;
  2431. (* needed for old binary object file format*)
  2432. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2433. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2434. BEGIN
  2435. IF AddImport(moduleName,module,TRUE) THEN
  2436. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2437. IF procedure = NIL THEN
  2438. s := "Instruction not supported on target, emulation procedure ";
  2439. Strings.Append(s,moduleName);
  2440. Strings.Append(s,".");
  2441. Strings.Append(s,procedureName);
  2442. Strings.Append(s," not present");
  2443. Error(position,s);
  2444. ELSE
  2445. StaticCallOperand(r,procedure);
  2446. ReleaseOperand(r);
  2447. fp := GetFingerprint(procedure);
  2448. END;
  2449. END;
  2450. END EnsureSymbol;
  2451. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2452. VAR exit: Label; trueL,falseL: Label;
  2453. BEGIN
  2454. trueL := NewLabel();
  2455. falseL := NewLabel();
  2456. exit := NewLabel();
  2457. Condition(x,trueL,falseL);
  2458. InitOperand(result,ModeValue);
  2459. SetLabel(trueL);
  2460. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2461. Emit(Mov(position,result.op,true));
  2462. BrL(exit);
  2463. SetLabel(falseL);
  2464. Emit(MovReplace(position,result.op,false));
  2465. SetLabel(exit);
  2466. END ConditionToValue;
  2467. PROCEDURE ValueToCondition(VAR op: Operand);
  2468. BEGIN
  2469. LoadValue(op,system.booleanType);
  2470. BrneL(trueLabel,op.op, false);
  2471. ReleaseOperand(op);
  2472. BrL(falseLabel);
  2473. END ValueToCondition;
  2474. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2475. VAR size: LONGINT;
  2476. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2477. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2478. BEGIN
  2479. ASSERT(type.form = SyntaxTree.Open);
  2480. baseType := type.arrayBase.resolved;
  2481. IF IsOpenArray(baseType) THEN
  2482. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2483. ELSE
  2484. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2485. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2486. END;
  2487. len := tag;
  2488. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2489. IntermediateCode.MakeMemory(len,addressType);
  2490. UseIntermediateOperand(len);
  2491. Reuse2(res,sizeOperand,len);
  2492. Emit(Mul(position,res,sizeOperand,len));
  2493. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2494. RETURN res
  2495. END GetArraySize;
  2496. BEGIN
  2497. type := type.resolved;
  2498. IF IsOpenArray(type) THEN
  2499. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2500. RETURN tag
  2501. ELSE
  2502. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2503. END;
  2504. ELSE
  2505. size := ToMemoryUnits(system,system.SizeOf(type));
  2506. RETURN IntermediateCode.Immediate(addressType,size)
  2507. END;
  2508. END GetDynamicSize;
  2509. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2510. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2511. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2512. BEGIN
  2513. ASSERT(type.form = SyntaxTree.Open);
  2514. baseType := type.arrayBase.resolved;
  2515. IF IsOpenArray(baseType) THEN
  2516. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2517. ELSE
  2518. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2519. END;
  2520. len := tag;
  2521. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2522. IntermediateCode.MakeMemory(len,addressType);
  2523. UseIntermediateOperand(len);
  2524. Reuse2(res,sizeOperand,len);
  2525. Emit(Mul(position,res,sizeOperand,len));
  2526. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2527. RETURN res
  2528. END GetLength;
  2529. BEGIN
  2530. type := type.resolved;
  2531. IF IsOpenArray(type) THEN
  2532. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2533. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2534. ELSE
  2535. RETURN IntermediateCode.Immediate(addressType,1)
  2536. END;
  2537. END GetArrayLength;
  2538. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2539. VAR result: IntermediateCode.Operand;
  2540. BEGIN
  2541. IF backend.cooperative THEN
  2542. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2543. ELSE
  2544. MakeMemory(result, tag, addressType, 0);
  2545. END;
  2546. RETURN result
  2547. END GetSizeFromTag;
  2548. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2549. VAR parameter: SyntaxTree.Parameter;
  2550. BEGIN
  2551. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2552. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2553. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2554. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2555. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2556. END;
  2557. RETURN GetDynamicSize(e.type, tag);
  2558. END GetArrayOfBytesSize;
  2559. (*
  2560. to find imported symbol. not needed ?
  2561. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2562. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2563. BEGIN
  2564. IF AddImport(moduleName,importedModule,FALSE) THEN
  2565. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2566. RETURN symbol
  2567. ELSE
  2568. RETURN NIL
  2569. END
  2570. END SymbolByName;
  2571. *)
  2572. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2573. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2574. BEGIN
  2575. IF AddImport(moduleName,runtimeModule,force) THEN
  2576. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2577. IF procedure = NIL THEN
  2578. s := "Procedure ";
  2579. Strings.Append(s,moduleName);
  2580. Strings.Append(s,".");
  2581. Strings.Append(s,procedureName);
  2582. Strings.Append(s," not present");
  2583. Error(position,s);
  2584. RETURN FALSE
  2585. ELSE
  2586. RETURN TRUE
  2587. END;
  2588. ELSE RETURN FALSE
  2589. END;
  2590. END GetRuntimeProcedure;
  2591. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2592. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2593. s: Basic.MessageString;
  2594. BEGIN
  2595. Basic.InitSegmentedName(name);
  2596. name[0] := Basic.MakeString(moduleName);
  2597. name[1] := Basic.MakeString(typeName);
  2598. name[2] := -1;
  2599. IF AddImport(moduleName,importedModule, FALSE) THEN
  2600. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2601. IF symbol = NIL THEN
  2602. s := "type ";
  2603. Strings.Append(s,moduleName);
  2604. Strings.Append(s,".");
  2605. Strings.Append(s,typeName);
  2606. Strings.Append(s," not present");
  2607. Error(position,s);
  2608. END;
  2609. ELSE symbol := NIL;
  2610. END;
  2611. IF importedModule = moduleScope.ownerModule THEN
  2612. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2613. ELSE
  2614. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2615. END;
  2616. RETURN symbol
  2617. END GetTypeDescriptor;
  2618. (* 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. *)
  2619. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2620. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2621. pooledName: Basic.SegmentedName; size: LONGINT;
  2622. BEGIN
  2623. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2624. StaticCallOperand(result,procedure);
  2625. IF numberParameters < 0 THEN
  2626. size := ProcedureParametersSize(system,procedure);
  2627. ELSE
  2628. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2629. IF size # ProcedureParametersSize(system,procedure) THEN
  2630. Error(position,"runtime call parameter count mismatch");
  2631. END;
  2632. END;
  2633. Emit(Call(position, result.op, size));
  2634. ReleaseOperand(result);
  2635. ELSE (* only static linking possible *)
  2636. Basic.InitSegmentedName(pooledName);
  2637. pooledName[0] := Basic.MakeString(moduleName);
  2638. pooledName[1] := Basic.MakeString(procedureName);
  2639. pooledName[2] := -1;
  2640. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2641. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2642. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2643. END;
  2644. END CallThis;
  2645. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2646. VAR
  2647. left,right: Operand;
  2648. leftSize, rightSize: IntermediateCode.Operand;
  2649. saved: RegisterEntry;
  2650. reg: IntermediateCode.Operand;
  2651. procedureName: SyntaxTree.IdentifierString;
  2652. BEGIN
  2653. procedureName := "CompareString";
  2654. SaveRegisters();ReleaseUsedRegisters(saved);
  2655. Designate(leftExpression,left);
  2656. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2657. Emit(Push(position,leftSize));
  2658. ReleaseIntermediateOperand(leftSize);
  2659. Emit(Push(position,left.op));
  2660. ReleaseOperand(left);
  2661. Designate(rightExpression,right);
  2662. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2663. Emit(Push(position,rightSize));
  2664. ReleaseIntermediateOperand(rightSize);
  2665. Emit(Push(position,right.op));
  2666. ReleaseOperand(right);
  2667. IF backend.cooperative THEN
  2668. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2669. ELSE
  2670. CallThis(position,runtimeModuleName,procedureName, 4);
  2671. END;
  2672. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2673. Emit(Result(position,reg));
  2674. (*
  2675. AcquireThisRegister(int8,IntermediateCode.Result);
  2676. *)
  2677. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2678. (*
  2679. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2680. *)
  2681. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2682. ReleaseIntermediateOperand(reg);
  2683. BrL(falseLabel);
  2684. END CompareString;
  2685. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2686. VAR
  2687. left,right: Operand;
  2688. leftSize, rightSize: IntermediateCode.Operand;
  2689. saved: RegisterEntry;
  2690. procedureName: SyntaxTree.IdentifierString;
  2691. BEGIN
  2692. procedureName := "CopyString";
  2693. SaveRegisters();ReleaseUsedRegisters(saved);
  2694. Designate(leftExpression,left);
  2695. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2696. Emit(Push(position,leftSize));
  2697. ReleaseIntermediateOperand(leftSize);
  2698. Emit(Push(position,left.op));
  2699. ReleaseOperand(left);
  2700. Designate(rightExpression,right);
  2701. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2702. Emit(Push(position,rightSize));
  2703. ReleaseIntermediateOperand(rightSize);
  2704. Emit(Push(position,right.op));
  2705. ReleaseOperand(right);
  2706. IF backend.cooperative THEN
  2707. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2708. ELSE
  2709. CallThis(position,runtimeModuleName,procedureName,4);
  2710. END;
  2711. RestoreRegisters(saved);
  2712. END CopyString;
  2713. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2714. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2715. leftType,rightType: SyntaxTree.Type;
  2716. leftExpression,rightExpression : SyntaxTree.Expression;
  2717. componentType: IntermediateCode.Type;
  2718. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2719. size: LONGINT;
  2720. BEGIN
  2721. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2722. dest := destination; destination := emptyOperand;
  2723. leftType := x.left.type.resolved;
  2724. rightType := x.right.type.resolved;
  2725. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2726. CASE x.operator OF
  2727. Scanner.Or:
  2728. (* shortcut evaluation of left OR right *)
  2729. IF ~conditional THEN ConditionToValue(x);
  2730. ELSE
  2731. next := NewLabel();
  2732. Condition(x.left,trueLabel,next);
  2733. SetLabel(next);
  2734. Condition(x.right,trueLabel,falseLabel);
  2735. END;
  2736. |Scanner.And:
  2737. (* shortcut evaluation of left & right *)
  2738. IF ~conditional THEN ConditionToValue(x);
  2739. ELSE
  2740. next := NewLabel();
  2741. Condition(x.left,next,falseLabel);
  2742. SetLabel(next);
  2743. Condition(x.right,trueLabel,falseLabel);
  2744. END;
  2745. |Scanner.Is:
  2746. IF ~conditional THEN ConditionToValue(x);
  2747. ELSE
  2748. (* get type desc tag *)
  2749. IF IsPointerToRecord(leftType,recordType) THEN
  2750. Evaluate(x.left,left);
  2751. Dereference(left,recordType,IsUnsafePointer(leftType))
  2752. ELSE
  2753. Designate(x.left,left);
  2754. END;
  2755. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2756. ReleaseOperand(left);
  2757. END;
  2758. |Scanner.Plus:
  2759. Evaluate(x.left,left);
  2760. Evaluate(x.right,right);
  2761. IF leftType IS SyntaxTree.SetType THEN
  2762. InitOperand(result,ModeValue);
  2763. Reuse2a(result.op,left.op,right.op,dest);
  2764. Emit(Or(position,result.op,left.op,right.op));
  2765. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2766. InitOperand(result,ModeValue);
  2767. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2768. Reuse2(result.tag,left.tag,right.tag);
  2769. Emit(Add(position,result.op,left.op,right.op));
  2770. Emit(Add(position,result.tag,left.tag,right.tag))
  2771. ELSE
  2772. InitOperand(result,ModeValue);
  2773. Reuse2a(result.op,left.op,right.op,dest);
  2774. Emit(Add(position,result.op,left.op,right.op));
  2775. END;
  2776. ReleaseOperand(left); ReleaseOperand(right);
  2777. |Scanner.Minus:
  2778. Evaluate(x.left,left);
  2779. Evaluate(x.right,right);
  2780. IF leftType IS SyntaxTree.SetType THEN
  2781. InitOperand(result,ModeValue);
  2782. Reuse1(result.op,right.op);
  2783. Emit(Not(position,result.op,right.op));
  2784. ReleaseOperand(right);
  2785. Emit(And(position,result.op,result.op,left.op));
  2786. ReleaseOperand(left);
  2787. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2788. InitOperand(result,ModeValue);
  2789. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2790. Reuse2(result.tag,left.tag,right.tag);
  2791. Emit(Sub(position,result.op,left.op,right.op));
  2792. Emit(Sub(position,result.tag,left.tag,right.tag));
  2793. ReleaseOperand(left); ReleaseOperand(right)
  2794. ELSE
  2795. InitOperand(result,ModeValue);
  2796. Reuse2a(result.op,left.op,right.op,dest);
  2797. Emit(Sub(position,result.op,left.op,right.op));
  2798. ReleaseOperand(left); ReleaseOperand(right);
  2799. END;
  2800. |Scanner.Times:
  2801. Evaluate(x.left,left);
  2802. Evaluate(x.right,right);
  2803. IF leftType IS SyntaxTree.SetType THEN
  2804. InitOperand(result,ModeValue);
  2805. Reuse2a(result.op,left.op,right.op,dest);
  2806. Emit(And(position,result.op,left.op,right.op));
  2807. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2808. InitOperand(result,ModeValue);
  2809. Reuse1a(result.op,left.op,dest);
  2810. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2811. Emit(Shl(position,result.op,left.op,right.op));
  2812. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2813. InitOperand(result, ModeValue);
  2814. componentType := left.op.type;
  2815. (* TODO: review this *)
  2816. (*
  2817. result.op = left.op * right.op - left.tag * right.tag
  2818. result.tag = left.tag * right.op + left.op * right.tag
  2819. *)
  2820. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2821. Emit(Mul(position,result.op, left.op, right.op));
  2822. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2823. Emit(Mul(position,tempReg, left.tag, right.tag));
  2824. Emit(Sub(position,result.op, result.op, tempReg));
  2825. Reuse2(result.tag, left.tag, right.op);
  2826. Emit(Mul(position,result.tag, left.tag, right.op));
  2827. Emit(Mul(position,tempReg, left.op, right.tag));
  2828. Emit(Add(position,result.tag, result.tag, tempReg));
  2829. ReleaseIntermediateOperand(tempReg)
  2830. ELSE
  2831. InitOperand(result,ModeValue);
  2832. Reuse2a(result.op,left.op,right.op,dest);
  2833. Emit(Mul(position,result.op,left.op,right.op));
  2834. END;
  2835. ReleaseOperand(left); ReleaseOperand(right);
  2836. |Scanner.Div:
  2837. Evaluate(x.left,left);
  2838. Evaluate(x.right,right);
  2839. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2840. InitOperand(result,ModeValue);
  2841. Reuse1a(result.op,left.op,dest);
  2842. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2843. Emit(Shr(position,result.op,left.op,right.op));
  2844. ELSE
  2845. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2846. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2847. IF ~isUnchecked THEN
  2848. exit := NewLabel();
  2849. BrltL(exit,zero,right.op);
  2850. EmitTrap(position,NegativeDivisorTrap);
  2851. SetLabel(exit);
  2852. END;
  2853. END;
  2854. InitOperand(result,ModeValue);
  2855. Reuse2a(result.op,left.op,right.op,dest);
  2856. Emit(Div(position,result.op,left.op,right.op));
  2857. END;
  2858. ReleaseOperand(left); ReleaseOperand(right);
  2859. |Scanner.Mod:
  2860. Evaluate(x.left,left);
  2861. Evaluate(x.right,right);
  2862. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2863. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2864. InitOperand(result,ModeValue);
  2865. Reuse1a(result.op,left.op,dest);
  2866. Emit(And(position,result.op,left.op,right.op));
  2867. ELSE
  2868. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2869. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2870. IF ~isUnchecked THEN
  2871. exit := NewLabel();
  2872. BrltL(exit,zero,right.op);
  2873. EmitTrap(position,NegativeDivisorTrap);
  2874. SetLabel(exit);
  2875. END;
  2876. END;
  2877. InitOperand(result,ModeValue);
  2878. Reuse2a(result.op,left.op,right.op,dest);
  2879. Emit(Mod(position,result.op,left.op,right.op));
  2880. END;
  2881. ReleaseOperand(left); ReleaseOperand(right);
  2882. |Scanner.Slash:
  2883. Evaluate(x.left,left);
  2884. Evaluate(x.right,right);
  2885. IF leftType IS SyntaxTree.SetType THEN
  2886. InitOperand(result,ModeValue);
  2887. Reuse2a(result.op,left.op,right.op,dest);
  2888. Emit(Xor(position,result.op,left.op,right.op));
  2889. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2890. InitOperand(result,ModeValue);
  2891. componentType := left.op.type;
  2892. (* review this *)
  2893. (*
  2894. divisor = right.op * right.op + right.tag * right.tag
  2895. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2896. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2897. *)
  2898. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2899. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2900. Emit(Mul(position,tempReg, right.op, right.op));
  2901. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2902. Emit(Add(position,tempReg, tempReg, tempReg2));
  2903. result.op := tempReg2;
  2904. Emit(Mul(position,result.op, left.op, right.op));
  2905. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2906. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2907. Emit(Add(position,result.op, result.op, tempReg2));
  2908. Emit(Div(position,result.op, result.op, tempReg));
  2909. Reuse2(result.tag, left.tag, right.op);
  2910. Emit(Mul(position,result.tag, left.tag, right.op));
  2911. Emit(Mul(position,tempReg2, left.op, right.tag));
  2912. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2913. Emit(Div(position,result.tag, result.tag, tempReg));
  2914. ReleaseIntermediateOperand(tempReg);
  2915. ReleaseIntermediateOperand(tempReg2)
  2916. ELSE
  2917. InitOperand(result,ModeValue);
  2918. Reuse2a(result.op,left.op,right.op,dest);
  2919. Emit(Div(position,result.op,left.op,right.op));
  2920. END;
  2921. ReleaseOperand(left); ReleaseOperand(right);
  2922. |Scanner.Equal:
  2923. IF ~conditional THEN ConditionToValue(x);
  2924. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2925. CompareString(BreqL,x.left,x.right);
  2926. ELSE
  2927. Evaluate(x.left,left);
  2928. Evaluate(x.right,right);
  2929. IF leftType IS SyntaxTree.RangeType THEN
  2930. ASSERT(rightType IS SyntaxTree.RangeType);
  2931. BrneL(falseLabel, left.op, right.op); (* first *)
  2932. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2933. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2934. ReleaseOperand(left); ReleaseOperand(right);
  2935. BrL(trueLabel)
  2936. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2937. BrneL(falseLabel, left.op, right.op); (* first *)
  2938. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2939. ReleaseOperand(left); ReleaseOperand(right);
  2940. BrL(trueLabel)
  2941. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2942. (* TODO: review this *)
  2943. BrneL(falseLabel, left.op, right.op); (* real part *)
  2944. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2945. ReleaseOperand(left); ReleaseOperand(right);
  2946. BrL(trueLabel)
  2947. ELSE
  2948. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2949. ReleaseOperand(left); ReleaseOperand(right);
  2950. BrL(trueLabel);
  2951. END;
  2952. END;
  2953. |Scanner.LessEqual:
  2954. IF ~conditional THEN ConditionToValue(x);
  2955. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2956. CompareString(BrgeL,x.right,x.left);
  2957. ELSE
  2958. Evaluate(x.left,left);
  2959. Evaluate(x.right,right);
  2960. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2961. Reuse1(temp.op,right.op);
  2962. Emit(And(position,temp.op,left.op,right.op));
  2963. ReleaseOperand(right);
  2964. BreqL(trueLabel,temp.op,left.op);
  2965. BrL(falseLabel);
  2966. ReleaseOperand(temp);ReleaseOperand(left);
  2967. ELSE
  2968. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  2969. ReleaseOperand(left); ReleaseOperand(right);
  2970. BrL(trueLabel);
  2971. END;
  2972. END;
  2973. |Scanner.Less:
  2974. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  2975. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  2976. leftExpression.SetType(system.booleanType);
  2977. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  2978. rightExpression.SetType(system.booleanType);
  2979. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  2980. leftExpression.SetType(system.booleanType);
  2981. Expression(leftExpression);
  2982. ELSIF ~conditional THEN ConditionToValue(x);
  2983. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2984. CompareString(BrltL,x.left,x.right);
  2985. ELSE
  2986. Evaluate(x.left,left);
  2987. Evaluate(x.right,right);
  2988. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2989. ReleaseOperand(left); ReleaseOperand(right);
  2990. BrL(trueLabel);
  2991. END;
  2992. |Scanner.Greater:
  2993. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  2994. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  2995. leftExpression.SetType(system.booleanType);
  2996. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  2997. rightExpression.SetType(system.booleanType);
  2998. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  2999. leftExpression.SetType(system.booleanType);
  3000. Expression(leftExpression);
  3001. ELSIF ~conditional THEN ConditionToValue(x);
  3002. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3003. CompareString(BrltL,x.right,x.left);
  3004. ELSE
  3005. Evaluate(x.left,left);
  3006. Evaluate(x.right,right);
  3007. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3008. ReleaseOperand(left); ReleaseOperand(right);
  3009. BrL(trueLabel);
  3010. END;
  3011. |Scanner.GreaterEqual:
  3012. IF ~conditional THEN ConditionToValue(x);
  3013. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3014. CompareString(BrgeL,x.left,x.right);
  3015. ELSE
  3016. Evaluate(x.left,left);
  3017. Evaluate(x.right,right);
  3018. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3019. Reuse1(temp.op,left.op);
  3020. Emit(And(position,temp.op,left.op,right.op));
  3021. ReleaseOperand(left);
  3022. BreqL(trueLabel, temp.op,right.op);
  3023. ReleaseOperand(temp); ReleaseOperand(right);
  3024. BrL(falseLabel);
  3025. ELSE
  3026. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3027. ReleaseOperand(left); ReleaseOperand(right);
  3028. BrL(trueLabel);
  3029. END;
  3030. END;
  3031. |Scanner.Unequal:
  3032. IF ~conditional THEN ConditionToValue(x);
  3033. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3034. CompareString(BrneL,x.left,x.right);
  3035. ELSE
  3036. Evaluate(x.left,left);
  3037. Evaluate(x.right,right);
  3038. IF leftType IS SyntaxTree.RangeType THEN
  3039. ASSERT(rightType IS SyntaxTree.RangeType);
  3040. BrneL(trueLabel, left.op, right.op); (* first *)
  3041. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3042. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3043. ReleaseOperand(left); ReleaseOperand(right);
  3044. BrL(falseLabel)
  3045. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3046. BrneL(trueLabel, left.op, right.op); (* first *)
  3047. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3048. ReleaseOperand(left); ReleaseOperand(right);
  3049. BrL(falseLabel)
  3050. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3051. (* TODO: review this *)
  3052. BrneL(trueLabel, left.op, right.op); (* real part *)
  3053. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3054. ReleaseOperand(left); ReleaseOperand(right);
  3055. BrL(falseLabel)
  3056. ELSE
  3057. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3058. ReleaseOperand(left); ReleaseOperand(right);
  3059. BrL(trueLabel);
  3060. END;
  3061. END;
  3062. |Scanner.In:
  3063. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3064. Evaluate(x.left,left);
  3065. Evaluate(x.right,right);
  3066. Convert(left.op,setType);
  3067. ReuseCopy(temp.op,right.op);
  3068. Emit(Shr(position,temp.op,temp.op,left.op));
  3069. ReleaseOperand(right); ReleaseOperand(left);
  3070. IntermediateCode.InitImmediate(one,setType,1);
  3071. Emit(And(position,temp.op,temp.op,one));
  3072. IF conditional THEN
  3073. IntermediateCode.InitImmediate(zero,setType,0);
  3074. BrneL(trueLabel,temp.op,zero);
  3075. ReleaseOperand(temp);
  3076. BrL(falseLabel);
  3077. ELSE
  3078. Convert(temp.op,bool);
  3079. result.mode := ModeValue;
  3080. result.op := temp.op;
  3081. result.tag := nil; (* may be left over from calls to evaluate *)
  3082. END;
  3083. ELSE
  3084. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3085. IF x.operator = Scanner.Questionmarks THEN
  3086. leftExpression := x.left;
  3087. rightExpression := x.right;
  3088. ELSE
  3089. leftExpression := x.right;
  3090. rightExpression := x.left;
  3091. END;
  3092. Evaluate(leftExpression, left);
  3093. Emit(Push(position,left.op));
  3094. ReleaseOperand(left);
  3095. Designate(rightExpression, right);
  3096. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3097. IF ~backend.cellsAreObjects THEN
  3098. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3099. END;
  3100. Emit(Push(position,right.op));
  3101. ReleaseOperand(right);
  3102. IF backend.cellsAreObjects THEN
  3103. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3104. ELSE
  3105. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3106. END;
  3107. InitOperand(result, ModeValue);
  3108. result.op := NewRegisterOperand(bool);
  3109. Emit(Result(position,result.op));
  3110. IF conditional THEN
  3111. IntermediateCode.InitImmediate(zero,setType,0);
  3112. BrneL(trueLabel,result.op,zero);
  3113. ReleaseOperand(result);
  3114. BrL(falseLabel);
  3115. END;
  3116. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3117. leftExpression := x.left;
  3118. rightExpression := x.right;
  3119. Evaluate(leftExpression, left);
  3120. Emit(Push(position,left.op));
  3121. ReleaseOperand(left);
  3122. Evaluate(rightExpression, right);
  3123. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3124. IF ~backend.cellsAreObjects THEN
  3125. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3126. END;
  3127. Emit(Push(position,right.op));
  3128. ReleaseOperand(right);
  3129. IF backend.cellsAreObjects THEN
  3130. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3131. ELSE
  3132. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3133. END;
  3134. InitOperand(result, ModeValue);
  3135. result.op := NewRegisterOperand(bool);
  3136. Emit(Result(position,result.op));
  3137. IF conditional THEN
  3138. IntermediateCode.InitImmediate(zero,setType,0);
  3139. BrneL(trueLabel,result.op,zero);
  3140. ReleaseOperand(result);
  3141. BrL(falseLabel);
  3142. END;
  3143. ELSE
  3144. HALT(100);
  3145. END;
  3146. END;
  3147. destination := dest;
  3148. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3149. END VisitBinaryExpression;
  3150. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3151. VAR localResult, operand: Operand;
  3152. BEGIN
  3153. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3154. InitOperand(localResult, ModeValue);
  3155. ASSERT(x.first # NIL);
  3156. Evaluate(x.first, operand);
  3157. localResult.op := operand.op;
  3158. ReleaseOperand(operand);
  3159. UseIntermediateOperand(localResult.op);
  3160. ASSERT(x.last # NIL);
  3161. Evaluate(x.last, operand);
  3162. localResult.tag := operand.op;
  3163. ReleaseOperand(operand);
  3164. UseIntermediateOperand(localResult.tag);
  3165. IF x.step # NIL THEN
  3166. Evaluate(x.step, operand);
  3167. localResult.extra := operand.op;
  3168. ReleaseOperand(operand);
  3169. UseIntermediateOperand(localResult.extra);
  3170. END;
  3171. result := localResult;
  3172. IF Trace THEN TraceExit("VisitRangeExpression") END
  3173. END VisitRangeExpression;
  3174. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3175. BEGIN
  3176. HALT(100); (* should never be evaluated *)
  3177. END VisitTensorRangeExpression;
  3178. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3179. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3180. BEGIN
  3181. IF Trace THEN TraceEnter("VisitConversion") END;
  3182. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3183. dest := destination; destination := emptyOperand;
  3184. Evaluate(x.expression,old);
  3185. InitOperand(result,ModeValue);
  3186. result.op := old.op;
  3187. ASSERT(result.op.mode # 0);
  3188. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3189. (* convert TO a complex number *)
  3190. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3191. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3192. ASSERT(result.op.mode # 0);
  3193. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3194. (* convert FROM a complex number TO a complex number*)
  3195. result.tag := old.tag;
  3196. ASSERT(result.tag.mode # 0);
  3197. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3198. ASSERT(result.tag.mode # 0)
  3199. ELSE
  3200. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3201. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3202. END
  3203. ELSE
  3204. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3205. ASSERT(result.op.mode # 0);
  3206. result.tag := old.tag; (*! probably never used *)
  3207. END;
  3208. destination := dest;
  3209. IF Trace THEN TraceExit("VisitConversion") END;
  3210. END VisitConversion;
  3211. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3212. BEGIN
  3213. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3214. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3215. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3216. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3217. END VisitTypeDeclaration;
  3218. (** designators (expressions) *)
  3219. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3220. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3221. BEGIN
  3222. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3223. IF x.left # NIL THEN Expression(x.left) END;
  3224. Symbol(x.symbol,result);
  3225. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3226. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3227. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3228. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3229. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3230. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3231. END;
  3232. END;
  3233. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3234. END VisitSymbolDesignator;
  3235. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3236. BEGIN
  3237. IF isUnchecked THEN RETURN END;
  3238. IF tagsAvailable THEN
  3239. TrapC(BrltL,index,length,IndexCheckTrap);
  3240. END;
  3241. END BoundCheck;
  3242. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3243. VAR d: IntermediateCode.Operand;
  3244. BEGIN
  3245. IF isUnchecked THEN RETURN END;
  3246. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3247. TrapC(op,dim,d,ArraySizeTrap);
  3248. ReleaseIntermediateOperand(d);
  3249. END DimensionCheck;
  3250. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3251. VAR
  3252. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3253. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3254. expression: SyntaxTree.Expression;
  3255. resultingType, leftType, baseType: SyntaxTree.Type;
  3256. skipLabel1: Label;
  3257. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3258. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3259. variableOp: Operand;
  3260. variable: SyntaxTree.Variable;
  3261. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3262. VAR expression: SyntaxTree.Expression;
  3263. BEGIN
  3264. (* count the number of indices, ranges and tensorRanges in the index list *)
  3265. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3266. FOR i := 0 TO parameters.Length() - 1 DO
  3267. expression := parameters.GetExpression(i);
  3268. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3269. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3270. ELSE INC(indexCount)
  3271. END
  3272. END;
  3273. END CountIndices;
  3274. BEGIN
  3275. ASSERT(tagsAvailable);
  3276. resultingType := x.type.resolved; (* resulting type *)
  3277. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3278. InitOperand(localResult, ModeReference);
  3279. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3280. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3281. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3282. (* a globally defined array destination tag is available -> use and invalidate it*)
  3283. localResult.tag := arrayDestinationTag;
  3284. IntermediateCode.InitOperand(arrayDestinationTag)
  3285. ELSE
  3286. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3287. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3288. Symbol(variable, variableOp);
  3289. ReuseCopy(localResult.tag, variableOp.op);
  3290. ReleaseOperand(variableOp);
  3291. END
  3292. END;
  3293. indexListSize := x.parameters.Length();
  3294. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3295. ASSERT(tensorRangeCount <= 1);
  3296. (* designate the array to be indexed over, perform tensor range check if known *)
  3297. Designate(x.left, array);
  3298. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3299. Dereference(array, leftType,FALSE);
  3300. IF tensorRangeCount=0 THEN
  3301. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3302. END
  3303. END;
  3304. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3305. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3306. srcDimOffset := -indexListSize;
  3307. destDimOffset := -rangeCount
  3308. ELSE
  3309. srcDimOffset := 0;
  3310. destDimOffset := 0
  3311. END;
  3312. indexDim := 0;
  3313. (* use address of source array as basis *)
  3314. (*
  3315. ReuseCopy(localResult.op, array.op);
  3316. *)
  3317. localResult.op := array.op;
  3318. UseIntermediateOperand(localResult.op);
  3319. (* go through the index list *)
  3320. FOR i := 0 TO indexListSize - 1 DO
  3321. expression := x.parameters.GetExpression(i);
  3322. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3323. (* nothing to do *)
  3324. ELSE
  3325. (* determine which dimension of source array is currently looked at *)
  3326. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3327. (* get the memory operand pointing to array descriptor dimension *)
  3328. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3329. (* make a reusable register from it *)
  3330. ReuseCopy(srcDim, tmp);
  3331. ReleaseIntermediateOperand(tmp);
  3332. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3333. ELSE
  3334. srcDim := IntermediateCode.Immediate(int32, i)
  3335. END;
  3336. (* get length and increment of source array for current dimension *)
  3337. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3338. Convert(sourceLength.op, sizeType);
  3339. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3340. Convert(sourceIncrement.op, sizeType);
  3341. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3342. ReleaseIntermediateOperand(srcDim);
  3343. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3344. (* SINGLE INDEX *)
  3345. Evaluate(expression, index);
  3346. ReleaseIntermediateOperand(index.tag);
  3347. index.tag := emptyOperand;
  3348. Convert(index.op, sizeType);
  3349. (* lower bound check *)
  3350. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3351. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3352. ELSIF isUnchecked THEN (* do nothing *)
  3353. ELSE
  3354. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3355. END;
  3356. (* upper bound check *)
  3357. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3358. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3359. ELSIF isUnchecked THEN (* do nothing *)
  3360. ELSE
  3361. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3362. END;
  3363. ReleaseOperand(sourceLength);
  3364. Convert(index.op, addressType);
  3365. summand := index.op;
  3366. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3367. (* RANGE OF INDICES *)
  3368. Evaluate(expression, range);
  3369. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3370. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3371. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3372. ReleaseOperand(range);
  3373. Convert(firstIndex, sizeType);
  3374. Convert(lastIndex, sizeType);
  3375. Convert(stepSize, sizeType);
  3376. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3377. if it is 'MAX(LONGINT)' *)
  3378. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3379. TransferToRegister(lastIndex, lastIndex);
  3380. skipLabel1 := NewLabel();
  3381. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3382. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3383. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3384. SetLabel(skipLabel1)
  3385. END;
  3386. (* check if step size is valid *)
  3387. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3388. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3389. ELSIF isUnchecked THEN (* do nothing *)
  3390. ELSE
  3391. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3392. END;
  3393. (* check lower bound check *)
  3394. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3395. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3396. ELSIF isUnchecked THEN (* do nothing *)
  3397. ELSE
  3398. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3399. END;
  3400. (* check upper bound check *)
  3401. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3402. (* statically open range: nothing to do *)
  3403. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3404. ASSERT(staticLastIndex < staticSourceLength)
  3405. ELSIF isUnchecked THEN (* do nothing *)
  3406. ELSE
  3407. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3408. END;
  3409. (* determine length of target array for current dimension *)
  3410. (* 1. incorporate last index: *)
  3411. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3412. (* last index is static *)
  3413. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3414. targetLength := sourceLength.op
  3415. ELSE
  3416. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3417. END;
  3418. UseIntermediateOperand(targetLength);
  3419. ELSE
  3420. (* targetLength := lastIndex + 1
  3421. Reuse1(targetLength, lastIndex);
  3422. *)
  3423. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3424. END;
  3425. ReleaseOperand(sourceLength);
  3426. ReleaseIntermediateOperand(lastIndex);
  3427. (* 2. incorporate first index: *)
  3428. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3429. (* first index and current target length are static *)
  3430. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3431. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3432. (* first index = 0: nothing to do *)
  3433. ELSE
  3434. (* targetLength := targetLength - firstIndex *)
  3435. TransferToRegister(targetLength, targetLength);
  3436. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3437. END;
  3438. (* clip negative lengths to 0 *)
  3439. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3440. IF staticTargetLength < 0 THEN
  3441. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3442. END
  3443. ELSE
  3444. skipLabel1 := NewLabel();
  3445. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3446. TransferToRegister(targetLength, targetLength);
  3447. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3448. SetLabel(skipLabel1)
  3449. END;
  3450. (* 3. incorporate index step size: *)
  3451. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3452. (*step size and current target length are static *)
  3453. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3454. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3455. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3456. (* step size = 1: nothing to do *)
  3457. ELSE
  3458. (* emit code for this:
  3459. targetLength := (targetLength-1) DIV stepSize +1;
  3460. *)
  3461. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3462. DivInt(targetLength, targetLength, stepSize);
  3463. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3464. END;
  3465. (* determine increment of target array for current dimension *)
  3466. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3467. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3468. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3469. (* step size = 1 *)
  3470. targetIncrement := sourceIncrement.op;
  3471. UseIntermediateOperand(targetIncrement)
  3472. ELSE
  3473. (* targetIncrement := sourceIncrement * stepSize *)
  3474. Reuse1(targetIncrement, stepSize);
  3475. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3476. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3477. END;
  3478. ReleaseIntermediateOperand(stepSize);
  3479. (* write length and increment of target array to descriptor *)
  3480. IF destDimOffset < 0 THEN
  3481. (* determine which dimension of target array is currently looked at *)
  3482. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3483. TransferToRegister(destDim, tmp);
  3484. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3485. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3486. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3487. ReleaseIntermediateOperand(destDim)
  3488. ELSE
  3489. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3490. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3491. END;
  3492. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3493. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3494. INC(indexDim);
  3495. Convert(firstIndex, addressType);
  3496. TransferToRegister(summand, firstIndex);
  3497. ELSE HALT(100);
  3498. END;
  3499. (*
  3500. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3501. *)
  3502. Convert(sourceIncrement.op, addressType);
  3503. Convert(summand, addressType);
  3504. MulInt(summand, summand, sourceIncrement.op);
  3505. ReleaseIntermediateOperand(sourceIncrement.op);
  3506. AddInt(localResult.op, localResult.op, summand);
  3507. ReleaseIntermediateOperand(summand);
  3508. END
  3509. END;
  3510. result := localResult;
  3511. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3512. ReleaseIntermediateOperand(result.tag);
  3513. result.tag := TypeDescriptorAdr(resultingType);
  3514. IF ~newObjectFile THEN
  3515. IntermediateCode.MakeMemory(result.tag,addressType);
  3516. END;
  3517. ELSIF IsDelegate(resultingType) THEN
  3518. ReleaseIntermediateOperand(result.tag);
  3519. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3520. UseIntermediateOperand(result.tag);
  3521. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3522. ReleaseIntermediateOperand(result.tag);
  3523. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3524. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3525. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3526. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3527. (* finalize target array descriptor *)
  3528. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3529. (* write lengths and increments of target array for remaining dimensions *)
  3530. i := indexListSize;
  3531. WHILE indexDim < targetArrayDimensionality DO
  3532. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3533. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3534. ReleaseOperand(sourceLength);
  3535. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3536. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3537. ReleaseOperand(sourceIncrement);
  3538. INC(i); INC(indexDim);
  3539. END;
  3540. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3541. tmp := nil;
  3542. ELSE
  3543. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3544. END;
  3545. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3546. ReleaseIntermediateOperand(tmp);
  3547. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3548. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3549. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3550. ELSE
  3551. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3552. END;
  3553. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3554. ReleaseIntermediateOperand(tmp);
  3555. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3556. (* write dimensionality *)
  3557. IF targetArrayDimensionality # 0 THEN
  3558. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3559. END;
  3560. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3561. END;
  3562. ReleaseOperand(array);
  3563. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3564. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3565. END;
  3566. END MathIndexDesignator;
  3567. (* TENTATIVE *)
  3568. PROCEDURE DumpOperand(operand: Operand);
  3569. BEGIN
  3570. D.Log.String(" op = ");
  3571. IntermediateCode.DumpOperand(D.Log, operand.op );
  3572. D.Log.Ln;
  3573. D.Log.String(" tag = ");
  3574. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3575. D.Log.Ln;
  3576. D.Log.String(" extra = ");
  3577. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3578. D.Log.Ln;
  3579. D.Log.Update
  3580. END DumpOperand;
  3581. (* get the length of an array , trying to make use of static information *)
  3582. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3583. VAR res: IntermediateCode.Operand; size: LONGINT;
  3584. BEGIN
  3585. type := type.resolved;
  3586. IF type IS SyntaxTree.ArrayType THEN
  3587. WITH type: SyntaxTree.ArrayType DO
  3588. IF type.form = SyntaxTree.Static THEN
  3589. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3590. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3591. Evaluate(type.length, op);
  3592. ReleaseIntermediateOperand(op.tag);
  3593. RETURN op.op;*)
  3594. ELSE
  3595. res := tag;
  3596. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3597. IntermediateCode.MakeMemory(res,addressType);
  3598. UseIntermediateOperand(res);
  3599. RETURN res
  3600. END
  3601. END;
  3602. ELSE
  3603. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3604. RETURN IntermediateCode.Immediate(addressType,size);
  3605. END;
  3606. END ArrayLength;
  3607. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3608. BEGIN
  3609. ReleaseIntermediateOperand(res);
  3610. IF IsImmediate(x) & IsImmediate(y) THEN
  3611. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3612. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3613. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3614. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3615. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3616. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3617. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3618. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3619. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3620. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3621. UseIntermediateOperand(res);
  3622. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3623. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3624. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3625. UseIntermediateOperand(res);
  3626. ELSE
  3627. IF ~ReusableRegister(res) THEN
  3628. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3629. ELSE
  3630. UseIntermediateOperand(res);
  3631. END;
  3632. IF IsImmediate(x) & (x.intValue = 0) THEN
  3633. Emit(Mov(position,res,y))
  3634. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3635. Emit(Mov(position,res,x))
  3636. ELSE
  3637. Emit(Add(position,res, x, y));
  3638. END;
  3639. END;
  3640. END AddInt;
  3641. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3642. BEGIN
  3643. ReleaseIntermediateOperand(res);
  3644. IF IsImmediate(x) & IsImmediate(y) THEN
  3645. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3646. ELSE
  3647. IF ~ReusableRegister(res) THEN
  3648. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3649. ELSE UseIntermediateOperand(res);
  3650. END;
  3651. IF IsImmediate(x) & (x.intValue = 1) THEN
  3652. Emit(Mov(position,res,y))
  3653. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3654. Emit(Mov(position,res,x))
  3655. ELSE
  3656. Emit(Mul(position,res, x, y));
  3657. END;
  3658. END;
  3659. END MulInt;
  3660. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3661. BEGIN
  3662. ReleaseIntermediateOperand(res);
  3663. IF IsImmediate(x) & IsImmediate(y) THEN
  3664. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3665. ELSE
  3666. IF ~ReusableRegister(res) THEN
  3667. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3668. ELSE UseIntermediateOperand(res);
  3669. END;
  3670. IF IsImmediate(x) & (x.intValue = 1) THEN
  3671. Emit(Mov(position,res,y))
  3672. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3673. Emit(Mov(position,res,x))
  3674. ELSE
  3675. Emit(Div(position,res, x, y));
  3676. END;
  3677. END;
  3678. END DivInt;
  3679. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3680. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3681. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3682. BEGIN
  3683. type := x.left.type.resolved;
  3684. IF type IS SyntaxTree.StringType THEN
  3685. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3686. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3687. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3688. type := atype;
  3689. x.left.SetType(type);
  3690. END;
  3691. IntermediateCode.InitImmediate(res,addressType,0);
  3692. maxDim := x.parameters.Length()-1;
  3693. FOR i := 0 TO maxDim DO
  3694. e := x.parameters.GetExpression(i);
  3695. Evaluate(e,index);
  3696. Convert(index.op,addressType);
  3697. AddInt(res, res, index.op);
  3698. IF i = 0 THEN
  3699. (*
  3700. ReuseCopy(res, index.op);
  3701. *)
  3702. Designate(x.left,array);
  3703. type := x.left.type.resolved;
  3704. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3705. Dereference(array, type, FALSE);
  3706. END;
  3707. (*
  3708. ELSE AddInt(res, res, index.op);
  3709. *)
  3710. END;
  3711. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3712. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3713. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3714. BoundCheck(index.op, length);
  3715. END;
  3716. ReleaseIntermediateOperand(length);
  3717. END;
  3718. ReleaseOperand(index);
  3719. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3720. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3721. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3722. MulInt(res,res,length);
  3723. END;
  3724. ReleaseIntermediateOperand(length);
  3725. END;
  3726. IF (type IS SyntaxTree.ArrayType) THEN
  3727. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3728. size := StaticSize(system, type);
  3729. IF size # 1 THEN
  3730. length := IntermediateCode.Immediate(addressType,size);
  3731. MulInt(res,res,length);
  3732. END;
  3733. ELSE
  3734. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3735. IF size # 1 THEN
  3736. length := IntermediateCode.Immediate(addressType,size);
  3737. MulInt(res,res,length);
  3738. END;
  3739. END;
  3740. END;
  3741. AddInt(res,res,array.op);
  3742. InitOperand(result,ModeReference);
  3743. result.op := res;
  3744. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3745. ReleaseIntermediateOperand(result.tag);
  3746. result.tag := TypeDescriptorAdr(type);
  3747. IF ~newObjectFile THEN
  3748. IntermediateCode.MakeMemory(result.tag,addressType);
  3749. END
  3750. ELSIF IsDelegate(type) THEN
  3751. ReleaseIntermediateOperand(result.tag);
  3752. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3753. UseIntermediateOperand(result.tag);
  3754. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3755. ReleaseIntermediateOperand(result.tag);
  3756. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3757. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3758. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3759. END;
  3760. ReleaseOperand(array);
  3761. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3762. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3763. END;
  3764. END IndexDesignator;
  3765. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3766. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3767. BEGIN
  3768. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3769. dest := destination; destination := emptyOperand;
  3770. type := x.left.type.resolved;
  3771. IF type IS SyntaxTree.MathArrayType THEN
  3772. MathIndexDesignator(x);
  3773. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3774. IndexDesignator(x);
  3775. END;
  3776. destination := dest;
  3777. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3778. END VisitIndexDesignator;
  3779. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3780. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3781. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3782. procedure: SyntaxTree.Procedure;
  3783. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3784. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3785. BEGIN
  3786. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3787. parameters := expression.parameters;
  3788. moduleName := "FoxArrayBase";
  3789. procedureName := "CopyDescriptor";
  3790. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3791. SaveRegisters();ReleaseUsedRegisters(saved);
  3792. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3793. IF procedure = NIL THEN
  3794. s := "procedure ";
  3795. Strings.Append(s,moduleName);
  3796. Strings.Append(s,".");
  3797. Strings.Append(s,procedureName);
  3798. Strings.Append(s," not present");
  3799. Error(position,s);
  3800. ELSE
  3801. (* push address of temporary variable *)
  3802. Symbol(variable,destOperand);
  3803. Emit(Push(position,destOperand.op));
  3804. ReleaseOperand(destOperand);
  3805. (* push src *)
  3806. Evaluate(expression.left,srcOperand);
  3807. (*
  3808. Dereference(srcOperand,expression.type.resolved);
  3809. Emit(Push(position,srcOperand.tag));
  3810. *)
  3811. Emit(Push(position,srcOperand.op));
  3812. ReleaseOperand(srcOperand);
  3813. tensorFound := FALSE;
  3814. FOR i := 0 TO parameters.Length()-1 DO
  3815. e := parameters.GetExpression(i);
  3816. IF e IS SyntaxTree.TensorRangeExpression THEN
  3817. tensorFound := TRUE;
  3818. ELSIF e IS SyntaxTree.RangeExpression THEN
  3819. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3820. ELSE
  3821. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3822. END;
  3823. END;
  3824. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3825. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3826. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3827. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3828. StaticCallOperand(procOp,procedure);
  3829. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3830. ReleaseOperand(procOp);
  3831. END;
  3832. RestoreRegisters(saved);
  3833. END;
  3834. RETURN variable
  3835. END PrepareTensorDescriptor;
  3836. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3837. VAR
  3838. type, descriptorType, baseType: SyntaxTree.Type;
  3839. operand, tmpOperand, variableOp, variable2Op: Operand;
  3840. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3841. variable, variable2: SyntaxTree.Variable;
  3842. dim, i, size: LONGINT;
  3843. (* TODO: needed? *)
  3844. oldArrayDestinationTag: IntermediateCode.Operand;
  3845. oldArrayDestinationDimension: LONGINT;
  3846. position: LONGINT;
  3847. saved: RegisterEntry;
  3848. PROCEDURE Pass(op: IntermediateCode.Operand);
  3849. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3850. BEGIN
  3851. IF numberRegister >= 0 THEN
  3852. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3853. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3854. Emit(Mov(position,parameterRegister, op));
  3855. ELSE
  3856. Emit(Push(position,op))
  3857. END
  3858. END Pass;
  3859. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3860. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3861. BEGIN
  3862. formalType := formalType.resolved; actualType := actualType.resolved;
  3863. IF IsOpenArray(formalType)THEN
  3864. IF actualType IS SyntaxTree.StringType THEN
  3865. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3866. RETURN;
  3867. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3868. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3869. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3870. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3871. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3872. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3873. ELSE
  3874. tmp := baseReg;
  3875. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3876. IntermediateCode.MakeMemory(tmp,addressType);
  3877. Pass((tmp));
  3878. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3879. END;
  3880. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3881. END;
  3882. END PushArrayLens;
  3883. BEGIN
  3884. IF Trace THEN TraceEnter("PushParameter") END;
  3885. position := expression.position;
  3886. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3887. type := expression.type.resolved;
  3888. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3889. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3890. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3891. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3892. );
  3893. (* TODO: needed? *)
  3894. oldArrayDestinationTag := arrayDestinationTag;
  3895. oldArrayDestinationDimension := arrayDestinationDimension;
  3896. IF IsArrayOfSystemByte(parameter.type) THEN
  3897. Designate(expression,operand);
  3898. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3899. ReleaseIntermediateOperand(operand.tag);
  3900. operand.tag := tmp;
  3901. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3902. Pass((operand.tag));
  3903. END;
  3904. Pass((operand.op));
  3905. ELSIF IsOpenArray(parameter.type) THEN
  3906. Designate(expression,operand);
  3907. baseReg := operand.tag;
  3908. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3909. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3910. END;
  3911. Pass((operand.op)); (* address of the array *)
  3912. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3913. (* case 1 *)
  3914. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3915. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3916. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3917. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3918. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3919. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3920. arrayDestinationTag := sp;
  3921. (* case 1b *)
  3922. IF expression IS SyntaxTree.IndexDesignator THEN
  3923. (*
  3924. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3925. descriptorType := GetMathArrayDescriptorType(dim);
  3926. variable := GetTemporaryVariable(descriptorType,expression.position);
  3927. Symbol(variable,variableOp);
  3928. arrayDestinationTag := variableOp.op;
  3929. *)
  3930. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3931. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3932. arrayDestinationDimension := dim;
  3933. Designate(expression,operand);
  3934. (* case 1a *)
  3935. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3936. Designate(expression,operand);
  3937. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3938. i := 0;
  3939. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3940. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3941. INC(i);
  3942. END;
  3943. type := expression.type.resolved;
  3944. WHILE (i<dim) DO (* remaining static dimensions *)
  3945. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3946. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3947. ReleaseOperand(tmpOperand);
  3948. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3949. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3950. ReleaseOperand(tmpOperand);
  3951. INC(i);
  3952. END;
  3953. dimOp := IntermediateCode.Immediate(addressType,dim);
  3954. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3955. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3956. (* case 1d *)
  3957. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3958. Designate(expression,operand);
  3959. Dereference(operand,type.resolved,FALSE);
  3960. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3961. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3962. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3963. (* case 1f *)
  3964. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3965. Designate(expression,operand);
  3966. FOR i := 0 TO dim-1 DO
  3967. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3968. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3969. ReleaseOperand(tmpOperand);
  3970. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3971. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3972. ReleaseOperand(tmpOperand);
  3973. END;
  3974. dimOp := IntermediateCode.Immediate(addressType,dim);
  3975. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3976. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  3977. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  3978. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  3979. baseType := SemanticChecker.ArrayBase(type,dim);
  3980. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3981. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  3982. ELSE HALT(100);
  3983. END;
  3984. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  3985. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3986. (* case 2b *)
  3987. IF expression IS SyntaxTree.IndexDesignator THEN
  3988. descriptorType := GetMathArrayDescriptorType(dim);
  3989. variable := GetTemporaryVariable(descriptorType, FALSE);
  3990. Symbol(variable,variableOp);
  3991. arrayDestinationTag := variableOp.op;
  3992. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3993. arrayDestinationDimension := dim;
  3994. NeedDescriptor := TRUE;
  3995. Designate(expression,operand);
  3996. Pass((operand.tag));
  3997. NeedDescriptor := FALSE;
  3998. (* case 2a *)
  3999. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4000. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4001. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4002. INC(i);
  4003. END;
  4004. IF i = dim THEN
  4005. Designate(expression,operand);
  4006. Pass((operand.tag));
  4007. ELSE (* open-static *)
  4008. type := expression.type.resolved;
  4009. descriptorType := GetMathArrayDescriptorType(dim);
  4010. variable := GetTemporaryVariable(descriptorType, FALSE);
  4011. Symbol(variable,variableOp);
  4012. arrayDestinationTag := variableOp.op;
  4013. Designate(expression,operand);
  4014. FOR i := 0 TO dim-1 DO
  4015. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4016. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4017. ReleaseOperand(tmpOperand);
  4018. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4019. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4020. ReleaseOperand(tmpOperand);
  4021. END;
  4022. dimOp := IntermediateCode.Immediate(addressType,dim);
  4023. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4024. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4025. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4026. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4027. baseType := SemanticChecker.ArrayBase(type,dim);
  4028. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4029. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4030. Pass((arrayDestinationTag));
  4031. END;
  4032. (* case 2d *)
  4033. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4034. Designate(expression,operand);
  4035. Dereference(operand,type.resolved,FALSE);
  4036. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4037. Pass((operand.tag));
  4038. (* case 2f *)
  4039. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4040. descriptorType := GetMathArrayDescriptorType(dim);
  4041. variable := GetTemporaryVariable(descriptorType, FALSE);
  4042. Symbol(variable,variableOp);
  4043. arrayDestinationTag := variableOp.op;
  4044. Designate(expression,operand);
  4045. FOR i := 0 TO dim-1 DO
  4046. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4047. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4048. ReleaseOperand(tmpOperand);
  4049. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4050. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4051. ReleaseOperand(tmpOperand);
  4052. END;
  4053. dimOp := IntermediateCode.Immediate(addressType,dim);
  4054. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4055. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4056. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4057. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4058. baseType := SemanticChecker.ArrayBase(type,dim);
  4059. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4060. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4061. Pass((arrayDestinationTag));
  4062. ELSE HALT(100);
  4063. END;
  4064. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4065. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4066. (* case 3b *)
  4067. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4068. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4069. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4070. Symbol(variable,variableOp);
  4071. LoadValue(variableOp,system.addressType);
  4072. ELSE
  4073. descriptorType := GetMathArrayDescriptorType(dim);
  4074. variable := GetTemporaryVariable(descriptorType, FALSE);
  4075. Symbol(variable,variableOp);
  4076. END;
  4077. arrayDestinationTag := variableOp.op;
  4078. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4079. arrayDestinationDimension := 0;
  4080. Designate(expression,operand);
  4081. Pass((operand.tag));
  4082. (* case 3a *)
  4083. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4084. i := 0;
  4085. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4086. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4087. INC(i);
  4088. END;
  4089. IF i = dim THEN
  4090. Designate(expression,operand);
  4091. Pass((operand.tag));
  4092. ELSE (* open-static *)
  4093. type := expression.type.resolved;
  4094. descriptorType := GetMathArrayDescriptorType(dim);
  4095. variable := GetTemporaryVariable(descriptorType, FALSE);
  4096. Symbol(variable,variableOp);
  4097. arrayDestinationTag := variableOp.op;
  4098. Designate(expression,operand);
  4099. FOR i := 0 TO dim-1 DO
  4100. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4101. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4102. ReleaseOperand(tmpOperand);
  4103. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4104. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4105. ReleaseOperand(tmpOperand);
  4106. END;
  4107. dimOp := IntermediateCode.Immediate(addressType,dim);
  4108. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4109. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4110. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4111. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4112. baseType := SemanticChecker.ArrayBase(type,dim);
  4113. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4114. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4115. Pass((arrayDestinationTag));
  4116. END;
  4117. (* case 3d *)
  4118. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4119. Designate(expression,operand);
  4120. Dereference(operand,type.resolved,FALSE);
  4121. (*
  4122. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4123. *)
  4124. Pass((operand.tag));
  4125. (* case 3f *)
  4126. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4127. descriptorType := GetMathArrayDescriptorType(dim);
  4128. variable := GetTemporaryVariable(descriptorType, FALSE);
  4129. Symbol(variable,variableOp);
  4130. arrayDestinationTag := variableOp.op;
  4131. Designate(expression,operand);
  4132. IF operand.op.type.length >1 THEN (* vector register *)
  4133. variable2 := GetTemporaryVariable(type, FALSE);
  4134. Symbol(variable2, variable2Op);
  4135. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4136. Emit(Mov(position,tmp, operand.op));
  4137. ReleaseOperand(operand);
  4138. Symbol(variable2, operand);
  4139. END;
  4140. FOR i := 0 TO dim-1 DO
  4141. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4142. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4143. ReleaseOperand(tmpOperand);
  4144. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4145. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4146. ReleaseOperand(tmpOperand);
  4147. END;
  4148. dimOp := IntermediateCode.Immediate(addressType,dim);
  4149. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4150. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4151. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4152. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4153. baseType := SemanticChecker.ArrayBase(type,dim);
  4154. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4155. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4156. Pass((arrayDestinationTag));
  4157. ELSE HALT(100);
  4158. END;
  4159. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4160. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4161. (* case 4b *)
  4162. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4163. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4164. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4165. Symbol(variable,variableOp);
  4166. LoadValue(variableOp,system.addressType);
  4167. ELSE
  4168. descriptorType := GetMathArrayDescriptorType(dim);
  4169. variable := GetTemporaryVariable(descriptorType, FALSE);
  4170. Symbol(variable,variableOp);
  4171. END;
  4172. arrayDestinationTag := variableOp.op;
  4173. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4174. arrayDestinationDimension := 0;
  4175. Designate(expression,operand);
  4176. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4177. Symbol(variable,variableOp);
  4178. ELSE
  4179. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4180. Symbol(variable,variableOp);
  4181. MakeMemory(tmp,variableOp.op,addressType,0);
  4182. Emit(Mov(position,tmp,operand.tag));
  4183. ReleaseIntermediateOperand(tmp);
  4184. END;
  4185. Pass((variableOp.op));
  4186. ReleaseOperand(variableOp);
  4187. (* case 4a *)
  4188. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4189. i := 0;
  4190. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4191. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4192. INC(i);
  4193. END;
  4194. IF i = dim THEN
  4195. Designate(expression,operand);
  4196. arrayDestinationTag := operand.tag;
  4197. ELSE (* open-static *)
  4198. type := expression.type.resolved;
  4199. descriptorType := GetMathArrayDescriptorType(dim);
  4200. variable := GetTemporaryVariable(descriptorType, FALSE);
  4201. Symbol(variable,variableOp);
  4202. arrayDestinationTag := variableOp.op;
  4203. Designate(expression,operand);
  4204. FOR i := 0 TO dim-1 DO
  4205. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4206. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4207. ReleaseOperand(tmpOperand);
  4208. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4209. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4210. ReleaseOperand(tmpOperand);
  4211. END;
  4212. dimOp := IntermediateCode.Immediate(addressType,dim);
  4213. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4214. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4215. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4216. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4217. baseType := SemanticChecker.ArrayBase(type,dim);
  4218. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4219. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4220. END;
  4221. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4222. Symbol(variable,variableOp);
  4223. MakeMemory(tmp,variableOp.op,addressType,0);
  4224. Emit(Mov(position,tmp,arrayDestinationTag));
  4225. ReleaseIntermediateOperand(tmp);
  4226. Pass((variableOp.op));
  4227. ReleaseOperand(variableOp);
  4228. (* case 4d *)
  4229. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4230. Designate(expression,operand);
  4231. (*
  4232. Dereference(operand,type.resolved,FALSE);
  4233. *)
  4234. (*
  4235. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4236. *)
  4237. Pass((operand.op));
  4238. (* case 4f *)
  4239. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4240. descriptorType := GetMathArrayDescriptorType(dim);
  4241. variable := GetTemporaryVariable(descriptorType, FALSE);
  4242. Symbol(variable,variableOp);
  4243. arrayDestinationTag := variableOp.op;
  4244. Designate(expression,operand);
  4245. FOR i := 0 TO dim-1 DO
  4246. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4247. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4248. ReleaseOperand(tmpOperand);
  4249. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4250. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4251. ReleaseOperand(tmpOperand);
  4252. END;
  4253. dimOp := IntermediateCode.Immediate(addressType,dim);
  4254. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4255. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4256. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4257. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4258. baseType := SemanticChecker.ArrayBase(type,dim);
  4259. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4260. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4261. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4262. Symbol(variable,variableOp);
  4263. MakeMemory(tmp,variableOp.op,addressType,0);
  4264. Emit(Mov(position,tmp,arrayDestinationTag));
  4265. ReleaseIntermediateOperand(tmp);
  4266. Pass((variableOp.op));
  4267. ReleaseOperand(variableOp);
  4268. ELSE HALT(100);
  4269. END;
  4270. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4271. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4272. Designate(expression,operand);
  4273. IF operand.op.type.length > 1 THEN
  4274. Emit(Push(position, operand.op));
  4275. ReleaseOperand(operand);
  4276. ELSE
  4277. size := system.SizeOf(type);
  4278. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4279. size := ToMemoryUnits(system,size);
  4280. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4281. arrayDestinationTag := sp;
  4282. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4283. END;
  4284. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4285. Designate(expression,operand);
  4286. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4287. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4288. Symbol(variable,variableOp);
  4289. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4290. Emit(Mov(position,tmp,operand.op));
  4291. Emit(Push(position,variableOp.op));
  4292. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4293. Pass((operand.op));
  4294. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4295. END;
  4296. ELSE HALT(200)
  4297. END;
  4298. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4299. IF parameter.kind = SyntaxTree.VarParameter THEN
  4300. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4301. Designate(expression, operand);
  4302. Pass((operand.op));
  4303. ELSE
  4304. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4305. Evaluate(expression, operand);
  4306. Pass((operand.extra)); (* step *)
  4307. Pass((operand.tag)); (* last *)
  4308. Pass((operand.op)) (* first *)
  4309. END
  4310. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4311. IF parameter.kind = SyntaxTree.VarParameter THEN
  4312. Designate(expression, operand);
  4313. Pass((operand.op));
  4314. ELSE
  4315. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4316. Evaluate(expression, operand);
  4317. Pass((operand.tag)); (* real part *)
  4318. Pass((operand.op)) (* imaginary part *)
  4319. END
  4320. ELSE
  4321. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4322. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4323. Designate(expression,operand);
  4324. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4325. (* stack allocation *)
  4326. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4327. (*! parameter alignment to be discussed ... *)
  4328. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4329. size := type(SyntaxTree.StringType).length;
  4330. END;
  4331. IF backend.cooperative & parameter.NeedsTrace() THEN
  4332. dst := NewRegisterOperand (addressType);
  4333. Emit(Mov(position,dst, sp));
  4334. IntermediateCode.InitImmediate(null, byteType, 0);
  4335. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4336. ReleaseIntermediateOperand(dst);
  4337. SaveRegisters();ReleaseUsedRegisters(saved);
  4338. (* register dst has been freed before SaveRegisters already *)
  4339. CallAssignMethod(dst, operand.op, parameter.type);
  4340. RestoreRegisters(saved);
  4341. END;
  4342. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4343. ELSIF IsOpenArray(parameter.type) THEN
  4344. Designate(expression,operand);
  4345. baseReg := operand.tag;
  4346. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4347. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4348. END;
  4349. Pass((operand.op)); (* address of the array *)
  4350. ELSIF IsDelegate(parameter.type) THEN
  4351. Evaluate(expression,operand);
  4352. IF backend.cooperative & parameter.NeedsTrace() THEN
  4353. Emit(Push(position, nil));
  4354. dst := NewRegisterOperand (addressType);
  4355. Emit(Mov(position,dst, sp));
  4356. ReleaseIntermediateOperand(dst);
  4357. SaveRegisters();ReleaseUsedRegisters(saved);
  4358. Emit(Push(position, dst));
  4359. (* register dst has been freed before SaveRegisters already *)
  4360. Emit(Push(position, operand.tag));
  4361. CallThis(position,"GarbageCollector","Assign",2);
  4362. RestoreRegisters(saved);
  4363. ELSE
  4364. Pass((operand.tag));
  4365. END;
  4366. Pass((operand.op));
  4367. ELSE
  4368. Evaluate(expression,operand);
  4369. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4370. Emit(Push(position, nil));
  4371. dst := NewRegisterOperand (addressType);
  4372. Emit(Mov(position,dst, sp));
  4373. ReleaseIntermediateOperand(dst);
  4374. SaveRegisters();ReleaseUsedRegisters(saved);
  4375. Emit(Push(position, dst));
  4376. (* register dst has been freed before SaveRegisters already *)
  4377. Emit(Push(position, operand.op));
  4378. CallThis(position,"GarbageCollector","Assign",2);
  4379. RestoreRegisters(saved);
  4380. ELSE
  4381. Pass((operand.op));
  4382. END;
  4383. END;
  4384. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4385. Evaluate(expression,operand);
  4386. Pass((operand.op));
  4387. ELSE (* var parameter *)
  4388. Designate(expression,operand);
  4389. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4390. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4391. Pass((operand.tag));
  4392. END;
  4393. END;
  4394. Pass((operand.op));
  4395. END;
  4396. END;
  4397. (* TODO: needed? *)
  4398. arrayDestinationTag := oldArrayDestinationTag;
  4399. arrayDestinationDimension := oldArrayDestinationDimension;
  4400. IF needsParameterBackup THEN
  4401. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4402. ReuseCopy(parameterBackup, operand.op)
  4403. END;
  4404. ReleaseOperand(operand);
  4405. IF Trace THEN TraceExit("PushParameter") END;
  4406. END PushParameter;
  4407. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4408. VAR prevConditional: BOOLEAN;
  4409. BEGIN
  4410. prevConditional := conditional;
  4411. conditional := FALSE;
  4412. 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
  4413. Expression(x.result); (* use register *)
  4414. END;
  4415. Statement(x.statement);
  4416. conditional := prevConditional;
  4417. IF x.result # NIL THEN Expression(x.result) END;
  4418. 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
  4419. ReleaseIntermediateOperand(result.op);
  4420. END;
  4421. END VisitStatementDesignator;
  4422. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4423. VAR
  4424. procedure: SyntaxTree.Procedure;
  4425. procedureType: SyntaxTree.ProcedureType;
  4426. wasInline: BOOLEAN;
  4427. actualParameters: SyntaxTree.ExpressionList;
  4428. formalParameter: SyntaxTree.Parameter;
  4429. actualParameter: SyntaxTree.Expression;
  4430. i: LONGINT;
  4431. localVariable: SyntaxTree.Variable;
  4432. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4433. src, dest: Operand;
  4434. prevInlineExit : Label;
  4435. prevMapper: SymbolMapper;
  4436. tooComplex: BOOLEAN;
  4437. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4438. BEGIN
  4439. IF e = NIL THEN RETURN TRUE
  4440. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4441. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4442. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4443. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4444. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4445. ELSE RETURN FALSE
  4446. END;
  4447. END SimpleExpression;
  4448. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4449. BEGIN
  4450. RETURN checker.CanPassInRegister(type)
  4451. END FitsInRegister;
  4452. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4453. VAR
  4454. variable: SyntaxTree.Variable;
  4455. variableDesignator: SyntaxTree.Designator;
  4456. BEGIN
  4457. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4458. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4459. variableDesignator.SetType(type);
  4460. RETURN variableDesignator
  4461. END GetTemp;
  4462. BEGIN
  4463. wasInline := currentIsInline;
  4464. prevInlineExit := currentInlineExit;
  4465. prevMapper := currentMapper;
  4466. currentInlineExit := NewLabel();
  4467. tooComplex := FALSE;
  4468. NEW(currentMapper);
  4469. currentIsInline := TRUE;
  4470. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4471. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4472. formalParameter := procedureType.firstParameter;
  4473. actualParameters := x.parameters;
  4474. i := 0;
  4475. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4476. actualParameter := actualParameters.GetExpression(i);
  4477. IF actualParameter.resolved # NIL THEN
  4478. actualParameter := actualParameter.resolved
  4479. END;
  4480. (*
  4481. if expression is simple and can be passed immediately
  4482. or if type fits in register then we can proceed
  4483. otherwise we escape to ordinary procedure call.
  4484. *)
  4485. (* cases where the expression can be mapped identically *)
  4486. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4487. currentMapper.Add(formalParameter, actualParameter, NIL);
  4488. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4489. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4490. (*
  4491. Assign(variableDesignator, actualParameter);
  4492. *)
  4493. Evaluate(actualParameter, src);
  4494. Designate(variableDesignator, dest);
  4495. Emit(Mov(x.position, dest.op, src.op));
  4496. ReleaseOperand(dest);
  4497. ReleaseOperand(src);
  4498. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4499. ELSE tooComplex := TRUE
  4500. END;
  4501. (*
  4502. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4503. currentMapper.Add(formalParameter, actualParameter, NIL);
  4504. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4505. variableDesignator := GetTemp(system.addressType, FALSE);
  4506. Designate(actualParameter, src);
  4507. Designate(variableDesignator, dest);
  4508. IntermediateCode.MakeMemory(dest.op,addressType);
  4509. Emit(Mov(x.position, dest.op, src.op));
  4510. ReleaseOperand(dest);
  4511. IF src.tag.mode # IntermediateCode.Undefined THEN
  4512. tagDesignator := GetTemp(system.addressType, FALSE);
  4513. Designate(tagDesignator, dest);
  4514. IntermediateCode.MakeMemory(dest.op,addressType);
  4515. Emit(Mov(x.position, dest.op, src.op));
  4516. END;
  4517. ReleaseOperand(dest); ReleaseOperand(src);
  4518. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4519. END;
  4520. *)
  4521. formalParameter := formalParameter.nextParameter;
  4522. INC(i);
  4523. END;
  4524. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4525. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4526. currentMapper.Add(NIL, returnDesignator, NIL);
  4527. END;
  4528. localVariable := procedure.procedureScope.firstVariable;
  4529. WHILE ~tooComplex & (localVariable # NIL) DO
  4530. variableDesignator := GetTemp(localVariable.type, FALSE);
  4531. currentMapper.Add(localVariable, variableDesignator, NIL);
  4532. localVariable := localVariable.nextVariable;
  4533. END;
  4534. IF ~tooComplex THEN
  4535. VisitStatementBlock(procedure.procedureScope.body);
  4536. SetLabel(currentInlineExit);
  4537. IF procedureType.returnType # NIL THEN
  4538. Designate(returnDesignator, result);
  4539. IF conditional THEN
  4540. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4541. ValueToCondition(result)
  4542. END;
  4543. END;
  4544. END;
  4545. currentMapper := prevMapper;
  4546. currentInlineExit := prevInlineExit;
  4547. currentIsInline := wasInline;
  4548. RETURN ~tooComplex
  4549. END InlineProcedureCall;
  4550. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4551. VAR
  4552. parameters: SyntaxTree.ExpressionList;
  4553. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4554. designator: SyntaxTree.Designator;
  4555. procedureType: SyntaxTree.ProcedureType;
  4556. formalParameter: SyntaxTree.Parameter;
  4557. operand, returnValue: Operand;
  4558. reg, size, mask, dest: IntermediateCode.Operand;
  4559. saved: RegisterEntry;
  4560. symbol: SyntaxTree.Symbol;
  4561. variable: SyntaxTree.Variable;
  4562. i, parametersSize, returnTypeSize : LONGINT;
  4563. structuredReturnType: BOOLEAN;
  4564. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4565. tempVariableDesignator: SyntaxTree.Designator;
  4566. gap, alignment: LONGINT; (*fld*)
  4567. (* TODO: remove unnecessary backup variables *)
  4568. oldResult: Operand;
  4569. oldCurrentScope: SyntaxTree.Scope;
  4570. oldArrayDestinationTag: IntermediateCode.Operand;
  4571. oldArrayDestinationDimension: LONGINT;
  4572. oldConstantDeclaration: SyntaxTree.Symbol;
  4573. oldDestination: IntermediateCode.Operand;
  4574. oldCurrentLoop: Label;
  4575. oldConditional: BOOLEAN;
  4576. oldTrueLabel, oldFalseLabel: Label;
  4577. oldLocked: BOOLEAN;
  4578. usedRegisters,oldUsedRegisters: RegisterEntry;
  4579. return: IntermediateCode.Operand;
  4580. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4581. arg: IntermediateCode.Operand;
  4582. dummy: IntermediateCode.Operand;
  4583. recordType: SyntaxTree.RecordType;
  4584. operatorSelectionProcedureOperand: Operand;
  4585. operatorSelectionProcedure: SyntaxTree.Procedure;
  4586. fingerPrint: SyntaxTree.FingerPrint;
  4587. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4588. identifierNumber: LONGINT;
  4589. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4590. procedure: SyntaxTree.Procedure;
  4591. PROCEDURE BackupGlobalState;
  4592. BEGIN
  4593. oldResult := result;
  4594. oldCurrentScope := currentScope;
  4595. oldArrayDestinationTag := arrayDestinationTag;
  4596. oldArrayDestinationDimension := arrayDestinationDimension;
  4597. oldConstantDeclaration := constantDeclaration;
  4598. oldDestination := destination;
  4599. oldCurrentLoop := currentLoop;
  4600. oldConditional := conditional;
  4601. oldTrueLabel := trueLabel;
  4602. oldFalseLabel := falseLabel;
  4603. oldLocked := locked;
  4604. oldUsedRegisters := usedRegisters
  4605. END BackupGlobalState;
  4606. PROCEDURE RestoreGlobalState;
  4607. BEGIN
  4608. result := oldResult;
  4609. currentScope := oldCurrentScope;
  4610. arrayDestinationTag := oldArrayDestinationTag;
  4611. arrayDestinationDimension := oldArrayDestinationDimension;
  4612. constantDeclaration := oldConstantDeclaration;
  4613. destination := oldDestination;
  4614. currentLoop := oldCurrentLoop;
  4615. conditional := oldConditional;
  4616. trueLabel := oldTrueLabel;
  4617. falseLabel := oldFalseLabel;
  4618. locked := oldLocked;
  4619. usedRegisters := oldUsedRegisters
  4620. END RestoreGlobalState;
  4621. (** do preparations before parameter push for array-structured object types (ASOTs):
  4622. if ASOT is passed as VAR parameter:
  4623. - allocate temporary variable of math array type
  4624. - copy contents of ASOT to be passed to temporary variable
  4625. - use temporary variable as the actual parameter instead
  4626. - 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)
  4627. **)
  4628. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4629. VAR
  4630. expression: SyntaxTree.Expression;
  4631. BEGIN
  4632. IF actualParameter IS SyntaxTree.Designator THEN
  4633. designator := actualParameter(SyntaxTree.Designator);
  4634. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4635. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4636. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4637. ASSERT(checker # NIL);
  4638. checker.SetCurrentScope(currentScope);
  4639. (* allocate temporary variable *)
  4640. ASSERT(actualParameter.type # NIL);
  4641. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4642. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4643. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4644. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4645. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4646. ASSERT(tempVariableDesignator.type # NIL);
  4647. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4648. (* copy math array stored in actual parameter to temporary variable *)
  4649. BackupGlobalState;
  4650. AssignMathArray(tempVariableDesignator, actualParameter);
  4651. RestoreGlobalState;
  4652. (* use temporary variable as actual parameter instead of the original one *)
  4653. actualParameter := tempVariableDesignator;
  4654. (* create write-back call and store it in linked list *)
  4655. (* create new list entry *)
  4656. IF firstWriteBackCall = NIL THEN
  4657. NEW(firstWriteBackCall);
  4658. currentWriteBackCall := firstWriteBackCall
  4659. ELSE
  4660. ASSERT(currentWriteBackCall # NIL);
  4661. NEW(currentWriteBackCall.next);
  4662. currentWriteBackCall := currentWriteBackCall.next
  4663. END;
  4664. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4665. ASSERT(expression.type = NIL);
  4666. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4667. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4668. (* prepare writeback for any other "normal" indexer *)
  4669. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4670. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4671. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4672. Assign(tempVariableDesignator, actualParameter);
  4673. actualParameter := tempVariableDesignator;
  4674. IF firstWriteBackCall = NIL THEN
  4675. NEW(firstWriteBackCall);
  4676. currentWriteBackCall := firstWriteBackCall
  4677. ELSE
  4678. ASSERT(currentWriteBackCall # NIL);
  4679. NEW(currentWriteBackCall.next);
  4680. currentWriteBackCall := currentWriteBackCall.next
  4681. END;
  4682. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4683. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4684. END
  4685. END
  4686. END PrepareParameter;
  4687. BEGIN
  4688. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4689. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4690. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4691. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4692. IF InlineProcedureCall(x) THEN
  4693. RETURN
  4694. ELSE
  4695. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4696. END
  4697. END;
  4698. END;
  4699. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4700. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4701. dest := destination; destination := emptyOperand;
  4702. SaveRegisters();ReleaseUsedRegisters(saved);
  4703. parameters := x.parameters;
  4704. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4705. (* an operator is called *)
  4706. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4707. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4708. (* check if a dynamic operator call should be performed *)
  4709. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4710. ELSE
  4711. isCallOfDynamicOperator := FALSE
  4712. END;
  4713. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4714. Emit(Push(position, ap));
  4715. END;
  4716. alignment := procedureType.stackAlignment;
  4717. IF alignment > 1 THEN
  4718. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4719. Emit(Mov(position,reg, sp));
  4720. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4721. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4722. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4723. Emit(And(position,sp, sp, mask));
  4724. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4725. Emit(Push(position,reg));
  4726. (*
  4727. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4728. Emit(Mov(position,mem,reg));
  4729. *)
  4730. ReleaseIntermediateOperand(reg);
  4731. END;
  4732. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4733. IntermediateCode.InitImmediate(mask,addressType,-16);
  4734. Emit(And(position,sp, sp, mask));
  4735. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4736. IF gap # 0 THEN
  4737. IntermediateCode.InitImmediate(size,addressType,gap);
  4738. Emit(Sub(position,sp,sp,size))
  4739. END;
  4740. END;
  4741. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4742. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4743. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4744. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4745. ELSE
  4746. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4747. END;
  4748. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4749. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4750. (* push ID *)
  4751. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4752. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4753. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4754. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4755. formalParameter := procedureType.firstParameter;
  4756. FOR i := 0 TO parameters.Length() - 1 DO
  4757. IF formalParameter.access # SyntaxTree.Hidden THEN
  4758. ASSERT(i < 2);
  4759. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4760. (* push pointer *)
  4761. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4762. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4763. (* add dereference *)
  4764. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4765. ReleaseIntermediateOperand(parameterBackups[i]);
  4766. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4767. END;
  4768. Emit(Push(position,parameterBackups[i]));
  4769. ReleaseIntermediateOperand(parameterBackups[i]);
  4770. (* push typetag *)
  4771. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4772. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4773. arg := TypeDescriptorAdr(recordType);
  4774. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4775. Emit(Push(position,arg));
  4776. ELSE
  4777. (* push 'NonPointer' *)
  4778. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4779. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4780. (* push fingerprint *)
  4781. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4782. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4783. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4784. END
  4785. END;
  4786. formalParameter := formalParameter.nextParameter
  4787. END;
  4788. (* for unary operators: complete the information for the second parameter *)
  4789. IF procedureType.numberParameters < 2 THEN
  4790. (* push 'NonPointer' *)
  4791. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4792. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4793. (* push 'NoType' *)
  4794. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4795. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4796. END;
  4797. (* call operator selection procedure *)
  4798. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4799. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4800. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4801. ReleaseOperand(operatorSelectionProcedureOperand);
  4802. (* use the address that the operator selection procedure returned as the target address of the call *)
  4803. InitOperand(operand, ModeValue);
  4804. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4805. Emit(Result(position,operand.op))
  4806. END
  4807. ELSE
  4808. Evaluate(x.left, operand)
  4809. END;
  4810. IF symbol IS SyntaxTree.Procedure THEN
  4811. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4812. Emit(Push(position,operand.tag));
  4813. ELSIF (procedureType.isDelegate) THEN
  4814. Emit(Push(position,operand.tag));
  4815. END;
  4816. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4817. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4818. Emit(Push(position,operand.tag));
  4819. END;
  4820. ELSE HALT(200);
  4821. END;
  4822. ReleaseIntermediateOperand(operand.tag);
  4823. operand.tag := emptyOperand;
  4824. (* determine if a structured return type is needed *)
  4825. structuredReturnType := StructuredReturnType(procedureType);
  4826. IF structuredReturnType THEN
  4827. IF resultDesignator # NIL THEN
  4828. d := resultDesignator;
  4829. ELSE
  4830. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4831. variable.SetUntraced(procedureType.hasUntracedReturn);
  4832. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4833. d.SetType(variable.type);
  4834. END;
  4835. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4836. Designate(d,returnValue);
  4837. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4838. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4839. Emit(Push(position,size));
  4840. Emit(Push(position,returnValue.op));
  4841. ReleaseOperand(returnValue);
  4842. ELSE
  4843. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4844. END;
  4845. END;
  4846. firstWriteBackCall := NIL; (* reset write-back call list *)
  4847. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4848. passByRegister := system.registerParameters > 0;
  4849. registerNumber := 0;
  4850. formalParameter := procedureType.lastParameter;
  4851. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4852. actualParameter := parameters.GetExpression(i);
  4853. PrepareParameter(actualParameter, formalParameter);
  4854. IF passByRegister & (i < system.registerParameters) THEN
  4855. IF ~PassInRegister(formalParameter) THEN
  4856. Error(actualParameter.position,"cannot be passed by register")
  4857. ELSE
  4858. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4859. END;
  4860. INC(registerNumber);
  4861. ELSE
  4862. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4863. END;
  4864. formalParameter := formalParameter.prevParameter;
  4865. END;
  4866. IF passByRegister & (registerNumber > 0) THEN
  4867. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4868. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4869. END;
  4870. ELSE
  4871. hasDynamicOperands := FALSE;
  4872. formalParameter := procedureType.firstParameter;
  4873. FOR i := 0 TO parameters.Length() - 1 DO
  4874. actualParameter := parameters.GetExpression(i);
  4875. IF formalParameter # NIL THEN (* TENTATIVE *)
  4876. PrepareParameter(actualParameter, formalParameter);
  4877. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4878. ASSERT(i < 2);
  4879. hasDynamicOperands := TRUE;
  4880. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4881. ELSE
  4882. IF passByRegister & (registerNumber > 0) THEN
  4883. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4884. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4885. END;
  4886. passByRegister := FALSE;
  4887. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4888. END;
  4889. formalParameter := formalParameter.nextParameter;
  4890. END;
  4891. END;
  4892. END;
  4893. IF symbol IS SyntaxTree.Procedure THEN
  4894. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4895. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4896. Emit(Push(position,reg));
  4897. ReleaseIntermediateOperand(reg);
  4898. END;
  4899. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4900. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4901. parametersSize := ParametersSize(system,procedureType,FALSE);
  4902. END;
  4903. ReleaseParameterRegisters();
  4904. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4905. Emit(Call(position,operand.op,0));
  4906. ELSE
  4907. Emit(Call(position,operand.op,parametersSize));
  4908. END;
  4909. ReleaseOperand(operand);
  4910. IF procedureType.noReturn THEN
  4911. EmitTrap(position,NoReturnTrap);
  4912. END;
  4913. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4914. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4915. Emit(Result(position,return));
  4916. END;
  4917. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4918. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4919. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4920. END;
  4921. size := IntermediateCode.Immediate(addressType,parametersSize);
  4922. Emit(Add(position,sp,sp,size));
  4923. END;
  4924. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4925. IF structuredReturnType THEN
  4926. (* stack pointer rewinding done by callee
  4927. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4928. Emit(Add(position,sp,sp,size));
  4929. *)
  4930. RestoreRegisters(saved);
  4931. InitOperand(result,ModeReference);
  4932. Symbol(variable,result);
  4933. ELSE
  4934. RestoreRegisters(saved);
  4935. InitOperand(result,ModeValue);
  4936. result.op := return;
  4937. END;
  4938. END;
  4939. IF alignment > 1 THEN
  4940. Emit(Pop(position,sp));
  4941. END;
  4942. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4943. Emit(Pop(position, ap));
  4944. END;
  4945. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4946. ValueToCondition(result);
  4947. END;
  4948. destination := dest;
  4949. (* perform all write-back calls in the list *)
  4950. BackupGlobalState;
  4951. currentWriteBackCall := firstWriteBackCall;
  4952. WHILE currentWriteBackCall # NIL DO
  4953. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4954. currentWriteBackCall := currentWriteBackCall.next
  4955. END;
  4956. RestoreGlobalState;
  4957. (* TENATIVE *)
  4958. (*
  4959. IF isOperatorCall THEN
  4960. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4961. END;
  4962. *)
  4963. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  4964. END VisitProcedureCallDesignator;
  4965. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  4966. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  4967. td: SyntaxTree.Symbol;
  4968. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  4969. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4970. BEGIN
  4971. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4972. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4973. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4974. variable.SetType(system.anyType);
  4975. scope.AddVariable(variable);
  4976. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  4977. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  4978. typeDeclaration.SetDeclaredType(hiddenPointerType);
  4979. typeDeclaration.SetScope(module.module.moduleScope);
  4980. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  4981. hiddenPointerType.SetState(SyntaxTree.Resolved);
  4982. END;
  4983. RETURN hiddenPointerType;
  4984. END GetHiddenPointerType;
  4985. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  4986. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4987. BEGIN
  4988. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4989. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4990. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  4991. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  4992. scope.AddVariable(variable);
  4993. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4994. variable.SetType(system.anyType);
  4995. scope.AddVariable(variable);
  4996. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  4997. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  4998. typeDeclaration.SetDeclaredType(delegatePointerType);
  4999. typeDeclaration.SetScope(module.module.moduleScope);
  5000. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5001. delegatePointerType.SetState(SyntaxTree.Resolved);
  5002. END;
  5003. RETURN delegatePointerType
  5004. END GetDelegateType;
  5005. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5006. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5007. such as in VAR a: RECORD ... END;
  5008. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5009. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5010. *)
  5011. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5012. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5013. BEGIN (* no code emission *)
  5014. source := NIL;
  5015. x := x.resolved;
  5016. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5017. x := GetHiddenPointerType();
  5018. ELSIF IsDelegate(x) THEN
  5019. x := GetDelegateType();
  5020. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5021. ELSE HALT(200);
  5022. END;
  5023. td := x.typeDeclaration;
  5024. IF td = NIL THEN
  5025. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5026. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5027. ASSERT(td # NIL);
  5028. END;
  5029. IF newObjectFile THEN
  5030. GetCodeSectionNameForSymbol(td,name);
  5031. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5032. meta.CheckTypeDeclaration(x);
  5033. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5034. ELSE
  5035. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5036. END;
  5037. IF backend.cooperative THEN
  5038. offset := 0;
  5039. ELSE
  5040. IF x IS SyntaxTree.CellType THEN
  5041. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5042. IF baseRecord = NIL THEN
  5043. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5044. ELSE
  5045. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5046. END;
  5047. ELSE
  5048. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5049. END;
  5050. END;
  5051. ELSE
  5052. offset := 0;
  5053. source := module.allSections.FindBySymbol(td); (*TODO*)
  5054. IF source = NIL THEN
  5055. null := 0;
  5056. GetCodeSectionNameForSymbol(td,name);
  5057. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5058. IntermediateCode.InitImmediate(op,addressType,0);
  5059. source(IntermediateCode.Section).Emit(Data(position,op));
  5060. source.SetReferenced(FALSE)
  5061. ELSE
  5062. name := source.name;
  5063. END;
  5064. END;
  5065. RETURN td
  5066. END GetBackendType;
  5067. BEGIN
  5068. (*td := t.typeDeclaration;*)
  5069. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5070. (*
  5071. IF t IS SyntaxTree.PointerType THEN
  5072. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5073. ELSE
  5074. source := GetBackendType(t);
  5075. END;
  5076. *)
  5077. IF newObjectFile THEN
  5078. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5079. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5080. IntermediateCode.SetOffset(res,offset);
  5081. ELSE
  5082. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5083. END;
  5084. (*
  5085. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5086. make memory should be used when tag is used, not earlier
  5087. *)
  5088. RETURN res
  5089. END TypeDescriptorAdr;
  5090. (*
  5091. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5092. VAR result: IntermediateCode.Operand;
  5093. BEGIN
  5094. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5095. operand.tag := TypeDescriptorAdr(operand.type);
  5096. IntermediateCode.MakeMemory(operand.tag,addressType);
  5097. UseIntermediateOperand(operand.tag);
  5098. END;
  5099. END MakeTypeTag;
  5100. *)
  5101. PROCEDURE ProfilerInit;
  5102. VAR reg: IntermediateCode.Operand;
  5103. BEGIN
  5104. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5105. Emit(Call(position,reg,0));
  5106. END ProfilerInit;
  5107. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5108. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5109. BEGIN
  5110. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5111. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5112. THEN
  5113. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5114. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5115. Emit(Push(position,reg));
  5116. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5117. Emit(Push(position,reg));
  5118. StaticCallOperand(result,procedure);
  5119. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5120. ReleaseOperand(result);
  5121. END;
  5122. END ProfilerEnterExit;
  5123. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5124. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5125. BEGIN
  5126. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5127. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5128. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5129. profileInit.Emit(Push(position,reg));
  5130. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5131. profileInit.Emit(Push(position,reg));
  5132. NEW(string, LEN(name)); COPY(name, string^);
  5133. sv := SyntaxTree.NewStringValue(-1,string);
  5134. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5135. sv.SetType(type);
  5136. Designate(sv,result);
  5137. profileInit.Emit(Push(position,result.tag));
  5138. profileInit.Emit(Push(position,result.op));
  5139. StaticCallOperand(result,procedure);
  5140. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5141. ReleaseOperand(result);
  5142. END;
  5143. END ProfilerAddProcedure;
  5144. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5145. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5146. BEGIN
  5147. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5148. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5149. profileInit.Emit(Push(position,reg));
  5150. profileInitPatchPosition := profileInit.pc;
  5151. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5152. NEW(string, LEN(name)); COPY(name, string^);
  5153. sv := SyntaxTree.NewStringValue(-1,string);
  5154. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5155. sv.SetType(type);
  5156. Designate(sv,result);
  5157. profileInit.Emit(Push(position,result.tag));
  5158. profileInit.Emit(Push(position,result.op));
  5159. StaticCallOperand(result,procedure);
  5160. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5161. ReleaseOperand(result);
  5162. END;
  5163. END ProfilerAddModule;
  5164. PROCEDURE ProfilerPatchInit;
  5165. VAR reg: IntermediateCode.Operand;
  5166. BEGIN
  5167. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5168. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5169. EmitLeave(profileInit,position,0);
  5170. profileInit.Emit(Exit(position,0,0));
  5171. END ProfilerPatchInit;
  5172. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5173. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5174. VAR
  5175. id: LONGINT;
  5176. leftType, rightType: SyntaxTree.Type;
  5177. procedureType: SyntaxTree.ProcedureType;
  5178. runtimeProcedure: SyntaxTree.Procedure;
  5179. runtimeProcedureOperand, operatorOperand: Operand;
  5180. kind: SET;
  5181. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5182. VAR
  5183. arg: IntermediateCode.Operand;
  5184. recordType: SyntaxTree.RecordType;
  5185. fingerPrint: SyntaxTree.FingerPrint;
  5186. BEGIN
  5187. IF type = NIL THEN
  5188. (* no type: push 'NoType' *)
  5189. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5190. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5191. ELSIF IsStrictlyPointerToRecord(type) THEN
  5192. (* pointer to record type: push typetag *)
  5193. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5194. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5195. arg := TypeDescriptorAdr(recordType);
  5196. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5197. ELSE
  5198. (* non-pointer to record type: push fingerprint *)
  5199. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5200. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5201. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5202. END;
  5203. operatorInitializationCodeSection.Emit(Push(position,arg))
  5204. END PushTypeInfo;
  5205. BEGIN
  5206. ASSERT(operatorInitializationCodeSection # NIL);
  5207. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5208. procedureType := operator.type(SyntaxTree.ProcedureType);
  5209. (* determine types *)
  5210. leftType := procedureType.firstParameter.type;
  5211. IF procedureType.numberParameters = 2 THEN
  5212. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5213. rightType := procedureType.firstParameter.nextParameter.type;
  5214. ELSE
  5215. rightType := NIL
  5216. END;
  5217. (* determine operator kind *)
  5218. IF IsStrictlyPointerToRecord(leftType) THEN
  5219. kind := {LhsIsPointer}
  5220. ELSE
  5221. kind := {}
  5222. END;
  5223. IF IsStrictlyPointerToRecord(rightType) THEN
  5224. kind := kind + {RhsIsPointer}
  5225. END;
  5226. IF kind # {} THEN (* TODO: to be removed later on *)
  5227. (* at least one of the types is a pointer to record *)
  5228. (* emit a code that registers this specific operator in the runtime *)
  5229. dump := operatorInitializationCodeSection.comments;
  5230. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5231. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5232. (* push ID *)
  5233. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5234. id := Global.GetSymbol(module.module.case, operator.name);
  5235. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5236. (* push kind *)
  5237. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5238. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5239. (* push type infos *)
  5240. PushTypeInfo(leftType);
  5241. PushTypeInfo(rightType);
  5242. (* push operator address *)
  5243. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5244. StaticCallOperand(operatorOperand, operator);
  5245. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5246. ReleaseOperand(operatorOperand);
  5247. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5248. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5249. ReleaseOperand(runtimeProcedureOperand)
  5250. END
  5251. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5252. END
  5253. END RegisterDynamicOperator;
  5254. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5255. VAR
  5256. traceModule: SyntaxTree.Module;
  5257. procedure: SyntaxTree.Procedure;
  5258. procedureVariable: SyntaxTree.Variable;
  5259. s,msg: Basic.MessageString;
  5260. res: Operand;
  5261. i: LONGINT;
  5262. sv: SyntaxTree.StringValue;
  5263. type: SyntaxTree.Type;
  5264. recordType: SyntaxTree.RecordType;
  5265. printout: Printout.Printer;
  5266. stringWriter: Streams.StringWriter;
  5267. expression: SyntaxTree.Expression;
  5268. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5269. BEGIN
  5270. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5271. IF procedure = NIL THEN
  5272. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5273. END;
  5274. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5275. s := "procedure ";
  5276. Strings.Append(s,backend.traceModuleName);
  5277. Strings.Append(s,".");
  5278. Strings.Append(s,procedureName);
  5279. Strings.Append(s," not present");
  5280. Error(position,s);
  5281. RETURN FALSE
  5282. ELSE
  5283. RETURN TRUE
  5284. END;
  5285. END GetProcedure;
  5286. PROCEDURE CallProcedure;
  5287. VAR size: LONGINT;
  5288. BEGIN
  5289. IF procedure # NIL THEN
  5290. StaticCallOperand(result,procedure);
  5291. size := ProcedureParametersSize(system,procedure);
  5292. ELSE
  5293. Symbol(procedureVariable, result);
  5294. LoadValue(result, procedureVariable.type.resolved);
  5295. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5296. END;
  5297. Emit(Call(position,result.op,size));
  5298. END CallProcedure;
  5299. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5300. VAR res: Operand; string: SyntaxTree.String;
  5301. BEGIN
  5302. IF GetProcedure("String") THEN
  5303. NEW(string, LEN(s)); COPY(s, string^);
  5304. sv := SyntaxTree.NewStringValue(-1,string);
  5305. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5306. sv.SetType(type);
  5307. Designate(sv,res);
  5308. Emit(Push(position,res.tag));
  5309. Emit(Push(position,res.op));
  5310. ReleaseOperand(res);
  5311. CallProcedure;
  5312. END;
  5313. END String;
  5314. PROCEDURE Integer(op: IntermediateCode.Operand);
  5315. BEGIN
  5316. IF GetProcedure("Int") THEN
  5317. Emit(Push(position,op));
  5318. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5319. CallProcedure;
  5320. END;
  5321. END Integer;
  5322. PROCEDURE Float(op: IntermediateCode.Operand);
  5323. BEGIN
  5324. IF GetProcedure("HIntHex") THEN
  5325. Emit(Push(position,op));
  5326. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5327. CallProcedure;
  5328. END;
  5329. END Float;
  5330. PROCEDURE Set(op: IntermediateCode.Operand);
  5331. BEGIN
  5332. IF GetProcedure("Set") THEN
  5333. Emit(Push(position,op));
  5334. (*
  5335. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5336. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5337. *)
  5338. CallProcedure;
  5339. END;
  5340. END Set;
  5341. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5342. BEGIN
  5343. IF GetProcedure("Boolean") THEN
  5344. Emit(Push(position,op));
  5345. CallProcedure;
  5346. END;
  5347. END Boolean;
  5348. PROCEDURE Char(op: IntermediateCode.Operand);
  5349. BEGIN
  5350. IF GetProcedure("Char") THEN
  5351. Emit(Push(position,op));
  5352. CallProcedure;
  5353. END;
  5354. END Char;
  5355. PROCEDURE Address(op: IntermediateCode.Operand);
  5356. BEGIN
  5357. IF GetProcedure("Address") THEN
  5358. Emit(Push(position,op));
  5359. CallProcedure;
  5360. END;
  5361. END Address;
  5362. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5363. BEGIN
  5364. IF GetProcedure("String") THEN
  5365. Emit(Push(position,tag));
  5366. Emit(Push(position,op));
  5367. CallProcedure;
  5368. END;
  5369. END StringOperand;
  5370. PROCEDURE Ln;
  5371. BEGIN
  5372. IF GetProcedure("Ln") THEN
  5373. CallProcedure;
  5374. END;
  5375. END Ln;
  5376. BEGIN
  5377. IF backend.traceModuleName = "" THEN RETURN END;
  5378. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5379. IF GetProcedure("Enter") THEN
  5380. CallProcedure
  5381. END;
  5382. NEW(stringWriter,LEN(s));
  5383. FOR i := 0 TO x.Length()-1 DO
  5384. msg := "";
  5385. expression := x.GetExpression(i);
  5386. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5387. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5388. ELSE
  5389. Global.GetModuleName(module.module, s);
  5390. END;
  5391. IF i = 0 THEN
  5392. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5393. stringWriter.String(":");
  5394. END;
  5395. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5396. IF ~(expression IS SyntaxTree.StringValue) THEN
  5397. printout.Expression(expression);
  5398. stringWriter.Get(s);
  5399. Strings.Append(msg,s);
  5400. Strings.Append(msg,"= ");
  5401. ELSE stringWriter.Get(s); (* remove from string writer *)
  5402. Strings.Append(msg, s);
  5403. END;
  5404. String(msg);
  5405. IF SemanticChecker.IsStringType(expression.type) THEN
  5406. Designate(expression,res);
  5407. StringOperand(res.op,res.tag);
  5408. ELSE
  5409. Evaluate(expression,res);
  5410. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5411. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5412. Convert(res.op,int64);
  5413. END;
  5414. Integer(res.op);
  5415. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5416. Boolean(res.op);
  5417. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5418. Set(res.op);
  5419. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5420. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5421. Convert(res.op,float64);
  5422. END;
  5423. Float(res.op);
  5424. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5425. Char(res.op);
  5426. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5427. Address(res.op);String("H");
  5428. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5429. Address(res.op);String("H");
  5430. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5431. Address(res.op);String("H");
  5432. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5433. Address(res.op);String("H");
  5434. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5435. String("NIL");
  5436. ELSE HALT(200);
  5437. END;
  5438. END;
  5439. ReleaseOperand(res);
  5440. String("; ");
  5441. END;
  5442. IF GetProcedure("Exit") THEN
  5443. CallProcedure
  5444. ELSE
  5445. Ln;
  5446. END;
  5447. END;
  5448. END SystemTrace;
  5449. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5450. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5451. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5452. BEGIN
  5453. type := type.resolved;
  5454. IF type IS SyntaxTree.RecordType THEN
  5455. WITH type: SyntaxTree.RecordType DO
  5456. baseType := type.baseType;
  5457. IF baseType # NIL THEN
  5458. baseType := baseType.resolved;
  5459. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5460. InitFields(baseType,adr,offset);
  5461. END;
  5462. variable := type.recordScope.firstVariable;
  5463. WHILE variable # NIL DO
  5464. IF variable.initializer # NIL THEN
  5465. Evaluate(variable.initializer,initializerOp);
  5466. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5467. Emit(Mov(position,mem,initializerOp.op));
  5468. ReleaseOperand(initializerOp);
  5469. ReleaseIntermediateOperand(mem);
  5470. END;
  5471. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5472. variable := variable.nextVariable
  5473. END;
  5474. END;
  5475. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5476. WITH type: SyntaxTree.ArrayType DO
  5477. IF type.form = SyntaxTree.Static THEN
  5478. baseType := type.arrayBase;
  5479. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5480. FOR i := 0 TO type.staticLength-1 DO
  5481. InitFields(baseType,adr,offset+i*size);
  5482. END;
  5483. END;
  5484. END;
  5485. ELSIF type IS SyntaxTree.MathArrayType THEN
  5486. WITH type: SyntaxTree.MathArrayType DO
  5487. IF type.form = SyntaxTree.Open THEN
  5488. dim := DynamicDim(type);
  5489. imm := IntermediateCode.Immediate(addressType,dim);
  5490. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5491. baseType := SemanticChecker.ArrayBase(type,dim);
  5492. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5493. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5494. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5495. ReleaseIntermediateOperand(imm);
  5496. (* flags remain empty (=0) for open array *)
  5497. ELSIF type.form = SyntaxTree.Static THEN
  5498. baseType := type.arrayBase;
  5499. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5500. FOR i := 0 TO type.staticLength-1 DO
  5501. InitFields(baseType,adr,offset+i*size);
  5502. END;
  5503. END;
  5504. END;
  5505. END;
  5506. END InitFields;
  5507. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5508. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5509. BEGIN
  5510. type := variable.type.resolved;
  5511. IF (type IS SyntaxTree.MathArrayType) THEN
  5512. WITH type: SyntaxTree.MathArrayType DO
  5513. IF type.form = SyntaxTree.Open THEN
  5514. Symbol(variable,operand);
  5515. InitFields(type, operand.tag,0);
  5516. ELSIF type.form = SyntaxTree.Tensor THEN
  5517. Symbol(variable, operand);
  5518. MakeMemory(tmp,operand.op,addressType,0);
  5519. ReleaseOperand(operand);
  5520. Emit(Mov(position,tmp, nil ) );
  5521. ReleaseIntermediateOperand(tmp);
  5522. END;
  5523. END;
  5524. ELSE
  5525. Symbol(variable,operand);
  5526. IF variable.initializer # NIL THEN
  5527. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5528. reference.SetType(variable.type.resolved);
  5529. reference.SetAssignable(TRUE);
  5530. Assign(reference,variable.initializer);
  5531. END;
  5532. InitFields(type, operand.op,0);
  5533. ReleaseOperand(operand);
  5534. END;
  5535. END InitVariable;
  5536. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5537. VAR end: Label;
  5538. BEGIN
  5539. IF type.form = SyntaxTree.Tensor THEN
  5540. InitOperand(result,ModeValue);
  5541. ReuseCopy(result.op,base);
  5542. end := NewLabel();
  5543. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5544. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5545. SetLabel(end);
  5546. Convert(result.op,int32);
  5547. ELSE
  5548. InitOperand(result,ModeValue);
  5549. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5550. END
  5551. END MathArrayDim;
  5552. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5553. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5554. BEGIN
  5555. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5556. MakeMemory(mem,base,addressType,offset);
  5557. Emit(Mov(position,mem,value));
  5558. ReleaseIntermediateOperand(mem);
  5559. END PutMathArrayField;
  5560. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5561. VAR mem: IntermediateCode.Operand;
  5562. BEGIN
  5563. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5564. MakeMemory(mem,base,addressType,offset);
  5565. Emit(Mov(position,mem,value));
  5566. ReleaseIntermediateOperand(mem);
  5567. END PutMathArrayFieldOffset;
  5568. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5569. BEGIN
  5570. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5571. MakeMemory(value,base,addressType,offset);
  5572. END GetMathArrayField;
  5573. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5574. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5575. BEGIN
  5576. IF incr THEN
  5577. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5578. ELSE
  5579. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5580. END;
  5581. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5582. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5583. ELSE
  5584. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5585. Emit(Mov(position,reg,dim));
  5586. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5587. Emit(Add(position,reg,reg,base));
  5588. MakeMemory(mem, reg, addressType, offset);
  5589. ReleaseIntermediateOperand(reg);
  5590. Emit(Mov(position,mem,value));
  5591. ReleaseIntermediateOperand(mem);
  5592. END;
  5593. END PutMathArrayLenOrIncr;
  5594. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5595. BEGIN
  5596. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5597. END PutMathArrayLength;
  5598. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5599. BEGIN
  5600. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5601. END PutMathArrayIncrement;
  5602. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5603. BEGIN
  5604. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5605. END GetMathArrayIncrement;
  5606. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5607. BEGIN
  5608. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5609. END GetMathArrayLength;
  5610. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5611. VAR dimOp: IntermediateCode.Operand;
  5612. BEGIN
  5613. dimOp := IntermediateCode.Immediate(int32, dim);
  5614. GetMathArrayLength(type, operand, dimOp, check, result);
  5615. END GetMathArrayLengthAt;
  5616. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5617. VAR dimOp: IntermediateCode.Operand;
  5618. BEGIN
  5619. dimOp := IntermediateCode.Immediate(int32, dim);
  5620. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5621. END GetMathArrayIncrementAt;
  5622. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5623. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5624. offset: LONGINT;
  5625. BEGIN
  5626. IF increment THEN
  5627. offset := MathIncrOffset;
  5628. ELSE
  5629. offset := MathLenOffset;
  5630. END;
  5631. INC(offset,operand.dimOffset*2);
  5632. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5633. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5634. END;
  5635. (* static dimension *)
  5636. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5637. IF check & (type.form = SyntaxTree.Tensor) THEN
  5638. DimensionCheck(operand.tag,dim,BrltL);
  5639. END;
  5640. val := SHORT(dim.intValue);
  5641. IF type.form # SyntaxTree.Tensor THEN
  5642. t := SemanticChecker.ArrayBase(type,val);
  5643. type := t.resolved(SyntaxTree.MathArrayType);
  5644. IF type.form = SyntaxTree.Static THEN
  5645. IF increment THEN
  5646. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5647. ELSE
  5648. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5649. END;
  5650. InitOperand(result,ModeValue);
  5651. result.op := res;
  5652. RETURN;
  5653. END;
  5654. END;
  5655. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5656. MakeMemory(res,operand.tag,addressType,offset);
  5657. (*
  5658. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5659. *)
  5660. InitOperand(result,ModeValue);
  5661. result.op := res;
  5662. ELSE
  5663. Convert(dim,addressType);
  5664. IF check THEN
  5665. IF type.form = SyntaxTree.Tensor THEN
  5666. DimensionCheck(operand.tag,dim,BrltL);
  5667. ELSIF isUnchecked THEN (* do nothing *)
  5668. ELSE
  5669. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5670. END;
  5671. END;
  5672. end := NewLabel(); next := NIL;
  5673. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5674. Emit(Mov(position,res,dim));
  5675. Convert(res,int32);
  5676. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5677. WHILE t IS SyntaxTree.MathArrayType DO
  5678. type := t(SyntaxTree.MathArrayType);
  5679. IF type.form = SyntaxTree.Static THEN
  5680. imm := IntermediateCode.Immediate(int32,val);
  5681. next := NewLabel();
  5682. BrneL(next,imm,res);
  5683. IF increment THEN
  5684. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5685. ELSE
  5686. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5687. END;
  5688. Emit(MovReplace(position,res,imm));
  5689. BrL(end);
  5690. ELSE hasDynamicPart := TRUE;
  5691. END;
  5692. t := type.arrayBase.resolved;
  5693. val := val + 1;
  5694. IF next # NIL THEN SetLabel(next) END;
  5695. END;
  5696. IF hasDynamicPart THEN
  5697. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5698. Emit(Mov(position,res2,dim));
  5699. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5700. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5701. Emit(Add(position,res2,res2,imm));
  5702. Emit(Add(position,res2,res2,operand.tag));
  5703. IntermediateCode.MakeMemory(res2,int32);
  5704. Emit(MovReplace(position,res,res2));
  5705. ReleaseIntermediateOperand(res2);
  5706. END;
  5707. SetLabel(end);
  5708. Convert(res,int32);
  5709. InitOperand(result,ModeValue);
  5710. result.op := res;
  5711. END;
  5712. END MathArrayLenOrIncr;
  5713. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5714. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5715. offset: LONGINT;
  5716. BEGIN
  5717. offset := operand.dimOffset+DynamicDim(type)-1;
  5718. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5719. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5720. val := SHORT(dim.intValue);
  5721. t := SemanticChecker.ArrayBase(type,val);
  5722. type := t.resolved(SyntaxTree.ArrayType);
  5723. IF type.form = SyntaxTree.Static THEN
  5724. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5725. ELSE
  5726. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5727. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5728. END;
  5729. UseIntermediateOperand(res);
  5730. InitOperand(result,ModeValue);
  5731. result.op := res;
  5732. ELSE
  5733. Convert(dim,addressType);
  5734. IF ~isUnchecked THEN
  5735. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5736. END;
  5737. end := NewLabel(); next := NIL;
  5738. (* ReuseCopy(dim,res); *)
  5739. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5740. Emit(Mov(position,res,dim));
  5741. Convert(res,int32);
  5742. Convert(res,int32);
  5743. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5744. WHILE t IS SyntaxTree.ArrayType DO
  5745. type := t(SyntaxTree.ArrayType);
  5746. IF type.form = SyntaxTree.Static THEN
  5747. imm := IntermediateCode.Immediate(int32,val);
  5748. next := NewLabel();
  5749. BrneL(next,imm,res);
  5750. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5751. Emit(MovReplace(position,res,imm));
  5752. BrL(end);
  5753. ELSE hasDynamicPart := TRUE;
  5754. END;
  5755. t := type.arrayBase.resolved;
  5756. val := val + 1;
  5757. IF next # NIL THEN SetLabel(next) END;
  5758. END;
  5759. IF hasDynamicPart THEN
  5760. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5761. Convert(res2,addressType);
  5762. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5763. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5764. Emit(Sub(position,res2,imm,res2));
  5765. Emit(Add(position,res2,res2,operand.tag));
  5766. IntermediateCode.MakeMemory(res2,int32);
  5767. Emit(MovReplace(position,res,res2));
  5768. ReleaseIntermediateOperand(res2);
  5769. END;
  5770. SetLabel(end);
  5771. Convert(res,int32);
  5772. InitOperand(result,ModeValue);
  5773. result.op := res;
  5774. END;
  5775. END ArrayLen;
  5776. (**
  5777. create a temporary variable in current scope
  5778. **)
  5779. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5780. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5781. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5782. BEGIN
  5783. IF ~register THEN
  5784. variable := temporaries.GetFreeVariable(type, index);
  5785. ELSE
  5786. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5787. END;
  5788. scope := currentScope;
  5789. IF variable = NIL THEN
  5790. COPY("@hiddenIRVar",string);
  5791. Basic.AppendNumber(string,index);
  5792. name := SyntaxTree.NewIdentifier(string);
  5793. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5794. variable.SetType(type);
  5795. variable.SetAccess(SyntaxTree.Hidden);
  5796. IF ~register THEN
  5797. temporaries.AddVariable(variable);
  5798. IF scope.lastVariable # NIL THEN
  5799. offset := scope.lastVariable.offsetInBits;
  5800. ELSE
  5801. offset := 0;
  5802. END;
  5803. DEC(offset,system.SizeOf(variable.type));
  5804. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5805. variable(SyntaxTree.Variable).SetOffset(offset);
  5806. scope.AddVariable(variable(SyntaxTree.Variable));
  5807. scope.EnterSymbol(variable, duplicate);
  5808. ASSERT(~duplicate);
  5809. InitVariable(variable(SyntaxTree.Variable));
  5810. ELSE
  5811. variable.SetUseRegister(TRUE);
  5812. variable(SyntaxTree.Variable).SetOffset(0);
  5813. END;
  5814. ELSE
  5815. InitVariable(variable(SyntaxTree.Variable));
  5816. (*
  5817. ASSERT(variable.type.resolved = type.resolved)
  5818. *)
  5819. END;
  5820. RETURN variable(SyntaxTree.Variable)
  5821. END GetTemporaryVariable;
  5822. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5823. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5824. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5825. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5826. i: LONGINT; duplicate: BOOLEAN;
  5827. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5828. VAR variable: SyntaxTree.Variable;
  5829. BEGIN
  5830. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5831. variable.SetType(type);
  5832. recordScope.AddVariable(variable);
  5833. END AddVariable;
  5834. BEGIN
  5835. name := "@ArrayDescriptor";
  5836. Basic.AppendNumber(name,dimensions);
  5837. identifier := SyntaxTree.NewIdentifier(name);
  5838. parentScope := module.module.moduleScope;
  5839. symbol := parentScope.FindSymbol(identifier);
  5840. IF symbol # NIL THEN
  5841. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5842. type := typeDeclaration.declaredType;
  5843. ELSE
  5844. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5845. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5846. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5847. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5848. recordType.SetTypeDeclaration(typeDeclaration);
  5849. recordType.SetState(SyntaxTree.Resolved);
  5850. typeDeclaration.SetDeclaredType(recordType);
  5851. AddVariable("@ptr",system.anyType);
  5852. AddVariable("@adr",system.addressType);
  5853. AddVariable("@flags",system.addressType);
  5854. AddVariable("@dim",system.addressType);
  5855. AddVariable("@elementSize",system.addressType);
  5856. FOR i := 0 TO dimensions-1 DO
  5857. name := "@len";
  5858. Basic.AppendNumber(name,i);
  5859. AddVariable(name,system.addressType);
  5860. name := "@incr";
  5861. Basic.AppendNumber(name,i);
  5862. AddVariable(name,system.addressType);
  5863. END;
  5864. parentScope.AddTypeDeclaration(typeDeclaration);
  5865. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5866. ASSERT(~duplicate);
  5867. type := recordType;
  5868. END;
  5869. RETURN type
  5870. END GetMathArrayDescriptorType;
  5871. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5872. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5873. BEGIN
  5874. type := GetMathArrayDescriptorType(dimensions);
  5875. Emit(Push(position,op.op));
  5876. (* push type descriptor *)
  5877. reg := TypeDescriptorAdr(type);
  5878. IF ~newObjectFile THEN
  5879. IntermediateCode.MakeMemory(reg,addressType);
  5880. END;
  5881. Emit(Push(position,reg));
  5882. ReleaseIntermediateOperand(reg);
  5883. (* push realtime flag: false by default *)
  5884. Emit(Push(position,false));
  5885. CallThis(position,"Heaps","NewRec",3);
  5886. END NewMathArrayDescriptor;
  5887. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5888. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5889. BEGIN
  5890. NEW(string, LEN(s)); COPY(s, string^);
  5891. sv := SyntaxTree.NewStringValue(-1,string);
  5892. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5893. sv.SetType(type);
  5894. Designate(sv,res);
  5895. Emit(Push(position,res.tag));
  5896. Emit(Push(position,res.op));
  5897. ReleaseOperand(res);
  5898. END PushConstString;
  5899. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5900. BEGIN
  5901. IF b THEN
  5902. Emit(Push(-1, true));
  5903. ELSE
  5904. Emit(Push(-1, false));
  5905. END;
  5906. END PushConstBoolean;
  5907. PROCEDURE PushConstSet(v: SET);
  5908. VAR value: SyntaxTree.Value; op: Operand;
  5909. BEGIN
  5910. value := SyntaxTree.NewSetValue(-1, v);
  5911. value.SetType(system.setType);
  5912. Evaluate(value, op);
  5913. Emit(Push(-1, op.op));
  5914. ReleaseOperand(op);
  5915. END PushConstSet;
  5916. PROCEDURE PushConstInteger(v: LONGINT);
  5917. VAR value: SyntaxTree.Value; op: Operand;
  5918. BEGIN
  5919. value := SyntaxTree.NewIntegerValue(-1, v);
  5920. value.SetType(system.longintType);
  5921. Evaluate(value, op);
  5922. Emit(Push(-1, op.op));
  5923. ReleaseOperand(op);
  5924. END PushConstInteger;
  5925. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5926. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5927. section: IntermediateCode.Section;
  5928. BEGIN
  5929. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5930. Global.GetSymbolSegmentedName(symbol, name);
  5931. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5932. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5933. procedure.SetScope(moduleScope);
  5934. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5935. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5936. procedure.SetAccess(SyntaxTree.Hidden);
  5937. currentScope := procedureScope;
  5938. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5939. EmitEnter(section, -1,NIL,0,0,0);
  5940. RETURN section;
  5941. END OpenInitializer;
  5942. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5943. BEGIN
  5944. EmitLeave(section, 0, 0 );
  5945. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5946. section := prev;
  5947. END CloseInitializer;
  5948. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5949. VAR name: SyntaxTree.IdentifierString;
  5950. variable: SyntaxTree.Variable;
  5951. type: SyntaxTree.Type;
  5952. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5953. BEGIN
  5954. InitOperand(op,ModeReference);
  5955. op.op := fp;
  5956. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5957. Dereference(op, x, FALSE);
  5958. result := op;
  5959. Symbol(symbol, op);
  5960. END Field;
  5961. PROCEDURE Direction(direction: LONGINT): SET;
  5962. BEGIN
  5963. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  5964. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  5965. ELSE HALT(100);
  5966. END;
  5967. END Direction;
  5968. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  5969. VAR name: ARRAY 256 OF CHAR; op: Operand;
  5970. BEGIN
  5971. Field(port, op);
  5972. ToMemory(op.op,addressType,0);
  5973. Emit(Push(-1, op.op));
  5974. ReleaseOperand(op);
  5975. Basic.GetString(modifier.identifier, name);
  5976. PushConstString(name);
  5977. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  5978. ASSERT(SemanticChecker.IsStringType(value.type));
  5979. Designate(value, op);
  5980. Emit(Push(modifier.position, op.tag));
  5981. Emit(Push(modifier.position, op.op));
  5982. ReleaseOperand(op);
  5983. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  5984. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  5985. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  5986. Evaluate(value, op);
  5987. Emit(Push(modifier.position, op.op));
  5988. ReleaseOperand(op);
  5989. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  5990. ELSE
  5991. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  5992. END;
  5993. END AddPortProperty;
  5994. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  5995. VAR modifier: SyntaxTree.Modifier;
  5996. BEGIN
  5997. modifier := variable.modifiers;
  5998. WHILE modifier # NIL DO
  5999. AddPortProperty(variable,modifier, modifier.expression);
  6000. modifier := modifier.nextModifier;
  6001. END;
  6002. END AddPortProperties;
  6003. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6004. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6005. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6006. PROCEDURE PushLens(type: SyntaxTree.Type);
  6007. BEGIN
  6008. IF type IS SyntaxTree.ArrayType THEN
  6009. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6010. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6011. Emit(Push(-1, op.op));
  6012. ReleaseOperand(op);
  6013. INC(dim);
  6014. ELSE
  6015. baseType := type;
  6016. END;
  6017. END PushLens;
  6018. BEGIN
  6019. (* cell *)
  6020. IF variable.type IS SyntaxTree.ArrayType THEN
  6021. type := variable.type;
  6022. dim := 0;
  6023. PushLens(type);
  6024. portType := baseType.resolved(SyntaxTree.PortType);
  6025. ELSE
  6026. portType := variable.type(SyntaxTree.PortType);
  6027. END;
  6028. PushSelfPointer();
  6029. (* port *)
  6030. Field(variable, op);
  6031. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6032. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6033. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6034. Designate(d, op);*)
  6035. Emit(Push(-1, op.op));
  6036. ReleaseOperand(op);
  6037. (* name *)
  6038. PushConstString(name);
  6039. (* inout *)
  6040. PushConstSet(Direction(portType.direction));
  6041. (* width *)
  6042. PushConstInteger(portType.sizeInBits);
  6043. IF variable.type IS SyntaxTree.PortType THEN
  6044. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6045. AddPortProperties(variable);
  6046. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6047. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6048. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6049. Emit(Add(position,reg, sp, size));
  6050. (* dim *)
  6051. PushConstInteger(dim);
  6052. (* len array *)
  6053. Emit(Push(position, reg));
  6054. ReleaseIntermediateOperand(reg);
  6055. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6056. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6057. Emit(Add(position, sp,sp, size));
  6058. END;
  6059. END Variable;
  6060. BEGIN
  6061. IF backend.cellsAreObjects THEN
  6062. variable := x.cellScope.firstVariable;
  6063. WHILE (variable # NIL) DO
  6064. type := variable.type.resolved;
  6065. WHILE (type IS SyntaxTree.ArrayType) DO
  6066. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6067. END;
  6068. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6069. (*!!! check port arrays !! *)
  6070. Global.GetSymbolName(variable,name);
  6071. Variable(name,variable);
  6072. END;
  6073. variable := variable.nextVariable;
  6074. END;
  6075. ELSE HALT(200)
  6076. END;
  6077. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6078. (*
  6079. x.typeDeclaration.GetName(componentName);
  6080. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6081. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6082. instanceType.SetDataMemorySize(dataMemorySize);
  6083. END;
  6084. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6085. instanceType.SetInstructionMemorySize(codeMemorySize)
  6086. END;
  6087. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6088. instanceType.AddCapability(ActiveCells.VectorCapability)
  6089. END;
  6090. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6091. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6092. END;
  6093. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6094. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6095. END;
  6096. AddDevices(instanceType, x);
  6097. *)
  6098. (*
  6099. IF x.isCellNet THEN
  6100. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6101. END;
  6102. *)
  6103. (*currentActiveCellsScope := instanceType;*)
  6104. (*
  6105. parameter := x.firstParameter;
  6106. portIndex := 0;
  6107. WHILE parameter # NIL DO
  6108. parameter.GetName(parameterName);
  6109. parameterType := parameter.type.resolved;
  6110. Parameter(parameterName, parameterType);
  6111. (*
  6112. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6113. ParameterArray(parameterType);
  6114. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6115. FOR i := 0 TO len-1 DO
  6116. COPY(parameterName,name);
  6117. AppendIndex(name,i);
  6118. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6119. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6120. INC(portIndex);
  6121. END;
  6122. ELSE
  6123. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6124. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6125. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6126. INC(portIndex);
  6127. END;
  6128. *)
  6129. parameter := parameter.nextParameter;
  6130. END;
  6131. *)
  6132. (*
  6133. Scope(x.cellScope);
  6134. currentActiveCellsScope := prevActiveCellsScope;
  6135. AddModules(instanceType,x.cellScope);
  6136. *)
  6137. END AddPorts;
  6138. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6139. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6140. BEGIN
  6141. Symbol(cell,op);
  6142. ToMemory(op.op,addressType,0);
  6143. Emit(Push(position,op.op));
  6144. ReleaseOperand(op);
  6145. Basic.GetString(property.name, name);
  6146. PushConstString(name);
  6147. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6148. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6149. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6150. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6151. Designate(d, op);
  6152. IF SemanticChecker.IsStringType(property.type) THEN
  6153. Emit(Push(-1, op.tag))
  6154. END;
  6155. Emit(Push(-1, op.op));
  6156. ReleaseOperand(op);
  6157. END;
  6158. IF SemanticChecker.IsStringType(property.type) THEN
  6159. ASSERT(SemanticChecker.IsStringType(value.type));
  6160. Designate(value, op);
  6161. Emit(Push(property.position, op.tag));
  6162. Emit(Push(property.position, op.op));
  6163. ReleaseOperand(op);
  6164. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6165. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6166. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6167. Evaluate(value, op);
  6168. Emit(Push(property.position, op.op));
  6169. ReleaseOperand(op);
  6170. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6171. ELSE
  6172. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6173. END;
  6174. END AddProperty;
  6175. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6176. VAR symbol: SyntaxTree.Symbol;
  6177. BEGIN
  6178. WHILE modifier # NIL DO
  6179. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6180. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6181. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6182. ELSE
  6183. (*! move this check to checker *)
  6184. Error(modifier.position, "undefined property");
  6185. END;
  6186. modifier := modifier.nextModifier;
  6187. END;
  6188. END AddModifiers;
  6189. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6190. VAR last: SyntaxTree.Modifier;
  6191. BEGIN
  6192. IF to = NIL THEN
  6193. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6194. ELSE
  6195. last := to;
  6196. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6197. last := last.nextModifier;
  6198. END;
  6199. IF last.identifier # this.identifier THEN
  6200. ASSERT(last.nextModifier = NIL);
  6201. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6202. END;
  6203. END;
  6204. END AppendModifier;
  6205. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6206. BEGIN
  6207. WHILE this # NIL DO
  6208. AppendModifier(to, this);
  6209. this := this.nextModifier;
  6210. END;
  6211. END AppendModifiers;
  6212. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6213. VAR op: Operand;
  6214. BEGIN
  6215. Evaluate(p, op);
  6216. Emit(Push(p.position, op.op));
  6217. ReleaseOperand(op);
  6218. (*
  6219. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6220. p := p(SyntaxTree.Designator).left;
  6221. END;
  6222. IF p # NIL THEN
  6223. Evaluate(p, op);
  6224. Emit(Push(p.position, op.op));
  6225. ReleaseOperand(op);
  6226. ELSE
  6227. Emit(Push(-1, nil));
  6228. END;
  6229. *)
  6230. END PushPort;
  6231. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6232. VAR
  6233. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6234. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6235. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6236. tmp:IntermediateCode.Operand;
  6237. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6238. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6239. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6240. dest: IntermediateCode.Operand;
  6241. staticLength: LONGINT; itype: IntermediateCode.Type;
  6242. convert,isTensor: BOOLEAN;
  6243. recordType: SyntaxTree.RecordType;
  6244. baseType: SyntaxTree.Type;
  6245. flags: SET;
  6246. left: SyntaxTree.Expression;
  6247. call: SyntaxTree.Designator;
  6248. procedure: SyntaxTree.Procedure;
  6249. temporaryVariable: SyntaxTree.Variable;
  6250. dummy: IntermediateCode.Operand;
  6251. customBuiltin: SyntaxTree.CustomBuiltin;
  6252. isVarPar: ARRAY 3 OF BOOLEAN;
  6253. callsection: Sections.Section;
  6254. segmentedName: Basic.SegmentedName;
  6255. needsTrace: BOOLEAN;
  6256. n: ARRAY 256 OF CHAR;
  6257. modifier: SyntaxTree.Modifier;
  6258. previous, init: IntermediateCode.Section;
  6259. prevScope: SyntaxTree.Scope;
  6260. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6261. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6262. priority: IntermediateCode.Operand;
  6263. op,callop: Operand;
  6264. BEGIN
  6265. IF type = NIL THEN RETURN END;
  6266. type := type.resolved;
  6267. IF type IS SyntaxTree.PointerType THEN
  6268. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6269. END;
  6270. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6271. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6272. recordScope := type(SyntaxTree.RecordType).recordScope;
  6273. IF recordScope.bodyProcedure # NIL THEN
  6274. procedure := recordScope.bodyProcedure;
  6275. body := procedure.procedureScope.body;
  6276. Emit(Push(position,self));
  6277. IF body.isActive THEN
  6278. StaticCallOperand(callop,procedure);
  6279. Emit(Push(position,callop.op));
  6280. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6281. Convert(priority,sizeType);
  6282. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6283. END;
  6284. Emit(Push(position,priority));
  6285. ReleaseIntermediateOperand(priority);
  6286. IF backend.cooperative THEN
  6287. Emit(Push(position,self));
  6288. CallThis(position,"Activities","Create",3)
  6289. ELSE
  6290. flags := 0;
  6291. IF body.isSafe THEN
  6292. flags := 1;
  6293. END;
  6294. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6295. Emit(Push(position,self));
  6296. CallThis(position,"Objects","CreateProcess",4)
  6297. END;
  6298. ELSE
  6299. Emit(Push(position,self));
  6300. StaticCallOperand(callop,procedure);
  6301. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6302. END;
  6303. Emit(Pop(position,self));
  6304. END;
  6305. END CallBodies;
  6306. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6307. BEGIN
  6308. actualType := actualType.resolved;
  6309. IF actualType IS SyntaxTree.StringType THEN
  6310. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6311. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6312. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6313. ELSE
  6314. tmp := op.tag;
  6315. IntermediateCode.MakeMemory(tmp,addressType);
  6316. Emit(Push(position,tmp));
  6317. END;
  6318. Emit(Push(position,op.op))
  6319. END PushString;
  6320. PROCEDURE PushTD(type: SyntaxTree.Type);
  6321. VAR op: IntermediateCode.Operand;
  6322. BEGIN
  6323. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6324. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6325. ELSE
  6326. IF type.resolved IS SyntaxTree.PointerType THEN
  6327. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6328. END;
  6329. op := TypeDescriptorAdr(type.resolved);
  6330. IF ~newObjectFile THEN
  6331. IntermediateCode.MakeMemory(op,addressType);
  6332. END;
  6333. Emit(Push(position,op));
  6334. END
  6335. END PushTD;
  6336. BEGIN
  6337. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6338. dest := destination; destination := emptyOperand;
  6339. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6340. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6341. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6342. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6343. CASE x.id OF
  6344. (* ---- COPY ----- *)
  6345. |Global.Copy:
  6346. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6347. (* ---- EXCL, INCL----- *)
  6348. |Global.Excl,Global.Incl:
  6349. Evaluate(p0,s0);
  6350. Evaluate(p1,s1);
  6351. Convert(s1.op,setType);
  6352. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6353. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6354. END;
  6355. ReuseCopy(res,s0.op);
  6356. ReleaseOperand(s0);
  6357. Reuse1(tmp,s1.op);
  6358. ReleaseOperand(s1);
  6359. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6360. IF x.id = Global.Excl THEN
  6361. Emit(Not(position,tmp,tmp));
  6362. Emit(And(position,res,res,tmp));
  6363. ELSE
  6364. Emit(Or(position,res,res,tmp));
  6365. END;
  6366. ReleaseIntermediateOperand(tmp);
  6367. Designate(p0,s0);
  6368. ToMemory(s0.op,setType,0);
  6369. Emit(Mov(position,s0.op,res));
  6370. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6371. (* ---- DISPOSE ----- *)
  6372. |Global.Dispose:
  6373. Designate(p0,s0);
  6374. Emit(Push(position,s0.op));
  6375. ReleaseOperand(s0);
  6376. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6377. (* ---- GETPROCEDURE ----- *)
  6378. |Global.GetProcedure:
  6379. Designate(p0,s0);
  6380. PushString(s0,p0.type);
  6381. Designate(p1,s1);
  6382. PushString(s1,p1.type);
  6383. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6384. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6385. ELSE PushTD(procedureType.firstParameter.type)
  6386. END;
  6387. PushTD(procedureType.returnType);
  6388. Designate(p2,s2);
  6389. Emit(Push(position,s2.op));
  6390. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6391. CallThis(position,"Modules","GetProcedure", 7);
  6392. (* ---- ASH, LSH, ROT ----- *)
  6393. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6394. Evaluate(p0,s0);
  6395. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6396. (* make unsigned arguments in order to produced a logical shift *)
  6397. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6398. convert:= TRUE;
  6399. itype := s0.op.type;
  6400. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6401. Convert(s0.op,itype);
  6402. ELSE
  6403. convert := FALSE;
  6404. END;
  6405. END;
  6406. Evaluate(p1,s1);
  6407. IF IsIntegerConstant(p1,hint) THEN
  6408. ReuseCopy(reg,s0.op);
  6409. IF hint > 0 THEN
  6410. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6411. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6412. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6413. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6414. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6415. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6416. END;
  6417. ELSIF hint < 0 THEN
  6418. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6419. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6420. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6421. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6422. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6423. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6424. END;
  6425. END;
  6426. ReleaseOperand(s0); ReleaseOperand(s1);
  6427. ELSE
  6428. exit := NewLabel();
  6429. end := NewLabel();
  6430. ReuseCopy(reg,s0.op);
  6431. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6432. Reuse1(tmp,s1.op);
  6433. Emit(Neg(position,tmp,s1.op));
  6434. Convert(tmp,s1.op.type);
  6435. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6436. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6437. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6438. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6439. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6440. END;
  6441. ReleaseIntermediateOperand(tmp);
  6442. BrL(end);
  6443. SetLabel(exit);
  6444. ReuseCopy(tmp,s1.op);
  6445. Convert(tmp,s1.op.type);
  6446. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6447. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6448. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6449. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6450. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6451. END;
  6452. ReleaseIntermediateOperand(tmp);
  6453. SetLabel(end);
  6454. ReleaseOperand(s0); ReleaseOperand(s1);
  6455. END;
  6456. InitOperand(result,ModeValue);
  6457. IF convert THEN
  6458. itype := reg.type;
  6459. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6460. Convert(reg,itype);
  6461. END;
  6462. result.op := reg;
  6463. (* ---- CAP ----- *)
  6464. |Global.Cap:
  6465. Evaluate(p0,result);
  6466. ReuseCopy(reg,result.op);
  6467. ReleaseIntermediateOperand(result.op);
  6468. ignore := NewLabel();
  6469. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6470. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6471. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6472. SetLabel(ignore);
  6473. result.op := reg;
  6474. (* ---- CHR ----- *)
  6475. |Global.Chr, Global.Chr32:
  6476. Evaluate(p0,result);
  6477. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6478. |Global.Entier, Global.EntierH:
  6479. Evaluate(p0,result);
  6480. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6481. (* ---- MIN and MAX ----- *)
  6482. |Global.Max,Global.Min:
  6483. Evaluate(p0,s0);
  6484. Evaluate(p1,s1);
  6485. Reuse2(res,s0.op,s1.op);
  6486. else := NewLabel();
  6487. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6488. ELSE BrltL(else,s1.op,s0.op) END;
  6489. Emit(Mov(position,res,s0.op));
  6490. ReleaseOperand(s0);
  6491. end := NewLabel();
  6492. BrL(end);
  6493. SetLabel(else);
  6494. Emit(MovReplace(position,res,s1.op));
  6495. SetLabel(end);
  6496. ReleaseOperand(s1);
  6497. InitOperand(result,ModeValue);
  6498. result.op := res;
  6499. (* ---- ODD ----- *)
  6500. |Global.Odd:
  6501. IF ~conditional THEN
  6502. ConditionToValue(x)
  6503. ELSE
  6504. Evaluate(p0,result);
  6505. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6506. Reuse1(res,result.op);
  6507. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6508. ReleaseIntermediateOperand(result.op);
  6509. result.op := res;
  6510. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6511. ReleaseOperand(result);
  6512. BrL(falseLabel);
  6513. END;
  6514. (* ---- ORD ----- *)
  6515. |Global.Ord, Global.Ord32:
  6516. Evaluate(p0,result);
  6517. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6518. (* ---- SHORT, LONG ----- *)
  6519. |Global.Short, Global.Long:
  6520. Evaluate(p0,result);
  6521. IF x.type IS SyntaxTree.ComplexType THEN
  6522. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6523. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6524. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6525. ELSE
  6526. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6527. END
  6528. (* ---- HALT, SYSTEM.HALT----- *)
  6529. |Global.Halt, Global.systemHalt:
  6530. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6531. EmitTrap (position, val);
  6532. (* ---- ASSERT ----- *)
  6533. |Global.Assert:
  6534. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6535. trueL := NewLabel();
  6536. falseL := NewLabel();
  6537. Condition(p0,trueL,falseL);
  6538. IF p1 = NIL THEN val := AssertTrap
  6539. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6540. END;
  6541. SetLabel(falseL);
  6542. EmitTrap(position,val);
  6543. SetLabel(trueL);
  6544. END;
  6545. (*
  6546. Emit(TrapC(result.op,val);
  6547. *)
  6548. (* ---- INC, DEC----- *)
  6549. |Global.Inc,Global.Dec:
  6550. Expression(p0); adr := result.op;
  6551. LoadValue(result,p0.type); l := result;
  6552. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6553. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6554. END;
  6555. IF x.id = Global.Inc THEN
  6556. Emit(Add(position,l.op,l.op,r.op));
  6557. ELSE
  6558. Emit(Sub(position,l.op,l.op,r.op));
  6559. END;
  6560. ReleaseOperand(l); ReleaseOperand(r);
  6561. (* ---- LEN ----- *)
  6562. |Global.Len: (* dynamic length, static length done by checker *)
  6563. Designate(p0,operand);
  6564. IF p1 = NIL THEN
  6565. InitOperand(l,ModeValue);
  6566. l.op := IntermediateCode.Immediate(int32,0);
  6567. ELSE
  6568. Evaluate(p1,l);
  6569. END;
  6570. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6571. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6572. Dereference(operand, p0.type.resolved, FALSE);
  6573. END;
  6574. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6575. ReleaseOperand(operand); ReleaseOperand(l);
  6576. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6577. ASSERT(p1 # NIL);
  6578. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6579. Dereference(operand,p0.type.resolved,FALSE);
  6580. END;
  6581. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6582. ReleaseOperand(operand); ReleaseOperand(l);
  6583. ELSE HALT(100);
  6584. END;
  6585. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6586. (* ---- FIRST ---- *)
  6587. |Global.First:
  6588. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6589. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6590. ELSE
  6591. Designate(p0, result)
  6592. END
  6593. (* ---- LAST ---- *)
  6594. |Global.Last:
  6595. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6596. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6597. ELSE
  6598. Designate(p0, result);
  6599. (* make sure result.op is a register *)
  6600. tmp := result.op;
  6601. ReuseCopy(result.op, result.op);
  6602. ReleaseIntermediateOperand(tmp);
  6603. (* add offset to result.op *)
  6604. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6605. END
  6606. (* ---- STEP ---- *)
  6607. |Global.Step:
  6608. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6609. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6610. ELSE
  6611. Designate(p0, result);
  6612. (* make sure result.op is a register *)
  6613. tmp := result.op;
  6614. ReuseCopy(result.op, result.op);
  6615. ReleaseIntermediateOperand(tmp);
  6616. (* add offset to result.op *)
  6617. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6618. END
  6619. (* ---- RE ---- *)
  6620. |Global.Re:
  6621. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6622. Designate(p0, result)
  6623. ELSE
  6624. Evaluate(p0, result)
  6625. END
  6626. (* ---- IM ---- *)
  6627. |Global.Im:
  6628. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6629. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6630. Designate(p0, result);
  6631. (* make sure result.op is a register *)
  6632. tmp := result.op;
  6633. ReuseCopy(result.op, result.op);
  6634. ReleaseIntermediateOperand(tmp);
  6635. (* add offset to result.op *)
  6636. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6637. (* ---- ABS ----- *)
  6638. |Global.Abs:
  6639. Evaluate(p0,operand);
  6640. type := p0.type.resolved;
  6641. InitOperand(result,ModeValue);
  6642. Reuse1a(result.op,operand.op,dest);
  6643. Emit(Abs(position,result.op,operand.op));
  6644. ReleaseOperand(operand);
  6645. (* ---- WAIT ----- *)
  6646. |Global.Wait:
  6647. Evaluate(p0,operand);
  6648. Emit(Push(position,operand.op));
  6649. ReleaseOperand(operand);
  6650. CallThis(position,"Activities","Wait", 1);
  6651. (* ---- NEW ----- *)
  6652. |Global.New:
  6653. (*! the following code is only correct for "standard" Oberon calling convention *)
  6654. type := p0.type.resolved;
  6655. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6656. THEN
  6657. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6658. IF backend.cooperative THEN
  6659. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6660. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6661. IF recordType.isObject THEN
  6662. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6663. IF recordType.IsActive() THEN
  6664. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6665. END;
  6666. IF recordType.IsProtected() THEN
  6667. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6668. END;
  6669. ELSE
  6670. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6671. END;
  6672. END;
  6673. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6674. CallThis(position,"Runtime","New", 1);
  6675. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6676. Emit(Result(position, pointer));
  6677. exit := NewLabel();
  6678. BreqL(exit,pointer,nil);
  6679. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6680. GetRecordTypeName (recordType,name);
  6681. IF ~recordType.isObject THEN
  6682. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6683. END;
  6684. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6685. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6686. IF recordType.isObject THEN
  6687. IF recordType.IsProtected() THEN
  6688. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6689. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6690. END;
  6691. IF recordType.IsActive() THEN
  6692. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6693. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6694. END;
  6695. END;
  6696. END;
  6697. (* initialize fields *)
  6698. IF type(SyntaxTree.PointerType).isPlain THEN
  6699. size := 0;
  6700. ELSIF recordType.isObject THEN
  6701. size := BaseObjectTypeSize;
  6702. ELSE
  6703. size := BaseRecordTypeSize;
  6704. END;
  6705. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6706. (* call initializer *)
  6707. constructor := GetConstructor(recordType);
  6708. IF constructor # NIL THEN
  6709. (*! should be unified with ProcedureCallDesignator *)
  6710. Emit(Push(position,pointer));
  6711. ReleaseIntermediateOperand(pointer);
  6712. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6713. FOR i := 1 TO x.parameters.Length()-1 DO
  6714. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6715. formalParameter := formalParameter.nextParameter;
  6716. END;
  6717. (* static call of the constructor *)
  6718. GetCodeSectionNameForSymbol(constructor,name);
  6719. ASSERT(~constructor.isInline);
  6720. IF constructor.scope.ownerModule # module.module THEN
  6721. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6722. ELSE
  6723. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6724. END;
  6725. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6726. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6727. Emit(Pop(position,pointer));
  6728. END;
  6729. (* call bodies *)
  6730. CallBodies(pointer,p0.type);
  6731. SetLabel(exit);
  6732. needsTrace := p0.NeedsTrace();
  6733. IF needsTrace THEN ModifyAssignments(true) END;
  6734. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6735. Emit(Push(position, pointer));
  6736. CallThis(position,"GarbageCollector","Watch",0);
  6737. Emit(Pop(position, pointer));
  6738. END;
  6739. Designate(p0,l);
  6740. IF needsTrace THEN
  6741. CallAssignPointer(l.op, pointer);
  6742. ELSE
  6743. ToMemory(l.op,addressType,0);
  6744. Emit(Mov(position,l.op,pointer));
  6745. END;
  6746. ReleaseIntermediateOperand(pointer);
  6747. ReleaseOperand(l);
  6748. IF needsTrace THEN ModifyAssignments(false) END;
  6749. ELSE
  6750. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6751. IF temporaryVariable # NIL THEN
  6752. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6753. ELSE
  6754. Designate(p0,l);
  6755. END;
  6756. (* l.op contains address of pointer to record *)
  6757. Emit(Push(position,l.op)); (* address for use after syscall *)
  6758. Emit(Push(position,l.op));
  6759. ReleaseOperand(l);
  6760. (* push type descriptor *)
  6761. reg := TypeDescriptorAdr(recordType);
  6762. IF ~newObjectFile THEN
  6763. IntermediateCode.MakeMemory(reg,addressType);
  6764. END;
  6765. Emit(Push(position,reg));
  6766. ReleaseIntermediateOperand(reg);
  6767. (* push realtime flag *)
  6768. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6769. ELSE Emit(Push(position,false));
  6770. END;
  6771. CallThis(position,"Heaps","NewRec", 3);
  6772. (* check allocation success, if not successful then do not call initializers and bodies *)
  6773. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6774. Emit(Pop(position,pointer));
  6775. MakeMemory(reg,pointer,addressType,0);
  6776. ReleaseIntermediateOperand(pointer);
  6777. pointer := reg;
  6778. exit := NewLabel();
  6779. BreqL(exit,pointer,nil);
  6780. Emit(Push(position,pointer));
  6781. (* initialize fields *)
  6782. InitFields(recordType, pointer,0);
  6783. (* call initializer *)
  6784. constructor := GetConstructor(recordType);
  6785. IF constructor # NIL THEN
  6786. (*! should be unified with ProcedureCallDesignator *)
  6787. Emit(Push(position,pointer));
  6788. ReleaseIntermediateOperand(pointer);
  6789. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6790. FOR i := 1 TO x.parameters.Length()-1 DO
  6791. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6792. formalParameter := formalParameter.nextParameter;
  6793. END;
  6794. (* static call of the constructor *)
  6795. GetCodeSectionNameForSymbol(constructor,name);
  6796. ASSERT(~constructor.isInline);
  6797. IF constructor.scope.ownerModule # module.module THEN
  6798. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6799. ELSE
  6800. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6801. END;
  6802. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6803. ELSE
  6804. ReleaseIntermediateOperand(pointer);
  6805. END;
  6806. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6807. Emit(Pop(position,pointer));
  6808. IF temporaryVariable # NIL THEN
  6809. Designate(p0,l);
  6810. ToMemory(l.op,addressType,0);
  6811. Emit(Mov(position,l.op,pointer));
  6812. ReleaseOperand(l);
  6813. result.tag := emptyOperand;
  6814. END;
  6815. (* call bodies *)
  6816. CallBodies(pointer,p0.type);
  6817. ReleaseIntermediateOperand(pointer);
  6818. IF temporaryVariable # NIL THEN
  6819. end := NewLabel();
  6820. BrL(end);
  6821. SetLabel(exit);
  6822. Designate(p0,l);
  6823. ToMemory(l.op,addressType,0);
  6824. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6825. ReleaseOperand(l);
  6826. SetLabel(end);
  6827. ELSE
  6828. SetLabel(exit);
  6829. END;
  6830. END;
  6831. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6832. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6833. dim := 0;
  6834. IF p1 # NIL THEN
  6835. FOR i := 1 TO x.parameters.Length()-1 DO
  6836. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6837. parameter := x.parameters.GetExpression(i);
  6838. Evaluate(parameter,r);
  6839. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6840. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6841. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6842. END;
  6843. Emit(Push(position,r.op));
  6844. IF i=1 THEN
  6845. ReuseCopy(reg,r.op);
  6846. ELSE
  6847. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6848. END;
  6849. ReleaseOperand(r);
  6850. INC(dim);
  6851. END;
  6852. Convert(reg,addressType);
  6853. ELSE
  6854. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6855. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6856. END;
  6857. openDim := dim;
  6858. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6859. IF backend.cooperative THEN
  6860. size := ToMemoryUnits(system,system.SizeOf(type));
  6861. WHILE type IS SyntaxTree.ArrayType DO
  6862. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6863. END;
  6864. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6865. IF (size # 1) THEN
  6866. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6867. END;
  6868. Emit(Push(position,reg));
  6869. size := ToMemoryUnits(system,system.SizeOf(type));
  6870. IF (size # 1) THEN
  6871. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6872. END;
  6873. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6874. Emit(Push(position,reg));
  6875. ReleaseIntermediateOperand(reg);
  6876. CallThis(position,"Runtime","New", 1);
  6877. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6878. Emit(Result(position, pointer));
  6879. exit := NewLabel();
  6880. else := NewLabel();
  6881. BreqL(else,pointer,nil);
  6882. IF ~type.hasPointers THEN
  6883. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6884. ELSIF type IS SyntaxTree.RecordType THEN
  6885. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6886. ELSIF type IS SyntaxTree.ProcedureType THEN
  6887. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6888. ELSE
  6889. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6890. END;
  6891. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6892. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6893. 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))));
  6894. IF type IS SyntaxTree.RecordType THEN
  6895. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6896. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6897. ELSE
  6898. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6899. END;
  6900. i := openDim;
  6901. WHILE i > 0 DO
  6902. DEC (i);
  6903. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6904. END;
  6905. needsTrace := p0.NeedsTrace();
  6906. IF needsTrace THEN ModifyAssignments(true) END;
  6907. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6908. Emit(Push(position, pointer));
  6909. CallThis(position,"GarbageCollector","Watch",0);
  6910. Emit(Pop(position, pointer));
  6911. END;
  6912. Designate(p0,l);
  6913. IF needsTrace THEN
  6914. CallAssignPointer(l.op, pointer);
  6915. ModifyAssignments(false);
  6916. ELSE
  6917. ToMemory(l.op,addressType,0);
  6918. Emit(Mov(position,l.op,pointer));
  6919. END;
  6920. ReleaseIntermediateOperand(pointer);
  6921. ReleaseOperand(l);
  6922. BrL(exit);
  6923. SetLabel(else);
  6924. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6925. Designate(p0,l);
  6926. IF needsTrace THEN
  6927. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6928. ELSE
  6929. ToMemory(l.op,addressType,0);
  6930. Emit(Mov(position,l.op,pointer));
  6931. END;
  6932. ReleaseOperand(l);
  6933. SetLabel(exit);
  6934. ELSE
  6935. (*! the following code is only correct for "standard" Oberon calling convention *)
  6936. IF SemanticChecker.ContainsPointer(type) THEN
  6937. IF type IS SyntaxTree.ArrayType THEN
  6938. staticLength := 1;
  6939. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6940. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6941. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6942. END;
  6943. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6944. Emit(Mul(position,reg,reg,tmp));
  6945. END;
  6946. Designate(p0,l);
  6947. IF openDim > 0 THEN
  6948. Emit(Push(position,l.op)); (* address for use after syscall *)
  6949. END;
  6950. Emit(Push(position,l.op)); (* address *)
  6951. ReleaseOperand(l);
  6952. tmp := TypeDescriptorAdr(type);
  6953. IF ~newObjectFile THEN
  6954. IntermediateCode.MakeMemory(tmp,addressType);
  6955. END;
  6956. Emit(Push(position,tmp)); (* type descriptor *)
  6957. ReleaseIntermediateOperand(tmp);
  6958. Emit(Push(position,reg)); (* number Elements *)
  6959. ReleaseIntermediateOperand(reg);
  6960. tmp := IntermediateCode.Immediate(addressType,dim);
  6961. Emit(Push(position,tmp)); (* dimensions *)
  6962. (* push realtime flag *)
  6963. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6964. ELSE Emit(Push(position,false));
  6965. END;
  6966. CallThis(position,"Heaps","NewArr",5)
  6967. ELSE
  6968. size := ToMemoryUnits(system,system.SizeOf(type));
  6969. IF (size # 1) THEN
  6970. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6971. END;
  6972. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  6973. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  6974. Emit(Add(position,reg,reg,tmp));
  6975. Designate(p0,l);
  6976. IF openDim >0 THEN
  6977. Emit(Push(position,l.op)); (* address for use after syscall *)
  6978. END;
  6979. Emit(Push(position,l.op)); (* address for syscall *)
  6980. ReleaseOperand(l); (* pointer address *)
  6981. Emit(Push(position,reg)); (* size *)
  6982. ReleaseIntermediateOperand(reg);
  6983. (* push realtime flag *)
  6984. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6985. ELSE Emit(Push(position,false));
  6986. END;
  6987. CallThis(position,"Heaps","NewSys", 3)
  6988. END;
  6989. IF openDim > 0 THEN
  6990. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6991. Emit(Pop(position,adr));
  6992. ToMemory(adr,addressType,0);
  6993. ReuseCopy(tmp,adr);
  6994. ReleaseIntermediateOperand(adr);
  6995. adr := tmp;
  6996. else := NewLabel();
  6997. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  6998. i := openDim-1;
  6999. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7000. WHILE (i >= 0) DO
  7001. Emit(Pop(position,reg));
  7002. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7003. Emit(Mov(position,res,reg));
  7004. DEC(i);
  7005. END;
  7006. ReleaseIntermediateOperand(adr);
  7007. ReleaseIntermediateOperand(reg);
  7008. exit := NewLabel();
  7009. BrL(exit);
  7010. SetLabel(else);
  7011. (* else part: array could not be allocated *)
  7012. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7013. Emit(Add(position,sp,sp,tmp));
  7014. SetLabel(exit);
  7015. END;
  7016. END;
  7017. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7018. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7019. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7020. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7021. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7022. left.SetType(procedure.type);
  7023. formalParameter := procedureType.firstParameter;
  7024. (* push array to allocate *)
  7025. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7026. formalParameter :=formalParameter.nextParameter;
  7027. (* push length array *)
  7028. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7029. (* push size *)
  7030. type := t0;
  7031. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7032. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7033. END;
  7034. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7035. Emit(Push(position,tmp));
  7036. (* *)
  7037. IF SemanticChecker.ContainsPointer(type) THEN
  7038. tmp := TypeDescriptorAdr(type);
  7039. IF ~newObjectFile THEN
  7040. IntermediateCode.MakeMemory(tmp,addressType);
  7041. END;
  7042. ELSE
  7043. tmp := IntermediateCode.Immediate(addressType, 0);
  7044. END;
  7045. Emit(Push(position,tmp)); (* type descriptor *)
  7046. StaticCallOperand(result,procedure);
  7047. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7048. ReleaseOperand(result);
  7049. END;
  7050. (*
  7051. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7052. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7053. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7054. *)
  7055. ELSE
  7056. dim := 0;
  7057. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7058. (* generate geometry descriptor *)
  7059. Designate(p0,l);
  7060. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7061. ReleaseOperand(l);
  7062. isTensor := TRUE;
  7063. ELSE
  7064. isTensor := FALSE;
  7065. END;
  7066. FOR i := 1 TO x.parameters.Length()-1 DO
  7067. IF ~isTensor THEN
  7068. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7069. END;
  7070. parameter := x.parameters.GetExpression(i);
  7071. Evaluate(parameter,r);
  7072. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7073. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7074. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7075. END;
  7076. Emit(Push(position,r.op));
  7077. IF i=1 THEN
  7078. ReuseCopy(reg,r.op);
  7079. ELSE
  7080. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7081. END;
  7082. ReleaseOperand(r);
  7083. INC(dim);
  7084. END;
  7085. Convert(reg,addressType);
  7086. openDim := dim;
  7087. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7088. (*! the following code is only correct for "standard" Oberon calling convention *)
  7089. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7090. t := type;
  7091. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7092. staticLength := 1;
  7093. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7094. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7095. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7096. END;
  7097. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7098. Emit(Mul(position,reg,reg,tmp));
  7099. END;
  7100. Designate(p0,l);
  7101. IF isTensor THEN
  7102. Dereference(l,type,FALSE);
  7103. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7104. END;
  7105. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7106. Emit(Push(position,l.tag)); (* address *)
  7107. ReleaseOperand(l);
  7108. tmp := TypeDescriptorAdr(t);
  7109. IF ~newObjectFile THEN
  7110. IntermediateCode.MakeMemory(tmp,addressType);
  7111. END;
  7112. Emit(Push(position,tmp)); (* type descriptor *)
  7113. ReleaseIntermediateOperand(tmp);
  7114. Emit(Push(position,reg)); (* number Elements *)
  7115. ReleaseIntermediateOperand(reg);
  7116. tmp := IntermediateCode.Immediate(addressType,0);
  7117. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7118. (* push realtime flag: false by default *)
  7119. Emit(Push(position,false));
  7120. CallThis(position,"Heaps","NewArr",5);
  7121. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7122. Emit(Pop(position,adr));
  7123. GetMathArrayField(tmp,adr,MathPtrOffset);
  7124. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7125. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7126. PutMathArrayField(adr,reg,MathAdrOffset);
  7127. ReleaseIntermediateOperand(tmp);
  7128. ReleaseIntermediateOperand(reg);
  7129. ELSE
  7130. IF isTensor THEN
  7131. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7132. ELSE
  7133. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7134. END;
  7135. IF (size # 1) THEN
  7136. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7137. END;
  7138. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7139. Emit(Add(position,reg,reg,tmp));
  7140. Designate(p0,l);
  7141. IF isTensor THEN
  7142. Dereference(l,type,FALSE);
  7143. END;
  7144. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7145. Emit(Push(position,l.tag)); (* address for syscall *)
  7146. ReleaseOperand(l); (* pointer address *)
  7147. Emit(Push(position,reg)); (* size *)
  7148. ReleaseIntermediateOperand(reg);
  7149. (* push realtime flag: false by default *)
  7150. Emit(Push(position,false));
  7151. CallThis(position,"Heaps","NewSys",3);
  7152. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7153. Emit(Pop(position,adr));
  7154. GetMathArrayField(tmp,adr,MathPtrOffset);
  7155. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7156. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7157. PutMathArrayField(adr,reg,MathAdrOffset);
  7158. ReleaseIntermediateOperand(tmp);
  7159. ReleaseIntermediateOperand(reg);
  7160. END;
  7161. flags := {};
  7162. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7163. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7164. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7165. PutMathArrayField(adr,tmp,MathDimOffset);
  7166. else := NewLabel();
  7167. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7168. i := openDim-1;
  7169. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7170. IF isTensor THEN
  7171. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7172. ELSE
  7173. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7174. END;
  7175. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7176. WHILE (i >= 0) DO
  7177. Emit(Pop(position,reg));
  7178. PutMathArrayLength(adr,reg,i);
  7179. PutMathArrayIncrement(adr,tmp,i);
  7180. IF i > 0 THEN
  7181. IF i=openDim-1 THEN
  7182. ReuseCopy(tmp,tmp);
  7183. END;
  7184. Emit(Mul(position,tmp,tmp,reg));
  7185. END;
  7186. DEC(i);
  7187. END;
  7188. ReleaseIntermediateOperand(adr);
  7189. ReleaseIntermediateOperand(reg);
  7190. ReleaseIntermediateOperand(tmp);
  7191. exit := NewLabel();
  7192. BrL(exit);
  7193. SetLabel(else);
  7194. (* else part: array could not be allocated *)
  7195. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7196. Emit(Add(position,sp,sp,tmp));
  7197. SetLabel(exit);
  7198. END;
  7199. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7200. THEN
  7201. IF ~backend.cellsAreObjects THEN RETURN END;
  7202. IF InCellScope(currentScope) THEN
  7203. PushSelfPointer()
  7204. ELSE
  7205. Emit(Push(position, nil));
  7206. END;
  7207. (* push temp address *)
  7208. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7209. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7210. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7211. (* l.op contains address of pointer to record *)
  7212. Emit(Push(position,l.op)); (* address for use after syscall *)
  7213. ReleaseOperand(l);
  7214. (* push type descriptor *)
  7215. reg := TypeDescriptorAdr(baseType);
  7216. IF ~newObjectFile THEN
  7217. IntermediateCode.MakeMemory(reg,addressType);
  7218. END;
  7219. Emit(Push(position,reg));
  7220. ReleaseIntermediateOperand(reg);
  7221. (* push name *)
  7222. (*Global.GetSymbolName(p0, n);*)
  7223. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7224. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7225. ELSE
  7226. Global.GetModuleName(module.module, n);
  7227. END;
  7228. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7229. (*type.typeDeclaration.GetName(n);*)
  7230. PushConstString(n);
  7231. (* push cellnet boolean *)
  7232. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7233. (* push engine boolean *)
  7234. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7235. (* allocate *)
  7236. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7237. (* add capabilities *)
  7238. modifier := p0(SyntaxTree.Designator).modifiers;
  7239. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7240. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7241. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7242. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7243. END;
  7244. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7245. (*
  7246. modifier := baseType(SyntaxTree.CellType).modifiers;
  7247. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7248. modifier := p0(SyntaxTree.Designator).modifiers;
  7249. *)
  7250. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7251. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7252. (* l.op contains address of pointer to record *)
  7253. ToMemory(l.op,addressType,0);
  7254. (* l.op contains value of pointer to record *)
  7255. Emit(Push(position,l.op)); (* address for use after syscall *)
  7256. ReleaseOperand(l);
  7257. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7258. prevScope := currentScope;
  7259. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7260. previous := section;
  7261. section := init;
  7262. (* add ports *)
  7263. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7264. CloseInitializer(previous);
  7265. currentScope := prevScope;
  7266. Symbol(temporaryVariable,l);
  7267. ToMemory(l.op,addressType,0);
  7268. Emit(Push(position,l.op));
  7269. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7270. (*
  7271. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7272. IF constructor # NIL THEN
  7273. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7274. FOR i := 1 TO x.parameters.Length()-1 DO
  7275. p := x.parameters.GetExpression(i);
  7276. Global.GetSymbolName(parameter,name);
  7277. Evaluate(p, value);
  7278. ASSERT(value.type # NIL);
  7279. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7280. par := instance.AddParameter(name);
  7281. par.SetInteger(value.integer);
  7282. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7283. par := instance.AddParameter(name);
  7284. par.SetBoolean(value.boolean);
  7285. ELSE Error(x.position,NotYetImplemented)
  7286. END;
  7287. parameter := parameter.nextParameter
  7288. END;
  7289. END;
  7290. *)
  7291. (* call initializer *)
  7292. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7293. IF constructor # NIL THEN
  7294. (*! should be unified with ProcedureCallDesignator *)
  7295. IF backend.cellsAreObjects THEN
  7296. Symbol(temporaryVariable,l);
  7297. ToMemory(l.op,addressType,0);
  7298. Emit(Push(position,l.op));
  7299. ReleaseOperand(l);
  7300. END;
  7301. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7302. FOR i := 1 TO x.parameters.Length()-1 DO
  7303. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7304. formalParameter := formalParameter.nextParameter;
  7305. END;
  7306. (* static call of the constructor *)
  7307. Global.GetSymbolSegmentedName(constructor,name);
  7308. ASSERT(~constructor.isInline);
  7309. IF constructor.scope.ownerModule # module.module THEN
  7310. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7311. ELSE
  7312. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7313. END;
  7314. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7315. (*ELSE
  7316. ReleaseIntermediateOperand(pointer);*)
  7317. END;
  7318. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7319. ToMemory(l.op, addressType, 0);
  7320. Designate(p0,s0);
  7321. ToMemory(s0.op,addressType,0);
  7322. Emit(Mov(position,s0.op,l.op));
  7323. ReleaseOperand(l);
  7324. ReleaseOperand(s0);
  7325. result.tag := emptyOperand;
  7326. (* start *)
  7327. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7328. (* push cell *)
  7329. Symbol(temporaryVariable, l);
  7330. ToMemory(l.op,addressType,0);
  7331. Emit(Push(-1,l.op));
  7332. (* push delegate *)
  7333. Emit(Push(-1,l.op));
  7334. ReleaseOperand(l);
  7335. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7336. Emit(Push(position, s1.op));
  7337. ReleaseOperand(s1);
  7338. CallThis(position,"ActiveCellsRuntime","Start",3);
  7339. END;
  7340. (*IF temporaryVariable # NIL THEN
  7341. end := NewLabel();
  7342. BrL(end);
  7343. SetLabel(exit);
  7344. Designate(p0,l);
  7345. ToMemory(l.op,addressType,0);
  7346. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7347. ReleaseOperand(l);
  7348. SetLabel(end);
  7349. ELSE
  7350. SetLabel(exit);
  7351. END;
  7352. *)
  7353. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7354. ELSE (* no pointer to record, no pointer to array *)
  7355. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7356. (* ignore new statement *)
  7357. Warning(p0.position, "cannot run on final hardware");
  7358. ELSE
  7359. HALT(200);
  7360. END;
  7361. END;
  7362. (* ---- ADDRESSOF----- *)
  7363. |Global.systemAdr:
  7364. Designate(p0,s0);
  7365. s0.mode := ModeValue;
  7366. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7367. ReleaseIntermediateOperand(s0.op);
  7368. s0.op := s0.tag;
  7369. IntermediateCode.InitOperand(s0.tag);
  7370. END;
  7371. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7372. result := s0;
  7373. (* ---- BIT ----- *)
  7374. |Global.systemBit:
  7375. Evaluate(p0,s0);
  7376. ToMemory(s0.op,addressType,0);
  7377. ReuseCopy(res,s0.op);
  7378. ReleaseOperand(s0);
  7379. Evaluate(p1,s1);
  7380. Emit(Ror(position,res,res,s1.op));
  7381. ReleaseOperand(s1);
  7382. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7383. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7384. IF ~conditional THEN
  7385. InitOperand(result,ModeValue); result.op := res;
  7386. ELSE
  7387. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7388. BrL(falseLabel);
  7389. ReleaseIntermediateOperand(res);
  7390. END;
  7391. (* --- MSK ----*)
  7392. |Global.systemMsk:
  7393. Evaluate(p0,s0);
  7394. Evaluate(p1,s1);
  7395. ReuseCopy(res,s0.op);
  7396. ReleaseOperand(s0);
  7397. Emit(And(position,res,res,s1.op));
  7398. ReleaseOperand(s1);
  7399. InitOperand(result,ModeValue);
  7400. result.op := res;
  7401. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7402. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7403. Evaluate(p0,s0);
  7404. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7405. ReleaseOperand(s0);
  7406. InitOperand(result,ModeValue);
  7407. result.op := res;
  7408. (* ---- SYSTEM.GetStackPointer ----- *)
  7409. |Global.systemGetStackPointer:
  7410. InitOperand(result,ModeValue);
  7411. result.op := sp;
  7412. (* ---- SYSTEM.GetFramePointer ----- *)
  7413. |Global.systemGetFramePointer:
  7414. InitOperand(result,ModeValue);
  7415. result.op := fp;
  7416. (* ---- SYSTEM.GetActivity ----- *)
  7417. |Global.systemGetActivity:
  7418. ASSERT(backend.cooperative);
  7419. InitOperand(result,ModeValue);
  7420. result.op := ap;
  7421. (* ---- SYSTEM.SetStackPointer ----- *)
  7422. |Global.systemSetStackPointer:
  7423. Evaluate(p0,s0); (* *)
  7424. Emit(Mov(position,sp,s0.op));
  7425. ReleaseOperand(s0);
  7426. (* ---- SYSTEM.SetFramePointer ----- *)
  7427. |Global.systemSetFramePointer:
  7428. Evaluate(p0,s0); (* *)
  7429. Emit(Mov(position,fp,s0.op));
  7430. ReleaseOperand(s0);
  7431. (* ---- SYSTEM.Activity ----- *)
  7432. |Global.systemSetActivity:
  7433. ASSERT(backend.cooperative);
  7434. Evaluate(p0,s0); (* *)
  7435. Emit(Mov(position,ap,s0.op));
  7436. ReleaseOperand(s0);
  7437. (* ---- SYSTEM.VAL ----- *)
  7438. |Global.systemVal:
  7439. Expression(p1);
  7440. s1 := result;
  7441. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7442. IF s1.mode = ModeReference THEN
  7443. (* nothing to be done if not record type, just take over new type *)
  7444. IF (type IS SyntaxTree.RecordType) THEN
  7445. ReleaseIntermediateOperand(s1.tag);
  7446. s1.tag := TypeDescriptorAdr(type);
  7447. IF ~newObjectFile THEN
  7448. IntermediateCode.MakeMemory(s1.tag,addressType);
  7449. END;
  7450. UseIntermediateOperand(s1.tag);
  7451. END;
  7452. result := s1;
  7453. ELSE (* copy over result to different type, may not use convert *)
  7454. itype := IntermediateCode.GetType(system,type);
  7455. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7456. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7457. Emit(Mov(position,s0.op,s1.op));
  7458. ReleaseOperand(s1);
  7459. InitOperand(result,ModeValue);
  7460. result.op := s0.op;
  7461. ELSE (* different size, must convert *)
  7462. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7463. Convert(s1.op, IntermediateCode.GetType(system,type));
  7464. result := s1;
  7465. END;
  7466. END;
  7467. (* ---- SYSTEM.GET ----- *)
  7468. |Global.systemGet:
  7469. Evaluate(p0,s0); (* adr *)
  7470. Designate(p1,s1); (* variable *)
  7471. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7472. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7473. Emit(Mov(position,s1.op,s0.op));
  7474. ReleaseOperand(s1);
  7475. ReleaseOperand(s0);
  7476. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7477. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7478. Evaluate(p0,s0); (* *)
  7479. Evaluate(p1,s1); (* variable *)
  7480. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7481. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7482. (* real part *)
  7483. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7484. Emit(Mov(position,res, s1.op));
  7485. ReleaseIntermediateOperand(res);
  7486. (* imaginary part *)
  7487. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7488. Emit(Mov(position,res, s1.tag));
  7489. ReleaseIntermediateOperand(res);
  7490. ReleaseOperand(s1);
  7491. ReleaseOperand(s0);
  7492. ELSE
  7493. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7494. ReleaseOperand(s0);
  7495. Emit(Mov(position,res,s1.op));
  7496. ReleaseIntermediateOperand(res);
  7497. ReleaseOperand(s1);
  7498. END;
  7499. (* ---- SYSTEM.MOVE ----- *)
  7500. |Global.systemMove:
  7501. Evaluate(p0,s0);
  7502. Evaluate(p1,s1);
  7503. Evaluate(p2,s2);
  7504. Emit(Copy(position,s1.op,s0.op,s2.op));
  7505. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7506. (* ---- SYSTEM.NEW ----- *)
  7507. |Global.systemNew:
  7508. Designate(p0,s0);
  7509. Emit(Push(position,s0.op));
  7510. ReleaseOperand(s0);
  7511. Evaluate(p1,s1);
  7512. Emit(Push(position,s1.op));
  7513. ReleaseOperand(s1);
  7514. (* push realtime flag: false by default *)
  7515. Emit(Push(position,false));
  7516. CallThis(position,"Heaps","NewSys",3);
  7517. (* ---- SYSTEM.CALL ----- *)
  7518. |Global.systemRef:
  7519. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7520. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7521. s0.mode := ModeValue;
  7522. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7523. result := s0
  7524. (* ---- INCR ----- *)
  7525. |Global.Incr:
  7526. Designate(p0,operand);
  7527. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7528. Dereference(operand,p0.type.resolved,FALSE);
  7529. END;
  7530. ASSERT(p1 # NIL);
  7531. Evaluate(p1,l);
  7532. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7533. ReleaseOperand(operand); ReleaseOperand(l);
  7534. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7535. (* ---- SUM ----- *)
  7536. |Global.Sum: HALT(200);
  7537. (* ---- CAS ----- *)
  7538. |Global.Cas:
  7539. needsTrace := p0.NeedsTrace();
  7540. IF needsTrace THEN ModifyAssignments(true) END;
  7541. Designate(p0,s0);
  7542. Evaluate(p1,s1);
  7543. Evaluate(p2,s2);
  7544. IF needsTrace THEN
  7545. Emit(Push(position, s0.op));
  7546. Emit(Push(position, s1.op));
  7547. Emit(Push(position, s2.op));
  7548. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7549. ELSE
  7550. Emit(Cas(position,s0.op,s1.op,s2.op));
  7551. END;
  7552. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7553. IF needsTrace THEN ModifyAssignments(false) END;
  7554. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7555. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7556. IF conditional THEN
  7557. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7558. BrL(falseLabel);
  7559. ReleaseIntermediateOperand(res);
  7560. END;
  7561. (* ---- DIM ----- *)
  7562. |Global.Dim:
  7563. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7564. Designate(p0,s0);
  7565. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7566. Dereference(s0,p0.type.resolved,FALSE);
  7567. END;
  7568. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7569. ReleaseOperand(s0);
  7570. (* ---- RESHAPE ----- *)
  7571. |Global.Reshape:
  7572. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7573. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7574. left.SetType(procedure.type);
  7575. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7576. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7577. END;
  7578. (* ---- SYSTEM.TYPECODE ----- *)
  7579. |Global.systemTypeCode:
  7580. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7581. IF type.resolved IS SyntaxTree.PointerType THEN
  7582. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7583. END;
  7584. result.op := TypeDescriptorAdr(type);
  7585. IF ~newObjectFile THEN
  7586. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7587. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7588. END;
  7589. result.mode := ModeValue;
  7590. (* ---- SYSTEM.TRACE ----- *)
  7591. |Global.systemTrace:
  7592. SystemTrace(x.parameters, x.position);
  7593. (* ----- CONNECT ------*)
  7594. |Global.Connect:
  7595. IF backend.cellsAreObjects THEN
  7596. PushPort(p0);
  7597. PushPort(p1);
  7598. IF p2 # NIL THEN
  7599. Evaluate(p2, s2);
  7600. Emit(Push(p2.position, s2.op));
  7601. ReleaseOperand(s2);
  7602. ELSE
  7603. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7604. END;
  7605. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7606. ELSE
  7607. Warning(x.position, "cannot run on final hardware");
  7608. END;
  7609. (* ----- DELEGATE ------*)
  7610. |Global.Delegate:
  7611. IF backend.cellsAreObjects THEN
  7612. PushPort(p0);
  7613. PushPort(p1);
  7614. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7615. ELSE
  7616. Warning(x.position, "cannot run on final hardware");
  7617. END;
  7618. (* ----- SEND ------*)
  7619. |Global.Send:
  7620. Evaluate(p0,s0);
  7621. Evaluate(p1,s1);
  7622. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7623. Emit(Push(position,s0.op));
  7624. Emit(Push(position,s1.op));
  7625. (*
  7626. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7627. *)
  7628. IF ~backend.cellsAreObjects THEN
  7629. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7630. END;
  7631. ReleaseOperand(s0);
  7632. ReleaseOperand(s1);
  7633. IF backend.cellsAreObjects THEN
  7634. CallThis(position,"ActiveCellsRuntime","Send",2);
  7635. ELSE
  7636. CallThis(position,ChannelModuleName,"Send",2);
  7637. END;
  7638. (* ----- RECEIVE ------*)
  7639. |Global.Receive:
  7640. Evaluate(p0,s0);
  7641. Emit(Push(position,s0.op));
  7642. Designate(p1,s1);
  7643. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7644. Emit(Push(position,s1.op));
  7645. IF p2 # NIL THEN
  7646. Designate(p2,s2);
  7647. Emit(Push(position,s2.op));
  7648. END;
  7649. (*
  7650. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7651. *)
  7652. IF ~backend.cellsAreObjects THEN
  7653. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7654. END;
  7655. ReleaseOperand(s0);
  7656. ReleaseOperand(s1);
  7657. ReleaseOperand(s2);
  7658. IF backend.cellsAreObjects THEN
  7659. IF p2 = NIL THEN
  7660. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7661. ELSE
  7662. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7663. END;
  7664. ELSE
  7665. IF p2 = NIL THEN
  7666. CallThis(position,ChannelModuleName,"Receive",2)
  7667. ELSE
  7668. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7669. END;
  7670. END;
  7671. | Global.systemSpecial:
  7672. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7673. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7674. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7675. (* determine if parameters are of the VAR kind *)
  7676. ASSERT(x.parameters.Length() <= 3);
  7677. formalParameter := procedureType.firstParameter;
  7678. FOR i := 0 TO x.parameters.Length() - 1 DO
  7679. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7680. formalParameter := formalParameter.nextParameter
  7681. END;
  7682. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7683. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7684. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7685. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7686. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7687. IF procedureType.returnType # NIL THEN
  7688. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7689. Emit(Result(position, res));
  7690. (*InitOperand(result,ModeValue);
  7691. result.op := res;
  7692. *)
  7693. IF ~conditional THEN
  7694. InitOperand(result,ModeValue); result.op := res;
  7695. ELSE
  7696. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7697. BrL(falseLabel);
  7698. ReleaseIntermediateOperand(res);
  7699. END;
  7700. END
  7701. ELSE (* function not yet implemented *)
  7702. Error(position,"not yet implemented");
  7703. END;
  7704. destination := dest;
  7705. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7706. END VisitBuiltinCallDesignator;
  7707. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7708. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7709. BEGIN
  7710. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7711. dest := destination; destination := emptyOperand;
  7712. Expression(x.left);
  7713. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7714. ELSIF isUnchecked THEN (* no check *)
  7715. ELSE
  7716. trueL := NewLabel();
  7717. falseL := NewLabel();
  7718. IF IsPointerToRecord(x.left.type,recordType) THEN
  7719. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7720. Emit(Mov(position,tag, result.op));
  7721. IF result.mode # ModeValue THEN
  7722. ptr := tag;
  7723. IntermediateCode.MakeMemory(ptr,addressType);
  7724. Emit(Mov(position,tag, ptr));
  7725. END;
  7726. IF ~backend.cooperative THEN
  7727. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7728. END;
  7729. IntermediateCode.MakeMemory(tag,addressType);
  7730. ELSE
  7731. tag := result.tag;
  7732. END;
  7733. TypeTest(tag,x.type,trueL,falseL);
  7734. ReleaseIntermediateOperand(tag);
  7735. SetLabel(falseL);
  7736. EmitTrap(position,TypeCheckTrap);
  7737. SetLabel(trueL);
  7738. END;
  7739. destination := dest;
  7740. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7741. END VisitTypeGuardDesignator;
  7742. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7743. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7744. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7745. VAR label: Label; pc: LONGINT;
  7746. BEGIN
  7747. IF backend.cooperative & ~isUnchecked THEN
  7748. pc := section.pc;
  7749. label := NewLabel();
  7750. BrneL(label, operand.op, nil);
  7751. EmitTrap(position, NilPointerTrap);
  7752. SetLabel(label);
  7753. INC(statCoopNilCheck, section.pc - pc);
  7754. END;
  7755. END NilCheck;
  7756. BEGIN
  7757. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7758. ReuseCopy(dereferenced,operand.op);
  7759. ReleaseOperand(operand);
  7760. operand.mode := ModeReference;
  7761. operand.op := dereferenced;
  7762. operand.tag := dereferenced;
  7763. UseIntermediateOperand(operand.tag);
  7764. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7765. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7766. ReleaseIntermediateOperand(operand.tag);
  7767. operand.tag := TypeDescriptorAdr(type);
  7768. IF ~newObjectFile THEN
  7769. IntermediateCode.MakeMemory(operand.tag,addressType);
  7770. END;
  7771. ELSE
  7772. IF ~backend.cooperative THEN
  7773. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7774. END;
  7775. IntermediateCode.MakeMemory(operand.tag,addressType);
  7776. END;
  7777. NilCheck(operand.op);
  7778. ELSIF type IS SyntaxTree.ArrayType THEN
  7779. IF isUnsafe THEN
  7780. NilCheck(operand.op);
  7781. ReleaseIntermediateOperand(operand.tag);
  7782. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7783. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7784. ELSE
  7785. operand.tag := emptyOperand;
  7786. END;
  7787. ELSE
  7788. NilCheck(operand.op);
  7789. IF backend.cooperative THEN
  7790. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7791. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7792. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7793. ELSE
  7794. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7795. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7796. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7797. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7798. END;
  7799. END;
  7800. ELSIF type IS SyntaxTree.MathArrayType THEN
  7801. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7802. IntermediateCode.MakeMemory(operand.op,addressType);
  7803. ELSE HALT(100);
  7804. END;
  7805. END Dereference;
  7806. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7807. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7808. BEGIN
  7809. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7810. dest := destination; destination := emptyOperand;
  7811. Evaluate(x.left,d);
  7812. type := x.type.resolved;
  7813. prevIsUnchecked := isUnchecked;
  7814. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7815. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7816. END;
  7817. Dereference(d,type,IsUnsafePointer(x.left.type));
  7818. isUnchecked := prevIsUnchecked;
  7819. result := d;
  7820. 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
  7821. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7822. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7823. END;
  7824. END;
  7825. destination := dest;
  7826. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7827. END VisitDereferenceDesignator;
  7828. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7829. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7830. BEGIN
  7831. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7832. dest := destination; destination := emptyOperand;
  7833. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7834. tag := result.op;
  7835. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7836. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7837. StaticCallOperand(result,procedure.super);
  7838. ReleaseIntermediateOperand(result.tag);
  7839. UseIntermediateOperand(tag); (* necessary ? *)
  7840. result.tag := tag;
  7841. destination := dest;
  7842. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7843. END VisitSupercallDesignator;
  7844. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7845. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7846. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7847. name: Basic.SegmentedName;
  7848. BEGIN
  7849. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7850. dest := destination; destination := emptyOperand;
  7851. scope := currentScope;
  7852. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7853. scope := scope.outerScope;
  7854. END;
  7855. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7856. IF newObjectFile THEN
  7857. moduleSection := meta.ModuleSection();
  7858. IF backend.cooperative THEN
  7859. moduleOffset := 0;
  7860. ELSE
  7861. moduleOffset := moduleSection.pc;
  7862. END;
  7863. result.mode := ModeValue;
  7864. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7865. ELSE
  7866. Symbol(moduleSelf,result);
  7867. IntermediateCode.MakeMemory(result.op,addressType);
  7868. END
  7869. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7870. result.mode := ModeValue;
  7871. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7872. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7873. ELSE
  7874. GetBaseRegister(basereg,currentScope,scope);
  7875. InitOperand(result,ModeReference);
  7876. result.op := basereg;
  7877. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7878. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7879. IF backend.cooperative THEN
  7880. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7881. END;
  7882. (* tag must be loaded when dereferencing SELF pointer *)
  7883. END;
  7884. destination := dest;
  7885. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7886. END VisitSelfDesignator;
  7887. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7888. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7889. BEGIN
  7890. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7891. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7892. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7893. parameter := procedureType.returnParameter;
  7894. VisitParameter(parameter);
  7895. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7896. END VisitResultDesignator;
  7897. (** values *)
  7898. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7899. BEGIN
  7900. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7901. IF conditional THEN
  7902. IF x.value THEN BrL(trueLabel)
  7903. ELSE BrL(falseLabel)
  7904. END;
  7905. ELSE
  7906. InitOperand(result,ModeValue);
  7907. IF x.value THEN result.op := true ELSE result.op := false END;
  7908. END;
  7909. END VisitBooleanValue;
  7910. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7911. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7912. BEGIN
  7913. Global.GetModuleSegmentedName(module.module, name);
  7914. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7915. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7916. RETURN section
  7917. END GetDataSection;
  7918. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7919. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7920. BEGIN
  7921. type := vop.type;
  7922. data := GetDataSection();
  7923. pc := EnterImmediate(data,vop);
  7924. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7925. IntermediateCode.MakeMemory(vop, type);
  7926. END GetImmediateMem;
  7927. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7928. BEGIN
  7929. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7930. InitOperand(result,ModeValue);
  7931. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7932. IF ~supportedImmediate(result.op) &~inData THEN
  7933. GetImmediateMem(result.op)
  7934. END;
  7935. END VisitIntegerValue;
  7936. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7937. BEGIN
  7938. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7939. InitOperand(result,ModeValue);
  7940. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7941. END VisitCharacterValue;
  7942. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7943. BEGIN
  7944. IF Trace THEN TraceEnter("VisitSetValue") END;
  7945. InitOperand(result,ModeValue);
  7946. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7947. END VisitSetValue;
  7948. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7949. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7950. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7951. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7952. BEGIN
  7953. numberElements := x.elements.Length();
  7954. FOR i := 0 TO numberElements-1 DO
  7955. expression := x.elements.GetExpression(i);
  7956. IF expression IS SyntaxTree.MathArrayExpression THEN
  7957. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7958. ELSE
  7959. inData := TRUE;
  7960. Evaluate(expression,op);
  7961. irv.Emit(Data(position,op.op));
  7962. inData := FALSE;
  7963. ReleaseOperand(op);
  7964. END;
  7965. END;
  7966. END RecursiveData;
  7967. BEGIN
  7968. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  7969. IF ~TryConstantDeclaration() THEN
  7970. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  7971. GetCodeSectionNameForSymbol(constantDeclaration,name);
  7972. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  7973. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  7974. ELSE
  7975. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  7976. END;
  7977. RecursiveData(x.array);
  7978. InitOperand(result,ModeReference);
  7979. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  7980. END
  7981. END VisitMathArrayValue;
  7982. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  7983. VAR constant: Sections.Section;
  7984. BEGIN
  7985. IF constantDeclaration = NIL THEN
  7986. RETURN FALSE
  7987. ELSE
  7988. (* Is a constant in this module: did we generate it already? *)
  7989. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  7990. IF constant # NIL THEN
  7991. InitOperand(result,ModeReference);
  7992. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  7993. RETURN TRUE;
  7994. END;
  7995. END;
  7996. RETURN FALSE
  7997. END TryConstantDeclaration;
  7998. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  7999. BEGIN
  8000. constantDeclaration := x;
  8001. x.value.resolved.Accept(SELF);
  8002. END VisitConstant;
  8003. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8004. BEGIN
  8005. IF Trace THEN TraceEnter("VisitRealValue") END;
  8006. InitOperand(result,ModeValue);
  8007. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8008. END VisitRealValue;
  8009. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8010. VAR
  8011. componentType: SyntaxTree.Type;
  8012. BEGIN
  8013. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8014. ASSERT(x.type IS SyntaxTree.ComplexType);
  8015. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8016. InitOperand(result,ModeValue);
  8017. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8018. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8019. END VisitComplexValue;
  8020. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8021. VAR i: LONGINT; name: Basic.SegmentedName;
  8022. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8023. BEGIN
  8024. IF Trace THEN TraceEnter("VisitStringValue") END;
  8025. IF ~TryConstantDeclaration() THEN
  8026. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8027. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8028. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8029. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8030. ELSE
  8031. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8032. END;
  8033. FOR i := 0 TO x.length-1 DO
  8034. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8035. irv.Emit(Data(position,op));
  8036. END;
  8037. InitOperand(result,ModeReference);
  8038. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8039. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8040. END
  8041. END VisitStringValue;
  8042. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8043. BEGIN
  8044. IF Trace THEN TraceEnter("VisitNilValue") END;
  8045. InitOperand(result,ModeValue);
  8046. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8047. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8048. END VisitNilValue;
  8049. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8050. BEGIN
  8051. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8052. InitOperand(result,ModeValue);
  8053. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8054. END VisitEnumerationValue;
  8055. (** symbols *)
  8056. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8057. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8058. END VisitImport;
  8059. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8060. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8061. BEGIN
  8062. IF scope # baseScope THEN
  8063. (* left := [fp+8] *)
  8064. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8065. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8066. ReuseCopy(left,right);
  8067. ReleaseIntermediateOperand(right);
  8068. scope := scope.outerScope; DEC(level);
  8069. (* { left := [left+8] } *)
  8070. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8071. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8072. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8073. Emit(Mov(position,left,right));
  8074. scope := scope.outerScope; DEC(level);
  8075. END;
  8076. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8077. result := left;
  8078. ELSE
  8079. result := fp;
  8080. END;
  8081. END GetBaseRegister;
  8082. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8083. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8084. BEGIN
  8085. IF Trace THEN TraceEnter("VisitVariable"); END;
  8086. type := x.type.resolved;
  8087. IF (x.useRegister) THEN
  8088. InitOperand(result, ModeValue);
  8089. IF x.registerNumber < 0 THEN
  8090. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8091. reg := x.registerNumber;
  8092. ELSE
  8093. reg := registerUsageCount.Map(x.registerNumber);
  8094. UseRegister(reg);
  8095. END;
  8096. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8097. ELSIF x.externalName # NIL THEN
  8098. InitOperand(result,ModeReference);
  8099. Basic.ToSegmentedName(x.externalName^, name);
  8100. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8101. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8102. InitOperand(result,ModeReference);
  8103. GetBaseRegister(result.op,currentScope,x.scope);
  8104. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8105. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8106. InitOperand(result,ModeReference);
  8107. GetCodeSectionNameForSymbol(x,name);
  8108. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8109. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8110. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8111. InitOperand(result,ModeReference);
  8112. GetCodeSectionNameForSymbol(x,name);
  8113. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8114. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8115. ELSE (* field, left designator must have been emitted *)
  8116. ASSERT(result.mode = ModeReference);
  8117. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8118. ReuseCopy(temp,result.op);
  8119. ReleaseIntermediateOperand(result.op);
  8120. result.op := temp;
  8121. END;
  8122. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8123. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8124. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8125. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8126. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8127. END;
  8128. END;
  8129. END;
  8130. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8131. ValueToCondition(result);
  8132. ELSIF type IS SyntaxTree.ProcedureType THEN
  8133. ReleaseIntermediateOperand(result.tag);
  8134. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8135. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8136. UseIntermediateOperand(result.tag);
  8137. ELSE
  8138. result.tag := nil; (* nil *)
  8139. END;
  8140. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8141. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8142. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8143. ReleaseIntermediateOperand(result.tag);
  8144. Global.GetSymbolSegmentedName(x,name);
  8145. Basic.AppendToSegmentedName(name,"@len");
  8146. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8147. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8148. ELSE
  8149. END;
  8150. ELSE
  8151. ReleaseIntermediateOperand(result.tag);
  8152. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8153. END;
  8154. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8155. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8156. ReleaseIntermediateOperand(result.tag);
  8157. result.tag := result.op;
  8158. UseIntermediateOperand(result.tag);
  8159. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8160. IntermediateCode.MakeMemory(result.op,addressType);
  8161. END;
  8162. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8163. ReleaseIntermediateOperand(result.tag);
  8164. result.tag := TypeDescriptorAdr(type);
  8165. IF ~newObjectFile THEN
  8166. IntermediateCode.MakeMemory(result.tag,addressType);
  8167. END;
  8168. UseIntermediateOperand(result.tag);
  8169. (* tag for pointer type computed not here but during dereferencing *)
  8170. END;
  8171. IF Trace THEN TraceExit("VisitVariable") END;
  8172. END VisitVariable;
  8173. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8174. BEGIN
  8175. VisitVariable(property);
  8176. END VisitProperty;
  8177. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8178. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8179. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8180. BEGIN
  8181. type := x.type.resolved;
  8182. IF Trace THEN TraceEnter("VisitParameter") END;
  8183. IF x.ownerType IS SyntaxTree.CellType THEN
  8184. ptype := x.type.resolved;
  8185. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8186. IF ~(ptype IS SyntaxTree.PortType) THEN
  8187. InitOperand(result,ModeReference);
  8188. GetCodeSectionNameForSymbol(x,name);
  8189. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8190. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8191. RETURN
  8192. ELSE
  8193. InitOperand(result, ModeValue);
  8194. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8195. adr := 0;
  8196. WHILE parameter # x DO
  8197. parameterType := parameter.type;
  8198. inc := 1;
  8199. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8200. INC(adr, inc);
  8201. parameter := parameter.nextParameter
  8202. END;
  8203. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8204. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8205. RETURN
  8206. END;
  8207. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8208. InitOperand(result,ModeReference);
  8209. GetCodeSectionNameForSymbol(x,name);
  8210. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8211. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8212. RETURN
  8213. ELSE
  8214. GetBaseRegister(basereg,currentScope,x.scope);
  8215. InitOperand(result,ModeReference);
  8216. result.op := basereg;
  8217. END;
  8218. IF IsOpenArray(type) THEN
  8219. result.tag := basereg;
  8220. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8221. IntermediateCode.MakeMemory(result.op,addressType);
  8222. IF Global.IsOberonProcedure(x.ownerType) THEN
  8223. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8224. UseIntermediateOperand(result.tag);
  8225. ELSE
  8226. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8227. END;
  8228. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8229. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8230. ELSIF IsStaticArray(type) THEN
  8231. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8232. IntermediateCode.MakeMemory(result.op,addressType);
  8233. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8234. ELSIF type IS SyntaxTree.MathArrayType THEN
  8235. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8236. WITH type: SyntaxTree.MathArrayType DO
  8237. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8238. IF type.form = SyntaxTree.Tensor THEN
  8239. ELSIF type.form = SyntaxTree.Open THEN
  8240. result.tag := result.op;
  8241. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8242. IntermediateCode.MakeMemory(result.op,addressType);
  8243. UseIntermediateOperand(result.tag);
  8244. ELSIF type.form = SyntaxTree.Static THEN
  8245. IF x.kind = SyntaxTree.ConstParameter THEN
  8246. IntermediateCode.MakeMemory(result.op,addressType);
  8247. END;
  8248. ELSE HALT(100)
  8249. END;
  8250. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8251. IF type.form = SyntaxTree.Tensor THEN
  8252. ToMemory(result.op,addressType,0);
  8253. ELSIF type.form = SyntaxTree.Open THEN
  8254. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8255. ReuseCopy(result.tag, mem);
  8256. ReleaseIntermediateOperand(mem);
  8257. ReleaseIntermediateOperand(result.op);
  8258. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8259. ELSIF type.form = SyntaxTree.Static THEN
  8260. IntermediateCode.MakeMemory(result.op,addressType);
  8261. ELSE HALT(100)
  8262. END;
  8263. ELSE HALT(100)
  8264. END;
  8265. END;
  8266. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8267. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8268. IntermediateCode.MakeMemory(result.op,addressType);
  8269. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8270. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8271. END;
  8272. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8273. ValueToCondition(result);
  8274. ELSIF type IS SyntaxTree.ProcedureType THEN
  8275. ReleaseIntermediateOperand(result.tag);
  8276. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8277. IF x.kind = SyntaxTree.VarParameter THEN
  8278. ReuseCopy(result.tag,result.op);
  8279. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8280. IntermediateCode.MakeMemory(result.tag,addressType);
  8281. ELSE
  8282. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8283. UseIntermediateOperand(result.tag);
  8284. END;
  8285. ELSE
  8286. result.tag := nil;
  8287. END;
  8288. (* tag for pointer type computed not here but during dereferencing *)
  8289. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8290. ReleaseIntermediateOperand(result.tag);
  8291. result.tag := basereg;
  8292. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8293. IntermediateCode.MakeMemory(result.tag,addressType);
  8294. UseIntermediateOperand(result.tag);
  8295. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8296. ReleaseIntermediateOperand(result.tag);
  8297. result.tag := TypeDescriptorAdr(type);
  8298. IF ~newObjectFile THEN
  8299. IntermediateCode.MakeMemory(result.tag,addressType);
  8300. END;
  8301. UseIntermediateOperand(result.tag);
  8302. END;
  8303. IF Trace THEN TraceExit("VisitParameter") END;
  8304. END VisitParameter;
  8305. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8306. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8307. BEGIN
  8308. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8309. (* left.p: left already emitted *)
  8310. tag := result.op; (* value of pointer to left *)
  8311. (* get type desc *)
  8312. tmp := result.tag;
  8313. IntermediateCode.MakeMemory(tmp,addressType);
  8314. (* get method adr *)
  8315. Reuse1(reg,tmp);
  8316. ReleaseIntermediateOperand(tmp);
  8317. IF backend.cooperative THEN
  8318. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8319. WHILE recordType.baseType # NIL DO
  8320. recordType := recordType.GetBaseRecord ();
  8321. END;
  8322. GetRecordTypeName (recordType,name);
  8323. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8324. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8325. offset := 0;
  8326. ELSE
  8327. offset := 2;
  8328. END;
  8329. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8330. ELSE
  8331. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8332. END;
  8333. InitOperand(operand,ModeReference);
  8334. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8335. operand.op := reg;
  8336. operand.tag := tag;
  8337. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8338. END DynamicCallOperand;
  8339. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8340. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8341. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8342. BEGIN
  8343. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8344. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8345. tag := operand.op;
  8346. ReleaseIntermediateOperand(operand.tag);
  8347. ELSE tag := nil
  8348. END;
  8349. IF x.isInline THEN
  8350. sectionType := Sections.InlineCodeSection;
  8351. ELSE
  8352. sectionType := Sections.CodeSection;
  8353. END;
  8354. IF x.externalName # NIL THEN
  8355. Basic.ToSegmentedName(x.externalName^, name);
  8356. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8357. ELSE
  8358. GetCodeSectionNameForSymbol(x, name);
  8359. IF (x.scope.ownerModule = module.module) THEN
  8360. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8361. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8362. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8363. IF source.pc = 0 THEN (* no code yet *)
  8364. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8365. END;
  8366. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8367. bits := x.procedureScope.body.code.inlineCode;
  8368. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8369. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8370. binary.CopyBits(bits, 0, bits.GetSize());
  8371. source.SetResolved(binary);
  8372. ELSE
  8373. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8374. END;
  8375. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8376. END;
  8377. InitOperand(operand,ModeValue);
  8378. operand.op := reg;
  8379. operand.tag := tag;
  8380. IF Trace THEN TraceExit("StaticCallOperand") END;
  8381. END StaticCallOperand;
  8382. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8383. (* handle expressions of the form designator.procedure or procedure *)
  8384. BEGIN
  8385. IF Trace THEN TraceEnter("VisitProcedure") END;
  8386. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8387. DynamicCallOperand(result,x);
  8388. ELSIF x.isInline THEN
  8389. StaticCallOperand(result,x);
  8390. ELSE
  8391. StaticCallOperand(result,x);
  8392. END;
  8393. IF Trace THEN TraceExit("VisitProcedure") END;
  8394. END VisitProcedure;
  8395. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8396. BEGIN
  8397. VisitProcedure(x);
  8398. END VisitOperator;
  8399. (** statements *)
  8400. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8401. BEGIN
  8402. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8403. Expression(x.call);
  8404. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8405. ReleaseOperand(result)
  8406. END;
  8407. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8408. END VisitProcedureCallStatement;
  8409. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8410. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8411. leftBase, rightBase: SyntaxTree.Type;
  8412. procedureName,s: SyntaxTree.IdentifierString;
  8413. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8414. size: LONGINT; rightKind: LONGINT;
  8415. dummy: IntermediateCode.Operand;
  8416. CONST moduleName = "FoxArrayBase";
  8417. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8418. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8419. BEGIN
  8420. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8421. IF base IS SyntaxTree.MathArrayType THEN
  8422. base := OpenArray(base(SyntaxTree.MathArrayType));
  8423. END;
  8424. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8425. result.SetArrayBase(base);
  8426. RETURN result
  8427. END OpenArray;
  8428. BEGIN
  8429. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8430. SaveRegisters();ReleaseUsedRegisters(saved);
  8431. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8432. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8433. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8434. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8435. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8436. IF leftType.form = SyntaxTree.Tensor THEN
  8437. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8438. ELSIF leftType.form = SyntaxTree.Open THEN
  8439. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8440. ELSIF leftType.form = SyntaxTree.Static THEN
  8441. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8442. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8443. END;
  8444. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8445. IF procedure = NIL THEN
  8446. s := "Instruction not supported on target, emulation procedure ";
  8447. Strings.Append(s,moduleName);
  8448. Strings.Append(s,".");
  8449. Strings.Append(s,procedureName);
  8450. Strings.Append(s," not present");
  8451. Error(position,s);
  8452. ELSE
  8453. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8454. parameter.SetType(leftType);
  8455. parameter.SetAccess(SyntaxTree.Internal);
  8456. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8457. parameter.SetKind(rightKind);
  8458. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8459. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8460. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8461. StaticCallOperand(result,procedure);
  8462. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8463. ReleaseOperand(result);
  8464. END;
  8465. RestoreRegisters(saved);
  8466. END;
  8467. END AssignMathArray;
  8468. VAR modifyAssignmentCounter := 0: LONGINT;
  8469. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8470. VAR processor,mem,dst: IntermediateCode.Operand;
  8471. BEGIN
  8472. IF value.intValue = true.intValue THEN
  8473. INC(modifyAssignmentCounter);
  8474. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8475. modifyAssignmentsPC := section.pc;
  8476. ELSE
  8477. DEC(modifyAssignmentCounter);
  8478. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8479. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8480. END;
  8481. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8482. dst := NewRegisterOperand (addressType);
  8483. Emit(Mov(position,dst, processor));
  8484. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8485. Emit(Mov(position,mem, value));
  8486. ReleaseIntermediateOperand(dst);
  8487. END ModifyAssignments;
  8488. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8489. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8490. BEGIN
  8491. type := left.type.resolved;
  8492. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8493. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8494. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8495. IF procedureType.returnParameter = parameter THEN
  8496. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8497. END;
  8498. END;
  8499. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8500. END CopySize;
  8501. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8502. VAR
  8503. leftO, rightO: Operand;
  8504. mem, sizeOp: IntermediateCode.Operand;
  8505. leftType, rightType, componentType: SyntaxTree.Type;
  8506. size: LONGINT;
  8507. parameters: SyntaxTree.ExpressionList;
  8508. procedure: SyntaxTree.Procedure;
  8509. call: SyntaxTree.ProcedureCallDesignator;
  8510. designator: SyntaxTree.Designator;
  8511. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8512. VAR procedureType: SyntaxTree.ProcedureType;
  8513. BEGIN
  8514. IF ReturnedAsParameter(right.type) THEN
  8515. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8516. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8517. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8518. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8519. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8520. IF right.id = Global.Reshape THEN RETURN TRUE
  8521. END;
  8522. END;
  8523. END;
  8524. END;
  8525. RETURN FALSE
  8526. END CanPassAsResultParameter;
  8527. BEGIN
  8528. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8529. leftType := left.type.resolved; rightType:= right.type.resolved;
  8530. IF backend.cooperative & left.NeedsTrace() THEN
  8531. ModifyAssignments(true);
  8532. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8533. Designate(right, rightO);
  8534. Designate(left, leftO);
  8535. ASSERT(leftO.mode = ModeReference);
  8536. TransferToRegister(leftO.op, leftO.op);
  8537. TransferToRegister(rightO.op, rightO.op);
  8538. Emit(Push(position, leftO.op));
  8539. Emit(Push(position, rightO.op));
  8540. CallAssignMethod(leftO.op, rightO.op, left.type);
  8541. Emit(Pop(position, rightO.op));
  8542. Emit(Pop(position, leftO.op));
  8543. sizeOp := CopySize(left);
  8544. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8545. ReleaseOperand(leftO);
  8546. ReleaseOperand(rightO);
  8547. ELSE
  8548. Evaluate(right,rightO);
  8549. Designate(left,leftO);
  8550. ASSERT(leftO.mode = ModeReference);
  8551. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8552. (* copy procedure address first *)
  8553. MakeMemory(mem,leftO.op,addressType,0);
  8554. Emit(Mov(position,mem,rightO.op));
  8555. ReleaseIntermediateOperand(mem);
  8556. (* copy pointer address *)
  8557. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8558. CallAssignPointer(leftO.tag, rightO.tag);
  8559. ELSE
  8560. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8561. CallAssignPointer(leftO.op, rightO.op);
  8562. END;
  8563. ReleaseOperand(leftO);
  8564. ReleaseOperand(rightO);
  8565. END;
  8566. ModifyAssignments(false);
  8567. RETURN;
  8568. END;
  8569. IF CanPassAsResultParameter(right) THEN
  8570. procedureResultDesignator := left(SyntaxTree.Designator);
  8571. Expression(right);
  8572. procedureResultDesignator := NIL;
  8573. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8574. (* left <-- ALIAS OF right: zerocopy *)
  8575. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8576. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8577. designator.SetType(procedure.type);
  8578. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8579. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8580. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8581. END;
  8582. ELSIF leftType IS SyntaxTree.RangeType THEN
  8583. (* LHS is of array range type *)
  8584. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8585. Evaluate(right, rightO);
  8586. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8587. (* first *)
  8588. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8589. Emit(Mov(position,mem, rightO.op));
  8590. ReleaseIntermediateOperand(mem);
  8591. (* last *)
  8592. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8593. Emit(Mov(position,mem, rightO.tag));
  8594. ReleaseIntermediateOperand(mem);
  8595. (* step *)
  8596. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8597. Emit(Mov(position,mem, rightO.extra));
  8598. ReleaseIntermediateOperand(mem);
  8599. ReleaseOperand(rightO);
  8600. ReleaseOperand(leftO)
  8601. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8602. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8603. Evaluate(right, rightO);
  8604. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8605. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8606. (* real part *)
  8607. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8608. Emit(Mov(position,mem, rightO.op));
  8609. ReleaseIntermediateOperand(mem);
  8610. (* imaginary part *)
  8611. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8612. Emit(Mov(position,mem, rightO.tag));
  8613. ReleaseIntermediateOperand(mem);
  8614. ReleaseOperand(rightO);
  8615. ReleaseOperand(leftO)
  8616. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8617. OR (leftType IS SyntaxTree.PortType) THEN
  8618. (* rightO := leftO;*)
  8619. Evaluate(right,rightO);
  8620. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8621. Designate(left,leftO);
  8622. IF leftO.mode = ModeReference THEN
  8623. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8624. destination := mem;
  8625. ELSE
  8626. destination := leftO.op;
  8627. END;
  8628. ReleaseOperand(leftO);
  8629. IF destination.mode # IntermediateCode.Undefined THEN
  8630. Emit(Mov(position,destination,rightO.op));
  8631. END;
  8632. ReleaseOperand(rightO);
  8633. ReleaseIntermediateOperand(mem);
  8634. IntermediateCode.InitOperand(destination);
  8635. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8636. Evaluate(right,rightO);
  8637. Designate(left,leftO);
  8638. MakeMemory(mem,leftO.op,addressType,0);
  8639. Emit(Mov(position,mem,rightO.op));
  8640. ReleaseIntermediateOperand(mem);
  8641. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8642. (* delegate *)
  8643. (*
  8644. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8645. *)
  8646. Emit(Mov(position,leftO.tag,rightO.tag));
  8647. END;
  8648. ReleaseOperand(leftO);
  8649. ReleaseOperand(rightO);
  8650. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8651. Designate(right,rightO);
  8652. Designate(left,leftO);
  8653. sizeOp := CopySize(left);
  8654. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8655. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8656. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8657. IF (rightType IS SyntaxTree.StringType) THEN
  8658. CopyString(left,right);
  8659. 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
  8660. Designate(right,rightO);
  8661. Designate(left,leftO);
  8662. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8663. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8664. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8665. ELSE
  8666. HALT(201)
  8667. END;
  8668. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8669. AssignMathArray(left,right);
  8670. ELSE
  8671. HALT(200);
  8672. END;
  8673. END Assign;
  8674. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8675. BEGIN
  8676. IF Trace THEN TraceEnter("VisitAssignment") END;
  8677. Assign(x.left,x.right);
  8678. IF Trace THEN TraceExit("VisitAssignment") END;
  8679. END VisitAssignment;
  8680. PROCEDURE EmitCooperativeSwitch;
  8681. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8682. BEGIN
  8683. ASSERT (cooperativeSwitches);
  8684. pc := section.pc;
  8685. IF lastSwitchPC = section.pc THEN RETURN END;
  8686. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8687. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8688. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8689. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8690. INC(statCoopSwitch, section.pc - pc);
  8691. END EmitCooperativeSwitch;
  8692. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8693. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8694. BEGIN
  8695. p0 := communication.left; p1 := communication.right;
  8696. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8697. Evaluate(p0,s0);
  8698. Evaluate(p1,s1);
  8699. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8700. Emit(Push(position,s0.op));
  8701. Emit(Push(position,s1.op));
  8702. (*
  8703. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8704. *)
  8705. IF ~backend.cellsAreObjects THEN
  8706. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8707. END;
  8708. ReleaseOperand(s0);
  8709. ReleaseOperand(s1);
  8710. IF backend.cellsAreObjects THEN
  8711. CallThis(position,"ActiveCellsRuntime","Send",2);
  8712. ELSE
  8713. CallThis(position,ChannelModuleName,"Send",2);
  8714. END;
  8715. (* ----- RECEIVE ------*)
  8716. ELSE
  8717. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8718. tmp := p0; p0 := p1; p1 := tmp;
  8719. END;
  8720. Evaluate(p0,s0);
  8721. Emit(Push(position,s0.op));
  8722. Designate(p1,s1);
  8723. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8724. Emit(Push(position,s1.op));
  8725. (*
  8726. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8727. *)
  8728. IF ~backend.cellsAreObjects THEN
  8729. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8730. END;
  8731. ReleaseOperand(s0);
  8732. ReleaseOperand(s1);
  8733. IF backend.cellsAreObjects THEN
  8734. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8735. ELSE
  8736. CallThis(position,ChannelModuleName,"Receive",2)
  8737. END;
  8738. END;
  8739. END VisitCommunicationStatement;
  8740. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8741. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8742. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8743. VAR true, false: Label; condition, value: BOOLEAN;
  8744. BEGIN
  8745. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8746. IF condition THEN
  8747. true := NewLabel();
  8748. false := NewLabel();
  8749. Condition(if.condition,true,false);
  8750. SetLabel(true);
  8751. StatementSequence(if.statements);
  8752. BrL(end);
  8753. SetLabel(false);
  8754. ELSE
  8755. IF value THEN (* always true *)
  8756. escape := TRUE;
  8757. StatementSequence(if.statements);
  8758. (* no branch necessary -- rest skipped *)
  8759. END;
  8760. END;
  8761. END IfPart;
  8762. BEGIN
  8763. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8764. end := NewLabel();
  8765. elsifs := x.ElsifParts();
  8766. IfPart(x.ifPart);
  8767. FOR i := 0 TO elsifs-1 DO
  8768. IF ~escape THEN
  8769. elsif := x.GetElsifPart(i);
  8770. IfPart(elsif);
  8771. END;
  8772. END;
  8773. IF (x.elsePart # NIL) & ~escape THEN
  8774. StatementSequence(x.elsePart);
  8775. END;
  8776. SetLabel(end);
  8777. IF Trace THEN TraceExit("VisitIfStatement") END;
  8778. END VisitIfStatement;
  8779. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8780. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8781. BEGIN
  8782. (*IF x.variable.type.resolved = x.type.resolved THEN
  8783. (* always true, do nothing *)
  8784. ELSE*)
  8785. Designate(x.variable,res);
  8786. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8787. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8788. END;
  8789. trueL := NewLabel();
  8790. TypeTest(res.tag,x.type,trueL,falseL);
  8791. ReleaseOperand(res);
  8792. SetLabel(trueL);
  8793. StatementSequence(x.statements);
  8794. BrL(endL);
  8795. END WithPart;
  8796. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8797. VAR endL,falseL: Label;i: LONGINT;
  8798. BEGIN
  8799. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8800. endL := NewLabel();
  8801. FOR i := 0 TO x.WithParts()-1 DO
  8802. falseL := NewLabel();
  8803. WithPart(x.GetWithPart(i),falseL,endL);
  8804. SetLabel(falseL);
  8805. END;
  8806. IF x.elsePart = NIL THEN
  8807. IF ~isUnchecked THEN
  8808. EmitTrap(position,WithTrap);
  8809. END;
  8810. ELSE
  8811. StatementSequence(x.elsePart)
  8812. END;
  8813. SetLabel(endL);
  8814. IF Trace THEN TraceExit("VisitWithStatement") END;
  8815. END VisitWithStatement;
  8816. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8817. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8818. out,else: Label; label: Label;
  8819. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8820. symbol: SyntaxTree.Symbol;
  8821. BEGIN
  8822. (*! split case statement into if-elsif statements for large case label lists *)
  8823. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8824. size := x.max-x.min+1;
  8825. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8826. END;
  8827. Evaluate(x.variable,var);
  8828. ReuseCopy(tmp,var.op);
  8829. ReleaseIntermediateOperand(var.op);
  8830. var.op := tmp;
  8831. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8832. Convert(var.op,addressType);
  8833. size := x.max-x.min+1;
  8834. else := NewLabel();
  8835. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8836. (*
  8837. UniqueId(name,module.module,"case",caseId);
  8838. *)
  8839. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8840. Global.GetModuleSegmentedName(module.module, name);
  8841. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8842. symbol := SyntaxTree.NewSymbol(name[1]);
  8843. symbol.SetScope(moduleScope);
  8844. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8845. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8846. section.isCaseTable := TRUE;
  8847. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8848. ReuseCopy(res,var.op);
  8849. ReleaseOperand(var);
  8850. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8851. Emit(Add(position,res,res,jmp));
  8852. IntermediateCode.MakeMemory(res,addressType);
  8853. Emit(Br(position,res));
  8854. ReleaseIntermediateOperand(res);
  8855. out := NewLabel();
  8856. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8857. part := x.GetCasePart(i);
  8858. constant := part.firstConstant;
  8859. label := NewLabel();
  8860. SetLabel(label);
  8861. WHILE(constant # NIL) DO (* case labels for this case part *)
  8862. FOR j := constant.min TO constant.max DO
  8863. fixups[j-x.min] := label;
  8864. END;
  8865. constant := constant.next;
  8866. END;
  8867. StatementSequence(part.statements);
  8868. BrL(out);
  8869. END;
  8870. SetLabel(else);
  8871. FOR i := 0 TO size-1 DO
  8872. IF fixups[i] = NIL THEN
  8873. fixups[i] := else;
  8874. END;
  8875. END;
  8876. IF x.elsePart # NIL THEN
  8877. StatementSequence(x.elsePart);
  8878. ELSIF ~isUnchecked THEN
  8879. EmitTrap(position,CaseTrap);
  8880. END;
  8881. SetLabel(out);
  8882. FOR i := 0 TO size-1 DO
  8883. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8884. section.Emit(Data(position,op));
  8885. END;
  8886. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8887. END VisitCaseStatement;
  8888. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8889. VAR start: Label; true,false: Label;
  8890. BEGIN
  8891. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8892. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8893. start := NewLabel();
  8894. true := NewLabel();
  8895. false := NewLabel();
  8896. SetLabel(start);
  8897. Condition(x.condition,true,false);
  8898. SetLabel(true);
  8899. StatementSequence(x.statements);
  8900. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8901. BrL(start);
  8902. SetLabel(false);
  8903. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8904. END VisitWhileStatement;
  8905. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8906. VAR false,true: Label;
  8907. BEGIN
  8908. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8909. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8910. true := NewLabel();
  8911. false := NewLabel();
  8912. SetLabel(false);
  8913. StatementSequence(x.statements);
  8914. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8915. Condition(x.condition,true,false);
  8916. SetLabel(true);
  8917. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8918. END VisitRepeatStatement;
  8919. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8920. VAR
  8921. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8922. temporaryVariable: SyntaxTree.Variable;
  8923. temporaryVariableDesignator : SyntaxTree.Designator;
  8924. BEGIN
  8925. IF Trace THEN TraceEnter("VisitForStatement") END;
  8926. true := NewLabel();
  8927. false := NewLabel();
  8928. start := NewLabel();
  8929. Assign(x.variable,x.from);
  8930. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8931. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8932. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8933. Assign(temporaryVariableDesignator,x.to);
  8934. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8935. IF by > 0 THEN
  8936. cmp := Scanner.LessEqual
  8937. ELSE
  8938. cmp := Scanner.GreaterEqual
  8939. END;
  8940. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8941. binary.SetType(system.booleanType);
  8942. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8943. SetLabel(start);
  8944. Condition(binary,true,false);
  8945. SetLabel(true);
  8946. StatementSequence(x.statements);
  8947. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8948. binary.SetType(x.variable.type);
  8949. Assign(x.variable,binary);
  8950. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8951. BrL(start);
  8952. SetLabel(false);
  8953. IF Trace THEN TraceExit("VisitForStatement") END;
  8954. END VisitForStatement;
  8955. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8956. VAR prevLoop: Label;
  8957. BEGIN
  8958. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8959. prevLoop := currentLoop;
  8960. currentLoop := NewLabel();
  8961. StatementSequence(x.statements);
  8962. SetLabel(currentLoop);
  8963. currentLoop := prevLoop;
  8964. IF Trace THEN TraceExit("VisitExitableBlock") END;
  8965. END VisitExitableBlock;
  8966. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  8967. VAR prevLoop,start: Label;
  8968. BEGIN
  8969. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  8970. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8971. start := NewLabel();
  8972. prevLoop := currentLoop;
  8973. SetLabel(start);
  8974. currentLoop := NewLabel();
  8975. StatementSequence(x.statements);
  8976. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8977. BrL(start);
  8978. SetLabel(currentLoop);
  8979. currentLoop := prevLoop;
  8980. IF Trace THEN TraceExit("VisitLoopStatement") END;
  8981. END VisitLoopStatement;
  8982. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  8983. VAR outer: SyntaxTree.Statement;
  8984. BEGIN
  8985. IF Trace THEN TraceEnter("VisitExitStatement") END;
  8986. IF locked THEN (* r if we jump out of an exclusive block *)
  8987. outer := x.outer;
  8988. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  8989. outer := outer.outer;
  8990. END;
  8991. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  8992. Lock(FALSE);
  8993. END;
  8994. END;
  8995. BrL(currentLoop);
  8996. IF Trace THEN TraceExit("VisitExitStatement") END;
  8997. END VisitExitStatement;
  8998. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  8999. VAR
  9000. expression, parameterDesignator: SyntaxTree.Expression;
  9001. type, componentType: SyntaxTree.Type;
  9002. res, right: Operand;
  9003. left, mem, reg: IntermediateCode.Operand;
  9004. parameter: SyntaxTree.Parameter;
  9005. procedure: SyntaxTree.Procedure;
  9006. procedureType: SyntaxTree.ProcedureType;
  9007. returnTypeOffset: LONGINT;
  9008. delegate: BOOLEAN;
  9009. map: SymbolMap;
  9010. BEGIN
  9011. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9012. expression := x.returnValue;
  9013. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9014. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9015. IF currentIsInline THEN
  9016. IF expression # NIL THEN
  9017. map := currentMapper.Get(NIL);
  9018. IF map # NIL THEN
  9019. Assign(map.to, expression);
  9020. END;
  9021. END;
  9022. BrL(currentInlineExit);
  9023. RETURN;
  9024. END;
  9025. IF expression # NIL THEN
  9026. type := expression.type.resolved;
  9027. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9028. IF locked THEN Lock(FALSE) END;
  9029. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9030. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9031. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9032. (* return without structured return parameter *)
  9033. Evaluate(expression,res);
  9034. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9035. IF locked OR profile THEN
  9036. Emit(Push(position,res.op));
  9037. IF delegate THEN HALT(200) END;
  9038. ReleaseOperand(res);
  9039. IF locked THEN Lock(FALSE) END;
  9040. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9041. reg := NewRegisterOperand(res.op.type);
  9042. Emit(Pop(position,reg));
  9043. Emit(Return(position,reg));
  9044. ReleaseIntermediateOperand(reg);
  9045. ELSE
  9046. Emit(Return(position,res.op));
  9047. ReleaseOperand(res);
  9048. END;
  9049. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9050. THEN
  9051. (* return using structured return parameter *)
  9052. 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)
  9053. OR SemanticChecker.IsPointerType(type));
  9054. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9055. parameter :=procedureType.returnParameter;
  9056. ASSERT(parameter # NIL);
  9057. returnTypeOffset := parameter.offsetInBits;
  9058. (*
  9059. IF parameter# NIL THEN
  9060. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9061. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9062. ELSE
  9063. returnTypeOffset := system.offsetFirstParameter
  9064. END;
  9065. *)
  9066. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9067. IF type IS SyntaxTree.RangeType THEN
  9068. (* array range type *)
  9069. Evaluate(expression, right);
  9070. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9071. Emit(Mov(position,mem, right.op)); (* first *)
  9072. ReleaseIntermediateOperand(mem);
  9073. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9074. Emit(Mov(position,mem, right.tag)); (* last *)
  9075. ReleaseIntermediateOperand(mem);
  9076. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9077. Emit(Mov(position,mem, right.extra)); (* step *)
  9078. ReleaseIntermediateOperand(mem);
  9079. ReleaseOperand(right);
  9080. ELSIF type IS SyntaxTree.ComplexType THEN
  9081. Evaluate(expression, right);
  9082. componentType := type(SyntaxTree.ComplexType).componentType;
  9083. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9084. Emit(Mov(position,mem, right.op)); (* real part *)
  9085. ReleaseIntermediateOperand(mem);
  9086. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9087. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9088. ReleaseIntermediateOperand(mem);
  9089. ReleaseOperand(right);
  9090. ELSE (* covers cases: pointer / record / array *)
  9091. parameter := procedureType.returnParameter;
  9092. checker.SetCurrentScope(currentScope);
  9093. ASSERT(parameter # NIL);
  9094. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9095. Assign(parameterDesignator,expression);
  9096. END;
  9097. ReleaseIntermediateOperand(left);
  9098. IF locked THEN Lock(FALSE) END;
  9099. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9100. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9101. parameter := procedureType.returnParameter;
  9102. checker.SetCurrentScope(currentScope);
  9103. IF parameter = NIL THEN
  9104. Error(procedure.position, "structured return of parameter of procedure not found");
  9105. ELSE
  9106. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9107. Assign(parameterDesignator,expression);
  9108. END;
  9109. IF locked THEN Lock(FALSE) END;
  9110. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9111. ELSE
  9112. HALT(200);
  9113. END;
  9114. ELSE
  9115. IF locked THEN Lock(FALSE) END;
  9116. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9117. END;
  9118. IF backend.cooperative THEN
  9119. BrL(exitLabel);
  9120. ELSE
  9121. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9122. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9123. END;
  9124. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9125. END VisitReturnStatement;
  9126. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9127. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9128. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9129. statements: SyntaxTree.StatementSequence;
  9130. name, suffix: SyntaxTree.IdentifierString;
  9131. BEGIN
  9132. Strings.IntToStr(awaitProcCounter,suffix);
  9133. Strings.Concat("@AwaitProcedure",suffix,name);
  9134. identifier := SyntaxTree.NewIdentifier(name);
  9135. INC(awaitProcCounter);
  9136. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9137. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9138. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9139. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9140. procedure.SetAccess(SyntaxTree.Hidden);
  9141. procedure.SetScope(currentScope);
  9142. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9143. procedureType.SetReturnType(system.booleanType);
  9144. procedure.SetType(procedureType);
  9145. body := SyntaxTree.NewBody(x.position,procedureScope);
  9146. procedureScope.SetBody(body);
  9147. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9148. returnStatement.SetReturnValue(x.condition);
  9149. statements := SyntaxTree.NewStatementSequence();
  9150. statements.AddStatement(returnStatement);
  9151. body.SetStatementSequence(statements);
  9152. currentScope.AddProcedure(procedure);
  9153. RETURN procedure
  9154. END MakeAwaitProcedure;
  9155. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9156. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9157. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9158. BEGIN
  9159. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9160. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9161. IF backend.cooperative THEN
  9162. start := NewLabel();
  9163. true := NewLabel();
  9164. false := NewLabel();
  9165. SetLabel(start);
  9166. Condition(x.condition,true,false);
  9167. SetLabel(false);
  9168. PushSelfPointer();
  9169. CallThis(position,"ExclusiveBlocks","Await",1);
  9170. BrL(start);
  9171. SetLabel(true);
  9172. PushSelfPointer();
  9173. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9174. ELSE
  9175. proc := MakeAwaitProcedure(x);
  9176. Emit(Push(position,fp));
  9177. GetCodeSectionNameForSymbol(proc,name);
  9178. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9179. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9180. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9181. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9182. Emit(Result(position,res));
  9183. (*
  9184. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9185. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9186. *)
  9187. InitOperand(result,ModeValue);
  9188. result.op := res;
  9189. label := NewLabel();
  9190. BreqL(label, result.op, SELF.true);
  9191. ReleaseOperand(result);
  9192. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9193. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9194. Emit(Push(position,res));
  9195. Emit(Push(position,fp));
  9196. PushSelfPointer();
  9197. Emit(Push(position,nil));
  9198. CallThis(position,"Objects","Await",4);
  9199. SetLabel(label);
  9200. END;
  9201. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9202. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9203. END VisitAwaitStatement;
  9204. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9205. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9206. BEGIN
  9207. FOR i := 0 TO x.Length() - 1 DO
  9208. statement := x.GetStatement( i );
  9209. Statement(statement);
  9210. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9211. END;
  9212. END StatementSequence;
  9213. PROCEDURE PushSelfPointer;
  9214. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9215. BEGIN
  9216. scope := currentScope;
  9217. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9218. scope := scope.outerScope;
  9219. END;
  9220. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9221. IF ~newObjectFile THEN
  9222. Symbol(moduleSelf,op);
  9223. IntermediateCode.MakeMemory(op.op,addressType);
  9224. ELSE
  9225. moduleSection := meta.ModuleSection();
  9226. IF backend.cooperative THEN
  9227. moduleOffset := 0;
  9228. ELSE
  9229. moduleOffset := moduleSection.pc;
  9230. END;
  9231. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9232. END;
  9233. ELSE
  9234. GetBaseRegister(op.op,currentScope,scope);
  9235. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9236. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9237. IF backend.cooperative THEN
  9238. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9239. END;
  9240. IntermediateCode.MakeMemory(op.op,addressType);
  9241. END;
  9242. Emit(Push(position,op.op));
  9243. ReleaseOperand(op);
  9244. END PushSelfPointer;
  9245. PROCEDURE Lock(lock: BOOLEAN);
  9246. BEGIN
  9247. IF Trace THEN TraceEnter("Lock") END;
  9248. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9249. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9250. ASSERT(modifyAssignmentCounter = 0);
  9251. IF dump # NIL THEN
  9252. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9253. dump.Ln;dump.Update;
  9254. END;
  9255. PushSelfPointer;
  9256. IF backend.cooperative THEN
  9257. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9258. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9259. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9260. END;
  9261. ELSE
  9262. Emit(Push(position,true));
  9263. IF lock THEN CallThis(position,"Objects","Lock",2)
  9264. ELSE CallThis(position,"Objects","Unlock",2);
  9265. END;
  9266. END;
  9267. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9268. IF Trace THEN TraceExit("Lock") END;
  9269. END Lock;
  9270. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9271. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9272. BEGIN
  9273. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9274. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9275. previouslyUnchecked := isUnchecked;
  9276. isUnchecked := isUnchecked OR x.isUnchecked;
  9277. previouslyCooperativeSwitches := cooperativeSwitches;
  9278. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9279. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9280. IF x.statements # NIL THEN
  9281. StatementSequence(x.statements);
  9282. END;
  9283. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9284. isUnchecked := previouslyUnchecked;
  9285. cooperativeSwitches := previouslyCooperativeSwitches;
  9286. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9287. END VisitStatementBlock;
  9288. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9289. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9290. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9291. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9292. procedure: SyntaxTree.Procedure;
  9293. map: SymbolMap;
  9294. BEGIN
  9295. scope := currentScope;
  9296. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9297. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9298. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9299. return := emptyOperand;
  9300. IF Trace THEN TraceEnter("VisitCode") END;
  9301. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9302. NEW(in, x.inRules.Length());
  9303. FOR i := 0 TO LEN(in)-1 DO
  9304. statement := x.inRules.GetStatement(i);
  9305. WITH statement: SyntaxTree.Assignment DO
  9306. Evaluate(statement.right, operand);
  9307. result := operand.op;
  9308. NEW(str, 64);
  9309. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9310. in[i] := result; IntermediateCode.SetString(in[i], str);
  9311. ReleaseIntermediateOperand(operand.tag);
  9312. END;
  9313. END;
  9314. ELSE in := NIL
  9315. END;
  9316. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9317. NEW(out, x.outRules.Length());
  9318. FOR i := 0 TO LEN(out)-1 DO
  9319. statement := x.outRules.GetStatement(i);
  9320. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9321. WITH statement: SyntaxTree.Assignment DO
  9322. Evaluate(statement.left, operand); (*?? or designate *)
  9323. result := operand.op;
  9324. NEW(str, 64);
  9325. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9326. out[i] := result; IntermediateCode.SetString(out[i], str);
  9327. ReleaseIntermediateOperand(operand.tag);
  9328. |statement: SyntaxTree.ReturnStatement DO
  9329. NEW(str, 64);
  9330. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9331. IF currentIsInline THEN
  9332. map := currentMapper.Get(NIL);
  9333. Evaluate(map.to, operand);
  9334. out[i] := operand.op;
  9335. ELSE
  9336. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9337. END;
  9338. IntermediateCode.SetString(out[i], str);
  9339. ReleaseIntermediateOperand(operand.tag);
  9340. return := out[i];
  9341. ELSE
  9342. END;
  9343. END;
  9344. ELSE out := NIL
  9345. END;
  9346. Emit(Asm(x.position,x.sourceCode, in, out));
  9347. IF in # NIL THEN
  9348. FOR i := 0 TO LEN(in)-1 DO
  9349. ReleaseIntermediateOperand(in[i]);
  9350. END;
  9351. END;
  9352. IF out # NIL THEN
  9353. FOR i := 0 TO LEN(out)-1 DO
  9354. WITH statement: SyntaxTree.Assignment DO
  9355. ReleaseIntermediateOperand(out[i]);
  9356. ELSE
  9357. IF currentIsInline THEN
  9358. ReleaseIntermediateOperand(out[i]);
  9359. END;
  9360. END;
  9361. statement := x.outRules.GetStatement(i);
  9362. END;
  9363. END;
  9364. IF return.mode # IntermediateCode.Undefined THEN
  9365. IF currentIsInline THEN
  9366. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9367. Symbol(procedureType.returnParameter, par);
  9368. MakeMemory(mem, par.op, return.type, 0);
  9369. ReleaseOperand(par);
  9370. Emit(Mov(position, mem, return));
  9371. ReleaseIntermediateOperand(mem);
  9372. ELSE
  9373. Emit(Return(position,return));
  9374. END;
  9375. IF currentIsInline THEN RETURN END;
  9376. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9377. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9378. ReleaseIntermediateOperand(return);
  9379. END;
  9380. IF Trace THEN TraceExit("VisitCode") END;
  9381. END VisitCode;
  9382. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9383. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9384. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9385. const, call: IntermediateCode.Operand;
  9386. parameterDesignator: SyntaxTree.Expression;
  9387. saved: RegisterEntry;
  9388. name: Basic.SegmentedName;
  9389. BEGIN
  9390. IF Trace THEN TraceEnter("ParameterCopies") END;
  9391. parameter := x.firstParameter;
  9392. WHILE parameter # NIL DO
  9393. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9394. type := parameter.type.resolved;
  9395. IF IsOpenArray(type) THEN
  9396. VisitParameter(parameter);
  9397. op := result;
  9398. IF backend.cooperative & parameter.NeedsTrace() THEN
  9399. length := GetArrayLength(type, op.tag);
  9400. size := NewRegisterOperand(addressType);
  9401. base := ArrayBaseType(type);
  9402. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9403. Emit(Mul(position, size, length, const));
  9404. dst := NewRegisterOperand (addressType);
  9405. Emit(Mov(position, dst, size));
  9406. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9407. Emit(Sub(position,dst,sp,dst));
  9408. Emit(And(position,dst,dst,const));
  9409. Emit(Mov(position,sp,dst));
  9410. par := fp;
  9411. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9412. IntermediateCode.InitImmediate(null, byteType, 0);
  9413. Emit(Fill(position, dst, size, null));
  9414. ReleaseIntermediateOperand(dst);
  9415. ReleaseIntermediateOperand(length);
  9416. SaveRegisters();ReleaseUsedRegisters(saved);
  9417. (* register dst has been freed before SaveRegisters already *)
  9418. base := ArrayBaseType(type);
  9419. (* assign method of open array *)
  9420. IF base.IsRecordType() THEN
  9421. Emit (Push(position, length));
  9422. Emit (Push(position, dst));
  9423. Emit (Push(position, op.op));
  9424. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9425. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9426. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9427. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9428. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9429. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9430. Emit (Push(position,length));
  9431. Emit (Push(position, dst));
  9432. Emit (Push(position, length));
  9433. Emit (Push(position, op.op));
  9434. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9435. ELSE
  9436. Emit (Push(position, length));
  9437. Emit (Push(position, dst));
  9438. Emit (Push(position, length));
  9439. Emit (Push(position, op.op));
  9440. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9441. ASSERT(ArrayBaseType(type).IsPointer());
  9442. END;
  9443. RestoreRegisters(saved);
  9444. ELSE
  9445. temp := GetDynamicSize(type,op.tag);
  9446. ReuseCopy(size,temp);
  9447. ReleaseIntermediateOperand(temp);
  9448. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9449. Emit(Sub(position,size,sp,size));
  9450. Emit(And(position,size,size,const));
  9451. Emit(Mov(position,sp,size));
  9452. par := fp;
  9453. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9454. ReleaseIntermediateOperand(size);
  9455. size := GetDynamicSize(type,op.tag);
  9456. END;
  9457. Emit(Copy(position,sp,op.op,size));
  9458. ReleaseIntermediateOperand(size);
  9459. ReleaseOperand(op);
  9460. IntermediateCode.MakeMemory(par,addressType);
  9461. Emit(Mov(position,par,sp));
  9462. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9463. checker.SetCurrentScope(currentScope);
  9464. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9465. Assign(parameterDesignator,parameterDesignator);
  9466. END;
  9467. END;
  9468. parameter := parameter.nextParameter;
  9469. END;
  9470. IF Trace THEN TraceExit("ParameterCopies") END;
  9471. END ParameterCopies;
  9472. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9473. VAR x: SyntaxTree.Variable;
  9474. BEGIN
  9475. x := scope.firstVariable;
  9476. WHILE x # NIL DO
  9477. InitVariable(x);
  9478. x := x.nextVariable;
  9479. END;
  9480. END InitVariables;
  9481. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9482. BEGIN
  9483. IF (symbol # NIL) THEN
  9484. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9485. ELSE
  9486. RETURN 0
  9487. END;
  9488. END GetFingerprint;
  9489. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9490. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9491. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9492. saved: RegisterEntry;
  9493. BEGIN
  9494. IF Trace THEN TraceEnter("Body") END;
  9495. ReleaseUsedRegisters(saved); (* just in case ... *)
  9496. section := ir;
  9497. exitLabel := NewLabel ();
  9498. IF moduleBody THEN moduleBodySection := section END;
  9499. IF ir.comments # NIL THEN
  9500. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9501. commentPrintout.SingleStatement(TRUE);
  9502. dump := ir.comments;
  9503. ELSE
  9504. commentPrintout := NIL;
  9505. dump := NIL;
  9506. END;
  9507. registerUsageCount.Init;
  9508. prevScope := currentScope;
  9509. currentScope := scope;
  9510. lastSwitchPC := 0;
  9511. cooperativeSwitches := backend.cooperative;
  9512. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9513. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9514. IF x # NIL THEN
  9515. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9516. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9517. IF moduleBody THEN
  9518. ProfilerInit();
  9519. ELSE
  9520. Basic.SegmentedNameToString(ir.name, string);
  9521. ProfilerAddProcedure(numberProcedures,string);
  9522. ProfilerEnterExit(numberProcedures,TRUE);
  9523. END;
  9524. END;
  9525. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9526. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9527. END;
  9528. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9529. IF moduleBody THEN
  9530. InitVariables(moduleScope)
  9531. END;
  9532. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9533. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9534. IF procedure = cellScope.bodyProcedure THEN
  9535. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9536. StaticCallOperand(op, cellScope.constructor);
  9537. Emit(Call(position,op.op,0));
  9538. END;
  9539. END;
  9540. END;
  9541. ParameterCopies(procedureType);
  9542. InitVariables(scope);
  9543. IF x.code = NIL THEN
  9544. VisitStatementBlock(x);
  9545. ELSE
  9546. VisitCode(x.code)
  9547. END;
  9548. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9549. ir.SetFinally(ir.pc);
  9550. StatementSequence(x.finally)
  9551. END;
  9552. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9553. IF ~backend.cooperative THEN
  9554. ProfilerEnterExit(numberProcedures,FALSE);
  9555. END;
  9556. INC(numberProcedures);
  9557. END;
  9558. END;
  9559. IF backend.cooperative THEN
  9560. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9561. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9562. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9563. (*! not required any more? check and delete!
  9564. PushSelfPointer();
  9565. CallThis(position,"Modules","SetInitialized",1);
  9566. *)
  9567. (*
  9568. SetLabel(end);
  9569. *)
  9570. END;
  9571. IF x # NIL THEN
  9572. SELF.position := x.position;
  9573. END;
  9574. CheckRegistersFree();
  9575. ASSERT(modifyAssignmentCounter = 0);
  9576. currentScope := prevScope;
  9577. IF Trace THEN TraceExit("Body") END;
  9578. END Body;
  9579. END ImplementationVisitor;
  9580. MetaDataGenerator=OBJECT
  9581. VAR
  9582. implementationVisitor: ImplementationVisitor;
  9583. declarationVisitor: DeclarationVisitor;
  9584. module: Sections.Module;
  9585. moduleName: ARRAY 128 OF CHAR;
  9586. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9587. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9588. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9589. TypeTags: LONGINT; (* type extension level support *)
  9590. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9591. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9592. BEGIN
  9593. IF implementationVisitor.backend.cooperative THEN
  9594. TypeTags := MAX(LONGINT); (* only 3 extensions allowed *)
  9595. BaseTypesTableOffset := 0;
  9596. MethodTableOffset := 2;
  9597. TypeRecordBaseOffset := 0;
  9598. ELSIF simple THEN
  9599. TypeTags := 3; (* only 3 extensions allowed *)
  9600. BaseTypesTableOffset := -1;
  9601. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9602. TypeRecordBaseOffset := TypeTags;
  9603. ELSE
  9604. TypeTags := 16;
  9605. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9606. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9607. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9608. END;
  9609. SELF.simple := simple;
  9610. SELF.implementationVisitor := implementationVisitor;
  9611. SELF.declarationVisitor := declarationVisitor;
  9612. implementationVisitor.meta := SELF;
  9613. declarationVisitor.meta := SELF;
  9614. END InitMetaDataGenerator;
  9615. PROCEDURE SetModule(module: Sections.Module);
  9616. BEGIN
  9617. SELF.module := module
  9618. END SetModule;
  9619. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9620. BEGIN
  9621. IF implementationVisitor.backend.cooperative THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9622. END GetTypeRecordBaseOffset;
  9623. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9624. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9625. BEGIN
  9626. INC(dataAdrOffset,6);
  9627. Info(section,"headerAdr");
  9628. Address(section,0);
  9629. Info(section,"typeDesc");
  9630. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9631. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9632. NamedSymbol(section, name, symbol, 0, offset);
  9633. Info(section,"mark: LONGINT;");
  9634. Longint(section,-1);
  9635. Info(section,"dataAdr-: ADDRESS");
  9636. Symbol(section,section, dataAdrOffset,0);
  9637. Info(section,"size-: SIZE");
  9638. Address(section,0);
  9639. Info(section,"nextRealtime: HeapBlock;");
  9640. Address(section,0);
  9641. END HeapBlock;
  9642. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9643. VAR i: LONGINT;
  9644. BEGIN
  9645. INC(dataAdrOffset,14);
  9646. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9647. Info(section,"count*: LONGINT");
  9648. Longint(section,0);
  9649. Info(section,"locked*: BOOLEAN");
  9650. Longint(section,0);
  9651. Info(section,"awaitingLock*: ProcessQueue");
  9652. Address(section,0);
  9653. Address(section,0);
  9654. Info(section,"awaitingCond*: ProcessQueue");
  9655. Address(section,0);
  9656. Address(section,0);
  9657. Info(section,"lockedBy*: ANY");
  9658. Address(section,0);
  9659. Info(section,"lock*: ANY");
  9660. Address(section,0);
  9661. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9662. Longint(section,1);
  9663. FOR i := 2 TO 6 DO
  9664. Longint(section,0);
  9665. END;
  9666. END ProtectedHeapBlock;
  9667. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9668. BEGIN
  9669. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9670. END Info;
  9671. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9672. VAR op: IntermediateCode.Operand;
  9673. BEGIN
  9674. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9675. section.Emit(Data(-11,op));
  9676. END Address;
  9677. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9678. VAR op: IntermediateCode.Operand;
  9679. BEGIN
  9680. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9681. section.Emit(Data(-12,op));
  9682. END Size;
  9683. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9684. VAR op: IntermediateCode.Operand;
  9685. BEGIN
  9686. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9687. section.Emit(Data(-1,op));
  9688. END Set;
  9689. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9690. VAR op: IntermediateCode.Operand;
  9691. BEGIN
  9692. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9693. section.Emit(Data(-1,op));
  9694. END Longint;
  9695. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9696. VAR op,noOperand: IntermediateCode.Operand;
  9697. BEGIN
  9698. IntermediateCode.InitOperand(noOperand);
  9699. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9700. section.PatchOperands(pc,op,noOperand,noOperand);
  9701. END PatchLongint;
  9702. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9703. VAR op, noOperand: IntermediateCode.Operand;
  9704. BEGIN
  9705. IntermediateCode.InitOperand(noOperand);
  9706. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9707. section.PatchOperands(pc,op,noOperand,noOperand);
  9708. END PatchSymbol;
  9709. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9710. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9711. BEGIN
  9712. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9713. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9714. section.Emit(Data(-1,op));
  9715. END Boolean;
  9716. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9717. VAR op: IntermediateCode.Operand;
  9718. BEGIN
  9719. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9720. section.Emit(Data(-1,op));
  9721. END Char;
  9722. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9723. VAR i: LONGINT;
  9724. BEGIN
  9725. Info(section,str);
  9726. i := 0;
  9727. WHILE(str[i] # 0X) DO
  9728. Char(section,str[i]);
  9729. INC(i);
  9730. END;
  9731. Char(section,0X);
  9732. END String;
  9733. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9734. VAR op: IntermediateCode.Operand;
  9735. BEGIN
  9736. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9737. IntermediateCode.SetOffset(op,realOffset);
  9738. section.Emit(Data(-1,op));
  9739. END NamedSymbol;
  9740. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9741. BEGIN
  9742. IF symbol= NIL THEN
  9743. Address( section, realOffset);
  9744. ASSERT(virtualOffset = 0);
  9745. ELSE
  9746. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9747. END;
  9748. END Symbol;
  9749. (* OutPointers delivers
  9750. {pointerOffset}
  9751. *)
  9752. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9753. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9754. BEGIN
  9755. type := type.resolved;
  9756. IF type IS SyntaxTree.AnyType THEN
  9757. Symbol(section, symbol, 0, (offset ));
  9758. INC(numberPointers);
  9759. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9760. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9761. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9762. ELSIF type IS SyntaxTree.PointerType THEN
  9763. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9764. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9765. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9766. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9767. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9768. ELSIF (type IS SyntaxTree.RecordType) THEN
  9769. (* never treat a record like a pointer, even if the pointer field is set! *)
  9770. WITH type: SyntaxTree.RecordType DO
  9771. base := type.GetBaseRecord();
  9772. IF base # NIL THEN
  9773. Pointers(offset,symbol,section, base,numberPointers);
  9774. END;
  9775. variable := type.recordScope.firstVariable;
  9776. WHILE(variable # NIL) DO
  9777. IF ~(variable.untraced) THEN
  9778. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9779. END;
  9780. variable := variable.nextVariable;
  9781. END;
  9782. END;
  9783. ELSIF (type IS SyntaxTree.CellType) THEN
  9784. WITH type: SyntaxTree.CellType DO
  9785. base := type.GetBaseRecord();
  9786. IF base # NIL THEN
  9787. Pointers(offset,symbol,section, base,numberPointers);
  9788. END;
  9789. variable := type.cellScope.firstVariable;
  9790. WHILE(variable # NIL) DO
  9791. IF ~(variable.untraced) THEN
  9792. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9793. END;
  9794. variable := variable.nextVariable;
  9795. END;
  9796. END;
  9797. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9798. WITH type: SyntaxTree.ArrayType DO
  9799. IF type.form= SyntaxTree.Static THEN
  9800. n := type.staticLength;
  9801. base := type.arrayBase.resolved;
  9802. WHILE(base IS SyntaxTree.ArrayType) DO
  9803. type := base(SyntaxTree.ArrayType);
  9804. n := n* type.staticLength;
  9805. base := type.arrayBase.resolved;
  9806. END;
  9807. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9808. IF SemanticChecker.ContainsPointer(base) THEN
  9809. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9810. FOR i := 0 TO n-1 DO
  9811. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9812. END;
  9813. END;
  9814. ELSE
  9815. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9816. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9817. END;
  9818. END;
  9819. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9820. WITH type: SyntaxTree.MathArrayType DO
  9821. IF type.form = SyntaxTree.Static THEN
  9822. n := type.staticLength;
  9823. base := type.arrayBase.resolved;
  9824. WHILE(base IS SyntaxTree.MathArrayType) DO
  9825. type := base(SyntaxTree.MathArrayType);
  9826. n := n* type.staticLength;
  9827. base := type.arrayBase.resolved;
  9828. END;
  9829. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9830. IF SemanticChecker.ContainsPointer(base) THEN
  9831. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9832. FOR i := 0 TO n-1 DO
  9833. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9834. END;
  9835. END;
  9836. ELSE
  9837. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9838. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9839. END
  9840. END;
  9841. (* ELSE no pointers in type *)
  9842. END;
  9843. END Pointers;
  9844. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9845. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9846. BEGIN
  9847. IF pool.Has(name) THEN
  9848. RETURN pool.GetInt(name)
  9849. ELSE
  9850. position := source.pc;
  9851. pool.PutInt(name, position);
  9852. StringPool.GetString(name, s);
  9853. Info(source, s);
  9854. i := 0;
  9855. REPEAT
  9856. ch := s[i]; INC(i);
  9857. Char( source, ch);
  9858. UNTIL ch = 0X;
  9859. END;
  9860. RETURN position;
  9861. END DynamicName;
  9862. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9863. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9864. BEGIN
  9865. COPY(moduleName,name);
  9866. Strings.Append(name,suffix);
  9867. Basic.ToSegmentedName(name, pooledName);
  9868. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9869. IF implementationVisitor.backend.cooperative THEN
  9870. Info(section, "TypeDescriptor");
  9871. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9872. NamedSymbol(section, pooledName,NIL, 0, 0);
  9873. BasePointer(section);
  9874. offset := 0;
  9875. ELSE
  9876. HeapBlock(mName,typeName,section,2);
  9877. Info(section, "HeapBlock");
  9878. (*
  9879. Symbol(section,section,2,0);
  9880. *)
  9881. Address(section,0); (* empty such that GC does not go on traversing *)
  9882. Info(section, "TypeDescriptor");
  9883. Address(section,0);
  9884. offset := section.pc;
  9885. END;
  9886. RETURN section
  9887. END Block;
  9888. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9889. VAR name: Basic.SegmentedName;
  9890. BEGIN
  9891. Info(source,"ArrayHeader");
  9892. IF implementationVisitor.backend.cooperative THEN
  9893. sizePC := source.pc;
  9894. Address(source,0);
  9895. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9896. IF baseType # "" THEN
  9897. Basic.ToSegmentedName(baseType, name);
  9898. NamedSymbol(source, name,NIL, 0, 0);
  9899. ELSE
  9900. Address(source,0);
  9901. END;
  9902. Address(source,0);
  9903. ELSE
  9904. Address(source,0);
  9905. Address(source,0);
  9906. Address(source,0);
  9907. sizePC := source.pc;
  9908. Address(source,0);
  9909. Info(source,"array data");
  9910. END;
  9911. END Array;
  9912. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9913. BEGIN
  9914. IF implementationVisitor.backend.cooperative THEN
  9915. PatchLongint(section, pc, size);
  9916. PatchLongint(section, pc + 3, size);
  9917. ELSE
  9918. PatchLongint(section, pc, size);
  9919. END;
  9920. END PatchArray;
  9921. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9922. VAR
  9923. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9924. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9925. poolMap: Basic.HashTableInt;
  9926. namePool: IntermediateCode.Section;
  9927. namePoolOffset: LONGINT;
  9928. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9929. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9930. BEGIN
  9931. Basic.SegmentedNameToString(s1.name,n1);
  9932. Basic.SegmentedNameToString(s2.name,n2);
  9933. index := 0;
  9934. ch1 := n1[index];
  9935. ch2 := n2[index];
  9936. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9937. INC(index);
  9938. ch1 := n1[index];
  9939. ch2 := n2[index];
  9940. END;
  9941. RETURN ch1 < ch2;
  9942. END Compare;
  9943. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9944. VAR
  9945. i, j: LONGINT;
  9946. x, t: Sections.Section;
  9947. BEGIN
  9948. IF lo < hi THEN
  9949. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9950. WHILE i <= j DO
  9951. WHILE Compare(list[i], x) DO INC(i) END;
  9952. WHILE Compare(x, list[j]) DO DEC(j) END;
  9953. IF i <= j THEN
  9954. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9955. INC(i); DEC(j)
  9956. END
  9957. END;
  9958. IF lo < j THEN QuickSort(list, lo, j) END;
  9959. IF i < hi THEN QuickSort(list, i, hi) END
  9960. END;
  9961. END QuickSort;
  9962. (*
  9963. ExportDesc* = RECORD
  9964. fp*: ADDRESS;
  9965. name* {UNTRACED}: DynamicName;
  9966. adr*: ADDRESS;
  9967. exports*: LONGINT;
  9968. dsc* {UNTRACED}: ExportArray
  9969. END;
  9970. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  9971. *)
  9972. PROCEDURE ExportDesc2(
  9973. source: IntermediateCode.Section;
  9974. namePool: IntermediateCode.Section;
  9975. fingerPrinter: FingerPrinter.FingerPrinter;
  9976. symbol: Sections.Section;
  9977. name: StringPool.Index;
  9978. VAR patchAdr: LONGINT
  9979. ): BOOLEAN;
  9980. VAR fingerPrint: SyntaxTree.FingerPrint;
  9981. BEGIN
  9982. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  9983. & (symbol.type # Sections.InlineCodeSection)
  9984. THEN
  9985. *)
  9986. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  9987. & (symbol.type # Sections.InlineCodeSection))
  9988. THEN
  9989. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  9990. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  9991. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  9992. Longint(source,fingerPrint.shallow);
  9993. ELSE
  9994. Longint(source, 0);
  9995. END;
  9996. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  9997. Symbol(source, symbol,0,0);
  9998. patchAdr := source.pc;
  9999. Address(source,0);
  10000. Address(source,0);
  10001. END;
  10002. RETURN TRUE
  10003. ELSE
  10004. RETURN FALSE
  10005. END;
  10006. END ExportDesc2;
  10007. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10008. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10009. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10010. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10011. TYPE
  10012. Scope = RECORD
  10013. elements: LONGINT;
  10014. gelements: LONGINT;
  10015. section: IntermediateCode.Section;
  10016. patchAdr: LONGINT;
  10017. arraySizePC: LONGINT;
  10018. beginPC: LONGINT; (* current scope start pc *)
  10019. END;
  10020. BEGIN
  10021. Basic.InitSegmentedName(prev);
  10022. olevel := -1;
  10023. scopes[0].section := source;
  10024. FOR s := 0 TO LEN(sections)-1 DO
  10025. symbol := sections[s];
  10026. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10027. this := sections[s].name;
  10028. level := 0;
  10029. WHILE (this[level] > 0) DO
  10030. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10031. INC(level);
  10032. END;
  10033. WHILE level < olevel DO
  10034. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10035. IF olevel > 0 THEN
  10036. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10037. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10038. END;
  10039. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10040. DEC(olevel);
  10041. END;
  10042. IF (this[level] > 0) THEN
  10043. IF level > olevel THEN
  10044. (*TRACE("opening",level); *)
  10045. IF scopes[level].section = NIL THEN
  10046. arrayName := ".@ExportArray";
  10047. Strings.AppendInt(arrayName, level);
  10048. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10049. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10050. END;
  10051. scopes[level].beginPC := scopes[level].section.pc;
  10052. olevel := level;
  10053. scopes[olevel].elements := 0;
  10054. END;
  10055. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10056. sym := sections[s];
  10057. ELSE
  10058. sym := NIL;
  10059. END;
  10060. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10061. THEN
  10062. INC(scopes[olevel].elements);
  10063. END;
  10064. (*StringPool.GetString(this[level], name);*)
  10065. (*TRACE(level, name);*)
  10066. (* enter string in scope *)
  10067. INC(level);
  10068. END;
  10069. END;
  10070. Basic.SegmentedNameToString(this, name);
  10071. (*TRACE(level, "enter", name);*)
  10072. prev := this;
  10073. END;
  10074. END;
  10075. WHILE 0 <= olevel DO
  10076. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10077. IF olevel > 0 THEN
  10078. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10079. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10080. END;
  10081. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10082. DEC(olevel);
  10083. END;
  10084. level := 0;
  10085. WHILE (level < LEN(scopes)) DO
  10086. IF scopes[level].section # NIL THEN
  10087. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10088. END;
  10089. INC(level);
  10090. END;
  10091. END Export;
  10092. BEGIN
  10093. NEW(fingerPrinter, module.system);
  10094. NEW(poolMap, 64);
  10095. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10096. NEW(sectionArray, module.allSections.Length());
  10097. FOR i := 0 TO module.allSections.Length() - 1 DO
  10098. section := module.allSections.GetSection(i);
  10099. sectionArray[i] := section;
  10100. END;
  10101. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10102. Export(sectionArray^);
  10103. END ExportDesc;
  10104. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10105. VAR
  10106. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10107. BEGIN
  10108. Info(source, "exception table offsets array descriptor");
  10109. size := 0;
  10110. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10111. Info(source, "exception table content");
  10112. FOR i := 0 TO module.allSections.Length() - 1 DO
  10113. p := module.allSections.GetSection(i);
  10114. IF p.type = Sections.CodeSection THEN
  10115. finallyPC := p(IntermediateCode.Section).finally;
  10116. IF finallyPC>=0 THEN
  10117. Symbol( source, p, 0,0);
  10118. Symbol( source, p, finallyPC, 0);
  10119. Symbol( source, p, finallyPC,0);
  10120. INC(size);
  10121. END;
  10122. END
  10123. END;
  10124. PatchArray(source,sizePC,size);
  10125. END ExceptionArray;
  10126. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10127. VAR i: LONGINT; ch: CHAR;
  10128. BEGIN
  10129. i := 0;
  10130. REPEAT
  10131. ch := name[i]; INC(i);
  10132. Char( section, ch);
  10133. UNTIL ch = 0X;
  10134. WHILE i < 32 DO
  10135. Char( section, 0X); INC(i);
  10136. END;
  10137. END Name;
  10138. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10139. VAR i: LONGINT; ch: CHAR;
  10140. BEGIN
  10141. i := 0;
  10142. REPEAT
  10143. ch := name[i]; INC(i);
  10144. Char( section, ch);
  10145. UNTIL ch = 0X;
  10146. WHILE i < 128 DO
  10147. Char( section, 0X); INC(i);
  10148. END;
  10149. END LongName;
  10150. PROCEDURE References(section: IntermediateCode.Section);
  10151. CONST
  10152. rfDirect = 1X; rfIndirect = 3X;
  10153. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10154. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10155. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10156. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10157. rfArrayFlag = 80X;
  10158. VAR
  10159. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10160. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10161. VAR char: CHAR;
  10162. BEGIN
  10163. IF type = NIL THEN char := rfLongint
  10164. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10165. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10166. ELSIF type IS SyntaxTree.CharacterType THEN
  10167. IF type.sizeInBits = 8 THEN char := rfChar8
  10168. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10169. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10170. END;
  10171. ELSIF type IS SyntaxTree.IntegerType THEN
  10172. IF type.sizeInBits = 8 THEN char := rfShortint
  10173. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10174. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10175. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10176. END;
  10177. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10178. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10179. ELSIF type IS SyntaxTree.FloatType THEN
  10180. IF type.sizeInBits = 32 THEN char := rfReal
  10181. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10182. END;
  10183. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10184. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10185. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10186. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10187. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10188. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10189. END;
  10190. IF arrayOf THEN
  10191. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10192. ELSE
  10193. Char(section,char)
  10194. END;
  10195. INC(size);
  10196. END BaseType;
  10197. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10198. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10199. BEGIN
  10200. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10201. IF destination = NIL THEN
  10202. (* imported unused record type *)
  10203. Char(section,0X); (* nil type *)
  10204. INC(size);
  10205. type.typeDeclaration.GetName(name);
  10206. (*
  10207. 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
  10208. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10209. *)
  10210. ELSE
  10211. IF type.pointerType # NIL THEN
  10212. Char(section,rfRecordPointer)
  10213. ELSE
  10214. Char(section,rfRecord);
  10215. END;
  10216. INC(size);
  10217. Longint(section,(destination.offset ));
  10218. INC(size,4);
  10219. END;
  10220. END RecordType;
  10221. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10222. BEGIN
  10223. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10224. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10225. INC(size);
  10226. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10227. END;
  10228. IF type.form = SyntaxTree.Static THEN
  10229. Longint(section,type.staticLength)
  10230. ELSE
  10231. Longint(section,0)
  10232. END;
  10233. INC(size,4);
  10234. END ArrayType;
  10235. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10236. BEGIN
  10237. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10238. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType 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 MathArrayType;
  10250. PROCEDURE Type(type: SyntaxTree.Type);
  10251. BEGIN
  10252. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10253. IF type IS SyntaxTree.BasicType THEN
  10254. BaseType(FALSE,type)
  10255. ELSIF type IS SyntaxTree.RecordType THEN
  10256. RecordType(type(SyntaxTree.RecordType));
  10257. ELSIF type IS SyntaxTree.ArrayType THEN
  10258. ArrayType(type(SyntaxTree.ArrayType))
  10259. ELSIF type IS SyntaxTree.EnumerationType THEN
  10260. BaseType(FALSE,module.system.longintType)
  10261. ELSIF type IS SyntaxTree.PointerType THEN
  10262. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10263. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10264. ELSE
  10265. BaseType(FALSE,type)
  10266. END;
  10267. ELSIF type IS SyntaxTree.ProcedureType THEN
  10268. BaseType(FALSE,type);
  10269. ELSIF type IS SyntaxTree.MathArrayType THEN
  10270. MathArrayType(type(SyntaxTree.MathArrayType));
  10271. ELSIF type IS SyntaxTree.CellType THEN
  10272. BaseType(FALSE,module.system.anyType);
  10273. ELSE HALT(200)
  10274. END;
  10275. END Type;
  10276. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10277. VAR name: ARRAY 256 OF CHAR;
  10278. BEGIN
  10279. IF variable.externalName # NIL THEN RETURN END;
  10280. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10281. INC(size);
  10282. variable.GetName(name);
  10283. Type(variable.type);
  10284. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10285. INC(size,4);
  10286. String(section,name);
  10287. INC(size,Strings.Length(name)+1);
  10288. END WriteVariable;
  10289. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10290. VAR name: ARRAY 256 OF CHAR;
  10291. BEGIN
  10292. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10293. INC(size);
  10294. variable.GetName(name);
  10295. Type(variable.type);
  10296. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10297. INC(size,4);
  10298. variable.GetName(name);
  10299. String(section,name);
  10300. INC(size,Strings.Length(name)+1);
  10301. END WriteParameter;
  10302. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10303. BEGIN
  10304. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10305. IF type IS SyntaxTree.ArrayType THEN
  10306. WITH type: SyntaxTree.ArrayType DO
  10307. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10308. ELSE Char(section,rfOpenArray)
  10309. END;
  10310. INC(size);
  10311. END
  10312. ELSIF type IS SyntaxTree.MathArrayType THEN
  10313. WITH type: SyntaxTree.MathArrayType DO
  10314. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10315. ELSE Char(section,rfOpenArray)
  10316. END;
  10317. INC(size);
  10318. END
  10319. ELSIF type IS SyntaxTree.RecordType THEN
  10320. Char(section,rfRecord);
  10321. INC(size);
  10322. ELSE
  10323. BaseType(FALSE,type);
  10324. END;
  10325. END ReturnType;
  10326. PROCEDURE Procedure(s: Sections.Section);
  10327. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10328. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10329. name: ARRAY 256 OF CHAR;
  10330. BEGIN
  10331. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10332. procedure.GetName(name);
  10333. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10334. Char(section,0F9X);
  10335. INC(size);
  10336. Symbol(section,s,0,0);
  10337. INC(size,4);
  10338. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10339. INC(size,4);
  10340. Longint(section,procedureType.numberParameters);
  10341. INC(size,4);
  10342. ReturnType(procedureType.returnType);
  10343. Longint(section,0); (*! level *)
  10344. INC(size,4);
  10345. Longint(section,0);
  10346. INC(size,4);
  10347. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10348. (*
  10349. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10350. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10351. recordName := "";
  10352. IF record.pointerType # NIL THEN
  10353. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10354. ELSE
  10355. DeclarationName(record.typeDeclaration,recordName);
  10356. END;
  10357. i := 0;
  10358. Info(section,recordName);
  10359. WHILE recordName[i] # 0X DO
  10360. Char(section,recordName[i]); INC(i);
  10361. INC(size);
  10362. END;
  10363. Char(section,".");
  10364. INC(size);
  10365. END;
  10366. *)
  10367. String(section,name);
  10368. INC(size,Strings.Length(name)+1);
  10369. parameter := procedureType.firstParameter;
  10370. WHILE(parameter # NIL) DO
  10371. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10372. parameter := parameter.nextParameter;
  10373. END;
  10374. variable := procedure.procedureScope.firstVariable;
  10375. WHILE(variable # NIL) DO
  10376. WriteVariable(variable,FALSE);
  10377. variable := variable.nextVariable;
  10378. END;
  10379. END Procedure;
  10380. PROCEDURE Scope(s: Sections.Section);
  10381. BEGIN
  10382. Char(section,0F8X);
  10383. INC(size);
  10384. Symbol(section,s,0,0); (* start *)
  10385. INC(size,4);
  10386. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10387. INC(size,4);
  10388. String(section,"$$");
  10389. INC(size,3);
  10390. (* removed variables -- wrongly interpreted by Reflection
  10391. variable := module.module.moduleScope.firstVariable;
  10392. WHILE(variable # NIL) DO
  10393. WriteVariable(variable,FALSE);
  10394. variable := variable.nextVariable;
  10395. END;
  10396. *)
  10397. END Scope;
  10398. BEGIN
  10399. Array(section,sizePC,"");
  10400. size := 0;
  10401. Char(section,0FFX); (* sign for trap writer *)
  10402. INC(size);
  10403. FOR i := 0 TO module.allSections.Length() - 1 DO
  10404. s := module.allSections.GetSection(i);
  10405. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10406. Scope(s) (*! must be first procedure in ref section *)
  10407. END
  10408. END;
  10409. FOR i := 0 TO module.allSections.Length() - 1 DO
  10410. s := module.allSections.GetSection(i);
  10411. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10412. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10413. Procedure(s)
  10414. END
  10415. END;
  10416. PatchArray(section,sizePC,size);
  10417. END References;
  10418. (*
  10419. Command* = RECORD
  10420. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10421. name*: Name; (* name of the procedure *)
  10422. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10423. entryAdr* : ADDRESS; (* entry address of procedure *)
  10424. END;
  10425. *)
  10426. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10427. VAR
  10428. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10429. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10430. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10431. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10432. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10433. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10434. BEGIN
  10435. RETURN
  10436. (type = NIL) OR
  10437. (type.resolved IS SyntaxTree.RecordType) OR
  10438. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10439. (type.resolved IS SyntaxTree.AnyType);
  10440. END TypeAllowed;
  10441. BEGIN
  10442. numberParameters := procedureType.numberParameters;
  10443. RETURN
  10444. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10445. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10446. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10447. END GetProcedureAllowed;
  10448. PROCEDURE WriteType(type : SyntaxTree.Type);
  10449. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10450. name: Basic.SegmentedName; offset: LONGINT;
  10451. BEGIN
  10452. IF type = NIL THEN
  10453. Address(source,0);
  10454. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10455. Address(source,1);
  10456. ELSE
  10457. type := type.resolved;
  10458. IF type IS SyntaxTree.PointerType THEN
  10459. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10460. END;
  10461. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10462. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10463. name[0] := typeDeclaration.name; name[1] := -1;
  10464. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10465. ASSERT(section # NIL);
  10466. ELSE
  10467. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10468. (* TODO *)
  10469. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10470. END;
  10471. IF implementationVisitor.backend.cooperative THEN
  10472. offset := 0;
  10473. ELSE
  10474. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10475. END;
  10476. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10477. END;
  10478. END WriteType;
  10479. BEGIN
  10480. Info(source, "command array descriptor");
  10481. Array(source,sizePC,"Modules.Command");
  10482. numberCommands := 0;
  10483. Info(source, "command array content");
  10484. FOR i := 0 TO module.allSections.Length() - 1 DO
  10485. p := module.allSections.GetSection(i);
  10486. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10487. procedure := p.symbol(SyntaxTree.Procedure);
  10488. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10489. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10490. procedure.GetName(name);
  10491. Name(source,name);
  10492. numberParameters := procedureType.numberParameters;
  10493. (* offset of type of first parameter *)
  10494. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10495. ELSE WriteType(procedureType.firstParameter.type)
  10496. END;
  10497. (* offset of type of return parameter *)
  10498. WriteType(procedureType.returnType);
  10499. (* command name *)
  10500. (* command code offset *)
  10501. Symbol(source,p,0,0);
  10502. INC(numberCommands);
  10503. IF Trace THEN
  10504. D.Ln;
  10505. END;
  10506. END;
  10507. END
  10508. END;
  10509. PatchArray(source,sizePC,numberCommands);
  10510. END CommandArray;
  10511. (* to prevent from double import of different module aliases *)
  10512. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10513. VAR i: SyntaxTree.Import;
  10514. BEGIN
  10515. i := module.module.moduleScope.firstImport;
  10516. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10517. i := i.nextImport;
  10518. END;
  10519. RETURN i = import
  10520. END IsFirstDirectOccurence;
  10521. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10522. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10523. BEGIN
  10524. Array(source,pc,"");
  10525. Info(source, "import module array data");
  10526. IF implementationVisitor.backend.cooperative THEN
  10527. offset := 0;
  10528. ELSE
  10529. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10530. END;
  10531. import := module.module.moduleScope.firstImport;
  10532. numberImports := 0;
  10533. WHILE import # NIL DO
  10534. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10535. Global.GetModuleSegmentedName(import.module,name);
  10536. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10537. NamedSymbol(source, name, NIL, 0, offset);
  10538. INC(numberImports);
  10539. END;
  10540. import := import.nextImport
  10541. END;
  10542. PatchArray(source,pc,numberImports);
  10543. END ImportsArray;
  10544. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10545. VAR
  10546. p: Sections.Section; sizePC, size, i: LONGINT;
  10547. BEGIN
  10548. Info(source, "Type info section");
  10549. size := 0;
  10550. Array(source,sizePC,"Modules.TypeDesc");
  10551. FOR i := 0 TO module.allSections.Length() - 1 DO
  10552. p := module.allSections.GetSection(i);
  10553. WITH p: IntermediateCode.Section DO
  10554. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10555. Symbol(source,p,0,0);
  10556. INC(size);
  10557. END;
  10558. END
  10559. END;
  10560. PatchArray(source,sizePC,size);
  10561. END TypeInfoSection;
  10562. (*
  10563. ProcTableEntry* = RECORD
  10564. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10565. noPtr*: LONGINT;
  10566. END;
  10567. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10568. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10569. *)
  10570. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10571. VAR
  10572. destination: Sections.Section;
  10573. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10574. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10575. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10576. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10577. BEGIN
  10578. Info(procArray,"pcFrom");
  10579. Symbol(procArray,destination,0,0);
  10580. Info(procArray,"pcTo");
  10581. Symbol(procArray,destination,destination.pc,0);
  10582. Info(procArray,"pcStatementBegin");
  10583. Symbol(procArray,destination,destination.validPAFEnter,0);
  10584. Info(procArray,"pcStatementEnd");
  10585. Symbol(procArray,destination,destination.validPAFExit,0);
  10586. IF ~implementationVisitor.backend.cooperative THEN
  10587. Basic.SegmentedNameToString(destination.name, string);
  10588. Info(pointerArray,string);
  10589. procedure := destination.symbol(SyntaxTree.Procedure);
  10590. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10591. variable := procedure.procedureScope.firstVariable;
  10592. WHILE(variable # NIL) DO
  10593. IF ~(variable.untraced) THEN
  10594. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10595. END;
  10596. variable := variable.nextVariable
  10597. END;
  10598. parameter := procedureType.firstParameter;
  10599. WHILE(parameter # NIL) DO
  10600. IF ~(parameter.untraced) THEN
  10601. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10602. END;
  10603. parameter := parameter.nextParameter;
  10604. END;
  10605. END;
  10606. Info(procArray,"numberPointers");
  10607. Longint(procArray,numberPointers);
  10608. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10609. INC(pointerArraySize, numberPointers);
  10610. END PointerOffsets;
  10611. BEGIN
  10612. maxPointers := 0;
  10613. Info(procArray, "proc array descriptor");
  10614. Address(procArray,0);
  10615. Address(procArray,0);
  10616. Address(procArray,0);
  10617. procArraySizePC := procArray.pc;
  10618. Address(procArray,0);
  10619. procArraySize := 0;
  10620. IF ~implementationVisitor.backend.cooperative THEN
  10621. Info(pointerArray, "pointer array descriptor");
  10622. Address(pointerArray,0);
  10623. Address(pointerArray,0);
  10624. Address(pointerArray,0);
  10625. pointerArraySizePC := pointerArray.pc;
  10626. Address(pointerArray,0);
  10627. pointerArraySize := 0;
  10628. END;
  10629. procArraySize := 0;
  10630. FOR i := 0 TO module.allSections.Length() - 1 DO
  10631. destination := module.allSections.GetSection(i);
  10632. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10633. PointerOffsets(destination(IntermediateCode.Section));
  10634. INC(procArraySize);
  10635. END
  10636. END;
  10637. PatchLongint(procArray,procArraySizePC,procArraySize);
  10638. IF ~implementationVisitor.backend.cooperative THEN
  10639. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10640. END;
  10641. END PointersInProcTables;
  10642. (*
  10643. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10644. VAR
  10645. next*: Module; (** once a module is published, all fields are read-only *)
  10646. name*: Name;
  10647. init, published: BOOLEAN;
  10648. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10649. sb*: ADDRESS; <- set to beginning of data section by loader
  10650. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10651. command*: POINTER TO ARRAY OF Command;
  10652. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10653. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10654. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10655. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10656. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10657. data*, code*: Bytes;
  10658. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10659. export*: ExportDesc;
  10660. term*: TerminationHandler;
  10661. exTable*: ExceptionTable;
  10662. noProcs*: LONGINT;
  10663. firstProc*: ADDRESS; <- done by loader
  10664. maxPtrs*: LONGINT;
  10665. crc*: LONGINT;
  10666. *)
  10667. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10668. BEGIN
  10669. Info(section, "cycle");
  10670. Size(section,0);
  10671. Info(section, "references");
  10672. Size(section,0);
  10673. Info(section, "nextMarked");
  10674. Address(section,0);
  10675. Info(section, "nextWatched");
  10676. Address(section,0);
  10677. END BasePointer;
  10678. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10679. BEGIN
  10680. BasePointer(section);
  10681. Info(section, "action");
  10682. Address(section,0);
  10683. Info(section, "monitor");
  10684. Address(section,0);
  10685. END BaseObject;
  10686. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10687. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10688. pooledName: Basic.SegmentedName;
  10689. symbol: SyntaxTree.Symbol;
  10690. BEGIN
  10691. Global.GetModuleName(module.module,name);
  10692. Strings.Append(name,".@Module.@Descriptor");
  10693. Basic.ToSegmentedName(name, pooledName);
  10694. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10695. Symbol(section,descriptorSection,0,0);
  10696. Info(descriptorSection, "descriptor");
  10697. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10698. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10699. Address(descriptorSection,0);
  10700. Global.GetModuleName(module.module,name);
  10701. Strings.Append(name,".@Trace");
  10702. Basic.ToSegmentedName(name, pooledName);
  10703. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10704. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10705. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10706. END ModuleDescriptor;
  10707. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10708. VAR name: ARRAY 128 OF CHAR;
  10709. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10710. symbol: SyntaxTree.Symbol;
  10711. BEGIN
  10712. Global.GetModuleName(module.module,name);
  10713. Strings.Append(name,".@Module");
  10714. Basic.ToSegmentedName(name, pooledName);
  10715. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10716. moduleSection.SetExported(TRUE);
  10717. IF moduleSection.pc = 0 THEN
  10718. IF implementationVisitor.backend.cooperative THEN
  10719. Info(moduleSection, "descriptor");
  10720. ModuleDescriptor(moduleSection);
  10721. BaseObject(moduleSection);
  10722. implementationVisitor.CreateTraceModuleMethod(module.module);
  10723. ELSE
  10724. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10725. Info(moduleSection, "HeapBlock");
  10726. Symbol(moduleSection,moduleSection,2,0);
  10727. Info(moduleSection, "TypeDescriptor");
  10728. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10729. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10730. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10731. END;
  10732. END;
  10733. RETURN moduleSection;
  10734. END ModuleSection;
  10735. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10736. VAR
  10737. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10738. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10739. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10740. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10741. referenceSectionOffset : LONGINT;
  10742. BEGIN
  10743. Global.GetModuleName(module.module,moduleName);
  10744. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10745. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10746. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10747. ImportsArray(importSection);
  10748. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10749. CommandArray(commandsSection);
  10750. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10751. ExceptionArray(exceptionSection);
  10752. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10753. TypeInfoSection(typeInfoSection);
  10754. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10755. References(referenceSection);
  10756. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10757. IF ~implementationVisitor.backend.cooperative THEN
  10758. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10759. ELSE
  10760. ptrTableSection := NIL;
  10761. END;
  10762. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10763. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10764. Array(emptyArraySection,temp,"");
  10765. moduleSection := ModuleSection();
  10766. Info(moduleSection, "nextRoot*: RootObject");
  10767. Address(moduleSection,0);
  10768. Info(moduleSection, "next*: Module");
  10769. Address(moduleSection,0);
  10770. Info(moduleSection, "name*: Name");
  10771. Name(moduleSection,moduleName);
  10772. Info(moduleSection, "init, published: BOOLEAN");
  10773. Boolean(moduleSection,FALSE);
  10774. Boolean(moduleSection,FALSE);
  10775. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10776. Boolean(moduleSection,FALSE);
  10777. Boolean(moduleSection,FALSE);
  10778. Info(moduleSection, "refcnt*: LONGINT");
  10779. Longint(moduleSection,0);
  10780. Info(moduleSection, "sb*: ADDRESS");
  10781. Address(moduleSection,0);
  10782. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10783. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10784. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10785. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10786. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10787. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10788. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10789. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10790. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10791. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10792. Info(moduleSection, "procTable*: ProcTable");
  10793. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10794. Info(moduleSection, "ptrTable*: PtrTable");
  10795. IF ~implementationVisitor.backend.cooperative THEN
  10796. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10797. ELSE
  10798. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10799. END;
  10800. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10801. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10802. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10803. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10804. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10805. Info(moduleSection, "export*: ExportDesc");
  10806. ExportDesc(moduleSection);
  10807. Info(moduleSection, "term*: TerminationHandler");
  10808. Address(moduleSection,0);
  10809. Info(moduleSection, "exTable*: ExceptionTable");
  10810. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10811. Info(moduleSection, "noProcs*: LONGINT");
  10812. Longint(moduleSection,numberProcs);
  10813. Info(moduleSection, "firstProc*: ADDRESS");
  10814. Address(moduleSection,0);
  10815. Info(moduleSection, "maxPtrs*: LONGINT");
  10816. Longint(moduleSection,maxPointers);
  10817. Info(moduleSection, "crc*: LONGINT");
  10818. Longint(moduleSection, 0); (*! must be implemented *)
  10819. Info(moduleSection, "body*: ADDRESS");
  10820. Symbol(moduleSection, bodyProc, 0,0);
  10821. IF implementationVisitor.backend.cooperative THEN
  10822. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10823. Info(moduleSection, "monitor.owner");
  10824. Address(moduleSection,0);
  10825. Info(moduleSection, "monitor.nestingLevel");
  10826. Address(moduleSection,0);
  10827. Info(moduleSection, "monitor.blockedQueue");
  10828. Address(moduleSection,0); Address(moduleSection,0);
  10829. Info(moduleSection, "monitor.waitingQueue");
  10830. Address(moduleSection,0); Address(moduleSection,0);
  10831. Info(moduleSection, "monitor.waitingSentinel");
  10832. Address(moduleSection,0);
  10833. END;
  10834. END Module;
  10835. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10836. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10837. BEGIN
  10838. Array(source,pc,"");
  10839. Info(source, "pointer offsets array data");
  10840. IF scope IS SyntaxTree.RecordScope THEN
  10841. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10842. ELSIF scope IS SyntaxTree.CellScope THEN
  10843. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10844. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10845. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10846. WHILE variable # NIL DO
  10847. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10848. symbol := module.allSections.FindBySymbol(variable);
  10849. ASSERT(symbol # NIL);
  10850. Pointers(0,symbol, source,variable.type,numberPointers);
  10851. END;
  10852. variable := variable.nextVariable;
  10853. END;
  10854. END;
  10855. PatchArray(source,pc,numberPointers);
  10856. END PointerArray;
  10857. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10858. VAR recordType: SyntaxTree.RecordType;
  10859. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10860. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10861. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10862. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10863. segmentedName: Basic.SegmentedName;
  10864. td: SyntaxTree.TypeDeclaration;
  10865. BEGIN
  10866. Array(source,pc,"Modules.FieldEntry");
  10867. Info(source, "FieldArray");
  10868. IF x IS SyntaxTree.RecordType THEN
  10869. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10870. ELSE
  10871. variable := NIL;
  10872. END;
  10873. size :=0;
  10874. WHILE variable # NIL DO
  10875. type := variable.type.resolved;
  10876. Info(source,"offset");
  10877. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10878. Info(source,"type class");
  10879. IF type IS SyntaxTree.PointerType THEN
  10880. Size(source, 1);
  10881. ELSIF type IS SyntaxTree.RecordType THEN
  10882. Size(source, 2);
  10883. ELSIF type IS SyntaxTree.NumberType THEN
  10884. Size(source, 3);
  10885. ELSE
  10886. Size(source, 0);
  10887. END;
  10888. Info(source, "type desc");
  10889. IF type IS SyntaxTree.RecordType THEN
  10890. td := type(SyntaxTree.RecordType).typeDeclaration;
  10891. Global.GetSymbolSegmentedName(td,segmentedName);
  10892. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10893. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10894. ELSE
  10895. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10896. END;
  10897. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10898. Symbol(source, tir, 0, offset);
  10899. ELSE
  10900. Address(source, 0);
  10901. END;
  10902. Info(source,"flags");
  10903. Set(source, {});
  10904. Info(source,"name");
  10905. variable.GetName(name);
  10906. LongName(source, name);
  10907. variable := variable.nextVariable;
  10908. INC(size);
  10909. END;
  10910. PatchArray(source,pc,size);
  10911. END FieldArray;
  10912. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10913. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10914. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10915. sectionName: Basic.SectionName;
  10916. CONST MPO=-40000000H;
  10917. BEGIN
  10918. (*
  10919. TypeDesc* = POINTER TO RECORD
  10920. descSize: LONGINT;
  10921. sentinel: LONGINT; (* = MPO-4 *)
  10922. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10923. flags*: SET;
  10924. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10925. name*: Name;
  10926. END;
  10927. *)
  10928. (* source := module.sections.FindByName(...) *)
  10929. Global.GetSymbolSegmentedName(td,name);
  10930. Basic.AppendToSegmentedName(name,"@Info");
  10931. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10932. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10933. Address(source,MPO-4);
  10934. Info(source, "type tag pointer");
  10935. Symbol( source, tag, offset, 0);
  10936. Info(source, "type flags");
  10937. flags := {};
  10938. IF isProtected THEN INCL(flags,31) END;
  10939. Set( source, flags);
  10940. Info(source, "pointer to module");
  10941. moduleSection := ModuleSection();
  10942. Symbol( source, moduleSection, moduleSection.pc,0);
  10943. Info(source, "type name");
  10944. i := 0;
  10945. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10946. (*
  10947. Global.GetSymbolSegmentedName(td,name);
  10948. Basic.SegmentedNameToString(name, sectionName);
  10949. *)
  10950. Name(source,sectionName);
  10951. source.SetReferenced(FALSE);
  10952. Global.GetSymbolSegmentedName(td,name);
  10953. Basic.AppendToSegmentedName(name,"@Fields");
  10954. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10955. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10956. Info(fieldSection, "HeapBlock");
  10957. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10958. Info(fieldSection, "TypeDescriptor");
  10959. Address(fieldSection,0);
  10960. Info(source, "FieldArray ref");
  10961. Symbol(source, fieldSection, fieldSection.pc, 0);
  10962. FieldArray(fieldSection);
  10963. RETURN source;
  10964. END NewTypeDescriptorInfo;
  10965. PROCEDURE NewTypeDescriptor;
  10966. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  10967. procedure: SyntaxTree.Procedure;
  10968. baseRecord: SyntaxTree.RecordType; baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  10969. numberPointers: LONGINT; padding,offset, i: LONGINT;
  10970. CONST MPO=-40000000H;
  10971. PROCEDURE TdTable(size: LONGINT);
  10972. VAR i: LONGINT;
  10973. BEGIN
  10974. Info(source, "tag table");
  10975. baseRecord := recordType;
  10976. i := 0;
  10977. WHILE baseRecord # NIL DO
  10978. INC(i);
  10979. baseRecord := baseRecord.GetBaseRecord();
  10980. END;
  10981. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  10982. WHILE i < size DO
  10983. Address(source,0);
  10984. INC(i);
  10985. END;
  10986. baseRecord := recordType;
  10987. WHILE baseRecord # NIL DO
  10988. baseTD := baseRecord.typeDeclaration;
  10989. Global.GetSymbolSegmentedName(baseTD,name);
  10990. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  10991. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  10992. ELSE
  10993. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  10994. END;
  10995. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*module.system.addressSize);
  10996. Symbol(source, tir, 0, offset);
  10997. baseRecord := baseRecord.GetBaseRecord();
  10998. END;
  10999. END TdTable;
  11000. PROCEDURE MethodTable;
  11001. VAR i,methods: LONGINT;
  11002. BEGIN
  11003. Info(source, "method table");
  11004. IF recordType # NIL THEN
  11005. methods := recordType.recordScope.numberMethods;
  11006. FOR i := methods-1 TO 0 BY -1 DO
  11007. procedure := recordType.recordScope.FindMethod(i);
  11008. Global.GetSymbolSegmentedName(procedure, name);
  11009. NamedSymbol(source, name,procedure, 0,0);
  11010. END;
  11011. END;
  11012. END MethodTable;
  11013. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11014. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11015. BEGIN
  11016. Info(source, "method table");
  11017. baseRecord := recordType;
  11018. WHILE baseRecord.baseType # NIL DO
  11019. baseRecord := baseRecord.GetBaseRecord ();
  11020. END;
  11021. GetRecordTypeName (baseRecord, name);
  11022. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11023. IF name = stackFrame THEN
  11024. start := 0;
  11025. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11026. baseRecord := recordType;
  11027. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11028. baseRecord := baseRecord.GetBaseRecord ();
  11029. END;
  11030. IF baseRecord # NIL THEN
  11031. GetRecordTypeName (baseRecord, name);
  11032. IF pointer & ~baseRecord.isObject THEN
  11033. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11034. END;
  11035. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11036. ELSIF recordType.isObject THEN
  11037. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11038. ELSIF pointer THEN
  11039. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11040. ELSE
  11041. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11042. END;
  11043. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11044. Symbol(source, tir, 0, 0);
  11045. start := 0;
  11046. baseRecord := recordType;
  11047. WHILE (baseRecord # NIL) DO
  11048. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11049. baseRecord := baseRecord.GetBaseRecord ();
  11050. END;
  11051. ELSE
  11052. (* explicit trace method: *)
  11053. procedure := recordType.recordScope.FindMethod(0);
  11054. IF ~procedure.isFinalizer THEN
  11055. Global.GetSymbolSegmentedName(procedure, name);
  11056. NamedSymbol(source, name,procedure, 0,0);
  11057. END;
  11058. start := 1;
  11059. END;
  11060. IF (name # stackFrame) & recordType.isObject THEN
  11061. baseRecord := recordType;
  11062. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11063. baseRecord := baseRecord.GetBaseRecord ();
  11064. END;
  11065. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11066. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11067. ELSE
  11068. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11069. END;
  11070. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11071. Symbol(source, tir, 0, 0);
  11072. END;
  11073. methods := recordType.recordScope.numberMethods;
  11074. FOR i := start TO methods-1 DO
  11075. procedure := recordType.recordScope.FindMethod(i);
  11076. IF ~procedure.isFinalizer THEN
  11077. Global.GetSymbolSegmentedName(procedure, name);
  11078. NamedSymbol(source, name,procedure, 0,0);
  11079. END;
  11080. END;
  11081. END CooperativeMethodTable;
  11082. BEGIN
  11083. Global.GetSymbolSegmentedName(td,name);
  11084. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11085. source.SetExported(IsExported(td));
  11086. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11087. IF implementationVisitor.backend.cooperative THEN
  11088. base := NIL;
  11089. baseRecord := recordType.GetBaseRecord();
  11090. IF baseRecord # NIL THEN
  11091. baseTD := baseRecord.typeDeclaration;
  11092. END;
  11093. IF ~recordType.isObject THEN
  11094. Info(source, "parent");
  11095. IF baseRecord # NIL THEN
  11096. Global.GetSymbolSegmentedName(baseTD,name);
  11097. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11098. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11099. ELSE
  11100. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11101. END;
  11102. Symbol(source, tir, 0, 0);
  11103. ELSE
  11104. Address(source,0);
  11105. END;
  11106. Info(source, "record size");
  11107. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11108. source.SetReferenced(FALSE);
  11109. CooperativeMethodTable(FALSE);
  11110. base := source;
  11111. Global.GetSymbolSegmentedName(td,name);
  11112. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11113. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11114. source.SetExported(IsExported(td));
  11115. source.SetReferenced(FALSE);
  11116. END;
  11117. Info(source, "parent");
  11118. IF baseRecord # NIL THEN
  11119. Global.GetSymbolSegmentedName(baseTD,name);
  11120. sym := baseTD;
  11121. IF ~recordType.isObject THEN
  11122. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11123. sym := NIL;
  11124. END;
  11125. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11126. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11127. ELSE
  11128. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11129. END;
  11130. Symbol(source, tir, 0, 0);
  11131. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11132. Address(source,0);
  11133. ELSE
  11134. IF recordType.isObject THEN
  11135. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11136. ELSE
  11137. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11138. END;
  11139. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11140. Symbol(source, tir, 0, 0);
  11141. END;
  11142. Info(source, "base record descriptor");
  11143. Symbol(source, base, 0, 0);
  11144. CooperativeMethodTable(TRUE);
  11145. source.SetReferenced(FALSE);
  11146. IF recordType.hasPointers THEN
  11147. IF ~HasExplicitTraceMethod (recordType) THEN
  11148. implementationVisitor.CreateTraceMethod(recordType);
  11149. END;
  11150. implementationVisitor.CreateResetProcedure(recordType);
  11151. implementationVisitor.CreateAssignProcedure(recordType);
  11152. END;
  11153. ELSIF ~simple THEN
  11154. (*
  11155. MethodEnd = MPO
  11156. ---
  11157. methods (# methods)
  11158. ---
  11159. tags (16)
  11160. ---
  11161. TypeDesc = TypeInfoAdr
  11162. ---
  11163. td adr ---> rec size
  11164. ----
  11165. pointer offsets
  11166. ----
  11167. (padding)
  11168. -----
  11169. empty [2 addresses aligned]
  11170. empty
  11171. empty
  11172. numPtrs
  11173. ---
  11174. pointer offsets
  11175. ---
  11176. *)
  11177. Info(source, "MethodEnd = MPO");
  11178. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11179. source(IntermediateCode.Section).Emit(Data(-1,op));
  11180. MethodTable;
  11181. TdTable(TypeTags);
  11182. Info(source, "type descriptor info pointer");
  11183. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11184. IF (cellType # NIL) THEN
  11185. IF cellType.sizeInBits < 0 THEN
  11186. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11187. END;
  11188. Info(source, "cell size");
  11189. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11190. ELSE
  11191. Info(source, "record size");
  11192. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11193. END;
  11194. Info(source, "pointer offsets pointer");
  11195. padding := 1- source.pc MOD 2;
  11196. Symbol(source, source, source.pc+1+padding,0);
  11197. IF padding >0 THEN
  11198. Info(source, "padding");
  11199. FOR i := 1 TO padding DO Address(source,0) END;
  11200. END;
  11201. IF cellType # NIL THEN
  11202. PointerArray(source, cellType.cellScope, numberPointers);
  11203. ELSE
  11204. PointerArray(source, recordType.recordScope, numberPointers);
  11205. END;
  11206. ELSE
  11207. (*
  11208. simple:
  11209. methods
  11210. tag(2)
  11211. tag(1)
  11212. td adr ---> tag(0)
  11213. td adr ---> size
  11214. *)
  11215. MethodTable;
  11216. TdTable(TypeTags);
  11217. Info(source, "record size");
  11218. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11219. source.SetReferenced(FALSE);
  11220. END;
  11221. END NewTypeDescriptor;
  11222. BEGIN
  11223. x := x.resolved;
  11224. IF (x IS SyntaxTree.PointerType) THEN
  11225. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11226. END;
  11227. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11228. recordType := x(SyntaxTree.RecordType);
  11229. td := x.typeDeclaration;
  11230. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11231. ASSERT(td # NIL);
  11232. section := module.allSections.FindBySymbol(td); (* TODO *)
  11233. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11234. IF implementationVisitor.newObjectFile THEN
  11235. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11236. NewTypeDescriptor
  11237. END;
  11238. ELSE
  11239. (* data section in intermediate code *)
  11240. Global.GetSymbolSegmentedName(td,name);
  11241. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11242. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11243. tir.Emit(Data(-1,op));
  11244. END;
  11245. END;
  11246. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11247. cellType := x(SyntaxTree.CellType);
  11248. td := x.typeDeclaration;
  11249. section := module.allSections.FindBySymbol(td); (* TODO *)
  11250. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11251. IF implementationVisitor.newObjectFile THEN
  11252. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11253. NewTypeDescriptor
  11254. END;
  11255. END;
  11256. END;
  11257. END
  11258. END CheckTypeDeclaration
  11259. END MetaDataGenerator;
  11260. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11261. VAR
  11262. trace-: BOOLEAN;
  11263. traceString-: SyntaxTree.IdentifierString;
  11264. traceModuleName-: SyntaxTree.IdentifierString;
  11265. newObjectFile-: BOOLEAN;
  11266. profile-: BOOLEAN;
  11267. noRuntimeChecks: BOOLEAN;
  11268. simpleMetaData-: BOOLEAN;
  11269. noAsserts: BOOLEAN;
  11270. optimize-: BOOLEAN;
  11271. cooperative-: BOOLEAN;
  11272. preregisterStatic-: BOOLEAN;
  11273. dump-: Basic.Writer;
  11274. cellsAreObjects: BOOLEAN;
  11275. PROCEDURE &InitIntermediateBackend*;
  11276. BEGIN
  11277. simpleMetaData := FALSE;
  11278. newObjectFile := FALSE;
  11279. InitBackend;
  11280. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11281. SetTraceModuleName(DefaultTraceModuleName);
  11282. END InitIntermediateBackend;
  11283. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11284. VAR
  11285. declarationVisitor: DeclarationVisitor;
  11286. implementationVisitor: ImplementationVisitor;
  11287. module: Sections.Module;
  11288. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11289. meta: MetaDataGenerator;
  11290. BEGIN
  11291. ResetError;
  11292. Global.GetSymbolName(x,name);
  11293. IF activeCellsSpecification # NIL THEN
  11294. GetDescription(instructionSet);
  11295. activeCellsSpecification.SetInstructionSet(instructionSet)
  11296. END;
  11297. NEW(module,x,system); (* backend structures *)
  11298. Global.GetModuleName(x, name);
  11299. module.SetModuleName(name);
  11300. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11301. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11302. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11303. declarationVisitor.Module(x,module);
  11304. IF newObjectFile & ~meta.simple THEN
  11305. meta.Module(implementationVisitor.moduleBodySection);
  11306. END;
  11307. GetDescription(platformName);
  11308. module.SetPlatformName(platformName);
  11309. RETURN module
  11310. END GenerateIntermediate;
  11311. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11312. BEGIN RETURN TRUE
  11313. END SupportedImmediate;
  11314. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11315. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11316. END ProcessSyntaxTreeModule;
  11317. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11318. VAR
  11319. result: Sections.Module;
  11320. traceName: Basic.MessageString;
  11321. BEGIN
  11322. ASSERT(intermediateCodeModule IS Sections.Module);
  11323. result := intermediateCodeModule(Sections.Module);
  11324. IF trace THEN
  11325. traceName := "intermediate code trace: ";
  11326. Strings.Append(traceName,traceString);
  11327. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11328. IF (traceString="") OR (traceString="*") THEN
  11329. result.Dump(dump);
  11330. dump.Update
  11331. ELSE
  11332. Sections.DumpFiltered(dump, result, traceString);
  11333. END
  11334. END;
  11335. RETURN result
  11336. END ProcessIntermediateCodeModule;
  11337. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11338. BEGIN instructionSet := "Intermediate";
  11339. END GetDescription;
  11340. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11341. BEGIN
  11342. SELF.newObjectFile := newObjectFile;
  11343. SELF.simpleMetaData := simpleMetaData;
  11344. END SetNewObjectFile;
  11345. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11346. BEGIN COPY(name, traceModuleName)
  11347. END SetTraceModuleName;
  11348. PROCEDURE DefineOptions(options: Options.Options);
  11349. BEGIN
  11350. DefineOptions^(options);
  11351. options.Add(0X,"trace",Options.String);
  11352. options.Add(0X,"runtime",Options.String);
  11353. options.Add(0X,"newObjectFile",Options.Flag);
  11354. options.Add(0X,"traceModule",Options.String);
  11355. options.Add(0X,"profile",Options.Flag);
  11356. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11357. options.Add(0X,"noAsserts",Options.Flag);
  11358. options.Add(0X,"metaData",Options.String);
  11359. options.Add('o',"optimize", Options.Flag);
  11360. options.Add(0X,"preregisterStatic", Options.Flag);
  11361. options.Add(0X,"cellsAreObjects", Options.Flag);
  11362. END DefineOptions;
  11363. PROCEDURE GetOptions(options: Options.Options);
  11364. VAR name,string: SyntaxTree.IdentifierString;
  11365. BEGIN
  11366. GetOptions^(options);
  11367. trace := options.GetString("trace",traceString);
  11368. profile := options.GetFlag("profile");
  11369. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11370. noAsserts := options.GetFlag("noAsserts");
  11371. cooperative := options.GetFlag("cooperative");
  11372. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11373. IF cooperative THEN
  11374. SetRuntimeModuleName("CPU") END
  11375. END;
  11376. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11377. simpleMetaData := TRUE
  11378. END;
  11379. IF options.GetString("metaData",string) THEN
  11380. IF string = "simple" THEN simpleMetaData := TRUE
  11381. ELSIF string ="full" THEN simpleMetaData := FALSE
  11382. END;
  11383. END;
  11384. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11385. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11386. optimize := options.GetFlag("optimize");
  11387. preregisterStatic := options.GetFlag("preregisterStatic");
  11388. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11389. END GetOptions;
  11390. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11391. BEGIN RETURN SymbolFileFormat.Get()
  11392. END DefaultSymbolFileFormat;
  11393. END IntermediateBackend;
  11394. (* ----------------------------------- register allocation ------------------------------------- *)
  11395. (* register mapping scheme
  11396. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11397. spill offset
  11398. part(n) --> ticket <--> physical register
  11399. spill offset
  11400. *)
  11401. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11402. emptyOperand: IntermediateCode.Operand;
  11403. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11404. statCoopResetVariables: LONGINT;
  11405. statCoopModifyAssignments: LONGINT;
  11406. modifyAssignmentsPC : LONGINT;
  11407. statCoopNilCheck: LONGINT;
  11408. statCoopSwitch: LONGINT;
  11409. statCoopAssignProcedure: LONGINT;
  11410. statCoopTraceMethod: LONGINT;
  11411. statCoopResetProcedure: LONGINT;
  11412. statCoopTraceModule: LONGINT;
  11413. PROCEDURE ResetStatistics*;
  11414. BEGIN
  11415. statCoopResetVariables := 0;
  11416. statCoopModifyAssignments := 0;
  11417. statCoopNilCheck:= 0;
  11418. statCoopSwitch:= 0;
  11419. statCoopAssignProcedure:= 0;
  11420. statCoopTraceMethod:= 0;
  11421. statCoopResetProcedure:= 0;
  11422. statCoopTraceModule:= 0;
  11423. END ResetStatistics;
  11424. PROCEDURE Statistics*;
  11425. BEGIN
  11426. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11427. TRACE(statCoopNilCheck, statCoopSwitch);
  11428. TRACE(statCoopAssignProcedure,
  11429. statCoopTraceMethod,
  11430. statCoopResetProcedure,
  11431. statCoopTraceModule)
  11432. END Statistics;
  11433. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11434. BEGIN
  11435. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11436. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11437. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11438. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11439. 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)
  11440. END
  11441. END PassBySingleReference;
  11442. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11443. BEGIN
  11444. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11445. END PassInRegister;
  11446. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11447. VAR new: RegisterEntry;
  11448. BEGIN
  11449. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11450. IF queue = NIL THEN
  11451. queue := new
  11452. ELSE
  11453. new.next := queue;
  11454. IF queue#NIL THEN queue.prev := new END;
  11455. queue := new
  11456. END;
  11457. END AddRegisterEntry;
  11458. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11459. VAR this: RegisterEntry;
  11460. BEGIN
  11461. this := queue;
  11462. WHILE (this # NIL) & (this.register # register) DO
  11463. this := this.next;
  11464. END;
  11465. IF this = NIL THEN
  11466. RETURN FALSE
  11467. END;
  11468. ASSERT(this # NIL);
  11469. IF this = queue THEN queue := queue.next END;
  11470. IF this.prev # NIL THEN this.prev.next := this.next END;
  11471. IF this.next # NIL THEN this.next.prev := this.prev END;
  11472. RETURN TRUE
  11473. END RemoveRegisterEntry;
  11474. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11475. BEGIN ASSERT(cond);
  11476. END Assert;
  11477. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11478. BEGIN
  11479. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11480. END ReusableRegister;
  11481. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11482. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11483. BEGIN
  11484. procedure := moduleScope.bodyProcedure;
  11485. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11486. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11487. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11488. procedure.SetScope(moduleScope);
  11489. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11490. procedure.SetAccess(SyntaxTree.Hidden);
  11491. moduleScope.SetBodyProcedure(procedure);
  11492. moduleScope.AddProcedure(procedure);
  11493. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11494. END;
  11495. END EnsureBodyProcedure;
  11496. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11497. VAR import: SyntaxTree.Import;
  11498. s: Basic.MessageString;
  11499. selfName: SyntaxTree.IdentifierString;
  11500. module: SyntaxTree.Module;
  11501. BEGIN
  11502. scope.ownerModule.GetName(selfName);
  11503. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11504. module := scope.ownerModule
  11505. ELSE
  11506. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11507. IF import = NIL THEN
  11508. RETURN NIL
  11509. ELSIF import.module = NIL THEN
  11510. RETURN NIL
  11511. ELSE module := import.module
  11512. END;
  11513. END;
  11514. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11515. END GetSymbol;
  11516. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11517. BEGIN
  11518. op.mode := mode;
  11519. IntermediateCode.InitOperand(op.op);
  11520. IntermediateCode.InitOperand(op.tag);
  11521. IntermediateCode.InitOperand(op.extra);
  11522. op.dimOffset := 0;
  11523. END InitOperand;
  11524. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11525. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11526. BEGIN RETURN IntermediateCode.GetType(system, type)
  11527. END GetType;
  11528. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11529. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11530. BEGIN
  11531. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11532. (*
  11533. UniqueId(name,module,name,adr);
  11534. *)
  11535. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11536. constant.SetValue(value);
  11537. module.moduleScope.AddConstant(constant);
  11538. constant.SetScope(module.moduleScope);
  11539. RETURN constant
  11540. END BuildConstant;
  11541. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11542. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11543. BEGIN
  11544. variable := scope.firstVariable;
  11545. WHILE variable # NIL DO
  11546. IF variable.NeedsTrace() THEN
  11547. RETURN TRUE;
  11548. END;
  11549. variable := variable.nextVariable;
  11550. END;
  11551. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11552. WHILE parameter # NIL DO
  11553. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11554. RETURN TRUE;
  11555. END;
  11556. parameter := parameter.nextParameter;
  11557. END;
  11558. RETURN FALSE;
  11559. END HasPointers;
  11560. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11561. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11562. END IsVariableParameter;
  11563. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11564. VAR parameter: SyntaxTree.Parameter;
  11565. BEGIN
  11566. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11567. WHILE parameter # NIL DO
  11568. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11569. IF parameter.movable THEN RETURN TRUE END;
  11570. parameter := parameter.nextParameter;
  11571. END;
  11572. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11573. END HasVariableParameters;
  11574. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11575. BEGIN
  11576. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11577. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11578. END HasExplicitTraceMethod;
  11579. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11580. BEGIN
  11581. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11582. IF (expression IS SyntaxTree.IntegerValue) THEN
  11583. val := expression(SyntaxTree.IntegerValue).value;
  11584. RETURN TRUE
  11585. ELSE
  11586. RETURN FALSE
  11587. END;
  11588. END IsIntegerConstant;
  11589. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11590. BEGIN
  11591. IF val <= 0 THEN RETURN FALSE END;
  11592. exp := 0;
  11593. WHILE ~ODD(val) DO
  11594. val := val DIV 2;
  11595. INC(exp)
  11596. END;
  11597. RETURN val = 1
  11598. END PowerOf2;
  11599. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11600. VAR procedure: SyntaxTree.Procedure;
  11601. BEGIN
  11602. procedure := record.recordScope.constructor;
  11603. IF procedure = NIL THEN
  11604. record := record.GetBaseRecord();
  11605. IF record # NIL THEN
  11606. procedure := GetConstructor(record)
  11607. END;
  11608. END;
  11609. RETURN procedure;
  11610. END GetConstructor;
  11611. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11612. BEGIN
  11613. value := SHORT(op.intValue);
  11614. RETURN op.mode = IntermediateCode.ModeImmediate;
  11615. END IsIntegerImmediate;
  11616. (** whether a type strictily is a pointer to record or object type
  11617. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11618. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11619. BEGIN
  11620. IF type = NIL THEN
  11621. RETURN FALSE
  11622. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11623. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11624. ELSE
  11625. RETURN FALSE
  11626. END
  11627. END IsStrictlyPointerToRecord;
  11628. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11629. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11630. END IsUnsafePointer;
  11631. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11632. BEGIN type := type.resolved;
  11633. IF type IS SyntaxTree.PointerType THEN
  11634. type := type(SyntaxTree.PointerType).pointerBase;
  11635. type := type.resolved;
  11636. IF type IS SyntaxTree.RecordType THEN
  11637. recordType := type(SyntaxTree.RecordType);
  11638. RETURN TRUE
  11639. ELSE
  11640. RETURN FALSE
  11641. END
  11642. ELSIF type IS SyntaxTree.RecordType THEN
  11643. recordType := type(SyntaxTree.RecordType);
  11644. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11645. ELSIF type IS SyntaxTree.ObjectType THEN
  11646. RETURN TRUE
  11647. ELSIF type IS SyntaxTree.AnyType THEN
  11648. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11649. ELSE
  11650. RETURN FALSE
  11651. END;
  11652. END IsPointerToRecord;
  11653. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11654. BEGIN
  11655. type := type.resolved;
  11656. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11657. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11658. END IsArrayOfSystemByte;
  11659. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11660. BEGIN
  11661. IF type = NIL THEN RETURN FALSE END;
  11662. type := type.resolved;
  11663. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11664. END IsOpenArray;
  11665. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11666. BEGIN
  11667. IF type = NIL THEN RETURN FALSE END;
  11668. type := type.resolved;
  11669. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11670. END IsStaticArray;
  11671. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11672. VAR size: LONGINT;
  11673. BEGIN
  11674. size := 1;
  11675. WHILE (IsStaticArray(type)) DO
  11676. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11677. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11678. END;
  11679. RETURN size;
  11680. END StaticArrayNumElements;
  11681. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11682. BEGIN
  11683. WHILE (IsStaticArray(type)) DO
  11684. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11685. END;
  11686. RETURN type;
  11687. END StaticArrayBaseType;
  11688. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11689. BEGIN
  11690. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11691. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11692. END;
  11693. RETURN type;
  11694. END ArrayBaseType;
  11695. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11696. BEGIN
  11697. IF type = NIL THEN RETURN FALSE END;
  11698. type := type.resolved;
  11699. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11700. END IsDelegate;
  11701. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11702. VAR i: LONGINT;
  11703. BEGIN
  11704. i := 0; type := type.resolved;
  11705. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11706. INC(i);
  11707. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11708. END;
  11709. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11710. INC(i);
  11711. type := type(SyntaxTree.MathArrayType).arrayBase;
  11712. IF type # NIL THEN type := type.resolved END;
  11713. END;
  11714. RETURN i
  11715. END DynamicDim;
  11716. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11717. BEGIN
  11718. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11719. type := type(SyntaxTree.ArrayType).arrayBase;
  11720. END;
  11721. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11722. type := type(SyntaxTree.MathArrayType).arrayBase;
  11723. END;
  11724. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11725. END StaticSize;
  11726. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11727. BEGIN
  11728. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11729. END IsImmediate;
  11730. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11731. BEGIN
  11732. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11733. END IsAddress;
  11734. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11735. BEGIN
  11736. RETURN (x.mode = IntermediateCode.ModeRegister);
  11737. END IsRegister;
  11738. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11739. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11740. BEGIN
  11741. typeDeclaration := recordType.typeDeclaration;
  11742. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11743. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11744. END GetRecordTypeName;
  11745. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11746. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11747. BEGIN
  11748. parSize := 0;
  11749. IF StructuredReturnType(procedureType) THEN
  11750. parameter := procedureType.returnParameter;
  11751. INC(parSize,system.SizeOfParameter(parameter));
  11752. parSize := parSize + (-parSize) MOD system.addressSize;
  11753. END;
  11754. parameter :=procedureType.lastParameter;
  11755. WHILE (parameter # NIL) DO
  11756. INC(parSize,system.SizeOfParameter(parameter));
  11757. parSize := parSize + (-parSize) MOD system.addressSize;
  11758. parameter := parameter.prevParameter;
  11759. END;
  11760. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11761. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11762. RETURN ToMemoryUnits(system,parSize)
  11763. END ParametersSize;
  11764. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11765. BEGIN
  11766. IF type = NIL THEN RETURN FALSE
  11767. ELSE
  11768. type := type.resolved;
  11769. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11770. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11771. END
  11772. END ReturnedAsParameter;
  11773. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11774. BEGIN
  11775. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11776. END StructuredReturnType;
  11777. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11778. BEGIN
  11779. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11780. END IsNested;
  11781. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11782. BEGIN
  11783. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11784. scope := scope.outerScope;
  11785. END;
  11786. RETURN scope # NIL;
  11787. END InCellScope;
  11788. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11789. BEGIN
  11790. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11791. RETURN 0
  11792. ELSE
  11793. *)
  11794. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11795. (*END;*)
  11796. END ProcedureParametersSize;
  11797. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11798. VAR dataUnit: LONGINT;
  11799. BEGIN dataUnit := system.dataUnit;
  11800. ASSERT(size MOD system.dataUnit = 0);
  11801. RETURN size DIV system.dataUnit
  11802. END ToMemoryUnits;
  11803. PROCEDURE Get*(): Backend.Backend;
  11804. VAR backend: IntermediateBackend;
  11805. BEGIN NEW(backend); RETURN backend
  11806. END Get;
  11807. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11808. VAR instruction: IntermediateCode.Instruction;
  11809. BEGIN
  11810. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11811. RETURN instruction
  11812. END Nop;
  11813. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11814. VAR instruction: IntermediateCode.Instruction;
  11815. BEGIN
  11816. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11817. RETURN instruction
  11818. END Mov;
  11819. (* like Mov but ensures that no new register will be allocated for dest *)
  11820. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11821. VAR instruction: IntermediateCode.Instruction;
  11822. BEGIN
  11823. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11824. RETURN instruction
  11825. END MovReplace;
  11826. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11827. VAR instruction: IntermediateCode.Instruction;
  11828. BEGIN
  11829. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11830. RETURN instruction
  11831. END Conv;
  11832. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11833. VAR instruction: IntermediateCode.Instruction;
  11834. BEGIN
  11835. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11836. RETURN instruction
  11837. END Call;
  11838. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11839. VAR op1, op2: IntermediateCode.Operand;
  11840. VAR instruction: IntermediateCode.Instruction;
  11841. BEGIN
  11842. IntermediateCode.InitNumber(op1,pcOffset);
  11843. IntermediateCode.InitNumber(op2,callingConvention);
  11844. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11845. RETURN instruction
  11846. END Exit;
  11847. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11848. VAR instruction: IntermediateCode.Instruction;
  11849. BEGIN
  11850. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11851. RETURN instruction
  11852. END Return;
  11853. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11854. VAR instruction: IntermediateCode.Instruction;
  11855. BEGIN
  11856. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11857. RETURN instruction
  11858. END Result;
  11859. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11860. VAR op1: IntermediateCode.Operand;
  11861. VAR instruction: IntermediateCode.Instruction;
  11862. BEGIN
  11863. IntermediateCode.InitNumber(op1,nr);
  11864. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11865. RETURN instruction
  11866. END Trap;
  11867. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11868. VAR instruction: IntermediateCode.Instruction;
  11869. BEGIN
  11870. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11871. RETURN instruction
  11872. END Br;
  11873. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11874. VAR instruction: IntermediateCode.Instruction;
  11875. BEGIN
  11876. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11877. RETURN instruction
  11878. END Breq;
  11879. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11880. VAR instruction: IntermediateCode.Instruction;
  11881. BEGIN
  11882. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11883. RETURN instruction
  11884. END Brne;
  11885. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11886. VAR instruction: IntermediateCode.Instruction;
  11887. BEGIN
  11888. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11889. RETURN instruction
  11890. END Brge;
  11891. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11892. VAR instruction: IntermediateCode.Instruction;
  11893. BEGIN
  11894. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11895. RETURN instruction
  11896. END Brlt;
  11897. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11898. VAR instruction: IntermediateCode.Instruction;
  11899. BEGIN
  11900. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11901. RETURN instruction
  11902. END Pop;
  11903. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11904. VAR instruction: IntermediateCode.Instruction;
  11905. BEGIN
  11906. ASSERT(op.mode # IntermediateCode.Undefined);
  11907. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11908. RETURN instruction
  11909. END Push;
  11910. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11911. VAR instruction: IntermediateCode.Instruction;
  11912. BEGIN
  11913. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11914. RETURN instruction
  11915. END Neg;
  11916. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11917. VAR instruction: IntermediateCode.Instruction;
  11918. BEGIN
  11919. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11920. RETURN instruction
  11921. END Not;
  11922. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11923. VAR instruction: IntermediateCode.Instruction;
  11924. BEGIN
  11925. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11926. RETURN instruction
  11927. END Abs;
  11928. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11929. VAR instruction: IntermediateCode.Instruction;
  11930. BEGIN
  11931. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  11932. RETURN instruction
  11933. END Mul;
  11934. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11935. VAR instruction: IntermediateCode.Instruction;
  11936. BEGIN
  11937. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  11938. RETURN instruction
  11939. END Div;
  11940. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11941. VAR instruction: IntermediateCode.Instruction;
  11942. BEGIN
  11943. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  11944. RETURN instruction
  11945. END Mod;
  11946. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11947. VAR instruction: IntermediateCode.Instruction;
  11948. BEGIN
  11949. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  11950. RETURN instruction
  11951. END Sub;
  11952. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11953. VAR instruction: IntermediateCode.Instruction;
  11954. BEGIN
  11955. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  11956. RETURN instruction
  11957. END Add;
  11958. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11959. VAR instruction: IntermediateCode.Instruction;
  11960. BEGIN
  11961. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  11962. RETURN instruction
  11963. END And;
  11964. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11965. VAR instruction: IntermediateCode.Instruction;
  11966. BEGIN
  11967. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  11968. RETURN instruction
  11969. END Or;
  11970. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11971. VAR instruction: IntermediateCode.Instruction;
  11972. BEGIN
  11973. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  11974. RETURN instruction
  11975. END Xor;
  11976. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11977. VAR instruction: IntermediateCode.Instruction;
  11978. BEGIN
  11979. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  11980. RETURN instruction
  11981. END Shl;
  11982. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11983. VAR instruction: IntermediateCode.Instruction;
  11984. BEGIN
  11985. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  11986. RETURN instruction
  11987. END Shr;
  11988. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11989. VAR instruction: IntermediateCode.Instruction;
  11990. BEGIN
  11991. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  11992. RETURN instruction
  11993. END Rol;
  11994. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11995. VAR instruction: IntermediateCode.Instruction;
  11996. BEGIN
  11997. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  11998. RETURN instruction
  11999. END Ror;
  12000. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12001. VAR instruction: IntermediateCode.Instruction;
  12002. BEGIN
  12003. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12004. RETURN instruction
  12005. END Cas;
  12006. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12007. VAR instruction: IntermediateCode.Instruction;
  12008. BEGIN
  12009. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12010. RETURN instruction
  12011. END Copy;
  12012. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12013. VAR instruction: IntermediateCode.Instruction;
  12014. BEGIN
  12015. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12016. RETURN instruction
  12017. END Fill;
  12018. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12019. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12020. BEGIN
  12021. string := IntermediateCode.String(s);
  12022. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12023. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12024. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12025. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12026. RETURN instruction
  12027. END Asm;
  12028. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12029. VAR instruction: IntermediateCode.Instruction;
  12030. BEGIN
  12031. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12032. RETURN instruction
  12033. END Data;
  12034. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12035. VAR instruction: IntermediateCode.Instruction;
  12036. BEGIN
  12037. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12038. IntermediateCode.SetSubType(instruction, subtype);
  12039. RETURN instruction
  12040. END SpecialInstruction;
  12041. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12042. VAR op1: IntermediateCode.Operand;
  12043. VAR instruction: IntermediateCode.Instruction;
  12044. BEGIN
  12045. (*! generate a warning if size exceeds a certain limit *)
  12046. (*
  12047. ASSERT(bytes < 1000000); (* sanity check *)
  12048. *)
  12049. ASSERT(0 <= units); (* sanity check *)
  12050. IntermediateCode.InitNumber(op1,units);
  12051. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12052. RETURN instruction
  12053. END Reserve;
  12054. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12055. VAR op1: IntermediateCode.Operand;
  12056. VAR instruction: IntermediateCode.Instruction;
  12057. BEGIN
  12058. IntermediateCode.InitNumber(op1,position);
  12059. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12060. RETURN instruction
  12061. END LabelInstruction;
  12062. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12063. VAR pc: LONGINT;
  12064. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12065. BEGIN
  12066. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12067. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12068. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12069. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12070. IF instr.op1.type.form = IntermediateCode.Float THEN
  12071. RETURN instr.op1.floatValue = op.floatValue
  12072. ELSE
  12073. RETURN instr.op1.intValue = op.intValue
  12074. END;
  12075. END ProvidesValue;
  12076. BEGIN
  12077. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12078. pc := 0;
  12079. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12080. INC(pc);
  12081. END;
  12082. IF pc = data.pc THEN
  12083. data.Emit(Data(-1,vop));
  12084. END;
  12085. RETURN pc
  12086. END EnterImmediate;
  12087. PROCEDURE Init;
  12088. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12089. BEGIN
  12090. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12091. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12092. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12093. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12094. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12095. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12096. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12097. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12098. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12099. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12100. IntermediateCode.InitOperand(emptyOperand);
  12101. FOR i := 0 TO NumberSystemCalls-1 DO
  12102. name := "@SystemCall";
  12103. Basic.AppendNumber(name,i);
  12104. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12105. END;
  12106. END Init;
  12107. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12108. BEGIN
  12109. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12110. END IsExported;
  12111. BEGIN
  12112. Init;
  12113. END FoxIntermediateBackend.
  12114. Compiler.Compile FoxIntermediateBackend.Mod ~