FoxIntermediateBackend.Mod 518 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311
  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. IF ~procedureType.noPAF THEN (* no procedure activation frame ! *)
  513. implementationVisitor.EmitEnter(ir,x.position,x,cc,ToMemoryUnits(system,stackSize),registerNumber);
  514. END;
  515. pc := ir.pc-1;
  516. (*
  517. ir.Emit(Nop(position)); (* placeholder for fill *)
  518. *)
  519. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  520. formalParameter := procedureType.lastParameter;
  521. WHILE (formalParameter # NIL) & (registerNumber < system.registerParameters) DO
  522. IF ~PassInRegister(formalParameter) THEN
  523. Error(formalParameter.position,"Calling convention error: cannot be passed as register");
  524. ELSE
  525. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(registerNumber));
  526. type := GetType(system, formalParameter.type);
  527. src := IntermediateCode.Register(type, registerClass, implementationVisitor.AcquireRegister(type, registerClass));
  528. IntermediateCode.InitMemory(dest,GetType(system,formalParameter.type),implementationVisitor.fp,ToMemoryUnits(system,formalParameter.offsetInBits));
  529. ir.Emit(Mov(-1,dest, src));
  530. implementationVisitor.ReleaseIntermediateOperand(src);
  531. INC(registerNumber);
  532. formalParameter := formalParameter.prevParameter;
  533. END;
  534. END;
  535. END;
  536. ir.EnterValidPAF;
  537. END;
  538. implementationVisitor.tagsAvailable := procedureType.callingConvention = SyntaxTree.OberonCallingConvention;
  539. implementationVisitor.Body(scope.body,currentScope,ir,isModuleBody);
  540. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  541. IF scope.lastVariable # NIL THEN
  542. stackSize := scope.lastVariable.offsetInBits;
  543. IF stackSize <0 THEN stackSize := -stackSize END;
  544. Basic.Align(stackSize,system.AlignmentOf(system.parameterAlignment,system.byteType)); (* round up to parameter alignment *)
  545. END;
  546. END;
  547. IF ~inline & ~(procedureType.noPAF) & ~x.isEntry & ~x.isExit THEN
  548. IF ToMemoryUnits(system,stackSize) > 4*1024-256 THEN (* stack frame potentially larger than page size *) (*! get page size from backend *)
  549. (*! unnecessary with new implementation of ENTER -- should potentially be called by backend
  550. IF implementationVisitor.GetRuntimeProcedure(implementationVisitor.runtimeModuleName,"EnsureAllocatedStack",procedure,TRUE) THEN
  551. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,stackSize+256));
  552. ir.EmitAt(pc,Push(size));
  553. implementationVisitor.StaticCallOperand(result,procedure);
  554. ir.EmitAt(pc+1,Call(result.op,ProcedureParametersSize(system,procedure)));
  555. END;
  556. *)
  557. END;
  558. ir.EmitAt(pc(*+2*),implementationVisitor.Enter(x.position,cc,ToMemoryUnits(system,stackSize))); (*!!*)
  559. IF stackSize > 0 THEN
  560. IF (stackSize MOD system.addressSize = 0) THEN
  561. null := IntermediateCode.Immediate(addressType,0);
  562. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  563. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-system.addressSize));
  564. size := IntermediateCode.Immediate(addressType,stackSize DIV system.addressSize);
  565. ELSE
  566. null := IntermediateCode.Immediate(int8,0);
  567. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  568. IntermediateCode.AddOffset(fp,ToMemoryUnits(system,-null.type.sizeInBits));
  569. size := IntermediateCode.Immediate(addressType,stackSize DIV null.type.sizeInBits);
  570. END;
  571. (*! should potentially be called by backend -- enter might initialize
  572. ir.EmitAt(pc+3,Fill(fp,null,size,TRUE));
  573. *)
  574. END;
  575. ir.ExitValidPAF;
  576. IF (procedureType.returnType = NIL) OR (scope.body.code # NIL) THEN
  577. finalizer := FALSE;
  578. IF backend.cooperative & x.isFinalizer THEN
  579. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  580. Basic.ToSegmentedName("BaseTypes.Object", baseObject);
  581. GetRecordTypeName(recordType,name);
  582. finalizer := (name # baseObject) & (recordType.baseType = NIL);
  583. END;
  584. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  585. IF backend.cooperative THEN
  586. IF HasPointers (scope) THEN implementationVisitor.ResetVariables(scope); END;
  587. IF implementationVisitor.profile & ~isModuleBody THEN
  588. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE)
  589. END;
  590. END;
  591. implementationVisitor.EmitLeave(ir, x.position,cc);
  592. IF finalizer THEN
  593. Basic.ToSegmentedName("BaseTypes.Object.Finalize", name);
  594. IntermediateCode.InitAddress(dest, addressType, name , 0, 0);
  595. ir.Emit(Br(x.position,dest));
  596. ELSE
  597. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  598. END;
  599. ELSE
  600. IF ~scope.body.isUnchecked & ~backend.noRuntimeChecks THEN
  601. implementationVisitor.EmitTrap(x.position,ReturnTrap);
  602. END;
  603. implementationVisitor.SetLabel(implementationVisitor.exitLabel);
  604. IF backend.cooperative THEN
  605. IF HasPointers (scope) THEN
  606. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  607. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  608. ir.Emit(Result(x.position, res));
  609. ir.Emit(Push(x.position, res));
  610. implementationVisitor.ResetVariables(scope);
  611. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  612. ir.Emit(Pop(x.position, res));
  613. ir.Emit(Return(x.position, res));
  614. ELSE
  615. implementationVisitor.ResetVariables(scope);
  616. IF implementationVisitor.profile & ~isModuleBody THEN implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE) END;
  617. END;
  618. ELSIF implementationVisitor.profile & ~isModuleBody THEN
  619. IF ~ReturnedAsParameter(procedureType.returnType) THEN
  620. res := implementationVisitor.NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  621. ir.Emit(Result(x.position, res));
  622. ir.Emit(Push(x.position, res));
  623. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  624. ir.Emit(Pop(x.position, res));
  625. ir.Emit(Return(x.position, res));
  626. ELSE
  627. implementationVisitor.ProfilerEnterExit(implementationVisitor.numberProcedures-1, FALSE);
  628. END;
  629. END;
  630. implementationVisitor.EmitLeave(ir,x.position,cc);
  631. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  632. ELSE
  633. ir.Emit(Nop(x.position));
  634. END;
  635. END
  636. END;
  637. ELSE (* force body for procedures *)
  638. implementationVisitor.EmitEnter(ir, x.position,x,cc,0,0);
  639. ir.EnterValidPAF;
  640. implementationVisitor.usedRegisters := NIL;
  641. implementationVisitor.Body(scope.body,currentScope,ir,x = module.module.moduleScope.bodyProcedure);
  642. IF implementationVisitor.usedRegisters # NIL THEN D.TraceBack END;
  643. ir.ExitValidPAF;
  644. implementationVisitor.EmitLeave(ir,x.position,cc);
  645. ir.Emit(Exit(x.position,procedureType.pcOffset,cc));
  646. END;
  647. Scope(scope);
  648. Signature;
  649. IF (x IS SyntaxTree.Operator) & x(SyntaxTree.Operator).isDynamic THEN implementationVisitor.RegisterDynamicOperator(x(SyntaxTree.Operator)) END;
  650. currentScope := prevScope;
  651. END Procedure;
  652. PROCEDURE AddBodyCallStub(bodyProcedure: SyntaxTree.Procedure); (* code that is only necessary for static linkers *)
  653. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope; name: Basic.SegmentedName;
  654. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  655. BEGIN
  656. ASSERT (bodyProcedure # NIL);
  657. procedureScope := SyntaxTree.NewProcedureScope(bodyProcedure.scope);
  658. procedure := SyntaxTree.NewProcedure(-1,SyntaxTree.NewIdentifier("@BodyStub"), procedureScope);
  659. procedure.SetScope(bodyProcedure.scope);
  660. procedure.SetType(SyntaxTree.NewProcedureType(-1,bodyProcedure.scope));
  661. procedure.SetAccess(SyntaxTree.Hidden);
  662. Global.GetSymbolSegmentedName (procedure,name);
  663. ir := implementationVisitor.NewSection(module.allSections, Sections.InitCodeSection, name,procedure,dump);
  664. ir.SetExported(TRUE);
  665. ir.SetPriority(InitPriority);
  666. Global.GetSymbolSegmentedName (bodyProcedure,name);
  667. IF (backend.newObjectFile OR backend.cooperative) & ~meta.simple THEN
  668. implementationVisitor.currentScope := module.module.moduleScope;
  669. implementationVisitor.section := ir;
  670. implementationVisitor.PushSelfPointer();
  671. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Register",1);
  672. ELSIF backend.preregisterStatic THEN
  673. implementationVisitor.currentScope := module.module.moduleScope;
  674. implementationVisitor.section := ir;
  675. implementationVisitor.PushSelfPointer();
  676. implementationVisitor.CallThis(bodyProcedure.position,"Modules","Preregister",1);
  677. ELSE
  678. IntermediateCode.InitAddress(op, addressType, name, implementationVisitor.GetFingerprint(bodyProcedure), 0);
  679. ir.Emit(Call(bodyProcedure.position,op, 0));
  680. END;
  681. END AddBodyCallStub;
  682. PROCEDURE AddStackAllocation(symbol: SyntaxTree.Symbol; initStack: LONGINT); (* code that is only necessary for static linkers *)
  683. VAR name: Basic.SegmentedName;
  684. ir: IntermediateCode.Section; op: IntermediateCode.Operand;
  685. BEGIN
  686. Global.GetSymbolSegmentedName (symbol,name);
  687. Basic.RemoveSuffix(name);
  688. Basic.SuffixSegmentedName(name, Basic.MakeString("@StackAllocation"));
  689. ir := implementationVisitor.NewSection(module.allSections,Sections.InitCodeSection,name,NIL,dump);
  690. ir.SetExported(TRUE);
  691. ir.SetPriority(FirstPriority);
  692. IntermediateCode.InitImmediate(op,addressType,initStack);
  693. ir.Emit(Mov(-1,implementationVisitor.sp,op));
  694. END AddStackAllocation;
  695. (** entry function to visit a complete module *)
  696. PROCEDURE Module(x: SyntaxTree.Module; module: Sections.Module);
  697. VAR
  698. ir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; idstr: SyntaxTree.IdentifierString;
  699. hasDynamicOperatorDeclarations: BOOLEAN;
  700. operator: SyntaxTree.Operator;
  701. import: SyntaxTree.Import;
  702. PROCEDURE TypeNeedsInitialization(type: SyntaxTree.Type): BOOLEAN;
  703. BEGIN
  704. type := type.resolved;
  705. IF type IS SyntaxTree.RecordType THEN
  706. IF ScopeNeedsInitialization(type(SyntaxTree.RecordType).recordScope) THEN RETURN TRUE END;
  707. ELSIF (type IS SyntaxTree.ArrayType) THEN
  708. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  709. IF TypeNeedsInitialization(type(SyntaxTree.ArrayType).arrayBase) THEN RETURN TRUE END;
  710. END;
  711. ELSIF type IS SyntaxTree.MathArrayType THEN
  712. WITH type: SyntaxTree.MathArrayType DO
  713. IF type.form = SyntaxTree.Open THEN
  714. RETURN TRUE
  715. ELSIF type.form = SyntaxTree.Static THEN
  716. IF TypeNeedsInitialization(type.arrayBase) THEN RETURN TRUE END;
  717. END;
  718. END;
  719. END;
  720. RETURN FALSE
  721. END TypeNeedsInitialization;
  722. PROCEDURE ScopeNeedsInitialization(scope: SyntaxTree.Scope): BOOLEAN;
  723. VAR variable: SyntaxTree.Variable;
  724. BEGIN
  725. variable := scope.firstVariable;
  726. WHILE variable # NIL DO
  727. IF TypeNeedsInitialization(variable.type) THEN RETURN TRUE END;
  728. IF variable.initializer # NIL THEN RETURN TRUE END;
  729. variable := variable.nextVariable;
  730. END;
  731. RETURN FALSE
  732. END ScopeNeedsInitialization;
  733. BEGIN
  734. ASSERT(x # NIL); ASSERT(module # NIL);
  735. SELF.module := module;
  736. (* add import names to the generated Sections.Module *)
  737. import := x.moduleScope.firstImport;
  738. WHILE import # NIL DO
  739. import.module.GetName(idstr);
  740. module.imports.AddName(idstr);
  741. import := import.nextImport
  742. END;
  743. IF ~implementationVisitor.newObjectFile & ~meta.simple THEN
  744. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@moduleSelf"));
  745. moduleSelf := SyntaxTree.NewVariable(0,SyntaxTree.NewIdentifier("@moduleSelf"));
  746. moduleSelf.SetType(system.anyType);
  747. moduleSelf.SetScope(x.moduleScope);
  748. moduleSelf.SetUntraced(TRUE);
  749. ir := implementationVisitor.NewSection(module.allSections, Sections.ConstSection, name,moduleSelf,dump); ir.SetOffset(0);
  750. ir.SetExported(TRUE);
  751. IntermediateCode.InitImmediate(op,addressType,0);
  752. ir.Emit(Data(-1,op));
  753. END;
  754. implementationVisitor.module := module;
  755. implementationVisitor.moduleScope := x.moduleScope;
  756. implementationVisitor.moduleSelf := moduleSelf;
  757. implementationVisitor.canBeLoaded := TRUE;
  758. meta.SetModule(module);
  759. IF (forceModuleBody OR implementationVisitor.newObjectFile & ~meta.simple OR ScopeNeedsInitialization(x.moduleScope)) THEN
  760. EnsureBodyProcedure(x.moduleScope); (* currently needed in Oberon, remove ? *)
  761. END;
  762. IF backend.profile THEN
  763. EnsureBodyProcedure(x.moduleScope);
  764. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@ModuleId"));
  765. implementationVisitor.profileId := implementationVisitor.NewSection(module.allSections, Sections.VarSection, name,NIL,dump);
  766. implementationVisitor.profileId.Emit(Reserve(-1,ToMemoryUnits(system,system.SizeOf(system.longintType))));
  767. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@InitProfiler"));
  768. implementationVisitor.profileInit := implementationVisitor.NewSection(module.allSections, Sections.CodeSection, name,NIL,dump);
  769. implementationVisitor.EmitEnter(implementationVisitor.profileInit,-1,NIL,0,0,0);
  770. Global.GetModuleName(module.module,idstr);
  771. implementationVisitor.ProfilerAddModule(idstr);
  772. implementationVisitor.numberProcedures := 0;
  773. END;
  774. implementationVisitor.profile := backend.profile;
  775. (* check if there is at least one dynamic operator locally defined *)
  776. hasDynamicOperatorDeclarations := FALSE;
  777. operator := x.moduleScope.firstOperator;
  778. WHILE operator # NIL DO
  779. IF operator.isDynamic THEN hasDynamicOperatorDeclarations := TRUE END;
  780. operator := operator.nextOperator
  781. END;
  782. (* add operator initialization code section *)
  783. IF hasDynamicOperatorDeclarations THEN
  784. EnsureBodyProcedure(x.moduleScope);
  785. Global.GetModuleSegmentedName(module.module,name); Basic.SuffixSegmentedName(name, Basic.MakeString("@OperatorInitialization"));
  786. implementationVisitor.operatorInitializationCodeSection := implementationVisitor.NewSection(module.allSections, Sections.CodeSection,name, NIL, dump);
  787. implementationVisitor.EmitEnter(implementationVisitor.operatorInitializationCodeSection,-1,NIL,0,0,0);
  788. END;
  789. Scope(x.moduleScope);
  790. IF hasDynamicOperatorDeclarations THEN
  791. implementationVisitor.EmitLeave(implementationVisitor.operatorInitializationCodeSection,-1,0);
  792. implementationVisitor.operatorInitializationCodeSection.Emit(Exit(-1,0,0));
  793. END;
  794. IF backend.profile THEN
  795. implementationVisitor.ProfilerPatchInit;
  796. END;
  797. END Module;
  798. END DeclarationVisitor;
  799. UsedArray*=POINTER TO ARRAY OF RECORD count: LONGINT; map: LONGINT; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass END;
  800. RegisterUsageCount*=OBJECT
  801. VAR used: UsedArray; count: LONGINT;
  802. PROCEDURE &Init;
  803. VAR i: LONGINT;
  804. BEGIN
  805. count := 0;
  806. IF used = NIL THEN NEW(used,64); END;
  807. FOR i := 0 TO LEN(used)-1 DO used[i].count := 0; used[i].map := i END;
  808. END Init;
  809. PROCEDURE Grow;
  810. VAR new: UsedArray; size,i: LONGINT;
  811. BEGIN
  812. size := LEN(used)*2;
  813. NEW(new,size);
  814. FOR i := 0 TO LEN(used)-1 DO
  815. new[i].count := used[i].count;
  816. new[i].type := used[i].type;
  817. new[i].map := used[i].map
  818. END;
  819. FOR i := LEN(used) TO LEN(new)-1 DO new[i].count := 0 END;
  820. used := new
  821. END Grow;
  822. PROCEDURE Next(type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  823. BEGIN
  824. INC(count);
  825. IF count = LEN(used) THEN Grow END;
  826. used[count].type := type;
  827. used[count].class := class;
  828. used[count].map := count;
  829. RETURN count;
  830. END Next;
  831. PROCEDURE IncUse(register: LONGINT);
  832. BEGIN
  833. INC(used[register].count);
  834. (*
  835. IF (register = 1) & (count > 30) THEN
  836. D.TraceBack;
  837. END;
  838. *)
  839. END IncUse;
  840. PROCEDURE DecUse(register: LONGINT);
  841. BEGIN
  842. DEC(used[register].count);
  843. END DecUse;
  844. PROCEDURE Map(register: LONGINT): LONGINT;
  845. VAR map : LONGINT;
  846. BEGIN
  847. IF register > 0 THEN
  848. map := used[register].map;
  849. WHILE register # map DO register := map; map := used[register].map END;
  850. END;
  851. RETURN register
  852. END Map;
  853. PROCEDURE Remap(register: LONGINT; to: LONGINT);
  854. BEGIN
  855. used[register].map:= to;
  856. used[to].count := used[register].count;
  857. used[register].count := 0;
  858. END Remap;
  859. PROCEDURE Use(register: LONGINT): LONGINT;
  860. BEGIN
  861. IF register< LEN(used) THEN
  862. RETURN used[register].count
  863. ELSE
  864. RETURN 0
  865. END
  866. END Use;
  867. END RegisterUsageCount;
  868. RegisterEntry = POINTER TO RECORD
  869. prev,next: RegisterEntry;
  870. register: LONGINT;
  871. registerClass: IntermediateCode.RegisterClass;
  872. type: IntermediateCode.Type;
  873. END;
  874. VariableUse= ARRAY 32 OF SET; (* upper bound of 1024 temporary variables in a procedure .. should be enough for all times *)
  875. Variables = OBJECT (Basic.List)
  876. VAR
  877. inUse: VariableUse;
  878. registerIndex: LONGINT;
  879. PROCEDURE & Init;
  880. VAR i: LONGINT;
  881. BEGIN
  882. InitList(16);
  883. FOR i := 0 TO LEN(inUse)-1 DO inUse[i] := {} END;
  884. registerIndex := 1024;
  885. END Init;
  886. PROCEDURE GetUsage(VAR use: VariableUse);
  887. BEGIN
  888. use := inUse;
  889. END GetUsage;
  890. PROCEDURE SetUsage(CONST use: VariableUse);
  891. BEGIN
  892. inUse := use;
  893. END SetUsage;
  894. PROCEDURE GetVariable(i: LONGINT): SyntaxTree.Variable;
  895. VAR any: ANY;
  896. BEGIN
  897. any := Get(i);;
  898. IF any = NIL THEN RETURN NIL ELSE RETURN any(SyntaxTree.Variable) END;
  899. END GetVariable;
  900. PROCEDURE Occupy(pos: LONGINT);
  901. BEGIN
  902. INCL(inUse[pos DIV 32], pos MOD 32);
  903. END Occupy;
  904. PROCEDURE AddVariable(v: SyntaxTree.Variable);
  905. BEGIN
  906. Occupy(Length());
  907. Add(v);
  908. END AddVariable;
  909. PROCEDURE GetFreeVariable(type: SyntaxTree.Type; VAR pos: LONGINT): SyntaxTree.Variable;
  910. VAR var : SyntaxTree.Variable;
  911. BEGIN
  912. FOR pos := 0 TO Length()-1 DO
  913. IF ~((pos MOD 32) IN inUse[pos DIV 32]) THEN
  914. var := GetVariable(pos);
  915. IF type.SameType(var.type) THEN
  916. Occupy(pos); RETURN var
  917. END;
  918. END;
  919. END;
  920. pos := Length();
  921. RETURN NIL
  922. END GetFreeVariable;
  923. END Variables;
  924. SymbolMap = POINTER TO RECORD this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression; next: SymbolMap; END;
  925. SymbolMapper = OBJECT
  926. VAR
  927. first: SymbolMap;
  928. PROCEDURE & Init;
  929. BEGIN
  930. first := NIL;
  931. END Init;
  932. PROCEDURE Add(this: SyntaxTree.Symbol; to, tag: SyntaxTree.Expression);
  933. VAR new: SymbolMap;
  934. BEGIN
  935. NEW(new); new.this := this; new.to := to; new.tag := tag;
  936. new.next := first; first := new;
  937. END Add;
  938. PROCEDURE Get(this: SyntaxTree.Symbol): SymbolMap;
  939. VAR s: SymbolMap;
  940. BEGIN
  941. s := first;
  942. WHILE (s # NIL) & (s.this # this) DO
  943. s := s.next
  944. END;
  945. RETURN s
  946. END Get;
  947. END SymbolMapper;
  948. ImplementationVisitor =OBJECT(SyntaxTree.Visitor)
  949. VAR
  950. system: Global.System;
  951. section: IntermediateCode.Section;
  952. module: Sections.Module;
  953. moduleScope : SyntaxTree.ModuleScope; (* shortcut for module.module.moduleScope *)
  954. awaitProcCounter, labelId, constId, caseId: LONGINT;
  955. hiddenPointerType: SyntaxTree.RecordType; (* used as hidden pointer, for example for ARRAY OF ANY *)
  956. delegatePointerType: SyntaxTree.RecordType; (* used for delegates, for example in ARRAY OF PROCEDURE{DELEGATE} *)
  957. checker: SemanticChecker.Checker;
  958. backend: IntermediateBackend;
  959. meta: MetaDataGenerator;
  960. position: LONGINT;
  961. moduleSelf: SyntaxTree.Variable;
  962. (* variables for hand over of variables / temporary state *)
  963. currentScope: SyntaxTree.Scope;
  964. constantDeclaration : SyntaxTree.Symbol;
  965. result: Operand; (* result of the most recent expression / statement *)
  966. destination: IntermediateCode.Operand;
  967. arrayDestinationTag: IntermediateCode.Operand;
  968. arrayDestinationDimension:LONGINT;
  969. currentLoop: Label; (* variable to hand over loop exit jump list *)
  970. conditional: BOOLEAN;
  971. trueLabel, falseLabel, exitLabel: Label;
  972. locked: BOOLEAN;
  973. (*
  974. usedRegisters: Registers;
  975. *)
  976. registerUsageCount: RegisterUsageCount;
  977. usedRegisters: RegisterEntry;
  978. (* useful operands and types *)
  979. nil,fp,sp,ap,lr,true,false: IntermediateCode.Operand;
  980. bool,addressType,setType, sizeType, byteType: IntermediateCode.Type;
  981. commentPrintout: Printout.Printer;
  982. dump: Streams.Writer;
  983. tagsAvailable : BOOLEAN;
  984. supportedInstruction: SupportedInstructionProcedure;
  985. supportedImmediate: SupportedImmediateProcedure;
  986. inData: BOOLEAN; (* to prevent indirect reference to data within data sections, cf. VisitIntegerValue *)
  987. emitLabels: BOOLEAN;
  988. runtimeModuleName : SyntaxTree.IdentifierString;
  989. newObjectFile: BOOLEAN;
  990. indexCounter: LONGINT;
  991. profile: BOOLEAN;
  992. profileId, profileInit: IntermediateCode.Section;
  993. profileInitPatchPosition: LONGINT;
  994. numberProcedures: LONGINT;
  995. procedureResultDesignator : SyntaxTree.Designator;
  996. operatorInitializationCodeSection: IntermediateCode.Section;
  997. fingerPrinter: FingerPrinter.FingerPrinter;
  998. temporaries: Variables;
  999. canBeLoaded : BOOLEAN;
  1000. currentIsInline: BOOLEAN;
  1001. currentMapper: SymbolMapper;
  1002. currentInlineExit: Label;
  1003. moduleBodySection: IntermediateCode.Section;
  1004. NeedDescriptor : BOOLEAN;
  1005. cooperativeSwitches: BOOLEAN;
  1006. lastSwitchPC: LONGINT;
  1007. isUnchecked: BOOLEAN;
  1008. PROCEDURE & Init(system: Global.System; checker: SemanticChecker.Checker; supportedInstructionProcedure: SupportedInstructionProcedure; supportedImmediateProcedure: SupportedImmediateProcedure; emitLabels: BOOLEAN; CONST runtime: SyntaxTree.IdentifierString; backend: IntermediateBackend;
  1009. newObjectFile: BOOLEAN);
  1010. BEGIN
  1011. SELF.system := system;
  1012. SELF.runtimeModuleName := runtime;
  1013. currentScope := NIL;
  1014. hiddenPointerType := NIL;
  1015. delegatePointerType := NIL;
  1016. awaitProcCounter := 0;
  1017. labelId := 0; constId := 0; labelId := 0;
  1018. SELF.checker := checker;
  1019. SELF.backend := backend;
  1020. position := Diagnostics.Invalid;
  1021. conditional := FALSE;
  1022. locked := FALSE;
  1023. InitOperand(result,ModeUndefined);
  1024. addressType := IntermediateCode.GetType(system,system.addressType);
  1025. setType := IntermediateCode.GetType(system,system.setType);
  1026. sizeType := IntermediateCode.GetType(system, system.sizeType);
  1027. byteType := IntermediateCode.GetType(system, system.byteType);
  1028. fp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.FP);
  1029. sp := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.SP);
  1030. ap := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.AP);
  1031. lr := IntermediateCode.Register(addressType,IntermediateCode.GeneralPurposeRegister,IntermediateCode.LR);
  1032. nil := IntermediateCode.Immediate(addressType,0);
  1033. IntermediateCode.InitOperand(destination);
  1034. tagsAvailable := TRUE;
  1035. supportedInstruction := supportedInstructionProcedure;
  1036. supportedImmediate := supportedImmediateProcedure;
  1037. inData := FALSE;
  1038. SELF.emitLabels := emitLabels;
  1039. IntermediateCode.InitOperand(arrayDestinationTag);
  1040. bool := IntermediateCode.GetType(system,system.booleanType);
  1041. IntermediateCode.InitImmediate(false,bool,0);
  1042. IntermediateCode.InitImmediate(true,bool,1);
  1043. SELF.newObjectFile := newObjectFile;
  1044. indexCounter := 0;
  1045. NEW(registerUsageCount);
  1046. usedRegisters := NIL;
  1047. procedureResultDesignator := NIL;
  1048. NEW(fingerPrinter, system);
  1049. NEW(temporaries);
  1050. currentIsInline := FALSE;
  1051. NeedDescriptor := FALSE;
  1052. isUnchecked := backend.noRuntimeChecks;
  1053. END Init;
  1054. TYPE Context = RECORD
  1055. section: IntermediateCode.Section;
  1056. registerUsageCount: RegisterUsageCount;
  1057. usedRegisters: RegisterEntry;
  1058. END;
  1059. PROCEDURE SwitchContext(new: IntermediateCode.Section): Context;
  1060. VAR context: Context;
  1061. BEGIN
  1062. context.section := section;
  1063. context.registerUsageCount := registerUsageCount;
  1064. context.usedRegisters := usedRegisters;
  1065. section := new;
  1066. NEW(registerUsageCount);
  1067. usedRegisters := NIL;
  1068. RETURN context;
  1069. END SwitchContext;
  1070. PROCEDURE ReturnToContext(context: Context);
  1071. BEGIN
  1072. section := context.section;
  1073. registerUsageCount := context.registerUsageCount;
  1074. usedRegisters := context.usedRegisters;
  1075. END ReturnToContext;
  1076. PROCEDURE NewSection(list: Sections.SectionList; type: SHORTINT; CONST name: Basic.SegmentedName; syntaxTreeSymbol: SyntaxTree.Symbol; dump: BOOLEAN): IntermediateCode.Section;
  1077. VAR fp: SyntaxTree.FingerPrint; section: IntermediateCode.Section;
  1078. BEGIN
  1079. IF (syntaxTreeSymbol # NIL) & ~((syntaxTreeSymbol IS SyntaxTree.Procedure) & (syntaxTreeSymbol(SyntaxTree.Procedure).isInline)) THEN
  1080. fp := fingerPrinter.SymbolFP(syntaxTreeSymbol)
  1081. END;
  1082. section := IntermediateCode.NewSection(list, type, name, syntaxTreeSymbol, dump);
  1083. section.SetExported(IsExported(syntaxTreeSymbol));
  1084. RETURN section
  1085. END NewSection;
  1086. PROCEDURE AcquireRegister(CONST type: IntermediateCode.Type; class: IntermediateCode.RegisterClass): LONGINT;
  1087. VAR new: LONGINT;
  1088. BEGIN
  1089. new := registerUsageCount.Next(type,class);
  1090. UseRegister(new);
  1091. RETURN new
  1092. END AcquireRegister;
  1093. (** get the name for the code section that represens a certain symbol
  1094. (essentially the same as Global.GetSymbolName, apart from operators) **)
  1095. PROCEDURE GetCodeSectionNameForSymbol(symbol: SyntaxTree.Symbol; VAR name: Basic.SegmentedName);
  1096. VAR
  1097. operatorFingerPrint: SyntaxTree.FingerPrint;
  1098. operatorFingerPrintString,string: ARRAY 32 OF CHAR;
  1099. BEGIN
  1100. Global.GetSymbolSegmentedName(symbol, name);
  1101. (* if the symbol is an operator, then append the fingerprint to the name *)
  1102. IF symbol IS SyntaxTree.Operator THEN
  1103. operatorFingerPrint := fingerPrinter.SymbolFP(symbol);
  1104. string := "[";
  1105. Strings.IntToHexStr(operatorFingerPrint.shallow, 8, operatorFingerPrintString);
  1106. Strings.Append(string, operatorFingerPrintString);
  1107. Strings.Append(string, "]");
  1108. Basic.AppendToSegmentedName(name,string);
  1109. END
  1110. END GetCodeSectionNameForSymbol;
  1111. PROCEDURE TraceEnter(CONST s: ARRAY OF CHAR);
  1112. BEGIN
  1113. IF dump # NIL THEN
  1114. dump.String("enter "); dump.String(s); dump.Ln;
  1115. END;
  1116. END TraceEnter;
  1117. PROCEDURE TraceExit(CONST s: ARRAY OF CHAR);
  1118. BEGIN
  1119. IF dump # NIL THEN
  1120. dump.String("exit "); dump.String(s); dump.Ln;
  1121. END;
  1122. END TraceExit;
  1123. PROCEDURE Emit(instruction: IntermediateCode.Instruction);
  1124. VAR moduleName, procedureName: SyntaxTree.IdentifierString;
  1125. PROCEDURE CheckRegister(VAR op: IntermediateCode.Operand);
  1126. VAR i: LONGINT;
  1127. BEGIN
  1128. IF op.register >0 THEN IntermediateCode.SetRegister(op,registerUsageCount.Map(op.register)) END;
  1129. IF op.rule # NIL THEN
  1130. FOR i := 0 TO LEN(op.rule)-1 DO
  1131. CheckRegister(op.rule[i])
  1132. END;
  1133. END;
  1134. END CheckRegister;
  1135. BEGIN
  1136. CheckRegister(instruction.op1);
  1137. CheckRegister(instruction.op2);
  1138. CheckRegister(instruction.op3);
  1139. IF supportedInstruction(instruction,moduleName,procedureName) THEN section.Emit(instruction)
  1140. ELSE section.Emit(instruction);
  1141. EnsureSymbol(moduleName,procedureName); (* remainder for binary object file *)
  1142. END;
  1143. END Emit;
  1144. PROCEDURE EmitTrap (position, trapNo: LONGINT);
  1145. BEGIN
  1146. IF backend.cooperative THEN
  1147. Emit(Push(position,IntermediateCode.Immediate(sizeType,trapNo)));
  1148. CallThis(position,DefaultRuntimeModuleName,"Trap",1);
  1149. ELSE
  1150. Emit(Trap(position,trapNo));
  1151. END;
  1152. END EmitTrap;
  1153. PROCEDURE EmitEnter (section: IntermediateCode.Section; position: LONGINT; procedure: SyntaxTree.Procedure; callconv: LONGINT; varSize: LONGINT; numRegs: LONGINT);
  1154. VAR name: Basic.SegmentedName;
  1155. VAR op1, op2, reg: IntermediateCode.Operand;
  1156. VAR call, nocall: Label;
  1157. VAR parametersSize: LONGINT;
  1158. VAR prevSection: IntermediateCode.Section;
  1159. BEGIN
  1160. ASSERT(~procedure.type(SyntaxTree.ProcedureType).noPAF);
  1161. prevSection := SELF.section;
  1162. SELF.section := section;
  1163. IF backend.hasLinkRegister THEN
  1164. Emit(Push(-1, lr));
  1165. END;
  1166. Emit(Push(-1,fp));
  1167. IF backend.cooperative & (callconv = SyntaxTree.OberonCallingConvention) THEN
  1168. IF (procedure # NIL) & (HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure)) THEN
  1169. GetCodeSectionNameForSymbol(procedure, name);
  1170. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  1171. ELSE
  1172. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  1173. END;
  1174. IntermediateCode.InitAddress(op1, addressType, name , 0, 0);
  1175. Emit(Push(-1,op1));
  1176. Emit(Mov(-1,fp, sp));
  1177. NEW(call, section);
  1178. NEW(nocall, section);
  1179. reg := NewRegisterOperand(addressType);
  1180. IntermediateCode.InitImmediate(op1,addressType, varSize);
  1181. Emit(Sub(-1,reg, sp, op1));
  1182. BrltL(call, sp, reg);
  1183. IntermediateCode.InitMemory(op2, addressType,ap,ToMemoryUnits(system,system.addressSize*10));
  1184. BrgeL(nocall, sp, op2);
  1185. call.Resolve(section.pc);
  1186. parametersSize := ProcedureParametersSize(backend.system,procedure);
  1187. IntermediateCode.InitImmediate(op2,addressType, parametersSize);
  1188. Emit(Push(-1, op2));
  1189. Emit(Push(-1, reg));
  1190. ReleaseIntermediateOperand(reg);
  1191. CallThis(position, "Activities","ExpandStack",0);
  1192. Emit(Result(-1, sp));
  1193. ReleaseIntermediateOperand(reg);
  1194. nocall.Resolve(section.pc);
  1195. ELSE
  1196. Emit(Mov(-1, fp, sp));
  1197. END;
  1198. Emit(Enter(-1, callconv, varSize));
  1199. SELF.section := prevSection;
  1200. END EmitEnter;
  1201. PROCEDURE Enter(position: LONGINT; callconv: LONGINT; varSize: LONGINT): IntermediateCode.Instruction;
  1202. VAR op1,op2: IntermediateCode.Operand;
  1203. VAR instruction: IntermediateCode.Instruction;
  1204. BEGIN
  1205. IntermediateCode.InitNumber(op1,callconv);
  1206. IntermediateCode.InitNumber(op2,varSize);
  1207. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.enter,op1,op2,emptyOperand);
  1208. RETURN instruction
  1209. END Enter;
  1210. PROCEDURE Leave(position: LONGINT; callconv: LONGINT): IntermediateCode.Instruction;
  1211. VAR op1: IntermediateCode.Operand;
  1212. VAR instruction: IntermediateCode.Instruction;
  1213. BEGIN
  1214. IntermediateCode.InitNumber(op1,callconv);
  1215. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.leave,op1,emptyOperand,emptyOperand);
  1216. RETURN instruction
  1217. END Leave;
  1218. PROCEDURE EmitLeave(section: IntermediateCode.Section; position: LONGINT; callconv: LONGINT);
  1219. VAR prevSection: IntermediateCode.Section;
  1220. VAR op2: IntermediateCode.Operand;
  1221. BEGIN
  1222. prevSection := SELF.section;
  1223. SELF.section := section;
  1224. Emit(Leave(position, callconv));
  1225. IF backend.cooperative THEN
  1226. IntermediateCode.InitImmediate(op2,addressType, ToMemoryUnits(system, system.addressSize));
  1227. Emit(Add(position, sp, fp, op2));
  1228. ELSE
  1229. Emit(Mov(position, sp, fp));
  1230. END;
  1231. Emit(Pop(position, fp));
  1232. SELF.section := prevSection;
  1233. END EmitLeave;
  1234. PROCEDURE Symbol(x: SyntaxTree.Symbol; VAR op: Operand);
  1235. VAR m: SymbolMap;
  1236. BEGIN
  1237. position := x.position;
  1238. IF currentIsInline THEN
  1239. m := currentMapper.Get(x);
  1240. IF m # NIL THEN
  1241. (*
  1242. Printout.Info("mapping from", x);
  1243. Printout.Info("mapping to ", m.to);
  1244. *)
  1245. m.to.Accept(SELF);
  1246. op := result;
  1247. IF m.tag # NIL THEN
  1248. ReleaseIntermediateOperand(result.tag);
  1249. m.tag.Accept(SELF);
  1250. op.tag := result.op;
  1251. ReleaseIntermediateOperand(result.tag);
  1252. END;
  1253. RETURN
  1254. END;
  1255. END;
  1256. x.Accept(SELF);
  1257. op := result;
  1258. END Symbol;
  1259. PROCEDURE Expression(x: SyntaxTree.Expression);
  1260. BEGIN
  1261. position := x.position;
  1262. constantDeclaration := NIL;
  1263. IF (x IS SyntaxTree.SymbolDesignator) & (x(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Constant) THEN
  1264. constantDeclaration := x(SyntaxTree.SymbolDesignator).symbol;
  1265. END;
  1266. IF x.resolved # NIL THEN
  1267. x.resolved.Accept(SELF)
  1268. ELSE
  1269. x.Accept(SELF)
  1270. END;
  1271. (* check this, was commented out in ActiveCells3 *)
  1272. IF (x IS SyntaxTree.Designator) & (x(SyntaxTree.Designator).modifiers # NIL) & ~backend.cellsAreObjects THEN
  1273. Error(x.position, "unsupported modifier");
  1274. END;
  1275. END Expression;
  1276. (*
  1277. PROCEDURE ResetUsedTemporaries(previous: VariableUse);
  1278. VAR current: VariableUse; set: SET; i,j: LONGINT; variable: SyntaxTree.Variable; op: Operand; tmp: IntermediateCode.Operand;
  1279. BEGIN
  1280. temporaries.GetUsage(current);
  1281. FOR i := 0 TO LEN(current)-1 DO
  1282. set := current[i] - previous[i];
  1283. IF set # {} THEN
  1284. FOR j := 0 TO MAX(SET)-1 DO
  1285. IF j IN set THEN
  1286. variable := temporaries.GetVariable(i*MAX(SET)+j);
  1287. IF (variable.type.resolved IS SyntaxTree.MathArrayType) & (variable.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  1288. Symbol(variable, op);
  1289. MakeMemory(tmp,op.op,addressType,0);
  1290. ReleaseOperand(op);
  1291. Emit(Mov(position,tmp, nil ) );
  1292. ReleaseIntermediateOperand(tmp);
  1293. END;
  1294. END;
  1295. END;
  1296. END;
  1297. END;
  1298. END ResetUsedTemporaries;
  1299. *)
  1300. PROCEDURE Statement(x: SyntaxTree.Statement);
  1301. VAR use: VariableUse;
  1302. BEGIN
  1303. temporaries.GetUsage(use);
  1304. position := x.position;
  1305. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  1306. IF commentPrintout # NIL THEN
  1307. commentPrintout.Statement(x);
  1308. dump.Ln;
  1309. (*dump.Update;*)
  1310. END;
  1311. x.Accept(SELF);
  1312. (*
  1313. CheckRegistersFree();
  1314. *)
  1315. (*ResetUsedTemporaries(use);*)
  1316. temporaries.SetUsage(use);
  1317. END Statement;
  1318. (* dereference op. If op is already a memory operand then use auxiliary register to dereference
  1319. result will be registered as a new use of operand, op is not released (op must be released by caller)
  1320. *)
  1321. PROCEDURE MakeMemory(VAR res: IntermediateCode.Operand; op: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1322. BEGIN
  1323. IF op.mode = IntermediateCode.ModeMemory THEN
  1324. ReuseCopy(res,op);
  1325. ELSE
  1326. res := op;
  1327. UseIntermediateOperand(res);
  1328. END;
  1329. IntermediateCode.AddOffset(res,offset);
  1330. IntermediateCode.MakeMemory(res,type);
  1331. END MakeMemory;
  1332. PROCEDURE ToMemory(VAR res: IntermediateCode.Operand; type: IntermediateCode.Type; offset: LONGINT);
  1333. VAR mem: IntermediateCode.Operand;
  1334. BEGIN
  1335. MakeMemory(mem,res,type,offset);
  1336. ReleaseIntermediateOperand(res);
  1337. res := mem;
  1338. END ToMemory;
  1339. PROCEDURE LoadValue(VAR operand: Operand; type: SyntaxTree.Type);
  1340. VAR mem: IntermediateCode.Operand;
  1341. firstOp, lastOp, stepOp: IntermediateCode.Operand;
  1342. componentType: SyntaxTree.Type;
  1343. BEGIN
  1344. type := type.resolved;
  1345. IF operand.mode = ModeReference THEN
  1346. IF type IS SyntaxTree.RangeType THEN
  1347. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, system.longintType), 0);
  1348. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1349. MakeMemory(stepOp, operand.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  1350. ReleaseOperand(operand);
  1351. operand.op := firstOp;
  1352. operand.tag := lastOp;
  1353. operand.extra := stepOp;
  1354. ELSIF type IS SyntaxTree.ComplexType THEN
  1355. componentType := type(SyntaxTree.ComplexType).componentType;
  1356. ASSERT((componentType.SameType(system.realType)) OR (componentType.SameType(system.longrealType)));
  1357. MakeMemory(firstOp, operand.op, IntermediateCode.GetType(system, componentType), 0);
  1358. MakeMemory(lastOp, operand.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  1359. ReleaseOperand(operand);
  1360. operand.op := firstOp;
  1361. operand.tag := lastOp
  1362. ELSE
  1363. MakeMemory(mem,operand.op,IntermediateCode.GetType(system,type),0);
  1364. ReleaseIntermediateOperand(operand.op);
  1365. operand.op := mem;
  1366. END;
  1367. operand.mode := ModeValue;
  1368. END;
  1369. ASSERT(operand.mode = ModeValue);
  1370. END LoadValue;
  1371. PROCEDURE Evaluate(x: SyntaxTree.Expression; VAR op: Operand);
  1372. VAR prevConditional: BOOLEAN;
  1373. BEGIN
  1374. prevConditional := conditional;
  1375. conditional := FALSE;
  1376. InitOperand(result, ModeUndefined);
  1377. Expression(x);
  1378. op := result;
  1379. LoadValue(op,x.type.resolved);
  1380. conditional := prevConditional;
  1381. END Evaluate;
  1382. PROCEDURE Designate(x: SyntaxTree.Expression; VAR op: Operand);
  1383. VAR prevConditional: BOOLEAN;
  1384. BEGIN
  1385. prevConditional := conditional;
  1386. conditional := FALSE;
  1387. InitOperand(result,ModeUndefined);
  1388. Expression(x);
  1389. op := result;
  1390. (*
  1391. ASSERT((op.mode = ModeReference) OR (x.type.resolved IS SyntaxTree.NilType)); (* special case: winapi NIL parameter on references *)
  1392. *)
  1393. conditional := prevConditional;
  1394. END Designate;
  1395. PROCEDURE Condition(x: SyntaxTree.Expression; trueL,falseL: Label);
  1396. VAR prevTrue, prevFalse: Label; prevConditional: BOOLEAN;
  1397. BEGIN
  1398. ASSERT(trueL # NIL); ASSERT(falseL # NIL);
  1399. prevTrue := trueLabel; prevFalse := falseLabel; prevConditional := conditional;
  1400. conditional := TRUE;
  1401. trueLabel := trueL; falseLabel := falseL;
  1402. Expression(x);
  1403. trueL := trueLabel; falseL := falseLabel;
  1404. trueLabel := prevTrue;falseLabel := prevFalse;conditional := prevConditional;
  1405. END Condition;
  1406. PROCEDURE NewRegisterOperand(type: IntermediateCode.Type): IntermediateCode.Operand;
  1407. VAR op: IntermediateCode.Operand; reg: LONGINT;
  1408. BEGIN
  1409. reg := AcquireRegister(type,IntermediateCode.GeneralPurposeRegister);
  1410. IntermediateCode.InitRegister(op, type, IntermediateCode.GeneralPurposeRegister,reg);
  1411. RETURN op
  1412. END NewRegisterOperand;
  1413. PROCEDURE UnuseRegister(register: LONGINT);
  1414. BEGIN
  1415. IF (register > 0) THEN
  1416. register := registerUsageCount.Map(register);
  1417. registerUsageCount.DecUse(register);
  1418. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1419. dump.String("unuse register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1420. END;
  1421. IF registerUsageCount.Use(register)=0 THEN
  1422. IF ~RemoveRegisterEntry(usedRegisters,register) THEN
  1423. Warning(position, "register cannot be removed");
  1424. END;
  1425. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1426. dump.String("remove register from usedRegisters"); dump.Ln; dump.Update;
  1427. END;
  1428. ELSIF registerUsageCount.Use(register)<0 THEN
  1429. Warning(position, "register removed too often");
  1430. D.TraceBack;
  1431. END;
  1432. END;
  1433. END UnuseRegister;
  1434. PROCEDURE UseRegister(register: LONGINT);
  1435. BEGIN
  1436. IF (register > 0) THEN
  1437. register := registerUsageCount.Map(register);
  1438. registerUsageCount.IncUse(register);
  1439. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1440. dump.String("use register "); dump.Int(register,1); dump.String(": ");dump.Int(registerUsageCount.Use(register),1); dump.Ln; dump.Update;
  1441. END;
  1442. IF registerUsageCount.Use(register)=1 THEN
  1443. AddRegisterEntry(usedRegisters,register, registerUsageCount.used[register].class, registerUsageCount.used[register].type);
  1444. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1445. dump.String("add register to usedRegisters"); dump.Ln; dump.Update;
  1446. END;
  1447. END;
  1448. END;
  1449. END UseRegister;
  1450. PROCEDURE ReleaseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1451. BEGIN
  1452. UnuseRegister(op.register)
  1453. END ReleaseIntermediateOperand;
  1454. PROCEDURE UseIntermediateOperand(CONST op: IntermediateCode.Operand);
  1455. BEGIN
  1456. UseRegister(op.register)
  1457. END UseIntermediateOperand;
  1458. PROCEDURE ReleaseOperand(CONST op: Operand);
  1459. BEGIN
  1460. UnuseRegister(op.op.register);
  1461. UnuseRegister(op.tag.register);
  1462. UnuseRegister(op.extra.register);
  1463. END ReleaseOperand;
  1464. (* save registers marked in array "markedRegisters" to the stack
  1465. remove entries from array "markedRegisters" and save to array "saved" (=> recursion possible)
  1466. *)
  1467. PROCEDURE SaveRegisters();
  1468. VAR op: IntermediateCode.Operand; entry: RegisterEntry; type: IntermediateCode.Type;
  1469. BEGIN
  1470. entry := usedRegisters;
  1471. WHILE entry # NIL DO
  1472. type := registerUsageCount.used[entry.register].type;
  1473. IntermediateCode.InitRegister(op,entry.type,entry.registerClass, entry.register);
  1474. Emit(Push(position,op));
  1475. entry := entry.next;
  1476. END;
  1477. END SaveRegisters;
  1478. PROCEDURE ReleaseUsedRegisters(VAR saved: RegisterEntry);
  1479. BEGIN
  1480. saved := usedRegisters;
  1481. usedRegisters := NIL;
  1482. END ReleaseUsedRegisters;
  1483. (** remove parameter registers from used queue *)
  1484. PROCEDURE ReleaseParameterRegisters;
  1485. VAR entry,prev,next: RegisterEntry;
  1486. BEGIN
  1487. entry := usedRegisters; prev := NIL; usedRegisters := NIL;
  1488. WHILE entry # NIL DO
  1489. next := entry.next;
  1490. IF entry.registerClass.class = IntermediateCode.Parameter THEN
  1491. registerUsageCount.DecUse(entry.register);
  1492. ASSERT(registerUsageCount.Use(entry.register)=0);
  1493. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1494. dump.String("unuse register "); dump.Int(entry.register,1); dump.Ln; dump.Update;
  1495. END;
  1496. ELSIF prev = NIL THEN
  1497. usedRegisters := entry; entry.prev := NIL; entry.next := NIL; prev := entry;
  1498. ELSE
  1499. prev.next := entry; entry.prev := prev; entry.next := NIL; prev:= entry;
  1500. END;
  1501. entry := next;
  1502. END;
  1503. END ReleaseParameterRegisters;
  1504. (* restore registers from array saved and re-enter into array markedRegisters (recursion possible) *)
  1505. PROCEDURE RestoreRegisters(CONST saved: RegisterEntry);
  1506. VAR op: IntermediateCode.Operand; entry,prev: RegisterEntry; type: IntermediateCode.Type; class: IntermediateCode.RegisterClass;
  1507. BEGIN
  1508. entry := saved;
  1509. WHILE (entry # NIL) DO prev := entry; entry := entry.next END;
  1510. entry := prev;
  1511. WHILE entry # NIL DO
  1512. prev := entry.prev;
  1513. type := entry.type;
  1514. class := entry.registerClass;
  1515. IntermediateCode.InitRegister(op,type,class,entry.register);
  1516. (*
  1517. new := registerUsageCount.Next(type,class);
  1518. registerUsageCount.Remap(entry.register,new);
  1519. IF TraceRegisterUsageCount & (dump# NIL) THEN
  1520. dump.String("remap register "); dump.Int(entry.register,1);
  1521. dump.String("to "); dump.Int(new,1);
  1522. dump.String("with count "); dump.Int(registerUsageCount.Use(new),1); dump.Ln; dump.Update;
  1523. END;
  1524. entry.register := new;
  1525. *)
  1526. Emit(Pop(position,op));
  1527. AddRegisterEntry(usedRegisters,entry.register,entry.registerClass, entry.type);
  1528. entry := prev;
  1529. END;
  1530. (*
  1531. usedRegisters := saved;
  1532. *)
  1533. END RestoreRegisters;
  1534. PROCEDURE CheckRegistersFree;
  1535. VAR r: RegisterEntry; warning: ARRAY 128 OF CHAR; i: LONGINT;
  1536. BEGIN
  1537. IF usedRegisters # NIL THEN
  1538. r := usedRegisters;
  1539. WHILE r # NIL DO
  1540. warning := "register ";
  1541. Strings.AppendInt(warning, r.register);
  1542. Strings.Append(warning, " not released.");
  1543. Warning(position,warning);
  1544. r := r .next;
  1545. END;
  1546. END;
  1547. FOR i := 0 TO registerUsageCount.count-1 DO
  1548. IF registerUsageCount.used[i].count < 0 THEN
  1549. warning := "register ";
  1550. Strings.AppendInt(warning, i);
  1551. Strings.Append(warning, " unused too often.");
  1552. Warning(position,warning);
  1553. ELSIF registerUsageCount.used[i].count > 0 THEN (* should always coincide with cases above *)
  1554. warning := "register ";
  1555. Strings.AppendInt(warning, i);
  1556. Strings.Append(warning, " not unused often enough.");
  1557. Warning(position,warning);
  1558. END;
  1559. END;
  1560. END CheckRegistersFree;
  1561. (* Reuse2: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1562. Otherwise allocate a new register.
  1563. Does NOT necessarily keep the content of src1 or src2 in result! *)
  1564. PROCEDURE Reuse2(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand);
  1565. BEGIN
  1566. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1567. UseIntermediateOperand(result);
  1568. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1569. UseIntermediateOperand(result);
  1570. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass,AcquireRegister(src1.type, src1.registerClass));
  1571. END;
  1572. END Reuse2;
  1573. (* Reuse2a: reuse src1 or src2 for ongoing computation if src1 or src2, respectively, is a register.
  1574. Otherwise check if an alternative destination is available. If so, then take the alternative (which is not necessarily a register).
  1575. If not then allocate a new register.
  1576. Does NOT necessarily keep the content of src1 or src2 in result!
  1577. *)
  1578. PROCEDURE Reuse2a(VAR result: IntermediateCode.Operand; src1,src2: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1579. BEGIN
  1580. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1581. UseIntermediateOperand(result);
  1582. ELSIF ReusableRegister(src2) THEN IntermediateCode.InitRegister(result,src2.type,src2.registerClass, src2.register);
  1583. UseIntermediateOperand(result);
  1584. ELSIF alternative.mode # IntermediateCode.Undefined THEN
  1585. result := alternative; alternative := emptyOperand;
  1586. UseIntermediateOperand(result);
  1587. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1588. END;
  1589. END Reuse2a;
  1590. (* like reuse2 but only one source *)
  1591. PROCEDURE Reuse1(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1592. BEGIN
  1593. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1594. UseIntermediateOperand(result);
  1595. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1596. END;
  1597. END Reuse1;
  1598. (* like reuse2a but only one source *)
  1599. PROCEDURE Reuse1a(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand; VAR alternative: IntermediateCode.Operand);
  1600. BEGIN
  1601. IF ReusableRegister(src1) THEN IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1602. UseIntermediateOperand(result);
  1603. ELSIF alternative.mode # IntermediateCode.Undefined THEN result := alternative; alternative := emptyOperand;
  1604. UseIntermediateOperand(result);
  1605. ELSE IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1606. END;
  1607. END Reuse1a;
  1608. (* like reuse1 but guarantees that content of src1 is in result *)
  1609. PROCEDURE ReuseCopy(VAR result: IntermediateCode.Operand; src1: IntermediateCode.Operand);
  1610. BEGIN
  1611. IF ReusableRegister(src1) THEN
  1612. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, src1.register);
  1613. ASSERT((src1.mode = IntermediateCode.ModeRegister) & (src1.offset = 0));
  1614. UseIntermediateOperand(result);
  1615. ELSE
  1616. IntermediateCode.InitRegister(result,src1.type,src1.registerClass, AcquireRegister(src1.type, src1.registerClass));
  1617. Emit(Mov(position,result,src1));
  1618. END
  1619. END ReuseCopy;
  1620. PROCEDURE TransferToRegister(VAR result: IntermediateCode.Operand; src: IntermediateCode.Operand);
  1621. BEGIN
  1622. IF ReusableRegister(src) THEN
  1623. IntermediateCode.InitRegister(result,src.type,src.registerClass, src.register);
  1624. ELSE
  1625. IntermediateCode.InitRegister(result,src.type,src.registerClass, AcquireRegister(src.type, src.registerClass));
  1626. Emit(Mov(position,result,src));
  1627. ReleaseIntermediateOperand(src);
  1628. END
  1629. END TransferToRegister;
  1630. (** labels and branches **)
  1631. PROCEDURE NewLabel(): Label;
  1632. VAR label: Label;
  1633. BEGIN
  1634. NEW(label,section); RETURN label;
  1635. END NewLabel;
  1636. PROCEDURE SetLabel(label: Label);
  1637. BEGIN label.Resolve(section.pc);
  1638. END SetLabel;
  1639. PROCEDURE LabelOperand(label: Label): IntermediateCode.Operand;
  1640. BEGIN
  1641. ASSERT(label # NIL);
  1642. IF label.pc < 0 THEN (* label not yet set *)
  1643. label.AddFixup(section.pc);
  1644. END;
  1645. RETURN IntermediateCode.Address(addressType,label.section.name,GetFingerprint(label.section.symbol), label.pc);
  1646. END LabelOperand;
  1647. PROCEDURE BrL(label: Label);
  1648. BEGIN
  1649. Emit(Br(position,LabelOperand(label)));
  1650. END BrL;
  1651. PROCEDURE BrgeL(label: Label; left,right: IntermediateCode.Operand);
  1652. BEGIN
  1653. Emit(Brge(position,LabelOperand(label),left,right));
  1654. END BrgeL;
  1655. PROCEDURE BrltL(label: Label; left,right: IntermediateCode.Operand);
  1656. BEGIN
  1657. Emit(Brlt(position,LabelOperand(label),left,right));
  1658. END BrltL;
  1659. PROCEDURE BreqL(label: Label; left,right: IntermediateCode.Operand);
  1660. BEGIN
  1661. Emit(Breq(position,LabelOperand(label),left,right));
  1662. END BreqL;
  1663. PROCEDURE BrneL(label: Label; left,right: IntermediateCode.Operand);
  1664. BEGIN
  1665. Emit(Brne(position,LabelOperand(label),left,right));
  1666. END BrneL;
  1667. PROCEDURE Convert(VAR operand: IntermediateCode.Operand; type: IntermediateCode.Type);
  1668. VAR new: IntermediateCode.Operand;
  1669. BEGIN
  1670. IF Trace THEN TraceEnter("Convert") END;
  1671. IF IntermediateCode.TypeEquals(type,operand.type) THEN (* nothing to be done *)
  1672. ELSIF (operand.mode = IntermediateCode.ModeRegister) THEN
  1673. IF (type.sizeInBits = operand.type.sizeInBits) & (type.form IN IntermediateCode.Integer) & (operand.type.form IN IntermediateCode.Integer)
  1674. & (operand.offset = 0)
  1675. THEN
  1676. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,operand.register);
  1677. Emit(Conv(position,new,operand));
  1678. ELSE
  1679. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1680. Emit(Conv(position,new,operand));
  1681. ReleaseIntermediateOperand(operand);
  1682. END;
  1683. operand := new;
  1684. 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
  1685. IntermediateCode.InitImmediate(operand,type,operand.intValue);
  1686. ELSE
  1687. IntermediateCode.InitRegister(new,type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(type,IntermediateCode.GeneralPurposeRegister));
  1688. Emit(Conv(position,new,operand));
  1689. ReleaseIntermediateOperand(operand);
  1690. operand := new;
  1691. END;
  1692. IF Trace THEN TraceExit("Convert") END;
  1693. END Convert;
  1694. PROCEDURE TrapC(br: ConditionalBranch; left,right:IntermediateCode.Operand; trapNo: LONGINT);
  1695. VAR exit: Label;
  1696. BEGIN
  1697. Assert((left.mode # IntermediateCode.ModeImmediate) OR (right.mode # IntermediateCode.ModeImmediate),"trap emission with two immediates");
  1698. exit := NewLabel();
  1699. br(exit,left,right);
  1700. EmitTrap(position,trapNo);
  1701. SetLabel(exit);
  1702. END TrapC;
  1703. (** expressions *)
  1704. (** emit necessary runtime check for set elements **)
  1705. PROCEDURE CheckSetElement(o: IntermediateCode.Operand);
  1706. VAR max: IntermediateCode.Operand;
  1707. BEGIN
  1708. IF isUnchecked THEN RETURN END;
  1709. IF o.mode # IntermediateCode.ModeImmediate THEN (* otherwise it's the job of the checker *)
  1710. IntermediateCode.InitImmediate(max, setType, setType.sizeInBits (* number of bits in set *) -1);
  1711. TrapC(BrgeL, max, o, SetElementTrap);
  1712. END;
  1713. END CheckSetElement;
  1714. (** the set that a range represents **)
  1715. PROCEDURE SetFromRange(x: SyntaxTree.RangeExpression): IntermediateCode.Operand;
  1716. VAR
  1717. operand: Operand;
  1718. resultingSet, temp, size, allBits, noBits, one: IntermediateCode.Operand;
  1719. BEGIN
  1720. ASSERT((x.first # NIL) & (x.last # NIL)); (* ensured by the checker *)
  1721. allBits := IntermediateCode.Immediate(setType, -1); (* bit mask 111...11111 *)
  1722. noBits := IntermediateCode.Immediate(setType, 0); (* bit mask 0...0 *)
  1723. one := IntermediateCode.Immediate(setType, 1);
  1724. Evaluate(x, operand);
  1725. Convert(operand.op, setType);
  1726. Convert(operand.tag, setType);
  1727. CheckSetElement(operand.op);
  1728. CheckSetElement(operand.tag);
  1729. (* create mask for lower bound
  1730. i.e. shift 11111111 to the left by the value of the lower bound
  1731. *)
  1732. Reuse1(temp, operand.op);
  1733. Emit(Shl(position,temp, allBits, operand.op));
  1734. ReleaseIntermediateOperand(operand.op);
  1735. operand.op := temp;
  1736. (* create mask for upper bound
  1737. i.e. shift 11111111 to the right by the difference between the
  1738. upper bound and the maximum number of set elements
  1739. *)
  1740. IF (operand.tag.mode = IntermediateCode.ModeImmediate) & (operand.tag.symbol.name = "") THEN
  1741. IntermediateCode.InitImmediate(operand.tag, operand.tag.type, operand.op.type.sizeInBits - 1- operand.tag.intValue);
  1742. Reuse1(temp, operand.tag);
  1743. ELSE
  1744. Reuse1(temp, operand.tag);
  1745. IntermediateCode.InitImmediate(size, operand.tag.type, operand.op.type.sizeInBits - 1);
  1746. Emit(Sub(position,temp, size, operand.tag));
  1747. END;
  1748. Emit(Shr(position,temp, allBits, operand.tag));
  1749. ReleaseIntermediateOperand(operand.tag);
  1750. operand.tag := temp;
  1751. Reuse2(resultingSet, operand.op, operand.tag);
  1752. (* intersect the two masks *)
  1753. Emit(And(position,resultingSet, operand.op, operand.tag));
  1754. ReleaseOperand(operand);
  1755. RETURN resultingSet
  1756. END SetFromRange;
  1757. PROCEDURE VisitSet(x: SyntaxTree.Set);
  1758. VAR
  1759. res, operand: Operand;
  1760. temp, one, noBits, dest: IntermediateCode.Operand;
  1761. expression: SyntaxTree.Expression;
  1762. i: LONGINT;
  1763. BEGIN
  1764. IF Trace THEN TraceEnter("VisitSet") END;
  1765. dest := destination;
  1766. destination := emptyOperand;
  1767. noBits := IntermediateCode.Immediate(setType, 0);
  1768. one := IntermediateCode.Immediate(setType, 1);
  1769. (* start off with the empty set *)
  1770. InitOperand(res, ModeValue);
  1771. IntermediateCode.InitRegister(res.op, setType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(setType, IntermediateCode.GeneralPurposeRegister));
  1772. Emit(Mov(position,res.op, noBits));
  1773. FOR i := 0 TO x.elements.Length() - 1 DO
  1774. expression := x.elements.GetExpression(i);
  1775. IF expression IS SyntaxTree.RangeExpression THEN
  1776. (* range of set elements *)
  1777. temp := SetFromRange(expression(SyntaxTree.RangeExpression));
  1778. ASSERT(IntermediateCode.TypeEquals(setType, temp.type));
  1779. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1780. ReleaseIntermediateOperand(temp)
  1781. ELSE
  1782. (* singelton element *)
  1783. Evaluate(expression, operand);
  1784. Convert(operand.op, setType);
  1785. CheckSetElement(operand.op);
  1786. (* create subset containing single element *)
  1787. Reuse1(temp, operand.op);
  1788. Emit(Shl(position,temp, one, operand.op));
  1789. ReleaseOperand(operand);
  1790. Emit(Or(position,res.op, res.op, temp)); (* unify subset with current set *)
  1791. ReleaseIntermediateOperand(temp);
  1792. END
  1793. END;
  1794. result := res;
  1795. destination := dest;
  1796. IF Trace THEN TraceExit("VisitSet") END;
  1797. END VisitSet;
  1798. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1799. VAR variable: SyntaxTree.Variable; index: SyntaxTree.IndexDesignator; dim: LONGINT;
  1800. designator: SyntaxTree.Designator; i: LONGINT; element: SyntaxTree.IntegerValue;
  1801. PROCEDURE RecursiveAssignment(x: SyntaxTree.MathArrayExpression; dim: LONGINT);
  1802. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1803. element: SyntaxTree.IntegerValue;
  1804. BEGIN
  1805. numberElements := x.elements.Length();
  1806. expression := index.parameters.GetExpression(dim);
  1807. element := expression(SyntaxTree.IntegerValue);
  1808. FOR i := 0 TO numberElements-1 DO
  1809. expression := x.elements.GetExpression(i);
  1810. element.SetValue(i);
  1811. IF expression IS SyntaxTree.MathArrayExpression THEN
  1812. RecursiveAssignment(expression(SyntaxTree.MathArrayExpression),dim+1);
  1813. ELSE
  1814. Assign(index,expression);
  1815. END;
  1816. END;
  1817. END RecursiveAssignment;
  1818. BEGIN
  1819. variable := GetTemporaryVariable(x.type, FALSE);
  1820. designator := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  1821. designator.SetType(variable.type);
  1822. dim := SemanticChecker.Dimension(x.type,{SyntaxTree.Static});
  1823. index := SyntaxTree.NewIndexDesignator(x.position,designator);
  1824. FOR i := 0 TO dim-1 DO
  1825. element := SyntaxTree.NewIntegerValue(x.position,0);
  1826. element.SetType(system.longintType);
  1827. index.parameters.AddExpression(element);
  1828. END;
  1829. index.SetType(SemanticChecker.ArrayBase(x.type,dim));
  1830. RecursiveAssignment(x,0);
  1831. Expression(designator);
  1832. END VisitMathArrayExpression;
  1833. PROCEDURE VisitUnaryExpression(x: SyntaxTree.UnaryExpression);
  1834. VAR type,t0: SyntaxTree.Type; operand: Operand; dest: IntermediateCode.Operand;
  1835. BEGIN
  1836. IF Trace THEN TraceEnter("VisitUnaryExpression") END;
  1837. dest := destination; destination := emptyOperand;
  1838. IF x.operator = Scanner.Not THEN
  1839. IF conditional THEN
  1840. Condition(x.left,falseLabel,trueLabel)
  1841. ELSE
  1842. Evaluate(x.left,operand);
  1843. InitOperand(result,ModeValue);
  1844. Reuse1a(result.op,operand.op,dest);
  1845. Emit(Xor(position,result.op,operand.op,true));
  1846. ReleaseOperand(operand);
  1847. END;
  1848. ELSIF x.operator = Scanner.Minus THEN
  1849. Evaluate(x.left,operand);
  1850. InitOperand(result,ModeValue);
  1851. Reuse1a(result.op,operand.op,dest);
  1852. type := x.left.type.resolved;
  1853. IF type IS SyntaxTree.SetType THEN
  1854. Emit(Not(position,result.op,operand.op));
  1855. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1856. Reuse1(result.tag,operand.tag);
  1857. Emit(Neg(position,result.op,operand.op)); (* real part *)
  1858. Emit(Neg(position,result.tag,operand.tag)) (* imaginary part *)
  1859. ELSIF (type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.SizeType) OR (type IS SyntaxTree.AddressType) THEN
  1860. Emit(Neg(position,result.op,operand.op));
  1861. ELSE HALT(200)
  1862. END;
  1863. ReleaseOperand(operand);
  1864. ELSIF x.operator = Scanner.Address THEN
  1865. Designate(x.left,operand);
  1866. operand.mode := ModeValue;
  1867. t0 := x.left.type.resolved;
  1868. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  1869. ReleaseIntermediateOperand(operand.op);
  1870. operand.op := operand.tag;
  1871. IntermediateCode.InitOperand(operand.tag);
  1872. END;
  1873. Convert(operand.op,IntermediateCode.GetType(system,x.type));
  1874. result := operand;
  1875. ELSE HALT(100)
  1876. END;
  1877. destination := dest;
  1878. IF Trace THEN TraceExit("VisitUnaryExpression") END;
  1879. END VisitUnaryExpression;
  1880. (* test if e is of type type, side effect: result of evaluation of e stays in the operand *)
  1881. PROCEDURE TypeTest(tag: IntermediateCode.Operand; type: SyntaxTree.Type; trueL,falseL: Label);
  1882. VAR left,right: IntermediateCode.Operand; level,offset: LONGINT; repeatL: Label; originalType: SyntaxTree.Type;
  1883. BEGIN
  1884. type := type.resolved;
  1885. originalType := type;
  1886. IF type IS SyntaxTree.PointerType THEN
  1887. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  1888. END;
  1889. IF type IS SyntaxTree.ObjectType THEN
  1890. BrL(trueL);
  1891. ELSE
  1892. ASSERT(type IS SyntaxTree.RecordType);
  1893. (*
  1894. IntermediateCode.MakeMemory(tag,addressType); (*! already done during generation *)
  1895. *)
  1896. ReuseCopy(left,tag);
  1897. right := TypeDescriptorAdr(type);
  1898. IF ~newObjectFile THEN
  1899. IntermediateCode.MakeMemory(right,addressType);
  1900. END;
  1901. IF backend.cooperative THEN
  1902. repeatL := NewLabel();
  1903. IF (originalType IS SyntaxTree.PointerType) & ~type(SyntaxTree.RecordType).isObject THEN
  1904. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,ToMemoryUnits(system,addressType.sizeInBits))));
  1905. END;
  1906. SetLabel(repeatL);
  1907. BreqL(trueL,left,right);
  1908. Emit(Mov(position,left,IntermediateCode.Memory(addressType,left,0)));
  1909. BrneL(repeatL,left,nil);
  1910. ELSE
  1911. level := type(SyntaxTree.RecordType).Level();
  1912. (* get type desc tag of level relative to base tag *)
  1913. offset := (meta.BaseTypesTableOffset - level) * addressType.sizeInBits;
  1914. IntermediateCode.AddOffset(left,ToMemoryUnits(system,offset));
  1915. IntermediateCode.MakeMemory(left,addressType);
  1916. BreqL(trueL,left,right);
  1917. END;
  1918. ReleaseIntermediateOperand(left); ReleaseIntermediateOperand(right);
  1919. BrL(falseL);
  1920. END;
  1921. END TypeTest;
  1922. PROCEDURE Error(position: LONGINT; CONST s: ARRAY OF CHAR);
  1923. BEGIN
  1924. backend.Error(module.module.sourceName,position,Diagnostics.Invalid,s);
  1925. IF dump # NIL THEN
  1926. dump.String(s); dump.Ln;
  1927. END;
  1928. END Error;
  1929. PROCEDURE Warning(position: LONGINT; CONST s: ARRAY OF CHAR);
  1930. BEGIN
  1931. backend.diagnostics.Warning(module.module.sourceName,position,Diagnostics.Invalid,s);
  1932. IF dump # NIL THEN
  1933. dump.String(s); dump.Ln; dump.Update;
  1934. END;
  1935. END Warning;
  1936. PROCEDURE CreateTraceModuleMethod(mod: SyntaxTree.Module);
  1937. VAR name: Basic.SectionName; pooledName: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  1938. VAR variable: SyntaxTree.Variable; register,op: IntermediateCode.Operand;
  1939. operand:Operand;
  1940. BEGIN
  1941. previousSection := section;
  1942. Global.GetModuleName(mod,name);
  1943. Strings.Append(name,".@Trace");
  1944. Basic.ToSegmentedName(name, pooledName);
  1945. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, pooledName,NIL,TRUE);
  1946. IF dump # NIL THEN dump := section.comments END;
  1947. variable := mod.moduleScope.firstVariable;
  1948. WHILE variable # NIL DO
  1949. IF ~variable.untraced & variable.type.resolved.hasPointers THEN
  1950. Symbol(variable, operand);
  1951. register := operand.op;
  1952. CallTraceMethod(register, variable.type);
  1953. ReleaseIntermediateOperand(register);
  1954. END;
  1955. variable := variable.nextVariable;
  1956. END;
  1957. Basic.ToSegmentedName ("Modules.Module.@Trace",pooledName);
  1958. IntermediateCode.InitAddress(op, addressType, pooledName , 0, 0);
  1959. Emit(Br(position,op));
  1960. INC(statCoopTraceModule, section.pc);
  1961. section := previousSection;
  1962. IF dump # NIL THEN dump := section.comments END;
  1963. END CreateTraceModuleMethod;
  1964. PROCEDURE CallAssignPointer(CONST dst (* address *) , src (* value *): IntermediateCode.Operand);
  1965. BEGIN
  1966. Emit (Push(position, dst));
  1967. Emit (Push(position, src));
  1968. CallThis(position,"GarbageCollector","Assign", 2);
  1969. END CallAssignPointer;
  1970. PROCEDURE CallAssignMethod(CONST dst (* address *) , src (* address *) : IntermediateCode.Operand; type: SyntaxTree.Type);
  1971. VAR name: Basic.SegmentedName; size: LONGINT; base: SyntaxTree.Type; op: IntermediateCode.Operand;
  1972. BEGIN
  1973. IF SemanticChecker.IsPointerType (type) THEN
  1974. CallAssignPointer(dst, IntermediateCode.Memory (addressType,src,0));
  1975. ELSIF type.IsRecordType() THEN
  1976. Emit (Push(position,dst));
  1977. Emit (Push(position,src));
  1978. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  1979. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1980. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1981. Emit(Call(position,op, ToMemoryUnits(system, 2*system.addressSize)));
  1982. ELSIF IsStaticArray(type) THEN
  1983. size := StaticArrayNumElements(type);
  1984. base := StaticArrayBaseType(type);
  1985. IF base.IsRecordType() THEN
  1986. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1987. Emit (Push(position, dst));
  1988. Emit (Push(position, src));
  1989. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  1990. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  1991. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  1992. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  1993. Emit(Call(position,op,ToMemoryUnits(system, 3*system.addressSize)));
  1994. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  1995. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1996. Emit (Push(position, dst));
  1997. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  1998. Emit (Push(position, src));
  1999. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  2000. ELSE
  2001. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2002. Emit (Push(position, dst));
  2003. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2004. Emit (Push(position, src));
  2005. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  2006. ASSERT(StaticArrayBaseType(type).IsPointer());
  2007. END;
  2008. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2009. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2010. Emit (Push(position, dst));
  2011. Emit (Push(position, src));
  2012. CallThis(position,"GarbageCollector","AssignDelegate", 2);
  2013. ELSE HALT(100); (* missing ? *)
  2014. END;
  2015. END CallAssignMethod;
  2016. PROCEDURE CreateAssignProcedure (recordType: SyntaxTree.RecordType);
  2017. VAR name: Basic.SegmentedName;
  2018. VAR variable: SyntaxTree.Variable; src, dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2019. parameter1, parameter2, parameter0: IntermediateCode.Operand; label: Label;
  2020. context: Context;
  2021. BEGIN
  2022. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,3*addressType.sizeInBits));
  2023. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2024. parameter2 (* src *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2025. GetRecordTypeName (recordType,name);
  2026. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2027. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2028. IF dump # NIL THEN dump := section.comments END;
  2029. variable := recordType.recordScope.firstVariable;
  2030. WHILE variable # NIL DO
  2031. IF variable.NeedsTrace() THEN
  2032. dst := NewRegisterOperand (addressType);
  2033. src := NewRegisterOperand (addressType);
  2034. Emit (Mov(position, dst, parameter1));
  2035. Emit (Mov(position, src, parameter2));
  2036. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2037. IntermediateCode.AddOffset(src,ToMemoryUnits(system,variable.offsetInBits));
  2038. CallAssignMethod(dst, src, variable.type);
  2039. ReleaseIntermediateOperand(src);
  2040. ReleaseIntermediateOperand(dst);
  2041. END;
  2042. variable := variable.nextVariable;
  2043. END;
  2044. recordBase := recordType.GetBaseRecord();
  2045. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2046. GetRecordTypeName (recordBase,name);
  2047. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2048. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2049. Emit(Br(position,op));
  2050. ELSE
  2051. Emit(Exit(position,0,0));
  2052. END;
  2053. IF ~recordType.isObject THEN
  2054. GetRecordTypeName (recordType,name);
  2055. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2056. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2057. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2058. NEW(registerUsageCount);
  2059. usedRegisters := NIL;
  2060. dst := NewRegisterOperand (addressType);
  2061. src := NewRegisterOperand (addressType);
  2062. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2063. Emit(Mov(position, dst, parameter1));
  2064. Emit(Mov(position, src, parameter2));
  2065. label := NewLabel();
  2066. SetLabel(label);
  2067. Emit(Push(position, dst));
  2068. Emit(Push(position, src));
  2069. GetRecordTypeName (recordType,name);
  2070. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  2071. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2072. Emit(Call(position, op, 0));
  2073. Emit(Pop(position, src));
  2074. Emit(Pop(position, dst));
  2075. Emit(Add(position, dst, dst, ofs));
  2076. Emit(Add(position, src, src, ofs));
  2077. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2078. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2079. Emit(Exit(position,0,0));
  2080. END;
  2081. INC(statCoopAssignProcedure, section.pc);
  2082. ReturnToContext(context);
  2083. IF dump # NIL THEN dump := section.comments END;
  2084. END CreateAssignProcedure;
  2085. PROCEDURE CallTraceMethod(CONST register: IntermediateCode.Operand; type: SyntaxTree.Type);
  2086. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; size: LONGINT; base: SyntaxTree.Type; skip: Label;
  2087. BEGIN
  2088. IF IsUnsafePointer (type) THEN
  2089. skip := NewLabel();
  2090. IntermediateCode.InitRegister(op, addressType, IntermediateCode.GeneralPurposeRegister, register.register);
  2091. Emit (Mov (position, op, IntermediateCode.Memory (addressType,register,0)));
  2092. BreqL (skip, op, nil);
  2093. CallTraceMethod (op,type.resolved(SyntaxTree.PointerType).pointerBase);
  2094. SetLabel (skip);
  2095. ELSIF SemanticChecker.IsPointerType (type) THEN
  2096. Emit (Push(position, IntermediateCode.Memory (addressType,register,0)));
  2097. CallThis(position,"GarbageCollector","Mark", 1);
  2098. ELSIF type.IsRecordType() THEN
  2099. Emit (Push(position,register));
  2100. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2101. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2102. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2103. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2104. ELSIF IsStaticArray(type) THEN
  2105. size := StaticArrayNumElements(type);
  2106. base := StaticArrayBaseType(type);
  2107. IF base.IsRecordType() THEN
  2108. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2109. Emit (Push(position, register));
  2110. GetRecordTypeName (base.resolved(SyntaxTree.RecordType), name);
  2111. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2112. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2113. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2114. Emit(Call(position, op, ToMemoryUnits(system, system.addressSize*2)));
  2115. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2116. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2117. Emit (Push(position, register));
  2118. CallThis(position,"GarbageCollector","MarkDelegateArray", 2);
  2119. ELSE
  2120. Emit (Push(position, IntermediateCode.Immediate (sizeType,size)));
  2121. Emit (Push(position, register));
  2122. CallThis(position,"GarbageCollector","MarkPointerArray", 2);
  2123. ASSERT(base.IsPointer());
  2124. END;
  2125. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2126. Emit (Push(position, IntermediateCode.Memory (addressType,register,ToMemoryUnits(system,addressType.sizeInBits))));
  2127. CallThis(position,"GarbageCollector","Mark", 1);
  2128. ELSE HALT(100); (* missing ? *)
  2129. END;
  2130. END CallTraceMethod;
  2131. PROCEDURE CreateTraceMethod (recordType: SyntaxTree.RecordType);
  2132. VAR name: Basic.SegmentedName; previousSection: IntermediateCode.Section;
  2133. VAR variable: SyntaxTree.Variable; register,op,ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2134. parameter0, parameter1: IntermediateCode.Operand; label: Label; context: Context;
  2135. BEGIN
  2136. previousSection := section;
  2137. parameter0 (* size *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2138. parameter1 (* address *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2139. GetRecordTypeName (recordType,name);
  2140. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2141. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2142. IF dump # NIL THEN dump := section.comments END;
  2143. variable := recordType.recordScope.firstVariable;
  2144. WHILE variable # NIL DO
  2145. IF variable.NeedsTrace() THEN
  2146. register := NewRegisterOperand (addressType);
  2147. Emit (Mov(position,register,parameter1));
  2148. IntermediateCode.AddOffset(register,ToMemoryUnits(system,variable.offsetInBits));
  2149. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2150. IntermediateCode.AddOffset(register,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2151. END;
  2152. CallTraceMethod(register, variable.type);
  2153. ReleaseIntermediateOperand(register);
  2154. END;
  2155. variable := variable.nextVariable;
  2156. END;
  2157. recordBase := recordType.GetBaseRecord();
  2158. WHILE (recordBase # NIL) & ~recordBase.hasPointers DO
  2159. recordBase := recordBase.GetBaseRecord();
  2160. END;
  2161. IF recordBase # NIL THEN
  2162. GetRecordTypeName (recordBase,name);
  2163. IF HasExplicitTraceMethod (recordBase) THEN
  2164. Basic.SuffixSegmentedName (name, Basic.MakeString ("Trace"));
  2165. ELSE
  2166. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2167. END;
  2168. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2169. Emit(Br(position,op));
  2170. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  2171. Emit(Exit(position,0,0));
  2172. ELSE
  2173. IF recordType.isObject THEN
  2174. Basic.ToSegmentedName ("BaseTypes.Object",name);
  2175. ELSE
  2176. Basic.ToSegmentedName ("BaseTypes.Record",name);
  2177. END;
  2178. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2179. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2180. Emit(Br(position,op));
  2181. END;
  2182. IF ~recordType.isObject THEN
  2183. GetRecordTypeName (recordType,name);
  2184. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  2185. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2186. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2187. NEW(registerUsageCount);
  2188. usedRegisters := NIL;
  2189. IF dump # NIL THEN dump := section.comments END;
  2190. register := NewRegisterOperand (addressType);
  2191. Emit (Mov(position,register,IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits))));
  2192. IF (recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain THEN
  2193. IntermediateCode.AddOffset(register,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2194. END;
  2195. Emit (Push(position,register));
  2196. GetRecordTypeName (recordType,name);
  2197. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2198. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2199. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2200. ReleaseIntermediateOperand(register);
  2201. Emit(Exit(position,0,0));
  2202. GetRecordTypeName (recordType,name);
  2203. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2204. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2205. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2206. NEW(registerUsageCount);
  2207. usedRegisters := NIL;
  2208. register := NewRegisterOperand (addressType);
  2209. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2210. Emit(Mov(position, register, parameter1));
  2211. label := NewLabel();
  2212. SetLabel(label);
  2213. Emit(Push(position, register));
  2214. GetRecordTypeName (recordType,name);
  2215. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  2216. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2217. Emit(Call(position, op, 0));
  2218. Emit(Pop(position, register));
  2219. Emit(Add(position, register, register, ofs));
  2220. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2221. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2222. Emit(Exit(position,0,0));
  2223. END;
  2224. INC(statCoopTraceMethod, section.pc);
  2225. ReturnToContext(context);
  2226. IF dump # NIL THEN dump := section.comments END;
  2227. END CreateTraceMethod;
  2228. PROCEDURE CreateResetProcedure (recordType: SyntaxTree.RecordType);
  2229. VAR name: Basic.SegmentedName;
  2230. VAR variable: SyntaxTree.Variable; dst, op, ofs: IntermediateCode.Operand; recordBase: SyntaxTree.RecordType;
  2231. parameter1, parameter0: IntermediateCode.Operand; label: Label;
  2232. context: Context;
  2233. BEGIN
  2234. IF recordType.isObject THEN RETURN END;
  2235. parameter0 (* len *) := IntermediateCode.Memory(sizeType,sp,ToMemoryUnits(system,2*addressType.sizeInBits));
  2236. parameter1 (* dest *) := IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,1*addressType.sizeInBits));
  2237. GetRecordTypeName (recordType,name);
  2238. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2239. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2240. IF dump # NIL THEN dump := section.comments END;
  2241. variable := recordType.recordScope.firstVariable;
  2242. WHILE variable # NIL DO
  2243. IF variable.NeedsTrace() THEN
  2244. dst := NewRegisterOperand (addressType);
  2245. Emit (Mov(position, dst, parameter1));
  2246. IntermediateCode.AddOffset(dst,ToMemoryUnits(system,variable.offsetInBits));
  2247. IF recordType.isObject & ((recordType.pointerType = NIL) OR ~recordType.pointerType.isPlain) THEN
  2248. IntermediateCode.AddOffset(dst,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  2249. END;
  2250. CallResetProcedure(dst, nil, variable.type);
  2251. ReleaseIntermediateOperand(dst);
  2252. END;
  2253. variable := variable.nextVariable;
  2254. END;
  2255. recordBase := recordType.GetBaseRecord();
  2256. IF (recordBase # NIL) & recordBase.NeedsTrace() THEN
  2257. GetRecordTypeName (recordBase,name);
  2258. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2259. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2260. Emit(Br(position,op));
  2261. ELSE
  2262. Emit(Exit(position,0,0));
  2263. END;
  2264. GetRecordTypeName (recordType,name);
  2265. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2266. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2267. section := IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE);
  2268. NEW(registerUsageCount);
  2269. usedRegisters := NIL;
  2270. dst := NewRegisterOperand (addressType);
  2271. ofs := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(recordType)));
  2272. Emit(Mov(position, dst, parameter1));
  2273. label := NewLabel();
  2274. SetLabel(label);
  2275. Emit(Push(position, dst));
  2276. GetRecordTypeName (recordType,name);
  2277. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2278. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2279. Emit(Call(position, op, 0));
  2280. Emit(Pop(position, dst));
  2281. Emit(Add(position, dst, dst, ofs));
  2282. Emit(Sub(position, parameter0, parameter0, IntermediateCode.Immediate(sizeType, 1)));
  2283. BrneL(label, parameter0, IntermediateCode.Immediate(sizeType, 0));
  2284. Emit(Exit(position,0,0));
  2285. INC(statCoopResetProcedure, section.pc);
  2286. ReturnToContext(context);
  2287. IF dump # NIL THEN dump := section.comments END;
  2288. END CreateResetProcedure;
  2289. PROCEDURE CreateResetMethod (scope: SyntaxTree.ProcedureScope);
  2290. VAR name: Basic.SegmentedName; context: Context;
  2291. BEGIN
  2292. GetCodeSectionNameForSymbol(scope.ownerProcedure, name);
  2293. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2294. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.CodeSection, name,NIL,TRUE));
  2295. IF dump # NIL THEN dump := section.comments END;
  2296. Emit(Push(position,fp));
  2297. Emit(Mov(position,fp, IntermediateCode.Memory(addressType,sp,ToMemoryUnits(system,addressType.sizeInBits * 2))));
  2298. ResetVariables(scope);
  2299. Emit(Pop(position,fp));
  2300. Emit(Exit(position,0,0));
  2301. ReturnToContext(context);
  2302. IF dump # NIL THEN dump := section.comments END;
  2303. END CreateResetMethod;
  2304. PROCEDURE CallResetProcedure(dest, tag: IntermediateCode.Operand; type: SyntaxTree.Type);
  2305. VAR base: SyntaxTree.Type; op, size: IntermediateCode.Operand; name: Basic.SegmentedName;
  2306. BEGIN
  2307. IF SemanticChecker.IsPointerType (type) THEN
  2308. Emit (Push(position, dest));
  2309. CallThis(position,"GarbageCollector","Reset", 1);
  2310. ELSIF type.IsRecordType() THEN
  2311. Emit (Push(position, dest));
  2312. GetRecordTypeName (type.resolved(SyntaxTree.RecordType),name);
  2313. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2314. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2315. Emit(Call(position,op, ToMemoryUnits(system, system.addressSize)));
  2316. ELSIF type.resolved IS SyntaxTree.ArrayType THEN
  2317. size := GetArrayLength(type, tag);
  2318. base := ArrayBaseType(type);
  2319. IF base.IsRecordType() THEN
  2320. Emit (Push(position, size));
  2321. Emit (Push(position, dest));
  2322. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  2323. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  2324. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2325. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2326. Emit(Call(position,op,ToMemoryUnits(system, 2*system.addressSize)));
  2327. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  2328. Emit (Push(position, size));
  2329. Emit (Push(position, dest));
  2330. CallThis(position,"GarbageCollector","ResetDelegateArray", 2);
  2331. ELSE
  2332. Emit (Push(position, size));
  2333. Emit (Push(position, dest));
  2334. CallThis(position,"GarbageCollector","ResetArray", 2);
  2335. ASSERT(ArrayBaseType(type).IsPointer());
  2336. END;
  2337. ReleaseIntermediateOperand(size);
  2338. ELSIF type.resolved IS SyntaxTree.ProcedureType THEN
  2339. ASSERT(type.resolved(SyntaxTree.ProcedureType).isDelegate);
  2340. Emit (Push(position, dest));
  2341. CallThis(position,"GarbageCollector","ResetDelegate", 1);
  2342. ELSE HALT(100); (* missing ? *)
  2343. END;
  2344. END CallResetProcedure;
  2345. PROCEDURE ResetVariables (scope: SyntaxTree.ProcedureScope);
  2346. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; previousScope: SyntaxTree.Scope; pc: LONGINT;
  2347. PROCEDURE Reset (symbol: SyntaxTree.Symbol);
  2348. VAR operand: Operand;
  2349. BEGIN
  2350. Symbol (symbol, operand);
  2351. CallResetProcedure(operand.op, operand.tag, symbol.type.resolved);
  2352. ReleaseOperand(operand);
  2353. END Reset;
  2354. BEGIN
  2355. previousScope := currentScope;
  2356. currentScope := scope;
  2357. pc := section.pc;
  2358. variable := scope.firstVariable;
  2359. WHILE variable # NIL DO
  2360. IF variable.NeedsTrace() THEN
  2361. Reset (variable);
  2362. END;
  2363. variable := variable.nextVariable;
  2364. END;
  2365. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  2366. WHILE parameter # NIL DO
  2367. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  2368. Reset (parameter);
  2369. END;
  2370. parameter := parameter.nextParameter;
  2371. END;
  2372. INC(statCoopResetVariables, section.pc - pc);
  2373. currentScope := previousScope;
  2374. END ResetVariables;
  2375. PROCEDURE CreateProcedureDescriptor (procedure: SyntaxTree.Procedure);
  2376. VAR previousSection: IntermediateCode.Section; name: Basic.SegmentedName;
  2377. VAR op: IntermediateCode.Operand; context: Context;
  2378. BEGIN
  2379. previousSection := section;
  2380. GetCodeSectionNameForSymbol(procedure, name);
  2381. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Descriptor"));
  2382. context := SwitchContext(IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE));
  2383. IF dump # NIL THEN dump := section.comments END;
  2384. Basic.ToSegmentedName ("BaseTypes.StackFrame",name);
  2385. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2386. Emit(Data(position,op));
  2387. Emit(Data(position,nil));
  2388. IF HasPointers (procedure.procedureScope) THEN
  2389. GetCodeSectionNameForSymbol(procedure, name);
  2390. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Reset"));
  2391. ELSE
  2392. Basic.ToSegmentedName ("BaseTypes.StackFrame.Reset",name);
  2393. END;
  2394. IntermediateCode.InitAddress(op, addressType, name , 0, 0);
  2395. Emit(Data(position,op));
  2396. ReturnToContext(context);
  2397. IF dump # NIL THEN dump := section.comments END;
  2398. END CreateProcedureDescriptor;
  2399. PROCEDURE AddImport(CONST moduleName: ARRAY OF CHAR; VAR module: SyntaxTree.Module; force: BOOLEAN): BOOLEAN;
  2400. VAR import: SyntaxTree.Import;
  2401. s: Basic.MessageString;
  2402. selfName: SyntaxTree.IdentifierString;
  2403. BEGIN
  2404. moduleScope.ownerModule.GetName(selfName);
  2405. IF (moduleName = selfName) & (moduleScope.ownerModule.context = Global.A2Name) THEN
  2406. module := moduleScope.ownerModule
  2407. ELSE
  2408. import := moduleScope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  2409. IF import = NIL THEN
  2410. import := SyntaxTree.NewImport(-1,SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier(moduleName),TRUE);
  2411. import.SetContext(SyntaxTree.NewIdentifier("A2"));
  2412. IF ~checker.AddImport(moduleScope.ownerModule,import) THEN
  2413. s := "Module ";
  2414. Strings.Append(s,moduleName);
  2415. Strings.Append(s," cannot be imported.");
  2416. IF force THEN
  2417. Error(position,s);
  2418. ELSIF canBeLoaded THEN
  2419. Strings.Append(s, "=> no dynamic linking.");
  2420. Warning(position, s);
  2421. canBeLoaded := FALSE;
  2422. END;
  2423. RETURN FALSE
  2424. ELSE
  2425. SELF.module.imports.AddName(moduleName)
  2426. END;
  2427. ELSIF import.module = NIL THEN (* already tried *)
  2428. RETURN FALSE
  2429. END;
  2430. module := import.module;
  2431. END;
  2432. RETURN TRUE
  2433. END AddImport;
  2434. (* needed for old binary object file format*)
  2435. PROCEDURE EnsureSymbol(CONST moduleName,procedureName: SyntaxTree.IdentifierString);
  2436. VAR r: Operand; procedure: SyntaxTree.Procedure; module: SyntaxTree.Module; s: ARRAY 128 OF CHAR; fp: LONGINT;
  2437. BEGIN
  2438. IF AddImport(moduleName,module,TRUE) THEN
  2439. procedure := module.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2440. IF procedure = NIL THEN
  2441. s := "Instruction not supported on target, emulation procedure ";
  2442. Strings.Append(s,moduleName);
  2443. Strings.Append(s,".");
  2444. Strings.Append(s,procedureName);
  2445. Strings.Append(s," not present");
  2446. Error(position,s);
  2447. ELSE
  2448. StaticCallOperand(r,procedure);
  2449. ReleaseOperand(r);
  2450. fp := GetFingerprint(procedure);
  2451. END;
  2452. END;
  2453. END EnsureSymbol;
  2454. PROCEDURE ConditionToValue(x: SyntaxTree.Expression);
  2455. VAR exit: Label; trueL,falseL: Label;
  2456. BEGIN
  2457. trueL := NewLabel();
  2458. falseL := NewLabel();
  2459. exit := NewLabel();
  2460. Condition(x,trueL,falseL);
  2461. InitOperand(result,ModeValue);
  2462. SetLabel(trueL);
  2463. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister,AcquireRegister(IntermediateCode.GetType(system,x.type),IntermediateCode.GeneralPurposeRegister));
  2464. Emit(Mov(position,result.op,true));
  2465. BrL(exit);
  2466. SetLabel(falseL);
  2467. Emit(MovReplace(position,result.op,false));
  2468. SetLabel(exit);
  2469. END ConditionToValue;
  2470. PROCEDURE ValueToCondition(VAR op: Operand);
  2471. BEGIN
  2472. LoadValue(op,system.booleanType);
  2473. BrneL(trueLabel,op.op, false);
  2474. ReleaseOperand(op);
  2475. BrL(falseLabel);
  2476. END ValueToCondition;
  2477. PROCEDURE GetDynamicSize(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2478. VAR size: LONGINT;
  2479. PROCEDURE GetArraySize(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2480. VAR baseType: SyntaxTree.Type; size: LONGINT; sizeOperand,len,res: IntermediateCode.Operand;
  2481. BEGIN
  2482. ASSERT(type.form = SyntaxTree.Open);
  2483. baseType := type.arrayBase.resolved;
  2484. IF IsOpenArray(baseType) THEN
  2485. sizeOperand := GetArraySize(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2486. ELSE
  2487. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  2488. sizeOperand := IntermediateCode.Immediate(addressType,size);
  2489. END;
  2490. len := tag;
  2491. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2492. IntermediateCode.MakeMemory(len,addressType);
  2493. UseIntermediateOperand(len);
  2494. Reuse2(res,sizeOperand,len);
  2495. Emit(Mul(position,res,sizeOperand,len));
  2496. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2497. RETURN res
  2498. END GetArraySize;
  2499. BEGIN
  2500. type := type.resolved;
  2501. IF IsOpenArray(type) THEN
  2502. IF tag.mode = IntermediateCode.ModeImmediate THEN (* special rule for winapi/c arrays *)
  2503. RETURN tag
  2504. ELSE
  2505. RETURN GetArraySize(type.resolved(SyntaxTree.ArrayType),0)
  2506. END;
  2507. ELSE
  2508. size := ToMemoryUnits(system,system.SizeOf(type));
  2509. RETURN IntermediateCode.Immediate(addressType,size)
  2510. END;
  2511. END GetDynamicSize;
  2512. PROCEDURE GetArrayLength(type: SyntaxTree.Type; tag: IntermediateCode.Operand):IntermediateCode.Operand;
  2513. PROCEDURE GetLength(type: SyntaxTree.ArrayType; offset: LONGINT):IntermediateCode.Operand;
  2514. VAR baseType: SyntaxTree.Type; sizeOperand,len,res: IntermediateCode.Operand;
  2515. BEGIN
  2516. ASSERT(type.form = SyntaxTree.Open);
  2517. baseType := type.arrayBase.resolved;
  2518. IF IsOpenArray(baseType) THEN
  2519. sizeOperand := GetLength(baseType(SyntaxTree.ArrayType),offset+system.addressSize);
  2520. ELSE
  2521. sizeOperand := IntermediateCode.Immediate(addressType,StaticArrayNumElements(baseType));
  2522. END;
  2523. len := tag;
  2524. IntermediateCode.AddOffset(len,ToMemoryUnits(system,offset));
  2525. IntermediateCode.MakeMemory(len,addressType);
  2526. UseIntermediateOperand(len);
  2527. Reuse2(res,sizeOperand,len);
  2528. Emit(Mul(position,res,sizeOperand,len));
  2529. ReleaseIntermediateOperand(sizeOperand); ReleaseIntermediateOperand(len);
  2530. RETURN res
  2531. END GetLength;
  2532. BEGIN
  2533. type := type.resolved;
  2534. IF IsOpenArray(type) THEN
  2535. ASSERT(tag.mode # IntermediateCode.ModeImmediate);
  2536. RETURN GetLength(type.resolved(SyntaxTree.ArrayType),0)
  2537. ELSE
  2538. RETURN IntermediateCode.Immediate(addressType,1)
  2539. END;
  2540. END GetArrayLength;
  2541. PROCEDURE GetSizeFromTag(tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2542. VAR result: IntermediateCode.Operand;
  2543. BEGIN
  2544. IF backend.cooperative THEN
  2545. MakeMemory(result, tag, addressType, ToMemoryUnits(system,system.addressSize));
  2546. ELSE
  2547. MakeMemory(result, tag, addressType, 0);
  2548. END;
  2549. RETURN result
  2550. END GetSizeFromTag;
  2551. PROCEDURE GetArrayOfBytesSize(e: SyntaxTree.Expression; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  2552. VAR parameter: SyntaxTree.Parameter;
  2553. BEGIN
  2554. IF (e IS SyntaxTree.SymbolDesignator) & (e(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  2555. parameter := e(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  2556. IF (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) & (parameter.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2557. ELSIF e IS SyntaxTree.DereferenceDesignator THEN
  2558. IF (e.type.resolved IS SyntaxTree.RecordType) THEN RETURN GetSizeFromTag(tag) END;
  2559. END;
  2560. RETURN GetDynamicSize(e.type, tag);
  2561. END GetArrayOfBytesSize;
  2562. (*
  2563. to find imported symbol. not needed ?
  2564. PROCEDURE SymbolByName(CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  2565. VAR importedModule: SyntaxTree.Module; symbol: SyntaxTree.Symbol;
  2566. BEGIN
  2567. IF AddImport(moduleName,importedModule,FALSE) THEN
  2568. symbol := importedModule.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  2569. RETURN symbol
  2570. ELSE
  2571. RETURN NIL
  2572. END
  2573. END SymbolByName;
  2574. *)
  2575. PROCEDURE GetRuntimeProcedure(CONST moduleName, procedureName: ARRAY OF CHAR; VAR procedure: SyntaxTree.Procedure; force: BOOLEAN): BOOLEAN;
  2576. VAR runtimeModule: SyntaxTree.Module; s: Basic.MessageString;
  2577. BEGIN
  2578. IF AddImport(moduleName,runtimeModule,force) THEN
  2579. procedure := runtimeModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  2580. IF procedure = NIL THEN
  2581. s := "Procedure ";
  2582. Strings.Append(s,moduleName);
  2583. Strings.Append(s,".");
  2584. Strings.Append(s,procedureName);
  2585. Strings.Append(s," not present");
  2586. Error(position,s);
  2587. RETURN FALSE
  2588. ELSE
  2589. RETURN TRUE
  2590. END;
  2591. ELSE RETURN FALSE
  2592. END;
  2593. END GetRuntimeProcedure;
  2594. PROCEDURE GetTypeDescriptor(CONST moduleName, typeName: ARRAY OF CHAR; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  2595. VAR importedModule: SyntaxTree.Module; source: IntermediateCode.Section; symbol: SyntaxTree.Symbol;
  2596. s: Basic.MessageString;
  2597. BEGIN
  2598. Basic.InitSegmentedName(name);
  2599. name[0] := Basic.MakeString(moduleName);
  2600. name[1] := Basic.MakeString(typeName);
  2601. name[2] := -1;
  2602. IF AddImport(moduleName,importedModule, FALSE) THEN
  2603. symbol := importedModule.moduleScope.FindTypeDeclaration(SyntaxTree.NewIdentifier(typeName));
  2604. IF symbol = NIL THEN
  2605. s := "type ";
  2606. Strings.Append(s,moduleName);
  2607. Strings.Append(s,".");
  2608. Strings.Append(s,typeName);
  2609. Strings.Append(s," not present");
  2610. Error(position,s);
  2611. END;
  2612. ELSE symbol := NIL;
  2613. END;
  2614. IF importedModule = moduleScope.ownerModule THEN
  2615. source := NewSection(module.allSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2616. ELSE
  2617. source := NewSection(module.importedSections, Sections.ConstSection, name, symbol, commentPrintout # NIL);
  2618. END;
  2619. RETURN symbol
  2620. END GetTypeDescriptor;
  2621. (* 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. *)
  2622. PROCEDURE CallThis(position: LONGINT; CONST moduleName, procedureName: ARRAY OF CHAR; numberParameters: LONGINT);
  2623. VAR procedure: SyntaxTree.Procedure; result: Operand; reg: IntermediateCode.Operand; source: IntermediateCode.Section;
  2624. pooledName: Basic.SegmentedName; size: LONGINT;
  2625. BEGIN
  2626. IF GetRuntimeProcedure(moduleName,procedureName,procedure,numberParameters < 0) THEN (* ready for dynamic linking *)
  2627. StaticCallOperand(result,procedure);
  2628. IF numberParameters < 0 THEN
  2629. size := ProcedureParametersSize(system,procedure);
  2630. ELSE
  2631. size := ToMemoryUnits(system,numberParameters * system.addressSize);
  2632. IF size # ProcedureParametersSize(system,procedure) THEN
  2633. Error(position,"runtime call parameter count mismatch");
  2634. END;
  2635. END;
  2636. Emit(Call(position, result.op, size));
  2637. ReleaseOperand(result);
  2638. ELSE (* only static linking possible *)
  2639. Basic.InitSegmentedName(pooledName);
  2640. pooledName[0] := Basic.MakeString(moduleName);
  2641. pooledName[1] := Basic.MakeString(procedureName);
  2642. pooledName[2] := -1;
  2643. source := NewSection(module.importedSections, Sections.CodeSection, pooledName, NIL,commentPrintout # NIL);
  2644. IntermediateCode.InitAddress(reg, addressType, pooledName , 0, 0);
  2645. Emit(Call(position,reg, ToMemoryUnits(system,numberParameters * system.addressSize)));
  2646. END;
  2647. END CallThis;
  2648. PROCEDURE CompareString(br: ConditionalBranch; leftExpression,rightExpression: SyntaxTree.Expression);
  2649. VAR
  2650. left,right: Operand;
  2651. leftSize, rightSize: IntermediateCode.Operand;
  2652. saved: RegisterEntry;
  2653. reg: IntermediateCode.Operand;
  2654. procedureName: SyntaxTree.IdentifierString;
  2655. BEGIN
  2656. procedureName := "CompareString";
  2657. SaveRegisters();ReleaseUsedRegisters(saved);
  2658. Designate(leftExpression,left);
  2659. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2660. Emit(Push(position,leftSize));
  2661. ReleaseIntermediateOperand(leftSize);
  2662. Emit(Push(position,left.op));
  2663. ReleaseOperand(left);
  2664. Designate(rightExpression,right);
  2665. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2666. Emit(Push(position,rightSize));
  2667. ReleaseIntermediateOperand(rightSize);
  2668. Emit(Push(position,right.op));
  2669. ReleaseOperand(right);
  2670. IF backend.cooperative THEN
  2671. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2672. ELSE
  2673. CallThis(position,runtimeModuleName,procedureName, 4);
  2674. END;
  2675. IntermediateCode.InitRegister(reg,int8,IntermediateCode.GeneralPurposeRegister,AcquireRegister(int8,IntermediateCode.GeneralPurposeRegister));
  2676. Emit(Result(position,reg));
  2677. (*
  2678. AcquireThisRegister(int8,IntermediateCode.Result);
  2679. *)
  2680. RestoreRegisters(saved); (* must be done before use of result, might be jumped over otherwise *)
  2681. (*
  2682. IntermediateCode.InitRegister(reg,int8,IntermediateCode.Result);
  2683. *)
  2684. br(trueLabel,reg,IntermediateCode.Immediate(int8,0));
  2685. ReleaseIntermediateOperand(reg);
  2686. BrL(falseLabel);
  2687. END CompareString;
  2688. PROCEDURE CopyString(leftExpression,rightExpression: SyntaxTree.Expression);
  2689. VAR
  2690. left,right: Operand;
  2691. leftSize, rightSize: IntermediateCode.Operand;
  2692. saved: RegisterEntry;
  2693. procedureName: SyntaxTree.IdentifierString;
  2694. BEGIN
  2695. procedureName := "CopyString";
  2696. SaveRegisters();ReleaseUsedRegisters(saved);
  2697. Designate(leftExpression,left);
  2698. leftSize := GetDynamicSize(leftExpression.type,left.tag);
  2699. Emit(Push(position,leftSize));
  2700. ReleaseIntermediateOperand(leftSize);
  2701. Emit(Push(position,left.op));
  2702. ReleaseOperand(left);
  2703. Designate(rightExpression,right);
  2704. rightSize := GetDynamicSize(rightExpression.type,right.tag);
  2705. Emit(Push(position,rightSize));
  2706. ReleaseIntermediateOperand(rightSize);
  2707. Emit(Push(position,right.op));
  2708. ReleaseOperand(right);
  2709. IF backend.cooperative THEN
  2710. CallThis(position,DefaultRuntimeModuleName,procedureName, 4);
  2711. ELSE
  2712. CallThis(position,runtimeModuleName,procedureName,4);
  2713. END;
  2714. RestoreRegisters(saved);
  2715. END CopyString;
  2716. PROCEDURE VisitBinaryExpression(x: SyntaxTree.BinaryExpression);
  2717. VAR left,right: Operand; temp: Operand; zero, one, tempReg, tempReg2: IntermediateCode.Operand;
  2718. leftType,rightType: SyntaxTree.Type;
  2719. leftExpression,rightExpression : SyntaxTree.Expression;
  2720. componentType: IntermediateCode.Type;
  2721. value: HUGEINT; exp: LONGINT;next,exit: Label; recordType: SyntaxTree.RecordType; dest: IntermediateCode.Operand;
  2722. size: LONGINT;
  2723. BEGIN
  2724. IF Trace THEN TraceEnter("VisitBinaryExpression") END;
  2725. dest := destination; destination := emptyOperand;
  2726. leftType := x.left.type.resolved;
  2727. rightType := x.right.type.resolved;
  2728. (* for "OR" and "&" the left and right expressions may not be emitted first <= shortcut evaluation *)
  2729. CASE x.operator OF
  2730. Scanner.Or:
  2731. (* shortcut evaluation of left OR right *)
  2732. IF ~conditional THEN ConditionToValue(x);
  2733. ELSE
  2734. next := NewLabel();
  2735. Condition(x.left,trueLabel,next);
  2736. SetLabel(next);
  2737. Condition(x.right,trueLabel,falseLabel);
  2738. END;
  2739. |Scanner.And:
  2740. (* shortcut evaluation of left & right *)
  2741. IF ~conditional THEN ConditionToValue(x);
  2742. ELSE
  2743. next := NewLabel();
  2744. Condition(x.left,next,falseLabel);
  2745. SetLabel(next);
  2746. Condition(x.right,trueLabel,falseLabel);
  2747. END;
  2748. |Scanner.Is:
  2749. IF ~conditional THEN ConditionToValue(x);
  2750. ELSE
  2751. (* get type desc tag *)
  2752. IF IsPointerToRecord(leftType,recordType) THEN
  2753. Evaluate(x.left,left);
  2754. Dereference(left,recordType,IsUnsafePointer(leftType))
  2755. ELSE
  2756. Designate(x.left,left);
  2757. END;
  2758. TypeTest(left.tag,x.right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved,trueLabel,falseLabel);
  2759. ReleaseOperand(left);
  2760. END;
  2761. |Scanner.Plus:
  2762. Evaluate(x.left,left);
  2763. Evaluate(x.right,right);
  2764. IF leftType IS SyntaxTree.SetType THEN
  2765. InitOperand(result,ModeValue);
  2766. Reuse2a(result.op,left.op,right.op,dest);
  2767. Emit(Or(position,result.op,left.op,right.op));
  2768. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2769. InitOperand(result,ModeValue);
  2770. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2771. Reuse2(result.tag,left.tag,right.tag);
  2772. Emit(Add(position,result.op,left.op,right.op));
  2773. Emit(Add(position,result.tag,left.tag,right.tag))
  2774. ELSE
  2775. InitOperand(result,ModeValue);
  2776. Reuse2a(result.op,left.op,right.op,dest);
  2777. Emit(Add(position,result.op,left.op,right.op));
  2778. END;
  2779. ReleaseOperand(left); ReleaseOperand(right);
  2780. |Scanner.Minus:
  2781. Evaluate(x.left,left);
  2782. Evaluate(x.right,right);
  2783. IF leftType IS SyntaxTree.SetType THEN
  2784. InitOperand(result,ModeValue);
  2785. Reuse1(result.op,right.op);
  2786. Emit(Not(position,result.op,right.op));
  2787. ReleaseOperand(right);
  2788. Emit(And(position,result.op,result.op,left.op));
  2789. ReleaseOperand(left);
  2790. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2791. InitOperand(result,ModeValue);
  2792. Reuse2a(result.op,left.op,right.op,dest); (* TODO: review this *)
  2793. Reuse2(result.tag,left.tag,right.tag);
  2794. Emit(Sub(position,result.op,left.op,right.op));
  2795. Emit(Sub(position,result.tag,left.tag,right.tag));
  2796. ReleaseOperand(left); ReleaseOperand(right)
  2797. ELSE
  2798. InitOperand(result,ModeValue);
  2799. Reuse2a(result.op,left.op,right.op,dest);
  2800. Emit(Sub(position,result.op,left.op,right.op));
  2801. ReleaseOperand(left); ReleaseOperand(right);
  2802. END;
  2803. |Scanner.Times:
  2804. Evaluate(x.left,left);
  2805. Evaluate(x.right,right);
  2806. IF leftType IS SyntaxTree.SetType THEN
  2807. InitOperand(result,ModeValue);
  2808. Reuse2a(result.op,left.op,right.op,dest);
  2809. Emit(And(position,result.op,left.op,right.op));
  2810. ELSIF (x.type.resolved IS SyntaxTree.IntegerType) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2811. InitOperand(result,ModeValue);
  2812. Reuse1a(result.op,left.op,dest);
  2813. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2814. Emit(Shl(position,result.op,left.op,right.op));
  2815. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2816. InitOperand(result, ModeValue);
  2817. componentType := left.op.type;
  2818. (* TODO: review this *)
  2819. (*
  2820. result.op = left.op * right.op - left.tag * right.tag
  2821. result.tag = left.tag * right.op + left.op * right.tag
  2822. *)
  2823. result.op := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2824. Emit(Mul(position,result.op, left.op, right.op));
  2825. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2826. Emit(Mul(position,tempReg, left.tag, right.tag));
  2827. Emit(Sub(position,result.op, result.op, tempReg));
  2828. Reuse2(result.tag, left.tag, right.op);
  2829. Emit(Mul(position,result.tag, left.tag, right.op));
  2830. Emit(Mul(position,tempReg, left.op, right.tag));
  2831. Emit(Add(position,result.tag, result.tag, tempReg));
  2832. ReleaseIntermediateOperand(tempReg)
  2833. ELSE
  2834. InitOperand(result,ModeValue);
  2835. Reuse2a(result.op,left.op,right.op,dest);
  2836. Emit(Mul(position,result.op,left.op,right.op));
  2837. END;
  2838. ReleaseOperand(left); ReleaseOperand(right);
  2839. |Scanner.Div:
  2840. Evaluate(x.left,left);
  2841. Evaluate(x.right,right);
  2842. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2843. InitOperand(result,ModeValue);
  2844. Reuse1a(result.op,left.op,dest);
  2845. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system, system.longintType),exp);
  2846. Emit(Shr(position,result.op,left.op,right.op));
  2847. ELSE
  2848. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2849. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2850. IF ~isUnchecked THEN
  2851. exit := NewLabel();
  2852. BrltL(exit,zero,right.op);
  2853. EmitTrap(position,NegativeDivisorTrap);
  2854. SetLabel(exit);
  2855. END;
  2856. END;
  2857. InitOperand(result,ModeValue);
  2858. Reuse2a(result.op,left.op,right.op,dest);
  2859. Emit(Div(position,result.op,left.op,right.op));
  2860. END;
  2861. ReleaseOperand(left); ReleaseOperand(right);
  2862. |Scanner.Mod:
  2863. Evaluate(x.left,left);
  2864. Evaluate(x.right,right);
  2865. IF ((x.type.resolved IS SyntaxTree.IntegerType) OR (x.type.resolved IS SyntaxTree.AddressType)) & IsIntegerConstant(x.right,value) & PowerOf2(value,exp) THEN
  2866. IntermediateCode.InitImmediate(right.op,IntermediateCode.GetType(system,x.type),value-1);
  2867. InitOperand(result,ModeValue);
  2868. Reuse1a(result.op,left.op,dest);
  2869. Emit(And(position,result.op,left.op,right.op));
  2870. ELSE
  2871. IF (x.type.resolved IS SyntaxTree.IntegerType) & (x.right.resolved = NIL) THEN (* divisor negative check *)
  2872. IntermediateCode.InitImmediate(zero,IntermediateCode.GetType(system,rightType),0);
  2873. IF ~isUnchecked THEN
  2874. exit := NewLabel();
  2875. BrltL(exit,zero,right.op);
  2876. EmitTrap(position,NegativeDivisorTrap);
  2877. SetLabel(exit);
  2878. END;
  2879. END;
  2880. InitOperand(result,ModeValue);
  2881. Reuse2a(result.op,left.op,right.op,dest);
  2882. Emit(Mod(position,result.op,left.op,right.op));
  2883. END;
  2884. ReleaseOperand(left); ReleaseOperand(right);
  2885. |Scanner.Slash:
  2886. Evaluate(x.left,left);
  2887. Evaluate(x.right,right);
  2888. IF leftType IS SyntaxTree.SetType THEN
  2889. InitOperand(result,ModeValue);
  2890. Reuse2a(result.op,left.op,right.op,dest);
  2891. Emit(Xor(position,result.op,left.op,right.op));
  2892. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2893. InitOperand(result,ModeValue);
  2894. componentType := left.op.type;
  2895. (* review this *)
  2896. (*
  2897. divisor = right.op * right.op + right.tag * right.tag
  2898. result.op = (left.op * right.op + left.tag * right.tag) / divisor
  2899. result.tag = (left.tag * right.op - left.op * right.tag) / divisor
  2900. *)
  2901. tempReg := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2902. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister,AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2903. Emit(Mul(position,tempReg, right.op, right.op));
  2904. Emit(Mul(position,tempReg2, right.tag, right.tag));
  2905. Emit(Add(position,tempReg, tempReg, tempReg2));
  2906. result.op := tempReg2;
  2907. Emit(Mul(position,result.op, left.op, right.op));
  2908. tempReg2 := IntermediateCode.Register(componentType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(componentType,IntermediateCode.GeneralPurposeRegister));
  2909. Emit(Mul(position,tempReg2, left.tag, right.tag));
  2910. Emit(Add(position,result.op, result.op, tempReg2));
  2911. Emit(Div(position,result.op, result.op, tempReg));
  2912. Reuse2(result.tag, left.tag, right.op);
  2913. Emit(Mul(position,result.tag, left.tag, right.op));
  2914. Emit(Mul(position,tempReg2, left.op, right.tag));
  2915. Emit(Sub(position,result.tag, result.tag, tempReg2));
  2916. Emit(Div(position,result.tag, result.tag, tempReg));
  2917. ReleaseIntermediateOperand(tempReg);
  2918. ReleaseIntermediateOperand(tempReg2)
  2919. ELSE
  2920. InitOperand(result,ModeValue);
  2921. Reuse2a(result.op,left.op,right.op,dest);
  2922. Emit(Div(position,result.op,left.op,right.op));
  2923. END;
  2924. ReleaseOperand(left); ReleaseOperand(right);
  2925. |Scanner.Equal:
  2926. IF ~conditional THEN ConditionToValue(x);
  2927. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2928. CompareString(BreqL,x.left,x.right);
  2929. ELSE
  2930. Evaluate(x.left,left);
  2931. Evaluate(x.right,right);
  2932. IF leftType IS SyntaxTree.RangeType THEN
  2933. ASSERT(rightType IS SyntaxTree.RangeType);
  2934. BrneL(falseLabel, left.op, right.op); (* first *)
  2935. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2936. BrneL(falseLabel, left.extra, right.extra); (* step *)
  2937. ReleaseOperand(left); ReleaseOperand(right);
  2938. BrL(trueLabel)
  2939. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  2940. BrneL(falseLabel, left.op, right.op); (* first *)
  2941. BrneL(falseLabel, left.tag, right.tag); (* last *)
  2942. ReleaseOperand(left); ReleaseOperand(right);
  2943. BrL(trueLabel)
  2944. ELSIF leftType IS SyntaxTree.ComplexType THEN
  2945. (* TODO: review this *)
  2946. BrneL(falseLabel, left.op, right.op); (* real part *)
  2947. BrneL(falseLabel, left.tag, right.tag); (* imaginary part *)
  2948. ReleaseOperand(left); ReleaseOperand(right);
  2949. BrL(trueLabel)
  2950. ELSE
  2951. BrneL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2952. ReleaseOperand(left); ReleaseOperand(right);
  2953. BrL(trueLabel);
  2954. END;
  2955. END;
  2956. |Scanner.LessEqual:
  2957. IF ~conditional THEN ConditionToValue(x);
  2958. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2959. CompareString(BrgeL,x.right,x.left);
  2960. ELSE
  2961. Evaluate(x.left,left);
  2962. Evaluate(x.right,right);
  2963. IF leftType IS SyntaxTree.SetType THEN (* left subsetequal right: left \cap right = left *)
  2964. Reuse1(temp.op,right.op);
  2965. Emit(And(position,temp.op,left.op,right.op));
  2966. ReleaseOperand(right);
  2967. BreqL(trueLabel,temp.op,left.op);
  2968. BrL(falseLabel);
  2969. ReleaseOperand(temp);ReleaseOperand(left);
  2970. ELSE
  2971. BrltL(falseLabel,right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  2972. ReleaseOperand(left); ReleaseOperand(right);
  2973. BrL(trueLabel);
  2974. END;
  2975. END;
  2976. |Scanner.Less:
  2977. IF leftType IS SyntaxTree.SetType THEN (* left < right <=> left <= right & left # right *)
  2978. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.LessEqual);
  2979. leftExpression.SetType(system.booleanType);
  2980. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  2981. rightExpression.SetType(system.booleanType);
  2982. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  2983. leftExpression.SetType(system.booleanType);
  2984. Expression(leftExpression);
  2985. ELSIF ~conditional THEN ConditionToValue(x);
  2986. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  2987. CompareString(BrltL,x.left,x.right);
  2988. ELSE
  2989. Evaluate(x.left,left);
  2990. Evaluate(x.right,right);
  2991. BrgeL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  2992. ReleaseOperand(left); ReleaseOperand(right);
  2993. BrL(trueLabel);
  2994. END;
  2995. |Scanner.Greater:
  2996. IF leftType IS SyntaxTree.SetType THEN (* left > right <=> left >= right & left # right *)
  2997. leftExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.GreaterEqual);
  2998. leftExpression.SetType(system.booleanType);
  2999. rightExpression := SyntaxTree.NewBinaryExpression(-1,x.left,x.right,Scanner.Unequal);
  3000. rightExpression.SetType(system.booleanType);
  3001. leftExpression := SyntaxTree.NewBinaryExpression(-1,leftExpression,rightExpression,Scanner.And);
  3002. leftExpression.SetType(system.booleanType);
  3003. Expression(leftExpression);
  3004. ELSIF ~conditional THEN ConditionToValue(x);
  3005. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3006. CompareString(BrltL,x.right,x.left);
  3007. ELSE
  3008. Evaluate(x.left,left);
  3009. Evaluate(x.right,right);
  3010. BrgeL(falseLabel, right.op,left.op); (* inverse evaluation to optimize jumps for true case *)
  3011. ReleaseOperand(left); ReleaseOperand(right);
  3012. BrL(trueLabel);
  3013. END;
  3014. |Scanner.GreaterEqual:
  3015. IF ~conditional THEN ConditionToValue(x);
  3016. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3017. CompareString(BrgeL,x.left,x.right);
  3018. ELSE
  3019. Evaluate(x.left,left);
  3020. Evaluate(x.right,right);
  3021. IF leftType IS SyntaxTree.SetType THEN (* left supsetequal right: left \cap right = right *)
  3022. Reuse1(temp.op,left.op);
  3023. Emit(And(position,temp.op,left.op,right.op));
  3024. ReleaseOperand(left);
  3025. BreqL(trueLabel, temp.op,right.op);
  3026. ReleaseOperand(temp); ReleaseOperand(right);
  3027. BrL(falseLabel);
  3028. ELSE
  3029. BrltL(falseLabel, left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3030. ReleaseOperand(left); ReleaseOperand(right);
  3031. BrL(trueLabel);
  3032. END;
  3033. END;
  3034. |Scanner.Unequal:
  3035. IF ~conditional THEN ConditionToValue(x);
  3036. ELSIF (leftType IS SyntaxTree.ArrayType) OR (leftType IS SyntaxTree.StringType) THEN (* string comparison *)
  3037. CompareString(BrneL,x.left,x.right);
  3038. ELSE
  3039. Evaluate(x.left,left);
  3040. Evaluate(x.right,right);
  3041. IF leftType IS SyntaxTree.RangeType THEN
  3042. ASSERT(rightType IS SyntaxTree.RangeType);
  3043. BrneL(trueLabel, left.op, right.op); (* first *)
  3044. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3045. BrneL(trueLabel, left.extra, right.extra); (* step *)
  3046. ReleaseOperand(left); ReleaseOperand(right);
  3047. BrL(falseLabel)
  3048. ELSIF IsDelegate(leftType) THEN (* delegate comparison *)
  3049. BrneL(trueLabel, left.op, right.op); (* first *)
  3050. BrneL(trueLabel, left.tag, right.tag); (* last *)
  3051. ReleaseOperand(left); ReleaseOperand(right);
  3052. BrL(falseLabel)
  3053. ELSIF leftType IS SyntaxTree.ComplexType THEN
  3054. (* TODO: review this *)
  3055. BrneL(trueLabel, left.op, right.op); (* real part *)
  3056. BrneL(trueLabel, left.tag, right.tag); (* imaginary part *)
  3057. ReleaseOperand(left); ReleaseOperand(right);
  3058. BrL(falseLabel)
  3059. ELSE
  3060. BreqL(falseLabel,left.op,right.op); (* inverse evaluation to optimize jumps for true case *)
  3061. ReleaseOperand(left); ReleaseOperand(right);
  3062. BrL(trueLabel);
  3063. END;
  3064. END;
  3065. |Scanner.In:
  3066. ASSERT(rightType.resolved IS SyntaxTree.SetType);
  3067. Evaluate(x.left,left);
  3068. Evaluate(x.right,right);
  3069. Convert(left.op,setType);
  3070. ReuseCopy(temp.op,right.op);
  3071. Emit(Shr(position,temp.op,temp.op,left.op));
  3072. ReleaseOperand(right); ReleaseOperand(left);
  3073. IntermediateCode.InitImmediate(one,setType,1);
  3074. Emit(And(position,temp.op,temp.op,one));
  3075. IF conditional THEN
  3076. IntermediateCode.InitImmediate(zero,setType,0);
  3077. BrneL(trueLabel,temp.op,zero);
  3078. ReleaseOperand(temp);
  3079. BrL(falseLabel);
  3080. ELSE
  3081. Convert(temp.op,bool);
  3082. result.mode := ModeValue;
  3083. result.op := temp.op;
  3084. result.tag := nil; (* may be left over from calls to evaluate *)
  3085. END;
  3086. ELSE
  3087. IF (x.operator = Scanner.Questionmarks) OR (x.operator = Scanner.LessLessQ) & (x.right.type.resolved IS SyntaxTree.PortType) THEN
  3088. IF x.operator = Scanner.Questionmarks THEN
  3089. leftExpression := x.left;
  3090. rightExpression := x.right;
  3091. ELSE
  3092. leftExpression := x.right;
  3093. rightExpression := x.left;
  3094. END;
  3095. Evaluate(leftExpression, left);
  3096. Emit(Push(position,left.op));
  3097. ReleaseOperand(left);
  3098. Designate(rightExpression, right);
  3099. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3100. IF ~backend.cellsAreObjects THEN
  3101. IF size # 1 THEN Error(x.right.position,"receive not implemented for complex data types") END;
  3102. END;
  3103. Emit(Push(position,right.op));
  3104. ReleaseOperand(right);
  3105. IF backend.cellsAreObjects THEN
  3106. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlocking",2);
  3107. ELSE
  3108. CallThis(position,ChannelModuleName,"ReceiveNonBlocking",2);
  3109. END;
  3110. InitOperand(result, ModeValue);
  3111. result.op := NewRegisterOperand(bool);
  3112. Emit(Result(position,result.op));
  3113. IF conditional THEN
  3114. IntermediateCode.InitImmediate(zero,setType,0);
  3115. BrneL(trueLabel,result.op,zero);
  3116. ReleaseOperand(result);
  3117. BrL(falseLabel);
  3118. END;
  3119. ELSIF (x.operator = Scanner.ExclamationMarks) OR (x.operator = Scanner.LessLessQ) & (x.left.type.resolved IS SyntaxTree.PortType) THEN
  3120. leftExpression := x.left;
  3121. rightExpression := x.right;
  3122. Evaluate(leftExpression, left);
  3123. Emit(Push(position,left.op));
  3124. ReleaseOperand(left);
  3125. Evaluate(rightExpression, right);
  3126. size := ToMemoryUnits(system,system.SizeOf(x.right.type));
  3127. IF ~backend.cellsAreObjects THEN
  3128. IF size # 1 THEN Error(x.right.position,"send not implemented for complex data types") END;
  3129. END;
  3130. Emit(Push(position,right.op));
  3131. ReleaseOperand(right);
  3132. IF backend.cellsAreObjects THEN
  3133. CallThis(position,"ActiveCellsRuntime","SendNonBlocking",2);
  3134. ELSE
  3135. CallThis(position,ChannelModuleName,"SendNonBlocking",2);
  3136. END;
  3137. InitOperand(result, ModeValue);
  3138. result.op := NewRegisterOperand(bool);
  3139. Emit(Result(position,result.op));
  3140. IF conditional THEN
  3141. IntermediateCode.InitImmediate(zero,setType,0);
  3142. BrneL(trueLabel,result.op,zero);
  3143. ReleaseOperand(result);
  3144. BrL(falseLabel);
  3145. END;
  3146. ELSE
  3147. HALT(100);
  3148. END;
  3149. END;
  3150. destination := dest;
  3151. IF Trace THEN TraceExit("VisitBinaryExpression") END;
  3152. END VisitBinaryExpression;
  3153. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3154. VAR localResult, operand: Operand;
  3155. BEGIN
  3156. IF Trace THEN TraceEnter("VisitRangeExpression") END;
  3157. InitOperand(localResult, ModeValue);
  3158. ASSERT(x.first # NIL);
  3159. Evaluate(x.first, operand);
  3160. localResult.op := operand.op;
  3161. ReleaseOperand(operand);
  3162. UseIntermediateOperand(localResult.op);
  3163. ASSERT(x.last # NIL);
  3164. Evaluate(x.last, operand);
  3165. localResult.tag := operand.op;
  3166. ReleaseOperand(operand);
  3167. UseIntermediateOperand(localResult.tag);
  3168. IF x.step # NIL THEN
  3169. Evaluate(x.step, operand);
  3170. localResult.extra := operand.op;
  3171. ReleaseOperand(operand);
  3172. UseIntermediateOperand(localResult.extra);
  3173. END;
  3174. result := localResult;
  3175. IF Trace THEN TraceExit("VisitRangeExpression") END
  3176. END VisitRangeExpression;
  3177. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3178. BEGIN
  3179. HALT(100); (* should never be evaluated *)
  3180. END VisitTensorRangeExpression;
  3181. PROCEDURE VisitConversion(x: SyntaxTree.Conversion);
  3182. VAR old: Operand; dest: IntermediateCode.Operand; componentType: SyntaxTree.Type;
  3183. BEGIN
  3184. IF Trace THEN TraceEnter("VisitConversion") END;
  3185. ASSERT(~(x.expression.type.resolved IS SyntaxTree.RangeType));
  3186. dest := destination; destination := emptyOperand;
  3187. Evaluate(x.expression,old);
  3188. InitOperand(result,ModeValue);
  3189. result.op := old.op;
  3190. ASSERT(result.op.mode # 0);
  3191. IF x.type.resolved IS SyntaxTree.ComplexType THEN
  3192. (* convert TO a complex number *)
  3193. componentType := x.type.resolved(SyntaxTree.ComplexType).componentType;
  3194. Convert(result.op,IntermediateCode.GetType(system, componentType));
  3195. ASSERT(result.op.mode # 0);
  3196. IF x.expression.type.resolved IS SyntaxTree.ComplexType THEN
  3197. (* convert FROM a complex number TO a complex number*)
  3198. result.tag := old.tag;
  3199. ASSERT(result.tag.mode # 0);
  3200. Convert(result.tag,IntermediateCode.GetType(system, componentType));
  3201. ASSERT(result.tag.mode # 0)
  3202. ELSE
  3203. ASSERT(componentType IS SyntaxTree.FloatType); (* this excludes complex types based on integer types *)
  3204. result.tag := IntermediateCode.FloatImmediate(IntermediateCode.GetType(system, componentType), 0); (* the imaginary part is set to 0 *)
  3205. END
  3206. ELSE
  3207. Convert(result.op,IntermediateCode.GetType(system,x.type));
  3208. ASSERT(result.op.mode # 0);
  3209. result.tag := old.tag; (*! probably never used *)
  3210. END;
  3211. destination := dest;
  3212. IF Trace THEN TraceExit("VisitConversion") END;
  3213. END VisitConversion;
  3214. PROCEDURE VisitTypeDeclaration(x: SyntaxTree.TypeDeclaration);
  3215. BEGIN
  3216. IF Trace THEN TraceEnter("VisitTypeDeclaration") END;
  3217. ASSERT((x.declaredType.resolved IS SyntaxTree.EnumerationType) OR (x.declaredType.resolved IS SyntaxTree.RecordType)
  3218. OR (x.declaredType.resolved IS SyntaxTree.PointerType) & (x.declaredType.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType));
  3219. IF Trace THEN TraceExit("VisitTypeDeclaration") END;
  3220. END VisitTypeDeclaration;
  3221. (** designators (expressions) *)
  3222. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3223. VAR ownerType, designatorType: SyntaxTree.RecordType;
  3224. BEGIN
  3225. IF Trace THEN TraceEnter("VisitSymbolDesignator") END;
  3226. IF x.left # NIL THEN Expression(x.left) END;
  3227. Symbol(x.symbol,result);
  3228. IF backend.cooperative & (x.symbol IS SyntaxTree.Variable) & (x.symbol.scope IS SyntaxTree.RecordScope) THEN
  3229. ASSERT ((x.left # NIL) & (x.left.type.resolved IS SyntaxTree.RecordType));
  3230. ownerType := x.symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  3231. designatorType := x.left.type.resolved(SyntaxTree.RecordType);
  3232. IF ~ownerType.isObject & designatorType.isObject & ~designatorType.pointerType.isPlain THEN
  3233. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  3234. END;
  3235. END;
  3236. IF Trace THEN TraceExit("VisitSymbolDesignator") END;
  3237. END VisitSymbolDesignator;
  3238. PROCEDURE BoundCheck(index,length: IntermediateCode.Operand);
  3239. BEGIN
  3240. IF isUnchecked THEN RETURN END;
  3241. IF tagsAvailable THEN
  3242. TrapC(BrltL,index,length,IndexCheckTrap);
  3243. END;
  3244. END BoundCheck;
  3245. PROCEDURE DimensionCheck(base,dim: IntermediateCode.Operand; op: ConditionalBranch );
  3246. VAR d: IntermediateCode.Operand;
  3247. BEGIN
  3248. IF isUnchecked THEN RETURN END;
  3249. MakeMemory(d,base,dim.type,ToMemoryUnits(system,MathDimOffset * addressType.sizeInBits));
  3250. TrapC(op,dim,d,ArraySizeTrap);
  3251. ReleaseIntermediateOperand(d);
  3252. END DimensionCheck;
  3253. PROCEDURE MathIndexDesignator(x: SyntaxTree.IndexDesignator);
  3254. VAR
  3255. index, range, array, sourceLength, sourceIncrement, localResult: Operand;
  3256. firstIndex, lastIndex, stepSize, summand, targetLength, targetIncrement, tmp, srcDim, destDim: IntermediateCode.Operand;
  3257. expression: SyntaxTree.Expression;
  3258. resultingType, leftType, baseType: SyntaxTree.Type;
  3259. skipLabel1: Label;
  3260. i, indexListSize, indexDim, rangeCount, indexCount, tensorRangeCount, srcDimOffset, destDimOffset, targetArrayDimensionality: LONGINT;
  3261. staticSourceLength, staticSourceIncrement, staticIndex, staticFirstIndex, staticLastIndex, staticStepSize, staticTargetLength: LONGINT;
  3262. variableOp: Operand;
  3263. variable: SyntaxTree.Variable;
  3264. PROCEDURE CountIndices(parameters: SyntaxTree.ExpressionList; VAR indexCount: LONGINT; VAR rangeCount: LONGINT; VAR tensorRangeCount: LONGINT);
  3265. VAR expression: SyntaxTree.Expression;
  3266. BEGIN
  3267. (* count the number of indices, ranges and tensorRanges in the index list *)
  3268. indexCount := 0; rangeCount := 0; tensorRangeCount := 0;
  3269. FOR i := 0 TO parameters.Length() - 1 DO
  3270. expression := parameters.GetExpression(i);
  3271. IF expression IS SyntaxTree.TensorRangeExpression THEN INC(tensorRangeCount)
  3272. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.RangeType) THEN INC(indexCount)
  3273. ELSE INC(indexCount)
  3274. END
  3275. END;
  3276. END CountIndices;
  3277. BEGIN
  3278. ASSERT(tagsAvailable);
  3279. resultingType := x.type.resolved; (* resulting type *)
  3280. leftType := x.left.type.resolved; (* type of array to be indexed over *)
  3281. InitOperand(localResult, ModeReference);
  3282. IF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3283. targetArrayDimensionality := resultingType(SyntaxTree.MathArrayType).Dimensionality();
  3284. IF arrayDestinationTag.mode # IntermediateCode.Undefined THEN
  3285. (* a globally defined array destination tag is available -> use and invalidate it*)
  3286. localResult.tag := arrayDestinationTag;
  3287. IntermediateCode.InitOperand(arrayDestinationTag)
  3288. ELSE
  3289. (* otherwise, create a temporary variable and use it to store the array destination tag *)
  3290. variable := GetTemporaryVariable(GetMathArrayDescriptorType(targetArrayDimensionality), FALSE);
  3291. Symbol(variable, variableOp);
  3292. ReuseCopy(localResult.tag, variableOp.op);
  3293. ReleaseOperand(variableOp);
  3294. END
  3295. END;
  3296. indexListSize := x.parameters.Length();
  3297. CountIndices(x.parameters, indexCount, rangeCount, tensorRangeCount);
  3298. ASSERT(tensorRangeCount <= 1);
  3299. (* designate the array to be indexed over, perform tensor range check if known *)
  3300. Designate(x.left, array);
  3301. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3302. Dereference(array, leftType,FALSE);
  3303. IF tensorRangeCount=0 THEN
  3304. DimensionCheck(array.tag, IntermediateCode.Immediate(int32, rangeCount+indexCount), BreqL)
  3305. END
  3306. END;
  3307. (* determine source and destination dimension offsets; this depends on if the list starts with a '?' *)
  3308. IF x.parameters.GetExpression(0) IS SyntaxTree.TensorRangeExpression THEN
  3309. srcDimOffset := -indexListSize;
  3310. destDimOffset := -rangeCount
  3311. ELSE
  3312. srcDimOffset := 0;
  3313. destDimOffset := 0
  3314. END;
  3315. indexDim := 0;
  3316. (* use address of source array as basis *)
  3317. (*
  3318. ReuseCopy(localResult.op, array.op);
  3319. *)
  3320. localResult.op := array.op;
  3321. UseIntermediateOperand(localResult.op);
  3322. (* go through the index list *)
  3323. FOR i := 0 TO indexListSize - 1 DO
  3324. expression := x.parameters.GetExpression(i);
  3325. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3326. (* nothing to do *)
  3327. ELSE
  3328. (* determine which dimension of source array is currently looked at *)
  3329. IF srcDimOffset < 0 THEN (* tensor expression or the form a[?,i,j] *)
  3330. (* get the memory operand pointing to array descriptor dimension *)
  3331. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3332. (* make a reusable register from it *)
  3333. ReuseCopy(srcDim, tmp);
  3334. ReleaseIntermediateOperand(tmp);
  3335. AddInt(srcDim, srcDim, IntermediateCode.Immediate(addressType, i + srcDimOffset));
  3336. ELSE
  3337. srcDim := IntermediateCode.Immediate(int32, i)
  3338. END;
  3339. (* get length and increment of source array for current dimension *)
  3340. GetMathArrayLength(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceLength);
  3341. Convert(sourceLength.op, sizeType);
  3342. GetMathArrayIncrement(leftType(SyntaxTree.MathArrayType), array, srcDim, FALSE, sourceIncrement);
  3343. Convert(sourceIncrement.op, sizeType);
  3344. (* release the dim operand, if dynamic. No register reuse to decrease register pressure *)
  3345. ReleaseIntermediateOperand(srcDim);
  3346. IF SemanticChecker.IsIntegerType(expression.type.resolved) THEN
  3347. (* SINGLE INDEX *)
  3348. Evaluate(expression, index);
  3349. ReleaseIntermediateOperand(index.tag);
  3350. index.tag := emptyOperand;
  3351. Convert(index.op, sizeType);
  3352. (* lower bound check *)
  3353. IF IsIntegerImmediate(index.op, staticIndex) THEN
  3354. ASSERT(staticIndex >= 0) (* ensured by the checker *)
  3355. ELSIF isUnchecked THEN (* do nothing *)
  3356. ELSE
  3357. TrapC(BrgeL, index.op, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3358. END;
  3359. (* upper bound check *)
  3360. IF IsIntegerImmediate(index.op, staticIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3361. ASSERT(staticIndex < staticSourceLength) (* ensured by checker *)
  3362. ELSIF isUnchecked THEN (* do nothing *)
  3363. ELSE
  3364. TrapC(BrltL, index.op, sourceLength.op, IndexCheckTrap)
  3365. END;
  3366. ReleaseOperand(sourceLength);
  3367. Convert(index.op, addressType);
  3368. summand := index.op;
  3369. ELSIF expression.type.resolved IS SyntaxTree.RangeType THEN
  3370. (* RANGE OF INDICES *)
  3371. Evaluate(expression, range);
  3372. firstIndex := range.op; UseIntermediateOperand(firstIndex);
  3373. lastIndex := range.tag; UseIntermediateOperand(lastIndex);
  3374. stepSize := range.extra; UseIntermediateOperand(stepSize);
  3375. ReleaseOperand(range);
  3376. Convert(firstIndex, sizeType);
  3377. Convert(lastIndex, sizeType);
  3378. Convert(stepSize, sizeType);
  3379. (* for dynamic upper bounds: add a runtime check, which repaces the upper bound with the largest valid index
  3380. if it is 'MAX(LONGINT)' *)
  3381. IF ~IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3382. TransferToRegister(lastIndex, lastIndex);
  3383. skipLabel1 := NewLabel();
  3384. BrneL(skipLabel1, lastIndex, IntermediateCode.Immediate(sizeType, MAX(LONGINT)));
  3385. Emit(MovReplace(position,lastIndex, sourceLength.op)); (* make sure that no new register is allocated *)
  3386. Emit(Sub(position,lastIndex, lastIndex, IntermediateCode.Immediate(sizeType, 1)));
  3387. SetLabel(skipLabel1)
  3388. END;
  3389. (* check if step size is valid *)
  3390. IF IsIntegerImmediate(stepSize, staticStepSize) THEN
  3391. ASSERT(staticStepSize >= 1) (* ensured by the checker *)
  3392. ELSIF isUnchecked THEN (* do nothing *)
  3393. ELSE
  3394. TrapC(BrgeL, stepSize, IntermediateCode.Immediate(sizeType, 1), IndexCheckTrap)
  3395. END;
  3396. (* check lower bound check *)
  3397. IF IsIntegerImmediate(firstIndex, staticFirstIndex) THEN
  3398. ASSERT(staticFirstIndex >= 0) (* ensured by the checker *)
  3399. ELSIF isUnchecked THEN (* do nothing *)
  3400. ELSE
  3401. TrapC(BrgeL, firstIndex, IntermediateCode.Immediate(sizeType, 0), IndexCheckTrap)
  3402. END;
  3403. (* check upper bound check *)
  3404. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3405. (* statically open range: nothing to do *)
  3406. ELSIF IsIntegerImmediate(lastIndex, staticLastIndex) & IsIntegerImmediate(sourceLength.op, staticSourceLength) THEN
  3407. ASSERT(staticLastIndex < staticSourceLength)
  3408. ELSIF isUnchecked THEN (* do nothing *)
  3409. ELSE
  3410. TrapC(BrltL, lastIndex, sourceLength.op, IndexCheckTrap)
  3411. END;
  3412. (* determine length of target array for current dimension *)
  3413. (* 1. incorporate last index: *)
  3414. IF IsIntegerImmediate(lastIndex, staticLastIndex) THEN
  3415. (* last index is static *)
  3416. IF IsIntegerImmediate(lastIndex, staticLastIndex) & (staticLastIndex = MAX(LONGINT)) THEN
  3417. targetLength := sourceLength.op
  3418. ELSE
  3419. targetLength := IntermediateCode.Immediate(sizeType, staticLastIndex + 1)
  3420. END;
  3421. UseIntermediateOperand(targetLength);
  3422. ELSE
  3423. (* targetLength := lastIndex + 1
  3424. Reuse1(targetLength, lastIndex);
  3425. *)
  3426. AddInt(targetLength, lastIndex, IntermediateCode.Immediate(sizeType, 1));
  3427. END;
  3428. ReleaseOperand(sourceLength);
  3429. ReleaseIntermediateOperand(lastIndex);
  3430. (* 2. incorporate first index: *)
  3431. IF IsIntegerImmediate(firstIndex, staticFirstIndex) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3432. (* first index and current target length are static *)
  3433. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength - staticFirstIndex)
  3434. ELSIF IsIntegerImmediate(firstIndex, staticFirstIndex) & (staticFirstIndex = 0) THEN
  3435. (* first index = 0: nothing to do *)
  3436. ELSE
  3437. (* targetLength := targetLength - firstIndex *)
  3438. TransferToRegister(targetLength, targetLength);
  3439. Emit(Sub(position,targetLength, targetLength, firstIndex))
  3440. END;
  3441. (* clip negative lengths to 0 *)
  3442. IF IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3443. IF staticTargetLength < 0 THEN
  3444. targetLength := IntermediateCode.Immediate(sizeType, 0)
  3445. END
  3446. ELSE
  3447. skipLabel1 := NewLabel();
  3448. BrgeL(skipLabel1, targetLength, IntermediateCode.Immediate(sizeType, 0));
  3449. TransferToRegister(targetLength, targetLength);
  3450. Emit(Mov(position,targetLength, IntermediateCode.Immediate(sizeType, 0)));
  3451. SetLabel(skipLabel1)
  3452. END;
  3453. (* 3. incorporate index step size: *)
  3454. IF IsIntegerImmediate(stepSize, staticStepSize) & IsIntegerImmediate(targetLength, staticTargetLength) THEN
  3455. (*step size and current target length are static *)
  3456. staticTargetLength := (staticTargetLength-1) DIV staticStepSize + 1;
  3457. targetLength := IntermediateCode.Immediate(sizeType, staticTargetLength)
  3458. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3459. (* step size = 1: nothing to do *)
  3460. ELSE
  3461. (* emit code for this:
  3462. targetLength := (targetLength-1) DIV stepSize +1;
  3463. *)
  3464. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, -1));
  3465. DivInt(targetLength, targetLength, stepSize);
  3466. AddInt(targetLength, targetLength, IntermediateCode.Immediate(sizeType, 1));
  3467. END;
  3468. (* determine increment of target array for current dimension *)
  3469. IF IsIntegerImmediate(sourceIncrement.op, staticSourceIncrement) & IsIntegerImmediate(stepSize, staticStepSize) THEN
  3470. targetIncrement := IntermediateCode.Immediate(sizeType, staticSourceIncrement * staticStepSize);
  3471. ELSIF IsIntegerImmediate(stepSize, staticStepSize) & (staticStepSize = 1) THEN
  3472. (* step size = 1 *)
  3473. targetIncrement := sourceIncrement.op;
  3474. UseIntermediateOperand(targetIncrement)
  3475. ELSE
  3476. (* targetIncrement := sourceIncrement * stepSize *)
  3477. Reuse1(targetIncrement, stepSize);
  3478. ASSERT((sourceIncrement.op.mode # IntermediateCode.ModeImmediate) OR (stepSize.mode # IntermediateCode.ModeImmediate));
  3479. MulInt(targetIncrement, sourceIncrement.op, stepSize);
  3480. END;
  3481. ReleaseIntermediateOperand(stepSize);
  3482. (* write length and increment of target array to descriptor *)
  3483. IF destDimOffset < 0 THEN
  3484. (* determine which dimension of target array is currently looked at *)
  3485. GetMathArrayField(tmp, array.tag, MathDimOffset);
  3486. TransferToRegister(destDim, tmp);
  3487. AddInt(destDim, destDim, IntermediateCode.Immediate(sizeType, indexDim + destDimOffset));
  3488. PutMathArrayLenOrIncr(localResult.tag, targetLength, destDim, FALSE);
  3489. PutMathArrayLenOrIncr(localResult.tag, targetIncrement, destDim, TRUE);
  3490. ReleaseIntermediateOperand(destDim)
  3491. ELSE
  3492. PutMathArrayLength(localResult.tag, targetLength, indexDim);
  3493. PutMathArrayIncrement(localResult.tag , targetIncrement, indexDim)
  3494. END;
  3495. ReleaseIntermediateOperand(targetLength); targetLength := nil;
  3496. ReleaseIntermediateOperand(targetIncrement); targetIncrement := nil;
  3497. INC(indexDim);
  3498. Convert(firstIndex, addressType);
  3499. TransferToRegister(summand, firstIndex);
  3500. ELSE HALT(100);
  3501. END;
  3502. (*
  3503. ASSERT((summand.mode # IntermediateCode.ModeImmediate) OR (sourceIncrement.op.mode # IntermediateCode.ModeImmediate));
  3504. *)
  3505. Convert(sourceIncrement.op, addressType);
  3506. Convert(summand, addressType);
  3507. MulInt(summand, summand, sourceIncrement.op);
  3508. ReleaseIntermediateOperand(sourceIncrement.op);
  3509. AddInt(localResult.op, localResult.op, summand);
  3510. ReleaseIntermediateOperand(summand);
  3511. END
  3512. END;
  3513. result := localResult;
  3514. IF (resultingType IS SyntaxTree.RecordType) & (resultingType(SyntaxTree.RecordType).pointerType = NIL) THEN
  3515. ReleaseIntermediateOperand(result.tag);
  3516. result.tag := TypeDescriptorAdr(resultingType);
  3517. IF ~newObjectFile THEN
  3518. IntermediateCode.MakeMemory(result.tag,addressType);
  3519. END;
  3520. ELSIF IsDelegate(resultingType) THEN
  3521. ReleaseIntermediateOperand(result.tag);
  3522. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3523. UseIntermediateOperand(result.tag);
  3524. ELSIF (resultingType IS SyntaxTree.ArrayType) & (resultingType(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3525. ReleaseIntermediateOperand(result.tag);
  3526. IntermediateCode.InitImmediate(result.tag,addressType,resultingType(SyntaxTree.ArrayType).staticLength);
  3527. ELSIF (resultingType IS SyntaxTree.ArrayType) THEN
  3528. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+indexListSize-1;
  3529. ELSIF (resultingType IS SyntaxTree.MathArrayType) & ( (resultingType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) OR NeedDescriptor) THEN
  3530. (* finalize target array descriptor *)
  3531. ASSERT(result.tag.mode # IntermediateCode.Undefined); (* tag has been already set in the beginning *)
  3532. (* write lengths and increments of target array for remaining dimensions *)
  3533. i := indexListSize;
  3534. WHILE indexDim < targetArrayDimensionality DO
  3535. GetMathArrayLengthAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE, sourceLength);
  3536. PutMathArrayLength(result.tag, sourceLength.op,indexDim);
  3537. ReleaseOperand(sourceLength);
  3538. GetMathArrayIncrementAt(leftType(SyntaxTree.MathArrayType),array,i,FALSE,sourceIncrement);
  3539. PutMathArrayIncrement(result.tag, sourceIncrement.op,indexDim);
  3540. ReleaseOperand(sourceIncrement);
  3541. INC(i); INC(indexDim);
  3542. END;
  3543. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3544. tmp := nil;
  3545. ELSE
  3546. GetMathArrayField(tmp,array.tag,MathPtrOffset);
  3547. END;
  3548. PutMathArrayField(result.tag, tmp, MathPtrOffset);
  3549. ReleaseIntermediateOperand(tmp);
  3550. IF leftType(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3551. baseType := SemanticChecker.ArrayBase(resultingType, indexDim);
  3552. tmp := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3553. ELSE
  3554. GetMathArrayField(tmp,array.tag, MathElementSizeOffset);
  3555. END;
  3556. PutMathArrayField(result.tag, tmp, MathElementSizeOffset);
  3557. ReleaseIntermediateOperand(tmp);
  3558. PutMathArrayField(result.tag, result.op, MathAdrOffset);
  3559. (* write dimensionality *)
  3560. IF targetArrayDimensionality # 0 THEN
  3561. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType, targetArrayDimensionality),MathDimOffset);
  3562. END;
  3563. PutMathArrayField(result.tag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{RangeFlag})),MathFlagsOffset);
  3564. END;
  3565. ReleaseOperand(array);
  3566. IF conditional & (resultingType.resolved IS SyntaxTree.BooleanType) THEN
  3567. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3568. END;
  3569. END MathIndexDesignator;
  3570. (* TENTATIVE *)
  3571. PROCEDURE DumpOperand(operand: Operand);
  3572. BEGIN
  3573. D.Log.String(" op = ");
  3574. IntermediateCode.DumpOperand(D.Log, operand.op );
  3575. D.Log.Ln;
  3576. D.Log.String(" tag = ");
  3577. IntermediateCode.DumpOperand(D.Log, operand.tag );
  3578. D.Log.Ln;
  3579. D.Log.String(" extra = ");
  3580. IntermediateCode.DumpOperand(D.Log, operand.extra );
  3581. D.Log.Ln;
  3582. D.Log.Update
  3583. END DumpOperand;
  3584. (* get the length of an array , trying to make use of static information *)
  3585. PROCEDURE ArrayLength(type: SyntaxTree.Type; dim: LONGINT; tag: IntermediateCode.Operand): IntermediateCode.Operand;
  3586. VAR res: IntermediateCode.Operand; size: LONGINT;
  3587. BEGIN
  3588. type := type.resolved;
  3589. IF type IS SyntaxTree.ArrayType THEN
  3590. WITH type: SyntaxTree.ArrayType DO
  3591. IF type.form = SyntaxTree.Static THEN
  3592. RETURN IntermediateCode.Immediate(addressType,type.staticLength);
  3593. (*ELSIF (type.form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3594. Evaluate(type.length, op);
  3595. ReleaseIntermediateOperand(op.tag);
  3596. RETURN op.op;*)
  3597. ELSE
  3598. res := tag;
  3599. IntermediateCode.AddOffset(res,ToMemoryUnits(system,addressType.sizeInBits*(DynamicDim(type)-1)));
  3600. IntermediateCode.MakeMemory(res,addressType);
  3601. UseIntermediateOperand(res);
  3602. RETURN res
  3603. END
  3604. END;
  3605. ELSE
  3606. size := ToMemoryUnits(system,system.AlignedSizeOf(type));
  3607. RETURN IntermediateCode.Immediate(addressType,size);
  3608. END;
  3609. END ArrayLength;
  3610. PROCEDURE AddInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3611. BEGIN
  3612. ReleaseIntermediateOperand(res);
  3613. IF IsImmediate(x) & IsImmediate(y) THEN
  3614. IntermediateCode.InitImmediate(res,x.type,x.intValue+y.intValue);
  3615. ELSIF IsAddress(x) & IsImmediate(y) THEN
  3616. IntermediateCode.InitAddress(res,x.type,x.symbol.name, x.symbol.fingerprint, x.symbolOffset);
  3617. IntermediateCode.AddOffset(res, LONGINT(y.intValue)+x.offset);
  3618. ELSIF IsAddress(y) & IsImmediate(x) THEN
  3619. IntermediateCode.InitAddress(res,y.type,y.symbol.name, y.symbol.fingerprint, y.symbolOffset);
  3620. IntermediateCode.AddOffset(res, LONGINT(x.intValue)+y.offset);
  3621. ELSIF IsRegister(x) & IsImmediate(y) THEN
  3622. IntermediateCode.InitRegister(res, x.type, x.registerClass, x.register);
  3623. IntermediateCode.AddOffset(res, x.offset + LONGINT(y.intValue));
  3624. UseIntermediateOperand(res);
  3625. ELSIF IsRegister(y) & IsImmediate(x) THEN
  3626. IntermediateCode.InitRegister(res, y.type, y.registerClass, y.register);
  3627. IntermediateCode.AddOffset(res, y.offset + LONGINT(x.intValue));
  3628. UseIntermediateOperand(res);
  3629. ELSE
  3630. IF ~ReusableRegister(res) THEN
  3631. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3632. ELSE
  3633. UseIntermediateOperand(res);
  3634. END;
  3635. IF IsImmediate(x) & (x.intValue = 0) THEN
  3636. Emit(Mov(position,res,y))
  3637. ELSIF IsImmediate(y) & (y.intValue=0) THEN
  3638. Emit(Mov(position,res,x))
  3639. ELSE
  3640. Emit(Add(position,res, x, y));
  3641. END;
  3642. END;
  3643. END AddInt;
  3644. PROCEDURE MulInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3645. BEGIN
  3646. ReleaseIntermediateOperand(res);
  3647. IF IsImmediate(x) & IsImmediate(y) THEN
  3648. IntermediateCode.InitImmediate(res,x.type,x.intValue*y.intValue);
  3649. ELSE
  3650. IF ~ReusableRegister(res) THEN
  3651. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3652. ELSE UseIntermediateOperand(res);
  3653. END;
  3654. IF IsImmediate(x) & (x.intValue = 1) THEN
  3655. Emit(Mov(position,res,y))
  3656. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3657. Emit(Mov(position,res,x))
  3658. ELSE
  3659. Emit(Mul(position,res, x, y));
  3660. END;
  3661. END;
  3662. END MulInt;
  3663. PROCEDURE DivInt(VAR res: IntermediateCode.Operand; x,y: IntermediateCode.Operand);
  3664. BEGIN
  3665. ReleaseIntermediateOperand(res);
  3666. IF IsImmediate(x) & IsImmediate(y) THEN
  3667. IntermediateCode.InitImmediate(res,x.type,x.intValue DIV y.intValue);
  3668. ELSE
  3669. IF ~ReusableRegister(res) THEN
  3670. IntermediateCode.InitRegister(res,x.type,IntermediateCode.GeneralPurposeRegister,AcquireRegister(x.type,IntermediateCode.GeneralPurposeRegister));
  3671. ELSE UseIntermediateOperand(res);
  3672. END;
  3673. IF IsImmediate(x) & (x.intValue = 1) THEN
  3674. Emit(Mov(position,res,y))
  3675. ELSIF IsImmediate(y) & (y.intValue=1) THEN
  3676. Emit(Mov(position,res,x))
  3677. ELSE
  3678. Emit(Div(position,res, x, y));
  3679. END;
  3680. END;
  3681. END DivInt;
  3682. PROCEDURE IndexDesignator(x: SyntaxTree.IndexDesignator);
  3683. VAR length,res: IntermediateCode.Operand; type: SyntaxTree.Type; maxDim: LONGINT; array:Operand;
  3684. index: Operand; e: SyntaxTree.Expression;i: LONGINT; size: LONGINT; atype: SyntaxTree.ArrayType;
  3685. BEGIN
  3686. type := x.left.type.resolved;
  3687. IF type IS SyntaxTree.StringType THEN
  3688. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  3689. atype.SetArrayBase(type(SyntaxTree.StringType).baseType);
  3690. atype.SetLength(Global.NewIntegerValue(system,-1, type(SyntaxTree.StringType).length));
  3691. type := atype;
  3692. x.left.SetType(type);
  3693. END;
  3694. IntermediateCode.InitImmediate(res,addressType,0);
  3695. maxDim := x.parameters.Length()-1;
  3696. FOR i := 0 TO maxDim DO
  3697. e := x.parameters.GetExpression(i);
  3698. Evaluate(e,index);
  3699. Convert(index.op,addressType);
  3700. AddInt(res, res, index.op);
  3701. IF i = 0 THEN
  3702. (*
  3703. ReuseCopy(res, index.op);
  3704. *)
  3705. Designate(x.left,array);
  3706. type := x.left.type.resolved;
  3707. IF (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & backend.cellsAreObjects THEN
  3708. Dereference(array, type, FALSE);
  3709. END;
  3710. (*
  3711. ELSE AddInt(res, res, index.op);
  3712. *)
  3713. END;
  3714. IF (array.tag.mode # IntermediateCode.Undefined ) THEN
  3715. length := ArrayLength(type(SyntaxTree.ArrayType),array.dimOffset+i,array.tag);
  3716. IF ((length.mode # IntermediateCode.ModeImmediate) OR (index.op.mode # IntermediateCode.ModeImmediate)) & tagsAvailable THEN
  3717. BoundCheck(index.op, length);
  3718. END;
  3719. ReleaseIntermediateOperand(length);
  3720. END;
  3721. ReleaseOperand(index);
  3722. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3723. length := ArrayLength(type,array.dimOffset+i-1,array.tag);
  3724. IF (length.mode # IntermediateCode.ModeImmediate) OR (length.intValue # 1) THEN
  3725. MulInt(res,res,length);
  3726. END;
  3727. ReleaseIntermediateOperand(length);
  3728. END;
  3729. IF (type IS SyntaxTree.ArrayType) THEN
  3730. IF (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  3731. size := StaticSize(system, type);
  3732. IF size # 1 THEN
  3733. length := IntermediateCode.Immediate(addressType,size);
  3734. MulInt(res,res,length);
  3735. END;
  3736. ELSE
  3737. size := StaticSize(system, type(SyntaxTree.ArrayType).arrayBase);
  3738. IF size # 1 THEN
  3739. length := IntermediateCode.Immediate(addressType,size);
  3740. MulInt(res,res,length);
  3741. END;
  3742. END;
  3743. END;
  3744. AddInt(res,res,array.op);
  3745. InitOperand(result,ModeReference);
  3746. result.op := res;
  3747. IF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  3748. ReleaseIntermediateOperand(result.tag);
  3749. result.tag := TypeDescriptorAdr(type);
  3750. IF ~newObjectFile THEN
  3751. IntermediateCode.MakeMemory(result.tag,addressType);
  3752. END
  3753. ELSIF IsDelegate(type) THEN
  3754. ReleaseIntermediateOperand(result.tag);
  3755. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  3756. UseIntermediateOperand(result.tag);
  3757. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  3758. ReleaseIntermediateOperand(result.tag);
  3759. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  3760. ELSIF (type IS SyntaxTree.ArrayType) THEN
  3761. result.tag := array.tag; UseIntermediateOperand(result.tag); result.dimOffset := array.dimOffset+maxDim;
  3762. END;
  3763. ReleaseOperand(array);
  3764. IF (conditional) & (type.resolved IS SyntaxTree.BooleanType) THEN
  3765. ValueToCondition(result); (*! wrong as the result of an index designator is always an address *)
  3766. END;
  3767. END IndexDesignator;
  3768. PROCEDURE VisitIndexDesignator(x: SyntaxTree.IndexDesignator);
  3769. VAR type: SyntaxTree.Type; dest: IntermediateCode.Operand;
  3770. BEGIN
  3771. IF Trace THEN TraceEnter("VisitIndexDesignator") END;
  3772. dest := destination; destination := emptyOperand;
  3773. type := x.left.type.resolved;
  3774. IF type IS SyntaxTree.MathArrayType THEN
  3775. MathIndexDesignator(x);
  3776. ELSE ASSERT((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.StringType));
  3777. IndexDesignator(x);
  3778. END;
  3779. destination := dest;
  3780. IF Trace THEN TraceExit("VisitIndexDesignator") END;
  3781. END VisitIndexDesignator;
  3782. PROCEDURE PrepareTensorDescriptor(expression: SyntaxTree.IndexDesignator): SyntaxTree.Variable;
  3783. VAR variable: SyntaxTree.Variable; srcOperand,destOperand,procOp: Operand;
  3784. moduleName, procedureName: SyntaxTree.IdentifierString; arrayBase: SyntaxTree.Module; saved: RegisterEntry; s: Basic.MessageString;
  3785. procedure: SyntaxTree.Procedure;
  3786. parameters: SyntaxTree.ExpressionList; e: SyntaxTree.Expression;
  3787. prefixIndices, prefixRanges, suffixIndices, suffixRanges,i : LONGINT; tensorFound: BOOLEAN;
  3788. BEGIN
  3789. variable := GetTemporaryVariable(expression.left.type, FALSE);
  3790. parameters := expression.parameters;
  3791. moduleName := "FoxArrayBase";
  3792. procedureName := "CopyDescriptor";
  3793. IF AddImport(moduleName,arrayBase,TRUE) THEN
  3794. SaveRegisters();ReleaseUsedRegisters(saved);
  3795. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  3796. IF procedure = NIL THEN
  3797. s := "procedure ";
  3798. Strings.Append(s,moduleName);
  3799. Strings.Append(s,".");
  3800. Strings.Append(s,procedureName);
  3801. Strings.Append(s," not present");
  3802. Error(position,s);
  3803. ELSE
  3804. (* push address of temporary variable *)
  3805. Symbol(variable,destOperand);
  3806. Emit(Push(position,destOperand.op));
  3807. ReleaseOperand(destOperand);
  3808. (* push src *)
  3809. Evaluate(expression.left,srcOperand);
  3810. (*
  3811. Dereference(srcOperand,expression.type.resolved);
  3812. Emit(Push(position,srcOperand.tag));
  3813. *)
  3814. Emit(Push(position,srcOperand.op));
  3815. ReleaseOperand(srcOperand);
  3816. tensorFound := FALSE;
  3817. FOR i := 0 TO parameters.Length()-1 DO
  3818. e := parameters.GetExpression(i);
  3819. IF e IS SyntaxTree.TensorRangeExpression THEN
  3820. tensorFound := TRUE;
  3821. ELSIF e IS SyntaxTree.RangeExpression THEN
  3822. IF tensorFound THEN INC(suffixRanges) ELSE INC(prefixRanges) END;
  3823. ELSE
  3824. IF tensorFound THEN INC(suffixIndices) ELSE INC(prefixIndices) END;
  3825. END;
  3826. END;
  3827. Emit(Push(position,IntermediateCode.Immediate(int32,prefixIndices)));
  3828. Emit(Push(position,IntermediateCode.Immediate(int32,prefixRanges)));
  3829. Emit(Push(position,IntermediateCode.Immediate(int32,suffixIndices)));
  3830. Emit(Push(position,IntermediateCode.Immediate(int32,suffixRanges)));
  3831. StaticCallOperand(procOp,procedure);
  3832. Emit(Call(position,procOp.op,ProcedureParametersSize(system,procedure)));
  3833. ReleaseOperand(procOp);
  3834. END;
  3835. RestoreRegisters(saved);
  3836. END;
  3837. RETURN variable
  3838. END PrepareTensorDescriptor;
  3839. PROCEDURE PushParameter(expression: SyntaxTree.Expression; parameter: SyntaxTree.Parameter; callingConvention: LONGINT; needsParameterBackup: BOOLEAN; VAR parameterBackup: IntermediateCode.Operand; numberRegister: LONGINT);
  3840. VAR
  3841. type, descriptorType, baseType: SyntaxTree.Type;
  3842. operand, tmpOperand, variableOp, variable2Op: Operand;
  3843. baseReg, tmp, dimOp, null, dst: IntermediateCode.Operand;
  3844. variable, variable2: SyntaxTree.Variable;
  3845. dim, i, size: LONGINT;
  3846. (* TODO: needed? *)
  3847. oldArrayDestinationTag: IntermediateCode.Operand;
  3848. oldArrayDestinationDimension: LONGINT;
  3849. position: LONGINT;
  3850. saved: RegisterEntry;
  3851. PROCEDURE Pass(op: IntermediateCode.Operand);
  3852. VAR registerClass: IntermediateCode.RegisterClass; parameterRegister: IntermediateCode.Operand;
  3853. BEGIN
  3854. IF numberRegister >= 0 THEN
  3855. IntermediateCode.InitRegisterClass(registerClass, IntermediateCode.Parameter, SHORT(numberRegister));
  3856. IntermediateCode.InitRegister(parameterRegister, op.type, registerClass, AcquireRegister(op.type, registerClass));
  3857. Emit(Mov(position,parameterRegister, op));
  3858. ELSE
  3859. Emit(Push(position,op))
  3860. END
  3861. END Pass;
  3862. PROCEDURE PushArrayLens(formalType,actualType: SyntaxTree.Type; dim: LONGINT);
  3863. VAR tmp: IntermediateCode.Operand; actualArrayBase: SyntaxTree.Type;
  3864. BEGIN
  3865. formalType := formalType.resolved; actualType := actualType.resolved;
  3866. IF IsOpenArray(formalType)THEN
  3867. IF actualType IS SyntaxTree.StringType THEN
  3868. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  3869. RETURN;
  3870. ELSIF (actualType IS SyntaxTree.MathArrayType) & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  3871. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.MathArrayType).staticLength)));
  3872. actualArrayBase := actualType(SyntaxTree.MathArrayType).arrayBase.resolved;
  3873. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  3874. Pass((IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  3875. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3876. ELSE
  3877. tmp := baseReg;
  3878. IntermediateCode.AddOffset(tmp,ToMemoryUnits(system,dim*system.addressSize));
  3879. IntermediateCode.MakeMemory(tmp,addressType);
  3880. Pass((tmp));
  3881. actualArrayBase := actualType(SyntaxTree.ArrayType).arrayBase.resolved;
  3882. END;
  3883. PushArrayLens(formalType(SyntaxTree.ArrayType).arrayBase.resolved, actualArrayBase,dim-1);
  3884. END;
  3885. END PushArrayLens;
  3886. BEGIN
  3887. IF Trace THEN TraceEnter("PushParameter") END;
  3888. position := expression.position;
  3889. IF expression.resolved # NIL THEN expression := expression.resolved END;
  3890. type := expression.type.resolved;
  3891. ASSERT( ((type IS SyntaxTree.MathArrayType) = (parameter.type.resolved IS SyntaxTree.MathArrayType))
  3892. OR (type IS SyntaxTree.MathArrayType) & (parameter.type.resolved IS SyntaxTree.ArrayType)
  3893. & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  3894. & (parameter.type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  3895. );
  3896. (* TODO: needed? *)
  3897. oldArrayDestinationTag := arrayDestinationTag;
  3898. oldArrayDestinationDimension := arrayDestinationDimension;
  3899. IF IsArrayOfSystemByte(parameter.type) THEN
  3900. Designate(expression,operand);
  3901. tmp := GetArrayOfBytesSize(expression,operand.tag);
  3902. ReleaseIntermediateOperand(operand.tag);
  3903. operand.tag := tmp;
  3904. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3905. Pass((operand.tag));
  3906. END;
  3907. Pass((operand.op));
  3908. ELSIF IsOpenArray(parameter.type) THEN
  3909. Designate(expression,operand);
  3910. baseReg := operand.tag;
  3911. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  3912. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  3913. END;
  3914. Pass((operand.op)); (* address of the array *)
  3915. ELSIF parameter.type.resolved IS SyntaxTree.MathArrayType THEN
  3916. (* case 1 *)
  3917. IF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) &
  3918. (parameter.kind IN {SyntaxTree.ValueParameter, SyntaxTree.ConstParameter}) THEN
  3919. size := MathLenOffset + 2*SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3920. size := ToMemoryUnits(system,size*addressType.sizeInBits);
  3921. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  3922. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3923. arrayDestinationTag := sp;
  3924. (* case 1b *)
  3925. IF expression IS SyntaxTree.IndexDesignator THEN
  3926. (*
  3927. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3928. descriptorType := GetMathArrayDescriptorType(dim);
  3929. variable := GetTemporaryVariable(descriptorType,expression.position);
  3930. Symbol(variable,variableOp);
  3931. arrayDestinationTag := variableOp.op;
  3932. *)
  3933. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3934. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3935. arrayDestinationDimension := dim;
  3936. Designate(expression,operand);
  3937. (* case 1a *)
  3938. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  3939. Designate(expression,operand);
  3940. Emit(Copy(position,arrayDestinationTag,operand.tag,IntermediateCode.Immediate(addressType,size)));
  3941. i := 0;
  3942. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  3943. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  3944. INC(i);
  3945. END;
  3946. type := expression.type.resolved;
  3947. WHILE (i<dim) DO (* remaining static dimensions *)
  3948. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3949. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3950. ReleaseOperand(tmpOperand);
  3951. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3952. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3953. ReleaseOperand(tmpOperand);
  3954. INC(i);
  3955. END;
  3956. dimOp := IntermediateCode.Immediate(addressType,dim);
  3957. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3958. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3959. (* case 1d *)
  3960. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  3961. Designate(expression,operand);
  3962. Dereference(operand,type.resolved,FALSE);
  3963. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  3964. Emit(Copy(position,sp(*arrayDestinationTag*),operand.tag,IntermediateCode.Immediate(addressType,size)));
  3965. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  3966. (* case 1f *)
  3967. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  3968. Designate(expression,operand);
  3969. FOR i := 0 TO dim-1 DO
  3970. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3971. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  3972. ReleaseOperand(tmpOperand);
  3973. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  3974. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  3975. ReleaseOperand(tmpOperand);
  3976. END;
  3977. dimOp := IntermediateCode.Immediate(addressType,dim);
  3978. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  3979. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  3980. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  3981. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  3982. baseType := SemanticChecker.ArrayBase(type,dim);
  3983. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  3984. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  3985. ELSE HALT(100);
  3986. END;
  3987. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) & (parameter.kind = SyntaxTree.VarParameter) THEN
  3988. dim := SemanticChecker.Dimension(parameter.type.resolved,{SyntaxTree.Open});
  3989. (* case 2b *)
  3990. IF expression IS SyntaxTree.IndexDesignator THEN
  3991. descriptorType := GetMathArrayDescriptorType(dim);
  3992. variable := GetTemporaryVariable(descriptorType, FALSE);
  3993. Symbol(variable,variableOp);
  3994. arrayDestinationTag := variableOp.op;
  3995. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  3996. arrayDestinationDimension := dim;
  3997. NeedDescriptor := TRUE;
  3998. Designate(expression,operand);
  3999. Pass((operand.tag));
  4000. NeedDescriptor := FALSE;
  4001. (* case 2a *)
  4002. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4003. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4004. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4005. INC(i);
  4006. END;
  4007. IF i = dim THEN
  4008. Designate(expression,operand);
  4009. Pass((operand.tag));
  4010. ELSE (* open-static *)
  4011. type := expression.type.resolved;
  4012. descriptorType := GetMathArrayDescriptorType(dim);
  4013. variable := GetTemporaryVariable(descriptorType, FALSE);
  4014. Symbol(variable,variableOp);
  4015. arrayDestinationTag := variableOp.op;
  4016. Designate(expression,operand);
  4017. FOR i := 0 TO dim-1 DO
  4018. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4019. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4020. ReleaseOperand(tmpOperand);
  4021. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4022. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4023. ReleaseOperand(tmpOperand);
  4024. END;
  4025. dimOp := IntermediateCode.Immediate(addressType,dim);
  4026. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4027. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4028. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4029. PutMathArrayField(arrayDestinationTag, IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4030. baseType := SemanticChecker.ArrayBase(type,dim);
  4031. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4032. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4033. Pass((arrayDestinationTag));
  4034. END;
  4035. (* case 2d *)
  4036. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4037. Designate(expression,operand);
  4038. Dereference(operand,type.resolved,FALSE);
  4039. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4040. Pass((operand.tag));
  4041. (* case 2f *)
  4042. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4043. descriptorType := GetMathArrayDescriptorType(dim);
  4044. variable := GetTemporaryVariable(descriptorType, FALSE);
  4045. Symbol(variable,variableOp);
  4046. arrayDestinationTag := variableOp.op;
  4047. Designate(expression,operand);
  4048. FOR i := 0 TO dim-1 DO
  4049. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4050. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4051. ReleaseOperand(tmpOperand);
  4052. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4053. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4054. ReleaseOperand(tmpOperand);
  4055. END;
  4056. dimOp := IntermediateCode.Immediate(addressType,dim);
  4057. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4058. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4059. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4060. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4061. baseType := SemanticChecker.ArrayBase(type,dim);
  4062. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4063. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4064. Pass((arrayDestinationTag));
  4065. ELSE HALT(100);
  4066. END;
  4067. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind IN {SyntaxTree.ConstParameter,SyntaxTree.ValueParameter}) THEN
  4068. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4069. (* case 3b *)
  4070. IF (expression IS SyntaxTree.IndexDesignator) & (type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4071. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4072. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4073. Symbol(variable,variableOp);
  4074. LoadValue(variableOp,system.addressType);
  4075. ELSE
  4076. descriptorType := GetMathArrayDescriptorType(dim);
  4077. variable := GetTemporaryVariable(descriptorType, FALSE);
  4078. Symbol(variable,variableOp);
  4079. END;
  4080. arrayDestinationTag := variableOp.op;
  4081. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4082. arrayDestinationDimension := 0;
  4083. Designate(expression,operand);
  4084. Pass((operand.tag));
  4085. (* case 3a *)
  4086. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4087. i := 0;
  4088. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4089. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4090. INC(i);
  4091. END;
  4092. IF i = dim THEN
  4093. Designate(expression,operand);
  4094. Pass((operand.tag));
  4095. ELSE (* open-static *)
  4096. type := expression.type.resolved;
  4097. descriptorType := GetMathArrayDescriptorType(dim);
  4098. variable := GetTemporaryVariable(descriptorType, FALSE);
  4099. Symbol(variable,variableOp);
  4100. arrayDestinationTag := variableOp.op;
  4101. Designate(expression,operand);
  4102. FOR i := 0 TO dim-1 DO
  4103. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4104. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4105. ReleaseOperand(tmpOperand);
  4106. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4107. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4108. ReleaseOperand(tmpOperand);
  4109. END;
  4110. dimOp := IntermediateCode.Immediate(addressType,dim);
  4111. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4112. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4113. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4114. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset); (* static flag ? *)
  4115. baseType := SemanticChecker.ArrayBase(type,dim);
  4116. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4117. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4118. Pass((arrayDestinationTag));
  4119. END;
  4120. (* case 3d *)
  4121. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4122. Designate(expression,operand);
  4123. Dereference(operand,type.resolved,FALSE);
  4124. (*
  4125. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4126. *)
  4127. Pass((operand.tag));
  4128. (* case 3f *)
  4129. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4130. descriptorType := GetMathArrayDescriptorType(dim);
  4131. variable := GetTemporaryVariable(descriptorType, FALSE);
  4132. Symbol(variable,variableOp);
  4133. arrayDestinationTag := variableOp.op;
  4134. Designate(expression,operand);
  4135. IF operand.op.type.length >1 THEN (* vector register *)
  4136. variable2 := GetTemporaryVariable(type, FALSE);
  4137. Symbol(variable2, variable2Op);
  4138. MakeMemory(tmp,variable2Op.op,operand.op.type,0);
  4139. Emit(Mov(position,tmp, operand.op));
  4140. ReleaseOperand(operand);
  4141. Symbol(variable2, operand);
  4142. END;
  4143. FOR i := 0 TO dim-1 DO
  4144. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4145. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4146. ReleaseOperand(tmpOperand);
  4147. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4148. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4149. ReleaseOperand(tmpOperand);
  4150. END;
  4151. dimOp := IntermediateCode.Immediate(addressType,dim);
  4152. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4153. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4154. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4155. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4156. baseType := SemanticChecker.ArrayBase(type,dim);
  4157. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4158. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4159. Pass((arrayDestinationTag));
  4160. ELSE HALT(100);
  4161. END;
  4162. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (parameter.kind = SyntaxTree.VarParameter) THEN
  4163. dim := SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static});
  4164. (* case 4b *)
  4165. IF (expression IS SyntaxTree.IndexDesignator) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  4166. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN (* indexer of form a[e,....,?] *)
  4167. variable := PrepareTensorDescriptor(expression(SyntaxTree.IndexDesignator));
  4168. Symbol(variable,variableOp);
  4169. LoadValue(variableOp,system.addressType);
  4170. ELSE
  4171. descriptorType := GetMathArrayDescriptorType(dim);
  4172. variable := GetTemporaryVariable(descriptorType, FALSE);
  4173. Symbol(variable,variableOp);
  4174. END;
  4175. arrayDestinationTag := variableOp.op;
  4176. ReuseCopy(arrayDestinationTag,arrayDestinationTag);
  4177. arrayDestinationDimension := 0;
  4178. Designate(expression,operand);
  4179. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4180. Symbol(variable,variableOp);
  4181. ELSE
  4182. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4183. Symbol(variable,variableOp);
  4184. MakeMemory(tmp,variableOp.op,addressType,0);
  4185. Emit(Mov(position,tmp,operand.tag));
  4186. ReleaseIntermediateOperand(tmp);
  4187. END;
  4188. Pass((variableOp.op));
  4189. ReleaseOperand(variableOp);
  4190. (* case 4a *)
  4191. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open THEN
  4192. i := 0;
  4193. WHILE (i< dim) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Open) DO
  4194. type := type.resolved(SyntaxTree.MathArrayType).arrayBase;
  4195. INC(i);
  4196. END;
  4197. IF i = dim THEN
  4198. Designate(expression,operand);
  4199. arrayDestinationTag := operand.tag;
  4200. ELSE (* open-static *)
  4201. type := expression.type.resolved;
  4202. descriptorType := GetMathArrayDescriptorType(dim);
  4203. variable := GetTemporaryVariable(descriptorType, FALSE);
  4204. Symbol(variable,variableOp);
  4205. arrayDestinationTag := variableOp.op;
  4206. Designate(expression,operand);
  4207. FOR i := 0 TO dim-1 DO
  4208. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4209. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4210. ReleaseOperand(tmpOperand);
  4211. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4212. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4213. ReleaseOperand(tmpOperand);
  4214. END;
  4215. dimOp := IntermediateCode.Immediate(addressType,dim);
  4216. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4217. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4218. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4219. PutMathArrayField(arrayDestinationTag,nil,MathFlagsOffset);
  4220. baseType := SemanticChecker.ArrayBase(type,dim);
  4221. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4222. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4223. END;
  4224. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4225. Symbol(variable,variableOp);
  4226. MakeMemory(tmp,variableOp.op,addressType,0);
  4227. Emit(Mov(position,tmp,arrayDestinationTag));
  4228. ReleaseIntermediateOperand(tmp);
  4229. Pass((variableOp.op));
  4230. ReleaseOperand(variableOp);
  4231. (* case 4d *)
  4232. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  4233. Designate(expression,operand);
  4234. (*
  4235. Dereference(operand,type.resolved,FALSE);
  4236. *)
  4237. (*
  4238. DimensionCheck(operand.tag, IntermediateCode.Immediate(int32,dim),BreqL);
  4239. *)
  4240. Pass((operand.op));
  4241. (* case 4f *)
  4242. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4243. descriptorType := GetMathArrayDescriptorType(dim);
  4244. variable := GetTemporaryVariable(descriptorType, FALSE);
  4245. Symbol(variable,variableOp);
  4246. arrayDestinationTag := variableOp.op;
  4247. Designate(expression,operand);
  4248. FOR i := 0 TO dim-1 DO
  4249. GetMathArrayLengthAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4250. PutMathArrayLength(arrayDestinationTag,tmpOperand.op,i);
  4251. ReleaseOperand(tmpOperand);
  4252. GetMathArrayIncrementAt(type.resolved(SyntaxTree.MathArrayType),operand,i,FALSE,tmpOperand);
  4253. PutMathArrayIncrement(arrayDestinationTag,tmpOperand.op,i);
  4254. ReleaseOperand(tmpOperand);
  4255. END;
  4256. dimOp := IntermediateCode.Immediate(addressType,dim);
  4257. PutMathArrayField(arrayDestinationTag,dimOp,MathDimOffset);
  4258. PutMathArrayField(arrayDestinationTag,operand.op,MathAdrOffset);
  4259. PutMathArrayField(arrayDestinationTag,nil,MathPtrOffset);
  4260. PutMathArrayField(arrayDestinationTag,IntermediateCode.Immediate(addressType,SYSTEM.VAL(LONGINT,{StaticFlag})),MathFlagsOffset);
  4261. baseType := SemanticChecker.ArrayBase(type,dim);
  4262. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.AlignedSizeOf(baseType)));
  4263. PutMathArrayField(arrayDestinationTag,tmp,MathElementSizeOffset);
  4264. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4265. Symbol(variable,variableOp);
  4266. MakeMemory(tmp,variableOp.op,addressType,0);
  4267. Emit(Mov(position,tmp,arrayDestinationTag));
  4268. ReleaseIntermediateOperand(tmp);
  4269. Pass((variableOp.op));
  4270. ReleaseOperand(variableOp);
  4271. ELSE HALT(100);
  4272. END;
  4273. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind = SyntaxTree.ValueParameter) THEN
  4274. ASSERT(type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static);
  4275. Designate(expression,operand);
  4276. IF operand.op.type.length > 1 THEN
  4277. Emit(Push(position, operand.op));
  4278. ReleaseOperand(operand);
  4279. ELSE
  4280. size := system.SizeOf(type);
  4281. Basic.Align(size,system.AlignmentOf(system.parameterAlignment,type));
  4282. size := ToMemoryUnits(system,size);
  4283. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size)));
  4284. arrayDestinationTag := sp;
  4285. Emit(Copy(position,arrayDestinationTag,operand.op,IntermediateCode.Immediate(addressType,size)));
  4286. END;
  4287. ELSIF (parameter.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (parameter.kind IN {SyntaxTree.VarParameter,SyntaxTree.ConstParameter}) THEN
  4288. Designate(expression,operand);
  4289. IF operand.op.type.length > 1 THEN (* need temporary to pass register *)
  4290. variable := GetTemporaryVariable(parameter.type.resolved, FALSE);
  4291. Symbol(variable,variableOp);
  4292. MakeMemory(tmp,variableOp.op,operand.op.type,0);
  4293. Emit(Mov(position,tmp,operand.op));
  4294. Emit(Push(position,variableOp.op));
  4295. ELSIF type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Static THEN
  4296. Pass((operand.op));
  4297. ELSE Error(position,"Forbidden non-static actual type. Conversion involved?");
  4298. END;
  4299. ELSE HALT(200)
  4300. END;
  4301. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  4302. IF parameter.kind = SyntaxTree.VarParameter THEN
  4303. ASSERT(~(expression IS SyntaxTree.RangeExpression));
  4304. Designate(expression, operand);
  4305. Pass((operand.op));
  4306. ELSE
  4307. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4308. Evaluate(expression, operand);
  4309. Pass((operand.extra)); (* step *)
  4310. Pass((operand.tag)); (* last *)
  4311. Pass((operand.op)) (* first *)
  4312. END
  4313. ELSIF parameter.type.resolved IS SyntaxTree.ComplexType THEN
  4314. IF parameter.kind = SyntaxTree.VarParameter THEN
  4315. Designate(expression, operand);
  4316. Pass((operand.op));
  4317. ELSE
  4318. ASSERT((parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter));
  4319. Evaluate(expression, operand);
  4320. Pass((operand.tag)); (* real part *)
  4321. Pass((operand.op)) (* imaginary part *)
  4322. END
  4323. ELSE
  4324. IF (parameter.kind = SyntaxTree.ValueParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ~(parameter.type.resolved IS SyntaxTree.RecordType) & ~(parameter.type.resolved IS SyntaxTree.ArrayType) THEN
  4325. IF (type IS SyntaxTree.RecordType) OR IsStaticArray(parameter.type) THEN
  4326. Designate(expression,operand);
  4327. size := ToMemoryUnits(system,system.SizeOf(parameter.type));
  4328. (* stack allocation *)
  4329. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,size + (-size) MOD (system.addressSize DIV system.dataUnit))));
  4330. (*! parameter alignment to be discussed ... *)
  4331. IF type IS SyntaxTree.StringType THEN (* source potentially shorter than destination *)
  4332. size := type(SyntaxTree.StringType).length;
  4333. END;
  4334. IF backend.cooperative & parameter.NeedsTrace() THEN
  4335. dst := NewRegisterOperand (addressType);
  4336. Emit(Mov(position,dst, sp));
  4337. IntermediateCode.InitImmediate(null, byteType, 0);
  4338. Emit(Fill(position, dst, IntermediateCode.Immediate(addressType,size), null));
  4339. ReleaseIntermediateOperand(dst);
  4340. SaveRegisters();ReleaseUsedRegisters(saved);
  4341. (* register dst has been freed before SaveRegisters already *)
  4342. CallAssignMethod(dst, operand.op, parameter.type);
  4343. RestoreRegisters(saved);
  4344. END;
  4345. Emit(Copy(position,sp,operand.op,IntermediateCode.Immediate(addressType,size)));
  4346. ELSIF IsOpenArray(parameter.type) THEN
  4347. Designate(expression,operand);
  4348. baseReg := operand.tag;
  4349. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4350. PushArrayLens(parameter.type,type,operand.dimOffset+DynamicDim(parameter.type)-1);
  4351. END;
  4352. Pass((operand.op)); (* address of the array *)
  4353. ELSIF IsDelegate(parameter.type) THEN
  4354. Evaluate(expression,operand);
  4355. IF backend.cooperative & parameter.NeedsTrace() THEN
  4356. Emit(Push(position, nil));
  4357. dst := NewRegisterOperand (addressType);
  4358. Emit(Mov(position,dst, sp));
  4359. ReleaseIntermediateOperand(dst);
  4360. SaveRegisters();ReleaseUsedRegisters(saved);
  4361. Emit(Push(position, dst));
  4362. (* register dst has been freed before SaveRegisters already *)
  4363. Emit(Push(position, operand.tag));
  4364. CallThis(position,"GarbageCollector","Assign",2);
  4365. RestoreRegisters(saved);
  4366. ELSE
  4367. Pass((operand.tag));
  4368. END;
  4369. Pass((operand.op));
  4370. ELSE
  4371. Evaluate(expression,operand);
  4372. IF backend.cooperative & parameter.NeedsTrace() & (operand.op.mode # IntermediateCode.ModeImmediate) THEN
  4373. Emit(Push(position, nil));
  4374. dst := NewRegisterOperand (addressType);
  4375. Emit(Mov(position,dst, sp));
  4376. ReleaseIntermediateOperand(dst);
  4377. SaveRegisters();ReleaseUsedRegisters(saved);
  4378. Emit(Push(position, dst));
  4379. (* register dst has been freed before SaveRegisters already *)
  4380. Emit(Push(position, operand.op));
  4381. CallThis(position,"GarbageCollector","Assign",2);
  4382. RestoreRegisters(saved);
  4383. ELSE
  4384. Pass((operand.op));
  4385. END;
  4386. END;
  4387. ELSIF expression IS SyntaxTree.NilValue THEN (* for special WinAPI rule *)
  4388. Evaluate(expression,operand);
  4389. Pass((operand.op));
  4390. ELSE (* var parameter *)
  4391. Designate(expression,operand);
  4392. IF (type IS SyntaxTree.RecordType) & (parameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4393. IF callingConvention = SyntaxTree.OberonCallingConvention THEN
  4394. Pass((operand.tag));
  4395. END;
  4396. END;
  4397. Pass((operand.op));
  4398. END;
  4399. END;
  4400. (* TODO: needed? *)
  4401. arrayDestinationTag := oldArrayDestinationTag;
  4402. arrayDestinationDimension := oldArrayDestinationDimension;
  4403. IF needsParameterBackup THEN
  4404. (* IF dump # NIL THEN dump.String("backup parameter"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4405. ReuseCopy(parameterBackup, operand.op)
  4406. END;
  4407. ReleaseOperand(operand);
  4408. IF Trace THEN TraceExit("PushParameter") END;
  4409. END PushParameter;
  4410. PROCEDURE VisitStatementDesignator(x: SyntaxTree.StatementDesignator);
  4411. VAR prevConditional: BOOLEAN;
  4412. BEGIN
  4413. prevConditional := conditional;
  4414. conditional := FALSE;
  4415. 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
  4416. Expression(x.result); (* use register *)
  4417. END;
  4418. Statement(x.statement);
  4419. conditional := prevConditional;
  4420. IF x.result # NIL THEN Expression(x.result) END;
  4421. 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
  4422. ReleaseIntermediateOperand(result.op);
  4423. END;
  4424. END VisitStatementDesignator;
  4425. PROCEDURE InlineProcedureCall(x: SyntaxTree.ProcedureCallDesignator): BOOLEAN;
  4426. VAR
  4427. procedure: SyntaxTree.Procedure;
  4428. procedureType: SyntaxTree.ProcedureType;
  4429. wasInline: BOOLEAN;
  4430. actualParameters: SyntaxTree.ExpressionList;
  4431. formalParameter: SyntaxTree.Parameter;
  4432. actualParameter: SyntaxTree.Expression;
  4433. i: LONGINT;
  4434. localVariable: SyntaxTree.Variable;
  4435. variableDesignator, returnDesignator: SyntaxTree.Expression;
  4436. src, dest: Operand;
  4437. prevInlineExit : Label;
  4438. prevMapper: SymbolMapper;
  4439. tooComplex: BOOLEAN;
  4440. PROCEDURE SimpleExpression(e: SyntaxTree.Expression): BOOLEAN;
  4441. BEGIN
  4442. IF e = NIL THEN RETURN TRUE
  4443. ELSIF (e IS SyntaxTree.SymbolDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.SymbolDesignator).left)
  4444. ELSIF (e IS SyntaxTree.Value) THEN RETURN TRUE
  4445. ELSIF (e IS SyntaxTree.SelfDesignator) THEN RETURN TRUE
  4446. ELSIF (e IS SyntaxTree.ResultDesignator) THEN RETURN TRUE
  4447. ELSIF (e IS SyntaxTree.DereferenceDesignator) THEN RETURN SimpleExpression(e(SyntaxTree.DereferenceDesignator).left)
  4448. ELSE RETURN FALSE
  4449. END;
  4450. END SimpleExpression;
  4451. PROCEDURE FitsInRegister(type: SyntaxTree.Type): BOOLEAN;
  4452. BEGIN
  4453. RETURN checker.CanPassInRegister(type)
  4454. END FitsInRegister;
  4455. PROCEDURE GetTemp(type: SyntaxTree.Type; tryRegister: BOOLEAN): SyntaxTree.Expression;
  4456. VAR
  4457. variable: SyntaxTree.Variable;
  4458. variableDesignator: SyntaxTree.Designator;
  4459. BEGIN
  4460. variable := GetTemporaryVariable(type, tryRegister & FitsInRegister(type));
  4461. variableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4462. variableDesignator.SetType(type);
  4463. RETURN variableDesignator
  4464. END GetTemp;
  4465. BEGIN
  4466. wasInline := currentIsInline;
  4467. prevInlineExit := currentInlineExit;
  4468. prevMapper := currentMapper;
  4469. currentInlineExit := NewLabel();
  4470. tooComplex := FALSE;
  4471. NEW(currentMapper);
  4472. currentIsInline := TRUE;
  4473. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4474. procedureType := procedure.type(SyntaxTree.ProcedureType);
  4475. formalParameter := procedureType.firstParameter;
  4476. actualParameters := x.parameters;
  4477. i := 0;
  4478. WHILE (i < actualParameters.Length()) & ~tooComplex DO
  4479. actualParameter := actualParameters.GetExpression(i);
  4480. IF actualParameter.resolved # NIL THEN
  4481. actualParameter := actualParameter.resolved
  4482. END;
  4483. (*
  4484. if expression is simple and can be passed immediately
  4485. or if type fits in register then we can proceed
  4486. otherwise we escape to ordinary procedure call.
  4487. *)
  4488. (* cases where the expression can be mapped identically *)
  4489. IF SimpleExpression(actualParameter) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.VarParameter}) THEN
  4490. currentMapper.Add(formalParameter, actualParameter, NIL);
  4491. ELSIF FitsInRegister(actualParameter.type) & (formalParameter.kind IN {SyntaxTree.ConstParameter, SyntaxTree.ValueParameter}) THEN
  4492. variableDesignator := GetTemp(formalParameter.type, TRUE);
  4493. (*
  4494. Assign(variableDesignator, actualParameter);
  4495. *)
  4496. Evaluate(actualParameter, src);
  4497. Designate(variableDesignator, dest);
  4498. Emit(Mov(x.position, dest.op, src.op));
  4499. ReleaseOperand(dest);
  4500. ReleaseOperand(src);
  4501. currentMapper.Add(formalParameter, variableDesignator, NIL);
  4502. ELSE tooComplex := TRUE
  4503. END;
  4504. (*
  4505. ELSIF (formalParameter.kind = SyntaxTree.ConstParameter) & IsSimple(actualParameter) THEN
  4506. currentMapper.Add(formalParameter, actualParameter, NIL);
  4507. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) OR formalParameter.type.IsComposite() & (formalParameter.kind = SyntaxTree.ConstParameter) THEN
  4508. variableDesignator := GetTemp(system.addressType, FALSE);
  4509. Designate(actualParameter, src);
  4510. Designate(variableDesignator, dest);
  4511. IntermediateCode.MakeMemory(dest.op,addressType);
  4512. Emit(Mov(x.position, dest.op, src.op));
  4513. ReleaseOperand(dest);
  4514. IF src.tag.mode # IntermediateCode.Undefined THEN
  4515. tagDesignator := GetTemp(system.addressType, FALSE);
  4516. Designate(tagDesignator, dest);
  4517. IntermediateCode.MakeMemory(dest.op,addressType);
  4518. Emit(Mov(x.position, dest.op, src.op));
  4519. END;
  4520. ReleaseOperand(dest); ReleaseOperand(src);
  4521. currentMapper.Add(formalParameter, variableDesignator, tagDesignator);
  4522. END;
  4523. *)
  4524. formalParameter := formalParameter.nextParameter;
  4525. INC(i);
  4526. END;
  4527. IF ~tooComplex & (procedureType.returnType # NIL) THEN
  4528. returnDesignator := GetTemp(procedureType.returnType, TRUE);
  4529. currentMapper.Add(NIL, returnDesignator, NIL);
  4530. END;
  4531. localVariable := procedure.procedureScope.firstVariable;
  4532. WHILE ~tooComplex & (localVariable # NIL) DO
  4533. variableDesignator := GetTemp(localVariable.type, FALSE);
  4534. currentMapper.Add(localVariable, variableDesignator, NIL);
  4535. localVariable := localVariable.nextVariable;
  4536. END;
  4537. IF ~tooComplex THEN
  4538. VisitStatementBlock(procedure.procedureScope.body);
  4539. SetLabel(currentInlineExit);
  4540. IF procedureType.returnType # NIL THEN
  4541. Designate(returnDesignator, result);
  4542. IF conditional THEN
  4543. ASSERT (procedureType.returnType.resolved IS SyntaxTree.BooleanType);
  4544. ValueToCondition(result)
  4545. END;
  4546. END;
  4547. END;
  4548. currentMapper := prevMapper;
  4549. currentInlineExit := prevInlineExit;
  4550. currentIsInline := wasInline;
  4551. RETURN ~tooComplex
  4552. END InlineProcedureCall;
  4553. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4554. VAR
  4555. parameters: SyntaxTree.ExpressionList;
  4556. d, resultDesignator, actualParameter: SyntaxTree.Expression;
  4557. designator: SyntaxTree.Designator;
  4558. procedureType: SyntaxTree.ProcedureType;
  4559. formalParameter: SyntaxTree.Parameter;
  4560. operand, returnValue: Operand;
  4561. reg, size, mask, dest: IntermediateCode.Operand;
  4562. saved: RegisterEntry;
  4563. symbol: SyntaxTree.Symbol;
  4564. variable: SyntaxTree.Variable;
  4565. i, parametersSize, returnTypeSize : LONGINT;
  4566. structuredReturnType: BOOLEAN;
  4567. firstWriteBackCall, currentWriteBackCall: WriteBackCall;
  4568. tempVariableDesignator: SyntaxTree.Designator;
  4569. gap, alignment: LONGINT; (*fld*)
  4570. (* TODO: remove unnecessary backup variables *)
  4571. oldResult: Operand;
  4572. oldCurrentScope: SyntaxTree.Scope;
  4573. oldArrayDestinationTag: IntermediateCode.Operand;
  4574. oldArrayDestinationDimension: LONGINT;
  4575. oldConstantDeclaration: SyntaxTree.Symbol;
  4576. oldDestination: IntermediateCode.Operand;
  4577. oldCurrentLoop: Label;
  4578. oldConditional: BOOLEAN;
  4579. oldTrueLabel, oldFalseLabel: Label;
  4580. oldLocked: BOOLEAN;
  4581. usedRegisters,oldUsedRegisters: RegisterEntry;
  4582. return: IntermediateCode.Operand;
  4583. parameterBackups: ARRAY 2 OF IntermediateCode.Operand;
  4584. arg: IntermediateCode.Operand;
  4585. dummy: IntermediateCode.Operand;
  4586. recordType: SyntaxTree.RecordType;
  4587. operatorSelectionProcedureOperand: Operand;
  4588. operatorSelectionProcedure: SyntaxTree.Procedure;
  4589. fingerPrint: SyntaxTree.FingerPrint;
  4590. isCallOfDynamicOperator, hasDynamicOperands: BOOLEAN;
  4591. identifierNumber: LONGINT;
  4592. passByRegister: BOOLEAN; registerNumber,stackSize: LONGINT;
  4593. procedure: SyntaxTree.Procedure;
  4594. PROCEDURE BackupGlobalState;
  4595. BEGIN
  4596. oldResult := result;
  4597. oldCurrentScope := currentScope;
  4598. oldArrayDestinationTag := arrayDestinationTag;
  4599. oldArrayDestinationDimension := arrayDestinationDimension;
  4600. oldConstantDeclaration := constantDeclaration;
  4601. oldDestination := destination;
  4602. oldCurrentLoop := currentLoop;
  4603. oldConditional := conditional;
  4604. oldTrueLabel := trueLabel;
  4605. oldFalseLabel := falseLabel;
  4606. oldLocked := locked;
  4607. oldUsedRegisters := usedRegisters
  4608. END BackupGlobalState;
  4609. PROCEDURE RestoreGlobalState;
  4610. BEGIN
  4611. result := oldResult;
  4612. currentScope := oldCurrentScope;
  4613. arrayDestinationTag := oldArrayDestinationTag;
  4614. arrayDestinationDimension := oldArrayDestinationDimension;
  4615. constantDeclaration := oldConstantDeclaration;
  4616. destination := oldDestination;
  4617. currentLoop := oldCurrentLoop;
  4618. conditional := oldConditional;
  4619. trueLabel := oldTrueLabel;
  4620. falseLabel := oldFalseLabel;
  4621. locked := oldLocked;
  4622. usedRegisters := oldUsedRegisters
  4623. END RestoreGlobalState;
  4624. (** do preparations before parameter push for array-structured object types (ASOTs):
  4625. if ASOT is passed as VAR parameter:
  4626. - allocate temporary variable of math array type
  4627. - copy contents of ASOT to be passed to temporary variable
  4628. - use temporary variable as the actual parameter instead
  4629. - 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)
  4630. **)
  4631. PROCEDURE PrepareParameter(VAR actualParameter: SyntaxTree.Expression; formalParameter: SyntaxTree.Parameter);
  4632. VAR
  4633. expression: SyntaxTree.Expression;
  4634. BEGIN
  4635. IF actualParameter IS SyntaxTree.Designator THEN
  4636. designator := actualParameter(SyntaxTree.Designator);
  4637. IF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  4638. (* actual parameter is an index read operator call on ASOT and formal parameter is of VAR kind *)
  4639. (* IF dump # NIL THEN dump.String("!!! BEGIN ASOT VAR parameter preparation"); dump.Ln; dump.Update END; *) (* TENTATIVE*)
  4640. ASSERT(checker # NIL);
  4641. checker.SetCurrentScope(currentScope);
  4642. (* allocate temporary variable *)
  4643. ASSERT(actualParameter.type # NIL);
  4644. ASSERT(actualParameter.type.resolved IS SyntaxTree.MathArrayType);
  4645. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4646. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4647. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4648. ASSERT(tempVariableDesignator IS SyntaxTree.SymbolDesignator);
  4649. ASSERT(tempVariableDesignator.type # NIL);
  4650. ASSERT(tempVariableDesignator.type.resolved IS SyntaxTree.MathArrayType);
  4651. (* copy math array stored in actual parameter to temporary variable *)
  4652. BackupGlobalState;
  4653. AssignMathArray(tempVariableDesignator, actualParameter);
  4654. RestoreGlobalState;
  4655. (* use temporary variable as actual parameter instead of the original one *)
  4656. actualParameter := tempVariableDesignator;
  4657. (* create write-back call and store it in linked list *)
  4658. (* create new list entry *)
  4659. IF firstWriteBackCall = NIL THEN
  4660. NEW(firstWriteBackCall);
  4661. currentWriteBackCall := firstWriteBackCall
  4662. ELSE
  4663. ASSERT(currentWriteBackCall # NIL);
  4664. NEW(currentWriteBackCall.next);
  4665. currentWriteBackCall := currentWriteBackCall.next
  4666. END;
  4667. expression := checker.NewIndexOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4668. ASSERT(expression.type = NIL);
  4669. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4670. ELSIF (formalParameter.kind = SyntaxTree.VarParameter) & (designator.relatedAsot # NIL) THEN
  4671. (* prepare writeback for any other "normal" indexer *)
  4672. variable := GetTemporaryVariable(actualParameter.type.resolved, FALSE);
  4673. tempVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, variable);
  4674. tempVariableDesignator.SetType(actualParameter.type.resolved);
  4675. Assign(tempVariableDesignator, actualParameter);
  4676. actualParameter := tempVariableDesignator;
  4677. IF firstWriteBackCall = NIL THEN
  4678. NEW(firstWriteBackCall);
  4679. currentWriteBackCall := firstWriteBackCall
  4680. ELSE
  4681. ASSERT(currentWriteBackCall # NIL);
  4682. NEW(currentWriteBackCall.next);
  4683. currentWriteBackCall := currentWriteBackCall.next
  4684. END;
  4685. expression := checker.NewObjectOperatorCall(SemanticChecker.InvalidPosition, designator.relatedAsot, designator.relatedIndexList, tempVariableDesignator);
  4686. currentWriteBackCall.call := expression(SyntaxTree.ProcedureCallDesignator);
  4687. END
  4688. END
  4689. END PrepareParameter;
  4690. BEGIN
  4691. IF Trace THEN TraceEnter("VisitProcedureCallDesignator") END;
  4692. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  4693. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  4694. IF procedure.isOberonInline & (procedure.scope.ownerModule = module.module) THEN
  4695. IF InlineProcedureCall(x) THEN
  4696. RETURN
  4697. ELSE
  4698. Warning(x.position, "Inline call with complex parameters not yet implemented. Emitting ordinary procedure call.")
  4699. END
  4700. END;
  4701. END;
  4702. resultDesignator := procedureResultDesignator; procedureResultDesignator := NIL;
  4703. procedureType := x.left.type.resolved(SyntaxTree.ProcedureType);
  4704. dest := destination; destination := emptyOperand;
  4705. SaveRegisters();ReleaseUsedRegisters(saved);
  4706. parameters := x.parameters;
  4707. IF (x.left IS SyntaxTree.SymbolDesignator) & (x.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Operator) THEN
  4708. (* an operator is called *)
  4709. (* IF dump # NIL THEN dump.String("*** begin of operator call ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4710. ASSERT(procedureType.callingConvention = SyntaxTree.OberonCallingConvention);
  4711. (* check if a dynamic operator call should be performed *)
  4712. isCallOfDynamicOperator := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Operator).isDynamic
  4713. ELSE
  4714. isCallOfDynamicOperator := FALSE
  4715. END;
  4716. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4717. Emit(Push(position, ap));
  4718. END;
  4719. alignment := procedureType.stackAlignment;
  4720. IF alignment > 1 THEN
  4721. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4722. Emit(Mov(position,reg, sp));
  4723. gap := ParametersSize(system, procedureType, FALSE) + ToMemoryUnits(system,system.offsetFirstParameter) + ToMemoryUnits(system,system.addressSize);
  4724. Emit(Sub(position,sp, sp, IntermediateCode.Immediate(addressType,gap)));
  4725. IntermediateCode.InitImmediate(mask,addressType,-alignment);
  4726. Emit(And(position,sp, sp, mask));
  4727. Emit(Add(position, sp, sp, IntermediateCode.Immediate(addressType, gap)));
  4728. Emit(Push(position,reg));
  4729. (*
  4730. IntermediateCode.InitMemory(mem, addressType, sp, 0);
  4731. Emit(Mov(position,mem,reg));
  4732. *)
  4733. ReleaseIntermediateOperand(reg);
  4734. END;
  4735. IF procedureType.callingConvention = SyntaxTree.DarwinCCallingConvention THEN (*fld*)
  4736. IntermediateCode.InitImmediate(mask,addressType,-16);
  4737. Emit(And(position,sp, sp, mask));
  4738. gap := (-ParametersSize( system, procedureType, FALSE )) MOD 16;
  4739. IF gap # 0 THEN
  4740. IntermediateCode.InitImmediate(size,addressType,gap);
  4741. Emit(Sub(position,sp,sp,size))
  4742. END;
  4743. END;
  4744. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4745. symbol := x.left(SyntaxTree.SupercallDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4746. ELSIF x.left IS SyntaxTree.IndexDesignator THEN
  4747. symbol := x.left(SyntaxTree.IndexDesignator).left(SyntaxTree.SymbolDesignator).symbol;
  4748. ELSE
  4749. symbol := x.left(SyntaxTree.SymbolDesignator).symbol;
  4750. END;
  4751. IF isCallOfDynamicOperator & hasDynamicOperands THEN
  4752. IF dump # NIL THEN dump.String("++++++++++ dynamic operator call ++++++++++"); dump.Ln; dump.Update END; (* TENTATIVE *)
  4753. (* push ID *)
  4754. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4755. ASSERT(x.left IS SyntaxTree.SymbolDesignator);
  4756. identifierNumber := Global.GetSymbol(module.module.case, x.left(SyntaxTree.SymbolDesignator).symbol.name);
  4757. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), identifierNumber)));
  4758. formalParameter := procedureType.firstParameter;
  4759. FOR i := 0 TO parameters.Length() - 1 DO
  4760. IF formalParameter.access # SyntaxTree.Hidden THEN
  4761. ASSERT(i < 2);
  4762. IF IsStrictlyPointerToRecord(formalParameter.type) THEN
  4763. (* push pointer *)
  4764. (* IF dump # NIL THEN dump.String("push pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4765. IF formalParameter.kind = SyntaxTree.VarParameter THEN
  4766. (* add dereference *)
  4767. (* IF dump # NIL THEN dump.String("dereference pointer"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4768. ReleaseIntermediateOperand(parameterBackups[i]);
  4769. MakeMemory(parameterBackups[i], parameterBackups[i], addressType, 0)
  4770. END;
  4771. Emit(Push(position,parameterBackups[i]));
  4772. ReleaseIntermediateOperand(parameterBackups[i]);
  4773. (* push typetag *)
  4774. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4775. recordType := formalParameter.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4776. arg := TypeDescriptorAdr(recordType);
  4777. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  4778. Emit(Push(position,arg));
  4779. ELSE
  4780. (* push 'NonPointer' *)
  4781. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4782. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4783. (* push fingerprint *)
  4784. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4785. fingerPrint := fingerPrinter.TypeFP(formalParameter.type.resolved);
  4786. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow))) (* TODO: push the type's fingerprint *)
  4787. END
  4788. END;
  4789. formalParameter := formalParameter.nextParameter
  4790. END;
  4791. (* for unary operators: complete the information for the second parameter *)
  4792. IF procedureType.numberParameters < 2 THEN
  4793. (* push 'NonPointer' *)
  4794. (* IF dump # NIL THEN dump.String("push 'NonPointer'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4795. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NonPointer)));
  4796. (* push 'NoType' *)
  4797. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  4798. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)));
  4799. END;
  4800. (* call operator selection procedure *)
  4801. IF GetRuntimeProcedure("FoxOperatorRuntime", "SelectOperator", operatorSelectionProcedure, TRUE) THEN
  4802. StaticCallOperand(operatorSelectionProcedureOperand, operatorSelectionProcedure);
  4803. Emit(Call(position,operatorSelectionProcedureOperand.op, ProcedureParametersSize(system, operatorSelectionProcedure)));
  4804. ReleaseOperand(operatorSelectionProcedureOperand);
  4805. (* use the address that the operator selection procedure returned as the target address of the call *)
  4806. InitOperand(operand, ModeValue);
  4807. operand.op := IntermediateCode.Register(addressType, IntermediateCode.GeneralPurposeRegister, AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  4808. Emit(Result(position,operand.op))
  4809. END
  4810. ELSE
  4811. Evaluate(x.left, operand)
  4812. END;
  4813. IF symbol IS SyntaxTree.Procedure THEN
  4814. IF x.left IS SyntaxTree.SupercallDesignator THEN
  4815. Emit(Push(position,operand.tag));
  4816. ELSIF (procedureType.isDelegate) THEN
  4817. Emit(Push(position,operand.tag));
  4818. END;
  4819. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4820. IF (procedureType.isDelegate) THEN (* push self pointer only if procedure is a method *)
  4821. Emit(Push(position,operand.tag));
  4822. END;
  4823. ELSE HALT(200);
  4824. END;
  4825. ReleaseIntermediateOperand(operand.tag);
  4826. operand.tag := emptyOperand;
  4827. (* determine if a structured return type is needed *)
  4828. structuredReturnType := StructuredReturnType(procedureType);
  4829. IF structuredReturnType THEN
  4830. IF resultDesignator # NIL THEN
  4831. d := resultDesignator;
  4832. ELSE
  4833. variable := GetTemporaryVariable(procedureType.returnType, FALSE);
  4834. variable.SetUntraced(procedureType.hasUntracedReturn);
  4835. d := SyntaxTree.NewSymbolDesignator(-1,NIL,variable);
  4836. d.SetType(variable.type);
  4837. END;
  4838. IF (procedureType.returnType.resolved IS SyntaxTree.RecordType) THEN
  4839. Designate(d,returnValue);
  4840. returnTypeSize := system.SizeOf(procedureType.returnType.resolved);
  4841. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,returnTypeSize));
  4842. Emit(Push(position,size));
  4843. Emit(Push(position,returnValue.op));
  4844. ReleaseOperand(returnValue);
  4845. ELSE
  4846. PushParameter(d,procedureType.returnParameter,procedureType.callingConvention, FALSE, dummy,-1)
  4847. END;
  4848. END;
  4849. firstWriteBackCall := NIL; (* reset write-back call list *)
  4850. IF procedureType.callingConvention # SyntaxTree.OberonCallingConvention THEN
  4851. passByRegister := system.registerParameters > 0;
  4852. registerNumber := 0;
  4853. formalParameter := procedureType.lastParameter;
  4854. FOR i := parameters.Length() - 1 TO 0 BY -1 DO
  4855. actualParameter := parameters.GetExpression(i);
  4856. PrepareParameter(actualParameter, formalParameter);
  4857. IF passByRegister & (i < system.registerParameters) THEN
  4858. IF ~PassInRegister(formalParameter) THEN
  4859. Error(actualParameter.position,"cannot be passed by register")
  4860. ELSE
  4861. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,i);
  4862. END;
  4863. INC(registerNumber);
  4864. ELSE
  4865. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4866. END;
  4867. formalParameter := formalParameter.prevParameter;
  4868. END;
  4869. IF passByRegister & (registerNumber > 0) THEN
  4870. stackSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4871. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4872. END;
  4873. ELSE
  4874. hasDynamicOperands := FALSE;
  4875. formalParameter := procedureType.firstParameter;
  4876. FOR i := 0 TO parameters.Length() - 1 DO
  4877. actualParameter := parameters.GetExpression(i);
  4878. IF formalParameter # NIL THEN (* TENTATIVE *)
  4879. PrepareParameter(actualParameter, formalParameter);
  4880. IF isCallOfDynamicOperator & IsStrictlyPointerToRecord(formalParameter.type) & (formalParameter.access # SyntaxTree.Hidden) THEN (* TODO: remove hidden parameters *)
  4881. ASSERT(i < 2);
  4882. hasDynamicOperands := TRUE;
  4883. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, TRUE, parameterBackups[i],-1)
  4884. ELSE
  4885. IF passByRegister & (registerNumber > 0) THEN
  4886. stackSize := ToMemoryUnits(system,registerNumber*addressType.sizeInBits);
  4887. Emit(Sub(position,sp,sp,IntermediateCode.Immediate(addressType,stackSize)));
  4888. END;
  4889. passByRegister := FALSE;
  4890. PushParameter(actualParameter, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  4891. END;
  4892. formalParameter := formalParameter.nextParameter;
  4893. END;
  4894. END;
  4895. END;
  4896. IF symbol IS SyntaxTree.Procedure THEN
  4897. IF IsNested(symbol(SyntaxTree.Procedure)) THEN
  4898. GetBaseRegister(reg,currentScope,symbol.scope); (* static link, may be fp or [fp+8] (if nested proc calls itself) *)
  4899. Emit(Push(position,reg));
  4900. ReleaseIntermediateOperand(reg);
  4901. END;
  4902. parametersSize := ProcedureParametersSize(system,symbol(SyntaxTree.Procedure));
  4903. ELSIF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  4904. parametersSize := ParametersSize(system,procedureType,FALSE);
  4905. END;
  4906. ReleaseParameterRegisters();
  4907. IF (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) OR (procedureType.callingConvention = SyntaxTree.CCallingConvention) THEN
  4908. Emit(Call(position,operand.op,0));
  4909. ELSE
  4910. Emit(Call(position,operand.op,parametersSize));
  4911. END;
  4912. ReleaseOperand(operand);
  4913. IF procedureType.noReturn THEN
  4914. EmitTrap(position,NoReturnTrap);
  4915. END;
  4916. IF (procedureType.returnType # NIL) & ~structuredReturnType THEN
  4917. return := NewRegisterOperand(IntermediateCode.GetType(system,procedureType.returnType));
  4918. Emit(Result(position,return));
  4919. END;
  4920. IF procedureType.callingConvention = SyntaxTree.CCallingConvention THEN
  4921. IF passByRegister & (registerNumber > 0) & (registerNumber < system.registerParameters) THEN (* allocated space for all registers *)
  4922. parametersSize := ToMemoryUnits(system,system.registerParameters*addressType.sizeInBits);
  4923. END;
  4924. size := IntermediateCode.Immediate(addressType,parametersSize);
  4925. Emit(Add(position,sp,sp,size));
  4926. END;
  4927. IF (resultDesignator = NIL) & (procedureType.returnType # NIL) THEN
  4928. IF structuredReturnType THEN
  4929. (* stack pointer rewinding done by callee
  4930. size := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOfParameter(procedureType.returnParameter)));
  4931. Emit(Add(position,sp,sp,size));
  4932. *)
  4933. RestoreRegisters(saved);
  4934. InitOperand(result,ModeReference);
  4935. Symbol(variable,result);
  4936. ELSE
  4937. RestoreRegisters(saved);
  4938. InitOperand(result,ModeValue);
  4939. result.op := return;
  4940. END;
  4941. END;
  4942. IF alignment > 1 THEN
  4943. Emit(Pop(position,sp));
  4944. END;
  4945. IF backend.cooperative & (procedureType.callingConvention = SyntaxTree.WinAPICallingConvention) THEN
  4946. Emit(Pop(position, ap));
  4947. END;
  4948. IF conditional & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  4949. ValueToCondition(result);
  4950. END;
  4951. destination := dest;
  4952. (* perform all write-back calls in the list *)
  4953. BackupGlobalState;
  4954. currentWriteBackCall := firstWriteBackCall;
  4955. WHILE currentWriteBackCall # NIL DO
  4956. VisitProcedureCallDesignator(currentWriteBackCall.call);
  4957. currentWriteBackCall := currentWriteBackCall.next
  4958. END;
  4959. RestoreGlobalState;
  4960. (* TENATIVE *)
  4961. (*
  4962. IF isOperatorCall THEN
  4963. IF dump # NIL THEN dump.String("*** end of operator call ***"); dump.Ln; dump.Update END
  4964. END;
  4965. *)
  4966. IF Trace THEN TraceExit("VisitProcedureCallDesignator") END;
  4967. END VisitProcedureCallDesignator;
  4968. PROCEDURE TypeDescriptorAdr(t: SyntaxTree.Type): IntermediateCode.Operand;
  4969. VAR res: IntermediateCode.Operand; offset: LONGINT; name: Basic.SegmentedName;
  4970. td: SyntaxTree.Symbol;
  4971. PROCEDURE GetHiddenPointerType(): SyntaxTree.Type;
  4972. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4973. BEGIN
  4974. IF (hiddenPointerType = NIL) OR (hiddenPointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4975. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4976. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4977. variable.SetType(system.anyType);
  4978. scope.AddVariable(variable);
  4979. hiddenPointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  4980. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@HdPtrDesc"));
  4981. typeDeclaration.SetDeclaredType(hiddenPointerType);
  4982. typeDeclaration.SetScope(module.module.moduleScope);
  4983. hiddenPointerType.SetTypeDeclaration(typeDeclaration);
  4984. hiddenPointerType.SetState(SyntaxTree.Resolved);
  4985. END;
  4986. RETURN hiddenPointerType;
  4987. END GetHiddenPointerType;
  4988. PROCEDURE GetDelegateType(): SyntaxTree.Type;
  4989. VAR scope: SyntaxTree.RecordScope; variable: SyntaxTree.Variable; typeDeclaration: SyntaxTree.TypeDeclaration;
  4990. BEGIN
  4991. IF (delegatePointerType = NIL) OR (delegatePointerType.typeDeclaration.scope.ownerModule # module.module) THEN
  4992. scope := SyntaxTree.NewRecordScope(module.module.moduleScope);
  4993. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Procedure"));
  4994. variable.SetType(SyntaxTree.NewProcedureType(-1,NIL));
  4995. scope.AddVariable(variable);
  4996. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier("@Any"));
  4997. variable.SetType(system.anyType);
  4998. scope.AddVariable(variable);
  4999. delegatePointerType := SyntaxTree.NewRecordType(-1,NIL,scope);
  5000. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier("@Delegate"));
  5001. typeDeclaration.SetDeclaredType(delegatePointerType);
  5002. typeDeclaration.SetScope(module.module.moduleScope);
  5003. delegatePointerType.SetTypeDeclaration(typeDeclaration);
  5004. delegatePointerType.SetState(SyntaxTree.Resolved);
  5005. END;
  5006. RETURN delegatePointerType
  5007. END GetDelegateType;
  5008. PROCEDURE GetBackendType(x: SyntaxTree.Type; VAR offset: LONGINT; VAR name: Basic.SegmentedName): SyntaxTree.Symbol;
  5009. (* create anonymous type declaration for types that need a type descriptor but have been declared anonymously
  5010. such as in VAR a: RECORD ... END;
  5011. reason: type desciptors in Sections are then accessible via a type declaration symbol and for types
  5012. and variables, constants and procedures the same mechanism can be used for fixups etc.
  5013. *)
  5014. VAR source: Sections.Section;null: HUGEINT; td: SyntaxTree.TypeDeclaration;
  5015. op: IntermediateCode.Operand; baseRecord: SyntaxTree.RecordType;
  5016. BEGIN (* no code emission *)
  5017. source := NIL;
  5018. x := x.resolved;
  5019. IF (x IS SyntaxTree.AnyType) OR (x IS SyntaxTree.PointerType) THEN
  5020. x := GetHiddenPointerType();
  5021. ELSIF IsDelegate(x) THEN
  5022. x := GetDelegateType();
  5023. ELSIF (x IS SyntaxTree.RecordType) OR (x IS SyntaxTree.CellType) THEN
  5024. ELSE HALT(200);
  5025. END;
  5026. td := x.typeDeclaration;
  5027. IF td = NIL THEN
  5028. ASSERT(x(SyntaxTree.RecordType).pointerType # NIL);
  5029. td := x(SyntaxTree.RecordType).pointerType.resolved.typeDeclaration;
  5030. ASSERT(td # NIL);
  5031. END;
  5032. IF newObjectFile THEN
  5033. GetCodeSectionNameForSymbol(td,name);
  5034. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  5035. meta.CheckTypeDeclaration(x);
  5036. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5037. ELSE
  5038. source := NewSection(module.importedSections, Sections.ConstSection,name,td,commentPrintout # NIL);
  5039. END;
  5040. IF backend.cooperative THEN
  5041. offset := 0;
  5042. ELSE
  5043. IF x IS SyntaxTree.CellType THEN
  5044. baseRecord := x(SyntaxTree.CellType).GetBaseRecord();
  5045. IF baseRecord = NIL THEN
  5046. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(0));
  5047. ELSE
  5048. offset := ToMemoryUnits(system, meta.GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*system.addressSize);
  5049. END;
  5050. ELSE
  5051. offset := ToMemoryUnits(system,meta.GetTypeRecordBaseOffset(x(SyntaxTree.RecordType).recordScope.numberMethods)*system.addressSize);
  5052. END;
  5053. END;
  5054. ELSE
  5055. offset := 0;
  5056. source := module.allSections.FindBySymbol(td); (*TODO*)
  5057. IF source = NIL THEN
  5058. null := 0;
  5059. GetCodeSectionNameForSymbol(td,name);
  5060. source := NewSection(module.allSections, Sections.ConstSection, name,td,commentPrintout # NIL);
  5061. IntermediateCode.InitImmediate(op,addressType,0);
  5062. source(IntermediateCode.Section).Emit(Data(position,op));
  5063. source.SetReferenced(FALSE)
  5064. ELSE
  5065. name := source.name;
  5066. END;
  5067. END;
  5068. RETURN td
  5069. END GetBackendType;
  5070. BEGIN
  5071. (*td := t.typeDeclaration;*)
  5072. td := GetBackendType(t,offset,name); (*! do not dereference a pointer here as the type descriptor for the pointer might be asked for *)
  5073. (*
  5074. IF t IS SyntaxTree.PointerType THEN
  5075. source := GetBackendType(t(SyntaxTree.PointerType).pointerBase.resolved);
  5076. ELSE
  5077. source := GetBackendType(t);
  5078. END;
  5079. *)
  5080. IF newObjectFile THEN
  5081. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0 (*
  5082. 1+t(SyntaxTree.RecordType).recordScope.numberMethods+16+1 *));
  5083. IntermediateCode.SetOffset(res,offset);
  5084. ELSE
  5085. IntermediateCode.InitAddress(res, addressType, name, GetFingerprint(td), 0);
  5086. END;
  5087. (*
  5088. IntermediateCode.MakeMemory(res,IntermediateCode.UnsignedInteger,addressType.sizeInBits);
  5089. make memory should be used when tag is used, not earlier
  5090. *)
  5091. RETURN res
  5092. END TypeDescriptorAdr;
  5093. (*
  5094. PROCEDURE MakeTypeTag(VAR operand: Operand);
  5095. VAR result: IntermediateCode.Operand;
  5096. BEGIN
  5097. IF operand.tag.mode = IntermediateCode.Undefined THEN
  5098. operand.tag := TypeDescriptorAdr(operand.type);
  5099. IntermediateCode.MakeMemory(operand.tag,addressType);
  5100. UseIntermediateOperand(operand.tag);
  5101. END;
  5102. END MakeTypeTag;
  5103. *)
  5104. PROCEDURE ProfilerInit;
  5105. VAR reg: IntermediateCode.Operand;
  5106. BEGIN
  5107. IntermediateCode.InitAddress(reg, addressType, profileInit.name , GetFingerprint(profileInit.symbol), 0);
  5108. Emit(Call(position,reg,0));
  5109. END ProfilerInit;
  5110. PROCEDURE ProfilerEnterExit(procedureNumber: LONGINT; enter: BOOLEAN);
  5111. VAR reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure;
  5112. BEGIN
  5113. IF enter & GetRuntimeProcedure("FoxProfiler","EnterProcedure",procedure,TRUE)
  5114. OR ~enter & GetRuntimeProcedure("FoxProfiler","ExitProcedure",procedure,TRUE)
  5115. THEN
  5116. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5117. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5118. Emit(Push(position,reg));
  5119. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5120. Emit(Push(position,reg));
  5121. StaticCallOperand(result,procedure);
  5122. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5123. ReleaseOperand(result);
  5124. END;
  5125. END ProfilerEnterExit;
  5126. PROCEDURE ProfilerAddProcedure(procedureNumber: LONGINT; CONST name: ARRAY OF CHAR);
  5127. VAR string: SyntaxTree.String; reg: IntermediateCode.Operand; result: Operand; procedure: SyntaxTree.Procedure; sv: SyntaxTree.StringValue;type: SyntaxTree.Type;
  5128. BEGIN
  5129. IF GetRuntimeProcedure("FoxProfiler","AddProcedure",procedure,TRUE) THEN
  5130. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5131. IntermediateCode.MakeMemory(reg, IntermediateCode.GetType(system,system.longintType));
  5132. profileInit.Emit(Push(position,reg));
  5133. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), procedureNumber);
  5134. profileInit.Emit(Push(position,reg));
  5135. NEW(string, LEN(name)); COPY(name, string^);
  5136. sv := SyntaxTree.NewStringValue(-1,string);
  5137. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5138. sv.SetType(type);
  5139. Designate(sv,result);
  5140. profileInit.Emit(Push(position,result.tag));
  5141. profileInit.Emit(Push(position,result.op));
  5142. StaticCallOperand(result,procedure);
  5143. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5144. ReleaseOperand(result);
  5145. END;
  5146. END ProfilerAddProcedure;
  5147. PROCEDURE ProfilerAddModule(CONST name: ARRAY OF CHAR);
  5148. VAR string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type; result: Operand; reg: IntermediateCode.Operand; procedure: SyntaxTree.Procedure;
  5149. BEGIN
  5150. IF GetRuntimeProcedure("FoxProfiler","AddModule",procedure,TRUE) THEN
  5151. IntermediateCode.InitAddress(reg, addressType, profileId.name , GetFingerprint(profileId.symbol), 0);
  5152. profileInit.Emit(Push(position,reg));
  5153. profileInitPatchPosition := profileInit.pc;
  5154. profileInit.Emit(Nop(position)); (* placeholder, will be patched by number of procedures *)
  5155. NEW(string, LEN(name)); COPY(name, string^);
  5156. sv := SyntaxTree.NewStringValue(-1,string);
  5157. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(name));
  5158. sv.SetType(type);
  5159. Designate(sv,result);
  5160. profileInit.Emit(Push(position,result.tag));
  5161. profileInit.Emit(Push(position,result.op));
  5162. StaticCallOperand(result,procedure);
  5163. profileInit.Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  5164. ReleaseOperand(result);
  5165. END;
  5166. END ProfilerAddModule;
  5167. PROCEDURE ProfilerPatchInit;
  5168. VAR reg: IntermediateCode.Operand;
  5169. BEGIN
  5170. IntermediateCode.InitImmediate(reg, IntermediateCode.GetType(system,system.longintType), numberProcedures);
  5171. profileInit.EmitAt(profileInitPatchPosition,Push(position,reg));
  5172. EmitLeave(profileInit,position,0);
  5173. profileInit.Emit(Exit(position,0,0));
  5174. END ProfilerPatchInit;
  5175. (** if operator can be overloaded dynamically, emit code that registers it in the runtime **)
  5176. PROCEDURE RegisterDynamicOperator(operator: SyntaxTree.Operator);
  5177. VAR
  5178. id: LONGINT;
  5179. leftType, rightType: SyntaxTree.Type;
  5180. procedureType: SyntaxTree.ProcedureType;
  5181. runtimeProcedure: SyntaxTree.Procedure;
  5182. runtimeProcedureOperand, operatorOperand: Operand;
  5183. kind: SET;
  5184. PROCEDURE PushTypeInfo(type: SyntaxTree.Type);
  5185. VAR
  5186. arg: IntermediateCode.Operand;
  5187. recordType: SyntaxTree.RecordType;
  5188. fingerPrint: SyntaxTree.FingerPrint;
  5189. BEGIN
  5190. IF type = NIL THEN
  5191. (* no type: push 'NoType' *)
  5192. (* IF dump # NIL THEN dump.String("push 'NoType'"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5193. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), NoType)
  5194. ELSIF IsStrictlyPointerToRecord(type) THEN
  5195. (* pointer to record type: push typetag *)
  5196. (* IF dump # NIL THEN dump.String("push typetag"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5197. recordType := type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  5198. arg := TypeDescriptorAdr(recordType);
  5199. IF ~newObjectFile THEN IntermediateCode.MakeMemory(arg, addressType) END;
  5200. ELSE
  5201. (* non-pointer to record type: push fingerprint *)
  5202. (* IF dump # NIL THEN dump.String("push fingerprint"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5203. fingerPrint := fingerPrinter.TypeFP(type.resolved);
  5204. arg := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), fingerPrint.shallow)
  5205. END;
  5206. operatorInitializationCodeSection.Emit(Push(position,arg))
  5207. END PushTypeInfo;
  5208. BEGIN
  5209. ASSERT(operatorInitializationCodeSection # NIL);
  5210. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5211. procedureType := operator.type(SyntaxTree.ProcedureType);
  5212. (* determine types *)
  5213. leftType := procedureType.firstParameter.type;
  5214. IF procedureType.numberParameters = 2 THEN
  5215. ASSERT(procedureType.firstParameter.nextParameter # NIL);
  5216. rightType := procedureType.firstParameter.nextParameter.type;
  5217. ELSE
  5218. rightType := NIL
  5219. END;
  5220. (* determine operator kind *)
  5221. IF IsStrictlyPointerToRecord(leftType) THEN
  5222. kind := {LhsIsPointer}
  5223. ELSE
  5224. kind := {}
  5225. END;
  5226. IF IsStrictlyPointerToRecord(rightType) THEN
  5227. kind := kind + {RhsIsPointer}
  5228. END;
  5229. IF kind # {} THEN (* TODO: to be removed later on *)
  5230. (* at least one of the types is a pointer to record *)
  5231. (* emit a code that registers this specific operator in the runtime *)
  5232. dump := operatorInitializationCodeSection.comments;
  5233. (* IF dump # NIL THEN dump.String("*** begin of operator registration ***"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5234. IF GetRuntimeProcedure("FoxOperatorRuntime", "RegisterOperator", runtimeProcedure, TRUE) THEN
  5235. (* push ID *)
  5236. (* IF dump # NIL THEN dump.String("push ID"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5237. id := Global.GetSymbol(module.module.case, operator.name);
  5238. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType), id)));
  5239. (* push kind *)
  5240. (* IF dump # NIL THEN dump.String("push kind"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5241. operatorInitializationCodeSection.Emit(Push(position,IntermediateCode.Immediate(setType, SYSTEM.VAL(LONGINT, kind))));
  5242. (* push type infos *)
  5243. PushTypeInfo(leftType);
  5244. PushTypeInfo(rightType);
  5245. (* push operator address *)
  5246. (* IF dump # NIL THEN dump.String("push operator address"); dump.Ln; dump.Update END; *) (* TENTATIVE *)
  5247. StaticCallOperand(operatorOperand, operator);
  5248. operatorInitializationCodeSection.Emit(Push(position,operatorOperand.op));
  5249. ReleaseOperand(operatorOperand);
  5250. StaticCallOperand(runtimeProcedureOperand, runtimeProcedure);
  5251. operatorInitializationCodeSection.Emit(Call(position,runtimeProcedureOperand.op, ProcedureParametersSize(system, runtimeProcedure)));
  5252. ReleaseOperand(runtimeProcedureOperand)
  5253. END
  5254. (* IF dump # NIL THEN dump.String("*** end of operator registration ***"); dump.Ln; dump.Update END *) (* TENTATIVE *)
  5255. END
  5256. END RegisterDynamicOperator;
  5257. PROCEDURE SystemTrace(x: SyntaxTree.ExpressionList; pos: LONGINT);
  5258. VAR
  5259. traceModule: SyntaxTree.Module;
  5260. procedure: SyntaxTree.Procedure;
  5261. procedureVariable: SyntaxTree.Variable;
  5262. s,msg: Basic.MessageString;
  5263. res: Operand;
  5264. i: LONGINT;
  5265. sv: SyntaxTree.StringValue;
  5266. type: SyntaxTree.Type;
  5267. recordType: SyntaxTree.RecordType;
  5268. printout: Printout.Printer;
  5269. stringWriter: Streams.StringWriter;
  5270. expression: SyntaxTree.Expression;
  5271. PROCEDURE GetProcedure(CONST procedureName: ARRAY OF CHAR): BOOLEAN;
  5272. BEGIN
  5273. procedure := traceModule.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  5274. IF procedure = NIL THEN
  5275. procedureVariable := traceModule.moduleScope.FindVariable(SyntaxTree.NewIdentifier(procedureName));
  5276. END;
  5277. IF (procedure = NIL) & (procedureVariable = NIL) THEN
  5278. s := "procedure ";
  5279. Strings.Append(s,backend.traceModuleName);
  5280. Strings.Append(s,".");
  5281. Strings.Append(s,procedureName);
  5282. Strings.Append(s," not present");
  5283. Error(position,s);
  5284. RETURN FALSE
  5285. ELSE
  5286. RETURN TRUE
  5287. END;
  5288. END GetProcedure;
  5289. PROCEDURE CallProcedure;
  5290. VAR size: LONGINT;
  5291. BEGIN
  5292. IF procedure # NIL THEN
  5293. StaticCallOperand(result,procedure);
  5294. size := ProcedureParametersSize(system,procedure);
  5295. ELSE
  5296. Symbol(procedureVariable, result);
  5297. LoadValue(result, procedureVariable.type.resolved);
  5298. size := ParametersSize(system, procedureVariable.type.resolved(SyntaxTree.ProcedureType), FALSE);
  5299. END;
  5300. Emit(Call(position,result.op,size));
  5301. END CallProcedure;
  5302. PROCEDURE String(CONST s: ARRAY OF CHAR);
  5303. VAR res: Operand; string: SyntaxTree.String;
  5304. BEGIN
  5305. IF GetProcedure("String") THEN
  5306. NEW(string, LEN(s)); COPY(s, string^);
  5307. sv := SyntaxTree.NewStringValue(-1,string);
  5308. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5309. sv.SetType(type);
  5310. Designate(sv,res);
  5311. Emit(Push(position,res.tag));
  5312. Emit(Push(position,res.op));
  5313. ReleaseOperand(res);
  5314. CallProcedure;
  5315. END;
  5316. END String;
  5317. PROCEDURE Integer(op: IntermediateCode.Operand);
  5318. BEGIN
  5319. IF GetProcedure("Int") THEN
  5320. Emit(Push(position,op));
  5321. Emit(Push(position,IntermediateCode.Immediate(int32,1)));
  5322. CallProcedure;
  5323. END;
  5324. END Integer;
  5325. PROCEDURE Float(op: IntermediateCode.Operand);
  5326. BEGIN
  5327. IF GetProcedure("HIntHex") THEN
  5328. Emit(Push(position,op));
  5329. Emit(Push(position,IntermediateCode.Immediate(int32,16)));
  5330. CallProcedure;
  5331. END;
  5332. END Float;
  5333. PROCEDURE Set(op: IntermediateCode.Operand);
  5334. BEGIN
  5335. IF GetProcedure("Set") THEN
  5336. Emit(Push(position,op));
  5337. (*
  5338. Emit(Push(position,IntermediateCode.Immediate(int32,0))); (* ofs *)
  5339. Emit(Push(position,IntermediateCode.Immediate(int32,32))); (* n *)
  5340. *)
  5341. CallProcedure;
  5342. END;
  5343. END Set;
  5344. PROCEDURE Boolean(op: IntermediateCode.Operand);
  5345. BEGIN
  5346. IF GetProcedure("Boolean") THEN
  5347. Emit(Push(position,op));
  5348. CallProcedure;
  5349. END;
  5350. END Boolean;
  5351. PROCEDURE Char(op: IntermediateCode.Operand);
  5352. BEGIN
  5353. IF GetProcedure("Char") THEN
  5354. Emit(Push(position,op));
  5355. CallProcedure;
  5356. END;
  5357. END Char;
  5358. PROCEDURE Address(op: IntermediateCode.Operand);
  5359. BEGIN
  5360. IF GetProcedure("Address") THEN
  5361. Emit(Push(position,op));
  5362. CallProcedure;
  5363. END;
  5364. END Address;
  5365. PROCEDURE StringOperand(op,tag: IntermediateCode.Operand);
  5366. BEGIN
  5367. IF GetProcedure("String") THEN
  5368. Emit(Push(position,tag));
  5369. Emit(Push(position,op));
  5370. CallProcedure;
  5371. END;
  5372. END StringOperand;
  5373. PROCEDURE Ln;
  5374. BEGIN
  5375. IF GetProcedure("Ln") THEN
  5376. CallProcedure;
  5377. END;
  5378. END Ln;
  5379. BEGIN
  5380. IF backend.traceModuleName = "" THEN RETURN END;
  5381. IF AddImport(backend.traceModuleName,traceModule,TRUE) THEN
  5382. IF GetProcedure("Enter") THEN
  5383. CallProcedure
  5384. END;
  5385. NEW(stringWriter,LEN(s));
  5386. FOR i := 0 TO x.Length()-1 DO
  5387. msg := "";
  5388. expression := x.GetExpression(i);
  5389. IF currentScope IS SyntaxTree.ProcedureScope THEN
  5390. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, s)
  5391. ELSE
  5392. Global.GetModuleName(module.module, s);
  5393. END;
  5394. IF i = 0 THEN
  5395. stringWriter.String(s); stringWriter.String("@"); stringWriter.Int(pos,1);
  5396. stringWriter.String(":");
  5397. END;
  5398. printout := Printout.NewPrinter(stringWriter,Printout.SourceCode,FALSE);
  5399. IF ~(expression IS SyntaxTree.StringValue) THEN
  5400. printout.Expression(expression);
  5401. stringWriter.Get(s);
  5402. Strings.Append(msg,s);
  5403. Strings.Append(msg,"= ");
  5404. ELSE stringWriter.Get(s); (* remove from string writer *)
  5405. Strings.Append(msg, s);
  5406. END;
  5407. String(msg);
  5408. IF SemanticChecker.IsStringType(expression.type) THEN
  5409. Designate(expression,res);
  5410. StringOperand(res.op,res.tag);
  5411. ELSE
  5412. Evaluate(expression,res);
  5413. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  5414. IF res.op.type.sizeInBits < IntermediateCode.Bits64 THEN
  5415. Convert(res.op,int64);
  5416. END;
  5417. Integer(res.op);
  5418. ELSIF expression.type.resolved IS SyntaxTree.BooleanType THEN
  5419. Boolean(res.op);
  5420. ELSIF expression.type.resolved IS SyntaxTree.SetType THEN
  5421. Set(res.op);
  5422. ELSIF expression.type.resolved IS SyntaxTree.FloatType THEN
  5423. IF res.op.type.sizeInBits = IntermediateCode.Bits32 THEN
  5424. Convert(res.op,float64);
  5425. END;
  5426. Float(res.op);
  5427. ELSIF (expression.type.resolved IS SyntaxTree.CharacterType) & (expression.type.resolved.sizeInBits = 8) THEN
  5428. Char(res.op);
  5429. ELSIF expression.type.resolved IS SyntaxTree.AddressType THEN
  5430. Address(res.op);String("H");
  5431. ELSIF expression.type.resolved IS SyntaxTree.SizeType THEN
  5432. Address(res.op);String("H");
  5433. ELSIF (expression.type.resolved IS SyntaxTree.PointerType) OR IsPointerToRecord(expression.type,recordType) THEN
  5434. Address(res.op);String("H");
  5435. ELSIF (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  5436. Address(res.op);String("H");
  5437. ELSIF expression.type.resolved IS SyntaxTree.NilType THEN
  5438. String("NIL");
  5439. ELSE HALT(200);
  5440. END;
  5441. END;
  5442. ReleaseOperand(res);
  5443. String("; ");
  5444. END;
  5445. IF GetProcedure("Exit") THEN
  5446. CallProcedure
  5447. ELSE
  5448. Ln;
  5449. END;
  5450. END;
  5451. END SystemTrace;
  5452. PROCEDURE InitFields(type: SyntaxTree.Type; CONST adr: IntermediateCode.Operand; offset: LONGINT);
  5453. VAR baseType: SyntaxTree.Type; imm,mem: IntermediateCode.Operand; dim,size: LONGINT;
  5454. variable: SyntaxTree.Variable; i: LONGINT; initializerOp: Operand;
  5455. BEGIN
  5456. type := type.resolved;
  5457. IF type IS SyntaxTree.RecordType THEN
  5458. WITH type: SyntaxTree.RecordType DO
  5459. baseType := type.baseType;
  5460. IF baseType # NIL THEN
  5461. baseType := baseType.resolved;
  5462. IF baseType IS SyntaxTree.PointerType THEN baseType := baseType(SyntaxTree.PointerType).pointerBase END;
  5463. InitFields(baseType,adr,offset);
  5464. END;
  5465. variable := type.recordScope.firstVariable;
  5466. WHILE variable # NIL DO
  5467. IF variable.initializer # NIL THEN
  5468. Evaluate(variable.initializer,initializerOp);
  5469. MakeMemory(mem,adr,IntermediateCode.GetType(system,variable.type),offset+ ToMemoryUnits(system,variable.offsetInBits));
  5470. Emit(Mov(position,mem,initializerOp.op));
  5471. ReleaseOperand(initializerOp);
  5472. ReleaseIntermediateOperand(mem);
  5473. END;
  5474. InitFields(variable.type, adr, offset+ ToMemoryUnits(system,variable.offsetInBits));
  5475. variable := variable.nextVariable
  5476. END;
  5477. END;
  5478. ELSIF (type IS SyntaxTree.ArrayType) THEN
  5479. WITH type: SyntaxTree.ArrayType DO
  5480. IF type.form = SyntaxTree.Static THEN
  5481. baseType := type.arrayBase;
  5482. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5483. FOR i := 0 TO type.staticLength-1 DO
  5484. InitFields(baseType,adr,offset+i*size);
  5485. END;
  5486. END;
  5487. END;
  5488. ELSIF type IS SyntaxTree.MathArrayType THEN
  5489. WITH type: SyntaxTree.MathArrayType DO
  5490. IF type.form = SyntaxTree.Open THEN
  5491. dim := DynamicDim(type);
  5492. imm := IntermediateCode.Immediate(addressType,dim);
  5493. PutMathArrayFieldOffset(adr,imm,MathDimOffset,offset);
  5494. baseType := SemanticChecker.ArrayBase(type,dim);
  5495. IF baseType = NIL THEN size := 0 ELSE size := system.AlignedSizeOf(baseType) END;
  5496. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,size));
  5497. PutMathArrayFieldOffset(adr,imm,MathElementSizeOffset,offset);
  5498. ReleaseIntermediateOperand(imm);
  5499. (* flags remain empty (=0) for open array *)
  5500. ELSIF type.form = SyntaxTree.Static THEN
  5501. baseType := type.arrayBase;
  5502. size := ToMemoryUnits(system,system.AlignedSizeOf(baseType));
  5503. FOR i := 0 TO type.staticLength-1 DO
  5504. InitFields(baseType,adr,offset+i*size);
  5505. END;
  5506. END;
  5507. END;
  5508. END;
  5509. END InitFields;
  5510. PROCEDURE InitVariable(VAR variable: SyntaxTree.Variable);
  5511. VAR type: SyntaxTree.Type; operand: Operand; tmp: IntermediateCode.Operand; reference: SyntaxTree.Expression;
  5512. BEGIN
  5513. type := variable.type.resolved;
  5514. IF (type IS SyntaxTree.MathArrayType) THEN
  5515. WITH type: SyntaxTree.MathArrayType DO
  5516. IF type.form = SyntaxTree.Open THEN
  5517. Symbol(variable,operand);
  5518. InitFields(type, operand.tag,0);
  5519. ELSIF type.form = SyntaxTree.Tensor THEN
  5520. Symbol(variable, operand);
  5521. MakeMemory(tmp,operand.op,addressType,0);
  5522. ReleaseOperand(operand);
  5523. Emit(Mov(position,tmp, nil ) );
  5524. ReleaseIntermediateOperand(tmp);
  5525. END;
  5526. END;
  5527. ELSE
  5528. Symbol(variable,operand);
  5529. IF variable.initializer # NIL THEN
  5530. reference := SyntaxTree.NewSymbolDesignator(variable.initializer.position,NIL,variable);
  5531. reference.SetType(variable.type.resolved);
  5532. reference.SetAssignable(TRUE);
  5533. Assign(reference,variable.initializer);
  5534. END;
  5535. InitFields(type, operand.op,0);
  5536. ReleaseOperand(operand);
  5537. END;
  5538. END InitVariable;
  5539. PROCEDURE MathArrayDim(type: SyntaxTree.MathArrayType; CONST base: IntermediateCode.Operand; VAR result: Operand);
  5540. VAR end: Label;
  5541. BEGIN
  5542. IF type.form = SyntaxTree.Tensor THEN
  5543. InitOperand(result,ModeValue);
  5544. ReuseCopy(result.op,base);
  5545. end := NewLabel();
  5546. BreqL(end,result.op,IntermediateCode.Immediate(addressType,0));
  5547. Emit(MovReplace(position,result.op,IntermediateCode.Memory(addressType,result.op,ToMemoryUnits(system,MathDimOffset*addressType.sizeInBits))));
  5548. SetLabel(end);
  5549. Convert(result.op,int32);
  5550. ELSE
  5551. InitOperand(result,ModeValue);
  5552. IntermediateCode.InitImmediate(result.op, int32, SemanticChecker.Dimension(type,{SyntaxTree.Open, SyntaxTree.Static}));
  5553. END
  5554. END MathArrayDim;
  5555. PROCEDURE PutMathArrayField(base,value: IntermediateCode.Operand; fieldOffset: LONGINT);
  5556. VAR mem: IntermediateCode.Operand; offset: LONGINT;
  5557. BEGIN
  5558. offset := ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5559. MakeMemory(mem,base,addressType,offset);
  5560. Emit(Mov(position,mem,value));
  5561. ReleaseIntermediateOperand(mem);
  5562. END PutMathArrayField;
  5563. PROCEDURE PutMathArrayFieldOffset(base,value: IntermediateCode.Operand; fieldOffset, offset: LONGINT);
  5564. VAR mem: IntermediateCode.Operand;
  5565. BEGIN
  5566. offset := offset + ToMemoryUnits(system,fieldOffset*addressType.sizeInBits);
  5567. MakeMemory(mem,base,addressType,offset);
  5568. Emit(Mov(position,mem,value));
  5569. ReleaseIntermediateOperand(mem);
  5570. END PutMathArrayFieldOffset;
  5571. PROCEDURE GetMathArrayField(VAR value: IntermediateCode.Operand; base: IntermediateCode.Operand; offset: LONGINT);
  5572. BEGIN
  5573. offset := ToMemoryUnits(system,offset*addressType.sizeInBits);
  5574. MakeMemory(value,base,addressType,offset);
  5575. END GetMathArrayField;
  5576. PROCEDURE PutMathArrayLenOrIncr(CONST base,value,dim: IntermediateCode.Operand; incr: BOOLEAN);
  5577. VAR offset: LONGINT; reg,mem: IntermediateCode.Operand;
  5578. BEGIN
  5579. IF incr THEN
  5580. offset := ToMemoryUnits(system,MathIncrOffset*addressType.sizeInBits);
  5581. ELSE
  5582. offset := ToMemoryUnits(system,MathLenOffset*addressType.sizeInBits);
  5583. END;
  5584. IF dim.mode=IntermediateCode.ModeImmediate THEN
  5585. PutMathArrayField(base,value,offset + ToMemoryUnits(system,SHORT(dim.intValue) * 2 * addressType.sizeInBits));
  5586. ELSE
  5587. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5588. Emit(Mov(position,reg,dim));
  5589. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,2*addressType.sizeInBits))));
  5590. Emit(Add(position,reg,reg,base));
  5591. MakeMemory(mem, reg, addressType, offset);
  5592. ReleaseIntermediateOperand(reg);
  5593. Emit(Mov(position,mem,value));
  5594. ReleaseIntermediateOperand(mem);
  5595. END;
  5596. END PutMathArrayLenOrIncr;
  5597. PROCEDURE PutMathArrayLength(base,value: IntermediateCode.Operand; dim: LONGINT);
  5598. BEGIN
  5599. PutMathArrayField(base,value,MathLenOffset + dim * 2);
  5600. END PutMathArrayLength;
  5601. PROCEDURE PutMathArrayIncrement(base,value: IntermediateCode.Operand; dim: LONGINT);
  5602. BEGIN
  5603. PutMathArrayField(base,value,MathIncrOffset + dim * 2);
  5604. END PutMathArrayIncrement;
  5605. PROCEDURE GetMathArrayIncrement(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5606. BEGIN
  5607. MathArrayLenOrIncr(type,operand,dim,TRUE,check,result);
  5608. END GetMathArrayIncrement;
  5609. PROCEDURE GetMathArrayLength(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; check: BOOLEAN; VAR result: Operand);
  5610. BEGIN
  5611. MathArrayLenOrIncr(type,operand,dim,FALSE,check,result);
  5612. END GetMathArrayLength;
  5613. PROCEDURE GetMathArrayLengthAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5614. VAR dimOp: IntermediateCode.Operand;
  5615. BEGIN
  5616. dimOp := IntermediateCode.Immediate(int32, dim);
  5617. GetMathArrayLength(type, operand, dimOp, check, result);
  5618. END GetMathArrayLengthAt;
  5619. PROCEDURE GetMathArrayIncrementAt(type: SyntaxTree.MathArrayType; CONST operand: Operand; dim: LONGINT; check: BOOLEAN; VAR result: Operand);
  5620. VAR dimOp: IntermediateCode.Operand;
  5621. BEGIN
  5622. dimOp := IntermediateCode.Immediate(int32, dim);
  5623. GetMathArrayIncrement(type, operand, dimOp, check, result);
  5624. END GetMathArrayIncrementAt;
  5625. PROCEDURE MathArrayLenOrIncr(type: SyntaxTree.MathArrayType; CONST operand: Operand; VAR dim: IntermediateCode.Operand; increment: BOOLEAN; check: BOOLEAN; VAR result: Operand );
  5626. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5627. offset: LONGINT;
  5628. BEGIN
  5629. IF increment THEN
  5630. offset := MathIncrOffset;
  5631. ELSE
  5632. offset := MathLenOffset;
  5633. END;
  5634. INC(offset,operand.dimOffset*2);
  5635. IF check & (type.form = SyntaxTree.Tensor) & ~isUnchecked THEN
  5636. TrapC(BrneL,operand.tag,IntermediateCode.Immediate(addressType,0),IndexCheckTrap);
  5637. END;
  5638. (* static dimension *)
  5639. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5640. IF check & (type.form = SyntaxTree.Tensor) THEN
  5641. DimensionCheck(operand.tag,dim,BrltL);
  5642. END;
  5643. val := SHORT(dim.intValue);
  5644. IF type.form # SyntaxTree.Tensor THEN
  5645. t := SemanticChecker.ArrayBase(type,val);
  5646. type := t.resolved(SyntaxTree.MathArrayType);
  5647. IF type.form = SyntaxTree.Static THEN
  5648. IF increment THEN
  5649. res := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,type.staticIncrementInBits));
  5650. ELSE
  5651. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5652. END;
  5653. InitOperand(result,ModeValue);
  5654. result.op := res;
  5655. RETURN;
  5656. END;
  5657. END;
  5658. offset := ToMemoryUnits(system, (val*2+offset)*addressType.sizeInBits);
  5659. MakeMemory(res,operand.tag,addressType,offset);
  5660. (*
  5661. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5662. *)
  5663. InitOperand(result,ModeValue);
  5664. result.op := res;
  5665. ELSE
  5666. Convert(dim,addressType);
  5667. IF check THEN
  5668. IF type.form = SyntaxTree.Tensor THEN
  5669. DimensionCheck(operand.tag,dim,BrltL);
  5670. ELSIF isUnchecked THEN (* do nothing *)
  5671. ELSE
  5672. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5673. END;
  5674. END;
  5675. end := NewLabel(); next := NIL;
  5676. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5677. Emit(Mov(position,res,dim));
  5678. Convert(res,int32);
  5679. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5680. WHILE t IS SyntaxTree.MathArrayType DO
  5681. type := t(SyntaxTree.MathArrayType);
  5682. IF type.form = SyntaxTree.Static THEN
  5683. imm := IntermediateCode.Immediate(int32,val);
  5684. next := NewLabel();
  5685. BrneL(next,imm,res);
  5686. IF increment THEN
  5687. imm := IntermediateCode.Immediate(int32,ToMemoryUnits(system,type.staticIncrementInBits));
  5688. ELSE
  5689. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5690. END;
  5691. Emit(MovReplace(position,res,imm));
  5692. BrL(end);
  5693. ELSE hasDynamicPart := TRUE;
  5694. END;
  5695. t := type.arrayBase.resolved;
  5696. val := val + 1;
  5697. IF next # NIL THEN SetLabel(next) END;
  5698. END;
  5699. IF hasDynamicPart THEN
  5700. IntermediateCode.InitRegister(res2,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  5701. Emit(Mov(position,res2,dim));
  5702. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,2*ToMemoryUnits(system,addressType.sizeInBits))));
  5703. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5704. Emit(Add(position,res2,res2,imm));
  5705. Emit(Add(position,res2,res2,operand.tag));
  5706. IntermediateCode.MakeMemory(res2,int32);
  5707. Emit(MovReplace(position,res,res2));
  5708. ReleaseIntermediateOperand(res2);
  5709. END;
  5710. SetLabel(end);
  5711. Convert(res,int32);
  5712. InitOperand(result,ModeValue);
  5713. result.op := res;
  5714. END;
  5715. END MathArrayLenOrIncr;
  5716. PROCEDURE ArrayLen(type: SyntaxTree.ArrayType; VAR operand: Operand; VAR dim: IntermediateCode.Operand; VAR result: Operand );
  5717. VAR val: LONGINT; res,res2: IntermediateCode.Operand; end,next: Label; t: SyntaxTree.Type; imm: IntermediateCode.Operand; hasDynamicPart: BOOLEAN;
  5718. offset: LONGINT;
  5719. BEGIN
  5720. offset := operand.dimOffset+DynamicDim(type)-1;
  5721. IF dim.mode = IntermediateCode.ModeImmediate THEN
  5722. ASSERT(type.form IN {SyntaxTree.Open, SyntaxTree.SemiDynamic});
  5723. val := SHORT(dim.intValue);
  5724. t := SemanticChecker.ArrayBase(type,val);
  5725. type := t.resolved(SyntaxTree.ArrayType);
  5726. IF type.form = SyntaxTree.Static THEN
  5727. res := IntermediateCode.Immediate(addressType,type.staticLength);
  5728. ELSE
  5729. offset := ToMemoryUnits(system, (offset-val)*addressType.sizeInBits);
  5730. res := IntermediateCode.Memory(addressType,operand.tag,offset);
  5731. END;
  5732. UseIntermediateOperand(res);
  5733. InitOperand(result,ModeValue);
  5734. result.op := res;
  5735. ELSE
  5736. Convert(dim,addressType);
  5737. IF ~isUnchecked THEN
  5738. TrapC(BrltL,dim,IntermediateCode.Immediate(addressType,SemanticChecker.Dimension(type,{SyntaxTree.Open,SyntaxTree.Static})), IndexCheckTrap);
  5739. END;
  5740. end := NewLabel(); next := NIL;
  5741. (* ReuseCopy(dim,res); *)
  5742. IntermediateCode.InitRegister(res,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType, IntermediateCode.GeneralPurposeRegister));
  5743. Emit(Mov(position,res,dim));
  5744. Convert(res,int32);
  5745. Convert(res,int32);
  5746. t := type; val := operand.dimOffset; hasDynamicPart := FALSE;
  5747. WHILE t IS SyntaxTree.ArrayType DO
  5748. type := t(SyntaxTree.ArrayType);
  5749. IF type.form = SyntaxTree.Static THEN
  5750. imm := IntermediateCode.Immediate(int32,val);
  5751. next := NewLabel();
  5752. BrneL(next,imm,res);
  5753. imm := IntermediateCode.Immediate(int32,type.staticLength);
  5754. Emit(MovReplace(position,res,imm));
  5755. BrL(end);
  5756. ELSE hasDynamicPart := TRUE;
  5757. END;
  5758. t := type.arrayBase.resolved;
  5759. val := val + 1;
  5760. IF next # NIL THEN SetLabel(next) END;
  5761. END;
  5762. IF hasDynamicPart THEN
  5763. ReuseCopy(res2,dim); (* dim is now in register res2 *)
  5764. Convert(res2,addressType);
  5765. Emit(Mul(position,res2,res2,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,addressType.sizeInBits))));
  5766. imm := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,offset*addressType.sizeInBits));
  5767. Emit(Sub(position,res2,imm,res2));
  5768. Emit(Add(position,res2,res2,operand.tag));
  5769. IntermediateCode.MakeMemory(res2,int32);
  5770. Emit(MovReplace(position,res,res2));
  5771. ReleaseIntermediateOperand(res2);
  5772. END;
  5773. SetLabel(end);
  5774. Convert(res,int32);
  5775. InitOperand(result,ModeValue);
  5776. result.op := res;
  5777. END;
  5778. END ArrayLen;
  5779. (**
  5780. create a temporary variable in current scope
  5781. **)
  5782. PROCEDURE GetTemporaryVariable(type: SyntaxTree.Type; register: BOOLEAN): SyntaxTree.Variable;
  5783. VAR name: SyntaxTree.Identifier; string: SyntaxTree.IdentifierString ; variable: SyntaxTree.Variable;
  5784. scope: SyntaxTree.Scope; duplicate: BOOLEAN; offset, index: LONGINT;
  5785. BEGIN
  5786. IF ~register THEN
  5787. variable := temporaries.GetFreeVariable(type, index);
  5788. ELSE
  5789. index := temporaries.registerIndex; INC(temporaries.registerIndex);
  5790. END;
  5791. scope := currentScope;
  5792. IF variable = NIL THEN
  5793. COPY("@hiddenIRVar",string);
  5794. Basic.AppendNumber(string,index);
  5795. name := SyntaxTree.NewIdentifier(string);
  5796. variable := SyntaxTree.NewVariable(Diagnostics.Invalid,name);
  5797. variable.SetType(type);
  5798. variable.SetAccess(SyntaxTree.Hidden);
  5799. IF ~register THEN
  5800. temporaries.AddVariable(variable);
  5801. IF scope.lastVariable # NIL THEN
  5802. offset := scope.lastVariable.offsetInBits;
  5803. ELSE
  5804. offset := 0;
  5805. END;
  5806. DEC(offset,system.SizeOf(variable.type));
  5807. Basic.Align(offset,-system.AlignmentOf(system.variableAlignment,variable.type));
  5808. variable(SyntaxTree.Variable).SetOffset(offset);
  5809. scope.AddVariable(variable(SyntaxTree.Variable));
  5810. scope.EnterSymbol(variable, duplicate);
  5811. ASSERT(~duplicate);
  5812. InitVariable(variable(SyntaxTree.Variable));
  5813. ELSE
  5814. variable.SetUseRegister(TRUE);
  5815. variable(SyntaxTree.Variable).SetOffset(0);
  5816. END;
  5817. ELSE
  5818. InitVariable(variable(SyntaxTree.Variable));
  5819. (*
  5820. ASSERT(variable.type.resolved = type.resolved)
  5821. *)
  5822. END;
  5823. RETURN variable(SyntaxTree.Variable)
  5824. END GetTemporaryVariable;
  5825. PROCEDURE GetMathArrayDescriptorType(dimensions: LONGINT): SyntaxTree.Type;
  5826. VAR name: ARRAY 32 OF CHAR; symbol: SyntaxTree.Symbol; typeDeclaration: SyntaxTree.TypeDeclaration;
  5827. recordType: SyntaxTree.RecordType; type: SyntaxTree.Type;
  5828. recordScope: SyntaxTree.RecordScope; parentScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  5829. i: LONGINT; duplicate: BOOLEAN;
  5830. PROCEDURE AddVariable(CONST name: ARRAY OF CHAR; type: SyntaxTree.Type);
  5831. VAR variable: SyntaxTree.Variable;
  5832. BEGIN
  5833. variable := SyntaxTree.NewVariable(-1,SyntaxTree.NewIdentifier(name));
  5834. variable.SetType(type);
  5835. recordScope.AddVariable(variable);
  5836. END AddVariable;
  5837. BEGIN
  5838. name := "@ArrayDescriptor";
  5839. Basic.AppendNumber(name,dimensions);
  5840. identifier := SyntaxTree.NewIdentifier(name);
  5841. parentScope := module.module.moduleScope;
  5842. symbol := parentScope.FindSymbol(identifier);
  5843. IF symbol # NIL THEN
  5844. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  5845. type := typeDeclaration.declaredType;
  5846. ELSE
  5847. typeDeclaration := SyntaxTree.NewTypeDeclaration(-1,SyntaxTree.NewIdentifier(name));
  5848. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  5849. recordScope := SyntaxTree.NewRecordScope(parentScope);
  5850. recordType := SyntaxTree.NewRecordType( -1, parentScope, recordScope);
  5851. recordType.SetTypeDeclaration(typeDeclaration);
  5852. recordType.SetState(SyntaxTree.Resolved);
  5853. typeDeclaration.SetDeclaredType(recordType);
  5854. AddVariable("@ptr",system.anyType);
  5855. AddVariable("@adr",system.addressType);
  5856. AddVariable("@flags",system.addressType);
  5857. AddVariable("@dim",system.addressType);
  5858. AddVariable("@elementSize",system.addressType);
  5859. FOR i := 0 TO dimensions-1 DO
  5860. name := "@len";
  5861. Basic.AppendNumber(name,i);
  5862. AddVariable(name,system.addressType);
  5863. name := "@incr";
  5864. Basic.AppendNumber(name,i);
  5865. AddVariable(name,system.addressType);
  5866. END;
  5867. parentScope.AddTypeDeclaration(typeDeclaration);
  5868. parentScope.EnterSymbol(typeDeclaration,duplicate);
  5869. ASSERT(~duplicate);
  5870. type := recordType;
  5871. END;
  5872. RETURN type
  5873. END GetMathArrayDescriptorType;
  5874. PROCEDURE NewMathArrayDescriptor(op: Operand; dimensions: LONGINT);
  5875. VAR reg: IntermediateCode.Operand; type: SyntaxTree.Type;
  5876. BEGIN
  5877. type := GetMathArrayDescriptorType(dimensions);
  5878. Emit(Push(position,op.op));
  5879. (* push type descriptor *)
  5880. reg := TypeDescriptorAdr(type);
  5881. IF ~newObjectFile THEN
  5882. IntermediateCode.MakeMemory(reg,addressType);
  5883. END;
  5884. Emit(Push(position,reg));
  5885. ReleaseIntermediateOperand(reg);
  5886. (* push realtime flag: false by default *)
  5887. Emit(Push(position,false));
  5888. CallThis(position,"Heaps","NewRec",3);
  5889. END NewMathArrayDescriptor;
  5890. PROCEDURE PushConstString(CONST s: ARRAY OF CHAR);
  5891. VAR res: Operand; string: SyntaxTree.String; sv: SyntaxTree.StringValue; type: SyntaxTree.Type;
  5892. BEGIN
  5893. NEW(string, LEN(s)); COPY(s, string^);
  5894. sv := SyntaxTree.NewStringValue(-1,string);
  5895. type := SyntaxTree.NewStringType(-1,system.characterType,Strings.Length(s));
  5896. sv.SetType(type);
  5897. Designate(sv,res);
  5898. Emit(Push(position,res.tag));
  5899. Emit(Push(position,res.op));
  5900. ReleaseOperand(res);
  5901. END PushConstString;
  5902. PROCEDURE PushConstBoolean(b: BOOLEAN);
  5903. BEGIN
  5904. IF b THEN
  5905. Emit(Push(-1, true));
  5906. ELSE
  5907. Emit(Push(-1, false));
  5908. END;
  5909. END PushConstBoolean;
  5910. PROCEDURE PushConstSet(v: SET);
  5911. VAR value: SyntaxTree.Value; op: Operand;
  5912. BEGIN
  5913. value := SyntaxTree.NewSetValue(-1, v);
  5914. value.SetType(system.setType);
  5915. Evaluate(value, op);
  5916. Emit(Push(-1, op.op));
  5917. ReleaseOperand(op);
  5918. END PushConstSet;
  5919. PROCEDURE PushConstInteger(v: LONGINT);
  5920. VAR value: SyntaxTree.Value; op: Operand;
  5921. BEGIN
  5922. value := SyntaxTree.NewIntegerValue(-1, v);
  5923. value.SetType(system.longintType);
  5924. Evaluate(value, op);
  5925. Emit(Push(-1, op.op));
  5926. ReleaseOperand(op);
  5927. END PushConstInteger;
  5928. PROCEDURE OpenInitializer(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope): IntermediateCode.Section;
  5929. VAR name: Basic.SegmentedName; procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  5930. section: IntermediateCode.Section;
  5931. BEGIN
  5932. procedureScope := SyntaxTree.NewProcedureScope(scope);
  5933. Global.GetSymbolSegmentedName(symbol, name);
  5934. Basic.SuffixSegmentedName(name, Basic.MakeString("@Initializer"));
  5935. procedure := SyntaxTree.NewProcedure(-1, SyntaxTree.NewIdentifier(""), procedureScope);
  5936. procedure.SetScope(moduleScope);
  5937. procedure.SetType(SyntaxTree.NewProcedureType(-1,scope));
  5938. procedure.type(SyntaxTree.ProcedureType).SetDelegate(TRUE);
  5939. procedure.SetAccess(SyntaxTree.Hidden);
  5940. currentScope := procedureScope;
  5941. section := NewSection(module.allSections, Sections.CodeSection, name, NIL,commentPrintout # NIL);
  5942. EmitEnter(section, -1,NIL,0,0,0);
  5943. RETURN section;
  5944. END OpenInitializer;
  5945. PROCEDURE CloseInitializer(prev: IntermediateCode.Section);
  5946. BEGIN
  5947. EmitLeave(section, 0, 0 );
  5948. Emit(Exit(-1,ToMemoryUnits(system,addressType.sizeInBits),0));
  5949. section := prev;
  5950. END CloseInitializer;
  5951. PROCEDURE AddPorts(cell: SyntaxTree.Symbol; x: SyntaxTree.CellType);
  5952. VAR name: SyntaxTree.IdentifierString;
  5953. variable: SyntaxTree.Variable;
  5954. type: SyntaxTree.Type;
  5955. PROCEDURE Field(symbol: SyntaxTree.Symbol; VAR op: Operand);
  5956. BEGIN
  5957. InitOperand(op,ModeReference);
  5958. op.op := fp;
  5959. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,2*addressType.sizeInBits));
  5960. Dereference(op, x, FALSE);
  5961. result := op;
  5962. Symbol(symbol, op);
  5963. END Field;
  5964. PROCEDURE Direction(direction: LONGINT): SET;
  5965. BEGIN
  5966. IF direction = SyntaxTree.OutPort THEN RETURN {0}
  5967. ELSIF direction = SyntaxTree.InPort THEN RETURN {1}
  5968. ELSE HALT(100);
  5969. END;
  5970. END Direction;
  5971. PROCEDURE AddPortProperty(port: SyntaxTree.Variable; modifier: SyntaxTree.Modifier; value: SyntaxTree.Expression);
  5972. VAR name: ARRAY 256 OF CHAR; op: Operand;
  5973. BEGIN
  5974. Field(port, op);
  5975. ToMemory(op.op,addressType,0);
  5976. Emit(Push(-1, op.op));
  5977. ReleaseOperand(op);
  5978. Basic.GetString(modifier.identifier, name);
  5979. PushConstString(name);
  5980. IF SemanticChecker.IsStringType(modifier.expression.type) THEN
  5981. ASSERT(SemanticChecker.IsStringType(value.type));
  5982. Designate(value, op);
  5983. Emit(Push(modifier.position, op.tag));
  5984. Emit(Push(modifier.position, op.op));
  5985. ReleaseOperand(op);
  5986. CallThis(position,"ActiveCellsRuntime","AddPortStringProperty",5);
  5987. ELSIF (modifier.expression.type.resolved IS SyntaxTree.IntegerType) THEN
  5988. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  5989. Evaluate(value, op);
  5990. Emit(Push(modifier.position, op.op));
  5991. ReleaseOperand(op);
  5992. CallThis(position,"ActiveCellsRuntime","AddPortIntegerProperty",4);
  5993. ELSE
  5994. CallThis(position,"ActiveCellsRuntime","AddPortFlagProperty",3);
  5995. END;
  5996. END AddPortProperty;
  5997. PROCEDURE AddPortProperties(variable: SyntaxTree.Variable);
  5998. VAR modifier: SyntaxTree.Modifier;
  5999. BEGIN
  6000. modifier := variable.modifiers;
  6001. WHILE modifier # NIL DO
  6002. AddPortProperty(variable,modifier, modifier.expression);
  6003. modifier := modifier.nextModifier;
  6004. END;
  6005. END AddPortProperties;
  6006. PROCEDURE Variable(name: ARRAY OF CHAR; variable: SyntaxTree.Variable);
  6007. VAR op : Operand; portType: SyntaxTree.PortType; baseType: SyntaxTree.Type;
  6008. size, reg: IntermediateCode.Operand; dim: LONGINT;
  6009. PROCEDURE PushLens(type: SyntaxTree.Type);
  6010. BEGIN
  6011. IF type IS SyntaxTree.ArrayType THEN
  6012. PushLens(type(SyntaxTree.ArrayType).arrayBase.resolved);
  6013. Evaluate(type(SyntaxTree.ArrayType).length, op);
  6014. Emit(Push(-1, op.op));
  6015. ReleaseOperand(op);
  6016. INC(dim);
  6017. ELSE
  6018. baseType := type;
  6019. END;
  6020. END PushLens;
  6021. BEGIN
  6022. (* cell *)
  6023. IF variable.type IS SyntaxTree.ArrayType THEN
  6024. type := variable.type;
  6025. dim := 0;
  6026. PushLens(type);
  6027. portType := baseType.resolved(SyntaxTree.PortType);
  6028. ELSE
  6029. portType := variable.type(SyntaxTree.PortType);
  6030. END;
  6031. PushSelfPointer();
  6032. (* port *)
  6033. Field(variable, op);
  6034. (*left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6035. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(x);
  6036. d := SyntaxTree.NewSymbolDesignator(-1, left, variable); d.SetType(variable.type);
  6037. Designate(d, op);*)
  6038. Emit(Push(-1, op.op));
  6039. ReleaseOperand(op);
  6040. (* name *)
  6041. PushConstString(name);
  6042. (* inout *)
  6043. PushConstSet(Direction(portType.direction));
  6044. (* width *)
  6045. PushConstInteger(portType.sizeInBits);
  6046. IF variable.type IS SyntaxTree.PortType THEN
  6047. CallThis(variable.position,"ActiveCellsRuntime","AddPort",6);
  6048. AddPortProperties(variable);
  6049. ELSIF variable.type IS SyntaxTree.ArrayType THEN
  6050. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6051. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,6*addressType.sizeInBits));
  6052. Emit(Add(position,reg, sp, size));
  6053. (* dim *)
  6054. PushConstInteger(dim);
  6055. (* len array *)
  6056. Emit(Push(position, reg));
  6057. ReleaseIntermediateOperand(reg);
  6058. CallThis(position,"ActiveCellsRuntime","AddPortArray",8);
  6059. size := IntermediateCode.Immediate(addressType, ToMemoryUnits(system,dim*addressType.sizeInBits));
  6060. Emit(Add(position, sp,sp, size));
  6061. END;
  6062. END Variable;
  6063. BEGIN
  6064. IF backend.cellsAreObjects THEN
  6065. variable := x.cellScope.firstVariable;
  6066. WHILE (variable # NIL) DO
  6067. type := variable.type.resolved;
  6068. WHILE (type IS SyntaxTree.ArrayType) DO
  6069. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6070. END;
  6071. IF (type IS SyntaxTree.PortType) THEN (* port found *)
  6072. (*!!! check port arrays !! *)
  6073. Global.GetSymbolName(variable,name);
  6074. Variable(name,variable);
  6075. END;
  6076. variable := variable.nextVariable;
  6077. END;
  6078. ELSE HALT(200)
  6079. END;
  6080. (*prevActiveCellsScope := currentActiveCellsScope;*)
  6081. (*
  6082. x.typeDeclaration.GetName(componentName);
  6083. instanceType := currentActiveCellsScope.NewType(componentName); (*backend.cification.NewType(componentName);*)
  6084. IF HasValue(x.modifiers,Global.StringDataMemorySize,dataMemorySize) THEN
  6085. instanceType.SetDataMemorySize(dataMemorySize);
  6086. END;
  6087. IF HasValue(x.modifiers,Global.StringCodeMemorySize,codeMemorySize) THEN
  6088. instanceType.SetInstructionMemorySize(codeMemorySize)
  6089. END;
  6090. IF HasFlag(x.modifiers, Global.StringVector) THEN
  6091. instanceType.AddCapability(ActiveCells.VectorCapability)
  6092. END;
  6093. IF HasFlag(x.modifiers, Global.StringTRMS) THEN
  6094. instanceType.AddCapability(ActiveCells.TRMSCapability)
  6095. END;
  6096. IF HasFlag(x.modifiers, Global.StringFloatingPoint) THEN
  6097. instanceType.AddCapability(ActiveCells.FloatingPointCapability)
  6098. END;
  6099. AddDevices(instanceType, x);
  6100. *)
  6101. (*
  6102. IF x.isCellNet THEN
  6103. IF HasValue(x.modifiers,Global.StringFrequencyDivider,value) THEN backend.activeCellsSpecification.SetFrequencyDivider(value) END;
  6104. END;
  6105. *)
  6106. (*currentActiveCellsScope := instanceType;*)
  6107. (*
  6108. parameter := x.firstParameter;
  6109. portIndex := 0;
  6110. WHILE parameter # NIL DO
  6111. parameter.GetName(parameterName);
  6112. parameterType := parameter.type.resolved;
  6113. Parameter(parameterName, parameterType);
  6114. (*
  6115. IF SemanticChecker.IsStaticArray(parameterType,parameterType,len) THEN
  6116. ParameterArray(parameterType);
  6117. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6118. FOR i := 0 TO len-1 DO
  6119. COPY(parameterName,name);
  6120. AppendIndex(name,i);
  6121. port := instanceType.NewPort(name,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6122. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6123. INC(portIndex);
  6124. END;
  6125. ELSE
  6126. direction := Direction(parameterType(SyntaxTree.PortType).direction);
  6127. port := instanceType.NewPort(parameterName,direction,backend.activeCellsSpecification.GetPortAddress(portIndex));
  6128. port.SetWidth(parameterType(SyntaxTree.PortType).sizeInBits);
  6129. INC(portIndex);
  6130. END;
  6131. *)
  6132. parameter := parameter.nextParameter;
  6133. END;
  6134. *)
  6135. (*
  6136. Scope(x.cellScope);
  6137. currentActiveCellsScope := prevActiveCellsScope;
  6138. AddModules(instanceType,x.cellScope);
  6139. *)
  6140. END AddPorts;
  6141. PROCEDURE AddProperty(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; property: SyntaxTree.Property; value: SyntaxTree.Expression);
  6142. VAR name: ARRAY 256 OF CHAR; op: Operand; left, d: SyntaxTree.Designator;
  6143. BEGIN
  6144. Symbol(cell,op);
  6145. ToMemory(op.op,addressType,0);
  6146. Emit(Push(position,op.op));
  6147. ReleaseOperand(op);
  6148. Basic.GetString(property.name, name);
  6149. PushConstString(name);
  6150. IF SemanticChecker.IsStringType(property.type) OR (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6151. left := SyntaxTree.NewSymbolDesignator(-1,left,cell); left.SetType(system.anyType);
  6152. left := SyntaxTree.NewDereferenceDesignator(-1, left); left.SetType(cellType);
  6153. d := SyntaxTree.NewSymbolDesignator(-1, left, property); d.SetType(property.type);
  6154. Designate(d, op);
  6155. IF SemanticChecker.IsStringType(property.type) THEN
  6156. Emit(Push(-1, op.tag))
  6157. END;
  6158. Emit(Push(-1, op.op));
  6159. ReleaseOperand(op);
  6160. END;
  6161. IF SemanticChecker.IsStringType(property.type) THEN
  6162. ASSERT(SemanticChecker.IsStringType(value.type));
  6163. Designate(value, op);
  6164. Emit(Push(property.position, op.tag));
  6165. Emit(Push(property.position, op.op));
  6166. ReleaseOperand(op);
  6167. CallThis(position,"ActiveCellsRuntime","AddStringProperty",7);
  6168. ELSIF (property.type.resolved IS SyntaxTree.IntegerType) THEN
  6169. ASSERT(value.type.resolved IS SyntaxTree.IntegerType);
  6170. Evaluate(value, op);
  6171. Emit(Push(property.position, op.op));
  6172. ReleaseOperand(op);
  6173. CallThis(position,"ActiveCellsRuntime","AddIntegerProperty",5);
  6174. ELSE
  6175. CallThis(position,"ActiveCellsRuntime","AddFlagProperty",3);
  6176. END;
  6177. END AddProperty;
  6178. PROCEDURE AddModifiers(cellType: SyntaxTree.CellType; cell: SyntaxTree.Symbol; modifier: SyntaxTree.Modifier);
  6179. VAR symbol: SyntaxTree.Symbol;
  6180. BEGIN
  6181. WHILE modifier # NIL DO
  6182. symbol := cellType.cellScope.FindSymbol(modifier.identifier);
  6183. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  6184. AddProperty(cellType, cell, symbol(SyntaxTree.Property), modifier.expression);
  6185. ELSE
  6186. (*! move this check to checker *)
  6187. Error(modifier.position, "undefined property");
  6188. END;
  6189. modifier := modifier.nextModifier;
  6190. END;
  6191. END AddModifiers;
  6192. PROCEDURE AppendModifier(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6193. VAR last: SyntaxTree.Modifier;
  6194. BEGIN
  6195. IF to = NIL THEN
  6196. to := SyntaxTree.NewModifier(this.position, this.identifier, this.expression);
  6197. ELSE
  6198. last := to;
  6199. WHILE (last.nextModifier # NIL) & (this.identifier # last.identifier) DO
  6200. last := last.nextModifier;
  6201. END;
  6202. IF last.identifier # this.identifier THEN
  6203. ASSERT(last.nextModifier = NIL);
  6204. last.SetNext(SyntaxTree.NewModifier(this.position, this.identifier, this.expression));
  6205. END;
  6206. END;
  6207. END AppendModifier;
  6208. PROCEDURE AppendModifiers(VAR to: SyntaxTree.Modifier; this: SyntaxTree.Modifier);
  6209. BEGIN
  6210. WHILE this # NIL DO
  6211. AppendModifier(to, this);
  6212. this := this.nextModifier;
  6213. END;
  6214. END AppendModifiers;
  6215. PROCEDURE PushPort(p: SyntaxTree.Expression);
  6216. VAR op: Operand;
  6217. BEGIN
  6218. Evaluate(p, op);
  6219. Emit(Push(p.position, op.op));
  6220. ReleaseOperand(op);
  6221. (*
  6222. WHILE (p # NIL) & ~(p.type.resolved IS SyntaxTree.CellType) DO
  6223. p := p(SyntaxTree.Designator).left;
  6224. END;
  6225. IF p # NIL THEN
  6226. Evaluate(p, op);
  6227. Emit(Push(p.position, op.op));
  6228. ReleaseOperand(op);
  6229. ELSE
  6230. Emit(Push(-1, nil));
  6231. END;
  6232. *)
  6233. END PushPort;
  6234. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  6235. VAR
  6236. p0,p1,p2,parameter: SyntaxTree.Expression; len,val: LONGINT; l,r: Operand; res,adr,reg: IntermediateCode.Operand; type, componentType: SyntaxTree.Type;
  6237. constructor: SyntaxTree.Procedure; s0,s1,s2: Operand; hint: HUGEINT;
  6238. i: LONGINT; formalParameter: SyntaxTree.Parameter;
  6239. tmp:IntermediateCode.Operand;
  6240. size: LONGINT; dim,openDim: LONGINT; pointer: IntermediateCode.Operand; t,t0,t1,t2: SyntaxTree.Type; trueL,falseL,ignore: Label;
  6241. exit,else,end: Label; procedureType: SyntaxTree.ProcedureType;
  6242. name: Basic.SegmentedName; symbol: Sections.Section; operand: Operand;
  6243. dest: IntermediateCode.Operand;
  6244. staticLength: LONGINT; itype: IntermediateCode.Type;
  6245. convert,isTensor: BOOLEAN;
  6246. recordType: SyntaxTree.RecordType;
  6247. baseType: SyntaxTree.Type;
  6248. flags: SET;
  6249. left: SyntaxTree.Expression;
  6250. call: SyntaxTree.Designator;
  6251. procedure: SyntaxTree.Procedure;
  6252. temporaryVariable: SyntaxTree.Variable;
  6253. dummy: IntermediateCode.Operand;
  6254. customBuiltin: SyntaxTree.CustomBuiltin;
  6255. isVarPar: ARRAY 3 OF BOOLEAN;
  6256. callsection: Sections.Section;
  6257. segmentedName: Basic.SegmentedName;
  6258. needsTrace: BOOLEAN;
  6259. n: ARRAY 256 OF CHAR;
  6260. modifier: SyntaxTree.Modifier;
  6261. previous, init: IntermediateCode.Section;
  6262. prevScope: SyntaxTree.Scope;
  6263. PROCEDURE CallBodies(self: IntermediateCode.Operand; type: SyntaxTree.Type);
  6264. VAR recordScope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure; body: SyntaxTree.Body; flags: LONGINT;
  6265. priority: IntermediateCode.Operand;
  6266. op,callop: Operand;
  6267. BEGIN
  6268. IF type = NIL THEN RETURN END;
  6269. type := type.resolved;
  6270. IF type IS SyntaxTree.PointerType THEN
  6271. type := type(SyntaxTree.PointerType).pointerBase.resolved
  6272. END;
  6273. IF type IS SyntaxTree.MathArrayType THEN RETURN END;
  6274. CallBodies(self,type(SyntaxTree.RecordType).baseType);
  6275. recordScope := type(SyntaxTree.RecordType).recordScope;
  6276. IF recordScope.bodyProcedure # NIL THEN
  6277. procedure := recordScope.bodyProcedure;
  6278. body := procedure.procedureScope.body;
  6279. Emit(Push(position,self));
  6280. IF body.isActive THEN
  6281. StaticCallOperand(callop,procedure);
  6282. Emit(Push(position,callop.op));
  6283. IF body.priority # NIL THEN Evaluate(body.priority,op); priority := op.op;
  6284. Convert(priority,sizeType);
  6285. ELSE priority := IntermediateCode.Immediate(sizeType,0)
  6286. END;
  6287. Emit(Push(position,priority));
  6288. ReleaseIntermediateOperand(priority);
  6289. IF backend.cooperative THEN
  6290. Emit(Push(position,self));
  6291. CallThis(position,"Activities","Create",3)
  6292. ELSE
  6293. flags := 0;
  6294. IF body.isSafe THEN
  6295. flags := 1;
  6296. END;
  6297. Emit(Push(position,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.setType),flags)));
  6298. Emit(Push(position,self));
  6299. CallThis(position,"Objects","CreateProcess",4)
  6300. END;
  6301. ELSE
  6302. Emit(Push(position,self));
  6303. StaticCallOperand(callop,procedure);
  6304. Emit(Call(position,callop.op,ProcedureParametersSize(system,procedure)));
  6305. END;
  6306. Emit(Pop(position,self));
  6307. END;
  6308. END CallBodies;
  6309. PROCEDURE PushString(op: Operand; actualType: SyntaxTree.Type);
  6310. BEGIN
  6311. actualType := actualType.resolved;
  6312. IF actualType IS SyntaxTree.StringType THEN
  6313. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.StringType).length)));
  6314. ELSIF actualType(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  6315. Emit(Push(position,IntermediateCode.Immediate(addressType,actualType(SyntaxTree.ArrayType).staticLength)));
  6316. ELSE
  6317. tmp := op.tag;
  6318. IntermediateCode.MakeMemory(tmp,addressType);
  6319. Emit(Push(position,tmp));
  6320. END;
  6321. Emit(Push(position,op.op))
  6322. END PushString;
  6323. PROCEDURE PushTD(type: SyntaxTree.Type);
  6324. VAR op: IntermediateCode.Operand;
  6325. BEGIN
  6326. IF type = NIL THEN Emit(Push(position,IntermediateCode.Immediate(addressType,0)))
  6327. ELSIF type.resolved IS SyntaxTree.AnyType THEN Emit(Push(position,IntermediateCode.Immediate(addressType,1)))
  6328. ELSE
  6329. IF type.resolved IS SyntaxTree.PointerType THEN
  6330. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  6331. END;
  6332. op := TypeDescriptorAdr(type.resolved);
  6333. IF ~newObjectFile THEN
  6334. IntermediateCode.MakeMemory(op,addressType);
  6335. END;
  6336. Emit(Push(position,op));
  6337. END
  6338. END PushTD;
  6339. BEGIN
  6340. IF Trace THEN TraceEnter("VisitBuiltinCallDesignator") END;
  6341. dest := destination; destination := emptyOperand;
  6342. p0 := NIL; p1 := NIL; p2 := NIL; len := x.parameters.Length();
  6343. IF len > 0 THEN p0 := x.parameters.GetExpression(0); t0 := p0.type.resolved END;
  6344. IF len > 1 THEN p1 := x.parameters.GetExpression(1); t1 := p1.type.resolved END;
  6345. IF len > 2 THEN p2 := x.parameters.GetExpression(2); t2 := p2.type.resolved END;
  6346. CASE x.id OF
  6347. (* ---- COPY ----- *)
  6348. |Global.Copy:
  6349. CopyString(p1,p0); (* roles exchanged: COPY ( src => dest ) *)
  6350. (* ---- EXCL, INCL----- *)
  6351. |Global.Excl,Global.Incl:
  6352. Evaluate(p0,s0);
  6353. Evaluate(p1,s1);
  6354. Convert(s1.op,setType);
  6355. IF (s1.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6356. TrapC(BrltL,s1.op,IntermediateCode.Immediate(setType,setType.sizeInBits),IndexCheckTrap);
  6357. END;
  6358. ReuseCopy(res,s0.op);
  6359. ReleaseOperand(s0);
  6360. Reuse1(tmp,s1.op);
  6361. ReleaseOperand(s1);
  6362. Emit(Shl(position,tmp,IntermediateCode.Immediate(setType,1),s1.op));
  6363. IF x.id = Global.Excl THEN
  6364. Emit(Not(position,tmp,tmp));
  6365. Emit(And(position,res,res,tmp));
  6366. ELSE
  6367. Emit(Or(position,res,res,tmp));
  6368. END;
  6369. ReleaseIntermediateOperand(tmp);
  6370. Designate(p0,s0);
  6371. ToMemory(s0.op,setType,0);
  6372. Emit(Mov(position,s0.op,res));
  6373. ReleaseOperand(s0); ReleaseIntermediateOperand(res);
  6374. (* ---- DISPOSE ----- *)
  6375. |Global.Dispose:
  6376. Designate(p0,s0);
  6377. Emit(Push(position,s0.op));
  6378. ReleaseOperand(s0);
  6379. CallThis(position,DefaultRuntimeModuleName,"Dispose", 1);
  6380. (* ---- GETPROCEDURE ----- *)
  6381. |Global.GetProcedure:
  6382. Designate(p0,s0);
  6383. PushString(s0,p0.type);
  6384. Designate(p1,s1);
  6385. PushString(s1,p1.type);
  6386. procedureType := p2.type.resolved(SyntaxTree.ProcedureType);
  6387. IF (procedureType.firstParameter = NIL) OR (procedureType.firstParameter.access = SyntaxTree.Hidden) THEN PushTD(NIL)
  6388. ELSE PushTD(procedureType.firstParameter.type)
  6389. END;
  6390. PushTD(procedureType.returnType);
  6391. Designate(p2,s2);
  6392. Emit(Push(position,s2.op));
  6393. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  6394. CallThis(position,"Modules","GetProcedure", 7);
  6395. (* ---- ASH, LSH, ROT ----- *)
  6396. |Global.Ash, Global.Asr, Global.Lsh, Global.Rot, Global.Ror:
  6397. Evaluate(p0,s0);
  6398. IF (x.id = Global.Lsh) OR (x.id = Global.Rot) OR (x.id = Global.Ror)THEN
  6399. (* make unsigned arguments in order to produced a logical shift *)
  6400. IF s0.op.type.form = IntermediateCode.SignedInteger THEN
  6401. convert:= TRUE;
  6402. itype := s0.op.type;
  6403. IntermediateCode.InitType(itype,IntermediateCode.UnsignedInteger,s0.op.type.sizeInBits);
  6404. Convert(s0.op,itype);
  6405. ELSE
  6406. convert := FALSE;
  6407. END;
  6408. END;
  6409. Evaluate(p1,s1);
  6410. IF IsIntegerConstant(p1,hint) THEN
  6411. ReuseCopy(reg,s0.op);
  6412. IF hint > 0 THEN
  6413. IntermediateCode.InitImmediate(s1.op,s1.op.type,hint);
  6414. IF x.id = Global.Ash THEN Emit(Shl(position,reg,s0.op,s1.op))
  6415. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,s0.op,s1.op))
  6416. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,s0.op,s1.op))
  6417. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,s0.op,s1.op))
  6418. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,s0.op,s1.op))
  6419. END;
  6420. ELSIF hint < 0 THEN
  6421. IntermediateCode.InitImmediate(s1.op,s1.op.type,-hint);
  6422. IF x.id = Global.Ash THEN Emit(Shr(position,reg,s0.op,s1.op));
  6423. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,s0.op,s1.op));
  6424. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,s0.op,s1.op));
  6425. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,s0.op,s1.op))
  6426. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,s0.op,s1.op))
  6427. END;
  6428. END;
  6429. ReleaseOperand(s0); ReleaseOperand(s1);
  6430. ELSE
  6431. exit := NewLabel();
  6432. end := NewLabel();
  6433. ReuseCopy(reg,s0.op);
  6434. BrgeL(exit,s1.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p1.type),0));
  6435. Reuse1(tmp,s1.op);
  6436. Emit(Neg(position,tmp,s1.op));
  6437. Convert(tmp,s1.op.type);
  6438. IF x.id = Global.Ash THEN Emit(Shr(position,reg,reg,tmp))
  6439. ELSIF x.id = Global.Lsh THEN Emit(Shr(position,reg,reg,tmp))
  6440. ELSIF x.id = Global.Rot THEN Emit(Ror(position,reg,reg,tmp))
  6441. ELSIF x.id = Global.Ror THEN Emit(Rol(position,reg,reg,tmp))
  6442. ELSIF x.id = Global.Asr THEN Emit(Shl(position,reg,reg,tmp))
  6443. END;
  6444. ReleaseIntermediateOperand(tmp);
  6445. BrL(end);
  6446. SetLabel(exit);
  6447. ReuseCopy(tmp,s1.op);
  6448. Convert(tmp,s1.op.type);
  6449. IF x.id = Global.Ash THEN Emit(Shl(position,reg,reg,tmp))
  6450. ELSIF x.id = Global.Lsh THEN Emit(Shl(position,reg,reg,tmp))
  6451. ELSIF x.id = Global.Rot THEN Emit(Rol(position,reg,reg,tmp))
  6452. ELSIF x.id = Global.Ror THEN Emit(Ror(position,reg,reg,tmp))
  6453. ELSIF x.id = Global.Asr THEN Emit(Shr(position,reg,reg,tmp))
  6454. END;
  6455. ReleaseIntermediateOperand(tmp);
  6456. SetLabel(end);
  6457. ReleaseOperand(s0); ReleaseOperand(s1);
  6458. END;
  6459. InitOperand(result,ModeValue);
  6460. IF convert THEN
  6461. itype := reg.type;
  6462. IntermediateCode.InitType(itype,IntermediateCode.SignedInteger,reg.type.sizeInBits);
  6463. Convert(reg,itype);
  6464. END;
  6465. result.op := reg;
  6466. (* ---- CAP ----- *)
  6467. |Global.Cap:
  6468. Evaluate(p0,result);
  6469. ReuseCopy(reg,result.op);
  6470. ReleaseIntermediateOperand(result.op);
  6471. ignore := NewLabel();
  6472. BrltL(ignore, reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("a")));
  6473. BrltL(ignore,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),ORD("z")),reg);
  6474. Emit(And(position,reg,reg,IntermediateCode.Immediate(IntermediateCode.GetType(system,system.characterType),5FH)));
  6475. SetLabel(ignore);
  6476. result.op := reg;
  6477. (* ---- CHR ----- *)
  6478. |Global.Chr, Global.Chr32:
  6479. Evaluate(p0,result);
  6480. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6481. |Global.Entier, Global.EntierH:
  6482. Evaluate(p0,result);
  6483. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6484. (* ---- MIN and MAX ----- *)
  6485. |Global.Max,Global.Min:
  6486. Evaluate(p0,s0);
  6487. Evaluate(p1,s1);
  6488. Reuse2(res,s0.op,s1.op);
  6489. else := NewLabel();
  6490. IF x.id = Global.Max THEN BrltL(else,s0.op,s1.op);
  6491. ELSE BrltL(else,s1.op,s0.op) END;
  6492. Emit(Mov(position,res,s0.op));
  6493. ReleaseOperand(s0);
  6494. end := NewLabel();
  6495. BrL(end);
  6496. SetLabel(else);
  6497. Emit(MovReplace(position,res,s1.op));
  6498. SetLabel(end);
  6499. ReleaseOperand(s1);
  6500. InitOperand(result,ModeValue);
  6501. result.op := res;
  6502. (* ---- ODD ----- *)
  6503. |Global.Odd:
  6504. IF ~conditional THEN
  6505. ConditionToValue(x)
  6506. ELSE
  6507. Evaluate(p0,result);
  6508. res := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6509. Reuse1(res,result.op);
  6510. Emit(And(position,res,result.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  6511. ReleaseIntermediateOperand(result.op);
  6512. result.op := res;
  6513. BreqL(trueLabel,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1),result.op);
  6514. ReleaseOperand(result);
  6515. BrL(falseLabel);
  6516. END;
  6517. (* ---- ORD ----- *)
  6518. |Global.Ord, Global.Ord32:
  6519. Evaluate(p0,result);
  6520. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6521. (* ---- SHORT, LONG ----- *)
  6522. |Global.Short, Global.Long:
  6523. Evaluate(p0,result);
  6524. IF x.type IS SyntaxTree.ComplexType THEN
  6525. componentType := x.type(SyntaxTree.ComplexType).componentType;
  6526. Convert(result.op, IntermediateCode.GetType(system, componentType));
  6527. Convert(result.tag, IntermediateCode.GetType(system, componentType));
  6528. ELSE
  6529. Convert(result.op,IntermediateCode.GetType(system,x.type));
  6530. END
  6531. (* ---- HALT, SYSTEM.HALT----- *)
  6532. |Global.Halt, Global.systemHalt:
  6533. val := p0.resolved(SyntaxTree.IntegerValue).value;
  6534. EmitTrap (position, val);
  6535. (* ---- ASSERT ----- *)
  6536. |Global.Assert:
  6537. IF ~backend.noAsserts & (p0.resolved = NIL) THEN
  6538. trueL := NewLabel();
  6539. falseL := NewLabel();
  6540. Condition(p0,trueL,falseL);
  6541. IF p1 = NIL THEN val := AssertTrap
  6542. ELSE val := p1.resolved(SyntaxTree.IntegerValue).value;
  6543. END;
  6544. SetLabel(falseL);
  6545. EmitTrap(position,val);
  6546. SetLabel(trueL);
  6547. END;
  6548. (*
  6549. Emit(TrapC(result.op,val);
  6550. *)
  6551. (* ---- INC, DEC----- *)
  6552. |Global.Inc,Global.Dec:
  6553. Expression(p0); adr := result.op;
  6554. LoadValue(result,p0.type); l := result;
  6555. IF p1 = NIL THEN r.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1);
  6556. ELSE Expression(p1); LoadValue(result,p1.type); r := result;
  6557. END;
  6558. IF x.id = Global.Inc THEN
  6559. Emit(Add(position,l.op,l.op,r.op));
  6560. ELSE
  6561. Emit(Sub(position,l.op,l.op,r.op));
  6562. END;
  6563. ReleaseOperand(l); ReleaseOperand(r);
  6564. (* ---- LEN ----- *)
  6565. |Global.Len: (* dynamic length, static length done by checker *)
  6566. Designate(p0,operand);
  6567. IF p1 = NIL THEN
  6568. InitOperand(l,ModeValue);
  6569. l.op := IntermediateCode.Immediate(int32,0);
  6570. ELSE
  6571. Evaluate(p1,l);
  6572. END;
  6573. IF p0.type.resolved IS SyntaxTree.ArrayType THEN
  6574. IF (p0.type.resolved(SyntaxTree.ArrayType).form= SyntaxTree.SemiDynamic) THEN
  6575. Dereference(operand, p0.type.resolved, FALSE);
  6576. END;
  6577. ArrayLen(p0.type.resolved(SyntaxTree.ArrayType),operand,l.op, result);
  6578. ReleaseOperand(operand); ReleaseOperand(l);
  6579. ELSIF p0.type.resolved IS SyntaxTree.MathArrayType THEN
  6580. ASSERT(p1 # NIL);
  6581. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  6582. Dereference(operand,p0.type.resolved,FALSE);
  6583. END;
  6584. GetMathArrayLength(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op, TRUE, result);
  6585. ReleaseOperand(operand); ReleaseOperand(l);
  6586. ELSE HALT(100);
  6587. END;
  6588. Convert(result.op,IntermediateCode.GetType(system, x.type));
  6589. (* ---- FIRST ---- *)
  6590. |Global.First:
  6591. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6592. Evaluate(p0(SyntaxTree.RangeExpression).first, result)
  6593. ELSE
  6594. Designate(p0, result)
  6595. END
  6596. (* ---- LAST ---- *)
  6597. |Global.Last:
  6598. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6599. Evaluate(p0(SyntaxTree.RangeExpression).last, result)
  6600. ELSE
  6601. Designate(p0, result);
  6602. (* make sure result.op is a register *)
  6603. tmp := result.op;
  6604. ReuseCopy(result.op, result.op);
  6605. ReleaseIntermediateOperand(tmp);
  6606. (* add offset to result.op *)
  6607. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6608. END
  6609. (* ---- STEP ---- *)
  6610. |Global.Step:
  6611. IF p0 IS SyntaxTree.RangeExpression THEN (* optimization, could also work with designate *)
  6612. Evaluate(p0(SyntaxTree.RangeExpression).step, result)
  6613. ELSE
  6614. Designate(p0, result);
  6615. (* make sure result.op is a register *)
  6616. tmp := result.op;
  6617. ReuseCopy(result.op, result.op);
  6618. ReleaseIntermediateOperand(tmp);
  6619. (* add offset to result.op *)
  6620. IntermediateCode.AddOffset(result.op, 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)))
  6621. END
  6622. (* ---- RE ---- *)
  6623. |Global.Re:
  6624. IF p0.type.resolved IS SyntaxTree.ComplexType THEN
  6625. Designate(p0, result)
  6626. ELSE
  6627. Evaluate(p0, result)
  6628. END
  6629. (* ---- IM ---- *)
  6630. |Global.Im:
  6631. ASSERT(p0.type.resolved IS SyntaxTree.ComplexType);
  6632. componentType := p0.type.resolved(SyntaxTree.ComplexType).componentType;
  6633. Designate(p0, result);
  6634. (* make sure result.op is a register *)
  6635. tmp := result.op;
  6636. ReuseCopy(result.op, result.op);
  6637. ReleaseIntermediateOperand(tmp);
  6638. (* add offset to result.op *)
  6639. IntermediateCode.AddOffset(result.op, ToMemoryUnits(system, system.SizeOf(componentType)));
  6640. (* ---- ABS ----- *)
  6641. |Global.Abs:
  6642. Evaluate(p0,operand);
  6643. type := p0.type.resolved;
  6644. InitOperand(result,ModeValue);
  6645. Reuse1a(result.op,operand.op,dest);
  6646. Emit(Abs(position,result.op,operand.op));
  6647. ReleaseOperand(operand);
  6648. (* ---- WAIT ----- *)
  6649. |Global.Wait:
  6650. Evaluate(p0,operand);
  6651. Emit(Push(position,operand.op));
  6652. ReleaseOperand(operand);
  6653. CallThis(position,"Activities","Wait", 1);
  6654. (* ---- NEW ----- *)
  6655. |Global.New:
  6656. (*! the following code is only correct for "standard" Oberon calling convention *)
  6657. type := p0.type.resolved;
  6658. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  6659. THEN
  6660. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  6661. IF backend.cooperative THEN
  6662. size := ToMemoryUnits(system,system.SizeOf(recordType));
  6663. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6664. IF recordType.isObject THEN
  6665. INC (size, BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6666. IF recordType.IsActive() THEN
  6667. INC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6668. END;
  6669. IF recordType.IsProtected() THEN
  6670. INC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits))
  6671. END;
  6672. ELSE
  6673. INC (size, BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6674. END;
  6675. END;
  6676. Emit(Push(position,IntermediateCode.Immediate(sizeType,size)));
  6677. CallThis(position,"Runtime","New", 1);
  6678. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6679. Emit(Result(position, pointer));
  6680. exit := NewLabel();
  6681. BreqL(exit,pointer,nil);
  6682. IF ~type(SyntaxTree.PointerType).isPlain THEN
  6683. GetRecordTypeName (recordType,name);
  6684. IF ~recordType.isObject THEN
  6685. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  6686. END;
  6687. IntermediateCode.InitAddress(adr, addressType, name , 0, 0);
  6688. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),adr));
  6689. IF recordType.isObject THEN
  6690. IF recordType.IsProtected() THEN
  6691. DEC (size, MonitorTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6692. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,MonitorOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6693. END;
  6694. IF recordType.IsActive() THEN
  6695. DEC (size, ActionTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  6696. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ActionOffset * ToMemoryUnits(system,addressType.sizeInBits)), IntermediateCode.RegisterOffset(addressType,IntermediateCode.GeneralPurposeRegister,pointer.register,size)));
  6697. END;
  6698. END;
  6699. END;
  6700. (* initialize fields *)
  6701. IF type(SyntaxTree.PointerType).isPlain THEN
  6702. size := 0;
  6703. ELSIF recordType.isObject THEN
  6704. size := BaseObjectTypeSize;
  6705. ELSE
  6706. size := BaseRecordTypeSize;
  6707. END;
  6708. InitFields(recordType, pointer,size*ToMemoryUnits(system,addressType.sizeInBits));
  6709. (* call initializer *)
  6710. constructor := GetConstructor(recordType);
  6711. IF constructor # NIL THEN
  6712. (*! should be unified with ProcedureCallDesignator *)
  6713. Emit(Push(position,pointer));
  6714. ReleaseIntermediateOperand(pointer);
  6715. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6716. FOR i := 1 TO x.parameters.Length()-1 DO
  6717. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6718. formalParameter := formalParameter.nextParameter;
  6719. END;
  6720. (* static call of the constructor *)
  6721. GetCodeSectionNameForSymbol(constructor,name);
  6722. ASSERT(~constructor.isInline);
  6723. IF constructor.scope.ownerModule # module.module THEN
  6724. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6725. ELSE
  6726. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6727. END;
  6728. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor) - ToMemoryUnits(system,addressType.sizeInBits)));
  6729. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6730. Emit(Pop(position,pointer));
  6731. END;
  6732. (* call bodies *)
  6733. CallBodies(pointer,p0.type);
  6734. SetLabel(exit);
  6735. needsTrace := p0.NeedsTrace();
  6736. IF needsTrace THEN ModifyAssignments(true) END;
  6737. IF ~type(SyntaxTree.PointerType).isDisposable THEN
  6738. Emit(Push(position, pointer));
  6739. CallThis(position,"GarbageCollector","Watch",0);
  6740. Emit(Pop(position, pointer));
  6741. END;
  6742. Designate(p0,l);
  6743. IF needsTrace THEN
  6744. CallAssignPointer(l.op, pointer);
  6745. ELSE
  6746. ToMemory(l.op,addressType,0);
  6747. Emit(Mov(position,l.op,pointer));
  6748. END;
  6749. ReleaseIntermediateOperand(pointer);
  6750. ReleaseOperand(l);
  6751. IF needsTrace THEN ModifyAssignments(false) END;
  6752. ELSE
  6753. temporaryVariable := GetTemporaryVariable(type, FALSE);
  6754. IF temporaryVariable # NIL THEN
  6755. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  6756. ELSE
  6757. Designate(p0,l);
  6758. END;
  6759. (* l.op contains address of pointer to record *)
  6760. Emit(Push(position,l.op)); (* address for use after syscall *)
  6761. Emit(Push(position,l.op));
  6762. ReleaseOperand(l);
  6763. (* push type descriptor *)
  6764. reg := TypeDescriptorAdr(recordType);
  6765. IF ~newObjectFile THEN
  6766. IntermediateCode.MakeMemory(reg,addressType);
  6767. END;
  6768. Emit(Push(position,reg));
  6769. ReleaseIntermediateOperand(reg);
  6770. (* push realtime flag *)
  6771. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6772. ELSE Emit(Push(position,false));
  6773. END;
  6774. CallThis(position,"Heaps","NewRec", 3);
  6775. (* check allocation success, if not successful then do not call initializers and bodies *)
  6776. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6777. Emit(Pop(position,pointer));
  6778. MakeMemory(reg,pointer,addressType,0);
  6779. ReleaseIntermediateOperand(pointer);
  6780. pointer := reg;
  6781. exit := NewLabel();
  6782. BreqL(exit,pointer,nil);
  6783. Emit(Push(position,pointer));
  6784. (* initialize fields *)
  6785. InitFields(recordType, pointer,0);
  6786. (* call initializer *)
  6787. constructor := GetConstructor(recordType);
  6788. IF constructor # NIL THEN
  6789. (*! should be unified with ProcedureCallDesignator *)
  6790. Emit(Push(position,pointer));
  6791. ReleaseIntermediateOperand(pointer);
  6792. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  6793. FOR i := 1 TO x.parameters.Length()-1 DO
  6794. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  6795. formalParameter := formalParameter.nextParameter;
  6796. END;
  6797. (* static call of the constructor *)
  6798. GetCodeSectionNameForSymbol(constructor,name);
  6799. ASSERT(~constructor.isInline);
  6800. IF constructor.scope.ownerModule # module.module THEN
  6801. symbol := NewSection(module.importedSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6802. ELSE
  6803. symbol := NewSection(module.allSections, Sections.CodeSection, name,constructor,commentPrintout # NIL);
  6804. END;
  6805. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  6806. ELSE
  6807. ReleaseIntermediateOperand(pointer);
  6808. END;
  6809. IntermediateCode.InitRegister(pointer,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6810. Emit(Pop(position,pointer));
  6811. IF temporaryVariable # NIL THEN
  6812. Designate(p0,l);
  6813. ToMemory(l.op,addressType,0);
  6814. Emit(Mov(position,l.op,pointer));
  6815. ReleaseOperand(l);
  6816. result.tag := emptyOperand;
  6817. END;
  6818. (* call bodies *)
  6819. CallBodies(pointer,p0.type);
  6820. ReleaseIntermediateOperand(pointer);
  6821. IF temporaryVariable # NIL THEN
  6822. end := NewLabel();
  6823. BrL(end);
  6824. SetLabel(exit);
  6825. Designate(p0,l);
  6826. ToMemory(l.op,addressType,0);
  6827. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  6828. ReleaseOperand(l);
  6829. SetLabel(end);
  6830. ELSE
  6831. SetLabel(exit);
  6832. END;
  6833. END;
  6834. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN
  6835. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  6836. dim := 0;
  6837. IF p1 # NIL THEN
  6838. FOR i := 1 TO x.parameters.Length()-1 DO
  6839. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6840. parameter := x.parameters.GetExpression(i);
  6841. Evaluate(parameter,r);
  6842. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  6843. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  6844. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  6845. END;
  6846. Emit(Push(position,r.op));
  6847. IF i=1 THEN
  6848. ReuseCopy(reg,r.op);
  6849. ELSE
  6850. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  6851. END;
  6852. ReleaseOperand(r);
  6853. INC(dim);
  6854. END;
  6855. Convert(reg,addressType);
  6856. ELSE
  6857. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6858. Emit(Mov(position,reg,IntermediateCode.Immediate(addressType,1)));
  6859. END;
  6860. openDim := dim;
  6861. ASSERT(~(type IS SyntaxTree.ArrayType) OR (type(SyntaxTree.ArrayType).form = SyntaxTree.Static));
  6862. IF backend.cooperative THEN
  6863. size := ToMemoryUnits(system,system.SizeOf(type));
  6864. WHILE type IS SyntaxTree.ArrayType DO
  6865. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6866. END;
  6867. size := size DIV ToMemoryUnits(system,system.SizeOf(type));
  6868. IF (size # 1) THEN
  6869. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6870. END;
  6871. Emit(Push(position,reg));
  6872. size := ToMemoryUnits(system,system.SizeOf(type));
  6873. IF (size # 1) THEN
  6874. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6875. END;
  6876. Emit(Add(position,reg,reg,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(BaseArrayTypeSize + openDim)* system.addressSize))));
  6877. Emit(Push(position,reg));
  6878. ReleaseIntermediateOperand(reg);
  6879. CallThis(position,"Runtime","New", 1);
  6880. pointer := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  6881. Emit(Result(position, pointer));
  6882. exit := NewLabel();
  6883. else := NewLabel();
  6884. BreqL(else,pointer,nil);
  6885. IF ~type.hasPointers THEN
  6886. Basic.ToSegmentedName ("BaseTypes.Array",name);
  6887. ELSIF type IS SyntaxTree.RecordType THEN
  6888. Basic.ToSegmentedName ("BaseTypes.RecordArray",name);
  6889. ELSIF type IS SyntaxTree.ProcedureType THEN
  6890. Basic.ToSegmentedName ("BaseTypes.DelegateArray",name);
  6891. ELSE
  6892. Basic.ToSegmentedName ("BaseTypes.PointerArray",name);
  6893. END;
  6894. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,0),IntermediateCode.Address(addressType,name,0,0)));
  6895. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,LengthOffset * system.addressSize))));
  6896. 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))));
  6897. IF type IS SyntaxTree.RecordType THEN
  6898. GetRecordTypeName(type(SyntaxTree.RecordType),name);
  6899. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),IntermediateCode.Address(addressType,name,0,0)));
  6900. ELSE
  6901. Emit(Mov(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,DescriptorOffset * system.addressSize)),nil));
  6902. END;
  6903. i := openDim;
  6904. WHILE i > 0 DO
  6905. DEC (i);
  6906. Emit(Pop(position,IntermediateCode.Memory(addressType,pointer,ToMemoryUnits(system,(BaseArrayTypeSize + i)* system.addressSize))));
  6907. END;
  6908. needsTrace := p0.NeedsTrace();
  6909. IF needsTrace THEN ModifyAssignments(true) END;
  6910. IF ~p0.type.resolved(SyntaxTree.PointerType).isDisposable THEN
  6911. Emit(Push(position, pointer));
  6912. CallThis(position,"GarbageCollector","Watch",0);
  6913. Emit(Pop(position, pointer));
  6914. END;
  6915. Designate(p0,l);
  6916. IF needsTrace THEN
  6917. CallAssignPointer(l.op, pointer);
  6918. ModifyAssignments(false);
  6919. ELSE
  6920. ToMemory(l.op,addressType,0);
  6921. Emit(Mov(position,l.op,pointer));
  6922. END;
  6923. ReleaseIntermediateOperand(pointer);
  6924. ReleaseOperand(l);
  6925. BrL(exit);
  6926. SetLabel(else);
  6927. Emit(Add(position,sp,sp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,(openDim+1)*system.addressSize))));
  6928. Designate(p0,l);
  6929. IF needsTrace THEN
  6930. CallResetProcedure(l.op,l.tag,p0.type.resolved);
  6931. ELSE
  6932. ToMemory(l.op,addressType,0);
  6933. Emit(Mov(position,l.op,pointer));
  6934. END;
  6935. ReleaseOperand(l);
  6936. SetLabel(exit);
  6937. ELSE
  6938. (*! the following code is only correct for "standard" Oberon calling convention *)
  6939. IF SemanticChecker.ContainsPointer(type) THEN
  6940. IF type IS SyntaxTree.ArrayType THEN
  6941. staticLength := 1;
  6942. WHILE (type IS SyntaxTree.ArrayType) DO (* static array *)
  6943. staticLength := staticLength * type(SyntaxTree.ArrayType).staticLength;
  6944. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  6945. END;
  6946. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  6947. Emit(Mul(position,reg,reg,tmp));
  6948. END;
  6949. Designate(p0,l);
  6950. IF openDim > 0 THEN
  6951. Emit(Push(position,l.op)); (* address for use after syscall *)
  6952. END;
  6953. Emit(Push(position,l.op)); (* address *)
  6954. ReleaseOperand(l);
  6955. tmp := TypeDescriptorAdr(type);
  6956. IF ~newObjectFile THEN
  6957. IntermediateCode.MakeMemory(tmp,addressType);
  6958. END;
  6959. Emit(Push(position,tmp)); (* type descriptor *)
  6960. ReleaseIntermediateOperand(tmp);
  6961. Emit(Push(position,reg)); (* number Elements *)
  6962. ReleaseIntermediateOperand(reg);
  6963. tmp := IntermediateCode.Immediate(addressType,dim);
  6964. Emit(Push(position,tmp)); (* dimensions *)
  6965. (* push realtime flag *)
  6966. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6967. ELSE Emit(Push(position,false));
  6968. END;
  6969. CallThis(position,"Heaps","NewArr",5)
  6970. ELSE
  6971. size := ToMemoryUnits(system,system.SizeOf(type));
  6972. IF (size # 1) THEN
  6973. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  6974. END;
  6975. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrayDimTable * system.addressSize+ system.addressSize+ system.addressSize * 2 * (openDim DIV 2)));
  6976. (* DIV 2 term for some strange alignment, don't understand it at the moment - copied from PCC *)
  6977. Emit(Add(position,reg,reg,tmp));
  6978. Designate(p0,l);
  6979. IF openDim >0 THEN
  6980. Emit(Push(position,l.op)); (* address for use after syscall *)
  6981. END;
  6982. Emit(Push(position,l.op)); (* address for syscall *)
  6983. ReleaseOperand(l); (* pointer address *)
  6984. Emit(Push(position,reg)); (* size *)
  6985. ReleaseIntermediateOperand(reg);
  6986. (* push realtime flag *)
  6987. IF (p0.type.resolved.isRealtime) THEN Emit(Push(position,true));
  6988. ELSE Emit(Push(position,false));
  6989. END;
  6990. CallThis(position,"Heaps","NewSys", 3)
  6991. END;
  6992. IF openDim > 0 THEN
  6993. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  6994. Emit(Pop(position,adr));
  6995. ToMemory(adr,addressType,0);
  6996. ReuseCopy(tmp,adr);
  6997. ReleaseIntermediateOperand(adr);
  6998. adr := tmp;
  6999. else := NewLabel();
  7000. BreqL(else,adr,IntermediateCode.Immediate(addressType,0));
  7001. i := openDim-1;
  7002. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7003. WHILE (i >= 0) DO
  7004. Emit(Pop(position,reg));
  7005. IntermediateCode.InitMemory(res,addressType,adr,ToMemoryUnits(system,ArrayDimTable* system.addressSize + system.addressSize*((openDim-1)-i)));
  7006. Emit(Mov(position,res,reg));
  7007. DEC(i);
  7008. END;
  7009. ReleaseIntermediateOperand(adr);
  7010. ReleaseIntermediateOperand(reg);
  7011. exit := NewLabel();
  7012. BrL(exit);
  7013. SetLabel(else);
  7014. (* else part: array could not be allocated *)
  7015. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7016. Emit(Add(position,sp,sp,tmp));
  7017. SetLabel(exit);
  7018. END;
  7019. END;
  7020. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  7021. IF t1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  7022. IF GetRuntimeProcedure("FoxArrayBase","AllocateTensorX",procedure,TRUE) THEN
  7023. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7024. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7025. left.SetType(procedure.type);
  7026. formalParameter := procedureType.firstParameter;
  7027. (* push array to allocate *)
  7028. PushParameter(p0, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7029. formalParameter :=formalParameter.nextParameter;
  7030. (* push length array *)
  7031. PushParameter(p1, formalParameter, procedureType.callingConvention, FALSE, dummy,-1);
  7032. (* push size *)
  7033. type := t0;
  7034. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  7035. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7036. END;
  7037. tmp := IntermediateCode.Immediate(IntermediateCode.GetType(system, system.longintType),ToMemoryUnits(system,system.SizeOf(type))); (* alignment *)
  7038. Emit(Push(position,tmp));
  7039. (* *)
  7040. IF SemanticChecker.ContainsPointer(type) THEN
  7041. tmp := TypeDescriptorAdr(type);
  7042. IF ~newObjectFile THEN
  7043. IntermediateCode.MakeMemory(tmp,addressType);
  7044. END;
  7045. ELSE
  7046. tmp := IntermediateCode.Immediate(addressType, 0);
  7047. END;
  7048. Emit(Push(position,tmp)); (* type descriptor *)
  7049. StaticCallOperand(result,procedure);
  7050. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  7051. ReleaseOperand(result);
  7052. END;
  7053. (*
  7054. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  7055. designator := SyntaxTree.NewSelectorDesignator(InvalidPosition, designator, SyntaxTree.NewIdentifier(InvalidPosition, "AllocateTensorX"));
  7056. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, actualParameters));
  7057. *)
  7058. ELSE
  7059. dim := 0;
  7060. IF type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7061. (* generate geometry descriptor *)
  7062. Designate(p0,l);
  7063. NewMathArrayDescriptor(l, x.parameters.Length()-1);
  7064. ReleaseOperand(l);
  7065. isTensor := TRUE;
  7066. ELSE
  7067. isTensor := FALSE;
  7068. END;
  7069. FOR i := 1 TO x.parameters.Length()-1 DO
  7070. IF ~isTensor THEN
  7071. type := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  7072. END;
  7073. parameter := x.parameters.GetExpression(i);
  7074. Evaluate(parameter,r);
  7075. IF (r.op.mode # IntermediateCode.ModeImmediate) & ~isUnchecked THEN
  7076. IntermediateCode.InitImmediate(tmp,IntermediateCode.GetType(system,parameter.type),0);
  7077. TrapC(BrgeL,r.op,tmp,ArraySizeTrap);
  7078. END;
  7079. Emit(Push(position,r.op));
  7080. IF i=1 THEN
  7081. ReuseCopy(reg,r.op);
  7082. ELSE
  7083. Emit(Mul(position,reg,reg,r.op)); (*! optimize the multiplication of immediate operands *)
  7084. END;
  7085. ReleaseOperand(r);
  7086. INC(dim);
  7087. END;
  7088. Convert(reg,addressType);
  7089. openDim := dim;
  7090. ASSERT(~(type IS SyntaxTree.MathArrayType) OR (type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Static,SyntaxTree.Tensor}));
  7091. (*! the following code is only correct for "standard" Oberon calling convention *)
  7092. IF SemanticChecker.ContainsPointer(SemanticChecker.ArrayBase(type,MAX(LONGINT))) THEN
  7093. t := type;
  7094. IF ~isTensor & (t IS SyntaxTree.MathArrayType) THEN
  7095. staticLength := 1;
  7096. WHILE (t IS SyntaxTree.MathArrayType) DO (* static array *)
  7097. staticLength := staticLength * t(SyntaxTree.MathArrayType).staticLength;
  7098. t := t(SyntaxTree.MathArrayType).arrayBase.resolved;
  7099. END;
  7100. tmp := IntermediateCode.Immediate(reg.type,staticLength);
  7101. Emit(Mul(position,reg,reg,tmp));
  7102. END;
  7103. Designate(p0,l);
  7104. IF isTensor THEN
  7105. Dereference(l,type,FALSE);
  7106. t := SemanticChecker.ArrayBase(type,MAX(LONGINT));
  7107. END;
  7108. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7109. Emit(Push(position,l.tag)); (* address *)
  7110. ReleaseOperand(l);
  7111. tmp := TypeDescriptorAdr(t);
  7112. IF ~newObjectFile THEN
  7113. IntermediateCode.MakeMemory(tmp,addressType);
  7114. END;
  7115. Emit(Push(position,tmp)); (* type descriptor *)
  7116. ReleaseIntermediateOperand(tmp);
  7117. Emit(Push(position,reg)); (* number Elements *)
  7118. ReleaseIntermediateOperand(reg);
  7119. tmp := IntermediateCode.Immediate(addressType,0);
  7120. Emit(Push(position,tmp)); (* dimensions = 0, we control dimensions in the geometry descriptor *)
  7121. (* push realtime flag: false by default *)
  7122. Emit(Push(position,false));
  7123. CallThis(position,"Heaps","NewArr",5);
  7124. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7125. Emit(Pop(position,adr));
  7126. GetMathArrayField(tmp,adr,MathPtrOffset);
  7127. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7128. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,ArrDataArrayOffset))));
  7129. PutMathArrayField(adr,reg,MathAdrOffset);
  7130. ReleaseIntermediateOperand(tmp);
  7131. ReleaseIntermediateOperand(reg);
  7132. ELSE
  7133. IF isTensor THEN
  7134. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT))));
  7135. ELSE
  7136. size := ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim)));
  7137. END;
  7138. IF (size # 1) THEN
  7139. Emit(Mul(position,reg,reg,IntermediateCode.Immediate(addressType,size))); (*! optimize the multiplication of immediate operands *)
  7140. END;
  7141. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset));
  7142. Emit(Add(position,reg,reg,tmp));
  7143. Designate(p0,l);
  7144. IF isTensor THEN
  7145. Dereference(l,type,FALSE);
  7146. END;
  7147. Emit(Push(position,l.tag)); (* address for use after syscall *)
  7148. Emit(Push(position,l.tag)); (* address for syscall *)
  7149. ReleaseOperand(l); (* pointer address *)
  7150. Emit(Push(position,reg)); (* size *)
  7151. ReleaseIntermediateOperand(reg);
  7152. (* push realtime flag: false by default *)
  7153. Emit(Push(position,false));
  7154. CallThis(position,"Heaps","NewSys",3);
  7155. IntermediateCode.InitRegister(adr,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7156. Emit(Pop(position,adr));
  7157. GetMathArrayField(tmp,adr,MathPtrOffset);
  7158. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7159. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,SysDataArrayOffset))));
  7160. PutMathArrayField(adr,reg,MathAdrOffset);
  7161. ReleaseIntermediateOperand(tmp);
  7162. ReleaseIntermediateOperand(reg);
  7163. END;
  7164. flags := {};
  7165. IntermediateCode.InitImmediate(tmp,addressType,SYSTEM.VAL(LONGINT,flags));
  7166. PutMathArrayField(adr,tmp,MathFlagsOffset);
  7167. IntermediateCode.InitImmediate(tmp,addressType,openDim);
  7168. PutMathArrayField(adr,tmp,MathDimOffset);
  7169. else := NewLabel();
  7170. BreqL(else,IntermediateCode.Memory(addressType,adr,0),IntermediateCode.Immediate(addressType,0));
  7171. i := openDim-1;
  7172. IntermediateCode.InitRegister(reg,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7173. IF isTensor THEN
  7174. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,MAX(LONGINT)))));
  7175. ELSE
  7176. IntermediateCode.InitImmediate(tmp,addressType,ToMemoryUnits(system,system.AlignedSizeOf(SemanticChecker.ArrayBase(type,openDim))));
  7177. END;
  7178. PutMathArrayField(adr,tmp,MathElementSizeOffset);
  7179. WHILE (i >= 0) DO
  7180. Emit(Pop(position,reg));
  7181. PutMathArrayLength(adr,reg,i);
  7182. PutMathArrayIncrement(adr,tmp,i);
  7183. IF i > 0 THEN
  7184. IF i=openDim-1 THEN
  7185. ReuseCopy(tmp,tmp);
  7186. END;
  7187. Emit(Mul(position,tmp,tmp,reg));
  7188. END;
  7189. DEC(i);
  7190. END;
  7191. ReleaseIntermediateOperand(adr);
  7192. ReleaseIntermediateOperand(reg);
  7193. ReleaseIntermediateOperand(tmp);
  7194. exit := NewLabel();
  7195. BrL(exit);
  7196. SetLabel(else);
  7197. (* else part: array could not be allocated *)
  7198. tmp := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,openDim*system.addressSize));
  7199. Emit(Add(position,sp,sp,tmp));
  7200. SetLabel(exit);
  7201. END;
  7202. ELSIF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.CellType)
  7203. THEN
  7204. IF ~backend.cellsAreObjects THEN RETURN END;
  7205. IF InCellScope(currentScope) THEN
  7206. PushSelfPointer()
  7207. ELSE
  7208. Emit(Push(position, nil));
  7209. END;
  7210. (* push temp address *)
  7211. baseType := type(SyntaxTree.PointerType).pointerBase.resolved;
  7212. temporaryVariable := GetTemporaryVariable(type, FALSE);
  7213. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7214. (* l.op contains address of pointer to record *)
  7215. Emit(Push(position,l.op)); (* address for use after syscall *)
  7216. ReleaseOperand(l);
  7217. (* push type descriptor *)
  7218. reg := TypeDescriptorAdr(baseType);
  7219. IF ~newObjectFile THEN
  7220. IntermediateCode.MakeMemory(reg,addressType);
  7221. END;
  7222. Emit(Push(position,reg));
  7223. ReleaseIntermediateOperand(reg);
  7224. (* push name *)
  7225. (*Global.GetSymbolName(p0, n);*)
  7226. IF currentScope IS SyntaxTree.ProcedureScope THEN
  7227. Global.GetSymbolName(currentScope(SyntaxTree.ProcedureScope).ownerProcedure, n)
  7228. ELSE
  7229. Global.GetModuleName(module.module, n);
  7230. END;
  7231. Strings.Append(n,"@"); Strings.AppendInt(n, p0.position);
  7232. (*type.typeDeclaration.GetName(n);*)
  7233. PushConstString(n);
  7234. (* push cellnet boolean *)
  7235. PushConstBoolean(baseType(SyntaxTree.CellType).isCellNet);
  7236. (* push engine boolean *)
  7237. PushConstBoolean(baseType(SyntaxTree.CellType).FindProperty(Global.NameEngine) # NIL);
  7238. (* allocate *)
  7239. CallThis(position,"ActiveCellsRuntime","Allocate",7);
  7240. (* add capabilities *)
  7241. modifier := p0(SyntaxTree.Designator).modifiers;
  7242. IF (p0 IS SyntaxTree.SymbolDesignator) & (p0(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  7243. (*modifier := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers;*)
  7244. AppendModifiers(modifier, p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).modifiers );
  7245. (* AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);*)
  7246. END;
  7247. AppendModifiers(modifier, baseType(SyntaxTree.CellType).modifiers);
  7248. (*
  7249. modifier := baseType(SyntaxTree.CellType).modifiers;
  7250. AddProperties(baseType(SyntaxTree.CellType), temporaryVariable, baseType(SyntaxTree.CellType).firstProperty);
  7251. modifier := p0(SyntaxTree.Designator).modifiers;
  7252. *)
  7253. AddModifiers(baseType(SyntaxTree.CellType), temporaryVariable, modifier);
  7254. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7255. (* l.op contains address of pointer to record *)
  7256. ToMemory(l.op,addressType,0);
  7257. (* l.op contains value of pointer to record *)
  7258. Emit(Push(position,l.op)); (* address for use after syscall *)
  7259. ReleaseOperand(l);
  7260. CallThis(position,"ActiveCellsRuntime","FinishedProperties",1);
  7261. prevScope := currentScope;
  7262. init := OpenInitializer(temporaryVariable, baseType(SyntaxTree.CellType).cellScope);
  7263. previous := section;
  7264. section := init;
  7265. (* add ports *)
  7266. AddPorts(temporaryVariable, baseType(SyntaxTree.CellType));
  7267. CloseInitializer(previous);
  7268. currentScope := prevScope;
  7269. Symbol(temporaryVariable,l);
  7270. ToMemory(l.op,addressType,0);
  7271. Emit(Push(position,l.op));
  7272. Emit(Call(position,IntermediateCode.Address(addressType, init.name, 0, 0), ToMemoryUnits(system, addressType.sizeInBits)));
  7273. (*
  7274. constructor := type(SyntaxTree.CellType).cellScope.constructor;
  7275. IF constructor # NIL THEN
  7276. parameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7277. FOR i := 1 TO x.parameters.Length()-1 DO
  7278. p := x.parameters.GetExpression(i);
  7279. Global.GetSymbolName(parameter,name);
  7280. Evaluate(p, value);
  7281. ASSERT(value.type # NIL);
  7282. IF value.type.resolved IS SyntaxTree.IntegerType THEN
  7283. par := instance.AddParameter(name);
  7284. par.SetInteger(value.integer);
  7285. ELSIF value.type.resolved IS SyntaxTree.BooleanType THEN
  7286. par := instance.AddParameter(name);
  7287. par.SetBoolean(value.boolean);
  7288. ELSE Error(x.position,NotYetImplemented)
  7289. END;
  7290. parameter := parameter.nextParameter
  7291. END;
  7292. END;
  7293. *)
  7294. (* call initializer *)
  7295. constructor := baseType(SyntaxTree.CellType).cellScope.constructor (*GetConstructor(p0.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType))*);
  7296. IF constructor # NIL THEN
  7297. (*! should be unified with ProcedureCallDesignator *)
  7298. IF backend.cellsAreObjects THEN
  7299. Symbol(temporaryVariable,l);
  7300. ToMemory(l.op,addressType,0);
  7301. Emit(Push(position,l.op));
  7302. ReleaseOperand(l);
  7303. END;
  7304. formalParameter := constructor.type(SyntaxTree.ProcedureType).firstParameter;
  7305. FOR i := 1 TO x.parameters.Length()-1 DO
  7306. PushParameter(x.parameters.GetExpression(i), formalParameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  7307. formalParameter := formalParameter.nextParameter;
  7308. END;
  7309. (* static call of the constructor *)
  7310. Global.GetSymbolSegmentedName(constructor,name);
  7311. ASSERT(~constructor.isInline);
  7312. IF constructor.scope.ownerModule # module.module THEN
  7313. symbol := NewSection(module.importedSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7314. ELSE
  7315. symbol := NewSection(module.allSections, Sections.CodeSection, name, constructor, commentPrintout # NIL);
  7316. END;
  7317. Emit(Call(position,IntermediateCode.Address(addressType, symbol.name, GetFingerprint(constructor), 0),ProcedureParametersSize(system,constructor)));
  7318. (*ELSE
  7319. ReleaseIntermediateOperand(pointer);*)
  7320. END;
  7321. Symbol(temporaryVariable,l); (*Designate(temporaryVariable,l)*)
  7322. ToMemory(l.op, addressType, 0);
  7323. Designate(p0,s0);
  7324. ToMemory(s0.op,addressType,0);
  7325. Emit(Mov(position,s0.op,l.op));
  7326. ReleaseOperand(l);
  7327. ReleaseOperand(s0);
  7328. result.tag := emptyOperand;
  7329. (* start *)
  7330. IF baseType(SyntaxTree.CellType).cellScope.bodyProcedure # NIL THEN
  7331. (* push cell *)
  7332. Symbol(temporaryVariable, l);
  7333. ToMemory(l.op,addressType,0);
  7334. Emit(Push(-1,l.op));
  7335. (* push delegate *)
  7336. Emit(Push(-1,l.op));
  7337. ReleaseOperand(l);
  7338. StaticCallOperand(s1,baseType(SyntaxTree.CellType).cellScope.bodyProcedure);
  7339. Emit(Push(position, s1.op));
  7340. ReleaseOperand(s1);
  7341. CallThis(position,"ActiveCellsRuntime","Start",3);
  7342. END;
  7343. (*IF temporaryVariable # NIL THEN
  7344. end := NewLabel();
  7345. BrL(end);
  7346. SetLabel(exit);
  7347. Designate(p0,l);
  7348. ToMemory(l.op,addressType,0);
  7349. Emit(Mov(position,l.op,nil)); (* write NIL to adr *)
  7350. ReleaseOperand(l);
  7351. SetLabel(end);
  7352. ELSE
  7353. SetLabel(exit);
  7354. END;
  7355. *)
  7356. (*Error(p0.position,"cannot be allocated in runtime yet");*)
  7357. ELSE (* no pointer to record, no pointer to array *)
  7358. IF ~backend.cellsAreObjects & (type IS SyntaxTree.CellType) THEN
  7359. (* ignore new statement *)
  7360. Warning(p0.position, "cannot run on final hardware");
  7361. ELSE
  7362. HALT(200);
  7363. END;
  7364. END;
  7365. (* ---- ADDRESSOF----- *)
  7366. |Global.systemAdr:
  7367. Designate(p0,s0);
  7368. s0.mode := ModeValue;
  7369. IF (t0 IS SyntaxTree.MathArrayType) & (t0(SyntaxTree.MathArrayType).form = SyntaxTree.Open) THEN
  7370. ReleaseIntermediateOperand(s0.op);
  7371. s0.op := s0.tag;
  7372. IntermediateCode.InitOperand(s0.tag);
  7373. END;
  7374. Convert(s0.op,IntermediateCode.GetType(system,x.type));
  7375. result := s0;
  7376. (* ---- BIT ----- *)
  7377. |Global.systemBit:
  7378. Evaluate(p0,s0);
  7379. ToMemory(s0.op,addressType,0);
  7380. ReuseCopy(res,s0.op);
  7381. ReleaseOperand(s0);
  7382. Evaluate(p1,s1);
  7383. Emit(Ror(position,res,res,s1.op));
  7384. ReleaseOperand(s1);
  7385. Emit(And(position,res,res,IntermediateCode.Immediate(IntermediateCode.GetType(system,p0.type),1)));
  7386. Convert(res,IntermediateCode.GetType(system,system.booleanType));
  7387. IF ~conditional THEN
  7388. InitOperand(result,ModeValue); result.op := res;
  7389. ELSE
  7390. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7391. BrL(falseLabel);
  7392. ReleaseIntermediateOperand(res);
  7393. END;
  7394. (* --- MSK ----*)
  7395. |Global.systemMsk:
  7396. Evaluate(p0,s0);
  7397. Evaluate(p1,s1);
  7398. ReuseCopy(res,s0.op);
  7399. ReleaseOperand(s0);
  7400. Emit(And(position,res,res,s1.op));
  7401. ReleaseOperand(s1);
  7402. InitOperand(result,ModeValue);
  7403. result.op := res;
  7404. (* ---- SYSTEM.GET8|16|32|64 ----- *)
  7405. |Global.systemGet8, Global.systemGet16, Global.systemGet32, Global.systemGet64:
  7406. Evaluate(p0,s0);
  7407. MakeMemory(res,s0.op,IntermediateCode.GetType(system,x.type),0);
  7408. ReleaseOperand(s0);
  7409. InitOperand(result,ModeValue);
  7410. result.op := res;
  7411. (* ---- SYSTEM.GetStackPointer ----- *)
  7412. |Global.systemGetStackPointer:
  7413. InitOperand(result,ModeValue);
  7414. result.op := sp;
  7415. (* ---- SYSTEM.GetFramePointer ----- *)
  7416. |Global.systemGetFramePointer:
  7417. InitOperand(result,ModeValue);
  7418. result.op := fp;
  7419. (* ---- SYSTEM.GetActivity ----- *)
  7420. |Global.systemGetActivity:
  7421. ASSERT(backend.cooperative);
  7422. InitOperand(result,ModeValue);
  7423. result.op := ap;
  7424. (* ---- SYSTEM.SetStackPointer ----- *)
  7425. |Global.systemSetStackPointer:
  7426. Evaluate(p0,s0); (* *)
  7427. Emit(Mov(position,sp,s0.op));
  7428. ReleaseOperand(s0);
  7429. (* ---- SYSTEM.SetFramePointer ----- *)
  7430. |Global.systemSetFramePointer:
  7431. Evaluate(p0,s0); (* *)
  7432. Emit(Mov(position,fp,s0.op));
  7433. ReleaseOperand(s0);
  7434. (* ---- SYSTEM.Activity ----- *)
  7435. |Global.systemSetActivity:
  7436. ASSERT(backend.cooperative);
  7437. Evaluate(p0,s0); (* *)
  7438. Emit(Mov(position,ap,s0.op));
  7439. ReleaseOperand(s0);
  7440. (* ---- SYSTEM.VAL ----- *)
  7441. |Global.systemVal:
  7442. Expression(p1);
  7443. s1 := result;
  7444. type :=p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7445. IF s1.mode = ModeReference THEN
  7446. (* nothing to be done if not record type, just take over new type *)
  7447. IF (type IS SyntaxTree.RecordType) THEN
  7448. ReleaseIntermediateOperand(s1.tag);
  7449. s1.tag := TypeDescriptorAdr(type);
  7450. IF ~newObjectFile THEN
  7451. IntermediateCode.MakeMemory(s1.tag,addressType);
  7452. END;
  7453. UseIntermediateOperand(s1.tag);
  7454. END;
  7455. result := s1;
  7456. ELSE (* copy over result to different type, may not use convert *)
  7457. itype := IntermediateCode.GetType(system,type);
  7458. IF itype.sizeInBits = s1.op.type.sizeInBits THEN
  7459. IntermediateCode.InitRegister(s0.op,itype,IntermediateCode.GeneralPurposeRegister,AcquireRegister(itype,IntermediateCode.GeneralPurposeRegister));
  7460. Emit(Mov(position,s0.op,s1.op));
  7461. ReleaseOperand(s1);
  7462. InitOperand(result,ModeValue);
  7463. result.op := s0.op;
  7464. ELSE (* different size, must convert *)
  7465. (*! this is not very clean, should we forbid conversions between operands with different sizes or should we implement a cutting move ??? *)
  7466. Convert(s1.op, IntermediateCode.GetType(system,type));
  7467. result := s1;
  7468. END;
  7469. END;
  7470. (* ---- SYSTEM.GET ----- *)
  7471. |Global.systemGet:
  7472. Evaluate(p0,s0); (* adr *)
  7473. Designate(p1,s1); (* variable *)
  7474. ToMemory(s0.op,IntermediateCode.GetType(system,p1.type),0);
  7475. ToMemory(s1.op,IntermediateCode.GetType(system,p1.type),0);
  7476. Emit(Mov(position,s1.op,s0.op));
  7477. ReleaseOperand(s1);
  7478. ReleaseOperand(s0);
  7479. (* ---- SYSTEM.PUT 8|16|32|64 ----- *)
  7480. |Global.systemPut, Global.systemPut64, Global.systemPut32, Global.systemPut16, Global.systemPut8:
  7481. Evaluate(p0,s0); (* *)
  7482. Evaluate(p1,s1); (* variable *)
  7483. IF p1.type.resolved IS SyntaxTree.ComplexType THEN
  7484. componentType := p1.type.resolved(SyntaxTree.ComplexType).componentType;
  7485. (* real part *)
  7486. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), 0);
  7487. Emit(Mov(position,res, s1.op));
  7488. ReleaseIntermediateOperand(res);
  7489. (* imaginary part *)
  7490. MakeMemory(res, s0.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  7491. Emit(Mov(position,res, s1.tag));
  7492. ReleaseIntermediateOperand(res);
  7493. ReleaseOperand(s1);
  7494. ReleaseOperand(s0);
  7495. ELSE
  7496. MakeMemory(res,s0.op,IntermediateCode.GetType(system,p1.type),0);
  7497. ReleaseOperand(s0);
  7498. Emit(Mov(position,res,s1.op));
  7499. ReleaseIntermediateOperand(res);
  7500. ReleaseOperand(s1);
  7501. END;
  7502. (* ---- SYSTEM.MOVE ----- *)
  7503. |Global.systemMove:
  7504. Evaluate(p0,s0);
  7505. Evaluate(p1,s1);
  7506. Evaluate(p2,s2);
  7507. Emit(Copy(position,s1.op,s0.op,s2.op));
  7508. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7509. (* ---- SYSTEM.NEW ----- *)
  7510. |Global.systemNew:
  7511. Designate(p0,s0);
  7512. Emit(Push(position,s0.op));
  7513. ReleaseOperand(s0);
  7514. Evaluate(p1,s1);
  7515. Emit(Push(position,s1.op));
  7516. ReleaseOperand(s1);
  7517. (* push realtime flag: false by default *)
  7518. Emit(Push(position,false));
  7519. CallThis(position,"Heaps","NewSys",3);
  7520. (* ---- SYSTEM.CALL ----- *)
  7521. |Global.systemRef:
  7522. Basic.ToSegmentedName(p0(SyntaxTree.StringValue).value^, segmentedName);
  7523. callsection := NewSection(module.allSections, Sections.CodeSection, segmentedName, NIL,commentPrintout # NIL);
  7524. s0.mode := ModeValue;
  7525. IntermediateCode.InitAddress(s0.op, addressType, callsection.name, 0, 0);
  7526. result := s0
  7527. (* ---- INCR ----- *)
  7528. |Global.Incr:
  7529. Designate(p0,operand);
  7530. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7531. Dereference(operand,p0.type.resolved,FALSE);
  7532. END;
  7533. ASSERT(p1 # NIL);
  7534. Evaluate(p1,l);
  7535. GetMathArrayIncrement(p0.type.resolved(SyntaxTree.MathArrayType),operand, l.op,TRUE, result);
  7536. ReleaseOperand(operand); ReleaseOperand(l);
  7537. Convert(result.op,IntermediateCode.GetType(system, x.type));
  7538. (* ---- SUM ----- *)
  7539. |Global.Sum: HALT(200);
  7540. (* ---- CAS ----- *)
  7541. |Global.Cas:
  7542. needsTrace := p0.NeedsTrace();
  7543. IF needsTrace THEN ModifyAssignments(true) END;
  7544. Designate(p0,s0);
  7545. Evaluate(p1,s1);
  7546. Evaluate(p2,s2);
  7547. IF needsTrace THEN
  7548. Emit(Push(position, s0.op));
  7549. Emit(Push(position, s1.op));
  7550. Emit(Push(position, s2.op));
  7551. CallThis(position,"GarbageCollector","CompareAndSwap",3);
  7552. ELSE
  7553. Emit(Cas(position,s0.op,s1.op,s2.op));
  7554. END;
  7555. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7556. IF needsTrace THEN ModifyAssignments(false) END;
  7557. res := NewRegisterOperand(IntermediateCode.GetType(system, p0.type));
  7558. Emit(Result(position, res)); result.op := res; result.mode := ModeValue;
  7559. IF conditional THEN
  7560. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7561. BrL(falseLabel);
  7562. ReleaseIntermediateOperand(res);
  7563. END;
  7564. (* ---- DIM ----- *)
  7565. |Global.Dim:
  7566. ASSERT(~SemanticChecker.IsArrayStructuredObjectType(p0.type));
  7567. Designate(p0,s0);
  7568. IF p0.type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7569. Dereference(s0,p0.type.resolved,FALSE);
  7570. END;
  7571. MathArrayDim(p0.type.resolved(SyntaxTree.MathArrayType),s0.tag,result);
  7572. ReleaseOperand(s0);
  7573. (* ---- RESHAPE ----- *)
  7574. |Global.Reshape:
  7575. IF GetRuntimeProcedure("FoxArrayBase","Reshape",procedure,TRUE) THEN
  7576. left := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid,NIL,procedure);
  7577. left.SetType(procedure.type);
  7578. call := SyntaxTree.NewProcedureCallDesignator(position,left(SyntaxTree.Designator),x.parameters);
  7579. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  7580. END;
  7581. (* ---- SYSTEM.TYPECODE ----- *)
  7582. |Global.systemTypeCode:
  7583. type := p0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  7584. IF type.resolved IS SyntaxTree.PointerType THEN
  7585. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  7586. END;
  7587. result.op := TypeDescriptorAdr(type);
  7588. IF ~newObjectFile THEN
  7589. ToMemory(result.op,IntermediateCode.GetType(system,x.type),0);
  7590. ELSE Convert(result.op, IntermediateCode.GetType(system,x.type));
  7591. END;
  7592. result.mode := ModeValue;
  7593. (* ---- SYSTEM.TRACE ----- *)
  7594. |Global.systemTrace:
  7595. SystemTrace(x.parameters, x.position);
  7596. (* ----- CONNECT ------*)
  7597. |Global.Connect:
  7598. IF backend.cellsAreObjects THEN
  7599. PushPort(p0);
  7600. PushPort(p1);
  7601. IF p2 # NIL THEN
  7602. Evaluate(p2, s2);
  7603. Emit(Push(p2.position, s2.op));
  7604. ReleaseOperand(s2);
  7605. ELSE
  7606. Emit(Push(-1, IntermediateCode.Immediate(int32, -1)));
  7607. END;
  7608. CallThis(position,"ActiveCellsRuntime","Connect",3);
  7609. ELSE
  7610. Warning(x.position, "cannot run on final hardware");
  7611. END;
  7612. (* ----- DELEGATE ------*)
  7613. |Global.Delegate:
  7614. IF backend.cellsAreObjects THEN
  7615. PushPort(p0);
  7616. PushPort(p1);
  7617. CallThis(position,"ActiveCellsRuntime","Delegate",2);
  7618. ELSE
  7619. Warning(x.position, "cannot run on final hardware");
  7620. END;
  7621. (* ----- SEND ------*)
  7622. |Global.Send:
  7623. Evaluate(p0,s0);
  7624. Evaluate(p1,s1);
  7625. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7626. Emit(Push(position,s0.op));
  7627. Emit(Push(position,s1.op));
  7628. (*
  7629. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7630. *)
  7631. IF ~backend.cellsAreObjects THEN
  7632. IF size # 1 THEN Error(p1.position,"send not implemented for complex data types") END;
  7633. END;
  7634. ReleaseOperand(s0);
  7635. ReleaseOperand(s1);
  7636. IF backend.cellsAreObjects THEN
  7637. CallThis(position,"ActiveCellsRuntime","Send",2);
  7638. ELSE
  7639. CallThis(position,ChannelModuleName,"Send",2);
  7640. END;
  7641. (* ----- RECEIVE ------*)
  7642. |Global.Receive:
  7643. Evaluate(p0,s0);
  7644. Emit(Push(position,s0.op));
  7645. Designate(p1,s1);
  7646. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  7647. Emit(Push(position,s1.op));
  7648. IF p2 # NIL THEN
  7649. Designate(p2,s2);
  7650. Emit(Push(position,s2.op));
  7651. END;
  7652. (*
  7653. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  7654. *)
  7655. IF ~backend.cellsAreObjects THEN
  7656. IF size # 1 THEN Error(p1.position,"receive not implemented for complex data types") END;
  7657. END;
  7658. ReleaseOperand(s0);
  7659. ReleaseOperand(s1);
  7660. ReleaseOperand(s2);
  7661. IF backend.cellsAreObjects THEN
  7662. IF p2 = NIL THEN
  7663. CallThis(position,"ActiveCellsRuntime","Receive",2)
  7664. ELSE
  7665. CallThis(position,"ActiveCellsRuntime","ReceiveNonBlockingVar",3)
  7666. END;
  7667. ELSE
  7668. IF p2 = NIL THEN
  7669. CallThis(position,ChannelModuleName,"Receive",2)
  7670. ELSE
  7671. CallThis(position,ChannelModuleName,"ReceiveNonBlockingVar",3)
  7672. END;
  7673. END;
  7674. | Global.systemSpecial:
  7675. customBuiltin := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.CustomBuiltin);
  7676. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  7677. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  7678. (* determine if parameters are of the VAR kind *)
  7679. ASSERT(x.parameters.Length() <= 3);
  7680. formalParameter := procedureType.firstParameter;
  7681. FOR i := 0 TO x.parameters.Length() - 1 DO
  7682. isVarPar[i] := formalParameter.kind = SyntaxTree.VarParameter;
  7683. formalParameter := formalParameter.nextParameter
  7684. END;
  7685. IF p0 # NIL THEN IF isVarPar[0] THEN Designate(p0, s0) ELSE Evaluate(p0,s0) END ELSE InitOperand(s0, ModeValue) END;
  7686. IF p1 # NIL THEN IF isVarPar[1] THEN Designate(p1, s1) ELSE Evaluate(p1,s1) END ELSE InitOperand(s1, ModeValue) END;
  7687. IF p2 # NIL THEN IF isVarPar[2] THEN Designate(p2, s2) ELSE Evaluate(p2,s2) END ELSE InitOperand(s2, ModeValue) END;
  7688. Emit(SpecialInstruction(x.position, customBuiltin.subType,s0.op, s1.op, s2.op));
  7689. ReleaseOperand(s0); ReleaseOperand(s1); ReleaseOperand(s2);
  7690. IF procedureType.returnType # NIL THEN
  7691. res := NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  7692. Emit(Result(position, res));
  7693. (*InitOperand(result,ModeValue);
  7694. result.op := res;
  7695. *)
  7696. IF ~conditional THEN
  7697. InitOperand(result,ModeValue); result.op := res;
  7698. ELSE
  7699. BreqL(trueLabel,IntermediateCode.Immediate(res.type,1),res);
  7700. BrL(falseLabel);
  7701. ReleaseIntermediateOperand(res);
  7702. END;
  7703. END
  7704. ELSE (* function not yet implemented *)
  7705. Error(position,"not yet implemented");
  7706. END;
  7707. destination := dest;
  7708. IF Trace THEN TraceExit("VisitBuiltinCallDesignator") END;
  7709. END VisitBuiltinCallDesignator;
  7710. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  7711. VAR trueL,falseL: Label; recordType: SyntaxTree.RecordType; dest,tag,ptr: IntermediateCode.Operand;
  7712. BEGIN
  7713. IF Trace THEN TraceEnter("VisitTypeGuardDesignator") END;
  7714. dest := destination; destination := emptyOperand;
  7715. Expression(x.left);
  7716. IF x.left.type.resolved = x.type.resolved THEN (* always true: do nothing *)
  7717. ELSIF isUnchecked THEN (* no check *)
  7718. ELSE
  7719. trueL := NewLabel();
  7720. falseL := NewLabel();
  7721. IF IsPointerToRecord(x.left.type,recordType) THEN
  7722. IntermediateCode.InitRegister(tag,addressType,IntermediateCode.GeneralPurposeRegister,AcquireRegister(addressType,IntermediateCode.GeneralPurposeRegister));
  7723. Emit(Mov(position,tag, result.op));
  7724. IF result.mode # ModeValue THEN
  7725. ptr := tag;
  7726. IntermediateCode.MakeMemory(ptr,addressType);
  7727. Emit(Mov(position,tag, ptr));
  7728. END;
  7729. IF ~backend.cooperative THEN
  7730. IntermediateCode.AddOffset(tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7731. END;
  7732. IntermediateCode.MakeMemory(tag,addressType);
  7733. ELSE
  7734. tag := result.tag;
  7735. END;
  7736. TypeTest(tag,x.type,trueL,falseL);
  7737. ReleaseIntermediateOperand(tag);
  7738. SetLabel(falseL);
  7739. EmitTrap(position,TypeCheckTrap);
  7740. SetLabel(trueL);
  7741. END;
  7742. destination := dest;
  7743. IF Trace THEN TraceExit("VisitTypeGuardDesignator") END;
  7744. END VisitTypeGuardDesignator;
  7745. PROCEDURE Dereference(VAR operand: Operand; type: SyntaxTree.Type; isUnsafe: BOOLEAN);
  7746. VAR dereferenced: IntermediateCode.Operand; arrayDataOffset: LONGINT;
  7747. PROCEDURE NilCheck(CONST op: IntermediateCode.Operand);
  7748. VAR label: Label; pc: LONGINT;
  7749. BEGIN
  7750. IF backend.cooperative & ~isUnchecked THEN
  7751. pc := section.pc;
  7752. label := NewLabel();
  7753. BrneL(label, operand.op, nil);
  7754. EmitTrap(position, NilPointerTrap);
  7755. SetLabel(label);
  7756. INC(statCoopNilCheck, section.pc - pc);
  7757. END;
  7758. END NilCheck;
  7759. BEGIN
  7760. LoadValue(operand,system.addressType); (* in case operand is not a value yet *)
  7761. ReuseCopy(dereferenced,operand.op);
  7762. ReleaseOperand(operand);
  7763. operand.mode := ModeReference;
  7764. operand.op := dereferenced;
  7765. operand.tag := dereferenced;
  7766. UseIntermediateOperand(operand.tag);
  7767. IF (type=NIL) OR (type IS SyntaxTree.RecordType)OR (type IS SyntaxTree.CellType) THEN
  7768. IF isUnsafe & ((type = NIL) OR ~type(SyntaxTree.RecordType).isObject) THEN
  7769. ReleaseIntermediateOperand(operand.tag);
  7770. operand.tag := TypeDescriptorAdr(type);
  7771. IF ~newObjectFile THEN
  7772. IntermediateCode.MakeMemory(operand.tag,addressType);
  7773. END;
  7774. ELSE
  7775. IF ~backend.cooperative THEN
  7776. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,-addressType.sizeInBits));
  7777. END;
  7778. IntermediateCode.MakeMemory(operand.tag,addressType);
  7779. END;
  7780. NilCheck(operand.op);
  7781. ELSIF type IS SyntaxTree.ArrayType THEN
  7782. IF isUnsafe THEN
  7783. NilCheck(operand.op);
  7784. ReleaseIntermediateOperand(operand.tag);
  7785. IF type(SyntaxTree.ArrayType).form = SyntaxTree.Static THEN
  7786. IntermediateCode.InitImmediate(operand.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  7787. ELSE
  7788. operand.tag := emptyOperand;
  7789. END;
  7790. ELSE
  7791. NilCheck(operand.op);
  7792. IF backend.cooperative THEN
  7793. arrayDataOffset := (BaseArrayTypeSize + DynamicDim(type)) * addressType.sizeInBits;
  7794. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7795. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,BaseArrayTypeSize*system.addressSize));
  7796. ELSE
  7797. arrayDataOffset := DynamicDim(type) * addressType.sizeInBits + 3 * addressType.sizeInBits;
  7798. INC(arrayDataOffset, (-arrayDataOffset) MOD ArrayAlignment); (* round up to multiple of ArrayAlignment to ensure that first array element is aligned at 0 MOD ArrayAlignment *)
  7799. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,arrayDataOffset));
  7800. IntermediateCode.AddOffset(operand.tag,ToMemoryUnits(system,ArrayDimTable*system.addressSize))
  7801. END;
  7802. END;
  7803. ELSIF type IS SyntaxTree.MathArrayType THEN
  7804. IntermediateCode.AddOffset(operand.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  7805. IntermediateCode.MakeMemory(operand.op,addressType);
  7806. ELSE HALT(100);
  7807. END;
  7808. END Dereference;
  7809. PROCEDURE VisitDereferenceDesignator(x: SyntaxTree.DereferenceDesignator);
  7810. VAR type: SyntaxTree.Type; d: Operand; dest: IntermediateCode.Operand;prevIsUnchecked: BOOLEAN;
  7811. BEGIN
  7812. IF Trace THEN TraceEnter("VisitDereferenceDesignator") END;
  7813. dest := destination; destination := emptyOperand;
  7814. Evaluate(x.left,d);
  7815. type := x.type.resolved;
  7816. prevIsUnchecked := isUnchecked;
  7817. IF (x.left # NIL) & (x.left IS SyntaxTree.SelfDesignator) THEN
  7818. isUnchecked := TRUE; (* avoid NIL-pointer-check for SELF pointer *)
  7819. END;
  7820. Dereference(d,type,IsUnsafePointer(x.left.type));
  7821. isUnchecked := prevIsUnchecked;
  7822. result := d;
  7823. 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
  7824. IF (type IS SyntaxTree.RecordType) & ~type(SyntaxTree.RecordType).isObject THEN
  7825. IntermediateCode.AddOffset(result.op,BaseRecordTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  7826. END;
  7827. END;
  7828. destination := dest;
  7829. IF Trace THEN TraceExit("VisitDereferenceDesignator") END;
  7830. END VisitDereferenceDesignator;
  7831. PROCEDURE VisitSupercallDesignator(x: SyntaxTree.SupercallDesignator);
  7832. VAR procedure: SyntaxTree.Procedure; tag: IntermediateCode.Operand; dest: IntermediateCode.Operand;
  7833. BEGIN
  7834. IF Trace THEN TraceEnter("VisitSupercallDesignator") END;
  7835. dest := destination; destination := emptyOperand;
  7836. Designate(x.left(SyntaxTree.SymbolDesignator).left,result);
  7837. tag := result.op;
  7838. (*ReleaseIntermediateOperand(result.tag); not necessary -- done in StaticCallOperand *)
  7839. procedure := x.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  7840. StaticCallOperand(result,procedure.super);
  7841. ReleaseIntermediateOperand(result.tag);
  7842. UseIntermediateOperand(tag); (* necessary ? *)
  7843. result.tag := tag;
  7844. destination := dest;
  7845. IF Trace THEN TraceExit("VisitSupercallDesignator") END;
  7846. END VisitSupercallDesignator;
  7847. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  7848. VAR basereg: IntermediateCode.Operand; scope: SyntaxTree.Scope; dest: IntermediateCode.Operand;
  7849. moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  7850. name: Basic.SegmentedName;
  7851. BEGIN
  7852. IF Trace THEN TraceEnter("VisitSelfDesignator") END;
  7853. dest := destination; destination := emptyOperand;
  7854. scope := currentScope;
  7855. WHILE (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.ProcedureScope) DO
  7856. scope := scope.outerScope;
  7857. END;
  7858. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  7859. IF newObjectFile THEN
  7860. moduleSection := meta.ModuleSection();
  7861. IF backend.cooperative THEN
  7862. moduleOffset := 0;
  7863. ELSE
  7864. moduleOffset := moduleSection.pc;
  7865. END;
  7866. result.mode := ModeValue;
  7867. result.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  7868. ELSE
  7869. Symbol(moduleSelf,result);
  7870. IntermediateCode.MakeMemory(result.op,addressType);
  7871. END
  7872. ELSIF (scope.outerScope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  7873. result.mode := ModeValue;
  7874. Global.GetSymbolSegmentedName(scope.outerScope(SyntaxTree.CellScope).ownerCell.typeDeclaration, name);
  7875. result.op := IntermediateCode.Address(addressType, name, 0, moduleOffset);
  7876. ELSE
  7877. GetBaseRegister(basereg,currentScope,scope);
  7878. InitOperand(result,ModeReference);
  7879. result.op := basereg;
  7880. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  7881. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  7882. IF backend.cooperative THEN
  7883. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,addressType.sizeInBits));
  7884. END;
  7885. (* tag must be loaded when dereferencing SELF pointer *)
  7886. END;
  7887. destination := dest;
  7888. IF Trace THEN TraceExit("VisitSelfDesignator") END;
  7889. END VisitSelfDesignator;
  7890. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  7891. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  7892. BEGIN
  7893. IF Trace THEN TraceEnter("VisitResultDesignator") END;
  7894. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  7895. procedureType := procedure.type(SyntaxTree.ProcedureType);
  7896. parameter := procedureType.returnParameter;
  7897. VisitParameter(parameter);
  7898. IF Trace THEN TraceExit("VisitResultDesignator") END;
  7899. END VisitResultDesignator;
  7900. (** values *)
  7901. PROCEDURE VisitBooleanValue(x: SyntaxTree.BooleanValue);
  7902. BEGIN
  7903. IF Trace THEN TraceEnter("VisitBooleanValue") END;
  7904. IF conditional THEN
  7905. IF x.value THEN BrL(trueLabel)
  7906. ELSE BrL(falseLabel)
  7907. END;
  7908. ELSE
  7909. InitOperand(result,ModeValue);
  7910. IF x.value THEN result.op := true ELSE result.op := false END;
  7911. END;
  7912. END VisitBooleanValue;
  7913. PROCEDURE GetDataSection*(): IntermediateCode.Section;
  7914. VAR name: Basic.SegmentedName; section: IntermediateCode.Section;
  7915. BEGIN
  7916. Global.GetModuleSegmentedName(module.module, name);
  7917. Basic.SuffixSegmentedName(name,Basic.MakeString("@Immediates"));
  7918. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,TRUE);
  7919. RETURN section
  7920. END GetDataSection;
  7921. PROCEDURE GetImmediateMem(VAR vop: IntermediateCode.Operand);
  7922. VAR data: IntermediateCode.Section;pc: LONGINT; type: IntermediateCode.Type;
  7923. BEGIN
  7924. type := vop.type;
  7925. data := GetDataSection();
  7926. pc := EnterImmediate(data,vop);
  7927. IntermediateCode.InitAddress(vop, addressType, data.name, 0, pc);
  7928. IntermediateCode.MakeMemory(vop, type);
  7929. END GetImmediateMem;
  7930. PROCEDURE VisitIntegerValue(x: SyntaxTree.IntegerValue);
  7931. BEGIN
  7932. IF Trace THEN TraceEnter("VisitIntegerValue") END;
  7933. InitOperand(result,ModeValue);
  7934. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),x.hvalue);
  7935. IF ~supportedImmediate(result.op) &~inData THEN
  7936. GetImmediateMem(result.op)
  7937. END;
  7938. END VisitIntegerValue;
  7939. PROCEDURE VisitCharacterValue(x: SyntaxTree.CharacterValue);
  7940. BEGIN
  7941. IF Trace THEN TraceEnter("VisitCharacterValue") END;
  7942. InitOperand(result,ModeValue);
  7943. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),ORD(x.value));
  7944. END VisitCharacterValue;
  7945. PROCEDURE VisitSetValue(x: SyntaxTree.SetValue);
  7946. BEGIN
  7947. IF Trace THEN TraceEnter("VisitSetValue") END;
  7948. InitOperand(result,ModeValue);
  7949. IntermediateCode.InitImmediate(result.op,IntermediateCode.GetType(system,x.type),SYSTEM.VAL(LONGINT,x.value));
  7950. END VisitSetValue;
  7951. PROCEDURE VisitMathArrayValue(x: SyntaxTree.MathArrayValue);
  7952. VAR irv: IntermediateCode.Section; name:Basic.SegmentedName;
  7953. PROCEDURE RecursiveData(x: SyntaxTree.MathArrayExpression);
  7954. VAR numberElements,i: LONGINT; expression: SyntaxTree.Expression; op: Operand;
  7955. BEGIN
  7956. numberElements := x.elements.Length();
  7957. FOR i := 0 TO numberElements-1 DO
  7958. expression := x.elements.GetExpression(i);
  7959. IF expression IS SyntaxTree.MathArrayExpression THEN
  7960. RecursiveData(expression(SyntaxTree.MathArrayExpression));
  7961. ELSE
  7962. inData := TRUE;
  7963. Evaluate(expression,op);
  7964. irv.Emit(Data(position,op.op));
  7965. inData := FALSE;
  7966. ReleaseOperand(op);
  7967. END;
  7968. END;
  7969. END RecursiveData;
  7970. BEGIN
  7971. IF Trace THEN TraceEnter("VisitMathArrayValue") END;
  7972. IF ~TryConstantDeclaration() THEN
  7973. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  7974. GetCodeSectionNameForSymbol(constantDeclaration,name);
  7975. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  7976. irv := NewSection(module.allSections,Sections.ConstSection,name,constantDeclaration,commentPrintout # NIL);
  7977. ELSE
  7978. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  7979. END;
  7980. RecursiveData(x.array);
  7981. InitOperand(result,ModeReference);
  7982. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  7983. END
  7984. END VisitMathArrayValue;
  7985. PROCEDURE TryConstantDeclaration(): BOOLEAN;
  7986. VAR constant: Sections.Section;
  7987. BEGIN
  7988. IF constantDeclaration = NIL THEN
  7989. RETURN FALSE
  7990. ELSE
  7991. (* Is a constant in this module: did we generate it already? *)
  7992. constant := module.allSections.FindBySymbol(constantDeclaration); (*TODO*)
  7993. IF constant # NIL THEN
  7994. InitOperand(result,ModeReference);
  7995. IntermediateCode.InitAddress(result.op,addressType,constant.name,GetFingerprint(constant.symbol), 0);
  7996. RETURN TRUE;
  7997. END;
  7998. END;
  7999. RETURN FALSE
  8000. END TryConstantDeclaration;
  8001. PROCEDURE VisitConstant(x: SyntaxTree.Constant);
  8002. BEGIN
  8003. constantDeclaration := x;
  8004. x.value.resolved.Accept(SELF);
  8005. END VisitConstant;
  8006. PROCEDURE VisitRealValue(x: SyntaxTree.RealValue);
  8007. BEGIN
  8008. IF Trace THEN TraceEnter("VisitRealValue") END;
  8009. InitOperand(result,ModeValue);
  8010. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,x.type),x.value);
  8011. END VisitRealValue;
  8012. PROCEDURE VisitComplexValue(x: SyntaxTree.ComplexValue);
  8013. VAR
  8014. componentType: SyntaxTree.Type;
  8015. BEGIN
  8016. IF Trace THEN TraceEnter("VisitComplexValue") END;
  8017. ASSERT(x.type IS SyntaxTree.ComplexType);
  8018. componentType := x.type(SyntaxTree.ComplexType).componentType;
  8019. InitOperand(result,ModeValue);
  8020. IntermediateCode.InitFloatImmediate(result.op,IntermediateCode.GetType(system,componentType),x.realValue); (* real part *)
  8021. IntermediateCode.InitFloatImmediate(result.tag,IntermediateCode.GetType(system,componentType),x.imagValue); (* imaginary part *)
  8022. END VisitComplexValue;
  8023. PROCEDURE VisitStringValue(x: SyntaxTree.StringValue);
  8024. VAR i: LONGINT; name: Basic.SegmentedName;
  8025. irv: IntermediateCode.Section; op: IntermediateCode.Operand;
  8026. BEGIN
  8027. IF Trace THEN TraceEnter("VisitStringValue") END;
  8028. IF ~TryConstantDeclaration() THEN
  8029. IF constantDeclaration = NIL THEN constantDeclaration:=BuildConstant(module.module,x,constId) END;
  8030. GetCodeSectionNameForSymbol(constantDeclaration,name);
  8031. IF ~newObjectFile OR (constantDeclaration.scope = NIL) OR (constantDeclaration.scope.ownerModule = module.module) THEN
  8032. irv := NewSection(module.allSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8033. ELSE
  8034. irv := NewSection(module.importedSections, Sections.ConstSection, name,constantDeclaration,commentPrintout # NIL);
  8035. END;
  8036. FOR i := 0 TO x.length-1 DO
  8037. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(system,system.characterType),ORD(x.value[i]));
  8038. irv.Emit(Data(position,op));
  8039. END;
  8040. InitOperand(result,ModeReference);
  8041. IntermediateCode.InitAddress(result.op, addressType, irv.name, GetFingerprint(irv.symbol), 0);
  8042. result.tag := IntermediateCode.Immediate(addressType,x.length);
  8043. END
  8044. END VisitStringValue;
  8045. PROCEDURE VisitNilValue(x: SyntaxTree.NilValue);
  8046. BEGIN
  8047. IF Trace THEN TraceEnter("VisitNilValue") END;
  8048. InitOperand(result,ModeValue);
  8049. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8050. result.tag := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),0);
  8051. END VisitNilValue;
  8052. PROCEDURE VisitEnumerationValue(x: SyntaxTree.EnumerationValue);
  8053. BEGIN
  8054. IF Trace THEN TraceEnter("VisitEnumerationValue") END;
  8055. InitOperand(result,ModeValue);
  8056. result.op := IntermediateCode.Immediate(IntermediateCode.GetType(system,x.type),x.value);
  8057. END VisitEnumerationValue;
  8058. (** symbols *)
  8059. PROCEDURE VisitImport(x: SyntaxTree.Import);
  8060. BEGIN (* nothing to be done, might however be called via some designator module.procedure *)
  8061. END VisitImport;
  8062. PROCEDURE GetBaseRegister(VAR result: IntermediateCode.Operand; scope,baseScope: SyntaxTree.Scope);
  8063. VAR left,right: IntermediateCode.Operand;level: LONGINT;
  8064. BEGIN
  8065. IF scope # baseScope THEN
  8066. (* left := [fp+8] *)
  8067. IntermediateCode.InitMemory(right,addressType,fp,ToMemoryUnits(system,2*addressType.sizeInBits));
  8068. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8069. ReuseCopy(left,right);
  8070. ReleaseIntermediateOperand(right);
  8071. scope := scope.outerScope; DEC(level);
  8072. (* { left := [left+8] } *)
  8073. IntermediateCode.InitMemory(right,addressType,left,ToMemoryUnits(system,2*addressType.sizeInBits));
  8074. IF backend.cooperative THEN IntermediateCode.AddOffset (right, ToMemoryUnits(system,addressType.sizeInBits)) END;
  8075. WHILE (scope # baseScope) & (scope IS SyntaxTree.ProcedureScope) DO
  8076. Emit(Mov(position,left,right));
  8077. scope := scope.outerScope; DEC(level);
  8078. END;
  8079. ASSERT((scope = baseScope) OR (baseScope = NIL));
  8080. result := left;
  8081. ELSE
  8082. result := fp;
  8083. END;
  8084. END GetBaseRegister;
  8085. PROCEDURE VisitVariable(x: SyntaxTree.Variable);
  8086. VAR symbol: Sections.Section; type: SyntaxTree.Type; recordType: SyntaxTree.RecordType; name: Basic.SegmentedName; temp: IntermediateCode.Operand; reg: LONGINT;
  8087. BEGIN
  8088. IF Trace THEN TraceEnter("VisitVariable"); END;
  8089. type := x.type.resolved;
  8090. IF (x.useRegister) THEN
  8091. InitOperand(result, ModeValue);
  8092. IF x.registerNumber < 0 THEN
  8093. x.SetRegisterNumber(AcquireRegister(IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister));
  8094. reg := x.registerNumber;
  8095. ELSE
  8096. reg := registerUsageCount.Map(x.registerNumber);
  8097. UseRegister(reg);
  8098. END;
  8099. IntermediateCode.InitRegister(result.op,IntermediateCode.GetType(system, type),IntermediateCode.GeneralPurposeRegister,reg);
  8100. ELSIF x.externalName # NIL THEN
  8101. InitOperand(result,ModeReference);
  8102. Basic.ToSegmentedName(x.externalName^, name);
  8103. IntermediateCode.InitAddress(result.op, addressType, name, 0, 0);
  8104. ELSIF (x.scope IS SyntaxTree.ProcedureScope) THEN (* local variable (potentially via nested procedure) *)
  8105. InitOperand(result,ModeReference);
  8106. GetBaseRegister(result.op,currentScope,x.scope);
  8107. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8108. ELSIF (x.scope = moduleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN (* global variable *)
  8109. InitOperand(result,ModeReference);
  8110. GetCodeSectionNameForSymbol(x,name);
  8111. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8112. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8113. ELSIF x.scope IS SyntaxTree.ModuleScope THEN (* global variable in imported module *)
  8114. InitOperand(result,ModeReference);
  8115. GetCodeSectionNameForSymbol(x,name);
  8116. symbol := NewSection(module.importedSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8117. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0)
  8118. ELSE (* field, left designator must have been emitted *)
  8119. ASSERT(result.mode = ModeReference);
  8120. IF result.op.mode = IntermediateCode.ModeMemory THEN
  8121. ReuseCopy(temp,result.op);
  8122. ReleaseIntermediateOperand(result.op);
  8123. result.op := temp;
  8124. END;
  8125. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8126. IF backend.cooperative & (x.scope IS SyntaxTree.RecordScope) THEN
  8127. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8128. IF recordType.isObject & ~recordType.pointerType.isPlain THEN
  8129. IntermediateCode.AddOffset(result.op,BaseObjectTypeSize * ToMemoryUnits(system,addressType.sizeInBits));
  8130. END;
  8131. END;
  8132. END;
  8133. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8134. ValueToCondition(result);
  8135. ELSIF type IS SyntaxTree.ProcedureType THEN
  8136. ReleaseIntermediateOperand(result.tag);
  8137. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8138. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8139. UseIntermediateOperand(result.tag);
  8140. ELSE
  8141. result.tag := nil; (* nil *)
  8142. END;
  8143. ELSIF (type IS SyntaxTree.ArrayType) THEN
  8144. IF type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic THEN
  8145. IF (x.scope IS SyntaxTree.ModuleScope) OR (x.scope IS SyntaxTree.CellScope) & ~backend.cellsAreObjects THEN
  8146. ReleaseIntermediateOperand(result.tag);
  8147. Global.GetSymbolSegmentedName(x,name);
  8148. Basic.AppendToSegmentedName(name,"@len");
  8149. symbol := NewSection(module.allSections, Sections.VarSection, name,NIL ,commentPrintout # NIL);
  8150. IntermediateCode.InitAddress(result.tag, addressType, symbol.name,0 , 0);
  8151. ELSE
  8152. END;
  8153. ELSE
  8154. ReleaseIntermediateOperand(result.tag);
  8155. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8156. END;
  8157. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  8158. IF type(SyntaxTree.MathArrayType).form IN {SyntaxTree.Open} THEN
  8159. ReleaseIntermediateOperand(result.tag);
  8160. result.tag := result.op;
  8161. UseIntermediateOperand(result.tag);
  8162. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8163. IntermediateCode.MakeMemory(result.op,addressType);
  8164. END;
  8165. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType = NIL) THEN
  8166. ReleaseIntermediateOperand(result.tag);
  8167. result.tag := TypeDescriptorAdr(type);
  8168. IF ~newObjectFile THEN
  8169. IntermediateCode.MakeMemory(result.tag,addressType);
  8170. END;
  8171. UseIntermediateOperand(result.tag);
  8172. (* tag for pointer type computed not here but during dereferencing *)
  8173. END;
  8174. IF Trace THEN TraceExit("VisitVariable") END;
  8175. END VisitVariable;
  8176. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  8177. BEGIN
  8178. VisitVariable(property);
  8179. END VisitProperty;
  8180. PROCEDURE VisitParameter(x: SyntaxTree.Parameter);
  8181. VAR type: SyntaxTree.Type; basereg, mem: IntermediateCode.Operand; parameter: SyntaxTree.Parameter;adr: LONGINT; symbol: Sections.Section;
  8182. name: Basic.SegmentedName; parameterType, ptype: SyntaxTree.Type; len,inc: LONGINT;
  8183. BEGIN
  8184. type := x.type.resolved;
  8185. IF Trace THEN TraceEnter("VisitParameter") END;
  8186. IF x.ownerType IS SyntaxTree.CellType THEN
  8187. ptype := x.type.resolved;
  8188. IF ptype IS SyntaxTree.ArrayType THEN ptype := ptype(SyntaxTree.ArrayType).arrayBase.resolved END;
  8189. IF ~(ptype IS SyntaxTree.PortType) THEN
  8190. InitOperand(result,ModeReference);
  8191. GetCodeSectionNameForSymbol(x,name);
  8192. symbol := NewSection(module.allSections, Sections.ConstSection, name,x,commentPrintout # NIL);
  8193. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol),0);
  8194. RETURN
  8195. ELSE
  8196. InitOperand(result, ModeValue);
  8197. parameter := x.ownerType(SyntaxTree.CellType).firstParameter;
  8198. adr := 0;
  8199. WHILE parameter # x DO
  8200. parameterType := parameter.type;
  8201. inc := 1;
  8202. WHILE SemanticChecker.IsStaticArray(parameterType, parameterType, len) DO inc := inc * len END;
  8203. INC(adr, inc);
  8204. parameter := parameter.nextParameter
  8205. END;
  8206. adr := backend.activeCellsSpecification.GetPortAddress(adr);
  8207. IntermediateCode.InitImmediate(result.op,addressType,adr);
  8208. RETURN
  8209. END;
  8210. ELSIF ~backend.cellsAreObjects & (currentScope IS SyntaxTree.ProcedureScope) & (currentScope(SyntaxTree.ProcedureScope).ownerProcedure.isConstructor) & (currentScope.outerScope IS SyntaxTree.CellScope) THEN
  8211. InitOperand(result,ModeReference);
  8212. GetCodeSectionNameForSymbol(x,name);
  8213. symbol := NewSection(module.allSections, Sections.VarSection, name,x,commentPrintout # NIL);
  8214. IntermediateCode.InitAddress(result.op, addressType, symbol.name, GetFingerprint(symbol.symbol), 0);
  8215. RETURN
  8216. ELSE
  8217. GetBaseRegister(basereg,currentScope,x.scope);
  8218. InitOperand(result,ModeReference);
  8219. result.op := basereg;
  8220. END;
  8221. IF IsOpenArray(type) THEN
  8222. result.tag := basereg;
  8223. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8224. IntermediateCode.MakeMemory(result.op,addressType);
  8225. IF Global.IsOberonProcedure(x.ownerType) THEN
  8226. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+addressType.sizeInBits));
  8227. UseIntermediateOperand(result.tag);
  8228. ELSE
  8229. IntermediateCode.InitImmediate(result.tag,addressType,MAX(LONGINT)); (* non-Oberon procedure => unbounded array length *)
  8230. END;
  8231. ELSIF IsStaticArray(type) & (x.kind = SyntaxTree.ValueParameter) THEN
  8232. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8233. ELSIF IsStaticArray(type) THEN
  8234. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8235. IntermediateCode.MakeMemory(result.op,addressType);
  8236. IntermediateCode.InitImmediate(result.tag,addressType,type(SyntaxTree.ArrayType).staticLength);
  8237. ELSIF type IS SyntaxTree.MathArrayType THEN
  8238. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8239. WITH type: SyntaxTree.MathArrayType DO
  8240. IF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8241. IF type.form = SyntaxTree.Tensor THEN
  8242. ELSIF type.form = SyntaxTree.Open THEN
  8243. result.tag := result.op;
  8244. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8245. IntermediateCode.MakeMemory(result.op,addressType);
  8246. UseIntermediateOperand(result.tag);
  8247. ELSIF type.form = SyntaxTree.Static THEN
  8248. IF x.kind = SyntaxTree.ConstParameter THEN
  8249. IntermediateCode.MakeMemory(result.op,addressType);
  8250. END;
  8251. ELSE HALT(100)
  8252. END;
  8253. ELSIF x.kind = SyntaxTree.VarParameter THEN
  8254. IF type.form = SyntaxTree.Tensor THEN
  8255. ToMemory(result.op,addressType,0);
  8256. ELSIF type.form = SyntaxTree.Open THEN
  8257. MakeMemory(mem, result.op, addressType, 0); (* offset already added above *)
  8258. ReuseCopy(result.tag, mem);
  8259. ReleaseIntermediateOperand(mem);
  8260. ReleaseIntermediateOperand(result.op);
  8261. MakeMemory(result.op, result.tag, addressType, ToMemoryUnits(system,MathAdrOffset*addressType.sizeInBits));
  8262. ELSIF type.form = SyntaxTree.Static THEN
  8263. IntermediateCode.MakeMemory(result.op,addressType);
  8264. ELSE HALT(100)
  8265. END;
  8266. ELSE HALT(100)
  8267. END;
  8268. END;
  8269. ELSIF (x.kind = SyntaxTree.VarParameter) OR (x.kind = SyntaxTree.ConstParameter) & (type IS SyntaxTree.RecordType) THEN
  8270. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8271. IntermediateCode.MakeMemory(result.op,addressType);
  8272. ELSIF (x.kind = SyntaxTree.ValueParameter) OR (x.kind = SyntaxTree.ConstParameter) THEN
  8273. IntermediateCode.AddOffset(result.op,ToMemoryUnits(system,x.offsetInBits));
  8274. END;
  8275. IF conditional & (x.type.resolved IS SyntaxTree.BooleanType) THEN
  8276. ValueToCondition(result);
  8277. ELSIF type IS SyntaxTree.ProcedureType THEN
  8278. ReleaseIntermediateOperand(result.tag);
  8279. IF type(SyntaxTree.ProcedureType).isDelegate THEN
  8280. IF x.kind = SyntaxTree.VarParameter THEN
  8281. ReuseCopy(result.tag,result.op);
  8282. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,system.addressSize));
  8283. IntermediateCode.MakeMemory(result.tag,addressType);
  8284. ELSE
  8285. IntermediateCode.InitMemory(result.tag,addressType,result.op,ToMemoryUnits(system,system.addressSize));
  8286. UseIntermediateOperand(result.tag);
  8287. END;
  8288. ELSE
  8289. result.tag := nil;
  8290. END;
  8291. (* tag for pointer type computed not here but during dereferencing *)
  8292. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind IN {SyntaxTree.VarParameter, SyntaxTree.ConstParameter}) THEN
  8293. ReleaseIntermediateOperand(result.tag);
  8294. result.tag := basereg;
  8295. IntermediateCode.AddOffset(result.tag,ToMemoryUnits(system,x.offsetInBits+system.addressSize));
  8296. IntermediateCode.MakeMemory(result.tag,addressType);
  8297. UseIntermediateOperand(result.tag);
  8298. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType= NIL) & (x.kind = SyntaxTree.ValueParameter) THEN
  8299. ReleaseIntermediateOperand(result.tag);
  8300. result.tag := TypeDescriptorAdr(type);
  8301. IF ~newObjectFile THEN
  8302. IntermediateCode.MakeMemory(result.tag,addressType);
  8303. END;
  8304. UseIntermediateOperand(result.tag);
  8305. END;
  8306. IF Trace THEN TraceExit("VisitParameter") END;
  8307. END VisitParameter;
  8308. PROCEDURE DynamicCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8309. VAR tag,reg,tmp: IntermediateCode.Operand; offset: LONGINT; recordType: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName;
  8310. BEGIN
  8311. IF Trace THEN TraceEnter("DynamicCallOperand") END;
  8312. (* left.p: left already emitted *)
  8313. tag := result.op; (* value of pointer to left *)
  8314. (* get type desc *)
  8315. tmp := result.tag;
  8316. IntermediateCode.MakeMemory(tmp,addressType);
  8317. (* get method adr *)
  8318. Reuse1(reg,tmp);
  8319. ReleaseIntermediateOperand(tmp);
  8320. IF backend.cooperative THEN
  8321. recordType := x.scope(SyntaxTree.RecordScope).ownerRecord;
  8322. WHILE recordType.baseType # NIL DO
  8323. recordType := recordType.GetBaseRecord ();
  8324. END;
  8325. GetRecordTypeName (recordType,name);
  8326. Basic.ToSegmentedName ("BaseTypes.StackFrame",stackFrame);
  8327. IF (name = stackFrame) OR HasExplicitTraceMethod (recordType) THEN
  8328. offset := 0;
  8329. ELSE
  8330. offset := 2;
  8331. END;
  8332. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset + x.methodNumber + offset)))));
  8333. ELSE
  8334. Emit(Add(position,reg,tmp,IntermediateCode.Immediate(addressType, ToMemoryUnits(system,system.addressSize *(meta.MethodTableOffset - x.methodNumber)))));
  8335. END;
  8336. InitOperand(operand,ModeReference);
  8337. (* then operand.op contains the method adr and operand.tag contains the potential self pointer value *)
  8338. operand.op := reg;
  8339. operand.tag := tag;
  8340. IF Trace THEN TraceExit("DynamicCallOperand") END;
  8341. END DynamicCallOperand;
  8342. PROCEDURE StaticCallOperand(VAR operand: Operand; x: SyntaxTree.Procedure);
  8343. VAR source: IntermediateCode.Section; tag,reg: IntermediateCode.Operand; name:Basic.SegmentedName; sectionType: SHORTINT;
  8344. binary: BinaryCode.Section; bits: SyntaxTree.BinaryCode;
  8345. BEGIN
  8346. IF Trace THEN TraceEnter("StaticCallOperand") END;
  8347. IF x.type(SyntaxTree.ProcedureType).isDelegate THEN
  8348. tag := operand.op;
  8349. ReleaseIntermediateOperand(operand.tag);
  8350. ELSE tag := nil
  8351. END;
  8352. IF x.isInline THEN
  8353. sectionType := Sections.InlineCodeSection;
  8354. ELSE
  8355. sectionType := Sections.CodeSection;
  8356. END;
  8357. IF x.externalName # NIL THEN
  8358. Basic.ToSegmentedName(x.externalName^, name);
  8359. IntermediateCode.InitAddress(reg, addressType, name, 0, 0);
  8360. ELSE
  8361. GetCodeSectionNameForSymbol(x, name);
  8362. IF (x.scope.ownerModule = module.module) THEN
  8363. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8364. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.sourceCode # NIL) THEN
  8365. source := NewSection(module.allSections, sectionType, name,x,commentPrintout # NIL);
  8366. IF source.pc = 0 THEN (* no code yet *)
  8367. source.Emit(Asm(position,x.procedureScope.body.code.sourceCode,NIL,NIL));
  8368. END;
  8369. ELSIF (sectionType = Sections.InlineCodeSection) & (x.procedureScope.body.code.inlineCode # NIL) THEN
  8370. bits := x.procedureScope.body.code.inlineCode;
  8371. source := NewSection(module.allSections, sectionType, name, x, commentPrintout # NIL);
  8372. binary := BinaryCode.NewBinarySection(source.type, source.priority, system.codeUnit, name, FALSE, FALSE);
  8373. binary.CopyBits(bits, 0, bits.GetSize());
  8374. source.SetResolved(binary);
  8375. ELSE
  8376. source := NewSection(module.importedSections, sectionType, name,x,commentPrintout # NIL);
  8377. END;
  8378. IntermediateCode.InitAddress(reg, addressType, source.name , GetFingerprint(source.symbol), 0);
  8379. END;
  8380. InitOperand(operand,ModeValue);
  8381. operand.op := reg;
  8382. operand.tag := tag;
  8383. IF Trace THEN TraceExit("StaticCallOperand") END;
  8384. END StaticCallOperand;
  8385. PROCEDURE VisitProcedure(x: SyntaxTree.Procedure);
  8386. (* handle expressions of the form designator.procedure or procedure *)
  8387. BEGIN
  8388. IF Trace THEN TraceEnter("VisitProcedure") END;
  8389. IF (x.type(SyntaxTree.ProcedureType).isDelegate) & ~SemanticChecker.IsStaticProcedure(x) THEN
  8390. DynamicCallOperand(result,x);
  8391. ELSIF x.isInline THEN
  8392. StaticCallOperand(result,x);
  8393. ELSE
  8394. StaticCallOperand(result,x);
  8395. END;
  8396. IF Trace THEN TraceExit("VisitProcedure") END;
  8397. END VisitProcedure;
  8398. PROCEDURE VisitOperator(x: SyntaxTree.Operator);
  8399. BEGIN
  8400. VisitProcedure(x);
  8401. END VisitOperator;
  8402. (** statements *)
  8403. PROCEDURE VisitProcedureCallStatement(x: SyntaxTree.ProcedureCallStatement);
  8404. BEGIN
  8405. IF Trace THEN TraceEnter("VisitProcedureCallStatement") END;
  8406. Expression(x.call);
  8407. IF (x.call.type # NIL) THEN (* WINAPI call: procedure returning unused value *)
  8408. ReleaseOperand(result)
  8409. END;
  8410. IF Trace THEN TraceExit("VisitProcedureCallStatement") END;
  8411. END VisitProcedureCallStatement;
  8412. PROCEDURE AssignMathArray(left,right: SyntaxTree.Expression);
  8413. VAR leftType, rightType: SyntaxTree.MathArrayType;
  8414. leftBase, rightBase: SyntaxTree.Type;
  8415. procedureName,s: SyntaxTree.IdentifierString;
  8416. arrayBase: SyntaxTree.Module; saved: RegisterEntry; procedure: SyntaxTree.Procedure; parameter: SyntaxTree.Parameter;
  8417. size: LONGINT; rightKind: LONGINT;
  8418. dummy: IntermediateCode.Operand;
  8419. CONST moduleName = "FoxArrayBase";
  8420. PROCEDURE OpenArray(from: SyntaxTree.MathArrayType): SyntaxTree.MathArrayType;
  8421. VAR result: SyntaxTree.MathArrayType; base: SyntaxTree.Type;
  8422. BEGIN
  8423. base := from(SyntaxTree.MathArrayType).arrayBase.resolved;
  8424. IF base IS SyntaxTree.MathArrayType THEN
  8425. base := OpenArray(base(SyntaxTree.MathArrayType));
  8426. END;
  8427. result := SyntaxTree.NewMathArrayType(left.position,currentScope,SyntaxTree.Open);
  8428. result.SetArrayBase(base);
  8429. RETURN result
  8430. END OpenArray;
  8431. BEGIN
  8432. IF AddImport(moduleName,arrayBase,TRUE) THEN
  8433. SaveRegisters();ReleaseUsedRegisters(saved);
  8434. leftType := left.type.resolved(SyntaxTree.MathArrayType);
  8435. rightType := right.type.resolved(SyntaxTree.MathArrayType);
  8436. leftBase := SemanticChecker.ArrayBase(leftType,MAX(LONGINT));
  8437. rightBase := SemanticChecker.ArrayBase(rightType,MAX(LONGINT));
  8438. ASSERT(leftBase.resolved.SameType(rightBase.resolved));
  8439. IF leftType.form = SyntaxTree.Tensor THEN
  8440. procedureName := "CopyTensor"; rightKind := SyntaxTree.ValueParameter;
  8441. ELSIF leftType.form = SyntaxTree.Open THEN
  8442. procedureName := "CopyArray"; rightKind := SyntaxTree.VarParameter;
  8443. ELSIF leftType.form = SyntaxTree.Static THEN
  8444. procedureName := "CopyArray";rightKind := SyntaxTree.VarParameter;
  8445. leftType := OpenArray(leftType); (* necessary since copy procedure presumes an open array *)
  8446. END;
  8447. procedure := arrayBase.moduleScope.FindProcedure(SyntaxTree.NewIdentifier(procedureName));
  8448. IF procedure = NIL THEN
  8449. s := "Instruction not supported on target, emulation procedure ";
  8450. Strings.Append(s,moduleName);
  8451. Strings.Append(s,".");
  8452. Strings.Append(s,procedureName);
  8453. Strings.Append(s," not present");
  8454. Error(position,s);
  8455. ELSE
  8456. parameter := SyntaxTree.NewParameter(left.position,procedure.type(SyntaxTree.ProcedureType),SyntaxTree.NewIdentifier("temp"), SyntaxTree.VarParameter);
  8457. parameter.SetType(leftType);
  8458. parameter.SetAccess(SyntaxTree.Internal);
  8459. PushParameter(left,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8460. parameter.SetKind(rightKind);
  8461. PushParameter(right,parameter,SyntaxTree.OberonCallingConvention, FALSE, dummy,-1);
  8462. size := ToMemoryUnits(system,system.SizeOf(rightBase));
  8463. Emit(Push(position,IntermediateCode.Immediate(int32,size)));
  8464. StaticCallOperand(result,procedure);
  8465. Emit(Call(position,result.op,ProcedureParametersSize(system,procedure)));
  8466. ReleaseOperand(result);
  8467. END;
  8468. RestoreRegisters(saved);
  8469. END;
  8470. END AssignMathArray;
  8471. VAR modifyAssignmentCounter := 0: LONGINT;
  8472. PROCEDURE ModifyAssignments(CONST value: IntermediateCode.Operand);
  8473. VAR processor,mem,dst: IntermediateCode.Operand;
  8474. BEGIN
  8475. IF value.intValue = true.intValue THEN
  8476. INC(modifyAssignmentCounter);
  8477. IF (modifyAssignmentCounter > 1) THEN RETURN END;
  8478. modifyAssignmentsPC := section.pc;
  8479. ELSE
  8480. DEC(modifyAssignmentCounter);
  8481. IF (modifyAssignmentCounter > 0) THEN RETURN END;
  8482. INC(statCoopModifyAssignments , section.pc - modifyAssignmentsPC);
  8483. END;
  8484. IntermediateCode.InitMemory (processor, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, ProcessorOffset * addressType.sizeInBits));
  8485. dst := NewRegisterOperand (addressType);
  8486. Emit(Mov(position,dst, processor));
  8487. IntermediateCode.InitMemory(mem,bool, dst, 0);
  8488. Emit(Mov(position,mem, value));
  8489. ReleaseIntermediateOperand(dst);
  8490. END ModifyAssignments;
  8491. PROCEDURE CopySize(left: SyntaxTree.Expression): IntermediateCode.Operand;
  8492. VAR type: SyntaxTree.Type; procedureType: SyntaxTree.ProcedureType; parameter: SyntaxTree.Parameter;
  8493. BEGIN
  8494. type := left.type.resolved;
  8495. IF (type IS SyntaxTree.RecordType) & (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  8496. parameter := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter);
  8497. procedureType := parameter.ownerType.resolved(SyntaxTree.ProcedureType);
  8498. IF procedureType.returnParameter = parameter THEN
  8499. RETURN IntermediateCode.Memory(addressType, fp, ToMemoryUnits(system, parameter.offsetInBits + system.addressSize));
  8500. END;
  8501. END;
  8502. RETURN IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(type)));
  8503. END CopySize;
  8504. PROCEDURE Assign(left,right: SyntaxTree.Expression);
  8505. VAR
  8506. leftO, rightO: Operand;
  8507. mem, sizeOp: IntermediateCode.Operand;
  8508. leftType, rightType, componentType: SyntaxTree.Type;
  8509. size: LONGINT;
  8510. parameters: SyntaxTree.ExpressionList;
  8511. procedure: SyntaxTree.Procedure;
  8512. call: SyntaxTree.ProcedureCallDesignator;
  8513. designator: SyntaxTree.Designator;
  8514. PROCEDURE CanPassAsResultParameter(right: SyntaxTree.Expression): BOOLEAN;
  8515. VAR procedureType: SyntaxTree.ProcedureType;
  8516. BEGIN
  8517. IF ReturnedAsParameter(right.type) THEN
  8518. IF right IS SyntaxTree.ProcedureCallDesignator THEN
  8519. procedureType := right(SyntaxTree.ProcedureCallDesignator).left.type.resolved(SyntaxTree.ProcedureType);
  8520. RETURN procedureType.callingConvention = SyntaxTree.OberonCallingConvention
  8521. ELSIF right IS SyntaxTree.BuiltinCallDesignator THEN
  8522. WITH right: SyntaxTree.BuiltinCallDesignator DO
  8523. IF right.id = Global.Reshape THEN RETURN TRUE
  8524. END;
  8525. END;
  8526. END;
  8527. END;
  8528. RETURN FALSE
  8529. END CanPassAsResultParameter;
  8530. BEGIN
  8531. ASSERT(left.type # NIL); ASSERT(right.type # NIL);
  8532. leftType := left.type.resolved; rightType:= right.type.resolved;
  8533. IF backend.cooperative & left.NeedsTrace() THEN
  8534. ModifyAssignments(true);
  8535. IF (leftType IS SyntaxTree.RecordType) OR IsStaticArray(leftType) THEN
  8536. Designate(right, rightO);
  8537. Designate(left, leftO);
  8538. ASSERT(leftO.mode = ModeReference);
  8539. TransferToRegister(leftO.op, leftO.op);
  8540. TransferToRegister(rightO.op, rightO.op);
  8541. Emit(Push(position, leftO.op));
  8542. Emit(Push(position, rightO.op));
  8543. CallAssignMethod(leftO.op, rightO.op, left.type);
  8544. Emit(Pop(position, rightO.op));
  8545. Emit(Pop(position, leftO.op));
  8546. sizeOp := CopySize(left);
  8547. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8548. ReleaseOperand(leftO);
  8549. ReleaseOperand(rightO);
  8550. ELSE
  8551. Evaluate(right,rightO);
  8552. Designate(left,leftO);
  8553. ASSERT(leftO.mode = ModeReference);
  8554. IF (leftType IS SyntaxTree.ProcedureType) THEN
  8555. (* copy procedure address first *)
  8556. MakeMemory(mem,leftO.op,addressType,0);
  8557. Emit(Mov(position,mem,rightO.op));
  8558. ReleaseIntermediateOperand(mem);
  8559. (* copy pointer address *)
  8560. IntermediateCode.MakeAddress(leftO.tag, addressType);
  8561. CallAssignPointer(leftO.tag, rightO.tag);
  8562. ELSE
  8563. ASSERT(system.SizeOf(left.type) = system.addressSize);
  8564. CallAssignPointer(leftO.op, rightO.op);
  8565. END;
  8566. ReleaseOperand(leftO);
  8567. ReleaseOperand(rightO);
  8568. END;
  8569. ModifyAssignments(false);
  8570. RETURN;
  8571. END;
  8572. IF CanPassAsResultParameter(right) THEN
  8573. procedureResultDesignator := left(SyntaxTree.Designator);
  8574. Expression(right);
  8575. procedureResultDesignator := NIL;
  8576. ELSIF (right IS SyntaxTree.UnaryExpression) & (right(SyntaxTree.UnaryExpression).operator = Scanner.Alias) THEN
  8577. (* left <-- ALIAS OF right: zerocopy *)
  8578. IF GetRuntimeProcedure("FoxArrayBase","ZeroCopy",procedure,TRUE) THEN
  8579. designator := SyntaxTree.NewSymbolDesignator(Diagnostics.Invalid, NIL, procedure);
  8580. designator.SetType(procedure.type);
  8581. parameters := SyntaxTree.NewExpressionList(); parameters.AddExpression(right(SyntaxTree.UnaryExpression).left); parameters.AddExpression(left);
  8582. call := SyntaxTree.NewProcedureCallDesignator(position,designator(SyntaxTree.Designator),parameters);
  8583. VisitProcedureCallDesignator(call(SyntaxTree.ProcedureCallDesignator));
  8584. END;
  8585. ELSIF leftType IS SyntaxTree.RangeType THEN
  8586. (* LHS is of array range type *)
  8587. ASSERT(rightType IS SyntaxTree.RangeType); (* ensured by the checker *)
  8588. Evaluate(right, rightO);
  8589. Designate(left, leftO);(* The order is crucial. Do not reorder emission of left and right *)
  8590. (* first *)
  8591. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 0);
  8592. Emit(Mov(position,mem, rightO.op));
  8593. ReleaseIntermediateOperand(mem);
  8594. (* last *)
  8595. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8596. Emit(Mov(position,mem, rightO.tag));
  8597. ReleaseIntermediateOperand(mem);
  8598. (* step *)
  8599. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  8600. Emit(Mov(position,mem, rightO.extra));
  8601. ReleaseIntermediateOperand(mem);
  8602. ReleaseOperand(rightO);
  8603. ReleaseOperand(leftO)
  8604. ELSIF leftType IS SyntaxTree.ComplexType THEN
  8605. ASSERT(leftType.SameType(rightType)); (* ensured by the checker *)
  8606. Evaluate(right, rightO);
  8607. Designate(left, leftO); (* The order is crucial. Do not reorder emission of left and right *)
  8608. componentType := leftType(SyntaxTree.ComplexType).componentType;
  8609. (* real part *)
  8610. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), 0);
  8611. Emit(Mov(position,mem, rightO.op));
  8612. ReleaseIntermediateOperand(mem);
  8613. (* imaginary part *)
  8614. MakeMemory(mem, leftO.op, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  8615. Emit(Mov(position,mem, rightO.tag));
  8616. ReleaseIntermediateOperand(mem);
  8617. ReleaseOperand(rightO);
  8618. ReleaseOperand(leftO)
  8619. ELSIF (leftType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.PointerType) OR (leftType IS SyntaxTree.EnumerationType)
  8620. OR (leftType IS SyntaxTree.PortType) THEN
  8621. (* rightO := leftO;*)
  8622. Evaluate(right,rightO);
  8623. (* DO NOT REORDER EMISSION OF LEFT AND RIGHT OPERAND *)
  8624. Designate(left,leftO);
  8625. IF leftO.mode = ModeReference THEN
  8626. MakeMemory(mem,leftO.op,IntermediateCode.GetType(system,left.type),0);
  8627. destination := mem;
  8628. ELSE
  8629. destination := leftO.op;
  8630. END;
  8631. ReleaseOperand(leftO);
  8632. IF destination.mode # IntermediateCode.Undefined THEN
  8633. Emit(Mov(position,destination,rightO.op));
  8634. END;
  8635. ReleaseOperand(rightO);
  8636. ReleaseIntermediateOperand(mem);
  8637. IntermediateCode.InitOperand(destination);
  8638. ELSIF (leftType IS SyntaxTree.ProcedureType) THEN
  8639. Evaluate(right,rightO);
  8640. Designate(left,leftO);
  8641. MakeMemory(mem,leftO.op,addressType,0);
  8642. Emit(Mov(position,mem,rightO.op));
  8643. ReleaseIntermediateOperand(mem);
  8644. IF leftType(SyntaxTree.ProcedureType).isDelegate THEN
  8645. (* delegate *)
  8646. (*
  8647. MakeMemory(leftO.tag,leftO.tag,addressType); no! is already memory
  8648. *)
  8649. Emit(Mov(position,leftO.tag,rightO.tag));
  8650. END;
  8651. ReleaseOperand(leftO);
  8652. ReleaseOperand(rightO);
  8653. ELSIF (leftType IS SyntaxTree.RecordType) THEN
  8654. Designate(right,rightO);
  8655. Designate(left,leftO);
  8656. sizeOp := CopySize(left);
  8657. Emit(Copy(position,leftO.op,rightO.op,sizeOp));
  8658. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8659. ELSIF (leftType IS SyntaxTree.ArrayType) THEN
  8660. IF (rightType IS SyntaxTree.StringType) THEN
  8661. CopyString(left,right);
  8662. 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
  8663. Designate(right,rightO);
  8664. Designate(left,leftO);
  8665. size := ToMemoryUnits(system,system.SizeOf(rightType));
  8666. Emit(Copy(position,leftO.op, rightO.op, IntermediateCode.Immediate(addressType,size)));
  8667. ReleaseOperand(leftO); ReleaseOperand(rightO);
  8668. ELSE
  8669. HALT(201)
  8670. END;
  8671. ELSIF (leftType IS SyntaxTree.MathArrayType) THEN
  8672. AssignMathArray(left,right);
  8673. ELSE
  8674. HALT(200);
  8675. END;
  8676. END Assign;
  8677. PROCEDURE VisitAssignment(x: SyntaxTree.Assignment);
  8678. BEGIN
  8679. IF Trace THEN TraceEnter("VisitAssignment") END;
  8680. Assign(x.left,x.right);
  8681. IF Trace THEN TraceExit("VisitAssignment") END;
  8682. END VisitAssignment;
  8683. PROCEDURE EmitCooperativeSwitch;
  8684. VAR quantum, offset, zero: IntermediateCode.Operand; skip: Label; pc: LONGINT;
  8685. BEGIN
  8686. ASSERT (cooperativeSwitches);
  8687. pc := section.pc;
  8688. IF lastSwitchPC = section.pc THEN RETURN END;
  8689. IntermediateCode.InitMemory (quantum, IntermediateCode.SignedIntegerType(addressType.sizeInBits), ap, ToMemoryUnits(system, QuantumOffset * addressType.sizeInBits));
  8690. IntermediateCode.InitImmediate(offset, quantum.type, section.pc - lastSwitchPC); IntermediateCode.InitImmediate(zero, quantum.type, 0);
  8691. Emit(Sub(position,quantum,quantum, offset)); skip := NewLabel(); BrgeL(skip, quantum, zero);
  8692. lastSwitchPC := section.pc; CallThis(position,"Activities","Switch",0); SetLabel(skip);
  8693. INC(statCoopSwitch, section.pc - pc);
  8694. END EmitCooperativeSwitch;
  8695. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  8696. VAR p0,p1,tmp: SyntaxTree.Expression; s0,s1: Operand; size: LONGINT;
  8697. BEGIN
  8698. p0 := communication.left; p1 := communication.right;
  8699. IF (communication.op = Scanner.ExclamationMark) OR (communication.op = Scanner.LessLess) & (communication.left.type.resolved IS SyntaxTree.PortType) THEN
  8700. Evaluate(p0,s0);
  8701. Evaluate(p1,s1);
  8702. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8703. Emit(Push(position,s0.op));
  8704. Emit(Push(position,s1.op));
  8705. (*
  8706. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8707. *)
  8708. IF ~backend.cellsAreObjects THEN
  8709. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"send not implemented for complex data types") END;
  8710. END;
  8711. ReleaseOperand(s0);
  8712. ReleaseOperand(s1);
  8713. IF backend.cellsAreObjects THEN
  8714. CallThis(position,"ActiveCellsRuntime","Send",2);
  8715. ELSE
  8716. CallThis(position,ChannelModuleName,"Send",2);
  8717. END;
  8718. (* ----- RECEIVE ------*)
  8719. ELSE
  8720. IF (communication.op = Scanner.LessLess) & (communication.right.type.resolved IS SyntaxTree.PortType) THEN
  8721. tmp := p0; p0 := p1; p1 := tmp;
  8722. END;
  8723. Evaluate(p0,s0);
  8724. Emit(Push(position,s0.op));
  8725. Designate(p1,s1);
  8726. size := ToMemoryUnits(system,system.SizeOf(p1.type));
  8727. Emit(Push(position,s1.op));
  8728. (*
  8729. Emit(Push(position,IntermediateCode.Immediate(addressType,size)));
  8730. *)
  8731. IF ~backend.cellsAreObjects THEN
  8732. IF size > ToMemoryUnits(system, system.addressSize) THEN Error(p1.position,"receive not implemented for complex data types") END;
  8733. END;
  8734. ReleaseOperand(s0);
  8735. ReleaseOperand(s1);
  8736. IF backend.cellsAreObjects THEN
  8737. CallThis(position,"ActiveCellsRuntime","Receive",2);
  8738. ELSE
  8739. CallThis(position,ChannelModuleName,"Receive",2)
  8740. END;
  8741. END;
  8742. END VisitCommunicationStatement;
  8743. PROCEDURE VisitIfStatement(x: SyntaxTree.IfStatement);
  8744. VAR end: Label; i,elsifs: LONGINT; elsif: SyntaxTree.IfPart; escape: BOOLEAN;
  8745. PROCEDURE IfPart(if: SyntaxTree.IfPart);
  8746. VAR true, false: Label; condition, value: BOOLEAN;
  8747. BEGIN
  8748. condition := ~SemanticChecker.IsBooleanValue(if.condition, value);
  8749. IF condition THEN
  8750. true := NewLabel();
  8751. false := NewLabel();
  8752. Condition(if.condition,true,false);
  8753. SetLabel(true);
  8754. StatementSequence(if.statements);
  8755. BrL(end);
  8756. SetLabel(false);
  8757. ELSE
  8758. IF value THEN (* always true *)
  8759. escape := TRUE;
  8760. StatementSequence(if.statements);
  8761. (* no branch necessary -- rest skipped *)
  8762. END;
  8763. END;
  8764. END IfPart;
  8765. BEGIN
  8766. IF Trace THEN TraceEnter("VisitIfStatement") END;
  8767. end := NewLabel();
  8768. elsifs := x.ElsifParts();
  8769. IfPart(x.ifPart);
  8770. FOR i := 0 TO elsifs-1 DO
  8771. IF ~escape THEN
  8772. elsif := x.GetElsifPart(i);
  8773. IfPart(elsif);
  8774. END;
  8775. END;
  8776. IF (x.elsePart # NIL) & ~escape THEN
  8777. StatementSequence(x.elsePart);
  8778. END;
  8779. SetLabel(end);
  8780. IF Trace THEN TraceExit("VisitIfStatement") END;
  8781. END VisitIfStatement;
  8782. PROCEDURE WithPart(x: SyntaxTree.WithPart; VAR falseL, endL: Label);
  8783. VAR trueL: Label; res: Operand; recordType: SyntaxTree.RecordType;
  8784. BEGIN
  8785. (*IF x.variable.type.resolved = x.type.resolved THEN
  8786. (* always true, do nothing *)
  8787. ELSE*)
  8788. Designate(x.variable,res);
  8789. IF IsPointerToRecord(x.variable.type,recordType) THEN
  8790. Dereference(res,recordType,IsUnsafePointer(x.variable.type))
  8791. END;
  8792. trueL := NewLabel();
  8793. TypeTest(res.tag,x.type,trueL,falseL);
  8794. ReleaseOperand(res);
  8795. SetLabel(trueL);
  8796. StatementSequence(x.statements);
  8797. BrL(endL);
  8798. END WithPart;
  8799. PROCEDURE VisitWithStatement(x: SyntaxTree.WithStatement);
  8800. VAR endL,falseL: Label;i: LONGINT;
  8801. BEGIN
  8802. IF Trace THEN TraceEnter("VisitWithStatement") END;
  8803. endL := NewLabel();
  8804. FOR i := 0 TO x.WithParts()-1 DO
  8805. falseL := NewLabel();
  8806. WithPart(x.GetWithPart(i),falseL,endL);
  8807. SetLabel(falseL);
  8808. END;
  8809. IF x.elsePart = NIL THEN
  8810. IF ~isUnchecked THEN
  8811. EmitTrap(position,WithTrap);
  8812. END;
  8813. ELSE
  8814. StatementSequence(x.elsePart)
  8815. END;
  8816. SetLabel(endL);
  8817. IF Trace THEN TraceExit("VisitWithStatement") END;
  8818. END VisitWithStatement;
  8819. PROCEDURE VisitCaseStatement(x: SyntaxTree.CaseStatement);
  8820. VAR var: Operand; jmp,res,op,tmp: IntermediateCode.Operand; j,i,size: LONGINT; part: SyntaxTree.CasePart; constant: SyntaxTree.CaseConstant;
  8821. out,else: Label; label: Label;
  8822. fixups: POINTER TO ARRAY OF Label; section: IntermediateCode.Section; name: Basic.SegmentedName; string: ARRAY 32 OF CHAR;
  8823. symbol: SyntaxTree.Symbol;
  8824. BEGIN
  8825. (*! split case statement into if-elsif statements for large case label lists *)
  8826. IF Trace THEN TraceEnter("VisitCaseStatement") END;
  8827. size := x.max-x.min+1;
  8828. IF (size<0) OR (size > 1024*1024) THEN Error(x.position,"implementation restriction: case table size too large"); RETURN
  8829. END;
  8830. Evaluate(x.variable,var);
  8831. ReuseCopy(tmp,var.op);
  8832. ReleaseIntermediateOperand(var.op);
  8833. var.op := tmp;
  8834. Emit(Sub(position,var.op,var.op,IntermediateCode.Immediate(IntermediateCode.GetType(system,x.variable.type),x.min)));
  8835. Convert(var.op,addressType);
  8836. size := x.max-x.min+1;
  8837. else := NewLabel();
  8838. BrgeL(else,var.op,IntermediateCode.Immediate(addressType,size));
  8839. (*
  8840. UniqueId(name,module.module,"case",caseId);
  8841. *)
  8842. string := "@case"; Basic.AppendNumber(string, caseId); INC(caseId);
  8843. Global.GetModuleSegmentedName(module.module, name);
  8844. Basic.SuffixSegmentedName(name,Basic.MakeString(string));
  8845. symbol := SyntaxTree.NewSymbol(name[1]);
  8846. symbol.SetScope(moduleScope);
  8847. NEW(fixups,size); FOR i := 0 TO size-1 DO fixups[i] := NIL END;
  8848. section := NewSection(module.allSections, Sections.ConstSection,name,SyntaxTree.NewSymbol(name[1]),commentPrintout # NIL);
  8849. section.isCaseTable := TRUE;
  8850. IntermediateCode.InitAddress(jmp, addressType, section.name, GetFingerprint(section.symbol), 0);
  8851. ReuseCopy(res,var.op);
  8852. ReleaseOperand(var);
  8853. Emit(Mul(position,res,res,IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.addressSize))));
  8854. Emit(Add(position,res,res,jmp));
  8855. IntermediateCode.MakeMemory(res,addressType);
  8856. Emit(Br(position,res));
  8857. ReleaseIntermediateOperand(res);
  8858. out := NewLabel();
  8859. FOR i := 0 TO x.caseParts.Length()-1 DO (* case parts *)
  8860. part := x.GetCasePart(i);
  8861. constant := part.firstConstant;
  8862. label := NewLabel();
  8863. SetLabel(label);
  8864. WHILE(constant # NIL) DO (* case labels for this case part *)
  8865. FOR j := constant.min TO constant.max DO
  8866. fixups[j-x.min] := label;
  8867. END;
  8868. constant := constant.next;
  8869. END;
  8870. StatementSequence(part.statements);
  8871. BrL(out);
  8872. END;
  8873. SetLabel(else);
  8874. FOR i := 0 TO size-1 DO
  8875. IF fixups[i] = NIL THEN
  8876. fixups[i] := else;
  8877. END;
  8878. END;
  8879. IF x.elsePart # NIL THEN
  8880. StatementSequence(x.elsePart);
  8881. ELSIF ~isUnchecked THEN
  8882. EmitTrap(position,CaseTrap);
  8883. END;
  8884. SetLabel(out);
  8885. FOR i := 0 TO size-1 DO
  8886. IntermediateCode.InitAddress(op, addressType, fixups[i].section.name, GetFingerprint(fixups[i].section.symbol), fixups[i].pc);
  8887. section.Emit(Data(position,op));
  8888. END;
  8889. IF Trace THEN TraceExit("VisitCaseStatement") END;
  8890. END VisitCaseStatement;
  8891. PROCEDURE VisitWhileStatement(x: SyntaxTree.WhileStatement);
  8892. VAR start: Label; true,false: Label;
  8893. BEGIN
  8894. IF Trace THEN TraceEnter("VisitWhileStatement") END;
  8895. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8896. start := NewLabel();
  8897. true := NewLabel();
  8898. false := NewLabel();
  8899. SetLabel(start);
  8900. Condition(x.condition,true,false);
  8901. SetLabel(true);
  8902. StatementSequence(x.statements);
  8903. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8904. BrL(start);
  8905. SetLabel(false);
  8906. IF Trace THEN TraceExit("VisitWhileStatement") END;
  8907. END VisitWhileStatement;
  8908. PROCEDURE VisitRepeatStatement(x: SyntaxTree.RepeatStatement);
  8909. VAR false,true: Label;
  8910. BEGIN
  8911. IF Trace THEN TraceEnter("VisitRepeatStatement") END;
  8912. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8913. true := NewLabel();
  8914. false := NewLabel();
  8915. SetLabel(false);
  8916. StatementSequence(x.statements);
  8917. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8918. Condition(x.condition,true,false);
  8919. SetLabel(true);
  8920. IF Trace THEN TraceExit("VisitRepeatStatement") END;
  8921. END VisitRepeatStatement;
  8922. PROCEDURE VisitForStatement(x: SyntaxTree.ForStatement);
  8923. VAR
  8924. binary: SyntaxTree.BinaryExpression; start,true,false : Label; cmp: LONGINT; by: HUGEINT;
  8925. temporaryVariable: SyntaxTree.Variable;
  8926. temporaryVariableDesignator : SyntaxTree.Designator;
  8927. BEGIN
  8928. IF Trace THEN TraceEnter("VisitForStatement") END;
  8929. true := NewLabel();
  8930. false := NewLabel();
  8931. start := NewLabel();
  8932. Assign(x.variable,x.from);
  8933. temporaryVariable := GetTemporaryVariable(x.variable.type, FALSE);
  8934. temporaryVariableDesignator := SyntaxTree.NewSymbolDesignator(SemanticChecker.InvalidPosition, NIL, temporaryVariable);
  8935. temporaryVariableDesignator.SetType(x.variable.type.resolved);
  8936. Assign(temporaryVariableDesignator,x.to);
  8937. IF x.by = NIL THEN by := 1 ELSE by := x.by.resolved(SyntaxTree.IntegerValue).hvalue END;
  8938. IF by > 0 THEN
  8939. cmp := Scanner.LessEqual
  8940. ELSE
  8941. cmp := Scanner.GreaterEqual
  8942. END;
  8943. binary := SyntaxTree.NewBinaryExpression(0,x.variable,temporaryVariableDesignator,cmp);
  8944. binary.SetType(system.booleanType);
  8945. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8946. SetLabel(start);
  8947. Condition(binary,true,false);
  8948. SetLabel(true);
  8949. StatementSequence(x.statements);
  8950. binary := SyntaxTree.NewBinaryExpression(0,x.variable,x.by,Scanner.Plus);
  8951. binary.SetType(x.variable.type);
  8952. Assign(x.variable,binary);
  8953. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8954. BrL(start);
  8955. SetLabel(false);
  8956. IF Trace THEN TraceExit("VisitForStatement") END;
  8957. END VisitForStatement;
  8958. PROCEDURE VisitExitableBlock(x: SyntaxTree.ExitableBlock);
  8959. VAR prevLoop: Label;
  8960. BEGIN
  8961. IF Trace THEN TraceEnter("VisitExitableBlock") END;
  8962. prevLoop := currentLoop;
  8963. currentLoop := NewLabel();
  8964. StatementSequence(x.statements);
  8965. SetLabel(currentLoop);
  8966. currentLoop := prevLoop;
  8967. IF Trace THEN TraceExit("VisitExitableBlock") END;
  8968. END VisitExitableBlock;
  8969. PROCEDURE VisitLoopStatement(x: SyntaxTree.LoopStatement);
  8970. VAR prevLoop,start: Label;
  8971. BEGIN
  8972. IF Trace THEN TraceEnter("VisitLoopStatement") END;
  8973. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8974. start := NewLabel();
  8975. prevLoop := currentLoop;
  8976. SetLabel(start);
  8977. currentLoop := NewLabel();
  8978. StatementSequence(x.statements);
  8979. IF cooperativeSwitches THEN EmitCooperativeSwitch END;
  8980. BrL(start);
  8981. SetLabel(currentLoop);
  8982. currentLoop := prevLoop;
  8983. IF Trace THEN TraceExit("VisitLoopStatement") END;
  8984. END VisitLoopStatement;
  8985. PROCEDURE VisitExitStatement(x: SyntaxTree.ExitStatement);
  8986. VAR outer: SyntaxTree.Statement;
  8987. BEGIN
  8988. IF Trace THEN TraceEnter("VisitExitStatement") END;
  8989. IF locked THEN (* r if we jump out of an exclusive block *)
  8990. outer := x.outer;
  8991. WHILE ~(outer IS SyntaxTree.ExitableBlock) & ~((outer IS SyntaxTree.StatementBlock) & outer(SyntaxTree.StatementBlock).isExclusive) DO
  8992. outer := outer.outer;
  8993. END;
  8994. IF ~(outer IS SyntaxTree.ExitableBlock) THEN
  8995. Lock(FALSE);
  8996. END;
  8997. END;
  8998. BrL(currentLoop);
  8999. IF Trace THEN TraceExit("VisitExitStatement") END;
  9000. END VisitExitStatement;
  9001. PROCEDURE VisitReturnStatement(x: SyntaxTree.ReturnStatement);
  9002. VAR
  9003. expression, parameterDesignator: SyntaxTree.Expression;
  9004. type, componentType: SyntaxTree.Type;
  9005. res, right: Operand;
  9006. left, mem, reg: IntermediateCode.Operand;
  9007. parameter: SyntaxTree.Parameter;
  9008. procedure: SyntaxTree.Procedure;
  9009. procedureType: SyntaxTree.ProcedureType;
  9010. returnTypeOffset: LONGINT;
  9011. delegate: BOOLEAN;
  9012. map: SymbolMap;
  9013. BEGIN
  9014. IF Trace THEN TraceEnter("VisitReturnStatement") END;
  9015. expression := x.returnValue;
  9016. procedure := currentScope(SyntaxTree.ProcedureScope).ownerProcedure;
  9017. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9018. IF currentIsInline THEN
  9019. IF expression # NIL THEN
  9020. map := currentMapper.Get(NIL);
  9021. IF map # NIL THEN
  9022. Assign(map.to, expression);
  9023. END;
  9024. END;
  9025. BrL(currentInlineExit);
  9026. RETURN;
  9027. END;
  9028. IF expression # NIL THEN
  9029. type := expression.type.resolved;
  9030. IF (expression IS SyntaxTree.ResultDesignator) THEN
  9031. IF locked THEN Lock(FALSE) END;
  9032. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9033. (* "RETURN RESULT" -> no assignment, it is assumed that result has been written to return parameter via structured return type *)
  9034. ELSIF (type IS SyntaxTree.BasicType) & ~(type IS SyntaxTree.RangeType) & ~(type IS SyntaxTree.ComplexType) & ~type.IsPointer() OR (procedureType.callingConvention # SyntaxTree.OberonCallingConvention) THEN
  9035. (* return without structured return parameter *)
  9036. Evaluate(expression,res);
  9037. delegate := (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate);
  9038. IF locked OR profile THEN
  9039. Emit(Push(position,res.op));
  9040. IF delegate THEN HALT(200) END;
  9041. ReleaseOperand(res);
  9042. IF locked THEN Lock(FALSE) END;
  9043. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9044. reg := NewRegisterOperand(res.op.type);
  9045. Emit(Pop(position,reg));
  9046. Emit(Return(position,reg));
  9047. ReleaseIntermediateOperand(reg);
  9048. ELSE
  9049. Emit(Return(position,res.op));
  9050. ReleaseOperand(res);
  9051. END;
  9052. ELSIF (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR type.IsPointer()
  9053. THEN
  9054. (* return using structured return parameter *)
  9055. 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)
  9056. OR SemanticChecker.IsPointerType(type));
  9057. (* parameter := currentScope(SyntaxTree.ProcedureScope).FindParameter(Global.ReturnParameterName); *)
  9058. parameter :=procedureType.returnParameter;
  9059. ASSERT(parameter # NIL);
  9060. returnTypeOffset := parameter.offsetInBits;
  9061. (*
  9062. IF parameter# NIL THEN
  9063. returnTypeOffset := parameter.offsetInBits + system.SizeOfParameter(parameter);
  9064. INC(returnTypeOffset,(-returnTypeOffset) MOD system.AlignmentOf(system.parameterAlignment,parameter.type));
  9065. ELSE
  9066. returnTypeOffset := system.offsetFirstParameter
  9067. END;
  9068. *)
  9069. left := IntermediateCode.Memory(addressType,fp,ToMemoryUnits(system,returnTypeOffset));
  9070. IF type IS SyntaxTree.RangeType THEN
  9071. (* array range type *)
  9072. Evaluate(expression, right);
  9073. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 0);
  9074. Emit(Mov(position,mem, right.op)); (* first *)
  9075. ReleaseIntermediateOperand(mem);
  9076. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9077. Emit(Mov(position,mem, right.tag)); (* last *)
  9078. ReleaseIntermediateOperand(mem);
  9079. MakeMemory(mem, left, IntermediateCode.GetType(system, system.longintType), 2 * ToMemoryUnits(system, system.SizeOf(system.longintType)));
  9080. Emit(Mov(position,mem, right.extra)); (* step *)
  9081. ReleaseIntermediateOperand(mem);
  9082. ReleaseOperand(right);
  9083. ELSIF type IS SyntaxTree.ComplexType THEN
  9084. Evaluate(expression, right);
  9085. componentType := type(SyntaxTree.ComplexType).componentType;
  9086. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), 0);
  9087. Emit(Mov(position,mem, right.op)); (* real part *)
  9088. ReleaseIntermediateOperand(mem);
  9089. MakeMemory(mem, left, IntermediateCode.GetType(system, componentType), ToMemoryUnits(system, system.SizeOf(componentType)));
  9090. Emit(Mov(position,mem, right.tag)); (* imaginary part *)
  9091. ReleaseIntermediateOperand(mem);
  9092. ReleaseOperand(right);
  9093. ELSE (* covers cases: pointer / record / array *)
  9094. parameter := procedureType.returnParameter;
  9095. checker.SetCurrentScope(currentScope);
  9096. ASSERT(parameter # NIL);
  9097. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9098. Assign(parameterDesignator,expression);
  9099. END;
  9100. ReleaseIntermediateOperand(left);
  9101. IF locked THEN Lock(FALSE) END;
  9102. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9103. ELSIF (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.ProcedureType) THEN
  9104. parameter := procedureType.returnParameter;
  9105. checker.SetCurrentScope(currentScope);
  9106. IF parameter = NIL THEN
  9107. Error(procedure.position, "structured return of parameter of procedure not found");
  9108. ELSE
  9109. parameterDesignator := checker.NewSymbolDesignator(expression.position,NIL,parameter);
  9110. Assign(parameterDesignator,expression);
  9111. END;
  9112. IF locked THEN Lock(FALSE) END;
  9113. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9114. ELSE
  9115. HALT(200);
  9116. END;
  9117. ELSE
  9118. IF locked THEN Lock(FALSE) END;
  9119. IF ~backend.cooperative & profile THEN ProfilerEnterExit(numberProcedures,FALSE) END;
  9120. END;
  9121. IF backend.cooperative THEN
  9122. BrL(exitLabel);
  9123. ELSE
  9124. EmitLeave(section, position,procedure.type(SyntaxTree.ProcedureType).callingConvention);
  9125. Emit(Exit(position,procedure.type(SyntaxTree.ProcedureType).pcOffset,procedure.type(SyntaxTree.ProcedureType).callingConvention));
  9126. END;
  9127. IF Trace THEN TraceExit("VisitReturnStatement") END;
  9128. END VisitReturnStatement;
  9129. PROCEDURE MakeAwaitProcedure(x: SyntaxTree.AwaitStatement): SyntaxTree.Procedure;
  9130. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; procedureScope: SyntaxTree.ProcedureScope;
  9131. identifier: SyntaxTree.Identifier; body: SyntaxTree.Body; returnStatement : SyntaxTree.ReturnStatement;
  9132. statements: SyntaxTree.StatementSequence;
  9133. name, suffix: SyntaxTree.IdentifierString;
  9134. BEGIN
  9135. Strings.IntToStr(awaitProcCounter,suffix);
  9136. Strings.Concat("@AwaitProcedure",suffix,name);
  9137. identifier := SyntaxTree.NewIdentifier(name);
  9138. INC(awaitProcCounter);
  9139. ASSERT(currentScope IS SyntaxTree.ProcedureScope);
  9140. procedureScope := SyntaxTree.NewProcedureScope(currentScope);
  9141. ASSERT(procedureScope.outerScope IS SyntaxTree.ProcedureScope);
  9142. procedure := SyntaxTree.NewProcedure(x.position,identifier,procedureScope);
  9143. procedure.SetAccess(SyntaxTree.Hidden);
  9144. procedure.SetScope(currentScope);
  9145. procedureType := SyntaxTree.NewProcedureType(x.position,currentScope);
  9146. procedureType.SetReturnType(system.booleanType);
  9147. procedure.SetType(procedureType);
  9148. body := SyntaxTree.NewBody(x.position,procedureScope);
  9149. procedureScope.SetBody(body);
  9150. returnStatement := SyntaxTree.NewReturnStatement(x.position,body);
  9151. returnStatement.SetReturnValue(x.condition);
  9152. statements := SyntaxTree.NewStatementSequence();
  9153. statements.AddStatement(returnStatement);
  9154. body.SetStatementSequence(statements);
  9155. currentScope.AddProcedure(procedure);
  9156. RETURN procedure
  9157. END MakeAwaitProcedure;
  9158. PROCEDURE VisitAwaitStatement(x: SyntaxTree.AwaitStatement);
  9159. VAR proc: SyntaxTree.Procedure; res: IntermediateCode.Operand; symbol: Sections.Section;
  9160. call: IntermediateCode.Operand; label, start, true, false: Label; name: Basic.SegmentedName;
  9161. BEGIN
  9162. IF Trace THEN TraceEnter("VisitAwaitStatement") END;
  9163. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9164. IF backend.cooperative THEN
  9165. start := NewLabel();
  9166. true := NewLabel();
  9167. false := NewLabel();
  9168. SetLabel(start);
  9169. Condition(x.condition,true,false);
  9170. SetLabel(false);
  9171. PushSelfPointer();
  9172. CallThis(position,"ExclusiveBlocks","Await",1);
  9173. BrL(start);
  9174. SetLabel(true);
  9175. PushSelfPointer();
  9176. CallThis(position,"ExclusiveBlocks","FinalizeAwait",1);
  9177. ELSE
  9178. proc := MakeAwaitProcedure(x);
  9179. Emit(Push(position,fp));
  9180. GetCodeSectionNameForSymbol(proc,name);
  9181. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9182. IntermediateCode.InitAddress(call,addressType,name, GetFingerprint(proc), 0);
  9183. res := NewRegisterOperand(IntermediateCode.GetType(system,system.booleanType));
  9184. Emit(Call(position,call,ProcedureParametersSize(system,proc)));
  9185. Emit(Result(position,res));
  9186. (*
  9187. AcquireThisRegister(IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9188. IntermediateCode.InitRegister(res,IntermediateCode.GetType(system,system.booleanType),IntermediateCode.Result);
  9189. *)
  9190. InitOperand(result,ModeValue);
  9191. result.op := res;
  9192. label := NewLabel();
  9193. BreqL(label, result.op, SELF.true);
  9194. ReleaseOperand(result);
  9195. symbol := NewSection(module.allSections, Sections.CodeSection, name,proc,commentPrintout # NIL);
  9196. IntermediateCode.InitAddress(res, addressType, name,GetFingerprint(proc), 0);
  9197. Emit(Push(position,res));
  9198. Emit(Push(position,fp));
  9199. PushSelfPointer();
  9200. Emit(Push(position,nil));
  9201. CallThis(position,"Objects","Await",4);
  9202. SetLabel(label);
  9203. END;
  9204. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9205. IF Trace THEN TraceExit("VisitAwaitStatement") END;
  9206. END VisitAwaitStatement;
  9207. PROCEDURE StatementSequence(x: SyntaxTree.StatementSequence);
  9208. VAR statement: SyntaxTree.Statement; i: LONGINT; (* pos: LONGINT; *)
  9209. BEGIN
  9210. FOR i := 0 TO x.Length() - 1 DO
  9211. statement := x.GetStatement( i );
  9212. Statement(statement);
  9213. IF cooperativeSwitches & (section.pc - lastSwitchPC > 1000) THEN EmitCooperativeSwitch END;
  9214. END;
  9215. END StatementSequence;
  9216. PROCEDURE PushSelfPointer;
  9217. VAR scope: SyntaxTree.Scope; op: Operand; moduleSection: IntermediateCode.Section; moduleOffset, parametersSize: LONGINT;
  9218. BEGIN
  9219. scope := currentScope;
  9220. WHILE(scope.outerScope IS SyntaxTree.ProcedureScope) DO
  9221. scope := scope.outerScope;
  9222. END;
  9223. IF scope.outerScope IS SyntaxTree.ModuleScope THEN
  9224. IF ~newObjectFile THEN
  9225. Symbol(moduleSelf,op);
  9226. IntermediateCode.MakeMemory(op.op,addressType);
  9227. ELSE
  9228. moduleSection := meta.ModuleSection();
  9229. IF backend.cooperative THEN
  9230. moduleOffset := 0;
  9231. ELSE
  9232. moduleOffset := moduleSection.pc;
  9233. END;
  9234. op.op := IntermediateCode.Address(addressType, moduleSection.name, GetFingerprint(moduleSection.symbol), moduleOffset);
  9235. END;
  9236. ELSE
  9237. GetBaseRegister(op.op,currentScope,scope);
  9238. parametersSize := ProcedureParametersSize(system,scope(SyntaxTree.ProcedureScope).ownerProcedure);
  9239. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits)+parametersSize);
  9240. IF backend.cooperative THEN
  9241. IntermediateCode.AddOffset(op.op,ToMemoryUnits(system,addressType.sizeInBits));
  9242. END;
  9243. IntermediateCode.MakeMemory(op.op,addressType);
  9244. END;
  9245. Emit(Push(position,op.op));
  9246. ReleaseOperand(op);
  9247. END PushSelfPointer;
  9248. PROCEDURE Lock(lock: BOOLEAN);
  9249. BEGIN
  9250. IF Trace THEN TraceEnter("Lock") END;
  9251. IF profile THEN ProfilerEnterExit(numberProcedures, FALSE) END;
  9252. CheckRegistersFree(); (* no register may be in use as operands should not be preserved over the lock / unlock boundary *)
  9253. ASSERT(modifyAssignmentCounter = 0);
  9254. IF dump # NIL THEN
  9255. IF lock THEN dump.String("lock") ELSE dump.String("unlock") END;
  9256. dump.Ln;dump.Update;
  9257. END;
  9258. PushSelfPointer;
  9259. IF backend.cooperative THEN
  9260. Emit(Push(position,IntermediateCode.Immediate(sizeType, 1)));
  9261. IF lock THEN CallThis(position,"ExclusiveBlocks","Enter",2)
  9262. ELSE CallThis(position,"ExclusiveBlocks","Exit",2);
  9263. END;
  9264. ELSE
  9265. Emit(Push(position,true));
  9266. IF lock THEN CallThis(position,"Objects","Lock",2)
  9267. ELSE CallThis(position,"Objects","Unlock",2);
  9268. END;
  9269. END;
  9270. IF profile THEN ProfilerEnterExit(numberProcedures, TRUE) END;
  9271. IF Trace THEN TraceExit("Lock") END;
  9272. END Lock;
  9273. PROCEDURE VisitStatementBlock(x: SyntaxTree.StatementBlock);
  9274. VAR previouslyUnchecked, previouslyCooperativeSwitches: BOOLEAN;
  9275. BEGIN
  9276. IF Trace THEN TraceEnter("VisitStatementBlock") END;
  9277. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9278. previouslyUnchecked := isUnchecked;
  9279. isUnchecked := isUnchecked OR x.isUnchecked;
  9280. previouslyCooperativeSwitches := cooperativeSwitches;
  9281. cooperativeSwitches := cooperativeSwitches & ~x.isUncooperative;
  9282. IF x.isExclusive THEN Lock(TRUE); ASSERT(~locked); locked := TRUE; END;
  9283. IF x.statements # NIL THEN
  9284. StatementSequence(x.statements);
  9285. END;
  9286. IF x.isExclusive THEN Lock(FALSE); ASSERT(locked); locked := FALSE; END;
  9287. isUnchecked := previouslyUnchecked;
  9288. cooperativeSwitches := previouslyCooperativeSwitches;
  9289. IF Trace THEN TraceExit("VisitStatementBlock") END;
  9290. END VisitStatementBlock;
  9291. PROCEDURE VisitCode(x: SyntaxTree.Code);
  9292. VAR (* inline: Sections.CellNet; symbol: SyntaxTree.Symbol; *)
  9293. in, out: IntermediateCode.Rules; statement: SyntaxTree.Statement; i: LONGINT; operand,par: Operand; str: POINTER TO ARRAY OF CHAR;
  9294. result, mem: IntermediateCode.Operand; scope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; return: IntermediateCode.Operand;
  9295. procedure: SyntaxTree.Procedure;
  9296. map: SymbolMap;
  9297. BEGIN
  9298. scope := currentScope;
  9299. WHILE ~(scope IS SyntaxTree.ProcedureScope) DO scope := scope.outerScope END;
  9300. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9301. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9302. return := emptyOperand;
  9303. IF Trace THEN TraceEnter("VisitCode") END;
  9304. IF (x.inRules # NIL) & (x.inRules.Length()>0) THEN
  9305. NEW(in, x.inRules.Length());
  9306. FOR i := 0 TO LEN(in)-1 DO
  9307. statement := x.inRules.GetStatement(i);
  9308. WITH statement: SyntaxTree.Assignment DO
  9309. Evaluate(statement.right, operand);
  9310. result := operand.op;
  9311. NEW(str, 64);
  9312. Basic.GetString(statement.left(SyntaxTree.IdentifierDesignator).identifier, str^);
  9313. in[i] := result; IntermediateCode.SetString(in[i], str);
  9314. ReleaseIntermediateOperand(operand.tag);
  9315. END;
  9316. END;
  9317. ELSE in := NIL
  9318. END;
  9319. IF (x.outRules # NIL) & (x.outRules.Length()>0) THEN
  9320. NEW(out, x.outRules.Length());
  9321. FOR i := 0 TO LEN(out)-1 DO
  9322. statement := x.outRules.GetStatement(i);
  9323. IF statement IS SyntaxTree.StatementBlock THEN statement := statement(SyntaxTree.StatementBlock).statements.GetStatement(0) END;
  9324. WITH statement: SyntaxTree.Assignment DO
  9325. Evaluate(statement.left, operand); (*?? or designate *)
  9326. result := operand.op;
  9327. NEW(str, 64);
  9328. Basic.GetString(statement.right(SyntaxTree.IdentifierDesignator).identifier, str^);
  9329. out[i] := result; IntermediateCode.SetString(out[i], str);
  9330. ReleaseIntermediateOperand(operand.tag);
  9331. |statement: SyntaxTree.ReturnStatement DO
  9332. NEW(str, 64);
  9333. Basic.GetString(statement.returnValue(SyntaxTree.IdentifierDesignator).identifier, str^);
  9334. IF currentIsInline THEN
  9335. map := currentMapper.Get(NIL);
  9336. Evaluate(map.to, operand);
  9337. out[i] := operand.op;
  9338. ELSE
  9339. out[i] :=NewRegisterOperand(IntermediateCode.GetType(system, procedureType.returnType));
  9340. END;
  9341. IntermediateCode.SetString(out[i], str);
  9342. ReleaseIntermediateOperand(operand.tag);
  9343. return := out[i];
  9344. ELSE
  9345. END;
  9346. END;
  9347. ELSE out := NIL
  9348. END;
  9349. Emit(Asm(x.position,x.sourceCode, in, out));
  9350. IF in # NIL THEN
  9351. FOR i := 0 TO LEN(in)-1 DO
  9352. ReleaseIntermediateOperand(in[i]);
  9353. END;
  9354. END;
  9355. IF out # NIL THEN
  9356. FOR i := 0 TO LEN(out)-1 DO
  9357. WITH statement: SyntaxTree.Assignment DO
  9358. ReleaseIntermediateOperand(out[i]);
  9359. ELSE
  9360. IF currentIsInline THEN
  9361. ReleaseIntermediateOperand(out[i]);
  9362. END;
  9363. END;
  9364. statement := x.outRules.GetStatement(i);
  9365. END;
  9366. END;
  9367. IF return.mode # IntermediateCode.Undefined THEN
  9368. IF currentIsInline THEN
  9369. ELSIF ReturnedAsParameter(procedureType.returnType) THEN
  9370. Symbol(procedureType.returnParameter, par);
  9371. MakeMemory(mem, par.op, return.type, 0);
  9372. ReleaseOperand(par);
  9373. Emit(Mov(position, mem, return));
  9374. ReleaseIntermediateOperand(mem);
  9375. ELSE
  9376. Emit(Return(position,return));
  9377. END;
  9378. IF currentIsInline THEN RETURN END;
  9379. EmitLeave(section, position,procedureType(SyntaxTree.ProcedureType).callingConvention);
  9380. Emit(Exit(position,procedureType(SyntaxTree.ProcedureType).pcOffset,procedureType(SyntaxTree.ProcedureType).callingConvention));
  9381. ReleaseIntermediateOperand(return);
  9382. END;
  9383. IF Trace THEN TraceExit("VisitCode") END;
  9384. END VisitCode;
  9385. PROCEDURE ParameterCopies(x: SyntaxTree.ProcedureType);
  9386. VAR parameter: SyntaxTree.Parameter; type, base: SyntaxTree.Type;
  9387. op: Operand; temp,size,par,dst, length,null: IntermediateCode.Operand;
  9388. const, call: IntermediateCode.Operand;
  9389. parameterDesignator: SyntaxTree.Expression;
  9390. saved: RegisterEntry;
  9391. name: Basic.SegmentedName;
  9392. BEGIN
  9393. IF Trace THEN TraceEnter("ParameterCopies") END;
  9394. parameter := x.firstParameter;
  9395. WHILE parameter # NIL DO
  9396. IF parameter.kind = SyntaxTree.ValueParameter THEN
  9397. type := parameter.type.resolved;
  9398. IF IsOpenArray(type) THEN
  9399. VisitParameter(parameter);
  9400. op := result;
  9401. IF backend.cooperative & parameter.NeedsTrace() THEN
  9402. length := GetArrayLength(type, op.tag);
  9403. size := NewRegisterOperand(addressType);
  9404. base := ArrayBaseType(type);
  9405. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,system.SizeOf(base)));
  9406. Emit(Mul(position, size, length, const));
  9407. dst := NewRegisterOperand (addressType);
  9408. Emit(Mov(position, dst, size));
  9409. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9410. Emit(Sub(position,dst,sp,dst));
  9411. Emit(And(position,dst,dst,const));
  9412. Emit(Mov(position,sp,dst));
  9413. par := fp;
  9414. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9415. IntermediateCode.InitImmediate(null, byteType, 0);
  9416. Emit(Fill(position, dst, size, null));
  9417. ReleaseIntermediateOperand(dst);
  9418. ReleaseIntermediateOperand(length);
  9419. SaveRegisters();ReleaseUsedRegisters(saved);
  9420. (* register dst has been freed before SaveRegisters already *)
  9421. base := ArrayBaseType(type);
  9422. (* assign method of open array *)
  9423. IF base.IsRecordType() THEN
  9424. Emit (Push(position, length));
  9425. Emit (Push(position, dst));
  9426. Emit (Push(position, op.op));
  9427. GetRecordTypeName (base.resolved(SyntaxTree.RecordType),name);
  9428. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Array"));
  9429. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Assign"));
  9430. IntermediateCode.InitAddress(call, addressType, name , 0, 0);
  9431. Emit(Call(position,call,ToMemoryUnits(system, 3*system.addressSize)));
  9432. ELSIF base.resolved IS SyntaxTree.ProcedureType THEN (* array of delegates *)
  9433. Emit (Push(position,length));
  9434. Emit (Push(position, dst));
  9435. Emit (Push(position, length));
  9436. Emit (Push(position, op.op));
  9437. CallThis(position,"GarbageCollector","AssignDelegateArray", 4);
  9438. ELSE
  9439. Emit (Push(position, length));
  9440. Emit (Push(position, dst));
  9441. Emit (Push(position, length));
  9442. Emit (Push(position, op.op));
  9443. CallThis(position,"GarbageCollector","AssignPointerArray", 4);
  9444. ASSERT(ArrayBaseType(type).IsPointer());
  9445. END;
  9446. RestoreRegisters(saved);
  9447. ELSE
  9448. temp := GetDynamicSize(type,op.tag);
  9449. ReuseCopy(size,temp);
  9450. ReleaseIntermediateOperand(temp);
  9451. const := IntermediateCode.Immediate(addressType,ToMemoryUnits(system,-system.addressSize)); (* alignment *)
  9452. Emit(Sub(position,size,sp,size));
  9453. Emit(And(position,size,size,const));
  9454. Emit(Mov(position,sp,size));
  9455. par := fp;
  9456. IntermediateCode.AddOffset(par,ToMemoryUnits(system,parameter.offsetInBits));
  9457. ReleaseIntermediateOperand(size);
  9458. size := GetDynamicSize(type,op.tag);
  9459. END;
  9460. Emit(Copy(position,sp,op.op,size));
  9461. ReleaseIntermediateOperand(size);
  9462. ReleaseOperand(op);
  9463. IntermediateCode.MakeMemory(par,addressType);
  9464. Emit(Mov(position,par,sp));
  9465. ELSIF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  9466. checker.SetCurrentScope(currentScope);
  9467. parameterDesignator := checker.NewSymbolDesignator(position,NIL,parameter);
  9468. Assign(parameterDesignator,parameterDesignator);
  9469. END;
  9470. END;
  9471. parameter := parameter.nextParameter;
  9472. END;
  9473. IF Trace THEN TraceExit("ParameterCopies") END;
  9474. END ParameterCopies;
  9475. PROCEDURE InitVariables(scope: SyntaxTree.Scope);
  9476. VAR x: SyntaxTree.Variable;
  9477. BEGIN
  9478. x := scope.firstVariable;
  9479. WHILE x # NIL DO
  9480. InitVariable(x);
  9481. x := x.nextVariable;
  9482. END;
  9483. END InitVariables;
  9484. PROCEDURE GetFingerprint(symbol: SyntaxTree.Symbol): LONGINT;
  9485. BEGIN
  9486. IF (symbol # NIL) THEN
  9487. RETURN fingerPrinter.SymbolFP(symbol).shallow
  9488. ELSE
  9489. RETURN 0
  9490. END;
  9491. END GetFingerprint;
  9492. PROCEDURE Body(x: SyntaxTree.Body; scope: SyntaxTree.Scope; ir: IntermediateCode.Section; moduleBody: BOOLEAN);
  9493. VAR prevScope: SyntaxTree.Scope; procedureType: SyntaxTree.ProcedureType; procedure: SyntaxTree.Procedure;
  9494. cellScope: SyntaxTree.CellScope; op: Operand; string: SyntaxTree.IdentifierString;
  9495. saved: RegisterEntry;
  9496. BEGIN
  9497. IF Trace THEN TraceEnter("Body") END;
  9498. ReleaseUsedRegisters(saved); (* just in case ... *)
  9499. section := ir;
  9500. exitLabel := NewLabel ();
  9501. IF moduleBody THEN moduleBodySection := section END;
  9502. IF ir.comments # NIL THEN
  9503. commentPrintout := Printout.NewPrinter(ir.comments,Printout.SourceCode,FALSE);
  9504. commentPrintout.SingleStatement(TRUE);
  9505. dump := ir.comments;
  9506. ELSE
  9507. commentPrintout := NIL;
  9508. dump := NIL;
  9509. END;
  9510. registerUsageCount.Init;
  9511. prevScope := currentScope;
  9512. currentScope := scope;
  9513. lastSwitchPC := 0;
  9514. cooperativeSwitches := backend.cooperative;
  9515. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  9516. procedureType := procedure.type(SyntaxTree.ProcedureType);
  9517. IF x # NIL THEN
  9518. IF emitLabels THEN Emit(LabelInstruction(x.position)) END;
  9519. IF profile & (x.code = NIL) THEN (* do not profile assembler code sections *)
  9520. IF moduleBody THEN
  9521. ProfilerInit();
  9522. ELSE
  9523. Basic.SegmentedNameToString(ir.name, string);
  9524. ProfilerAddProcedure(numberProcedures,string);
  9525. ProfilerEnterExit(numberProcedures,TRUE);
  9526. END;
  9527. END;
  9528. IF moduleBody & (operatorInitializationCodeSection # NIL) THEN
  9529. Emit(Call(position,IntermediateCode.Address(addressType, operatorInitializationCodeSection.name, GetFingerprint(operatorInitializationCodeSection.symbol), 0), 0))
  9530. END;
  9531. section.SetPositionOrAlignment(procedure.fixed, procedure.alignment);
  9532. IF moduleBody THEN
  9533. InitVariables(moduleScope)
  9534. END;
  9535. IF (scope.outerScope # NIL) & (scope.outerScope IS SyntaxTree.CellScope) THEN
  9536. cellScope := scope.outerScope(SyntaxTree.CellScope);
  9537. IF procedure = cellScope.bodyProcedure THEN
  9538. IF (cellScope.constructor # NIL) & ~backend.cellsAreObjects THEN
  9539. StaticCallOperand(op, cellScope.constructor);
  9540. Emit(Call(position,op.op,0));
  9541. END;
  9542. END;
  9543. END;
  9544. ParameterCopies(procedureType);
  9545. InitVariables(scope);
  9546. IF x.code = NIL THEN
  9547. VisitStatementBlock(x);
  9548. ELSE
  9549. VisitCode(x.code)
  9550. END;
  9551. IF x.finally # NIL THEN (*! mark finally block for object file *)
  9552. ir.SetFinally(ir.pc);
  9553. StatementSequence(x.finally)
  9554. END;
  9555. IF profile & (x.code = NIL) & ~moduleBody THEN (* do not profile assembler code sections *)
  9556. IF ~backend.cooperative THEN
  9557. ProfilerEnterExit(numberProcedures,FALSE);
  9558. END;
  9559. INC(numberProcedures);
  9560. END;
  9561. END;
  9562. IF backend.cooperative THEN
  9563. IF HasPointers (procedure.procedureScope) THEN CreateResetMethod (procedure.procedureScope) END;
  9564. IF HasPointers (procedure.procedureScope) OR HasVariableParameters (procedure.procedureScope) OR IsNested (procedure) THEN CreateProcedureDescriptor (procedure) END;
  9565. ELSIF newObjectFile & moduleBody & ~suppressModuleRegistration & ~meta.simple THEN
  9566. (*! not required any more? check and delete!
  9567. PushSelfPointer();
  9568. CallThis(position,"Modules","SetInitialized",1);
  9569. *)
  9570. (*
  9571. SetLabel(end);
  9572. *)
  9573. END;
  9574. IF x # NIL THEN
  9575. SELF.position := x.position;
  9576. END;
  9577. CheckRegistersFree();
  9578. ASSERT(modifyAssignmentCounter = 0);
  9579. currentScope := prevScope;
  9580. IF Trace THEN TraceExit("Body") END;
  9581. END Body;
  9582. END ImplementationVisitor;
  9583. MetaDataGenerator=OBJECT
  9584. VAR
  9585. implementationVisitor: ImplementationVisitor;
  9586. declarationVisitor: DeclarationVisitor;
  9587. module: Sections.Module;
  9588. moduleName: ARRAY 128 OF CHAR;
  9589. simple: BOOLEAN; (* simple = no methods, no module loading, no reflection *)
  9590. MethodTableOffset: LONGINT; (* method table offset from zero *)
  9591. BaseTypesTableOffset: LONGINT; (* table with all record extensions offset *)
  9592. TypeTags: LONGINT; (* type extension level support *)
  9593. TypeRecordBaseOffset: LONGINT; (* offset of type zero offset (without method entries) *)
  9594. PROCEDURE &InitMetaDataGenerator(implementationVisitor: ImplementationVisitor; declarationVisitor: DeclarationVisitor; simple: BOOLEAN);
  9595. BEGIN
  9596. IF implementationVisitor.backend.cooperative THEN
  9597. TypeTags := MAX(LONGINT); (* only 3 extensions allowed *)
  9598. BaseTypesTableOffset := 0;
  9599. MethodTableOffset := 2;
  9600. TypeRecordBaseOffset := 0;
  9601. ELSIF simple THEN
  9602. TypeTags := 3; (* only 3 extensions allowed *)
  9603. BaseTypesTableOffset := -1;
  9604. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9605. TypeRecordBaseOffset := TypeTags;
  9606. ELSE
  9607. TypeTags := 16;
  9608. BaseTypesTableOffset := -2; (* typeInfo and size field *)
  9609. MethodTableOffset := -TypeTags+BaseTypesTableOffset;
  9610. TypeRecordBaseOffset := TypeTags + 2; (* MPO, typeInfo *)
  9611. END;
  9612. SELF.simple := simple;
  9613. SELF.implementationVisitor := implementationVisitor;
  9614. SELF.declarationVisitor := declarationVisitor;
  9615. implementationVisitor.meta := SELF;
  9616. declarationVisitor.meta := SELF;
  9617. END InitMetaDataGenerator;
  9618. PROCEDURE SetModule(module: Sections.Module);
  9619. BEGIN
  9620. SELF.module := module
  9621. END SetModule;
  9622. PROCEDURE GetTypeRecordBaseOffset(numberMethods: LONGINT): LONGINT;
  9623. BEGIN
  9624. IF implementationVisitor.backend.cooperative THEN RETURN 0 ELSE RETURN TypeRecordBaseOffset + numberMethods END;
  9625. END GetTypeRecordBaseOffset;
  9626. PROCEDURE HeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9627. VAR offset: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol;
  9628. BEGIN
  9629. INC(dataAdrOffset,6);
  9630. Info(section,"headerAdr");
  9631. Address(section,0);
  9632. Info(section,"typeDesc");
  9633. symbol := implementationVisitor.GetTypeDescriptor(moduleName,typeName, name);
  9634. offset := ToMemoryUnits(module.system,TypeRecordBaseOffset*module.system.addressSize);
  9635. NamedSymbol(section, name, symbol, 0, offset);
  9636. Info(section,"mark: LONGINT;");
  9637. Longint(section,-1);
  9638. Info(section,"dataAdr-: ADDRESS");
  9639. Symbol(section,section, dataAdrOffset,0);
  9640. Info(section,"size-: SIZE");
  9641. Address(section,0);
  9642. Info(section,"nextRealtime: HeapBlock;");
  9643. Address(section,0);
  9644. END HeapBlock;
  9645. PROCEDURE ProtectedHeapBlock(CONST moduleName, typeName: ARRAY OF CHAR; section: IntermediateCode.Section; dataAdrOffset: LONGINT);
  9646. VAR i: LONGINT;
  9647. BEGIN
  9648. INC(dataAdrOffset,14);
  9649. HeapBlock(moduleName,typeName,section,dataAdrOffset);
  9650. Info(section,"count*: LONGINT");
  9651. Longint(section,0);
  9652. Info(section,"locked*: BOOLEAN");
  9653. Longint(section,0);
  9654. Info(section,"awaitingLock*: ProcessQueue");
  9655. Address(section,0);
  9656. Address(section,0);
  9657. Info(section,"awaitingCond*: ProcessQueue");
  9658. Address(section,0);
  9659. Address(section,0);
  9660. Info(section,"lockedBy*: ANY");
  9661. Address(section,0);
  9662. Info(section,"lock*: ANY");
  9663. Address(section,0);
  9664. Info(section,"waitingPriorities*: ARRAY NumPriorities OF LONGINT");
  9665. Longint(section,1);
  9666. FOR i := 2 TO 6 DO
  9667. Longint(section,0);
  9668. END;
  9669. END ProtectedHeapBlock;
  9670. PROCEDURE Info(section: IntermediateCode.Section; CONST s: ARRAY OF CHAR);
  9671. BEGIN
  9672. IF section.comments # NIL THEN section.comments.String(s); section.comments.Ln; section.comments.Update END;
  9673. END Info;
  9674. PROCEDURE Address(section: IntermediateCode.Section; value: LONGINT);
  9675. VAR op: IntermediateCode.Operand;
  9676. BEGIN
  9677. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),value);
  9678. section.Emit(Data(-11,op));
  9679. END Address;
  9680. PROCEDURE Size(section: IntermediateCode.Section; value: LONGINT);
  9681. VAR op: IntermediateCode.Operand;
  9682. BEGIN
  9683. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.sizeType),value);
  9684. section.Emit(Data(-12,op));
  9685. END Size;
  9686. PROCEDURE Set(section: IntermediateCode.Section; value: SET);
  9687. VAR op: IntermediateCode.Operand;
  9688. BEGIN
  9689. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),SYSTEM.VAL(LONGINT,value));
  9690. section.Emit(Data(-1,op));
  9691. END Set;
  9692. PROCEDURE Longint(section: IntermediateCode.Section; value: LONGINT);
  9693. VAR op: IntermediateCode.Operand;
  9694. BEGIN
  9695. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9696. section.Emit(Data(-1,op));
  9697. END Longint;
  9698. PROCEDURE PatchLongint(section: IntermediateCode.Section; pc: LONGINT; value: LONGINT);
  9699. VAR op,noOperand: IntermediateCode.Operand;
  9700. BEGIN
  9701. IntermediateCode.InitOperand(noOperand);
  9702. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.longintType),value);
  9703. section.PatchOperands(pc,op,noOperand,noOperand);
  9704. END PatchLongint;
  9705. PROCEDURE PatchSymbol(section: IntermediateCode.Section; pc: LONGINT; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9706. VAR op, noOperand: IntermediateCode.Operand;
  9707. BEGIN
  9708. IntermediateCode.InitOperand(noOperand);
  9709. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9710. section.PatchOperands(pc,op,noOperand,noOperand);
  9711. END PatchSymbol;
  9712. PROCEDURE Boolean(section: IntermediateCode.Section; value: BOOLEAN);
  9713. VAR op: IntermediateCode.Operand; intValue: LONGINT;
  9714. BEGIN
  9715. IF value = FALSE THEN intValue := 0 ELSE intValue :=1 END;
  9716. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.booleanType),intValue);
  9717. section.Emit(Data(-1,op));
  9718. END Boolean;
  9719. PROCEDURE Char(section: IntermediateCode.Section; char: CHAR);
  9720. VAR op: IntermediateCode.Operand;
  9721. BEGIN
  9722. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.characterType),ORD(char));
  9723. section.Emit(Data(-1,op));
  9724. END Char;
  9725. PROCEDURE String(section: IntermediateCode.Section; CONST str: ARRAY OF CHAR);
  9726. VAR i: LONGINT;
  9727. BEGIN
  9728. Info(section,str);
  9729. i := 0;
  9730. WHILE(str[i] # 0X) DO
  9731. Char(section,str[i]);
  9732. INC(i);
  9733. END;
  9734. Char(section,0X);
  9735. END String;
  9736. PROCEDURE NamedSymbol(section: IntermediateCode.Section; name: Basic.SegmentedName; symbol: SyntaxTree.Symbol; virtualOffset, realOffset: LONGINT);
  9737. VAR op: IntermediateCode.Operand;
  9738. BEGIN
  9739. IntermediateCode.InitAddress(op, IntermediateCode.GetType(module.system, module.system.addressType), name,implementationVisitor.GetFingerprint(symbol), virtualOffset);
  9740. IntermediateCode.SetOffset(op,realOffset);
  9741. section.Emit(Data(-1,op));
  9742. END NamedSymbol;
  9743. PROCEDURE Symbol(section: IntermediateCode.Section; symbol: Sections.Section; virtualOffset, realOffset: LONGINT);
  9744. BEGIN
  9745. IF symbol= NIL THEN
  9746. Address( section, realOffset);
  9747. ASSERT(virtualOffset = 0);
  9748. ELSE
  9749. NamedSymbol(section, symbol.name, symbol.symbol, virtualOffset, realOffset)
  9750. END;
  9751. END Symbol;
  9752. (* OutPointers delivers
  9753. {pointerOffset}
  9754. *)
  9755. PROCEDURE Pointers(offset: LONGINT; symbol: Sections.Section; section: IntermediateCode.Section; type: SyntaxTree.Type; VAR numberPointers: LONGINT);
  9756. VAR variable: SyntaxTree.Variable; i,n,size: LONGINT; base: SyntaxTree.Type;
  9757. BEGIN
  9758. type := type.resolved;
  9759. IF type IS SyntaxTree.AnyType THEN
  9760. Symbol(section, symbol, 0, (offset ));
  9761. INC(numberPointers);
  9762. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9763. ELSIF (type IS SyntaxTree.PortType) & implementationVisitor.backend.cellsAreObjects THEN
  9764. Symbol(section, symbol, 0, offset); INC(numberPointers);
  9765. ELSIF type IS SyntaxTree.PointerType THEN
  9766. Symbol(section, symbol, 0, (offset )); INC(numberPointers);
  9767. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1);D.Ln; END;
  9768. ELSIF (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate) THEN
  9769. Symbol(section, symbol, 0, (offset )+ToMemoryUnits(module.system,module.system.addressSize)); INC(numberPointers);
  9770. IF Trace THEN D.Str("ptr at offset="); D.Int(offset+ToMemoryUnits(module.system,module.system.addressSize),1); END;
  9771. ELSIF (type IS SyntaxTree.RecordType) THEN
  9772. (* never treat a record like a pointer, even if the pointer field is set! *)
  9773. WITH type: SyntaxTree.RecordType DO
  9774. base := type.GetBaseRecord();
  9775. IF base # NIL THEN
  9776. Pointers(offset,symbol,section, base,numberPointers);
  9777. END;
  9778. variable := type.recordScope.firstVariable;
  9779. WHILE(variable # NIL) DO
  9780. IF ~(variable.untraced) THEN
  9781. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9782. END;
  9783. variable := variable.nextVariable;
  9784. END;
  9785. END;
  9786. ELSIF (type IS SyntaxTree.CellType) THEN
  9787. WITH type: SyntaxTree.CellType DO
  9788. base := type.GetBaseRecord();
  9789. IF base # NIL THEN
  9790. Pointers(offset,symbol,section, base,numberPointers);
  9791. END;
  9792. variable := type.cellScope.firstVariable;
  9793. WHILE(variable # NIL) DO
  9794. IF ~(variable.untraced) THEN
  9795. Pointers(offset+ToMemoryUnits(module.system,variable.offsetInBits), symbol, section, variable.type,numberPointers);
  9796. END;
  9797. variable := variable.nextVariable;
  9798. END;
  9799. END;
  9800. ELSIF (type IS SyntaxTree.ArrayType) THEN
  9801. WITH type: SyntaxTree.ArrayType DO
  9802. IF type.form= SyntaxTree.Static THEN
  9803. n := type.staticLength;
  9804. base := type.arrayBase.resolved;
  9805. WHILE(base IS SyntaxTree.ArrayType) DO
  9806. type := base(SyntaxTree.ArrayType);
  9807. n := n* type.staticLength;
  9808. base := type.arrayBase.resolved;
  9809. END;
  9810. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9811. IF SemanticChecker.ContainsPointer(base) THEN
  9812. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9813. FOR i := 0 TO n-1 DO
  9814. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9815. END;
  9816. END;
  9817. ELSE
  9818. Symbol( section, symbol, 0, (offset )); INC(numberPointers);
  9819. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9820. END;
  9821. END;
  9822. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  9823. WITH type: SyntaxTree.MathArrayType DO
  9824. IF type.form = SyntaxTree.Static THEN
  9825. n := type.staticLength;
  9826. base := type.arrayBase.resolved;
  9827. WHILE(base IS SyntaxTree.MathArrayType) DO
  9828. type := base(SyntaxTree.MathArrayType);
  9829. n := n* type.staticLength;
  9830. base := type.arrayBase.resolved;
  9831. END;
  9832. size := ToMemoryUnits(module.system,module.system.AlignedSizeOf(base));
  9833. IF SemanticChecker.ContainsPointer(base) THEN
  9834. ASSERT(n<1000000); (* not more than one million pointers on the stack ... *)
  9835. FOR i := 0 TO n-1 DO
  9836. Pointers(offset+i*size, symbol, section, base,numberPointers);
  9837. END;
  9838. END;
  9839. ELSE
  9840. Symbol(section, symbol, 0, (offset )); INC(numberPointers); (* GC relevant pointer is at offset 0 *)
  9841. IF Trace THEN D.Str("ptr at offset="); D.Int(offset,1); D.Ln; END;
  9842. END
  9843. END;
  9844. (* ELSE no pointers in type *)
  9845. END;
  9846. END Pointers;
  9847. PROCEDURE DynamicName(source: IntermediateCode.Section; name: StringPool.Index; pool: Basic.HashTableInt): LONGINT;
  9848. VAR s: Basic.SectionName; i: LONGINT; ch: CHAR; position: LONGINT;
  9849. BEGIN
  9850. IF pool.Has(name) THEN
  9851. RETURN pool.GetInt(name)
  9852. ELSE
  9853. position := source.pc;
  9854. pool.PutInt(name, position);
  9855. StringPool.GetString(name, s);
  9856. Info(source, s);
  9857. i := 0;
  9858. REPEAT
  9859. ch := s[i]; INC(i);
  9860. Char( source, ch);
  9861. UNTIL ch = 0X;
  9862. END;
  9863. RETURN position;
  9864. END DynamicName;
  9865. PROCEDURE Block(CONST mName, typeName, suffix: ARRAY OF CHAR; VAR offset: LONGINT): IntermediateCode.Section;
  9866. VAR name: ARRAY 128 OF CHAR; section: IntermediateCode.Section; pooledName: Basic.SegmentedName;
  9867. BEGIN
  9868. COPY(moduleName,name);
  9869. Strings.Append(name,suffix);
  9870. Basic.ToSegmentedName(name, pooledName);
  9871. section := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName, NIL,TRUE);
  9872. IF implementationVisitor.backend.cooperative THEN
  9873. Info(section, "TypeDescriptor");
  9874. Basic.ToSegmentedName("BaseTypes.Array", pooledName);
  9875. NamedSymbol(section, pooledName,NIL, 0, 0);
  9876. BasePointer(section);
  9877. offset := 0;
  9878. ELSE
  9879. HeapBlock(mName,typeName,section,2);
  9880. Info(section, "HeapBlock");
  9881. (*
  9882. Symbol(section,section,2,0);
  9883. *)
  9884. Address(section,0); (* empty such that GC does not go on traversing *)
  9885. Info(section, "TypeDescriptor");
  9886. Address(section,0);
  9887. offset := section.pc;
  9888. END;
  9889. RETURN section
  9890. END Block;
  9891. PROCEDURE Array(source: IntermediateCode.Section; VAR sizePC: LONGINT; CONST baseType: ARRAY OF CHAR);
  9892. VAR name: Basic.SegmentedName;
  9893. BEGIN
  9894. Info(source,"ArrayHeader");
  9895. IF implementationVisitor.backend.cooperative THEN
  9896. sizePC := source.pc;
  9897. Address(source,0);
  9898. NamedSymbol(source,source.name,NIL,0,ToMemoryUnits(implementationVisitor.system,(BaseArrayTypeSize + 1)*implementationVisitor.addressType.sizeInBits));
  9899. IF baseType # "" THEN
  9900. Basic.ToSegmentedName(baseType, name);
  9901. NamedSymbol(source, name,NIL, 0, 0);
  9902. ELSE
  9903. Address(source,0);
  9904. END;
  9905. Address(source,0);
  9906. ELSE
  9907. Address(source,0);
  9908. Address(source,0);
  9909. Address(source,0);
  9910. sizePC := source.pc;
  9911. Address(source,0);
  9912. Info(source,"array data");
  9913. END;
  9914. END Array;
  9915. PROCEDURE PatchArray(section: IntermediateCode.Section; pc: LONGINT; size: LONGINT);
  9916. BEGIN
  9917. IF implementationVisitor.backend.cooperative THEN
  9918. PatchLongint(section, pc, size);
  9919. PatchLongint(section, pc + 3, size);
  9920. ELSE
  9921. PatchLongint(section, pc, size);
  9922. END;
  9923. END PatchArray;
  9924. PROCEDURE ExportDesc(source: IntermediateCode.Section);
  9925. VAR
  9926. i: LONGINT; section: Sections.Section; fingerPrinter : FingerPrinter.FingerPrinter;
  9927. sectionArray: POINTER TO ARRAY OF Sections.Section;
  9928. poolMap: Basic.HashTableInt;
  9929. namePool: IntermediateCode.Section;
  9930. namePoolOffset: LONGINT;
  9931. PROCEDURE Compare(VAR s1, s2: Sections.Section): BOOLEAN;
  9932. VAR n1, n2: Basic.SectionName; index: LONGINT; ch1, ch2: CHAR;
  9933. BEGIN
  9934. Basic.SegmentedNameToString(s1.name,n1);
  9935. Basic.SegmentedNameToString(s2.name,n2);
  9936. index := 0;
  9937. ch1 := n1[index];
  9938. ch2 := n2[index];
  9939. WHILE (ch1 # 0X) & (ch1 = ch2) DO
  9940. INC(index);
  9941. ch1 := n1[index];
  9942. ch2 := n2[index];
  9943. END;
  9944. RETURN ch1 < ch2;
  9945. END Compare;
  9946. PROCEDURE QuickSort(VAR list: ARRAY OF Sections.Section; lo, hi: LONGINT);
  9947. VAR
  9948. i, j: LONGINT;
  9949. x, t: Sections.Section;
  9950. BEGIN
  9951. IF lo < hi THEN
  9952. i := lo; j := hi; x:= list[(lo+hi) DIV 2];
  9953. WHILE i <= j DO
  9954. WHILE Compare(list[i], x) DO INC(i) END;
  9955. WHILE Compare(x, list[j]) DO DEC(j) END;
  9956. IF i <= j THEN
  9957. t := list[i]; list[i] := list[j]; list[j] := t; (* swap i and j *)
  9958. INC(i); DEC(j)
  9959. END
  9960. END;
  9961. IF lo < j THEN QuickSort(list, lo, j) END;
  9962. IF i < hi THEN QuickSort(list, i, hi) END
  9963. END;
  9964. END QuickSort;
  9965. (*
  9966. ExportDesc* = RECORD
  9967. fp*: ADDRESS;
  9968. name* {UNTRACED}: DynamicName;
  9969. adr*: ADDRESS;
  9970. exports*: LONGINT;
  9971. dsc* {UNTRACED}: ExportArray
  9972. END;
  9973. ExportArray* = POINTER {UNSAFE} TO ARRAY OF ExportDesc;
  9974. *)
  9975. PROCEDURE ExportDesc2(
  9976. source: IntermediateCode.Section;
  9977. namePool: IntermediateCode.Section;
  9978. fingerPrinter: FingerPrinter.FingerPrinter;
  9979. symbol: Sections.Section;
  9980. name: StringPool.Index;
  9981. VAR patchAdr: LONGINT
  9982. ): BOOLEAN;
  9983. VAR fingerPrint: SyntaxTree.FingerPrint;
  9984. BEGIN
  9985. (*IF (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  9986. & (symbol.type # Sections.InlineCodeSection)
  9987. THEN
  9988. *)
  9989. IF (symbol = NIL) OR ( (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection)
  9990. & (symbol.type # Sections.InlineCodeSection))
  9991. THEN
  9992. IF (symbol = NIL) OR (symbol # NIL) & (symbol.type # Sections.InlineCodeSection) THEN
  9993. IF (symbol # NIL) & (symbol.symbol # NIL) THEN
  9994. fingerPrint := fingerPrinter.SymbolFP(symbol.symbol);
  9995. Longint(source,fingerPrint.shallow);
  9996. ELSE
  9997. Longint(source, 0);
  9998. END;
  9999. Symbol(source, namePool, DynamicName(namePool, name, poolMap), 0); (* reference to dynamic name *)
  10000. Symbol(source, symbol,0,0);
  10001. patchAdr := source.pc;
  10002. Address(source,0);
  10003. Address(source,0);
  10004. END;
  10005. RETURN TRUE
  10006. ELSE
  10007. RETURN FALSE
  10008. END;
  10009. END ExportDesc2;
  10010. PROCEDURE Export(CONST sections: ARRAY OF Sections.Section);
  10011. VAR level, olevel, s: LONGINT; prev, this: Basic.SegmentedName; name: ARRAY 256 OF CHAR;
  10012. scopes: ARRAY LEN(prev)+1 OF Scope; arrayName: ARRAY 32 OF CHAR;
  10013. sym: Sections.Section; ignore: LONGINT; symbol: Sections.Section;
  10014. TYPE
  10015. Scope = RECORD
  10016. elements: LONGINT;
  10017. gelements: LONGINT;
  10018. section: IntermediateCode.Section;
  10019. patchAdr: LONGINT;
  10020. arraySizePC: LONGINT;
  10021. beginPC: LONGINT; (* current scope start pc *)
  10022. END;
  10023. BEGIN
  10024. Basic.InitSegmentedName(prev);
  10025. olevel := -1;
  10026. scopes[0].section := source;
  10027. FOR s := 0 TO LEN(sections)-1 DO
  10028. symbol := sections[s];
  10029. IF (symbol # NIL) & (implementationVisitor.backend.cooperative) & (symbol.symbol = NIL) OR (symbol.symbol # NIL) & (symbol.type # Sections.InitCodeSection) & (symbol.type # Sections.InlineCodeSection) THEN
  10030. this := sections[s].name;
  10031. level := 0;
  10032. WHILE (this[level] > 0) DO
  10033. WHILE (this[level] > 0) & (prev[level] = this[level]) DO
  10034. INC(level);
  10035. END;
  10036. WHILE level < olevel DO
  10037. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10038. IF olevel > 0 THEN
  10039. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10040. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10041. END;
  10042. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10043. DEC(olevel);
  10044. END;
  10045. IF (this[level] > 0) THEN
  10046. IF level > olevel THEN
  10047. (*TRACE("opening",level); *)
  10048. IF scopes[level].section = NIL THEN
  10049. arrayName := ".@ExportArray";
  10050. Strings.AppendInt(arrayName, level);
  10051. scopes[level].section := Block("Heaps","SystemBlockDesc",arrayName,ignore);
  10052. Array(scopes[level].section,scopes[level].arraySizePC,"Modules.ExportDesc");
  10053. END;
  10054. scopes[level].beginPC := scopes[level].section.pc;
  10055. olevel := level;
  10056. scopes[olevel].elements := 0;
  10057. END;
  10058. IF (level = LEN(this)-1) OR (this[level+1] <= 0) THEN
  10059. sym := sections[s];
  10060. ELSE
  10061. sym := NIL;
  10062. END;
  10063. IF ExportDesc2(scopes[level].section, namePool, fingerPrinter, sym, this[level], scopes[level].patchAdr)
  10064. THEN
  10065. INC(scopes[olevel].elements);
  10066. END;
  10067. (*StringPool.GetString(this[level], name);*)
  10068. (*TRACE(level, name);*)
  10069. (* enter string in scope *)
  10070. INC(level);
  10071. END;
  10072. END;
  10073. Basic.SegmentedNameToString(this, name);
  10074. (*TRACE(level, "enter", name);*)
  10075. prev := this;
  10076. END;
  10077. END;
  10078. WHILE 0 <= olevel DO
  10079. (*TRACE("closing",olevel,scopes[olevel].elements); *)
  10080. IF olevel > 0 THEN
  10081. PatchLongint(scopes[olevel-1].section,scopes[olevel-1].patchAdr, scopes[olevel].elements);
  10082. PatchSymbol(scopes[olevel-1].section,scopes[olevel-1].patchAdr+1, scopes[olevel].section.name, scopes[olevel].section.symbol, scopes[olevel].beginPC, 0);
  10083. END;
  10084. scopes[olevel].gelements := scopes[olevel].gelements + scopes[olevel].elements;
  10085. DEC(olevel);
  10086. END;
  10087. level := 0;
  10088. WHILE (level < LEN(scopes)) DO
  10089. IF scopes[level].section # NIL THEN
  10090. PatchArray(scopes[level].section, scopes[level].arraySizePC, scopes[level].gelements);
  10091. END;
  10092. INC(level);
  10093. END;
  10094. END Export;
  10095. BEGIN
  10096. NEW(fingerPrinter, module.system);
  10097. NEW(poolMap, 64);
  10098. namePool := Block("Heaps","SystemBlockDesc",".@NamePool",namePoolOffset);
  10099. NEW(sectionArray, module.allSections.Length());
  10100. FOR i := 0 TO module.allSections.Length() - 1 DO
  10101. section := module.allSections.GetSection(i);
  10102. sectionArray[i] := section;
  10103. END;
  10104. QuickSort(sectionArray^,0,module.allSections.Length()-1);
  10105. Export(sectionArray^);
  10106. END ExportDesc;
  10107. PROCEDURE ExceptionArray(source: IntermediateCode.Section);
  10108. VAR
  10109. p: Sections.Section; finallyPC, sizePC, size, i: LONGINT;
  10110. BEGIN
  10111. Info(source, "exception table offsets array descriptor");
  10112. size := 0;
  10113. Array(source,sizePC,"Modules.ExceptionTableEntry");
  10114. Info(source, "exception table content");
  10115. FOR i := 0 TO module.allSections.Length() - 1 DO
  10116. p := module.allSections.GetSection(i);
  10117. IF p.type = Sections.CodeSection THEN
  10118. finallyPC := p(IntermediateCode.Section).finally;
  10119. IF finallyPC>=0 THEN
  10120. Symbol( source, p, 0,0);
  10121. Symbol( source, p, finallyPC, 0);
  10122. Symbol( source, p, finallyPC,0);
  10123. INC(size);
  10124. END;
  10125. END
  10126. END;
  10127. PatchArray(source,sizePC,size);
  10128. END ExceptionArray;
  10129. PROCEDURE Name(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10130. VAR i: LONGINT; ch: CHAR;
  10131. BEGIN
  10132. i := 0;
  10133. REPEAT
  10134. ch := name[i]; INC(i);
  10135. Char( section, ch);
  10136. UNTIL ch = 0X;
  10137. WHILE i < 32 DO
  10138. Char( section, 0X); INC(i);
  10139. END;
  10140. END Name;
  10141. PROCEDURE LongName(section: IntermediateCode.Section; CONST name: ARRAY OF CHAR);
  10142. VAR i: LONGINT; ch: CHAR;
  10143. BEGIN
  10144. i := 0;
  10145. REPEAT
  10146. ch := name[i]; INC(i);
  10147. Char( section, ch);
  10148. UNTIL ch = 0X;
  10149. WHILE i < 128 DO
  10150. Char( section, 0X); INC(i);
  10151. END;
  10152. END LongName;
  10153. PROCEDURE References(section: IntermediateCode.Section);
  10154. CONST
  10155. rfDirect = 1X; rfIndirect = 3X;
  10156. rfStaticArray= 12X; rfDynamicArray=14X; rfOpenArray=15X;
  10157. rfByte = 1X; rfBoolean = 2X; rfChar8=3X; rfShortint=04X; rfInteger = 05X; rfLongint = 06X;
  10158. rfReal = 07X; rfLongreal = 08X; rfSet = 09X; rfDelegate = 0EX; rfString = 0FH; rfPointer = 0DX; rfHugeint = 10X;
  10159. rfChar16=11X; rfChar32=12X; rfAll=13X; rfSame=14X; rfRange=15X; rfRecord=16X; rfRecordPointer=1DX;
  10160. rfArrayFlag = 80X;
  10161. VAR
  10162. size: LONGINT; s: Sections.Section; sizePC, i: LONGINT;
  10163. PROCEDURE BaseType(arrayOf: BOOLEAN; type: SyntaxTree.Type);
  10164. VAR char: CHAR;
  10165. BEGIN
  10166. IF type = NIL THEN char := rfLongint
  10167. ELSIF type IS SyntaxTree.ByteType THEN char := rfByte
  10168. ELSIF type IS SyntaxTree.BooleanType THEN char := rfBoolean
  10169. ELSIF type IS SyntaxTree.CharacterType THEN
  10170. IF type.sizeInBits = 8 THEN char := rfChar8
  10171. ELSIF type.sizeInBits = 16 THEN char := rfChar16
  10172. ELSIF type.sizeInBits = 32 THEN char := rfChar32
  10173. END;
  10174. ELSIF type IS SyntaxTree.IntegerType THEN
  10175. IF type.sizeInBits = 8 THEN char := rfShortint
  10176. ELSIF type.sizeInBits = 16 THEN char := rfInteger
  10177. ELSIF type.sizeInBits = 32 THEN char := rfLongint
  10178. ELSIF type.sizeInBits = 64 THEN char := rfHugeint
  10179. END;
  10180. ELSIF type IS SyntaxTree.SizeType THEN char := rfLongint
  10181. ELSIF type IS SyntaxTree.AddressType THEN char := rfLongint
  10182. ELSIF type IS SyntaxTree.FloatType THEN
  10183. IF type.sizeInBits = 32 THEN char := rfReal
  10184. ELSIF type.sizeInBits = 64 THEN char := rfLongreal
  10185. END;
  10186. ELSIF type IS SyntaxTree.SetType THEN char := rfSet
  10187. ELSIF type IS SyntaxTree.AnyType THEN char := rfPointer
  10188. ELSIF type IS SyntaxTree.ObjectType THEN char := rfPointer
  10189. ELSIF type IS SyntaxTree.PointerType THEN char := rfPointer
  10190. ELSIF type IS SyntaxTree.ProcedureType THEN char := rfDelegate
  10191. ELSE (*ASSERT(arrayOf);*) char := rfPointer; (*RETURN (* ARRAY OF unknown (record): do not write anything *)*)
  10192. END;
  10193. IF arrayOf THEN
  10194. Char(section,CHR(ORD(char)+ORD(rfArrayFlag)));
  10195. ELSE
  10196. Char(section,char)
  10197. END;
  10198. INC(size);
  10199. END BaseType;
  10200. PROCEDURE RecordType(type: SyntaxTree.RecordType);
  10201. VAR destination: Sections.Section; name: SyntaxTree.IdentifierString;
  10202. BEGIN
  10203. destination := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10204. IF destination = NIL THEN
  10205. (* imported unused record type *)
  10206. Char(section,0X); (* nil type *)
  10207. INC(size);
  10208. type.typeDeclaration.GetName(name);
  10209. (*
  10210. 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
  10211. D.Str("Warning: Unreferenced record type encountered: "); D.String(name); D.String(" unused? "); D.Ln;
  10212. *)
  10213. ELSE
  10214. IF type.pointerType # NIL THEN
  10215. Char(section,rfRecordPointer)
  10216. ELSE
  10217. Char(section,rfRecord);
  10218. END;
  10219. INC(size);
  10220. Longint(section,(destination.offset ));
  10221. INC(size,4);
  10222. END;
  10223. END RecordType;
  10224. PROCEDURE ArrayType(type: SyntaxTree.ArrayType);
  10225. BEGIN
  10226. IF type.arrayBase.resolved IS SyntaxTree.ArrayType THEN
  10227. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10228. INC(size);
  10229. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10230. END;
  10231. IF type.form = SyntaxTree.Static THEN
  10232. Longint(section,type.staticLength)
  10233. ELSE
  10234. Longint(section,0)
  10235. END;
  10236. INC(size,4);
  10237. END ArrayType;
  10238. PROCEDURE MathArrayType(type: SyntaxTree.MathArrayType);
  10239. BEGIN
  10240. IF type.arrayBase = NIL THEN BaseType(TRUE,NIL)
  10241. ELSIF type.arrayBase.resolved IS SyntaxTree.MathArrayType THEN
  10242. Char(section,CHR(ORD(rfPointer)+ORD(rfArrayFlag)));
  10243. INC(size);
  10244. ELSE BaseType(TRUE,type.arrayBase.resolved)
  10245. END;
  10246. IF type.form = SyntaxTree.Static THEN
  10247. Longint(section,type.staticLength)
  10248. ELSE
  10249. Longint(section,0)
  10250. END;
  10251. INC(size,4);
  10252. END MathArrayType;
  10253. PROCEDURE Type(type: SyntaxTree.Type);
  10254. BEGIN
  10255. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10256. IF type IS SyntaxTree.BasicType THEN
  10257. BaseType(FALSE,type)
  10258. ELSIF type IS SyntaxTree.RecordType THEN
  10259. RecordType(type(SyntaxTree.RecordType));
  10260. ELSIF type IS SyntaxTree.ArrayType THEN
  10261. ArrayType(type(SyntaxTree.ArrayType))
  10262. ELSIF type IS SyntaxTree.EnumerationType THEN
  10263. BaseType(FALSE,module.system.longintType)
  10264. ELSIF type IS SyntaxTree.PointerType THEN
  10265. IF type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType THEN
  10266. RecordType(type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType));
  10267. ELSE
  10268. BaseType(FALSE,type)
  10269. END;
  10270. ELSIF type IS SyntaxTree.ProcedureType THEN
  10271. BaseType(FALSE,type);
  10272. ELSIF type IS SyntaxTree.MathArrayType THEN
  10273. MathArrayType(type(SyntaxTree.MathArrayType));
  10274. ELSIF type IS SyntaxTree.CellType THEN
  10275. BaseType(FALSE,module.system.anyType);
  10276. ELSE HALT(200)
  10277. END;
  10278. END Type;
  10279. PROCEDURE WriteVariable(variable: SyntaxTree.Variable; indirect: BOOLEAN);
  10280. VAR name: ARRAY 256 OF CHAR;
  10281. BEGIN
  10282. IF variable.externalName # NIL THEN RETURN END;
  10283. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10284. INC(size);
  10285. variable.GetName(name);
  10286. Type(variable.type);
  10287. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10288. INC(size,4);
  10289. String(section,name);
  10290. INC(size,Strings.Length(name)+1);
  10291. END WriteVariable;
  10292. PROCEDURE WriteParameter(variable: SyntaxTree.Parameter; indirect: BOOLEAN);
  10293. VAR name: ARRAY 256 OF CHAR;
  10294. BEGIN
  10295. IF indirect THEN Char(section,rfIndirect) ELSE Char(section,rfDirect) END;
  10296. INC(size);
  10297. variable.GetName(name);
  10298. Type(variable.type);
  10299. Longint(section,ToMemoryUnits(module.system,variable.offsetInBits));
  10300. INC(size,4);
  10301. variable.GetName(name);
  10302. String(section,name);
  10303. INC(size,Strings.Length(name)+1);
  10304. END WriteParameter;
  10305. PROCEDURE ReturnType(type: SyntaxTree.Type);
  10306. BEGIN
  10307. IF type = NIL THEN Char(section,0X); INC(size); RETURN ELSE type := type.resolved END;
  10308. IF type IS SyntaxTree.ArrayType THEN
  10309. WITH type: SyntaxTree.ArrayType DO
  10310. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10311. ELSE Char(section,rfOpenArray)
  10312. END;
  10313. INC(size);
  10314. END
  10315. ELSIF type IS SyntaxTree.MathArrayType THEN
  10316. WITH type: SyntaxTree.MathArrayType DO
  10317. IF type.form = SyntaxTree.Static THEN Char(section,rfStaticArray)
  10318. ELSE Char(section,rfOpenArray)
  10319. END;
  10320. INC(size);
  10321. END
  10322. ELSIF type IS SyntaxTree.RecordType THEN
  10323. Char(section,rfRecord);
  10324. INC(size);
  10325. ELSE
  10326. BaseType(FALSE,type);
  10327. END;
  10328. END ReturnType;
  10329. PROCEDURE Procedure(s: Sections.Section);
  10330. VAR procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10331. parameter: SyntaxTree.Parameter; variable: SyntaxTree.Variable;
  10332. name: ARRAY 256 OF CHAR;
  10333. BEGIN
  10334. procedure := s.symbol(SyntaxTree.Procedure); (*! check for variable or type symbol for object body *)
  10335. procedure.GetName(name);
  10336. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10337. Char(section,0F9X);
  10338. INC(size);
  10339. Symbol(section,s,0,0);
  10340. INC(size,4);
  10341. Symbol(section,s,s(IntermediateCode.Section).pc,0);
  10342. INC(size,4);
  10343. Longint(section,procedureType.numberParameters);
  10344. INC(size,4);
  10345. ReturnType(procedureType.returnType);
  10346. Longint(section,0); (*! level *)
  10347. INC(size,4);
  10348. Longint(section,0);
  10349. INC(size,4);
  10350. Global.GetSymbolNameInScope(procedure, module.module.moduleScope, name);
  10351. (*
  10352. IF procedure.scope IS SyntaxTree.RecordScope THEN (* add object name *)
  10353. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  10354. recordName := "";
  10355. IF record.pointerType # NIL THEN
  10356. DeclarationName(record.pointerType.typeDeclaration,recordName);
  10357. ELSE
  10358. DeclarationName(record.typeDeclaration,recordName);
  10359. END;
  10360. i := 0;
  10361. Info(section,recordName);
  10362. WHILE recordName[i] # 0X DO
  10363. Char(section,recordName[i]); INC(i);
  10364. INC(size);
  10365. END;
  10366. Char(section,".");
  10367. INC(size);
  10368. END;
  10369. *)
  10370. String(section,name);
  10371. INC(size,Strings.Length(name)+1);
  10372. parameter := procedureType.firstParameter;
  10373. WHILE(parameter # NIL) DO
  10374. WriteParameter(parameter,parameter.kind # SyntaxTree.ValueParameter); (*!treat exceptions !*)
  10375. parameter := parameter.nextParameter;
  10376. END;
  10377. variable := procedure.procedureScope.firstVariable;
  10378. WHILE(variable # NIL) DO
  10379. WriteVariable(variable,FALSE);
  10380. variable := variable.nextVariable;
  10381. END;
  10382. END Procedure;
  10383. PROCEDURE Scope(s: Sections.Section);
  10384. BEGIN
  10385. Char(section,0F8X);
  10386. INC(size);
  10387. Symbol(section,s,0,0); (* start *)
  10388. INC(size,4);
  10389. Symbol(section,s,s(IntermediateCode.Section).pc,0); (* end *)
  10390. INC(size,4);
  10391. String(section,"$$");
  10392. INC(size,3);
  10393. (* removed variables -- wrongly interpreted by Reflection
  10394. variable := module.module.moduleScope.firstVariable;
  10395. WHILE(variable # NIL) DO
  10396. WriteVariable(variable,FALSE);
  10397. variable := variable.nextVariable;
  10398. END;
  10399. *)
  10400. END Scope;
  10401. BEGIN
  10402. Array(section,sizePC,"");
  10403. size := 0;
  10404. Char(section,0FFX); (* sign for trap writer *)
  10405. INC(size);
  10406. FOR i := 0 TO module.allSections.Length() - 1 DO
  10407. s := module.allSections.GetSection(i);
  10408. IF (s.type # Sections.InitCodeSection) & (s.symbol = module.module.moduleScope.bodyProcedure) THEN
  10409. Scope(s) (*! must be first procedure in ref section *)
  10410. END
  10411. END;
  10412. FOR i := 0 TO module.allSections.Length() - 1 DO
  10413. s := module.allSections.GetSection(i);
  10414. IF (s.symbol = module.module.moduleScope.bodyProcedure) THEN (* already done, see above *)
  10415. ELSIF (s.type # Sections.InitCodeSection) & (s.symbol # NIL) & (s.symbol IS SyntaxTree.Procedure) & ~s.symbol(SyntaxTree.Procedure).isInline THEN
  10416. Procedure(s)
  10417. END
  10418. END;
  10419. PatchArray(section,sizePC,size);
  10420. END References;
  10421. (*
  10422. Command* = RECORD
  10423. (* Fields exported for initialization by loader/linker only! Consider read-only! *)
  10424. name*: Name; (* name of the procedure *)
  10425. argTdAdr*, retTdAdr* : ADDRESS; (* address of type descriptors of argument and return type, 0 if no type *)
  10426. entryAdr* : ADDRESS; (* entry address of procedure *)
  10427. END;
  10428. *)
  10429. PROCEDURE CommandArray(source: IntermediateCode.Section);
  10430. VAR
  10431. p: Sections.Section; sizePC, numberCommands: LONGINT;
  10432. procedure : SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10433. name: ARRAY 32 OF CHAR; numberParameters, i: LONGINT;
  10434. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  10435. PROCEDURE GetProcedureAllowed() : BOOLEAN;
  10436. PROCEDURE TypeAllowed(type : SyntaxTree.Type) : BOOLEAN;
  10437. BEGIN
  10438. RETURN
  10439. (type = NIL) OR
  10440. (type.resolved IS SyntaxTree.RecordType) OR
  10441. (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) OR
  10442. (type.resolved IS SyntaxTree.AnyType);
  10443. END TypeAllowed;
  10444. BEGIN
  10445. numberParameters := procedureType.numberParameters;
  10446. RETURN
  10447. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  10448. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  10449. (numberParameters = 1) & (procedureType.firstParameter.type.resolved IS SyntaxTree.AnyType) & (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.AnyType);
  10450. END GetProcedureAllowed;
  10451. PROCEDURE WriteType(type : SyntaxTree.Type);
  10452. VAR typeDeclaration: SyntaxTree.TypeDeclaration; section: Sections.Section;
  10453. name: Basic.SegmentedName; offset: LONGINT;
  10454. BEGIN
  10455. IF type = NIL THEN
  10456. Address(source,0);
  10457. ELSIF (type.resolved IS SyntaxTree.AnyType) OR (type.resolved IS SyntaxTree.ObjectType) THEN
  10458. Address(source,1);
  10459. ELSE
  10460. type := type.resolved;
  10461. IF type IS SyntaxTree.PointerType THEN
  10462. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  10463. END;
  10464. typeDeclaration := type.typeDeclaration; (* must be non-nil *)
  10465. IF (typeDeclaration.scope = NIL) OR (typeDeclaration.scope.ownerModule = module.module) THEN
  10466. name[0] := typeDeclaration.name; name[1] := -1;
  10467. section := module.allSections.FindBySymbol(type.typeDeclaration); (*TODO*)
  10468. ASSERT(section # NIL);
  10469. ELSE
  10470. Global.GetSymbolSegmentedName(typeDeclaration,name);
  10471. (* TODO *)
  10472. section := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,typeDeclaration, source.comments # NIL);
  10473. END;
  10474. IF implementationVisitor.backend.cooperative THEN
  10475. offset := 0;
  10476. ELSE
  10477. offset := 1 + type(SyntaxTree.RecordType).recordScope.numberMethods+16+1;
  10478. END;
  10479. Symbol(source,section, 0, ToMemoryUnits(module.system,offset*module.system.addressSize));
  10480. END;
  10481. END WriteType;
  10482. BEGIN
  10483. Info(source, "command array descriptor");
  10484. Array(source,sizePC,"Modules.Command");
  10485. numberCommands := 0;
  10486. Info(source, "command array content");
  10487. FOR i := 0 TO module.allSections.Length() - 1 DO
  10488. p := module.allSections.GetSection(i);
  10489. IF (p.symbol # NIL) & (p.symbol IS SyntaxTree.Procedure) THEN
  10490. procedure := p.symbol(SyntaxTree.Procedure);
  10491. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10492. IF (SyntaxTree.PublicWrite IN procedure.access) & ~(procedure.isInline) & ~(procedureType.isDelegate) & GetProcedureAllowed() THEN
  10493. procedure.GetName(name);
  10494. Name(source,name);
  10495. numberParameters := procedureType.numberParameters;
  10496. (* offset of type of first parameter *)
  10497. IF (numberParameters = 0 ) THEN WriteType(NIL)
  10498. ELSE WriteType(procedureType.firstParameter.type)
  10499. END;
  10500. (* offset of type of return parameter *)
  10501. WriteType(procedureType.returnType);
  10502. (* command name *)
  10503. (* command code offset *)
  10504. Symbol(source,p,0,0);
  10505. INC(numberCommands);
  10506. IF Trace THEN
  10507. D.Ln;
  10508. END;
  10509. END;
  10510. END
  10511. END;
  10512. PatchArray(source,sizePC,numberCommands);
  10513. END CommandArray;
  10514. (* to prevent from double import of different module aliases *)
  10515. PROCEDURE IsFirstDirectOccurence(import: SyntaxTree.Import): BOOLEAN; (*! inefficient *)
  10516. VAR i: SyntaxTree.Import;
  10517. BEGIN
  10518. i := module.module.moduleScope.firstImport;
  10519. WHILE (i # NIL) & ((i.module # import.module) OR ~i.direct) DO
  10520. i := i.nextImport;
  10521. END;
  10522. RETURN i = import
  10523. END IsFirstDirectOccurence;
  10524. PROCEDURE ImportsArray(source: IntermediateCode.Section);
  10525. VAR import: SyntaxTree.Import ; pc: LONGINT;name: Basic.SegmentedName; numberImports: LONGINT; offset: LONGINT;
  10526. BEGIN
  10527. Array(source,pc,"");
  10528. Info(source, "import module array data");
  10529. IF implementationVisitor.backend.cooperative THEN
  10530. offset := 0;
  10531. ELSE
  10532. offset := ToMemoryUnits(module.system, 22* module.system.addressSize) (* Module pointer offset -- cf. ModuleSection(), how to encode generically correct? *);
  10533. END;
  10534. import := module.module.moduleScope.firstImport;
  10535. numberImports := 0;
  10536. WHILE import # NIL DO
  10537. IF import.direct & ~Global.IsSystemModule(import.module) & IsFirstDirectOccurence(import) THEN
  10538. Global.GetModuleSegmentedName(import.module,name);
  10539. Basic.SuffixSegmentedName(name, StringPool.GetIndex1("@Module"));
  10540. NamedSymbol(source, name, NIL, 0, offset);
  10541. INC(numberImports);
  10542. END;
  10543. import := import.nextImport
  10544. END;
  10545. PatchArray(source,pc,numberImports);
  10546. END ImportsArray;
  10547. PROCEDURE TypeInfoSection(source: IntermediateCode.Section);
  10548. VAR
  10549. p: Sections.Section; sizePC, size, i: LONGINT;
  10550. BEGIN
  10551. Info(source, "Type info section");
  10552. size := 0;
  10553. Array(source,sizePC,"Modules.TypeDesc");
  10554. FOR i := 0 TO module.allSections.Length() - 1 DO
  10555. p := module.allSections.GetSection(i);
  10556. WITH p: IntermediateCode.Section DO
  10557. IF Basic.SegmentedNameEndsWith(p.name,"@Info") THEN
  10558. Symbol(source,p,0,0);
  10559. INC(size);
  10560. END;
  10561. END
  10562. END;
  10563. PatchArray(source,sizePC,size);
  10564. END TypeInfoSection;
  10565. (*
  10566. ProcTableEntry* = RECORD
  10567. pcFrom*, pcLimit*, pcStatementBegin*, pcStatementEnd*: ADDRESS;
  10568. noPtr*: LONGINT;
  10569. END;
  10570. ProcTable* = POINTER TO ARRAY OF ProcTableEntry;
  10571. PtrTable* = POINTER TO ARRAY OF ADDRESS;
  10572. *)
  10573. PROCEDURE PointersInProcTables(procArray, pointerArray: IntermediateCode.Section; VAR procArraySize, maxPointers: LONGINT);
  10574. VAR
  10575. destination: Sections.Section;
  10576. pointerArraySizePC, procArraySizePC, pointerArraySize, i: LONGINT;
  10577. PROCEDURE PointerOffsets(destination : IntermediateCode.Section);
  10578. VAR numberPointers: LONGINT; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType;
  10579. variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter; string: Basic.SectionName;
  10580. BEGIN
  10581. Info(procArray,"pcFrom");
  10582. Symbol(procArray,destination,0,0);
  10583. Info(procArray,"pcTo");
  10584. Symbol(procArray,destination,destination.pc,0);
  10585. Info(procArray,"pcStatementBegin");
  10586. Symbol(procArray,destination,destination.validPAFEnter,0);
  10587. Info(procArray,"pcStatementEnd");
  10588. Symbol(procArray,destination,destination.validPAFExit,0);
  10589. IF ~implementationVisitor.backend.cooperative THEN
  10590. Basic.SegmentedNameToString(destination.name, string);
  10591. Info(pointerArray,string);
  10592. procedure := destination.symbol(SyntaxTree.Procedure);
  10593. procedureType := procedure.type(SyntaxTree.ProcedureType);
  10594. variable := procedure.procedureScope.firstVariable;
  10595. WHILE(variable # NIL) DO
  10596. IF ~(variable.untraced) THEN
  10597. Pointers(ToMemoryUnits(module.system,variable.offsetInBits), NIL, pointerArray, variable.type, numberPointers);
  10598. END;
  10599. variable := variable.nextVariable
  10600. END;
  10601. parameter := procedureType.firstParameter;
  10602. WHILE(parameter # NIL) DO
  10603. IF ~(parameter.untraced) THEN
  10604. Pointers(ToMemoryUnits(module.system,parameter.offsetInBits), NIL, pointerArray, parameter.type, numberPointers);
  10605. END;
  10606. parameter := parameter.nextParameter;
  10607. END;
  10608. END;
  10609. Info(procArray,"numberPointers");
  10610. Longint(procArray,numberPointers);
  10611. IF numberPointers > maxPointers THEN maxPointers := numberPointers END;
  10612. INC(pointerArraySize, numberPointers);
  10613. END PointerOffsets;
  10614. BEGIN
  10615. maxPointers := 0;
  10616. Info(procArray, "proc array descriptor");
  10617. Address(procArray,0);
  10618. Address(procArray,0);
  10619. Address(procArray,0);
  10620. procArraySizePC := procArray.pc;
  10621. Address(procArray,0);
  10622. procArraySize := 0;
  10623. IF ~implementationVisitor.backend.cooperative THEN
  10624. Info(pointerArray, "pointer array descriptor");
  10625. Address(pointerArray,0);
  10626. Address(pointerArray,0);
  10627. Address(pointerArray,0);
  10628. pointerArraySizePC := pointerArray.pc;
  10629. Address(pointerArray,0);
  10630. pointerArraySize := 0;
  10631. END;
  10632. procArraySize := 0;
  10633. FOR i := 0 TO module.allSections.Length() - 1 DO
  10634. destination := module.allSections.GetSection(i);
  10635. IF (destination.type IN {Sections.CodeSection, Sections.BodyCodeSection}) & (destination.symbol # NIL) & (destination.symbol IS SyntaxTree.Procedure) & ~destination.symbol(SyntaxTree.Procedure).isInline THEN
  10636. PointerOffsets(destination(IntermediateCode.Section));
  10637. INC(procArraySize);
  10638. END
  10639. END;
  10640. PatchLongint(procArray,procArraySizePC,procArraySize);
  10641. IF ~implementationVisitor.backend.cooperative THEN
  10642. PatchLongint(pointerArray,pointerArraySizePC,pointerArraySize);
  10643. END;
  10644. END PointersInProcTables;
  10645. (*
  10646. Module* = OBJECT (Heaps.RootObject) (* cf. Linker0 & Heaps.WriteType *)
  10647. VAR
  10648. next*: Module; (** once a module is published, all fields are read-only *)
  10649. name*: Name;
  10650. init, published: BOOLEAN;
  10651. refcnt*: LONGINT; (* counts loaded modules that import this module *)
  10652. sb*: ADDRESS; <- set to beginning of data section by loader
  10653. entry*: POINTER TO ARRAY OF ADDRESS; <- not needed in new loader
  10654. command*: POINTER TO ARRAY OF Command;
  10655. ptrAdr*: POINTER TO ARRAY OF ADDRESS;
  10656. typeInfo*: POINTER TO ARRAY OF TypeDesc;
  10657. module*: POINTER TO ARRAY OF Module; <---- currently done by loader
  10658. procTable*: ProcTable; (* information inserted by loader, removed after use in Publish *)
  10659. ptrTable*: PtrTable; (* information inserted by loader, removed after use in Publish *)
  10660. data*, code*: Bytes;
  10661. staticTypeDescs* (* ug *), refs*: Bytes; <- staticTypeDescs in data section, refs currently unsupported
  10662. export*: ExportDesc;
  10663. term*: TerminationHandler;
  10664. exTable*: ExceptionTable;
  10665. noProcs*: LONGINT;
  10666. firstProc*: ADDRESS; <- done by loader
  10667. maxPtrs*: LONGINT;
  10668. crc*: LONGINT;
  10669. *)
  10670. PROCEDURE BasePointer (section: IntermediateCode.Section);
  10671. BEGIN
  10672. Info(section, "cycle");
  10673. Size(section,0);
  10674. Info(section, "references");
  10675. Size(section,0);
  10676. Info(section, "nextMarked");
  10677. Address(section,0);
  10678. Info(section, "nextWatched");
  10679. Address(section,0);
  10680. END BasePointer;
  10681. PROCEDURE BaseObject (section: IntermediateCode.Section);
  10682. BEGIN
  10683. BasePointer(section);
  10684. Info(section, "action");
  10685. Address(section,0);
  10686. Info(section, "monitor");
  10687. Address(section,0);
  10688. END BaseObject;
  10689. PROCEDURE ModuleDescriptor(section: IntermediateCode.Section);
  10690. VAR descriptorSection: IntermediateCode.Section; name: ARRAY 128 OF CHAR;
  10691. pooledName: Basic.SegmentedName;
  10692. symbol: SyntaxTree.Symbol;
  10693. BEGIN
  10694. Global.GetModuleName(module.module,name);
  10695. Strings.Append(name,".@Module.@Descriptor");
  10696. Basic.ToSegmentedName(name, pooledName);
  10697. descriptorSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10698. Symbol(section,descriptorSection,0,0);
  10699. Info(descriptorSection, "descriptor");
  10700. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10701. NamedSymbol(descriptorSection, pooledName,symbol, 0, 0);
  10702. Address(descriptorSection,0);
  10703. Global.GetModuleName(module.module,name);
  10704. Strings.Append(name,".@Trace");
  10705. Basic.ToSegmentedName(name, pooledName);
  10706. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10707. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",pooledName);
  10708. NamedSymbol(descriptorSection, pooledName,NIL, 0, 0);
  10709. END ModuleDescriptor;
  10710. PROCEDURE ModuleSection(): IntermediateCode.Section;
  10711. VAR name: ARRAY 128 OF CHAR;
  10712. moduleSection: IntermediateCode.Section; offset: LONGINT; pooledName: Basic.SegmentedName;
  10713. symbol: SyntaxTree.Symbol;
  10714. BEGIN
  10715. Global.GetModuleName(module.module,name);
  10716. Strings.Append(name,".@Module");
  10717. Basic.ToSegmentedName(name, pooledName);
  10718. moduleSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, pooledName,NIL,TRUE);
  10719. moduleSection.SetExported(TRUE);
  10720. IF moduleSection.pc = 0 THEN
  10721. IF implementationVisitor.backend.cooperative THEN
  10722. Info(moduleSection, "descriptor");
  10723. ModuleDescriptor(moduleSection);
  10724. BaseObject(moduleSection);
  10725. implementationVisitor.CreateTraceModuleMethod(module.module);
  10726. ELSE
  10727. ProtectedHeapBlock("Heaps","ProtRecBlockDesc",moduleSection,2);
  10728. Info(moduleSection, "HeapBlock");
  10729. Symbol(moduleSection,moduleSection,2,0);
  10730. Info(moduleSection, "TypeDescriptor");
  10731. symbol := implementationVisitor.GetTypeDescriptor("Modules","Module", pooledName);
  10732. offset := ToMemoryUnits(module.system,(TypeRecordBaseOffset + 1 (*= numberMethods*))*module.system.addressSize);
  10733. NamedSymbol(moduleSection, pooledName,symbol, 0, offset);
  10734. END;
  10735. END;
  10736. RETURN moduleSection;
  10737. END ModuleSection;
  10738. PROCEDURE Module(bodyProc: IntermediateCode.Section);
  10739. VAR
  10740. moduleSection, pointerSection, importSection, emptyArraySection, exceptionSection, commandsSection,
  10741. typeInfoSection, procTableSection, ptrTableSection, referenceSection : IntermediateCode.Section;
  10742. emptyArraySectionOffset, pointerSectionOffset, importSectionOffset, numberPointers,
  10743. exceptionSectionOffset, commandsSectionOffset, typeInfoSectionOffset, procTableSectionOffset, ptrTableSectionOffset, maxPointers, numberProcs,temp,
  10744. referenceSectionOffset : LONGINT;
  10745. BEGIN
  10746. Global.GetModuleName(module.module,moduleName);
  10747. pointerSection := Block("Heaps","SystemBlockDesc",".@PointerArray",pointerSectionOffset);
  10748. PointerArray(pointerSection,module.module.moduleScope, numberPointers);
  10749. importSection := Block("Heaps","SystemBlockDesc",".@ImportsArray",importSectionOffset);
  10750. ImportsArray(importSection);
  10751. commandsSection := Block("Heaps","SystemBlockDesc",".@CommandArray",commandsSectionOffset);
  10752. CommandArray(commandsSection);
  10753. exceptionSection := Block("Heaps","SystemBlockDesc",".@ExceptionArray",exceptionSectionOffset);
  10754. ExceptionArray(exceptionSection);
  10755. typeInfoSection := Block("Heaps","SystemBlockDesc",".@TypeInfoArray",typeInfoSectionOffset);
  10756. TypeInfoSection(typeInfoSection);
  10757. referenceSection := Block("Heaps","SystemBlockDesc",".@References",referenceSectionOffset);
  10758. References(referenceSection);
  10759. procTableSection := Block("Heaps","SystemBlockDesc",".@ProcTable",procTableSectionOffset);
  10760. IF ~implementationVisitor.backend.cooperative THEN
  10761. ptrTableSection := Block("Heaps","SystemBlockDesc",".@PtrTable",ptrTableSectionOffset);
  10762. ELSE
  10763. ptrTableSection := NIL;
  10764. END;
  10765. PointersInProcTables(procTableSection,ptrTableSection,numberProcs,maxPointers);
  10766. emptyArraySection := Block("Heaps","SystemBlockDesc",".@EmptyArray",emptyArraySectionOffset);
  10767. Array(emptyArraySection,temp,"");
  10768. moduleSection := ModuleSection();
  10769. Info(moduleSection, "nextRoot*: RootObject");
  10770. Address(moduleSection,0);
  10771. Info(moduleSection, "next*: Module");
  10772. Address(moduleSection,0);
  10773. Info(moduleSection, "name*: Name");
  10774. Name(moduleSection,moduleName);
  10775. Info(moduleSection, "init, published: BOOLEAN");
  10776. Boolean(moduleSection,FALSE);
  10777. Boolean(moduleSection,FALSE);
  10778. Info(moduleSection,"filler"); (*! introduce alignment! *)
  10779. Boolean(moduleSection,FALSE);
  10780. Boolean(moduleSection,FALSE);
  10781. Info(moduleSection, "refcnt*: LONGINT");
  10782. Longint(moduleSection,0);
  10783. Info(moduleSection, "sb*: ADDRESS");
  10784. Address(moduleSection,0);
  10785. Info(moduleSection, "entry*: POINTER TO ARRAY OF ADDRESS");
  10786. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10787. Info(moduleSection, "command*: POINTER TO ARRAY OF Command");
  10788. Symbol(moduleSection,commandsSection,commandsSectionOffset,0);
  10789. Info(moduleSection, "ptrAdr*: POINTER TO ARRAY OF ADDRESS");
  10790. Symbol(moduleSection,pointerSection,pointerSectionOffset,0);
  10791. Info(moduleSection, "typeInfo*: POINTER TO ARRAY OF TypeDesc");
  10792. Symbol(moduleSection,typeInfoSection,typeInfoSectionOffset,0);
  10793. Info(moduleSection, "module*: POINTER TO ARRAY OF Module");
  10794. Symbol(moduleSection,importSection,emptyArraySectionOffset,0);
  10795. Info(moduleSection, "procTable*: ProcTable");
  10796. Symbol(moduleSection,procTableSection,procTableSectionOffset,0);
  10797. Info(moduleSection, "ptrTable*: PtrTable");
  10798. IF ~implementationVisitor.backend.cooperative THEN
  10799. Symbol(moduleSection,ptrTableSection,ptrTableSectionOffset,0);
  10800. ELSE
  10801. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10802. END;
  10803. Info(moduleSection, "data*, code*, staticTypeDescs*, refs*: Bytes");
  10804. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10805. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10806. Symbol(moduleSection,emptyArraySection,emptyArraySectionOffset,0);
  10807. Symbol(moduleSection,referenceSection,referenceSectionOffset,0);
  10808. Info(moduleSection, "export*: ExportDesc");
  10809. ExportDesc(moduleSection);
  10810. Info(moduleSection, "term*: TerminationHandler");
  10811. Address(moduleSection,0);
  10812. Info(moduleSection, "exTable*: ExceptionTable");
  10813. Symbol(moduleSection,exceptionSection,exceptionSectionOffset,0);
  10814. Info(moduleSection, "noProcs*: LONGINT");
  10815. Longint(moduleSection,numberProcs);
  10816. Info(moduleSection, "firstProc*: ADDRESS");
  10817. Address(moduleSection,0);
  10818. Info(moduleSection, "maxPtrs*: LONGINT");
  10819. Longint(moduleSection,maxPointers);
  10820. Info(moduleSection, "crc*: LONGINT");
  10821. Longint(moduleSection, 0); (*! must be implemented *)
  10822. Info(moduleSection, "body*: ADDRESS");
  10823. Symbol(moduleSection, bodyProc, 0,0);
  10824. IF implementationVisitor.backend.cooperative THEN
  10825. PatchSymbol(moduleSection,MonitorOffset,moduleSection.name,NIL,moduleSection.pc,0);
  10826. Info(moduleSection, "monitor.owner");
  10827. Address(moduleSection,0);
  10828. Info(moduleSection, "monitor.nestingLevel");
  10829. Address(moduleSection,0);
  10830. Info(moduleSection, "monitor.blockedQueue");
  10831. Address(moduleSection,0); Address(moduleSection,0);
  10832. Info(moduleSection, "monitor.waitingQueue");
  10833. Address(moduleSection,0); Address(moduleSection,0);
  10834. Info(moduleSection, "monitor.waitingSentinel");
  10835. Address(moduleSection,0);
  10836. END;
  10837. END Module;
  10838. PROCEDURE PointerArray(source: IntermediateCode.Section; scope: SyntaxTree.Scope; VAR numberPointers: LONGINT);
  10839. VAR variable: SyntaxTree.Variable; pc: LONGINT; symbol: Sections.Section;
  10840. BEGIN
  10841. Array(source,pc,"");
  10842. Info(source, "pointer offsets array data");
  10843. IF scope IS SyntaxTree.RecordScope THEN
  10844. Pointers(0,symbol, source,scope(SyntaxTree.RecordScope).ownerRecord,numberPointers);
  10845. ELSIF scope IS SyntaxTree.CellScope THEN
  10846. Pointers(0, symbol, source, scope(SyntaxTree.CellScope).ownerCell, numberPointers);
  10847. ELSIF scope IS SyntaxTree.ModuleScope THEN
  10848. variable := scope(SyntaxTree.ModuleScope).firstVariable;
  10849. WHILE variable # NIL DO
  10850. IF ~(variable.untraced) & (variable.externalName = NIL) THEN
  10851. symbol := module.allSections.FindBySymbol(variable);
  10852. ASSERT(symbol # NIL);
  10853. Pointers(0,symbol, source,variable.type,numberPointers);
  10854. END;
  10855. variable := variable.nextVariable;
  10856. END;
  10857. END;
  10858. PatchArray(source,pc,numberPointers);
  10859. END PointerArray;
  10860. PROCEDURE CheckTypeDeclaration(x: SyntaxTree.Type);
  10861. VAR recordType: SyntaxTree.RecordType;
  10862. tir: IntermediateCode.Section; op: IntermediateCode.Operand; name: Basic.SegmentedName; td: SyntaxTree.TypeDeclaration;
  10863. section: Sections.Section; type: SyntaxTree.Type; cellType: SyntaxTree.CellType;
  10864. PROCEDURE FieldArray(source: IntermediateCode.Section);
  10865. VAR variable: SyntaxTree.Variable; pc, offset: LONGINT; tir: Sections.Section; size: LONGINT; name: ARRAY 128 OF CHAR;
  10866. segmentedName: Basic.SegmentedName;
  10867. td: SyntaxTree.TypeDeclaration;
  10868. BEGIN
  10869. Array(source,pc,"Modules.FieldEntry");
  10870. Info(source, "FieldArray");
  10871. IF x IS SyntaxTree.RecordType THEN
  10872. variable := x(SyntaxTree.RecordType).recordScope.firstVariable
  10873. ELSE
  10874. variable := NIL;
  10875. END;
  10876. size :=0;
  10877. WHILE variable # NIL DO
  10878. type := variable.type.resolved;
  10879. Info(source,"offset");
  10880. Size(source, ToMemoryUnits(module.system,variable.offsetInBits));
  10881. Info(source,"type class");
  10882. IF type IS SyntaxTree.PointerType THEN
  10883. Size(source, 1);
  10884. ELSIF type IS SyntaxTree.RecordType THEN
  10885. Size(source, 2);
  10886. ELSIF type IS SyntaxTree.NumberType THEN
  10887. Size(source, 3);
  10888. ELSE
  10889. Size(source, 0);
  10890. END;
  10891. Info(source, "type desc");
  10892. IF type IS SyntaxTree.RecordType THEN
  10893. td := type(SyntaxTree.RecordType).typeDeclaration;
  10894. Global.GetSymbolSegmentedName(td,segmentedName);
  10895. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  10896. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10897. ELSE
  10898. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, segmentedName,td,declarationVisitor.dump);
  10899. END;
  10900. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(type(SyntaxTree.RecordType).recordScope.numberMethods)*module.system.addressSize);
  10901. Symbol(source, tir, 0, offset);
  10902. ELSE
  10903. Address(source, 0);
  10904. END;
  10905. Info(source,"flags");
  10906. Set(source, {});
  10907. Info(source,"name");
  10908. variable.GetName(name);
  10909. LongName(source, name);
  10910. variable := variable.nextVariable;
  10911. INC(size);
  10912. END;
  10913. PatchArray(source,pc,size);
  10914. END FieldArray;
  10915. PROCEDURE NewTypeDescriptorInfo(tag: Sections.Section; offset: LONGINT; isProtected: BOOLEAN): Sections.Section;
  10916. VAR name: Basic.SegmentedName;source, fieldSection: IntermediateCode.Section;
  10917. moduleSection: IntermediateCode.Section; i: LONGINT; flags: SET;
  10918. sectionName: Basic.SectionName;
  10919. CONST MPO=-40000000H;
  10920. BEGIN
  10921. (*
  10922. TypeDesc* = POINTER TO RECORD
  10923. descSize: LONGINT;
  10924. sentinel: LONGINT; (* = MPO-4 *)
  10925. tag*: ADDRESS; (* pointer to static type descriptor, only used by linker and loader *)
  10926. flags*: SET;
  10927. mod*: Module; (* hint only, because module may have been freed (at Heaps.ModOfs) *)
  10928. name*: Name;
  10929. END;
  10930. *)
  10931. (* source := module.sections.FindByName(...) *)
  10932. Global.GetSymbolSegmentedName(td,name);
  10933. Basic.AppendToSegmentedName(name,"@Info");
  10934. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  10935. Info(source, "type info size"); Address(source, 3*ToMemoryUnits(module.system,module.system.addressSize)+32);
  10936. Address(source,MPO-4);
  10937. Info(source, "type tag pointer");
  10938. Symbol( source, tag, offset, 0);
  10939. Info(source, "type flags");
  10940. flags := {};
  10941. IF isProtected THEN INCL(flags,31) END;
  10942. Set( source, flags);
  10943. Info(source, "pointer to module");
  10944. moduleSection := ModuleSection();
  10945. Symbol( source, moduleSection, moduleSection.pc,0);
  10946. Info(source, "type name");
  10947. i := 0;
  10948. Global.GetSymbolNameInScope(td, module.module.moduleScope, sectionName);
  10949. (*
  10950. Global.GetSymbolSegmentedName(td,name);
  10951. Basic.SegmentedNameToString(name, sectionName);
  10952. *)
  10953. Name(source,sectionName);
  10954. source.SetReferenced(FALSE);
  10955. Global.GetSymbolSegmentedName(td,name);
  10956. Basic.AppendToSegmentedName(name,"@Fields");
  10957. fieldSection := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name, NIL,TRUE);
  10958. HeapBlock("Heaps","SystemBlockDesc", fieldSection, 2);
  10959. Info(fieldSection, "HeapBlock");
  10960. Address(fieldSection,0); (* empty such that GC does not go on traversing *)
  10961. Info(fieldSection, "TypeDescriptor");
  10962. Address(fieldSection,0);
  10963. Info(source, "FieldArray ref");
  10964. Symbol(source, fieldSection, fieldSection.pc, 0);
  10965. FieldArray(fieldSection);
  10966. RETURN source;
  10967. END NewTypeDescriptorInfo;
  10968. PROCEDURE NewTypeDescriptor;
  10969. VAR name: Basic.SegmentedName; op: IntermediateCode.Operand; source, base: IntermediateCode.Section;
  10970. procedure: SyntaxTree.Procedure;
  10971. baseRecord: SyntaxTree.RecordType; baseTD: SyntaxTree.TypeDeclaration; sym: SyntaxTree.Symbol;
  10972. numberPointers: LONGINT; padding,offset, i: LONGINT;
  10973. CONST MPO=-40000000H;
  10974. PROCEDURE TdTable(size: LONGINT);
  10975. VAR i: LONGINT;
  10976. BEGIN
  10977. Info(source, "tag table");
  10978. baseRecord := recordType;
  10979. i := 0;
  10980. WHILE baseRecord # NIL DO
  10981. INC(i);
  10982. baseRecord := baseRecord.GetBaseRecord();
  10983. END;
  10984. IF i > size THEN implementationVisitor.Error(x.position,"maximal extension level exceeded") END;
  10985. WHILE i < size DO
  10986. Address(source,0);
  10987. INC(i);
  10988. END;
  10989. baseRecord := recordType;
  10990. WHILE baseRecord # NIL DO
  10991. baseTD := baseRecord.typeDeclaration;
  10992. Global.GetSymbolSegmentedName(baseTD,name);
  10993. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  10994. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  10995. ELSE
  10996. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  10997. END;
  10998. offset := ToMemoryUnits(module.system,GetTypeRecordBaseOffset(baseRecord.recordScope.numberMethods)*module.system.addressSize);
  10999. Symbol(source, tir, 0, offset);
  11000. baseRecord := baseRecord.GetBaseRecord();
  11001. END;
  11002. END TdTable;
  11003. PROCEDURE MethodTable;
  11004. VAR i,methods: LONGINT;
  11005. BEGIN
  11006. Info(source, "method table");
  11007. IF recordType # NIL THEN
  11008. methods := recordType.recordScope.numberMethods;
  11009. FOR i := methods-1 TO 0 BY -1 DO
  11010. procedure := recordType.recordScope.FindMethod(i);
  11011. Global.GetSymbolSegmentedName(procedure, name);
  11012. NamedSymbol(source, name,procedure, 0,0);
  11013. END;
  11014. END;
  11015. END MethodTable;
  11016. PROCEDURE CooperativeMethodTable(pointer: BOOLEAN);
  11017. VAR baseRecord: SyntaxTree.RecordType; name, stackFrame: Basic.SegmentedName; i,start,methods: LONGINT;
  11018. BEGIN
  11019. Info(source, "method table");
  11020. baseRecord := recordType;
  11021. WHILE baseRecord.baseType # NIL DO
  11022. baseRecord := baseRecord.GetBaseRecord ();
  11023. END;
  11024. GetRecordTypeName (baseRecord, name);
  11025. Basic.ToSegmentedName ("BaseTypes.StackFrame", stackFrame);
  11026. IF name = stackFrame THEN
  11027. start := 0;
  11028. ELSIF ~HasExplicitTraceMethod(recordType) THEN
  11029. baseRecord := recordType;
  11030. WHILE (baseRecord # NIL) & ~baseRecord.hasPointers DO
  11031. baseRecord := baseRecord.GetBaseRecord ();
  11032. END;
  11033. IF baseRecord # NIL THEN
  11034. GetRecordTypeName (baseRecord, name);
  11035. IF pointer & ~baseRecord.isObject THEN
  11036. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11037. END;
  11038. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Trace"));
  11039. ELSIF recordType.isObject THEN
  11040. Basic.ToSegmentedName ("BaseTypes.Object.@Trace",name);
  11041. ELSIF pointer THEN
  11042. Basic.ToSegmentedName ("BaseTypes.Pointer.Trace",name);
  11043. ELSE
  11044. Basic.ToSegmentedName ("BaseTypes.Record.@Trace",name);
  11045. END;
  11046. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11047. Symbol(source, tir, 0, 0);
  11048. start := 0;
  11049. baseRecord := recordType;
  11050. WHILE (baseRecord # NIL) DO
  11051. IF HasExplicitTraceMethod(baseRecord) THEN start := 1 END;
  11052. baseRecord := baseRecord.GetBaseRecord ();
  11053. END;
  11054. ELSE
  11055. (* explicit trace method: *)
  11056. procedure := recordType.recordScope.FindMethod(0);
  11057. IF ~procedure.isFinalizer THEN
  11058. Global.GetSymbolSegmentedName(procedure, name);
  11059. NamedSymbol(source, name,procedure, 0,0);
  11060. END;
  11061. start := 1;
  11062. END;
  11063. IF (name # stackFrame) & recordType.isObject THEN
  11064. baseRecord := recordType;
  11065. WHILE (baseRecord # NIL) & (baseRecord.recordScope.finalizer = NIL) DO
  11066. baseRecord := baseRecord.GetBaseRecord ();
  11067. END;
  11068. IF (baseRecord = NIL) OR (baseRecord.recordScope.finalizer = NIL) THEN
  11069. Basic.ToSegmentedName ("BaseTypes.Object.Finalize",name);
  11070. ELSE
  11071. Global.GetSymbolSegmentedName(baseRecord.recordScope.finalizer, name);
  11072. END;
  11073. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11074. Symbol(source, tir, 0, 0);
  11075. END;
  11076. methods := recordType.recordScope.numberMethods;
  11077. FOR i := start TO methods-1 DO
  11078. procedure := recordType.recordScope.FindMethod(i);
  11079. IF ~procedure.isFinalizer THEN
  11080. Global.GetSymbolSegmentedName(procedure, name);
  11081. NamedSymbol(source, name,procedure, 0,0);
  11082. END;
  11083. END;
  11084. END CooperativeMethodTable;
  11085. BEGIN
  11086. Global.GetSymbolSegmentedName(td,name);
  11087. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,declarationVisitor.dump);
  11088. source.SetExported(IsExported(td));
  11089. IF (cellType # NIL) THEN recordType := cellType.GetBaseRecord() END;
  11090. IF implementationVisitor.backend.cooperative THEN
  11091. base := NIL;
  11092. baseRecord := recordType.GetBaseRecord();
  11093. IF baseRecord # NIL THEN
  11094. baseTD := baseRecord.typeDeclaration;
  11095. END;
  11096. IF ~recordType.isObject THEN
  11097. Info(source, "parent");
  11098. IF baseRecord # NIL THEN
  11099. Global.GetSymbolSegmentedName(baseTD,name);
  11100. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11101. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11102. ELSE
  11103. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,baseTD,declarationVisitor.dump);
  11104. END;
  11105. Symbol(source, tir, 0, 0);
  11106. ELSE
  11107. Address(source,0);
  11108. END;
  11109. Info(source, "record size");
  11110. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11111. source.SetReferenced(FALSE);
  11112. CooperativeMethodTable(FALSE);
  11113. base := source;
  11114. Global.GetSymbolSegmentedName(td,name);
  11115. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11116. source := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11117. source.SetExported(IsExported(td));
  11118. source.SetReferenced(FALSE);
  11119. END;
  11120. Info(source, "parent");
  11121. IF baseRecord # NIL THEN
  11122. Global.GetSymbolSegmentedName(baseTD,name);
  11123. sym := baseTD;
  11124. IF ~recordType.isObject THEN
  11125. Basic.SuffixSegmentedName (name, Basic.MakeString ("@Pointer"));
  11126. sym := NIL;
  11127. END;
  11128. IF (baseTD.scope = NIL) OR (baseTD.scope.ownerModule = module.module) THEN
  11129. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11130. ELSE
  11131. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,sym,declarationVisitor.dump);
  11132. END;
  11133. Symbol(source, tir, 0, 0);
  11134. ELSIF (recordType.pointerType # NIL) & recordType.pointerType.isPlain THEN
  11135. Address(source,0);
  11136. ELSE
  11137. IF recordType.isObject THEN
  11138. Basic.ToSegmentedName ("BaseTypes.Object",name);
  11139. ELSE
  11140. Basic.ToSegmentedName ("BaseTypes.Record",name);
  11141. END;
  11142. tir := IntermediateCode.NewSection(module.importedSections, Sections.ConstSection, name,NIL,declarationVisitor.dump);
  11143. Symbol(source, tir, 0, 0);
  11144. END;
  11145. Info(source, "base record descriptor");
  11146. Symbol(source, base, 0, 0);
  11147. CooperativeMethodTable(TRUE);
  11148. source.SetReferenced(FALSE);
  11149. IF recordType.hasPointers THEN
  11150. IF ~HasExplicitTraceMethod (recordType) THEN
  11151. implementationVisitor.CreateTraceMethod(recordType);
  11152. END;
  11153. implementationVisitor.CreateResetProcedure(recordType);
  11154. implementationVisitor.CreateAssignProcedure(recordType);
  11155. END;
  11156. ELSIF ~simple THEN
  11157. (*
  11158. MethodEnd = MPO
  11159. ---
  11160. methods (# methods)
  11161. ---
  11162. tags (16)
  11163. ---
  11164. TypeDesc = TypeInfoAdr
  11165. ---
  11166. td adr ---> rec size
  11167. ----
  11168. pointer offsets
  11169. ----
  11170. (padding)
  11171. -----
  11172. empty [2 addresses aligned]
  11173. empty
  11174. empty
  11175. numPtrs
  11176. ---
  11177. pointer offsets
  11178. ---
  11179. *)
  11180. Info(source, "MethodEnd = MPO");
  11181. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),MPO);
  11182. source(IntermediateCode.Section).Emit(Data(-1,op));
  11183. MethodTable;
  11184. TdTable(TypeTags);
  11185. Info(source, "type descriptor info pointer");
  11186. Symbol(source, NewTypeDescriptorInfo(source,source.pc+1,recordType.IsProtected()),0,0);
  11187. IF (cellType # NIL) THEN
  11188. IF cellType.sizeInBits < 0 THEN
  11189. ASSERT(module.system.GenerateVariableOffsets(cellType.cellScope));
  11190. END;
  11191. Info(source, "cell size");
  11192. Address(source, ToMemoryUnits(module.system,cellType.sizeInBits));
  11193. ELSE
  11194. Info(source, "record size");
  11195. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11196. END;
  11197. Info(source, "pointer offsets pointer");
  11198. padding := 1- source.pc MOD 2;
  11199. Symbol(source, source, source.pc+1+padding,0);
  11200. IF padding >0 THEN
  11201. Info(source, "padding");
  11202. FOR i := 1 TO padding DO Address(source,0) END;
  11203. END;
  11204. IF cellType # NIL THEN
  11205. PointerArray(source, cellType.cellScope, numberPointers);
  11206. ELSE
  11207. PointerArray(source, recordType.recordScope, numberPointers);
  11208. END;
  11209. ELSE
  11210. (*
  11211. simple:
  11212. methods
  11213. tag(2)
  11214. tag(1)
  11215. td adr ---> tag(0)
  11216. td adr ---> size
  11217. *)
  11218. MethodTable;
  11219. TdTable(TypeTags);
  11220. Info(source, "record size");
  11221. Address(source, ToMemoryUnits(module.system,module.system.SizeOf(recordType)));
  11222. source.SetReferenced(FALSE);
  11223. END;
  11224. END NewTypeDescriptor;
  11225. BEGIN
  11226. x := x.resolved;
  11227. IF (x IS SyntaxTree.PointerType) THEN
  11228. x := x(SyntaxTree.PointerType).pointerBase.resolved;
  11229. END;
  11230. IF (x IS SyntaxTree.RecordType) THEN (* enter: insert only if not already inserted *)
  11231. recordType := x(SyntaxTree.RecordType);
  11232. td := x.typeDeclaration;
  11233. IF td = NIL THEN td := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11234. ASSERT(td # NIL);
  11235. section := module.allSections.FindBySymbol(td); (* TODO *)
  11236. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11237. IF implementationVisitor.newObjectFile THEN
  11238. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11239. NewTypeDescriptor
  11240. END;
  11241. ELSE
  11242. (* data section in intermediate code *)
  11243. Global.GetSymbolSegmentedName(td,name);
  11244. tir := IntermediateCode.NewSection(module.allSections, Sections.ConstSection, name,td,implementationVisitor.dump # NIL);
  11245. IntermediateCode.InitImmediate(op,IntermediateCode.GetType(module.system, module.system.addressType),0);
  11246. tir.Emit(Data(-1,op));
  11247. END;
  11248. END;
  11249. ELSIF (x IS SyntaxTree.CellType) & implementationVisitor.backend.cellsAreObjects THEN
  11250. cellType := x(SyntaxTree.CellType);
  11251. td := x.typeDeclaration;
  11252. section := module.allSections.FindBySymbol(td); (* TODO *)
  11253. IF (section = NIL) OR (section(IntermediateCode.Section).pc = 0) THEN
  11254. IF implementationVisitor.newObjectFile THEN
  11255. IF (td.scope = NIL) OR (td.scope.ownerModule = module.module) THEN
  11256. NewTypeDescriptor
  11257. END;
  11258. END;
  11259. END;
  11260. END
  11261. END CheckTypeDeclaration
  11262. END MetaDataGenerator;
  11263. IntermediateBackend*= OBJECT (IntermediateCode.IntermediateBackend)
  11264. VAR
  11265. trace-: BOOLEAN;
  11266. traceString-: SyntaxTree.IdentifierString;
  11267. traceModuleName-: SyntaxTree.IdentifierString;
  11268. newObjectFile-: BOOLEAN;
  11269. profile-: BOOLEAN;
  11270. noRuntimeChecks: BOOLEAN;
  11271. simpleMetaData-: BOOLEAN;
  11272. noAsserts: BOOLEAN;
  11273. optimize-: BOOLEAN;
  11274. cooperative-: BOOLEAN;
  11275. preregisterStatic-: BOOLEAN;
  11276. dump-: Basic.Writer;
  11277. cellsAreObjects: BOOLEAN;
  11278. PROCEDURE &InitIntermediateBackend*;
  11279. BEGIN
  11280. simpleMetaData := FALSE;
  11281. newObjectFile := FALSE;
  11282. InitBackend;
  11283. SetRuntimeModuleName(DefaultRuntimeModuleName);
  11284. SetTraceModuleName(DefaultTraceModuleName);
  11285. END InitIntermediateBackend;
  11286. PROCEDURE GenerateIntermediate*(x: SyntaxTree.Module; supportedInstruction: SupportedInstructionProcedure; supportedImmediate: SupportedImmediateProcedure): Sections.Module;
  11287. VAR
  11288. declarationVisitor: DeclarationVisitor;
  11289. implementationVisitor: ImplementationVisitor;
  11290. module: Sections.Module;
  11291. name, instructionSet, platformName: SyntaxTree.IdentifierString;
  11292. meta: MetaDataGenerator;
  11293. BEGIN
  11294. ResetError;
  11295. Global.GetSymbolName(x,name);
  11296. IF activeCellsSpecification # NIL THEN
  11297. GetDescription(instructionSet);
  11298. activeCellsSpecification.SetInstructionSet(instructionSet)
  11299. END;
  11300. NEW(module,x,system); (* backend structures *)
  11301. Global.GetModuleName(x, name);
  11302. module.SetModuleName(name);
  11303. NEW(implementationVisitor,system,checker,supportedInstruction, supportedImmediate, Compiler.FindPC IN flags, runtimeModuleName, SELF, newObjectFile);
  11304. NEW(declarationVisitor,system,implementationVisitor,SELF,Compiler.ForceModuleBodies IN flags,trace & (Compiler.Info IN flags));
  11305. NEW(meta, implementationVisitor, declarationVisitor,simpleMetaData);
  11306. declarationVisitor.Module(x,module);
  11307. IF newObjectFile & ~meta.simple THEN
  11308. meta.Module(implementationVisitor.moduleBodySection);
  11309. END;
  11310. GetDescription(platformName);
  11311. module.SetPlatformName(platformName);
  11312. RETURN module
  11313. END GenerateIntermediate;
  11314. PROCEDURE SupportedImmediate*(CONST op: IntermediateCode.Operand): BOOLEAN;
  11315. BEGIN RETURN TRUE
  11316. END SupportedImmediate;
  11317. PROCEDURE ProcessSyntaxTreeModule(syntaxTreeModule: SyntaxTree.Module): Formats.GeneratedModule;
  11318. BEGIN RETURN ProcessIntermediateCodeModule(GenerateIntermediate(syntaxTreeModule, SupportedInstruction, SupportedImmediate))
  11319. END ProcessSyntaxTreeModule;
  11320. PROCEDURE ProcessIntermediateCodeModule(intermediateCodeModule: Formats.GeneratedModule): Formats.GeneratedModule;
  11321. VAR
  11322. result: Sections.Module;
  11323. traceName: Basic.MessageString;
  11324. BEGIN
  11325. ASSERT(intermediateCodeModule IS Sections.Module);
  11326. result := intermediateCodeModule(Sections.Module);
  11327. IF trace THEN
  11328. traceName := "intermediate code trace: ";
  11329. Strings.Append(traceName,traceString);
  11330. dump := Basic.GetWriter(Basic.GetDebugWriter(traceName));
  11331. IF (traceString="") OR (traceString="*") THEN
  11332. result.Dump(dump);
  11333. dump.Update
  11334. ELSE
  11335. Sections.DumpFiltered(dump, result, traceString);
  11336. END
  11337. END;
  11338. RETURN result
  11339. END ProcessIntermediateCodeModule;
  11340. PROCEDURE GetDescription*(VAR instructionSet: ARRAY OF CHAR);
  11341. BEGIN instructionSet := "Intermediate";
  11342. END GetDescription;
  11343. PROCEDURE SetNewObjectFile*(newObjectFile: BOOLEAN; simpleMetaData: BOOLEAN);
  11344. BEGIN
  11345. SELF.newObjectFile := newObjectFile;
  11346. SELF.simpleMetaData := simpleMetaData;
  11347. END SetNewObjectFile;
  11348. PROCEDURE SetTraceModuleName(CONST name: ARRAY OF CHAR);
  11349. BEGIN COPY(name, traceModuleName)
  11350. END SetTraceModuleName;
  11351. PROCEDURE DefineOptions(options: Options.Options);
  11352. BEGIN
  11353. DefineOptions^(options);
  11354. options.Add(0X,"trace",Options.String);
  11355. options.Add(0X,"runtime",Options.String);
  11356. options.Add(0X,"newObjectFile",Options.Flag);
  11357. options.Add(0X,"traceModule",Options.String);
  11358. options.Add(0X,"profile",Options.Flag);
  11359. options.Add(0X,"noRuntimeChecks",Options.Flag);
  11360. options.Add(0X,"noAsserts",Options.Flag);
  11361. options.Add(0X,"metaData",Options.String);
  11362. options.Add('o',"optimize", Options.Flag);
  11363. options.Add(0X,"preregisterStatic", Options.Flag);
  11364. options.Add(0X,"cellsAreObjects", Options.Flag);
  11365. END DefineOptions;
  11366. PROCEDURE GetOptions(options: Options.Options);
  11367. VAR name,string: SyntaxTree.IdentifierString;
  11368. BEGIN
  11369. GetOptions^(options);
  11370. trace := options.GetString("trace",traceString);
  11371. profile := options.GetFlag("profile");
  11372. noRuntimeChecks := options.GetFlag("noRuntimeChecks");
  11373. noAsserts := options.GetFlag("noAsserts");
  11374. cooperative := options.GetFlag("cooperative");
  11375. IF options.GetFlag("newObjectFile") THEN newObjectFile := TRUE;
  11376. IF cooperative THEN
  11377. SetRuntimeModuleName("CPU") END
  11378. END;
  11379. IF options.GetString("objectFile",string) & (string = "Minos") THEN
  11380. simpleMetaData := TRUE
  11381. END;
  11382. IF options.GetString("metaData",string) THEN
  11383. IF string = "simple" THEN simpleMetaData := TRUE
  11384. ELSIF string ="full" THEN simpleMetaData := FALSE
  11385. END;
  11386. END;
  11387. IF options.GetString("runtime",name) THEN SetRuntimeModuleName(name) END;
  11388. IF options.GetString("traceModule",name) THEN SetTraceModuleName(name) END;
  11389. optimize := options.GetFlag("optimize");
  11390. preregisterStatic := options.GetFlag("preregisterStatic");
  11391. cellsAreObjects := options.GetFlag("cellsAreObjects");
  11392. END GetOptions;
  11393. PROCEDURE DefaultSymbolFileFormat(): Formats.SymbolFileFormat;
  11394. BEGIN RETURN SymbolFileFormat.Get()
  11395. END DefaultSymbolFileFormat;
  11396. END IntermediateBackend;
  11397. (* ----------------------------------- register allocation ------------------------------------- *)
  11398. (* register mapping scheme
  11399. virtual register number --> register mapping = part(0) --> ticket <--> physical register
  11400. spill offset
  11401. part(n) --> ticket <--> physical register
  11402. spill offset
  11403. *)
  11404. VAR int8-, int16-, int32-, int64-, uint8-, uint16-, uint32-, uint64-, float32-, float64-: IntermediateCode.Type;
  11405. emptyOperand: IntermediateCode.Operand;
  11406. systemCalls: ARRAY NumberSystemCalls OF SyntaxTree.Symbol;
  11407. statCoopResetVariables: LONGINT;
  11408. statCoopModifyAssignments: LONGINT;
  11409. modifyAssignmentsPC : LONGINT;
  11410. statCoopNilCheck: LONGINT;
  11411. statCoopSwitch: LONGINT;
  11412. statCoopAssignProcedure: LONGINT;
  11413. statCoopTraceMethod: LONGINT;
  11414. statCoopResetProcedure: LONGINT;
  11415. statCoopTraceModule: LONGINT;
  11416. PROCEDURE ResetStatistics*;
  11417. BEGIN
  11418. statCoopResetVariables := 0;
  11419. statCoopModifyAssignments := 0;
  11420. statCoopNilCheck:= 0;
  11421. statCoopSwitch:= 0;
  11422. statCoopAssignProcedure:= 0;
  11423. statCoopTraceMethod:= 0;
  11424. statCoopResetProcedure:= 0;
  11425. statCoopTraceModule:= 0;
  11426. END ResetStatistics;
  11427. PROCEDURE Statistics*;
  11428. BEGIN
  11429. TRACE(statCoopResetVariables, statCoopModifyAssignments);
  11430. TRACE(statCoopNilCheck, statCoopSwitch);
  11431. TRACE(statCoopAssignProcedure,
  11432. statCoopTraceMethod,
  11433. statCoopResetProcedure,
  11434. statCoopTraceModule)
  11435. END Statistics;
  11436. PROCEDURE PassBySingleReference(parameter: SyntaxTree.Parameter): BOOLEAN;
  11437. BEGIN
  11438. IF parameter.kind = SyntaxTree.ValueParameter THEN RETURN FALSE
  11439. ELSIF parameter.kind = SyntaxTree.ConstParameter THEN
  11440. RETURN (parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType) & (parameter.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.CCallingConvention)
  11441. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  11442. 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)
  11443. END
  11444. END PassBySingleReference;
  11445. PROCEDURE PassInRegister(parameter: SyntaxTree.Parameter): BOOLEAN;
  11446. BEGIN
  11447. RETURN ~parameter.type.IsComposite() OR PassBySingleReference(parameter)
  11448. END PassInRegister;
  11449. PROCEDURE AddRegisterEntry(VAR queue: RegisterEntry; register: LONGINT; class: IntermediateCode.RegisterClass; type: IntermediateCode.Type);
  11450. VAR new: RegisterEntry;
  11451. BEGIN
  11452. NEW(new); new.register := register; new.registerClass := class; new.type := type; new.next := NIL; new.prev := NIL;
  11453. IF queue = NIL THEN
  11454. queue := new
  11455. ELSE
  11456. new.next := queue;
  11457. IF queue#NIL THEN queue.prev := new END;
  11458. queue := new
  11459. END;
  11460. END AddRegisterEntry;
  11461. PROCEDURE RemoveRegisterEntry(VAR queue: RegisterEntry; register: LONGINT): BOOLEAN;
  11462. VAR this: RegisterEntry;
  11463. BEGIN
  11464. this := queue;
  11465. WHILE (this # NIL) & (this.register # register) DO
  11466. this := this.next;
  11467. END;
  11468. IF this = NIL THEN
  11469. RETURN FALSE
  11470. END;
  11471. ASSERT(this # NIL);
  11472. IF this = queue THEN queue := queue.next END;
  11473. IF this.prev # NIL THEN this.prev.next := this.next END;
  11474. IF this.next # NIL THEN this.next.prev := this.prev END;
  11475. RETURN TRUE
  11476. END RemoveRegisterEntry;
  11477. PROCEDURE Assert(cond: BOOLEAN; CONST reason: ARRAY OF CHAR);
  11478. BEGIN ASSERT(cond);
  11479. END Assert;
  11480. PROCEDURE ReusableRegister(op: IntermediateCode.Operand): BOOLEAN;
  11481. BEGIN
  11482. RETURN (op.mode = IntermediateCode.ModeRegister) & (op.register > 0) & (op.offset = 0);
  11483. END ReusableRegister;
  11484. PROCEDURE EnsureBodyProcedure(moduleScope: SyntaxTree.ModuleScope);
  11485. VAR procedure: SyntaxTree.Procedure; procedureScope: SyntaxTree.ProcedureScope;
  11486. BEGIN
  11487. procedure := moduleScope.bodyProcedure;
  11488. IF procedure = NIL THEN (* artificially add body procedure if not existing. Really needed? *)
  11489. procedureScope := SyntaxTree.NewProcedureScope(moduleScope);
  11490. procedure := SyntaxTree.NewProcedure(-1,Global.ModuleBodyName, procedureScope);
  11491. procedure.SetScope(moduleScope);
  11492. procedure.SetType(SyntaxTree.NewProcedureType(-1,moduleScope));
  11493. procedure.SetAccess(SyntaxTree.Hidden);
  11494. moduleScope.SetBodyProcedure(procedure);
  11495. moduleScope.AddProcedure(procedure);
  11496. procedureScope.SetBody(SyntaxTree.NewBody(-1,procedureScope)); (* empty body *)
  11497. END;
  11498. END EnsureBodyProcedure;
  11499. PROCEDURE GetSymbol*(scope: SyntaxTree.ModuleScope; CONST moduleName, symbolName: ARRAY OF CHAR): SyntaxTree.Symbol;
  11500. VAR import: SyntaxTree.Import;
  11501. s: Basic.MessageString;
  11502. selfName: SyntaxTree.IdentifierString;
  11503. module: SyntaxTree.Module;
  11504. BEGIN
  11505. scope.ownerModule.GetName(selfName);
  11506. IF (moduleName = selfName) & (scope.ownerModule.context = Global.A2Name) THEN
  11507. module := scope.ownerModule
  11508. ELSE
  11509. import := scope.ImportByModuleName(SyntaxTree.NewIdentifier(moduleName),SyntaxTree.NewIdentifier("A2"));
  11510. IF import = NIL THEN
  11511. RETURN NIL
  11512. ELSIF import.module = NIL THEN
  11513. RETURN NIL
  11514. ELSE module := import.module
  11515. END;
  11516. END;
  11517. RETURN module.moduleScope.FindSymbol(SyntaxTree.NewIdentifier(symbolName));
  11518. END GetSymbol;
  11519. PROCEDURE InitOperand(VAR op: Operand; mode: SHORTINT);
  11520. BEGIN
  11521. op.mode := mode;
  11522. IntermediateCode.InitOperand(op.op);
  11523. IntermediateCode.InitOperand(op.tag);
  11524. IntermediateCode.InitOperand(op.extra);
  11525. op.dimOffset := 0;
  11526. END InitOperand;
  11527. (* TODO: remove this, and redirect calls to 'IntermediateCode.GetType' directly *)
  11528. PROCEDURE GetType*(system: Global.System; type: SyntaxTree.Type): IntermediateCode.Type;
  11529. BEGIN RETURN IntermediateCode.GetType(system, type)
  11530. END GetType;
  11531. PROCEDURE BuildConstant(module: SyntaxTree.Module; value: SyntaxTree.Value; VAR adr: LONGINT): SyntaxTree.Constant;
  11532. VAR name: SyntaxTree.IdentifierString; constant: SyntaxTree.Constant;
  11533. BEGIN
  11534. name := "@const"; Basic.AppendNumber(name, adr); INC(adr);
  11535. (*
  11536. UniqueId(name,module,name,adr);
  11537. *)
  11538. constant := SyntaxTree.NewConstant(-1,SyntaxTree.NewIdentifier(name));
  11539. constant.SetValue(value);
  11540. module.moduleScope.AddConstant(constant);
  11541. constant.SetScope(module.moduleScope);
  11542. RETURN constant
  11543. END BuildConstant;
  11544. PROCEDURE HasPointers (scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11545. VAR variable: SyntaxTree.Variable; parameter: SyntaxTree.Parameter;
  11546. BEGIN
  11547. variable := scope.firstVariable;
  11548. WHILE variable # NIL DO
  11549. IF variable.NeedsTrace() THEN
  11550. RETURN TRUE;
  11551. END;
  11552. variable := variable.nextVariable;
  11553. END;
  11554. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11555. WHILE parameter # NIL DO
  11556. IF parameter.NeedsTrace() & ~IsVariableParameter(parameter) THEN
  11557. RETURN TRUE;
  11558. END;
  11559. parameter := parameter.nextParameter;
  11560. END;
  11561. RETURN FALSE;
  11562. END HasPointers;
  11563. PROCEDURE IsVariableParameter (parameter: SyntaxTree.Parameter): BOOLEAN;
  11564. BEGIN RETURN (parameter.kind = SyntaxTree.VarParameter) OR (parameter.kind = SyntaxTree.ConstParameter) & ((parameter.type.resolved IS SyntaxTree.RecordType) OR (parameter.type.resolved IS SyntaxTree.ArrayType));
  11565. END IsVariableParameter;
  11566. PROCEDURE HasVariableParameters(scope: SyntaxTree.ProcedureScope): BOOLEAN;
  11567. VAR parameter: SyntaxTree.Parameter;
  11568. BEGIN
  11569. parameter := scope.ownerProcedure.type(SyntaxTree.ProcedureType).firstParameter;
  11570. WHILE parameter # NIL DO
  11571. IF IsVariableParameter (parameter) THEN RETURN TRUE END;
  11572. IF parameter.movable THEN RETURN TRUE END;
  11573. parameter := parameter.nextParameter;
  11574. END;
  11575. RETURN scope.ownerProcedure.type(SyntaxTree.ProcedureType).returnParameter # NIL;
  11576. END HasVariableParameters;
  11577. PROCEDURE HasExplicitTraceMethod(recordType: SyntaxTree.RecordType): BOOLEAN;
  11578. BEGIN
  11579. IF (recordType.pointerType # NIL) & ~recordType.pointerType.isPlain THEN RETURN FALSE END;
  11580. RETURN (recordType.recordScope.firstProcedure # NIL) & Basic.StringEqual (Basic.MakeString ("Trace"), recordType.recordScope.firstProcedure.name);
  11581. END HasExplicitTraceMethod;
  11582. PROCEDURE IsIntegerConstant(expression: SyntaxTree.Expression; VAR val: HUGEINT): BOOLEAN;
  11583. BEGIN
  11584. IF expression.resolved # NIL THEN expression := expression.resolved END;
  11585. IF (expression IS SyntaxTree.IntegerValue) THEN
  11586. val := expression(SyntaxTree.IntegerValue).value;
  11587. RETURN TRUE
  11588. ELSE
  11589. RETURN FALSE
  11590. END;
  11591. END IsIntegerConstant;
  11592. PROCEDURE PowerOf2(val: HUGEINT; VAR exp: LONGINT): BOOLEAN;
  11593. BEGIN
  11594. IF val <= 0 THEN RETURN FALSE END;
  11595. exp := 0;
  11596. WHILE ~ODD(val) DO
  11597. val := val DIV 2;
  11598. INC(exp)
  11599. END;
  11600. RETURN val = 1
  11601. END PowerOf2;
  11602. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  11603. VAR procedure: SyntaxTree.Procedure;
  11604. BEGIN
  11605. procedure := record.recordScope.constructor;
  11606. IF procedure = NIL THEN
  11607. record := record.GetBaseRecord();
  11608. IF record # NIL THEN
  11609. procedure := GetConstructor(record)
  11610. END;
  11611. END;
  11612. RETURN procedure;
  11613. END GetConstructor;
  11614. PROCEDURE IsIntegerImmediate(CONST op: IntermediateCode.Operand; VAR value: LONGINT): BOOLEAN;
  11615. BEGIN
  11616. value := SHORT(op.intValue);
  11617. RETURN op.mode = IntermediateCode.ModeImmediate;
  11618. END IsIntegerImmediate;
  11619. (** whether a type strictily is a pointer to record or object type
  11620. (however, the basic type <<OBJECT>> is explicitly excluded) **)
  11621. PROCEDURE IsStrictlyPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  11622. BEGIN
  11623. IF type = NIL THEN
  11624. RETURN FALSE
  11625. ELSIF type.resolved IS SyntaxTree.PointerType THEN
  11626. RETURN type.resolved(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType
  11627. ELSE
  11628. RETURN FALSE
  11629. END
  11630. END IsStrictlyPointerToRecord;
  11631. PROCEDURE IsUnsafePointer(type: SyntaxTree.Type): BOOLEAN;
  11632. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & type.resolved(SyntaxTree.PointerType).isUnsafe
  11633. END IsUnsafePointer;
  11634. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  11635. BEGIN type := type.resolved;
  11636. IF type IS SyntaxTree.PointerType THEN
  11637. type := type(SyntaxTree.PointerType).pointerBase;
  11638. type := type.resolved;
  11639. IF type IS SyntaxTree.RecordType THEN
  11640. recordType := type(SyntaxTree.RecordType);
  11641. RETURN TRUE
  11642. ELSE
  11643. RETURN FALSE
  11644. END
  11645. ELSIF type IS SyntaxTree.RecordType THEN
  11646. recordType := type(SyntaxTree.RecordType);
  11647. RETURN type(SyntaxTree.RecordType).pointerType # NIL
  11648. ELSIF type IS SyntaxTree.ObjectType THEN
  11649. RETURN TRUE
  11650. ELSIF type IS SyntaxTree.AnyType THEN
  11651. RETURN TRUE (*! potentially is a pointer to record, treat it this way?? *)
  11652. ELSE
  11653. RETURN FALSE
  11654. END;
  11655. END IsPointerToRecord;
  11656. PROCEDURE IsArrayOfSystemByte(type: SyntaxTree.Type): BOOLEAN;
  11657. BEGIN
  11658. type := type.resolved;
  11659. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open)
  11660. & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType);
  11661. END IsArrayOfSystemByte;
  11662. PROCEDURE IsOpenArray(type: SyntaxTree.Type): BOOLEAN;
  11663. BEGIN
  11664. IF type = NIL THEN RETURN FALSE END;
  11665. type := type.resolved;
  11666. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open);
  11667. END IsOpenArray;
  11668. PROCEDURE IsStaticArray(type: SyntaxTree.Type): BOOLEAN;
  11669. BEGIN
  11670. IF type = NIL THEN RETURN FALSE END;
  11671. type := type.resolved;
  11672. RETURN (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static);
  11673. END IsStaticArray;
  11674. PROCEDURE StaticArrayNumElements(type: SyntaxTree.Type): LONGINT;
  11675. VAR size: LONGINT;
  11676. BEGIN
  11677. size := 1;
  11678. WHILE (IsStaticArray(type)) DO
  11679. size := size * type.resolved(SyntaxTree.ArrayType).staticLength;
  11680. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11681. END;
  11682. RETURN size;
  11683. END StaticArrayNumElements;
  11684. PROCEDURE StaticArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11685. BEGIN
  11686. WHILE (IsStaticArray(type)) DO
  11687. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11688. END;
  11689. RETURN type;
  11690. END StaticArrayBaseType;
  11691. PROCEDURE ArrayBaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  11692. BEGIN
  11693. WHILE (type.resolved IS SyntaxTree.ArrayType) DO
  11694. type := type.resolved(SyntaxTree.ArrayType).arrayBase;
  11695. END;
  11696. RETURN type;
  11697. END ArrayBaseType;
  11698. PROCEDURE IsDelegate(type: SyntaxTree.Type): BOOLEAN;
  11699. BEGIN
  11700. IF type = NIL THEN RETURN FALSE END;
  11701. type := type.resolved;
  11702. RETURN (type IS SyntaxTree.ProcedureType) & (type(SyntaxTree.ProcedureType).isDelegate)
  11703. END IsDelegate;
  11704. PROCEDURE DynamicDim(type:SyntaxTree.Type): LONGINT;
  11705. VAR i: LONGINT;
  11706. BEGIN
  11707. i := 0; type := type.resolved;
  11708. WHILE(type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11709. INC(i);
  11710. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  11711. END;
  11712. WHILE(type # NIL) & (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11713. INC(i);
  11714. type := type(SyntaxTree.MathArrayType).arrayBase;
  11715. IF type # NIL THEN type := type.resolved END;
  11716. END;
  11717. RETURN i
  11718. END DynamicDim;
  11719. PROCEDURE StaticSize(system: Global.System; type: SyntaxTree.Type): LONGINT;
  11720. BEGIN
  11721. WHILE (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form # SyntaxTree.Static) DO
  11722. type := type(SyntaxTree.ArrayType).arrayBase;
  11723. END;
  11724. WHILE (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form # SyntaxTree.Static) DO
  11725. type := type(SyntaxTree.MathArrayType).arrayBase;
  11726. END;
  11727. RETURN ToMemoryUnits(system,system.AlignedSizeOf(type));
  11728. END StaticSize;
  11729. PROCEDURE IsImmediate(x: IntermediateCode.Operand): BOOLEAN;
  11730. BEGIN
  11731. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name = "");
  11732. END IsImmediate;
  11733. PROCEDURE IsAddress(x: IntermediateCode.Operand): BOOLEAN;
  11734. BEGIN
  11735. RETURN (x.mode = IntermediateCode.ModeImmediate) & (x.symbol.name # "")
  11736. END IsAddress;
  11737. PROCEDURE IsRegister(x: IntermediateCode.Operand): BOOLEAN;
  11738. BEGIN
  11739. RETURN (x.mode = IntermediateCode.ModeRegister);
  11740. END IsRegister;
  11741. PROCEDURE GetRecordTypeName(recordType: SyntaxTree.RecordType; VAR name: Basic.SegmentedName);
  11742. VAR typeDeclaration: SyntaxTree.TypeDeclaration;
  11743. BEGIN
  11744. typeDeclaration := recordType.typeDeclaration;
  11745. IF typeDeclaration = NIL THEN typeDeclaration := recordType.pointerType.resolved.typeDeclaration END; (* for compatibility with paco *)
  11746. Global.GetSymbolSegmentedName(typeDeclaration,name);
  11747. END GetRecordTypeName;
  11748. PROCEDURE ParametersSize(system: Global.System; procedureType: SyntaxTree.ProcedureType; isNested: BOOLEAN): LONGINT;
  11749. VAR parSize: LONGINT; parameter: SyntaxTree.Parameter;
  11750. BEGIN
  11751. parSize := 0;
  11752. IF StructuredReturnType(procedureType) THEN
  11753. parameter := procedureType.returnParameter;
  11754. INC(parSize,system.SizeOfParameter(parameter));
  11755. parSize := parSize + (-parSize) MOD system.addressSize;
  11756. END;
  11757. parameter :=procedureType.lastParameter;
  11758. WHILE (parameter # NIL) DO
  11759. INC(parSize,system.SizeOfParameter(parameter));
  11760. parSize := parSize + (-parSize) MOD system.addressSize;
  11761. parameter := parameter.prevParameter;
  11762. END;
  11763. IF procedureType.isDelegate THEN INC(parSize,system.addressSize) END; (* method => self pointer *)
  11764. IF isNested THEN INC(parSize,system.addressSize) END; (* nested procedure => static base *)
  11765. RETURN ToMemoryUnits(system,parSize)
  11766. END ParametersSize;
  11767. PROCEDURE ReturnedAsParameter(type: SyntaxTree.Type): BOOLEAN;
  11768. BEGIN
  11769. IF type = NIL THEN RETURN FALSE
  11770. ELSE
  11771. type := type.resolved;
  11772. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR SemanticChecker.IsPointerType(type)
  11773. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  11774. END
  11775. END ReturnedAsParameter;
  11776. PROCEDURE StructuredReturnType(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  11777. BEGIN
  11778. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  11779. END StructuredReturnType;
  11780. PROCEDURE IsNested(procedure: SyntaxTree.Procedure): BOOLEAN;
  11781. BEGIN
  11782. RETURN (procedure.scope IS SyntaxTree.ProcedureScope) & (procedure.externalName = NIL);
  11783. END IsNested;
  11784. PROCEDURE InCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  11785. BEGIN
  11786. WHILE (scope # NIL) & ~(scope IS SyntaxTree.CellScope) DO
  11787. scope := scope.outerScope;
  11788. END;
  11789. RETURN scope # NIL;
  11790. END InCellScope;
  11791. PROCEDURE ProcedureParametersSize*(system: Global.System; procedure: SyntaxTree.Procedure): LONGINT;
  11792. BEGIN
  11793. (*IF (procedure.scope IS SyntaxTree.CellScope) & (procedure = procedure.scope(SyntaxTree.CellScope).constructor) & ~backend.cellsAreObjects THEN
  11794. RETURN 0
  11795. ELSE
  11796. *)
  11797. RETURN ParametersSize(system,procedure.type(SyntaxTree.ProcedureType),IsNested(procedure));
  11798. (*END;*)
  11799. END ProcedureParametersSize;
  11800. PROCEDURE ToMemoryUnits*(system: Global.System; size: LONGINT): LONGINT;
  11801. VAR dataUnit: LONGINT;
  11802. BEGIN dataUnit := system.dataUnit;
  11803. ASSERT(size MOD system.dataUnit = 0);
  11804. RETURN size DIV system.dataUnit
  11805. END ToMemoryUnits;
  11806. PROCEDURE Get*(): Backend.Backend;
  11807. VAR backend: IntermediateBackend;
  11808. BEGIN NEW(backend); RETURN backend
  11809. END Get;
  11810. PROCEDURE Nop(position: LONGINT):IntermediateCode.Instruction;
  11811. VAR instruction: IntermediateCode.Instruction;
  11812. BEGIN
  11813. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.nop,emptyOperand,emptyOperand,emptyOperand);
  11814. RETURN instruction
  11815. END Nop;
  11816. PROCEDURE Mov(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11817. VAR instruction: IntermediateCode.Instruction;
  11818. BEGIN
  11819. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,emptyOperand);
  11820. RETURN instruction
  11821. END Mov;
  11822. (* like Mov but ensures that no new register will be allocated for dest *)
  11823. PROCEDURE MovReplace(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11824. VAR instruction: IntermediateCode.Instruction;
  11825. BEGIN
  11826. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.mov,dest,src,dest);
  11827. RETURN instruction
  11828. END MovReplace;
  11829. PROCEDURE Conv(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11830. VAR instruction: IntermediateCode.Instruction;
  11831. BEGIN
  11832. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.conv,dest,src,emptyOperand);
  11833. RETURN instruction
  11834. END Conv;
  11835. PROCEDURE Call*(position: LONGINT;op: IntermediateCode.Operand; parSize: LONGINT): IntermediateCode.Instruction;
  11836. VAR instruction: IntermediateCode.Instruction;
  11837. BEGIN
  11838. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.call,op,IntermediateCode.Number(parSize),emptyOperand);
  11839. RETURN instruction
  11840. END Call;
  11841. PROCEDURE Exit(position: LONGINT;pcOffset: LONGINT; callingConvention: LONGINT): IntermediateCode.Instruction;
  11842. VAR op1, op2: IntermediateCode.Operand;
  11843. VAR instruction: IntermediateCode.Instruction;
  11844. BEGIN
  11845. IntermediateCode.InitNumber(op1,pcOffset);
  11846. IntermediateCode.InitNumber(op2,callingConvention);
  11847. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.exit,op1,op2,emptyOperand);
  11848. RETURN instruction
  11849. END Exit;
  11850. PROCEDURE Return(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11851. VAR instruction: IntermediateCode.Instruction;
  11852. BEGIN
  11853. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.return,res,emptyOperand,emptyOperand);
  11854. RETURN instruction
  11855. END Return;
  11856. PROCEDURE Result*(position: LONGINT;res: IntermediateCode.Operand): IntermediateCode.Instruction;
  11857. VAR instruction: IntermediateCode.Instruction;
  11858. BEGIN
  11859. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.result,res,emptyOperand,emptyOperand);
  11860. RETURN instruction
  11861. END Result;
  11862. PROCEDURE Trap(position: LONGINT;nr: LONGINT): IntermediateCode.Instruction;
  11863. VAR op1: IntermediateCode.Operand;
  11864. VAR instruction: IntermediateCode.Instruction;
  11865. BEGIN
  11866. IntermediateCode.InitNumber(op1,nr);
  11867. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.trap,op1,emptyOperand,emptyOperand);
  11868. RETURN instruction
  11869. END Trap;
  11870. PROCEDURE Br(position: LONGINT;dest: IntermediateCode.Operand): IntermediateCode.Instruction;
  11871. VAR instruction: IntermediateCode.Instruction;
  11872. BEGIN
  11873. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.br,dest,emptyOperand,emptyOperand);
  11874. RETURN instruction
  11875. END Br;
  11876. PROCEDURE Breq(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11877. VAR instruction: IntermediateCode.Instruction;
  11878. BEGIN
  11879. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.breq,dest,left,right);
  11880. RETURN instruction
  11881. END Breq;
  11882. PROCEDURE Brne(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11883. VAR instruction: IntermediateCode.Instruction;
  11884. BEGIN
  11885. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brne,dest,left,right);
  11886. RETURN instruction
  11887. END Brne;
  11888. PROCEDURE Brge(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11889. VAR instruction: IntermediateCode.Instruction;
  11890. BEGIN
  11891. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brge,dest,left,right);
  11892. RETURN instruction
  11893. END Brge;
  11894. PROCEDURE Brlt(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11895. VAR instruction: IntermediateCode.Instruction;
  11896. BEGIN
  11897. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.brlt,dest,left,right);
  11898. RETURN instruction
  11899. END Brlt;
  11900. PROCEDURE Pop*(position: LONGINT;op:IntermediateCode.Operand): IntermediateCode.Instruction;
  11901. VAR instruction: IntermediateCode.Instruction;
  11902. BEGIN
  11903. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.pop,op,emptyOperand,emptyOperand);
  11904. RETURN instruction
  11905. END Pop;
  11906. PROCEDURE Push*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  11907. VAR instruction: IntermediateCode.Instruction;
  11908. BEGIN
  11909. ASSERT(op.mode # IntermediateCode.Undefined);
  11910. IntermediateCode.InitInstruction(instruction, position, IntermediateCode.push,op,emptyOperand,emptyOperand);
  11911. RETURN instruction
  11912. END Push;
  11913. PROCEDURE Neg(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11914. VAR instruction: IntermediateCode.Instruction;
  11915. BEGIN
  11916. IntermediateCode.InitInstruction(instruction,position, IntermediateCode.neg,dest,src,emptyOperand);
  11917. RETURN instruction
  11918. END Neg;
  11919. PROCEDURE Not(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11920. VAR instruction: IntermediateCode.Instruction;
  11921. BEGIN
  11922. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.not,dest,src,emptyOperand);
  11923. RETURN instruction
  11924. END Not;
  11925. PROCEDURE Abs(position: LONGINT;dest,src: IntermediateCode.Operand): IntermediateCode.Instruction;
  11926. VAR instruction: IntermediateCode.Instruction;
  11927. BEGIN
  11928. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.abs,dest,src,emptyOperand);
  11929. RETURN instruction
  11930. END Abs;
  11931. PROCEDURE Mul(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11932. VAR instruction: IntermediateCode.Instruction;
  11933. BEGIN
  11934. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mul,dest,left,right);
  11935. RETURN instruction
  11936. END Mul;
  11937. PROCEDURE Div(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11938. VAR instruction: IntermediateCode.Instruction;
  11939. BEGIN
  11940. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.div,dest,left,right);
  11941. RETURN instruction
  11942. END Div;
  11943. PROCEDURE Mod(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11944. VAR instruction: IntermediateCode.Instruction;
  11945. BEGIN
  11946. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.mod,dest,left,right);
  11947. RETURN instruction
  11948. END Mod;
  11949. PROCEDURE Sub(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11950. VAR instruction: IntermediateCode.Instruction;
  11951. BEGIN
  11952. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.sub,dest,left,right);
  11953. RETURN instruction
  11954. END Sub;
  11955. PROCEDURE Add(position: LONGINT;dest: IntermediateCode.Operand; left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11956. VAR instruction: IntermediateCode.Instruction;
  11957. BEGIN
  11958. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.add,dest,left,right);
  11959. RETURN instruction
  11960. END Add;
  11961. PROCEDURE And(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11962. VAR instruction: IntermediateCode.Instruction;
  11963. BEGIN
  11964. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.and,dest,left,right);
  11965. RETURN instruction
  11966. END And;
  11967. PROCEDURE Or(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11968. VAR instruction: IntermediateCode.Instruction;
  11969. BEGIN
  11970. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.or,dest,left,right);
  11971. RETURN instruction
  11972. END Or;
  11973. PROCEDURE Xor(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11974. VAR instruction: IntermediateCode.Instruction;
  11975. BEGIN
  11976. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.xor,dest,left,right);
  11977. RETURN instruction
  11978. END Xor;
  11979. PROCEDURE Shl(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11980. VAR instruction: IntermediateCode.Instruction;
  11981. BEGIN
  11982. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shl,dest,left, IntermediateCode.ToUnsigned(right));
  11983. RETURN instruction
  11984. END Shl;
  11985. PROCEDURE Shr(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11986. VAR instruction: IntermediateCode.Instruction;
  11987. BEGIN
  11988. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.shr,dest,left, IntermediateCode.ToUnsigned(right));
  11989. RETURN instruction
  11990. END Shr;
  11991. PROCEDURE Rol(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11992. VAR instruction: IntermediateCode.Instruction;
  11993. BEGIN
  11994. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.rol,dest,left, IntermediateCode.ToUnsigned(right));
  11995. RETURN instruction
  11996. END Rol;
  11997. PROCEDURE Ror(position: LONGINT;dest,left,right: IntermediateCode.Operand): IntermediateCode.Instruction;
  11998. VAR instruction: IntermediateCode.Instruction;
  11999. BEGIN
  12000. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.ror,dest,left, IntermediateCode.ToUnsigned(right));
  12001. RETURN instruction
  12002. END Ror;
  12003. PROCEDURE Cas(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12004. VAR instruction: IntermediateCode.Instruction;
  12005. BEGIN
  12006. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.cas,dest,src,size);
  12007. RETURN instruction
  12008. END Cas;
  12009. PROCEDURE Copy(position: LONGINT;dest,src,size: IntermediateCode.Operand): IntermediateCode.Instruction;
  12010. VAR instruction: IntermediateCode.Instruction;
  12011. BEGIN
  12012. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.copy,dest,src,size);
  12013. RETURN instruction
  12014. END Copy;
  12015. PROCEDURE Fill(position: LONGINT;dest,size, value: IntermediateCode.Operand): IntermediateCode.Instruction;
  12016. VAR instruction: IntermediateCode.Instruction;
  12017. BEGIN
  12018. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.fill,dest,size,value);
  12019. RETURN instruction
  12020. END Fill;
  12021. PROCEDURE Asm(position: LONGINT;s: SyntaxTree.SourceCode; inRules, outRules: IntermediateCode.Rules): IntermediateCode.Instruction;
  12022. VAR instruction: IntermediateCode.Instruction; string, o1, o2: IntermediateCode.Operand;
  12023. BEGIN
  12024. string := IntermediateCode.String(s);
  12025. IntermediateCode.SetIntValue(string,position); (* for error reporting *)
  12026. IF inRules # NIL THEN IntermediateCode.InitRule(o1, inRules) ELSE o1 := emptyOperand END;
  12027. IF outRules # NIL THEN IntermediateCode.InitRule(o2, outRules) ELSE o2 := emptyOperand END;
  12028. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.asm,string,o1,o2);
  12029. RETURN instruction
  12030. END Asm;
  12031. PROCEDURE Data*(position: LONGINT;op: IntermediateCode.Operand): IntermediateCode.Instruction;
  12032. VAR instruction: IntermediateCode.Instruction;
  12033. BEGIN
  12034. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.data,op,emptyOperand,emptyOperand);
  12035. RETURN instruction
  12036. END Data;
  12037. PROCEDURE SpecialInstruction(position: LONGINT;subtype: SHORTINT; op1,op2,op3: IntermediateCode.Operand): IntermediateCode.Instruction;
  12038. VAR instruction: IntermediateCode.Instruction;
  12039. BEGIN
  12040. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.special,op1,op2,op3);
  12041. IntermediateCode.SetSubType(instruction, subtype);
  12042. RETURN instruction
  12043. END SpecialInstruction;
  12044. PROCEDURE Reserve(position: LONGINT;units: LONGINT): IntermediateCode.Instruction;
  12045. VAR op1: IntermediateCode.Operand;
  12046. VAR instruction: IntermediateCode.Instruction;
  12047. BEGIN
  12048. (*! generate a warning if size exceeds a certain limit *)
  12049. (*
  12050. ASSERT(bytes < 1000000); (* sanity check *)
  12051. *)
  12052. ASSERT(0 <= units); (* sanity check *)
  12053. IntermediateCode.InitNumber(op1,units);
  12054. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.reserve,op1,emptyOperand,emptyOperand);
  12055. RETURN instruction
  12056. END Reserve;
  12057. PROCEDURE LabelInstruction(position: LONGINT): IntermediateCode.Instruction;
  12058. VAR op1: IntermediateCode.Operand;
  12059. VAR instruction: IntermediateCode.Instruction;
  12060. BEGIN
  12061. IntermediateCode.InitNumber(op1,position);
  12062. IntermediateCode.InitInstruction(instruction,position,IntermediateCode.label,op1,emptyOperand,emptyOperand);
  12063. RETURN instruction
  12064. END LabelInstruction;
  12065. PROCEDURE EnterImmediate*(data: IntermediateCode.Section; CONST vop: IntermediateCode.Operand): LONGINT;
  12066. VAR pc: LONGINT;
  12067. PROCEDURE ProvidesValue(CONST instr: IntermediateCode.Instruction; op: IntermediateCode.Operand): BOOLEAN;
  12068. BEGIN
  12069. IF instr.opcode # IntermediateCode.data THEN RETURN FALSE END;
  12070. ASSERT(instr.op1.mode = IntermediateCode.ModeImmediate);
  12071. IF instr.op1.type.sizeInBits # op.type.sizeInBits THEN RETURN FALSE END;
  12072. IF instr.op1.type.form # op.type.form THEN RETURN FALSE END;
  12073. IF instr.op1.type.form = IntermediateCode.Float THEN
  12074. RETURN instr.op1.floatValue = op.floatValue
  12075. ELSE
  12076. RETURN instr.op1.intValue = op.intValue
  12077. END;
  12078. END ProvidesValue;
  12079. BEGIN
  12080. ASSERT(vop.mode = IntermediateCode.ModeImmediate);
  12081. pc := 0;
  12082. WHILE (pc<data.pc) & ~ProvidesValue(data.instructions[pc],vop) DO
  12083. INC(pc);
  12084. END;
  12085. IF pc = data.pc THEN
  12086. data.Emit(Data(-1,vop));
  12087. END;
  12088. RETURN pc
  12089. END EnterImmediate;
  12090. PROCEDURE Init;
  12091. VAR i: LONGINT; name: SyntaxTree.IdentifierString;
  12092. BEGIN
  12093. int8 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits8);
  12094. int16 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits16);
  12095. int32 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits32);
  12096. int64 := IntermediateCode.NewType(IntermediateCode.SignedInteger,IntermediateCode.Bits64);
  12097. uint8 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits8);
  12098. uint16 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits16);
  12099. uint32 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits32);
  12100. uint64 := IntermediateCode.NewType(IntermediateCode.UnsignedInteger,IntermediateCode.Bits64);
  12101. float32 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits32);
  12102. float64 := IntermediateCode.NewType(IntermediateCode.Float,IntermediateCode.Bits64);
  12103. IntermediateCode.InitOperand(emptyOperand);
  12104. FOR i := 0 TO NumberSystemCalls-1 DO
  12105. name := "@SystemCall";
  12106. Basic.AppendNumber(name,i);
  12107. systemCalls[i] := SyntaxTree.NewSymbol(SyntaxTree.NewIdentifier(name));
  12108. END;
  12109. END Init;
  12110. PROCEDURE IsExported(symbol: SyntaxTree.Symbol): BOOLEAN;
  12111. BEGIN
  12112. RETURN (symbol # NIL) & (symbol.access * SyntaxTree.Public # {})
  12113. END IsExported;
  12114. BEGIN
  12115. Init;
  12116. END FoxIntermediateBackend.
  12117. Compiler.Compile FoxIntermediateBackend.Mod ~